bn_prime.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright 1995-2020 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. /*
  14. * The quick sieve algorithm approach to weeding out primes is Philip
  15. * Zimmermann's, as implemented in PGP. I have had a read of his comments
  16. * and implemented my own version.
  17. */
  18. #include "bn_prime.h"
  19. static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
  20. const BIGNUM *a1_odd, int k, BN_CTX *ctx,
  21. BN_MONT_CTX *mont);
  22. static int probable_prime(BIGNUM *rnd, int bits, int safe, prime_t *mods);
  23. static int probable_prime_dh(BIGNUM *rnd, int bits, int safe, prime_t *mods,
  24. const BIGNUM *add, const BIGNUM *rem,
  25. BN_CTX *ctx);
  26. #define square(x) ((BN_ULONG)(x) * (BN_ULONG)(x))
  27. int BN_GENCB_call(BN_GENCB *cb, int a, int b)
  28. {
  29. /* No callback means continue */
  30. if (!cb)
  31. return 1;
  32. switch (cb->ver) {
  33. case 1:
  34. /* Deprecated-style callbacks */
  35. if (!cb->cb.cb_1)
  36. return 1;
  37. cb->cb.cb_1(a, b, cb->arg);
  38. return 1;
  39. case 2:
  40. /* New-style callbacks */
  41. return cb->cb.cb_2(a, b, cb);
  42. default:
  43. break;
  44. }
  45. /* Unrecognised callback type */
  46. return 0;
  47. }
  48. int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
  49. const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
  50. {
  51. BIGNUM *t;
  52. int found = 0;
  53. int i, j, c1 = 0;
  54. BN_CTX *ctx = NULL;
  55. prime_t *mods = NULL;
  56. int checks = BN_prime_checks_for_size(bits);
  57. if (bits < 2) {
  58. /* There are no prime numbers this small. */
  59. BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
  60. return 0;
  61. } else if (add == NULL && safe && bits < 6 && bits != 3) {
  62. /*
  63. * The smallest safe prime (7) is three bits.
  64. * But the following two safe primes with less than 6 bits (11, 23)
  65. * are unreachable for BN_rand with BN_RAND_TOP_TWO.
  66. */
  67. BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
  68. return 0;
  69. }
  70. mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
  71. if (mods == NULL)
  72. goto err;
  73. ctx = BN_CTX_new();
  74. if (ctx == NULL)
  75. goto err;
  76. BN_CTX_start(ctx);
  77. t = BN_CTX_get(ctx);
  78. if (t == NULL)
  79. goto err;
  80. loop:
  81. /* make a random number and set the top and bottom bits */
  82. if (add == NULL) {
  83. if (!probable_prime(ret, bits, safe, mods))
  84. goto err;
  85. } else {
  86. if (!probable_prime_dh(ret, bits, safe, mods, add, rem, ctx))
  87. goto err;
  88. }
  89. if (!BN_GENCB_call(cb, 0, c1++))
  90. /* aborted */
  91. goto err;
  92. if (!safe) {
  93. i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
  94. if (i == -1)
  95. goto err;
  96. if (i == 0)
  97. goto loop;
  98. } else {
  99. /*
  100. * for "safe prime" generation, check that (p-1)/2 is prime. Since a
  101. * prime is odd, We just need to divide by 2
  102. */
  103. if (!BN_rshift1(t, ret))
  104. goto err;
  105. for (i = 0; i < checks; i++) {
  106. j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
  107. if (j == -1)
  108. goto err;
  109. if (j == 0)
  110. goto loop;
  111. j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
  112. if (j == -1)
  113. goto err;
  114. if (j == 0)
  115. goto loop;
  116. if (!BN_GENCB_call(cb, 2, c1 - 1))
  117. goto err;
  118. /* We have a safe prime test pass */
  119. }
  120. }
  121. /* we have a prime :-) */
  122. found = 1;
  123. err:
  124. OPENSSL_free(mods);
  125. BN_CTX_end(ctx);
  126. BN_CTX_free(ctx);
  127. bn_check_top(ret);
  128. return found;
  129. }
  130. int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
  131. BN_GENCB *cb)
  132. {
  133. return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
  134. }
  135. int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
  136. int do_trial_division, BN_GENCB *cb)
  137. {
  138. int i, j, ret = -1;
  139. int k;
  140. BN_CTX *ctx = NULL;
  141. BIGNUM *A1, *A1_odd, *A3, *check; /* taken from ctx */
  142. BN_MONT_CTX *mont = NULL;
  143. /* Take care of the really small primes 2 & 3 */
  144. if (BN_is_word(a, 2) || BN_is_word(a, 3))
  145. return 1;
  146. /* Check odd and bigger than 1 */
  147. if (!BN_is_odd(a) || BN_cmp(a, BN_value_one()) <= 0)
  148. return 0;
  149. if (checks == BN_prime_checks)
  150. checks = BN_prime_checks_for_size(BN_num_bits(a));
  151. /* first look for small factors */
  152. if (do_trial_division) {
  153. for (i = 1; i < NUMPRIMES; i++) {
  154. BN_ULONG mod = BN_mod_word(a, primes[i]);
  155. if (mod == (BN_ULONG)-1)
  156. goto err;
  157. if (mod == 0)
  158. return BN_is_word(a, primes[i]);
  159. }
  160. if (!BN_GENCB_call(cb, 1, -1))
  161. goto err;
  162. }
  163. if (ctx_passed != NULL)
  164. ctx = ctx_passed;
  165. else if ((ctx = BN_CTX_new()) == NULL)
  166. goto err;
  167. BN_CTX_start(ctx);
  168. A1 = BN_CTX_get(ctx);
  169. A3 = BN_CTX_get(ctx);
  170. A1_odd = BN_CTX_get(ctx);
  171. check = BN_CTX_get(ctx);
  172. if (check == NULL)
  173. goto err;
  174. /* compute A1 := a - 1 */
  175. if (!BN_copy(A1, a) || !BN_sub_word(A1, 1))
  176. goto err;
  177. /* compute A3 := a - 3 */
  178. if (!BN_copy(A3, a) || !BN_sub_word(A3, 3))
  179. goto err;
  180. /* write A1 as A1_odd * 2^k */
  181. k = 1;
  182. while (!BN_is_bit_set(A1, k))
  183. k++;
  184. if (!BN_rshift(A1_odd, A1, k))
  185. goto err;
  186. /* Montgomery setup for computations mod a */
  187. mont = BN_MONT_CTX_new();
  188. if (mont == NULL)
  189. goto err;
  190. if (!BN_MONT_CTX_set(mont, a, ctx))
  191. goto err;
  192. for (i = 0; i < checks; i++) {
  193. /* 1 < check < a-1 */
  194. if (!BN_priv_rand_range(check, A3) || !BN_add_word(check, 2))
  195. goto err;
  196. j = witness(check, a, A1, A1_odd, k, ctx, mont);
  197. if (j == -1)
  198. goto err;
  199. if (j) {
  200. ret = 0;
  201. goto err;
  202. }
  203. if (!BN_GENCB_call(cb, 1, i))
  204. goto err;
  205. }
  206. ret = 1;
  207. err:
  208. if (ctx != NULL) {
  209. BN_CTX_end(ctx);
  210. if (ctx_passed == NULL)
  211. BN_CTX_free(ctx);
  212. }
  213. BN_MONT_CTX_free(mont);
  214. return ret;
  215. }
  216. static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
  217. const BIGNUM *a1_odd, int k, BN_CTX *ctx,
  218. BN_MONT_CTX *mont)
  219. {
  220. if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */
  221. return -1;
  222. if (BN_is_one(w))
  223. return 0; /* probably prime */
  224. if (BN_cmp(w, a1) == 0)
  225. return 0; /* w == -1 (mod a), 'a' is probably prime */
  226. while (--k) {
  227. if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */
  228. return -1;
  229. if (BN_is_one(w))
  230. return 1; /* 'a' is composite, otherwise a previous 'w'
  231. * would have been == -1 (mod 'a') */
  232. if (BN_cmp(w, a1) == 0)
  233. return 0; /* w == -1 (mod a), 'a' is probably prime */
  234. }
  235. /*
  236. * If we get here, 'w' is the (a-1)/2-th power of the original 'w', and
  237. * it is neither -1 nor +1 -- so 'a' cannot be prime
  238. */
  239. bn_check_top(w);
  240. return 1;
  241. }
  242. static int probable_prime(BIGNUM *rnd, int bits, int safe, prime_t *mods)
  243. {
  244. int i;
  245. BN_ULONG delta;
  246. BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
  247. again:
  248. /* TODO: Not all primes are private */
  249. if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
  250. return 0;
  251. if (safe && !BN_set_bit(rnd, 1))
  252. return 0;
  253. /* we now have a random number 'rnd' to test. */
  254. for (i = 1; i < NUMPRIMES; i++) {
  255. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  256. if (mod == (BN_ULONG)-1)
  257. return 0;
  258. mods[i] = (prime_t) mod;
  259. }
  260. delta = 0;
  261. loop:
  262. for (i = 1; i < NUMPRIMES; i++) {
  263. /*
  264. * check that rnd is a prime and also that
  265. * gcd(rnd-1,primes) == 1 (except for 2)
  266. * do the second check only if we are interested in safe primes
  267. * in the case that the candidate prime is a single word then
  268. * we check only the primes up to sqrt(rnd)
  269. */
  270. if (bits <= 31 && delta <= 0x7fffffff
  271. && square(primes[i]) > BN_get_word(rnd) + delta)
  272. break;
  273. if (safe ? (mods[i] + delta) % primes[i] <= 1
  274. : (mods[i] + delta) % primes[i] == 0) {
  275. delta += safe ? 4 : 2;
  276. if (delta > maxdelta)
  277. goto again;
  278. goto loop;
  279. }
  280. }
  281. if (!BN_add_word(rnd, delta))
  282. return 0;
  283. if (BN_num_bits(rnd) != bits)
  284. goto again;
  285. bn_check_top(rnd);
  286. return 1;
  287. }
  288. static int probable_prime_dh(BIGNUM *rnd, int bits, int safe, prime_t *mods,
  289. const BIGNUM *add, const BIGNUM *rem,
  290. BN_CTX *ctx)
  291. {
  292. int i, ret = 0;
  293. BIGNUM *t1;
  294. BN_ULONG delta;
  295. BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
  296. BN_CTX_start(ctx);
  297. if ((t1 = BN_CTX_get(ctx)) == NULL)
  298. goto err;
  299. if (maxdelta > BN_MASK2 - BN_get_word(add))
  300. maxdelta = BN_MASK2 - BN_get_word(add);
  301. again:
  302. if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  303. goto err;
  304. /* we need ((rnd-rem) % add) == 0 */
  305. if (!BN_mod(t1, rnd, add, ctx))
  306. goto err;
  307. if (!BN_sub(rnd, rnd, t1))
  308. goto err;
  309. if (rem == NULL) {
  310. if (!BN_add_word(rnd, safe ? 3u : 1u))
  311. goto err;
  312. } else {
  313. if (!BN_add(rnd, rnd, rem))
  314. goto err;
  315. }
  316. if (BN_num_bits(rnd) < bits
  317. || BN_get_word(rnd) < (safe ? 5u : 3u)) {
  318. if (!BN_add(rnd, rnd, add))
  319. goto err;
  320. }
  321. /* we now have a random number 'rnd' to test. */
  322. for (i = 1; i < NUMPRIMES; i++) {
  323. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  324. if (mod == (BN_ULONG)-1)
  325. goto err;
  326. mods[i] = (prime_t) mod;
  327. }
  328. delta = 0;
  329. loop:
  330. for (i = 1; i < NUMPRIMES; i++) {
  331. /* check that rnd is a prime */
  332. if (bits <= 31 && delta <= 0x7fffffff
  333. && square(primes[i]) > BN_get_word(rnd) + delta)
  334. break;
  335. /* rnd mod p == 1 implies q = (rnd-1)/2 is divisible by p */
  336. if (safe ? (mods[i] + delta) % primes[i] <= 1
  337. : (mods[i] + delta) % primes[i] == 0) {
  338. delta += BN_get_word(add);
  339. if (delta > maxdelta)
  340. goto again;
  341. goto loop;
  342. }
  343. }
  344. if (!BN_add_word(rnd, delta))
  345. goto err;
  346. ret = 1;
  347. err:
  348. BN_CTX_end(ctx);
  349. bn_check_top(rnd);
  350. return ret;
  351. }