bn_rand.c 9.2 KB

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