bn_prime.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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_local.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 probable_prime(BIGNUM *rnd, int bits, int safe, prime_t *mods,
  20. BN_CTX *ctx);
  21. static int probable_prime_dh(BIGNUM *rnd, int bits, int safe, prime_t *mods,
  22. const BIGNUM *add, const BIGNUM *rem,
  23. BN_CTX *ctx);
  24. static int bn_is_prime_int(const BIGNUM *w, int checks, BN_CTX *ctx,
  25. int do_trial_division, BN_GENCB *cb);
  26. #define square(x) ((BN_ULONG)(x) * (BN_ULONG)(x))
  27. #if BN_BITS2 == 64
  28. # define BN_DEF(lo, hi) (BN_ULONG)hi<<32|lo
  29. #else
  30. # define BN_DEF(lo, hi) lo, hi
  31. #endif
  32. /*
  33. * See SP800 89 5.3.3 (Step f)
  34. * The product of the set of primes ranging from 3 to 751
  35. * Generated using process in test/bn_internal_test.c test_bn_small_factors().
  36. * This includes 751 (which is not currently included in SP 800-89).
  37. */
  38. static const BN_ULONG small_prime_factors[] = {
  39. BN_DEF(0x3ef4e3e1, 0xc4309333), BN_DEF(0xcd2d655f, 0x71161eb6),
  40. BN_DEF(0x0bf94862, 0x95e2238c), BN_DEF(0x24f7912b, 0x3eb233d3),
  41. BN_DEF(0xbf26c483, 0x6b55514b), BN_DEF(0x5a144871, 0x0a84d817),
  42. BN_DEF(0x9b82210a, 0x77d12fee), BN_DEF(0x97f050b3, 0xdb5b93c2),
  43. BN_DEF(0x4d6c026b, 0x4acad6b9), BN_DEF(0x54aec893, 0xeb7751f3),
  44. BN_DEF(0x36bc85c4, 0xdba53368), BN_DEF(0x7f5ec78e, 0xd85a1b28),
  45. BN_DEF(0x6b322244, 0x2eb072d8), BN_DEF(0x5e2b3aea, 0xbba51112),
  46. BN_DEF(0x0e2486bf, 0x36ed1a6c), BN_DEF(0xec0c5727, 0x5f270460),
  47. (BN_ULONG)0x000017b1
  48. };
  49. #define BN_SMALL_PRIME_FACTORS_TOP OSSL_NELEM(small_prime_factors)
  50. static const BIGNUM _bignum_small_prime_factors = {
  51. (BN_ULONG *)small_prime_factors,
  52. BN_SMALL_PRIME_FACTORS_TOP,
  53. BN_SMALL_PRIME_FACTORS_TOP,
  54. 0,
  55. BN_FLG_STATIC_DATA
  56. };
  57. const BIGNUM *bn_get0_small_factors(void)
  58. {
  59. return &_bignum_small_prime_factors;
  60. }
  61. /*
  62. * Calculate the number of trial divisions that gives the best speed in
  63. * combination with Miller-Rabin prime test, based on the sized of the prime.
  64. */
  65. static int calc_trial_divisions(int bits)
  66. {
  67. if (bits <= 512)
  68. return 64;
  69. else if (bits <= 1024)
  70. return 128;
  71. else if (bits <= 2048)
  72. return 384;
  73. else if (bits <= 4096)
  74. return 1024;
  75. return NUMPRIMES;
  76. }
  77. /*
  78. * Use a minimum of 64 rounds of Miller-Rabin, which should give a false
  79. * positive rate of 2^-128. If the size of the prime is larger than 2048
  80. * the user probably wants a higher security level than 128, so switch
  81. * to 128 rounds giving a false positive rate of 2^-256.
  82. * Returns the number of rounds.
  83. */
  84. static int bn_mr_min_checks(int bits)
  85. {
  86. if (bits > 2048)
  87. return 128;
  88. return 64;
  89. }
  90. int BN_GENCB_call(BN_GENCB *cb, int a, int b)
  91. {
  92. /* No callback means continue */
  93. if (!cb)
  94. return 1;
  95. switch (cb->ver) {
  96. case 1:
  97. /* Deprecated-style callbacks */
  98. if (!cb->cb.cb_1)
  99. return 1;
  100. cb->cb.cb_1(a, b, cb->arg);
  101. return 1;
  102. case 2:
  103. /* New-style callbacks */
  104. return cb->cb.cb_2(a, b, cb);
  105. default:
  106. break;
  107. }
  108. /* Unrecognised callback type */
  109. return 0;
  110. }
  111. int BN_generate_prime_ex2(BIGNUM *ret, int bits, int safe,
  112. const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb,
  113. BN_CTX *ctx)
  114. {
  115. BIGNUM *t;
  116. int found = 0;
  117. int i, j, c1 = 0;
  118. prime_t *mods = NULL;
  119. int checks = bn_mr_min_checks(bits);
  120. if (bits < 2) {
  121. /* There are no prime numbers this small. */
  122. BNerr(BN_F_BN_GENERATE_PRIME_EX2, BN_R_BITS_TOO_SMALL);
  123. return 0;
  124. } else if (add == NULL && safe && bits < 6 && bits != 3) {
  125. /*
  126. * The smallest safe prime (7) is three bits.
  127. * But the following two safe primes with less than 6 bits (11, 23)
  128. * are unreachable for BN_rand with BN_RAND_TOP_TWO.
  129. */
  130. BNerr(BN_F_BN_GENERATE_PRIME_EX2, BN_R_BITS_TOO_SMALL);
  131. return 0;
  132. }
  133. mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
  134. if (mods == NULL)
  135. goto err;
  136. BN_CTX_start(ctx);
  137. t = BN_CTX_get(ctx);
  138. if (t == NULL)
  139. goto err;
  140. loop:
  141. /* make a random number and set the top and bottom bits */
  142. if (add == NULL) {
  143. if (!probable_prime(ret, bits, safe, mods, ctx))
  144. goto err;
  145. } else {
  146. if (!probable_prime_dh(ret, bits, safe, mods, add, rem, ctx))
  147. goto err;
  148. }
  149. if (!BN_GENCB_call(cb, 0, c1++))
  150. /* aborted */
  151. goto err;
  152. if (!safe) {
  153. i = bn_is_prime_int(ret, checks, ctx, 0, cb);
  154. if (i == -1)
  155. goto err;
  156. if (i == 0)
  157. goto loop;
  158. } else {
  159. /*
  160. * for "safe prime" generation, check that (p-1)/2 is prime. Since a
  161. * prime is odd, We just need to divide by 2
  162. */
  163. if (!BN_rshift1(t, ret))
  164. goto err;
  165. for (i = 0; i < checks; i++) {
  166. j = bn_is_prime_int(ret, 1, ctx, 0, cb);
  167. if (j == -1)
  168. goto err;
  169. if (j == 0)
  170. goto loop;
  171. j = bn_is_prime_int(t, 1, ctx, 0, cb);
  172. if (j == -1)
  173. goto err;
  174. if (j == 0)
  175. goto loop;
  176. if (!BN_GENCB_call(cb, 2, c1 - 1))
  177. goto err;
  178. /* We have a safe prime test pass */
  179. }
  180. }
  181. /* we have a prime :-) */
  182. found = 1;
  183. err:
  184. OPENSSL_free(mods);
  185. BN_CTX_end(ctx);
  186. bn_check_top(ret);
  187. return found;
  188. }
  189. #ifndef FIPS_MODULE
  190. int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
  191. const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
  192. {
  193. BN_CTX *ctx = BN_CTX_new();
  194. int retval;
  195. if (ctx == NULL)
  196. return 0;
  197. retval = BN_generate_prime_ex2(ret, bits, safe, add, rem, cb, ctx);
  198. BN_CTX_free(ctx);
  199. return retval;
  200. }
  201. #endif
  202. #ifndef OPENSSL_NO_DEPRECATED_3_0
  203. int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
  204. BN_GENCB *cb)
  205. {
  206. return bn_check_prime_int(a, checks, ctx_passed, 0, cb);
  207. }
  208. int BN_is_prime_fasttest_ex(const BIGNUM *w, int checks, BN_CTX *ctx,
  209. int do_trial_division, BN_GENCB *cb)
  210. {
  211. return bn_check_prime_int(w, checks, ctx, do_trial_division, cb);
  212. }
  213. #endif
  214. /* Wrapper around bn_is_prime_int that sets the minimum number of checks */
  215. int bn_check_prime_int(const BIGNUM *w, int checks, BN_CTX *ctx,
  216. int do_trial_division, BN_GENCB *cb)
  217. {
  218. int min_checks = bn_mr_min_checks(BN_num_bits(w));
  219. if (checks < min_checks)
  220. checks = min_checks;
  221. return bn_is_prime_int(w, checks, ctx, do_trial_division, cb);
  222. }
  223. int BN_check_prime(const BIGNUM *p, BN_CTX *ctx, BN_GENCB *cb)
  224. {
  225. return bn_check_prime_int(p, 0, ctx, 1, cb);
  226. }
  227. /*
  228. * Tests that |w| is probably prime
  229. * See FIPS 186-4 C.3.1 Miller Rabin Probabilistic Primality Test.
  230. *
  231. * Returns 0 when composite, 1 when probable prime, -1 on error.
  232. */
  233. static int bn_is_prime_int(const BIGNUM *w, int checks, BN_CTX *ctx,
  234. int do_trial_division, BN_GENCB *cb)
  235. {
  236. int i, status, ret = -1;
  237. #ifndef FIPS_MODULE
  238. BN_CTX *ctxlocal = NULL;
  239. #else
  240. if (ctx == NULL)
  241. return -1;
  242. #endif
  243. /* w must be bigger than 1 */
  244. if (BN_cmp(w, BN_value_one()) <= 0)
  245. return 0;
  246. /* w must be odd */
  247. if (BN_is_odd(w)) {
  248. /* Take care of the really small prime 3 */
  249. if (BN_is_word(w, 3))
  250. return 1;
  251. } else {
  252. /* 2 is the only even prime */
  253. return BN_is_word(w, 2);
  254. }
  255. /* first look for small factors */
  256. if (do_trial_division) {
  257. int trial_divisions = calc_trial_divisions(BN_num_bits(w));
  258. for (i = 1; i < trial_divisions; i++) {
  259. BN_ULONG mod = BN_mod_word(w, primes[i]);
  260. if (mod == (BN_ULONG)-1)
  261. return -1;
  262. if (mod == 0)
  263. return BN_is_word(w, primes[i]);
  264. }
  265. if (!BN_GENCB_call(cb, 1, -1))
  266. return -1;
  267. }
  268. #ifndef FIPS_MODULE
  269. if (ctx == NULL && (ctxlocal = ctx = BN_CTX_new()) == NULL)
  270. goto err;
  271. #endif
  272. ret = bn_miller_rabin_is_prime(w, checks, ctx, cb, 0, &status);
  273. if (!ret)
  274. goto err;
  275. ret = (status == BN_PRIMETEST_PROBABLY_PRIME);
  276. err:
  277. #ifndef FIPS_MODULE
  278. BN_CTX_free(ctxlocal);
  279. #endif
  280. return ret;
  281. }
  282. /*
  283. * Refer to FIPS 186-4 C.3.2 Enhanced Miller-Rabin Probabilistic Primality Test.
  284. * OR C.3.1 Miller-Rabin Probabilistic Primality Test (if enhanced is zero).
  285. * The Step numbers listed in the code refer to the enhanced case.
  286. *
  287. * if enhanced is set, then status returns one of the following:
  288. * BN_PRIMETEST_PROBABLY_PRIME
  289. * BN_PRIMETEST_COMPOSITE_WITH_FACTOR
  290. * BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME
  291. * if enhanced is zero, then status returns either
  292. * BN_PRIMETEST_PROBABLY_PRIME or
  293. * BN_PRIMETEST_COMPOSITE
  294. *
  295. * returns 0 if there was an error, otherwise it returns 1.
  296. */
  297. int bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
  298. BN_GENCB *cb, int enhanced, int *status)
  299. {
  300. int i, j, a, ret = 0;
  301. BIGNUM *g, *w1, *w3, *x, *m, *z, *b;
  302. BN_MONT_CTX *mont = NULL;
  303. /* w must be odd */
  304. if (!BN_is_odd(w))
  305. return 0;
  306. BN_CTX_start(ctx);
  307. g = BN_CTX_get(ctx);
  308. w1 = BN_CTX_get(ctx);
  309. w3 = BN_CTX_get(ctx);
  310. x = BN_CTX_get(ctx);
  311. m = BN_CTX_get(ctx);
  312. z = BN_CTX_get(ctx);
  313. b = BN_CTX_get(ctx);
  314. if (!(b != NULL
  315. /* w1 := w - 1 */
  316. && BN_copy(w1, w)
  317. && BN_sub_word(w1, 1)
  318. /* w3 := w - 3 */
  319. && BN_copy(w3, w)
  320. && BN_sub_word(w3, 3)))
  321. goto err;
  322. /* check w is larger than 3, otherwise the random b will be too small */
  323. if (BN_is_zero(w3) || BN_is_negative(w3))
  324. goto err;
  325. /* (Step 1) Calculate largest integer 'a' such that 2^a divides w-1 */
  326. a = 1;
  327. while (!BN_is_bit_set(w1, a))
  328. a++;
  329. /* (Step 2) m = (w-1) / 2^a */
  330. if (!BN_rshift(m, w1, a))
  331. goto err;
  332. /* Montgomery setup for computations mod a */
  333. mont = BN_MONT_CTX_new();
  334. if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))
  335. goto err;
  336. if (iterations == 0)
  337. iterations = bn_mr_min_checks(BN_num_bits(w));
  338. /* (Step 4) */
  339. for (i = 0; i < iterations; ++i) {
  340. /* (Step 4.1) obtain a Random string of bits b where 1 < b < w-1 */
  341. if (!BN_priv_rand_range_ex(b, w3, ctx)
  342. || !BN_add_word(b, 2)) /* 1 < b < w-1 */
  343. goto err;
  344. if (enhanced) {
  345. /* (Step 4.3) */
  346. if (!BN_gcd(g, b, w, ctx))
  347. goto err;
  348. /* (Step 4.4) */
  349. if (!BN_is_one(g)) {
  350. *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
  351. ret = 1;
  352. goto err;
  353. }
  354. }
  355. /* (Step 4.5) z = b^m mod w */
  356. if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))
  357. goto err;
  358. /* (Step 4.6) if (z = 1 or z = w-1) */
  359. if (BN_is_one(z) || BN_cmp(z, w1) == 0)
  360. goto outer_loop;
  361. /* (Step 4.7) for j = 1 to a-1 */
  362. for (j = 1; j < a ; ++j) {
  363. /* (Step 4.7.1 - 4.7.2) x = z. z = x^2 mod w */
  364. if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
  365. goto err;
  366. /* (Step 4.7.3) */
  367. if (BN_cmp(z, w1) == 0)
  368. goto outer_loop;
  369. /* (Step 4.7.4) */
  370. if (BN_is_one(z))
  371. goto composite;
  372. }
  373. /* At this point z = b^((w-1)/2) mod w */
  374. /* (Steps 4.8 - 4.9) x = z, z = x^2 mod w */
  375. if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
  376. goto err;
  377. /* (Step 4.10) */
  378. if (BN_is_one(z))
  379. goto composite;
  380. /* (Step 4.11) x = b^(w-1) mod w */
  381. if (!BN_copy(x, z))
  382. goto err;
  383. composite:
  384. if (enhanced) {
  385. /* (Step 4.1.2) g = GCD(x-1, w) */
  386. if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx))
  387. goto err;
  388. /* (Steps 4.1.3 - 4.1.4) */
  389. if (BN_is_one(g))
  390. *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME;
  391. else
  392. *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
  393. } else {
  394. *status = BN_PRIMETEST_COMPOSITE;
  395. }
  396. ret = 1;
  397. goto err;
  398. outer_loop: ;
  399. /* (Step 4.1.5) */
  400. if (!BN_GENCB_call(cb, 1, i))
  401. goto err;
  402. }
  403. /* (Step 5) */
  404. *status = BN_PRIMETEST_PROBABLY_PRIME;
  405. ret = 1;
  406. err:
  407. BN_clear(g);
  408. BN_clear(w1);
  409. BN_clear(w3);
  410. BN_clear(x);
  411. BN_clear(m);
  412. BN_clear(z);
  413. BN_clear(b);
  414. BN_CTX_end(ctx);
  415. BN_MONT_CTX_free(mont);
  416. return ret;
  417. }
  418. /*
  419. * Generate a random number of |bits| bits that is probably prime by sieving.
  420. * If |safe| != 0, it generates a safe prime.
  421. * |mods| is a preallocated array that gets reused when called again.
  422. *
  423. * The probably prime is saved in |rnd|.
  424. *
  425. * Returns 1 on success and 0 on error.
  426. */
  427. static int probable_prime(BIGNUM *rnd, int bits, int safe, prime_t *mods,
  428. BN_CTX *ctx)
  429. {
  430. int i;
  431. BN_ULONG delta;
  432. int trial_divisions = calc_trial_divisions(bits);
  433. BN_ULONG maxdelta = BN_MASK2 - primes[trial_divisions - 1];
  434. again:
  435. /* TODO: Not all primes are private */
  436. if (!BN_priv_rand_ex(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD, ctx))
  437. return 0;
  438. if (safe && !BN_set_bit(rnd, 1))
  439. return 0;
  440. /* we now have a random number 'rnd' to test. */
  441. for (i = 1; i < trial_divisions; i++) {
  442. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  443. if (mod == (BN_ULONG)-1)
  444. return 0;
  445. mods[i] = (prime_t) mod;
  446. }
  447. delta = 0;
  448. loop:
  449. for (i = 1; i < trial_divisions; i++) {
  450. /*
  451. * check that rnd is a prime and also that
  452. * gcd(rnd-1,primes) == 1 (except for 2)
  453. * do the second check only if we are interested in safe primes
  454. * in the case that the candidate prime is a single word then
  455. * we check only the primes up to sqrt(rnd)
  456. */
  457. if (bits <= 31 && delta <= 0x7fffffff
  458. && square(primes[i]) > BN_get_word(rnd) + delta)
  459. break;
  460. if (safe ? (mods[i] + delta) % primes[i] <= 1
  461. : (mods[i] + delta) % primes[i] == 0) {
  462. delta += safe ? 4 : 2;
  463. if (delta > maxdelta)
  464. goto again;
  465. goto loop;
  466. }
  467. }
  468. if (!BN_add_word(rnd, delta))
  469. return 0;
  470. if (BN_num_bits(rnd) != bits)
  471. goto again;
  472. bn_check_top(rnd);
  473. return 1;
  474. }
  475. /*
  476. * Generate a random number |rnd| of |bits| bits that is probably prime
  477. * and satisfies |rnd| % |add| == |rem| by sieving.
  478. * If |safe| != 0, it generates a safe prime.
  479. * |mods| is a preallocated array that gets reused when called again.
  480. *
  481. * Returns 1 on success and 0 on error.
  482. */
  483. static int probable_prime_dh(BIGNUM *rnd, int bits, int safe, prime_t *mods,
  484. const BIGNUM *add, const BIGNUM *rem,
  485. BN_CTX *ctx)
  486. {
  487. int i, ret = 0;
  488. BIGNUM *t1;
  489. BN_ULONG delta;
  490. int trial_divisions = calc_trial_divisions(bits);
  491. BN_ULONG maxdelta = BN_MASK2 - primes[trial_divisions - 1];
  492. BN_CTX_start(ctx);
  493. if ((t1 = BN_CTX_get(ctx)) == NULL)
  494. goto err;
  495. if (maxdelta > BN_MASK2 - BN_get_word(add))
  496. maxdelta = BN_MASK2 - BN_get_word(add);
  497. again:
  498. if (!BN_rand_ex(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD, ctx))
  499. goto err;
  500. /* we need ((rnd-rem) % add) == 0 */
  501. if (!BN_mod(t1, rnd, add, ctx))
  502. goto err;
  503. if (!BN_sub(rnd, rnd, t1))
  504. goto err;
  505. if (rem == NULL) {
  506. if (!BN_add_word(rnd, safe ? 3u : 1u))
  507. goto err;
  508. } else {
  509. if (!BN_add(rnd, rnd, rem))
  510. goto err;
  511. }
  512. if (BN_num_bits(rnd) < bits
  513. || BN_get_word(rnd) < (safe ? 5u : 3u)) {
  514. if (!BN_add(rnd, rnd, add))
  515. goto err;
  516. }
  517. /* we now have a random number 'rnd' to test. */
  518. for (i = 1; i < trial_divisions; i++) {
  519. BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
  520. if (mod == (BN_ULONG)-1)
  521. goto err;
  522. mods[i] = (prime_t) mod;
  523. }
  524. delta = 0;
  525. loop:
  526. for (i = 1; i < trial_divisions; i++) {
  527. /* check that rnd is a prime */
  528. if (bits <= 31 && delta <= 0x7fffffff
  529. && square(primes[i]) > BN_get_word(rnd) + delta)
  530. break;
  531. /* rnd mod p == 1 implies q = (rnd-1)/2 is divisible by p */
  532. if (safe ? (mods[i] + delta) % primes[i] <= 1
  533. : (mods[i] + delta) % primes[i] == 0) {
  534. delta += BN_get_word(add);
  535. if (delta > maxdelta)
  536. goto again;
  537. goto loop;
  538. }
  539. }
  540. if (!BN_add_word(rnd, delta))
  541. goto err;
  542. ret = 1;
  543. err:
  544. BN_CTX_end(ctx);
  545. bn_check_top(rnd);
  546. return ret;
  547. }