blob: 85d1f39be77bcd2cc5b59c1671fb3faa1bf5dbf6 [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;
46 size_t key_bits;
Ronald Cronecbc0682021-03-26 13:25:17 +010047 const mbedtls_cipher_info_t *cipher_info;
Ronald Cron46f91782021-03-17 08:16:34 +010048 mbedtls_cipher_id_t cipher_id;
49
Gilles Peskine449bd832023-01-11 14:50:10 +010050 (void) key_buffer_size;
Paul Elliottcc358592021-05-12 12:22:28 +010051
Ronald Cron46f91782021-03-17 08:16:34 +010052 key_bits = attributes->core.bits;
53
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 }
Ronald Cron46f91782021-03-17 08:16:34 +010060
Gilles Peskine449bd832023-01-11 14:50:10 +010061 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Ronald Cron46f91782021-03-17 08:16:34 +010062#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +010063 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Paul Elliott07a30c42021-04-20 14:13:23 +010064 operation->alg = PSA_ALG_CCM;
Ronald Cron46f91782021-03-17 08:16:34 +010065 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
66 * The call to mbedtls_ccm_encrypt_and_tag or
67 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +010068 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) {
69 return PSA_ERROR_INVALID_ARGUMENT;
70 }
Ronald Cron46f91782021-03-17 08:16:34 +010071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 mbedtls_ccm_init(&operation->ctx.ccm);
Ronald Cron46f91782021-03-17 08:16:34 +010073 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010074 mbedtls_ccm_setkey(&operation->ctx.ccm, cipher_id,
75 key_buffer, (unsigned int) key_bits));
76 if (status != PSA_SUCCESS) {
77 return status;
78 }
Ronald Cron46f91782021-03-17 08:16:34 +010079 break;
80#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
81
82#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +010083 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Paul Elliott07a30c42021-04-20 14:13:23 +010084 operation->alg = PSA_ALG_GCM;
Ronald Cron46f91782021-03-17 08:16:34 +010085 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
86 * The call to mbedtls_gcm_crypt_and_tag or
87 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) {
89 return PSA_ERROR_INVALID_ARGUMENT;
90 }
Ronald Cron46f91782021-03-17 08:16:34 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_gcm_init(&operation->ctx.gcm);
Ronald Cron46f91782021-03-17 08:16:34 +010093 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010094 mbedtls_gcm_setkey(&operation->ctx.gcm, cipher_id,
95 key_buffer, (unsigned int) key_bits));
96 if (status != PSA_SUCCESS) {
97 return status;
98 }
Ronald Cron46f91782021-03-17 08:16:34 +010099 break;
100#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
101
102#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Paul Elliott07a30c42021-04-20 14:13:23 +0100104 operation->alg = PSA_ALG_CHACHA20_POLY1305;
Ronald Cron46f91782021-03-17 08:16:34 +0100105 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (alg != PSA_ALG_CHACHA20_POLY1305) {
107 return PSA_ERROR_NOT_SUPPORTED;
108 }
Ronald Cron46f91782021-03-17 08:16:34 +0100109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_chachapoly_init(&operation->ctx.chachapoly);
Ronald Cron46f91782021-03-17 08:16:34 +0100111 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_chachapoly_setkey(&operation->ctx.chachapoly,
113 key_buffer));
114 if (status != PSA_SUCCESS) {
115 return status;
116 }
Ronald Cron46f91782021-03-17 08:16:34 +0100117 break;
118#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
119
120 default:
Ronald Cron7a55deb2021-04-28 14:29:00 +0200121 (void) status;
122 (void) key_buffer;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 operation->key_type = psa_get_key_type(attributes);
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 operation->tag_length = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Ronald Cron46f91782021-03-17 08:16:34 +0100129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return PSA_SUCCESS;
Ronald Cron46f91782021-03-17 08:16:34 +0100131}
132
133psa_status_t mbedtls_psa_aead_encrypt(
134 const psa_key_attributes_t *attributes,
135 const uint8_t *key_buffer, size_t key_buffer_size,
136 psa_algorithm_t alg,
137 const uint8_t *nonce, size_t nonce_length,
138 const uint8_t *additional_data, size_t additional_data_length,
139 const uint8_t *plaintext, size_t plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length)
Ronald Cron46f91782021-03-17 08:16:34 +0100141{
142 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100143 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100144 uint8_t *tag;
Ronald Cron46f91782021-03-17 08:16:34 +0100145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 status = psa_aead_setup(&operation, attributes, key_buffer,
147 key_buffer_size, alg);
Paul Elliottcc358592021-05-12 12:22:28 +0100148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100150 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 }
Ronald Cron46f91782021-03-17 08:16:34 +0100152
153 /* For all currently supported modes, the tag is at the end of the
154 * ciphertext. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if (ciphertext_size < (plaintext_length + operation.tag_length)) {
Ronald Cron46f91782021-03-17 08:16:34 +0100156 status = PSA_ERROR_BUFFER_TOO_SMALL;
157 goto exit;
158 }
159 tag = ciphertext + plaintext_length;
160
Ronald Cron46f91782021-03-17 08:16:34 +0100161#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if (operation.alg == PSA_ALG_CCM) {
Ronald Cron46f91782021-03-17 08:16:34 +0100163 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 mbedtls_ccm_encrypt_and_tag(&operation.ctx.ccm,
165 plaintext_length,
166 nonce, nonce_length,
167 additional_data,
168 additional_data_length,
169 plaintext, ciphertext,
170 tag, operation.tag_length));
171 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100172#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200173#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (operation.alg == PSA_ALG_GCM) {
Ronald Cron810eb162021-04-06 09:01:39 +0200175 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 mbedtls_gcm_crypt_and_tag(&operation.ctx.gcm,
177 MBEDTLS_GCM_ENCRYPT,
178 plaintext_length,
179 nonce, nonce_length,
180 additional_data, additional_data_length,
181 plaintext, ciphertext,
182 operation.tag_length, tag));
183 } else
Ronald Cron810eb162021-04-06 09:01:39 +0200184#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100185#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (operation.alg == PSA_ALG_CHACHA20_POLY1305) {
187 if (operation.tag_length != 16) {
Ronald Cron46f91782021-03-17 08:16:34 +0100188 status = PSA_ERROR_NOT_SUPPORTED;
189 goto exit;
190 }
191 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 mbedtls_chachapoly_encrypt_and_tag(&operation.ctx.chachapoly,
193 plaintext_length,
194 nonce,
195 additional_data,
196 additional_data_length,
197 plaintext,
198 ciphertext,
199 tag));
200 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100201#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
202 {
203 (void) tag;
Ronald Cron7a55deb2021-04-28 14:29:00 +0200204 (void) nonce;
205 (void) nonce_length;
206 (void) additional_data;
207 (void) additional_data_length;
208 (void) plaintext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100210 }
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100213 *ciphertext_length = plaintext_length + operation.tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 }
Ronald Cron46f91782021-03-17 08:16:34 +0100215
216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_psa_aead_abort(&operation);
Ronald Cron46f91782021-03-17 08:16:34 +0100218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return status;
Ronald Cron46f91782021-03-17 08:16:34 +0100220}
221
222/* Locate the tag in a ciphertext buffer containing the encrypted data
223 * followed by the tag. Return the length of the part preceding the tag in
224 * *plaintext_length. This is the size of the plaintext in modes where
225 * the encrypted data has the same size as the plaintext, such as
226 * CCM and GCM. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227static psa_status_t psa_aead_unpadded_locate_tag(size_t tag_length,
228 const uint8_t *ciphertext,
229 size_t ciphertext_length,
230 size_t plaintext_size,
231 const uint8_t **p_tag)
Ronald Cron46f91782021-03-17 08:16:34 +0100232{
233 size_t payload_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (tag_length > ciphertext_length) {
235 return PSA_ERROR_INVALID_ARGUMENT;
236 }
Ronald Cron46f91782021-03-17 08:16:34 +0100237 payload_length = ciphertext_length - tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if (payload_length > plaintext_size) {
239 return PSA_ERROR_BUFFER_TOO_SMALL;
240 }
Ronald Cron46f91782021-03-17 08:16:34 +0100241 *p_tag = ciphertext + payload_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_SUCCESS;
Ronald Cron46f91782021-03-17 08:16:34 +0100243}
244
245psa_status_t mbedtls_psa_aead_decrypt(
246 const psa_key_attributes_t *attributes,
247 const uint8_t *key_buffer, size_t key_buffer_size,
248 psa_algorithm_t alg,
249 const uint8_t *nonce, size_t nonce_length,
250 const uint8_t *additional_data, size_t additional_data_length,
251 const uint8_t *ciphertext, size_t ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length)
Ronald Cron46f91782021-03-17 08:16:34 +0100253{
254 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100255 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100256 const uint8_t *tag = NULL;
Ronald Cron46f91782021-03-17 08:16:34 +0100257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 status = psa_aead_setup(&operation, attributes, key_buffer,
259 key_buffer_size, alg);
Paul Elliottcc358592021-05-12 12:22:28 +0100260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100262 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 }
Ronald Cron46f91782021-03-17 08:16:34 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 status = psa_aead_unpadded_locate_tag(operation.tag_length,
266 ciphertext, ciphertext_length,
267 plaintext_size, &tag);
268 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100269 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 }
Ronald Cron46f91782021-03-17 08:16:34 +0100271
Ronald Cron46f91782021-03-17 08:16:34 +0100272#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (operation.alg == PSA_ALG_CCM) {
Ronald Cron46f91782021-03-17 08:16:34 +0100274 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 mbedtls_ccm_auth_decrypt(&operation.ctx.ccm,
276 ciphertext_length - operation.tag_length,
277 nonce, nonce_length,
278 additional_data,
279 additional_data_length,
280 ciphertext, plaintext,
281 tag, operation.tag_length));
282 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100283#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200284#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (operation.alg == PSA_ALG_GCM) {
Ronald Cron810eb162021-04-06 09:01:39 +0200286 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 mbedtls_gcm_auth_decrypt(&operation.ctx.gcm,
288 ciphertext_length - operation.tag_length,
289 nonce, nonce_length,
290 additional_data,
291 additional_data_length,
292 tag, operation.tag_length,
293 ciphertext, plaintext));
294 } else
Ronald Cron810eb162021-04-06 09:01:39 +0200295#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100296#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (operation.alg == PSA_ALG_CHACHA20_POLY1305) {
298 if (operation.tag_length != 16) {
Ronald Cron46f91782021-03-17 08:16:34 +0100299 status = PSA_ERROR_NOT_SUPPORTED;
300 goto exit;
301 }
302 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 mbedtls_chachapoly_auth_decrypt(&operation.ctx.chachapoly,
304 ciphertext_length - operation.tag_length,
305 nonce,
306 additional_data,
307 additional_data_length,
308 tag,
309 ciphertext,
310 plaintext));
311 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100312#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
313 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200314 (void) nonce;
315 (void) nonce_length;
316 (void) additional_data;
317 (void) additional_data_length;
318 (void) plaintext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100320 }
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100323 *plaintext_length = ciphertext_length - operation.tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 }
Ronald Cron46f91782021-03-17 08:16:34 +0100325
326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 mbedtls_psa_aead_abort(&operation);
Ronald Cron46f91782021-03-17 08:16:34 +0100328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100330 *plaintext_length = ciphertext_length - operation.tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 }
332 return status;
Ronald Cron46f91782021-03-17 08:16:34 +0100333}
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100334
Paul Elliottadb8b162021-04-20 16:06:57 +0100335/* Set the key and algorithm for a multipart authenticated encryption
336 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100337psa_status_t mbedtls_psa_aead_encrypt_setup(
338 mbedtls_psa_aead_operation_t *operation,
339 const psa_key_attributes_t *attributes,
340 const uint8_t *key_buffer,
341 size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 psa_algorithm_t alg)
Paul Elliottadb8b162021-04-20 16:06:57 +0100343{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 status = psa_aead_setup(operation, attributes, key_buffer,
347 key_buffer_size, alg);
Paul Elliottadb8b162021-04-20 16:06:57 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (status == PSA_SUCCESS) {
Paul Elliottadb8b162021-04-20 16:06:57 +0100350 operation->is_encrypt = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100354}
355
356/* Set the key and algorithm for a multipart authenticated decryption
357 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100358psa_status_t mbedtls_psa_aead_decrypt_setup(
359 mbedtls_psa_aead_operation_t *operation,
360 const psa_key_attributes_t *attributes,
361 const uint8_t *key_buffer,
362 size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 psa_algorithm_t alg)
Paul Elliottadb8b162021-04-20 16:06:57 +0100364{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 status = psa_aead_setup(operation, attributes, key_buffer,
368 key_buffer_size, alg);
Paul Elliottadb8b162021-04-20 16:06:57 +0100369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (status == PSA_SUCCESS) {
Paul Elliottadb8b162021-04-20 16:06:57 +0100371 operation->is_encrypt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100375}
376
Paul Elliottadb8b162021-04-20 16:06:57 +0100377/* Set a nonce for the multipart AEAD operation*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100378psa_status_t mbedtls_psa_aead_set_nonce(
379 mbedtls_psa_aead_operation_t *operation,
380 const uint8_t *nonce,
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 size_t nonce_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100382{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100383 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100384
Paul Elliottbc949782021-06-03 15:29:00 +0100385#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100387 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_gcm_starts(&operation->ctx.gcm,
389 operation->is_encrypt ?
390 MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
391 nonce,
392 nonce_length));
393 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100394#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100395#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +0100397 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 mbedtls_ccm_starts(&operation->ctx.ccm,
399 operation->is_encrypt ?
400 MBEDTLS_CCM_ENCRYPT : MBEDTLS_CCM_DECRYPT,
401 nonce,
402 nonce_length));
403 } else
Paul Elliotte193ea82021-10-01 13:00:16 +0100404#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100405#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
Paul Elliott946c9202021-09-28 14:32:55 +0100407 /* Note - ChaChaPoly allows an 8 byte nonce, but we would have to
408 * allocate a buffer in the operation, copy the nonce to it and pad
409 * it, so for now check the nonce is 12 bytes, as
410 * mbedtls_chachapoly_starts() assumes it can read 12 bytes from the
411 * passed in buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (nonce_length != 12) {
413 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliott946c9202021-09-28 14:32:55 +0100414 }
415
Paul Elliott2df40052021-05-07 17:52:18 +0100416 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 mbedtls_chachapoly_starts(&operation->ctx.chachapoly,
Paul Elliott2df40052021-05-07 17:52:18 +0100418 nonce,
419 operation->is_encrypt ?
420 MBEDTLS_CHACHAPOLY_ENCRYPT :
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 MBEDTLS_CHACHAPOLY_DECRYPT));
422 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100423#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
424 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 (void) operation;
426 (void) nonce;
427 (void) nonce_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100430 }
431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100433}
Paul Elliottefda3402021-08-25 17:16:52 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435/* Declare the lengths of the message and additional data for AEAD. */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100436psa_status_t mbedtls_psa_aead_set_lengths(
437 mbedtls_psa_aead_operation_t *operation,
438 size_t ad_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 size_t plaintext_length)
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100440{
Paul Elliotte193ea82021-10-01 13:00:16 +0100441#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (operation->alg == PSA_ALG_CCM) {
443 return mbedtls_to_psa_error(
444 mbedtls_ccm_set_lengths(&operation->ctx.ccm,
445 ad_length,
446 plaintext_length,
447 operation->tag_length));
Paul Elliotte193ea82021-10-01 13:00:16 +0100448
449 }
450#else /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 (void) operation;
452 (void) ad_length;
453 (void) plaintext_length;
Paul Elliotte193ea82021-10-01 13:00:16 +0100454#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return PSA_SUCCESS;
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100457}
458
Paul Elliottadb8b162021-04-20 16:06:57 +0100459/* Pass additional data to an active multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100460psa_status_t mbedtls_psa_aead_update_ad(
461 mbedtls_psa_aead_operation_t *operation,
462 const uint8_t *input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 size_t input_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100464{
465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
466
Paul Elliottadb8b162021-04-20 16:06:57 +0100467#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott2df40052021-05-07 17:52:18 +0100469 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 mbedtls_gcm_update_ad(&operation->ctx.gcm, input, input_length));
471 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100472#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100473#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +0100475 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 mbedtls_ccm_update_ad(&operation->ctx.ccm, input, input_length));
477 } else
Paul Elliotte193ea82021-10-01 13:00:16 +0100478#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100479#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
Paul Elliott2df40052021-05-07 17:52:18 +0100481 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 mbedtls_chachapoly_update_aad(&operation->ctx.chachapoly,
Paul Elliott2df40052021-05-07 17:52:18 +0100483 input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 input_length));
485 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100486#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
487 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 (void) operation;
489 (void) input;
490 (void) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100493 }
494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100496}
497
498/* Encrypt or decrypt a message fragment in an active multipart AEAD
499 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100500psa_status_t mbedtls_psa_aead_update(
501 mbedtls_psa_aead_operation_t *operation,
502 const uint8_t *input,
503 size_t input_length,
504 uint8_t *output,
505 size_t output_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 size_t *output_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100507{
Paul Elliotte2c788d2021-05-13 17:16:01 +0100508 size_t update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100509 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
510
Paul Elliotte2c788d2021-05-13 17:16:01 +0100511 update_output_length = input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100512
Paul Elliottadb8b162021-04-20 16:06:57 +0100513#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100515 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 mbedtls_gcm_update(&operation->ctx.gcm,
Paul Elliotte193ea82021-10-01 13:00:16 +0100517 input, input_length,
518 output, output_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 &update_output_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 if (output_size < input_length) {
525 return PSA_ERROR_BUFFER_TOO_SMALL;
526 }
Paul Elliottecce9012021-07-23 15:44:11 +0100527
Paul Elliott2df40052021-05-07 17:52:18 +0100528 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 mbedtls_ccm_update(&operation->ctx.ccm,
530 input, input_length,
531 output, output_size,
532 &update_output_length));
533 } else
534#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
535#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
536 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
537 if (output_size < input_length) {
538 return PSA_ERROR_BUFFER_TOO_SMALL;
539 }
540
541 status = mbedtls_to_psa_error(
542 mbedtls_chachapoly_update(&operation->ctx.chachapoly,
Paul Elliott2df40052021-05-07 17:52:18 +0100543 input_length,
544 input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 output));
546 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100547#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
548 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 (void) operation;
550 (void) input;
551 (void) output;
552 (void) output_size;
Paul Elliottadb8b162021-04-20 16:06:57 +0100553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100555 }
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (status == PSA_SUCCESS) {
Paul Elliotte2c788d2021-05-13 17:16:01 +0100558 *output_length = update_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100562}
563
Paul Elliottadb8b162021-04-20 16:06:57 +0100564/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100565psa_status_t mbedtls_psa_aead_finish(
566 mbedtls_psa_aead_operation_t *operation,
567 uint8_t *ciphertext,
568 size_t ciphertext_size,
569 size_t *ciphertext_length,
570 uint8_t *tag,
571 size_t tag_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 size_t *tag_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100573{
574 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100575 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (tag_size < operation->tag_length) {
578 return PSA_ERROR_BUFFER_TOO_SMALL;
579 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100580
581#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100583 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 mbedtls_gcm_finish(&operation->ctx.gcm,
585 ciphertext, ciphertext_size, ciphertext_length,
586 tag, operation->tag_length));
587 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100588#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100589#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +0100591 /* tag must be big enough to store a tag of size passed into set
592 * lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (tag_size < operation->tag_length) {
594 return PSA_ERROR_BUFFER_TOO_SMALL;
595 }
Paul Elliotte193ea82021-10-01 13:00:16 +0100596
597 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 mbedtls_ccm_finish(&operation->ctx.ccm,
599 tag, operation->tag_length));
600 } else
Paul Elliotte193ea82021-10-01 13:00:16 +0100601#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100602#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
Paul Elliotted08cf82021-07-22 18:48:24 +0100604 /* Belt and braces. Although the above tag_size check should have
605 * already done this, if we later start supporting smaller tag sizes
606 * for chachapoly, then passing a tag buffer smaller than 16 into here
607 * could cause a buffer overflow, so better safe than sorry. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (tag_size < 16) {
609 return PSA_ERROR_BUFFER_TOO_SMALL;
610 }
Paul Elliotted08cf82021-07-22 18:48:24 +0100611
Paul Elliott2df40052021-05-07 17:52:18 +0100612 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 mbedtls_chachapoly_finish(&operation->ctx.chachapoly,
614 tag));
615 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100616#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
617 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 (void) ciphertext;
619 (void) ciphertext_size;
620 (void) ciphertext_length;
621 (void) tag;
622 (void) tag_size;
623 (void) tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100626 }
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (status == PSA_SUCCESS) {
Paul Elliott8ff74212021-09-19 18:39:23 +0100629 /* This will be zero for all supported algorithms currently, but left
630 * here for future support. */
Paul Elliottadb8b162021-04-20 16:06:57 +0100631 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100632 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100633 }
634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100636}
637
Paul Elliottadb8b162021-04-20 16:06:57 +0100638/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100639psa_status_t mbedtls_psa_aead_abort(
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 mbedtls_psa_aead_operation_t *operation)
Paul Elliottadb8b162021-04-20 16:06:57 +0100641{
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 switch (operation->alg) {
Paul Elliott811d8d42021-04-22 11:31:14 +0100643#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
644 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 mbedtls_ccm_free(&operation->ctx.ccm);
Paul Elliottadb8b162021-04-20 16:06:57 +0100646 break;
647#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
648#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
649 case PSA_ALG_GCM:
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 mbedtls_gcm_free(&operation->ctx.gcm);
Paul Elliottadb8b162021-04-20 16:06:57 +0100651 break;
652#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
653#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100654 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 mbedtls_chachapoly_free(&operation->ctx.chachapoly);
Paul Elliott811d8d42021-04-22 11:31:14 +0100656 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100657#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
658 }
659
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100660 operation->is_encrypt = 0;
Paul Elliott1a98aca2021-05-20 18:24:07 +0100661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 return PSA_SUCCESS;
Paul Elliottadb8b162021-04-20 16:06:57 +0100663}
664
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100665#endif /* MBEDTLS_PSA_CRYPTO_C */