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+
141149bool 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 ()) {
0 commit comments