2
0

bn_rand.c 7.3 KB

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