Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bf8f2e2

Browse filesBrowse files
tniessendanielleadams
authored andcommitted
src: refactor DH groups to delete crypto_groups.h
Rewrite FindDiffieHellmanGroup() using OpenSSL helper functions to obtain the required constants directly, instead of loading them from our own crypto_groups.h and converting them to BIGNUMs. This also removes the need for the struct modp_group, so we can delete crypto_groups.h altogether. PR-URL: #43896 Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent 8a2a6e1 commit bf8f2e2
Copy full SHA for bf8f2e2

File tree

Expand file treeCollapse file tree

4 files changed

+39
-432
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+39
-432
lines changed
Open diff view settings
Collapse file

‎src/crypto/crypto_dh.cc‎

Copy file name to clipboardExpand all lines: src/crypto/crypto_dh.cc
+37-15Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "crypto/crypto_dh.h"
22
#include "async_wrap-inl.h"
33
#include "base_object-inl.h"
4-
#include "crypto/crypto_groups.h"
54
#include "crypto/crypto_keys.h"
65
#include "env-inl.h"
76
#include "memory_tracker-inl.h"
@@ -138,6 +137,15 @@ void DiffieHellman::MemoryInfo(MemoryTracker* tracker) const {
138137
tracker->TrackFieldWithSize("dh", dh_ ? kSizeOf_DH : 0);
139138
}
140139

140+
bool DiffieHellman::Init(BignumPointer&& bn_p, int g) {
141+
dh_.reset(DH_new());
142+
CHECK_GE(g, 2);
143+
BignumPointer bn_g(BN_new());
144+
return bn_g && BN_set_word(bn_g.get(), g) &&
145+
DH_set0_pqg(dh_.get(), bn_p.release(), nullptr, bn_g.release()) &&
146+
VerifyContext();
147+
}
148+
141149
bool DiffieHellman::Init(const char* p, int p_len, int g) {
142150
dh_.reset(DH_new());
143151
if (p_len <= 0) {
@@ -192,11 +200,29 @@ bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len) {
192200
return VerifyContext();
193201
}
194202

195-
inline const modp_group* FindDiffieHellmanGroup(const char* name) {
196-
for (const modp_group& group : modp_groups) {
197-
if (StringEqualNoCase(name, group.name))
198-
return &group;
199-
}
203+
constexpr int kStandardizedGenerator = 2;
204+
205+
template <BIGNUM* (*p)(BIGNUM*)>
206+
BignumPointer InstantiateStandardizedGroup() {
207+
return BignumPointer(p(nullptr));
208+
}
209+
210+
typedef BignumPointer (*StandardizedGroupInstantiator)();
211+
212+
// Returns a function that can be used to create an instance of a standardized
213+
// Diffie-Hellman group. The generator is always kStandardizedGenerator.
214+
inline StandardizedGroupInstantiator FindDiffieHellmanGroup(const char* name) {
215+
#define V(n, p) \
216+
if (StringEqualNoCase(name, n)) return InstantiateStandardizedGroup<p>
217+
V("modp1", BN_get_rfc2409_prime_768);
218+
V("modp2", BN_get_rfc2409_prime_1024);
219+
V("modp5", BN_get_rfc3526_prime_1536);
220+
V("modp14", BN_get_rfc3526_prime_2048);
221+
V("modp15", BN_get_rfc3526_prime_3072);
222+
V("modp16", BN_get_rfc3526_prime_4096);
223+
V("modp17", BN_get_rfc3526_prime_6144);
224+
V("modp18", BN_get_rfc3526_prime_8192);
225+
#undef V
200226
return nullptr;
201227
}
202228

@@ -211,13 +237,11 @@ void DiffieHellman::DiffieHellmanGroup(
211237
bool initialized = false;
212238

213239
const node::Utf8Value group_name(env->isolate(), args[0]);
214-
const modp_group* group = FindDiffieHellmanGroup(*group_name);
240+
auto group = FindDiffieHellmanGroup(*group_name);
215241
if (group == nullptr)
216242
return THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env);
217243

218-
initialized = diffieHellman->Init(group->prime,
219-
group->prime_size,
220-
group->gen);
244+
initialized = diffieHellman->Init(group(), kStandardizedGenerator);
221245
if (!initialized)
222246
THROW_ERR_CRYPTO_INITIALIZATION_FAILED(env);
223247
}
@@ -480,16 +504,14 @@ Maybe<bool> DhKeyGenTraits::AdditionalConfig(
480504

481505
if (args[*offset]->IsString()) {
482506
Utf8Value group_name(env->isolate(), args[*offset]);
483-
const modp_group* group = FindDiffieHellmanGroup(*group_name);
507+
auto group = FindDiffieHellmanGroup(*group_name);
484508
if (group == nullptr) {
485509
THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env);
486510
return Nothing<bool>();
487511
}
488512

489-
params->params.prime = BignumPointer(
490-
BN_bin2bn(reinterpret_cast<const unsigned char*>(group->prime),
491-
group->prime_size, nullptr));
492-
params->params.generator = group->gen;
513+
params->params.prime = group();
514+
params->params.generator = kStandardizedGenerator;
493515
*offset += 1;
494516
} else {
495517
if (args[*offset]->IsInt32()) {
Collapse file

‎src/crypto/crypto_dh.h‎

Copy file name to clipboardExpand all lines: src/crypto/crypto_dh.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class DiffieHellman : public BaseObject {
2020
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
2121

2222
bool Init(int primeLength, int g);
23+
bool Init(BignumPointer&& bn_p, int g);
2324
bool Init(const char* p, int p_len, int g);
2425
bool Init(const char* p, int p_len, const char* g, int g_len);
2526

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.