/src/openssl30/crypto/bn/bn_rand.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2023 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, TESTING, PRIVATE | 
| 21 |  | } BNRAND_FLAG; | 
| 22 |  |  | 
| 23 |  | static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom, | 
| 24 |  |                   unsigned int strength, BN_CTX *ctx) | 
| 25 | 278k | { | 
| 26 | 278k |     unsigned char *buf = NULL; | 
| 27 | 278k |     int b, ret = 0, bit, bytes, mask; | 
| 28 | 278k |     OSSL_LIB_CTX *libctx = ossl_bn_get_libctx(ctx); | 
| 29 |  |  | 
| 30 | 278k |     if (bits == 0) { | 
| 31 | 0 |         if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY) | 
| 32 | 0 |             goto toosmall; | 
| 33 | 0 |         BN_zero(rnd); | 
| 34 | 0 |         return 1; | 
| 35 | 0 |     } | 
| 36 | 278k |     if (bits < 0 || (bits == 1 && top > 0)) | 
| 37 | 0 |         goto toosmall; | 
| 38 |  |  | 
| 39 | 278k |     bytes = (bits + 7) / 8; | 
| 40 | 278k |     bit = (bits - 1) % 8; | 
| 41 | 278k |     mask = 0xff << (bit + 1); | 
| 42 |  |  | 
| 43 | 278k |     buf = OPENSSL_malloc(bytes); | 
| 44 | 278k |     if (buf == NULL) { | 
| 45 | 0 |         ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); | 
| 46 | 0 |         goto err; | 
| 47 | 0 |     } | 
| 48 |  |  | 
| 49 |  |     /* make a random number and set the top and bottom bits */ | 
| 50 | 278k |     b = flag == NORMAL ? RAND_bytes_ex(libctx, buf, bytes, strength) | 
| 51 | 278k |                        : RAND_priv_bytes_ex(libctx, buf, bytes, strength); | 
| 52 | 278k |     if (b <= 0) | 
| 53 | 0 |         goto err; | 
| 54 |  |  | 
| 55 | 278k |     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 | 278k |     if (top >= 0) { | 
| 75 | 171k |         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 | 171k |         } else { | 
| 83 | 171k |             buf[0] |= (1 << bit); | 
| 84 | 171k |         } | 
| 85 | 171k |     } | 
| 86 | 278k |     buf[0] &= ~mask; | 
| 87 | 278k |     if (bottom)                 /* set bottom bit if requested */ | 
| 88 | 0 |         buf[bytes - 1] |= 1; | 
| 89 | 278k |     if (!BN_bin2bn(buf, bytes, rnd)) | 
| 90 | 0 |         goto err; | 
| 91 | 278k |     ret = 1; | 
| 92 | 278k |  err: | 
| 93 | 278k |     OPENSSL_clear_free(buf, bytes); | 
| 94 | 278k |     bn_check_top(rnd); | 
| 95 | 278k |     return ret; | 
| 96 |  |  | 
| 97 | 0 | toosmall: | 
| 98 | 0 |     ERR_raise(ERR_LIB_BN, BN_R_BITS_TOO_SMALL); | 
| 99 | 0 |     return 0; | 
| 100 | 278k | } | 
| 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 | 169k | { | 
| 122 | 169k |     return bnrand(PRIVATE, rnd, bits, top, bottom, strength, ctx); | 
| 123 | 169k | } | 
| 124 |  |  | 
| 125 |  | #ifndef FIPS_MODULE | 
| 126 |  | int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom) | 
| 127 | 66.7k | { | 
| 128 | 66.7k |     return bnrand(PRIVATE, rnd, bits, top, bottom, 0, NULL); | 
| 129 | 66.7k | } | 
| 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 | 38.4k | { | 
| 136 | 38.4k |     int n; | 
| 137 | 38.4k |     int count = 100; | 
| 138 |  |  | 
| 139 | 38.4k |     if (r == NULL) { | 
| 140 | 0 |         ERR_raise(ERR_LIB_BN, ERR_R_PASSED_NULL_PARAMETER); | 
| 141 | 0 |         return 0; | 
| 142 | 0 |     } | 
| 143 |  |  | 
| 144 | 38.4k |     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 | 38.4k |     n = BN_num_bits(range);     /* n > 0 */ | 
| 150 |  |  | 
| 151 |  |     /* BN_is_bit_set(range, n - 1) always holds */ | 
| 152 |  |  | 
| 153 | 38.4k |     if (n == 1) | 
| 154 | 0 |         BN_zero(r); | 
| 155 | 38.4k |     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 | 11.8k |         do { | 
| 161 | 11.8k |             if (!bnrand(flag, r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, | 
| 162 | 11.8k |                         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 | 11.8k |             if (BN_cmp(r, range) >= 0) { | 
| 172 | 3.96k |                 if (!BN_sub(r, r, range)) | 
| 173 | 0 |                     return 0; | 
| 174 | 3.96k |                 if (BN_cmp(r, range) >= 0) | 
| 175 | 950 |                     if (!BN_sub(r, r, range)) | 
| 176 | 0 |                         return 0; | 
| 177 | 3.96k |             } | 
| 178 |  |  | 
| 179 | 11.8k |             if (!--count) { | 
| 180 | 0 |                 ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); | 
| 181 | 0 |                 return 0; | 
| 182 | 0 |             } | 
| 183 |  |  | 
| 184 | 11.8k |         } | 
| 185 | 11.8k |         while (BN_cmp(r, range) >= 0); | 
| 186 | 26.5k |     } else { | 
| 187 | 26.5k |         do { | 
| 188 |  |             /* range = 11..._2  or  range = 101..._2 */ | 
| 189 | 26.5k |             if (!bnrand(flag, r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, | 
| 190 | 26.5k |                         ctx)) | 
| 191 | 0 |                 return 0; | 
| 192 |  |  | 
| 193 | 26.5k |             if (!--count) { | 
| 194 | 0 |                 ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); | 
| 195 | 0 |                 return 0; | 
| 196 | 0 |             } | 
| 197 | 26.5k |         } | 
| 198 | 26.5k |         while (BN_cmp(r, range) >= 0); | 
| 199 | 26.5k |     } | 
| 200 |  |  | 
| 201 | 38.4k |     bn_check_top(r); | 
| 202 | 38.4k |     return 1; | 
| 203 | 38.4k | } | 
| 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 | 38.4k | { | 
| 221 | 38.4k |     return bnrand_range(PRIVATE, r, range, strength, ctx); | 
| 222 | 38.4k | } | 
| 223 |  |  | 
| 224 |  | #ifndef FIPS_MODULE | 
| 225 |  | int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range) | 
| 226 | 4.15k | { | 
| 227 | 4.15k |     return bnrand_range(PRIVATE, r, range, 0, NULL); | 
| 228 | 4.15k | } | 
| 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 |  | /* | 
| 244 |  |  * BN_generate_dsa_nonce generates a random number 0 <= out < range. Unlike | 
| 245 |  |  * BN_rand_range, it also includes the contents of |priv| and |message| in | 
| 246 |  |  * the generation so that an RNG failure isn't fatal as long as |priv| | 
| 247 |  |  * remains secret. This is intended for use in DSA and ECDSA where an RNG | 
| 248 |  |  * weakness leads directly to private key exposure unless this function is | 
| 249 |  |  * used. | 
| 250 |  |  */ | 
| 251 |  | int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range, | 
| 252 |  |                           const BIGNUM *priv, const unsigned char *message, | 
| 253 |  |                           size_t message_len, BN_CTX *ctx) | 
| 254 | 1.32k | { | 
| 255 | 1.32k |     EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); | 
| 256 |  |     /* | 
| 257 |  |      * We use 512 bits of random data per iteration to ensure that we have at | 
| 258 |  |      * least |range| bits of randomness. | 
| 259 |  |      */ | 
| 260 | 1.32k |     unsigned char random_bytes[64]; | 
| 261 | 1.32k |     unsigned char digest[SHA512_DIGEST_LENGTH]; | 
| 262 | 1.32k |     unsigned done, todo; | 
| 263 |  |     /* We generate |range|+8 bytes of random output. */ | 
| 264 | 1.32k |     const unsigned num_k_bytes = BN_num_bytes(range) + 8; | 
| 265 | 1.32k |     unsigned char private_bytes[96]; | 
| 266 | 1.32k |     unsigned char *k_bytes = NULL; | 
| 267 | 1.32k |     int ret = 0; | 
| 268 | 1.32k |     EVP_MD *md = NULL; | 
| 269 | 1.32k |     OSSL_LIB_CTX *libctx = ossl_bn_get_libctx(ctx); | 
| 270 |  |  | 
| 271 | 1.32k |     if (mdctx == NULL) | 
| 272 | 0 |         goto err; | 
| 273 |  |  | 
| 274 | 1.32k |     k_bytes = OPENSSL_malloc(num_k_bytes); | 
| 275 | 1.32k |     if (k_bytes == NULL) | 
| 276 | 0 |         goto err; | 
| 277 |  |  | 
| 278 |  |     /* We copy |priv| into a local buffer to avoid exposing its length. */ | 
| 279 | 1.32k |     if (BN_bn2binpad(priv, private_bytes, sizeof(private_bytes)) < 0) { | 
| 280 |  |         /* | 
| 281 |  |          * No reasonable DSA or ECDSA key should have a private key this | 
| 282 |  |          * large and we don't handle this case in order to avoid leaking the | 
| 283 |  |          * length of the private key. | 
| 284 |  |          */ | 
| 285 | 0 |         ERR_raise(ERR_LIB_BN, BN_R_PRIVATE_KEY_TOO_LARGE); | 
| 286 | 0 |         goto err; | 
| 287 | 0 |     } | 
| 288 |  |  | 
| 289 | 1.32k |     md = EVP_MD_fetch(libctx, "SHA512", NULL); | 
| 290 | 1.32k |     if (md == NULL) { | 
| 291 | 0 |         ERR_raise(ERR_LIB_BN, BN_R_NO_SUITABLE_DIGEST); | 
| 292 | 0 |         goto err; | 
| 293 | 0 |     } | 
| 294 | 2.65k |     for (done = 0; done < num_k_bytes;) { | 
| 295 | 1.32k |         if (RAND_priv_bytes_ex(libctx, random_bytes, sizeof(random_bytes), 0) <= 0) | 
| 296 | 0 |             goto err; | 
| 297 |  |  | 
| 298 | 1.32k |         if (!EVP_DigestInit_ex(mdctx, md, NULL) | 
| 299 | 1.32k |                 || !EVP_DigestUpdate(mdctx, &done, sizeof(done)) | 
| 300 | 1.32k |                 || !EVP_DigestUpdate(mdctx, private_bytes, | 
| 301 | 1.32k |                                      sizeof(private_bytes)) | 
| 302 | 1.32k |                 || !EVP_DigestUpdate(mdctx, message, message_len) | 
| 303 | 1.32k |                 || !EVP_DigestUpdate(mdctx, random_bytes, sizeof(random_bytes)) | 
| 304 | 1.32k |                 || !EVP_DigestFinal_ex(mdctx, digest, NULL)) | 
| 305 | 0 |             goto err; | 
| 306 |  |  | 
| 307 | 1.32k |         todo = num_k_bytes - done; | 
| 308 | 1.32k |         if (todo > SHA512_DIGEST_LENGTH) | 
| 309 | 0 |             todo = SHA512_DIGEST_LENGTH; | 
| 310 | 1.32k |         memcpy(k_bytes + done, digest, todo); | 
| 311 | 1.32k |         done += todo; | 
| 312 | 1.32k |     } | 
| 313 |  |  | 
| 314 | 1.32k |     if (!BN_bin2bn(k_bytes, num_k_bytes, out)) | 
| 315 | 0 |         goto err; | 
| 316 | 1.32k |     if (BN_mod(out, out, range, ctx) != 1) | 
| 317 | 0 |         goto err; | 
| 318 | 1.32k |     ret = 1; | 
| 319 |  |  | 
| 320 | 1.32k |  err: | 
| 321 | 1.32k |     EVP_MD_CTX_free(mdctx); | 
| 322 | 1.32k |     EVP_MD_free(md); | 
| 323 | 1.32k |     OPENSSL_clear_free(k_bytes, num_k_bytes); | 
| 324 | 1.32k |     OPENSSL_cleanse(digest, sizeof(digest)); | 
| 325 | 1.32k |     OPENSSL_cleanse(random_bytes, sizeof(random_bytes)); | 
| 326 | 1.32k |     OPENSSL_cleanse(private_bytes, sizeof(private_bytes)); | 
| 327 | 1.32k |     return ret; | 
| 328 | 1.32k | } |