blob: be06b970a0d0b62c41f7d892d321639228698a57 [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
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020021#include <test/helpers.h>
22
Steven Cooreman37941cb2020-07-28 18:49:51 +020023#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
24#include "psa/crypto.h"
Ronald Cron8d310ad2020-12-15 15:17:20 +010025#include "psa_crypto_cipher.h"
Steven Cooremanacb5a102020-09-08 14:06:57 +020026#include "psa_crypto_core.h"
Steven Cooreman37941cb2020-07-28 18:49:51 +020027#include "mbedtls/cipher.h"
28
Steven Cooremanacb5a102020-09-08 14:06:57 +020029#include "test/drivers/cipher.h"
Steven Cooreman37941cb2020-07-28 18:49:51 +020030
31#include "test/random.h"
32
33#include <string.h>
34
Ronald Cron7f13fa22021-04-13 12:41:34 +020035mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks =
36 MBEDTLS_TEST_DRIVER_CIPHER_INIT;
Steven Cooreman37941cb2020-07-28 18:49:51 +020037
gabor-mezei-arma9449a02021-03-25 11:17:10 +010038psa_status_t mbedtls_test_transparent_cipher_encrypt(
Steven Cooremanfe0ab552020-09-10 13:07:02 +020039 const psa_key_attributes_t *attributes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +010040 const uint8_t *key_buffer,
41 size_t key_buffer_size,
Steven Cooremanfe0ab552020-09-10 13:07:02 +020042 psa_algorithm_t alg,
gabor-mezei-arma9449a02021-03-25 11:17:10 +010043 const uint8_t *input,
44 size_t input_length,
45 uint8_t *output,
46 size_t output_size,
47 size_t *output_length )
Steven Cooremanfe0ab552020-09-10 13:07:02 +020048{
Ronald Cron7f13fa22021-04-13 12:41:34 +020049 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooremanfe0ab552020-09-10 13:07:02 +020050
Ronald Cron7f13fa22021-04-13 12:41:34 +020051 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
Steven Cooremanfe0ab552020-09-10 13:07:02 +020052 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020053 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
Steven Cooremanfe0ab552020-09-10 13:07:02 +020054 return( PSA_ERROR_BUFFER_TOO_SMALL );
55
56 memcpy( output,
Ronald Cron7f13fa22021-04-13 12:41:34 +020057 mbedtls_test_driver_cipher_hooks.forced_output,
58 mbedtls_test_driver_cipher_hooks.forced_output_length );
59 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
Steven Cooremanfe0ab552020-09-10 13:07:02 +020060
Ronald Cron7f13fa22021-04-13 12:41:34 +020061 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooremanfe0ab552020-09-10 13:07:02 +020062 }
63
gabor-mezei-arma9449a02021-03-25 11:17:10 +010064 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
65 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooremanfe0ab552020-09-10 13:07:02 +020066
gabor-mezei-arma9449a02021-03-25 11:17:10 +010067 psa_generate_random( output, PSA_CIPHER_IV_LENGTH( attributes->core.type, alg ) );
Steven Cooremanfe0ab552020-09-10 13:07:02 +020068
gabor-mezei-arma9449a02021-03-25 11:17:10 +010069 return( mbedtls_transparent_test_driver_cipher_encrypt(
70 attributes, key_buffer, key_buffer_size,
71 alg, input, input_length,
72 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +020073}
74
Ronald Cron7f13fa22021-04-13 12:41:34 +020075psa_status_t mbedtls_test_transparent_cipher_decrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +020076 const psa_key_attributes_t *attributes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +010077 const uint8_t *key_buffer,
78 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +020079 psa_algorithm_t alg,
gabor-mezei-arma9449a02021-03-25 11:17:10 +010080 const uint8_t *input,
81 size_t input_length,
82 uint8_t *output,
83 size_t output_size,
84 size_t *output_length )
Steven Cooreman37941cb2020-07-28 18:49:51 +020085{
gabor-mezei-arma9449a02021-03-25 11:17:10 +010086 mbedtls_test_driver_cipher_hooks.hits++;
87
88 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
89 {
90 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
91 return( PSA_ERROR_BUFFER_TOO_SMALL );
92
93 memcpy( output,
94 mbedtls_test_driver_cipher_hooks.forced_output,
95 mbedtls_test_driver_cipher_hooks.forced_output_length );
96 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
97
98 return( mbedtls_test_driver_cipher_hooks.forced_status );
99 }
100
101 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
102 return( mbedtls_test_driver_cipher_hooks.forced_status );
103
104 return( mbedtls_transparent_test_driver_cipher_decrypt(
105 attributes, key_buffer, key_buffer_size,
106 alg, input, input_length,
107 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200108}
109
Ronald Cron7f13fa22021-04-13 12:41:34 +0200110psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100111 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200112 const psa_key_attributes_t *attributes,
113 const uint8_t *key, size_t key_length,
114 psa_algorithm_t alg)
115{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200116 mbedtls_test_driver_cipher_hooks.hits++;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100117
118 /* Wiping the entire struct here, instead of member-by-member. This is
119 * useful for the test suite, since it gives a chance of catching memory
120 * corruption errors should the core not have allocated (enough) memory for
121 * our context struct. */
122 memset( operation, 0, sizeof( *operation ) );
123
Ronald Cron7f13fa22021-04-13 12:41:34 +0200124 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
125 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100126
Ronald Cron3522e322021-03-12 11:08:49 +0100127 return ( mbedtls_transparent_test_driver_cipher_encrypt_setup(
128 operation, attributes, key, key_length, alg ) );
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200129}
130
Ronald Cron7f13fa22021-04-13 12:41:34 +0200131psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100132 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200133 const psa_key_attributes_t *attributes,
134 const uint8_t *key, size_t key_length,
135 psa_algorithm_t alg)
136{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200137 mbedtls_test_driver_cipher_hooks.hits++;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100138
Ronald Cron7f13fa22021-04-13 12:41:34 +0200139 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
140 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100141
Ronald Cron3522e322021-03-12 11:08:49 +0100142 return ( mbedtls_transparent_test_driver_cipher_decrypt_setup(
143 operation, attributes, key, key_length, alg ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200144}
145
Ronald Cron7f13fa22021-04-13 12:41:34 +0200146psa_status_t mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100147 mbedtls_transparent_test_driver_cipher_operation_t *operation)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200148{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200149 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman89e54f22020-09-10 18:07:57 +0200150
Ronald Cron3522e322021-03-12 11:08:49 +0100151 mbedtls_transparent_test_driver_cipher_abort( operation );
Steven Cooreman8b122252020-09-03 15:30:32 +0200152
Ronald Cron8d310ad2020-12-15 15:17:20 +0100153 /* Wiping the entire struct here, instead of member-by-member. This is
154 * useful for the test suite, since it gives a chance of catching memory
155 * corruption errors should the core not have allocated (enough) memory for
156 * our context struct. */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200157 memset( operation, 0, sizeof( *operation ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200158
Ronald Cron7f13fa22021-04-13 12:41:34 +0200159 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200160}
161
Ronald Cron7f13fa22021-04-13 12:41:34 +0200162psa_status_t mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100163 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200164 const uint8_t *iv,
165 size_t iv_length)
166{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200167 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman8b122252020-09-03 15:30:32 +0200168
Ronald Cron7f13fa22021-04-13 12:41:34 +0200169 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
170 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman89e54f22020-09-10 18:07:57 +0200171
Ronald Cron3522e322021-03-12 11:08:49 +0100172 return( mbedtls_transparent_test_driver_cipher_set_iv(
173 operation, iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200174}
175
Ronald Cron7f13fa22021-04-13 12:41:34 +0200176psa_status_t mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100177 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200178 const uint8_t *input,
179 size_t input_length,
180 uint8_t *output,
181 size_t output_size,
182 size_t *output_length)
183{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200184 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200185
Ronald Cron7f13fa22021-04-13 12:41:34 +0200186 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200187 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200188 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200189 return PSA_ERROR_BUFFER_TOO_SMALL;
190
Steven Cooremanacb5a102020-09-08 14:06:57 +0200191 memcpy( output,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200192 mbedtls_test_driver_cipher_hooks.forced_output,
193 mbedtls_test_driver_cipher_hooks.forced_output_length );
194 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100195
Ronald Cron7f13fa22021-04-13 12:41:34 +0200196 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200197 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200198
Ronald Cron7f13fa22021-04-13 12:41:34 +0200199 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
200 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100201
Ronald Cron3522e322021-03-12 11:08:49 +0100202 return( mbedtls_transparent_test_driver_cipher_update(
203 operation, input, input_length,
204 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200205}
206
Ronald Cron7f13fa22021-04-13 12:41:34 +0200207psa_status_t mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100208 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200209 uint8_t *output,
210 size_t output_size,
211 size_t *output_length)
212{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200213 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200214
Ronald Cron7f13fa22021-04-13 12:41:34 +0200215 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200216 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200217 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200218 return PSA_ERROR_BUFFER_TOO_SMALL;
219
Steven Cooremanacb5a102020-09-08 14:06:57 +0200220 memcpy( output,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200221 mbedtls_test_driver_cipher_hooks.forced_output,
222 mbedtls_test_driver_cipher_hooks.forced_output_length );
223 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100224
Ronald Cron7f13fa22021-04-13 12:41:34 +0200225 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200226 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200227
Ronald Cron7f13fa22021-04-13 12:41:34 +0200228 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
229 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100230
Ronald Cron3522e322021-03-12 11:08:49 +0100231 return( mbedtls_transparent_test_driver_cipher_finish(
232 operation, output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200233}
234
235/*
236 * opaque versions, to do
237 */
Ronald Cron7f13fa22021-04-13 12:41:34 +0200238psa_status_t mbedtls_test_opaque_cipher_encrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +0200239 const psa_key_attributes_t *attributes,
240 const uint8_t *key, size_t key_length,
241 psa_algorithm_t alg,
242 const uint8_t *input, size_t input_length,
243 uint8_t *output, size_t output_size, size_t *output_length)
244{
245 (void) attributes;
246 (void) key;
247 (void) key_length;
248 (void) alg;
249 (void) input;
250 (void) input_length;
251 (void) output;
252 (void) output_size;
253 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200254 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200255}
256
Ronald Cron7f13fa22021-04-13 12:41:34 +0200257psa_status_t mbedtls_test_opaque_cipher_decrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +0200258 const psa_key_attributes_t *attributes,
259 const uint8_t *key, size_t key_length,
260 psa_algorithm_t alg,
261 const uint8_t *input, size_t input_length,
262 uint8_t *output, size_t output_size, size_t *output_length)
263{
264 (void) attributes;
265 (void) key;
266 (void) key_length;
267 (void) alg;
268 (void) input;
269 (void) input_length;
270 (void) output;
271 (void) output_size;
272 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200273 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200274}
275
Ronald Cron7f13fa22021-04-13 12:41:34 +0200276psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100277 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200278 const psa_key_attributes_t *attributes,
279 const uint8_t *key, size_t key_length,
280 psa_algorithm_t alg)
281{
282 (void) operation;
283 (void) attributes;
284 (void) key;
285 (void) key_length;
286 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200287 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200288}
289
Ronald Cron7f13fa22021-04-13 12:41:34 +0200290psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100291 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200292 const psa_key_attributes_t *attributes,
293 const uint8_t *key, size_t key_length,
294 psa_algorithm_t alg)
295{
296 (void) operation;
297 (void) attributes;
298 (void) key;
299 (void) key_length;
300 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200301 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200302}
303
Ronald Cron7f13fa22021-04-13 12:41:34 +0200304psa_status_t mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100305 mbedtls_opaque_test_driver_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200306{
307 (void) operation;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200308 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200309}
310
Ronald Cron7f13fa22021-04-13 12:41:34 +0200311psa_status_t mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100312 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200313 const uint8_t *iv,
314 size_t iv_length)
315{
316 (void) operation;
317 (void) iv;
318 (void) iv_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200319 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200320}
321
Ronald Cron7f13fa22021-04-13 12:41:34 +0200322psa_status_t mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100323 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200324 const uint8_t *input,
325 size_t input_length,
326 uint8_t *output,
327 size_t output_size,
328 size_t *output_length)
329{
330 (void) operation;
331 (void) input;
332 (void) input_length;
333 (void) output;
334 (void) output_size;
335 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200336 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200337}
338
Ronald Cron7f13fa22021-04-13 12:41:34 +0200339psa_status_t mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100340 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200341 uint8_t *output,
342 size_t output_size,
343 size_t *output_length)
344{
345 (void) operation;
346 (void) output;
347 (void) output_size;
348 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200349 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200350}
351#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */