1*e71b7053SJung-uk Kim=pod
2*e71b7053SJung-uk Kim
3*e71b7053SJung-uk Kim=head1 NAME
4*e71b7053SJung-uk Kim
5*e71b7053SJung-uk KimBN_generate_prime_ex, BN_is_prime_ex, BN_is_prime_fasttest_ex, BN_GENCB_call,
6*e71b7053SJung-uk KimBN_GENCB_new, BN_GENCB_free, BN_GENCB_set_old, BN_GENCB_set, BN_GENCB_get_arg,
7*e71b7053SJung-uk KimBN_generate_prime, BN_is_prime, BN_is_prime_fasttest - generate primes and test
8*e71b7053SJung-uk Kimfor primality
9*e71b7053SJung-uk Kim
10*e71b7053SJung-uk Kim=head1 SYNOPSIS
11*e71b7053SJung-uk Kim
12*e71b7053SJung-uk Kim #include <openssl/bn.h>
13*e71b7053SJung-uk Kim
14*e71b7053SJung-uk Kim int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
15*e71b7053SJung-uk Kim                          const BIGNUM *rem, BN_GENCB *cb);
16*e71b7053SJung-uk Kim
17*e71b7053SJung-uk Kim int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
18*e71b7053SJung-uk Kim
19*e71b7053SJung-uk Kim int BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx,
20*e71b7053SJung-uk Kim                             int do_trial_division, BN_GENCB *cb);
21*e71b7053SJung-uk Kim
22*e71b7053SJung-uk Kim int BN_GENCB_call(BN_GENCB *cb, int a, int b);
23*e71b7053SJung-uk Kim
24*e71b7053SJung-uk Kim BN_GENCB *BN_GENCB_new(void);
25*e71b7053SJung-uk Kim
26*e71b7053SJung-uk Kim void BN_GENCB_free(BN_GENCB *cb);
27*e71b7053SJung-uk Kim
28*e71b7053SJung-uk Kim void BN_GENCB_set_old(BN_GENCB *gencb,
29*e71b7053SJung-uk Kim                       void (*callback)(int, int, void *), void *cb_arg);
30*e71b7053SJung-uk Kim
31*e71b7053SJung-uk Kim void BN_GENCB_set(BN_GENCB *gencb,
32*e71b7053SJung-uk Kim                   int (*callback)(int, int, BN_GENCB *), void *cb_arg);
33*e71b7053SJung-uk Kim
34*e71b7053SJung-uk Kim void *BN_GENCB_get_arg(BN_GENCB *cb);
35*e71b7053SJung-uk Kim
36*e71b7053SJung-uk KimDeprecated:
37*e71b7053SJung-uk Kim
38*e71b7053SJung-uk Kim #if OPENSSL_API_COMPAT < 0x00908000L
39*e71b7053SJung-uk Kim BIGNUM *BN_generate_prime(BIGNUM *ret, int num, int safe, BIGNUM *add,
40*e71b7053SJung-uk Kim                           BIGNUM *rem, void (*callback)(int, int, void *),
41*e71b7053SJung-uk Kim                           void *cb_arg);
42*e71b7053SJung-uk Kim
43*e71b7053SJung-uk Kim int BN_is_prime(const BIGNUM *a, int checks,
44*e71b7053SJung-uk Kim                 void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg);
45*e71b7053SJung-uk Kim
46*e71b7053SJung-uk Kim int BN_is_prime_fasttest(const BIGNUM *a, int checks,
47*e71b7053SJung-uk Kim                          void (*callback)(int, int, void *), BN_CTX *ctx,
48*e71b7053SJung-uk Kim                          void *cb_arg, int do_trial_division);
49*e71b7053SJung-uk Kim #endif
50*e71b7053SJung-uk Kim
51*e71b7053SJung-uk Kim=head1 DESCRIPTION
52*e71b7053SJung-uk Kim
53*e71b7053SJung-uk KimBN_generate_prime_ex() generates a pseudo-random prime number of
54*e71b7053SJung-uk Kimat least bit length B<bits>.
55*e71b7053SJung-uk KimIf B<ret> is not B<NULL>, it will be used to store the number.
56*e71b7053SJung-uk Kim
57*e71b7053SJung-uk KimIf B<cb> is not B<NULL>, it is used as follows:
58*e71b7053SJung-uk Kim
59*e71b7053SJung-uk Kim=over 2
60*e71b7053SJung-uk Kim
61*e71b7053SJung-uk Kim=item *
62*e71b7053SJung-uk Kim
63*e71b7053SJung-uk KimB<BN_GENCB_call(cb, 0, i)> is called after generating the i-th
64*e71b7053SJung-uk Kimpotential prime number.
65*e71b7053SJung-uk Kim
66*e71b7053SJung-uk Kim=item *
67*e71b7053SJung-uk Kim
68*e71b7053SJung-uk KimWhile the number is being tested for primality,
69*e71b7053SJung-uk KimB<BN_GENCB_call(cb, 1, j)> is called as described below.
70*e71b7053SJung-uk Kim
71*e71b7053SJung-uk Kim=item *
72*e71b7053SJung-uk Kim
73*e71b7053SJung-uk KimWhen a prime has been found, B<BN_GENCB_call(cb, 2, i)> is called.
74*e71b7053SJung-uk Kim
75*e71b7053SJung-uk Kim=item *
76*e71b7053SJung-uk Kim
77*e71b7053SJung-uk KimThe callers of BN_generate_prime_ex() may call B<BN_GENCB_call(cb, i, j)> with
78*e71b7053SJung-uk Kimother values as described in their respective man pages; see L</SEE ALSO>.
79*e71b7053SJung-uk Kim
80*e71b7053SJung-uk Kim=back
81*e71b7053SJung-uk Kim
82*e71b7053SJung-uk KimThe prime may have to fulfill additional requirements for use in
83*e71b7053SJung-uk KimDiffie-Hellman key exchange:
84*e71b7053SJung-uk Kim
85*e71b7053SJung-uk KimIf B<add> is not B<NULL>, the prime will fulfill the condition p % B<add>
86*e71b7053SJung-uk Kim== B<rem> (p % B<add> == 1 if B<rem> == B<NULL>) in order to suit a given
87*e71b7053SJung-uk Kimgenerator.
88*e71b7053SJung-uk Kim
89*e71b7053SJung-uk KimIf B<safe> is true, it will be a safe prime (i.e. a prime p so
90*e71b7053SJung-uk Kimthat (p-1)/2 is also prime).
91*e71b7053SJung-uk Kim
92*e71b7053SJung-uk KimThe PRNG must be seeded prior to calling BN_generate_prime_ex().
93*e71b7053SJung-uk KimThe prime number generation has a negligible error probability.
94*e71b7053SJung-uk Kim
95*e71b7053SJung-uk KimBN_is_prime_ex() and BN_is_prime_fasttest_ex() test if the number B<p> is
96*e71b7053SJung-uk Kimprime.  The following tests are performed until one of them shows that
97*e71b7053SJung-uk KimB<p> is composite; if B<p> passes all these tests, it is considered
98*e71b7053SJung-uk Kimprime.
99*e71b7053SJung-uk Kim
100*e71b7053SJung-uk KimBN_is_prime_fasttest_ex(), when called with B<do_trial_division == 1>,
101*e71b7053SJung-uk Kimfirst attempts trial division by a number of small primes;
102*e71b7053SJung-uk Kimif no divisors are found by this test and B<cb> is not B<NULL>,
103*e71b7053SJung-uk KimB<BN_GENCB_call(cb, 1, -1)> is called.
104*e71b7053SJung-uk KimIf B<do_trial_division == 0>, this test is skipped.
105*e71b7053SJung-uk Kim
106*e71b7053SJung-uk KimBoth BN_is_prime_ex() and BN_is_prime_fasttest_ex() perform a Miller-Rabin
107*e71b7053SJung-uk Kimprobabilistic primality test with B<nchecks> iterations. If
108*e71b7053SJung-uk KimB<nchecks == BN_prime_checks>, a number of iterations is used that
109*e71b7053SJung-uk Kimyields a false positive rate of at most 2^-64 for random input.
110*e71b7053SJung-uk KimThe error rate depends on the size of the prime and goes down for bigger primes.
111*e71b7053SJung-uk KimThe rate is 2^-80 starting at 308 bits, 2^-112 at 852 bits, 2^-128 at 1080 bits,
112*e71b7053SJung-uk Kim2^-192 at 3747 bits and 2^-256 at 6394 bits.
113*e71b7053SJung-uk Kim
114*e71b7053SJung-uk KimWhen the source of the prime is not random or not trusted, the number
115*e71b7053SJung-uk Kimof checks needs to be much higher to reach the same level of assurance:
116*e71b7053SJung-uk KimIt should equal half of the targeted security level in bits (rounded up to the
117*e71b7053SJung-uk Kimnext integer if necessary).
118*e71b7053SJung-uk KimFor instance, to reach the 128 bit security level, B<nchecks> should be set to
119*e71b7053SJung-uk Kim64.
120*e71b7053SJung-uk Kim
121*e71b7053SJung-uk KimIf B<cb> is not B<NULL>, B<BN_GENCB_call(cb, 1, j)> is called
122*e71b7053SJung-uk Kimafter the j-th iteration (j = 0, 1, ...). B<ctx> is a
123*e71b7053SJung-uk Kimpre-allocated B<BN_CTX> (to save the overhead of allocating and
124*e71b7053SJung-uk Kimfreeing the structure in a loop), or B<NULL>.
125*e71b7053SJung-uk Kim
126*e71b7053SJung-uk KimBN_GENCB_call() calls the callback function held in the B<BN_GENCB> structure
127*e71b7053SJung-uk Kimand passes the ints B<a> and B<b> as arguments. There are two types of
128*e71b7053SJung-uk KimB<BN_GENCB> structure that are supported: "new" style and "old" style. New
129*e71b7053SJung-uk Kimprograms should prefer the "new" style, whilst the "old" style is provided
130*e71b7053SJung-uk Kimfor backwards compatibility purposes.
131*e71b7053SJung-uk Kim
132*e71b7053SJung-uk KimA B<BN_GENCB> structure should be created through a call to BN_GENCB_new(),
133*e71b7053SJung-uk Kimand freed through a call to BN_GENCB_free().
134*e71b7053SJung-uk Kim
135*e71b7053SJung-uk KimFor "new" style callbacks a BN_GENCB structure should be initialised with a
136*e71b7053SJung-uk Kimcall to BN_GENCB_set(), where B<gencb> is a B<BN_GENCB *>, B<callback> is of
137*e71b7053SJung-uk Kimtype B<int (*callback)(int, int, BN_GENCB *)> and B<cb_arg> is a B<void *>.
138*e71b7053SJung-uk Kim"Old" style callbacks are the same except they are initialised with a call
139*e71b7053SJung-uk Kimto BN_GENCB_set_old() and B<callback> is of type
140*e71b7053SJung-uk KimB<void (*callback)(int, int, void *)>.
141*e71b7053SJung-uk Kim
142*e71b7053SJung-uk KimA callback is invoked through a call to B<BN_GENCB_call>. This will check
143*e71b7053SJung-uk Kimthe type of the callback and will invoke B<callback(a, b, gencb)> for new
144*e71b7053SJung-uk Kimstyle callbacks or B<callback(a, b, cb_arg)> for old style.
145*e71b7053SJung-uk Kim
146*e71b7053SJung-uk KimIt is possible to obtain the argument associated with a BN_GENCB structure
147*e71b7053SJung-uk Kim(set via a call to BN_GENCB_set or BN_GENCB_set_old) using BN_GENCB_get_arg.
148*e71b7053SJung-uk Kim
149*e71b7053SJung-uk KimBN_generate_prime() (deprecated) works in the same way as
150*e71b7053SJung-uk KimBN_generate_prime_ex() but expects an old-style callback function
151*e71b7053SJung-uk Kimdirectly in the B<callback> parameter, and an argument to pass to it in
152*e71b7053SJung-uk Kimthe B<cb_arg>. BN_is_prime() and BN_is_prime_fasttest()
153*e71b7053SJung-uk Kimcan similarly be compared to BN_is_prime_ex() and
154*e71b7053SJung-uk KimBN_is_prime_fasttest_ex(), respectively.
155*e71b7053SJung-uk Kim
156*e71b7053SJung-uk Kim=head1 RETURN VALUES
157*e71b7053SJung-uk Kim
158*e71b7053SJung-uk KimBN_generate_prime_ex() return 1 on success or 0 on error.
159*e71b7053SJung-uk Kim
160*e71b7053SJung-uk KimBN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime() and
161*e71b7053SJung-uk KimBN_is_prime_fasttest() return 0 if the number is composite, 1 if it is
162*e71b7053SJung-uk Kimprime with an error probability of less than 0.25^B<nchecks>, and
163*e71b7053SJung-uk Kim-1 on error.
164*e71b7053SJung-uk Kim
165*e71b7053SJung-uk KimBN_generate_prime() returns the prime number on success, B<NULL> otherwise.
166*e71b7053SJung-uk Kim
167*e71b7053SJung-uk KimBN_GENCB_new returns a pointer to a BN_GENCB structure on success, or B<NULL>
168*e71b7053SJung-uk Kimotherwise.
169*e71b7053SJung-uk Kim
170*e71b7053SJung-uk KimBN_GENCB_get_arg returns the argument previously associated with a BN_GENCB
171*e71b7053SJung-uk Kimstructure.
172*e71b7053SJung-uk Kim
173*e71b7053SJung-uk KimCallback functions should return 1 on success or 0 on error.
174*e71b7053SJung-uk Kim
175*e71b7053SJung-uk KimThe error codes can be obtained by L<ERR_get_error(3)>.
176*e71b7053SJung-uk Kim
177*e71b7053SJung-uk Kim=head1 REMOVED FUNCTIONALITY
178*e71b7053SJung-uk Kim
179*e71b7053SJung-uk KimAs of OpenSSL 1.1.0 it is no longer possible to create a BN_GENCB structure
180*e71b7053SJung-uk Kimdirectly, as in:
181*e71b7053SJung-uk Kim
182*e71b7053SJung-uk Kim BN_GENCB callback;
183*e71b7053SJung-uk Kim
184*e71b7053SJung-uk KimInstead applications should create a BN_GENCB structure using BN_GENCB_new:
185*e71b7053SJung-uk Kim
186*e71b7053SJung-uk Kim BN_GENCB *callback;
187*e71b7053SJung-uk Kim callback = BN_GENCB_new();
188*e71b7053SJung-uk Kim if (!callback)
189*e71b7053SJung-uk Kim     /* error */
190*e71b7053SJung-uk Kim ...
191*e71b7053SJung-uk Kim BN_GENCB_free(callback);
192*e71b7053SJung-uk Kim
193*e71b7053SJung-uk Kim=head1 SEE ALSO
194*e71b7053SJung-uk Kim
195*e71b7053SJung-uk KimL<DH_generate_parameters(3)>, L<DSA_generate_parameters(3)>,
196*e71b7053SJung-uk KimL<RSA_generate_key(3)>, L<ERR_get_error(3)>, L<RAND_bytes(3)>
197*e71b7053SJung-uk Kim
198*e71b7053SJung-uk Kim=head1 HISTORY
199*e71b7053SJung-uk Kim
200*e71b7053SJung-uk KimBN_GENCB_new(), BN_GENCB_free(),
201*e71b7053SJung-uk Kimand BN_GENCB_get_arg() were added in OpenSSL 1.1.0
202*e71b7053SJung-uk Kim
203*e71b7053SJung-uk Kim=head1 COPYRIGHT
204*e71b7053SJung-uk Kim
205*e71b7053SJung-uk KimCopyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
206*e71b7053SJung-uk Kim
207*e71b7053SJung-uk KimLicensed under the OpenSSL license (the "License").  You may not use
208*e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
209*e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
210*e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
211*e71b7053SJung-uk Kim
212*e71b7053SJung-uk Kim=cut
213