blob: 6aca1938cba0460193ff45c2095fd6eefd9c4eb1 [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 psa_generate_random( output, PSA_CIPHER_IV_LENGTH( attributes->core.type, alg ) );
Steven Cooremanfe0ab552020-09-10 13:07:02 +020072
gabor-mezei-armfa990b52021-03-25 11:17:10 +010073 return( mbedtls_transparent_test_driver_cipher_encrypt(
74 attributes, key_buffer, key_buffer_size,
75 alg, input, input_length,
76 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +020077}
78
Ronald Cronc4bc12e2021-04-13 12:41:34 +020079psa_status_t mbedtls_test_transparent_cipher_decrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +020080 const psa_key_attributes_t *attributes,
gabor-mezei-armfa990b52021-03-25 11:17:10 +010081 const uint8_t *key_buffer,
82 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +020083 psa_algorithm_t alg,
gabor-mezei-armfa990b52021-03-25 11:17:10 +010084 const uint8_t *input,
85 size_t input_length,
86 uint8_t *output,
87 size_t output_size,
88 size_t *output_length )
Steven Cooreman37941cb2020-07-28 18:49:51 +020089{
gabor-mezei-armfa990b52021-03-25 11:17:10 +010090 mbedtls_test_driver_cipher_hooks.hits++;
91
92 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
93 {
94 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
95 return( PSA_ERROR_BUFFER_TOO_SMALL );
96
97 memcpy( output,
98 mbedtls_test_driver_cipher_hooks.forced_output,
99 mbedtls_test_driver_cipher_hooks.forced_output_length );
100 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
101
102 return( mbedtls_test_driver_cipher_hooks.forced_status );
103 }
104
105 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
106 return( mbedtls_test_driver_cipher_hooks.forced_status );
107
108 return( mbedtls_transparent_test_driver_cipher_decrypt(
109 attributes, key_buffer, key_buffer_size,
110 alg, input, input_length,
111 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200112}
113
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200114psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100115 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200116 const psa_key_attributes_t *attributes,
117 const uint8_t *key, size_t key_length,
118 psa_algorithm_t alg)
119{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200120 mbedtls_test_driver_cipher_hooks.hits++;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100121
122 /* Wiping the entire struct here, instead of member-by-member. This is
123 * useful for the test suite, since it gives a chance of catching memory
124 * corruption errors should the core not have allocated (enough) memory for
125 * our context struct. */
126 memset( operation, 0, sizeof( *operation ) );
127
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200128 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
129 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100130
Ronald Cron3522e322021-03-12 11:08:49 +0100131 return ( mbedtls_transparent_test_driver_cipher_encrypt_setup(
132 operation, attributes, key, key_length, alg ) );
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200133}
134
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200135psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100136 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200137 const psa_key_attributes_t *attributes,
138 const uint8_t *key, size_t key_length,
139 psa_algorithm_t alg)
140{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200141 mbedtls_test_driver_cipher_hooks.hits++;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100142
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200143 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
144 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100145
Ronald Cron3522e322021-03-12 11:08:49 +0100146 return ( mbedtls_transparent_test_driver_cipher_decrypt_setup(
147 operation, attributes, key, key_length, alg ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200148}
149
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200150psa_status_t mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100151 mbedtls_transparent_test_driver_cipher_operation_t *operation)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200152{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200153 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman89e54f22020-09-10 18:07:57 +0200154
Steven Cooreman8b122252020-09-03 15:30:32 +0200155 if( operation->alg == 0 )
156 return( PSA_SUCCESS );
Steven Cooreman8b122252020-09-03 15:30:32 +0200157
Ronald Cron3522e322021-03-12 11:08:49 +0100158 mbedtls_transparent_test_driver_cipher_abort( operation );
Steven Cooreman8b122252020-09-03 15:30:32 +0200159
Ronald Cron8d310ad2020-12-15 15:17:20 +0100160 /* Wiping the entire struct here, instead of member-by-member. This is
161 * useful for the test suite, since it gives a chance of catching memory
162 * corruption errors should the core not have allocated (enough) memory for
163 * our context struct. */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200164 memset( operation, 0, sizeof( *operation ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200165
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200166 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200167}
168
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200169psa_status_t mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100170 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200171 const uint8_t *iv,
172 size_t iv_length)
173{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200174 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman8b122252020-09-03 15:30:32 +0200175
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200176 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
177 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman89e54f22020-09-10 18:07:57 +0200178
Ronald Cron3522e322021-03-12 11:08:49 +0100179 return( mbedtls_transparent_test_driver_cipher_set_iv(
180 operation, iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200181}
182
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200183psa_status_t mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100184 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200185 const uint8_t *input,
186 size_t input_length,
187 uint8_t *output,
188 size_t output_size,
189 size_t *output_length)
190{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200191 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200192
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200193 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200194 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200195 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200196 return PSA_ERROR_BUFFER_TOO_SMALL;
197
Steven Cooremanacb5a102020-09-08 14:06:57 +0200198 memcpy( output,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200199 mbedtls_test_driver_cipher_hooks.forced_output,
200 mbedtls_test_driver_cipher_hooks.forced_output_length );
201 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100202
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200203 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200204 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200205
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200206 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
207 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100208
Ronald Cron3522e322021-03-12 11:08:49 +0100209 return( mbedtls_transparent_test_driver_cipher_update(
210 operation, input, input_length,
211 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200212}
213
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200214psa_status_t mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100215 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200216 uint8_t *output,
217 size_t output_size,
218 size_t *output_length)
219{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200220 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200221
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200222 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200223 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200224 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200225 return PSA_ERROR_BUFFER_TOO_SMALL;
226
Steven Cooremanacb5a102020-09-08 14:06:57 +0200227 memcpy( output,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200228 mbedtls_test_driver_cipher_hooks.forced_output,
229 mbedtls_test_driver_cipher_hooks.forced_output_length );
230 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100231
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200232 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200233 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200234
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200235 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
236 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100237
Ronald Cron3522e322021-03-12 11:08:49 +0100238 return( mbedtls_transparent_test_driver_cipher_finish(
239 operation, output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200240}
241
242/*
243 * opaque versions, to do
244 */
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200245psa_status_t mbedtls_test_opaque_cipher_encrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +0200246 const psa_key_attributes_t *attributes,
247 const uint8_t *key, size_t key_length,
248 psa_algorithm_t alg,
249 const uint8_t *input, size_t input_length,
250 uint8_t *output, size_t output_size, size_t *output_length)
251{
252 (void) attributes;
253 (void) key;
254 (void) key_length;
255 (void) alg;
256 (void) input;
257 (void) input_length;
258 (void) output;
259 (void) output_size;
260 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200261 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200262}
263
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200264psa_status_t mbedtls_test_opaque_cipher_decrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +0200265 const psa_key_attributes_t *attributes,
266 const uint8_t *key, size_t key_length,
267 psa_algorithm_t alg,
268 const uint8_t *input, size_t input_length,
269 uint8_t *output, size_t output_size, size_t *output_length)
270{
271 (void) attributes;
272 (void) key;
273 (void) key_length;
274 (void) alg;
275 (void) input;
276 (void) input_length;
277 (void) output;
278 (void) output_size;
279 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200280 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200281}
282
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200283psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100284 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200285 const psa_key_attributes_t *attributes,
286 const uint8_t *key, size_t key_length,
287 psa_algorithm_t alg)
288{
289 (void) operation;
290 (void) attributes;
291 (void) key;
292 (void) key_length;
293 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200294 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200295}
296
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200297psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100298 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200299 const psa_key_attributes_t *attributes,
300 const uint8_t *key, size_t key_length,
301 psa_algorithm_t alg)
302{
303 (void) operation;
304 (void) attributes;
305 (void) key;
306 (void) key_length;
307 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200308 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200309}
310
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200311psa_status_t mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100312 mbedtls_opaque_test_driver_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200313{
314 (void) operation;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200315 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200316}
317
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200318psa_status_t mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100319 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200320 const uint8_t *iv,
321 size_t iv_length)
322{
323 (void) operation;
324 (void) iv;
325 (void) iv_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200326 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200327}
328
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200329psa_status_t mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100330 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200331 const uint8_t *input,
332 size_t input_length,
333 uint8_t *output,
334 size_t output_size,
335 size_t *output_length)
336{
337 (void) operation;
338 (void) input;
339 (void) input_length;
340 (void) output;
341 (void) output_size;
342 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200343 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200344}
345
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200346psa_status_t mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100347 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200348 uint8_t *output,
349 size_t output_size,
350 size_t *output_length)
351{
352 (void) operation;
353 (void) output;
354 (void) output_size;
355 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200356 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200357}
358#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */