bn_prime.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright 1995-2017 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. if (ctx != NULL)
  126. BN_CTX_end(ctx);
  127. BN_CTX_free(ctx);
  128. bn_check_top(ret);
  129. return found;
  130. }
  131. int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
  132. BN_GENCB *cb)
  133. {
  134. return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
  135. }
  136. int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
  137. int do_trial_division, BN_GENCB *cb)
  138. {
  139. int i, j, ret = -1;
  140. int k;
  141. BN_CTX *ctx = NULL;
  142. BIGNUM *A1, *A1_odd, *check; /* taken from ctx */
  143. BN_MONT_CTX *mont = NULL;
  144. if (BN_cmp(a, BN_value_one()) <= 0)
  145. return 0;
  146. if (checks == BN_prime_checks)
  147. checks = BN_prime_checks_for_size(BN_num_bits(a));
  148. /* first look for small factors */
  149. if (!BN_is_odd(a))
  150. /* a is even => a is prime if and only if a == 2 */
  151. return BN_is_word(a, 2);
  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. A1_odd = BN_CTX_get(ctx);
  170. check = BN_CTX_get(ctx);
  171. if (check == NULL)
  172. goto err;
  173. /* compute A1 := a - 1 */
  174. if (!BN_copy(A1, a))
  175. goto err;
  176. if (!BN_sub_word(A1, 1))
  177. goto err;
  178. if (BN_is_zero(A1)) {
  179. ret = 0;
  180. goto err;
  181. }
  182. /* write A1 as A1_odd * 2^k */
  183. k = 1;
  184. while (!BN_is_bit_set(A1, k))
  185. k++;
  186. if (!BN_rshift(A1_odd, A1, k))
  187. goto err;
  188. /* Montgomery setup for computations mod a */
  189. mont = BN_MONT_CTX_new();
  190. if (mont == NULL)
  191. goto err;
  192. if (!BN_MONT_CTX_set(mont, a, ctx))
  193. goto err;
  194. for (i = 0; i < checks; i++) {
  195. if (!BN_rand_range(check, A1))
  196. goto err;
  197. if (!BN_add_word(check, 1))
  198. goto err;
  199. /* now 1 <= check < a */
  200. j = witness(check, a, A1, A1_odd, k, ctx, mont);
  201. if (j == -1)
  202. goto err;
  203. if (j) {
  204. ret = 0;
  205. goto err;
  206. }
  207. if (!BN_GENCB_call(cb, 1, i))
  208. goto err;
  209. }
  210. ret = 1;
  211. err:
  212. if (ctx != NULL) {
  213. BN_CTX_end(ctx);
  214. if (ctx_passed == NULL)
  215. BN_CTX_free(ctx);
  216. }
  217. BN_MONT_CTX_free(mont);
  218. return (ret);
  219. }
  220. static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
  221. const BIGNUM *a1_odd, int k, BN_CTX *ctx,
  222. BN_MONT_CTX *mont)
  223. {
  224. if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */
  225. return -1;
  226. if (BN_is_one(w))
  227. return 0; /* probably prime */
  228. if (BN_cmp(w, a1) == 0)
  229. return 0; /* w == -1 (mod a), 'a' is probably prime */
  230. while (--k) {
  231. if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */
  232. return -1;
  233. if (BN_is_one(w))
  234. return 1; /* 'a' is composite, otherwise a previous 'w'
  235. * would have been == -1 (mod 'a') */
  236. if (BN_cmp(w, a1) == 0)
  237. return 0; /* w == -1 (mod a), 'a' is probably prime */
  238. }
  239. /*
  240. * If we get here, 'w' is the (a-1)/2-th power of the original 'w', and
  241. * it is neither -1 nor +1 -- so 'a' cannot be prime
  242. */
  243. bn_check_top(w);
  244. return 1;
  245. }
  246. static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)
  247. {
  248. int i;
  249. BN_ULONG delta;
  250. BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
  251. char is_single_word = bits <= BN_BITS2;
  252. again:
  253. if (!BN_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
  254. return (0);
  255. /* we now have a random number 'rnd' to test. */
  256. for (i = 1; i < NUMPRIMES; i++) {
  257. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  258. if (mod == (BN_ULONG)-1)
  259. return 0;
  260. mods[i] = (prime_t) mod;
  261. }
  262. /*
  263. * If bits is so small that it fits into a single word then we
  264. * additionally don't want to exceed that many bits.
  265. */
  266. if (is_single_word) {
  267. BN_ULONG size_limit;
  268. if (bits == BN_BITS2) {
  269. /*
  270. * Shifting by this much has undefined behaviour so we do it a
  271. * different way
  272. */
  273. size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
  274. } else {
  275. size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
  276. }
  277. if (size_limit < maxdelta)
  278. maxdelta = size_limit;
  279. }
  280. delta = 0;
  281. loop:
  282. if (is_single_word) {
  283. BN_ULONG rnd_word = BN_get_word(rnd);
  284. /*-
  285. * In the case that the candidate prime is a single word then
  286. * we check that:
  287. * 1) It's greater than primes[i] because we shouldn't reject
  288. * 3 as being a prime number because it's a multiple of
  289. * three.
  290. * 2) That it's not a multiple of a known prime. We don't
  291. * check that rnd-1 is also coprime to all the known
  292. * primes because there aren't many small primes where
  293. * that's true.
  294. */
  295. for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
  296. if ((mods[i] + delta) % primes[i] == 0) {
  297. delta += 2;
  298. if (delta > maxdelta)
  299. goto again;
  300. goto loop;
  301. }
  302. }
  303. } else {
  304. for (i = 1; i < NUMPRIMES; i++) {
  305. /*
  306. * check that rnd is not a prime and also that gcd(rnd-1,primes)
  307. * == 1 (except for 2)
  308. */
  309. if (((mods[i] + delta) % primes[i]) <= 1) {
  310. delta += 2;
  311. if (delta > maxdelta)
  312. goto again;
  313. goto loop;
  314. }
  315. }
  316. }
  317. if (!BN_add_word(rnd, delta))
  318. return (0);
  319. if (BN_num_bits(rnd) != bits)
  320. goto again;
  321. bn_check_top(rnd);
  322. return (1);
  323. }
  324. int bn_probable_prime_dh(BIGNUM *rnd, int bits,
  325. const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
  326. {
  327. int i, ret = 0;
  328. BIGNUM *t1;
  329. BN_CTX_start(ctx);
  330. if ((t1 = BN_CTX_get(ctx)) == NULL)
  331. goto err;
  332. if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  333. goto err;
  334. /* we need ((rnd-rem) % add) == 0 */
  335. if (!BN_mod(t1, rnd, add, ctx))
  336. goto err;
  337. if (!BN_sub(rnd, rnd, t1))
  338. goto err;
  339. if (rem == NULL) {
  340. if (!BN_add_word(rnd, 1))
  341. goto err;
  342. } else {
  343. if (!BN_add(rnd, rnd, rem))
  344. goto err;
  345. }
  346. /* we now have a random number 'rand' to test. */
  347. loop:
  348. for (i = 1; i < NUMPRIMES; i++) {
  349. /* check that rnd is a prime */
  350. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  351. if (mod == (BN_ULONG)-1)
  352. goto err;
  353. if (mod <= 1) {
  354. if (!BN_add(rnd, rnd, add))
  355. goto err;
  356. goto loop;
  357. }
  358. }
  359. ret = 1;
  360. err:
  361. BN_CTX_end(ctx);
  362. bn_check_top(rnd);
  363. return (ret);
  364. }
  365. static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
  366. const BIGNUM *rem, BN_CTX *ctx)
  367. {
  368. int i, ret = 0;
  369. BIGNUM *t1, *qadd, *q;
  370. bits--;
  371. BN_CTX_start(ctx);
  372. t1 = BN_CTX_get(ctx);
  373. q = BN_CTX_get(ctx);
  374. qadd = BN_CTX_get(ctx);
  375. if (qadd == NULL)
  376. goto err;
  377. if (!BN_rshift1(qadd, padd))
  378. goto err;
  379. if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  380. goto err;
  381. /* we need ((rnd-rem) % add) == 0 */
  382. if (!BN_mod(t1, q, qadd, ctx))
  383. goto err;
  384. if (!BN_sub(q, q, t1))
  385. goto err;
  386. if (rem == NULL) {
  387. if (!BN_add_word(q, 1))
  388. goto err;
  389. } else {
  390. if (!BN_rshift1(t1, rem))
  391. goto err;
  392. if (!BN_add(q, q, t1))
  393. goto err;
  394. }
  395. /* we now have a random number 'rand' to test. */
  396. if (!BN_lshift1(p, q))
  397. goto err;
  398. if (!BN_add_word(p, 1))
  399. goto err;
  400. loop:
  401. for (i = 1; i < NUMPRIMES; i++) {
  402. /* check that p and q are prime */
  403. /*
  404. * check that for p and q gcd(p-1,primes) == 1 (except for 2)
  405. */
  406. BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);
  407. BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);
  408. if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
  409. goto err;
  410. if (pmod == 0 || qmod == 0) {
  411. if (!BN_add(p, p, padd))
  412. goto err;
  413. if (!BN_add(q, q, qadd))
  414. goto err;
  415. goto loop;
  416. }
  417. }
  418. ret = 1;
  419. err:
  420. BN_CTX_end(ctx);
  421. bn_check_top(p);
  422. return (ret);
  423. }