blob: 872e18f5f7f6cee969f24e97f870ca8b18a27a3d [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) || \
Ronald Cron73c9d9e2021-04-09 11:09:54 +020035 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
36 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Cron5d9b00d2021-03-10 14:43:20 +010037 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_DES) ) )
38#define BUILTIN_KEY_TYPE_DES 1
39#endif
40
41#if ( defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
Ronald Cron73c9d9e2021-04-09 11:09:54 +020042 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
43 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Cron5d9b00d2021-03-10 14:43:20 +010044 defined(MBEDTLS_PSA_ACCEL_ALG_CBC_NO_PADDING) ) )
45#define BUILTIN_ALG_CBC_NO_PADDING 1
46#endif
47
48#if ( defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) || \
Ronald Cron73c9d9e2021-04-09 11:09:54 +020049 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
50 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Cron5d9b00d2021-03-10 14:43:20 +010051 defined(MBEDTLS_PSA_ACCEL_ALG_CBC_PKCS7) ) )
52#define BUILTIN_ALG_CBC_PKCS7 1
53#endif
54
55#if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) || \
Ronald Cron73c9d9e2021-04-09 11:09:54 +020056 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
57 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Cron5d9b00d2021-03-10 14:43:20 +010058 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_CHACHA20) ) )
59#define BUILTIN_KEY_TYPE_CHACHA20 1
60#endif
61
Ronald Cron75e6ae22021-03-17 14:46:05 +010062const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
63 psa_algorithm_t alg,
64 psa_key_type_t key_type,
65 size_t key_bits,
66 mbedtls_cipher_id_t* cipher_id )
67{
68 mbedtls_cipher_mode_t mode;
69 mbedtls_cipher_id_t cipher_id_tmp;
70
71 if( PSA_ALG_IS_AEAD( alg ) )
72 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
73
74 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
75 {
76 switch( alg )
77 {
78 case PSA_ALG_STREAM_CIPHER:
79 mode = MBEDTLS_MODE_STREAM;
80 break;
81 case PSA_ALG_CTR:
82 mode = MBEDTLS_MODE_CTR;
83 break;
84 case PSA_ALG_CFB:
85 mode = MBEDTLS_MODE_CFB;
86 break;
87 case PSA_ALG_OFB:
88 mode = MBEDTLS_MODE_OFB;
89 break;
90 case PSA_ALG_ECB_NO_PADDING:
91 mode = MBEDTLS_MODE_ECB;
92 break;
93 case PSA_ALG_CBC_NO_PADDING:
94 mode = MBEDTLS_MODE_CBC;
95 break;
96 case PSA_ALG_CBC_PKCS7:
97 mode = MBEDTLS_MODE_CBC;
98 break;
Mateusz Starzyk594215b2021-10-14 12:23:06 +020099 case PSA_ALG_CCM_STAR_NO_TAG:
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200100 mode = MBEDTLS_MODE_CCM_STAR_NO_TAG;
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200101 break;
Ronald Cron75e6ae22021-03-17 14:46:05 +0100102 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
103 mode = MBEDTLS_MODE_CCM;
104 break;
105 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
106 mode = MBEDTLS_MODE_GCM;
107 break;
108 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
109 mode = MBEDTLS_MODE_CHACHAPOLY;
110 break;
111 default:
112 return( NULL );
113 }
114 }
115 else if( alg == PSA_ALG_CMAC )
116 mode = MBEDTLS_MODE_ECB;
117 else
118 return( NULL );
119
120 switch( key_type )
121 {
122 case PSA_KEY_TYPE_AES:
123 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
124 break;
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200125 case PSA_KEY_TYPE_ARIA:
126 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARIA;
127 break;
Ronald Cron75e6ae22021-03-17 14:46:05 +0100128 case PSA_KEY_TYPE_DES:
129 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
130 * and 192 for three-key Triple-DES. */
131 if( key_bits == 64 )
132 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
133 else
134 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
135 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
136 * but two-key Triple-DES is functionally three-key Triple-DES
137 * with K1=K3, so that's how we present it to mbedtls. */
138 if( key_bits == 128 )
139 key_bits = 192;
140 break;
141 case PSA_KEY_TYPE_CAMELLIA:
142 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
143 break;
Ronald Cron75e6ae22021-03-17 14:46:05 +0100144 case PSA_KEY_TYPE_CHACHA20:
145 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
146 break;
147 default:
148 return( NULL );
149 }
150 if( cipher_id != NULL )
151 *cipher_id = cipher_id_tmp;
152
153 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
154 (int) key_bits, mode ) );
155}
156
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200157#if defined(MBEDTLS_PSA_BUILTIN_CIPHER) || \
158 ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_CRYPTO_CONFIG) )
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100159
Ronald Crond6d28882020-12-14 14:56:02 +0100160static psa_status_t cipher_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +0100161 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +0100162 const psa_key_attributes_t *attributes,
163 const uint8_t *key_buffer, size_t key_buffer_size,
164 psa_algorithm_t alg,
165 mbedtls_operation_t cipher_operation )
166{
167 int ret = 0;
168 size_t key_bits;
169 const mbedtls_cipher_info_t *cipher_info = NULL;
170 psa_key_type_t key_type = attributes->core.type;
171
172 (void)key_buffer_size;
173
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200174 mbedtls_cipher_init( &operation->ctx.cipher );
Ronald Crond6d28882020-12-14 14:56:02 +0100175
Ronald Cron6e412a72021-03-10 09:58:47 +0100176 operation->alg = alg;
Ronald Crond6d28882020-12-14 14:56:02 +0100177 key_bits = attributes->core.bits;
178 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
179 key_bits, NULL );
180 if( cipher_info == NULL )
181 return( PSA_ERROR_NOT_SUPPORTED );
182
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200183 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Ronald Crond6d28882020-12-14 14:56:02 +0100184 if( ret != 0 )
185 goto exit;
186
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100187#if defined(BUILTIN_KEY_TYPE_DES)
Ronald Crond6d28882020-12-14 14:56:02 +0100188 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
189 {
190 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
191 uint8_t keys[24];
192 memcpy( keys, key_buffer, 16 );
193 memcpy( keys + 16, key_buffer, 8 );
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200194 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100195 keys,
196 192, cipher_operation );
197 }
198 else
199#endif
200 {
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200201 ret = mbedtls_cipher_setkey( &operation->ctx.cipher, key_buffer,
Ronald Crond6d28882020-12-14 14:56:02 +0100202 (int) key_bits, cipher_operation );
203 }
204 if( ret != 0 )
205 goto exit;
206
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100207#if defined(BUILTIN_ALG_CBC_NO_PADDING) || \
208 defined(BUILTIN_ALG_CBC_PKCS7)
Ronald Crond6d28882020-12-14 14:56:02 +0100209 switch( alg )
210 {
211 case PSA_ALG_CBC_NO_PADDING:
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200212 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100213 MBEDTLS_PADDING_NONE );
214 break;
215 case PSA_ALG_CBC_PKCS7:
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200216 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
Ronald Crond6d28882020-12-14 14:56:02 +0100217 MBEDTLS_PADDING_PKCS7 );
218 break;
219 default:
220 /* The algorithm doesn't involve padding. */
221 ret = 0;
222 break;
223 }
224 if( ret != 0 )
225 goto exit;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100226#endif /* BUILTIN_ALG_CBC_NO_PADDING || BUILTIN_ALG_CBC_PKCS7 */
Ronald Crond6d28882020-12-14 14:56:02 +0100227
Ronald Cron6ad554c2021-03-26 09:29:09 +0100228 operation->block_length = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
229 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Ronald Cronc17e8a92021-03-19 14:12:26 +0100230 operation->iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg );
Ronald Crond6d28882020-12-14 14:56:02 +0100231
232exit:
233 return( mbedtls_to_psa_error( ret ) );
234}
235
Ronald Cron8287e6b2021-03-12 10:35:18 +0100236static psa_status_t cipher_encrypt_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_ENCRYPT ) );
245}
246
Ronald Cron8287e6b2021-03-12 10:35:18 +0100247static psa_status_t cipher_decrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +0100248 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +0100249 const psa_key_attributes_t *attributes,
250 const uint8_t *key_buffer, size_t key_buffer_size,
251 psa_algorithm_t alg )
252{
253 return( cipher_setup( operation, attributes,
254 key_buffer, key_buffer_size,
255 alg, MBEDTLS_DECRYPT ) );
256}
Ronald Cron6d051732020-10-01 14:10:20 +0200257
Ronald Cron8287e6b2021-03-12 10:35:18 +0100258static psa_status_t cipher_set_iv( mbedtls_psa_cipher_operation_t *operation,
259 const uint8_t *iv, size_t iv_length )
260{
Ronald Cron6ad554c2021-03-26 09:29:09 +0100261 if( iv_length != operation->iv_length )
Ronald Cron8287e6b2021-03-12 10:35:18 +0100262 return( PSA_ERROR_INVALID_ARGUMENT );
263
264 return( mbedtls_to_psa_error(
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200265 mbedtls_cipher_set_iv( &operation->ctx.cipher,
Ronald Cron8287e6b2021-03-12 10:35:18 +0100266 iv, iv_length ) ) );
267}
268
Gilles Peskine55dffe52021-09-13 09:33:28 +0200269/** Process input for which the algorithm is set to ECB mode.
270 *
271 * This requires manual processing, since the PSA API is defined as being
272 * able to process arbitrary-length calls to psa_cipher_update() with ECB mode,
273 * but the underlying mbedtls_cipher_update only takes full blocks.
274 *
275 * \param ctx The mbedtls cipher context to use. It must have been
276 * set up for ECB.
277 * \param[in] input The input plaintext or ciphertext to process.
278 * \param input_length The number of bytes to process from \p input.
279 * This does not need to be aligned to a block boundary.
280 * If there is a partial block at the end of the input,
281 * it is stored in \p ctx for future processing.
Gilles Peskined87d8732021-09-13 12:20:51 +0200282 * \param output The buffer where the output is written. It must be
283 * at least `BS * floor((p + input_length) / BS)` bytes
284 * long, where `p` is the number of bytes in the
285 * unprocessed partial block in \p ctx (with
286 * `0 <= p <= BS - 1`) and `BS` is the block size.
Gilles Peskine55dffe52021-09-13 09:33:28 +0200287 * \param output_length On success, the number of bytes written to \p output.
288 * \c 0 on error.
289 *
290 * \return #PSA_SUCCESS or an error from a hardware accelerator
291 */
Ronald Cron6d051732020-10-01 14:10:20 +0200292static psa_status_t psa_cipher_update_ecb(
293 mbedtls_cipher_context_t *ctx,
294 const uint8_t *input,
295 size_t input_length,
296 uint8_t *output,
Ronald Cron6d051732020-10-01 14:10:20 +0200297 size_t *output_length )
298{
299 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
300 size_t block_size = ctx->cipher_info->block_size;
301 size_t internal_output_length = 0;
302 *output_length = 0;
303
304 if( input_length == 0 )
305 {
306 status = PSA_SUCCESS;
307 goto exit;
308 }
309
310 if( ctx->unprocessed_len > 0 )
311 {
312 /* Fill up to block size, and run the block if there's a full one. */
313 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
314
315 if( input_length < bytes_to_copy )
316 bytes_to_copy = input_length;
317
318 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
319 input, bytes_to_copy );
320 input_length -= bytes_to_copy;
321 input += bytes_to_copy;
322 ctx->unprocessed_len += bytes_to_copy;
323
324 if( ctx->unprocessed_len == block_size )
325 {
326 status = mbedtls_to_psa_error(
327 mbedtls_cipher_update( ctx,
328 ctx->unprocessed_data,
329 block_size,
330 output, &internal_output_length ) );
331
332 if( status != PSA_SUCCESS )
333 goto exit;
334
335 output += internal_output_length;
Ronald Cron6d051732020-10-01 14:10:20 +0200336 *output_length += internal_output_length;
337 ctx->unprocessed_len = 0;
338 }
339 }
340
341 while( input_length >= block_size )
342 {
343 /* Run all full blocks we have, one by one */
344 status = mbedtls_to_psa_error(
345 mbedtls_cipher_update( ctx, input,
346 block_size,
347 output, &internal_output_length ) );
348
349 if( status != PSA_SUCCESS )
350 goto exit;
351
352 input_length -= block_size;
353 input += block_size;
354
355 output += internal_output_length;
Ronald Cron6d051732020-10-01 14:10:20 +0200356 *output_length += internal_output_length;
357 }
358
359 if( input_length > 0 )
360 {
361 /* Save unprocessed bytes for later processing */
362 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
363 input, input_length );
364 ctx->unprocessed_len += input_length;
365 }
366
367 status = PSA_SUCCESS;
368
369exit:
370 return( status );
371}
372
Ronald Cron8287e6b2021-03-12 10:35:18 +0100373static psa_status_t cipher_update( mbedtls_psa_cipher_operation_t *operation,
374 const uint8_t *input,
375 size_t input_length,
376 uint8_t *output,
377 size_t output_size,
378 size_t *output_length )
Ronald Cron6d051732020-10-01 14:10:20 +0200379{
380 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
381 size_t expected_output_size;
382
Ronald Cron6e412a72021-03-10 09:58:47 +0100383 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Ronald Cron6d051732020-10-01 14:10:20 +0200384 {
385 /* Take the unprocessed partial block left over from previous
386 * update calls, if any, plus the input to this call. Remove
387 * the last partial block, if any. You get the data that will be
388 * output in this call. */
389 expected_output_size =
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200390 ( operation->ctx.cipher.unprocessed_len + input_length )
Ronald Cron6ad554c2021-03-26 09:29:09 +0100391 / operation->block_length * operation->block_length;
Ronald Cron6d051732020-10-01 14:10:20 +0200392 }
393 else
394 {
395 expected_output_size = input_length;
396 }
397
398 if( output_size < expected_output_size )
399 return( PSA_ERROR_BUFFER_TOO_SMALL );
400
Ronald Cron6e412a72021-03-10 09:58:47 +0100401 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
Ronald Cron6d051732020-10-01 14:10:20 +0200402 {
403 /* mbedtls_cipher_update has an API inconsistency: it will only
404 * process a single block at a time in ECB mode. Abstract away that
405 * inconsistency here to match the PSA API behaviour. */
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200406 status = psa_cipher_update_ecb( &operation->ctx.cipher,
Ronald Cron6d051732020-10-01 14:10:20 +0200407 input,
408 input_length,
409 output,
Ronald Cron6d051732020-10-01 14:10:20 +0200410 output_length );
411 }
412 else
413 {
414 status = mbedtls_to_psa_error(
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200415 mbedtls_cipher_update( &operation->ctx.cipher, input,
Ronald Cron6d051732020-10-01 14:10:20 +0200416 input_length, output, output_length ) );
gabor-mezei-arm58c17272021-06-29 16:41:25 +0200417
418 if( *output_length > output_size )
gabor-mezei-arm00e54f12021-06-29 19:06:30 +0200419 return( PSA_ERROR_CORRUPTION_DETECTED );
Ronald Cron6d051732020-10-01 14:10:20 +0200420 }
421
422 return( status );
423}
424
Ronald Cron8287e6b2021-03-12 10:35:18 +0100425static psa_status_t cipher_finish( mbedtls_psa_cipher_operation_t *operation,
426 uint8_t *output,
427 size_t output_size,
428 size_t *output_length )
Ronald Cron6d051732020-10-01 14:10:20 +0200429{
430 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
431 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
432
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200433 if( operation->ctx.cipher.unprocessed_len != 0 )
Ronald Cron6d051732020-10-01 14:10:20 +0200434 {
Ronald Cron6e412a72021-03-10 09:58:47 +0100435 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
436 operation->alg == PSA_ALG_CBC_NO_PADDING )
Ronald Cron6d051732020-10-01 14:10:20 +0200437 {
438 status = PSA_ERROR_INVALID_ARGUMENT;
439 goto exit;
440 }
441 }
442
443 status = mbedtls_to_psa_error(
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200444 mbedtls_cipher_finish( &operation->ctx.cipher,
Ronald Cron6d051732020-10-01 14:10:20 +0200445 temp_output_buffer,
446 output_length ) );
447 if( status != PSA_SUCCESS )
448 goto exit;
449
450 if( *output_length == 0 )
451 ; /* Nothing to copy. Note that output may be NULL in this case. */
452 else if( output_size >= *output_length )
453 memcpy( output, temp_output_buffer, *output_length );
454 else
455 status = PSA_ERROR_BUFFER_TOO_SMALL;
456
457exit:
458 mbedtls_platform_zeroize( temp_output_buffer,
459 sizeof( temp_output_buffer ) );
460
461 return( status );
462}
463
Ronald Cron8287e6b2021-03-12 10:35:18 +0100464static psa_status_t cipher_abort( mbedtls_psa_cipher_operation_t *operation )
Ronald Cron6d051732020-10-01 14:10:20 +0200465{
Ronald Cron937dfee2021-03-10 09:17:32 +0100466 /* Sanity check (shouldn't happen: operation->alg should
467 * always have been initialized to a valid value). */
Ronald Cron6e412a72021-03-10 09:58:47 +0100468 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
Ronald Cron937dfee2021-03-10 09:17:32 +0100469 return( PSA_ERROR_BAD_STATE );
470
gabor-mezei-arm42cdb2a2021-04-12 15:47:35 +0200471 mbedtls_cipher_free( &operation->ctx.cipher );
Ronald Cron6d051732020-10-01 14:10:20 +0200472
473 return( PSA_SUCCESS );
474}
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100475
476static psa_status_t cipher_encrypt( const psa_key_attributes_t *attributes,
477 const uint8_t *key_buffer,
478 size_t key_buffer_size,
479 psa_algorithm_t alg,
480 const uint8_t *input,
481 size_t input_length,
482 uint8_t *output,
483 size_t output_size,
484 size_t *output_length )
485{
486 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
487 mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200488 size_t olength, accumulated_length;
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100489
gabor-mezei-arm47a8e142021-06-25 15:44:47 +0200490 status = cipher_encrypt_setup( &operation, attributes,
491 key_buffer, key_buffer_size, alg );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100492 if( status != PSA_SUCCESS )
493 goto exit;
494
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200495 accumulated_length = 0;
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100496 if( operation.iv_length > 0 )
497 {
498 status = cipher_set_iv( &operation, output, operation.iv_length );
499 if( status != PSA_SUCCESS )
500 goto exit;
501
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200502 accumulated_length = operation.iv_length;
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100503 }
504
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100505 status = cipher_update( &operation, input, input_length,
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200506 output + operation.iv_length,
507 output_size - operation.iv_length,
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100508 &olength );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100509 if( status != PSA_SUCCESS )
510 goto exit;
511
gabor-mezei-arm6158e282021-06-29 16:42:13 +0200512 accumulated_length += olength;
513
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200514 status = cipher_finish( &operation, output + accumulated_length,
515 output_size - accumulated_length, &olength );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100516 if( status != PSA_SUCCESS )
517 goto exit;
518
gabor-mezei-arm00e54f12021-06-29 19:06:30 +0200519 *output_length = accumulated_length + olength;
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200520
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100521exit:
522 if( status == PSA_SUCCESS )
523 status = cipher_abort( &operation );
524 else
525 cipher_abort( &operation );
526 return( status );
527}
528
529static psa_status_t cipher_decrypt( const psa_key_attributes_t *attributes,
530 const uint8_t *key_buffer,
531 size_t key_buffer_size,
532 psa_algorithm_t alg,
533 const uint8_t *input,
534 size_t input_length,
535 uint8_t *output,
536 size_t output_size,
537 size_t *output_length )
538{
539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
540 mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200541 size_t olength, accumulated_length;
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100542
543 status = cipher_decrypt_setup( &operation, attributes,
544 key_buffer, key_buffer_size, alg );
545 if( status != PSA_SUCCESS )
546 goto exit;
547
548 if( operation.iv_length > 0 )
549 {
550 status = cipher_set_iv( &operation, input, operation.iv_length );
551 if( status != PSA_SUCCESS )
552 goto exit;
553 }
554
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100555 status = cipher_update( &operation, input + operation.iv_length,
556 input_length - operation.iv_length,
557 output, output_size, &olength );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100558 if( status != PSA_SUCCESS )
559 goto exit;
560
gabor-mezei-arm6158e282021-06-29 16:42:13 +0200561 accumulated_length = olength;
gabor-mezei-arm258ae072021-06-25 15:25:38 +0200562
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200563 status = cipher_finish( &operation, output + accumulated_length,
564 output_size - accumulated_length, &olength );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100565 if( status != PSA_SUCCESS )
566 goto exit;
567
gabor-mezei-arm00e54f12021-06-29 19:06:30 +0200568 *output_length = accumulated_length + olength;
gabor-mezei-arme5ff8f42021-06-25 15:23:05 +0200569
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100570exit:
571 if ( status == PSA_SUCCESS )
572 status = cipher_abort( &operation );
573 else
574 cipher_abort( &operation );
575 return( status );
576}
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200577#endif /* MBEDTLS_PSA_BUILTIN_CIPHER ||
578 (PSA_CRYPTO_DRIVER_TEST && MBEDTLS_PSA_CRYPTO_CONFIG) */
Ronald Cron6d051732020-10-01 14:10:20 +0200579
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100580#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron8287e6b2021-03-12 10:35:18 +0100581psa_status_t mbedtls_psa_cipher_encrypt_setup(
582 mbedtls_psa_cipher_operation_t *operation,
583 const psa_key_attributes_t *attributes,
584 const uint8_t *key_buffer, size_t key_buffer_size,
585 psa_algorithm_t alg )
586{
587 return( cipher_encrypt_setup(
588 operation, attributes, key_buffer, key_buffer_size, alg ) );
589}
590
591psa_status_t mbedtls_psa_cipher_decrypt_setup(
592 mbedtls_psa_cipher_operation_t *operation,
593 const psa_key_attributes_t *attributes,
594 const uint8_t *key_buffer, size_t key_buffer_size,
595 psa_algorithm_t alg )
596{
597 return( cipher_decrypt_setup(
598 operation, attributes, key_buffer, key_buffer_size, alg ) );
599}
600
Ronald Cron8287e6b2021-03-12 10:35:18 +0100601psa_status_t mbedtls_psa_cipher_set_iv( mbedtls_psa_cipher_operation_t *operation,
602 const uint8_t *iv,
603 size_t iv_length )
604{
605 return( cipher_set_iv( operation, iv, iv_length ) );
606}
607
608psa_status_t mbedtls_psa_cipher_update( mbedtls_psa_cipher_operation_t *operation,
609 const uint8_t *input,
610 size_t input_length,
611 uint8_t *output,
612 size_t output_size,
613 size_t *output_length )
614{
615 return( cipher_update( operation, input, input_length,
616 output, output_size, output_length ) );
617}
618
619psa_status_t mbedtls_psa_cipher_finish( mbedtls_psa_cipher_operation_t *operation,
620 uint8_t *output,
621 size_t output_size,
622 size_t *output_length )
623{
624 return( cipher_finish( operation, output, output_size, output_length ) );
625}
626
627psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation )
628{
629 return( cipher_abort( operation ) );
630}
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100631
632psa_status_t mbedtls_psa_cipher_encrypt( const psa_key_attributes_t *attributes,
633 const uint8_t *key_buffer,
634 size_t key_buffer_size,
635 psa_algorithm_t alg,
636 const uint8_t *input,
637 size_t input_length,
638 uint8_t *output,
639 size_t output_size,
640 size_t *output_length )
641{
642 return( cipher_encrypt( attributes, key_buffer, key_buffer_size,
643 alg, input, input_length,
644 output, output_size, output_length ) );
645}
646
647psa_status_t mbedtls_psa_cipher_decrypt( const psa_key_attributes_t *attributes,
648 const uint8_t *key_buffer,
649 size_t key_buffer_size,
650 psa_algorithm_t alg,
651 const uint8_t *input,
652 size_t input_length,
653 uint8_t *output,
654 size_t output_size,
655 size_t *output_length )
656{
657 return( cipher_decrypt( attributes, key_buffer, key_buffer_size,
658 alg, input, input_length,
659 output, output_size, output_length ) );
660}
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100661#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron8287e6b2021-03-12 10:35:18 +0100662
Ronald Cron3522e322021-03-12 11:08:49 +0100663/*
664 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
665 */
666
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200667#if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cron40170d92021-03-13 18:19:08 +0100668psa_status_t libtestdriver1_mbedtls_psa_cipher_encrypt_setup(
Ronald Cron3522e322021-03-12 11:08:49 +0100669 mbedtls_psa_cipher_operation_t *operation,
670 const psa_key_attributes_t *attributes,
671 const uint8_t *key_buffer, size_t key_buffer_size,
672 psa_algorithm_t alg )
673{
674 return( cipher_encrypt_setup(
675 operation, attributes, key_buffer, key_buffer_size, alg ) );
676}
677
Ronald Cron40170d92021-03-13 18:19:08 +0100678psa_status_t libtestdriver1_mbedtls_psa_cipher_decrypt_setup(
Ronald Cron3522e322021-03-12 11:08:49 +0100679 mbedtls_psa_cipher_operation_t *operation,
680 const psa_key_attributes_t *attributes,
681 const uint8_t *key_buffer, size_t key_buffer_size,
682 psa_algorithm_t alg )
683{
684 return( cipher_decrypt_setup(
685 operation, attributes, key_buffer, key_buffer_size, alg ) );
686}
687
Ronald Cron40170d92021-03-13 18:19:08 +0100688psa_status_t libtestdriver1_mbedtls_psa_cipher_set_iv(
Ronald Cron3522e322021-03-12 11:08:49 +0100689 mbedtls_psa_cipher_operation_t *operation,
690 const uint8_t *iv, size_t iv_length )
691{
692 return( cipher_set_iv( operation, iv, iv_length ) );
693}
694
Ronald Cron40170d92021-03-13 18:19:08 +0100695psa_status_t libtestdriver1_mbedtls_psa_cipher_update(
Ronald Cron3522e322021-03-12 11:08:49 +0100696 mbedtls_psa_cipher_operation_t *operation,
697 const uint8_t *input, size_t input_length,
698 uint8_t *output, size_t output_size, size_t *output_length )
699{
700 return( cipher_update( operation, input, input_length,
701 output, output_size, output_length ) );
702}
703
Ronald Cron40170d92021-03-13 18:19:08 +0100704psa_status_t libtestdriver1_mbedtls_psa_cipher_finish(
Ronald Cron3522e322021-03-12 11:08:49 +0100705 mbedtls_psa_cipher_operation_t *operation,
706 uint8_t *output, size_t output_size, size_t *output_length )
707{
708 return( cipher_finish( operation, output, output_size, output_length ) );
709}
710
Ronald Cron40170d92021-03-13 18:19:08 +0100711psa_status_t libtestdriver1_mbedtls_psa_cipher_abort(
Ronald Cron3522e322021-03-12 11:08:49 +0100712 mbedtls_psa_cipher_operation_t *operation )
713{
714 return( cipher_abort( operation ) );
715}
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100716
Ronald Cron40170d92021-03-13 18:19:08 +0100717psa_status_t libtestdriver1_mbedtls_psa_cipher_encrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100718 const psa_key_attributes_t *attributes,
719 const uint8_t *key_buffer,
720 size_t key_buffer_size,
721 psa_algorithm_t alg,
722 const uint8_t *input,
723 size_t input_length,
724 uint8_t *output,
725 size_t output_size,
726 size_t *output_length )
727{
728 return( cipher_encrypt( attributes, key_buffer, key_buffer_size,
729 alg, input, input_length,
730 output, output_size, output_length ) );
731}
732
Ronald Cron40170d92021-03-13 18:19:08 +0100733psa_status_t libtestdriver1_mbedtls_psa_cipher_decrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100734 const psa_key_attributes_t *attributes,
735 const uint8_t *key_buffer,
736 size_t key_buffer_size,
737 psa_algorithm_t alg,
738 const uint8_t *input,
739 size_t input_length,
740 uint8_t *output,
741 size_t output_size,
742 size_t *output_length )
743{
744 return( cipher_decrypt( attributes, key_buffer, key_buffer_size,
745 alg, input, input_length,
746 output, output_size, output_length ) );
747}
Ronald Cron40170d92021-03-13 18:19:08 +0100748
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200749#endif /* PSA_CRYPTO_DRIVER_TEST && MBEDTLS_PSA_CRYPTO_CONFIG */
Ronald Cron3522e322021-03-12 11:08:49 +0100750
Ronald Cron0ff57952021-03-08 16:46:35 +0100751#endif /* MBEDTLS_PSA_CRYPTO_C */