bn_rand.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "bn_lcl.h"
  13. #include <openssl/rand.h>
  14. #include <openssl/sha.h>
  15. static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
  16. {
  17. unsigned char *buf = NULL;
  18. int ret = 0, bit, bytes, mask;
  19. time_t tim;
  20. if (bits == 0) {
  21. if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)
  22. goto toosmall;
  23. BN_zero(rnd);
  24. return 1;
  25. }
  26. if (bits < 0 || (bits == 1 && top > 0))
  27. goto toosmall;
  28. bytes = (bits + 7) / 8;
  29. bit = (bits - 1) % 8;
  30. mask = 0xff << (bit + 1);
  31. buf = OPENSSL_malloc(bytes);
  32. if (buf == NULL) {
  33. BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
  34. goto err;
  35. }
  36. /* make a random number and set the top and bottom bits */
  37. time(&tim);
  38. RAND_add(&tim, sizeof(tim), 0.0);
  39. if (RAND_bytes(buf, bytes) <= 0)
  40. goto err;
  41. if (pseudorand == 2) {
  42. /*
  43. * generate patterns that are more likely to trigger BN library bugs
  44. */
  45. int i;
  46. unsigned char c;
  47. for (i = 0; i < bytes; i++) {
  48. if (RAND_bytes(&c, 1) <= 0)
  49. goto err;
  50. if (c >= 128 && i > 0)
  51. buf[i] = buf[i - 1];
  52. else if (c < 42)
  53. buf[i] = 0;
  54. else if (c < 84)
  55. buf[i] = 255;
  56. }
  57. }
  58. if (top >= 0) {
  59. if (top) {
  60. if (bit == 0) {
  61. buf[0] = 1;
  62. buf[1] |= 0x80;
  63. } else {
  64. buf[0] |= (3 << (bit - 1));
  65. }
  66. } else {
  67. buf[0] |= (1 << bit);
  68. }
  69. }
  70. buf[0] &= ~mask;
  71. if (bottom) /* set bottom bit if requested */
  72. buf[bytes - 1] |= 1;
  73. if (!BN_bin2bn(buf, bytes, rnd))
  74. goto err;
  75. ret = 1;
  76. err:
  77. OPENSSL_clear_free(buf, bytes);
  78. bn_check_top(rnd);
  79. return (ret);
  80. toosmall:
  81. BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);
  82. return 0;
  83. }
  84. int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
  85. {
  86. return bnrand(0, rnd, bits, top, bottom);
  87. }
  88. int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
  89. {
  90. return bnrand(1, rnd, bits, top, bottom);
  91. }
  92. int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
  93. {
  94. return bnrand(2, rnd, bits, top, bottom);
  95. }
  96. /* random number r: 0 <= r < range */
  97. static int bn_rand_range(int pseudo, BIGNUM *r, const BIGNUM *range)
  98. {
  99. int (*bn_rand) (BIGNUM *, int, int, int) =
  100. pseudo ? BN_pseudo_rand : BN_rand;
  101. int n;
  102. int count = 100;
  103. if (range->neg || BN_is_zero(range)) {
  104. BNerr(BN_F_BN_RAND_RANGE, BN_R_INVALID_RANGE);
  105. return 0;
  106. }
  107. n = BN_num_bits(range); /* n > 0 */
  108. /* BN_is_bit_set(range, n - 1) always holds */
  109. if (n == 1)
  110. BN_zero(r);
  111. else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
  112. /*
  113. * range = 100..._2, so 3*range (= 11..._2) is exactly one bit longer
  114. * than range
  115. */
  116. do {
  117. if (!bn_rand(r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
  118. return 0;
  119. /*
  120. * If r < 3*range, use r := r MOD range (which is either r, r -
  121. * range, or r - 2*range). Otherwise, iterate once more. Since
  122. * 3*range = 11..._2, each iteration succeeds with probability >=
  123. * .75.
  124. */
  125. if (BN_cmp(r, range) >= 0) {
  126. if (!BN_sub(r, r, range))
  127. return 0;
  128. if (BN_cmp(r, range) >= 0)
  129. if (!BN_sub(r, r, range))
  130. return 0;
  131. }
  132. if (!--count) {
  133. BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
  134. return 0;
  135. }
  136. }
  137. while (BN_cmp(r, range) >= 0);
  138. } else {
  139. do {
  140. /* range = 11..._2 or range = 101..._2 */
  141. if (!bn_rand(r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
  142. return 0;
  143. if (!--count) {
  144. BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
  145. return 0;
  146. }
  147. }
  148. while (BN_cmp(r, range) >= 0);
  149. }
  150. bn_check_top(r);
  151. return 1;
  152. }
  153. int BN_rand_range(BIGNUM *r, const BIGNUM *range)
  154. {
  155. return bn_rand_range(0, r, range);
  156. }
  157. int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range)
  158. {
  159. return bn_rand_range(1, r, range);
  160. }
  161. /*
  162. * BN_generate_dsa_nonce generates a random number 0 <= out < range. Unlike
  163. * BN_rand_range, it also includes the contents of |priv| and |message| in
  164. * the generation so that an RNG failure isn't fatal as long as |priv|
  165. * remains secret. This is intended for use in DSA and ECDSA where an RNG
  166. * weakness leads directly to private key exposure unless this function is
  167. * used.
  168. */
  169. int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
  170. const BIGNUM *priv, const unsigned char *message,
  171. size_t message_len, BN_CTX *ctx)
  172. {
  173. SHA512_CTX sha;
  174. /*
  175. * We use 512 bits of random data per iteration to ensure that we have at
  176. * least |range| bits of randomness.
  177. */
  178. unsigned char random_bytes[64];
  179. unsigned char digest[SHA512_DIGEST_LENGTH];
  180. unsigned done, todo;
  181. /* We generate |range|+8 bytes of random output. */
  182. const unsigned num_k_bytes = BN_num_bytes(range) + 8;
  183. unsigned char private_bytes[96];
  184. unsigned char *k_bytes;
  185. int ret = 0;
  186. k_bytes = OPENSSL_malloc(num_k_bytes);
  187. if (k_bytes == NULL)
  188. goto err;
  189. /* We copy |priv| into a local buffer to avoid exposing its length. */
  190. todo = sizeof(priv->d[0]) * priv->top;
  191. if (todo > sizeof(private_bytes)) {
  192. /*
  193. * No reasonable DSA or ECDSA key should have a private key this
  194. * large and we don't handle this case in order to avoid leaking the
  195. * length of the private key.
  196. */
  197. BNerr(BN_F_BN_GENERATE_DSA_NONCE, BN_R_PRIVATE_KEY_TOO_LARGE);
  198. goto err;
  199. }
  200. memcpy(private_bytes, priv->d, todo);
  201. memset(private_bytes + todo, 0, sizeof(private_bytes) - todo);
  202. for (done = 0; done < num_k_bytes;) {
  203. if (RAND_bytes(random_bytes, sizeof(random_bytes)) != 1)
  204. goto err;
  205. SHA512_Init(&sha);
  206. SHA512_Update(&sha, &done, sizeof(done));
  207. SHA512_Update(&sha, private_bytes, sizeof(private_bytes));
  208. SHA512_Update(&sha, message, message_len);
  209. SHA512_Update(&sha, random_bytes, sizeof(random_bytes));
  210. SHA512_Final(digest, &sha);
  211. todo = num_k_bytes - done;
  212. if (todo > SHA512_DIGEST_LENGTH)
  213. todo = SHA512_DIGEST_LENGTH;
  214. memcpy(k_bytes + done, digest, todo);
  215. done += todo;
  216. }
  217. if (!BN_bin2bn(k_bytes, num_k_bytes, out))
  218. goto err;
  219. if (BN_mod(out, out, range, ctx) != 1)
  220. goto err;
  221. ret = 1;
  222. err:
  223. OPENSSL_free(k_bytes);
  224. OPENSSL_cleanse(private_bytes, sizeof(private_bytes));
  225. return ret;
  226. }