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