EVP_PKEY_keygen.3ossl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "EVP_PKEY_KEYGEN 3ossl"
  58. .TH EVP_PKEY_KEYGEN 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. EVP_PKEY_Q_keygen,
  65. EVP_PKEY_keygen_init, EVP_PKEY_paramgen_init, EVP_PKEY_generate,
  66. EVP_PKEY_CTX_set_cb, EVP_PKEY_CTX_get_cb,
  67. EVP_PKEY_CTX_get_keygen_info, EVP_PKEY_CTX_set_app_data,
  68. EVP_PKEY_CTX_get_app_data,
  69. EVP_PKEY_gen_cb,
  70. EVP_PKEY_paramgen, EVP_PKEY_keygen
  71. \&\- key and parameter generation and check functions
  72. .SH SYNOPSIS
  73. .IX Header "SYNOPSIS"
  74. .Vb 1
  75. \& #include <openssl/evp.h>
  76. \&
  77. \& EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
  78. \& const char *type, ...);
  79. \&
  80. \& int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
  81. \& int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
  82. \& int EVP_PKEY_generate(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
  83. \& int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
  84. \& int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
  85. \&
  86. \& typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);
  87. \&
  88. \& void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb);
  89. \& EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx);
  90. \&
  91. \& int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx);
  92. \&
  93. \& void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data);
  94. \& void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx);
  95. .Ve
  96. .SH DESCRIPTION
  97. .IX Header "DESCRIPTION"
  98. Generating keys is sometimes straight forward, just generate the key's
  99. numbers and be done with it. However, there are certain key types that need
  100. key parameters, often called domain parameters but not necessarily limited
  101. to that, that also need to be generated. In addition to this, the caller
  102. may want to set user provided generation parameters that further affect key
  103. parameter or key generation, such as the desired key size.
  104. .PP
  105. To flexibly allow all that's just been described, key parameter and key
  106. generation is divided into an initialization of a key algorithm context,
  107. functions to set user provided parameters, and finally the key parameter or
  108. key generation function itself.
  109. .PP
  110. The key algorithm context must be created using \fBEVP_PKEY_CTX_new\fR\|(3) or
  111. variants thereof, see that manual for details.
  112. .PP
  113. \&\fBEVP_PKEY_keygen_init()\fR initializes a public key algorithm context \fIctx\fR
  114. for a key generation operation.
  115. .PP
  116. \&\fBEVP_PKEY_paramgen_init()\fR is similar to \fBEVP_PKEY_keygen_init()\fR except key
  117. parameters are generated.
  118. .PP
  119. After initialization, generation parameters may be provided with
  120. \&\fBEVP_PKEY_CTX_ctrl\fR\|(3) or \fBEVP_PKEY_CTX_set_params\fR\|(3), or any other
  121. function described in those manuals.
  122. .PP
  123. \&\fBEVP_PKEY_generate()\fR performs the generation operation, the resulting key
  124. parameters or key are written to \fI*ppkey\fR. If \fI*ppkey\fR is NULL when this
  125. function is called, it will be allocated, and should be freed by the caller
  126. when no longer useful, using \fBEVP_PKEY_free\fR\|(3).
  127. .PP
  128. \&\fBEVP_PKEY_paramgen()\fR and \fBEVP_PKEY_keygen()\fR do exactly the same thing as
  129. \&\fBEVP_PKEY_generate()\fR, after checking that the corresponding \fBEVP_PKEY_paramgen_init()\fR
  130. or \fBEVP_PKEY_keygen_init()\fR was used to initialize \fIctx\fR.
  131. These are older functions that are kept for backward compatibility.
  132. It is safe to use \fBEVP_PKEY_generate()\fR instead.
  133. .PP
  134. The function \fBEVP_PKEY_set_cb()\fR sets the key or parameter generation callback
  135. to \fIcb\fR. The function \fBEVP_PKEY_CTX_get_cb()\fR returns the key or parameter
  136. generation callback.
  137. .PP
  138. The function \fBEVP_PKEY_CTX_get_keygen_info()\fR returns parameters associated
  139. with the generation operation. If \fIidx\fR is \-1 the total number of
  140. parameters available is returned. Any non negative value returns the value of
  141. that parameter. \fBEVP_PKEY_CTX_gen_keygen_info()\fR with a nonnegative value for
  142. \&\fIidx\fR should only be called within the generation callback.
  143. .PP
  144. If the callback returns 0 then the key generation operation is aborted and an
  145. error occurs. This might occur during a time consuming operation where
  146. a user clicks on a "cancel" button.
  147. .PP
  148. The functions \fBEVP_PKEY_CTX_set_app_data()\fR and \fBEVP_PKEY_CTX_get_app_data()\fR set
  149. and retrieve an opaque pointer. This can be used to set some application
  150. defined value which can be retrieved in the callback: for example a handle
  151. which is used to update a "progress dialog".
  152. .PP
  153. \&\fBEVP_PKEY_Q_keygen()\fR abstracts from the explicit use of \fBEVP_PKEY_CTX\fR while
  154. providing a 'quick' but limited way of generating a new asymmetric key pair.
  155. It provides shorthands for simple and common cases of key generation.
  156. As usual, the library context \fIlibctx\fR and property query \fIpropq\fR
  157. can be given for fetching algorithms from providers.
  158. If \fItype\fR is \f(CW\*(C`RSA\*(C'\fR,
  159. a \fBsize_t\fR parameter must be given to specify the size of the RSA key.
  160. If \fItype\fR is \f(CW\*(C`EC\*(C'\fR,
  161. a string parameter must be given to specify the name of the EC curve.
  162. If \fItype\fR is \f(CW\*(C`X25519\*(C'\fR, \f(CW\*(C`X448\*(C'\fR, \f(CW\*(C`ED25519\*(C'\fR, \f(CW\*(C`ED448\*(C'\fR, or \f(CW\*(C`SM2\*(C'\fR
  163. no further parameter is needed.
  164. .SH "RETURN VALUES"
  165. .IX Header "RETURN VALUES"
  166. \&\fBEVP_PKEY_keygen_init()\fR, \fBEVP_PKEY_paramgen_init()\fR, \fBEVP_PKEY_keygen()\fR and
  167. \&\fBEVP_PKEY_paramgen()\fR return 1 for success and 0 or a negative value for failure.
  168. In particular a return value of \-2 indicates the operation is not supported by
  169. the public key algorithm.
  170. .PP
  171. \&\fBEVP_PKEY_Q_keygen()\fR returns an \fBEVP_PKEY\fR, or NULL on failure.
  172. .SH NOTES
  173. .IX Header "NOTES"
  174. After the call to \fBEVP_PKEY_keygen_init()\fR or \fBEVP_PKEY_paramgen_init()\fR algorithm
  175. specific control operations can be performed to set any appropriate parameters
  176. for the operation.
  177. .PP
  178. The functions \fBEVP_PKEY_keygen()\fR and \fBEVP_PKEY_paramgen()\fR can be called more than
  179. once on the same context if several operations are performed using the same
  180. parameters.
  181. .PP
  182. The meaning of the parameters passed to the callback will depend on the
  183. algorithm and the specific implementation of the algorithm. Some might not
  184. give any useful information at all during key or parameter generation. Others
  185. might not even call the callback.
  186. .PP
  187. The operation performed by key or parameter generation depends on the algorithm
  188. used. In some cases (e.g. EC with a supplied named curve) the "generation"
  189. option merely sets the appropriate fields in an EVP_PKEY structure.
  190. .PP
  191. In OpenSSL an EVP_PKEY structure containing a private key also contains the
  192. public key components and parameters (if any). An OpenSSL private key is
  193. equivalent to what some libraries call a "key pair". A private key can be used
  194. in functions which require the use of a public key or parameters.
  195. .SH EXAMPLES
  196. .IX Header "EXAMPLES"
  197. Generate a 2048 bit RSA key:
  198. .PP
  199. .Vb 2
  200. \& #include <openssl/evp.h>
  201. \& #include <openssl/rsa.h>
  202. \&
  203. \& EVP_PKEY_CTX *ctx;
  204. \& EVP_PKEY *pkey = NULL;
  205. \&
  206. \& ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
  207. \& if (!ctx)
  208. \& /* Error occurred */
  209. \& if (EVP_PKEY_keygen_init(ctx) <= 0)
  210. \& /* Error */
  211. \& if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0)
  212. \& /* Error */
  213. \&
  214. \& /* Generate key */
  215. \& if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
  216. \& /* Error */
  217. .Ve
  218. .PP
  219. Generate a key from a set of parameters:
  220. .PP
  221. .Vb 2
  222. \& #include <openssl/evp.h>
  223. \& #include <openssl/rsa.h>
  224. \&
  225. \& EVP_PKEY_CTX *ctx;
  226. \& ENGINE *eng;
  227. \& EVP_PKEY *pkey = NULL, *param;
  228. \&
  229. \& /* Assumed param, eng are set up already */
  230. \& ctx = EVP_PKEY_CTX_new(param, eng);
  231. \& if (!ctx)
  232. \& /* Error occurred */
  233. \& if (EVP_PKEY_keygen_init(ctx) <= 0)
  234. \& /* Error */
  235. \&
  236. \& /* Generate key */
  237. \& if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
  238. \& /* Error */
  239. .Ve
  240. .PP
  241. Example of generation callback for OpenSSL public key implementations:
  242. .PP
  243. .Vb 1
  244. \& /* Application data is a BIO to output status to */
  245. \&
  246. \& EVP_PKEY_CTX_set_app_data(ctx, status_bio);
  247. \&
  248. \& static int genpkey_cb(EVP_PKEY_CTX *ctx)
  249. \& {
  250. \& char c = \*(Aq*\*(Aq;
  251. \& BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  252. \& int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  253. \&
  254. \& if (p == 0)
  255. \& c = \*(Aq.\*(Aq;
  256. \& if (p == 1)
  257. \& c = \*(Aq+\*(Aq;
  258. \& if (p == 2)
  259. \& c = \*(Aq*\*(Aq;
  260. \& if (p == 3)
  261. \& c = \*(Aq\en\*(Aq;
  262. \& BIO_write(b, &c, 1);
  263. \& (void)BIO_flush(b);
  264. \& return 1;
  265. \& }
  266. .Ve
  267. .SH "SEE ALSO"
  268. .IX Header "SEE ALSO"
  269. \&\fBEVP_RSA_gen\fR\|(3), \fBEVP_EC_gen\fR\|(3),
  270. \&\fBEVP_PKEY_CTX_new\fR\|(3),
  271. \&\fBEVP_PKEY_encrypt\fR\|(3),
  272. \&\fBEVP_PKEY_decrypt\fR\|(3),
  273. \&\fBEVP_PKEY_sign\fR\|(3),
  274. \&\fBEVP_PKEY_verify\fR\|(3),
  275. \&\fBEVP_PKEY_verify_recover\fR\|(3),
  276. \&\fBEVP_PKEY_derive\fR\|(3)
  277. .SH HISTORY
  278. .IX Header "HISTORY"
  279. \&\fBEVP_PKEY_keygen_init()\fR, int \fBEVP_PKEY_paramgen_init()\fR, \fBEVP_PKEY_keygen()\fR,
  280. \&\fBEVP_PKEY_paramgen()\fR, \fBEVP_PKEY_gen_cb()\fR, \fBEVP_PKEY_CTX_set_cb()\fR,
  281. \&\fBEVP_PKEY_CTX_get_cb()\fR, \fBEVP_PKEY_CTX_get_keygen_info()\fR,
  282. \&\fBEVP_PKEY_CTX_set_app_data()\fR and \fBEVP_PKEY_CTX_get_app_data()\fR were added in
  283. OpenSSL 1.0.0.
  284. .PP
  285. \&\fBEVP_PKEY_Q_keygen()\fR and \fBEVP_PKEY_generate()\fR were added in OpenSSL 3.0.
  286. .SH COPYRIGHT
  287. .IX Header "COPYRIGHT"
  288. Copyright 2006\-2021 The OpenSSL Project Authors. All Rights Reserved.
  289. .PP
  290. Licensed under the Apache License 2.0 (the "License"). You may not use
  291. this file except in compliance with the License. You can obtain a copy
  292. in the file LICENSE in the source distribution or at
  293. <https://www.openssl.org/source/license.html>.