bn_rand.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 testing, 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 (testing) {
  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_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
  89. {
  90. return bnrand(1, rnd, bits, top, bottom);
  91. }
  92. /* random number r: 0 <= r < range */
  93. int BN_rand_range(BIGNUM *r, const BIGNUM *range)
  94. {
  95. int n;
  96. int count = 100;
  97. if (range->neg || BN_is_zero(range)) {
  98. BNerr(BN_F_BN_RAND_RANGE, BN_R_INVALID_RANGE);
  99. return 0;
  100. }
  101. n = BN_num_bits(range); /* n > 0 */
  102. /* BN_is_bit_set(range, n - 1) always holds */
  103. if (n == 1)
  104. BN_zero(r);
  105. else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
  106. /*
  107. * range = 100..._2, so 3*range (= 11..._2) is exactly one bit longer
  108. * than range
  109. */
  110. do {
  111. if (!BN_rand(r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
  112. return 0;
  113. /*
  114. * If r < 3*range, use r := r MOD range (which is either r, r -
  115. * range, or r - 2*range). Otherwise, iterate once more. Since
  116. * 3*range = 11..._2, each iteration succeeds with probability >=
  117. * .75.
  118. */
  119. if (BN_cmp(r, range) >= 0) {
  120. if (!BN_sub(r, r, range))
  121. return 0;
  122. if (BN_cmp(r, range) >= 0)
  123. if (!BN_sub(r, r, range))
  124. return 0;
  125. }
  126. if (!--count) {
  127. BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
  128. return 0;
  129. }
  130. }
  131. while (BN_cmp(r, range) >= 0);
  132. } else {
  133. do {
  134. /* range = 11..._2 or range = 101..._2 */
  135. if (!BN_rand(r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
  136. return 0;
  137. if (!--count) {
  138. BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
  139. return 0;
  140. }
  141. }
  142. while (BN_cmp(r, range) >= 0);
  143. }
  144. bn_check_top(r);
  145. return 1;
  146. }
  147. int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
  148. {
  149. return BN_rand(rnd, bits, top, bottom);
  150. }
  151. int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range)
  152. {
  153. return BN_rand_range(r, range);
  154. }
  155. /*
  156. * BN_generate_dsa_nonce generates a random number 0 <= out < range. Unlike
  157. * BN_rand_range, it also includes the contents of |priv| and |message| in
  158. * the generation so that an RNG failure isn't fatal as long as |priv|
  159. * remains secret. This is intended for use in DSA and ECDSA where an RNG
  160. * weakness leads directly to private key exposure unless this function is
  161. * used.
  162. */
  163. int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
  164. const BIGNUM *priv, const unsigned char *message,
  165. size_t message_len, BN_CTX *ctx)
  166. {
  167. SHA512_CTX sha;
  168. /*
  169. * We use 512 bits of random data per iteration to ensure that we have at
  170. * least |range| bits of randomness.
  171. */
  172. unsigned char random_bytes[64];
  173. unsigned char digest[SHA512_DIGEST_LENGTH];
  174. unsigned done, todo;
  175. /* We generate |range|+8 bytes of random output. */
  176. const unsigned num_k_bytes = BN_num_bytes(range) + 8;
  177. unsigned char private_bytes[96];
  178. unsigned char *k_bytes;
  179. int ret = 0;
  180. k_bytes = OPENSSL_malloc(num_k_bytes);
  181. if (k_bytes == NULL)
  182. goto err;
  183. /* We copy |priv| into a local buffer to avoid exposing its length. */
  184. todo = sizeof(priv->d[0]) * priv->top;
  185. if (todo > sizeof(private_bytes)) {
  186. /*
  187. * No reasonable DSA or ECDSA key should have a private key this
  188. * large and we don't handle this case in order to avoid leaking the
  189. * length of the private key.
  190. */
  191. BNerr(BN_F_BN_GENERATE_DSA_NONCE, BN_R_PRIVATE_KEY_TOO_LARGE);
  192. goto err;
  193. }
  194. memcpy(private_bytes, priv->d, todo);
  195. memset(private_bytes + todo, 0, sizeof(private_bytes) - todo);
  196. for (done = 0; done < num_k_bytes;) {
  197. if (RAND_bytes(random_bytes, sizeof(random_bytes)) != 1)
  198. goto err;
  199. SHA512_Init(&sha);
  200. SHA512_Update(&sha, &done, sizeof(done));
  201. SHA512_Update(&sha, private_bytes, sizeof(private_bytes));
  202. SHA512_Update(&sha, message, message_len);
  203. SHA512_Update(&sha, random_bytes, sizeof(random_bytes));
  204. SHA512_Final(digest, &sha);
  205. todo = num_k_bytes - done;
  206. if (todo > SHA512_DIGEST_LENGTH)
  207. todo = SHA512_DIGEST_LENGTH;
  208. memcpy(k_bytes + done, digest, todo);
  209. done += todo;
  210. }
  211. if (!BN_bin2bn(k_bytes, num_k_bytes, out))
  212. goto err;
  213. if (BN_mod(out, out, range, ctx) != 1)
  214. goto err;
  215. ret = 1;
  216. err:
  217. OPENSSL_free(k_bytes);
  218. OPENSSL_cleanse(private_bytes, sizeof(private_bytes));
  219. return ret;
  220. }