blob: f67b1ffed2b3b0f8651bbf0252b211f55968790b [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;
Ronald Cron75e6ae22021-03-17 14:46:05 +0100134 case PSA_KEY_TYPE_CHACHA20:
135 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
136 break;
137 default:
138 return( NULL );
139 }
140 if( cipher_id != NULL )
141 *cipher_id = cipher_id_tmp;
142
143 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
144 (int) key_bits, mode ) );
145}
146
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100147#if defined(MBEDTLS_PSA_BUILTIN_CIPHER) || defined(PSA_CRYPTO_DRIVER_TEST)
148
Ronald Crond6d28882020-12-14 14:56:02 +0100149static psa_status_t cipher_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +0100150 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +0100151 const psa_key_attributes_t *attributes,
152 const uint8_t *key_buffer, size_t key_buffer_size,
153 psa_algorithm_t alg,
154 mbedtls_operation_t cipher_operation )
155{
156 int ret = 0;
157 size_t key_bits;
158 const mbedtls_cipher_info_t *cipher_info = NULL;
159 psa_key_type_t key_type = attributes->core.type;
160
161 (void)key_buffer_size;
162
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200163 mbedtls_cipher_init( &operation->ctx.cipher );
Ronald Crond6d28882020-12-14 14:56:02 +0100164
Ronald Cron6e412a72021-03-10 09:58:47 +0100165 operation->alg = alg;
Ronald Crond6d28882020-12-14 14:56:02 +0100166 key_bits = attributes->core.bits;
167 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
168 key_bits, NULL );
169 if( cipher_info == NULL )
170 return( PSA_ERROR_NOT_SUPPORTED );
171
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200172 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Ronald Crond6d28882020-12-14 14:56:02 +0100173 if( ret != 0 )
174 goto exit;
175
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100176#if defined(BUILTIN_KEY_TYPE_DES)
Ronald Crond6d28882020-12-14 14:56:02 +0100177 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
178 {
179 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
180 uint8_t keys[24];
181 memcpy( keys, key_buffer, 16 );
182 memcpy( keys + 16, key_buffer, 8 );
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200183 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100184 keys,
185 192, cipher_operation );
186 }
187 else
188#endif
189 {
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200190 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, key_buffer,
Ronald Crond6d28882020-12-14 14:56:02 +0100191 (int) key_bits, cipher_operation );
192 }
193 if( ret != 0 )
194 goto exit;
195
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100196#if defined(BUILTIN_ALG_CBC_NO_PADDING) || \
197 defined(BUILTIN_ALG_CBC_PKCS7)
Ronald Crond6d28882020-12-14 14:56:02 +0100198 switch( alg )
199 {
200 case PSA_ALG_CBC_NO_PADDING:
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200201 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100202 MBEDTLS_PADDING_NONE );
203 break;
204 case PSA_ALG_CBC_PKCS7:
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200205 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100206 MBEDTLS_PADDING_PKCS7 );
207 break;
208 default:
209 /* The algorithm doesn't involve padding. */
210 ret = 0;
211 break;
212 }
213 if( ret != 0 )
214 goto exit;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100215#endif /* BUILTIN_ALG_CBC_NO_PADDING || BUILTIN_ALG_CBC_PKCS7 */
Ronald Crond6d28882020-12-14 14:56:02 +0100216
Ronald Cron6ad554c2021-03-26 09:29:09 +0100217 operation->block_length = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
218 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Ronald Cronc17e8a92021-03-19 14:12:26 +0100219 operation->iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg );
Ronald Crond6d28882020-12-14 14:56:02 +0100220
221exit:
222 return( mbedtls_to_psa_error( ret ) );
223}
224
Ronald Cron8287e6b2021-03-12 10:35:18 +0100225static psa_status_t cipher_encrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +0100226 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +0100227 const psa_key_attributes_t *attributes,
228 const uint8_t *key_buffer, size_t key_buffer_size,
229 psa_algorithm_t alg )
230{
231 return( cipher_setup( operation, attributes,
232 key_buffer, key_buffer_size,
233 alg, MBEDTLS_ENCRYPT ) );
234}
235
Ronald Cron8287e6b2021-03-12 10:35:18 +0100236static psa_status_t cipher_decrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +0100237 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +0100238 const psa_key_attributes_t *attributes,
239 const uint8_t *key_buffer, size_t key_buffer_size,
240 psa_algorithm_t alg )
241{
242 return( cipher_setup( operation, attributes,
243 key_buffer, key_buffer_size,
244 alg, MBEDTLS_DECRYPT ) );
245}
Ronald Cron6d051732020-10-01 14:10:20 +0200246
Ronald Cron8287e6b2021-03-12 10:35:18 +0100247static psa_status_t cipher_set_iv( mbedtls_psa_cipher_operation_t *operation,
248 const uint8_t *iv, size_t iv_length )
249{
Ronald Cron6ad554c2021-03-26 09:29:09 +0100250 if( iv_length != operation->iv_length )
Ronald Cron8287e6b2021-03-12 10:35:18 +0100251 return( PSA_ERROR_INVALID_ARGUMENT );
252
253 return( mbedtls_to_psa_error(
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200254 mbedtls_cipher_set_iv( &operation->ctx.cipher,
Ronald Cron8287e6b2021-03-12 10:35:18 +0100255 iv, iv_length ) ) );
256}
257
Gilles Peskine55dffe52021-09-13 09:33:28 +0200258/** Process input for which the algorithm is set to ECB mode.
259 *
260 * This requires manual processing, since the PSA API is defined as being
261 * able to process arbitrary-length calls to psa_cipher_update() with ECB mode,
262 * but the underlying mbedtls_cipher_update only takes full blocks.
263 *
264 * \param ctx The mbedtls cipher context to use. It must have been
265 * set up for ECB.
266 * \param[in] input The input plaintext or ciphertext to process.
267 * \param input_length The number of bytes to process from \p input.
268 * This does not need to be aligned to a block boundary.
269 * If there is a partial block at the end of the input,
270 * it is stored in \p ctx for future processing.
271 * \param output The buffer where the output is written.
272 * \param output_size The size of \p output in bytes.
273 * It must be at least `floor((p + input_length) / BS)`
274 * where `p` is the number of bytes in the unprocessed
275 * partial block in \p ctx (`0 <= p <= BS - 1`) and
276 * `BS` is the block size.
277 * \param output_length On success, the number of bytes written to \p output.
278 * \c 0 on error.
279 *
280 * \return #PSA_SUCCESS or an error from a hardware accelerator
281 */
Ronald Cron6d051732020-10-01 14:10:20 +0200282static psa_status_t psa_cipher_update_ecb(
283 mbedtls_cipher_context_t *ctx,
284 const uint8_t *input,
285 size_t input_length,
286 uint8_t *output,
287 size_t output_size,
288 size_t *output_length )
289{
290 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
291 size_t block_size = ctx->cipher_info->block_size;
292 size_t internal_output_length = 0;
293 *output_length = 0;
294
295 if( input_length == 0 )
296 {
297 status = PSA_SUCCESS;
298 goto exit;
299 }
300
301 if( ctx->unprocessed_len > 0 )
302 {
303 /* Fill up to block size, and run the block if there's a full one. */
304 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
305
306 if( input_length < bytes_to_copy )
307 bytes_to_copy = input_length;
308
309 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
310 input, bytes_to_copy );
311 input_length -= bytes_to_copy;
312 input += bytes_to_copy;
313 ctx->unprocessed_len += bytes_to_copy;
314
315 if( ctx->unprocessed_len == block_size )
316 {
317 status = mbedtls_to_psa_error(
318 mbedtls_cipher_update( ctx,
319 ctx->unprocessed_data,
320 block_size,
321 output, &internal_output_length ) );
322
323 if( status != PSA_SUCCESS )
324 goto exit;
325
326 output += internal_output_length;
327 output_size -= internal_output_length;
328 *output_length += internal_output_length;
329 ctx->unprocessed_len = 0;
330 }
331 }
332
333 while( input_length >= block_size )
334 {
335 /* Run all full blocks we have, one by one */
336 status = mbedtls_to_psa_error(
337 mbedtls_cipher_update( ctx, input,
338 block_size,
339 output, &internal_output_length ) );
340
341 if( status != PSA_SUCCESS )
342 goto exit;
343
344 input_length -= block_size;
345 input += block_size;
346
347 output += internal_output_length;
348 output_size -= internal_output_length;
349 *output_length += internal_output_length;
350 }
351
352 if( input_length > 0 )
353 {
354 /* Save unprocessed bytes for later processing */
355 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
356 input, input_length );
357 ctx->unprocessed_len += input_length;
358 }
359
360 status = PSA_SUCCESS;
361
362exit:
363 return( status );
364}
365
Ronald Cron8287e6b2021-03-12 10:35:18 +0100366static psa_status_t cipher_update( mbedtls_psa_cipher_operation_t *operation,
367 const uint8_t *input,
368 size_t input_length,
369 uint8_t *output,
370 size_t output_size,
371 size_t *output_length )
Ronald Cron6d051732020-10-01 14:10:20 +0200372{
373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
374 size_t expected_output_size;
375
Ronald Cron6e412a72021-03-10 09:58:47 +0100376 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Ronald Cron6d051732020-10-01 14:10:20 +0200377 {
378 /* Take the unprocessed partial block left over from previous
379 * update calls, if any, plus the input to this call. Remove
380 * the last partial block, if any. You get the data that will be
381 * output in this call. */
382 expected_output_size =
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200383 ( operation->ctx.cipher.unprocessed_len + input_length )
Ronald Cron6ad554c2021-03-26 09:29:09 +0100384 / operation->block_length * operation->block_length;
Ronald Cron6d051732020-10-01 14:10:20 +0200385 }
386 else
387 {
388 expected_output_size = input_length;
389 }
390
391 if( output_size < expected_output_size )
392 return( PSA_ERROR_BUFFER_TOO_SMALL );
393
Ronald Cron6e412a72021-03-10 09:58:47 +0100394 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
Ronald Cron6d051732020-10-01 14:10:20 +0200395 {
396 /* mbedtls_cipher_update has an API inconsistency: it will only
397 * process a single block at a time in ECB mode. Abstract away that
398 * inconsistency here to match the PSA API behaviour. */
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200399 status = psa_cipher_update_ecb( &operation->ctx.cipher,
Ronald Cron6d051732020-10-01 14:10:20 +0200400 input,
401 input_length,
402 output,
403 output_size,
404 output_length );
405 }
406 else
407 {
408 status = mbedtls_to_psa_error(
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200409 mbedtls_cipher_update( &operation->ctx.cipher, input,
Ronald Cron6d051732020-10-01 14:10:20 +0200410 input_length, output, output_length ) );
gabor-mezei-arm58c17272021-06-29 16:41:25 +0200411
412 if( *output_length > output_size )
gabor-mezei-arm00e54f12021-06-29 19:06:30 +0200413 return( PSA_ERROR_CORRUPTION_DETECTED );
Ronald Cron6d051732020-10-01 14:10:20 +0200414 }
415
416 return( status );
417}
418
Ronald Cron8287e6b2021-03-12 10:35:18 +0100419static psa_status_t cipher_finish( mbedtls_psa_cipher_operation_t *operation,
420 uint8_t *output,
421 size_t output_size,
422 size_t *output_length )
Ronald Cron6d051732020-10-01 14:10:20 +0200423{
424 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
425 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
426
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200427 if( operation->ctx.cipher.unprocessed_len != 0 )
Ronald Cron6d051732020-10-01 14:10:20 +0200428 {
Ronald Cron6e412a72021-03-10 09:58:47 +0100429 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
430 operation->alg == PSA_ALG_CBC_NO_PADDING )
Ronald Cron6d051732020-10-01 14:10:20 +0200431 {
432 status = PSA_ERROR_INVALID_ARGUMENT;
433 goto exit;
434 }
435 }
436
437 status = mbedtls_to_psa_error(
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200438 mbedtls_cipher_finish( &operation->ctx.cipher,
Ronald Cron6d051732020-10-01 14:10:20 +0200439 temp_output_buffer,
440 output_length ) );
441 if( status != PSA_SUCCESS )
442 goto exit;
443
444 if( *output_length == 0 )
445 ; /* Nothing to copy. Note that output may be NULL in this case. */
446 else if( output_size >= *output_length )
447 memcpy( output, temp_output_buffer, *output_length );
448 else
449 status = PSA_ERROR_BUFFER_TOO_SMALL;
450
451exit:
452 mbedtls_platform_zeroize( temp_output_buffer,
453 sizeof( temp_output_buffer ) );
454
455 return( status );
456}
457
Ronald Cron8287e6b2021-03-12 10:35:18 +0100458static psa_status_t cipher_abort( mbedtls_psa_cipher_operation_t *operation )
Ronald Cron6d051732020-10-01 14:10:20 +0200459{
Ronald Cron937dfee2021-03-10 09:17:32 +0100460 /* Sanity check (shouldn't happen: operation->alg should
461 * always have been initialized to a valid value). */
Ronald Cron6e412a72021-03-10 09:58:47 +0100462 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
Ronald Cron937dfee2021-03-10 09:17:32 +0100463 return( PSA_ERROR_BAD_STATE );
464
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200465 mbedtls_cipher_free( &operation->ctx.cipher );
Ronald Cron6d051732020-10-01 14:10:20 +0200466
467 return( PSA_SUCCESS );
468}
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100469
470static psa_status_t cipher_encrypt( const psa_key_attributes_t *attributes,
471 const uint8_t *key_buffer,
472 size_t key_buffer_size,
473 psa_algorithm_t alg,
474 const uint8_t *input,
475 size_t input_length,
476 uint8_t *output,
477 size_t output_size,
478 size_t *output_length )
479{
480 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
481 mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200482 size_t olength, accumulated_length;
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100483
gabor-mezei-arm47a8e142021-06-25 15:44:47 +0200484 status = cipher_encrypt_setup( &operation, attributes,
485 key_buffer, key_buffer_size, alg );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100486 if( status != PSA_SUCCESS )
487 goto exit;
488
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200489 accumulated_length = 0;
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100490 if( operation.iv_length > 0 )
491 {
492 status = cipher_set_iv( &operation, output, operation.iv_length );
493 if( status != PSA_SUCCESS )
494 goto exit;
495
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200496 accumulated_length = operation.iv_length;
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100497 }
498
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100499 status = cipher_update( &operation, input, input_length,
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200500 output + operation.iv_length,
501 output_size - operation.iv_length,
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100502 &olength );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100503 if( status != PSA_SUCCESS )
504 goto exit;
505
gabor-mezei-arm6158e282021-06-29 16:42:13 +0200506 accumulated_length += olength;
507
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200508 status = cipher_finish( &operation, output + accumulated_length,
509 output_size - accumulated_length, &olength );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100510 if( status != PSA_SUCCESS )
511 goto exit;
512
gabor-mezei-arm00e54f12021-06-29 19:06:30 +0200513 *output_length = accumulated_length + olength;
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200514
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100515exit:
516 if( status == PSA_SUCCESS )
517 status = cipher_abort( &operation );
518 else
519 cipher_abort( &operation );
520 return( status );
521}
522
523static psa_status_t cipher_decrypt( const psa_key_attributes_t *attributes,
524 const uint8_t *key_buffer,
525 size_t key_buffer_size,
526 psa_algorithm_t alg,
527 const uint8_t *input,
528 size_t input_length,
529 uint8_t *output,
530 size_t output_size,
531 size_t *output_length )
532{
533 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
534 mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200535 size_t olength, accumulated_length;
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100536
537 status = cipher_decrypt_setup( &operation, attributes,
538 key_buffer, key_buffer_size, alg );
539 if( status != PSA_SUCCESS )
540 goto exit;
541
542 if( operation.iv_length > 0 )
543 {
544 status = cipher_set_iv( &operation, input, operation.iv_length );
545 if( status != PSA_SUCCESS )
546 goto exit;
547 }
548
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100549 status = cipher_update( &operation, input + operation.iv_length,
550 input_length - operation.iv_length,
551 output, output_size, &olength );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100552 if( status != PSA_SUCCESS )
553 goto exit;
554
gabor-mezei-arm6158e282021-06-29 16:42:13 +0200555 accumulated_length = olength;
gabor-mezei-arm258ae072021-06-25 15:25:38 +0200556
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200557 status = cipher_finish( &operation, output + accumulated_length,
558 output_size - accumulated_length, &olength );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100559 if( status != PSA_SUCCESS )
560 goto exit;
561
gabor-mezei-arm00e54f12021-06-29 19:06:30 +0200562 *output_length = accumulated_length + olength;
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200563
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100564exit:
565 if ( status == PSA_SUCCESS )
566 status = cipher_abort( &operation );
567 else
568 cipher_abort( &operation );
569 return( status );
570}
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100571#endif /* MBEDTLS_PSA_BUILTIN_CIPHER || PSA_CRYPTO_DRIVER_TEST */
Ronald Cron6d051732020-10-01 14:10:20 +0200572
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100573#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron8287e6b2021-03-12 10:35:18 +0100574psa_status_t mbedtls_psa_cipher_encrypt_setup(
575 mbedtls_psa_cipher_operation_t *operation,
576 const psa_key_attributes_t *attributes,
577 const uint8_t *key_buffer, size_t key_buffer_size,
578 psa_algorithm_t alg )
579{
580 return( cipher_encrypt_setup(
581 operation, attributes, key_buffer, key_buffer_size, alg ) );
582}
583
584psa_status_t mbedtls_psa_cipher_decrypt_setup(
585 mbedtls_psa_cipher_operation_t *operation,
586 const psa_key_attributes_t *attributes,
587 const uint8_t *key_buffer, size_t key_buffer_size,
588 psa_algorithm_t alg )
589{
590 return( cipher_decrypt_setup(
591 operation, attributes, key_buffer, key_buffer_size, alg ) );
592}
593
Ronald Cron8287e6b2021-03-12 10:35:18 +0100594psa_status_t mbedtls_psa_cipher_set_iv( mbedtls_psa_cipher_operation_t *operation,
595 const uint8_t *iv,
596 size_t iv_length )
597{
598 return( cipher_set_iv( operation, iv, iv_length ) );
599}
600
601psa_status_t mbedtls_psa_cipher_update( mbedtls_psa_cipher_operation_t *operation,
602 const uint8_t *input,
603 size_t input_length,
604 uint8_t *output,
605 size_t output_size,
606 size_t *output_length )
607{
608 return( cipher_update( operation, input, input_length,
609 output, output_size, output_length ) );
610}
611
612psa_status_t mbedtls_psa_cipher_finish( mbedtls_psa_cipher_operation_t *operation,
613 uint8_t *output,
614 size_t output_size,
615 size_t *output_length )
616{
617 return( cipher_finish( operation, output, output_size, output_length ) );
618}
619
620psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation )
621{
622 return( cipher_abort( operation ) );
623}
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100624
625psa_status_t mbedtls_psa_cipher_encrypt( const psa_key_attributes_t *attributes,
626 const uint8_t *key_buffer,
627 size_t key_buffer_size,
628 psa_algorithm_t alg,
629 const uint8_t *input,
630 size_t input_length,
631 uint8_t *output,
632 size_t output_size,
633 size_t *output_length )
634{
635 return( cipher_encrypt( attributes, key_buffer, key_buffer_size,
636 alg, input, input_length,
637 output, output_size, output_length ) );
638}
639
640psa_status_t mbedtls_psa_cipher_decrypt( const psa_key_attributes_t *attributes,
641 const uint8_t *key_buffer,
642 size_t key_buffer_size,
643 psa_algorithm_t alg,
644 const uint8_t *input,
645 size_t input_length,
646 uint8_t *output,
647 size_t output_size,
648 size_t *output_length )
649{
650 return( cipher_decrypt( attributes, key_buffer, key_buffer_size,
651 alg, input, input_length,
652 output, output_size, output_length ) );
653}
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100654#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron8287e6b2021-03-12 10:35:18 +0100655
Ronald Cron3522e322021-03-12 11:08:49 +0100656/*
657 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
658 */
659
660#if defined(PSA_CRYPTO_DRIVER_TEST)
661psa_status_t mbedtls_transparent_test_driver_cipher_encrypt_setup(
662 mbedtls_psa_cipher_operation_t *operation,
663 const psa_key_attributes_t *attributes,
664 const uint8_t *key_buffer, size_t key_buffer_size,
665 psa_algorithm_t alg )
666{
667 return( cipher_encrypt_setup(
668 operation, attributes, key_buffer, key_buffer_size, alg ) );
669}
670
671psa_status_t mbedtls_transparent_test_driver_cipher_decrypt_setup(
672 mbedtls_psa_cipher_operation_t *operation,
673 const psa_key_attributes_t *attributes,
674 const uint8_t *key_buffer, size_t key_buffer_size,
675 psa_algorithm_t alg )
676{
677 return( cipher_decrypt_setup(
678 operation, attributes, key_buffer, key_buffer_size, alg ) );
679}
680
Ronald Cron3522e322021-03-12 11:08:49 +0100681psa_status_t mbedtls_transparent_test_driver_cipher_set_iv(
682 mbedtls_psa_cipher_operation_t *operation,
683 const uint8_t *iv, size_t iv_length )
684{
685 return( cipher_set_iv( operation, iv, iv_length ) );
686}
687
688psa_status_t mbedtls_transparent_test_driver_cipher_update(
689 mbedtls_psa_cipher_operation_t *operation,
690 const uint8_t *input, size_t input_length,
691 uint8_t *output, size_t output_size, size_t *output_length )
692{
693 return( cipher_update( operation, input, input_length,
694 output, output_size, output_length ) );
695}
696
697psa_status_t mbedtls_transparent_test_driver_cipher_finish(
698 mbedtls_psa_cipher_operation_t *operation,
699 uint8_t *output, size_t output_size, size_t *output_length )
700{
701 return( cipher_finish( operation, output, output_size, output_length ) );
702}
703
704psa_status_t mbedtls_transparent_test_driver_cipher_abort(
705 mbedtls_psa_cipher_operation_t *operation )
706{
707 return( cipher_abort( operation ) );
708}
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100709
710psa_status_t mbedtls_transparent_test_driver_cipher_encrypt(
711 const psa_key_attributes_t *attributes,
712 const uint8_t *key_buffer,
713 size_t key_buffer_size,
714 psa_algorithm_t alg,
715 const uint8_t *input,
716 size_t input_length,
717 uint8_t *output,
718 size_t output_size,
719 size_t *output_length )
720{
721 return( cipher_encrypt( attributes, key_buffer, key_buffer_size,
722 alg, input, input_length,
723 output, output_size, output_length ) );
724}
725
726psa_status_t mbedtls_transparent_test_driver_cipher_decrypt(
727 const psa_key_attributes_t *attributes,
728 const uint8_t *key_buffer,
729 size_t key_buffer_size,
730 psa_algorithm_t alg,
731 const uint8_t *input,
732 size_t input_length,
733 uint8_t *output,
734 size_t output_size,
735 size_t *output_length )
736{
737 return( cipher_decrypt( attributes, key_buffer, key_buffer_size,
738 alg, input, input_length,
739 output, output_size, output_length ) );
740}
Ronald Cron3522e322021-03-12 11:08:49 +0100741#endif /* PSA_CRYPTO_DRIVER_TEST */
742
Ronald Cron0ff57952021-03-08 16:46:35 +0100743#endif /* MBEDTLS_PSA_CRYPTO_C */