diff options
Diffstat (limited to 'src/lib/libcrypto/dh/dh_gen.c')
| -rw-r--r-- | src/lib/libcrypto/dh/dh_gen.c | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/src/lib/libcrypto/dh/dh_gen.c b/src/lib/libcrypto/dh/dh_gen.c index 23777f5a16..cfd5b11868 100644 --- a/src/lib/libcrypto/dh/dh_gen.c +++ b/src/lib/libcrypto/dh/dh_gen.c | |||
| @@ -56,11 +56,25 @@ | |||
| 56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
| 57 | */ | 57 | */ |
| 58 | 58 | ||
| 59 | /* NB: These functions have been upgraded - the previous prototypes are in | ||
| 60 | * dh_depr.c as wrappers to these ones. | ||
| 61 | * - Geoff | ||
| 62 | */ | ||
| 63 | |||
| 59 | #include <stdio.h> | 64 | #include <stdio.h> |
| 60 | #include "cryptlib.h" | 65 | #include "cryptlib.h" |
| 61 | #include <openssl/bn.h> | 66 | #include <openssl/bn.h> |
| 62 | #include <openssl/dh.h> | 67 | #include <openssl/dh.h> |
| 63 | 68 | ||
| 69 | static int dh_builtin_genparams(DH *ret, int prime_len, int generator, BN_GENCB *cb); | ||
| 70 | |||
| 71 | int DH_generate_parameters_ex(DH *ret, int prime_len, int generator, BN_GENCB *cb) | ||
| 72 | { | ||
| 73 | if(ret->meth->generate_params) | ||
| 74 | return ret->meth->generate_params(ret, prime_len, generator, cb); | ||
| 75 | return dh_builtin_genparams(ret, prime_len, generator, cb); | ||
| 76 | } | ||
| 77 | |||
| 64 | /* We generate DH parameters as follows | 78 | /* We generate DH parameters as follows |
| 65 | * find a prime q which is prime_len/2 bits long. | 79 | * find a prime q which is prime_len/2 bits long. |
| 66 | * p=(2*q)+1 or (p-1)/2 = q | 80 | * p=(2*q)+1 or (p-1)/2 = q |
| @@ -86,29 +100,26 @@ | |||
| 86 | * It's just as OK (and in some sense better) to use a generator of the | 100 | * It's just as OK (and in some sense better) to use a generator of the |
| 87 | * order-q subgroup. | 101 | * order-q subgroup. |
| 88 | */ | 102 | */ |
| 89 | 103 | static int dh_builtin_genparams(DH *ret, int prime_len, int generator, BN_GENCB *cb) | |
| 90 | #ifndef OPENSSL_FIPS | ||
| 91 | |||
| 92 | DH *DH_generate_parameters(int prime_len, int generator, | ||
| 93 | void (*callback)(int,int,void *), void *cb_arg) | ||
| 94 | { | 104 | { |
| 95 | BIGNUM *p=NULL,*t1,*t2; | 105 | BIGNUM *t1,*t2; |
| 96 | DH *ret=NULL; | ||
| 97 | int g,ok= -1; | 106 | int g,ok= -1; |
| 98 | BN_CTX *ctx=NULL; | 107 | BN_CTX *ctx=NULL; |
| 99 | 108 | ||
| 100 | ret=DH_new(); | ||
| 101 | if (ret == NULL) goto err; | ||
| 102 | ctx=BN_CTX_new(); | 109 | ctx=BN_CTX_new(); |
| 103 | if (ctx == NULL) goto err; | 110 | if (ctx == NULL) goto err; |
| 104 | BN_CTX_start(ctx); | 111 | BN_CTX_start(ctx); |
| 105 | t1 = BN_CTX_get(ctx); | 112 | t1 = BN_CTX_get(ctx); |
| 106 | t2 = BN_CTX_get(ctx); | 113 | t2 = BN_CTX_get(ctx); |
| 107 | if (t1 == NULL || t2 == NULL) goto err; | 114 | if (t1 == NULL || t2 == NULL) goto err; |
| 115 | |||
| 116 | /* Make sure 'ret' has the necessary elements */ | ||
| 117 | if(!ret->p && ((ret->p = BN_new()) == NULL)) goto err; | ||
| 118 | if(!ret->g && ((ret->g = BN_new()) == NULL)) goto err; | ||
| 108 | 119 | ||
| 109 | if (generator <= 1) | 120 | if (generator <= 1) |
| 110 | { | 121 | { |
| 111 | DHerr(DH_F_DH_GENERATE_PARAMETERS, DH_R_BAD_GENERATOR); | 122 | DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR); |
| 112 | goto err; | 123 | goto err; |
| 113 | } | 124 | } |
| 114 | if (generator == DH_GENERATOR_2) | 125 | if (generator == DH_GENERATOR_2) |
| @@ -144,18 +155,14 @@ DH *DH_generate_parameters(int prime_len, int generator, | |||
| 144 | g=generator; | 155 | g=generator; |
| 145 | } | 156 | } |
| 146 | 157 | ||
| 147 | p=BN_generate_prime(NULL,prime_len,1,t1,t2,callback,cb_arg); | 158 | if(!BN_generate_prime_ex(ret->p,prime_len,1,t1,t2,cb)) goto err; |
| 148 | if (p == NULL) goto err; | 159 | if(!BN_GENCB_call(cb, 3, 0)) goto err; |
| 149 | if (callback != NULL) callback(3,0,cb_arg); | ||
| 150 | ret->p=p; | ||
| 151 | ret->g=BN_new(); | ||
| 152 | if (ret->g == NULL) goto err; | ||
| 153 | if (!BN_set_word(ret->g,g)) goto err; | 160 | if (!BN_set_word(ret->g,g)) goto err; |
| 154 | ok=1; | 161 | ok=1; |
| 155 | err: | 162 | err: |
| 156 | if (ok == -1) | 163 | if (ok == -1) |
| 157 | { | 164 | { |
| 158 | DHerr(DH_F_DH_GENERATE_PARAMETERS,ERR_R_BN_LIB); | 165 | DHerr(DH_F_DH_BUILTIN_GENPARAMS,ERR_R_BN_LIB); |
| 159 | ok=0; | 166 | ok=0; |
| 160 | } | 167 | } |
| 161 | 168 | ||
| @@ -164,12 +171,5 @@ err: | |||
| 164 | BN_CTX_end(ctx); | 171 | BN_CTX_end(ctx); |
| 165 | BN_CTX_free(ctx); | 172 | BN_CTX_free(ctx); |
| 166 | } | 173 | } |
| 167 | if (!ok && (ret != NULL)) | 174 | return ok; |
| 168 | { | ||
| 169 | DH_free(ret); | ||
| 170 | ret=NULL; | ||
| 171 | } | ||
| 172 | return(ret); | ||
| 173 | } | 175 | } |
| 174 | |||
| 175 | #endif | ||
