bn_rand.c 9.1 KB

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