EVP_PKEY_new.3ossl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_NEW 3ossl"
  58. .TH EVP_PKEY_NEW 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,
  65. EVP_PKEY_new,
  66. EVP_PKEY_up_ref,
  67. EVP_PKEY_dup,
  68. EVP_PKEY_free,
  69. EVP_PKEY_new_raw_private_key_ex,
  70. EVP_PKEY_new_raw_private_key,
  71. EVP_PKEY_new_raw_public_key_ex,
  72. EVP_PKEY_new_raw_public_key,
  73. EVP_PKEY_new_CMAC_key,
  74. EVP_PKEY_new_mac_key,
  75. EVP_PKEY_get_raw_private_key,
  76. EVP_PKEY_get_raw_public_key
  77. \&\- public/private key allocation and raw key handling functions
  78. .SH SYNOPSIS
  79. .IX Header "SYNOPSIS"
  80. .Vb 1
  81. \& #include <openssl/evp.h>
  82. \&
  83. \& typedef evp_pkey_st EVP_PKEY;
  84. \&
  85. \& EVP_PKEY *EVP_PKEY_new(void);
  86. \& int EVP_PKEY_up_ref(EVP_PKEY *key);
  87. \& EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *key);
  88. \& void EVP_PKEY_free(EVP_PKEY *key);
  89. \&
  90. \& EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
  91. \& const char *keytype,
  92. \& const char *propq,
  93. \& const unsigned char *key,
  94. \& size_t keylen);
  95. \& EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
  96. \& const unsigned char *key, size_t keylen);
  97. \& EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
  98. \& const char *keytype,
  99. \& const char *propq,
  100. \& const unsigned char *key,
  101. \& size_t keylen);
  102. \& EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
  103. \& const unsigned char *key, size_t keylen);
  104. \& EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, const unsigned char *key,
  105. \& int keylen);
  106. \&
  107. \& int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
  108. \& size_t *len);
  109. \& int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
  110. \& size_t *len);
  111. .Ve
  112. .PP
  113. The following function has been deprecated since OpenSSL 3.0, and can be
  114. hidden entirely by defining \fBOPENSSL_API_COMPAT\fR with a suitable version value,
  115. see \fBopenssl_user_macros\fR\|(7):
  116. .PP
  117. .Vb 2
  118. \& EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
  119. \& size_t len, const EVP_CIPHER *cipher);
  120. .Ve
  121. .SH DESCRIPTION
  122. .IX Header "DESCRIPTION"
  123. \&\fBEVP_PKEY\fR is a generic structure to hold diverse types of asymmetric keys
  124. (also known as "key pairs"), and can be used for diverse operations, like
  125. signing, verifying signatures, key derivation, etc. The asymmetric keys
  126. themselves are often referred to as the "internal key", and are handled by
  127. backends, such as providers (through \fBEVP_KEYMGMT\fR\|(3)) or \fBENGINE\fRs.
  128. .PP
  129. Conceptually, an \fBEVP_PKEY\fR internal key may hold a private key, a public
  130. key, or both (a keypair), and along with those, key parameters if the key type
  131. requires them. The presence of these components determine what operations can
  132. be made; for example, signing normally requires the presence of a private key,
  133. and verifying normally requires the presence of a public key.
  134. .PP
  135. \&\fBEVP_PKEY\fR has also been used for MAC algorithm that were conceived as
  136. producing signatures, although not being public key algorithms; "POLY1305",
  137. "SIPHASH", "HMAC", "CMAC". This usage is considered legacy and is discouraged
  138. in favor of the \fBEVP_MAC\fR\|(3) API.
  139. .PP
  140. The \fBEVP_PKEY_new()\fR function allocates an empty \fBEVP_PKEY\fR structure which is
  141. used by OpenSSL to store public and private keys. The reference count is set to
  142. \&\fB1\fR.
  143. .PP
  144. \&\fBEVP_PKEY_up_ref()\fR increments the reference count of \fIkey\fR.
  145. .PP
  146. \&\fBEVP_PKEY_dup()\fR duplicates the \fIkey\fR. The \fIkey\fR must not be ENGINE based or
  147. a raw key, otherwise the duplication will fail.
  148. .PP
  149. \&\fBEVP_PKEY_free()\fR decrements the reference count of \fIkey\fR and, if the reference
  150. count is zero, frees it up. If \fIkey\fR is NULL, nothing is done.
  151. .PP
  152. \&\fBEVP_PKEY_new_raw_private_key_ex()\fR allocates a new \fBEVP_PKEY\fR. Unless an
  153. engine should be used for the key type, a provider for the key is found using
  154. the library context \fIlibctx\fR and the property query string \fIpropq\fR. The
  155. \&\fIkeytype\fR argument indicates what kind of key this is. The value should be a
  156. string for a public key algorithm that supports raw private keys, i.e one of
  157. "X25519", "ED25519", "X448" or "ED448". \fIkey\fR points to the raw private key
  158. data for this \fBEVP_PKEY\fR which should be of length \fIkeylen\fR. The length
  159. should be appropriate for the type of the key. The public key data will be
  160. automatically derived from the given private key data (if appropriate for the
  161. algorithm type).
  162. .PP
  163. \&\fBEVP_PKEY_new_raw_private_key()\fR does the same as
  164. \&\fBEVP_PKEY_new_raw_private_key_ex()\fR except that the default library context and
  165. default property query are used instead. If \fIe\fR is non-NULL then the new
  166. \&\fBEVP_PKEY\fR structure is associated with the engine \fIe\fR. The \fItype\fR argument
  167. indicates what kind of key this is. The value should be a NID for a public key
  168. algorithm that supports raw private keys, i.e. one of \fBEVP_PKEY_X25519\fR,
  169. \&\fBEVP_PKEY_ED25519\fR, \fBEVP_PKEY_X448\fR or \fBEVP_PKEY_ED448\fR.
  170. .PP
  171. \&\fBEVP_PKEY_new_raw_private_key_ex()\fR and \fBEVP_PKEY_new_raw_private_key()\fR may also
  172. be used with most MACs implemented as public key algorithms, so key types such
  173. as "HMAC", "POLY1305", "SIPHASH", or their NID form \fBEVP_PKEY_POLY1305\fR,
  174. \&\fBEVP_PKEY_SIPHASH\fR, \fBEVP_PKEY_HMAC\fR are also accepted. This usage is,
  175. as mentioned above, discouraged in favor of the \fBEVP_MAC\fR\|(3) API.
  176. .PP
  177. \&\fBEVP_PKEY_new_raw_public_key_ex()\fR works in the same way as
  178. \&\fBEVP_PKEY_new_raw_private_key_ex()\fR except that \fIkey\fR points to the raw
  179. public key data. The \fBEVP_PKEY\fR structure will be initialised without any
  180. private key information. Algorithm types that support raw public keys are
  181. "X25519", "ED25519", "X448" or "ED448".
  182. .PP
  183. \&\fBEVP_PKEY_new_raw_public_key()\fR works in the same way as
  184. \&\fBEVP_PKEY_new_raw_private_key()\fR except that \fIkey\fR points to the raw public key
  185. data. The \fBEVP_PKEY\fR structure will be initialised without any private key
  186. information. Algorithm types that support raw public keys are
  187. \&\fBEVP_PKEY_X25519\fR, \fBEVP_PKEY_ED25519\fR, \fBEVP_PKEY_X448\fR or \fBEVP_PKEY_ED448\fR.
  188. .PP
  189. \&\fBEVP_PKEY_new_mac_key()\fR works in the same way as \fBEVP_PKEY_new_raw_private_key()\fR.
  190. New applications should use \fBEVP_PKEY_new_raw_private_key()\fR instead.
  191. .PP
  192. \&\fBEVP_PKEY_get_raw_private_key()\fR fills the buffer provided by \fIpriv\fR with raw
  193. private key data. The size of the \fIpriv\fR buffer should be in \fI*len\fR on entry
  194. to the function, and on exit \fI*len\fR is updated with the number of bytes
  195. actually written. If the buffer \fIpriv\fR is NULL then \fI*len\fR is populated with
  196. the number of bytes required to hold the key. The calling application is
  197. responsible for ensuring that the buffer is large enough to receive the private
  198. key data. This function only works for algorithms that support raw private keys.
  199. Currently this is: \fBEVP_PKEY_HMAC\fR, \fBEVP_PKEY_POLY1305\fR, \fBEVP_PKEY_SIPHASH\fR,
  200. \&\fBEVP_PKEY_X25519\fR, \fBEVP_PKEY_ED25519\fR, \fBEVP_PKEY_X448\fR or \fBEVP_PKEY_ED448\fR.
  201. .PP
  202. \&\fBEVP_PKEY_get_raw_public_key()\fR fills the buffer provided by \fIpub\fR with raw
  203. public key data. The size of the \fIpub\fR buffer should be in \fI*len\fR on entry
  204. to the function, and on exit \fI*len\fR is updated with the number of bytes
  205. actually written. If the buffer \fIpub\fR is NULL then \fI*len\fR is populated with
  206. the number of bytes required to hold the key. The calling application is
  207. responsible for ensuring that the buffer is large enough to receive the public
  208. key data. This function only works for algorithms that support raw public keys.
  209. Currently this is: \fBEVP_PKEY_X25519\fR, \fBEVP_PKEY_ED25519\fR, \fBEVP_PKEY_X448\fR or
  210. \&\fBEVP_PKEY_ED448\fR.
  211. .PP
  212. \&\fBEVP_PKEY_new_CMAC_key()\fR works in the same way as \fBEVP_PKEY_new_raw_private_key()\fR
  213. except it is only for the \fBEVP_PKEY_CMAC\fR algorithm type. In addition to the
  214. raw private key data, it also takes a cipher algorithm to be used during
  215. creation of a CMAC in the \fBcipher\fR argument. The cipher should be a standard
  216. encryption-only cipher. For example AEAD and XTS ciphers should not be used.
  217. .PP
  218. Applications should use the \fBEVP_MAC\fR\|(3) API instead
  219. and set the \fBOSSL_MAC_PARAM_CIPHER\fR parameter on the \fBEVP_MAC_CTX\fR object
  220. with the name of the cipher being used.
  221. .SH NOTES
  222. .IX Header "NOTES"
  223. The \fBEVP_PKEY\fR structure is used by various OpenSSL functions which require a
  224. general private key without reference to any particular algorithm.
  225. .PP
  226. The structure returned by \fBEVP_PKEY_new()\fR is empty. To add a private or public
  227. key to this empty structure use the appropriate functions described in
  228. \&\fBEVP_PKEY_set1_RSA\fR\|(3), \fBEVP_PKEY_set1_DSA\fR\|(3), \fBEVP_PKEY_set1_DH\fR\|(3) or
  229. \&\fBEVP_PKEY_set1_EC_KEY\fR\|(3).
  230. .SH "RETURN VALUES"
  231. .IX Header "RETURN VALUES"
  232. \&\fBEVP_PKEY_new()\fR, \fBEVP_PKEY_new_raw_private_key()\fR, \fBEVP_PKEY_new_raw_public_key()\fR,
  233. \&\fBEVP_PKEY_new_CMAC_key()\fR and \fBEVP_PKEY_new_mac_key()\fR return either the newly
  234. allocated \fBEVP_PKEY\fR structure or NULL if an error occurred.
  235. .PP
  236. \&\fBEVP_PKEY_dup()\fR returns the key duplicate or NULL if an error occurred.
  237. .PP
  238. \&\fBEVP_PKEY_up_ref()\fR, \fBEVP_PKEY_get_raw_private_key()\fR and
  239. \&\fBEVP_PKEY_get_raw_public_key()\fR return 1 for success and 0 for failure.
  240. .SH "SEE ALSO"
  241. .IX Header "SEE ALSO"
  242. \&\fBEVP_PKEY_set1_RSA\fR\|(3), \fBEVP_PKEY_set1_DSA\fR\|(3), \fBEVP_PKEY_set1_DH\fR\|(3) or
  243. \&\fBEVP_PKEY_set1_EC_KEY\fR\|(3)
  244. .SH HISTORY
  245. .IX Header "HISTORY"
  246. The
  247. \&\fBEVP_PKEY_new()\fR and \fBEVP_PKEY_free()\fR functions exist in all versions of OpenSSL.
  248. .PP
  249. The \fBEVP_PKEY_up_ref()\fR function was added in OpenSSL 1.1.0.
  250. .PP
  251. The
  252. \&\fBEVP_PKEY_new_raw_private_key()\fR, \fBEVP_PKEY_new_raw_public_key()\fR,
  253. \&\fBEVP_PKEY_new_CMAC_key()\fR, \fBEVP_PKEY_new_raw_private_key()\fR and
  254. \&\fBEVP_PKEY_get_raw_public_key()\fR functions were added in OpenSSL 1.1.1.
  255. .PP
  256. The \fBEVP_PKEY_dup()\fR, \fBEVP_PKEY_new_raw_private_key_ex()\fR, and
  257. \&\fBEVP_PKEY_new_raw_public_key_ex()\fR
  258. functions were added in OpenSSL 3.0.
  259. .PP
  260. The \fBEVP_PKEY_new_CMAC_key()\fR was deprecated in OpenSSL 3.0.
  261. .PP
  262. The documentation of \fBEVP_PKEY\fR was amended in OpenSSL 3.0 to allow there to
  263. be the private part of the keypair without the public part, where this was
  264. previously implied to be disallowed.
  265. .SH COPYRIGHT
  266. .IX Header "COPYRIGHT"
  267. Copyright 2002\-2022 The OpenSSL Project Authors. All Rights Reserved.
  268. .PP
  269. Licensed under the Apache License 2.0 (the "License"). You may not use
  270. this file except in compliance with the License. You can obtain a copy
  271. in the file LICENSE in the source distribution or at
  272. <https://www.openssl.org/source/license.html>.