155714Skris/* crypto/bn/bn_prime.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280304Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280304Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280304Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280304Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280304Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5859191Skris/* ====================================================================
59109998Smarkm * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
6059191Skris *
6159191Skris * Redistribution and use in source and binary forms, with or without
6259191Skris * modification, are permitted provided that the following conditions
6359191Skris * are met:
6459191Skris *
6559191Skris * 1. Redistributions of source code must retain the above copyright
66280304Sjkim *    notice, this list of conditions and the following disclaimer.
6759191Skris *
6859191Skris * 2. Redistributions in binary form must reproduce the above copyright
6959191Skris *    notice, this list of conditions and the following disclaimer in
7059191Skris *    the documentation and/or other materials provided with the
7159191Skris *    distribution.
7259191Skris *
7359191Skris * 3. All advertising materials mentioning features or use of this
7459191Skris *    software must display the following acknowledgment:
7559191Skris *    "This product includes software developed by the OpenSSL Project
7659191Skris *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
7759191Skris *
7859191Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
7959191Skris *    endorse or promote products derived from this software without
8059191Skris *    prior written permission. For written permission, please contact
8159191Skris *    openssl-core@openssl.org.
8259191Skris *
8359191Skris * 5. Products derived from this software may not be called "OpenSSL"
8459191Skris *    nor may "OpenSSL" appear in their names without prior written
8559191Skris *    permission of the OpenSSL Project.
8659191Skris *
8759191Skris * 6. Redistributions of any form whatsoever must retain the following
8859191Skris *    acknowledgment:
8959191Skris *    "This product includes software developed by the OpenSSL Project
9059191Skris *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
9159191Skris *
9259191Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
9359191Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9459191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9559191Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
9659191Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9759191Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
9859191Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
9959191Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
10059191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
10159191Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10259191Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
10359191Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
10459191Skris * ====================================================================
10559191Skris *
10659191Skris * This product includes cryptographic software written by Eric Young
10759191Skris * (eay@cryptsoft.com).  This product includes software written by Tim
10859191Skris * Hudson (tjh@cryptsoft.com).
10959191Skris *
11059191Skris */
11155714Skris
11255714Skris#include <stdio.h>
11355714Skris#include <time.h>
11455714Skris#include "cryptlib.h"
11555714Skris#include "bn_lcl.h"
11655714Skris#include <openssl/rand.h>
11755714Skris
118280304Sjkim/*
119280304Sjkim * NB: these functions have been "upgraded", the deprecated versions (which
120280304Sjkim * are compatibility wrappers using these functions) are in bn_depr.c. -
121280304Sjkim * Geoff
122160814Ssimon */
123160814Ssimon
124280304Sjkim/*
125280304Sjkim * The quick sieve algorithm approach to weeding out primes is Philip
126280304Sjkim * Zimmermann's, as implemented in PGP.  I have had a read of his comments
127280304Sjkim * and implemented my own version.
12855714Skris */
12955714Skris#include "bn_prime.h"
13055714Skris
13159191Skrisstatic int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
132280304Sjkim                   const BIGNUM *a1_odd, int k, BN_CTX *ctx,
133280304Sjkim                   BN_MONT_CTX *mont);
13455714Skrisstatic int probable_prime(BIGNUM *rnd, int bits);
13555714Skrisstatic int probable_prime_dh(BIGNUM *rnd, int bits,
136280304Sjkim                             const BIGNUM *add, const BIGNUM *rem,
137280304Sjkim                             BN_CTX *ctx);
138280304Sjkimstatic int probable_prime_dh_safe(BIGNUM *rnd, int bits, const BIGNUM *add,
139280304Sjkim                                  const BIGNUM *rem, BN_CTX *ctx);
14059191Skris
141160814Ssimonint BN_GENCB_call(BN_GENCB *cb, int a, int b)
142280304Sjkim{
143280304Sjkim    /* No callback means continue */
144280304Sjkim    if (!cb)
145280304Sjkim        return 1;
146280304Sjkim    switch (cb->ver) {
147280304Sjkim    case 1:
148280304Sjkim        /* Deprecated-style callbacks */
149280304Sjkim        if (!cb->cb.cb_1)
150280304Sjkim            return 1;
151280304Sjkim        cb->cb.cb_1(a, b, cb->arg);
152280304Sjkim        return 1;
153280304Sjkim    case 2:
154280304Sjkim        /* New-style callbacks */
155280304Sjkim        return cb->cb.cb_2(a, b, cb);
156280304Sjkim    default:
157280304Sjkim        break;
158280304Sjkim    }
159280304Sjkim    /* Unrecognised callback type */
160280304Sjkim    return 0;
161280304Sjkim}
162160814Ssimon
163160814Ssimonint BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
164280304Sjkim                         const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
165280304Sjkim{
166280304Sjkim    BIGNUM *t;
167280304Sjkim    int found = 0;
168280304Sjkim    int i, j, c1 = 0;
169280304Sjkim    BN_CTX *ctx;
170280304Sjkim    int checks = BN_prime_checks_for_size(bits);
17155714Skris
172280304Sjkim    ctx = BN_CTX_new();
173280304Sjkim    if (ctx == NULL)
174280304Sjkim        goto err;
175280304Sjkim    BN_CTX_start(ctx);
176280304Sjkim    t = BN_CTX_get(ctx);
177280304Sjkim    if (!t)
178280304Sjkim        goto err;
179280304Sjkim loop:
180280304Sjkim    /* make a random number and set the top and bottom bits */
181280304Sjkim    if (add == NULL) {
182280304Sjkim        if (!probable_prime(ret, bits))
183280304Sjkim            goto err;
184280304Sjkim    } else {
185280304Sjkim        if (safe) {
186280304Sjkim            if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
187280304Sjkim                goto err;
188280304Sjkim        } else {
189280304Sjkim            if (!probable_prime_dh(ret, bits, add, rem, ctx))
190280304Sjkim                goto err;
191280304Sjkim        }
192280304Sjkim    }
193280304Sjkim    /* if (BN_mod_word(ret,(BN_ULONG)3) == 1) goto loop; */
194280304Sjkim    if (!BN_GENCB_call(cb, 0, c1++))
195280304Sjkim        /* aborted */
196280304Sjkim        goto err;
19755714Skris
198280304Sjkim    if (!safe) {
199280304Sjkim        i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
200280304Sjkim        if (i == -1)
201280304Sjkim            goto err;
202280304Sjkim        if (i == 0)
203280304Sjkim            goto loop;
204280304Sjkim    } else {
205280304Sjkim        /*
206280304Sjkim         * for "safe prime" generation, check that (p-1)/2 is prime. Since a
207280304Sjkim         * prime is odd, We just need to divide by 2
208280304Sjkim         */
209280304Sjkim        if (!BN_rshift1(t, ret))
210280304Sjkim            goto err;
21155714Skris
212280304Sjkim        for (i = 0; i < checks; i++) {
213280304Sjkim            j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
214280304Sjkim            if (j == -1)
215280304Sjkim                goto err;
216280304Sjkim            if (j == 0)
217280304Sjkim                goto loop;
21855714Skris
219280304Sjkim            j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
220280304Sjkim            if (j == -1)
221280304Sjkim                goto err;
222280304Sjkim            if (j == 0)
223280304Sjkim                goto loop;
22455714Skris
225280304Sjkim            if (!BN_GENCB_call(cb, 2, c1 - 1))
226280304Sjkim                goto err;
227280304Sjkim            /* We have a safe prime test pass */
228280304Sjkim        }
229280304Sjkim    }
230280304Sjkim    /* we have a prime :-) */
231280304Sjkim    found = 1;
232280304Sjkim err:
233280304Sjkim    if (ctx != NULL) {
234280304Sjkim        BN_CTX_end(ctx);
235280304Sjkim        BN_CTX_free(ctx);
236280304Sjkim    }
237280304Sjkim    bn_check_top(ret);
238280304Sjkim    return found;
239280304Sjkim}
24055714Skris
241280304Sjkimint BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
242280304Sjkim                   BN_GENCB *cb)
243280304Sjkim{
244280304Sjkim    return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
245280304Sjkim}
24655714Skris
247160814Ssimonint BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
248280304Sjkim                            int do_trial_division, BN_GENCB *cb)
249280304Sjkim{
250280304Sjkim    int i, j, ret = -1;
251280304Sjkim    int k;
252280304Sjkim    BN_CTX *ctx = NULL;
253280304Sjkim    BIGNUM *A1, *A1_odd, *check; /* taken from ctx */
254280304Sjkim    BN_MONT_CTX *mont = NULL;
255280304Sjkim    const BIGNUM *A = NULL;
25659191Skris
257280304Sjkim    if (BN_cmp(a, BN_value_one()) <= 0)
258280304Sjkim        return 0;
25959191Skris
260280304Sjkim    if (checks == BN_prime_checks)
261280304Sjkim        checks = BN_prime_checks_for_size(BN_num_bits(a));
26259191Skris
263280304Sjkim    /* first look for small factors */
264280304Sjkim    if (!BN_is_odd(a))
265280304Sjkim        /* a is even => a is prime if and only if a == 2 */
266280304Sjkim        return BN_is_word(a, 2);
267280304Sjkim    if (do_trial_division) {
268280304Sjkim        for (i = 1; i < NUMPRIMES; i++)
269280304Sjkim            if (BN_mod_word(a, primes[i]) == 0)
270280304Sjkim                return 0;
271280304Sjkim        if (!BN_GENCB_call(cb, 1, -1))
272280304Sjkim            goto err;
273280304Sjkim    }
27455714Skris
275280304Sjkim    if (ctx_passed != NULL)
276280304Sjkim        ctx = ctx_passed;
277280304Sjkim    else if ((ctx = BN_CTX_new()) == NULL)
278280304Sjkim        goto err;
279280304Sjkim    BN_CTX_start(ctx);
28055714Skris
281280304Sjkim    /* A := abs(a) */
282280304Sjkim    if (a->neg) {
283280304Sjkim        BIGNUM *t;
284280304Sjkim        if ((t = BN_CTX_get(ctx)) == NULL)
285280304Sjkim            goto err;
286280304Sjkim        BN_copy(t, a);
287280304Sjkim        t->neg = 0;
288280304Sjkim        A = t;
289280304Sjkim    } else
290280304Sjkim        A = a;
291280304Sjkim    A1 = BN_CTX_get(ctx);
292280304Sjkim    A1_odd = BN_CTX_get(ctx);
293280304Sjkim    check = BN_CTX_get(ctx);
294280304Sjkim    if (check == NULL)
295280304Sjkim        goto err;
29655714Skris
297280304Sjkim    /* compute A1 := A - 1 */
298280304Sjkim    if (!BN_copy(A1, A))
299280304Sjkim        goto err;
300280304Sjkim    if (!BN_sub_word(A1, 1))
301280304Sjkim        goto err;
302280304Sjkim    if (BN_is_zero(A1)) {
303280304Sjkim        ret = 0;
304280304Sjkim        goto err;
305280304Sjkim    }
30655714Skris
307280304Sjkim    /* write  A1  as  A1_odd * 2^k */
308280304Sjkim    k = 1;
309280304Sjkim    while (!BN_is_bit_set(A1, k))
310280304Sjkim        k++;
311280304Sjkim    if (!BN_rshift(A1_odd, A1, k))
312280304Sjkim        goto err;
31359191Skris
314280304Sjkim    /* Montgomery setup for computations mod A */
315280304Sjkim    mont = BN_MONT_CTX_new();
316280304Sjkim    if (mont == NULL)
317280304Sjkim        goto err;
318280304Sjkim    if (!BN_MONT_CTX_set(mont, A, ctx))
319280304Sjkim        goto err;
32059191Skris
321280304Sjkim    for (i = 0; i < checks; i++) {
322280304Sjkim        if (!BN_pseudo_rand_range(check, A1))
323280304Sjkim            goto err;
324280304Sjkim        if (!BN_add_word(check, 1))
325280304Sjkim            goto err;
326280304Sjkim        /* now 1 <= check < A */
32755714Skris
328280304Sjkim        j = witness(check, A, A1, A1_odd, k, ctx, mont);
329280304Sjkim        if (j == -1)
330280304Sjkim            goto err;
331280304Sjkim        if (j) {
332280304Sjkim            ret = 0;
333280304Sjkim            goto err;
334280304Sjkim        }
335280304Sjkim        if (!BN_GENCB_call(cb, 1, i))
336280304Sjkim            goto err;
337280304Sjkim    }
338280304Sjkim    ret = 1;
339280304Sjkim err:
340280304Sjkim    if (ctx != NULL) {
341280304Sjkim        BN_CTX_end(ctx);
342280304Sjkim        if (ctx_passed == NULL)
343280304Sjkim            BN_CTX_free(ctx);
344280304Sjkim    }
345280304Sjkim    if (mont != NULL)
346280304Sjkim        BN_MONT_CTX_free(mont);
347280304Sjkim
348280304Sjkim    return (ret);
349280304Sjkim}
350280304Sjkim
35159191Skrisstatic int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
352280304Sjkim                   const BIGNUM *a1_odd, int k, BN_CTX *ctx,
353280304Sjkim                   BN_MONT_CTX *mont)
354280304Sjkim{
355280304Sjkim    if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */
356280304Sjkim        return -1;
357280304Sjkim    if (BN_is_one(w))
358280304Sjkim        return 0;               /* probably prime */
359280304Sjkim    if (BN_cmp(w, a1) == 0)
360280304Sjkim        return 0;               /* w == -1 (mod a), 'a' is probably prime */
361280304Sjkim    while (--k) {
362280304Sjkim        if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */
363280304Sjkim            return -1;
364280304Sjkim        if (BN_is_one(w))
365280304Sjkim            return 1;           /* 'a' is composite, otherwise a previous 'w'
366280304Sjkim                                 * would have been == -1 (mod 'a') */
367280304Sjkim        if (BN_cmp(w, a1) == 0)
368280304Sjkim            return 0;           /* w == -1 (mod a), 'a' is probably prime */
369280304Sjkim    }
370280304Sjkim    /*
371280304Sjkim     * If we get here, 'w' is the (a-1)/2-th power of the original 'w', and
372280304Sjkim     * it is neither -1 nor +1 -- so 'a' cannot be prime
373280304Sjkim     */
374280304Sjkim    bn_check_top(w);
375280304Sjkim    return 1;
376280304Sjkim}
37755714Skris
37855714Skrisstatic int probable_prime(BIGNUM *rnd, int bits)
379280304Sjkim{
380280304Sjkim    int i;
381280304Sjkim    prime_t mods[NUMPRIMES];
382280304Sjkim    BN_ULONG delta, maxdelta;
38355714Skris
384280304Sjkim again:
385280304Sjkim    if (!BN_rand(rnd, bits, 1, 1))
386280304Sjkim        return (0);
387280304Sjkim    /* we now have a random number 'rand' to test. */
388280304Sjkim    for (i = 1; i < NUMPRIMES; i++)
389280304Sjkim        mods[i] = (prime_t) BN_mod_word(rnd, (BN_ULONG)primes[i]);
390280304Sjkim    maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
391280304Sjkim    delta = 0;
392280304Sjkim loop:for (i = 1; i < NUMPRIMES; i++) {
393280304Sjkim        /*
394280304Sjkim         * check that rnd is not a prime and also that gcd(rnd-1,primes) == 1
395280304Sjkim         * (except for 2)
396280304Sjkim         */
397280304Sjkim        if (((mods[i] + delta) % primes[i]) <= 1) {
398280304Sjkim            delta += 2;
399280304Sjkim            if (delta > maxdelta)
400280304Sjkim                goto again;
401280304Sjkim            goto loop;
402280304Sjkim        }
403280304Sjkim    }
404280304Sjkim    if (!BN_add_word(rnd, delta))
405280304Sjkim        return (0);
406280304Sjkim    bn_check_top(rnd);
407280304Sjkim    return (1);
408280304Sjkim}
40955714Skris
410109998Smarkmstatic int probable_prime_dh(BIGNUM *rnd, int bits,
411280304Sjkim                             const BIGNUM *add, const BIGNUM *rem,
412280304Sjkim                             BN_CTX *ctx)
413280304Sjkim{
414280304Sjkim    int i, ret = 0;
415280304Sjkim    BIGNUM *t1;
41655714Skris
417280304Sjkim    BN_CTX_start(ctx);
418280304Sjkim    if ((t1 = BN_CTX_get(ctx)) == NULL)
419280304Sjkim        goto err;
42055714Skris
421280304Sjkim    if (!BN_rand(rnd, bits, 0, 1))
422280304Sjkim        goto err;
42355714Skris
424280304Sjkim    /* we need ((rnd-rem) % add) == 0 */
42555714Skris
426280304Sjkim    if (!BN_mod(t1, rnd, add, ctx))
427280304Sjkim        goto err;
428280304Sjkim    if (!BN_sub(rnd, rnd, t1))
429280304Sjkim        goto err;
430280304Sjkim    if (rem == NULL) {
431280304Sjkim        if (!BN_add_word(rnd, 1))
432280304Sjkim            goto err;
433280304Sjkim    } else {
434280304Sjkim        if (!BN_add(rnd, rnd, rem))
435280304Sjkim            goto err;
436280304Sjkim    }
43755714Skris
438280304Sjkim    /* we now have a random number 'rand' to test. */
43955714Skris
440280304Sjkim loop:for (i = 1; i < NUMPRIMES; i++) {
441280304Sjkim        /* check that rnd is a prime */
442280304Sjkim        if (BN_mod_word(rnd, (BN_ULONG)primes[i]) <= 1) {
443280304Sjkim            if (!BN_add(rnd, rnd, add))
444280304Sjkim                goto err;
445280304Sjkim            goto loop;
446280304Sjkim        }
447280304Sjkim    }
448280304Sjkim    ret = 1;
449280304Sjkim err:
450280304Sjkim    BN_CTX_end(ctx);
451280304Sjkim    bn_check_top(rnd);
452280304Sjkim    return (ret);
453280304Sjkim}
45455714Skris
455109998Smarkmstatic int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
456280304Sjkim                                  const BIGNUM *rem, BN_CTX *ctx)
457280304Sjkim{
458280304Sjkim    int i, ret = 0;
459280304Sjkim    BIGNUM *t1, *qadd, *q;
46055714Skris
461280304Sjkim    bits--;
462280304Sjkim    BN_CTX_start(ctx);
463280304Sjkim    t1 = BN_CTX_get(ctx);
464280304Sjkim    q = BN_CTX_get(ctx);
465280304Sjkim    qadd = BN_CTX_get(ctx);
466280304Sjkim    if (qadd == NULL)
467280304Sjkim        goto err;
46855714Skris
469280304Sjkim    if (!BN_rshift1(qadd, padd))
470280304Sjkim        goto err;
47155714Skris
472280304Sjkim    if (!BN_rand(q, bits, 0, 1))
473280304Sjkim        goto err;
47455714Skris
475280304Sjkim    /* we need ((rnd-rem) % add) == 0 */
476280304Sjkim    if (!BN_mod(t1, q, qadd, ctx))
477280304Sjkim        goto err;
478280304Sjkim    if (!BN_sub(q, q, t1))
479280304Sjkim        goto err;
480280304Sjkim    if (rem == NULL) {
481280304Sjkim        if (!BN_add_word(q, 1))
482280304Sjkim            goto err;
483280304Sjkim    } else {
484280304Sjkim        if (!BN_rshift1(t1, rem))
485280304Sjkim            goto err;
486280304Sjkim        if (!BN_add(q, q, t1))
487280304Sjkim            goto err;
488280304Sjkim    }
48955714Skris
490280304Sjkim    /* we now have a random number 'rand' to test. */
491280304Sjkim    if (!BN_lshift1(p, q))
492280304Sjkim        goto err;
493280304Sjkim    if (!BN_add_word(p, 1))
494280304Sjkim        goto err;
495280304Sjkim
496280304Sjkim loop:for (i = 1; i < NUMPRIMES; i++) {
497280304Sjkim        /* check that p and q are prime */
498280304Sjkim        /*
499280304Sjkim         * check that for p and q gcd(p-1,primes) == 1 (except for 2)
500280304Sjkim         */
501280304Sjkim        if ((BN_mod_word(p, (BN_ULONG)primes[i]) == 0) ||
502280304Sjkim            (BN_mod_word(q, (BN_ULONG)primes[i]) == 0)) {
503280304Sjkim            if (!BN_add(p, p, padd))
504280304Sjkim                goto err;
505280304Sjkim            if (!BN_add(q, q, qadd))
506280304Sjkim                goto err;
507280304Sjkim            goto loop;
508280304Sjkim        }
509280304Sjkim    }
510280304Sjkim    ret = 1;
511280304Sjkim err:
512280304Sjkim    BN_CTX_end(ctx);
513280304Sjkim    bn_check_top(p);
514280304Sjkim    return (ret);
515280304Sjkim}
516