bn_prime.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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, *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_priv_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. /* TODO: Not all primes are private */
  254. if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
  255. return 0;
  256. /* we now have a random number 'rnd' to test. */
  257. for (i = 1; i < NUMPRIMES; i++) {
  258. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  259. if (mod == (BN_ULONG)-1)
  260. return 0;
  261. mods[i] = (prime_t) mod;
  262. }
  263. /*
  264. * If bits is so small that it fits into a single word then we
  265. * additionally don't want to exceed that many bits.
  266. */
  267. if (is_single_word) {
  268. BN_ULONG size_limit;
  269. if (bits == BN_BITS2) {
  270. /*
  271. * Shifting by this much has undefined behaviour so we do it a
  272. * different way
  273. */
  274. size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
  275. } else {
  276. size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
  277. }
  278. if (size_limit < maxdelta)
  279. maxdelta = size_limit;
  280. }
  281. delta = 0;
  282. loop:
  283. if (is_single_word) {
  284. BN_ULONG rnd_word = BN_get_word(rnd);
  285. /*-
  286. * In the case that the candidate prime is a single word then
  287. * we check that:
  288. * 1) It's greater than primes[i] because we shouldn't reject
  289. * 3 as being a prime number because it's a multiple of
  290. * three.
  291. * 2) That it's not a multiple of a known prime. We don't
  292. * check that rnd-1 is also coprime to all the known
  293. * primes because there aren't many small primes where
  294. * that's true.
  295. */
  296. for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
  297. if ((mods[i] + delta) % primes[i] == 0) {
  298. delta += 2;
  299. if (delta > maxdelta)
  300. goto again;
  301. goto loop;
  302. }
  303. }
  304. } else {
  305. for (i = 1; i < NUMPRIMES; i++) {
  306. /*
  307. * check that rnd is not a prime and also that gcd(rnd-1,primes)
  308. * == 1 (except for 2)
  309. */
  310. if (((mods[i] + delta) % primes[i]) <= 1) {
  311. delta += 2;
  312. if (delta > maxdelta)
  313. goto again;
  314. goto loop;
  315. }
  316. }
  317. }
  318. if (!BN_add_word(rnd, delta))
  319. return 0;
  320. if (BN_num_bits(rnd) != bits)
  321. goto again;
  322. bn_check_top(rnd);
  323. return 1;
  324. }
  325. int bn_probable_prime_dh(BIGNUM *rnd, int bits,
  326. const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
  327. {
  328. int i, ret = 0;
  329. BIGNUM *t1;
  330. BN_CTX_start(ctx);
  331. if ((t1 = BN_CTX_get(ctx)) == NULL)
  332. goto err;
  333. if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  334. goto err;
  335. /* we need ((rnd-rem) % add) == 0 */
  336. if (!BN_mod(t1, rnd, add, ctx))
  337. goto err;
  338. if (!BN_sub(rnd, rnd, t1))
  339. goto err;
  340. if (rem == NULL) {
  341. if (!BN_add_word(rnd, 1))
  342. goto err;
  343. } else {
  344. if (!BN_add(rnd, rnd, rem))
  345. goto err;
  346. }
  347. /* we now have a random number 'rand' to test. */
  348. loop:
  349. for (i = 1; i < NUMPRIMES; i++) {
  350. /* check that rnd is a prime */
  351. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  352. if (mod == (BN_ULONG)-1)
  353. goto err;
  354. if (mod <= 1) {
  355. if (!BN_add(rnd, rnd, add))
  356. goto err;
  357. goto loop;
  358. }
  359. }
  360. ret = 1;
  361. err:
  362. BN_CTX_end(ctx);
  363. bn_check_top(rnd);
  364. return ret;
  365. }
  366. static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
  367. const BIGNUM *rem, BN_CTX *ctx)
  368. {
  369. int i, ret = 0;
  370. BIGNUM *t1, *qadd, *q;
  371. bits--;
  372. BN_CTX_start(ctx);
  373. t1 = BN_CTX_get(ctx);
  374. q = BN_CTX_get(ctx);
  375. qadd = BN_CTX_get(ctx);
  376. if (qadd == NULL)
  377. goto err;
  378. if (!BN_rshift1(qadd, padd))
  379. goto err;
  380. if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
  381. goto err;
  382. /* we need ((rnd-rem) % add) == 0 */
  383. if (!BN_mod(t1, q, qadd, ctx))
  384. goto err;
  385. if (!BN_sub(q, q, t1))
  386. goto err;
  387. if (rem == NULL) {
  388. if (!BN_add_word(q, 1))
  389. goto err;
  390. } else {
  391. if (!BN_rshift1(t1, rem))
  392. goto err;
  393. if (!BN_add(q, q, t1))
  394. goto err;
  395. }
  396. /* we now have a random number 'rand' to test. */
  397. if (!BN_lshift1(p, q))
  398. goto err;
  399. if (!BN_add_word(p, 1))
  400. goto err;
  401. loop:
  402. for (i = 1; i < NUMPRIMES; i++) {
  403. /* check that p and q are prime */
  404. /*
  405. * check that for p and q gcd(p-1,primes) == 1 (except for 2)
  406. */
  407. BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);
  408. BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);
  409. if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
  410. goto err;
  411. if (pmod == 0 || qmod == 0) {
  412. if (!BN_add(p, p, padd))
  413. goto err;
  414. if (!BN_add(q, q, qadd))
  415. goto err;
  416. goto loop;
  417. }
  418. }
  419. ret = 1;
  420. err:
  421. BN_CTX_end(ctx);
  422. bn_check_top(p);
  423. return ret;
  424. }