psa_crypto_cipher.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * PSA cipher driver entry points and associated auxiliary functions
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  7. */
  8. #ifndef PSA_CRYPTO_CIPHER_H
  9. #define PSA_CRYPTO_CIPHER_H
  10. #include <mbedtls/cipher.h>
  11. #include <psa/crypto.h>
  12. /** Get Mbed TLS cipher information given the cipher algorithm PSA identifier
  13. * as well as the PSA type and size of the key to be used with the cipher
  14. * algorithm.
  15. *
  16. * \param[in] alg PSA cipher algorithm identifier
  17. * \param[in] key_type PSA key type
  18. * \param[in,out] key_bits Size of the key in bits. The value provided in input
  19. * might be updated if necessary.
  20. * \param[out] mode Mbed TLS cipher mode
  21. * \param[out] cipher_id Mbed TLS cipher algorithm identifier
  22. *
  23. * \return On success \c PSA_SUCCESS is returned and key_bits, mode and cipher_id
  24. * are properly updated.
  25. * \c PSA_ERROR_NOT_SUPPORTED is returned if the cipher algorithm is not
  26. * supported.
  27. */
  28. psa_status_t mbedtls_cipher_values_from_psa(psa_algorithm_t alg, psa_key_type_t key_type,
  29. size_t *key_bits, mbedtls_cipher_mode_t *mode,
  30. mbedtls_cipher_id_t *cipher_id);
  31. #if defined(MBEDTLS_CIPHER_C)
  32. /** Get Mbed TLS cipher information given the cipher algorithm PSA identifier
  33. * as well as the PSA type and size of the key to be used with the cipher
  34. * algorithm.
  35. *
  36. * \param alg PSA cipher algorithm identifier
  37. * \param key_type PSA key type
  38. * \param key_bits Size of the key in bits
  39. * \param[out] cipher_id Mbed TLS cipher algorithm identifier
  40. *
  41. * \return The Mbed TLS cipher information of the cipher algorithm.
  42. * \c NULL if the PSA cipher algorithm is not supported.
  43. */
  44. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
  45. psa_algorithm_t alg, psa_key_type_t key_type, size_t key_bits,
  46. mbedtls_cipher_id_t *cipher_id);
  47. #endif /* MBEDTLS_CIPHER_C */
  48. /**
  49. * \brief Set the key for a multipart symmetric encryption operation.
  50. *
  51. * \note The signature of this function is that of a PSA driver
  52. * cipher_encrypt_setup entry point. This function behaves as a
  53. * cipher_encrypt_setup entry point as defined in the PSA driver
  54. * interface specification for transparent drivers.
  55. *
  56. * \param[in,out] operation The operation object to set up. It has been
  57. * initialized as per the documentation for
  58. * #psa_cipher_operation_t and not yet in use.
  59. * \param[in] attributes The attributes of the key to use for the
  60. * operation.
  61. * \param[in] key_buffer The buffer containing the key context.
  62. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  63. * \param[in] alg The cipher algorithm to compute
  64. * (\c PSA_ALG_XXX value such that
  65. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  66. *
  67. * \retval #PSA_SUCCESS \emptydescription
  68. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  69. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  70. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  71. */
  72. psa_status_t mbedtls_psa_cipher_encrypt_setup(
  73. mbedtls_psa_cipher_operation_t *operation,
  74. const psa_key_attributes_t *attributes,
  75. const uint8_t *key_buffer, size_t key_buffer_size,
  76. psa_algorithm_t alg);
  77. /**
  78. * \brief Set the key for a multipart symmetric decryption operation.
  79. *
  80. * \note The signature of this function is that of a PSA driver
  81. * cipher_decrypt_setup entry point. This function behaves as a
  82. * cipher_decrypt_setup entry point as defined in the PSA driver
  83. * interface specification for transparent drivers.
  84. *
  85. * \param[in,out] operation The operation object to set up. It has been
  86. * initialized as per the documentation for
  87. * #psa_cipher_operation_t and not yet in use.
  88. * \param[in] attributes The attributes of the key to use for the
  89. * operation.
  90. * \param[in] key_buffer The buffer containing the key context.
  91. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  92. * \param[in] alg The cipher algorithm to compute
  93. * (\c PSA_ALG_XXX value such that
  94. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  95. *
  96. * \retval #PSA_SUCCESS \emptydescription
  97. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  98. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  99. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  100. */
  101. psa_status_t mbedtls_psa_cipher_decrypt_setup(
  102. mbedtls_psa_cipher_operation_t *operation,
  103. const psa_key_attributes_t *attributes,
  104. const uint8_t *key_buffer, size_t key_buffer_size,
  105. psa_algorithm_t alg);
  106. /** Set the IV for a symmetric encryption or decryption operation.
  107. *
  108. * This function sets the IV (initialization vector), nonce
  109. * or initial counter value for the encryption or decryption operation.
  110. *
  111. * \note The signature of this function is that of a PSA driver
  112. * cipher_set_iv entry point. This function behaves as a
  113. * cipher_set_iv entry point as defined in the PSA driver
  114. * interface specification for transparent drivers.
  115. *
  116. * \param[in,out] operation Active cipher operation.
  117. * \param[in] iv Buffer containing the IV to use.
  118. * \param[in] iv_length Size of the IV in bytes. It is guaranteed by
  119. * the core to be less or equal to
  120. * PSA_CIPHER_IV_MAX_SIZE.
  121. *
  122. * \retval #PSA_SUCCESS \emptydescription
  123. * \retval #PSA_ERROR_INVALID_ARGUMENT
  124. * The size of \p iv is not acceptable for the chosen algorithm,
  125. * or the chosen algorithm does not use an IV.
  126. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  127. */
  128. psa_status_t mbedtls_psa_cipher_set_iv(
  129. mbedtls_psa_cipher_operation_t *operation,
  130. const uint8_t *iv, size_t iv_length);
  131. /** Encrypt or decrypt a message fragment in an active cipher operation.
  132. *
  133. * \note The signature of this function is that of a PSA driver
  134. * cipher_update entry point. This function behaves as a
  135. * cipher_update entry point as defined in the PSA driver
  136. * interface specification for transparent drivers.
  137. *
  138. * \param[in,out] operation Active cipher operation.
  139. * \param[in] input Buffer containing the message fragment to
  140. * encrypt or decrypt.
  141. * \param[in] input_length Size of the \p input buffer in bytes.
  142. * \param[out] output Buffer where the output is to be written.
  143. * \param[in] output_size Size of the \p output buffer in bytes.
  144. * \param[out] output_length On success, the number of bytes
  145. * that make up the returned output.
  146. *
  147. * \retval #PSA_SUCCESS \emptydescription
  148. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  149. * The size of the \p output buffer is too small.
  150. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  151. */
  152. psa_status_t mbedtls_psa_cipher_update(
  153. mbedtls_psa_cipher_operation_t *operation,
  154. const uint8_t *input, size_t input_length,
  155. uint8_t *output, size_t output_size, size_t *output_length);
  156. /** Finish encrypting or decrypting a message in a cipher operation.
  157. *
  158. * \note The signature of this function is that of a PSA driver
  159. * cipher_finish entry point. This function behaves as a
  160. * cipher_finish entry point as defined in the PSA driver
  161. * interface specification for transparent drivers.
  162. *
  163. * \param[in,out] operation Active cipher operation.
  164. * \param[out] output Buffer where the output is to be written.
  165. * \param[in] output_size Size of the \p output buffer in bytes.
  166. * \param[out] output_length On success, the number of bytes
  167. * that make up the returned output.
  168. *
  169. * \retval #PSA_SUCCESS \emptydescription
  170. * \retval #PSA_ERROR_INVALID_ARGUMENT
  171. * The total input size passed to this operation is not valid for
  172. * this particular algorithm. For example, the algorithm is a based
  173. * on block cipher and requires a whole number of blocks, but the
  174. * total input size is not a multiple of the block size.
  175. * \retval #PSA_ERROR_INVALID_PADDING
  176. * This is a decryption operation for an algorithm that includes
  177. * padding, and the ciphertext does not contain valid padding.
  178. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  179. * The size of the \p output buffer is too small.
  180. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  181. */
  182. psa_status_t mbedtls_psa_cipher_finish(
  183. mbedtls_psa_cipher_operation_t *operation,
  184. uint8_t *output, size_t output_size, size_t *output_length);
  185. /** Abort a cipher operation.
  186. *
  187. * Aborting an operation frees all associated resources except for the
  188. * \p operation structure itself. Once aborted, the operation object
  189. * can be reused for another operation.
  190. *
  191. * \note The signature of this function is that of a PSA driver
  192. * cipher_abort entry point. This function behaves as a
  193. * cipher_abort entry point as defined in the PSA driver
  194. * interface specification for transparent drivers.
  195. *
  196. * \param[in,out] operation Initialized cipher operation.
  197. *
  198. * \retval #PSA_SUCCESS \emptydescription
  199. */
  200. psa_status_t mbedtls_psa_cipher_abort(mbedtls_psa_cipher_operation_t *operation);
  201. /** Encrypt a message using a symmetric cipher.
  202. *
  203. * \note The signature of this function is that of a PSA driver
  204. * cipher_encrypt entry point. This function behaves as a
  205. * cipher_encrypt entry point as defined in the PSA driver
  206. * interface specification for transparent drivers.
  207. *
  208. * \param[in] attributes The attributes of the key to use for the
  209. * operation.
  210. * \param[in] key_buffer The buffer containing the key context.
  211. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  212. * \param[in] alg The cipher algorithm to compute
  213. * (\c PSA_ALG_XXX value such that
  214. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  215. * \param[in] iv Buffer containing the IV for encryption. The
  216. * IV has been generated by the core.
  217. * \param[in] iv_length Size of the \p iv in bytes.
  218. * \param[in] input Buffer containing the message to encrypt.
  219. * \param[in] input_length Size of the \p input buffer in bytes.
  220. * \param[in,out] output Buffer where the output is to be written.
  221. * \param[in] output_size Size of the \p output buffer in bytes.
  222. * \param[out] output_length On success, the number of bytes that make up
  223. * the returned output. Initialized to zero
  224. * by the core.
  225. *
  226. * \retval #PSA_SUCCESS \emptydescription
  227. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  228. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  229. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  230. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  231. * The size of the \p output buffer is too small.
  232. * \retval #PSA_ERROR_INVALID_ARGUMENT
  233. * The size \p iv_length is not acceptable for the chosen algorithm,
  234. * or the chosen algorithm does not use an IV.
  235. * The total input size passed to this operation is not valid for
  236. * this particular algorithm. For example, the algorithm is a based
  237. * on block cipher and requires a whole number of blocks, but the
  238. * total input size is not a multiple of the block size.
  239. * \retval #PSA_ERROR_INVALID_PADDING
  240. * This is a decryption operation for an algorithm that includes
  241. * padding, and the ciphertext does not contain valid padding.
  242. */
  243. psa_status_t mbedtls_psa_cipher_encrypt(const psa_key_attributes_t *attributes,
  244. const uint8_t *key_buffer,
  245. size_t key_buffer_size,
  246. psa_algorithm_t alg,
  247. const uint8_t *iv,
  248. size_t iv_length,
  249. const uint8_t *input,
  250. size_t input_length,
  251. uint8_t *output,
  252. size_t output_size,
  253. size_t *output_length);
  254. /** Decrypt a message using a symmetric cipher.
  255. *
  256. * \note The signature of this function is that of a PSA driver
  257. * cipher_decrypt entry point. This function behaves as a
  258. * cipher_decrypt entry point as defined in the PSA driver
  259. * interface specification for transparent drivers.
  260. *
  261. * \param[in] attributes The attributes of the key to use for the
  262. * operation.
  263. * \param[in] key_buffer The buffer containing the key context.
  264. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  265. * \param[in] alg The cipher algorithm to compute
  266. * (\c PSA_ALG_XXX value such that
  267. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  268. * \param[in] input Buffer containing the iv and the ciphertext.
  269. * \param[in] input_length Size of the \p input buffer in bytes.
  270. * \param[out] output Buffer where the output is to be written.
  271. * \param[in] output_size Size of the \p output buffer in bytes.
  272. * \param[out] output_length On success, the number of bytes that make up
  273. * the returned output. Initialized to zero
  274. * by the core.
  275. *
  276. * \retval #PSA_SUCCESS \emptydescription
  277. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  278. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  279. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  280. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  281. * The size of the \p output buffer is too small.
  282. * \retval #PSA_ERROR_INVALID_ARGUMENT
  283. * The size of \p iv is not acceptable for the chosen algorithm,
  284. * or the chosen algorithm does not use an IV.
  285. * The total input size passed to this operation is not valid for
  286. * this particular algorithm. For example, the algorithm is a based
  287. * on block cipher and requires a whole number of blocks, but the
  288. * total input size is not a multiple of the block size.
  289. * \retval #PSA_ERROR_INVALID_PADDING
  290. * This is a decryption operation for an algorithm that includes
  291. * padding, and the ciphertext does not contain valid padding.
  292. */
  293. psa_status_t mbedtls_psa_cipher_decrypt(const psa_key_attributes_t *attributes,
  294. const uint8_t *key_buffer,
  295. size_t key_buffer_size,
  296. psa_algorithm_t alg,
  297. const uint8_t *input,
  298. size_t input_length,
  299. uint8_t *output,
  300. size_t output_size,
  301. size_t *output_length);
  302. #endif /* PSA_CRYPTO_CIPHER_H */