BN_generate_prime.3ossl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. .\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42)
  2. .\"
  3. .\" Standard preamble:
  4. .\" ========================================================================
  5. .de Sp \" Vertical space (when we can't use .PP)
  6. .if t .sp .5v
  7. .if n .sp
  8. ..
  9. .de Vb \" Begin verbatim text
  10. .ft CW
  11. .nf
  12. .ne \\$1
  13. ..
  14. .de Ve \" End verbatim text
  15. .ft R
  16. .fi
  17. ..
  18. .\" Set up some character translations and predefined strings. \*(-- will
  19. .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
  20. .\" double quote, and \*(R" will give a right double quote. \*(C+ will
  21. .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
  22. .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
  23. .\" nothing in troff, for use with C<>.
  24. .tr \(*W-
  25. .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
  26. .ie n \{\
  27. . ds -- \(*W-
  28. . ds PI pi
  29. . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
  30. . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
  31. . ds L" ""
  32. . ds R" ""
  33. . ds C` ""
  34. . ds C' ""
  35. 'br\}
  36. .el\{\
  37. . ds -- \|\(em\|
  38. . ds PI \(*p
  39. . ds L" ``
  40. . ds R" ''
  41. . ds C`
  42. . ds C'
  43. 'br\}
  44. .\"
  45. .\" Escape single quotes in literal strings from groff's Unicode transform.
  46. .ie \n(.g .ds Aq \(aq
  47. .el .ds Aq '
  48. .\"
  49. .\" If the F register is >0, we'll generate index entries on stderr for
  50. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  51. .\" entries marked with X<> in POD. Of course, you'll have to process the
  52. .\" output yourself in some meaningful fashion.
  53. .\"
  54. .\" Avoid warning from groff about undefined register 'F'.
  55. .de IX
  56. ..
  57. .nr rF 0
  58. .if \n(.g .if rF .nr rF 1
  59. .if (\n(rF:(\n(.g==0)) \{\
  60. . if \nF \{\
  61. . de IX
  62. . tm Index:\\$1\t\\n%\t"\\$2"
  63. ..
  64. . if !\nF==2 \{\
  65. . nr % 0
  66. . nr F 2
  67. . \}
  68. . \}
  69. .\}
  70. .rr rF
  71. .\"
  72. .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
  73. .\" Fear. Run. Save yourself. No user-serviceable parts.
  74. . \" fudge factors for nroff and troff
  75. .if n \{\
  76. . ds #H 0
  77. . ds #V .8m
  78. . ds #F .3m
  79. . ds #[ \f1
  80. . ds #] \fP
  81. .\}
  82. .if t \{\
  83. . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
  84. . ds #V .6m
  85. . ds #F 0
  86. . ds #[ \&
  87. . ds #] \&
  88. .\}
  89. . \" simple accents for nroff and troff
  90. .if n \{\
  91. . ds ' \&
  92. . ds ` \&
  93. . ds ^ \&
  94. . ds , \&
  95. . ds ~ ~
  96. . ds /
  97. .\}
  98. .if t \{\
  99. . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
  100. . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
  101. . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
  102. . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
  103. . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
  104. . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
  105. .\}
  106. . \" troff and (daisy-wheel) nroff accents
  107. .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
  108. .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
  109. .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
  110. .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
  111. .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
  112. .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
  113. .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
  114. .ds ae a\h'-(\w'a'u*4/10)'e
  115. .ds Ae A\h'-(\w'A'u*4/10)'E
  116. . \" corrections for vroff
  117. .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
  118. .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
  119. . \" for low resolution devices (crt and lpr)
  120. .if \n(.H>23 .if \n(.V>19 \
  121. \{\
  122. . ds : e
  123. . ds 8 ss
  124. . ds o a
  125. . ds d- d\h'-1'\(ga
  126. . ds D- D\h'-1'\(hy
  127. . ds th \o'bp'
  128. . ds Th \o'LP'
  129. . ds ae ae
  130. . ds Ae AE
  131. .\}
  132. .rm #[ #] #H #V #F C
  133. .\" ========================================================================
  134. .\"
  135. .IX Title "BN_GENERATE_PRIME 3ossl"
  136. .TH BN_GENERATE_PRIME 3ossl "2024-09-03" "3.3.2" "OpenSSL"
  137. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  138. .\" way too many mistakes in technical documents.
  139. .if n .ad l
  140. .nh
  141. .SH "NAME"
  142. BN_generate_prime_ex2, BN_generate_prime_ex, BN_is_prime_ex, BN_check_prime,
  143. BN_is_prime_fasttest_ex, BN_GENCB_call, BN_GENCB_new, BN_GENCB_free,
  144. BN_GENCB_set_old, BN_GENCB_set, BN_GENCB_get_arg, BN_generate_prime,
  145. BN_is_prime, BN_is_prime_fasttest \- generate primes and test for primality
  146. .SH "SYNOPSIS"
  147. .IX Header "SYNOPSIS"
  148. .Vb 1
  149. \& #include <openssl/bn.h>
  150. \&
  151. \& int BN_generate_prime_ex2(BIGNUM *ret, int bits, int safe,
  152. \& const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb,
  153. \& BN_CTX *ctx);
  154. \&
  155. \& int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
  156. \& const BIGNUM *rem, BN_GENCB *cb);
  157. \&
  158. \& int BN_check_prime(const BIGNUM *p, BN_CTX *ctx, BN_GENCB *cb);
  159. \&
  160. \& int BN_GENCB_call(BN_GENCB *cb, int a, int b);
  161. \&
  162. \& BN_GENCB *BN_GENCB_new(void);
  163. \&
  164. \& void BN_GENCB_free(BN_GENCB *cb);
  165. \&
  166. \& void BN_GENCB_set_old(BN_GENCB *gencb,
  167. \& void (*callback)(int, int, void *), void *cb_arg);
  168. \&
  169. \& void BN_GENCB_set(BN_GENCB *gencb,
  170. \& int (*callback)(int, int, BN_GENCB *), void *cb_arg);
  171. \&
  172. \& void *BN_GENCB_get_arg(BN_GENCB *cb);
  173. .Ve
  174. .PP
  175. The following functions have been deprecated since OpenSSL 0.9.8, and can be
  176. hidden entirely by defining \fB\s-1OPENSSL_API_COMPAT\s0\fR with a suitable version value,
  177. see \fBopenssl_user_macros\fR\|(7):
  178. .PP
  179. .Vb 3
  180. \& BIGNUM *BN_generate_prime(BIGNUM *ret, int num, int safe, BIGNUM *add,
  181. \& BIGNUM *rem, void (*callback)(int, int, void *),
  182. \& void *cb_arg);
  183. \&
  184. \& int BN_is_prime(const BIGNUM *p, int nchecks,
  185. \& void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg);
  186. \&
  187. \& int BN_is_prime_fasttest(const BIGNUM *p, int nchecks,
  188. \& void (*callback)(int, int, void *), BN_CTX *ctx,
  189. \& void *cb_arg, int do_trial_division);
  190. .Ve
  191. .PP
  192. The following functions have been deprecated since OpenSSL 3.0, and can be
  193. hidden entirely by defining \fB\s-1OPENSSL_API_COMPAT\s0\fR with a suitable version value,
  194. see \fBopenssl_user_macros\fR\|(7):
  195. .PP
  196. .Vb 1
  197. \& int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
  198. \&
  199. \& int BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx,
  200. \& int do_trial_division, BN_GENCB *cb);
  201. .Ve
  202. .SH "DESCRIPTION"
  203. .IX Header "DESCRIPTION"
  204. \&\fBBN_generate_prime_ex2()\fR generates a pseudo-random prime number of
  205. at least bit length \fBbits\fR using the \s-1BN_CTX\s0 provided in \fBctx\fR. The value of
  206. \&\fBctx\fR must not be \s-1NULL.\s0
  207. .PP
  208. The returned number is probably prime with a negligible error.
  209. The maximum error rate is 2^\-128.
  210. It's 2^\-287 for a 512 bit prime, 2^\-435 for a 1024 bit prime,
  211. 2^\-648 for a 2048 bit prime, and lower than 2^\-882 for primes larger
  212. than 2048 bit.
  213. .PP
  214. If \fBadd\fR is \fB\s-1NULL\s0\fR the returned prime number will have exact bit
  215. length \fBbits\fR with the top most two bits set.
  216. .PP
  217. If \fBret\fR is not \fB\s-1NULL\s0\fR, it will be used to store the number.
  218. .PP
  219. If \fBcb\fR is not \fB\s-1NULL\s0\fR, it is used as follows:
  220. .IP "\(bu" 2
  221. \&\fBBN_GENCB_call(cb, 0, i)\fR is called after generating the i\-th
  222. potential prime number.
  223. .IP "\(bu" 2
  224. While the number is being tested for primality,
  225. \&\fBBN_GENCB_call(cb, 1, j)\fR is called as described below.
  226. .IP "\(bu" 2
  227. When a prime has been found, \fBBN_GENCB_call(cb, 2, i)\fR is called.
  228. .IP "\(bu" 2
  229. The callers of \fBBN_generate_prime_ex()\fR may call \fBBN_GENCB_call(cb, i, j)\fR with
  230. other values as described in their respective man pages; see \*(L"\s-1SEE ALSO\*(R"\s0.
  231. .PP
  232. The prime may have to fulfill additional requirements for use in
  233. Diffie-Hellman key exchange:
  234. .PP
  235. If \fBadd\fR is not \fB\s-1NULL\s0\fR, the prime will fulfill the condition p % \fBadd\fR
  236. == \fBrem\fR (p % \fBadd\fR == 1 if \fBrem\fR == \fB\s-1NULL\s0\fR) in order to suit a given
  237. generator.
  238. .PP
  239. If \fBsafe\fR is true, it will be a safe prime (i.e. a prime p so
  240. that (p\-1)/2 is also prime). If \fBsafe\fR is true, and \fBrem\fR == \fB\s-1NULL\s0\fR
  241. the condition will be p % \fBadd\fR == 3.
  242. It is recommended that \fBadd\fR is a multiple of 4.
  243. .PP
  244. The random generator must be seeded prior to calling \fBBN_generate_prime_ex()\fR.
  245. If the automatic seeding or reseeding of the OpenSSL \s-1CSPRNG\s0 fails due to
  246. external circumstances (see \s-1\fBRAND\s0\fR\|(7)), the operation will fail.
  247. The random number generator configured for the \s-1OSSL_LIB_CTX\s0 associated with
  248. \&\fBctx\fR will be used.
  249. .PP
  250. \&\fBBN_generate_prime_ex()\fR is the same as \fBBN_generate_prime_ex2()\fR except that no
  251. \&\fBctx\fR parameter is passed.
  252. In this case the random number generator associated with the default \s-1OSSL_LIB_CTX\s0
  253. will be used.
  254. .PP
  255. \&\fBBN_check_prime()\fR, \fBBN_is_prime_ex()\fR, \fBBN_is_prime_fasttest_ex()\fR, \fBBN_is_prime()\fR
  256. and \fBBN_is_prime_fasttest()\fR test if the number \fBp\fR is prime.
  257. The functions tests until one of the tests shows that \fBp\fR is composite,
  258. or all the tests passed.
  259. If \fBp\fR passes all these tests, it is considered a probable prime.
  260. .PP
  261. The test performed on \fBp\fR are trial division by a number of small primes
  262. and rounds of the of the Miller-Rabin probabilistic primality test.
  263. .PP
  264. The functions do at least 64 rounds of the Miller-Rabin test giving a maximum
  265. false positive rate of 2^\-128.
  266. If the size of \fBp\fR is more than 2048 bits, they do at least 128 rounds
  267. giving a maximum false positive rate of 2^\-256.
  268. .PP
  269. If \fBnchecks\fR is larger than the minimum above (64 or 128), \fBnchecks\fR
  270. rounds of the Miller-Rabin test will be done.
  271. .PP
  272. If \fBdo_trial_division\fR set to \fB0\fR, the trial division will be skipped.
  273. \&\fBBN_is_prime_ex()\fR and \fBBN_is_prime()\fR always skip the trial division.
  274. .PP
  275. \&\fBBN_is_prime_ex()\fR, \fBBN_is_prime_fasttest_ex()\fR, \fBBN_is_prime()\fR
  276. and \fBBN_is_prime_fasttest()\fR are deprecated.
  277. .PP
  278. \&\fBBN_is_prime_fasttest()\fR and \fBBN_is_prime()\fR behave just like
  279. \&\fBBN_is_prime_fasttest_ex()\fR and \fBBN_is_prime_ex()\fR respectively, but with the old
  280. style call back.
  281. .PP
  282. \&\fBctx\fR is a preallocated \fB\s-1BN_CTX\s0\fR (to save the overhead of allocating and
  283. freeing the structure in a loop), or \fB\s-1NULL\s0\fR.
  284. .PP
  285. If the trial division is done, and no divisors are found and \fBcb\fR
  286. is not \fB\s-1NULL\s0\fR, \fBBN_GENCB_call(cb, 1, \-1)\fR is called.
  287. .PP
  288. After each round of the Miller-Rabin probabilistic primality test,
  289. if \fBcb\fR is not \fB\s-1NULL\s0\fR, \fBBN_GENCB_call(cb, 1, j)\fR is called
  290. with \fBj\fR the iteration (j = 0, 1, ...).
  291. .PP
  292. \&\fBBN_GENCB_call()\fR calls the callback function held in the \fB\s-1BN_GENCB\s0\fR structure
  293. and passes the ints \fBa\fR and \fBb\fR as arguments. There are two types of
  294. \&\fB\s-1BN_GENCB\s0\fR structure that are supported: \*(L"new\*(R" style and \*(L"old\*(R" style. New
  295. programs should prefer the \*(L"new\*(R" style, whilst the \*(L"old\*(R" style is provided
  296. for backwards compatibility purposes.
  297. .PP
  298. A \fB\s-1BN_GENCB\s0\fR structure should be created through a call to \fBBN_GENCB_new()\fR,
  299. and freed through a call to \fBBN_GENCB_free()\fR. If the argument is \s-1NULL,\s0
  300. nothing is done.
  301. .PP
  302. For \*(L"new\*(R" style callbacks a \s-1BN_GENCB\s0 structure should be initialised with a
  303. call to \fBBN_GENCB_set()\fR, where \fBgencb\fR is a \fB\s-1BN_GENCB\s0 *\fR, \fBcallback\fR is of
  304. type \fBint (*callback)(int, int, \s-1BN_GENCB\s0 *)\fR and \fBcb_arg\fR is a \fBvoid *\fR.
  305. \&\*(L"Old\*(R" style callbacks are the same except they are initialised with a call
  306. to \fBBN_GENCB_set_old()\fR and \fBcallback\fR is of type
  307. \&\fBvoid (*callback)(int, int, void *)\fR.
  308. .PP
  309. A callback is invoked through a call to \fBBN_GENCB_call\fR. This will check
  310. the type of the callback and will invoke \fBcallback(a, b, gencb)\fR for new
  311. style callbacks or \fBcallback(a, b, cb_arg)\fR for old style.
  312. .PP
  313. It is possible to obtain the argument associated with a \s-1BN_GENCB\s0 structure
  314. (set via a call to BN_GENCB_set or BN_GENCB_set_old) using BN_GENCB_get_arg.
  315. .PP
  316. \&\fBBN_generate_prime()\fR (deprecated) works in the same way as
  317. \&\fBBN_generate_prime_ex()\fR but expects an old-style callback function
  318. directly in the \fBcallback\fR parameter, and an argument to pass to it in
  319. the \fBcb_arg\fR. \fBBN_is_prime()\fR and \fBBN_is_prime_fasttest()\fR
  320. can similarly be compared to \fBBN_is_prime_ex()\fR and
  321. \&\fBBN_is_prime_fasttest_ex()\fR, respectively.
  322. .SH "RETURN VALUES"
  323. .IX Header "RETURN VALUES"
  324. \&\fBBN_generate_prime_ex()\fR return 1 on success or 0 on error.
  325. .PP
  326. \&\fBBN_is_prime_ex()\fR, \fBBN_is_prime_fasttest_ex()\fR, \fBBN_is_prime()\fR,
  327. \&\fBBN_is_prime_fasttest()\fR and BN_check_prime return 0 if the number is composite,
  328. 1 if it is prime with an error probability of less than 0.25^\fBnchecks\fR, and
  329. \&\-1 on error.
  330. .PP
  331. \&\fBBN_generate_prime()\fR returns the prime number on success, \fB\s-1NULL\s0\fR otherwise.
  332. .PP
  333. BN_GENCB_new returns a pointer to a \s-1BN_GENCB\s0 structure on success, or \fB\s-1NULL\s0\fR
  334. otherwise.
  335. .PP
  336. BN_GENCB_get_arg returns the argument previously associated with a \s-1BN_GENCB\s0
  337. structure.
  338. .PP
  339. Callback functions should return 1 on success or 0 on error.
  340. .PP
  341. The error codes can be obtained by \fBERR_get_error\fR\|(3).
  342. .SH "REMOVED FUNCTIONALITY"
  343. .IX Header "REMOVED FUNCTIONALITY"
  344. As of OpenSSL 1.1.0 it is no longer possible to create a \s-1BN_GENCB\s0 structure
  345. directly, as in:
  346. .PP
  347. .Vb 1
  348. \& BN_GENCB callback;
  349. .Ve
  350. .PP
  351. Instead applications should create a \s-1BN_GENCB\s0 structure using BN_GENCB_new:
  352. .PP
  353. .Vb 6
  354. \& BN_GENCB *callback;
  355. \& callback = BN_GENCB_new();
  356. \& if (!callback)
  357. \& /* error */
  358. \& ...
  359. \& BN_GENCB_free(callback);
  360. .Ve
  361. .SH "SEE ALSO"
  362. .IX Header "SEE ALSO"
  363. \&\fBDH_generate_parameters\fR\|(3), \fBDSA_generate_parameters\fR\|(3),
  364. \&\fBRSA_generate_key\fR\|(3), \fBERR_get_error\fR\|(3), \fBRAND_bytes\fR\|(3),
  365. \&\s-1\fBRAND\s0\fR\|(7)
  366. .SH "HISTORY"
  367. .IX Header "HISTORY"
  368. The \fBBN_is_prime_ex()\fR and \fBBN_is_prime_fasttest_ex()\fR functions were
  369. deprecated in OpenSSL 3.0.
  370. .PP
  371. The \fBBN_GENCB_new()\fR, \fBBN_GENCB_free()\fR,
  372. and \fBBN_GENCB_get_arg()\fR functions were added in OpenSSL 1.1.0.
  373. .PP
  374. \&\fBBN_check_prime()\fR was added in OpenSSL 3.0.
  375. .SH "COPYRIGHT"
  376. .IX Header "COPYRIGHT"
  377. Copyright 2000\-2024 The OpenSSL Project Authors. All Rights Reserved.
  378. .PP
  379. Licensed under the Apache License 2.0 (the \*(L"License\*(R"). You may not use
  380. this file except in compliance with the License. You can obtain a copy
  381. in the file \s-1LICENSE\s0 in the source distribution or at
  382. <https://www.openssl.org/source/license.html>.