bn_prime.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * WARNING: do not edit!
  3. * Generated by crypto/bn/bn_prime.pl
  4. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  5. *
  6. * Licensed under the OpenSSL license (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include "internal/cryptlib.h"
  14. #include "bn_lcl.h"
  15. /*
  16. * The quick sieve algorithm approach to weeding out primes is Philip
  17. * Zimmermann's, as implemented in PGP. I have had a read of his comments
  18. * and implemented my own version.
  19. */
  20. #include "bn_prime.h"
  21. static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
  22. const BIGNUM *a1_odd, int k, BN_CTX *ctx,
  23. BN_MONT_CTX *mont);
  24. static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods);
  25. static int probable_prime_dh_safe(BIGNUM *rnd, int bits,
  26. const BIGNUM *add, const BIGNUM *rem,
  27. BN_CTX *ctx);
  28. int BN_GENCB_call(BN_GENCB *cb, int a, int b)
  29. {
  30. /* No callback means continue */
  31. if (!cb)
  32. return 1;
  33. switch (cb->ver) {
  34. case 1:
  35. /* Deprecated-style callbacks */
  36. if (!cb->cb.cb_1)
  37. return 1;
  38. cb->cb.cb_1(a, b, cb->arg);
  39. return 1;
  40. case 2:
  41. /* New-style callbacks */
  42. return cb->cb.cb_2(a, b, cb);
  43. default:
  44. break;
  45. }
  46. /* Unrecognised callback type */
  47. return 0;
  48. }
  49. int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
  50. const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
  51. {
  52. BIGNUM *t;
  53. int found = 0;
  54. int i, j, c1 = 0;
  55. BN_CTX *ctx = NULL;
  56. prime_t *mods = NULL;
  57. int checks = BN_prime_checks_for_size(bits);
  58. if (bits < 2) {
  59. /* There are no prime numbers this small. */
  60. BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
  61. return 0;
  62. } else if (bits == 2 && safe) {
  63. /* The smallest safe prime (7) is three bits. */
  64. BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
  65. return 0;
  66. }
  67. mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
  68. if (mods == NULL)
  69. goto err;
  70. ctx = BN_CTX_new();
  71. if (ctx == NULL)
  72. goto err;
  73. BN_CTX_start(ctx);
  74. t = BN_CTX_get(ctx);
  75. if (!t)
  76. goto err;
  77. loop:
  78. /* make a random number and set the top and bottom bits */
  79. if (add == NULL) {
  80. if (!probable_prime(ret, bits, mods))
  81. goto err;
  82. } else {
  83. if (safe) {
  84. if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
  85. goto err;
  86. } else {
  87. if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))
  88. goto err;
  89. }
  90. }
  91. if (!BN_GENCB_call(cb, 0, c1++))
  92. /* aborted */
  93. goto err;
  94. if (!safe) {
  95. i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
  96. if (i == -1)
  97. goto err;
  98. if (i == 0)
  99. goto loop;
  100. } else {
  101. /*
  102. * for "safe prime" generation, check that (p-1)/2 is prime. Since a
  103. * prime is odd, We just need to divide by 2
  104. */
  105. if (!BN_rshift1(t, ret))
  106. goto err;
  107. for (i = 0; i < checks; i++) {
  108. j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
  109. if (j == -1)
  110. goto err;
  111. if (j == 0)
  112. goto loop;
  113. j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
  114. if (j == -1)
  115. goto err;
  116. if (j == 0)
  117. goto loop;
  118. if (!BN_GENCB_call(cb, 2, c1 - 1))
  119. goto err;
  120. /* We have a safe prime test pass */
  121. }
  122. }
  123. /* we have a prime :-) */
  124. found = 1;
  125. err:
  126. OPENSSL_free(mods);
  127. if (ctx != NULL)
  128. BN_CTX_end(ctx);
  129. BN_CTX_free(ctx);
  130. bn_check_top(ret);
  131. return found;
  132. }
  133. int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
  134. BN_GENCB *cb)
  135. {
  136. return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
  137. }
  138. int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
  139. int do_trial_division, BN_GENCB *cb)
  140. {
  141. int i, j, ret = -1;
  142. int k;
  143. BN_CTX *ctx = NULL;
  144. BIGNUM *A1, *A1_odd, *check; /* taken from ctx */
  145. BN_MONT_CTX *mont = NULL;
  146. const BIGNUM *A = NULL;
  147. if (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 (!BN_is_odd(a))
  153. /* a is even => a is prime if and only if a == 2 */
  154. return BN_is_word(a, 2);
  155. if (do_trial_division) {
  156. for (i = 1; i < NUMPRIMES; i++) {
  157. BN_ULONG mod = BN_mod_word(a, primes[i]);
  158. if (mod == (BN_ULONG)-1)
  159. goto err;
  160. if (mod == 0)
  161. return 0;
  162. }
  163. if (!BN_GENCB_call(cb, 1, -1))
  164. goto err;
  165. }
  166. if (ctx_passed != NULL)
  167. ctx = ctx_passed;
  168. else if ((ctx = BN_CTX_new()) == NULL)
  169. goto err;
  170. BN_CTX_start(ctx);
  171. /* A := abs(a) */
  172. if (a->neg) {
  173. BIGNUM *t;
  174. if ((t = BN_CTX_get(ctx)) == NULL)
  175. goto err;
  176. if (BN_copy(t, a) == NULL)
  177. goto err;
  178. t->neg = 0;
  179. A = t;
  180. } else
  181. A = a;
  182. A1 = BN_CTX_get(ctx);
  183. A1_odd = BN_CTX_get(ctx);
  184. check = BN_CTX_get(ctx);
  185. if (check == NULL)
  186. goto err;
  187. /* compute A1 := A - 1 */
  188. if (!BN_copy(A1, A))
  189. goto err;
  190. if (!BN_sub_word(A1, 1))
  191. goto err;
  192. if (BN_is_zero(A1)) {
  193. ret = 0;
  194. goto err;
  195. }
  196. /* write A1 as A1_odd * 2^k */
  197. k = 1;
  198. while (!BN_is_bit_set(A1, k))
  199. k++;
  200. if (!BN_rshift(A1_odd, A1, k))
  201. goto err;
  202. /* Montgomery setup for computations mod A */
  203. mont = BN_MONT_CTX_new();
  204. if (mont == NULL)
  205. goto err;
  206. if (!BN_MONT_CTX_set(mont, A, ctx))
  207. goto err;
  208. for (i = 0; i < checks; i++) {
  209. if (!BN_pseudo_rand_range(check, A1))
  210. goto err;
  211. if (!BN_add_word(check, 1))
  212. goto err;
  213. /* now 1 <= check < A */
  214. j = witness(check, A, A1, A1_odd, k, ctx, mont);
  215. if (j == -1)
  216. goto err;
  217. if (j) {
  218. ret = 0;
  219. goto err;
  220. }
  221. if (!BN_GENCB_call(cb, 1, i))
  222. goto err;
  223. }
  224. ret = 1;
  225. err:
  226. if (ctx != NULL) {
  227. BN_CTX_end(ctx);
  228. if (ctx_passed == NULL)
  229. BN_CTX_free(ctx);
  230. }
  231. BN_MONT_CTX_free(mont);
  232. return (ret);
  233. }
  234. static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
  235. const BIGNUM *a1_odd, int k, BN_CTX *ctx,
  236. BN_MONT_CTX *mont)
  237. {
  238. if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */
  239. return -1;
  240. if (BN_is_one(w))
  241. return 0; /* probably prime */
  242. if (BN_cmp(w, a1) == 0)
  243. return 0; /* w == -1 (mod a), 'a' is probably prime */
  244. while (--k) {
  245. if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */
  246. return -1;
  247. if (BN_is_one(w))
  248. return 1; /* 'a' is composite, otherwise a previous 'w'
  249. * would have been == -1 (mod 'a') */
  250. if (BN_cmp(w, a1) == 0)
  251. return 0; /* w == -1 (mod a), 'a' is probably prime */
  252. }
  253. /*
  254. * If we get here, 'w' is the (a-1)/2-th power of the original 'w', and
  255. * it is neither -1 nor +1 -- so 'a' cannot be prime
  256. */
  257. bn_check_top(w);
  258. return 1;
  259. }
  260. static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)
  261. {
  262. int i;
  263. BN_ULONG delta;
  264. BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
  265. char is_single_word = bits <= BN_BITS2;
  266. again:
  267. if (!BN_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
  268. return (0);
  269. /* we now have a random number 'rnd' to test. */
  270. for (i = 1; i < NUMPRIMES; i++) {
  271. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  272. if (mod == (BN_ULONG)-1)
  273. return 0;
  274. mods[i] = (prime_t) mod;
  275. }
  276. /*
  277. * If bits is so small that it fits into a single word then we
  278. * additionally don't want to exceed that many bits.
  279. */
  280. if (is_single_word) {
  281. BN_ULONG size_limit;
  282. if (bits == BN_BITS2) {
  283. /*
  284. * Shifting by this much has undefined behaviour so we do it a
  285. * different way
  286. */
  287. size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
  288. } else {
  289. size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
  290. }
  291. if (size_limit < maxdelta)
  292. maxdelta = size_limit;
  293. }
  294. delta = 0;
  295. loop:
  296. if (is_single_word) {
  297. BN_ULONG rnd_word = BN_get_word(rnd);
  298. /*-
  299. * In the case that the candidate prime is a single word then
  300. * we check that:
  301. * 1) It's greater than primes[i] because we shouldn't reject
  302. * 3 as being a prime number because it's a multiple of
  303. * three.
  304. * 2) That it's not a multiple of a known prime. We don't
  305. * check that rnd-1 is also coprime to all the known
  306. * primes because there aren't many small primes where
  307. * that's true.
  308. */
  309. for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
  310. if ((mods[i] + delta) % primes[i] == 0) {
  311. delta += 2;
  312. if (delta > maxdelta)
  313. goto again;
  314. goto loop;
  315. }
  316. }
  317. } else {
  318. for (i = 1; i < NUMPRIMES; i++) {
  319. /*
  320. * check that rnd is not a prime and also that gcd(rnd-1,primes)
  321. * == 1 (except for 2)
  322. */
  323. if (((mods[i] + delta) % primes[i]) <= 1) {
  324. delta += 2;
  325. if (delta > maxdelta)
  326. goto again;
  327. goto loop;
  328. }
  329. }
  330. }
  331. if (!BN_add_word(rnd, delta))
  332. return (0);
  333. if (BN_num_bits(rnd) != bits)
  334. goto again;
  335. bn_check_top(rnd);
  336. return (1);
  337. }
  338. int bn_probable_prime_dh(BIGNUM *rnd, int bits,
  339. const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
  340. {
  341. int i, ret = 0;
  342. BIGNUM *t1;
  343. BN_CTX_start(ctx);
  344. if ((t1 = BN_CTX_get(ctx)) == NULL)
  345. goto err;
  346. if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  347. goto err;
  348. /* we need ((rnd-rem) % add) == 0 */
  349. if (!BN_mod(t1, rnd, add, ctx))
  350. goto err;
  351. if (!BN_sub(rnd, rnd, t1))
  352. goto err;
  353. if (rem == NULL) {
  354. if (!BN_add_word(rnd, 1))
  355. goto err;
  356. } else {
  357. if (!BN_add(rnd, rnd, rem))
  358. goto err;
  359. }
  360. /* we now have a random number 'rand' to test. */
  361. loop:
  362. for (i = 1; i < NUMPRIMES; i++) {
  363. /* check that rnd is a prime */
  364. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  365. if (mod == (BN_ULONG)-1)
  366. goto err;
  367. if (mod <= 1) {
  368. if (!BN_add(rnd, rnd, add))
  369. goto err;
  370. goto loop;
  371. }
  372. }
  373. ret = 1;
  374. err:
  375. BN_CTX_end(ctx);
  376. bn_check_top(rnd);
  377. return (ret);
  378. }
  379. static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
  380. const BIGNUM *rem, BN_CTX *ctx)
  381. {
  382. int i, ret = 0;
  383. BIGNUM *t1, *qadd, *q;
  384. bits--;
  385. BN_CTX_start(ctx);
  386. t1 = BN_CTX_get(ctx);
  387. q = BN_CTX_get(ctx);
  388. qadd = BN_CTX_get(ctx);
  389. if (qadd == NULL)
  390. goto err;
  391. if (!BN_rshift1(qadd, padd))
  392. goto err;
  393. if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  394. goto err;
  395. /* we need ((rnd-rem) % add) == 0 */
  396. if (!BN_mod(t1, q, qadd, ctx))
  397. goto err;
  398. if (!BN_sub(q, q, t1))
  399. goto err;
  400. if (rem == NULL) {
  401. if (!BN_add_word(q, 1))
  402. goto err;
  403. } else {
  404. if (!BN_rshift1(t1, rem))
  405. goto err;
  406. if (!BN_add(q, q, t1))
  407. goto err;
  408. }
  409. /* we now have a random number 'rand' to test. */
  410. if (!BN_lshift1(p, q))
  411. goto err;
  412. if (!BN_add_word(p, 1))
  413. goto err;
  414. loop:
  415. for (i = 1; i < NUMPRIMES; i++) {
  416. /* check that p and q are prime */
  417. /*
  418. * check that for p and q gcd(p-1,primes) == 1 (except for 2)
  419. */
  420. BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);
  421. BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);
  422. if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
  423. goto err;
  424. if (pmod == 0 || qmod == 0) {
  425. if (!BN_add(p, p, padd))
  426. goto err;
  427. if (!BN_add(q, q, qadd))
  428. goto err;
  429. goto loop;
  430. }
  431. }
  432. ret = 1;
  433. err:
  434. BN_CTX_end(ctx);
  435. bn_check_top(p);
  436. return (ret);
  437. }