blob: 6f026a0d7d77671989ef07cceaa5fe6f2eddc482 [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;
Valerio Setti4a249822023-10-18 12:34:54 +020046 mbedtls_cipher_id_t cipher_id;
47 mbedtls_cipher_mode_t mode;
48 size_t key_bits = attributes->core.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +010049 (void) key_buffer_size;
Paul Elliottcc358592021-05-12 12:22:28 +010050
Valerio Setti4a249822023-10-18 12:34:54 +020051 status = mbedtls_cipher_values_from_psa(alg, attributes->core.type,
52 &key_bits, &mode, &cipher_id);
53 if (status != PSA_SUCCESS) {
54 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +010055 }
Ronald Cron46f91782021-03-17 08:16:34 +010056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Ronald Cron46f91782021-03-17 08:16:34 +010058#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +010059 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Paul Elliott07a30c42021-04-20 14:13:23 +010060 operation->alg = PSA_ALG_CCM;
Ronald Cron46f91782021-03-17 08:16:34 +010061 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
62 * The call to mbedtls_ccm_encrypt_and_tag or
63 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) {
65 return PSA_ERROR_INVALID_ARGUMENT;
66 }
Ronald Cron46f91782021-03-17 08:16:34 +010067
Gilles Peskine449bd832023-01-11 14:50:10 +010068 mbedtls_ccm_init(&operation->ctx.ccm);
Ronald Cron46f91782021-03-17 08:16:34 +010069 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010070 mbedtls_ccm_setkey(&operation->ctx.ccm, cipher_id,
71 key_buffer, (unsigned int) key_bits));
72 if (status != PSA_SUCCESS) {
73 return status;
74 }
Ronald Cron46f91782021-03-17 08:16:34 +010075 break;
76#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
77
78#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +010079 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Paul Elliott07a30c42021-04-20 14:13:23 +010080 operation->alg = PSA_ALG_GCM;
Ronald Cron46f91782021-03-17 08:16:34 +010081 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
82 * The call to mbedtls_gcm_crypt_and_tag or
83 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +010084 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) {
85 return PSA_ERROR_INVALID_ARGUMENT;
86 }
Ronald Cron46f91782021-03-17 08:16:34 +010087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_gcm_init(&operation->ctx.gcm);
Ronald Cron46f91782021-03-17 08:16:34 +010089 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_gcm_setkey(&operation->ctx.gcm, cipher_id,
91 key_buffer, (unsigned int) key_bits));
92 if (status != PSA_SUCCESS) {
93 return status;
94 }
Ronald Cron46f91782021-03-17 08:16:34 +010095 break;
96#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
97
98#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +010099 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Paul Elliott07a30c42021-04-20 14:13:23 +0100100 operation->alg = PSA_ALG_CHACHA20_POLY1305;
Ronald Cron46f91782021-03-17 08:16:34 +0100101 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if (alg != PSA_ALG_CHACHA20_POLY1305) {
103 return PSA_ERROR_NOT_SUPPORTED;
104 }
Ronald Cron46f91782021-03-17 08:16:34 +0100105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 mbedtls_chachapoly_init(&operation->ctx.chachapoly);
Ronald Cron46f91782021-03-17 08:16:34 +0100107 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 mbedtls_chachapoly_setkey(&operation->ctx.chachapoly,
109 key_buffer));
110 if (status != PSA_SUCCESS) {
111 return status;
112 }
Ronald Cron46f91782021-03-17 08:16:34 +0100113 break;
114#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
115
116 default:
Ronald Cron7a55deb2021-04-28 14:29:00 +0200117 (void) status;
118 (void) key_buffer;
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100120 }
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 operation->key_type = psa_get_key_type(attributes);
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 operation->tag_length = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Ronald Cron46f91782021-03-17 08:16:34 +0100125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 return PSA_SUCCESS;
Ronald Cron46f91782021-03-17 08:16:34 +0100127}
128
129psa_status_t mbedtls_psa_aead_encrypt(
130 const psa_key_attributes_t *attributes,
131 const uint8_t *key_buffer, size_t key_buffer_size,
132 psa_algorithm_t alg,
133 const uint8_t *nonce, size_t nonce_length,
134 const uint8_t *additional_data, size_t additional_data_length,
135 const uint8_t *plaintext, size_t plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length)
Ronald Cron46f91782021-03-17 08:16:34 +0100137{
138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100139 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100140 uint8_t *tag;
Ronald Cron46f91782021-03-17 08:16:34 +0100141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 status = psa_aead_setup(&operation, attributes, key_buffer,
143 key_buffer_size, alg);
Paul Elliottcc358592021-05-12 12:22:28 +0100144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 }
Ronald Cron46f91782021-03-17 08:16:34 +0100148
149 /* For all currently supported modes, the tag is at the end of the
150 * ciphertext. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (ciphertext_size < (plaintext_length + operation.tag_length)) {
Ronald Cron46f91782021-03-17 08:16:34 +0100152 status = PSA_ERROR_BUFFER_TOO_SMALL;
153 goto exit;
154 }
155 tag = ciphertext + plaintext_length;
156
Ronald Cron46f91782021-03-17 08:16:34 +0100157#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 if (operation.alg == PSA_ALG_CCM) {
Ronald Cron46f91782021-03-17 08:16:34 +0100159 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_ccm_encrypt_and_tag(&operation.ctx.ccm,
161 plaintext_length,
162 nonce, nonce_length,
163 additional_data,
164 additional_data_length,
165 plaintext, ciphertext,
166 tag, operation.tag_length));
167 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100168#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200169#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 if (operation.alg == PSA_ALG_GCM) {
Ronald Cron810eb162021-04-06 09:01:39 +0200171 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 mbedtls_gcm_crypt_and_tag(&operation.ctx.gcm,
173 MBEDTLS_GCM_ENCRYPT,
174 plaintext_length,
175 nonce, nonce_length,
176 additional_data, additional_data_length,
177 plaintext, ciphertext,
178 operation.tag_length, tag));
179 } else
Ronald Cron810eb162021-04-06 09:01:39 +0200180#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100181#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if (operation.alg == PSA_ALG_CHACHA20_POLY1305) {
183 if (operation.tag_length != 16) {
Ronald Cron46f91782021-03-17 08:16:34 +0100184 status = PSA_ERROR_NOT_SUPPORTED;
185 goto exit;
186 }
187 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 mbedtls_chachapoly_encrypt_and_tag(&operation.ctx.chachapoly,
189 plaintext_length,
190 nonce,
191 additional_data,
192 additional_data_length,
193 plaintext,
194 ciphertext,
195 tag));
196 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100197#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
198 {
199 (void) tag;
Ronald Cron7a55deb2021-04-28 14:29:00 +0200200 (void) nonce;
201 (void) nonce_length;
202 (void) additional_data;
203 (void) additional_data_length;
204 (void) plaintext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100206 }
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100209 *ciphertext_length = plaintext_length + operation.tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 }
Ronald Cron46f91782021-03-17 08:16:34 +0100211
212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 mbedtls_psa_aead_abort(&operation);
Ronald Cron46f91782021-03-17 08:16:34 +0100214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return status;
Ronald Cron46f91782021-03-17 08:16:34 +0100216}
217
218/* Locate the tag in a ciphertext buffer containing the encrypted data
219 * followed by the tag. Return the length of the part preceding the tag in
220 * *plaintext_length. This is the size of the plaintext in modes where
221 * the encrypted data has the same size as the plaintext, such as
222 * CCM and GCM. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223static psa_status_t psa_aead_unpadded_locate_tag(size_t tag_length,
224 const uint8_t *ciphertext,
225 size_t ciphertext_length,
226 size_t plaintext_size,
227 const uint8_t **p_tag)
Ronald Cron46f91782021-03-17 08:16:34 +0100228{
229 size_t payload_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (tag_length > ciphertext_length) {
231 return PSA_ERROR_INVALID_ARGUMENT;
232 }
Ronald Cron46f91782021-03-17 08:16:34 +0100233 payload_length = ciphertext_length - tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (payload_length > plaintext_size) {
235 return PSA_ERROR_BUFFER_TOO_SMALL;
236 }
Ronald Cron46f91782021-03-17 08:16:34 +0100237 *p_tag = ciphertext + payload_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return PSA_SUCCESS;
Ronald Cron46f91782021-03-17 08:16:34 +0100239}
240
241psa_status_t mbedtls_psa_aead_decrypt(
242 const psa_key_attributes_t *attributes,
243 const uint8_t *key_buffer, size_t key_buffer_size,
244 psa_algorithm_t alg,
245 const uint8_t *nonce, size_t nonce_length,
246 const uint8_t *additional_data, size_t additional_data_length,
247 const uint8_t *ciphertext, size_t ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length)
Ronald Cron46f91782021-03-17 08:16:34 +0100249{
250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100251 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100252 const uint8_t *tag = NULL;
Ronald Cron46f91782021-03-17 08:16:34 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 status = psa_aead_setup(&operation, attributes, key_buffer,
255 key_buffer_size, alg);
Paul Elliottcc358592021-05-12 12:22:28 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100258 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 }
Ronald Cron46f91782021-03-17 08:16:34 +0100260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 status = psa_aead_unpadded_locate_tag(operation.tag_length,
262 ciphertext, ciphertext_length,
263 plaintext_size, &tag);
264 if (status != PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100265 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 }
Ronald Cron46f91782021-03-17 08:16:34 +0100267
Ronald Cron46f91782021-03-17 08:16:34 +0100268#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (operation.alg == PSA_ALG_CCM) {
Ronald Cron46f91782021-03-17 08:16:34 +0100270 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 mbedtls_ccm_auth_decrypt(&operation.ctx.ccm,
272 ciphertext_length - operation.tag_length,
273 nonce, nonce_length,
274 additional_data,
275 additional_data_length,
276 ciphertext, plaintext,
277 tag, operation.tag_length));
278 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100279#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200280#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (operation.alg == PSA_ALG_GCM) {
Ronald Cron810eb162021-04-06 09:01:39 +0200282 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 mbedtls_gcm_auth_decrypt(&operation.ctx.gcm,
284 ciphertext_length - operation.tag_length,
285 nonce, nonce_length,
286 additional_data,
287 additional_data_length,
288 tag, operation.tag_length,
289 ciphertext, plaintext));
290 } else
Ronald Cron810eb162021-04-06 09:01:39 +0200291#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100292#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (operation.alg == PSA_ALG_CHACHA20_POLY1305) {
294 if (operation.tag_length != 16) {
Ronald Cron46f91782021-03-17 08:16:34 +0100295 status = PSA_ERROR_NOT_SUPPORTED;
296 goto exit;
297 }
298 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 mbedtls_chachapoly_auth_decrypt(&operation.ctx.chachapoly,
300 ciphertext_length - operation.tag_length,
301 nonce,
302 additional_data,
303 additional_data_length,
304 tag,
305 ciphertext,
306 plaintext));
307 } else
Ronald Cron46f91782021-03-17 08:16:34 +0100308#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
309 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200310 (void) nonce;
311 (void) nonce_length;
312 (void) additional_data;
313 (void) additional_data_length;
314 (void) plaintext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron46f91782021-03-17 08:16:34 +0100316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100319 *plaintext_length = ciphertext_length - operation.tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 }
Ronald Cron46f91782021-03-17 08:16:34 +0100321
322exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 mbedtls_psa_aead_abort(&operation);
Ronald Cron46f91782021-03-17 08:16:34 +0100324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (status == PSA_SUCCESS) {
Ronald Cron46f91782021-03-17 08:16:34 +0100326 *plaintext_length = ciphertext_length - operation.tag_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 }
328 return status;
Ronald Cron46f91782021-03-17 08:16:34 +0100329}
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100330
Paul Elliottadb8b162021-04-20 16:06:57 +0100331/* Set the key and algorithm for a multipart authenticated encryption
332 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100333psa_status_t mbedtls_psa_aead_encrypt_setup(
334 mbedtls_psa_aead_operation_t *operation,
335 const psa_key_attributes_t *attributes,
336 const uint8_t *key_buffer,
337 size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 psa_algorithm_t alg)
Paul Elliottadb8b162021-04-20 16:06:57 +0100339{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100340 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 status = psa_aead_setup(operation, attributes, key_buffer,
343 key_buffer_size, alg);
Paul Elliottadb8b162021-04-20 16:06:57 +0100344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (status == PSA_SUCCESS) {
Paul Elliottadb8b162021-04-20 16:06:57 +0100346 operation->is_encrypt = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100350}
351
352/* Set the key and algorithm for a multipart authenticated decryption
353 * operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100354psa_status_t mbedtls_psa_aead_decrypt_setup(
355 mbedtls_psa_aead_operation_t *operation,
356 const psa_key_attributes_t *attributes,
357 const uint8_t *key_buffer,
358 size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 psa_algorithm_t alg)
Paul Elliottadb8b162021-04-20 16:06:57 +0100360{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100361 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 status = psa_aead_setup(operation, attributes, key_buffer,
364 key_buffer_size, alg);
Paul Elliottadb8b162021-04-20 16:06:57 +0100365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (status == PSA_SUCCESS) {
Paul Elliottadb8b162021-04-20 16:06:57 +0100367 operation->is_encrypt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100371}
372
Paul Elliottadb8b162021-04-20 16:06:57 +0100373/* Set a nonce for the multipart AEAD operation*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100374psa_status_t mbedtls_psa_aead_set_nonce(
375 mbedtls_psa_aead_operation_t *operation,
376 const uint8_t *nonce,
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 size_t nonce_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100378{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100380
Paul Elliottbc949782021-06-03 15:29:00 +0100381#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100383 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 mbedtls_gcm_starts(&operation->ctx.gcm,
385 operation->is_encrypt ?
386 MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
387 nonce,
388 nonce_length));
389 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100390#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100391#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +0100393 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 mbedtls_ccm_starts(&operation->ctx.ccm,
395 operation->is_encrypt ?
396 MBEDTLS_CCM_ENCRYPT : MBEDTLS_CCM_DECRYPT,
397 nonce,
398 nonce_length));
399 } else
Paul Elliotte193ea82021-10-01 13:00:16 +0100400#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100401#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
Paul Elliott946c9202021-09-28 14:32:55 +0100403 /* Note - ChaChaPoly allows an 8 byte nonce, but we would have to
404 * allocate a buffer in the operation, copy the nonce to it and pad
405 * it, so for now check the nonce is 12 bytes, as
406 * mbedtls_chachapoly_starts() assumes it can read 12 bytes from the
407 * passed in buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (nonce_length != 12) {
409 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliott946c9202021-09-28 14:32:55 +0100410 }
411
Paul Elliott2df40052021-05-07 17:52:18 +0100412 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 mbedtls_chachapoly_starts(&operation->ctx.chachapoly,
Paul Elliott2df40052021-05-07 17:52:18 +0100414 nonce,
415 operation->is_encrypt ?
416 MBEDTLS_CHACHAPOLY_ENCRYPT :
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 MBEDTLS_CHACHAPOLY_DECRYPT));
418 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100419#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
420 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 (void) operation;
422 (void) nonce;
423 (void) nonce_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100426 }
427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100429}
Paul Elliottefda3402021-08-25 17:16:52 +0100430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431/* Declare the lengths of the message and additional data for AEAD. */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100432psa_status_t mbedtls_psa_aead_set_lengths(
433 mbedtls_psa_aead_operation_t *operation,
434 size_t ad_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 size_t plaintext_length)
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100436{
Paul Elliotte193ea82021-10-01 13:00:16 +0100437#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (operation->alg == PSA_ALG_CCM) {
439 return mbedtls_to_psa_error(
440 mbedtls_ccm_set_lengths(&operation->ctx.ccm,
441 ad_length,
442 plaintext_length,
443 operation->tag_length));
Paul Elliotte193ea82021-10-01 13:00:16 +0100444
445 }
446#else /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 (void) operation;
448 (void) ad_length;
449 (void) plaintext_length;
Paul Elliotte193ea82021-10-01 13:00:16 +0100450#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return PSA_SUCCESS;
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100453}
454
Paul Elliottadb8b162021-04-20 16:06:57 +0100455/* Pass additional data to an active multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100456psa_status_t mbedtls_psa_aead_update_ad(
457 mbedtls_psa_aead_operation_t *operation,
458 const uint8_t *input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 size_t input_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100460{
461 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
462
Paul Elliottadb8b162021-04-20 16:06:57 +0100463#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott2df40052021-05-07 17:52:18 +0100465 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 mbedtls_gcm_update_ad(&operation->ctx.gcm, input, input_length));
467 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100468#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100469#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +0100471 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 mbedtls_ccm_update_ad(&operation->ctx.ccm, input, input_length));
473 } else
Paul Elliotte193ea82021-10-01 13:00:16 +0100474#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100475#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
Paul Elliott2df40052021-05-07 17:52:18 +0100477 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 mbedtls_chachapoly_update_aad(&operation->ctx.chachapoly,
Paul Elliott2df40052021-05-07 17:52:18 +0100479 input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 input_length));
481 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100482#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
483 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 (void) operation;
485 (void) input;
486 (void) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100489 }
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100492}
493
494/* Encrypt or decrypt a message fragment in an active multipart AEAD
495 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100496psa_status_t mbedtls_psa_aead_update(
497 mbedtls_psa_aead_operation_t *operation,
498 const uint8_t *input,
499 size_t input_length,
500 uint8_t *output,
501 size_t output_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 size_t *output_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100503{
Paul Elliotte2c788d2021-05-13 17:16:01 +0100504 size_t update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100505 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
506
Paul Elliotte2c788d2021-05-13 17:16:01 +0100507 update_output_length = input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100508
Paul Elliottadb8b162021-04-20 16:06:57 +0100509#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100511 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 mbedtls_gcm_update(&operation->ctx.gcm,
Paul Elliotte193ea82021-10-01 13:00:16 +0100513 input, input_length,
514 output, output_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 &update_output_length));
516 } else
517#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
518#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
519 if (operation->alg == PSA_ALG_CCM) {
520 if (output_size < input_length) {
521 return PSA_ERROR_BUFFER_TOO_SMALL;
522 }
Paul Elliottecce9012021-07-23 15:44:11 +0100523
Paul Elliott2df40052021-05-07 17:52:18 +0100524 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 mbedtls_ccm_update(&operation->ctx.ccm,
526 input, input_length,
527 output, output_size,
528 &update_output_length));
529 } else
530#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
531#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
532 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
533 if (output_size < input_length) {
534 return PSA_ERROR_BUFFER_TOO_SMALL;
535 }
536
537 status = mbedtls_to_psa_error(
538 mbedtls_chachapoly_update(&operation->ctx.chachapoly,
Paul Elliott2df40052021-05-07 17:52:18 +0100539 input_length,
540 input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 output));
542 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100543#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
544 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 (void) operation;
546 (void) input;
547 (void) output;
548 (void) output_size;
Paul Elliottadb8b162021-04-20 16:06:57 +0100549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100551 }
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (status == PSA_SUCCESS) {
Paul Elliotte2c788d2021-05-13 17:16:01 +0100554 *output_length = update_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100558}
559
Paul Elliottadb8b162021-04-20 16:06:57 +0100560/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100561psa_status_t mbedtls_psa_aead_finish(
562 mbedtls_psa_aead_operation_t *operation,
563 uint8_t *ciphertext,
564 size_t ciphertext_size,
565 size_t *ciphertext_length,
566 uint8_t *tag,
567 size_t tag_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 size_t *tag_length)
Paul Elliottadb8b162021-04-20 16:06:57 +0100569{
570 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100571 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if (tag_size < operation->tag_length) {
574 return PSA_ERROR_BUFFER_TOO_SMALL;
575 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100576
577#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (operation->alg == PSA_ALG_GCM) {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100579 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 mbedtls_gcm_finish(&operation->ctx.gcm,
581 ciphertext, ciphertext_size, ciphertext_length,
582 tag, operation->tag_length));
583 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100584#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100585#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +0100587 /* tag must be big enough to store a tag of size passed into set
588 * lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (tag_size < operation->tag_length) {
590 return PSA_ERROR_BUFFER_TOO_SMALL;
591 }
Paul Elliotte193ea82021-10-01 13:00:16 +0100592
593 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 mbedtls_ccm_finish(&operation->ctx.ccm,
595 tag, operation->tag_length));
596 } else
Paul Elliotte193ea82021-10-01 13:00:16 +0100597#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100598#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if (operation->alg == PSA_ALG_CHACHA20_POLY1305) {
Paul Elliotted08cf82021-07-22 18:48:24 +0100600 /* Belt and braces. Although the above tag_size check should have
601 * already done this, if we later start supporting smaller tag sizes
602 * for chachapoly, then passing a tag buffer smaller than 16 into here
603 * could cause a buffer overflow, so better safe than sorry. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (tag_size < 16) {
605 return PSA_ERROR_BUFFER_TOO_SMALL;
606 }
Paul Elliotted08cf82021-07-22 18:48:24 +0100607
Paul Elliott2df40052021-05-07 17:52:18 +0100608 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 mbedtls_chachapoly_finish(&operation->ctx.chachapoly,
610 tag));
611 } else
Paul Elliottadb8b162021-04-20 16:06:57 +0100612#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
613 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 (void) ciphertext;
615 (void) ciphertext_size;
616 (void) ciphertext_length;
617 (void) tag;
618 (void) tag_size;
619 (void) tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100622 }
623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (status == PSA_SUCCESS) {
Paul Elliott8ff74212021-09-19 18:39:23 +0100625 /* This will be zero for all supported algorithms currently, but left
626 * here for future support. */
Paul Elliottadb8b162021-04-20 16:06:57 +0100627 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100628 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100629 }
630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 return status;
Paul Elliottadb8b162021-04-20 16:06:57 +0100632}
633
Paul Elliottadb8b162021-04-20 16:06:57 +0100634/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100635psa_status_t mbedtls_psa_aead_abort(
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 mbedtls_psa_aead_operation_t *operation)
Paul Elliottadb8b162021-04-20 16:06:57 +0100637{
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 switch (operation->alg) {
Paul Elliott811d8d42021-04-22 11:31:14 +0100639#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
640 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 mbedtls_ccm_free(&operation->ctx.ccm);
Paul Elliottadb8b162021-04-20 16:06:57 +0100642 break;
643#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
644#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
645 case PSA_ALG_GCM:
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 mbedtls_gcm_free(&operation->ctx.gcm);
Paul Elliottadb8b162021-04-20 16:06:57 +0100647 break;
648#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
649#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100650 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 mbedtls_chachapoly_free(&operation->ctx.chachapoly);
Paul Elliott811d8d42021-04-22 11:31:14 +0100652 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100653#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
654 }
655
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100656 operation->is_encrypt = 0;
Paul Elliott1a98aca2021-05-20 18:24:07 +0100657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 return PSA_SUCCESS;
Paul Elliottadb8b162021-04-20 16:06:57 +0100659}
660
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100661#endif /* MBEDTLS_PSA_CRYPTO_C */