1 /*
2  * Test driver for cipher functions
3  */
4 /*  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19 
20 #ifndef PSA_CRYPTO_TEST_DRIVERS_CIPHER_H
21 #define PSA_CRYPTO_TEST_DRIVERS_CIPHER_H
22 
23 #include "mbedtls/build_info.h"
24 
25 #if defined(PSA_CRYPTO_DRIVER_TEST)
26 #include <psa/crypto_driver_common.h>
27 #include <psa/crypto.h>
28 
29 #include "mbedtls/cipher.h"
30 
31 typedef struct {
32     /* If non-null, on success, copy this to the output. */
33     void *forced_output;
34     size_t forced_output_length;
35     /* If not PSA_SUCCESS, return this error code instead of processing the
36      * function call. */
37     psa_status_t forced_status;
38     /* Count the amount of times one of the cipher driver functions is called. */
39     unsigned long hits;
40 } mbedtls_test_driver_cipher_hooks_t;
41 
42 #define MBEDTLS_TEST_DRIVER_CIPHER_INIT { NULL, 0, PSA_SUCCESS, 0 }
43 static inline mbedtls_test_driver_cipher_hooks_t
mbedtls_test_driver_cipher_hooks_init(void)44      mbedtls_test_driver_cipher_hooks_init( void )
45 {
46     const mbedtls_test_driver_cipher_hooks_t v = MBEDTLS_TEST_DRIVER_CIPHER_INIT;
47     return( v );
48 }
49 
50 extern mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks;
51 
52 psa_status_t mbedtls_test_transparent_cipher_encrypt(
53     const psa_key_attributes_t *attributes,
54     const uint8_t *key, size_t key_length,
55     psa_algorithm_t alg,
56     const uint8_t *iv, size_t iv_length,
57     const uint8_t *input, size_t input_length,
58     uint8_t *output, size_t output_size, size_t *output_length);
59 
60 psa_status_t mbedtls_test_transparent_cipher_decrypt(
61     const psa_key_attributes_t *attributes,
62     const uint8_t *key, size_t key_length,
63     psa_algorithm_t alg,
64     const uint8_t *input, size_t input_length,
65     uint8_t *output, size_t output_size, size_t *output_length);
66 
67 psa_status_t mbedtls_test_transparent_cipher_encrypt_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_decrypt_setup(
74     mbedtls_transparent_test_driver_cipher_operation_t *operation,
75     const psa_key_attributes_t *attributes,
76     const uint8_t *key, size_t key_length,
77     psa_algorithm_t alg);
78 
79 psa_status_t mbedtls_test_transparent_cipher_abort(
80     mbedtls_transparent_test_driver_cipher_operation_t *operation );
81 
82 psa_status_t mbedtls_test_transparent_cipher_set_iv(
83     mbedtls_transparent_test_driver_cipher_operation_t *operation,
84     const uint8_t *iv, size_t iv_length);
85 
86 psa_status_t mbedtls_test_transparent_cipher_update(
87     mbedtls_transparent_test_driver_cipher_operation_t *operation,
88     const uint8_t *input, size_t input_length,
89     uint8_t *output, size_t output_size, size_t *output_length);
90 
91 psa_status_t mbedtls_test_transparent_cipher_finish(
92     mbedtls_transparent_test_driver_cipher_operation_t *operation,
93     uint8_t *output, size_t output_size, size_t *output_length);
94 
95 /*
96  * opaque versions
97  */
98 psa_status_t mbedtls_test_opaque_cipher_encrypt(
99     const psa_key_attributes_t *attributes,
100     const uint8_t *key, size_t key_length,
101     psa_algorithm_t alg,
102     const uint8_t *iv, size_t iv_length,
103     const uint8_t *input, size_t input_length,
104     uint8_t *output, size_t output_size, size_t *output_length);
105 
106 psa_status_t mbedtls_test_opaque_cipher_decrypt(
107     const psa_key_attributes_t *attributes,
108     const uint8_t *key, size_t key_length,
109     psa_algorithm_t alg,
110     const uint8_t *input, size_t input_length,
111     uint8_t *output, size_t output_size, size_t *output_length);
112 
113 psa_status_t mbedtls_test_opaque_cipher_encrypt_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_decrypt_setup(
120     mbedtls_opaque_test_driver_cipher_operation_t *operation,
121     const psa_key_attributes_t *attributes,
122     const uint8_t *key, size_t key_length,
123     psa_algorithm_t alg);
124 
125 psa_status_t mbedtls_test_opaque_cipher_abort(
126     mbedtls_opaque_test_driver_cipher_operation_t *operation);
127 
128 psa_status_t mbedtls_test_opaque_cipher_set_iv(
129     mbedtls_opaque_test_driver_cipher_operation_t *operation,
130     const uint8_t *iv, size_t iv_length);
131 
132 psa_status_t mbedtls_test_opaque_cipher_update(
133     mbedtls_opaque_test_driver_cipher_operation_t *operation,
134     const uint8_t *input, size_t input_length,
135     uint8_t *output, size_t output_size, size_t *output_length);
136 
137 psa_status_t mbedtls_test_opaque_cipher_finish(
138     mbedtls_opaque_test_driver_cipher_operation_t *operation,
139     uint8_t *output, size_t output_size, size_t *output_length);
140 
141 #endif /* PSA_CRYPTO_DRIVER_TEST */
142 #endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */
143