/src/openssl/crypto/bn/bn_x931p.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
23 | | static int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx, |
24 | | BN_GENCB *cb) |
25 | 0 | { |
26 | 0 | int i = 0, is_prime; |
27 | 0 | if (!BN_copy(pi, Xpi)) |
28 | 0 | return 0; |
29 | 0 | if (!BN_is_odd(pi) && !BN_add_word(pi, 1)) |
30 | 0 | return 0; |
31 | 0 | for (;;) { |
32 | 0 | i++; |
33 | 0 | BN_GENCB_call(cb, 0, i); |
34 | | /* NB 27 MR is specified in X9.31 */ |
35 | 0 | is_prime = BN_check_prime(pi, ctx, cb); |
36 | 0 | if (is_prime < 0) |
37 | 0 | return 0; |
38 | 0 | if (is_prime) |
39 | 0 | break; |
40 | 0 | if (!BN_add_word(pi, 2)) |
41 | 0 | return 0; |
42 | 0 | } |
43 | 0 | BN_GENCB_call(cb, 2, i); |
44 | 0 | return 1; |
45 | 0 | } |
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 | | |
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 | 0 | { |
58 | 0 | int ret = 0; |
59 | |
|
60 | 0 | BIGNUM *t, *p1p2, *pm1; |
61 | | |
62 | | /* Only even e supported */ |
63 | 0 | if (!BN_is_odd(e)) |
64 | 0 | return 0; |
65 | | |
66 | 0 | BN_CTX_start(ctx); |
67 | 0 | if (p1 == NULL) |
68 | 0 | p1 = BN_CTX_get(ctx); |
69 | |
|
70 | 0 | if (p2 == NULL) |
71 | 0 | p2 = BN_CTX_get(ctx); |
72 | |
|
73 | 0 | t = BN_CTX_get(ctx); |
74 | |
|
75 | 0 | p1p2 = BN_CTX_get(ctx); |
76 | |
|
77 | 0 | pm1 = BN_CTX_get(ctx); |
78 | |
|
79 | 0 | if (pm1 == NULL) |
80 | 0 | goto err; |
81 | | |
82 | 0 | if (!bn_x931_derive_pi(p1, Xp1, ctx, cb)) |
83 | 0 | goto err; |
84 | | |
85 | 0 | if (!bn_x931_derive_pi(p2, Xp2, ctx, cb)) |
86 | 0 | goto err; |
87 | | |
88 | 0 | if (!BN_mul(p1p2, p1, p2, ctx)) |
89 | 0 | goto err; |
90 | | |
91 | | /* First set p to value of Rp */ |
92 | | |
93 | 0 | if (!BN_mod_inverse(p, p2, p1, ctx)) |
94 | 0 | goto err; |
95 | | |
96 | 0 | if (!BN_mul(p, p, p2, ctx)) |
97 | 0 | goto err; |
98 | | |
99 | 0 | if (!BN_mod_inverse(t, p1, p2, ctx)) |
100 | 0 | goto err; |
101 | | |
102 | 0 | if (!BN_mul(t, t, p1, ctx)) |
103 | 0 | goto err; |
104 | | |
105 | 0 | if (!BN_sub(p, p, t)) |
106 | 0 | goto err; |
107 | | |
108 | 0 | if (p->neg && !BN_add(p, p, p1p2)) |
109 | 0 | goto err; |
110 | | |
111 | | /* p now equals Rp */ |
112 | | |
113 | 0 | if (!BN_mod_sub(p, p, Xp, p1p2, ctx)) |
114 | 0 | goto err; |
115 | | |
116 | 0 | if (!BN_add(p, p, Xp)) |
117 | 0 | goto err; |
118 | | |
119 | | /* p now equals Yp0 */ |
120 | | |
121 | 0 | for (;;) { |
122 | 0 | int i = 1; |
123 | 0 | BN_GENCB_call(cb, 0, i++); |
124 | 0 | if (!BN_copy(pm1, p)) |
125 | 0 | goto err; |
126 | 0 | if (!BN_sub_word(pm1, 1)) |
127 | 0 | goto err; |
128 | 0 | if (!BN_gcd(t, pm1, e, ctx)) |
129 | 0 | goto err; |
130 | 0 | 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 | 0 | int r = BN_check_prime(p, ctx, cb); |
137 | 0 | if (r < 0) |
138 | 0 | goto err; |
139 | 0 | if (r) |
140 | 0 | break; |
141 | 0 | } |
142 | 0 | if (!BN_add(p, p, p1p2)) |
143 | 0 | goto err; |
144 | 0 | } |
145 | | |
146 | 0 | BN_GENCB_call(cb, 3, 0); |
147 | |
|
148 | 0 | ret = 1; |
149 | |
|
150 | 0 | err: |
151 | |
|
152 | 0 | BN_CTX_end(ctx); |
153 | |
|
154 | 0 | return ret; |
155 | 0 | } |
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 | | |
162 | | int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx) |
163 | 0 | { |
164 | 0 | BIGNUM *t; |
165 | 0 | int i; |
166 | | /* |
167 | | * Number of bits for each prime is of the form 512+128s for s = 0, 1, |
168 | | * ... |
169 | | */ |
170 | 0 | if ((nbits < 1024) || (nbits & 0xff)) |
171 | 0 | return 0; |
172 | 0 | 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 | 0 | if (!BN_priv_rand_ex(Xp, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY, 0, |
179 | 0 | ctx)) |
180 | 0 | return 0; |
181 | | |
182 | 0 | BN_CTX_start(ctx); |
183 | 0 | t = BN_CTX_get(ctx); |
184 | 0 | if (t == NULL) |
185 | 0 | goto err; |
186 | | |
187 | 0 | for (i = 0; i < 1000; i++) { |
188 | 0 | if (!BN_priv_rand_ex(Xq, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY, 0, |
189 | 0 | ctx)) |
190 | 0 | goto err; |
191 | | |
192 | | /* Check that |Xp - Xq| > 2^(nbits - 100) */ |
193 | 0 | if (!BN_sub(t, Xp, Xq)) |
194 | 0 | goto err; |
195 | 0 | if (BN_num_bits(t) > (nbits - 100)) |
196 | 0 | break; |
197 | 0 | } |
198 | | |
199 | 0 | BN_CTX_end(ctx); |
200 | |
|
201 | 0 | if (i < 1000) |
202 | 0 | return 1; |
203 | | |
204 | 0 | return 0; |
205 | | |
206 | 0 | err: |
207 | 0 | BN_CTX_end(ctx); |
208 | 0 | return 0; |
209 | 0 | } |
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 | | |
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 | 0 | { |
224 | 0 | int ret = 0; |
225 | |
|
226 | 0 | BN_CTX_start(ctx); |
227 | 0 | if (Xp1 == NULL) |
228 | 0 | Xp1 = BN_CTX_get(ctx); |
229 | 0 | if (Xp2 == NULL) |
230 | 0 | Xp2 = BN_CTX_get(ctx); |
231 | 0 | if (Xp1 == NULL || Xp2 == NULL) |
232 | 0 | goto error; |
233 | | |
234 | 0 | if (!BN_priv_rand_ex(Xp1, 101, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY, 0, ctx)) |
235 | 0 | goto error; |
236 | 0 | if (!BN_priv_rand_ex(Xp2, 101, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY, 0, ctx)) |
237 | 0 | goto error; |
238 | 0 | if (!BN_X931_derive_prime_ex(p, p1, p2, Xp, Xp1, Xp2, e, ctx, cb)) |
239 | 0 | goto error; |
240 | | |
241 | 0 | ret = 1; |
242 | |
|
243 | 0 | error: |
244 | 0 | BN_CTX_end(ctx); |
245 | |
|
246 | 0 | return ret; |
247 | |
|
248 | 0 | } |