bn_prime.c 13 KB

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