bn_prime.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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_lcl.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, prime_t *mods);
  23. static int probable_prime_dh_safe(BIGNUM *rnd, int bits,
  24. const BIGNUM *add, const BIGNUM *rem,
  25. BN_CTX *ctx);
  26. int BN_GENCB_call(BN_GENCB *cb, int a, int b)
  27. {
  28. /* No callback means continue */
  29. if (!cb)
  30. return 1;
  31. switch (cb->ver) {
  32. case 1:
  33. /* Deprecated-style callbacks */
  34. if (!cb->cb.cb_1)
  35. return 1;
  36. cb->cb.cb_1(a, b, cb->arg);
  37. return 1;
  38. case 2:
  39. /* New-style callbacks */
  40. return cb->cb.cb_2(a, b, cb);
  41. default:
  42. break;
  43. }
  44. /* Unrecognised callback type */
  45. return 0;
  46. }
  47. int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
  48. const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
  49. {
  50. BIGNUM *t;
  51. int found = 0;
  52. int i, j, c1 = 0;
  53. BN_CTX *ctx = NULL;
  54. prime_t *mods = NULL;
  55. int checks = BN_prime_checks_for_size(bits);
  56. if (bits < 2) {
  57. /* There are no prime numbers this small. */
  58. BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
  59. return 0;
  60. } else if (bits == 2 && safe) {
  61. /* The smallest safe prime (7) is three bits. */
  62. BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
  63. return 0;
  64. }
  65. mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
  66. if (mods == NULL)
  67. goto err;
  68. ctx = BN_CTX_new();
  69. if (ctx == NULL)
  70. goto err;
  71. BN_CTX_start(ctx);
  72. t = BN_CTX_get(ctx);
  73. if (t == NULL)
  74. goto err;
  75. loop:
  76. /* make a random number and set the top and bottom bits */
  77. if (add == NULL) {
  78. if (!probable_prime(ret, bits, mods))
  79. goto err;
  80. } else {
  81. if (safe) {
  82. if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
  83. goto err;
  84. } else {
  85. if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))
  86. goto err;
  87. }
  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, prime_t *mods)
  243. {
  244. int i;
  245. BN_ULONG delta;
  246. BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
  247. char is_single_word = bits <= BN_BITS2;
  248. again:
  249. /* TODO: Not all primes are private */
  250. if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
  251. return 0;
  252. /* we now have a random number 'rnd' to test. */
  253. for (i = 1; i < NUMPRIMES; i++) {
  254. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  255. if (mod == (BN_ULONG)-1)
  256. return 0;
  257. mods[i] = (prime_t) mod;
  258. }
  259. /*
  260. * If bits is so small that it fits into a single word then we
  261. * additionally don't want to exceed that many bits.
  262. */
  263. if (is_single_word) {
  264. BN_ULONG size_limit;
  265. if (bits == BN_BITS2) {
  266. /*
  267. * Shifting by this much has undefined behaviour so we do it a
  268. * different way
  269. */
  270. size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
  271. } else {
  272. size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
  273. }
  274. if (size_limit < maxdelta)
  275. maxdelta = size_limit;
  276. }
  277. delta = 0;
  278. loop:
  279. if (is_single_word) {
  280. BN_ULONG rnd_word = BN_get_word(rnd);
  281. /*-
  282. * In the case that the candidate prime is a single word then
  283. * we check that:
  284. * 1) It's greater than primes[i] because we shouldn't reject
  285. * 3 as being a prime number because it's a multiple of
  286. * three.
  287. * 2) That it's not a multiple of a known prime. We don't
  288. * check that rnd-1 is also coprime to all the known
  289. * primes because there aren't many small primes where
  290. * that's true.
  291. */
  292. for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
  293. if ((mods[i] + delta) % primes[i] == 0) {
  294. delta += 2;
  295. if (delta > maxdelta)
  296. goto again;
  297. goto loop;
  298. }
  299. }
  300. } else {
  301. for (i = 1; i < NUMPRIMES; i++) {
  302. /*
  303. * check that rnd is not a prime and also that gcd(rnd-1,primes)
  304. * == 1 (except for 2)
  305. */
  306. if (((mods[i] + delta) % primes[i]) <= 1) {
  307. delta += 2;
  308. if (delta > maxdelta)
  309. goto again;
  310. goto loop;
  311. }
  312. }
  313. }
  314. if (!BN_add_word(rnd, delta))
  315. return 0;
  316. if (BN_num_bits(rnd) != bits)
  317. goto again;
  318. bn_check_top(rnd);
  319. return 1;
  320. }
  321. int bn_probable_prime_dh(BIGNUM *rnd, int bits,
  322. const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
  323. {
  324. int i, ret = 0;
  325. BIGNUM *t1;
  326. BN_CTX_start(ctx);
  327. if ((t1 = BN_CTX_get(ctx)) == NULL)
  328. goto err;
  329. if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  330. goto err;
  331. /* we need ((rnd-rem) % add) == 0 */
  332. if (!BN_mod(t1, rnd, add, ctx))
  333. goto err;
  334. if (!BN_sub(rnd, rnd, t1))
  335. goto err;
  336. if (rem == NULL) {
  337. if (!BN_add_word(rnd, 1))
  338. goto err;
  339. } else {
  340. if (!BN_add(rnd, rnd, rem))
  341. goto err;
  342. }
  343. /* we now have a random number 'rand' to test. */
  344. loop:
  345. for (i = 1; i < NUMPRIMES; i++) {
  346. /* check that rnd is a prime */
  347. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  348. if (mod == (BN_ULONG)-1)
  349. goto err;
  350. if (mod <= 1) {
  351. if (!BN_add(rnd, rnd, add))
  352. goto err;
  353. goto loop;
  354. }
  355. }
  356. ret = 1;
  357. err:
  358. BN_CTX_end(ctx);
  359. bn_check_top(rnd);
  360. return ret;
  361. }
  362. static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
  363. const BIGNUM *rem, BN_CTX *ctx)
  364. {
  365. int i, ret = 0;
  366. BIGNUM *t1, *qadd, *q;
  367. bits--;
  368. BN_CTX_start(ctx);
  369. t1 = BN_CTX_get(ctx);
  370. q = BN_CTX_get(ctx);
  371. qadd = BN_CTX_get(ctx);
  372. if (qadd == NULL)
  373. goto err;
  374. if (!BN_rshift1(qadd, padd))
  375. goto err;
  376. if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  377. goto err;
  378. /* we need ((rnd-rem) % add) == 0 */
  379. if (!BN_mod(t1, q, qadd, ctx))
  380. goto err;
  381. if (!BN_sub(q, q, t1))
  382. goto err;
  383. if (rem == NULL) {
  384. if (!BN_add_word(q, 1))
  385. goto err;
  386. } else {
  387. if (!BN_rshift1(t1, rem))
  388. goto err;
  389. if (!BN_add(q, q, t1))
  390. goto err;
  391. }
  392. /* we now have a random number 'rand' to test. */
  393. if (!BN_lshift1(p, q))
  394. goto err;
  395. if (!BN_add_word(p, 1))
  396. goto err;
  397. loop:
  398. for (i = 1; i < NUMPRIMES; i++) {
  399. /* check that p and q are prime */
  400. /*
  401. * check that for p and q gcd(p-1,primes) == 1 (except for 2)
  402. */
  403. BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);
  404. BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);
  405. if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
  406. goto err;
  407. if (pmod == 0 || qmod == 0) {
  408. if (!BN_add(p, p, padd))
  409. goto err;
  410. if (!BN_add(q, q, qadd))
  411. goto err;
  412. goto loop;
  413. }
  414. }
  415. ret = 1;
  416. err:
  417. BN_CTX_end(ctx);
  418. bn_check_top(p);
  419. return ret;
  420. }