1 /*
2 * Test driver for cipher functions.
3 * Currently only supports multi-part operations using AES-CTR.
4 */
5 /* Copyright The Mbed TLS Contributors
6 * 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.
19 */
20
21 #include <test/helpers.h>
22
23 #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
24 #include "psa/crypto.h"
25 #include "psa_crypto_cipher.h"
26 #include "psa_crypto_core.h"
27 #include "mbedtls/cipher.h"
28
29 #include "test/drivers/cipher.h"
30
31 #include "test/random.h"
32
33 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
34 #include "libtestdriver1/library/psa_crypto_cipher.h"
35 #endif
36
37 #include <string.h>
38
39 mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks =
40 MBEDTLS_TEST_DRIVER_CIPHER_INIT;
41
mbedtls_test_transparent_cipher_encrypt(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * iv,size_t iv_length,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)42 psa_status_t mbedtls_test_transparent_cipher_encrypt(
43 const psa_key_attributes_t *attributes,
44 const uint8_t *key_buffer,
45 size_t key_buffer_size,
46 psa_algorithm_t alg,
47 const uint8_t *iv,
48 size_t iv_length,
49 const uint8_t *input,
50 size_t input_length,
51 uint8_t *output,
52 size_t output_size,
53 size_t *output_length )
54 {
55 mbedtls_test_driver_cipher_hooks.hits++;
56
57 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
58 {
59 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
60 return( PSA_ERROR_BUFFER_TOO_SMALL );
61
62 memcpy( output,
63 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;
66
67 return( mbedtls_test_driver_cipher_hooks.forced_status );
68 }
69
70 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
71 return( mbedtls_test_driver_cipher_hooks.forced_status );
72
73 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
74 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
75 return( libtestdriver1_mbedtls_psa_cipher_encrypt(
76 (const libtestdriver1_psa_key_attributes_t *)attributes,
77 key_buffer, key_buffer_size,
78 alg, iv, iv_length, input, input_length,
79 output, output_size, output_length ) );
80 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
81 return( mbedtls_psa_cipher_encrypt(
82 attributes, key_buffer, key_buffer_size,
83 alg, iv, iv_length, input, input_length,
84 output, output_size, output_length ) );
85 #endif
86
87 return( PSA_ERROR_NOT_SUPPORTED );
88 }
89
mbedtls_test_transparent_cipher_decrypt(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)90 psa_status_t mbedtls_test_transparent_cipher_decrypt(
91 const psa_key_attributes_t *attributes,
92 const uint8_t *key_buffer,
93 size_t key_buffer_size,
94 psa_algorithm_t alg,
95 const uint8_t *input,
96 size_t input_length,
97 uint8_t *output,
98 size_t output_size,
99 size_t *output_length )
100 {
101 mbedtls_test_driver_cipher_hooks.hits++;
102
103 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
104 {
105 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
106 return( PSA_ERROR_BUFFER_TOO_SMALL );
107
108 memcpy( output,
109 mbedtls_test_driver_cipher_hooks.forced_output,
110 mbedtls_test_driver_cipher_hooks.forced_output_length );
111 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
112
113 return( mbedtls_test_driver_cipher_hooks.forced_status );
114 }
115
116 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
117 return( mbedtls_test_driver_cipher_hooks.forced_status );
118
119 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
120 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
121 return( libtestdriver1_mbedtls_psa_cipher_decrypt(
122 (const libtestdriver1_psa_key_attributes_t *)attributes,
123 key_buffer, key_buffer_size,
124 alg, input, input_length,
125 output, output_size, output_length ) );
126 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
127 return( mbedtls_psa_cipher_decrypt(
128 attributes, key_buffer, key_buffer_size,
129 alg, input, input_length,
130 output, output_size, output_length ) );
131 #endif
132
133 return( PSA_ERROR_NOT_SUPPORTED );
134 }
135
mbedtls_test_transparent_cipher_encrypt_setup(mbedtls_transparent_test_driver_cipher_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg)136 psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
137 mbedtls_transparent_test_driver_cipher_operation_t *operation,
138 const psa_key_attributes_t *attributes,
139 const uint8_t *key, size_t key_length,
140 psa_algorithm_t alg)
141 {
142 mbedtls_test_driver_cipher_hooks.hits++;
143
144 /* Wiping the entire struct here, instead of member-by-member. This is
145 * useful for the test suite, since it gives a chance of catching memory
146 * corruption errors should the core not have allocated (enough) memory for
147 * our context struct. */
148 memset( operation, 0, sizeof( *operation ) );
149
150 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
151 return( mbedtls_test_driver_cipher_hooks.forced_status );
152
153 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
154 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
155 return( libtestdriver1_mbedtls_psa_cipher_encrypt_setup(
156 operation,
157 (const libtestdriver1_psa_key_attributes_t *)attributes,
158 key, key_length, alg ) );
159 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
160 return( mbedtls_psa_cipher_encrypt_setup(
161 operation, attributes, key, key_length, alg ) );
162 #endif
163
164 return( PSA_ERROR_NOT_SUPPORTED );
165 }
166
mbedtls_test_transparent_cipher_decrypt_setup(mbedtls_transparent_test_driver_cipher_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg)167 psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
168 mbedtls_transparent_test_driver_cipher_operation_t *operation,
169 const psa_key_attributes_t *attributes,
170 const uint8_t *key, size_t key_length,
171 psa_algorithm_t alg)
172 {
173 mbedtls_test_driver_cipher_hooks.hits++;
174
175 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
176 return( mbedtls_test_driver_cipher_hooks.forced_status );
177
178 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
179 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
180 return( libtestdriver1_mbedtls_psa_cipher_decrypt_setup(
181 operation,
182 (const libtestdriver1_psa_key_attributes_t *)attributes,
183 key, key_length, alg ) );
184 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
185 return( mbedtls_psa_cipher_decrypt_setup(
186 operation, attributes, key, key_length, alg ) );
187 #endif
188
189 return( PSA_ERROR_NOT_SUPPORTED );
190 }
191
mbedtls_test_transparent_cipher_abort(mbedtls_transparent_test_driver_cipher_operation_t * operation)192 psa_status_t mbedtls_test_transparent_cipher_abort(
193 mbedtls_transparent_test_driver_cipher_operation_t *operation)
194 {
195 mbedtls_test_driver_cipher_hooks.hits++;
196
197 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
198 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
199 libtestdriver1_mbedtls_psa_cipher_abort( operation );
200 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
201 mbedtls_psa_cipher_abort( operation );
202 #endif
203
204 /* Wiping the entire struct here, instead of member-by-member. This is
205 * useful for the test suite, since it gives a chance of catching memory
206 * corruption errors should the core not have allocated (enough) memory for
207 * our context struct. */
208 memset( operation, 0, sizeof( *operation ) );
209
210 return( mbedtls_test_driver_cipher_hooks.forced_status );
211 }
212
mbedtls_test_transparent_cipher_set_iv(mbedtls_transparent_test_driver_cipher_operation_t * operation,const uint8_t * iv,size_t iv_length)213 psa_status_t mbedtls_test_transparent_cipher_set_iv(
214 mbedtls_transparent_test_driver_cipher_operation_t *operation,
215 const uint8_t *iv,
216 size_t iv_length)
217 {
218 mbedtls_test_driver_cipher_hooks.hits++;
219
220 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
221 return( mbedtls_test_driver_cipher_hooks.forced_status );
222
223 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
224 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
225 return( libtestdriver1_mbedtls_psa_cipher_set_iv(
226 operation, iv, iv_length ) );
227 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
228 return( mbedtls_psa_cipher_set_iv( operation, iv, iv_length ) );
229 #endif
230
231 return( PSA_ERROR_NOT_SUPPORTED );
232 }
233
mbedtls_test_transparent_cipher_update(mbedtls_transparent_test_driver_cipher_operation_t * operation,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)234 psa_status_t mbedtls_test_transparent_cipher_update(
235 mbedtls_transparent_test_driver_cipher_operation_t *operation,
236 const uint8_t *input,
237 size_t input_length,
238 uint8_t *output,
239 size_t output_size,
240 size_t *output_length)
241 {
242 mbedtls_test_driver_cipher_hooks.hits++;
243
244 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
245 {
246 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
247 return PSA_ERROR_BUFFER_TOO_SMALL;
248
249 memcpy( output,
250 mbedtls_test_driver_cipher_hooks.forced_output,
251 mbedtls_test_driver_cipher_hooks.forced_output_length );
252 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
253
254 return( mbedtls_test_driver_cipher_hooks.forced_status );
255 }
256
257 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
258 return( mbedtls_test_driver_cipher_hooks.forced_status );
259
260 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
261 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
262 return( libtestdriver1_mbedtls_psa_cipher_update(
263 operation, input, input_length,
264 output, output_size, output_length ) );
265 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
266 return( mbedtls_psa_cipher_update(
267 operation, input, input_length,
268 output, output_size, output_length ) );
269 #endif
270
271 return( PSA_ERROR_NOT_SUPPORTED );
272 }
273
mbedtls_test_transparent_cipher_finish(mbedtls_transparent_test_driver_cipher_operation_t * operation,uint8_t * output,size_t output_size,size_t * output_length)274 psa_status_t mbedtls_test_transparent_cipher_finish(
275 mbedtls_transparent_test_driver_cipher_operation_t *operation,
276 uint8_t *output,
277 size_t output_size,
278 size_t *output_length)
279 {
280 mbedtls_test_driver_cipher_hooks.hits++;
281
282 if( mbedtls_test_driver_cipher_hooks.forced_output != NULL )
283 {
284 if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length )
285 return PSA_ERROR_BUFFER_TOO_SMALL;
286
287 memcpy( output,
288 mbedtls_test_driver_cipher_hooks.forced_output,
289 mbedtls_test_driver_cipher_hooks.forced_output_length );
290 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length;
291
292 return( mbedtls_test_driver_cipher_hooks.forced_status );
293 }
294
295 if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS )
296 return( mbedtls_test_driver_cipher_hooks.forced_status );
297
298 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
299 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
300 return( libtestdriver1_mbedtls_psa_cipher_finish(
301 operation, output, output_size, output_length ) );
302 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER)
303 return( mbedtls_psa_cipher_finish(
304 operation, output, output_size, output_length ) );
305 #endif
306
307 return( PSA_ERROR_NOT_SUPPORTED );
308 }
309
310 /*
311 * opaque versions, to do
312 */
mbedtls_test_opaque_cipher_encrypt(const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg,const uint8_t * iv,size_t iv_length,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)313 psa_status_t mbedtls_test_opaque_cipher_encrypt(
314 const psa_key_attributes_t *attributes,
315 const uint8_t *key, size_t key_length,
316 psa_algorithm_t alg,
317 const uint8_t *iv, size_t iv_length,
318 const uint8_t *input, size_t input_length,
319 uint8_t *output, size_t output_size, size_t *output_length)
320 {
321 (void) attributes;
322 (void) key;
323 (void) key_length;
324 (void) alg;
325 (void) iv;
326 (void) iv_length;
327 (void) input;
328 (void) input_length;
329 (void) output;
330 (void) output_size;
331 (void) output_length;
332 return( PSA_ERROR_NOT_SUPPORTED );
333 }
334
mbedtls_test_opaque_cipher_decrypt(const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)335 psa_status_t mbedtls_test_opaque_cipher_decrypt(
336 const psa_key_attributes_t *attributes,
337 const uint8_t *key, size_t key_length,
338 psa_algorithm_t alg,
339 const uint8_t *input, size_t input_length,
340 uint8_t *output, size_t output_size, size_t *output_length)
341 {
342 (void) attributes;
343 (void) key;
344 (void) key_length;
345 (void) alg;
346 (void) input;
347 (void) input_length;
348 (void) output;
349 (void) output_size;
350 (void) output_length;
351 return( PSA_ERROR_NOT_SUPPORTED );
352 }
353
mbedtls_test_opaque_cipher_encrypt_setup(mbedtls_opaque_test_driver_cipher_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg)354 psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
355 mbedtls_opaque_test_driver_cipher_operation_t *operation,
356 const psa_key_attributes_t *attributes,
357 const uint8_t *key, size_t key_length,
358 psa_algorithm_t alg)
359 {
360 (void) operation;
361 (void) attributes;
362 (void) key;
363 (void) key_length;
364 (void) alg;
365 return( PSA_ERROR_NOT_SUPPORTED );
366 }
367
mbedtls_test_opaque_cipher_decrypt_setup(mbedtls_opaque_test_driver_cipher_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,psa_algorithm_t alg)368 psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
369 mbedtls_opaque_test_driver_cipher_operation_t *operation,
370 const psa_key_attributes_t *attributes,
371 const uint8_t *key, size_t key_length,
372 psa_algorithm_t alg)
373 {
374 (void) operation;
375 (void) attributes;
376 (void) key;
377 (void) key_length;
378 (void) alg;
379 return( PSA_ERROR_NOT_SUPPORTED );
380 }
381
mbedtls_test_opaque_cipher_abort(mbedtls_opaque_test_driver_cipher_operation_t * operation)382 psa_status_t mbedtls_test_opaque_cipher_abort(
383 mbedtls_opaque_test_driver_cipher_operation_t *operation )
384 {
385 (void) operation;
386 return( PSA_ERROR_NOT_SUPPORTED );
387 }
388
mbedtls_test_opaque_cipher_set_iv(mbedtls_opaque_test_driver_cipher_operation_t * operation,const uint8_t * iv,size_t iv_length)389 psa_status_t mbedtls_test_opaque_cipher_set_iv(
390 mbedtls_opaque_test_driver_cipher_operation_t *operation,
391 const uint8_t *iv,
392 size_t iv_length)
393 {
394 (void) operation;
395 (void) iv;
396 (void) iv_length;
397 return( PSA_ERROR_NOT_SUPPORTED );
398 }
399
mbedtls_test_opaque_cipher_update(mbedtls_opaque_test_driver_cipher_operation_t * operation,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)400 psa_status_t mbedtls_test_opaque_cipher_update(
401 mbedtls_opaque_test_driver_cipher_operation_t *operation,
402 const uint8_t *input,
403 size_t input_length,
404 uint8_t *output,
405 size_t output_size,
406 size_t *output_length)
407 {
408 (void) operation;
409 (void) input;
410 (void) input_length;
411 (void) output;
412 (void) output_size;
413 (void) output_length;
414 return( PSA_ERROR_NOT_SUPPORTED );
415 }
416
mbedtls_test_opaque_cipher_finish(mbedtls_opaque_test_driver_cipher_operation_t * operation,uint8_t * output,size_t output_size,size_t * output_length)417 psa_status_t mbedtls_test_opaque_cipher_finish(
418 mbedtls_opaque_test_driver_cipher_operation_t *operation,
419 uint8_t *output,
420 size_t output_size,
421 size_t *output_length)
422 {
423 (void) operation;
424 (void) output;
425 (void) output_size;
426 (void) output_length;
427 return( PSA_ERROR_NOT_SUPPORTED );
428 }
429 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
430