blob: 607cd949b85d5fd25718e2543b6aa3dd72ec74fa [file] [log] [blame]
Steven Cooreman37941cb2020-07-28 18:49:51 +02001/*
2 * Test driver for cipher functions.
3 * Currently only supports multi-part operations using AES-CTR.
4 */
Steven Cooreman3ec40182020-09-02 16:27:46 +02005/* Copyright The Mbed TLS Contributors
Steven Cooreman37941cb2020-07-28 18:49:51 +02006 * 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.
Steven Cooreman37941cb2020-07-28 18:49:51 +020019 */
20
21#if !defined(MBEDTLS_CONFIG_FILE)
22#include "mbedtls/config.h"
23#else
24#include MBEDTLS_CONFIG_FILE
25#endif
26
27#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
28#include "psa/crypto.h"
Ronald Cron8d310ad2020-12-15 15:17:20 +010029#include "psa_crypto_cipher.h"
Steven Cooremanacb5a102020-09-08 14:06:57 +020030#include "psa_crypto_core.h"
Steven Cooreman37941cb2020-07-28 18:49:51 +020031#include "mbedtls/cipher.h"
32
Steven Cooremanacb5a102020-09-08 14:06:57 +020033#include "test/drivers/cipher.h"
Steven Cooreman37941cb2020-07-28 18:49:51 +020034
35#include "test/random.h"
36
37#include <string.h>
38
Steven Cooreman5240e8b2020-09-09 11:51:45 +020039/* Test driver implements AES-CTR only. Its default behaviour (when its return
40 * status is not overridden through the hooks) is to take care of all AES-CTR
41 * operations, and return PSA_ERROR_NOT_SUPPORTED for all others.
Steven Cooremanacb5a102020-09-08 14:06:57 +020042 * Set test_driver_cipher_hooks.forced_status to PSA_ERROR_NOT_SUPPORTED to use
Steven Cooreman5240e8b2020-09-09 11:51:45 +020043 * fallback even for AES-CTR. */
Steven Cooremanacb5a102020-09-08 14:06:57 +020044test_driver_cipher_hooks_t test_driver_cipher_hooks = TEST_DRIVER_CIPHER_INIT;
Steven Cooreman37941cb2020-07-28 18:49:51 +020045
Steven Cooremanfe0ab552020-09-10 13:07:02 +020046static psa_status_t test_transparent_cipher_oneshot(
47 mbedtls_operation_t direction,
48 const psa_key_attributes_t *attributes,
49 const uint8_t *key, size_t key_length,
50 psa_algorithm_t alg,
51 const uint8_t *input, size_t input_length,
52 uint8_t *output, size_t output_size, size_t *output_length)
53{
54 test_driver_cipher_hooks.hits++;
55
56 /* Test driver supports AES-CTR only, to verify operation calls. */
57 if( alg != PSA_ALG_CTR ||
58 psa_get_key_type( attributes ) != PSA_KEY_TYPE_AES )
59 return( PSA_ERROR_NOT_SUPPORTED );
60
61 /* If test driver response code is not SUCCESS, we can return early */
62 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
63 return( test_driver_cipher_hooks.forced_status );
64
65 /* If test driver output is overridden, we don't need to do actual crypto */
66 if( test_driver_cipher_hooks.forced_output != NULL )
67 {
68 if( output_size < test_driver_cipher_hooks.forced_output_length )
69 return( PSA_ERROR_BUFFER_TOO_SMALL );
70
71 memcpy( output,
72 test_driver_cipher_hooks.forced_output,
73 test_driver_cipher_hooks.forced_output_length );
74 *output_length = test_driver_cipher_hooks.forced_output_length;
75
76 return( test_driver_cipher_hooks.forced_status );
77 }
78
79 /* Run AES-CTR using the cipher module */
80 {
81 mbedtls_test_rnd_pseudo_info rnd_info;
82 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
83
84 const mbedtls_cipher_info_t *cipher_info =
85 mbedtls_cipher_info_from_values( MBEDTLS_CIPHER_ID_AES,
86 key_length * 8,
87 MBEDTLS_MODE_CTR );
88 mbedtls_cipher_context_t cipher;
89 int ret = 0;
90 uint8_t temp_output_buffer[16] = {0};
91 size_t temp_output_length = 0;
92
93 if( direction == MBEDTLS_ENCRYPT )
94 {
95 /* Oneshot encrypt needs to prepend the IV to the output */
96 if( output_size < ( input_length + 16 ) )
97 return( PSA_ERROR_BUFFER_TOO_SMALL );
98 }
99 else
100 {
101 /* Oneshot decrypt has the IV prepended to the input */
102 if( output_size < ( input_length - 16 ) )
103 return( PSA_ERROR_BUFFER_TOO_SMALL );
104 }
105
106 if( cipher_info == NULL )
107 return( PSA_ERROR_NOT_SUPPORTED );
108
109 mbedtls_cipher_init( &cipher );
110 ret = mbedtls_cipher_setup( &cipher, cipher_info );
111 if( ret != 0 )
112 goto exit;
113
114 ret = mbedtls_cipher_setkey( &cipher,
115 key,
116 key_length * 8, direction );
117 if( ret != 0 )
118 goto exit;
119
120 if( direction == MBEDTLS_ENCRYPT )
121 {
122 mbedtls_test_rnd_pseudo_info rnd_info;
123 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
124
125 ret = mbedtls_test_rnd_pseudo_rand( &rnd_info,
126 temp_output_buffer,
127 16 );
128 if( ret != 0 )
129 goto exit;
130
131 ret = mbedtls_cipher_set_iv( &cipher, temp_output_buffer, 16 );
132 }
133 else
134 ret = mbedtls_cipher_set_iv( &cipher, input, 16 );
135
136 if( ret != 0 )
137 goto exit;
138
139 if( direction == MBEDTLS_ENCRYPT )
140 {
141 ret = mbedtls_cipher_update( &cipher,
142 input, input_length,
143 &output[16], output_length );
144 if( ret == 0 )
145 {
146 memcpy( output, temp_output_buffer, 16 );
147 *output_length += 16;
148 }
149 }
150 else
151 ret = mbedtls_cipher_update( &cipher,
152 &input[16], input_length - 16,
153 output, output_length );
154
155 if( ret != 0 )
156 goto exit;
157
158 ret = mbedtls_cipher_finish( &cipher,
159 temp_output_buffer,
160 &temp_output_length );
161
162exit:
163 if( ret != 0 )
164 {
165 *output_length = 0;
166 memset(output, 0, output_size);
167 }
168
169 mbedtls_cipher_free( &cipher );
170 return( mbedtls_to_psa_error( ret ) );
171 }
172}
173
Steven Cooreman37941cb2020-07-28 18:49:51 +0200174psa_status_t test_transparent_cipher_encrypt(
175 const psa_key_attributes_t *attributes,
176 const uint8_t *key, size_t key_length,
177 psa_algorithm_t alg,
178 const uint8_t *input, size_t input_length,
179 uint8_t *output, size_t output_size, size_t *output_length)
180{
Steven Cooremanfe0ab552020-09-10 13:07:02 +0200181 return (
182 test_transparent_cipher_oneshot(
183 MBEDTLS_ENCRYPT,
184 attributes,
185 key, key_length,
186 alg,
187 input, input_length,
188 output, output_size, output_length) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200189}
190
191psa_status_t test_transparent_cipher_decrypt(
192 const psa_key_attributes_t *attributes,
193 const uint8_t *key, size_t key_length,
194 psa_algorithm_t alg,
195 const uint8_t *input, size_t input_length,
196 uint8_t *output, size_t output_size, size_t *output_length)
197{
Steven Cooremanfe0ab552020-09-10 13:07:02 +0200198 return (
199 test_transparent_cipher_oneshot(
200 MBEDTLS_DECRYPT,
201 attributes,
202 key, key_length,
203 alg,
204 input, input_length,
205 output, output_size, output_length) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200206}
207
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200208psa_status_t test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100209 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200210 const psa_key_attributes_t *attributes,
211 const uint8_t *key, size_t key_length,
212 psa_algorithm_t alg)
213{
Ronald Cron8d310ad2020-12-15 15:17:20 +0100214 test_driver_cipher_hooks.hits++;
215
216 /* Wiping the entire struct here, instead of member-by-member. This is
217 * useful for the test suite, since it gives a chance of catching memory
218 * corruption errors should the core not have allocated (enough) memory for
219 * our context struct. */
220 memset( operation, 0, sizeof( *operation ) );
221
222 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
223 return( test_driver_cipher_hooks.forced_status );
224
225 return ( mbedtls_psa_cipher_encrypt_setup( operation,
226 attributes,
227 key,
228 key_length,
229 alg ) );
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200230}
231
Steven Cooreman37941cb2020-07-28 18:49:51 +0200232psa_status_t test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100233 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200234 const psa_key_attributes_t *attributes,
235 const uint8_t *key, size_t key_length,
236 psa_algorithm_t alg)
237{
Ronald Cron8d310ad2020-12-15 15:17:20 +0100238 test_driver_cipher_hooks.hits++;
239
240 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
241 return( test_driver_cipher_hooks.forced_status );
242
243 return ( mbedtls_psa_cipher_decrypt_setup( operation,
244 attributes,
245 key,
246 key_length,
247 alg ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200248}
249
250psa_status_t test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100251 mbedtls_transparent_test_driver_cipher_operation_t *operation)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200252{
Steven Cooreman89e54f22020-09-10 18:07:57 +0200253 test_driver_cipher_hooks.hits++;
254
Steven Cooreman8b122252020-09-03 15:30:32 +0200255 if( operation->alg == 0 )
256 return( PSA_SUCCESS );
Steven Cooreman8b122252020-09-03 15:30:32 +0200257
Ronald Cron8d310ad2020-12-15 15:17:20 +0100258 mbedtls_psa_cipher_abort( operation );
Steven Cooreman8b122252020-09-03 15:30:32 +0200259
Ronald Cron8d310ad2020-12-15 15:17:20 +0100260 /* Wiping the entire struct here, instead of member-by-member. This is
261 * useful for the test suite, since it gives a chance of catching memory
262 * corruption errors should the core not have allocated (enough) memory for
263 * our context struct. */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200264 memset( operation, 0, sizeof( *operation ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200265
Ronald Cron8d310ad2020-12-15 15:17:20 +0100266 return( test_driver_cipher_hooks.forced_status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200267}
268
269psa_status_t test_transparent_cipher_generate_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100270 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200271 uint8_t *iv,
272 size_t iv_size,
273 size_t *iv_length)
274{
Steven Cooremanacb5a102020-09-08 14:06:57 +0200275 test_driver_cipher_hooks.hits++;
Steven Cooreman8b122252020-09-03 15:30:32 +0200276
Steven Cooreman89e54f22020-09-10 18:07:57 +0200277 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
278 return( test_driver_cipher_hooks.forced_status );
279
Ronald Cron8d310ad2020-12-15 15:17:20 +0100280 return( mbedtls_psa_cipher_generate_iv( operation,
281 iv,
282 iv_size,
283 iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200284}
285
286psa_status_t test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100287 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200288 const uint8_t *iv,
289 size_t iv_length)
290{
Steven Cooremanacb5a102020-09-08 14:06:57 +0200291 test_driver_cipher_hooks.hits++;
Steven Cooreman8b122252020-09-03 15:30:32 +0200292
Steven Cooreman89e54f22020-09-10 18:07:57 +0200293 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
294 return( test_driver_cipher_hooks.forced_status );
295
Ronald Cron8d310ad2020-12-15 15:17:20 +0100296 return( mbedtls_psa_cipher_set_iv( operation,
297 iv,
298 iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200299}
300
301psa_status_t test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100302 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200303 const uint8_t *input,
304 size_t input_length,
305 uint8_t *output,
306 size_t output_size,
307 size_t *output_length)
308{
Steven Cooremanacb5a102020-09-08 14:06:57 +0200309 test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200310
Steven Cooremanacb5a102020-09-08 14:06:57 +0200311 if( test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200312 {
Steven Cooremanacb5a102020-09-08 14:06:57 +0200313 if( output_size < test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200314 return PSA_ERROR_BUFFER_TOO_SMALL;
315
Steven Cooremanacb5a102020-09-08 14:06:57 +0200316 memcpy( output,
317 test_driver_cipher_hooks.forced_output,
318 test_driver_cipher_hooks.forced_output_length );
319 *output_length = test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100320
321 return( test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200322 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200323
Ronald Cron8d310ad2020-12-15 15:17:20 +0100324 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
325 return( test_driver_cipher_hooks.forced_status );
326
327 return( mbedtls_psa_cipher_update( operation,
328 input, input_length,
329 output, output_size,
330 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200331}
332
333psa_status_t test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100334 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200335 uint8_t *output,
336 size_t output_size,
337 size_t *output_length)
338{
Steven Cooremanacb5a102020-09-08 14:06:57 +0200339 test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200340
Steven Cooremanacb5a102020-09-08 14:06:57 +0200341 if( test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200342 {
Steven Cooremanacb5a102020-09-08 14:06:57 +0200343 if( output_size < test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200344 return PSA_ERROR_BUFFER_TOO_SMALL;
345
Steven Cooremanacb5a102020-09-08 14:06:57 +0200346 memcpy( output,
347 test_driver_cipher_hooks.forced_output,
348 test_driver_cipher_hooks.forced_output_length );
349 *output_length = test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100350
351 return( test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200352 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200353
Ronald Cron8d310ad2020-12-15 15:17:20 +0100354 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
355 return( test_driver_cipher_hooks.forced_status );
356
357 return( mbedtls_psa_cipher_finish( operation,
358 output, output_size,
359 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200360}
361
362/*
363 * opaque versions, to do
364 */
365psa_status_t test_opaque_cipher_encrypt(
366 const psa_key_attributes_t *attributes,
367 const uint8_t *key, size_t key_length,
368 psa_algorithm_t alg,
369 const uint8_t *input, size_t input_length,
370 uint8_t *output, size_t output_size, size_t *output_length)
371{
372 (void) attributes;
373 (void) key;
374 (void) key_length;
375 (void) alg;
376 (void) input;
377 (void) input_length;
378 (void) output;
379 (void) output_size;
380 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200381 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200382}
383
384psa_status_t test_opaque_cipher_decrypt(
385 const psa_key_attributes_t *attributes,
386 const uint8_t *key, size_t key_length,
387 psa_algorithm_t alg,
388 const uint8_t *input, size_t input_length,
389 uint8_t *output, size_t output_size, size_t *output_length)
390{
391 (void) attributes;
392 (void) key;
393 (void) key_length;
394 (void) alg;
395 (void) input;
396 (void) input_length;
397 (void) output;
398 (void) output_size;
399 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200400 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200401}
402
403psa_status_t test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100404 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200405 const psa_key_attributes_t *attributes,
406 const uint8_t *key, size_t key_length,
407 psa_algorithm_t alg)
408{
409 (void) operation;
410 (void) attributes;
411 (void) key;
412 (void) key_length;
413 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200414 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200415}
416
417psa_status_t test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100418 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200419 const psa_key_attributes_t *attributes,
420 const uint8_t *key, size_t key_length,
421 psa_algorithm_t alg)
422{
423 (void) operation;
424 (void) attributes;
425 (void) key;
426 (void) key_length;
427 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200428 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200429}
430
431psa_status_t test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100432 mbedtls_opaque_test_driver_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200433{
434 (void) operation;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200435 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200436}
437
438psa_status_t test_opaque_cipher_generate_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100439 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200440 uint8_t *iv,
441 size_t iv_size,
442 size_t *iv_length)
443{
444 (void) operation;
445 (void) iv;
446 (void) iv_size;
447 (void) iv_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200448 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200449}
450
451psa_status_t test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100452 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200453 const uint8_t *iv,
454 size_t iv_length)
455{
456 (void) operation;
457 (void) iv;
458 (void) iv_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200459 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200460}
461
462psa_status_t test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100463 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200464 const uint8_t *input,
465 size_t input_length,
466 uint8_t *output,
467 size_t output_size,
468 size_t *output_length)
469{
470 (void) operation;
471 (void) input;
472 (void) input_length;
473 (void) output;
474 (void) output_size;
475 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200476 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200477}
478
479psa_status_t test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100480 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200481 uint8_t *output,
482 size_t output_size,
483 size_t *output_length)
484{
485 (void) operation;
486 (void) output;
487 (void) output_size;
488 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200489 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200490}
491#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */