1 /*
2  * Test driver for cipher functions
3  */
4 /*  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  */
7 
8 #ifndef PSA_CRYPTO_TEST_DRIVERS_CIPHER_H
9 #define PSA_CRYPTO_TEST_DRIVERS_CIPHER_H
10 
11 #include "mbedtls/build_info.h"
12 
13 #if defined(PSA_CRYPTO_DRIVER_TEST)
14 #include <psa/crypto_driver_common.h>
15 #include <psa/crypto.h>
16 
17 #include "mbedtls/cipher.h"
18 
19 typedef struct {
20     /* If non-null, on success, copy this to the output. */
21     void *forced_output;
22     size_t forced_output_length;
23     /* If not PSA_SUCCESS, return this error code instead of processing the
24      * function call. */
25     psa_status_t forced_status;
26     psa_status_t forced_status_encrypt;
27     psa_status_t forced_status_set_iv;
28     /* Count the amount of times one of the cipher driver functions is called. */
29     unsigned long hits;
30     unsigned long hits_encrypt;
31     unsigned long hits_set_iv;
32 } mbedtls_test_driver_cipher_hooks_t;
33 
34 #define MBEDTLS_TEST_DRIVER_CIPHER_INIT { NULL, 0, \
35                                           PSA_SUCCESS, PSA_SUCCESS, PSA_SUCCESS, \
36                                           0, 0, 0 }
37 static inline mbedtls_test_driver_cipher_hooks_t
mbedtls_test_driver_cipher_hooks_init(void)38 mbedtls_test_driver_cipher_hooks_init(void)
39 {
40     const mbedtls_test_driver_cipher_hooks_t v = MBEDTLS_TEST_DRIVER_CIPHER_INIT;
41     return v;
42 }
43 
44 extern mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks;
45 
46 psa_status_t mbedtls_test_transparent_cipher_encrypt(
47     const psa_key_attributes_t *attributes,
48     const uint8_t *key, size_t key_length,
49     psa_algorithm_t alg,
50     const uint8_t *iv, size_t iv_length,
51     const uint8_t *input, size_t input_length,
52     uint8_t *output, size_t output_size, size_t *output_length);
53 
54 psa_status_t mbedtls_test_transparent_cipher_decrypt(
55     const psa_key_attributes_t *attributes,
56     const uint8_t *key, size_t key_length,
57     psa_algorithm_t alg,
58     const uint8_t *input, size_t input_length,
59     uint8_t *output, size_t output_size, size_t *output_length);
60 
61 psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
62     mbedtls_transparent_test_driver_cipher_operation_t *operation,
63     const psa_key_attributes_t *attributes,
64     const uint8_t *key, size_t key_length,
65     psa_algorithm_t alg);
66 
67 psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
68     mbedtls_transparent_test_driver_cipher_operation_t *operation,
69     const psa_key_attributes_t *attributes,
70     const uint8_t *key, size_t key_length,
71     psa_algorithm_t alg);
72 
73 psa_status_t mbedtls_test_transparent_cipher_abort(
74     mbedtls_transparent_test_driver_cipher_operation_t *operation);
75 
76 psa_status_t mbedtls_test_transparent_cipher_set_iv(
77     mbedtls_transparent_test_driver_cipher_operation_t *operation,
78     const uint8_t *iv, size_t iv_length);
79 
80 psa_status_t mbedtls_test_transparent_cipher_update(
81     mbedtls_transparent_test_driver_cipher_operation_t *operation,
82     const uint8_t *input, size_t input_length,
83     uint8_t *output, size_t output_size, size_t *output_length);
84 
85 psa_status_t mbedtls_test_transparent_cipher_finish(
86     mbedtls_transparent_test_driver_cipher_operation_t *operation,
87     uint8_t *output, size_t output_size, size_t *output_length);
88 
89 /*
90  * opaque versions
91  */
92 psa_status_t mbedtls_test_opaque_cipher_encrypt(
93     const psa_key_attributes_t *attributes,
94     const uint8_t *key, size_t key_length,
95     psa_algorithm_t alg,
96     const uint8_t *iv, size_t iv_length,
97     const uint8_t *input, size_t input_length,
98     uint8_t *output, size_t output_size, size_t *output_length);
99 
100 psa_status_t mbedtls_test_opaque_cipher_decrypt(
101     const psa_key_attributes_t *attributes,
102     const uint8_t *key, size_t key_length,
103     psa_algorithm_t alg,
104     const uint8_t *input, size_t input_length,
105     uint8_t *output, size_t output_size, size_t *output_length);
106 
107 psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
108     mbedtls_opaque_test_driver_cipher_operation_t *operation,
109     const psa_key_attributes_t *attributes,
110     const uint8_t *key, size_t key_length,
111     psa_algorithm_t alg);
112 
113 psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
114     mbedtls_opaque_test_driver_cipher_operation_t *operation,
115     const psa_key_attributes_t *attributes,
116     const uint8_t *key, size_t key_length,
117     psa_algorithm_t alg);
118 
119 psa_status_t mbedtls_test_opaque_cipher_abort(
120     mbedtls_opaque_test_driver_cipher_operation_t *operation);
121 
122 psa_status_t mbedtls_test_opaque_cipher_set_iv(
123     mbedtls_opaque_test_driver_cipher_operation_t *operation,
124     const uint8_t *iv, size_t iv_length);
125 
126 psa_status_t mbedtls_test_opaque_cipher_update(
127     mbedtls_opaque_test_driver_cipher_operation_t *operation,
128     const uint8_t *input, size_t input_length,
129     uint8_t *output, size_t output_size, size_t *output_length);
130 
131 psa_status_t mbedtls_test_opaque_cipher_finish(
132     mbedtls_opaque_test_driver_cipher_operation_t *operation,
133     uint8_t *output, size_t output_size, size_t *output_length);
134 
135 #endif /* PSA_CRYPTO_DRIVER_TEST */
136 #endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */
137