blob: 73d8b01e9b2dd1f5a1937ac141e5a7628dd62ac0 [file] [log] [blame]
Ronald Cron7ceee8d2021-03-17 16:55:43 +01001/*
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
21#include "common.h"
22
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include "psa_crypto_aead.h"
Ronald Cron46f91782021-03-17 08:16:34 +010026#include "psa_crypto_core.h"
Dave Rodgman16304472022-11-02 09:25:38 +000027#include "psa_crypto_cipher.h"
Ronald Cron46f91782021-03-17 08:16:34 +010028
Paul Elliottadb8b162021-04-20 16:06:57 +010029#include <string.h>
30#include "mbedtls/platform.h"
Paul Elliottadb8b162021-04-20 16:06:57 +010031
Ronald Cron46f91782021-03-17 08:16:34 +010032#include "mbedtls/ccm.h"
33#include "mbedtls/chachapoly.h"
34#include "mbedtls/cipher.h"
35#include "mbedtls/gcm.h"
Paul Elliottadb8b162021-04-20 16:06:57 +010036#include "mbedtls/error.h"
Ronald Cron46f91782021-03-17 08:16:34 +010037
Ronald Cron46f91782021-03-17 08:16:34 +010038static psa_status_t psa_aead_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +010039 mbedtls_psa_aead_operation_t *operation,
Ronald Cron46f91782021-03-17 08:16:34 +010040 const psa_key_attributes_t *attributes,
41 const uint8_t *key_buffer,
Paul Elliottcc358592021-05-12 12:22:28 +010042 size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +010043 psa_algorithm_t alg)
Ronald Cron46f91782021-03-17 08:16:34 +010044{
45 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron46f91782021-03-17 08:16:34 +010046
Gilles Peskine449bd832023-01-11 14:50:10 +010047 (void) key_buffer_size;
Paul Elliottcc358592021-05-12 12:22:28 +010048
Valerio Setti2c2aded2023-08-25 09:22:19 +020049#if defined(MBEDTLS_CIPHER_C)
50 const mbedtls_cipher_info_t *cipher_info;
51 mbedtls_cipher_id_t cipher_id;
52 size_t key_bits = attributes->core.bits;
Ronald Cron46f91782021-03-17 08:16:34 +010053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 cipher_info = mbedtls_cipher_info_from_psa(alg,
55 attributes->core.type, key_bits,
56 &cipher_id);
57 if (cipher_info == NULL) {
58 return PSA_ERROR_NOT_SUPPORTED;
59 }
Valerio Setti2c2aded2023-08-25 09:22:19 +020060#endif /* MBEDTLS_CIPHER_C */
Ronald Cron46f91782021-03-17 08:16:34 +010061
Gilles Peskine449bd832023-01-11 14:50:10 +010062 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Ronald Cron46f91782021-03-17 08:16:34 +010063#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +010064 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Paul Elliott07a30c42021-04-20 14:13:23 +010065 operation->alg = PSA_ALG_CCM;
Ronald Cron46f91782021-03-17 08:16:34 +010066 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
67 * The call to mbedtls_ccm_encrypt_and_tag or
68 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +010069 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) {
70 return PSA_ERROR_INVALID_ARGUMENT;
71 }
Ronald Cron46f91782021-03-17 08:16:34 +010072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 mbedtls_ccm_init(&operation->ctx.ccm);
Ronald Cron46f91782021-03-17 08:16:34 +010074 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010075 mbedtls_ccm_setkey(&operation->ctx.ccm, cipher_id,
76 key_buffer, (unsigned int) key_bits));
77 if (status != PSA_SUCCESS) {
78 return status;
79 }
Ronald Cron46f91782021-03-17 08:16:34 +010080 break;
81#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
82
83#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +010084 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Paul Elliott07a30c42021-04-20 14:13:23 +010085 operation->alg = PSA_ALG_GCM;
Ronald Cron46f91782021-03-17 08:16:34 +010086 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
87 * The call to mbedtls_gcm_crypt_and_tag or
88 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +010089 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) {
90 return PSA_ERROR_INVALID_ARGUMENT;
91 }
Ronald Cron46f91782021-03-17 08:16:34 +010092
Gilles Peskine449bd832023-01-11 14:50:10 +010093 mbedtls_gcm_init(&operation->ctx.gcm);
Ronald Cron46f91782021-03-17 08:16:34 +010094 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010095 mbedtls_gcm_setkey(&operation->ctx.gcm, cipher_id,
96 key_buffer, (unsigned int) key_bits));
97 if (status != PSA_SUCCESS) {
98 return status;
99 }
Ronald Cron46f91782021-03-17 08:16:34 +0100100 break;
101#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
102
103#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Paul Elliott07a30c42021-04-20 14:13:23 +0100105 operation->alg = PSA_ALG_CHACHA20_POLY1305;
Ronald Cron46f91782021-03-17 08:16:34 +0100106 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (alg != PSA_ALG_CHACHA20_POLY1305) {
108 return PSA_ERROR_NOT_SUPPORTED;
109 }
Ronald Cron46f91782021-03-17 08:16:34 +0100110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_chachapoly_init(&operation->ctx.chachapoly);
Ronald Cron46f91782021-03-17 08:16:34 +0100112 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 mbedtls_chachapoly_setkey(&operation->ctx.chachapoly,
114 key_buffer));
115 if (status != PSA_SUCCESS) {
116 return status;
117 }
Ronald Cron46f91782021-03-17 08:16:34 +0100118 break;
119#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
120
121 default:
Ronald Cron7a55deb2021-04-28 14:29:00 +0200122 (void) status;
123 (void) key_buffer;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100125 }
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 operation->key_type = psa_get_key_type(attributes);
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 operation->tag_length = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Ronald Cron46f91782021-03-17 08:16:34 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return PSA_SUCCESS;
Ronald Cron46f91782021-03-17 08:16:34 +0100132}
133
134psa_status_t mbedtls_psa_aead_encrypt(
135 const psa_key_attributes_t *attributes,
136 const uint8_t *key_buffer, size_t key_buffer_size,
137 psa_algorithm_t alg,
138 const uint8_t *nonce, size_t nonce_length,
139 const uint8_t *additional_data, size_t additional_data_length,
140 const uint8_t *plaintext, size_t plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length)
Ronald Cron46f91782021-03-17 08:16:34 +0100142{
143 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100144 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100145 uint8_t *tag;
Ronald Cron46f91782021-03-17 08:16:34 +0100146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 status = psa_aead_setup(&operation, attributes, key_buffer,
148 key_buffer_size, alg);
Paul Elliottcc358592021-05-12 12:22:28 +0100149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 }
Ronald Cron46f91782021-03-17 08:16:34 +0100153
154 /* For all currently supported modes, the tag is at the end of the
155 * ciphertext. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if (ciphertext_size < (plaintext_length + operation.tag_length)) {
Ronald Cron46f91782021-03-17 08:16:34 +0100157 status = PSA_ERROR_BUFFER_TOO_SMALL;
158 goto exit;
159 }
160 tag = ciphertext + plaintext_length;
161
Ronald Cron46f91782021-03-17 08:16:34 +0100162#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (operation.alg == PSA_ALG_CCM) {
Ronald Cron46f91782021-03-17 08:16:34 +0100164 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_ccm_encrypt_and_tag(&operation.ctx.ccm,
166 plaintext_length,
167 nonce, nonce_length,
168 additional_data,
169 additional_data_length,
170 plaintext, ciphertext,
171 tag, operation.tag_length));
172 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100173#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200174#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (operation.alg == PSA_ALG_GCM) {
Ronald Cron810eb162021-04-06 09:01:39 +0200176 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 mbedtls_gcm_crypt_and_tag(&operation.ctx.gcm,
178 MBEDTLS_GCM_ENCRYPT,
179 plaintext_length,
180 nonce, nonce_length,
181 additional_data, additional_data_length,
182 plaintext, ciphertext,
183 operation.tag_length, tag));
184 } else
Ronald Cron810eb162021-04-06 09:01:39 +0200185#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100186#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (operation.alg == PSA_ALG_CHACHA20_POLY1305) {
188 if (operation.tag_length != 16) {
Ronald Cron46f91782021-03-17 08:16:34 +0100189 status = PSA_ERROR_NOT_SUPPORTED;
190 goto exit;
191 }
192 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_chachapoly_encrypt_and_tag(&operation.ctx.chachapoly,
194 plaintext_length,
195 nonce,
196 additional_data,
197 additional_data_length,
198 plaintext,
199 ciphertext,
200 tag));
201 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100202#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
203 {
204 (void) tag;
Ronald Cron7a55deb2021-04-28 14:29:00 +0200205 (void) nonce;
206 (void) nonce_length;
207 (void) additional_data;
208 (void) additional_data_length;
209 (void) plaintext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100211 }
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100214 *ciphertext_length = plaintext_length + operation.tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 }
Ronald Cron46f91782021-03-17 08:16:34 +0100216
217exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 mbedtls_psa_aead_abort(&operation);
Ronald Cron46f91782021-03-17 08:16:34 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return status;
Ronald Cron46f91782021-03-17 08:16:34 +0100221}
222
223/* Locate the tag in a ciphertext buffer containing the encrypted data
224 * followed by the tag. Return the length of the part preceding the tag in
225 * *plaintext_length. This is the size of the plaintext in modes where
226 * the encrypted data has the same size as the plaintext, such as
227 * CCM and GCM. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100228static psa_status_t psa_aead_unpadded_locate_tag(size_t tag_length,
229 const uint8_t *ciphertext,
230 size_t ciphertext_length,
231 size_t plaintext_size,
232 const uint8_t **p_tag)
Ronald Cron46f91782021-03-17 08:16:34 +0100233{
234 size_t payload_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (tag_length > ciphertext_length) {
236 return PSA_ERROR_INVALID_ARGUMENT;
237 }
Ronald Cron46f91782021-03-17 08:16:34 +0100238 payload_length = ciphertext_length - tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (payload_length > plaintext_size) {
240 return PSA_ERROR_BUFFER_TOO_SMALL;
241 }
Ronald Cron46f91782021-03-17 08:16:34 +0100242 *p_tag = ciphertext + payload_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_SUCCESS;
Ronald Cron46f91782021-03-17 08:16:34 +0100244}
245
246psa_status_t mbedtls_psa_aead_decrypt(
247 const psa_key_attributes_t *attributes,
248 const uint8_t *key_buffer, size_t key_buffer_size,
249 psa_algorithm_t alg,
250 const uint8_t *nonce, size_t nonce_length,
251 const uint8_t *additional_data, size_t additional_data_length,
252 const uint8_t *ciphertext, size_t ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length)
Ronald Cron46f91782021-03-17 08:16:34 +0100254{
255 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100256 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100257 const uint8_t *tag = NULL;
Ronald Cron46f91782021-03-17 08:16:34 +0100258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 status = psa_aead_setup(&operation, attributes, key_buffer,
260 key_buffer_size, alg);
Paul Elliottcc358592021-05-12 12:22:28 +0100261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100263 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 }
Ronald Cron46f91782021-03-17 08:16:34 +0100265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 status = psa_aead_unpadded_locate_tag(operation.tag_length,
267 ciphertext, ciphertext_length,
268 plaintext_size, &tag);
269 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100270 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 }
Ronald Cron46f91782021-03-17 08:16:34 +0100272
Ronald Cron46f91782021-03-17 08:16:34 +0100273#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (operation.alg == PSA_ALG_CCM) {
Ronald Cron46f91782021-03-17 08:16:34 +0100275 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 mbedtls_ccm_auth_decrypt(&operation.ctx.ccm,
277 ciphertext_length - operation.tag_length,
278 nonce, nonce_length,
279 additional_data,
280 additional_data_length,
281 ciphertext, plaintext,
282 tag, operation.tag_length));
283 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100284#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200285#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (operation.alg == PSA_ALG_GCM) {
Ronald Cron810eb162021-04-06 09:01:39 +0200287 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 mbedtls_gcm_auth_decrypt(&operation.ctx.gcm,
289 ciphertext_length - operation.tag_length,
290 nonce, nonce_length,
291 additional_data,
292 additional_data_length,
293 tag, operation.tag_length,
294 ciphertext, plaintext));
295 } else
Ronald Cron810eb162021-04-06 09:01:39 +0200296#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100297#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (operation.alg == PSA_ALG_CHACHA20_POLY1305) {
299 if (operation.tag_length != 16) {
Ronald Cron46f91782021-03-17 08:16:34 +0100300 status = PSA_ERROR_NOT_SUPPORTED;
301 goto exit;
302 }
303 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 mbedtls_chachapoly_auth_decrypt(&operation.ctx.chachapoly,
305 ciphertext_length - operation.tag_length,
306 nonce,
307 additional_data,
308 additional_data_length,
309 tag,
310 ciphertext,
311 plaintext));
312 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100313#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
314 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200315 (void) nonce;
316 (void) nonce_length;
317 (void) additional_data;
318 (void) additional_data_length;
319 (void) plaintext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100321 }
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100324 *plaintext_length = ciphertext_length - operation.tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 }
Ronald Cron46f91782021-03-17 08:16:34 +0100326
327exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 mbedtls_psa_aead_abort(&operation);
Ronald Cron46f91782021-03-17 08:16:34 +0100329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100331 *plaintext_length = ciphertext_length - operation.tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 }
333 return status;
Ronald Cron46f91782021-03-17 08:16:34 +0100334}
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100335
Paul Elliottadb8b162021-04-20 16:06:57 +0100336/* Set the key and algorithm for a multipart authenticated encryption
337 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100338psa_status_t mbedtls_psa_aead_encrypt_setup(
339 mbedtls_psa_aead_operation_t *operation,
340 const psa_key_attributes_t *attributes,
341 const uint8_t *key_buffer,
342 size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 psa_algorithm_t alg)
Paul Elliottadb8b162021-04-20 16:06:57 +0100344{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100345 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 status = psa_aead_setup(operation, attributes, key_buffer,
348 key_buffer_size, alg);
Paul Elliottadb8b162021-04-20 16:06:57 +0100349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (status == PSA_SUCCESS) {
Paul Elliottadb8b162021-04-20 16:06:57 +0100351 operation->is_encrypt = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100355}
356
357/* Set the key and algorithm for a multipart authenticated decryption
358 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100359psa_status_t mbedtls_psa_aead_decrypt_setup(
360 mbedtls_psa_aead_operation_t *operation,
361 const psa_key_attributes_t *attributes,
362 const uint8_t *key_buffer,
363 size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 psa_algorithm_t alg)
Paul Elliottadb8b162021-04-20 16:06:57 +0100365{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 status = psa_aead_setup(operation, attributes, key_buffer,
369 key_buffer_size, alg);
Paul Elliottadb8b162021-04-20 16:06:57 +0100370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (status == PSA_SUCCESS) {
Paul Elliottadb8b162021-04-20 16:06:57 +0100372 operation->is_encrypt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100376}
377
Paul Elliottadb8b162021-04-20 16:06:57 +0100378/* Set a nonce for the multipart AEAD operation*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100379psa_status_t mbedtls_psa_aead_set_nonce(
380 mbedtls_psa_aead_operation_t *operation,
381 const uint8_t *nonce,
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 size_t nonce_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100383{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100384 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100385
Paul Elliottbc949782021-06-03 15:29:00 +0100386#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100388 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 mbedtls_gcm_starts(&operation->ctx.gcm,
390 operation->is_encrypt ?
391 MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
392 nonce,
393 nonce_length));
394 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100395#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100396#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +0100398 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_ccm_starts(&operation->ctx.ccm,
400 operation->is_encrypt ?
401 MBEDTLS_CCM_ENCRYPT : MBEDTLS_CCM_DECRYPT,
402 nonce,
403 nonce_length));
404 } else
Paul Elliotte193ea82021-10-01 13:00:16 +0100405#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100406#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
Paul Elliott946c9202021-09-28 14:32:55 +0100408 /* Note - ChaChaPoly allows an 8 byte nonce, but we would have to
409 * allocate a buffer in the operation, copy the nonce to it and pad
410 * it, so for now check the nonce is 12 bytes, as
411 * mbedtls_chachapoly_starts() assumes it can read 12 bytes from the
412 * passed in buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if (nonce_length != 12) {
414 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliott946c9202021-09-28 14:32:55 +0100415 }
416
Paul Elliott2df40052021-05-07 17:52:18 +0100417 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 mbedtls_chachapoly_starts(&operation->ctx.chachapoly,
Paul Elliott2df40052021-05-07 17:52:18 +0100419 nonce,
420 operation->is_encrypt ?
421 MBEDTLS_CHACHAPOLY_ENCRYPT :
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 MBEDTLS_CHACHAPOLY_DECRYPT));
423 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100424#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
425 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 (void) operation;
427 (void) nonce;
428 (void) nonce_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100431 }
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100434}
Paul Elliottefda3402021-08-25 17:16:52 +0100435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436/* Declare the lengths of the message and additional data for AEAD. */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100437psa_status_t mbedtls_psa_aead_set_lengths(
438 mbedtls_psa_aead_operation_t *operation,
439 size_t ad_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 size_t plaintext_length)
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100441{
Paul Elliotte193ea82021-10-01 13:00:16 +0100442#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (operation->alg == PSA_ALG_CCM) {
444 return mbedtls_to_psa_error(
445 mbedtls_ccm_set_lengths(&operation->ctx.ccm,
446 ad_length,
447 plaintext_length,
448 operation->tag_length));
Paul Elliotte193ea82021-10-01 13:00:16 +0100449
450 }
451#else /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 (void) operation;
453 (void) ad_length;
454 (void) plaintext_length;
Paul Elliotte193ea82021-10-01 13:00:16 +0100455#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return PSA_SUCCESS;
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100458}
459
Paul Elliottadb8b162021-04-20 16:06:57 +0100460/* Pass additional data to an active multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100461psa_status_t mbedtls_psa_aead_update_ad(
462 mbedtls_psa_aead_operation_t *operation,
463 const uint8_t *input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 size_t input_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100465{
466 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
467
Paul Elliottadb8b162021-04-20 16:06:57 +0100468#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott2df40052021-05-07 17:52:18 +0100470 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 mbedtls_gcm_update_ad(&operation->ctx.gcm, input, input_length));
472 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100473#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100474#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +0100476 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 mbedtls_ccm_update_ad(&operation->ctx.ccm, input, input_length));
478 } else
Paul Elliotte193ea82021-10-01 13:00:16 +0100479#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100480#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
Paul Elliott2df40052021-05-07 17:52:18 +0100482 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 mbedtls_chachapoly_update_aad(&operation->ctx.chachapoly,
Paul Elliott2df40052021-05-07 17:52:18 +0100484 input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 input_length));
486 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100487#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
488 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 (void) operation;
490 (void) input;
491 (void) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100494 }
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100497}
498
499/* Encrypt or decrypt a message fragment in an active multipart AEAD
500 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100501psa_status_t mbedtls_psa_aead_update(
502 mbedtls_psa_aead_operation_t *operation,
503 const uint8_t *input,
504 size_t input_length,
505 uint8_t *output,
506 size_t output_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 size_t *output_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100508{
Paul Elliotte2c788d2021-05-13 17:16:01 +0100509 size_t update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100510 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
511
Paul Elliotte2c788d2021-05-13 17:16:01 +0100512 update_output_length = input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100513
Paul Elliottadb8b162021-04-20 16:06:57 +0100514#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100516 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 mbedtls_gcm_update(&operation->ctx.gcm,
Paul Elliotte193ea82021-10-01 13:00:16 +0100518 input, input_length,
519 output, output_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 &update_output_length));
521 } else
522#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
523#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
524 if (operation->alg == PSA_ALG_CCM) {
525 if (output_size < input_length) {
526 return PSA_ERROR_BUFFER_TOO_SMALL;
527 }
Paul Elliottecce9012021-07-23 15:44:11 +0100528
Paul Elliott2df40052021-05-07 17:52:18 +0100529 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 mbedtls_ccm_update(&operation->ctx.ccm,
531 input, input_length,
532 output, output_size,
533 &update_output_length));
534 } else
535#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
536#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
537 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
538 if (output_size < input_length) {
539 return PSA_ERROR_BUFFER_TOO_SMALL;
540 }
541
542 status = mbedtls_to_psa_error(
543 mbedtls_chachapoly_update(&operation->ctx.chachapoly,
Paul Elliott2df40052021-05-07 17:52:18 +0100544 input_length,
545 input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 output));
547 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100548#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
549 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 (void) operation;
551 (void) input;
552 (void) output;
553 (void) output_size;
Paul Elliottadb8b162021-04-20 16:06:57 +0100554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100556 }
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (status == PSA_SUCCESS) {
Paul Elliotte2c788d2021-05-13 17:16:01 +0100559 *output_length = update_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100563}
564
Paul Elliottadb8b162021-04-20 16:06:57 +0100565/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100566psa_status_t mbedtls_psa_aead_finish(
567 mbedtls_psa_aead_operation_t *operation,
568 uint8_t *ciphertext,
569 size_t ciphertext_size,
570 size_t *ciphertext_length,
571 uint8_t *tag,
572 size_t tag_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 size_t *tag_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100574{
575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100576 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (tag_size < operation->tag_length) {
579 return PSA_ERROR_BUFFER_TOO_SMALL;
580 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100581
582#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100584 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 mbedtls_gcm_finish(&operation->ctx.gcm,
586 ciphertext, ciphertext_size, ciphertext_length,
587 tag, operation->tag_length));
588 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100589#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100590#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +0100592 /* tag must be big enough to store a tag of size passed into set
593 * lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (tag_size < operation->tag_length) {
595 return PSA_ERROR_BUFFER_TOO_SMALL;
596 }
Paul Elliotte193ea82021-10-01 13:00:16 +0100597
598 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 mbedtls_ccm_finish(&operation->ctx.ccm,
600 tag, operation->tag_length));
601 } else
Paul Elliotte193ea82021-10-01 13:00:16 +0100602#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100603#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
Paul Elliotted08cf82021-07-22 18:48:24 +0100605 /* Belt and braces. Although the above tag_size check should have
606 * already done this, if we later start supporting smaller tag sizes
607 * for chachapoly, then passing a tag buffer smaller than 16 into here
608 * could cause a buffer overflow, so better safe than sorry. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 if (tag_size < 16) {
610 return PSA_ERROR_BUFFER_TOO_SMALL;
611 }
Paul Elliotted08cf82021-07-22 18:48:24 +0100612
Paul Elliott2df40052021-05-07 17:52:18 +0100613 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 mbedtls_chachapoly_finish(&operation->ctx.chachapoly,
615 tag));
616 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100617#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
618 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 (void) ciphertext;
620 (void) ciphertext_size;
621 (void) ciphertext_length;
622 (void) tag;
623 (void) tag_size;
624 (void) tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100627 }
628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (status == PSA_SUCCESS) {
Paul Elliott8ff74212021-09-19 18:39:23 +0100630 /* This will be zero for all supported algorithms currently, but left
631 * here for future support. */
Paul Elliottadb8b162021-04-20 16:06:57 +0100632 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100633 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100634 }
635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100637}
638
Paul Elliottadb8b162021-04-20 16:06:57 +0100639/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100640psa_status_t mbedtls_psa_aead_abort(
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 mbedtls_psa_aead_operation_t *operation)
Paul Elliottadb8b162021-04-20 16:06:57 +0100642{
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 switch (operation->alg) {
Paul Elliott811d8d42021-04-22 11:31:14 +0100644#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
645 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 mbedtls_ccm_free(&operation->ctx.ccm);
Paul Elliottadb8b162021-04-20 16:06:57 +0100647 break;
648#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
649#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
650 case PSA_ALG_GCM:
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 mbedtls_gcm_free(&operation->ctx.gcm);
Paul Elliottadb8b162021-04-20 16:06:57 +0100652 break;
653#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
654#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100655 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 mbedtls_chachapoly_free(&operation->ctx.chachapoly);
Paul Elliott811d8d42021-04-22 11:31:14 +0100657 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100658#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
659 }
660
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100661 operation->is_encrypt = 0;
Paul Elliott1a98aca2021-05-20 18:24:07 +0100662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 return PSA_SUCCESS;
Paul Elliottadb8b162021-04-20 16:06:57 +0100664}
665
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100666#endif /* MBEDTLS_PSA_CRYPTO_C */