VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.3/crypto/bn/bn_rand.c@ 95218

Last change on this file since 95218 was 94082, checked in by vboxsync, 3 years ago

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

File size: 9.1 KB
Line 
1/*
2 * Copyright 1995-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#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
19typedef enum bnrand_flag_e {
20 NORMAL, TESTING, PRIVATE
21} BNRAND_FLAG;
22
23static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom,
24 unsigned int strength, BN_CTX *ctx)
25{
26 unsigned char *buf = NULL;
27 int b, ret = 0, bit, bytes, mask;
28 OSSL_LIB_CTX *libctx = ossl_bn_get_libctx(ctx);
29
30 if (bits == 0) {
31 if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)
32 goto toosmall;
33 BN_zero(rnd);
34 return 1;
35 }
36 if (bits < 0 || (bits == 1 && top > 0))
37 goto toosmall;
38
39 bytes = (bits + 7) / 8;
40 bit = (bits - 1) % 8;
41 mask = 0xff << (bit + 1);
42
43 buf = OPENSSL_malloc(bytes);
44 if (buf == NULL) {
45 ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
46 goto err;
47 }
48
49 /* make a random number and set the top and bottom bits */
50 b = flag == NORMAL ? RAND_bytes_ex(libctx, buf, bytes, strength)
51 : RAND_priv_bytes_ex(libctx, buf, bytes, strength);
52 if (b <= 0)
53 goto err;
54
55 if (flag == TESTING) {
56 /*
57 * generate patterns that are more likely to trigger BN library bugs
58 */
59 int i;
60 unsigned char c;
61
62 for (i = 0; i < bytes; i++) {
63 if (RAND_bytes_ex(libctx, &c, 1, strength) <= 0)
64 goto err;
65 if (c >= 128 && i > 0)
66 buf[i] = buf[i - 1];
67 else if (c < 42)
68 buf[i] = 0;
69 else if (c < 84)
70 buf[i] = 255;
71 }
72 }
73
74 if (top >= 0) {
75 if (top) {
76 if (bit == 0) {
77 buf[0] = 1;
78 buf[1] |= 0x80;
79 } else {
80 buf[0] |= (3 << (bit - 1));
81 }
82 } else {
83 buf[0] |= (1 << bit);
84 }
85 }
86 buf[0] &= ~mask;
87 if (bottom) /* set bottom bit if requested */
88 buf[bytes - 1] |= 1;
89 if (!BN_bin2bn(buf, bytes, rnd))
90 goto err;
91 ret = 1;
92 err:
93 OPENSSL_clear_free(buf, bytes);
94 bn_check_top(rnd);
95 return ret;
96
97toosmall:
98 ERR_raise(ERR_LIB_BN, BN_R_BITS_TOO_SMALL);
99 return 0;
100}
101
102int BN_rand_ex(BIGNUM *rnd, int bits, int top, int bottom,
103 unsigned int strength, BN_CTX *ctx)
104{
105 return bnrand(NORMAL, rnd, bits, top, bottom, strength, ctx);
106}
107#ifndef FIPS_MODULE
108int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
109{
110 return bnrand(NORMAL, rnd, bits, top, bottom, 0, NULL);
111}
112
113int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
114{
115 return bnrand(TESTING, rnd, bits, top, bottom, 0, NULL);
116}
117#endif
118
119int BN_priv_rand_ex(BIGNUM *rnd, int bits, int top, int bottom,
120 unsigned int strength, BN_CTX *ctx)
121{
122 return bnrand(PRIVATE, rnd, bits, top, bottom, strength, ctx);
123}
124
125#ifndef FIPS_MODULE
126int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom)
127{
128 return bnrand(PRIVATE, rnd, bits, top, bottom, 0, NULL);
129}
130#endif
131
132/* random number r: 0 <= r < range */
133static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range,
134 unsigned int strength, BN_CTX *ctx)
135{
136 int n;
137 int count = 100;
138
139 if (range->neg || BN_is_zero(range)) {
140 ERR_raise(ERR_LIB_BN, BN_R_INVALID_RANGE);
141 return 0;
142 }
143
144 n = BN_num_bits(range); /* n > 0 */
145
146 /* BN_is_bit_set(range, n - 1) always holds */
147
148 if (n == 1)
149 BN_zero(r);
150 else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
151 /*
152 * range = 100..._2, so 3*range (= 11..._2) is exactly one bit longer
153 * than range
154 */
155 do {
156 if (!bnrand(flag, r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY,
157 strength, ctx))
158 return 0;
159
160 /*
161 * If r < 3*range, use r := r MOD range (which is either r, r -
162 * range, or r - 2*range). Otherwise, iterate once more. Since
163 * 3*range = 11..._2, each iteration succeeds with probability >=
164 * .75.
165 */
166 if (BN_cmp(r, range) >= 0) {
167 if (!BN_sub(r, r, range))
168 return 0;
169 if (BN_cmp(r, range) >= 0)
170 if (!BN_sub(r, r, range))
171 return 0;
172 }
173
174 if (!--count) {
175 ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
176 return 0;
177 }
178
179 }
180 while (BN_cmp(r, range) >= 0);
181 } else {
182 do {
183 /* range = 11..._2 or range = 101..._2 */
184 if (!bnrand(flag, r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0,
185 ctx))
186 return 0;
187
188 if (!--count) {
189 ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
190 return 0;
191 }
192 }
193 while (BN_cmp(r, range) >= 0);
194 }
195
196 bn_check_top(r);
197 return 1;
198}
199
200int BN_rand_range_ex(BIGNUM *r, const BIGNUM *range, unsigned int strength,
201 BN_CTX *ctx)
202{
203 return bnrand_range(NORMAL, r, range, strength, ctx);
204}
205
206#ifndef FIPS_MODULE
207int BN_rand_range(BIGNUM *r, const BIGNUM *range)
208{
209 return bnrand_range(NORMAL, r, range, 0, NULL);
210}
211#endif
212
213int BN_priv_rand_range_ex(BIGNUM *r, const BIGNUM *range, unsigned int strength,
214 BN_CTX *ctx)
215{
216 return bnrand_range(PRIVATE, r, range, strength, ctx);
217}
218
219#ifndef FIPS_MODULE
220int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range)
221{
222 return bnrand_range(PRIVATE, r, range, 0, NULL);
223}
224
225# ifndef OPENSSL_NO_DEPRECATED_3_0
226int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
227{
228 return BN_rand(rnd, bits, top, bottom);
229}
230
231int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range)
232{
233 return BN_rand_range(r, range);
234}
235# endif
236#endif
237
238/*
239 * BN_generate_dsa_nonce generates a random number 0 <= out < range. Unlike
240 * BN_rand_range, it also includes the contents of |priv| and |message| in
241 * the generation so that an RNG failure isn't fatal as long as |priv|
242 * remains secret. This is intended for use in DSA and ECDSA where an RNG
243 * weakness leads directly to private key exposure unless this function is
244 * used.
245 */
246int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
247 const BIGNUM *priv, const unsigned char *message,
248 size_t message_len, BN_CTX *ctx)
249{
250 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
251 /*
252 * We use 512 bits of random data per iteration to ensure that we have at
253 * least |range| bits of randomness.
254 */
255 unsigned char random_bytes[64];
256 unsigned char digest[SHA512_DIGEST_LENGTH];
257 unsigned done, todo;
258 /* We generate |range|+8 bytes of random output. */
259 const unsigned num_k_bytes = BN_num_bytes(range) + 8;
260 unsigned char private_bytes[96];
261 unsigned char *k_bytes = NULL;
262 int ret = 0;
263 EVP_MD *md = NULL;
264 OSSL_LIB_CTX *libctx = ossl_bn_get_libctx(ctx);
265
266 if (mdctx == NULL)
267 goto err;
268
269 k_bytes = OPENSSL_malloc(num_k_bytes);
270 if (k_bytes == NULL)
271 goto err;
272
273 /* We copy |priv| into a local buffer to avoid exposing its length. */
274 if (BN_bn2binpad(priv, private_bytes, sizeof(private_bytes)) < 0) {
275 /*
276 * No reasonable DSA or ECDSA key should have a private key this
277 * large and we don't handle this case in order to avoid leaking the
278 * length of the private key.
279 */
280 ERR_raise(ERR_LIB_BN, BN_R_PRIVATE_KEY_TOO_LARGE);
281 goto err;
282 }
283
284 md = EVP_MD_fetch(libctx, "SHA512", NULL);
285 if (md == NULL) {
286 ERR_raise(ERR_LIB_BN, BN_R_NO_SUITABLE_DIGEST);
287 goto err;
288 }
289 for (done = 0; done < num_k_bytes;) {
290 if (RAND_priv_bytes_ex(libctx, random_bytes, sizeof(random_bytes), 0) <= 0)
291 goto err;
292
293 if (!EVP_DigestInit_ex(mdctx, md, NULL)
294 || !EVP_DigestUpdate(mdctx, &done, sizeof(done))
295 || !EVP_DigestUpdate(mdctx, private_bytes,
296 sizeof(private_bytes))
297 || !EVP_DigestUpdate(mdctx, message, message_len)
298 || !EVP_DigestUpdate(mdctx, random_bytes, sizeof(random_bytes))
299 || !EVP_DigestFinal_ex(mdctx, digest, NULL))
300 goto err;
301
302 todo = num_k_bytes - done;
303 if (todo > SHA512_DIGEST_LENGTH)
304 todo = SHA512_DIGEST_LENGTH;
305 memcpy(k_bytes + done, digest, todo);
306 done += todo;
307 }
308
309 if (!BN_bin2bn(k_bytes, num_k_bytes, out))
310 goto err;
311 if (BN_mod(out, out, range, ctx) != 1)
312 goto err;
313 ret = 1;
314
315 err:
316 EVP_MD_CTX_free(mdctx);
317 EVP_MD_free(md);
318 OPENSSL_free(k_bytes);
319 OPENSSL_cleanse(private_bytes, sizeof(private_bytes));
320 return ret;
321}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette