blob: e241ba446ee878b87ffb430bb6a36018d91cfa46 [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 Cooremanacb5a102020-09-08 14:06:57 +020039test_driver_cipher_hooks_t test_driver_cipher_hooks = TEST_DRIVER_CIPHER_INIT;
Steven Cooreman37941cb2020-07-28 18:49:51 +020040
Steven Cooremanfe0ab552020-09-10 13:07:02 +020041static psa_status_t test_transparent_cipher_oneshot(
42 mbedtls_operation_t direction,
43 const psa_key_attributes_t *attributes,
44 const uint8_t *key, size_t key_length,
45 psa_algorithm_t alg,
46 const uint8_t *input, size_t input_length,
47 uint8_t *output, size_t output_size, size_t *output_length)
48{
49 test_driver_cipher_hooks.hits++;
50
51 /* Test driver supports AES-CTR only, to verify operation calls. */
52 if( alg != PSA_ALG_CTR ||
53 psa_get_key_type( attributes ) != PSA_KEY_TYPE_AES )
54 return( PSA_ERROR_NOT_SUPPORTED );
55
56 /* If test driver response code is not SUCCESS, we can return early */
57 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
58 return( test_driver_cipher_hooks.forced_status );
59
60 /* If test driver output is overridden, we don't need to do actual crypto */
61 if( test_driver_cipher_hooks.forced_output != NULL )
62 {
63 if( output_size < test_driver_cipher_hooks.forced_output_length )
64 return( PSA_ERROR_BUFFER_TOO_SMALL );
65
66 memcpy( output,
67 test_driver_cipher_hooks.forced_output,
68 test_driver_cipher_hooks.forced_output_length );
69 *output_length = test_driver_cipher_hooks.forced_output_length;
70
71 return( test_driver_cipher_hooks.forced_status );
72 }
73
74 /* Run AES-CTR using the cipher module */
75 {
76 mbedtls_test_rnd_pseudo_info rnd_info;
77 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
78
79 const mbedtls_cipher_info_t *cipher_info =
80 mbedtls_cipher_info_from_values( MBEDTLS_CIPHER_ID_AES,
81 key_length * 8,
82 MBEDTLS_MODE_CTR );
83 mbedtls_cipher_context_t cipher;
84 int ret = 0;
85 uint8_t temp_output_buffer[16] = {0};
86 size_t temp_output_length = 0;
87
88 if( direction == MBEDTLS_ENCRYPT )
89 {
90 /* Oneshot encrypt needs to prepend the IV to the output */
91 if( output_size < ( input_length + 16 ) )
92 return( PSA_ERROR_BUFFER_TOO_SMALL );
93 }
94 else
95 {
96 /* Oneshot decrypt has the IV prepended to the input */
97 if( output_size < ( input_length - 16 ) )
98 return( PSA_ERROR_BUFFER_TOO_SMALL );
99 }
100
101 if( cipher_info == NULL )
102 return( PSA_ERROR_NOT_SUPPORTED );
103
104 mbedtls_cipher_init( &cipher );
105 ret = mbedtls_cipher_setup( &cipher, cipher_info );
106 if( ret != 0 )
107 goto exit;
108
109 ret = mbedtls_cipher_setkey( &cipher,
110 key,
111 key_length * 8, direction );
112 if( ret != 0 )
113 goto exit;
114
115 if( direction == MBEDTLS_ENCRYPT )
116 {
117 mbedtls_test_rnd_pseudo_info rnd_info;
118 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
119
120 ret = mbedtls_test_rnd_pseudo_rand( &rnd_info,
121 temp_output_buffer,
122 16 );
123 if( ret != 0 )
124 goto exit;
125
126 ret = mbedtls_cipher_set_iv( &cipher, temp_output_buffer, 16 );
127 }
128 else
129 ret = mbedtls_cipher_set_iv( &cipher, input, 16 );
130
131 if( ret != 0 )
132 goto exit;
133
134 if( direction == MBEDTLS_ENCRYPT )
135 {
136 ret = mbedtls_cipher_update( &cipher,
137 input, input_length,
138 &output[16], output_length );
139 if( ret == 0 )
140 {
141 memcpy( output, temp_output_buffer, 16 );
142 *output_length += 16;
143 }
144 }
145 else
146 ret = mbedtls_cipher_update( &cipher,
147 &input[16], input_length - 16,
148 output, output_length );
149
150 if( ret != 0 )
151 goto exit;
152
153 ret = mbedtls_cipher_finish( &cipher,
154 temp_output_buffer,
155 &temp_output_length );
156
157exit:
158 if( ret != 0 )
159 {
160 *output_length = 0;
161 memset(output, 0, output_size);
162 }
163
164 mbedtls_cipher_free( &cipher );
165 return( mbedtls_to_psa_error( ret ) );
166 }
167}
168
Steven Cooreman37941cb2020-07-28 18:49:51 +0200169psa_status_t test_transparent_cipher_encrypt(
170 const psa_key_attributes_t *attributes,
171 const uint8_t *key, size_t key_length,
172 psa_algorithm_t alg,
173 const uint8_t *input, size_t input_length,
174 uint8_t *output, size_t output_size, size_t *output_length)
175{
Steven Cooremanfe0ab552020-09-10 13:07:02 +0200176 return (
177 test_transparent_cipher_oneshot(
178 MBEDTLS_ENCRYPT,
179 attributes,
180 key, key_length,
181 alg,
182 input, input_length,
183 output, output_size, output_length) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200184}
185
186psa_status_t test_transparent_cipher_decrypt(
187 const psa_key_attributes_t *attributes,
188 const uint8_t *key, size_t key_length,
189 psa_algorithm_t alg,
190 const uint8_t *input, size_t input_length,
191 uint8_t *output, size_t output_size, size_t *output_length)
192{
Steven Cooremanfe0ab552020-09-10 13:07:02 +0200193 return (
194 test_transparent_cipher_oneshot(
195 MBEDTLS_DECRYPT,
196 attributes,
197 key, key_length,
198 alg,
199 input, input_length,
200 output, output_size, output_length) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200201}
202
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200203psa_status_t test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100204 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200205 const psa_key_attributes_t *attributes,
206 const uint8_t *key, size_t key_length,
207 psa_algorithm_t alg)
208{
Ronald Cron8d310ad2020-12-15 15:17:20 +0100209 test_driver_cipher_hooks.hits++;
210
211 /* Wiping the entire struct here, instead of member-by-member. This is
212 * useful for the test suite, since it gives a chance of catching memory
213 * corruption errors should the core not have allocated (enough) memory for
214 * our context struct. */
215 memset( operation, 0, sizeof( *operation ) );
216
217 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
218 return( test_driver_cipher_hooks.forced_status );
219
Ronald Cron3522e322021-03-12 11:08:49 +0100220 return ( mbedtls_transparent_test_driver_cipher_encrypt_setup(
221 operation, attributes, key, key_length, alg ) );
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200222}
223
Steven Cooreman37941cb2020-07-28 18:49:51 +0200224psa_status_t test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100225 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200226 const psa_key_attributes_t *attributes,
227 const uint8_t *key, size_t key_length,
228 psa_algorithm_t alg)
229{
Ronald Cron8d310ad2020-12-15 15:17:20 +0100230 test_driver_cipher_hooks.hits++;
231
232 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
233 return( test_driver_cipher_hooks.forced_status );
234
Ronald Cron3522e322021-03-12 11:08:49 +0100235 return ( mbedtls_transparent_test_driver_cipher_decrypt_setup(
236 operation, attributes, key, key_length, alg ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200237}
238
239psa_status_t test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100240 mbedtls_transparent_test_driver_cipher_operation_t *operation)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200241{
Steven Cooreman89e54f22020-09-10 18:07:57 +0200242 test_driver_cipher_hooks.hits++;
243
Steven Cooreman8b122252020-09-03 15:30:32 +0200244 if( operation->alg == 0 )
245 return( PSA_SUCCESS );
Steven Cooreman8b122252020-09-03 15:30:32 +0200246
Ronald Cron3522e322021-03-12 11:08:49 +0100247 mbedtls_transparent_test_driver_cipher_abort( operation );
Steven Cooreman8b122252020-09-03 15:30:32 +0200248
Ronald Cron8d310ad2020-12-15 15:17:20 +0100249 /* Wiping the entire struct here, instead of member-by-member. This is
250 * useful for the test suite, since it gives a chance of catching memory
251 * corruption errors should the core not have allocated (enough) memory for
252 * our context struct. */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200253 memset( operation, 0, sizeof( *operation ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200254
Ronald Cron8d310ad2020-12-15 15:17:20 +0100255 return( test_driver_cipher_hooks.forced_status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200256}
257
Steven Cooreman37941cb2020-07-28 18:49:51 +0200258psa_status_t test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100259 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200260 const uint8_t *iv,
261 size_t iv_length)
262{
Steven Cooremanacb5a102020-09-08 14:06:57 +0200263 test_driver_cipher_hooks.hits++;
Steven Cooreman8b122252020-09-03 15:30:32 +0200264
Steven Cooreman89e54f22020-09-10 18:07:57 +0200265 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
266 return( test_driver_cipher_hooks.forced_status );
267
Ronald Cron3522e322021-03-12 11:08:49 +0100268 return( mbedtls_transparent_test_driver_cipher_set_iv(
269 operation, iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200270}
271
272psa_status_t test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100273 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200274 const uint8_t *input,
275 size_t input_length,
276 uint8_t *output,
277 size_t output_size,
278 size_t *output_length)
279{
Steven Cooremanacb5a102020-09-08 14:06:57 +0200280 test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200281
Steven Cooremanacb5a102020-09-08 14:06:57 +0200282 if( test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200283 {
Steven Cooremanacb5a102020-09-08 14:06:57 +0200284 if( output_size < test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200285 return PSA_ERROR_BUFFER_TOO_SMALL;
286
Steven Cooremanacb5a102020-09-08 14:06:57 +0200287 memcpy( output,
288 test_driver_cipher_hooks.forced_output,
289 test_driver_cipher_hooks.forced_output_length );
290 *output_length = test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100291
292 return( test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200293 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200294
Ronald Cron8d310ad2020-12-15 15:17:20 +0100295 if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
296 return( test_driver_cipher_hooks.forced_status );
297
Ronald Cron3522e322021-03-12 11:08:49 +0100298 return( mbedtls_transparent_test_driver_cipher_update(
299 operation, input, input_length,
300 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200301}
302
303psa_status_t test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100304 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200305 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
Ronald Cron3522e322021-03-12 11:08:49 +0100327 return( mbedtls_transparent_test_driver_cipher_finish(
328 operation, output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200329}
330
331/*
332 * opaque versions, to do
333 */
334psa_status_t test_opaque_cipher_encrypt(
335 const psa_key_attributes_t *attributes,
336 const uint8_t *key, size_t key_length,
337 psa_algorithm_t alg,
338 const uint8_t *input, size_t input_length,
339 uint8_t *output, size_t output_size, size_t *output_length)
340{
341 (void) attributes;
342 (void) key;
343 (void) key_length;
344 (void) alg;
345 (void) input;
346 (void) input_length;
347 (void) output;
348 (void) output_size;
349 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200350 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200351}
352
353psa_status_t test_opaque_cipher_decrypt(
354 const psa_key_attributes_t *attributes,
355 const uint8_t *key, size_t key_length,
356 psa_algorithm_t alg,
357 const uint8_t *input, size_t input_length,
358 uint8_t *output, size_t output_size, size_t *output_length)
359{
360 (void) attributes;
361 (void) key;
362 (void) key_length;
363 (void) alg;
364 (void) input;
365 (void) input_length;
366 (void) output;
367 (void) output_size;
368 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200369 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200370}
371
372psa_status_t test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100373 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200374 const psa_key_attributes_t *attributes,
375 const uint8_t *key, size_t key_length,
376 psa_algorithm_t alg)
377{
378 (void) operation;
379 (void) attributes;
380 (void) key;
381 (void) key_length;
382 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200383 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200384}
385
386psa_status_t test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100387 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200388 const psa_key_attributes_t *attributes,
389 const uint8_t *key, size_t key_length,
390 psa_algorithm_t alg)
391{
392 (void) operation;
393 (void) attributes;
394 (void) key;
395 (void) key_length;
396 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200397 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200398}
399
400psa_status_t test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100401 mbedtls_opaque_test_driver_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200402{
403 (void) operation;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200404 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200405}
406
Steven Cooreman37941cb2020-07-28 18:49:51 +0200407psa_status_t test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100408 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200409 const uint8_t *iv,
410 size_t iv_length)
411{
412 (void) operation;
413 (void) iv;
414 (void) iv_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200415 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200416}
417
418psa_status_t test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100419 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200420 const uint8_t *input,
421 size_t input_length,
422 uint8_t *output,
423 size_t output_size,
424 size_t *output_length)
425{
426 (void) operation;
427 (void) input;
428 (void) input_length;
429 (void) output;
430 (void) output_size;
431 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200432 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200433}
434
435psa_status_t test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100436 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200437 uint8_t *output,
438 size_t output_size,
439 size_t *output_length)
440{
441 (void) operation;
442 (void) output;
443 (void) output_size;
444 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200445 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200446}
447#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */