Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: refactor DH groups to delete crypto_groups.h #43896

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "crypto/crypto_dh.h"
#include "async_wrap-inl.h"
#include "base_object-inl.h"
#include "crypto/crypto_groups.h"
#include "crypto/crypto_keys.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
Expand Down Expand Up @@ -138,6 +137,15 @@ void DiffieHellman::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackFieldWithSize("dh", dh_ ? kSizeOf_DH : 0);
}

bool DiffieHellman::Init(BignumPointer&& bn_p, int g) {
dh_.reset(DH_new());
CHECK_GE(g, 2);
BignumPointer bn_g(BN_new());
return bn_g && BN_set_word(bn_g.get(), g) &&
DH_set0_pqg(dh_.get(), bn_p.release(), nullptr, bn_g.release()) &&
VerifyContext();
}

bool DiffieHellman::Init(const char* p, int p_len, int g) {
dh_.reset(DH_new());
if (p_len <= 0) {
Expand Down Expand Up @@ -192,11 +200,29 @@ bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len) {
return VerifyContext();
}

inline const modp_group* FindDiffieHellmanGroup(const char* name) {
for (const modp_group& group : modp_groups) {
if (StringEqualNoCase(name, group.name))
return &group;
}
constexpr int kStandardizedGenerator = 2;

template <BIGNUM* (*p)(BIGNUM*)>
BignumPointer InstantiateStandardizedGroup() {
return BignumPointer(p(nullptr));
}

typedef BignumPointer (*StandardizedGroupInstantiator)();

// Returns a function that can be used to create an instance of a standardized
// Diffie-Hellman group. The generator is always kStandardizedGenerator.
inline StandardizedGroupInstantiator FindDiffieHellmanGroup(const char* name) {
#define V(n, p) \
if (StringEqualNoCase(name, n)) return InstantiateStandardizedGroup<p>
V("modp1", BN_get_rfc2409_prime_768);
V("modp2", BN_get_rfc2409_prime_1024);
V("modp5", BN_get_rfc3526_prime_1536);
V("modp14", BN_get_rfc3526_prime_2048);
V("modp15", BN_get_rfc3526_prime_3072);
V("modp16", BN_get_rfc3526_prime_4096);
V("modp17", BN_get_rfc3526_prime_6144);
V("modp18", BN_get_rfc3526_prime_8192);
#undef V
return nullptr;
}

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

const node::Utf8Value group_name(env->isolate(), args[0]);
const modp_group* group = FindDiffieHellmanGroup(*group_name);
auto group = FindDiffieHellmanGroup(*group_name);
if (group == nullptr)
return THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env);

initialized = diffieHellman->Init(group->prime,
group->prime_size,
group->gen);
initialized = diffieHellman->Init(group(), kStandardizedGenerator);
if (!initialized)
THROW_ERR_CRYPTO_INITIALIZATION_FAILED(env);
}
Expand Down Expand Up @@ -480,16 +504,14 @@ Maybe<bool> DhKeyGenTraits::AdditionalConfig(

if (args[*offset]->IsString()) {
Utf8Value group_name(env->isolate(), args[*offset]);
const modp_group* group = FindDiffieHellmanGroup(*group_name);
auto group = FindDiffieHellmanGroup(*group_name);
if (group == nullptr) {
THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env);
return Nothing<bool>();
}

params->params.prime = BignumPointer(
BN_bin2bn(reinterpret_cast<const unsigned char*>(group->prime),
group->prime_size, nullptr));
params->params.generator = group->gen;
params->params.prime = group();
params->params.generator = kStandardizedGenerator;
*offset += 1;
} else {
if (args[*offset]->IsInt32()) {
Expand Down
1 change: 1 addition & 0 deletions src/crypto/crypto_dh.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class DiffieHellman : public BaseObject {
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

bool Init(int primeLength, int g);
bool Init(BignumPointer&& bn_p, int g);
bool Init(const char* p, int p_len, int g);
bool Init(const char* p, int p_len, const char* g, int g_len);

Expand Down
Loading