provider-cipher.pod 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_dispatch.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 *OSSL_FUNC_cipher_newctx(void *provctx);
  15. void OSSL_FUNC_cipher_freectx(void *cctx);
  16. void *OSSL_FUNC_cipher_dupctx(void *cctx);
  17. /* Encryption/decryption */
  18. int OSSL_FUNC_cipher_encrypt_init(void *cctx, const unsigned char *key,
  19. size_t keylen, const unsigned char *iv,
  20. size_t ivlen, const OSSL_PARAM params[]);
  21. int OSSL_FUNC_cipher_decrypt_init(void *cctx, const unsigned char *key,
  22. size_t keylen, const unsigned char *iv,
  23. size_t ivlen, const OSSL_PARAM params[]);
  24. int OSSL_FUNC_cipher_update(void *cctx, unsigned char *out, size_t *outl,
  25. size_t outsize, const unsigned char *in, size_t inl);
  26. int OSSL_FUNC_cipher_final(void *cctx, unsigned char *out, size_t *outl,
  27. size_t outsize);
  28. int OSSL_FUNC_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 *OSSL_FUNC_cipher_gettable_params(void *provctx);
  32. /* Cipher operation parameter descriptors */
  33. const OSSL_PARAM *OSSL_FUNC_cipher_gettable_ctx_params(void *cctx,
  34. void *provctx);
  35. const OSSL_PARAM *OSSL_FUNC_cipher_settable_ctx_params(void *cctx,
  36. void *provctx);
  37. /* Cipher parameters */
  38. int OSSL_FUNC_cipher_get_params(OSSL_PARAM params[]);
  39. /* Cipher operation parameters */
  40. int OSSL_FUNC_cipher_get_ctx_params(void *cctx, OSSL_PARAM params[]);
  41. int OSSL_FUNC_cipher_set_ctx_params(void *cctx, const OSSL_PARAM params[]);
  42. =head1 DESCRIPTION
  43. This documentation is primarily aimed at provider authors. See L<provider(7)>
  44. for further information.
  45. The CIPHER operation enables providers to implement cipher algorithms and make
  46. them available to applications via the API functions L<EVP_EncryptInit_ex(3)>,
  47. L<EVP_EncryptUpdate(3)> and L<EVP_EncryptFinal(3)> (as well as the decrypt
  48. equivalents and other related functions).
  49. All "functions" mentioned here are passed as function pointers between
  50. F<libcrypto> and the provider in L<OSSL_DISPATCH(3)> arrays via
  51. L<OSSL_ALGORITHM(3)> arrays that are returned by the provider's
  52. provider_query_operation() function
  53. (see L<provider-base(7)/Provider Functions>).
  54. All these "functions" have a corresponding function type definition
  55. named B<OSSL_FUNC_{name}_fn>, and a helper function to retrieve the
  56. function pointer from an L<OSSL_DISPATCH(3)> element named
  57. B<OSSL_FUNC_{name}>.
  58. For example, the "function" OSSL_FUNC_cipher_newctx() has these:
  59. typedef void *(OSSL_FUNC_cipher_newctx_fn)(void *provctx);
  60. static ossl_inline OSSL_FUNC_cipher_newctx_fn
  61. OSSL_FUNC_cipher_newctx(const OSSL_DISPATCH *opf);
  62. L<OSSL_DISPATCH(3)> arrays are indexed by numbers that are provided as
  63. macros in L<openssl-core_dispatch.h(7)>, as follows:
  64. OSSL_FUNC_cipher_newctx OSSL_FUNC_CIPHER_NEWCTX
  65. OSSL_FUNC_cipher_freectx OSSL_FUNC_CIPHER_FREECTX
  66. OSSL_FUNC_cipher_dupctx OSSL_FUNC_CIPHER_DUPCTX
  67. OSSL_FUNC_cipher_encrypt_init OSSL_FUNC_CIPHER_ENCRYPT_INIT
  68. OSSL_FUNC_cipher_decrypt_init OSSL_FUNC_CIPHER_DECRYPT_INIT
  69. OSSL_FUNC_cipher_update OSSL_FUNC_CIPHER_UPDATE
  70. OSSL_FUNC_cipher_final OSSL_FUNC_CIPHER_FINAL
  71. OSSL_FUNC_cipher_cipher OSSL_FUNC_CIPHER_CIPHER
  72. OSSL_FUNC_cipher_get_params OSSL_FUNC_CIPHER_GET_PARAMS
  73. OSSL_FUNC_cipher_get_ctx_params OSSL_FUNC_CIPHER_GET_CTX_PARAMS
  74. OSSL_FUNC_cipher_set_ctx_params OSSL_FUNC_CIPHER_SET_CTX_PARAMS
  75. OSSL_FUNC_cipher_gettable_params OSSL_FUNC_CIPHER_GETTABLE_PARAMS
  76. OSSL_FUNC_cipher_gettable_ctx_params OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS
  77. OSSL_FUNC_cipher_settable_ctx_params OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS
  78. A cipher algorithm implementation may not implement all of these functions.
  79. In order to be a consistent set of functions there must at least be a complete
  80. set of "encrypt" functions, or a complete set of "decrypt" functions, or a
  81. single "cipher" function.
  82. In all cases both the OSSL_FUNC_cipher_newctx and OSSL_FUNC_cipher_freectx functions must be
  83. present.
  84. All other functions are optional.
  85. =head2 Context Management Functions
  86. OSSL_FUNC_cipher_newctx() should create and return a pointer to a provider side
  87. structure for holding context information during a cipher operation.
  88. A pointer to this context will be passed back in a number of the other cipher
  89. operation function calls.
  90. The parameter I<provctx> is the provider context generated during provider
  91. initialisation (see L<provider(7)>).
  92. OSSL_FUNC_cipher_freectx() is passed a pointer to the provider side cipher context in
  93. the I<cctx> parameter.
  94. This function should free any resources associated with that context.
  95. OSSL_FUNC_cipher_dupctx() should duplicate the provider side cipher context in the
  96. I<cctx> parameter and return the duplicate copy.
  97. =head2 Encryption/Decryption Functions
  98. OSSL_FUNC_cipher_encrypt_init() initialises a cipher operation for encryption given a
  99. newly created provider side cipher context in the I<cctx> parameter.
  100. The key to be used is given in I<key> which is I<keylen> bytes long.
  101. The IV to be used is given in I<iv> which is I<ivlen> bytes long.
  102. The I<params>, if not NULL, should be set on the context in a manner similar to
  103. using OSSL_FUNC_cipher_set_ctx_params().
  104. OSSL_FUNC_cipher_decrypt_init() is the same as OSSL_FUNC_cipher_encrypt_init() except that it
  105. initialises the context for a decryption operation.
  106. OSSL_FUNC_cipher_update() is called to supply data to be encrypted/decrypted as part of
  107. a previously initialised cipher operation.
  108. The I<cctx> parameter contains a pointer to a previously initialised provider
  109. side context.
  110. OSSL_FUNC_cipher_update() should encrypt/decrypt I<inl> bytes of data at the location
  111. pointed to by I<in>.
  112. The encrypted data should be stored in I<out> and the amount of data written to
  113. I<*outl> which should not exceed I<outsize> bytes.
  114. OSSL_FUNC_cipher_update() may be called multiple times for a single cipher operation.
  115. It is the responsibility of the cipher implementation to handle input lengths
  116. that are not multiples of the block length.
  117. In such cases a cipher implementation will typically cache partial blocks of
  118. input data until a complete block is obtained.
  119. The pointers I<out> and I<in> may point to the same location, in which
  120. case the encryption must be done in-place. If I<out> and I<in> point to different
  121. locations, the requirements of L<EVP_EncryptUpdate(3)> and L<EVP_DecryptUpdate(3)>
  122. guarantee that the two buffers are disjoint.
  123. Similarly, the requirements of L<EVP_EncryptUpdate(3)> and L<EVP_DecryptUpdate(3)>
  124. ensure that the buffer pointed to by I<out> contains sufficient room for the
  125. operation being performed.
  126. OSSL_FUNC_cipher_final() completes an encryption or decryption started through previous
  127. OSSL_FUNC_cipher_encrypt_init() or OSSL_FUNC_cipher_decrypt_init(), and OSSL_FUNC_cipher_update()
  128. calls.
  129. The I<cctx> parameter contains a pointer to the provider side context.
  130. Any final encryption/decryption output should be written to I<out> and the
  131. amount of data written to I<*outl> which should not exceed I<outsize> bytes.
  132. The same expectations apply to I<outsize> as documented for
  133. L<EVP_EncryptFinal(3)> and L<EVP_DecryptFinal(3)>.
  134. OSSL_FUNC_cipher_cipher() performs encryption/decryption using the provider side cipher
  135. context in the I<cctx> parameter that should have been previously initialised via
  136. a call to OSSL_FUNC_cipher_encrypt_init() or OSSL_FUNC_cipher_decrypt_init().
  137. This should call the raw underlying cipher function without any padding.
  138. This will be invoked in the provider as a result of the application calling
  139. L<EVP_Cipher(3)>.
  140. The application is responsible for ensuring that the input is a multiple of the
  141. block length.
  142. The data to be encrypted/decrypted will be in I<in>, and it will be I<inl> bytes
  143. in length.
  144. The output from the encryption/decryption should be stored in I<out> and the
  145. amount of data stored should be put in I<*outl> which should be no more than
  146. I<outsize> bytes.
  147. =head2 Cipher Parameters
  148. See L<OSSL_PARAM(3)> for further details on the parameters structure used by
  149. these functions.
  150. OSSL_FUNC_cipher_get_params() gets details of the algorithm implementation
  151. and stores them in I<params>.
  152. OSSL_FUNC_cipher_set_ctx_params() sets cipher operation parameters for the
  153. provider side cipher context I<cctx> to I<params>.
  154. Any parameter settings are additional to any that were previously set.
  155. Passing NULL for I<params> should return true.
  156. OSSL_FUNC_cipher_get_ctx_params() gets cipher operation details details from
  157. the given provider side cipher context I<cctx> and stores them in I<params>.
  158. Passing NULL for I<params> should return true.
  159. OSSL_FUNC_cipher_gettable_params(), OSSL_FUNC_cipher_gettable_ctx_params(),
  160. and OSSL_FUNC_cipher_settable_ctx_params() all return constant L<OSSL_PARAM(3)>
  161. arrays as descriptors of the parameters that OSSL_FUNC_cipher_get_params(),
  162. OSSL_FUNC_cipher_get_ctx_params(), and OSSL_FUNC_cipher_set_ctx_params()
  163. can handle, respectively. OSSL_FUNC_cipher_gettable_ctx_params() and
  164. OSSL_FUNC_cipher_settable_ctx_params() will return the parameters associated
  165. with the provider side context I<cctx> in its current state if it is
  166. not NULL. Otherwise, they return the parameters associated with the
  167. provider side algorithm I<provctx>.
  168. Parameters currently recognised by built-in ciphers are listed in
  169. L<EVP_EncryptInit(3)/PARAMETERS>.
  170. Not all parameters are relevant to, or are understood by all ciphers.
  171. =head1 RETURN VALUES
  172. OSSL_FUNC_cipher_newctx() and OSSL_FUNC_cipher_dupctx() should return the newly created
  173. provider side cipher context, or NULL on failure.
  174. OSSL_FUNC_cipher_encrypt_init(), OSSL_FUNC_cipher_decrypt_init(), OSSL_FUNC_cipher_update(),
  175. OSSL_FUNC_cipher_final(), OSSL_FUNC_cipher_cipher(), OSSL_FUNC_cipher_get_params(),
  176. OSSL_FUNC_cipher_get_ctx_params() and OSSL_FUNC_cipher_set_ctx_params() should return 1 for
  177. success or 0 on error.
  178. OSSL_FUNC_cipher_gettable_params(), OSSL_FUNC_cipher_gettable_ctx_params() and
  179. OSSL_FUNC_cipher_settable_ctx_params() should return a constant L<OSSL_PARAM(3)>
  180. array, or NULL if none is offered.
  181. =head1 SEE ALSO
  182. L<provider(7)>, L<OSSL_PROVIDER-FIPS(7)>, L<OSSL_PROVIDER-default(7)>,
  183. L<OSSL_PROVIDER-legacy(7)>,
  184. L<EVP_CIPHER-AES(7)>, L<EVP_CIPHER-ARIA(7)>, L<EVP_CIPHER-BLOWFISH(7)>,
  185. L<EVP_CIPHER-CAMELLIA(7)>, L<EVP_CIPHER-CAST(7)>, L<EVP_CIPHER-CHACHA(7)>,
  186. L<EVP_CIPHER-DES(7)>, L<EVP_CIPHER-IDEA(7)>, L<EVP_CIPHER-RC2(7)>,
  187. L<EVP_CIPHER-RC4(7)>, L<EVP_CIPHER-RC5(7)>, L<EVP_CIPHER-SEED(7)>,
  188. L<EVP_CIPHER-SM4(7)>, L<EVP_CIPHER-NULL(7)>,
  189. L<life_cycle-cipher(7)>, L<EVP_EncryptInit(3)>
  190. =head1 HISTORY
  191. The provider CIPHER interface was introduced in OpenSSL 3.0.
  192. =head1 COPYRIGHT
  193. Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
  194. Licensed under the Apache License 2.0 (the "License"). You may not use
  195. this file except in compliance with the License. You can obtain a copy
  196. in the file LICENSE in the source distribution or at
  197. L<https://www.openssl.org/source/license.html>.
  198. =cut