blob: ed9e55ad6aee4476c081bc258a2f799f80862e13 [file] [log] [blame]
Ronald Cron7ceee8d2021-03-17 16:55:43 +01001/*
2 * PSA AEAD entry points
3 */
4/*
5 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Ronald Cron7ceee8d2021-03-17 16:55:43 +01007 */
8
9#include "common.h"
10
11#if defined(MBEDTLS_PSA_CRYPTO_C)
12
13#include "psa_crypto_aead.h"
Ronald Cron46f91782021-03-17 08:16:34 +010014#include "psa_crypto_core.h"
Dave Rodgman8e322b12022-11-02 09:25:38 +000015#include "psa_crypto_cipher.h"
Ronald Cron46f91782021-03-17 08:16:34 +010016
17#include "mbedtls/ccm.h"
18#include "mbedtls/chachapoly.h"
19#include "mbedtls/cipher.h"
20#include "mbedtls/gcm.h"
21
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010022typedef struct {
Gilles Peskinef5d7eef2021-11-08 22:12:47 +010023 psa_algorithm_t core_alg;
24 uint8_t tag_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010025 union {
Ronald Cron46f91782021-03-17 08:16:34 +010026 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
27#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
28 mbedtls_ccm_context ccm;
29#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
30#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
31 mbedtls_gcm_context gcm;
32#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
33#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
34 mbedtls_chachapoly_context chachapoly;
35#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
36 } ctx;
Ronald Cron46f91782021-03-17 08:16:34 +010037} aead_operation_t;
38
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010039#define AEAD_OPERATION_INIT { 0, 0, { 0 } }
Ronald Cron46f91782021-03-17 08:16:34 +010040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041static void psa_aead_abort_internal(aead_operation_t *operation)
Ronald Cron46f91782021-03-17 08:16:34 +010042{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010043 switch (operation->core_alg) {
Ronald Cron46f91782021-03-17 08:16:34 +010044#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
45 case PSA_ALG_CCM:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010046 mbedtls_ccm_free(&operation->ctx.ccm);
Ronald Cron46f91782021-03-17 08:16:34 +010047 break;
48#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
49#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
50 case PSA_ALG_GCM:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051 mbedtls_gcm_free(&operation->ctx.gcm);
Ronald Cron46f91782021-03-17 08:16:34 +010052 break;
53#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cronb9349a62021-03-26 13:32:29 +010054#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
55 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 mbedtls_chachapoly_free(&operation->ctx.chachapoly);
Ronald Cronb9349a62021-03-26 13:32:29 +010057 break;
58#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
Ronald Cron46f91782021-03-17 08:16:34 +010059 }
60}
61
62static psa_status_t psa_aead_setup(
63 aead_operation_t *operation,
64 const psa_key_attributes_t *attributes,
65 const uint8_t *key_buffer,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 psa_algorithm_t alg)
Ronald Cron46f91782021-03-17 08:16:34 +010067{
68 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
69 size_t key_bits;
Ronald Cronecbc0682021-03-26 13:25:17 +010070 const mbedtls_cipher_info_t *cipher_info;
Ronald Cron46f91782021-03-17 08:16:34 +010071 mbedtls_cipher_id_t cipher_id;
Ronald Cronecbc0682021-03-26 13:25:17 +010072 size_t full_tag_length = 0;
Ronald Cron46f91782021-03-17 08:16:34 +010073
74 key_bits = attributes->core.bits;
75
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 cipher_info = mbedtls_cipher_info_from_psa(alg,
77 attributes->core.type, key_bits,
78 &cipher_id);
79 if (cipher_info == NULL) {
80 return PSA_ERROR_NOT_SUPPORTED;
81 }
Ronald Cron46f91782021-03-17 08:16:34 +010082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Ronald Cron46f91782021-03-17 08:16:34 +010084#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Ronald Cron46f91782021-03-17 08:16:34 +010086 operation->core_alg = PSA_ALG_CCM;
Ronald Cronecbc0682021-03-26 13:25:17 +010087 full_tag_length = 16;
Ronald Cron46f91782021-03-17 08:16:34 +010088 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
89 * The call to mbedtls_ccm_encrypt_and_tag or
90 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) {
92 return PSA_ERROR_INVALID_ARGUMENT;
93 }
Ronald Cron46f91782021-03-17 08:16:34 +010094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 mbedtls_ccm_init(&operation->ctx.ccm);
Ronald Cron46f91782021-03-17 08:16:34 +010096 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 mbedtls_ccm_setkey(&operation->ctx.ccm, cipher_id,
98 key_buffer, (unsigned int) key_bits));
99 if (status != PSA_SUCCESS) {
100 return status;
101 }
Ronald Cron46f91782021-03-17 08:16:34 +0100102 break;
103#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
104
105#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Ronald Cron46f91782021-03-17 08:16:34 +0100107 operation->core_alg = PSA_ALG_GCM;
Ronald Cronecbc0682021-03-26 13:25:17 +0100108 full_tag_length = 16;
Ronald Cron46f91782021-03-17 08:16:34 +0100109 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
110 * The call to mbedtls_gcm_crypt_and_tag or
111 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) {
113 return PSA_ERROR_INVALID_ARGUMENT;
114 }
Ronald Cron46f91782021-03-17 08:16:34 +0100115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 mbedtls_gcm_init(&operation->ctx.gcm);
Ronald Cron46f91782021-03-17 08:16:34 +0100117 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 mbedtls_gcm_setkey(&operation->ctx.gcm, cipher_id,
119 key_buffer, (unsigned int) key_bits));
120 if (status != PSA_SUCCESS) {
121 return status;
122 }
Ronald Cron46f91782021-03-17 08:16:34 +0100123 break;
124#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
125
126#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Ronald Cron46f91782021-03-17 08:16:34 +0100128 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
Ronald Cronecbc0682021-03-26 13:25:17 +0100129 full_tag_length = 16;
Ronald Cron46f91782021-03-17 08:16:34 +0100130 /* We only support the default tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 if (alg != PSA_ALG_CHACHA20_POLY1305) {
132 return PSA_ERROR_NOT_SUPPORTED;
133 }
Ronald Cron46f91782021-03-17 08:16:34 +0100134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 mbedtls_chachapoly_init(&operation->ctx.chachapoly);
Ronald Cron46f91782021-03-17 08:16:34 +0100136 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 mbedtls_chachapoly_setkey(&operation->ctx.chachapoly,
138 key_buffer));
139 if (status != PSA_SUCCESS) {
140 return status;
141 }
Ronald Cron46f91782021-03-17 08:16:34 +0100142 break;
143#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
144
145 default:
Ronald Cron485559e2021-04-28 14:29:00 +0200146 (void) status;
147 (void) key_buffer;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100149 }
150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 if (PSA_AEAD_TAG_LENGTH(attributes->core.type,
152 key_bits, alg)
153 > full_tag_length) {
154 return PSA_ERROR_INVALID_ARGUMENT;
155 }
Ronald Cron46f91782021-03-17 08:16:34 +0100156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 operation->tag_length = PSA_AEAD_TAG_LENGTH(attributes->core.type,
158 key_bits,
159 alg);
Ronald Cron46f91782021-03-17 08:16:34 +0100160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 return PSA_SUCCESS;
Ronald Cron46f91782021-03-17 08:16:34 +0100162}
163
164psa_status_t mbedtls_psa_aead_encrypt(
165 const psa_key_attributes_t *attributes,
166 const uint8_t *key_buffer, size_t key_buffer_size,
167 psa_algorithm_t alg,
168 const uint8_t *nonce, size_t nonce_length,
169 const uint8_t *additional_data, size_t additional_data_length,
170 const uint8_t *plaintext, size_t plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length)
Ronald Cron46f91782021-03-17 08:16:34 +0100172{
173 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
174 aead_operation_t operation = AEAD_OPERATION_INIT;
175 uint8_t *tag;
176 (void) key_buffer_size;
177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 status = psa_aead_setup(&operation, attributes, key_buffer, alg);
179 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100180 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 }
Ronald Cron46f91782021-03-17 08:16:34 +0100182
183 /* For all currently supported modes, the tag is at the end of the
184 * ciphertext. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 if (ciphertext_size < (plaintext_length + operation.tag_length)) {
Ronald Cron46f91782021-03-17 08:16:34 +0100186 status = PSA_ERROR_BUFFER_TOO_SMALL;
187 goto exit;
188 }
189 tag = ciphertext + plaintext_length;
190
Ronald Cron46f91782021-03-17 08:16:34 +0100191#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 if (operation.core_alg == PSA_ALG_CCM) {
Ronald Cron46f91782021-03-17 08:16:34 +0100193 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 mbedtls_ccm_encrypt_and_tag(&operation.ctx.ccm,
195 plaintext_length,
196 nonce, nonce_length,
197 additional_data,
198 additional_data_length,
199 plaintext, ciphertext,
200 tag, operation.tag_length));
201 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100202#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200203#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 if (operation.core_alg == PSA_ALG_GCM) {
Ronald Cron810eb162021-04-06 09:01:39 +0200205 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 mbedtls_gcm_crypt_and_tag(&operation.ctx.gcm,
207 MBEDTLS_GCM_ENCRYPT,
208 plaintext_length,
209 nonce, nonce_length,
210 additional_data, additional_data_length,
211 plaintext, ciphertext,
212 operation.tag_length, tag));
213 } else
Ronald Cron810eb162021-04-06 09:01:39 +0200214#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100215#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 if (operation.core_alg == PSA_ALG_CHACHA20_POLY1305) {
217 if (nonce_length != 12) {
218 if (nonce_length == 8) {
Bence Szépkúti358e0ea2021-11-17 14:03:08 +0100219 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 } else {
Bence Szépkúti358e0ea2021-11-17 14:03:08 +0100221 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 }
Bence Szépkúti358e0ea2021-11-17 14:03:08 +0100223 goto exit;
224 }
225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 if (operation.tag_length != 16) {
Ronald Cron46f91782021-03-17 08:16:34 +0100227 status = PSA_ERROR_NOT_SUPPORTED;
228 goto exit;
229 }
230 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 mbedtls_chachapoly_encrypt_and_tag(&operation.ctx.chachapoly,
232 plaintext_length,
233 nonce,
234 additional_data,
235 additional_data_length,
236 plaintext,
237 ciphertext,
238 tag));
239 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100240#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
241 {
242 (void) tag;
Ronald Cron485559e2021-04-28 14:29:00 +0200243 (void) nonce;
244 (void) nonce_length;
245 (void) additional_data;
246 (void) additional_data_length;
247 (void) plaintext;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100249 }
250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100252 *ciphertext_length = plaintext_length + operation.tag_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 }
Ronald Cron46f91782021-03-17 08:16:34 +0100254
255exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 psa_aead_abort_internal(&operation);
Ronald Cron46f91782021-03-17 08:16:34 +0100257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 return status;
Ronald Cron46f91782021-03-17 08:16:34 +0100259}
260
261/* Locate the tag in a ciphertext buffer containing the encrypted data
262 * followed by the tag. Return the length of the part preceding the tag in
263 * *plaintext_length. This is the size of the plaintext in modes where
264 * the encrypted data has the same size as the plaintext, such as
265 * CCM and GCM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266static psa_status_t psa_aead_unpadded_locate_tag(size_t tag_length,
267 const uint8_t *ciphertext,
268 size_t ciphertext_length,
269 size_t plaintext_size,
270 const uint8_t **p_tag)
Ronald Cron46f91782021-03-17 08:16:34 +0100271{
272 size_t payload_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 if (tag_length > ciphertext_length) {
274 return PSA_ERROR_INVALID_ARGUMENT;
275 }
Ronald Cron46f91782021-03-17 08:16:34 +0100276 payload_length = ciphertext_length - tag_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 if (payload_length > plaintext_size) {
278 return PSA_ERROR_BUFFER_TOO_SMALL;
279 }
Ronald Cron46f91782021-03-17 08:16:34 +0100280 *p_tag = ciphertext + payload_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 return PSA_SUCCESS;
Ronald Cron46f91782021-03-17 08:16:34 +0100282}
283
284psa_status_t mbedtls_psa_aead_decrypt(
285 const psa_key_attributes_t *attributes,
286 const uint8_t *key_buffer, size_t key_buffer_size,
287 psa_algorithm_t alg,
288 const uint8_t *nonce, size_t nonce_length,
289 const uint8_t *additional_data, size_t additional_data_length,
290 const uint8_t *ciphertext, size_t ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length)
Ronald Cron46f91782021-03-17 08:16:34 +0100292{
293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
294 aead_operation_t operation = AEAD_OPERATION_INIT;
295 const uint8_t *tag = NULL;
296 (void) key_buffer_size;
297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 status = psa_aead_setup(&operation, attributes, key_buffer, alg);
299 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100300 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 }
Ronald Cron46f91782021-03-17 08:16:34 +0100302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 status = psa_aead_unpadded_locate_tag(operation.tag_length,
304 ciphertext, ciphertext_length,
305 plaintext_size, &tag);
306 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100307 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 }
Ronald Cron46f91782021-03-17 08:16:34 +0100309
Ronald Cron46f91782021-03-17 08:16:34 +0100310#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if (operation.core_alg == PSA_ALG_CCM) {
Ronald Cron46f91782021-03-17 08:16:34 +0100312 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 mbedtls_ccm_auth_decrypt(&operation.ctx.ccm,
314 ciphertext_length - operation.tag_length,
315 nonce, nonce_length,
316 additional_data,
317 additional_data_length,
318 ciphertext, plaintext,
319 tag, operation.tag_length));
320 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100321#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200322#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 if (operation.core_alg == PSA_ALG_GCM) {
Ronald Cron810eb162021-04-06 09:01:39 +0200324 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 mbedtls_gcm_auth_decrypt(&operation.ctx.gcm,
326 ciphertext_length - operation.tag_length,
327 nonce, nonce_length,
328 additional_data,
329 additional_data_length,
330 tag, operation.tag_length,
331 ciphertext, plaintext));
332 } else
Ronald Cron810eb162021-04-06 09:01:39 +0200333#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100334#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 if (operation.core_alg == PSA_ALG_CHACHA20_POLY1305) {
336 if (nonce_length != 12) {
337 if (nonce_length == 8) {
Bence Szépkúti358e0ea2021-11-17 14:03:08 +0100338 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 } else {
Bence Szépkúti358e0ea2021-11-17 14:03:08 +0100340 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 }
Bence Szépkúti358e0ea2021-11-17 14:03:08 +0100342 goto exit;
343 }
344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 if (operation.tag_length != 16) {
Ronald Cron46f91782021-03-17 08:16:34 +0100346 status = PSA_ERROR_NOT_SUPPORTED;
347 goto exit;
348 }
349 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 mbedtls_chachapoly_auth_decrypt(&operation.ctx.chachapoly,
351 ciphertext_length - operation.tag_length,
352 nonce,
353 additional_data,
354 additional_data_length,
355 tag,
356 ciphertext,
357 plaintext));
358 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100359#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
360 {
Ronald Cron485559e2021-04-28 14:29:00 +0200361 (void) nonce;
362 (void) nonce_length;
363 (void) additional_data;
364 (void) additional_data_length;
365 (void) plaintext;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100367 }
368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100370 *plaintext_length = ciphertext_length - operation.tag_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 }
Ronald Cron46f91782021-03-17 08:16:34 +0100372
373exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 psa_aead_abort_internal(&operation);
Ronald Cron46f91782021-03-17 08:16:34 +0100375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100377 *plaintext_length = ciphertext_length - operation.tag_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 }
379 return status;
Ronald Cron46f91782021-03-17 08:16:34 +0100380}
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100381
382#endif /* MBEDTLS_PSA_CRYPTO_C */