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