xref: /openssl/crypto/bn/bn_x931p.c (revision 5cbd2ea3)
1 /*
2  * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #define OPENSSL_SUPPRESS_DEPRECATED
11 
12 #include <stdio.h>
13 #include <openssl/bn.h>
14 #include "bn_local.h"
15 
16 /* X9.31 routines for prime derivation */
17 
18 /*
19  * X9.31 prime derivation. This is used to generate the primes pi (p1, p2,
20  * q1, q2) from a parameter Xpi by checking successive odd integers.
21  */
22 
bn_x931_derive_pi(BIGNUM * pi,const BIGNUM * Xpi,BN_CTX * ctx,BN_GENCB * cb)23 static int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx,
24                              BN_GENCB *cb)
25 {
26     int i = 0, is_prime;
27     if (!BN_copy(pi, Xpi))
28         return 0;
29     if (!BN_is_odd(pi) && !BN_add_word(pi, 1))
30         return 0;
31     for (;;) {
32         i++;
33         BN_GENCB_call(cb, 0, i);
34         /* NB 27 MR is specified in X9.31 */
35         is_prime = BN_check_prime(pi, ctx, cb);
36         if (is_prime < 0)
37             return 0;
38         if (is_prime)
39             break;
40         if (!BN_add_word(pi, 2))
41             return 0;
42     }
43     BN_GENCB_call(cb, 2, i);
44     return 1;
45 }
46 
47 /*
48  * This is the main X9.31 prime derivation function. From parameters Xp1, Xp2
49  * and Xp derive the prime p. If the parameters p1 or p2 are not NULL they
50  * will be returned too: this is needed for testing.
51  */
52 
BN_X931_derive_prime_ex(BIGNUM * p,BIGNUM * p1,BIGNUM * p2,const BIGNUM * Xp,const BIGNUM * Xp1,const BIGNUM * Xp2,const BIGNUM * e,BN_CTX * ctx,BN_GENCB * cb)53 int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
54                             const BIGNUM *Xp, const BIGNUM *Xp1,
55                             const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx,
56                             BN_GENCB *cb)
57 {
58     int ret = 0;
59 
60     BIGNUM *t, *p1p2, *pm1;
61 
62     /* Only even e supported */
63     if (!BN_is_odd(e))
64         return 0;
65 
66     BN_CTX_start(ctx);
67     if (p1 == NULL)
68         p1 = BN_CTX_get(ctx);
69 
70     if (p2 == NULL)
71         p2 = BN_CTX_get(ctx);
72 
73     t = BN_CTX_get(ctx);
74 
75     p1p2 = BN_CTX_get(ctx);
76 
77     pm1 = BN_CTX_get(ctx);
78 
79     if (pm1 == NULL)
80         goto err;
81 
82     if (!bn_x931_derive_pi(p1, Xp1, ctx, cb))
83         goto err;
84 
85     if (!bn_x931_derive_pi(p2, Xp2, ctx, cb))
86         goto err;
87 
88     if (!BN_mul(p1p2, p1, p2, ctx))
89         goto err;
90 
91     /* First set p to value of Rp */
92 
93     if (!BN_mod_inverse(p, p2, p1, ctx))
94         goto err;
95 
96     if (!BN_mul(p, p, p2, ctx))
97         goto err;
98 
99     if (!BN_mod_inverse(t, p1, p2, ctx))
100         goto err;
101 
102     if (!BN_mul(t, t, p1, ctx))
103         goto err;
104 
105     if (!BN_sub(p, p, t))
106         goto err;
107 
108     if (p->neg && !BN_add(p, p, p1p2))
109         goto err;
110 
111     /* p now equals Rp */
112 
113     if (!BN_mod_sub(p, p, Xp, p1p2, ctx))
114         goto err;
115 
116     if (!BN_add(p, p, Xp))
117         goto err;
118 
119     /* p now equals Yp0 */
120 
121     for (;;) {
122         int i = 1;
123         BN_GENCB_call(cb, 0, i++);
124         if (!BN_copy(pm1, p))
125             goto err;
126         if (!BN_sub_word(pm1, 1))
127             goto err;
128         if (!BN_gcd(t, pm1, e, ctx))
129             goto err;
130         if (BN_is_one(t)) {
131             /*
132              * X9.31 specifies 8 MR and 1 Lucas test or any prime test
133              * offering similar or better guarantees 50 MR is considerably
134              * better.
135              */
136             int r = BN_check_prime(p, ctx, cb);
137             if (r < 0)
138                 goto err;
139             if (r)
140                 break;
141         }
142         if (!BN_add(p, p, p1p2))
143             goto err;
144     }
145 
146     BN_GENCB_call(cb, 3, 0);
147 
148     ret = 1;
149 
150  err:
151 
152     BN_CTX_end(ctx);
153 
154     return ret;
155 }
156 
157 /*
158  * Generate pair of parameters Xp, Xq for X9.31 prime generation. Note: nbits
159  * parameter is sum of number of bits in both.
160  */
161 
BN_X931_generate_Xpq(BIGNUM * Xp,BIGNUM * Xq,int nbits,BN_CTX * ctx)162 int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx)
163 {
164     BIGNUM *t;
165     int i;
166     /*
167      * Number of bits for each prime is of the form 512+128s for s = 0, 1,
168      * ...
169      */
170     if ((nbits < 1024) || (nbits & 0xff))
171         return 0;
172     nbits >>= 1;
173     /*
174      * The random value Xp must be between sqrt(2) * 2^(nbits-1) and 2^nbits
175      * - 1. By setting the top two bits we ensure that the lower bound is
176      * exceeded.
177      */
178     if (!BN_priv_rand_ex(Xp, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY, 0,
179                          ctx))
180         return 0;
181 
182     BN_CTX_start(ctx);
183     t = BN_CTX_get(ctx);
184     if (t == NULL)
185         goto err;
186 
187     for (i = 0; i < 1000; i++) {
188         if (!BN_priv_rand_ex(Xq, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY, 0,
189                              ctx))
190             goto err;
191 
192         /* Check that |Xp - Xq| > 2^(nbits - 100) */
193         if (!BN_sub(t, Xp, Xq))
194             goto err;
195         if (BN_num_bits(t) > (nbits - 100))
196             break;
197     }
198 
199     BN_CTX_end(ctx);
200 
201     if (i < 1000)
202         return 1;
203 
204     return 0;
205 
206  err:
207     BN_CTX_end(ctx);
208     return 0;
209 }
210 
211 /*
212  * Generate primes using X9.31 algorithm. Of the values p, p1, p2, Xp1 and
213  * Xp2 only 'p' needs to be non-NULL. If any of the others are not NULL the
214  * relevant parameter will be stored in it. Due to the fact that |Xp - Xq| >
215  * 2^(nbits - 100) must be satisfied Xp and Xq are generated using the
216  * previous function and supplied as input.
217  */
218 
BN_X931_generate_prime_ex(BIGNUM * p,BIGNUM * p1,BIGNUM * p2,BIGNUM * Xp1,BIGNUM * Xp2,const BIGNUM * Xp,const BIGNUM * e,BN_CTX * ctx,BN_GENCB * cb)219 int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
220                               BIGNUM *Xp1, BIGNUM *Xp2,
221                               const BIGNUM *Xp,
222                               const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb)
223 {
224     int ret = 0;
225 
226     BN_CTX_start(ctx);
227     if (Xp1 == NULL)
228         Xp1 = BN_CTX_get(ctx);
229     if (Xp2 == NULL)
230         Xp2 = BN_CTX_get(ctx);
231     if (Xp1 == NULL || Xp2 == NULL)
232         goto error;
233 
234     if (!BN_priv_rand_ex(Xp1, 101, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY, 0, ctx))
235         goto error;
236     if (!BN_priv_rand_ex(Xp2, 101, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY, 0, ctx))
237         goto error;
238     if (!BN_X931_derive_prime_ex(p, p1, p2, Xp, Xp1, Xp2, e, ctx, cb))
239         goto error;
240 
241     ret = 1;
242 
243  error:
244     BN_CTX_end(ctx);
245 
246     return ret;
247 
248 }
249