2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/cryptlib.h"
13 #include "crypto/rand.h"
15 #include <openssl/rand.h>
16 #include <openssl/sha.h>
17 #include <openssl/evp.h>
19 typedef enum bnrand_flag_e
{
20 NORMAL
, TESTING
, PRIVATE
23 static int bnrand(BNRAND_FLAG flag
, BIGNUM
*rnd
, int bits
, int top
, int bottom
,
26 unsigned char *buf
= NULL
;
27 int b
, ret
= 0, bit
, bytes
, mask
;
28 OPENSSL_CTX
*libctx
= bn_get_lib_ctx(ctx
);
31 if (top
!= BN_RAND_TOP_ANY
|| bottom
!= BN_RAND_BOTTOM_ANY
)
36 if (bits
< 0 || (bits
== 1 && top
> 0))
39 bytes
= (bits
+ 7) / 8;
41 mask
= 0xff << (bit
+ 1);
43 buf
= OPENSSL_malloc(bytes
);
45 BNerr(BN_F_BNRAND
, ERR_R_MALLOC_FAILURE
);
49 /* make a random number and set the top and bottom bits */
50 b
= flag
== NORMAL
? rand_bytes_ex(libctx
, buf
, bytes
)
51 : rand_priv_bytes_ex(libctx
, buf
, bytes
);
55 if (flag
== TESTING
) {
57 * generate patterns that are more likely to trigger BN library bugs
62 for (i
= 0; i
< bytes
; i
++) {
63 if (rand_bytes_ex(libctx
, &c
, 1) <= 0)
65 if (c
>= 128 && i
> 0)
80 buf
[0] |= (3 << (bit
- 1));
87 if (bottom
) /* set bottom bit if requested */
89 if (!BN_bin2bn(buf
, bytes
, rnd
))
93 OPENSSL_clear_free(buf
, bytes
);
98 BNerr(BN_F_BNRAND
, BN_R_BITS_TOO_SMALL
);
102 int BN_rand_ex(BIGNUM
*rnd
, int bits
, int top
, int bottom
, BN_CTX
*ctx
)
104 return bnrand(NORMAL
, rnd
, bits
, top
, bottom
, ctx
);
107 int BN_rand(BIGNUM
*rnd
, int bits
, int top
, int bottom
)
109 return bnrand(NORMAL
, rnd
, bits
, top
, bottom
, NULL
);
112 int BN_bntest_rand(BIGNUM
*rnd
, int bits
, int top
, int bottom
)
114 return bnrand(TESTING
, rnd
, bits
, top
, bottom
, NULL
);
118 int BN_priv_rand_ex(BIGNUM
*rnd
, int bits
, int top
, int bottom
, BN_CTX
*ctx
)
120 return bnrand(PRIVATE
, rnd
, bits
, top
, bottom
, ctx
);
124 int BN_priv_rand(BIGNUM
*rnd
, int bits
, int top
, int bottom
)
126 return bnrand(PRIVATE
, rnd
, bits
, top
, bottom
, NULL
);
130 /* random number r: 0 <= r < range */
131 static int bnrand_range(BNRAND_FLAG flag
, BIGNUM
*r
, const BIGNUM
*range
,
137 if (range
->neg
|| BN_is_zero(range
)) {
138 BNerr(BN_F_BNRAND_RANGE
, BN_R_INVALID_RANGE
);
142 n
= BN_num_bits(range
); /* n > 0 */
144 /* BN_is_bit_set(range, n - 1) always holds */
148 else if (!BN_is_bit_set(range
, n
- 2) && !BN_is_bit_set(range
, n
- 3)) {
150 * range = 100..._2, so 3*range (= 11..._2) is exactly one bit longer
154 if (!bnrand(flag
, r
, n
+ 1, BN_RAND_TOP_ANY
, BN_RAND_BOTTOM_ANY
,
159 * If r < 3*range, use r := r MOD range (which is either r, r -
160 * range, or r - 2*range). Otherwise, iterate once more. Since
161 * 3*range = 11..._2, each iteration succeeds with probability >=
164 if (BN_cmp(r
, range
) >= 0) {
165 if (!BN_sub(r
, r
, range
))
167 if (BN_cmp(r
, range
) >= 0)
168 if (!BN_sub(r
, r
, range
))
173 BNerr(BN_F_BNRAND_RANGE
, BN_R_TOO_MANY_ITERATIONS
);
178 while (BN_cmp(r
, range
) >= 0);
181 /* range = 11..._2 or range = 101..._2 */
182 if (!bnrand(flag
, r
, n
, BN_RAND_TOP_ANY
, BN_RAND_BOTTOM_ANY
, ctx
))
186 BNerr(BN_F_BNRAND_RANGE
, BN_R_TOO_MANY_ITERATIONS
);
190 while (BN_cmp(r
, range
) >= 0);
197 int BN_rand_range_ex(BIGNUM
*r
, const BIGNUM
*range
, BN_CTX
*ctx
)
199 return bnrand_range(NORMAL
, r
, range
, ctx
);
203 int BN_rand_range(BIGNUM
*r
, const BIGNUM
*range
)
205 return bnrand_range(NORMAL
, r
, range
, NULL
);
209 int BN_priv_rand_range_ex(BIGNUM
*r
, const BIGNUM
*range
, BN_CTX
*ctx
)
211 return bnrand_range(PRIVATE
, r
, range
, ctx
);
215 int BN_priv_rand_range(BIGNUM
*r
, const BIGNUM
*range
)
217 return bnrand_range(PRIVATE
, r
, range
, NULL
);
220 int BN_pseudo_rand(BIGNUM
*rnd
, int bits
, int top
, int bottom
)
222 return BN_rand(rnd
, bits
, top
, bottom
);
225 int BN_pseudo_rand_range(BIGNUM
*r
, const BIGNUM
*range
)
227 return BN_rand_range(r
, range
);
232 * BN_generate_dsa_nonce generates a random number 0 <= out < range. Unlike
233 * BN_rand_range, it also includes the contents of |priv| and |message| in
234 * the generation so that an RNG failure isn't fatal as long as |priv|
235 * remains secret. This is intended for use in DSA and ECDSA where an RNG
236 * weakness leads directly to private key exposure unless this function is
239 int BN_generate_dsa_nonce(BIGNUM
*out
, const BIGNUM
*range
,
240 const BIGNUM
*priv
, const unsigned char *message
,
241 size_t message_len
, BN_CTX
*ctx
)
243 EVP_MD_CTX
*mdctx
= EVP_MD_CTX_new();
245 * We use 512 bits of random data per iteration to ensure that we have at
246 * least |range| bits of randomness.
248 unsigned char random_bytes
[64];
249 unsigned char digest
[SHA512_DIGEST_LENGTH
];
251 /* We generate |range|+8 bytes of random output. */
252 const unsigned num_k_bytes
= BN_num_bytes(range
) + 8;
253 unsigned char private_bytes
[96];
254 unsigned char *k_bytes
= NULL
;
257 OPENSSL_CTX
*libctx
= bn_get_lib_ctx(ctx
);
262 k_bytes
= OPENSSL_malloc(num_k_bytes
);
266 /* We copy |priv| into a local buffer to avoid exposing its length. */
267 if (BN_bn2binpad(priv
, private_bytes
, sizeof(private_bytes
)) < 0) {
269 * No reasonable DSA or ECDSA key should have a private key this
270 * large and we don't handle this case in order to avoid leaking the
271 * length of the private key.
273 BNerr(BN_F_BN_GENERATE_DSA_NONCE
, BN_R_PRIVATE_KEY_TOO_LARGE
);
277 md
= EVP_MD_fetch(libctx
, "SHA512", NULL
);
279 BNerr(BN_F_BN_GENERATE_DSA_NONCE
, BN_R_NO_SUITABLE_DIGEST
);
282 for (done
= 0; done
< num_k_bytes
;) {
283 if (!rand_priv_bytes_ex(libctx
, random_bytes
, sizeof(random_bytes
)))
286 if (!EVP_DigestInit_ex(mdctx
, md
, NULL
)
287 || !EVP_DigestUpdate(mdctx
, &done
, sizeof(done
))
288 || !EVP_DigestUpdate(mdctx
, private_bytes
,
289 sizeof(private_bytes
))
290 || !EVP_DigestUpdate(mdctx
, message
, message_len
)
291 || !EVP_DigestUpdate(mdctx
, random_bytes
, sizeof(random_bytes
))
292 || !EVP_DigestFinal_ex(mdctx
, digest
, NULL
))
295 todo
= num_k_bytes
- done
;
296 if (todo
> SHA512_DIGEST_LENGTH
)
297 todo
= SHA512_DIGEST_LENGTH
;
298 memcpy(k_bytes
+ done
, digest
, todo
);
302 if (!BN_bin2bn(k_bytes
, num_k_bytes
, out
))
304 if (BN_mod(out
, out
, range
, ctx
) != 1)
309 EVP_MD_CTX_free(mdctx
);
311 OPENSSL_free(k_bytes
);
312 OPENSSL_cleanse(private_bytes
, sizeof(private_bytes
));