1BN_GENERATE_PRIME(3) OpenSSL BN_GENERATE_PRIME(3)
2
3
4
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
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
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.
55
56 If ret is not NULL, it will be used to store the number.
57
58 If cb is not NULL, it is used as follows:
59
60 · BN_GENCB_call(cb, 0, i) is called after generating the i-th potential
61 prime number.
62
63 · While the number is being tested for primality, BN_GENCB_call(cb, 1,
64 j) is called as described below.
65
66 · When a prime has been found, BN_GENCB_call(cb, 2, i) is called.
67
68 · The callers of BN_generate_prime_ex() may call BN_GENCB_call(cb, i,
69 j) with other values as described in their respective man pages; see
70 "SEE ALSO".
71
72 The prime may have to fulfill additional requirements for use in
73 Diffie-Hellman key exchange:
74
75 If add is not NULL, the prime will fulfill the condition p % add == rem
76 (p % add == 1 if rem == NULL) in order to suit a given generator.
77
78 If safe is true, it will be a safe prime (i.e. a prime p so that
79 (p-1)/2 is also prime).
80
81 The random generator must be seeded prior to calling
82 BN_generate_prime_ex(). If the automatic seeding or reseeding of the
83 OpenSSL CSPRNG fails due to external circumstances (see RAND(7)), the
84 operation will fail.
85
86 BN_is_prime_ex() and BN_is_prime_fasttest_ex() test if the number p is
87 prime. The following tests are performed until one of them shows that
88 p is composite; if p passes all these tests, it is considered prime.
89
90 BN_is_prime_fasttest_ex(), when called with do_trial_division == 1,
91 first attempts trial division by a number of small primes; if no
92 divisors are found by this test and cb is not NULL, BN_GENCB_call(cb,
93 1, -1) is called. If do_trial_division == 0, this test is skipped.
94
95 Both BN_is_prime_ex() and BN_is_prime_fasttest_ex() perform a Miller-
96 Rabin probabilistic primality test with nchecks iterations. If nchecks
97 == BN_prime_checks, a number of iterations is used that yields a false
98 positive rate of at most 2^-64 for random input. The error rate
99 depends on the size of the prime and goes down for bigger primes. The
100 rate is 2^-80 starting at 308 bits, 2^-112 at 852 bits, 2^-128 at 1080
101 bits, 2^-192 at 3747 bits and 2^-256 at 6394 bits.
102
103 When the source of the prime is not random or not trusted, the number
104 of checks needs to be much higher to reach the same level of assurance:
105 It should equal half of the targeted security level in bits (rounded up
106 to the next integer if necessary). For instance, to reach the 128 bit
107 security level, nchecks should be set to 64.
108
109 If cb is not NULL, BN_GENCB_call(cb, 1, j) is called after the j-th
110 iteration (j = 0, 1, ...). ctx is a pre-allocated BN_CTX (to save the
111 overhead of allocating and freeing the structure in a loop), or NULL.
112
113 BN_GENCB_call() calls the callback function held in the BN_GENCB
114 structure and passes the ints a and b as arguments. There are two types
115 of BN_GENCB structure that are supported: "new" style and "old" style.
116 New programs should prefer the "new" style, whilst the "old" style is
117 provided for backwards compatibility purposes.
118
119 A BN_GENCB structure should be created through a call to
120 BN_GENCB_new(), and freed through a call to BN_GENCB_free().
121
122 For "new" style callbacks a BN_GENCB structure should be initialised
123 with a call to BN_GENCB_set(), where gencb is a BN_GENCB *, callback is
124 of type int (*callback)(int, int, BN_GENCB *) and cb_arg is a void *.
125 "Old" style callbacks are the same except they are initialised with a
126 call to BN_GENCB_set_old() and callback is of type void
127 (*callback)(int, int, void *).
128
129 A callback is invoked through a call to BN_GENCB_call. This will check
130 the type of the callback and will invoke callback(a, b, gencb) for new
131 style callbacks or callback(a, b, cb_arg) for old style.
132
133 It is possible to obtain the argument associated with a BN_GENCB
134 structure (set via a call to BN_GENCB_set or BN_GENCB_set_old) using
135 BN_GENCB_get_arg.
136
137 BN_generate_prime() (deprecated) works in the same way as
138 BN_generate_prime_ex() but expects an old-style callback function
139 directly in the callback parameter, and an argument to pass to it in
140 the cb_arg. BN_is_prime() and BN_is_prime_fasttest() can similarly be
141 compared to BN_is_prime_ex() and BN_is_prime_fasttest_ex(),
142 respectively.
143
145 BN_generate_prime_ex() return 1 on success or 0 on error.
146
147 BN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime() and
148 BN_is_prime_fasttest() return 0 if the number is composite, 1 if it is
149 prime with an error probability of less than 0.25^nchecks, and -1 on
150 error.
151
152 BN_generate_prime() returns the prime number on success, NULL
153 otherwise.
154
155 BN_GENCB_new returns a pointer to a BN_GENCB structure on success, or
156 NULL otherwise.
157
158 BN_GENCB_get_arg returns the argument previously associated with a
159 BN_GENCB structure.
160
161 Callback functions should return 1 on success or 0 on error.
162
163 The error codes can be obtained by ERR_get_error(3).
164
166 As of OpenSSL 1.1.0 it is no longer possible to create a BN_GENCB
167 structure directly, as in:
168
169 BN_GENCB callback;
170
171 Instead applications should create a BN_GENCB structure using
172 BN_GENCB_new:
173
174 BN_GENCB *callback;
175 callback = BN_GENCB_new();
176 if (!callback)
177 /* error */
178 ...
179 BN_GENCB_free(callback);
180
182 DH_generate_parameters(3), DSA_generate_parameters(3),
183 RSA_generate_key(3), ERR_get_error(3), RAND_bytes(3), RAND(7)
184
186 The BN_GENCB_new(), BN_GENCB_free(), and BN_GENCB_get_arg() functions
187 were added in OpenSSL 1.1.0.
188
190 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
191
192 Licensed under the OpenSSL license (the "License"). You may not use
193 this file except in compliance with the License. You can obtain a copy
194 in the file LICENSE in the source distribution or at
195 <https://www.openssl.org/source/license.html>.
196
197
198
1991.1.1d 2019-10-03 BN_GENERATE_PRIME(3)