blob: 67118efcbe219554f38164fcd4c2c71e1c1925e3 [file] [log] [blame]
Ronald Cronde822812021-03-17 16:08:20 +01001/*
2 * Test driver for AEAD entry points.
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#if !defined(MBEDTLS_CONFIG_FILE)
21#include "mbedtls/config.h"
22#else
23#include MBEDTLS_CONFIG_FILE
24#endif
25
26#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
27#include "psa_crypto_aead.h"
28
29#include "test/drivers/aead.h"
30
Ronald Cron7f13fa22021-04-13 12:41:34 +020031mbedtls_test_driver_aead_hooks_t
32 mbedtls_test_driver_aead_hooks = MBEDTLS_TEST_DRIVER_AEAD_INIT;
Ronald Cronbfe551d2021-03-23 09:33:25 +010033
Ronald Cron7f13fa22021-04-13 12:41:34 +020034psa_status_t mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +010035 const psa_key_attributes_t *attributes,
36 const uint8_t *key_buffer, size_t key_buffer_size,
37 psa_algorithm_t alg,
38 const uint8_t *nonce, size_t nonce_length,
39 const uint8_t *additional_data, size_t additional_data_length,
40 const uint8_t *plaintext, size_t plaintext_length,
41 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
42{
Ronald Cron7f13fa22021-04-13 12:41:34 +020043 mbedtls_test_driver_aead_hooks.hits++;
Ronald Cronbfe551d2021-03-23 09:33:25 +010044
Ronald Cron7f13fa22021-04-13 12:41:34 +020045 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Ronald Cronbfe551d2021-03-23 09:33:25 +010046 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020047 mbedtls_test_driver_aead_hooks.driver_status =
48 mbedtls_test_driver_aead_hooks.forced_status;
Ronald Cronbfe551d2021-03-23 09:33:25 +010049 }
50 else
51 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020052 mbedtls_test_driver_aead_hooks.driver_status =
Ronald Cronbfe551d2021-03-23 09:33:25 +010053 mbedtls_psa_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +010054 attributes, key_buffer, key_buffer_size,
55 alg,
56 nonce, nonce_length,
57 additional_data, additional_data_length,
58 plaintext, plaintext_length,
Ronald Cronbfe551d2021-03-23 09:33:25 +010059 ciphertext, ciphertext_size, ciphertext_length );
60 }
61
Ronald Cron7f13fa22021-04-13 12:41:34 +020062 return( mbedtls_test_driver_aead_hooks.driver_status );
Ronald Cronde822812021-03-17 16:08:20 +010063}
64
Ronald Cron7f13fa22021-04-13 12:41:34 +020065psa_status_t mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +010066 const psa_key_attributes_t *attributes,
67 const uint8_t *key_buffer, size_t key_buffer_size,
68 psa_algorithm_t alg,
69 const uint8_t *nonce, size_t nonce_length,
70 const uint8_t *additional_data, size_t additional_data_length,
71 const uint8_t *ciphertext, size_t ciphertext_length,
72 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
73{
Ronald Cron7f13fa22021-04-13 12:41:34 +020074 mbedtls_test_driver_aead_hooks.hits++;
Ronald Cronbfe551d2021-03-23 09:33:25 +010075
Ronald Cron7f13fa22021-04-13 12:41:34 +020076 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Ronald Cronbfe551d2021-03-23 09:33:25 +010077 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020078 mbedtls_test_driver_aead_hooks.driver_status =
79 mbedtls_test_driver_aead_hooks.forced_status;
Ronald Cronbfe551d2021-03-23 09:33:25 +010080 }
81 else
82 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020083 mbedtls_test_driver_aead_hooks.driver_status =
Ronald Cronbfe551d2021-03-23 09:33:25 +010084 mbedtls_psa_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +010085 attributes, key_buffer, key_buffer_size,
86 alg,
87 nonce, nonce_length,
88 additional_data, additional_data_length,
89 ciphertext, ciphertext_length,
Ronald Cronbfe551d2021-03-23 09:33:25 +010090 plaintext, plaintext_size, plaintext_length );
91 }
92
Ronald Cron7f13fa22021-04-13 12:41:34 +020093 return( mbedtls_test_driver_aead_hooks.driver_status );
Ronald Cronde822812021-03-17 16:08:20 +010094}
95
Paul Elliott4bbe82b2021-04-27 12:11:56 +010096psa_status_t test_transparent_aead_encrypt_setup(
97 psa_aead_operation_t *operation,
98 const psa_key_attributes_t *attributes,
99 const uint8_t *key_buffer, size_t key_buffer_size,
100 psa_algorithm_t alg )
101{
102 test_driver_aead_hooks.hits++;
103
104 if( test_driver_aead_hooks.forced_status != PSA_SUCCESS )
105 {
106 test_driver_aead_hooks.driver_status =
107 test_driver_aead_hooks.forced_status;
108 }
109 else
110 {
111 test_driver_aead_hooks.driver_status =
112 mbedtls_psa_aead_encrypt_setup( operation, attributes, key_buffer,
113 key_buffer_size, alg );
114 }
115
116 return( test_driver_aead_hooks.driver_status );
117}
118
119psa_status_t test_transparent_aead_decrypt_setup(
120 psa_aead_operation_t *operation,
121 const psa_key_attributes_t *attributes,
122 const uint8_t *key_buffer, size_t key_buffer_size,
123 psa_algorithm_t alg )
124{
125 test_driver_aead_hooks.hits++;
126
127 if( test_driver_aead_hooks.forced_status != PSA_SUCCESS )
128 {
129 test_driver_aead_hooks.driver_status =
130 test_driver_aead_hooks.forced_status;
131 }
132 else
133 {
134 test_driver_aead_hooks.driver_status =
135 mbedtls_psa_aead_decrypt_setup( operation, attributes, key_buffer,
136 key_buffer_size, alg );
137 }
138
139 return( test_driver_aead_hooks.driver_status );
140}
141
142psa_status_t test_transparent_aead_set_nonce(
143 psa_aead_operation_t *operation,
144 const uint8_t *nonce,
145 size_t nonce_length )
146{
147 test_driver_aead_hooks.hits++;
148
149 if( test_driver_aead_hooks.forced_status != PSA_SUCCESS )
150 {
151 test_driver_aead_hooks.driver_status =
152 test_driver_aead_hooks.forced_status;
153 }
154 else
155 {
156 test_driver_aead_hooks.driver_status =
157 mbedtls_psa_aead_set_nonce( operation, nonce, nonce_length );
158 }
159
160 return( test_driver_aead_hooks.driver_status );
161}
162
163psa_status_t test_transparent_aead_set_lengths(
164 psa_aead_operation_t *operation,
165 size_t ad_length,
166 size_t plaintext_length )
167{
168 test_driver_aead_hooks.hits++;
169
170 if( test_driver_aead_hooks.forced_status != PSA_SUCCESS )
171 {
172 test_driver_aead_hooks.driver_status =
173 test_driver_aead_hooks.forced_status;
174 }
175 else
176 {
177 test_driver_aead_hooks.driver_status =
178 mbedtls_psa_aead_set_lengths( operation, ad_length, plaintext_length );
179 }
180
181 return( test_driver_aead_hooks.driver_status );
182}
183
184psa_status_t test_transparent_aead_update_ad(
185 psa_aead_operation_t *operation,
186 const uint8_t *input,
187 size_t input_length )
188{
189 test_driver_aead_hooks.hits++;
190
191 if( test_driver_aead_hooks.forced_status != PSA_SUCCESS )
192 {
193 test_driver_aead_hooks.driver_status =
194 test_driver_aead_hooks.forced_status;
195 }
196 else
197 {
198 test_driver_aead_hooks.driver_status =
199 mbedtls_psa_aead_update_ad( operation, input, input_length );
200 }
201
202 return( test_driver_aead_hooks.driver_status );
203}
204
205psa_status_t test_transparent_aead_update(
206 psa_aead_operation_t *operation,
207 const uint8_t *input,
208 size_t input_length,
209 uint8_t *output,
210 size_t output_size,
211 size_t *output_length )
212{
213 test_driver_aead_hooks.hits++;
214
215 if( test_driver_aead_hooks.forced_status != PSA_SUCCESS )
216 {
217 test_driver_aead_hooks.driver_status =
218 test_driver_aead_hooks.forced_status;
219 }
220 else
221 {
222 test_driver_aead_hooks.driver_status =
223 mbedtls_psa_aead_update( operation, input, input_length, output,
224 output_size, output_length );
225 }
226
227 return( test_driver_aead_hooks.driver_status );
228}
229
230psa_status_t test_transparent_aead_finish(
231 psa_aead_operation_t *operation,
232 uint8_t *ciphertext,
233 size_t ciphertext_size,
234 size_t *ciphertext_length,
235 uint8_t *tag,
236 size_t tag_size,
237 size_t *tag_length )
238{
239 test_driver_aead_hooks.hits++;
240
241 if( test_driver_aead_hooks.forced_status != PSA_SUCCESS )
242 {
243 test_driver_aead_hooks.driver_status =
244 test_driver_aead_hooks.forced_status;
245 }
246 else
247 {
248 test_driver_aead_hooks.driver_status =
249 mbedtls_psa_aead_finish( operation, ciphertext, ciphertext_size,
250 ciphertext_length, tag, tag_size, tag_length );
251 }
252
253 return( test_driver_aead_hooks.driver_status );
254}
255
256psa_status_t test_transparent_aead_verify(
257 psa_aead_operation_t *operation,
258 uint8_t *plaintext,
259 size_t plaintext_size,
260 size_t *plaintext_length,
261 const uint8_t *tag,
262 size_t tag_length )
263{
264 test_driver_aead_hooks.hits++;
265
266 if( test_driver_aead_hooks.forced_status != PSA_SUCCESS )
267 {
268 test_driver_aead_hooks.driver_status =
269 test_driver_aead_hooks.forced_status;
270 }
271 else
272 {
273 test_driver_aead_hooks.driver_status =
274 mbedtls_psa_aead_verify( operation, plaintext, plaintext_size,
275 plaintext_length, tag, tag_length );
276 }
277
278 return( test_driver_aead_hooks.driver_status );
279}
280
281psa_status_t test_transparent_aead_abort(
282 psa_aead_operation_t *operation )
283{
284 test_driver_aead_hooks.hits++;
285
286 if( test_driver_aead_hooks.forced_status != PSA_SUCCESS )
287 {
288 test_driver_aead_hooks.driver_status =
289 test_driver_aead_hooks.forced_status;
290 }
291 else
292 {
293 test_driver_aead_hooks.driver_status =
294 mbedtls_psa_aead_abort( operation );
295 }
296
297 return( test_driver_aead_hooks.driver_status );
298}
299
Ronald Cronde822812021-03-17 16:08:20 +0100300#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */