2
0

provider-cipher.pod 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 B<OSSL_DISPATCH> arrays via
  51. B<OSSL_ALGORITHM> 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 B<OSSL_DISPATCH> 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. B<OSSL_DISPATCH> 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. I<out> may be the same location as I<in> but it should not partially overlap.
  120. The same expectations apply to I<outsize> as documented for
  121. L<EVP_EncryptUpdate(3)> and L<EVP_DecryptUpdate(3)>.
  122. OSSL_FUNC_cipher_final() completes an encryption or decryption started through previous
  123. OSSL_FUNC_cipher_encrypt_init() or OSSL_FUNC_cipher_decrypt_init(), and OSSL_FUNC_cipher_update()
  124. calls.
  125. The I<cctx> parameter contains a pointer to the provider side context.
  126. Any final encryption/decryption output should be written to I<out> and the
  127. amount of data written to I<*outl> which should not exceed I<outsize> bytes.
  128. The same expectations apply to I<outsize> as documented for
  129. L<EVP_EncryptFinal(3)> and L<EVP_DecryptFinal(3)>.
  130. OSSL_FUNC_cipher_cipher() performs encryption/decryption using the provider side cipher
  131. context in the I<cctx> parameter that should have been previously initialised via
  132. a call to OSSL_FUNC_cipher_encrypt_init() or OSSL_FUNC_cipher_decrypt_init().
  133. This should call the raw underlying cipher function without any padding.
  134. This will be invoked in the provider as a result of the application calling
  135. L<EVP_Cipher(3)>.
  136. The application is responsible for ensuring that the input is a multiple of the
  137. block length.
  138. The data to be encrypted/decrypted will be in I<in>, and it will be I<inl> bytes
  139. in length.
  140. The output from the encryption/decryption should be stored in I<out> and the
  141. amount of data stored should be put in I<*outl> which should be no more than
  142. I<outsize> bytes.
  143. =head2 Cipher Parameters
  144. See L<OSSL_PARAM(3)> for further details on the parameters structure used by
  145. these functions.
  146. OSSL_FUNC_cipher_get_params() gets details of the algorithm implementation
  147. and stores them in I<params>.
  148. OSSL_FUNC_cipher_set_ctx_params() sets cipher operation parameters for the
  149. provider side cipher context I<cctx> to I<params>.
  150. Any parameter settings are additional to any that were previously set.
  151. Passing NULL for I<params> should return true.
  152. OSSL_FUNC_cipher_get_ctx_params() gets cipher operation details details from
  153. the given provider side cipher context I<cctx> and stores them in I<params>.
  154. Passing NULL for I<params> should return true.
  155. OSSL_FUNC_cipher_gettable_params(), OSSL_FUNC_cipher_gettable_ctx_params(),
  156. and OSSL_FUNC_cipher_settable_ctx_params() all return constant B<OSSL_PARAM>
  157. arrays as descriptors of the parameters that OSSL_FUNC_cipher_get_params(),
  158. OSSL_FUNC_cipher_get_ctx_params(), and OSSL_FUNC_cipher_set_ctx_params()
  159. can handle, respectively. OSSL_FUNC_cipher_gettable_ctx_params() and
  160. OSSL_FUNC_cipher_settable_ctx_params() will return the parameters associated
  161. with the provider side context I<cctx> in its current state if it is
  162. not NULL. Otherwise, they return the parameters associated with the
  163. provider side algorithm I<provctx>.
  164. Parameters currently recognised by built-in ciphers are listed in
  165. L<EVP_EncryptInit(3)/PARAMETERS>.
  166. Not all parameters are relevant to, or are understood by all ciphers.
  167. =head1 RETURN VALUES
  168. OSSL_FUNC_cipher_newctx() and OSSL_FUNC_cipher_dupctx() should return the newly created
  169. provider side cipher context, or NULL on failure.
  170. OSSL_FUNC_cipher_encrypt_init(), OSSL_FUNC_cipher_decrypt_init(), OSSL_FUNC_cipher_update(),
  171. OSSL_FUNC_cipher_final(), OSSL_FUNC_cipher_cipher(), OSSL_FUNC_cipher_get_params(),
  172. OSSL_FUNC_cipher_get_ctx_params() and OSSL_FUNC_cipher_set_ctx_params() should return 1 for
  173. success or 0 on error.
  174. OSSL_FUNC_cipher_gettable_params(), OSSL_FUNC_cipher_gettable_ctx_params() and
  175. OSSL_FUNC_cipher_settable_ctx_params() should return a constant B<OSSL_PARAM>
  176. array, or NULL if none is offered.
  177. =head1 SEE ALSO
  178. L<provider(7)>, L<OSSL_PROVIDER-FIPS(7)>, L<OSSL_PROVIDER-default(7)>,
  179. L<OSSL_PROVIDER-legacy(7)>,
  180. L<EVP_CIPHER-AES(7)>, L<EVP_CIPHER-ARIA(7)>, L<EVP_CIPHER-BLOWFISH(7)>,
  181. L<EVP_CIPHER-CAMELLIA(7)>, L<EVP_CIPHER-CAST(7)>, L<EVP_CIPHER-CHACHA(7)>,
  182. L<EVP_CIPHER-DES(7)>, L<EVP_CIPHER-IDEA(7)>, L<EVP_CIPHER-RC2(7)>,
  183. L<EVP_CIPHER-RC4(7)>, L<EVP_CIPHER-RC5(7)>, L<EVP_CIPHER-SEED(7)>,
  184. L<EVP_CIPHER-SM4(7)>,
  185. L<life_cycle-cipher(7)>, L<EVP_EncryptInit(3)>
  186. =head1 HISTORY
  187. The provider CIPHER interface was introduced in OpenSSL 3.0.
  188. =head1 COPYRIGHT
  189. Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  190. Licensed under the Apache License 2.0 (the "License"). You may not use
  191. this file except in compliance with the License. You can obtain a copy
  192. in the file LICENSE in the source distribution or at
  193. L<https://www.openssl.org/source/license.html>.
  194. =cut