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