bn_prime.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. static const int prime_offsets[480] = {
  29. 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
  30. 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163,
  31. 167, 169, 173, 179, 181, 191, 193, 197, 199, 211, 221, 223, 227, 229,
  32. 233, 239, 241, 247, 251, 257, 263, 269, 271, 277, 281, 283, 289, 293,
  33. 299, 307, 311, 313, 317, 323, 331, 337, 347, 349, 353, 359, 361, 367,
  34. 373, 377, 379, 383, 389, 391, 397, 401, 403, 409, 419, 421, 431, 433,
  35. 437, 439, 443, 449, 457, 461, 463, 467, 479, 481, 487, 491, 493, 499,
  36. 503, 509, 521, 523, 527, 529, 533, 541, 547, 551, 557, 559, 563, 569,
  37. 571, 577, 587, 589, 593, 599, 601, 607, 611, 613, 617, 619, 629, 631,
  38. 641, 643, 647, 653, 659, 661, 667, 673, 677, 683, 689, 691, 697, 701,
  39. 703, 709, 713, 719, 727, 731, 733, 739, 743, 751, 757, 761, 767, 769,
  40. 773, 779, 787, 793, 797, 799, 809, 811, 817, 821, 823, 827, 829, 839,
  41. 841, 851, 853, 857, 859, 863, 871, 877, 881, 883, 887, 893, 899, 901,
  42. 907, 911, 919, 923, 929, 937, 941, 943, 947, 949, 953, 961, 967, 971,
  43. 977, 983, 989, 991, 997, 1003, 1007, 1009, 1013, 1019, 1021, 1027, 1031,
  44. 1033, 1037, 1039, 1049, 1051, 1061, 1063, 1069, 1073, 1079, 1081, 1087,
  45. 1091, 1093, 1097, 1103, 1109, 1117, 1121, 1123, 1129, 1139, 1147, 1151,
  46. 1153, 1157, 1159, 1163, 1171, 1181, 1187, 1189, 1193, 1201, 1207, 1213,
  47. 1217, 1219, 1223, 1229, 1231, 1237, 1241, 1247, 1249, 1259, 1261, 1271,
  48. 1273, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1313, 1319,
  49. 1321, 1327, 1333, 1339, 1343, 1349, 1357, 1361, 1363, 1367, 1369, 1373,
  50. 1381, 1387, 1391, 1399, 1403, 1409, 1411, 1417, 1423, 1427, 1429, 1433,
  51. 1439, 1447, 1451, 1453, 1457, 1459, 1469, 1471, 1481, 1483, 1487, 1489,
  52. 1493, 1499, 1501, 1511, 1513, 1517, 1523, 1531, 1537, 1541, 1543, 1549,
  53. 1553, 1559, 1567, 1571, 1577, 1579, 1583, 1591, 1597, 1601, 1607, 1609,
  54. 1613, 1619, 1621, 1627, 1633, 1637, 1643, 1649, 1651, 1657, 1663, 1667,
  55. 1669, 1679, 1681, 1691, 1693, 1697, 1699, 1703, 1709, 1711, 1717, 1721,
  56. 1723, 1733, 1739, 1741, 1747, 1751, 1753, 1759, 1763, 1769, 1777, 1781,
  57. 1783, 1787, 1789, 1801, 1807, 1811, 1817, 1819, 1823, 1829, 1831, 1843,
  58. 1847, 1849, 1853, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1891, 1901,
  59. 1907, 1909, 1913, 1919, 1921, 1927, 1931, 1933, 1937, 1943, 1949, 1951,
  60. 1957, 1961, 1963, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017,
  61. 2021, 2027, 2029, 2033, 2039, 2041, 2047, 2053, 2059, 2063, 2069, 2071,
  62. 2077, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2117, 2119, 2129, 2131,
  63. 2137, 2141, 2143, 2147, 2153, 2159, 2161, 2171, 2173, 2179, 2183, 2197,
  64. 2201, 2203, 2207, 2209, 2213, 2221, 2227, 2231, 2237, 2239, 2243, 2249,
  65. 2251, 2257, 2263, 2267, 2269, 2273, 2279, 2281, 2287, 2291, 2293, 2297,
  66. 2309, 2311
  67. };
  68. static const int prime_offset_count = 480;
  69. static const int prime_multiplier = 2310;
  70. static const int prime_multiplier_bits = 11; /* 2^|prime_multiplier_bits| <=
  71. * |prime_multiplier| */
  72. static const int first_prime_index = 5;
  73. int BN_GENCB_call(BN_GENCB *cb, int a, int b)
  74. {
  75. /* No callback means continue */
  76. if (!cb)
  77. return 1;
  78. switch (cb->ver) {
  79. case 1:
  80. /* Deprecated-style callbacks */
  81. if (!cb->cb.cb_1)
  82. return 1;
  83. cb->cb.cb_1(a, b, cb->arg);
  84. return 1;
  85. case 2:
  86. /* New-style callbacks */
  87. return cb->cb.cb_2(a, b, cb);
  88. default:
  89. break;
  90. }
  91. /* Unrecognised callback type */
  92. return 0;
  93. }
  94. int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
  95. const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
  96. {
  97. BIGNUM *t;
  98. int found = 0;
  99. int i, j, c1 = 0;
  100. BN_CTX *ctx = NULL;
  101. prime_t *mods = NULL;
  102. int checks = BN_prime_checks_for_size(bits);
  103. if (bits < 2) {
  104. /* There are no prime numbers this small. */
  105. BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
  106. return 0;
  107. } else if (bits == 2 && safe) {
  108. /* The smallest safe prime (7) is three bits. */
  109. BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
  110. return 0;
  111. }
  112. mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
  113. if (mods == NULL)
  114. goto err;
  115. ctx = BN_CTX_new();
  116. if (ctx == NULL)
  117. goto err;
  118. BN_CTX_start(ctx);
  119. t = BN_CTX_get(ctx);
  120. if (!t)
  121. goto err;
  122. loop:
  123. /* make a random number and set the top and bottom bits */
  124. if (add == NULL) {
  125. if (!probable_prime(ret, bits, mods))
  126. goto err;
  127. } else {
  128. if (safe) {
  129. if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
  130. goto err;
  131. } else {
  132. if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))
  133. goto err;
  134. }
  135. }
  136. if (!BN_GENCB_call(cb, 0, c1++))
  137. /* aborted */
  138. goto err;
  139. if (!safe) {
  140. i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
  141. if (i == -1)
  142. goto err;
  143. if (i == 0)
  144. goto loop;
  145. } else {
  146. /*
  147. * for "safe prime" generation, check that (p-1)/2 is prime. Since a
  148. * prime is odd, We just need to divide by 2
  149. */
  150. if (!BN_rshift1(t, ret))
  151. goto err;
  152. for (i = 0; i < checks; i++) {
  153. j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
  154. if (j == -1)
  155. goto err;
  156. if (j == 0)
  157. goto loop;
  158. j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
  159. if (j == -1)
  160. goto err;
  161. if (j == 0)
  162. goto loop;
  163. if (!BN_GENCB_call(cb, 2, c1 - 1))
  164. goto err;
  165. /* We have a safe prime test pass */
  166. }
  167. }
  168. /* we have a prime :-) */
  169. found = 1;
  170. err:
  171. OPENSSL_free(mods);
  172. if (ctx != NULL)
  173. BN_CTX_end(ctx);
  174. BN_CTX_free(ctx);
  175. bn_check_top(ret);
  176. return found;
  177. }
  178. int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
  179. BN_GENCB *cb)
  180. {
  181. return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
  182. }
  183. int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
  184. int do_trial_division, BN_GENCB *cb)
  185. {
  186. int i, j, ret = -1;
  187. int k;
  188. BN_CTX *ctx = NULL;
  189. BIGNUM *A1, *A1_odd, *check; /* taken from ctx */
  190. BN_MONT_CTX *mont = NULL;
  191. const BIGNUM *A = NULL;
  192. if (BN_cmp(a, BN_value_one()) <= 0)
  193. return 0;
  194. if (checks == BN_prime_checks)
  195. checks = BN_prime_checks_for_size(BN_num_bits(a));
  196. /* first look for small factors */
  197. if (!BN_is_odd(a))
  198. /* a is even => a is prime if and only if a == 2 */
  199. return BN_is_word(a, 2);
  200. if (do_trial_division) {
  201. for (i = 1; i < NUMPRIMES; i++) {
  202. BN_ULONG mod = BN_mod_word(a, primes[i]);
  203. if (mod == (BN_ULONG)-1)
  204. goto err;
  205. if (mod == 0)
  206. return 0;
  207. }
  208. if (!BN_GENCB_call(cb, 1, -1))
  209. goto err;
  210. }
  211. if (ctx_passed != NULL)
  212. ctx = ctx_passed;
  213. else if ((ctx = BN_CTX_new()) == NULL)
  214. goto err;
  215. BN_CTX_start(ctx);
  216. /* A := abs(a) */
  217. if (a->neg) {
  218. BIGNUM *t;
  219. if ((t = BN_CTX_get(ctx)) == NULL)
  220. goto err;
  221. BN_copy(t, a);
  222. t->neg = 0;
  223. A = t;
  224. } else
  225. A = a;
  226. A1 = BN_CTX_get(ctx);
  227. A1_odd = BN_CTX_get(ctx);
  228. check = BN_CTX_get(ctx);
  229. if (check == NULL)
  230. goto err;
  231. /* compute A1 := A - 1 */
  232. if (!BN_copy(A1, A))
  233. goto err;
  234. if (!BN_sub_word(A1, 1))
  235. goto err;
  236. if (BN_is_zero(A1)) {
  237. ret = 0;
  238. goto err;
  239. }
  240. /* write A1 as A1_odd * 2^k */
  241. k = 1;
  242. while (!BN_is_bit_set(A1, k))
  243. k++;
  244. if (!BN_rshift(A1_odd, A1, k))
  245. goto err;
  246. /* Montgomery setup for computations mod A */
  247. mont = BN_MONT_CTX_new();
  248. if (mont == NULL)
  249. goto err;
  250. if (!BN_MONT_CTX_set(mont, A, ctx))
  251. goto err;
  252. for (i = 0; i < checks; i++) {
  253. if (!BN_pseudo_rand_range(check, A1))
  254. goto err;
  255. if (!BN_add_word(check, 1))
  256. goto err;
  257. /* now 1 <= check < A */
  258. j = witness(check, A, A1, A1_odd, k, ctx, mont);
  259. if (j == -1)
  260. goto err;
  261. if (j) {
  262. ret = 0;
  263. goto err;
  264. }
  265. if (!BN_GENCB_call(cb, 1, i))
  266. goto err;
  267. }
  268. ret = 1;
  269. err:
  270. if (ctx != NULL) {
  271. BN_CTX_end(ctx);
  272. if (ctx_passed == NULL)
  273. BN_CTX_free(ctx);
  274. }
  275. BN_MONT_CTX_free(mont);
  276. return (ret);
  277. }
  278. int bn_probable_prime_dh_retry(BIGNUM *rnd, int bits, BN_CTX *ctx)
  279. {
  280. int i;
  281. int ret = 0;
  282. loop:
  283. if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  284. goto err;
  285. /* we now have a random number 'rand' to test. */
  286. for (i = 1; i < NUMPRIMES; i++) {
  287. /* check that rnd is a prime */
  288. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  289. if (mod == (BN_ULONG)-1)
  290. goto err;
  291. if (mod <= 1) {
  292. goto loop;
  293. }
  294. }
  295. ret = 1;
  296. err:
  297. bn_check_top(rnd);
  298. return (ret);
  299. }
  300. int bn_probable_prime_dh_coprime(BIGNUM *rnd, int bits, BN_CTX *ctx)
  301. {
  302. int i;
  303. BIGNUM *offset_index;
  304. BIGNUM *offset_count;
  305. int ret = 0;
  306. OPENSSL_assert(bits > prime_multiplier_bits);
  307. BN_CTX_start(ctx);
  308. if ((offset_index = BN_CTX_get(ctx)) == NULL)
  309. goto err;
  310. if ((offset_count = BN_CTX_get(ctx)) == NULL)
  311. goto err;
  312. if (!BN_add_word(offset_count, prime_offset_count))
  313. goto err;
  314. loop:
  315. if (!BN_rand(rnd, bits - prime_multiplier_bits,
  316. BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  317. goto err;
  318. if (BN_is_bit_set(rnd, bits))
  319. goto loop;
  320. if (!BN_rand_range(offset_index, offset_count))
  321. goto err;
  322. if (!BN_mul_word(rnd, prime_multiplier)
  323. || !BN_add_word(rnd, prime_offsets[BN_get_word(offset_index)]))
  324. goto err;
  325. /* we now have a random number 'rand' to test. */
  326. /* skip coprimes */
  327. for (i = first_prime_index; i < NUMPRIMES; i++) {
  328. /* check that rnd is a prime */
  329. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  330. if (mod == (BN_ULONG)-1)
  331. goto err;
  332. if (mod <= 1)
  333. goto loop;
  334. }
  335. ret = 1;
  336. err:
  337. BN_CTX_end(ctx);
  338. bn_check_top(rnd);
  339. return ret;
  340. }
  341. static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
  342. const BIGNUM *a1_odd, int k, BN_CTX *ctx,
  343. BN_MONT_CTX *mont)
  344. {
  345. if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */
  346. return -1;
  347. if (BN_is_one(w))
  348. return 0; /* probably prime */
  349. if (BN_cmp(w, a1) == 0)
  350. return 0; /* w == -1 (mod a), 'a' is probably prime */
  351. while (--k) {
  352. if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */
  353. return -1;
  354. if (BN_is_one(w))
  355. return 1; /* 'a' is composite, otherwise a previous 'w'
  356. * would have been == -1 (mod 'a') */
  357. if (BN_cmp(w, a1) == 0)
  358. return 0; /* w == -1 (mod a), 'a' is probably prime */
  359. }
  360. /*
  361. * If we get here, 'w' is the (a-1)/2-th power of the original 'w', and
  362. * it is neither -1 nor +1 -- so 'a' cannot be prime
  363. */
  364. bn_check_top(w);
  365. return 1;
  366. }
  367. static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)
  368. {
  369. int i;
  370. BN_ULONG delta;
  371. BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
  372. char is_single_word = bits <= BN_BITS2;
  373. again:
  374. if (!BN_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
  375. return (0);
  376. /* we now have a random number 'rnd' to test. */
  377. for (i = 1; i < NUMPRIMES; i++) {
  378. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  379. if (mod == (BN_ULONG)-1)
  380. return 0;
  381. mods[i] = (prime_t) mod;
  382. }
  383. /*
  384. * If bits is so small that it fits into a single word then we
  385. * additionally don't want to exceed that many bits.
  386. */
  387. if (is_single_word) {
  388. BN_ULONG size_limit;
  389. if (bits == BN_BITS2) {
  390. /*
  391. * Shifting by this much has undefined behaviour so we do it a
  392. * different way
  393. */
  394. size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
  395. } else {
  396. size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
  397. }
  398. if (size_limit < maxdelta)
  399. maxdelta = size_limit;
  400. }
  401. delta = 0;
  402. loop:
  403. if (is_single_word) {
  404. BN_ULONG rnd_word = BN_get_word(rnd);
  405. /*-
  406. * In the case that the candidate prime is a single word then
  407. * we check that:
  408. * 1) It's greater than primes[i] because we shouldn't reject
  409. * 3 as being a prime number because it's a multiple of
  410. * three.
  411. * 2) That it's not a multiple of a known prime. We don't
  412. * check that rnd-1 is also coprime to all the known
  413. * primes because there aren't many small primes where
  414. * that's true.
  415. */
  416. for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
  417. if ((mods[i] + delta) % primes[i] == 0) {
  418. delta += 2;
  419. if (delta > maxdelta)
  420. goto again;
  421. goto loop;
  422. }
  423. }
  424. } else {
  425. for (i = 1; i < NUMPRIMES; i++) {
  426. /*
  427. * check that rnd is not a prime and also that gcd(rnd-1,primes)
  428. * == 1 (except for 2)
  429. */
  430. if (((mods[i] + delta) % primes[i]) <= 1) {
  431. delta += 2;
  432. if (delta > maxdelta)
  433. goto again;
  434. goto loop;
  435. }
  436. }
  437. }
  438. if (!BN_add_word(rnd, delta))
  439. return (0);
  440. if (BN_num_bits(rnd) != bits)
  441. goto again;
  442. bn_check_top(rnd);
  443. return (1);
  444. }
  445. int bn_probable_prime_dh(BIGNUM *rnd, int bits,
  446. const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
  447. {
  448. int i, ret = 0;
  449. BIGNUM *t1;
  450. BN_CTX_start(ctx);
  451. if ((t1 = BN_CTX_get(ctx)) == NULL)
  452. goto err;
  453. if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  454. goto err;
  455. /* we need ((rnd-rem) % add) == 0 */
  456. if (!BN_mod(t1, rnd, add, ctx))
  457. goto err;
  458. if (!BN_sub(rnd, rnd, t1))
  459. goto err;
  460. if (rem == NULL) {
  461. if (!BN_add_word(rnd, 1))
  462. goto err;
  463. } else {
  464. if (!BN_add(rnd, rnd, rem))
  465. goto err;
  466. }
  467. /* we now have a random number 'rand' to test. */
  468. loop:
  469. for (i = 1; i < NUMPRIMES; i++) {
  470. /* check that rnd is a prime */
  471. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  472. if (mod == (BN_ULONG)-1)
  473. goto err;
  474. if (mod <= 1) {
  475. if (!BN_add(rnd, rnd, add))
  476. goto err;
  477. goto loop;
  478. }
  479. }
  480. ret = 1;
  481. err:
  482. BN_CTX_end(ctx);
  483. bn_check_top(rnd);
  484. return (ret);
  485. }
  486. static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
  487. const BIGNUM *rem, BN_CTX *ctx)
  488. {
  489. int i, ret = 0;
  490. BIGNUM *t1, *qadd, *q;
  491. bits--;
  492. BN_CTX_start(ctx);
  493. t1 = BN_CTX_get(ctx);
  494. q = BN_CTX_get(ctx);
  495. qadd = BN_CTX_get(ctx);
  496. if (qadd == NULL)
  497. goto err;
  498. if (!BN_rshift1(qadd, padd))
  499. goto err;
  500. if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  501. goto err;
  502. /* we need ((rnd-rem) % add) == 0 */
  503. if (!BN_mod(t1, q, qadd, ctx))
  504. goto err;
  505. if (!BN_sub(q, q, t1))
  506. goto err;
  507. if (rem == NULL) {
  508. if (!BN_add_word(q, 1))
  509. goto err;
  510. } else {
  511. if (!BN_rshift1(t1, rem))
  512. goto err;
  513. if (!BN_add(q, q, t1))
  514. goto err;
  515. }
  516. /* we now have a random number 'rand' to test. */
  517. if (!BN_lshift1(p, q))
  518. goto err;
  519. if (!BN_add_word(p, 1))
  520. goto err;
  521. loop:
  522. for (i = 1; i < NUMPRIMES; i++) {
  523. /* check that p and q are prime */
  524. /*
  525. * check that for p and q gcd(p-1,primes) == 1 (except for 2)
  526. */
  527. BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);
  528. BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);
  529. if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
  530. goto err;
  531. if (pmod == 0 || qmod == 0) {
  532. if (!BN_add(p, p, padd))
  533. goto err;
  534. if (!BN_add(q, q, qadd))
  535. goto err;
  536. goto loop;
  537. }
  538. }
  539. ret = 1;
  540. err:
  541. BN_CTX_end(ctx);
  542. bn_check_top(p);
  543. return (ret);
  544. }