/*----------------implemented the Generate DH key parameter------------*///dh_generate_parameters_ex#include <stdio.h> #include " Cryptlib.h "#include <openssl/bn.h> #include <openssl/dh.h> #ifdef openssl_fips# include <openssl/ fips.h> #endifstatic int dh_builtin_genparams (DH *ret, int prime_len, int generator, Bn_g ENCB *CB); int dh_generate_parameters_ex (DH *ret, int prime_len, int generator,//generate DH parameter, save to DH struct BN_GENCB *CB) {#ifdef openssl_fips//fips Federal Information Processing Standard is a set of standards that describe file processing, cryptographic algorithms, and other information technology standards. if (Fips_mode () &&! ( Ret->meth->flags & Dh_flag_fips_method) &&! (Ret->flags & Dh_flag_non_fips_allow)) {Dherr (DH_F_DH_GENERATE_PARAMETERS_EX, Dh_r_non_fips_method); return 0; } #endif if (ret->meth->generate_params)//generate_params is the callback function that generates the DH key parameter return Ret->meth->gene Rate_params (ret, Prime_len, generator, CB);//Generate DH Parameters #ifdef openssl_fips if (Fips_mode ())Return FIPS_DH_GENERATE_PARAMETERS_EX (ret, Prime_len, generator, CB); #endif return Dh_builtin_genparams (ret, prime_ Len, generator, CB);} /*-* We generate DH parameters as follows * Find a prime Q which is PRIME_LEN/2 bits long. * p= (2*Q) +1 or (p-1)/2 = Q * For this case, and G is a generator if * g^ ((p-1)/q) mod P! = 1 for values of Q which are the FAC Tors of P-1. * Since The factors of p-1 is Q and 2, we just need to check * g^2 mod P! = 1 and g^q mod p! = 1. * have said all, * There is another special case method for the generators 2, 3 and 5. * for 2, p mod = = one * for 3, p mod = = 5 <<<<< does not work for safe primes. * for 5, p mod = = 3 or 7 * * Thanks to Phil Karn <[email protected]> for the pointers about the * special GE Nerators and for answering some of my questions. * * I ' ve implemented the second simple method:-). * Since DH should be using a safe prime (both P and Q is prime), * This generator function can take a very very long Time to run. *//* * Actually there is the no reason to insist the ' generator ' be a generator. * It ' s just as OK (and in some sense better) to use a generator of the * order-q subgroup. */static int Dh_builtin_genparams (DH *ret, int prime_len, int generator, BN_GENCB *CB) { Bignum *t1, *t2; int g, OK =-1; Bn_ctx *ctx = NULL; CTX = Bn_ctx_new (); Request a new context structure if (CTX = = NULL) goto err; Bn_ctx_start (CTX); T1 = Bn_ctx_get (CTX); Create a new bignum structure t2 = bn_ctx_get (CTX); if (T1 = = NULL | | t2 = = NULL) goto err; /* Make sure ' ret ' have the necessary elements * * if (!ret->p && ((ret->p = Bn_new ()) = = NULL)//reborn into a bign UM structure goto err; if (!ret->g && (ret->g = bn_new () = = NULL)) goto err; if (generator <= 1) {//g>1 dherr (dh_f_dh_builtin_genparams, dh_r_bad_generator); Goto err; } if (generator = = dh_generator_2) {//Set the value of the bignum structure to Unsigned long int type integer value if (!bn_set_word (t1)) goto err; if (!bn_set_word (T2, one)) goto err; g = 2; } #if 0/* Does not work for safe primes */else if (generator = = dh_generator_3) {if (!bn_set_word (t1,)) goto err; if (!bn_set_word (T2, 5)) Goto err; g = 3; } #endif else if (generator = = dh_generator_5) {//g=dh_generator_5 if (!bn_set_word (t1)) goto ER R if (!bn_set_word (T2, 3)) goto err; /* * Bn_set_word (t3,7); Just has to miss out on these ones */g = 5; } else {/* * in the general case, and don ' t worry if ' generator ' is a generator or * not:since we are Using safe primes, it'll generate either an * Order-q or a order-2q group, which both is OK */ if (!bn_set_word (T1, 2)) goto err; if (!bn_set_word (T2, 1)) Goto ERR g = generator; } if (!BN_GENERATE_PRIME_EX (Ret->p, Prime_len, 1, T1, T2, CB)) goto err; if (!bn_gencb_call (CB, 3, 0)) goto err; if (!bn_set_word (Ret->g, G))//Set value goto err; OK = 1; Err:if (ok = =-1) {Dherr (dh_f_dh_builtin_genparams, err_r_bn_lib); OK = 0; } if (ctx! = NULL) {bn_ctx_end (CTX); Bn_ctx_free (CTX); Release structure} return OK;}
OpenSSL open source program DH algorithm parsing DH_GEN.C