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