blob: 06efa983af0d9a605d06a9a3a76f82b7998e2645 [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>
Ronald Cron8d310ad2020-12-15 15:17:20 +010031#include <psa/crypto.h>
Steven Cooreman37941cb2020-07-28 18:49:51 +020032
33#include "mbedtls/cipher.h"
Ronald Cron8d310ad2020-12-15 15:17:20 +010034typedef psa_cipher_operation_t test_transparent_cipher_operation_t;
Steven Cooreman37941cb2020-07-28 18:49:51 +020035
36typedef struct{
37 unsigned int initialised : 1;
38 test_transparent_cipher_operation_t ctx;
39} test_opaque_cipher_operation_t;
40
Steven Cooremanacb5a102020-09-08 14:06:57 +020041typedef struct {
42 /* If non-null, on success, copy this to the output. */
43 void *forced_output;
44 size_t forced_output_length;
45 /* If not PSA_SUCCESS, return this error code instead of processing the
46 * function call. */
47 psa_status_t forced_status;
Steven Cooreman5240e8b2020-09-09 11:51:45 +020048 /* Count the amount of times one of the cipher driver functions is called. */
Steven Cooremanacb5a102020-09-08 14:06:57 +020049 unsigned long hits;
50} test_driver_cipher_hooks_t;
Steven Cooreman37941cb2020-07-28 18:49:51 +020051
Steven Cooremanacb5a102020-09-08 14:06:57 +020052#define TEST_DRIVER_CIPHER_INIT { NULL, 0, PSA_SUCCESS, 0 }
53static inline test_driver_cipher_hooks_t test_driver_cipher_hooks_init( void )
54{
55 const test_driver_cipher_hooks_t v = TEST_DRIVER_CIPHER_INIT;
56 return( v );
57}
58
59extern test_driver_cipher_hooks_t test_driver_cipher_hooks;
Steven Cooreman37941cb2020-07-28 18:49:51 +020060
61psa_status_t test_transparent_cipher_encrypt(
62 const psa_key_attributes_t *attributes,
63 const uint8_t *key, size_t key_length,
64 psa_algorithm_t alg,
65 const uint8_t *input, size_t input_length,
66 uint8_t *output, size_t output_size, size_t *output_length);
67
68psa_status_t test_transparent_cipher_decrypt(
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_encrypt_setup(
76 test_transparent_cipher_operation_t *operation,
77 const psa_key_attributes_t *attributes,
78 const uint8_t *key, size_t key_length,
79 psa_algorithm_t alg);
80
81psa_status_t test_transparent_cipher_decrypt_setup(
82 test_transparent_cipher_operation_t *operation,
83 const psa_key_attributes_t *attributes,
84 const uint8_t *key, size_t key_length,
85 psa_algorithm_t alg);
86
87psa_status_t test_transparent_cipher_abort(
88 test_transparent_cipher_operation_t *operation);
89
90psa_status_t test_transparent_cipher_generate_iv(
91 test_transparent_cipher_operation_t *operation,
92 uint8_t *iv,
93 size_t iv_size,
94 size_t *iv_length);
95
96psa_status_t test_transparent_cipher_set_iv(
97 test_transparent_cipher_operation_t *operation,
98 const uint8_t *iv,
99 size_t iv_length);
100
101psa_status_t test_transparent_cipher_update(
102 test_transparent_cipher_operation_t *operation,
103 const uint8_t *input,
104 size_t input_length,
105 uint8_t *output,
106 size_t output_size,
107 size_t *output_length);
108
109psa_status_t test_transparent_cipher_finish(
110 test_transparent_cipher_operation_t *operation,
111 uint8_t *output,
112 size_t output_size,
113 size_t *output_length);
114
115/*
116 * opaque versions
117 */
118psa_status_t test_opaque_cipher_encrypt(
119 const psa_key_attributes_t *attributes,
120 const uint8_t *key, size_t key_length,
121 psa_algorithm_t alg,
122 const uint8_t *input, size_t input_length,
123 uint8_t *output, size_t output_size, size_t *output_length);
124
125psa_status_t test_opaque_cipher_decrypt(
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_encrypt_setup(
133 test_opaque_cipher_operation_t *operation,
134 const psa_key_attributes_t *attributes,
135 const uint8_t *key, size_t key_length,
136 psa_algorithm_t alg);
137
138psa_status_t test_opaque_cipher_decrypt_setup(
139 test_opaque_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
144psa_status_t test_opaque_cipher_abort(
145 test_opaque_cipher_operation_t *operation);
146
147psa_status_t test_opaque_cipher_generate_iv(
148 test_opaque_cipher_operation_t *operation,
149 uint8_t *iv,
150 size_t iv_size,
151 size_t *iv_length);
152
153psa_status_t test_opaque_cipher_set_iv(
154 test_opaque_cipher_operation_t *operation,
155 const uint8_t *iv,
156 size_t iv_length);
157
158psa_status_t test_opaque_cipher_update(
159 test_opaque_cipher_operation_t *operation,
160 const uint8_t *input,
161 size_t input_length,
162 uint8_t *output,
163 size_t output_size,
164 size_t *output_length);
165
166psa_status_t test_opaque_cipher_finish(
167 test_opaque_cipher_operation_t *operation,
168 uint8_t *output,
169 size_t output_size,
170 size_t *output_length);
171
172#endif /* PSA_CRYPTO_DRIVER_TEST */
173#endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */