bn_rand.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright 1995-2020 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. BN_CTX *ctx)
  22. {
  23. unsigned char *buf = NULL;
  24. int b, ret = 0, bit, bytes, mask;
  25. OSSL_LIB_CTX *libctx = 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)
  44. : RAND_priv_bytes_ex(libctx, buf, bytes);
  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) <= 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, BN_CTX *ctx)
  91. {
  92. return bnrand(NORMAL, rnd, bits, top, bottom, ctx);
  93. }
  94. #ifndef FIPS_MODULE
  95. int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
  96. {
  97. return bnrand(NORMAL, rnd, bits, top, bottom, NULL);
  98. }
  99. int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
  100. {
  101. return bnrand(TESTING, rnd, bits, top, bottom, NULL);
  102. }
  103. #endif
  104. int BN_priv_rand_ex(BIGNUM *rnd, int bits, int top, int bottom, BN_CTX *ctx)
  105. {
  106. return bnrand(PRIVATE, rnd, bits, top, bottom, 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, 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. BN_CTX *ctx)
  117. {
  118. int n;
  119. int count = 100;
  120. if (range->neg || BN_is_zero(range)) {
  121. ERR_raise(ERR_LIB_BN, BN_R_INVALID_RANGE);
  122. return 0;
  123. }
  124. n = BN_num_bits(range); /* n > 0 */
  125. /* BN_is_bit_set(range, n - 1) always holds */
  126. if (n == 1)
  127. BN_zero(r);
  128. else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
  129. /*
  130. * range = 100..._2, so 3*range (= 11..._2) is exactly one bit longer
  131. * than range
  132. */
  133. do {
  134. if (!bnrand(flag, r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY,
  135. ctx))
  136. return 0;
  137. /*
  138. * If r < 3*range, use r := r MOD range (which is either r, r -
  139. * range, or r - 2*range). Otherwise, iterate once more. Since
  140. * 3*range = 11..._2, each iteration succeeds with probability >=
  141. * .75.
  142. */
  143. if (BN_cmp(r, range) >= 0) {
  144. if (!BN_sub(r, r, range))
  145. return 0;
  146. if (BN_cmp(r, range) >= 0)
  147. if (!BN_sub(r, r, range))
  148. return 0;
  149. }
  150. if (!--count) {
  151. ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
  152. return 0;
  153. }
  154. }
  155. while (BN_cmp(r, range) >= 0);
  156. } else {
  157. do {
  158. /* range = 11..._2 or range = 101..._2 */
  159. if (!bnrand(flag, r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, ctx))
  160. return 0;
  161. if (!--count) {
  162. ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
  163. return 0;
  164. }
  165. }
  166. while (BN_cmp(r, range) >= 0);
  167. }
  168. bn_check_top(r);
  169. return 1;
  170. }
  171. int BN_rand_range_ex(BIGNUM *r, const BIGNUM *range, BN_CTX *ctx)
  172. {
  173. return bnrand_range(NORMAL, r, range, ctx);
  174. }
  175. #ifndef FIPS_MODULE
  176. int BN_rand_range(BIGNUM *r, const BIGNUM *range)
  177. {
  178. return bnrand_range(NORMAL, r, range, NULL);
  179. }
  180. #endif
  181. int BN_priv_rand_range_ex(BIGNUM *r, const BIGNUM *range, BN_CTX *ctx)
  182. {
  183. return bnrand_range(PRIVATE, r, range, ctx);
  184. }
  185. #ifndef FIPS_MODULE
  186. int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range)
  187. {
  188. return bnrand_range(PRIVATE, r, range, NULL);
  189. }
  190. int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
  191. {
  192. return BN_rand(rnd, bits, top, bottom);
  193. }
  194. int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range)
  195. {
  196. return BN_rand_range(r, range);
  197. }
  198. #endif
  199. /*
  200. * BN_generate_dsa_nonce generates a random number 0 <= out < range. Unlike
  201. * BN_rand_range, it also includes the contents of |priv| and |message| in
  202. * the generation so that an RNG failure isn't fatal as long as |priv|
  203. * remains secret. This is intended for use in DSA and ECDSA where an RNG
  204. * weakness leads directly to private key exposure unless this function is
  205. * used.
  206. */
  207. int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
  208. const BIGNUM *priv, const unsigned char *message,
  209. size_t message_len, BN_CTX *ctx)
  210. {
  211. EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
  212. /*
  213. * We use 512 bits of random data per iteration to ensure that we have at
  214. * least |range| bits of randomness.
  215. */
  216. unsigned char random_bytes[64];
  217. unsigned char digest[SHA512_DIGEST_LENGTH];
  218. unsigned done, todo;
  219. /* We generate |range|+8 bytes of random output. */
  220. const unsigned num_k_bytes = BN_num_bytes(range) + 8;
  221. unsigned char private_bytes[96];
  222. unsigned char *k_bytes = NULL;
  223. int ret = 0;
  224. EVP_MD *md = NULL;
  225. OSSL_LIB_CTX *libctx = bn_get_libctx(ctx);
  226. if (mdctx == NULL)
  227. goto err;
  228. k_bytes = OPENSSL_malloc(num_k_bytes);
  229. if (k_bytes == NULL)
  230. goto err;
  231. /* We copy |priv| into a local buffer to avoid exposing its length. */
  232. if (BN_bn2binpad(priv, private_bytes, sizeof(private_bytes)) < 0) {
  233. /*
  234. * No reasonable DSA or ECDSA key should have a private key this
  235. * large and we don't handle this case in order to avoid leaking the
  236. * length of the private key.
  237. */
  238. ERR_raise(ERR_LIB_BN, BN_R_PRIVATE_KEY_TOO_LARGE);
  239. goto err;
  240. }
  241. md = EVP_MD_fetch(libctx, "SHA512", NULL);
  242. if (md == NULL) {
  243. ERR_raise(ERR_LIB_BN, BN_R_NO_SUITABLE_DIGEST);
  244. goto err;
  245. }
  246. for (done = 0; done < num_k_bytes;) {
  247. if (!RAND_priv_bytes_ex(libctx, random_bytes, sizeof(random_bytes)))
  248. goto err;
  249. if (!EVP_DigestInit_ex(mdctx, md, NULL)
  250. || !EVP_DigestUpdate(mdctx, &done, sizeof(done))
  251. || !EVP_DigestUpdate(mdctx, private_bytes,
  252. sizeof(private_bytes))
  253. || !EVP_DigestUpdate(mdctx, message, message_len)
  254. || !EVP_DigestUpdate(mdctx, random_bytes, sizeof(random_bytes))
  255. || !EVP_DigestFinal_ex(mdctx, digest, NULL))
  256. goto err;
  257. todo = num_k_bytes - done;
  258. if (todo > SHA512_DIGEST_LENGTH)
  259. todo = SHA512_DIGEST_LENGTH;
  260. memcpy(k_bytes + done, digest, todo);
  261. done += todo;
  262. }
  263. if (!BN_bin2bn(k_bytes, num_k_bytes, out))
  264. goto err;
  265. if (BN_mod(out, out, range, ctx) != 1)
  266. goto err;
  267. ret = 1;
  268. err:
  269. EVP_MD_CTX_free(mdctx);
  270. EVP_MD_free(md);
  271. OPENSSL_free(k_bytes);
  272. OPENSSL_cleanse(private_bytes, sizeof(private_bytes));
  273. return ret;
  274. }