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 /* Count the amount of times one of the cipher driver functions is called. */
27 unsigned long hits;
28 } mbedtls_test_driver_cipher_hooks_t;
29
30 #define MBEDTLS_TEST_DRIVER_CIPHER_INIT { NULL, 0, PSA_SUCCESS, 0 }
31 static inline mbedtls_test_driver_cipher_hooks_t
mbedtls_test_driver_cipher_hooks_init(void)32 mbedtls_test_driver_cipher_hooks_init(void)
33 {
34 const mbedtls_test_driver_cipher_hooks_t v = MBEDTLS_TEST_DRIVER_CIPHER_INIT;
35 return v;
36 }
37
38 extern mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks;
39
40 psa_status_t mbedtls_test_transparent_cipher_encrypt(
41 const psa_key_attributes_t *attributes,
42 const uint8_t *key, size_t key_length,
43 psa_algorithm_t alg,
44 const uint8_t *iv, size_t iv_length,
45 const uint8_t *input, size_t input_length,
46 uint8_t *output, size_t output_size, size_t *output_length);
47
48 psa_status_t mbedtls_test_transparent_cipher_decrypt(
49 const psa_key_attributes_t *attributes,
50 const uint8_t *key, size_t key_length,
51 psa_algorithm_t alg,
52 const uint8_t *input, size_t input_length,
53 uint8_t *output, size_t output_size, size_t *output_length);
54
55 psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
56 mbedtls_transparent_test_driver_cipher_operation_t *operation,
57 const psa_key_attributes_t *attributes,
58 const uint8_t *key, size_t key_length,
59 psa_algorithm_t alg);
60
61 psa_status_t mbedtls_test_transparent_cipher_decrypt_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_abort(
68 mbedtls_transparent_test_driver_cipher_operation_t *operation);
69
70 psa_status_t mbedtls_test_transparent_cipher_set_iv(
71 mbedtls_transparent_test_driver_cipher_operation_t *operation,
72 const uint8_t *iv, size_t iv_length);
73
74 psa_status_t mbedtls_test_transparent_cipher_update(
75 mbedtls_transparent_test_driver_cipher_operation_t *operation,
76 const uint8_t *input, size_t input_length,
77 uint8_t *output, size_t output_size, size_t *output_length);
78
79 psa_status_t mbedtls_test_transparent_cipher_finish(
80 mbedtls_transparent_test_driver_cipher_operation_t *operation,
81 uint8_t *output, size_t output_size, size_t *output_length);
82
83 /*
84 * opaque versions
85 */
86 psa_status_t mbedtls_test_opaque_cipher_encrypt(
87 const psa_key_attributes_t *attributes,
88 const uint8_t *key, size_t key_length,
89 psa_algorithm_t alg,
90 const uint8_t *iv, size_t iv_length,
91 const uint8_t *input, size_t input_length,
92 uint8_t *output, size_t output_size, size_t *output_length);
93
94 psa_status_t mbedtls_test_opaque_cipher_decrypt(
95 const psa_key_attributes_t *attributes,
96 const uint8_t *key, size_t key_length,
97 psa_algorithm_t alg,
98 const uint8_t *input, size_t input_length,
99 uint8_t *output, size_t output_size, size_t *output_length);
100
101 psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
102 mbedtls_opaque_test_driver_cipher_operation_t *operation,
103 const psa_key_attributes_t *attributes,
104 const uint8_t *key, size_t key_length,
105 psa_algorithm_t alg);
106
107 psa_status_t mbedtls_test_opaque_cipher_decrypt_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_abort(
114 mbedtls_opaque_test_driver_cipher_operation_t *operation);
115
116 psa_status_t mbedtls_test_opaque_cipher_set_iv(
117 mbedtls_opaque_test_driver_cipher_operation_t *operation,
118 const uint8_t *iv, size_t iv_length);
119
120 psa_status_t mbedtls_test_opaque_cipher_update(
121 mbedtls_opaque_test_driver_cipher_operation_t *operation,
122 const uint8_t *input, size_t input_length,
123 uint8_t *output, size_t output_size, size_t *output_length);
124
125 psa_status_t mbedtls_test_opaque_cipher_finish(
126 mbedtls_opaque_test_driver_cipher_operation_t *operation,
127 uint8_t *output, size_t output_size, size_t *output_length);
128
129 #endif /* PSA_CRYPTO_DRIVER_TEST */
130 #endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */
131