/src/openssl30/crypto/bn/bn_rand.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2024 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 | | #include <stdio.h> |
11 | | #include <time.h> |
12 | | #include "internal/cryptlib.h" |
13 | | #include "crypto/rand.h" |
14 | | #include "bn_local.h" |
15 | | #include <openssl/rand.h> |
16 | | #include <openssl/sha.h> |
17 | | #include <openssl/evp.h> |
18 | | |
19 | | typedef enum bnrand_flag_e { |
20 | | NORMAL, |
21 | | TESTING, |
22 | | PRIVATE |
23 | | } BNRAND_FLAG; |
24 | | |
25 | | static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom, |
26 | | unsigned int strength, BN_CTX *ctx) |
27 | 624k | { |
28 | 624k | unsigned char *buf = NULL; |
29 | 624k | int b, ret = 0, bit, bytes, mask; |
30 | 624k | OSSL_LIB_CTX *libctx = ossl_bn_get_libctx(ctx); |
31 | | |
32 | 624k | if (bits == 0) { |
33 | 0 | if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY) |
34 | 0 | goto toosmall; |
35 | 0 | BN_zero(rnd); |
36 | 0 | return 1; |
37 | 0 | } |
38 | 624k | if (bits < 0 || (bits == 1 && top > 0)) |
39 | 0 | goto toosmall; |
40 | | |
41 | 624k | bytes = (bits + 7) / 8; |
42 | 624k | bit = (bits - 1) % 8; |
43 | 624k | mask = 0xff << (bit + 1); |
44 | | |
45 | 624k | buf = OPENSSL_malloc(bytes); |
46 | 624k | if (buf == NULL) { |
47 | 0 | ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); |
48 | 0 | goto err; |
49 | 0 | } |
50 | | |
51 | | /* make a random number and set the top and bottom bits */ |
52 | 624k | b = flag == NORMAL ? RAND_bytes_ex(libctx, buf, bytes, strength) |
53 | 624k | : RAND_priv_bytes_ex(libctx, buf, bytes, strength); |
54 | 624k | if (b <= 0) |
55 | 0 | goto err; |
56 | | |
57 | 624k | if (flag == TESTING) { |
58 | | /* |
59 | | * generate patterns that are more likely to trigger BN library bugs |
60 | | */ |
61 | 0 | int i; |
62 | 0 | unsigned char c; |
63 | |
|
64 | 0 | for (i = 0; i < bytes; i++) { |
65 | 0 | if (RAND_bytes_ex(libctx, &c, 1, strength) <= 0) |
66 | 0 | goto err; |
67 | 0 | if (c >= 128 && i > 0) |
68 | 0 | buf[i] = buf[i - 1]; |
69 | 0 | else if (c < 42) |
70 | 0 | buf[i] = 0; |
71 | 0 | else if (c < 84) |
72 | 0 | buf[i] = 255; |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | 624k | if (top >= 0) { |
77 | 292k | if (top) { |
78 | 0 | if (bit == 0) { |
79 | 0 | buf[0] = 1; |
80 | 0 | buf[1] |= 0x80; |
81 | 0 | } else { |
82 | 0 | buf[0] |= (3 << (bit - 1)); |
83 | 0 | } |
84 | 292k | } else { |
85 | 292k | buf[0] |= (1 << bit); |
86 | 292k | } |
87 | 292k | } |
88 | 624k | buf[0] &= ~mask; |
89 | 624k | if (bottom) /* set bottom bit if requested */ |
90 | 0 | buf[bytes - 1] |= 1; |
91 | 624k | if (!BN_bin2bn(buf, bytes, rnd)) |
92 | 0 | goto err; |
93 | 624k | ret = 1; |
94 | 624k | err: |
95 | 624k | OPENSSL_clear_free(buf, bytes); |
96 | 624k | bn_check_top(rnd); |
97 | 624k | return ret; |
98 | | |
99 | 0 | toosmall: |
100 | 0 | ERR_raise(ERR_LIB_BN, BN_R_BITS_TOO_SMALL); |
101 | 0 | return 0; |
102 | 624k | } |
103 | | |
104 | | int BN_rand_ex(BIGNUM *rnd, int bits, int top, int bottom, |
105 | | unsigned int strength, BN_CTX *ctx) |
106 | 0 | { |
107 | 0 | return bnrand(NORMAL, rnd, bits, top, bottom, strength, ctx); |
108 | 0 | } |
109 | | #ifndef FIPS_MODULE |
110 | | int BN_rand(BIGNUM *rnd, int bits, int top, int bottom) |
111 | 0 | { |
112 | 0 | return bnrand(NORMAL, rnd, bits, top, bottom, 0, NULL); |
113 | 0 | } |
114 | | |
115 | | int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom) |
116 | 0 | { |
117 | 0 | return bnrand(TESTING, rnd, bits, top, bottom, 0, NULL); |
118 | 0 | } |
119 | | #endif |
120 | | |
121 | | int BN_priv_rand_ex(BIGNUM *rnd, int bits, int top, int bottom, |
122 | | unsigned int strength, BN_CTX *ctx) |
123 | 472k | { |
124 | 472k | return bnrand(PRIVATE, rnd, bits, top, bottom, strength, ctx); |
125 | 472k | } |
126 | | |
127 | | #ifndef FIPS_MODULE |
128 | | int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom) |
129 | 0 | { |
130 | 0 | return bnrand(PRIVATE, rnd, bits, top, bottom, 0, NULL); |
131 | 0 | } |
132 | | #endif |
133 | | |
134 | | /* random number r: 0 <= r < range */ |
135 | | static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range, |
136 | | unsigned int strength, BN_CTX *ctx) |
137 | 152k | { |
138 | 152k | int n; |
139 | 152k | int count = 100; |
140 | | |
141 | 152k | if (r == NULL) { |
142 | 0 | ERR_raise(ERR_LIB_BN, ERR_R_PASSED_NULL_PARAMETER); |
143 | 0 | return 0; |
144 | 0 | } |
145 | | |
146 | 152k | if (range->neg || BN_is_zero(range)) { |
147 | 0 | ERR_raise(ERR_LIB_BN, BN_R_INVALID_RANGE); |
148 | 0 | return 0; |
149 | 0 | } |
150 | | |
151 | 152k | n = BN_num_bits(range); /* n > 0 */ |
152 | | |
153 | | /* BN_is_bit_set(range, n - 1) always holds */ |
154 | | |
155 | 152k | if (n == 1) |
156 | 0 | BN_zero(r); |
157 | 152k | else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) { |
158 | | /* |
159 | | * range = 100..._2, so 3*range (= 11..._2) is exactly one bit longer |
160 | | * than range |
161 | | */ |
162 | 40.0k | do { |
163 | 40.0k | if (!bnrand(flag, r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, |
164 | 40.0k | strength, ctx)) |
165 | 0 | return 0; |
166 | | |
167 | | /* |
168 | | * If r < 3*range, use r := r MOD range (which is either r, r - |
169 | | * range, or r - 2*range). Otherwise, iterate once more. Since |
170 | | * 3*range = 11..._2, each iteration succeeds with probability >= |
171 | | * .75. |
172 | | */ |
173 | 40.0k | if (BN_cmp(r, range) >= 0) { |
174 | 14.2k | if (!BN_sub(r, r, range)) |
175 | 0 | return 0; |
176 | 14.2k | if (BN_cmp(r, range) >= 0) |
177 | 4.40k | if (!BN_sub(r, r, range)) |
178 | 0 | return 0; |
179 | 14.2k | } |
180 | | |
181 | 40.0k | if (!--count) { |
182 | 0 | ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); |
183 | 0 | return 0; |
184 | 0 | } |
185 | | |
186 | 40.0k | } while (BN_cmp(r, range) >= 0); |
187 | 112k | } else { |
188 | 112k | do { |
189 | | /* range = 11..._2 or range = 101..._2 */ |
190 | 112k | if (!bnrand(flag, r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, |
191 | 112k | strength, ctx)) |
192 | 0 | return 0; |
193 | | |
194 | 112k | if (!--count) { |
195 | 0 | ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); |
196 | 0 | return 0; |
197 | 0 | } |
198 | 112k | } while (BN_cmp(r, range) >= 0); |
199 | 112k | } |
200 | | |
201 | 152k | bn_check_top(r); |
202 | 152k | return 1; |
203 | 152k | } |
204 | | |
205 | | int BN_rand_range_ex(BIGNUM *r, const BIGNUM *range, unsigned int strength, |
206 | | BN_CTX *ctx) |
207 | 0 | { |
208 | 0 | return bnrand_range(NORMAL, r, range, strength, ctx); |
209 | 0 | } |
210 | | |
211 | | #ifndef FIPS_MODULE |
212 | | int BN_rand_range(BIGNUM *r, const BIGNUM *range) |
213 | 0 | { |
214 | 0 | return bnrand_range(NORMAL, r, range, 0, NULL); |
215 | 0 | } |
216 | | #endif |
217 | | |
218 | | int BN_priv_rand_range_ex(BIGNUM *r, const BIGNUM *range, unsigned int strength, |
219 | | BN_CTX *ctx) |
220 | 152k | { |
221 | 152k | return bnrand_range(PRIVATE, r, range, strength, ctx); |
222 | 152k | } |
223 | | |
224 | | #ifndef FIPS_MODULE |
225 | | int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range) |
226 | 0 | { |
227 | 0 | return bnrand_range(PRIVATE, r, range, 0, NULL); |
228 | 0 | } |
229 | | |
230 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
231 | | int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom) |
232 | 0 | { |
233 | 0 | return BN_rand(rnd, bits, top, bottom); |
234 | 0 | } |
235 | | |
236 | | int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range) |
237 | 0 | { |
238 | 0 | return BN_rand_range(r, range); |
239 | 0 | } |
240 | | #endif |
241 | | #endif |
242 | | |
243 | | int ossl_bn_priv_rand_range_fixed_top(BIGNUM *r, const BIGNUM *range, |
244 | | unsigned int strength, BN_CTX *ctx) |
245 | 0 | { |
246 | 0 | int n; |
247 | 0 | int count = 100; |
248 | |
|
249 | 0 | if (r == NULL) { |
250 | 0 | ERR_raise(ERR_LIB_BN, ERR_R_PASSED_NULL_PARAMETER); |
251 | 0 | return 0; |
252 | 0 | } |
253 | | |
254 | 0 | if (range->neg || BN_is_zero(range)) { |
255 | 0 | ERR_raise(ERR_LIB_BN, BN_R_INVALID_RANGE); |
256 | 0 | return 0; |
257 | 0 | } |
258 | | |
259 | 0 | n = BN_num_bits(range); /* n > 0 */ |
260 | | |
261 | | /* BN_is_bit_set(range, n - 1) always holds */ |
262 | |
|
263 | 0 | if (n == 1) { |
264 | 0 | BN_zero(r); |
265 | 0 | } else { |
266 | 0 | BN_set_flags(r, BN_FLG_CONSTTIME); |
267 | 0 | do { |
268 | 0 | if (!bnrand(PRIVATE, r, n + 1, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY, |
269 | 0 | strength, ctx)) |
270 | 0 | return 0; |
271 | | |
272 | 0 | if (!--count) { |
273 | 0 | ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); |
274 | 0 | return 0; |
275 | 0 | } |
276 | 0 | ossl_bn_mask_bits_fixed_top(r, n); |
277 | 0 | } while (BN_ucmp(r, range) >= 0); |
278 | | #ifdef BN_DEBUG |
279 | | /* With BN_DEBUG on a fixed top number cannot be returned */ |
280 | | bn_correct_top(r); |
281 | | #endif |
282 | 0 | } |
283 | | |
284 | 0 | return 1; |
285 | 0 | } |
286 | | |
287 | | /* |
288 | | * ossl_bn_gen_dsa_nonce_fixed_top generates a random number 0 <= out < range. |
289 | | * Unlike BN_rand_range, it also includes the contents of |priv| and |message| |
290 | | * in the generation so that an RNG failure isn't fatal as long as |priv| |
291 | | * remains secret. This is intended for use in DSA and ECDSA where an RNG |
292 | | * weakness leads directly to private key exposure unless this function is |
293 | | * used. |
294 | | */ |
295 | | int ossl_bn_gen_dsa_nonce_fixed_top(BIGNUM *out, const BIGNUM *range, |
296 | | const BIGNUM *priv, |
297 | | const unsigned char *message, |
298 | | size_t message_len, BN_CTX *ctx) |
299 | 6.75k | { |
300 | 6.75k | EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); |
301 | | /* |
302 | | * We use 512 bits of random data per iteration to ensure that we have at |
303 | | * least |range| bits of randomness. |
304 | | */ |
305 | 6.75k | unsigned char random_bytes[64]; |
306 | 6.75k | unsigned char digest[SHA512_DIGEST_LENGTH]; |
307 | 6.75k | unsigned done, todo; |
308 | | /* We generate |range|+1 bytes of random output. */ |
309 | 6.75k | const unsigned num_k_bytes = BN_num_bytes(range) + 1; |
310 | 6.75k | unsigned char private_bytes[96]; |
311 | 6.75k | unsigned char *k_bytes = NULL; |
312 | 6.75k | const int max_n = 64; /* Pr(failure to generate) < 2^max_n */ |
313 | 6.75k | int n; |
314 | 6.75k | int ret = 0; |
315 | 6.75k | EVP_MD *md = NULL; |
316 | 6.75k | OSSL_LIB_CTX *libctx = ossl_bn_get_libctx(ctx); |
317 | | |
318 | 6.75k | if (mdctx == NULL) |
319 | 0 | goto end; |
320 | | |
321 | 6.75k | k_bytes = OPENSSL_malloc(num_k_bytes); |
322 | 6.75k | if (k_bytes == NULL) |
323 | 0 | goto end; |
324 | | /* Ensure top byte is set to avoid non-constant time in bin2bn */ |
325 | 6.75k | k_bytes[0] = 0xff; |
326 | | |
327 | | /* We copy |priv| into a local buffer to avoid exposing its length. */ |
328 | 6.75k | if (BN_bn2binpad(priv, private_bytes, sizeof(private_bytes)) < 0) { |
329 | | /* |
330 | | * No reasonable DSA or ECDSA key should have a private key this |
331 | | * large and we don't handle this case in order to avoid leaking the |
332 | | * length of the private key. |
333 | | */ |
334 | 0 | ERR_raise(ERR_LIB_BN, BN_R_PRIVATE_KEY_TOO_LARGE); |
335 | 0 | goto end; |
336 | 0 | } |
337 | | |
338 | 6.75k | md = EVP_MD_fetch(libctx, "SHA512", NULL); |
339 | 6.75k | if (md == NULL) { |
340 | 0 | ERR_raise(ERR_LIB_BN, BN_R_NO_SUITABLE_DIGEST); |
341 | 0 | goto end; |
342 | 0 | } |
343 | 6.75k | for (n = 0; n < max_n; n++) { |
344 | 6.75k | unsigned char i = 0; |
345 | | |
346 | 13.5k | for (done = 1; done < num_k_bytes;) { |
347 | 6.75k | if (RAND_priv_bytes_ex(libctx, random_bytes, sizeof(random_bytes), |
348 | 6.75k | 0) |
349 | 6.75k | <= 0) |
350 | 0 | goto end; |
351 | | |
352 | 6.75k | if (!EVP_DigestInit_ex(mdctx, md, NULL) |
353 | 6.75k | || !EVP_DigestUpdate(mdctx, &i, sizeof(i)) |
354 | 6.75k | || !EVP_DigestUpdate(mdctx, private_bytes, |
355 | 6.75k | sizeof(private_bytes)) |
356 | 6.75k | || !EVP_DigestUpdate(mdctx, message, message_len) |
357 | 6.75k | || !EVP_DigestUpdate(mdctx, random_bytes, |
358 | 6.75k | sizeof(random_bytes)) |
359 | 6.75k | || !EVP_DigestFinal_ex(mdctx, digest, NULL)) |
360 | 0 | goto end; |
361 | | |
362 | 6.75k | todo = num_k_bytes - done; |
363 | 6.75k | if (todo > SHA512_DIGEST_LENGTH) |
364 | 0 | todo = SHA512_DIGEST_LENGTH; |
365 | 6.75k | memcpy(k_bytes + done, digest, todo); |
366 | 6.75k | done += todo; |
367 | 6.75k | ++i; |
368 | 6.75k | } |
369 | | |
370 | 6.75k | if (!BN_bin2bn(k_bytes, num_k_bytes, out)) |
371 | 0 | goto end; |
372 | | |
373 | | /* Clear out the top bits and rejection filter into range */ |
374 | 6.75k | BN_set_flags(out, BN_FLG_CONSTTIME); |
375 | 6.75k | ossl_bn_mask_bits_fixed_top(out, BN_num_bits(range)); |
376 | | |
377 | 6.75k | if (BN_ucmp(out, range) < 0) { |
378 | 6.75k | ret = 1; |
379 | | #ifdef BN_DEBUG |
380 | | /* With BN_DEBUG on a fixed top number cannot be returned */ |
381 | | bn_correct_top(out); |
382 | | #endif |
383 | 6.75k | goto end; |
384 | 6.75k | } |
385 | 6.75k | } |
386 | | /* Failed to generate anything */ |
387 | 6.75k | ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR); |
388 | |
|
389 | 6.75k | end: |
390 | 6.75k | EVP_MD_CTX_free(mdctx); |
391 | 6.75k | EVP_MD_free(md); |
392 | 6.75k | OPENSSL_clear_free(k_bytes, num_k_bytes); |
393 | 6.75k | OPENSSL_cleanse(digest, sizeof(digest)); |
394 | 6.75k | OPENSSL_cleanse(random_bytes, sizeof(random_bytes)); |
395 | 6.75k | OPENSSL_cleanse(private_bytes, sizeof(private_bytes)); |
396 | 6.75k | return ret; |
397 | 0 | } |
398 | | |
399 | | int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range, |
400 | | const BIGNUM *priv, const unsigned char *message, |
401 | | size_t message_len, BN_CTX *ctx) |
402 | 0 | { |
403 | 0 | int ret; |
404 | |
|
405 | 0 | ret = ossl_bn_gen_dsa_nonce_fixed_top(out, range, priv, message, |
406 | 0 | message_len, ctx); |
407 | | /* |
408 | | * This call makes the BN_generate_dsa_nonce non-const-time, thus we |
409 | | * do not use it internally. But fixed_top BNs currently cannot be returned |
410 | | * from public API calls. |
411 | | */ |
412 | 0 | bn_correct_top(out); |
413 | 0 | return ret; |
414 | 0 | } |