summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_x931p.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bn/bn_x931p.c')
-rw-r--r--src/lib/libcrypto/bn/bn_x931p.c68
1 files changed, 32 insertions, 36 deletions
diff --git a/src/lib/libcrypto/bn/bn_x931p.c b/src/lib/libcrypto/bn/bn_x931p.c
index 04c5c874ec..025f1be51a 100644
--- a/src/lib/libcrypto/bn/bn_x931p.c
+++ b/src/lib/libcrypto/bn/bn_x931p.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -66,16 +66,16 @@
66 * integers. 66 * integers.
67 */ 67 */
68 68
69static int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx, 69static int
70 BN_GENCB *cb) 70bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx, BN_GENCB *cb)
71 { 71{
72 int i = 0; 72 int i = 0;
73
73 if (!BN_copy(pi, Xpi)) 74 if (!BN_copy(pi, Xpi))
74 return 0; 75 return 0;
75 if (!BN_is_odd(pi) && !BN_add_word(pi, 1)) 76 if (!BN_is_odd(pi) && !BN_add_word(pi, 1))
76 return 0; 77 return 0;
77 for(;;) 78 for (;;) {
78 {
79 i++; 79 i++;
80 BN_GENCB_call(cb, 0, i); 80 BN_GENCB_call(cb, 0, i);
81 /* NB 27 MR is specificed in X9.31 */ 81 /* NB 27 MR is specificed in X9.31 */
@@ -83,20 +83,21 @@ static int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx,
83 break; 83 break;
84 if (!BN_add_word(pi, 2)) 84 if (!BN_add_word(pi, 2))
85 return 0; 85 return 0;
86 } 86 }
87 BN_GENCB_call(cb, 2, i); 87 BN_GENCB_call(cb, 2, i);
88 return 1; 88 return 1;
89 } 89}
90 90
91/* This is the main X9.31 prime derivation function. From parameters 91/* This is the main X9.31 prime derivation function. From parameters
92 * Xp1, Xp2 and Xp derive the prime p. If the parameters p1 or p2 are 92 * Xp1, Xp2 and Xp derive the prime p. If the parameters p1 or p2 are
93 * not NULL they will be returned too: this is needed for testing. 93 * not NULL they will be returned too: this is needed for testing.
94 */ 94 */
95 95
96int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, 96int
97 const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2, 97BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, const BIGNUM *Xp,
98 const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb) 98 const BIGNUM *Xp1, const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx,
99 { 99 BN_GENCB *cb)
100{
100 int ret = 0; 101 int ret = 0;
101 102
102 BIGNUM *t, *p1p2, *pm1; 103 BIGNUM *t, *p1p2, *pm1;
@@ -157,8 +158,7 @@ int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
157 158
158 /* p now equals Yp0 */ 159 /* p now equals Yp0 */
159 160
160 for (;;) 161 for (;;) {
161 {
162 int i = 1; 162 int i = 1;
163 BN_GENCB_call(cb, 0, i++); 163 BN_GENCB_call(cb, 0, i++);
164 if (!BN_copy(pm1, p)) 164 if (!BN_copy(pm1, p))
@@ -169,34 +169,36 @@ int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
169 goto err; 169 goto err;
170 if (BN_is_one(t) 170 if (BN_is_one(t)
171 /* X9.31 specifies 8 MR and 1 Lucas test or any prime test 171 /* X9.31 specifies 8 MR and 1 Lucas test or any prime test
172 * offering similar or better guarantees 50 MR is considerably 172 * offering similar or better guarantees 50 MR is considerably
173 * better. 173 * better.
174 */ 174 */
175 && BN_is_prime_fasttest_ex(p, 50, ctx, 1, cb)) 175 && BN_is_prime_fasttest_ex(p, 50, ctx, 1, cb))
176 break; 176 break;
177 if (!BN_add(p, p, p1p2)) 177 if (!BN_add(p, p, p1p2))
178 goto err; 178 goto err;
179 } 179 }
180 180
181 BN_GENCB_call(cb, 3, 0); 181 BN_GENCB_call(cb, 3, 0);
182 182
183 ret = 1; 183 ret = 1;
184 184
185 err: 185err:
186 186
187 BN_CTX_end(ctx); 187 BN_CTX_end(ctx);
188 188
189 return ret; 189 return ret;
190 } 190}
191 191
192/* Generate pair of paramters Xp, Xq for X9.31 prime generation. 192/* Generate pair of paramters Xp, Xq for X9.31 prime generation.
193 * Note: nbits paramter is sum of number of bits in both. 193 * Note: nbits paramter is sum of number of bits in both.
194 */ 194 */
195 195
196int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx) 196int
197 { 197BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx)
198{
198 BIGNUM *t; 199 BIGNUM *t;
199 int i; 200 int i;
201
200 /* Number of bits for each prime is of the form 202 /* Number of bits for each prime is of the form
201 * 512+128s for s = 0, 1, ... 203 * 512+128s for s = 0, 1, ...
202 */ 204 */
@@ -213,15 +215,14 @@ int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx)
213 BN_CTX_start(ctx); 215 BN_CTX_start(ctx);
214 t = BN_CTX_get(ctx); 216 t = BN_CTX_get(ctx);
215 217
216 for (i = 0; i < 1000; i++) 218 for (i = 0; i < 1000; i++) {
217 {
218 if (!BN_rand(Xq, nbits, 1, 0)) 219 if (!BN_rand(Xq, nbits, 1, 0))
219 return 0; 220 return 0;
220 /* Check that |Xp - Xq| > 2^(nbits - 100) */ 221 /* Check that |Xp - Xq| > 2^(nbits - 100) */
221 BN_sub(t, Xp, Xq); 222 BN_sub(t, Xp, Xq);
222 if (BN_num_bits(t) > (nbits - 100)) 223 if (BN_num_bits(t) > (nbits - 100))
223 break; 224 break;
224 } 225 }
225 226
226 BN_CTX_end(ctx); 227 BN_CTX_end(ctx);
227 228
@@ -229,8 +230,7 @@ int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx)
229 return 1; 230 return 1;
230 231
231 return 0; 232 return 0;
232 233}
233 }
234 234
235/* Generate primes using X9.31 algorithm. Of the values p, p1, p2, Xp1 235/* Generate primes using X9.31 algorithm. Of the values p, p1, p2, Xp1
236 * and Xp2 only 'p' needs to be non-NULL. If any of the others are not NULL 236 * and Xp2 only 'p' needs to be non-NULL. If any of the others are not NULL
@@ -240,12 +240,10 @@ int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx)
240 * are generated using the previous function and supplied as input. 240 * are generated using the previous function and supplied as input.
241 */ 241 */
242 242
243int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, 243int
244 BIGNUM *Xp1, BIGNUM *Xp2, 244BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, BIGNUM *Xp1,
245 const BIGNUM *Xp, 245 BIGNUM *Xp2, const BIGNUM *Xp, const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb)
246 const BIGNUM *e, BN_CTX *ctx, 246{
247 BN_GENCB *cb)
248 {
249 int ret = 0; 247 int ret = 0;
250 248
251 BN_CTX_start(ctx); 249 BN_CTX_start(ctx);
@@ -263,10 +261,8 @@ int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
263 261
264 ret = 1; 262 ret = 1;
265 263
266 error: 264error:
267 BN_CTX_end(ctx); 265 BN_CTX_end(ctx);
268 266
269 return ret; 267 return ret;
270 268}
271 }
272