BN_generate_prime.pod 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. =pod
  2. =head1 NAME
  3. BN_generate_prime_ex2, BN_generate_prime_ex, BN_is_prime_ex, BN_check_prime,
  4. BN_is_prime_fasttest_ex, BN_GENCB_call, BN_GENCB_new, BN_GENCB_free,
  5. BN_GENCB_set_old, BN_GENCB_set, BN_GENCB_get_arg, BN_generate_prime,
  6. BN_is_prime, BN_is_prime_fasttest - generate primes and test for primality
  7. =head1 SYNOPSIS
  8. #include <openssl/bn.h>
  9. int BN_generate_prime_ex2(BIGNUM *ret, int bits, int safe,
  10. const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb,
  11. BN_CTX *ctx);
  12. int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
  13. const BIGNUM *rem, BN_GENCB *cb);
  14. int BN_check_prime(const BIGNUM *p, BN_CTX *ctx, BN_GENCB *cb);
  15. int BN_GENCB_call(BN_GENCB *cb, int a, int b);
  16. BN_GENCB *BN_GENCB_new(void);
  17. void BN_GENCB_free(BN_GENCB *cb);
  18. void BN_GENCB_set_old(BN_GENCB *gencb,
  19. void (*callback)(int, int, void *), void *cb_arg);
  20. void BN_GENCB_set(BN_GENCB *gencb,
  21. int (*callback)(int, int, BN_GENCB *), void *cb_arg);
  22. void *BN_GENCB_get_arg(BN_GENCB *cb);
  23. The following functions have been deprecated since OpenSSL 0.9.8, and can be
  24. hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
  25. see L<openssl_user_macros(7)>:
  26. BIGNUM *BN_generate_prime(BIGNUM *ret, int num, int safe, BIGNUM *add,
  27. BIGNUM *rem, void (*callback)(int, int, void *),
  28. void *cb_arg);
  29. int BN_is_prime(const BIGNUM *p, int nchecks,
  30. void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg);
  31. int BN_is_prime_fasttest(const BIGNUM *p, int nchecks,
  32. void (*callback)(int, int, void *), BN_CTX *ctx,
  33. void *cb_arg, int do_trial_division);
  34. The following functions have been deprecated since OpenSSL 3.0, and can be
  35. hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
  36. see L<openssl_user_macros(7)>:
  37. int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
  38. int BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx,
  39. int do_trial_division, BN_GENCB *cb);
  40. =head1 DESCRIPTION
  41. BN_generate_prime_ex2() generates a pseudo-random prime number of
  42. at least bit length B<bits> using the BN_CTX provided in B<ctx>. The value of
  43. B<ctx> must not be NULL.
  44. The returned number is probably prime with a negligible error.
  45. The maximum error rate is 2^-128.
  46. It's 2^-287 for a 512 bit prime, 2^-435 for a 1024 bit prime,
  47. 2^-648 for a 2048 bit prime, and lower than 2^-882 for primes larger
  48. than 2048 bit.
  49. If B<add> is B<NULL> the returned prime number will have exact bit
  50. length B<bits> with the top most two bits set.
  51. If B<ret> is not B<NULL>, it will be used to store the number.
  52. If B<cb> is not B<NULL>, it is used as follows:
  53. =over 2
  54. =item *
  55. B<BN_GENCB_call(cb, 0, i)> is called after generating the i-th
  56. potential prime number.
  57. =item *
  58. While the number is being tested for primality,
  59. B<BN_GENCB_call(cb, 1, j)> is called as described below.
  60. =item *
  61. When a prime has been found, B<BN_GENCB_call(cb, 2, i)> is called.
  62. =item *
  63. The callers of BN_generate_prime_ex() may call B<BN_GENCB_call(cb, i, j)> with
  64. other values as described in their respective man pages; see L</SEE ALSO>.
  65. =back
  66. The prime may have to fulfill additional requirements for use in
  67. Diffie-Hellman key exchange:
  68. If B<add> is not B<NULL>, the prime will fulfill the condition p % B<add>
  69. == B<rem> (p % B<add> == 1 if B<rem> == B<NULL>) in order to suit a given
  70. generator.
  71. If B<safe> is true, it will be a safe prime (i.e. a prime p so
  72. that (p-1)/2 is also prime). If B<safe> is true, and B<rem> == B<NULL>
  73. the condition will be p % B<add> == 3.
  74. It is recommended that B<add> is a multiple of 4.
  75. The random generator must be seeded prior to calling BN_generate_prime_ex().
  76. If the automatic seeding or reseeding of the OpenSSL CSPRNG fails due to
  77. external circumstances (see L<RAND(7)>), the operation will fail.
  78. The random number generator configured for the OSSL_LIB_CTX associated with
  79. B<ctx> will be used.
  80. BN_generate_prime_ex() is the same as BN_generate_prime_ex2() except that no
  81. B<ctx> parameter is passed.
  82. In this case the random number generator associated with the default OSSL_LIB_CTX
  83. will be used.
  84. BN_check_prime(), BN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime()
  85. and BN_is_prime_fasttest() test if the number B<p> is prime.
  86. The functions tests until one of the tests shows that B<p> is composite,
  87. or all the tests passed.
  88. If B<p> passes all these tests, it is considered a probable prime.
  89. The test performed on B<p> are trial division by a number of small primes
  90. and rounds of the of the Miller-Rabin probabilistic primality test.
  91. The functions do at least 64 rounds of the Miller-Rabin test giving a maximum
  92. false positive rate of 2^-128.
  93. If the size of B<p> is more than 2048 bits, they do at least 128 rounds
  94. giving a maximum false positive rate of 2^-256.
  95. If B<nchecks> is larger than the minimum above (64 or 128), B<nchecks>
  96. rounds of the Miller-Rabin test will be done.
  97. If B<do_trial_division> set to B<0>, the trial division will be skipped.
  98. BN_is_prime_ex() and BN_is_prime() always skip the trial division.
  99. BN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime()
  100. and BN_is_prime_fasttest() are deprecated.
  101. BN_is_prime_fasttest() and BN_is_prime() behave just like
  102. BN_is_prime_fasttest_ex() and BN_is_prime_ex() respectively, but with the old
  103. style call back.
  104. B<ctx> is a preallocated B<BN_CTX> (to save the overhead of allocating and
  105. freeing the structure in a loop), or B<NULL>.
  106. If the trial division is done, and no divisors are found and B<cb>
  107. is not B<NULL>, B<BN_GENCB_call(cb, 1, -1)> is called.
  108. After each round of the Miller-Rabin probabilistic primality test,
  109. if B<cb> is not B<NULL>, B<BN_GENCB_call(cb, 1, j)> is called
  110. with B<j> the iteration (j = 0, 1, ...).
  111. BN_GENCB_call() calls the callback function held in the B<BN_GENCB> structure
  112. and passes the ints B<a> and B<b> as arguments. There are two types of
  113. B<BN_GENCB> structure that are supported: "new" style and "old" style. New
  114. programs should prefer the "new" style, whilst the "old" style is provided
  115. for backwards compatibility purposes.
  116. A B<BN_GENCB> structure should be created through a call to BN_GENCB_new(),
  117. and freed through a call to BN_GENCB_free().
  118. For "new" style callbacks a BN_GENCB structure should be initialised with a
  119. call to BN_GENCB_set(), where B<gencb> is a B<BN_GENCB *>, B<callback> is of
  120. type B<int (*callback)(int, int, BN_GENCB *)> and B<cb_arg> is a B<void *>.
  121. "Old" style callbacks are the same except they are initialised with a call
  122. to BN_GENCB_set_old() and B<callback> is of type
  123. B<void (*callback)(int, int, void *)>.
  124. A callback is invoked through a call to B<BN_GENCB_call>. This will check
  125. the type of the callback and will invoke B<callback(a, b, gencb)> for new
  126. style callbacks or B<callback(a, b, cb_arg)> for old style.
  127. It is possible to obtain the argument associated with a BN_GENCB structure
  128. (set via a call to BN_GENCB_set or BN_GENCB_set_old) using BN_GENCB_get_arg.
  129. BN_generate_prime() (deprecated) works in the same way as
  130. BN_generate_prime_ex() but expects an old-style callback function
  131. directly in the B<callback> parameter, and an argument to pass to it in
  132. the B<cb_arg>. BN_is_prime() and BN_is_prime_fasttest()
  133. can similarly be compared to BN_is_prime_ex() and
  134. BN_is_prime_fasttest_ex(), respectively.
  135. =head1 RETURN VALUES
  136. BN_generate_prime_ex() return 1 on success or 0 on error.
  137. BN_is_prime_ex(), BN_is_prime_fasttest_ex(), BN_is_prime(),
  138. BN_is_prime_fasttest() and BN_check_prime return 0 if the number is composite,
  139. 1 if it is prime with an error probability of less than 0.25^B<nchecks>, and
  140. -1 on error.
  141. BN_generate_prime() returns the prime number on success, B<NULL> otherwise.
  142. BN_GENCB_new returns a pointer to a BN_GENCB structure on success, or B<NULL>
  143. otherwise.
  144. BN_GENCB_get_arg returns the argument previously associated with a BN_GENCB
  145. structure.
  146. Callback functions should return 1 on success or 0 on error.
  147. The error codes can be obtained by L<ERR_get_error(3)>.
  148. =head1 REMOVED FUNCTIONALITY
  149. As of OpenSSL 1.1.0 it is no longer possible to create a BN_GENCB structure
  150. directly, as in:
  151. BN_GENCB callback;
  152. Instead applications should create a BN_GENCB structure using BN_GENCB_new:
  153. BN_GENCB *callback;
  154. callback = BN_GENCB_new();
  155. if (!callback)
  156. /* error */
  157. ...
  158. BN_GENCB_free(callback);
  159. =head1 SEE ALSO
  160. L<DH_generate_parameters(3)>, L<DSA_generate_parameters(3)>,
  161. L<RSA_generate_key(3)>, L<ERR_get_error(3)>, L<RAND_bytes(3)>,
  162. L<RAND(7)>
  163. =head1 HISTORY
  164. The BN_is_prime_ex() and BN_is_prime_fasttest_ex() functions were
  165. deprecated in OpenSSL 3.0.
  166. The BN_GENCB_new(), BN_GENCB_free(),
  167. and BN_GENCB_get_arg() functions were added in OpenSSL 1.1.0.
  168. BN_check_prime() was added in OpenSSL 3.0.
  169. =head1 COPYRIGHT
  170. Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
  171. Licensed under the Apache License 2.0 (the "License"). You may not use
  172. this file except in compliance with the License. You can obtain a copy
  173. in the file LICENSE in the source distribution or at
  174. L<https://www.openssl.org/source/license.html>.
  175. =cut