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