blob: ac116ffb06e48a7dab83464a9d96576ff8775e36 [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"
Paul Elliott0a6a5692021-07-23 15:29:21 +010028#include "psa_crypto_core.h"
Ronald Cronde822812021-03-17 16:08:20 +010029
30#include "test/drivers/aead.h"
31
Ronald Cron7f13fa22021-04-13 12:41:34 +020032mbedtls_test_driver_aead_hooks_t
33 mbedtls_test_driver_aead_hooks = MBEDTLS_TEST_DRIVER_AEAD_INIT;
Ronald Cronbfe551d2021-03-23 09:33:25 +010034
Ronald Cron7f13fa22021-04-13 12:41:34 +020035psa_status_t mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +010036 const psa_key_attributes_t *attributes,
37 const uint8_t *key_buffer, size_t key_buffer_size,
38 psa_algorithm_t alg,
39 const uint8_t *nonce, size_t nonce_length,
40 const uint8_t *additional_data, size_t additional_data_length,
41 const uint8_t *plaintext, size_t plaintext_length,
42 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
43{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +010044 mbedtls_test_driver_aead_hooks.hits_encrypt++;
Ronald Cronbfe551d2021-03-23 09:33:25 +010045
Ronald Cron7f13fa22021-04-13 12:41:34 +020046 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Ronald Cronbfe551d2021-03-23 09:33:25 +010047 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020048 mbedtls_test_driver_aead_hooks.driver_status =
49 mbedtls_test_driver_aead_hooks.forced_status;
Ronald Cronbfe551d2021-03-23 09:33:25 +010050 }
51 else
52 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020053 mbedtls_test_driver_aead_hooks.driver_status =
Ronald Cronbfe551d2021-03-23 09:33:25 +010054 mbedtls_psa_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +010055 attributes, key_buffer, key_buffer_size,
56 alg,
57 nonce, nonce_length,
58 additional_data, additional_data_length,
59 plaintext, plaintext_length,
Ronald Cronbfe551d2021-03-23 09:33:25 +010060 ciphertext, ciphertext_size, ciphertext_length );
61 }
62
Ronald Cron7f13fa22021-04-13 12:41:34 +020063 return( mbedtls_test_driver_aead_hooks.driver_status );
Ronald Cronde822812021-03-17 16:08:20 +010064}
65
Ronald Cron7f13fa22021-04-13 12:41:34 +020066psa_status_t mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +010067 const psa_key_attributes_t *attributes,
68 const uint8_t *key_buffer, size_t key_buffer_size,
69 psa_algorithm_t alg,
70 const uint8_t *nonce, size_t nonce_length,
71 const uint8_t *additional_data, size_t additional_data_length,
72 const uint8_t *ciphertext, size_t ciphertext_length,
73 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
74{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +010075 mbedtls_test_driver_aead_hooks.hits_decrypt++;
Ronald Cronbfe551d2021-03-23 09:33:25 +010076
Ronald Cron7f13fa22021-04-13 12:41:34 +020077 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Ronald Cronbfe551d2021-03-23 09:33:25 +010078 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020079 mbedtls_test_driver_aead_hooks.driver_status =
80 mbedtls_test_driver_aead_hooks.forced_status;
Ronald Cronbfe551d2021-03-23 09:33:25 +010081 }
82 else
83 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020084 mbedtls_test_driver_aead_hooks.driver_status =
Ronald Cronbfe551d2021-03-23 09:33:25 +010085 mbedtls_psa_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +010086 attributes, key_buffer, key_buffer_size,
87 alg,
88 nonce, nonce_length,
89 additional_data, additional_data_length,
90 ciphertext, ciphertext_length,
Ronald Cronbfe551d2021-03-23 09:33:25 +010091 plaintext, plaintext_size, plaintext_length );
92 }
93
Ronald Cron7f13fa22021-04-13 12:41:34 +020094 return( mbedtls_test_driver_aead_hooks.driver_status );
Ronald Cronde822812021-03-17 16:08:20 +010095}
96
Paul Elliotta218ceb2021-05-07 15:10:31 +010097psa_status_t mbedtls_test_transparent_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +010098 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +010099 const psa_key_attributes_t *attributes,
100 const uint8_t *key_buffer, size_t key_buffer_size,
101 psa_algorithm_t alg )
102{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100103 mbedtls_test_driver_aead_hooks.hits_encrypt_setup++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100104
Paul Elliotta218ceb2021-05-07 15:10:31 +0100105 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100106 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100107 mbedtls_test_driver_aead_hooks.driver_status =
108 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100109 }
110 else
111 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100112 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100113 mbedtls_psa_aead_encrypt_setup( operation, attributes, key_buffer,
114 key_buffer_size, alg );
115 }
116
Paul Elliotta218ceb2021-05-07 15:10:31 +0100117 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100118}
119
Paul Elliotta218ceb2021-05-07 15:10:31 +0100120psa_status_t mbedtls_test_transparent_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100121 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100122 const psa_key_attributes_t *attributes,
123 const uint8_t *key_buffer, size_t key_buffer_size,
124 psa_algorithm_t alg )
125{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100126 mbedtls_test_driver_aead_hooks.hits_decrypt_setup++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100127
Paul Elliotta218ceb2021-05-07 15:10:31 +0100128 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100129 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100130 mbedtls_test_driver_aead_hooks.driver_status =
131 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100132 }
133 else
134 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100135 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100136 mbedtls_psa_aead_decrypt_setup( operation, attributes, key_buffer,
137 key_buffer_size, alg );
138 }
139
Paul Elliotta218ceb2021-05-07 15:10:31 +0100140 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100141}
142
Paul Elliotta218ceb2021-05-07 15:10:31 +0100143psa_status_t mbedtls_test_transparent_aead_set_nonce(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100144 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100145 const uint8_t *nonce,
146 size_t nonce_length )
147{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100148 mbedtls_test_driver_aead_hooks.hits_set_nonce++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100149
Paul Elliotta218ceb2021-05-07 15:10:31 +0100150 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100151 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100152 mbedtls_test_driver_aead_hooks.driver_status =
153 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100154 }
155 else
156 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100157 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100158 mbedtls_psa_aead_set_nonce( operation, nonce, nonce_length );
159 }
160
Paul Elliotta218ceb2021-05-07 15:10:31 +0100161 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100162}
163
Paul Elliotta218ceb2021-05-07 15:10:31 +0100164psa_status_t mbedtls_test_transparent_aead_set_lengths(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100165 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100166 size_t ad_length,
167 size_t plaintext_length )
168{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100169 mbedtls_test_driver_aead_hooks.hits_set_lengths++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100170
Paul Elliotta218ceb2021-05-07 15:10:31 +0100171 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100172 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100173 mbedtls_test_driver_aead_hooks.driver_status =
174 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100175 }
176 else
177 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100178 mbedtls_test_driver_aead_hooks.driver_status =
179 mbedtls_psa_aead_set_lengths( operation, ad_length,
180 plaintext_length );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100181 }
182
Paul Elliotta218ceb2021-05-07 15:10:31 +0100183 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100184}
185
Paul Elliotta218ceb2021-05-07 15:10:31 +0100186psa_status_t mbedtls_test_transparent_aead_update_ad(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100187 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100188 const uint8_t *input,
189 size_t input_length )
190{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100191 mbedtls_test_driver_aead_hooks.hits_update_ad++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100192
Paul Elliotta218ceb2021-05-07 15:10:31 +0100193 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100194 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100195 mbedtls_test_driver_aead_hooks.driver_status =
196 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100197 }
198 else
199 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100200 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100201 mbedtls_psa_aead_update_ad( operation, input, input_length );
202 }
203
Paul Elliotta218ceb2021-05-07 15:10:31 +0100204 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100205}
206
Paul Elliotta218ceb2021-05-07 15:10:31 +0100207psa_status_t mbedtls_test_transparent_aead_update(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100208 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100209 const uint8_t *input,
210 size_t input_length,
211 uint8_t *output,
212 size_t output_size,
213 size_t *output_length )
214{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100215 mbedtls_test_driver_aead_hooks.hits_update++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100216
Paul Elliotta218ceb2021-05-07 15:10:31 +0100217 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100218 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100219 mbedtls_test_driver_aead_hooks.driver_status =
220 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100221 }
222 else
223 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100224 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100225 mbedtls_psa_aead_update( operation, input, input_length, output,
226 output_size, output_length );
227 }
228
Paul Elliotta218ceb2021-05-07 15:10:31 +0100229 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100230}
231
Paul Elliotta218ceb2021-05-07 15:10:31 +0100232psa_status_t mbedtls_test_transparent_aead_finish(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100233 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100234 uint8_t *ciphertext,
235 size_t ciphertext_size,
236 size_t *ciphertext_length,
237 uint8_t *tag,
238 size_t tag_size,
239 size_t *tag_length )
240{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100241 mbedtls_test_driver_aead_hooks.hits_finish++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100242
Paul Elliotta218ceb2021-05-07 15:10:31 +0100243 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100244 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100245 mbedtls_test_driver_aead_hooks.driver_status =
246 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100247 }
248 else
249 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100250 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100251 mbedtls_psa_aead_finish( operation, ciphertext, ciphertext_size,
Paul Elliotta218ceb2021-05-07 15:10:31 +0100252 ciphertext_length, tag, tag_size,
253 tag_length );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100254 }
255
Paul Elliotta218ceb2021-05-07 15:10:31 +0100256 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100257}
258
Paul Elliotta218ceb2021-05-07 15:10:31 +0100259psa_status_t mbedtls_test_transparent_aead_verify(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100260 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100261 uint8_t *plaintext,
262 size_t plaintext_size,
263 size_t *plaintext_length,
264 const uint8_t *tag,
265 size_t tag_length )
266{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100267 mbedtls_test_driver_aead_hooks.hits_verify++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100268
Paul Elliotta218ceb2021-05-07 15:10:31 +0100269 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100270 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100271 mbedtls_test_driver_aead_hooks.driver_status =
272 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100273 }
274 else
275 {
Paul Elliott26f4aef2021-07-22 21:47:27 +0100276 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
277 size_t check_tag_length;
278
279 mbedtls_test_driver_aead_hooks.driver_status =
280 mbedtls_psa_aead_finish( operation,
281 plaintext,
282 plaintext_size,
283 plaintext_length,
284 check_tag,
285 tag_length,
286 &check_tag_length );
287
288 if( mbedtls_test_driver_aead_hooks.driver_status == PSA_SUCCESS )
289 {
290 if( tag_length != check_tag_length ||
291 mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
292 != 0 )
293 mbedtls_test_driver_aead_hooks.driver_status =
294 PSA_ERROR_INVALID_SIGNATURE;
295 }
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100296 }
297
Paul Elliotta218ceb2021-05-07 15:10:31 +0100298 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100299}
300
Paul Elliotta218ceb2021-05-07 15:10:31 +0100301psa_status_t mbedtls_test_transparent_aead_abort(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100302 mbedtls_transparent_test_driver_aead_operation_t *operation )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100303{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100304 mbedtls_test_driver_aead_hooks.hits_abort++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100305
Paul Elliotta218ceb2021-05-07 15:10:31 +0100306 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100307 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100308 mbedtls_test_driver_aead_hooks.driver_status =
309 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100310 }
311 else
312 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100313 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100314 mbedtls_psa_aead_abort( operation );
315 }
316
Paul Elliotta218ceb2021-05-07 15:10:31 +0100317 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100318}
319
Ronald Cronde822812021-03-17 16:08:20 +0100320#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */