psa_crypto_aead.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * PSA AEAD driver entry points
  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_AEAD_H
  9. #define PSA_CRYPTO_AEAD_H
  10. #include <psa/crypto.h>
  11. /**
  12. * \brief Process an authenticated encryption operation.
  13. *
  14. * \note The signature of this function is that of a PSA driver
  15. * aead_encrypt entry point. This function behaves as an aead_encrypt
  16. * entry point as defined in the PSA driver interface specification for
  17. * transparent drivers.
  18. *
  19. * \param[in] attributes The attributes of the key to use for the
  20. * operation.
  21. * \param[in] key_buffer The buffer containing the key context.
  22. * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
  23. * \param alg The AEAD algorithm to compute.
  24. * \param[in] nonce Nonce or IV to use.
  25. * \param nonce_length Size of the nonce buffer in bytes. This must
  26. * be appropriate for the selected algorithm.
  27. * The default nonce size is
  28. * PSA_AEAD_NONCE_LENGTH(key_type, alg) where
  29. * key_type is the type of key.
  30. * \param[in] additional_data Additional data that will be authenticated
  31. * but not encrypted.
  32. * \param additional_data_length Size of additional_data in bytes.
  33. * \param[in] plaintext Data that will be authenticated and encrypted.
  34. * \param plaintext_length Size of plaintext in bytes.
  35. * \param[out] ciphertext Output buffer for the authenticated and
  36. * encrypted data. The additional data is not
  37. * part of this output. For algorithms where the
  38. * encrypted data and the authentication tag are
  39. * defined as separate outputs, the
  40. * authentication tag is appended to the
  41. * encrypted data.
  42. * \param ciphertext_size Size of the ciphertext buffer in bytes. This
  43. * must be appropriate for the selected algorithm
  44. * and key:
  45. * - A sufficient output size is
  46. * PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
  47. * plaintext_length) where key_type is the type
  48. * of key.
  49. * - PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(
  50. * plaintext_length) evaluates to the maximum
  51. * ciphertext size of any supported AEAD
  52. * encryption.
  53. * \param[out] ciphertext_length On success, the size of the output in the
  54. * ciphertext buffer.
  55. *
  56. * \retval #PSA_SUCCESS Success.
  57. * \retval #PSA_ERROR_NOT_SUPPORTED
  58. * \p alg is not supported.
  59. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  60. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  61. * ciphertext_size is too small.
  62. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  63. */
  64. psa_status_t mbedtls_psa_aead_encrypt(
  65. const psa_key_attributes_t *attributes,
  66. const uint8_t *key_buffer, size_t key_buffer_size,
  67. psa_algorithm_t alg,
  68. const uint8_t *nonce, size_t nonce_length,
  69. const uint8_t *additional_data, size_t additional_data_length,
  70. const uint8_t *plaintext, size_t plaintext_length,
  71. uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length);
  72. /**
  73. * \brief Process an authenticated decryption operation.
  74. *
  75. * \note The signature of this function is that of a PSA driver
  76. * aead_decrypt entry point. This function behaves as an aead_decrypt
  77. * entry point as defined in the PSA driver interface specification for
  78. * transparent drivers.
  79. *
  80. * \param[in] attributes The attributes of the key to use for the
  81. * operation.
  82. * \param[in] key_buffer The buffer containing the key context.
  83. * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
  84. * \param alg The AEAD algorithm to compute.
  85. * \param[in] nonce Nonce or IV to use.
  86. * \param nonce_length Size of the nonce buffer in bytes. This must
  87. * be appropriate for the selected algorithm.
  88. * The default nonce size is
  89. * PSA_AEAD_NONCE_LENGTH(key_type, alg) where
  90. * key_type is the type of key.
  91. * \param[in] additional_data Additional data that has been authenticated
  92. * but not encrypted.
  93. * \param additional_data_length Size of additional_data in bytes.
  94. * \param[in] ciphertext Data that has been authenticated and
  95. * encrypted. For algorithms where the encrypted
  96. * data and the authentication tag are defined
  97. * as separate inputs, the buffer contains
  98. * encrypted data followed by the authentication
  99. * tag.
  100. * \param ciphertext_length Size of ciphertext in bytes.
  101. * \param[out] plaintext Output buffer for the decrypted data.
  102. * \param plaintext_size Size of the plaintext buffer in bytes. This
  103. * must be appropriate for the selected algorithm
  104. * and key:
  105. * - A sufficient output size is
  106. * PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
  107. * ciphertext_length) where key_type is the
  108. * type of key.
  109. * - PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(
  110. * ciphertext_length) evaluates to the maximum
  111. * plaintext size of any supported AEAD
  112. * decryption.
  113. * \param[out] plaintext_length On success, the size of the output in the
  114. * plaintext buffer.
  115. *
  116. * \retval #PSA_SUCCESS Success.
  117. * \retval #PSA_ERROR_INVALID_SIGNATURE
  118. * The cipher is not authentic.
  119. * \retval #PSA_ERROR_NOT_SUPPORTED
  120. * \p alg is not supported.
  121. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  122. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  123. * plaintext_size is too small.
  124. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  125. */
  126. psa_status_t mbedtls_psa_aead_decrypt(
  127. const psa_key_attributes_t *attributes,
  128. const uint8_t *key_buffer, size_t key_buffer_size,
  129. psa_algorithm_t alg,
  130. const uint8_t *nonce, size_t nonce_length,
  131. const uint8_t *additional_data, size_t additional_data_length,
  132. const uint8_t *ciphertext, size_t ciphertext_length,
  133. uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length);
  134. /** Set the key for a multipart authenticated encryption operation.
  135. *
  136. * \note The signature of this function is that of a PSA driver
  137. * aead_encrypt_setup entry point. This function behaves as an
  138. * aead_encrypt_setup entry point as defined in the PSA driver interface
  139. * specification for transparent drivers.
  140. *
  141. * If an error occurs at any step after a call to
  142. * mbedtls_psa_aead_encrypt_setup(), the operation is reset by the PSA core by a
  143. * call to mbedtls_psa_aead_abort(). The PSA core may call
  144. * mbedtls_psa_aead_abort() at any time after the operation has been
  145. * initialized, and is required to when the operation is no longer needed.
  146. *
  147. * \param[in,out] operation The operation object to set up. It must have
  148. * been initialized as per the documentation for
  149. * #mbedtls_psa_aead_operation_t and not yet in
  150. * use.
  151. * \param[in] attributes The attributes of the key to use for the
  152. * operation.
  153. * \param[in] key_buffer The buffer containing the key context.
  154. * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
  155. It must be consistent with the size in bits
  156. recorded in \p attributes.
  157. * \param alg The AEAD algorithm to compute
  158. * (\c PSA_ALG_XXX value such that
  159. * #PSA_ALG_IS_AEAD(\p alg) is true).
  160. *
  161. * \retval #PSA_SUCCESS
  162. * Success.
  163. * \retval #PSA_ERROR_INVALID_ARGUMENT
  164. * An invalid block length was supplied.
  165. * \retval #PSA_ERROR_NOT_SUPPORTED
  166. * \p alg is not supported.
  167. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  168. * Failed to allocate memory for key material
  169. */
  170. psa_status_t mbedtls_psa_aead_encrypt_setup(
  171. mbedtls_psa_aead_operation_t *operation,
  172. const psa_key_attributes_t *attributes,
  173. const uint8_t *key_buffer,
  174. size_t key_buffer_size,
  175. psa_algorithm_t alg);
  176. /** Set the key for a multipart authenticated decryption operation.
  177. *
  178. * \note The signature of this function is that of a PSA driver
  179. * aead_decrypt_setup entry point. This function behaves as an
  180. * aead_decrypt_setup entry point as defined in the PSA driver interface
  181. * specification for transparent drivers.
  182. *
  183. * If an error occurs at any step after a call to
  184. * mbedtls_psa_aead_decrypt_setup(), the PSA core resets the operation by a
  185. * call to mbedtls_psa_aead_abort(). The PSA core may call
  186. * mbedtls_psa_aead_abort() at any time after the operation has been
  187. * initialized, and is required to when the operation is no longer needed.
  188. *
  189. * \param[in,out] operation The operation object to set up. It must have
  190. * been initialized as per the documentation for
  191. * #mbedtls_psa_aead_operation_t and not yet in
  192. * use.
  193. * \param[in] attributes The attributes of the key to use for the
  194. * operation.
  195. * \param[in] key_buffer The buffer containing the key context.
  196. * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
  197. It must be consistent with the size in bits
  198. recorded in \p attributes.
  199. * \param alg The AEAD algorithm to compute
  200. * (\c PSA_ALG_XXX value such that
  201. * #PSA_ALG_IS_AEAD(\p alg) is true).
  202. *
  203. * \retval #PSA_SUCCESS
  204. * Success.
  205. * \retval #PSA_ERROR_INVALID_ARGUMENT
  206. * An invalid block length was supplied.
  207. * \retval #PSA_ERROR_NOT_SUPPORTED
  208. * \p alg is not supported.
  209. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  210. * Failed to allocate memory for key material
  211. */
  212. psa_status_t mbedtls_psa_aead_decrypt_setup(
  213. mbedtls_psa_aead_operation_t *operation,
  214. const psa_key_attributes_t *attributes,
  215. const uint8_t *key_buffer,
  216. size_t key_buffer_size,
  217. psa_algorithm_t alg);
  218. /** Set the nonce for an authenticated encryption or decryption operation.
  219. *
  220. * \note The signature of this function is that of a PSA driver aead_set_nonce
  221. * entry point. This function behaves as an aead_set_nonce entry point as
  222. * defined in the PSA driver interface specification for transparent
  223. * drivers.
  224. *
  225. * This function sets the nonce for the authenticated
  226. * encryption or decryption operation.
  227. *
  228. * The PSA core calls mbedtls_psa_aead_encrypt_setup() or
  229. * mbedtls_psa_aead_decrypt_setup() before calling this function.
  230. *
  231. * If this function returns an error status, the PSA core will call
  232. * mbedtls_psa_aead_abort().
  233. *
  234. * \param[in,out] operation Active AEAD operation.
  235. * \param[in] nonce Buffer containing the nonce to use.
  236. * \param nonce_length Size of the nonce in bytes.
  237. *
  238. * \retval #PSA_SUCCESS
  239. * Success.
  240. * \retval #PSA_ERROR_INVALID_ARGUMENT
  241. * The size of \p nonce is not acceptable for the chosen algorithm.
  242. * \retval #PSA_ERROR_NOT_SUPPORTED
  243. * Algorithm previously set is not supported in this configuration of
  244. * the library.
  245. */
  246. psa_status_t mbedtls_psa_aead_set_nonce(
  247. mbedtls_psa_aead_operation_t *operation,
  248. const uint8_t *nonce,
  249. size_t nonce_length);
  250. /** Declare the lengths of the message and additional data for AEAD.
  251. *
  252. * \note The signature of this function is that of a PSA driver aead_set_lengths
  253. * entry point. This function behaves as an aead_set_lengths entry point
  254. * as defined in the PSA driver interface specification for transparent
  255. * drivers.
  256. *
  257. * The PSA core calls this function before calling mbedtls_psa_aead_update_ad()
  258. * or mbedtls_psa_aead_update() if the algorithm for the operation requires it.
  259. * If the algorithm does not require it, calling this function is optional, but
  260. * if this function is called then the implementation must enforce the lengths.
  261. *
  262. * The PSA core may call this function before or after setting the nonce with
  263. * mbedtls_psa_aead_set_nonce().
  264. *
  265. * - For #PSA_ALG_CCM, calling this function is required.
  266. * - For the other AEAD algorithms defined in this specification, calling
  267. * this function is not required.
  268. *
  269. * If this function returns an error status, the PSA core calls
  270. * mbedtls_psa_aead_abort().
  271. *
  272. * \param[in,out] operation Active AEAD operation.
  273. * \param ad_length Size of the non-encrypted additional
  274. * authenticated data in bytes.
  275. * \param plaintext_length Size of the plaintext to encrypt in bytes.
  276. *
  277. * \retval #PSA_SUCCESS
  278. * Success.
  279. * \retval #PSA_ERROR_INVALID_ARGUMENT
  280. * At least one of the lengths is not acceptable for the chosen
  281. * algorithm.
  282. * \retval #PSA_ERROR_NOT_SUPPORTED
  283. * Algorithm previously set is not supported in this configuration of
  284. * the library.
  285. */
  286. psa_status_t mbedtls_psa_aead_set_lengths(
  287. mbedtls_psa_aead_operation_t *operation,
  288. size_t ad_length,
  289. size_t plaintext_length);
  290. /** Pass additional data to an active AEAD operation.
  291. *
  292. * \note The signature of this function is that of a PSA driver
  293. * aead_update_ad entry point. This function behaves as an aead_update_ad
  294. * entry point as defined in the PSA driver interface specification for
  295. * transparent drivers.
  296. *
  297. * Additional data is authenticated, but not encrypted.
  298. *
  299. * The PSA core can call this function multiple times to pass successive
  300. * fragments of the additional data. It will not call this function after
  301. * passing data to encrypt or decrypt with mbedtls_psa_aead_update().
  302. *
  303. * Before calling this function, the PSA core will:
  304. * 1. Call either mbedtls_psa_aead_encrypt_setup() or
  305. * mbedtls_psa_aead_decrypt_setup().
  306. * 2. Set the nonce with mbedtls_psa_aead_set_nonce().
  307. *
  308. * If this function returns an error status, the PSA core will call
  309. * mbedtls_psa_aead_abort().
  310. *
  311. * \param[in,out] operation Active AEAD operation.
  312. * \param[in] input Buffer containing the fragment of
  313. * additional data.
  314. * \param input_length Size of the \p input buffer in bytes.
  315. *
  316. * \retval #PSA_SUCCESS
  317. * Success.
  318. * \retval #PSA_ERROR_NOT_SUPPORTED
  319. * Algorithm previously set is not supported in this configuration of
  320. * the library.
  321. */
  322. psa_status_t mbedtls_psa_aead_update_ad(
  323. mbedtls_psa_aead_operation_t *operation,
  324. const uint8_t *input,
  325. size_t input_length);
  326. /** Encrypt or decrypt a message fragment in an active AEAD operation.
  327. *
  328. * \note The signature of this function is that of a PSA driver
  329. * aead_update entry point. This function behaves as an aead_update entry
  330. * point as defined in the PSA driver interface specification for
  331. * transparent drivers.
  332. *
  333. * Before calling this function, the PSA core will:
  334. * 1. Call either mbedtls_psa_aead_encrypt_setup() or
  335. * mbedtls_psa_aead_decrypt_setup(). The choice of setup function
  336. * determines whether this function encrypts or decrypts its input.
  337. * 2. Set the nonce with mbedtls_psa_aead_set_nonce().
  338. * 3. Call mbedtls_psa_aead_update_ad() to pass all the additional data.
  339. *
  340. * If this function returns an error status, the PSA core will call
  341. * mbedtls_psa_aead_abort().
  342. *
  343. * This function does not require the input to be aligned to any
  344. * particular block boundary. If the implementation can only process
  345. * a whole block at a time, it must consume all the input provided, but
  346. * it may delay the end of the corresponding output until a subsequent
  347. * call to mbedtls_psa_aead_update(), mbedtls_psa_aead_finish() provides
  348. * sufficient input. The amount of data that can be delayed in this way is
  349. * bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
  350. *
  351. * \param[in,out] operation Active AEAD operation.
  352. * \param[in] input Buffer containing the message fragment to
  353. * encrypt or decrypt.
  354. * \param input_length Size of the \p input buffer in bytes.
  355. * \param[out] output Buffer where the output is to be written.
  356. * \param output_size Size of the \p output buffer in bytes.
  357. * This must be appropriate for the selected
  358. * algorithm and key:
  359. * - A sufficient output size is
  360. * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
  361. * \c alg, \p input_length) where
  362. * \c key_type is the type of key and \c alg is
  363. * the algorithm that were used to set up the
  364. * operation.
  365. * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
  366. * input_length) evaluates to the maximum
  367. * output size of any supported AEAD
  368. * algorithm.
  369. * \param[out] output_length On success, the number of bytes
  370. * that make up the returned output.
  371. *
  372. * \retval #PSA_SUCCESS
  373. * Success.
  374. *
  375. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  376. * The size of the \p output buffer is too small.
  377. * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
  378. * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
  379. * determine the required buffer size.
  380. */
  381. psa_status_t mbedtls_psa_aead_update(
  382. mbedtls_psa_aead_operation_t *operation,
  383. const uint8_t *input,
  384. size_t input_length,
  385. uint8_t *output,
  386. size_t output_size,
  387. size_t *output_length);
  388. /** Finish encrypting a message in an AEAD operation.
  389. *
  390. * \note The signature of this function is that of a PSA driver
  391. * aead_finish entry point. This function behaves as an aead_finish entry
  392. * point as defined in the PSA driver interface specification for
  393. * transparent drivers.
  394. *
  395. * The operation must have been set up by the PSA core with
  396. * mbedtls_psa_aead_encrypt_setup().
  397. *
  398. * This function finishes the authentication of the additional data
  399. * formed by concatenating the inputs passed to preceding calls to
  400. * mbedtls_psa_aead_update_ad() with the plaintext formed by concatenating the
  401. * inputs passed to preceding calls to mbedtls_psa_aead_update().
  402. *
  403. * This function has two output buffers:
  404. * - \p ciphertext contains trailing ciphertext that was buffered from
  405. * preceding calls to mbedtls_psa_aead_update().
  406. * - \p tag contains the authentication tag.
  407. *
  408. * Whether or not this function returns successfully, the PSA core subsequently
  409. * calls mbedtls_psa_aead_abort() to deactivate the operation.
  410. *
  411. * \param[in,out] operation Active AEAD operation.
  412. * \param[out] ciphertext Buffer where the last part of the ciphertext
  413. * is to be written.
  414. * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
  415. * This must be appropriate for the selected
  416. * algorithm and key:
  417. * - A sufficient output size is
  418. * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
  419. * \c alg) where \c key_type is the type of key
  420. * and \c alg is the algorithm that were used to
  421. * set up the operation.
  422. * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
  423. * the maximum output size of any supported AEAD
  424. * algorithm.
  425. * \param[out] ciphertext_length On success, the number of bytes of
  426. * returned ciphertext.
  427. * \param[out] tag Buffer where the authentication tag is
  428. * to be written.
  429. * \param tag_size Size of the \p tag buffer in bytes.
  430. * This must be appropriate for the selected
  431. * algorithm and key:
  432. * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
  433. * key_type, \c key_bits, \c alg) where
  434. * \c key_type and \c key_bits are the type and
  435. * bit-size of the key, and \c alg are the
  436. * algorithm that were used in the call to
  437. * mbedtls_psa_aead_encrypt_setup().
  438. * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
  439. * maximum tag size of any supported AEAD
  440. * algorithm.
  441. * \param[out] tag_length On success, the number of bytes
  442. * that make up the returned tag.
  443. *
  444. * \retval #PSA_SUCCESS
  445. * Success.
  446. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  447. * The size of the \p tag buffer is too small.
  448. * #PSA_AEAD_TAG_LENGTH(\c key_type, key_bits, \c alg) or
  449. * #PSA_AEAD_TAG_MAX_SIZE can be used to determine the required \p tag
  450. * buffer size.
  451. */
  452. psa_status_t mbedtls_psa_aead_finish(
  453. mbedtls_psa_aead_operation_t *operation,
  454. uint8_t *ciphertext,
  455. size_t ciphertext_size,
  456. size_t *ciphertext_length,
  457. uint8_t *tag,
  458. size_t tag_size,
  459. size_t *tag_length);
  460. /** Abort an AEAD operation.
  461. *
  462. * \note The signature of this function is that of a PSA driver
  463. * aead_abort entry point. This function behaves as an aead_abort entry
  464. * point as defined in the PSA driver interface specification for
  465. * transparent drivers.
  466. *
  467. * Aborting an operation frees all associated resources except for the
  468. * \p operation structure itself. Once aborted, the operation object
  469. * can be reused for another operation by the PSA core by it calling
  470. * mbedtls_psa_aead_encrypt_setup() or mbedtls_psa_aead_decrypt_setup() again.
  471. *
  472. * The PSA core may call this function any time after the operation object has
  473. * been initialized as described in #mbedtls_psa_aead_operation_t.
  474. *
  475. * In particular, calling mbedtls_psa_aead_abort() after the operation has been
  476. * terminated by a call to mbedtls_psa_aead_abort() or
  477. * mbedtls_psa_aead_finish() is safe and has no effect.
  478. *
  479. * \param[in,out] operation Initialized AEAD operation.
  480. *
  481. * \retval #PSA_SUCCESS
  482. * Success.
  483. */
  484. psa_status_t mbedtls_psa_aead_abort(
  485. mbedtls_psa_aead_operation_t *operation);
  486. #endif /* PSA_CRYPTO_AEAD_H */