blob: 464bd5778297f17f4add5c4ce39e6e33afc63836 [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
166 /* Proceed with initializing an mbed TLS cipher context if no driver is
167 * available for the given algorithm & key. */
Ronald Cron6e412a72021-03-10 09:58:47 +0100168 mbedtls_cipher_init( &operation->cipher );
Ronald Crond6d28882020-12-14 14:56:02 +0100169
Ronald Cron6e412a72021-03-10 09:58:47 +0100170 operation->alg = alg;
Ronald Crond6d28882020-12-14 14:56:02 +0100171 key_bits = attributes->core.bits;
172 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
173 key_bits, NULL );
174 if( cipher_info == NULL )
175 return( PSA_ERROR_NOT_SUPPORTED );
176
Ronald Cron6e412a72021-03-10 09:58:47 +0100177 ret = mbedtls_cipher_setup( &operation->cipher, cipher_info );
Ronald Crond6d28882020-12-14 14:56:02 +0100178 if( ret != 0 )
179 goto exit;
180
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100181#if defined(BUILTIN_KEY_TYPE_DES)
Ronald Crond6d28882020-12-14 14:56:02 +0100182 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
183 {
184 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
185 uint8_t keys[24];
186 memcpy( keys, key_buffer, 16 );
187 memcpy( keys + 16, key_buffer, 8 );
Ronald Cron6e412a72021-03-10 09:58:47 +0100188 ret = mbedtls_cipher_setkey( &operation->cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100189 keys,
190 192, cipher_operation );
191 }
192 else
193#endif
194 {
Ronald Cron6e412a72021-03-10 09:58:47 +0100195 ret = mbedtls_cipher_setkey( &operation->cipher, key_buffer,
Ronald Crond6d28882020-12-14 14:56:02 +0100196 (int) key_bits, cipher_operation );
197 }
198 if( ret != 0 )
199 goto exit;
200
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100201#if defined(BUILTIN_ALG_CBC_NO_PADDING) || \
202 defined(BUILTIN_ALG_CBC_PKCS7)
Ronald Crond6d28882020-12-14 14:56:02 +0100203 switch( alg )
204 {
205 case PSA_ALG_CBC_NO_PADDING:
Ronald Cron6e412a72021-03-10 09:58:47 +0100206 ret = mbedtls_cipher_set_padding_mode( &operation->cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100207 MBEDTLS_PADDING_NONE );
208 break;
209 case PSA_ALG_CBC_PKCS7:
Ronald Cron6e412a72021-03-10 09:58:47 +0100210 ret = mbedtls_cipher_set_padding_mode( &operation->cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100211 MBEDTLS_PADDING_PKCS7 );
212 break;
213 default:
214 /* The algorithm doesn't involve padding. */
215 ret = 0;
216 break;
217 }
218 if( ret != 0 )
219 goto exit;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100220#endif /* BUILTIN_ALG_CBC_NO_PADDING || BUILTIN_ALG_CBC_PKCS7 */
Ronald Crond6d28882020-12-14 14:56:02 +0100221
Ronald Cron6ad554c2021-03-26 09:29:09 +0100222 operation->block_length = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
223 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Ronald Crond6d28882020-12-14 14:56:02 +0100224 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
225 alg != PSA_ALG_ECB_NO_PADDING )
226 {
Ronald Cron6ad554c2021-03-26 09:29:09 +0100227 operation->iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Ronald Crond6d28882020-12-14 14:56:02 +0100228 }
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100229#if defined(BUILTIN_KEY_TYPE_CHACHA20)
Ronald Crond6d28882020-12-14 14:56:02 +0100230 else
231 if( ( alg == PSA_ALG_STREAM_CIPHER ) &&
232 ( key_type == PSA_KEY_TYPE_CHACHA20 ) )
Ronald Cron6ad554c2021-03-26 09:29:09 +0100233 operation->iv_length = 12;
Ronald Crond6d28882020-12-14 14:56:02 +0100234#endif
235
236exit:
237 return( mbedtls_to_psa_error( ret ) );
238}
239
Ronald Cron8287e6b2021-03-12 10:35:18 +0100240static psa_status_t cipher_encrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +0100241 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +0100242 const psa_key_attributes_t *attributes,
243 const uint8_t *key_buffer, size_t key_buffer_size,
244 psa_algorithm_t alg )
245{
246 return( cipher_setup( operation, attributes,
247 key_buffer, key_buffer_size,
248 alg, MBEDTLS_ENCRYPT ) );
249}
250
Ronald Cron8287e6b2021-03-12 10:35:18 +0100251static psa_status_t cipher_decrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +0100252 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +0100253 const psa_key_attributes_t *attributes,
254 const uint8_t *key_buffer, size_t key_buffer_size,
255 psa_algorithm_t alg )
256{
257 return( cipher_setup( operation, attributes,
258 key_buffer, key_buffer_size,
259 alg, MBEDTLS_DECRYPT ) );
260}
Ronald Cron6d051732020-10-01 14:10:20 +0200261
Ronald Cron8287e6b2021-03-12 10:35:18 +0100262static psa_status_t cipher_set_iv( mbedtls_psa_cipher_operation_t *operation,
263 const uint8_t *iv, size_t iv_length )
264{
Ronald Cron6ad554c2021-03-26 09:29:09 +0100265 if( iv_length != operation->iv_length )
Ronald Cron8287e6b2021-03-12 10:35:18 +0100266 return( PSA_ERROR_INVALID_ARGUMENT );
267
268 return( mbedtls_to_psa_error(
269 mbedtls_cipher_set_iv( &operation->cipher,
270 iv, iv_length ) ) );
271}
272
273static psa_status_t cipher_generate_iv(
Ronald Cron6e412a72021-03-10 09:58:47 +0100274 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200275 uint8_t *iv, size_t iv_size, size_t *iv_length )
276{
Ronald Cronf2381aa2021-03-23 11:31:19 +0100277 int status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +0200278
Ronald Cron6ad554c2021-03-26 09:29:09 +0100279 if( iv_size < operation->iv_length )
Ronald Cron6d051732020-10-01 14:10:20 +0200280 return( PSA_ERROR_BUFFER_TOO_SMALL );
281
Ronald Cron6ad554c2021-03-26 09:29:09 +0100282 status = psa_generate_random( iv, operation->iv_length );
Ronald Cronf2381aa2021-03-23 11:31:19 +0100283 if( status != PSA_SUCCESS )
284 return( status );
Ronald Cron6d051732020-10-01 14:10:20 +0200285
Ronald Cron6ad554c2021-03-26 09:29:09 +0100286 *iv_length = operation->iv_length;
Ronald Cron6d051732020-10-01 14:10:20 +0200287
Ronald Cron8287e6b2021-03-12 10:35:18 +0100288 return( cipher_set_iv( operation, iv, *iv_length ) );
Ronald Cron6d051732020-10-01 14:10:20 +0200289}
290
291/* Process input for which the algorithm is set to ECB mode. This requires
292 * manual processing, since the PSA API is defined as being able to process
293 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
294 * underlying mbedtls_cipher_update only takes full blocks. */
295static psa_status_t psa_cipher_update_ecb(
296 mbedtls_cipher_context_t *ctx,
297 const uint8_t *input,
298 size_t input_length,
299 uint8_t *output,
300 size_t output_size,
301 size_t *output_length )
302{
303 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
304 size_t block_size = ctx->cipher_info->block_size;
305 size_t internal_output_length = 0;
306 *output_length = 0;
307
308 if( input_length == 0 )
309 {
310 status = PSA_SUCCESS;
311 goto exit;
312 }
313
314 if( ctx->unprocessed_len > 0 )
315 {
316 /* Fill up to block size, and run the block if there's a full one. */
317 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
318
319 if( input_length < bytes_to_copy )
320 bytes_to_copy = input_length;
321
322 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
323 input, bytes_to_copy );
324 input_length -= bytes_to_copy;
325 input += bytes_to_copy;
326 ctx->unprocessed_len += bytes_to_copy;
327
328 if( ctx->unprocessed_len == block_size )
329 {
330 status = mbedtls_to_psa_error(
331 mbedtls_cipher_update( ctx,
332 ctx->unprocessed_data,
333 block_size,
334 output, &internal_output_length ) );
335
336 if( status != PSA_SUCCESS )
337 goto exit;
338
339 output += internal_output_length;
340 output_size -= internal_output_length;
341 *output_length += internal_output_length;
342 ctx->unprocessed_len = 0;
343 }
344 }
345
346 while( input_length >= block_size )
347 {
348 /* Run all full blocks we have, one by one */
349 status = mbedtls_to_psa_error(
350 mbedtls_cipher_update( ctx, input,
351 block_size,
352 output, &internal_output_length ) );
353
354 if( status != PSA_SUCCESS )
355 goto exit;
356
357 input_length -= block_size;
358 input += block_size;
359
360 output += internal_output_length;
361 output_size -= internal_output_length;
362 *output_length += internal_output_length;
363 }
364
365 if( input_length > 0 )
366 {
367 /* Save unprocessed bytes for later processing */
368 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
369 input, input_length );
370 ctx->unprocessed_len += input_length;
371 }
372
373 status = PSA_SUCCESS;
374
375exit:
376 return( status );
377}
378
Ronald Cron8287e6b2021-03-12 10:35:18 +0100379static psa_status_t cipher_update( mbedtls_psa_cipher_operation_t *operation,
380 const uint8_t *input,
381 size_t input_length,
382 uint8_t *output,
383 size_t output_size,
384 size_t *output_length )
Ronald Cron6d051732020-10-01 14:10:20 +0200385{
386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
387 size_t expected_output_size;
388
Ronald Cron6e412a72021-03-10 09:58:47 +0100389 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Ronald Cron6d051732020-10-01 14:10:20 +0200390 {
391 /* Take the unprocessed partial block left over from previous
392 * update calls, if any, plus the input to this call. Remove
393 * the last partial block, if any. You get the data that will be
394 * output in this call. */
395 expected_output_size =
Ronald Cron6e412a72021-03-10 09:58:47 +0100396 ( operation->cipher.unprocessed_len + input_length )
Ronald Cron6ad554c2021-03-26 09:29:09 +0100397 / operation->block_length * operation->block_length;
Ronald Cron6d051732020-10-01 14:10:20 +0200398 }
399 else
400 {
401 expected_output_size = input_length;
402 }
403
404 if( output_size < expected_output_size )
405 return( PSA_ERROR_BUFFER_TOO_SMALL );
406
Ronald Cron6e412a72021-03-10 09:58:47 +0100407 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
Ronald Cron6d051732020-10-01 14:10:20 +0200408 {
409 /* mbedtls_cipher_update has an API inconsistency: it will only
410 * process a single block at a time in ECB mode. Abstract away that
411 * inconsistency here to match the PSA API behaviour. */
Ronald Cron6e412a72021-03-10 09:58:47 +0100412 status = psa_cipher_update_ecb( &operation->cipher,
Ronald Cron6d051732020-10-01 14:10:20 +0200413 input,
414 input_length,
415 output,
416 output_size,
417 output_length );
418 }
419 else
420 {
421 status = mbedtls_to_psa_error(
Ronald Cron6e412a72021-03-10 09:58:47 +0100422 mbedtls_cipher_update( &operation->cipher, input,
Ronald Cron6d051732020-10-01 14:10:20 +0200423 input_length, output, output_length ) );
424 }
425
426 return( status );
427}
428
Ronald Cron8287e6b2021-03-12 10:35:18 +0100429static psa_status_t cipher_finish( mbedtls_psa_cipher_operation_t *operation,
430 uint8_t *output,
431 size_t output_size,
432 size_t *output_length )
Ronald Cron6d051732020-10-01 14:10:20 +0200433{
434 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
435 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
436
Ronald Cron6e412a72021-03-10 09:58:47 +0100437 if( operation->cipher.unprocessed_len != 0 )
Ronald Cron6d051732020-10-01 14:10:20 +0200438 {
Ronald Cron6e412a72021-03-10 09:58:47 +0100439 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
440 operation->alg == PSA_ALG_CBC_NO_PADDING )
Ronald Cron6d051732020-10-01 14:10:20 +0200441 {
442 status = PSA_ERROR_INVALID_ARGUMENT;
443 goto exit;
444 }
445 }
446
447 status = mbedtls_to_psa_error(
Ronald Cron6e412a72021-03-10 09:58:47 +0100448 mbedtls_cipher_finish( &operation->cipher,
Ronald Cron6d051732020-10-01 14:10:20 +0200449 temp_output_buffer,
450 output_length ) );
451 if( status != PSA_SUCCESS )
452 goto exit;
453
454 if( *output_length == 0 )
455 ; /* Nothing to copy. Note that output may be NULL in this case. */
456 else if( output_size >= *output_length )
457 memcpy( output, temp_output_buffer, *output_length );
458 else
459 status = PSA_ERROR_BUFFER_TOO_SMALL;
460
461exit:
462 mbedtls_platform_zeroize( temp_output_buffer,
463 sizeof( temp_output_buffer ) );
464
465 return( status );
466}
467
Ronald Cron8287e6b2021-03-12 10:35:18 +0100468static psa_status_t cipher_abort( mbedtls_psa_cipher_operation_t *operation )
Ronald Cron6d051732020-10-01 14:10:20 +0200469{
Ronald Cron937dfee2021-03-10 09:17:32 +0100470 /* Sanity check (shouldn't happen: operation->alg should
471 * always have been initialized to a valid value). */
Ronald Cron6e412a72021-03-10 09:58:47 +0100472 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
Ronald Cron937dfee2021-03-10 09:17:32 +0100473 return( PSA_ERROR_BAD_STATE );
474
Ronald Cron6e412a72021-03-10 09:58:47 +0100475 mbedtls_cipher_free( &operation->cipher );
Ronald Cron6d051732020-10-01 14:10:20 +0200476
477 return( PSA_SUCCESS );
478}
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100479#endif /* MBEDTLS_PSA_BUILTIN_CIPHER || PSA_CRYPTO_DRIVER_TEST */
Ronald Cron6d051732020-10-01 14:10:20 +0200480
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100481#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron8287e6b2021-03-12 10:35:18 +0100482psa_status_t mbedtls_psa_cipher_encrypt_setup(
483 mbedtls_psa_cipher_operation_t *operation,
484 const psa_key_attributes_t *attributes,
485 const uint8_t *key_buffer, size_t key_buffer_size,
486 psa_algorithm_t alg )
487{
488 return( cipher_encrypt_setup(
489 operation, attributes, key_buffer, key_buffer_size, alg ) );
490}
491
492psa_status_t mbedtls_psa_cipher_decrypt_setup(
493 mbedtls_psa_cipher_operation_t *operation,
494 const psa_key_attributes_t *attributes,
495 const uint8_t *key_buffer, size_t key_buffer_size,
496 psa_algorithm_t alg )
497{
498 return( cipher_decrypt_setup(
499 operation, attributes, key_buffer, key_buffer_size, alg ) );
500}
501
502psa_status_t mbedtls_psa_cipher_generate_iv(
503 mbedtls_psa_cipher_operation_t *operation,
504 uint8_t *iv, size_t iv_size, size_t *iv_length )
505{
506 return( cipher_generate_iv( operation, iv, iv_size, iv_length ) );
507}
508
509psa_status_t mbedtls_psa_cipher_set_iv( mbedtls_psa_cipher_operation_t *operation,
510 const uint8_t *iv,
511 size_t iv_length )
512{
513 return( cipher_set_iv( operation, iv, iv_length ) );
514}
515
516psa_status_t mbedtls_psa_cipher_update( mbedtls_psa_cipher_operation_t *operation,
517 const uint8_t *input,
518 size_t input_length,
519 uint8_t *output,
520 size_t output_size,
521 size_t *output_length )
522{
523 return( cipher_update( operation, input, input_length,
524 output, output_size, output_length ) );
525}
526
527psa_status_t mbedtls_psa_cipher_finish( mbedtls_psa_cipher_operation_t *operation,
528 uint8_t *output,
529 size_t output_size,
530 size_t *output_length )
531{
532 return( cipher_finish( operation, output, output_size, output_length ) );
533}
534
535psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation )
536{
537 return( cipher_abort( operation ) );
538}
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100539#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron8287e6b2021-03-12 10:35:18 +0100540
Ronald Cron3522e322021-03-12 11:08:49 +0100541/*
542 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
543 */
544
545#if defined(PSA_CRYPTO_DRIVER_TEST)
546psa_status_t mbedtls_transparent_test_driver_cipher_encrypt_setup(
547 mbedtls_psa_cipher_operation_t *operation,
548 const psa_key_attributes_t *attributes,
549 const uint8_t *key_buffer, size_t key_buffer_size,
550 psa_algorithm_t alg )
551{
552 return( cipher_encrypt_setup(
553 operation, attributes, key_buffer, key_buffer_size, alg ) );
554}
555
556psa_status_t mbedtls_transparent_test_driver_cipher_decrypt_setup(
557 mbedtls_psa_cipher_operation_t *operation,
558 const psa_key_attributes_t *attributes,
559 const uint8_t *key_buffer, size_t key_buffer_size,
560 psa_algorithm_t alg )
561{
562 return( cipher_decrypt_setup(
563 operation, attributes, key_buffer, key_buffer_size, alg ) );
564}
565
566psa_status_t mbedtls_transparent_test_driver_cipher_generate_iv(
567 mbedtls_psa_cipher_operation_t *operation,
568 uint8_t *iv, size_t iv_size, size_t *iv_length )
569{
570 return( cipher_generate_iv( operation, iv, iv_size, iv_length ) );
571}
572
573psa_status_t mbedtls_transparent_test_driver_cipher_set_iv(
574 mbedtls_psa_cipher_operation_t *operation,
575 const uint8_t *iv, size_t iv_length )
576{
577 return( cipher_set_iv( operation, iv, iv_length ) );
578}
579
580psa_status_t mbedtls_transparent_test_driver_cipher_update(
581 mbedtls_psa_cipher_operation_t *operation,
582 const uint8_t *input, size_t input_length,
583 uint8_t *output, size_t output_size, size_t *output_length )
584{
585 return( cipher_update( operation, input, input_length,
586 output, output_size, output_length ) );
587}
588
589psa_status_t mbedtls_transparent_test_driver_cipher_finish(
590 mbedtls_psa_cipher_operation_t *operation,
591 uint8_t *output, size_t output_size, size_t *output_length )
592{
593 return( cipher_finish( operation, output, output_size, output_length ) );
594}
595
596psa_status_t mbedtls_transparent_test_driver_cipher_abort(
597 mbedtls_psa_cipher_operation_t *operation )
598{
599 return( cipher_abort( operation ) );
600}
601#endif /* PSA_CRYPTO_DRIVER_TEST */
602
Ronald Cron0ff57952021-03-08 16:46:35 +0100603#endif /* MBEDTLS_PSA_CRYPTO_C */