blob: 9e0dc30c551b5bfbb176eaff4e127d8497236aa3 [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,
Ronald Crona8331692021-07-09 09:19:35 +020047 const uint8_t *iv,
48 size_t iv_length,
gabor-mezei-armfa990b52021-03-25 11:17:10 +010049 const uint8_t *input,
50 size_t input_length,
51 uint8_t *output,
52 size_t output_size,
53 size_t *output_length )
Steven Cooremanfe0ab552020-09-10 13:07:02 +020054{
Ronald Cronc4bc12e2021-04-13 12:41:34 +020055 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooremanfe0ab552020-09-10 13:07:02 +020056
Ronald Cronc4bc12e2021-04-13 12:41:34 +020057 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
Steven Cooremanfe0ab552020-09-10 13:07:02 +020058 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +020059 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
Steven Cooremanfe0ab552020-09-10 13:07:02 +020060 return( PSA_ERROR_BUFFER_TOO_SMALL );
61
62 memcpy( output,
Ronald Cronc4bc12e2021-04-13 12:41:34 +020063 mbedtls_test_driver_cipher_hooks.forced_output,
64 mbedtls_test_driver_cipher_hooks.forced_output_length );
65 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
Steven Cooremanfe0ab552020-09-10 13:07:02 +020066
Ronald Cronc4bc12e2021-04-13 12:41:34 +020067 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooremanfe0ab552020-09-10 13:07:02 +020068 }
69
gabor-mezei-armfa990b52021-03-25 11:17:10 +010070 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
71 return( mbedtls_test_driver_cipher_hooks.forced_status );
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,
Ronald Crona8331692021-07-09 09:19:35 +020075 alg, iv, iv_length, input, input_length,
gabor-mezei-armfa990b52021-03-25 11:17:10 +010076 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,
Ronald Crona8331692021-07-09 09:19:35 +0200249 const uint8_t *iv, size_t iv_length,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200250 const uint8_t *input, size_t input_length,
251 uint8_t *output, size_t output_size, size_t *output_length)
252{
253 (void) attributes;
254 (void) key;
255 (void) key_length;
256 (void) alg;
Ronald Crona8331692021-07-09 09:19:35 +0200257 (void) iv;
258 (void) iv_length;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200259 (void) input;
260 (void) input_length;
261 (void) output;
262 (void) output_size;
263 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200264 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200265}
266
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200267psa_status_t mbedtls_test_opaque_cipher_decrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +0200268 const psa_key_attributes_t *attributes,
269 const uint8_t *key, size_t key_length,
270 psa_algorithm_t alg,
271 const uint8_t *input, size_t input_length,
272 uint8_t *output, size_t output_size, size_t *output_length)
273{
274 (void) attributes;
275 (void) key;
276 (void) key_length;
277 (void) alg;
278 (void) input;
279 (void) input_length;
280 (void) output;
281 (void) output_size;
282 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200283 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200284}
285
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200286psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100287 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200288 const psa_key_attributes_t *attributes,
289 const uint8_t *key, size_t key_length,
290 psa_algorithm_t alg)
291{
292 (void) operation;
293 (void) attributes;
294 (void) key;
295 (void) key_length;
296 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200297 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200298}
299
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200300psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100301 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200302 const psa_key_attributes_t *attributes,
303 const uint8_t *key, size_t key_length,
304 psa_algorithm_t alg)
305{
306 (void) operation;
307 (void) attributes;
308 (void) key;
309 (void) key_length;
310 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200311 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200312}
313
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200314psa_status_t mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100315 mbedtls_opaque_test_driver_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200316{
317 (void) operation;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200318 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200319}
320
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200321psa_status_t mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100322 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200323 const uint8_t *iv,
324 size_t iv_length)
325{
326 (void) operation;
327 (void) iv;
328 (void) iv_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200329 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200330}
331
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200332psa_status_t mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100333 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200334 const uint8_t *input,
335 size_t input_length,
336 uint8_t *output,
337 size_t output_size,
338 size_t *output_length)
339{
340 (void) operation;
341 (void) input;
342 (void) input_length;
343 (void) output;
344 (void) output_size;
345 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200346 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200347}
348
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200349psa_status_t mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100350 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200351 uint8_t *output,
352 size_t output_size,
353 size_t *output_length)
354{
355 (void) operation;
356 (void) output;
357 (void) output_size;
358 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200359 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200360}
361#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */