blob: 730ef9555c2e97080d346fcb91b1751ba89f15f5 [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,
Ronald Cron46f91782021-03-17 08:16:34 +010043 psa_algorithm_t alg )
44{
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
Paul Elliottcc358592021-05-12 12:22:28 +010050 ( void ) key_buffer_size;
51
Ronald Cron46f91782021-03-17 08:16:34 +010052 key_bits = attributes->core.bits;
53
Ronald Cronecbc0682021-03-26 13:25:17 +010054 cipher_info = mbedtls_cipher_info_from_psa( alg,
55 attributes->core.type, key_bits,
56 &cipher_id );
57 if( cipher_info == NULL )
Ronald Cron46f91782021-03-17 08:16:34 +010058 return( PSA_ERROR_NOT_SUPPORTED );
59
60 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
61 {
62#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
63 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. */
68 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( attributes->core.type ) != 16 )
69 return( PSA_ERROR_INVALID_ARGUMENT );
70
71 mbedtls_ccm_init( &operation->ctx.ccm );
72 status = mbedtls_to_psa_error(
73 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
74 key_buffer, (unsigned int) key_bits ) );
75 if( status != PSA_SUCCESS )
76 return( status );
77 break;
78#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
79
80#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
81 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Paul Elliott07a30c42021-04-20 14:13:23 +010082 operation->alg = PSA_ALG_GCM;
Ronald Cron46f91782021-03-17 08:16:34 +010083 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
84 * The call to mbedtls_gcm_crypt_and_tag or
85 * mbedtls_gcm_auth_decrypt will validate the tag length. */
86 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( attributes->core.type ) != 16 )
87 return( PSA_ERROR_INVALID_ARGUMENT );
88
89 mbedtls_gcm_init( &operation->ctx.gcm );
90 status = mbedtls_to_psa_error(
91 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
92 key_buffer, (unsigned int) key_bits ) );
93 if( status != PSA_SUCCESS )
94 return( status );
95 break;
96#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
97
98#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
99 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. */
102 if( alg != PSA_ALG_CHACHA20_POLY1305 )
103 return( PSA_ERROR_NOT_SUPPORTED );
104
105 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
106 status = mbedtls_to_psa_error(
107 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
108 key_buffer ) );
109 if( status != PSA_SUCCESS )
110 return( status );
111 break;
112#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
113
114 default:
Ronald Cron7a55deb2021-04-28 14:29:00 +0200115 (void) status;
116 (void) key_buffer;
Ronald Cron46f91782021-03-17 08:16:34 +0100117 return( PSA_ERROR_NOT_SUPPORTED );
118 }
119
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100120 operation->key_type = psa_get_key_type( attributes );
121
Przemek Stekiel88ade842022-10-08 17:56:18 +0200122 operation->tag_length = PSA_ALG_AEAD_GET_TAG_LENGTH( alg );
Ronald Cron46f91782021-03-17 08:16:34 +0100123
124 return( PSA_SUCCESS );
125}
126
127psa_status_t mbedtls_psa_aead_encrypt(
128 const psa_key_attributes_t *attributes,
129 const uint8_t *key_buffer, size_t key_buffer_size,
130 psa_algorithm_t alg,
131 const uint8_t *nonce, size_t nonce_length,
132 const uint8_t *additional_data, size_t additional_data_length,
133 const uint8_t *plaintext, size_t plaintext_length,
134 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
135{
136 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100137 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100138 uint8_t *tag;
Ronald Cron46f91782021-03-17 08:16:34 +0100139
Paul Elliottcc358592021-05-12 12:22:28 +0100140 status = psa_aead_setup( &operation, attributes, key_buffer,
141 key_buffer_size, alg );
142
Ronald Cron46f91782021-03-17 08:16:34 +0100143 if( status != PSA_SUCCESS )
144 goto exit;
145
146 /* For all currently supported modes, the tag is at the end of the
147 * ciphertext. */
148 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
149 {
150 status = PSA_ERROR_BUFFER_TOO_SMALL;
151 goto exit;
152 }
153 tag = ciphertext + plaintext_length;
154
Ronald Cron46f91782021-03-17 08:16:34 +0100155#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100156 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100157 {
158 status = mbedtls_to_psa_error(
159 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
160 plaintext_length,
161 nonce, nonce_length,
162 additional_data,
163 additional_data_length,
164 plaintext, ciphertext,
165 tag, operation.tag_length ) );
166 }
167 else
168#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200169#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100170 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200171 {
172 status = mbedtls_to_psa_error(
173 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
174 MBEDTLS_GCM_ENCRYPT,
175 plaintext_length,
176 nonce, nonce_length,
177 additional_data, additional_data_length,
178 plaintext, ciphertext,
179 operation.tag_length, tag ) );
180 }
181 else
182#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100183#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100184 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100185 {
Paul Elliott96b01732021-07-16 17:00:26 +0100186 if( operation.tag_length != 16 )
Ronald Cron46f91782021-03-17 08:16:34 +0100187 {
188 status = PSA_ERROR_NOT_SUPPORTED;
189 goto exit;
190 }
191 status = mbedtls_to_psa_error(
192 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 }
201 else
202#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;
Ronald Cron46f91782021-03-17 08:16:34 +0100210 return( PSA_ERROR_NOT_SUPPORTED );
211 }
212
213 if( status == PSA_SUCCESS )
214 *ciphertext_length = plaintext_length + operation.tag_length;
215
216exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100217 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100218
219 return( status );
220}
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. */
227static 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 )
232{
233 size_t payload_length;
234 if( tag_length > ciphertext_length )
235 return( PSA_ERROR_INVALID_ARGUMENT );
236 payload_length = ciphertext_length - tag_length;
237 if( payload_length > plaintext_size )
238 return( PSA_ERROR_BUFFER_TOO_SMALL );
239 *p_tag = ciphertext + payload_length;
240 return( PSA_SUCCESS );
241}
242
243psa_status_t mbedtls_psa_aead_decrypt(
244 const psa_key_attributes_t *attributes,
245 const uint8_t *key_buffer, size_t key_buffer_size,
246 psa_algorithm_t alg,
247 const uint8_t *nonce, size_t nonce_length,
248 const uint8_t *additional_data, size_t additional_data_length,
249 const uint8_t *ciphertext, size_t ciphertext_length,
250 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
251{
252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100253 mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT;
Ronald Cron46f91782021-03-17 08:16:34 +0100254 const uint8_t *tag = NULL;
Ronald Cron46f91782021-03-17 08:16:34 +0100255
Paul Elliottcc358592021-05-12 12:22:28 +0100256 status = psa_aead_setup( &operation, attributes, key_buffer,
257 key_buffer_size, alg );
258
Ronald Cron46f91782021-03-17 08:16:34 +0100259 if( status != PSA_SUCCESS )
260 goto exit;
261
262 status = psa_aead_unpadded_locate_tag( operation.tag_length,
263 ciphertext, ciphertext_length,
264 plaintext_size, &tag );
265 if( status != PSA_SUCCESS )
266 goto exit;
267
Ronald Cron46f91782021-03-17 08:16:34 +0100268#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100269 if( operation.alg == PSA_ALG_CCM )
Ronald Cron46f91782021-03-17 08:16:34 +0100270 {
271 status = mbedtls_to_psa_error(
272 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
273 ciphertext_length - operation.tag_length,
274 nonce, nonce_length,
275 additional_data,
276 additional_data_length,
277 ciphertext, plaintext,
278 tag, operation.tag_length ) );
279 }
280 else
281#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Ronald Cron810eb162021-04-06 09:01:39 +0200282#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliott07a30c42021-04-20 14:13:23 +0100283 if( operation.alg == PSA_ALG_GCM )
Ronald Cron810eb162021-04-06 09:01:39 +0200284 {
285 status = mbedtls_to_psa_error(
286 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
287 ciphertext_length - operation.tag_length,
288 nonce, nonce_length,
289 additional_data,
290 additional_data_length,
291 tag, operation.tag_length,
292 ciphertext, plaintext ) );
293 }
294 else
295#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Ronald Cron46f91782021-03-17 08:16:34 +0100296#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott07a30c42021-04-20 14:13:23 +0100297 if( operation.alg == PSA_ALG_CHACHA20_POLY1305 )
Ronald Cron46f91782021-03-17 08:16:34 +0100298 {
Paul Elliottcf2d66e2021-06-23 18:49:56 +0100299 if( operation.tag_length != 16 )
Ronald Cron46f91782021-03-17 08:16:34 +0100300 {
301 status = PSA_ERROR_NOT_SUPPORTED;
302 goto exit;
303 }
304 status = mbedtls_to_psa_error(
305 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
306 ciphertext_length - operation.tag_length,
307 nonce,
308 additional_data,
309 additional_data_length,
310 tag,
311 ciphertext,
312 plaintext ) );
313 }
314 else
315#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
316 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200317 (void) nonce;
318 (void) nonce_length;
319 (void) additional_data;
320 (void) additional_data_length;
321 (void) plaintext;
Ronald Cron46f91782021-03-17 08:16:34 +0100322 return( PSA_ERROR_NOT_SUPPORTED );
323 }
324
325 if( status == PSA_SUCCESS )
326 *plaintext_length = ciphertext_length - operation.tag_length;
327
328exit:
Paul Elliottadb8b162021-04-20 16:06:57 +0100329 mbedtls_psa_aead_abort( &operation );
Ronald Cron46f91782021-03-17 08:16:34 +0100330
331 if( status == PSA_SUCCESS )
332 *plaintext_length = ciphertext_length - operation.tag_length;
333 return( status );
334}
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,
343 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
Paul Elliottcc358592021-05-12 12:22:28 +0100347 status = psa_aead_setup( operation, attributes, key_buffer,
348 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100349
350 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100351 operation->is_encrypt = 1;
Paul Elliottadb8b162021-04-20 16:06:57 +0100352
353 return ( status );
354}
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,
363 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
Paul Elliottcc358592021-05-12 12:22:28 +0100367 status = psa_aead_setup( operation, attributes, key_buffer,
368 key_buffer_size, alg );
Paul Elliottadb8b162021-04-20 16:06:57 +0100369
370 if( status == PSA_SUCCESS )
Paul Elliottadb8b162021-04-20 16:06:57 +0100371 operation->is_encrypt = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100372
373 return ( status );
374}
375
Paul Elliottadb8b162021-04-20 16:06:57 +0100376/* Set a nonce for the multipart AEAD operation*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100377psa_status_t mbedtls_psa_aead_set_nonce(
378 mbedtls_psa_aead_operation_t *operation,
379 const uint8_t *nonce,
380 size_t nonce_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100381{
Paul Elliott9e8ccd72021-05-13 14:30:53 +0100382 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottadb8b162021-04-20 16:06:57 +0100383
Paul Elliottbc949782021-06-03 15:29:00 +0100384#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
Paul Elliottadb8b162021-04-20 16:06:57 +0100385 if( operation->alg == PSA_ALG_GCM )
386 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100387 status = mbedtls_to_psa_error(
388 mbedtls_gcm_starts( &operation->ctx.gcm,
389 operation->is_encrypt ?
390 MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
391 nonce,
392 nonce_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100393 }
394 else
395#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100396#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
397 if( operation->alg == PSA_ALG_CCM )
398 {
399 status = mbedtls_to_psa_error(
400 mbedtls_ccm_starts( &operation->ctx.ccm,
401 operation->is_encrypt ?
402 MBEDTLS_CCM_ENCRYPT : MBEDTLS_CCM_DECRYPT,
403 nonce,
404 nonce_length ) );
405 }
406 else
407#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100408#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
409 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
410 {
Paul Elliott946c9202021-09-28 14:32:55 +0100411 /* Note - ChaChaPoly allows an 8 byte nonce, but we would have to
412 * allocate a buffer in the operation, copy the nonce to it and pad
413 * it, so for now check the nonce is 12 bytes, as
414 * mbedtls_chachapoly_starts() assumes it can read 12 bytes from the
415 * passed in buffer. */
416 if( nonce_length != 12 )
417 {
418 return( PSA_ERROR_INVALID_ARGUMENT );
419 }
420
Paul Elliott2df40052021-05-07 17:52:18 +0100421 status = mbedtls_to_psa_error(
422 mbedtls_chachapoly_starts( &operation->ctx.chachapoly,
423 nonce,
424 operation->is_encrypt ?
425 MBEDTLS_CHACHAPOLY_ENCRYPT :
426 MBEDTLS_CHACHAPOLY_DECRYPT ) );
Paul Elliottefda3402021-08-25 17:16:52 +0100427 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100428 else
429#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
430 {
Ronald Cron17006702021-12-03 15:25:24 +0100431 ( void ) operation;
Paul Elliottadb8b162021-04-20 16:06:57 +0100432 ( void ) nonce;
Ronald Cron17006702021-12-03 15:25:24 +0100433 ( void ) nonce_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100434
435 return ( PSA_ERROR_NOT_SUPPORTED );
436 }
437
Paul Elliottadb8b162021-04-20 16:06:57 +0100438 return( status );
439}
Paul Elliottefda3402021-08-25 17:16:52 +0100440
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100441 /* Declare the lengths of the message and additional data for AEAD. */
442psa_status_t mbedtls_psa_aead_set_lengths(
443 mbedtls_psa_aead_operation_t *operation,
444 size_t ad_length,
445 size_t plaintext_length )
446{
Paul Elliotte193ea82021-10-01 13:00:16 +0100447#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
448 if( operation->alg == PSA_ALG_CCM )
449 {
450 return( mbedtls_to_psa_error(
451 mbedtls_ccm_set_lengths( &operation->ctx.ccm,
452 ad_length,
453 plaintext_length,
454 operation->tag_length ) ) );
455
456 }
457#else /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100458 ( void ) operation;
459 ( void ) ad_length;
460 ( void ) plaintext_length;
Paul Elliotte193ea82021-10-01 13:00:16 +0100461#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100462
Paul Elliottdff6c5d2021-09-28 11:00:20 +0100463 return ( PSA_SUCCESS );
464}
465
Paul Elliottadb8b162021-04-20 16:06:57 +0100466/* Pass additional data to an active multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100467psa_status_t mbedtls_psa_aead_update_ad(
468 mbedtls_psa_aead_operation_t *operation,
469 const uint8_t *input,
470 size_t input_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100471{
472 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
473
Paul Elliottadb8b162021-04-20 16:06:57 +0100474#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
475 if( operation->alg == PSA_ALG_GCM )
476 {
Paul Elliott2df40052021-05-07 17:52:18 +0100477 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100478 mbedtls_gcm_update_ad( &operation->ctx.gcm, input, input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100479 }
480 else
481#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100482#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
483 if( operation->alg == PSA_ALG_CCM )
484 {
485 status = mbedtls_to_psa_error(
486 mbedtls_ccm_update_ad( &operation->ctx.ccm, input, input_length ) );
487 }
488 else
489#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100490#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
491 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
492 {
Paul Elliott2df40052021-05-07 17:52:18 +0100493 status = mbedtls_to_psa_error(
494 mbedtls_chachapoly_update_aad( &operation->ctx.chachapoly,
495 input,
496 input_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100497 }
498 else
499#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
500 {
Paul Elliottbc949782021-06-03 15:29:00 +0100501 ( void ) operation;
502 ( void ) input;
503 ( void ) input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100504
505 return ( PSA_ERROR_NOT_SUPPORTED );
506 }
507
Paul Elliottadb8b162021-04-20 16:06:57 +0100508 return ( status );
509}
510
511/* Encrypt or decrypt a message fragment in an active multipart AEAD
512 * operation.*/
Paul Elliottbb8bf662021-05-19 17:29:42 +0100513psa_status_t mbedtls_psa_aead_update(
514 mbedtls_psa_aead_operation_t *operation,
515 const uint8_t *input,
516 size_t input_length,
517 uint8_t *output,
518 size_t output_size,
519 size_t *output_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100520{
Paul Elliotte2c788d2021-05-13 17:16:01 +0100521 size_t update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
523
Paul Elliotte2c788d2021-05-13 17:16:01 +0100524 update_output_length = input_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100525
Paul Elliottadb8b162021-04-20 16:06:57 +0100526#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
527 if( operation->alg == PSA_ALG_GCM )
528 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100529 status = mbedtls_to_psa_error(
530 mbedtls_gcm_update( &operation->ctx.gcm,
531 input, input_length,
532 output, output_size,
533 &update_output_length ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100534 }
535 else
536#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100537#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
538 if( operation->alg == PSA_ALG_CCM )
539 {
540 if( output_size < input_length )
541 return( PSA_ERROR_BUFFER_TOO_SMALL );
542
543 status = mbedtls_to_psa_error(
544 mbedtls_ccm_update( &operation->ctx.ccm,
545 input, input_length,
546 output, output_size,
547 &update_output_length ) );
548 }
549 else
550#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100551#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
552 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
553 {
Paul Elliottecce9012021-07-23 15:44:11 +0100554 if( output_size < input_length )
555 return( PSA_ERROR_BUFFER_TOO_SMALL );
556
Paul Elliott2df40052021-05-07 17:52:18 +0100557 status = mbedtls_to_psa_error(
558 mbedtls_chachapoly_update( &operation->ctx.chachapoly,
559 input_length,
560 input,
561 output ) );
Paul Elliottadb8b162021-04-20 16:06:57 +0100562 }
563 else
564#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
565 {
Ronald Cron17006702021-12-03 15:25:24 +0100566 ( void ) operation;
Paul Elliottbc949782021-06-03 15:29:00 +0100567 ( void ) input;
Ronald Cron17006702021-12-03 15:25:24 +0100568 ( void ) output;
569 ( void ) output_size;
Paul Elliottadb8b162021-04-20 16:06:57 +0100570
571 return ( PSA_ERROR_NOT_SUPPORTED );
572 }
573
574 if( status == PSA_SUCCESS )
Paul Elliotte2c788d2021-05-13 17:16:01 +0100575 *output_length = update_output_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100576
577 return( status );
578}
579
Paul Elliottadb8b162021-04-20 16:06:57 +0100580/* Finish encrypting a message in a multipart AEAD operation. */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100581psa_status_t mbedtls_psa_aead_finish(
582 mbedtls_psa_aead_operation_t *operation,
583 uint8_t *ciphertext,
584 size_t ciphertext_size,
585 size_t *ciphertext_length,
586 uint8_t *tag,
587 size_t tag_size,
588 size_t *tag_length )
Paul Elliottadb8b162021-04-20 16:06:57 +0100589{
590 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100591 size_t finish_output_size = 0;
Paul Elliottadb8b162021-04-20 16:06:57 +0100592
Paul Elliott315628d2021-07-20 18:25:54 +0100593 if( tag_size < operation->tag_length )
594 return( PSA_ERROR_BUFFER_TOO_SMALL );
Paul Elliottadb8b162021-04-20 16:06:57 +0100595
596#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
597 if( operation->alg == PSA_ALG_GCM )
Paul Elliottecce9012021-07-23 15:44:11 +0100598 {
Paul Elliott83f09ef2021-05-21 19:28:26 +0100599 status = mbedtls_to_psa_error(
600 mbedtls_gcm_finish( &operation->ctx.gcm,
Paul Elliott71b05672021-09-24 11:18:13 +0100601 ciphertext, ciphertext_size, ciphertext_length,
Paul Elliott2fe5db82021-07-22 18:10:43 +0100602 tag, operation->tag_length ) );
Paul Elliottecce9012021-07-23 15:44:11 +0100603 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100604 else
605#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
Paul Elliotte193ea82021-10-01 13:00:16 +0100606#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
607 if( operation->alg == PSA_ALG_CCM )
608 {
609 /* tag must be big enough to store a tag of size passed into set
610 * lengths. */
611 if( tag_size < operation->tag_length )
612 return( PSA_ERROR_BUFFER_TOO_SMALL );
613
614 status = mbedtls_to_psa_error(
615 mbedtls_ccm_finish( &operation->ctx.ccm,
616 tag, operation->tag_length ) );
617 }
618 else
619#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
Paul Elliottadb8b162021-04-20 16:06:57 +0100620#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
621 if( operation->alg == PSA_ALG_CHACHA20_POLY1305 )
Paul Elliotted08cf82021-07-22 18:48:24 +0100622 {
623 /* Belt and braces. Although the above tag_size check should have
624 * already done this, if we later start supporting smaller tag sizes
625 * for chachapoly, then passing a tag buffer smaller than 16 into here
626 * could cause a buffer overflow, so better safe than sorry. */
627 if( tag_size < 16 )
628 return( PSA_ERROR_BUFFER_TOO_SMALL );
629
Paul Elliott2df40052021-05-07 17:52:18 +0100630 status = mbedtls_to_psa_error(
Paul Elliott83f09ef2021-05-21 19:28:26 +0100631 mbedtls_chachapoly_finish( &operation->ctx.chachapoly,
632 tag ) );
Paul Elliotted08cf82021-07-22 18:48:24 +0100633 }
Paul Elliottadb8b162021-04-20 16:06:57 +0100634 else
635#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
636 {
637 ( void ) ciphertext;
638 ( void ) ciphertext_size;
639 ( void ) ciphertext_length;
640 ( void ) tag;
641 ( void ) tag_size;
642 ( void ) tag_length;
643
644 return ( PSA_ERROR_NOT_SUPPORTED );
645 }
646
647 if( status == PSA_SUCCESS )
648 {
Paul Elliott8ff74212021-09-19 18:39:23 +0100649 /* This will be zero for all supported algorithms currently, but left
650 * here for future support. */
Paul Elliottadb8b162021-04-20 16:06:57 +0100651 *ciphertext_length = finish_output_size;
Paul Elliottfd3ca242021-04-25 18:10:42 +0100652 *tag_length = operation->tag_length;
Paul Elliottadb8b162021-04-20 16:06:57 +0100653 }
654
Paul Elliottadb8b162021-04-20 16:06:57 +0100655 return ( status );
656}
657
Paul Elliottadb8b162021-04-20 16:06:57 +0100658/* Abort an AEAD operation */
Paul Elliottbb8bf662021-05-19 17:29:42 +0100659psa_status_t mbedtls_psa_aead_abort(
660 mbedtls_psa_aead_operation_t *operation )
Paul Elliottadb8b162021-04-20 16:06:57 +0100661{
Paul Elliott811d8d42021-04-22 11:31:14 +0100662 switch( operation->alg )
Paul Elliottadb8b162021-04-20 16:06:57 +0100663 {
Paul Elliott811d8d42021-04-22 11:31:14 +0100664#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
665 case PSA_ALG_CCM:
Paul Elliottadb8b162021-04-20 16:06:57 +0100666 mbedtls_ccm_free( &operation->ctx.ccm );
667 break;
668#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
669#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
670 case PSA_ALG_GCM:
671 mbedtls_gcm_free( &operation->ctx.gcm );
672 break;
673#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
674#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
Paul Elliott811d8d42021-04-22 11:31:14 +0100675 case PSA_ALG_CHACHA20_POLY1305:
676 mbedtls_chachapoly_free( &operation->ctx.chachapoly );
677 break;
Paul Elliottadb8b162021-04-20 16:06:57 +0100678#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
679 }
680
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100681 operation->is_encrypt = 0;
Paul Elliott1a98aca2021-05-20 18:24:07 +0100682
Paul Elliottadb8b162021-04-20 16:06:57 +0100683 return( PSA_SUCCESS );
684}
685
Ronald Cron7ceee8d2021-03-17 16:55:43 +0100686#endif /* MBEDTLS_PSA_CRYPTO_C */
687