blob: 4dc46789b250d4660288d705acb9ab5f2010bb78 [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
Ronald Cron3522e322021-03-12 11:08:49 +0100225 return ( mbedtls_transparent_test_driver_cipher_encrypt_setup(
226 operation, attributes, key, key_length, alg ) );
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200227}
228
Steven Cooreman37941cb2020-07-28 18:49:51 +0200229psa_status_t test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100230 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200231 const psa_key_attributes_t *attributes,
232 const uint8_t *key, size_t key_length,
233 psa_algorithm_t alg)
234{
Ronald Cron8d310ad2020-12-15 15:17:20 +0100235 test_driver_cipher_hooks.hits++;
236
237 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
238 return( test_driver_cipher_hooks.forced_status );
239
Ronald Cron3522e322021-03-12 11:08:49 +0100240 return ( mbedtls_transparent_test_driver_cipher_decrypt_setup(
241 operation, attributes, key, key_length, alg ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200242}
243
244psa_status_t test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100245 mbedtls_transparent_test_driver_cipher_operation_t *operation)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200246{
Steven Cooreman89e54f22020-09-10 18:07:57 +0200247 test_driver_cipher_hooks.hits++;
248
Steven Cooreman8b122252020-09-03 15:30:32 +0200249 if( operation->alg == 0 )
250 return( PSA_SUCCESS );
Steven Cooreman8b122252020-09-03 15:30:32 +0200251
Ronald Cron3522e322021-03-12 11:08:49 +0100252 mbedtls_transparent_test_driver_cipher_abort( operation );
Steven Cooreman8b122252020-09-03 15:30:32 +0200253
Ronald Cron8d310ad2020-12-15 15:17:20 +0100254 /* Wiping the entire struct here, instead of member-by-member. This is
255 * useful for the test suite, since it gives a chance of catching memory
256 * corruption errors should the core not have allocated (enough) memory for
257 * our context struct. */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200258 memset( operation, 0, sizeof( *operation ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200259
Ronald Cron8d310ad2020-12-15 15:17:20 +0100260 return( test_driver_cipher_hooks.forced_status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200261}
262
Steven Cooreman37941cb2020-07-28 18:49:51 +0200263psa_status_t test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100264 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200265 const uint8_t *iv,
266 size_t iv_length)
267{
Steven Cooremanacb5a102020-09-08 14:06:57 +0200268 test_driver_cipher_hooks.hits++;
Steven Cooreman8b122252020-09-03 15:30:32 +0200269
Steven Cooreman89e54f22020-09-10 18:07:57 +0200270 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
271 return( test_driver_cipher_hooks.forced_status );
272
Ronald Cron3522e322021-03-12 11:08:49 +0100273 return( mbedtls_transparent_test_driver_cipher_set_iv(
274 operation, iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200275}
276
277psa_status_t test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100278 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200279 const uint8_t *input,
280 size_t input_length,
281 uint8_t *output,
282 size_t output_size,
283 size_t *output_length)
284{
Steven Cooremanacb5a102020-09-08 14:06:57 +0200285 test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200286
Steven Cooremanacb5a102020-09-08 14:06:57 +0200287 if( test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200288 {
Steven Cooremanacb5a102020-09-08 14:06:57 +0200289 if( output_size < test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200290 return PSA_ERROR_BUFFER_TOO_SMALL;
291
Steven Cooremanacb5a102020-09-08 14:06:57 +0200292 memcpy( output,
293 test_driver_cipher_hooks.forced_output,
294 test_driver_cipher_hooks.forced_output_length );
295 *output_length = test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100296
297 return( test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200298 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200299
Ronald Cron8d310ad2020-12-15 15:17:20 +0100300 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
301 return( test_driver_cipher_hooks.forced_status );
302
Ronald Cron3522e322021-03-12 11:08:49 +0100303 return( mbedtls_transparent_test_driver_cipher_update(
304 operation, input, input_length,
305 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200306}
307
308psa_status_t test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100309 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200310 uint8_t *output,
311 size_t output_size,
312 size_t *output_length)
313{
Steven Cooremanacb5a102020-09-08 14:06:57 +0200314 test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200315
Steven Cooremanacb5a102020-09-08 14:06:57 +0200316 if( test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200317 {
Steven Cooremanacb5a102020-09-08 14:06:57 +0200318 if( output_size < test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200319 return PSA_ERROR_BUFFER_TOO_SMALL;
320
Steven Cooremanacb5a102020-09-08 14:06:57 +0200321 memcpy( output,
322 test_driver_cipher_hooks.forced_output,
323 test_driver_cipher_hooks.forced_output_length );
324 *output_length = test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100325
326 return( test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200327 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200328
Ronald Cron8d310ad2020-12-15 15:17:20 +0100329 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
330 return( test_driver_cipher_hooks.forced_status );
331
Ronald Cron3522e322021-03-12 11:08:49 +0100332 return( mbedtls_transparent_test_driver_cipher_finish(
333 operation, output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200334}
335
336/*
337 * opaque versions, to do
338 */
339psa_status_t test_opaque_cipher_encrypt(
340 const psa_key_attributes_t *attributes,
341 const uint8_t *key, size_t key_length,
342 psa_algorithm_t alg,
343 const uint8_t *input, size_t input_length,
344 uint8_t *output, size_t output_size, size_t *output_length)
345{
346 (void) attributes;
347 (void) key;
348 (void) key_length;
349 (void) alg;
350 (void) input;
351 (void) input_length;
352 (void) output;
353 (void) output_size;
354 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200355 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200356}
357
358psa_status_t test_opaque_cipher_decrypt(
359 const psa_key_attributes_t *attributes,
360 const uint8_t *key, size_t key_length,
361 psa_algorithm_t alg,
362 const uint8_t *input, size_t input_length,
363 uint8_t *output, size_t output_size, size_t *output_length)
364{
365 (void) attributes;
366 (void) key;
367 (void) key_length;
368 (void) alg;
369 (void) input;
370 (void) input_length;
371 (void) output;
372 (void) output_size;
373 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200374 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200375}
376
377psa_status_t test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100378 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200379 const psa_key_attributes_t *attributes,
380 const uint8_t *key, size_t key_length,
381 psa_algorithm_t alg)
382{
383 (void) operation;
384 (void) attributes;
385 (void) key;
386 (void) key_length;
387 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200388 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200389}
390
391psa_status_t test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100392 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200393 const psa_key_attributes_t *attributes,
394 const uint8_t *key, size_t key_length,
395 psa_algorithm_t alg)
396{
397 (void) operation;
398 (void) attributes;
399 (void) key;
400 (void) key_length;
401 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200402 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200403}
404
405psa_status_t test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100406 mbedtls_opaque_test_driver_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200407{
408 (void) operation;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200409 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200410}
411
Steven Cooreman37941cb2020-07-28 18:49:51 +0200412psa_status_t test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100413 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200414 const uint8_t *iv,
415 size_t iv_length)
416{
417 (void) operation;
418 (void) iv;
419 (void) iv_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200420 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200421}
422
423psa_status_t test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100424 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200425 const uint8_t *input,
426 size_t input_length,
427 uint8_t *output,
428 size_t output_size,
429 size_t *output_length)
430{
431 (void) operation;
432 (void) input;
433 (void) input_length;
434 (void) output;
435 (void) output_size;
436 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200437 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200438}
439
440psa_status_t test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100441 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200442 uint8_t *output,
443 size_t output_size,
444 size_t *output_length)
445{
446 (void) operation;
447 (void) output;
448 (void) output_size;
449 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200450 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200451}
452#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */