blob: 1500bd6da7e55090678bc66ca884840fd672d62d [file] [log] [blame]
Ronald Cron0ff57952021-03-08 16:46:35 +01001/*
2 * PSA cipher driver 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_cipher.h>
Ronald Crond6d28882020-12-14 14:56:02 +010026#include "psa_crypto_core.h"
Ronald Cron6d051732020-10-01 14:10:20 +020027#include "psa_crypto_random_impl.h"
28
Ronald Crond6d28882020-12-14 14:56:02 +010029#include "mbedtls/cipher.h"
Ronald Cron6d051732020-10-01 14:10:20 +020030#include "mbedtls/error.h"
Ronald Cron0ff57952021-03-08 16:46:35 +010031
Ronald Crond6d28882020-12-14 14:56:02 +010032#include <string.h>
33
Ronald Cron5d9b00d2021-03-10 14:43:20 +010034#if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) || \
35 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
36 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_DES) ) )
37#define BUILTIN_KEY_TYPE_DES 1
38#endif
39
40#if ( defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
41 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
42 defined(MBEDTLS_PSA_ACCEL_ALG_CBC_NO_PADDING) ) )
43#define BUILTIN_ALG_CBC_NO_PADDING 1
44#endif
45
46#if ( defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) || \
47 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
48 defined(MBEDTLS_PSA_ACCEL_ALG_CBC_PKCS7) ) )
49#define BUILTIN_ALG_CBC_PKCS7 1
50#endif
51
52#if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) || \
53 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
54 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_CHACHA20) ) )
55#define BUILTIN_KEY_TYPE_CHACHA20 1
56#endif
57
Ronald Cron75e6ae22021-03-17 14:46:05 +010058const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
59 psa_algorithm_t alg,
60 psa_key_type_t key_type,
61 size_t key_bits,
62 mbedtls_cipher_id_t* cipher_id )
63{
64 mbedtls_cipher_mode_t mode;
65 mbedtls_cipher_id_t cipher_id_tmp;
66
67 if( PSA_ALG_IS_AEAD( alg ) )
68 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
69
70 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
71 {
72 switch( alg )
73 {
74 case PSA_ALG_STREAM_CIPHER:
75 mode = MBEDTLS_MODE_STREAM;
76 break;
77 case PSA_ALG_CTR:
78 mode = MBEDTLS_MODE_CTR;
79 break;
80 case PSA_ALG_CFB:
81 mode = MBEDTLS_MODE_CFB;
82 break;
83 case PSA_ALG_OFB:
84 mode = MBEDTLS_MODE_OFB;
85 break;
86 case PSA_ALG_ECB_NO_PADDING:
87 mode = MBEDTLS_MODE_ECB;
88 break;
89 case PSA_ALG_CBC_NO_PADDING:
90 mode = MBEDTLS_MODE_CBC;
91 break;
92 case PSA_ALG_CBC_PKCS7:
93 mode = MBEDTLS_MODE_CBC;
94 break;
95 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
96 mode = MBEDTLS_MODE_CCM;
97 break;
98 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
99 mode = MBEDTLS_MODE_GCM;
100 break;
101 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
102 mode = MBEDTLS_MODE_CHACHAPOLY;
103 break;
104 default:
105 return( NULL );
106 }
107 }
108 else if( alg == PSA_ALG_CMAC )
109 mode = MBEDTLS_MODE_ECB;
110 else
111 return( NULL );
112
113 switch( key_type )
114 {
115 case PSA_KEY_TYPE_AES:
116 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
117 break;
118 case PSA_KEY_TYPE_DES:
119 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
120 * and 192 for three-key Triple-DES. */
121 if( key_bits == 64 )
122 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
123 else
124 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
125 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
126 * but two-key Triple-DES is functionally three-key Triple-DES
127 * with K1=K3, so that's how we present it to mbedtls. */
128 if( key_bits == 128 )
129 key_bits = 192;
130 break;
131 case PSA_KEY_TYPE_CAMELLIA:
132 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
133 break;
134 case PSA_KEY_TYPE_ARC4:
135 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
136 break;
137 case PSA_KEY_TYPE_CHACHA20:
138 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
139 break;
140 default:
141 return( NULL );
142 }
143 if( cipher_id != NULL )
144 *cipher_id = cipher_id_tmp;
145
146 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
147 (int) key_bits, mode ) );
148}
149
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100150#if defined(MBEDTLS_PSA_BUILTIN_CIPHER) || defined(PSA_CRYPTO_DRIVER_TEST)
151
Ronald Crond6d28882020-12-14 14:56:02 +0100152static psa_status_t cipher_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +0100153 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +0100154 const psa_key_attributes_t *attributes,
155 const uint8_t *key_buffer, size_t key_buffer_size,
156 psa_algorithm_t alg,
157 mbedtls_operation_t cipher_operation )
158{
159 int ret = 0;
160 size_t key_bits;
161 const mbedtls_cipher_info_t *cipher_info = NULL;
162 psa_key_type_t key_type = attributes->core.type;
163
164 (void)key_buffer_size;
165
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200166 mbedtls_cipher_init( &operation->ctx.cipher );
Ronald Crond6d28882020-12-14 14:56:02 +0100167
Ronald Cron6e412a72021-03-10 09:58:47 +0100168 operation->alg = alg;
Ronald Crond6d28882020-12-14 14:56:02 +0100169 key_bits = attributes->core.bits;
170 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
171 key_bits, NULL );
172 if( cipher_info == NULL )
173 return( PSA_ERROR_NOT_SUPPORTED );
174
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200175 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Ronald Crond6d28882020-12-14 14:56:02 +0100176 if( ret != 0 )
177 goto exit;
178
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100179#if defined(BUILTIN_KEY_TYPE_DES)
Ronald Crond6d28882020-12-14 14:56:02 +0100180 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
181 {
182 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
183 uint8_t keys[24];
184 memcpy( keys, key_buffer, 16 );
185 memcpy( keys + 16, key_buffer, 8 );
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200186 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100187 keys,
188 192, cipher_operation );
189 }
190 else
191#endif
192 {
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200193 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, key_buffer,
Ronald Crond6d28882020-12-14 14:56:02 +0100194 (int) key_bits, cipher_operation );
195 }
196 if( ret != 0 )
197 goto exit;
198
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100199#if defined(BUILTIN_ALG_CBC_NO_PADDING) || \
200 defined(BUILTIN_ALG_CBC_PKCS7)
Ronald Crond6d28882020-12-14 14:56:02 +0100201 switch( alg )
202 {
203 case PSA_ALG_CBC_NO_PADDING:
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200204 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100205 MBEDTLS_PADDING_NONE );
206 break;
207 case PSA_ALG_CBC_PKCS7:
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200208 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100209 MBEDTLS_PADDING_PKCS7 );
210 break;
211 default:
212 /* The algorithm doesn't involve padding. */
213 ret = 0;
214 break;
215 }
216 if( ret != 0 )
217 goto exit;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100218#endif /* BUILTIN_ALG_CBC_NO_PADDING || BUILTIN_ALG_CBC_PKCS7 */
Ronald Crond6d28882020-12-14 14:56:02 +0100219
Ronald Cron6ad554c2021-03-26 09:29:09 +0100220 operation->block_length = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
221 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Ronald Cronc17e8a92021-03-19 14:12:26 +0100222 operation->iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg );
Ronald Crond6d28882020-12-14 14:56:02 +0100223
224exit:
225 return( mbedtls_to_psa_error( ret ) );
226}
227
Ronald Cron8287e6b2021-03-12 10:35:18 +0100228static psa_status_t cipher_encrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +0100229 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +0100230 const psa_key_attributes_t *attributes,
231 const uint8_t *key_buffer, size_t key_buffer_size,
232 psa_algorithm_t alg )
233{
234 return( cipher_setup( operation, attributes,
235 key_buffer, key_buffer_size,
236 alg, MBEDTLS_ENCRYPT ) );
237}
238
Ronald Cron8287e6b2021-03-12 10:35:18 +0100239static psa_status_t cipher_decrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +0100240 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +0100241 const psa_key_attributes_t *attributes,
242 const uint8_t *key_buffer, size_t key_buffer_size,
243 psa_algorithm_t alg )
244{
245 return( cipher_setup( operation, attributes,
246 key_buffer, key_buffer_size,
247 alg, MBEDTLS_DECRYPT ) );
248}
Ronald Cron6d051732020-10-01 14:10:20 +0200249
Ronald Cron8287e6b2021-03-12 10:35:18 +0100250static psa_status_t cipher_set_iv( mbedtls_psa_cipher_operation_t *operation,
251 const uint8_t *iv, size_t iv_length )
252{
Ronald Cron6ad554c2021-03-26 09:29:09 +0100253 if( iv_length != operation->iv_length )
Ronald Cron8287e6b2021-03-12 10:35:18 +0100254 return( PSA_ERROR_INVALID_ARGUMENT );
255
256 return( mbedtls_to_psa_error(
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200257 mbedtls_cipher_set_iv( &operation->ctx.cipher,
Ronald Cron8287e6b2021-03-12 10:35:18 +0100258 iv, iv_length ) ) );
259}
260
Gilles Peskine7b1c9162021-09-13 09:33:28 +0200261/** Process input for which the algorithm is set to ECB mode.
262 *
263 * This requires manual processing, since the PSA API is defined as being
264 * able to process arbitrary-length calls to psa_cipher_update() with ECB mode,
265 * but the underlying mbedtls_cipher_update only takes full blocks.
266 *
267 * \param ctx The mbedtls cipher context to use. It must have been
268 * set up for ECB.
269 * \param[in] input The input plaintext or ciphertext to process.
270 * \param input_length The number of bytes to process from \p input.
271 * This does not need to be aligned to a block boundary.
272 * If there is a partial block at the end of the input,
273 * it is stored in \p ctx for future processing.
274 * \param output The buffer where the output is written.
275 * \param output_size The size of \p output in bytes.
276 * It must be at least `floor((p + input_length) / BS)`
277 * where `p` is the number of bytes in the unprocessed
278 * partial block in \p ctx (`0 <= p <= BS - 1`) and
279 * `BS` is the block size.
280 * \param output_length On success, the number of bytes written to \p output.
281 * \c 0 on error.
282 *
283 * \return #PSA_SUCCESS or an error from a hardware accelerator
284 */
Ronald Cron6d051732020-10-01 14:10:20 +0200285static psa_status_t psa_cipher_update_ecb(
286 mbedtls_cipher_context_t *ctx,
287 const uint8_t *input,
288 size_t input_length,
289 uint8_t *output,
290 size_t output_size,
291 size_t *output_length )
292{
293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
294 size_t block_size = ctx->cipher_info->block_size;
295 size_t internal_output_length = 0;
296 *output_length = 0;
297
298 if( input_length == 0 )
299 {
300 status = PSA_SUCCESS;
301 goto exit;
302 }
303
304 if( ctx->unprocessed_len > 0 )
305 {
306 /* Fill up to block size, and run the block if there's a full one. */
307 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
308
309 if( input_length < bytes_to_copy )
310 bytes_to_copy = input_length;
311
312 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
313 input, bytes_to_copy );
314 input_length -= bytes_to_copy;
315 input += bytes_to_copy;
316 ctx->unprocessed_len += bytes_to_copy;
317
318 if( ctx->unprocessed_len == block_size )
319 {
320 status = mbedtls_to_psa_error(
321 mbedtls_cipher_update( ctx,
322 ctx->unprocessed_data,
323 block_size,
324 output, &internal_output_length ) );
325
326 if( status != PSA_SUCCESS )
327 goto exit;
328
329 output += internal_output_length;
330 output_size -= internal_output_length;
331 *output_length += internal_output_length;
332 ctx->unprocessed_len = 0;
333 }
334 }
335
336 while( input_length >= block_size )
337 {
338 /* Run all full blocks we have, one by one */
339 status = mbedtls_to_psa_error(
340 mbedtls_cipher_update( ctx, input,
341 block_size,
342 output, &internal_output_length ) );
343
344 if( status != PSA_SUCCESS )
345 goto exit;
346
347 input_length -= block_size;
348 input += block_size;
349
350 output += internal_output_length;
351 output_size -= internal_output_length;
352 *output_length += internal_output_length;
353 }
354
355 if( input_length > 0 )
356 {
357 /* Save unprocessed bytes for later processing */
358 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
359 input, input_length );
360 ctx->unprocessed_len += input_length;
361 }
362
363 status = PSA_SUCCESS;
364
365exit:
366 return( status );
367}
368
Ronald Cron8287e6b2021-03-12 10:35:18 +0100369static psa_status_t cipher_update( mbedtls_psa_cipher_operation_t *operation,
370 const uint8_t *input,
371 size_t input_length,
372 uint8_t *output,
373 size_t output_size,
374 size_t *output_length )
Ronald Cron6d051732020-10-01 14:10:20 +0200375{
376 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
377 size_t expected_output_size;
378
Ronald Cron6e412a72021-03-10 09:58:47 +0100379 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Ronald Cron6d051732020-10-01 14:10:20 +0200380 {
381 /* Take the unprocessed partial block left over from previous
382 * update calls, if any, plus the input to this call. Remove
383 * the last partial block, if any. You get the data that will be
384 * output in this call. */
385 expected_output_size =
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200386 ( operation->ctx.cipher.unprocessed_len + input_length )
Ronald Cron6ad554c2021-03-26 09:29:09 +0100387 / operation->block_length * operation->block_length;
Ronald Cron6d051732020-10-01 14:10:20 +0200388 }
389 else
390 {
391 expected_output_size = input_length;
392 }
393
394 if( output_size < expected_output_size )
395 return( PSA_ERROR_BUFFER_TOO_SMALL );
396
Ronald Cron6e412a72021-03-10 09:58:47 +0100397 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
Ronald Cron6d051732020-10-01 14:10:20 +0200398 {
399 /* mbedtls_cipher_update has an API inconsistency: it will only
400 * process a single block at a time in ECB mode. Abstract away that
401 * inconsistency here to match the PSA API behaviour. */
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200402 status = psa_cipher_update_ecb( &operation->ctx.cipher,
Ronald Cron6d051732020-10-01 14:10:20 +0200403 input,
404 input_length,
405 output,
406 output_size,
407 output_length );
408 }
409 else
410 {
411 status = mbedtls_to_psa_error(
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200412 mbedtls_cipher_update( &operation->ctx.cipher, input,
Ronald Cron6d051732020-10-01 14:10:20 +0200413 input_length, output, output_length ) );
gabor-mezei-arm42373bd2021-06-29 16:41:25 +0200414
415 if( *output_length > output_size )
gabor-mezei-arm25230452021-06-29 19:06:30 +0200416 return( PSA_ERROR_CORRUPTION_DETECTED );
Ronald Cron6d051732020-10-01 14:10:20 +0200417 }
418
419 return( status );
420}
421
Ronald Cron8287e6b2021-03-12 10:35:18 +0100422static psa_status_t cipher_finish( mbedtls_psa_cipher_operation_t *operation,
423 uint8_t *output,
424 size_t output_size,
425 size_t *output_length )
Ronald Cron6d051732020-10-01 14:10:20 +0200426{
427 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
428 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
429
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200430 if( operation->ctx.cipher.unprocessed_len != 0 )
Ronald Cron6d051732020-10-01 14:10:20 +0200431 {
Ronald Cron6e412a72021-03-10 09:58:47 +0100432 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
433 operation->alg == PSA_ALG_CBC_NO_PADDING )
Ronald Cron6d051732020-10-01 14:10:20 +0200434 {
435 status = PSA_ERROR_INVALID_ARGUMENT;
436 goto exit;
437 }
438 }
439
440 status = mbedtls_to_psa_error(
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200441 mbedtls_cipher_finish( &operation->ctx.cipher,
Ronald Cron6d051732020-10-01 14:10:20 +0200442 temp_output_buffer,
443 output_length ) );
444 if( status != PSA_SUCCESS )
445 goto exit;
446
447 if( *output_length == 0 )
448 ; /* Nothing to copy. Note that output may be NULL in this case. */
449 else if( output_size >= *output_length )
450 memcpy( output, temp_output_buffer, *output_length );
451 else
452 status = PSA_ERROR_BUFFER_TOO_SMALL;
453
454exit:
455 mbedtls_platform_zeroize( temp_output_buffer,
456 sizeof( temp_output_buffer ) );
457
458 return( status );
459}
460
Ronald Cron8287e6b2021-03-12 10:35:18 +0100461static psa_status_t cipher_abort( mbedtls_psa_cipher_operation_t *operation )
Ronald Cron6d051732020-10-01 14:10:20 +0200462{
Ronald Cron937dfee2021-03-10 09:17:32 +0100463 /* Sanity check (shouldn't happen: operation->alg should
464 * always have been initialized to a valid value). */
Ronald Cron6e412a72021-03-10 09:58:47 +0100465 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
Ronald Cron937dfee2021-03-10 09:17:32 +0100466 return( PSA_ERROR_BAD_STATE );
467
gabor-mezei-armf67d8af2021-04-12 15:47:35 +0200468 mbedtls_cipher_free( &operation->ctx.cipher );
Ronald Cron6d051732020-10-01 14:10:20 +0200469
470 return( PSA_SUCCESS );
471}
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100472
473static psa_status_t cipher_encrypt( const psa_key_attributes_t *attributes,
474 const uint8_t *key_buffer,
475 size_t key_buffer_size,
476 psa_algorithm_t alg,
477 const uint8_t *input,
478 size_t input_length,
479 uint8_t *output,
480 size_t output_size,
481 size_t *output_length )
482{
483 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
484 mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm7fbea092021-06-25 15:23:05 +0200485 size_t olength, accumulated_length;
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100486
gabor-mezei-arm0e1d31b2021-06-25 15:44:47 +0200487 status = cipher_encrypt_setup( &operation, attributes,
488 key_buffer, key_buffer_size, alg );
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100489 if( status != PSA_SUCCESS )
490 goto exit;
491
gabor-mezei-arm7fbea092021-06-25 15:23:05 +0200492 accumulated_length = 0;
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100493 if( operation.iv_length > 0 )
494 {
495 status = cipher_set_iv( &operation, output, operation.iv_length );
496 if( status != PSA_SUCCESS )
497 goto exit;
498
gabor-mezei-arm7fbea092021-06-25 15:23:05 +0200499 accumulated_length = operation.iv_length;
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100500 }
501
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100502 status = cipher_update( &operation, input, input_length,
gabor-mezei-arm7fbea092021-06-25 15:23:05 +0200503 output + operation.iv_length,
504 output_size - operation.iv_length,
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100505 &olength );
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100506 if( status != PSA_SUCCESS )
507 goto exit;
508
gabor-mezei-arm809634d2021-06-29 16:42:13 +0200509 accumulated_length += olength;
510
gabor-mezei-arm7fbea092021-06-25 15:23:05 +0200511 status = cipher_finish( &operation, output + accumulated_length,
512 output_size - accumulated_length, &olength );
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100513 if( status != PSA_SUCCESS )
514 goto exit;
515
gabor-mezei-arm25230452021-06-29 19:06:30 +0200516 *output_length = accumulated_length + olength;
gabor-mezei-arm7fbea092021-06-25 15:23:05 +0200517
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100518exit:
519 if( status == PSA_SUCCESS )
520 status = cipher_abort( &operation );
521 else
522 cipher_abort( &operation );
523 return( status );
524}
525
526static psa_status_t cipher_decrypt( const psa_key_attributes_t *attributes,
527 const uint8_t *key_buffer,
528 size_t key_buffer_size,
529 psa_algorithm_t alg,
530 const uint8_t *input,
531 size_t input_length,
532 uint8_t *output,
533 size_t output_size,
534 size_t *output_length )
535{
536 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
537 mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm7fbea092021-06-25 15:23:05 +0200538 size_t olength, accumulated_length;
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100539
540 status = cipher_decrypt_setup( &operation, attributes,
541 key_buffer, key_buffer_size, alg );
542 if( status != PSA_SUCCESS )
543 goto exit;
544
545 if( operation.iv_length > 0 )
546 {
547 status = cipher_set_iv( &operation, input, operation.iv_length );
548 if( status != PSA_SUCCESS )
549 goto exit;
550 }
551
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100552 status = cipher_update( &operation, input + operation.iv_length,
553 input_length - operation.iv_length,
554 output, output_size, &olength );
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100555 if( status != PSA_SUCCESS )
556 goto exit;
557
gabor-mezei-arm809634d2021-06-29 16:42:13 +0200558 accumulated_length = olength;
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +0200559
gabor-mezei-arm7fbea092021-06-25 15:23:05 +0200560 status = cipher_finish( &operation, output + accumulated_length,
561 output_size - accumulated_length, &olength );
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100562 if( status != PSA_SUCCESS )
563 goto exit;
564
gabor-mezei-arm25230452021-06-29 19:06:30 +0200565 *output_length = accumulated_length + olength;
gabor-mezei-arm7fbea092021-06-25 15:23:05 +0200566
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100567exit:
568 if ( status == PSA_SUCCESS )
569 status = cipher_abort( &operation );
570 else
571 cipher_abort( &operation );
572 return( status );
573}
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100574#endif /* MBEDTLS_PSA_BUILTIN_CIPHER || PSA_CRYPTO_DRIVER_TEST */
Ronald Cron6d051732020-10-01 14:10:20 +0200575
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100576#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron8287e6b2021-03-12 10:35:18 +0100577psa_status_t mbedtls_psa_cipher_encrypt_setup(
578 mbedtls_psa_cipher_operation_t *operation,
579 const psa_key_attributes_t *attributes,
580 const uint8_t *key_buffer, size_t key_buffer_size,
581 psa_algorithm_t alg )
582{
583 return( cipher_encrypt_setup(
584 operation, attributes, key_buffer, key_buffer_size, alg ) );
585}
586
587psa_status_t mbedtls_psa_cipher_decrypt_setup(
588 mbedtls_psa_cipher_operation_t *operation,
589 const psa_key_attributes_t *attributes,
590 const uint8_t *key_buffer, size_t key_buffer_size,
591 psa_algorithm_t alg )
592{
593 return( cipher_decrypt_setup(
594 operation, attributes, key_buffer, key_buffer_size, alg ) );
595}
596
Ronald Cron8287e6b2021-03-12 10:35:18 +0100597psa_status_t mbedtls_psa_cipher_set_iv( mbedtls_psa_cipher_operation_t *operation,
598 const uint8_t *iv,
599 size_t iv_length )
600{
601 return( cipher_set_iv( operation, iv, iv_length ) );
602}
603
604psa_status_t mbedtls_psa_cipher_update( mbedtls_psa_cipher_operation_t *operation,
605 const uint8_t *input,
606 size_t input_length,
607 uint8_t *output,
608 size_t output_size,
609 size_t *output_length )
610{
611 return( cipher_update( operation, input, input_length,
612 output, output_size, output_length ) );
613}
614
615psa_status_t mbedtls_psa_cipher_finish( mbedtls_psa_cipher_operation_t *operation,
616 uint8_t *output,
617 size_t output_size,
618 size_t *output_length )
619{
620 return( cipher_finish( operation, output, output_size, output_length ) );
621}
622
623psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation )
624{
625 return( cipher_abort( operation ) );
626}
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100627
628psa_status_t mbedtls_psa_cipher_encrypt( const psa_key_attributes_t *attributes,
629 const uint8_t *key_buffer,
630 size_t key_buffer_size,
631 psa_algorithm_t alg,
632 const uint8_t *input,
633 size_t input_length,
634 uint8_t *output,
635 size_t output_size,
636 size_t *output_length )
637{
638 return( cipher_encrypt( attributes, key_buffer, key_buffer_size,
639 alg, input, input_length,
640 output, output_size, output_length ) );
641}
642
643psa_status_t mbedtls_psa_cipher_decrypt( const psa_key_attributes_t *attributes,
644 const uint8_t *key_buffer,
645 size_t key_buffer_size,
646 psa_algorithm_t alg,
647 const uint8_t *input,
648 size_t input_length,
649 uint8_t *output,
650 size_t output_size,
651 size_t *output_length )
652{
653 return( cipher_decrypt( attributes, key_buffer, key_buffer_size,
654 alg, input, input_length,
655 output, output_size, output_length ) );
656}
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100657#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron8287e6b2021-03-12 10:35:18 +0100658
Ronald Cron3522e322021-03-12 11:08:49 +0100659/*
660 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
661 */
662
663#if defined(PSA_CRYPTO_DRIVER_TEST)
664psa_status_t mbedtls_transparent_test_driver_cipher_encrypt_setup(
665 mbedtls_psa_cipher_operation_t *operation,
666 const psa_key_attributes_t *attributes,
667 const uint8_t *key_buffer, size_t key_buffer_size,
668 psa_algorithm_t alg )
669{
670 return( cipher_encrypt_setup(
671 operation, attributes, key_buffer, key_buffer_size, alg ) );
672}
673
674psa_status_t mbedtls_transparent_test_driver_cipher_decrypt_setup(
675 mbedtls_psa_cipher_operation_t *operation,
676 const psa_key_attributes_t *attributes,
677 const uint8_t *key_buffer, size_t key_buffer_size,
678 psa_algorithm_t alg )
679{
680 return( cipher_decrypt_setup(
681 operation, attributes, key_buffer, key_buffer_size, alg ) );
682}
683
Ronald Cron3522e322021-03-12 11:08:49 +0100684psa_status_t mbedtls_transparent_test_driver_cipher_set_iv(
685 mbedtls_psa_cipher_operation_t *operation,
686 const uint8_t *iv, size_t iv_length )
687{
688 return( cipher_set_iv( operation, iv, iv_length ) );
689}
690
691psa_status_t mbedtls_transparent_test_driver_cipher_update(
692 mbedtls_psa_cipher_operation_t *operation,
693 const uint8_t *input, size_t input_length,
694 uint8_t *output, size_t output_size, size_t *output_length )
695{
696 return( cipher_update( operation, input, input_length,
697 output, output_size, output_length ) );
698}
699
700psa_status_t mbedtls_transparent_test_driver_cipher_finish(
701 mbedtls_psa_cipher_operation_t *operation,
702 uint8_t *output, size_t output_size, size_t *output_length )
703{
704 return( cipher_finish( operation, output, output_size, output_length ) );
705}
706
707psa_status_t mbedtls_transparent_test_driver_cipher_abort(
708 mbedtls_psa_cipher_operation_t *operation )
709{
710 return( cipher_abort( operation ) );
711}
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100712
713psa_status_t mbedtls_transparent_test_driver_cipher_encrypt(
714 const psa_key_attributes_t *attributes,
715 const uint8_t *key_buffer,
716 size_t key_buffer_size,
717 psa_algorithm_t alg,
718 const uint8_t *input,
719 size_t input_length,
720 uint8_t *output,
721 size_t output_size,
722 size_t *output_length )
723{
724 return( cipher_encrypt( attributes, key_buffer, key_buffer_size,
725 alg, input, input_length,
726 output, output_size, output_length ) );
727}
728
729psa_status_t mbedtls_transparent_test_driver_cipher_decrypt(
730 const psa_key_attributes_t *attributes,
731 const uint8_t *key_buffer,
732 size_t key_buffer_size,
733 psa_algorithm_t alg,
734 const uint8_t *input,
735 size_t input_length,
736 uint8_t *output,
737 size_t output_size,
738 size_t *output_length )
739{
740 return( cipher_decrypt( attributes, key_buffer, key_buffer_size,
741 alg, input, input_length,
742 output, output_size, output_length ) );
743}
Ronald Cron3522e322021-03-12 11:08:49 +0100744#endif /* PSA_CRYPTO_DRIVER_TEST */
745
Ronald Cron0ff57952021-03-08 16:46:35 +0100746#endif /* MBEDTLS_PSA_CRYPTO_C */