1BN_GENERATE_PRIME(3)                OpenSSL               BN_GENERATE_PRIME(3)
2
3
4

NAME

6       BN_generate_prime_ex, BN_is_prime_ex, BN_is_prime_fasttest_ex,
7       BN_GENCB_call, BN_GENCB_new, BN_GENCB_free, BN_GENCB_set_old,
8       BN_GENCB_set, BN_GENCB_get_arg, BN_generate_prime, BN_is_prime,
9       BN_is_prime_fasttest - generate primes and test for primality
10

SYNOPSIS

12        #include <openssl/bn.h>
13
14        int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
15                                 const BIGNUM *rem, BN_GENCB *cb);
16
17        int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
18
19        int BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx,
20                                    int do_trial_division, BN_GENCB *cb);
21
22        int BN_GENCB_call(BN_GENCB *cb, int a, int b);
23
24        BN_GENCB *BN_GENCB_new(void);
25
26        void BN_GENCB_free(BN_GENCB *cb);
27
28        void BN_GENCB_set_old(BN_GENCB *gencb,
29                              void (*callback)(int, int, void *), void *cb_arg);
30
31        void BN_GENCB_set(BN_GENCB *gencb,
32                          int (*callback)(int, int, BN_GENCB *), void *cb_arg);
33
34        void *BN_GENCB_get_arg(BN_GENCB *cb);
35
36       Deprecated:
37
38        #if OPENSSL_API_COMPAT < 0x00908000L
39        BIGNUM *BN_generate_prime(BIGNUM *ret, int num, int safe, BIGNUM *add,
40                                  BIGNUM *rem, void (*callback)(int, int, void *),
41                                  void *cb_arg);
42
43        int BN_is_prime(const BIGNUM *a, int checks,
44                        void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg);
45
46        int BN_is_prime_fasttest(const BIGNUM *a, int checks,
47                                 void (*callback)(int, int, void *), BN_CTX *ctx,
48                                 void *cb_arg, int do_trial_division);
49        #endif
50

DESCRIPTION

52       BN_generate_prime_ex() generates a pseudo-random prime number of at
53       least bit length bits. The returned number is probably prime with a
54       negligible error. If add is NULL the returned prime number will have
55       exact bit length bits with the top most two bits set.
56
57       If ret is not NULL, it will be used to store the number.
58
59       If cb is not NULL, it is used as follows:
60
61BN_GENCB_call(cb, 0, i) is called after generating the i-th potential
62         prime number.
63
64       • While the number is being tested for primality, BN_GENCB_call(cb, 1,
65         j) is called as described below.
66
67       • When a prime has been found, BN_GENCB_call(cb, 2, i) is called.
68
69       • The callers of BN_generate_prime_ex() may call BN_GENCB_call(cb, i,
70         j) with other values as described in their respective man pages; see
71         "SEE ALSO".
72
73       The prime may have to fulfill additional requirements for use in
74       Diffie-Hellman key exchange:
75
76       If add is not NULL, the prime will fulfill the condition p % add == rem
77       (p % add == 1 if rem == NULL) in order to suit a given generator.
78
79       If safe is true, it will be a safe prime (i.e. a prime p so that
80       (p-1)/2 is also prime). If safe is true, and rem == NULL the condition
81       will be p % add == 3.  It is recommended that add is a multiple of 4.
82
83       The random generator must be seeded prior to calling
84       BN_generate_prime_ex().  If the automatic seeding or reseeding of the
85       OpenSSL CSPRNG fails due to external circumstances (see RAND(7)), the
86       operation will fail.
87
88       BN_is_prime_ex() and BN_is_prime_fasttest_ex() test if the number p is
89       prime.  The following tests are performed until one of them shows that
90       p is composite; if p passes all these tests, it is considered prime.
91
92       BN_is_prime_fasttest_ex(), when called with do_trial_division == 1,
93       first attempts trial division by a number of small primes; if no
94       divisors are found by this test and cb is not NULL, BN_GENCB_call(cb,
95       1, -1) is called.  If do_trial_division == 0, this test is skipped.
96
97       Both BN_is_prime_ex() and BN_is_prime_fasttest_ex() perform a Miller-
98       Rabin probabilistic primality test with nchecks iterations. If nchecks
99       == BN_prime_checks, a number of iterations is used that yields a false
100       positive rate of at most 2^-64 for random input.  The error rate
101       depends on the size of the prime and goes down for bigger primes.  The
102       rate is 2^-80 starting at 308 bits, 2^-112 at 852 bits, 2^-128 at 1080
103       bits, 2^-192 at 3747 bits and 2^-256 at 6394 bits.
104
105       When the source of the prime is not random or not trusted, the number
106       of checks needs to be much higher to reach the same level of assurance:
107       It should equal half of the targeted security level in bits (rounded up
108       to the next integer if necessary).  For instance, to reach the 128 bit
109       security level, nchecks should be set to 64.
110
111       If cb is not NULL, BN_GENCB_call(cb, 1, j) is called after the j-th
112       iteration (j = 0, 1, ...). ctx is a preallocated BN_CTX (to save the
113       overhead of allocating and freeing the structure in a loop), or NULL.
114
115       BN_GENCB_call() calls the callback function held in the BN_GENCB
116       structure and passes the ints a and b as arguments. There are two types
117       of BN_GENCB structure that are supported: "new" style and "old" style.
118       New programs should prefer the "new" style, whilst the "old" style is
119       provided for backwards compatibility purposes.
120
121       A BN_GENCB structure should be created through a call to
122       BN_GENCB_new(), and freed through a call to BN_GENCB_free().
123
124       For "new" style callbacks a BN_GENCB structure should be initialised
125       with a call to BN_GENCB_set(), where gencb is a BN_GENCB *, callback is
126       of type int (*callback)(int, int, BN_GENCB *) and cb_arg is a void *.
127       "Old" style callbacks are the same except they are initialised with a
128       call to BN_GENCB_set_old() and callback is of type void
129       (*callback)(int, int, void *).
130
131       A callback is invoked through a call to BN_GENCB_call. This will check
132       the type of the callback and will invoke callback(a, b, gencb) for new
133       style callbacks or callback(a, b, cb_arg) for old style.
134
135       It is possible to obtain the argument associated with a BN_GENCB
136       structure (set via a call to BN_GENCB_set or BN_GENCB_set_old) using
137       BN_GENCB_get_arg.
138
139       BN_generate_prime() (deprecated) works in the same way as
140       BN_generate_prime_ex() but expects an old-style callback function
141       directly in the callback parameter, and an argument to pass to it in
142       the cb_arg. BN_is_prime() and BN_is_prime_fasttest() can similarly be
143       compared to BN_is_prime_ex() and BN_is_prime_fasttest_ex(),
144       respectively.
145

RETURN VALUES

147       BN_generate_prime_ex() return 1 on success or 0 on error.
148
149       BN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime() and
150       BN_is_prime_fasttest() return 0 if the number is composite, 1 if it is
151       prime with an error probability of less than 0.25^nchecks, and -1 on
152       error.
153
154       BN_generate_prime() returns the prime number on success, NULL
155       otherwise.
156
157       BN_GENCB_new returns a pointer to a BN_GENCB structure on success, or
158       NULL otherwise.
159
160       BN_GENCB_get_arg returns the argument previously associated with a
161       BN_GENCB structure.
162
163       Callback functions should return 1 on success or 0 on error.
164
165       The error codes can be obtained by ERR_get_error(3).
166

REMOVED FUNCTIONALITY

168       As of OpenSSL 1.1.0 it is no longer possible to create a BN_GENCB
169       structure directly, as in:
170
171        BN_GENCB callback;
172
173       Instead applications should create a BN_GENCB structure using
174       BN_GENCB_new:
175
176        BN_GENCB *callback;
177        callback = BN_GENCB_new();
178        if (!callback)
179            /* error */
180        ...
181        BN_GENCB_free(callback);
182

SEE ALSO

184       DH_generate_parameters(3), DSA_generate_parameters(3),
185       RSA_generate_key(3), ERR_get_error(3), RAND_bytes(3), RAND(7)
186

HISTORY

188       The BN_GENCB_new(), BN_GENCB_free(), and BN_GENCB_get_arg() functions
189       were added in OpenSSL 1.1.0.
190
192       Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
193
194       Licensed under the OpenSSL license (the "License").  You may not use
195       this file except in compliance with the License.  You can obtain a copy
196       in the file LICENSE in the source distribution or at
197       <https://www.openssl.org/source/license.html>.
198
199
200
2011.1.1i                            2021-01-26              BN_GENERATE_PRIME(3)
Impressum