blob: 2fe77c8c78d795ff2115cdf3c9679bcd371db1b5 [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
Ronald Cronc4bc12e2021-04-13 12:41:34 +020039mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks =
40 MBEDTLS_TEST_DRIVER_CIPHER_INIT;
Steven Cooreman37941cb2020-07-28 18:49:51 +020041
gabor-mezei-armfa990b52021-03-25 11:17:10 +010042psa_status_t mbedtls_test_transparent_cipher_encrypt(
Steven Cooremanfe0ab552020-09-10 13:07:02 +020043 const psa_key_attributes_t *attributes,
gabor-mezei-armfa990b52021-03-25 11:17:10 +010044 const uint8_t *key_buffer,
45 size_t key_buffer_size,
Steven Cooremanfe0ab552020-09-10 13:07:02 +020046 psa_algorithm_t alg,
gabor-mezei-armfa990b52021-03-25 11:17:10 +010047 const uint8_t *input,
48 size_t input_length,
49 uint8_t *output,
50 size_t output_size,
51 size_t *output_length )
Steven Cooremanfe0ab552020-09-10 13:07:02 +020052{
Ronald Cronc4bc12e2021-04-13 12:41:34 +020053 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooremanfe0ab552020-09-10 13:07:02 +020054
Ronald Cronc4bc12e2021-04-13 12:41:34 +020055 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
Steven Cooremanfe0ab552020-09-10 13:07:02 +020056 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +020057 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
Steven Cooremanfe0ab552020-09-10 13:07:02 +020058 return( PSA_ERROR_BUFFER_TOO_SMALL );
59
60 memcpy( output,
Ronald Cronc4bc12e2021-04-13 12:41:34 +020061 mbedtls_test_driver_cipher_hooks.forced_output,
62 mbedtls_test_driver_cipher_hooks.forced_output_length );
63 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
Steven Cooremanfe0ab552020-09-10 13:07:02 +020064
Ronald Cronc4bc12e2021-04-13 12:41:34 +020065 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooremanfe0ab552020-09-10 13:07:02 +020066 }
67
gabor-mezei-armfa990b52021-03-25 11:17:10 +010068 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
69 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooremanfe0ab552020-09-10 13:07:02 +020070
gabor-mezei-armfa990b52021-03-25 11:17:10 +010071 return( mbedtls_transparent_test_driver_cipher_encrypt(
72 attributes, key_buffer, key_buffer_size,
73 alg, input, input_length,
74 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +020075}
76
Ronald Cronc4bc12e2021-04-13 12:41:34 +020077psa_status_t mbedtls_test_transparent_cipher_decrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +020078 const psa_key_attributes_t *attributes,
gabor-mezei-armfa990b52021-03-25 11:17:10 +010079 const uint8_t *key_buffer,
80 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +020081 psa_algorithm_t alg,
gabor-mezei-armfa990b52021-03-25 11:17:10 +010082 const uint8_t *input,
83 size_t input_length,
84 uint8_t *output,
85 size_t output_size,
86 size_t *output_length )
Steven Cooreman37941cb2020-07-28 18:49:51 +020087{
gabor-mezei-armfa990b52021-03-25 11:17:10 +010088 mbedtls_test_driver_cipher_hooks.hits++;
89
90 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
91 {
92 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
93 return( PSA_ERROR_BUFFER_TOO_SMALL );
94
95 memcpy( output,
96 mbedtls_test_driver_cipher_hooks.forced_output,
97 mbedtls_test_driver_cipher_hooks.forced_output_length );
98 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
99
100 return( mbedtls_test_driver_cipher_hooks.forced_status );
101 }
102
103 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
104 return( mbedtls_test_driver_cipher_hooks.forced_status );
105
106 return( mbedtls_transparent_test_driver_cipher_decrypt(
107 attributes, key_buffer, key_buffer_size,
108 alg, input, input_length,
109 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200110}
111
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200112psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100113 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200114 const psa_key_attributes_t *attributes,
115 const uint8_t *key, size_t key_length,
116 psa_algorithm_t alg)
117{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200118 mbedtls_test_driver_cipher_hooks.hits++;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100119
120 /* Wiping the entire struct here, instead of member-by-member. This is
121 * useful for the test suite, since it gives a chance of catching memory
122 * corruption errors should the core not have allocated (enough) memory for
123 * our context struct. */
124 memset( operation, 0, sizeof( *operation ) );
125
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200126 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
127 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100128
Ronald Cron3522e322021-03-12 11:08:49 +0100129 return ( mbedtls_transparent_test_driver_cipher_encrypt_setup(
130 operation, attributes, key, key_length, alg ) );
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200131}
132
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200133psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100134 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200135 const psa_key_attributes_t *attributes,
136 const uint8_t *key, size_t key_length,
137 psa_algorithm_t alg)
138{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200139 mbedtls_test_driver_cipher_hooks.hits++;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100140
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200141 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
142 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100143
Ronald Cron3522e322021-03-12 11:08:49 +0100144 return ( mbedtls_transparent_test_driver_cipher_decrypt_setup(
145 operation, attributes, key, key_length, alg ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200146}
147
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200148psa_status_t mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100149 mbedtls_transparent_test_driver_cipher_operation_t *operation)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200150{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200151 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman89e54f22020-09-10 18:07:57 +0200152
Steven Cooreman8b122252020-09-03 15:30:32 +0200153 if( operation->alg == 0 )
154 return( PSA_SUCCESS );
Steven Cooreman8b122252020-09-03 15:30:32 +0200155
Ronald Cron3522e322021-03-12 11:08:49 +0100156 mbedtls_transparent_test_driver_cipher_abort( operation );
Steven Cooreman8b122252020-09-03 15:30:32 +0200157
Ronald Cron8d310ad2020-12-15 15:17:20 +0100158 /* Wiping the entire struct here, instead of member-by-member. This is
159 * useful for the test suite, since it gives a chance of catching memory
160 * corruption errors should the core not have allocated (enough) memory for
161 * our context struct. */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200162 memset( operation, 0, sizeof( *operation ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200163
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200164 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200165}
166
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200167psa_status_t mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100168 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200169 const uint8_t *iv,
170 size_t iv_length)
171{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200172 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman8b122252020-09-03 15:30:32 +0200173
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200174 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
175 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman89e54f22020-09-10 18:07:57 +0200176
Ronald Cron3522e322021-03-12 11:08:49 +0100177 return( mbedtls_transparent_test_driver_cipher_set_iv(
178 operation, iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200179}
180
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200181psa_status_t mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100182 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200183 const uint8_t *input,
184 size_t input_length,
185 uint8_t *output,
186 size_t output_size,
187 size_t *output_length)
188{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200189 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200190
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200191 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200192 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200193 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200194 return PSA_ERROR_BUFFER_TOO_SMALL;
195
Steven Cooremanacb5a102020-09-08 14:06:57 +0200196 memcpy( output,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200197 mbedtls_test_driver_cipher_hooks.forced_output,
198 mbedtls_test_driver_cipher_hooks.forced_output_length );
199 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100200
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200201 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200202 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200203
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200204 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
205 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100206
Ronald Cron3522e322021-03-12 11:08:49 +0100207 return( mbedtls_transparent_test_driver_cipher_update(
208 operation, input, input_length,
209 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200210}
211
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200212psa_status_t mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100213 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200214 uint8_t *output,
215 size_t output_size,
216 size_t *output_length)
217{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200218 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200219
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200220 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200221 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200222 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200223 return PSA_ERROR_BUFFER_TOO_SMALL;
224
Steven Cooremanacb5a102020-09-08 14:06:57 +0200225 memcpy( output,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200226 mbedtls_test_driver_cipher_hooks.forced_output,
227 mbedtls_test_driver_cipher_hooks.forced_output_length );
228 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100229
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200230 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200231 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200232
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200233 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
234 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100235
Ronald Cron3522e322021-03-12 11:08:49 +0100236 return( mbedtls_transparent_test_driver_cipher_finish(
237 operation, output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200238}
239
240/*
241 * opaque versions, to do
242 */
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200243psa_status_t mbedtls_test_opaque_cipher_encrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +0200244 const psa_key_attributes_t *attributes,
245 const uint8_t *key, size_t key_length,
246 psa_algorithm_t alg,
247 const uint8_t *input, size_t input_length,
248 uint8_t *output, size_t output_size, size_t *output_length)
249{
250 (void) attributes;
251 (void) key;
252 (void) key_length;
253 (void) alg;
254 (void) input;
255 (void) input_length;
256 (void) output;
257 (void) output_size;
258 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200259 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200260}
261
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200262psa_status_t mbedtls_test_opaque_cipher_decrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +0200263 const psa_key_attributes_t *attributes,
264 const uint8_t *key, size_t key_length,
265 psa_algorithm_t alg,
266 const uint8_t *input, size_t input_length,
267 uint8_t *output, size_t output_size, size_t *output_length)
268{
269 (void) attributes;
270 (void) key;
271 (void) key_length;
272 (void) alg;
273 (void) input;
274 (void) input_length;
275 (void) output;
276 (void) output_size;
277 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200278 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200279}
280
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200281psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100282 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200283 const psa_key_attributes_t *attributes,
284 const uint8_t *key, size_t key_length,
285 psa_algorithm_t alg)
286{
287 (void) operation;
288 (void) attributes;
289 (void) key;
290 (void) key_length;
291 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200292 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200293}
294
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200295psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100296 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200297 const psa_key_attributes_t *attributes,
298 const uint8_t *key, size_t key_length,
299 psa_algorithm_t alg)
300{
301 (void) operation;
302 (void) attributes;
303 (void) key;
304 (void) key_length;
305 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200306 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200307}
308
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200309psa_status_t mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100310 mbedtls_opaque_test_driver_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200311{
312 (void) operation;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200313 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200314}
315
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200316psa_status_t mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100317 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200318 const uint8_t *iv,
319 size_t iv_length)
320{
321 (void) operation;
322 (void) iv;
323 (void) iv_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200324 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200325}
326
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200327psa_status_t mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100328 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200329 const uint8_t *input,
330 size_t input_length,
331 uint8_t *output,
332 size_t output_size,
333 size_t *output_length)
334{
335 (void) operation;
336 (void) input;
337 (void) input_length;
338 (void) output;
339 (void) output_size;
340 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200341 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200342}
343
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200344psa_status_t mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100345 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200346 uint8_t *output,
347 size_t output_size,
348 size_t *output_length)
349{
350 (void) operation;
351 (void) output;
352 (void) output_size;
353 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200354 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200355}
356#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */