psa_crypto_aead.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * PSA AEAD entry points
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "common.h"
  21. #if defined(MBEDTLS_PSA_CRYPTO_C)
  22. #include "psa_crypto_aead.h"
  23. #include "psa_crypto_core.h"
  24. #include "psa_crypto_cipher.h"
  25. #include <string.h>
  26. #include "mbedtls/platform.h"
  27. #include "mbedtls/ccm.h"
  28. #include "mbedtls/chachapoly.h"
  29. #include "mbedtls/cipher.h"
  30. #include "mbedtls/gcm.h"
  31. #include "mbedtls/error.h"
  32. static psa_status_t psa_aead_setup(
  33. mbedtls_psa_aead_operation_t *operation,
  34. const psa_key_attributes_t *attributes,
  35. const uint8_t *key_buffer,
  36. size_t key_buffer_size,
  37. psa_algorithm_t alg)
  38. {
  39. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  40. size_t key_bits;
  41. const mbedtls_cipher_info_t *cipher_info;
  42. mbedtls_cipher_id_t cipher_id;
  43. (void) key_buffer_size;
  44. key_bits = attributes->core.bits;
  45. cipher_info = mbedtls_cipher_info_from_psa(alg,
  46. attributes->core.type, key_bits,
  47. &cipher_id);
  48. if (cipher_info == NULL) {
  49. return PSA_ERROR_NOT_SUPPORTED;
  50. }
  51. switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
  52. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  53. case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
  54. operation->alg = PSA_ALG_CCM;
  55. /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
  56. * The call to mbedtls_ccm_encrypt_and_tag or
  57. * mbedtls_ccm_auth_decrypt will validate the tag length. */
  58. if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) {
  59. return PSA_ERROR_INVALID_ARGUMENT;
  60. }
  61. mbedtls_ccm_init(&operation->ctx.ccm);
  62. status = mbedtls_to_psa_error(
  63. mbedtls_ccm_setkey(&operation->ctx.ccm, cipher_id,
  64. key_buffer, (unsigned int) key_bits));
  65. if (status != PSA_SUCCESS) {
  66. return status;
  67. }
  68. break;
  69. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  70. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  71. case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
  72. operation->alg = PSA_ALG_GCM;
  73. /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
  74. * The call to mbedtls_gcm_crypt_and_tag or
  75. * mbedtls_gcm_auth_decrypt will validate the tag length. */
  76. if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) {
  77. return PSA_ERROR_INVALID_ARGUMENT;
  78. }
  79. mbedtls_gcm_init(&operation->ctx.gcm);
  80. status = mbedtls_to_psa_error(
  81. mbedtls_gcm_setkey(&operation->ctx.gcm, cipher_id,
  82. key_buffer, (unsigned int) key_bits));
  83. if (status != PSA_SUCCESS) {
  84. return status;
  85. }
  86. break;
  87. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  88. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  89. case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
  90. operation->alg = PSA_ALG_CHACHA20_POLY1305;
  91. /* We only support the default tag length. */
  92. if (alg != PSA_ALG_CHACHA20_POLY1305) {
  93. return PSA_ERROR_NOT_SUPPORTED;
  94. }
  95. mbedtls_chachapoly_init(&operation->ctx.chachapoly);
  96. status = mbedtls_to_psa_error(
  97. mbedtls_chachapoly_setkey(&operation->ctx.chachapoly,
  98. key_buffer));
  99. if (status != PSA_SUCCESS) {
  100. return status;
  101. }
  102. break;
  103. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  104. default:
  105. (void) status;
  106. (void) key_buffer;
  107. return PSA_ERROR_NOT_SUPPORTED;
  108. }
  109. operation->key_type = psa_get_key_type(attributes);
  110. operation->tag_length = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
  111. return PSA_SUCCESS;
  112. }
  113. psa_status_t mbedtls_psa_aead_encrypt(
  114. const psa_key_attributes_t *attributes,
  115. const uint8_t *key_buffer, size_t key_buffer_size,
  116. psa_algorithm_t alg,
  117. const uint8_t *nonce, size_t nonce_length,
  118. const uint8_t *additional_data, size_t additional_data_length,
  119. const uint8_t *plaintext, size_t plaintext_length,
  120. uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length)
  121. {
  122. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  123. mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
  124. uint8_t *tag;
  125. status = psa_aead_setup(&operation, attributes, key_buffer,
  126. key_buffer_size, alg);
  127. if (status != PSA_SUCCESS) {
  128. goto exit;
  129. }
  130. /* For all currently supported modes, the tag is at the end of the
  131. * ciphertext. */
  132. if (ciphertext_size < (plaintext_length + operation.tag_length)) {
  133. status = PSA_ERROR_BUFFER_TOO_SMALL;
  134. goto exit;
  135. }
  136. tag = ciphertext + plaintext_length;
  137. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  138. if (operation.alg == PSA_ALG_CCM) {
  139. status = mbedtls_to_psa_error(
  140. mbedtls_ccm_encrypt_and_tag(&operation.ctx.ccm,
  141. plaintext_length,
  142. nonce, nonce_length,
  143. additional_data,
  144. additional_data_length,
  145. plaintext, ciphertext,
  146. tag, operation.tag_length));
  147. } else
  148. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  149. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  150. if (operation.alg == PSA_ALG_GCM) {
  151. status = mbedtls_to_psa_error(
  152. mbedtls_gcm_crypt_and_tag(&operation.ctx.gcm,
  153. MBEDTLS_GCM_ENCRYPT,
  154. plaintext_length,
  155. nonce, nonce_length,
  156. additional_data, additional_data_length,
  157. plaintext, ciphertext,
  158. operation.tag_length, tag));
  159. } else
  160. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  161. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  162. if (operation.alg == PSA_ALG_CHACHA20_POLY1305) {
  163. if (operation.tag_length != 16) {
  164. status = PSA_ERROR_NOT_SUPPORTED;
  165. goto exit;
  166. }
  167. status = mbedtls_to_psa_error(
  168. mbedtls_chachapoly_encrypt_and_tag(&operation.ctx.chachapoly,
  169. plaintext_length,
  170. nonce,
  171. additional_data,
  172. additional_data_length,
  173. plaintext,
  174. ciphertext,
  175. tag));
  176. } else
  177. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  178. {
  179. (void) tag;
  180. (void) nonce;
  181. (void) nonce_length;
  182. (void) additional_data;
  183. (void) additional_data_length;
  184. (void) plaintext;
  185. return PSA_ERROR_NOT_SUPPORTED;
  186. }
  187. if (status == PSA_SUCCESS) {
  188. *ciphertext_length = plaintext_length + operation.tag_length;
  189. }
  190. exit:
  191. mbedtls_psa_aead_abort(&operation);
  192. return status;
  193. }
  194. /* Locate the tag in a ciphertext buffer containing the encrypted data
  195. * followed by the tag. Return the length of the part preceding the tag in
  196. * *plaintext_length. This is the size of the plaintext in modes where
  197. * the encrypted data has the same size as the plaintext, such as
  198. * CCM and GCM. */
  199. static psa_status_t psa_aead_unpadded_locate_tag(size_t tag_length,
  200. const uint8_t *ciphertext,
  201. size_t ciphertext_length,
  202. size_t plaintext_size,
  203. const uint8_t **p_tag)
  204. {
  205. size_t payload_length;
  206. if (tag_length > ciphertext_length) {
  207. return PSA_ERROR_INVALID_ARGUMENT;
  208. }
  209. payload_length = ciphertext_length - tag_length;
  210. if (payload_length > plaintext_size) {
  211. return PSA_ERROR_BUFFER_TOO_SMALL;
  212. }
  213. *p_tag = ciphertext + payload_length;
  214. return PSA_SUCCESS;
  215. }
  216. psa_status_t mbedtls_psa_aead_decrypt(
  217. const psa_key_attributes_t *attributes,
  218. const uint8_t *key_buffer, size_t key_buffer_size,
  219. psa_algorithm_t alg,
  220. const uint8_t *nonce, size_t nonce_length,
  221. const uint8_t *additional_data, size_t additional_data_length,
  222. const uint8_t *ciphertext, size_t ciphertext_length,
  223. uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length)
  224. {
  225. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  226. mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
  227. const uint8_t *tag = NULL;
  228. status = psa_aead_setup(&operation, attributes, key_buffer,
  229. key_buffer_size, alg);
  230. if (status != PSA_SUCCESS) {
  231. goto exit;
  232. }
  233. status = psa_aead_unpadded_locate_tag(operation.tag_length,
  234. ciphertext, ciphertext_length,
  235. plaintext_size, &tag);
  236. if (status != PSA_SUCCESS) {
  237. goto exit;
  238. }
  239. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  240. if (operation.alg == PSA_ALG_CCM) {
  241. status = mbedtls_to_psa_error(
  242. mbedtls_ccm_auth_decrypt(&operation.ctx.ccm,
  243. ciphertext_length - operation.tag_length,
  244. nonce, nonce_length,
  245. additional_data,
  246. additional_data_length,
  247. ciphertext, plaintext,
  248. tag, operation.tag_length));
  249. } else
  250. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  251. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  252. if (operation.alg == PSA_ALG_GCM) {
  253. status = mbedtls_to_psa_error(
  254. mbedtls_gcm_auth_decrypt(&operation.ctx.gcm,
  255. ciphertext_length - operation.tag_length,
  256. nonce, nonce_length,
  257. additional_data,
  258. additional_data_length,
  259. tag, operation.tag_length,
  260. ciphertext, plaintext));
  261. } else
  262. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  263. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  264. if (operation.alg == PSA_ALG_CHACHA20_POLY1305) {
  265. if (operation.tag_length != 16) {
  266. status = PSA_ERROR_NOT_SUPPORTED;
  267. goto exit;
  268. }
  269. status = mbedtls_to_psa_error(
  270. mbedtls_chachapoly_auth_decrypt(&operation.ctx.chachapoly,
  271. ciphertext_length - operation.tag_length,
  272. nonce,
  273. additional_data,
  274. additional_data_length,
  275. tag,
  276. ciphertext,
  277. plaintext));
  278. } else
  279. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  280. {
  281. (void) nonce;
  282. (void) nonce_length;
  283. (void) additional_data;
  284. (void) additional_data_length;
  285. (void) plaintext;
  286. return PSA_ERROR_NOT_SUPPORTED;
  287. }
  288. if (status == PSA_SUCCESS) {
  289. *plaintext_length = ciphertext_length - operation.tag_length;
  290. }
  291. exit:
  292. mbedtls_psa_aead_abort(&operation);
  293. if (status == PSA_SUCCESS) {
  294. *plaintext_length = ciphertext_length - operation.tag_length;
  295. }
  296. return status;
  297. }
  298. /* Set the key and algorithm for a multipart authenticated encryption
  299. * operation. */
  300. psa_status_t mbedtls_psa_aead_encrypt_setup(
  301. mbedtls_psa_aead_operation_t *operation,
  302. const psa_key_attributes_t *attributes,
  303. const uint8_t *key_buffer,
  304. size_t key_buffer_size,
  305. psa_algorithm_t alg)
  306. {
  307. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  308. status = psa_aead_setup(operation, attributes, key_buffer,
  309. key_buffer_size, alg);
  310. if (status == PSA_SUCCESS) {
  311. operation->is_encrypt = 1;
  312. }
  313. return status;
  314. }
  315. /* Set the key and algorithm for a multipart authenticated decryption
  316. * operation. */
  317. psa_status_t mbedtls_psa_aead_decrypt_setup(
  318. mbedtls_psa_aead_operation_t *operation,
  319. const psa_key_attributes_t *attributes,
  320. const uint8_t *key_buffer,
  321. size_t key_buffer_size,
  322. psa_algorithm_t alg)
  323. {
  324. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  325. status = psa_aead_setup(operation, attributes, key_buffer,
  326. key_buffer_size, alg);
  327. if (status == PSA_SUCCESS) {
  328. operation->is_encrypt = 0;
  329. }
  330. return status;
  331. }
  332. /* Set a nonce for the multipart AEAD operation*/
  333. psa_status_t mbedtls_psa_aead_set_nonce(
  334. mbedtls_psa_aead_operation_t *operation,
  335. const uint8_t *nonce,
  336. size_t nonce_length)
  337. {
  338. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  339. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  340. if (operation->alg == PSA_ALG_GCM) {
  341. status = mbedtls_to_psa_error(
  342. mbedtls_gcm_starts(&operation->ctx.gcm,
  343. operation->is_encrypt ?
  344. MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
  345. nonce,
  346. nonce_length));
  347. } else
  348. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  349. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  350. if (operation->alg == PSA_ALG_CCM) {
  351. status = mbedtls_to_psa_error(
  352. mbedtls_ccm_starts(&operation->ctx.ccm,
  353. operation->is_encrypt ?
  354. MBEDTLS_CCM_ENCRYPT : MBEDTLS_CCM_DECRYPT,
  355. nonce,
  356. nonce_length));
  357. } else
  358. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  359. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  360. if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
  361. /* Note - ChaChaPoly allows an 8 byte nonce, but we would have to
  362. * allocate a buffer in the operation, copy the nonce to it and pad
  363. * it, so for now check the nonce is 12 bytes, as
  364. * mbedtls_chachapoly_starts() assumes it can read 12 bytes from the
  365. * passed in buffer. */
  366. if (nonce_length != 12) {
  367. return PSA_ERROR_INVALID_ARGUMENT;
  368. }
  369. status = mbedtls_to_psa_error(
  370. mbedtls_chachapoly_starts(&operation->ctx.chachapoly,
  371. nonce,
  372. operation->is_encrypt ?
  373. MBEDTLS_CHACHAPOLY_ENCRYPT :
  374. MBEDTLS_CHACHAPOLY_DECRYPT));
  375. } else
  376. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  377. {
  378. (void) operation;
  379. (void) nonce;
  380. (void) nonce_length;
  381. return PSA_ERROR_NOT_SUPPORTED;
  382. }
  383. return status;
  384. }
  385. /* Declare the lengths of the message and additional data for AEAD. */
  386. psa_status_t mbedtls_psa_aead_set_lengths(
  387. mbedtls_psa_aead_operation_t *operation,
  388. size_t ad_length,
  389. size_t plaintext_length)
  390. {
  391. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  392. if (operation->alg == PSA_ALG_CCM) {
  393. return mbedtls_to_psa_error(
  394. mbedtls_ccm_set_lengths(&operation->ctx.ccm,
  395. ad_length,
  396. plaintext_length,
  397. operation->tag_length));
  398. }
  399. #else /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  400. (void) operation;
  401. (void) ad_length;
  402. (void) plaintext_length;
  403. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  404. return PSA_SUCCESS;
  405. }
  406. /* Pass additional data to an active multipart AEAD operation. */
  407. psa_status_t mbedtls_psa_aead_update_ad(
  408. mbedtls_psa_aead_operation_t *operation,
  409. const uint8_t *input,
  410. size_t input_length)
  411. {
  412. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  413. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  414. if (operation->alg == PSA_ALG_GCM) {
  415. status = mbedtls_to_psa_error(
  416. mbedtls_gcm_update_ad(&operation->ctx.gcm, input, input_length));
  417. } else
  418. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  419. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  420. if (operation->alg == PSA_ALG_CCM) {
  421. status = mbedtls_to_psa_error(
  422. mbedtls_ccm_update_ad(&operation->ctx.ccm, input, input_length));
  423. } else
  424. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  425. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  426. if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
  427. status = mbedtls_to_psa_error(
  428. mbedtls_chachapoly_update_aad(&operation->ctx.chachapoly,
  429. input,
  430. input_length));
  431. } else
  432. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  433. {
  434. (void) operation;
  435. (void) input;
  436. (void) input_length;
  437. return PSA_ERROR_NOT_SUPPORTED;
  438. }
  439. return status;
  440. }
  441. /* Encrypt or decrypt a message fragment in an active multipart AEAD
  442. * operation.*/
  443. psa_status_t mbedtls_psa_aead_update(
  444. mbedtls_psa_aead_operation_t *operation,
  445. const uint8_t *input,
  446. size_t input_length,
  447. uint8_t *output,
  448. size_t output_size,
  449. size_t *output_length)
  450. {
  451. size_t update_output_length;
  452. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  453. update_output_length = input_length;
  454. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  455. if (operation->alg == PSA_ALG_GCM) {
  456. status = mbedtls_to_psa_error(
  457. mbedtls_gcm_update(&operation->ctx.gcm,
  458. input, input_length,
  459. output, output_size,
  460. &update_output_length));
  461. } else
  462. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  463. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  464. if (operation->alg == PSA_ALG_CCM) {
  465. if (output_size < input_length) {
  466. return PSA_ERROR_BUFFER_TOO_SMALL;
  467. }
  468. status = mbedtls_to_psa_error(
  469. mbedtls_ccm_update(&operation->ctx.ccm,
  470. input, input_length,
  471. output, output_size,
  472. &update_output_length));
  473. } else
  474. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  475. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  476. if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
  477. if (output_size < input_length) {
  478. return PSA_ERROR_BUFFER_TOO_SMALL;
  479. }
  480. status = mbedtls_to_psa_error(
  481. mbedtls_chachapoly_update(&operation->ctx.chachapoly,
  482. input_length,
  483. input,
  484. output));
  485. } else
  486. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  487. {
  488. (void) operation;
  489. (void) input;
  490. (void) output;
  491. (void) output_size;
  492. return PSA_ERROR_NOT_SUPPORTED;
  493. }
  494. if (status == PSA_SUCCESS) {
  495. *output_length = update_output_length;
  496. }
  497. return status;
  498. }
  499. /* Finish encrypting a message in a multipart AEAD operation. */
  500. psa_status_t mbedtls_psa_aead_finish(
  501. mbedtls_psa_aead_operation_t *operation,
  502. uint8_t *ciphertext,
  503. size_t ciphertext_size,
  504. size_t *ciphertext_length,
  505. uint8_t *tag,
  506. size_t tag_size,
  507. size_t *tag_length)
  508. {
  509. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  510. size_t finish_output_size = 0;
  511. if (tag_size < operation->tag_length) {
  512. return PSA_ERROR_BUFFER_TOO_SMALL;
  513. }
  514. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  515. if (operation->alg == PSA_ALG_GCM) {
  516. status = mbedtls_to_psa_error(
  517. mbedtls_gcm_finish(&operation->ctx.gcm,
  518. ciphertext, ciphertext_size, ciphertext_length,
  519. tag, operation->tag_length));
  520. } else
  521. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  522. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  523. if (operation->alg == PSA_ALG_CCM) {
  524. /* tag must be big enough to store a tag of size passed into set
  525. * lengths. */
  526. if (tag_size < operation->tag_length) {
  527. return PSA_ERROR_BUFFER_TOO_SMALL;
  528. }
  529. status = mbedtls_to_psa_error(
  530. mbedtls_ccm_finish(&operation->ctx.ccm,
  531. tag, operation->tag_length));
  532. } else
  533. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  534. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  535. if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
  536. /* Belt and braces. Although the above tag_size check should have
  537. * already done this, if we later start supporting smaller tag sizes
  538. * for chachapoly, then passing a tag buffer smaller than 16 into here
  539. * could cause a buffer overflow, so better safe than sorry. */
  540. if (tag_size < 16) {
  541. return PSA_ERROR_BUFFER_TOO_SMALL;
  542. }
  543. status = mbedtls_to_psa_error(
  544. mbedtls_chachapoly_finish(&operation->ctx.chachapoly,
  545. tag));
  546. } else
  547. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  548. {
  549. (void) ciphertext;
  550. (void) ciphertext_size;
  551. (void) ciphertext_length;
  552. (void) tag;
  553. (void) tag_size;
  554. (void) tag_length;
  555. return PSA_ERROR_NOT_SUPPORTED;
  556. }
  557. if (status == PSA_SUCCESS) {
  558. /* This will be zero for all supported algorithms currently, but left
  559. * here for future support. */
  560. *ciphertext_length = finish_output_size;
  561. *tag_length = operation->tag_length;
  562. }
  563. return status;
  564. }
  565. /* Abort an AEAD operation */
  566. psa_status_t mbedtls_psa_aead_abort(
  567. mbedtls_psa_aead_operation_t *operation)
  568. {
  569. switch (operation->alg) {
  570. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  571. case PSA_ALG_CCM:
  572. mbedtls_ccm_free(&operation->ctx.ccm);
  573. break;
  574. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  575. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  576. case PSA_ALG_GCM:
  577. mbedtls_gcm_free(&operation->ctx.gcm);
  578. break;
  579. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  580. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  581. case PSA_ALG_CHACHA20_POLY1305:
  582. mbedtls_chachapoly_free(&operation->ctx.chachapoly);
  583. break;
  584. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  585. }
  586. operation->is_encrypt = 0;
  587. return PSA_SUCCESS;
  588. }
  589. #endif /* MBEDTLS_PSA_CRYPTO_C */