provider-cipher.pod 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. =pod
  2. =head1 NAME
  3. provider-cipher - The cipher library E<lt>-E<gt> provider functions
  4. =head1 SYNOPSIS
  5. =for openssl multiple includes
  6. #include <openssl/core_numbers.h>
  7. #include <openssl/core_names.h>
  8. /*
  9. * None of these are actual functions, but are displayed like this for
  10. * the function signatures for functions that are offered as function
  11. * pointers in OSSL_DISPATCH arrays.
  12. */
  13. /* Context management */
  14. void *OP_cipher_newctx(void *provctx);
  15. void OP_cipher_freectx(void *cctx);
  16. void *OP_cipher_dupctx(void *cctx);
  17. /* Encryption/decryption */
  18. int OP_cipher_encrypt_init(void *cctx, const unsigned char *key,
  19. size_t keylen, const unsigned char *iv,
  20. size_t ivlen);
  21. int OP_cipher_decrypt_init(void *cctx, const unsigned char *key,
  22. size_t keylen, const unsigned char *iv,
  23. size_t ivlen);
  24. int OP_cipher_update(void *cctx, unsigned char *out, size_t *outl,
  25. size_t outsize, const unsigned char *in, size_t inl);
  26. int OP_cipher_final(void *cctx, unsigned char *out, size_t *outl,
  27. size_t outsize);
  28. int OP_cipher_cipher(void *cctx, unsigned char *out, size_t *outl,
  29. size_t outsize, const unsigned char *in, size_t inl);
  30. /* Cipher parameter descriptors */
  31. const OSSL_PARAM *OP_cipher_gettable_params(void);
  32. /* Cipher operation parameter descriptors */
  33. const OSSL_PARAM *OP_cipher_gettable_ctx_params(void);
  34. const OSSL_PARAM *OP_cipher_settable_ctx_params(void);
  35. /* Cipher parameters */
  36. int OP_cipher_get_params(OSSL_PARAM params[]);
  37. /* Cipher operation parameters */
  38. int OP_cipher_get_ctx_params(void *cctx, OSSL_PARAM params[]);
  39. int OP_cipher_set_ctx_params(void *cctx, const OSSL_PARAM params[]);
  40. =head1 DESCRIPTION
  41. This documentation is primarily aimed at provider authors. See L<provider(7)>
  42. for further information.
  43. The CIPHER operation enables providers to implement cipher algorithms and make
  44. them available to applications via the API functions L<EVP_EncryptInit_ex(3)>,
  45. L<EVP_EncryptUpdate(3)> and L<EVP_EncryptFinal(3)> (as well as the decrypt
  46. equivalents and other related functions).
  47. All "functions" mentioned here are passed as function pointers between
  48. F<libcrypto> and the provider in B<OSSL_DISPATCH> arrays via
  49. B<OSSL_ALGORITHM> arrays that are returned by the provider's
  50. provider_query_operation() function
  51. (see L<provider-base(7)/Provider Functions>).
  52. All these "functions" have a corresponding function type definition
  53. named B<OSSL_{name}_fn>, and a helper function to retrieve the
  54. function pointer from an B<OSSL_DISPATCH> element named
  55. B<OSSL_get_{name}>.
  56. For example, the "function" OP_cipher_newctx() has these:
  57. typedef void *(OSSL_OP_cipher_newctx_fn)(void *provctx);
  58. static ossl_inline OSSL_OP_cipher_newctx_fn
  59. OSSL_get_OP_cipher_newctx(const OSSL_DISPATCH *opf);
  60. B<OSSL_DISPATCH> arrays are indexed by numbers that are provided as
  61. macros in L<openssl-core_numbers.h(7)>, as follows:
  62. OP_cipher_newctx OSSL_FUNC_CIPHER_NEWCTX
  63. OP_cipher_freectx OSSL_FUNC_CIPHER_FREECTX
  64. OP_cipher_dupctx OSSL_FUNC_CIPHER_DUPCTX
  65. OP_cipher_encrypt_init OSSL_FUNC_CIPHER_ENCRYPT_INIT
  66. OP_cipher_decrypt_init OSSL_FUNC_CIPHER_DECRYPT_INIT
  67. OP_cipher_update OSSL_FUNC_CIPHER_UPDATE
  68. OP_cipher_final OSSL_FUNC_CIPHER_FINAL
  69. OP_cipher_cipher OSSL_FUNC_CIPHER_CIPHER
  70. OP_cipher_get_params OSSL_FUNC_CIPHER_GET_PARAMS
  71. OP_cipher_get_ctx_params OSSL_FUNC_CIPHER_GET_CTX_PARAMS
  72. OP_cipher_set_ctx_params OSSL_FUNC_CIPHER_SET_CTX_PARAMS
  73. OP_cipher_gettable_params OSSL_FUNC_CIPHER_GETTABLE_PARAMS
  74. OP_cipher_gettable_ctx_params OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS
  75. OP_cipher_settable_ctx_params OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS
  76. A cipher algorithm implementation may not implement all of these functions.
  77. In order to be a consistent set of functions there must at least be a complete
  78. set of "encrypt" functions, or a complete set of "decrypt" functions, or a
  79. single "cipher" function.
  80. In all cases both the OP_cipher_newctx and OP_cipher_freectx functions must be
  81. present.
  82. All other functions are optional.
  83. =head2 Context Management Functions
  84. OP_cipher_newctx() should create and return a pointer to a provider side
  85. structure for holding context information during a cipher operation.
  86. A pointer to this context will be passed back in a number of the other cipher
  87. operation function calls.
  88. The parameter I<provctx> is the provider context generated during provider
  89. initialisation (see L<provider(7)>).
  90. OP_cipher_freectx() is passed a pointer to the provider side cipher context in
  91. the I<cctx> parameter.
  92. This function should free any resources associated with that context.
  93. OP_cipher_dupctx() should duplicate the provider side cipher context in the
  94. I<cctx> parameter and return the duplicate copy.
  95. =head2 Encryption/Decryption Functions
  96. OP_cipher_encrypt_init() initialises a cipher operation for encryption given a
  97. newly created provider side cipher context in the I<cctx> parameter.
  98. The key to be used is given in I<key> which is I<keylen> bytes long.
  99. The IV to be used is given in I<iv> which is I<ivlen> bytes long.
  100. OP_cipher_decrypt_init() is the same as OP_cipher_encrypt_init() except that it
  101. initialises the context for a decryption operation.
  102. OP_cipher_update() is called to supply data to be encrypted/decrypted as part of
  103. a previously initialised cipher operation.
  104. The I<cctx> parameter contains a pointer to a previously initialised provider
  105. side context.
  106. OP_cipher_update() should encrypt/decrypt I<inl> bytes of data at the location
  107. pointed to by I<in>.
  108. The encrypted data should be stored in I<out> and the amount of data written to
  109. I<*outl> which should not exceed I<outsize> bytes.
  110. OP_cipher_update() may be called multiple times for a single cipher operation.
  111. It is the responsibility of the cipher implementation to handle input lengths
  112. that are not multiples of the block length.
  113. In such cases a cipher implementation will typically cache partial blocks of
  114. input data until a complete block is obtained.
  115. I<out> may be the same location as I<in> but it should not partially overlap.
  116. The same expectations apply to I<outsize> as documented for
  117. L<EVP_EncryptUpdate(3)> and L<EVP_DecryptUpdate(3)>.
  118. OP_cipher_final() completes an encryption or decryption started through previous
  119. OP_cipher_encrypt_init() or OP_cipher_decrypt_init(), and OP_cipher_update()
  120. calls.
  121. The I<cctx> parameter contains a pointer to the provider side context.
  122. Any final encryption/decryption output should be written to I<out> and the
  123. amount of data written to I<*outl> which should not exceed I<outsize> bytes.
  124. The same expectations apply to I<outsize> as documented for
  125. L<EVP_EncryptFinal(3)> and L<EVP_DecryptFinal(3)>.
  126. OP_cipher_cipher() performs encryption/decryption using the provider side cipher
  127. context in the I<cctx> parameter that should have been previously initialised via
  128. a call to OP_cipher_encrypt_init() or OP_cipher_decrypt_init().
  129. This should call the raw underlying cipher function without any padding.
  130. This will be invoked in the provider as a result of the application calling
  131. L<EVP_Cipher(3)>.
  132. The application is responsible for ensuring that the input is a multiple of the
  133. block length.
  134. The data to be encrypted/decrypted will be in I<in>, and it will be I<inl> bytes
  135. in length.
  136. The output from the encryption/decryption should be stored in I<out> and the
  137. amount of data stored should be put in I<*outl> which should be no more than
  138. I<outsize> bytes.
  139. =head2 Cipher Parameters
  140. See L<OSSL_PARAM(3)> for further details on the parameters structure used by
  141. these functions.
  142. OP_cipher_get_params() gets details of the algorithm implementation
  143. and stores them in I<params>.
  144. OP_cipher_set_ctx_params() sets cipher operation parameters for the
  145. provider side cipher context I<cctx> to I<params>.
  146. Any parameter settings are additional to any that were previously set.
  147. OP_cipher_get_ctx_params() gets cipher operation details details from
  148. the given provider side cipher context I<cctx> and stores them in I<params>.
  149. OP_cipher_gettable_params(), OP_cipher_gettable_ctx_params(), and
  150. OP_cipher_settable_ctx_params() all return constant B<OSSL_PARAM> arrays
  151. as descriptors of the parameters that OP_cipher_get_params(),
  152. OP_cipher_get_ctx_params(), and OP_cipher_set_ctx_params() can handle,
  153. respectively.
  154. Parameters currently recognised by built-in ciphers are as follows. Not all
  155. parameters are relevant to, or are understood by all ciphers:
  156. =over 4
  157. =item "padding" (B<OSSL_CIPHER_PARAM_PADDING>) <unsigned integer>
  158. Sets the padding mode for the associated cipher ctx.
  159. Setting a value of 1 will turn padding on.
  160. Setting a value of 0 will turn padding off.
  161. =item "mode" (B<OSSL_CIPHER_PARAM_MODE>) <unsigned integer>
  162. Gets the mode for the associated cipher algorithm.
  163. See L<EVP_CIPHER_mode(3)> for a list of valid modes.
  164. =item "blocksize" (B<OSSL_CIPHER_PARAM_BLOCK_SIZE>) <unsigned integer>
  165. Gets the block size for the associated cipher algorithm.
  166. The block size should be 1 for stream ciphers.
  167. Note that the block size for a cipher may be different to the block size for
  168. the underlying encryption/decryption primitive.
  169. For example AES in CTR mode has a block size of 1 (because it operates like a
  170. stream cipher), even though AES has a block size of 16.
  171. The length of the "blocksize" parameter should not exceed that of a B<size_t>.
  172. =item "flags" (B<OSSL_CIPHER_PARAM_FLAGS>) <unsigned integer>
  173. Gets any flags for the associated cipher algorithm.
  174. See L<EVP_CIPHER_meth_set_flags(3)> for a list of currently defined cipher
  175. flags.
  176. The length of the "flags" parameter should equal that of an
  177. B<unsigned long int>.
  178. =item "keylen" (B<OSSL_CIPHER_PARAM_KEYLEN>) <unsigned integer>
  179. Gets the key length for the associated cipher algorithm.
  180. This can also be used to get or set the key length for the associated cipher
  181. ctx.
  182. The length of the "keylen" parameter should not exceed that of a B<size_t>.
  183. =item "ivlen" (B<OSSL_CIPHER_PARAM_IVLEN>) <unsigned integer>
  184. Gets the IV length for the associated cipher algorithm.
  185. The length of the "ivlen" parameter should not exceed that of a B<size_t>.
  186. =item "iv" (B<OSSL_CIPHER_PARAM_IV>) <octet string OR octet ptr>
  187. Gets the IV for the associated cipher ctx.
  188. =item "num" (B<OSSL_CIPHER_PARAM_NUM>) <unsigned integer>
  189. Gets or sets the cipher specific "num" parameter for the associated cipher ctx.
  190. Built-in ciphers typically use this to track how much of the current underlying
  191. block has been "used" already.
  192. =item "tag" (B<OSSL_CIPHER_PARAM_AEAD_TAG>) <octet string>
  193. Gets or sets the AEAD tag for the associated cipher ctx.
  194. See L<EVP_EncryptInit(3)/AEAD Interface>.
  195. =item "taglen" (B<OSSL_CIPHER_PARAM_AEAD_TAGLEN>) <unsigned integer>
  196. Gets the tag length to be used for an AEAD cipher for the associated cipher ctx.
  197. It returns a default value if it has not been set.
  198. The length of the "taglen" parameter should not exceed that of a B<size_t>.
  199. =item "tlsaad" (B<OSSL_CIPHER_PARAM_AEAD_TLS1_AAD>) <octet string>
  200. =for comment TODO(3.0): Consider changing this interface so that all ciphers
  201. use the standard AEAD interface - rather than having this special purpose
  202. interface for TLS
  203. Sets TLSv1.2 AAD information for the associated cipher ctx.
  204. TLSv1.2 AAD information is always 13 bytes in length and is as defined for the
  205. "additional_data" field described in section 6.2.3.3 of RFC5246.
  206. =item "tlsaadpad" (B<OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD>) <unsigned integer>
  207. Gets the length of the tag that will be added to a TLS record for the AEAD
  208. tag for the associated cipher ctx.
  209. The length of the "tlsaadpad" parameter should not exceed that of a B<size_t>.
  210. =item "tlsivfixed" (B<OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED>) <octet string>
  211. =for comment TODO(3.0): This interface needs completely redesigning!
  212. Sets the fixed portion of an IV for an AEAD cipher used in a TLS record
  213. encryption/ decryption for the associated cipher ctx.
  214. TLS record encryption/decryption always occurs "in place" so that the input and
  215. output buffers are always the same memory location.
  216. AEAD IVs in TLSv1.2 consist of an implicit "fixed" part and an explicit part
  217. that varies with every record.
  218. Setting a TLS fixed IV changes a cipher to encrypt/decrypt TLS records.
  219. TLS records are encrypted/decrypted using a single OP_cipher_cipher call per
  220. record.
  221. For a record decryption the first bytes of the input buffer will be the explicit
  222. part of the IV and the final bytes of the input buffer will be the AEAD tag.
  223. The length of the explicit part of the IV and the tag length will depend on the
  224. cipher in use and will be defined in the RFC for the relevant ciphersuite.
  225. In order to allow for "in place" decryption the plaintext output should be
  226. written to the same location in the output buffer that the ciphertext payload
  227. was read from, i.e. immediately after the explicit IV.
  228. When encrypting a record the first bytes of the input buffer will be empty to
  229. allow space for the explicit IV, as will the final bytes where the tag will
  230. be written.
  231. The length of the input buffer will include the length of the explicit IV, the
  232. payload, and the tag bytes.
  233. The cipher implementation should generate the explicit IV and write it to the
  234. beginning of the output buffer, do "in place" encryption of the payload and
  235. write that to the output buffer, and finally add the tag onto the end of the
  236. output buffer.
  237. Whether encrypting or decrypting the value written to I<*outl> in the
  238. OP_cipher_cipher call should be the length of the payload excluding the explicit
  239. IV length and the tag length.
  240. =item "ivlen" (B<OSSL_CIPHER_PARAM_AEAD_IVLEN>) <unsigned integer>
  241. Sets the IV length to be used for an AEAD cipher for the associated cipher ctx.
  242. The length of the "ivlen" parameter should not exceed that of a B<size_t>.
  243. =item "mackey" (B<OSSL_CIPHER_PARAM_AEAD_MAC_KEY>) <octet string>
  244. Sets the MAC key used by composite AEAD ciphers such as AES-CBC-HMAC-SHA256.
  245. =item "randkey" (B<OSSL_CIPHER_PARAM_RANDOM_KEY>) <octet string>
  246. Gets a implementation specific randomly generated key for the associated
  247. cipher ctx. This is currently only supported by 3DES (which sets the key to
  248. odd parity).
  249. =item "alg_id_param" (B<OSSL_CIPHER_PARAM_ALG_ID>) <octet string>
  250. Used to pass the DER encoded AlgorithmIdentifier parameter to or from
  251. the cipher implementation. Functions like L<EVP_CIPHER_param_to_asn1(3)>
  252. and L<EVP_CIPHER_asn1_to_param(3)> use this parameter for any implementation
  253. that has the flag B<EVP_CIPH_FLAG_CUSTOM_ASN1> set.
  254. =item "rounds" (B<OSSL_CIPHER_PARAM_ROUNDS>) <unsigned integer>
  255. Sets or gets the number of rounds to be used for a cipher.
  256. This is used by the RC5 cipher.
  257. =item "keybits" (B<OSSL_CIPHER_PARAM_RC2_KEYBITS>) <unsigned integer>
  258. Gets or sets the effective keybits used for a RC2 cipher.
  259. The length of the "keybits" parameter should not exceed that of a B<size_t>.
  260. =item "speed" (B<OSSL_CIPHER_PARAM_SPEED>) <unsigned integer>
  261. Sets the speed option for the associated cipher ctx. This is only supported
  262. by AES SIV ciphers which disallow multiple operations by default.
  263. Setting "speed" to 1 allows another encrypt or decrypt operation to be
  264. performed. This is used for performance testing.
  265. =item "tlsivgen" (B<OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN>) <octet string>
  266. Gets the invocation field generated for encryption.
  267. Can only be called after "tlsivfixed" is set.
  268. This is only used for GCM mode.
  269. =item "tlsivinv" (B<OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV>) <octet string>
  270. Sets the invocation field used for decryption.
  271. Can only be called after "tlsivfixed" is set.
  272. This is only used for GCM mode.
  273. =item "tls1multi_enc" (B<OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC>) <octet string>
  274. Triggers a multiblock tls1 encrypt operation for a tls1 aware cipher that supports
  275. sending 4 or 8 records in one go.
  276. The cipher performs both the MAC and encrypt stages and constructs the record
  277. headers itself.
  278. "tls1multi_enc" supplies the output buffer for the encrypt operation,
  279. "tls1multi_encin" & "tls1multi_interleave" must also be set in order to supply
  280. values to the encrypt operation.
  281. =item "tls1multi_enclen" (B<OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN>) <unsigned integer>
  282. Get the total length of the record returned from the "tls1multi_enc" operation.
  283. =item "tls1multi_interleave" (B<OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE>) <unsigned integer>
  284. Sets or gets the number of records being sent in one go for a tls1 multiblock
  285. cipher operation (either 4 or 8 records).
  286. =item "tls1multi_encin" (B<OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN>) <octet string>
  287. Supplies the data to encrypt for a tls1 multiblock cipher operation.
  288. =item "tls1multi_maxsndfrag" (B<OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT>) <unsigned integer>
  289. Sets the maximum send fragment size for a tls1 multiblock cipher operation.
  290. It must be set before using "tls1multi_maxbufsz".
  291. The length of the "tls1multi_maxsndfrag" parameter should not exceed that of a B<size_t>.
  292. =item "tls1multi_maxbufsz" (B<OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE>) <unsigned integer>
  293. Gets the maximum record length for a tls1 multiblock cipher operation.
  294. The length of the "tls1multi_maxbufsz" parameter should not exceed that of a B<size_t>.
  295. =item "tls1multi_aad" (B<OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD>) <octet string>
  296. Sets the authenticated additional data used by a tls1 multiblock cipher operation.
  297. The supplied data consists of 13 bytes of record data containing:
  298. Bytes 0-7: The sequence number of the first record
  299. Byte 8: The record type
  300. Byte 9-10: The protocol version
  301. Byte 11-12: Input length (Always 0)
  302. "tls1multi_interleave" must also be set for this operation.
  303. =item "tls1multi_aadpacklen" (B<OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN>) <unsigned integer>
  304. Gets the result of running the "tls1multi_aad" operation.
  305. =back
  306. =head1 RETURN VALUES
  307. OP_cipher_newctx() and OP_cipher_dupctx() should return the newly created
  308. provider side cipher context, or NULL on failure.
  309. OP_cipher_encrypt_init(), OP_cipher_decrypt_init(), OP_cipher_update(),
  310. OP_cipher_final(), OP_cipher_cipher(), OP_cipher_get_params(),
  311. OP_cipher_get_ctx_params() and OP_cipher_set_ctx_params() should return 1 for
  312. success or 0 on error.
  313. OP_cipher_gettable_params(), OP_cipher_gettable_ctx_params() and
  314. OP_cipher_settable_ctx_params() should return a constant B<OSSL_PARAM>
  315. array, or NULL if none is offered.
  316. =head1 SEE ALSO
  317. L<provider(7)>
  318. =head1 HISTORY
  319. The provider CIPHER interface was introduced in OpenSSL 3.0.
  320. =head1 COPYRIGHT
  321. Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  322. Licensed under the Apache License 2.0 (the "License"). You may not use
  323. this file except in compliance with the License. You can obtain a copy
  324. in the file LICENSE in the source distribution or at
  325. L<https://www.openssl.org/source/license.html>.
  326. =cut