blob: 96ab295561d39f7b5f8093eba4a3c0f355fa92a2 [file] [log] [blame]
Steven Cooreman37941cb2020-07-28 18:49:51 +02001/*
2 * Test driver for cipher functions
3 */
Steven Cooreman3ec40182020-09-02 16:27:46 +02004/* Copyright The Mbed TLS Contributors
Steven Cooreman37941cb2020-07-28 18:49:51 +02005 * 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.
Steven Cooreman37941cb2020-07-28 18:49:51 +020018 */
19
20#ifndef PSA_CRYPTO_TEST_DRIVERS_CIPHER_H
21#define PSA_CRYPTO_TEST_DRIVERS_CIPHER_H
22
23#if !defined(MBEDTLS_CONFIG_FILE)
24#include "mbedtls/config.h"
25#else
26#include MBEDTLS_CONFIG_FILE
27#endif
28
29#if defined(PSA_CRYPTO_DRIVER_TEST)
30#include <psa/crypto_driver_common.h>
31
32#include "mbedtls/cipher.h"
33typedef struct {
34 psa_algorithm_t alg;
35 unsigned int key_set : 1;
36 unsigned int iv_required : 1;
37 unsigned int iv_set : 1;
38 uint8_t iv_size;
39 uint8_t block_size;
40 mbedtls_cipher_context_t cipher;
41} test_transparent_cipher_operation_t;
42
43typedef struct{
44 unsigned int initialised : 1;
45 test_transparent_cipher_operation_t ctx;
46} test_opaque_cipher_operation_t;
47
Steven Cooremanacb5a102020-09-08 14:06:57 +020048typedef struct {
49 /* If non-null, on success, copy this to the output. */
50 void *forced_output;
51 size_t forced_output_length;
52 /* If not PSA_SUCCESS, return this error code instead of processing the
53 * function call. */
54 psa_status_t forced_status;
55 /* Count the amount of times one of the keygen driver functions is called. */
56 unsigned long hits;
57} test_driver_cipher_hooks_t;
Steven Cooreman37941cb2020-07-28 18:49:51 +020058
Steven Cooremanacb5a102020-09-08 14:06:57 +020059#define TEST_DRIVER_CIPHER_INIT { NULL, 0, PSA_SUCCESS, 0 }
60static inline test_driver_cipher_hooks_t test_driver_cipher_hooks_init( void )
61{
62 const test_driver_cipher_hooks_t v = TEST_DRIVER_CIPHER_INIT;
63 return( v );
64}
65
66extern test_driver_cipher_hooks_t test_driver_cipher_hooks;
Steven Cooreman37941cb2020-07-28 18:49:51 +020067
68psa_status_t test_transparent_cipher_encrypt(
69 const psa_key_attributes_t *attributes,
70 const uint8_t *key, size_t key_length,
71 psa_algorithm_t alg,
72 const uint8_t *input, size_t input_length,
73 uint8_t *output, size_t output_size, size_t *output_length);
74
75psa_status_t test_transparent_cipher_decrypt(
76 const psa_key_attributes_t *attributes,
77 const uint8_t *key, size_t key_length,
78 psa_algorithm_t alg,
79 const uint8_t *input, size_t input_length,
80 uint8_t *output, size_t output_size, size_t *output_length);
81
82psa_status_t test_transparent_cipher_encrypt_setup(
83 test_transparent_cipher_operation_t *operation,
84 const psa_key_attributes_t *attributes,
85 const uint8_t *key, size_t key_length,
86 psa_algorithm_t alg);
87
88psa_status_t test_transparent_cipher_decrypt_setup(
89 test_transparent_cipher_operation_t *operation,
90 const psa_key_attributes_t *attributes,
91 const uint8_t *key, size_t key_length,
92 psa_algorithm_t alg);
93
94psa_status_t test_transparent_cipher_abort(
95 test_transparent_cipher_operation_t *operation);
96
97psa_status_t test_transparent_cipher_generate_iv(
98 test_transparent_cipher_operation_t *operation,
99 uint8_t *iv,
100 size_t iv_size,
101 size_t *iv_length);
102
103psa_status_t test_transparent_cipher_set_iv(
104 test_transparent_cipher_operation_t *operation,
105 const uint8_t *iv,
106 size_t iv_length);
107
108psa_status_t test_transparent_cipher_update(
109 test_transparent_cipher_operation_t *operation,
110 const uint8_t *input,
111 size_t input_length,
112 uint8_t *output,
113 size_t output_size,
114 size_t *output_length);
115
116psa_status_t test_transparent_cipher_finish(
117 test_transparent_cipher_operation_t *operation,
118 uint8_t *output,
119 size_t output_size,
120 size_t *output_length);
121
122/*
123 * opaque versions
124 */
125psa_status_t test_opaque_cipher_encrypt(
126 const psa_key_attributes_t *attributes,
127 const uint8_t *key, size_t key_length,
128 psa_algorithm_t alg,
129 const uint8_t *input, size_t input_length,
130 uint8_t *output, size_t output_size, size_t *output_length);
131
132psa_status_t test_opaque_cipher_decrypt(
133 const psa_key_attributes_t *attributes,
134 const uint8_t *key, size_t key_length,
135 psa_algorithm_t alg,
136 const uint8_t *input, size_t input_length,
137 uint8_t *output, size_t output_size, size_t *output_length);
138
139psa_status_t test_opaque_cipher_encrypt_setup(
140 test_opaque_cipher_operation_t *operation,
141 const psa_key_attributes_t *attributes,
142 const uint8_t *key, size_t key_length,
143 psa_algorithm_t alg);
144
145psa_status_t test_opaque_cipher_decrypt_setup(
146 test_opaque_cipher_operation_t *operation,
147 const psa_key_attributes_t *attributes,
148 const uint8_t *key, size_t key_length,
149 psa_algorithm_t alg);
150
151psa_status_t test_opaque_cipher_abort(
152 test_opaque_cipher_operation_t *operation);
153
154psa_status_t test_opaque_cipher_generate_iv(
155 test_opaque_cipher_operation_t *operation,
156 uint8_t *iv,
157 size_t iv_size,
158 size_t *iv_length);
159
160psa_status_t test_opaque_cipher_set_iv(
161 test_opaque_cipher_operation_t *operation,
162 const uint8_t *iv,
163 size_t iv_length);
164
165psa_status_t test_opaque_cipher_update(
166 test_opaque_cipher_operation_t *operation,
167 const uint8_t *input,
168 size_t input_length,
169 uint8_t *output,
170 size_t output_size,
171 size_t *output_length);
172
173psa_status_t test_opaque_cipher_finish(
174 test_opaque_cipher_operation_t *operation,
175 uint8_t *output,
176 size_t output_size,
177 size_t *output_length);
178
179#endif /* PSA_CRYPTO_DRIVER_TEST */
180#endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */