blob: 755f6a0a78c0d8d47b5d6de16501e87fa0cb9476 [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 return( mbedtls_transparent_test_driver_cipher_encrypt(
68 attributes, key_buffer, key_buffer_size,
69 alg, input, input_length,
70 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +020071}
72
Ronald Cron7f13fa22021-04-13 12:41:34 +020073psa_status_t mbedtls_test_transparent_cipher_decrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +020074 const psa_key_attributes_t *attributes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +010075 const uint8_t *key_buffer,
76 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +020077 psa_algorithm_t alg,
gabor-mezei-arma9449a02021-03-25 11:17:10 +010078 const uint8_t *input,
79 size_t input_length,
80 uint8_t *output,
81 size_t output_size,
82 size_t *output_length )
Steven Cooreman37941cb2020-07-28 18:49:51 +020083{
gabor-mezei-arma9449a02021-03-25 11:17:10 +010084 mbedtls_test_driver_cipher_hooks.hits++;
85
86 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
87 {
88 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
89 return( PSA_ERROR_BUFFER_TOO_SMALL );
90
91 memcpy( output,
92 mbedtls_test_driver_cipher_hooks.forced_output,
93 mbedtls_test_driver_cipher_hooks.forced_output_length );
94 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
95
96 return( mbedtls_test_driver_cipher_hooks.forced_status );
97 }
98
99 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
100 return( mbedtls_test_driver_cipher_hooks.forced_status );
101
102 return( mbedtls_transparent_test_driver_cipher_decrypt(
103 attributes, key_buffer, key_buffer_size,
104 alg, input, input_length,
105 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200106}
107
Ronald Cron7f13fa22021-04-13 12:41:34 +0200108psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100109 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200110 const psa_key_attributes_t *attributes,
111 const uint8_t *key, size_t key_length,
112 psa_algorithm_t alg)
113{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200114 mbedtls_test_driver_cipher_hooks.hits++;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100115
116 /* Wiping the entire struct here, instead of member-by-member. This is
117 * useful for the test suite, since it gives a chance of catching memory
118 * corruption errors should the core not have allocated (enough) memory for
119 * our context struct. */
120 memset( operation, 0, sizeof( *operation ) );
121
Ronald Cron7f13fa22021-04-13 12:41:34 +0200122 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
123 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100124
Ronald Cron3522e322021-03-12 11:08:49 +0100125 return ( mbedtls_transparent_test_driver_cipher_encrypt_setup(
126 operation, attributes, key, key_length, alg ) );
Steven Cooreman16afd3d2020-09-09 15:36:39 +0200127}
128
Ronald Cron7f13fa22021-04-13 12:41:34 +0200129psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100130 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200131 const psa_key_attributes_t *attributes,
132 const uint8_t *key, size_t key_length,
133 psa_algorithm_t alg)
134{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200135 mbedtls_test_driver_cipher_hooks.hits++;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100136
Ronald Cron7f13fa22021-04-13 12:41:34 +0200137 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
138 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100139
Ronald Cron3522e322021-03-12 11:08:49 +0100140 return ( mbedtls_transparent_test_driver_cipher_decrypt_setup(
141 operation, attributes, key, key_length, alg ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200142}
143
Ronald Cron7f13fa22021-04-13 12:41:34 +0200144psa_status_t mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100145 mbedtls_transparent_test_driver_cipher_operation_t *operation)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200146{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200147 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman89e54f22020-09-10 18:07:57 +0200148
Steven Cooreman8b122252020-09-03 15:30:32 +0200149 if( operation->alg == 0 )
150 return( PSA_SUCCESS );
Steven Cooreman8b122252020-09-03 15:30:32 +0200151
Ronald Cron3522e322021-03-12 11:08:49 +0100152 mbedtls_transparent_test_driver_cipher_abort( operation );
Steven Cooreman8b122252020-09-03 15:30:32 +0200153
Ronald Cron8d310ad2020-12-15 15:17:20 +0100154 /* Wiping the entire struct here, instead of member-by-member. This is
155 * useful for the test suite, since it gives a chance of catching memory
156 * corruption errors should the core not have allocated (enough) memory for
157 * our context struct. */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200158 memset( operation, 0, sizeof( *operation ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200159
Ronald Cron7f13fa22021-04-13 12:41:34 +0200160 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200161}
162
Ronald Cron7f13fa22021-04-13 12:41:34 +0200163psa_status_t mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100164 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200165 const uint8_t *iv,
166 size_t iv_length)
167{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200168 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman8b122252020-09-03 15:30:32 +0200169
Ronald Cron7f13fa22021-04-13 12:41:34 +0200170 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
171 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman89e54f22020-09-10 18:07:57 +0200172
Ronald Cron3522e322021-03-12 11:08:49 +0100173 return( mbedtls_transparent_test_driver_cipher_set_iv(
174 operation, iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200175}
176
Ronald Cron7f13fa22021-04-13 12:41:34 +0200177psa_status_t mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100178 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200179 const uint8_t *input,
180 size_t input_length,
181 uint8_t *output,
182 size_t output_size,
183 size_t *output_length)
184{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200185 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200186
Ronald Cron7f13fa22021-04-13 12:41:34 +0200187 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200188 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200189 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200190 return PSA_ERROR_BUFFER_TOO_SMALL;
191
Steven Cooremanacb5a102020-09-08 14:06:57 +0200192 memcpy( output,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200193 mbedtls_test_driver_cipher_hooks.forced_output,
194 mbedtls_test_driver_cipher_hooks.forced_output_length );
195 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100196
Ronald Cron7f13fa22021-04-13 12:41:34 +0200197 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200198 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200199
Ronald Cron7f13fa22021-04-13 12:41:34 +0200200 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
201 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100202
Ronald Cron3522e322021-03-12 11:08:49 +0100203 return( mbedtls_transparent_test_driver_cipher_update(
204 operation, input, input_length,
205 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200206}
207
Ronald Cron7f13fa22021-04-13 12:41:34 +0200208psa_status_t mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100209 mbedtls_transparent_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200210 uint8_t *output,
211 size_t output_size,
212 size_t *output_length)
213{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200214 mbedtls_test_driver_cipher_hooks.hits++;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200215
Ronald Cron7f13fa22021-04-13 12:41:34 +0200216 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
Steven Cooreman8b122252020-09-03 15:30:32 +0200217 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200218 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
Steven Cooreman8b122252020-09-03 15:30:32 +0200219 return PSA_ERROR_BUFFER_TOO_SMALL;
220
Steven Cooremanacb5a102020-09-08 14:06:57 +0200221 memcpy( output,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200222 mbedtls_test_driver_cipher_hooks.forced_output,
223 mbedtls_test_driver_cipher_hooks.forced_output_length );
224 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
Ronald Cron8d310ad2020-12-15 15:17:20 +0100225
Ronald Cron7f13fa22021-04-13 12:41:34 +0200226 return( mbedtls_test_driver_cipher_hooks.forced_status );
Steven Cooreman8b122252020-09-03 15:30:32 +0200227 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200228
Ronald Cron7f13fa22021-04-13 12:41:34 +0200229 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
230 return( mbedtls_test_driver_cipher_hooks.forced_status );
Ronald Cron8d310ad2020-12-15 15:17:20 +0100231
Ronald Cron3522e322021-03-12 11:08:49 +0100232 return( mbedtls_transparent_test_driver_cipher_finish(
233 operation, output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200234}
235
236/*
237 * opaque versions, to do
238 */
Ronald Cron7f13fa22021-04-13 12:41:34 +0200239psa_status_t mbedtls_test_opaque_cipher_encrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +0200240 const psa_key_attributes_t *attributes,
241 const uint8_t *key, size_t key_length,
242 psa_algorithm_t alg,
243 const uint8_t *input, size_t input_length,
244 uint8_t *output, size_t output_size, size_t *output_length)
245{
246 (void) attributes;
247 (void) key;
248 (void) key_length;
249 (void) alg;
250 (void) input;
251 (void) input_length;
252 (void) output;
253 (void) output_size;
254 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200255 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200256}
257
Ronald Cron7f13fa22021-04-13 12:41:34 +0200258psa_status_t mbedtls_test_opaque_cipher_decrypt(
Steven Cooreman37941cb2020-07-28 18:49:51 +0200259 const psa_key_attributes_t *attributes,
260 const uint8_t *key, size_t key_length,
261 psa_algorithm_t alg,
262 const uint8_t *input, size_t input_length,
263 uint8_t *output, size_t output_size, size_t *output_length)
264{
265 (void) attributes;
266 (void) key;
267 (void) key_length;
268 (void) alg;
269 (void) input;
270 (void) input_length;
271 (void) output;
272 (void) output_size;
273 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200274 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200275}
276
Ronald Cron7f13fa22021-04-13 12:41:34 +0200277psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100278 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200279 const psa_key_attributes_t *attributes,
280 const uint8_t *key, size_t key_length,
281 psa_algorithm_t alg)
282{
283 (void) operation;
284 (void) attributes;
285 (void) key;
286 (void) key_length;
287 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200288 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200289}
290
Ronald Cron7f13fa22021-04-13 12:41:34 +0200291psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100292 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200293 const psa_key_attributes_t *attributes,
294 const uint8_t *key, size_t key_length,
295 psa_algorithm_t alg)
296{
297 (void) operation;
298 (void) attributes;
299 (void) key;
300 (void) key_length;
301 (void) alg;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200302 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200303}
304
Ronald Cron7f13fa22021-04-13 12:41:34 +0200305psa_status_t mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100306 mbedtls_opaque_test_driver_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200307{
308 (void) operation;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200309 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200310}
311
Ronald Cron7f13fa22021-04-13 12:41:34 +0200312psa_status_t mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100313 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200314 const uint8_t *iv,
315 size_t iv_length)
316{
317 (void) operation;
318 (void) iv;
319 (void) iv_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200320 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200321}
322
Ronald Cron7f13fa22021-04-13 12:41:34 +0200323psa_status_t mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100324 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200325 const uint8_t *input,
326 size_t input_length,
327 uint8_t *output,
328 size_t output_size,
329 size_t *output_length)
330{
331 (void) operation;
332 (void) input;
333 (void) input_length;
334 (void) output;
335 (void) output_size;
336 (void) output_length;
Steven Cooremanacb5a102020-09-08 14:06:57 +0200337 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200338}
339
Ronald Cron7f13fa22021-04-13 12:41:34 +0200340psa_status_t mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100341 mbedtls_opaque_test_driver_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200342 uint8_t *output,
343 size_t output_size,
344 size_t *output_length)
345{
346 (void) operation;
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#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */