blob: 6befe7cc0f5ab03a13c32c00666b681466e64e43 [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
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020020#include <test/helpers.h>
Ronald Cronde822812021-03-17 16:08:20 +010021
22#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
23#include "psa_crypto_aead.h"
Paul Elliott0a6a5692021-07-23 15:29:21 +010024#include "psa_crypto_core.h"
Ronald Cronde822812021-03-17 16:08:20 +010025
26#include "test/drivers/aead.h"
27
Ronald Cron7f13fa22021-04-13 12:41:34 +020028mbedtls_test_driver_aead_hooks_t
29 mbedtls_test_driver_aead_hooks = MBEDTLS_TEST_DRIVER_AEAD_INIT;
Ronald Cronbfe551d2021-03-23 09:33:25 +010030
Ronald Cron7f13fa22021-04-13 12:41:34 +020031psa_status_t mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +010032 const psa_key_attributes_t *attributes,
33 const uint8_t *key_buffer, size_t key_buffer_size,
34 psa_algorithm_t alg,
35 const uint8_t *nonce, size_t nonce_length,
36 const uint8_t *additional_data, size_t additional_data_length,
37 const uint8_t *plaintext, size_t plaintext_length,
38 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
39{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +010040 mbedtls_test_driver_aead_hooks.hits_encrypt++;
Ronald Cronbfe551d2021-03-23 09:33:25 +010041
Ronald Cron7f13fa22021-04-13 12:41:34 +020042 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Ronald Cronbfe551d2021-03-23 09:33:25 +010043 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020044 mbedtls_test_driver_aead_hooks.driver_status =
45 mbedtls_test_driver_aead_hooks.forced_status;
Ronald Cronbfe551d2021-03-23 09:33:25 +010046 }
47 else
48 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020049 mbedtls_test_driver_aead_hooks.driver_status =
Ronald Cronbfe551d2021-03-23 09:33:25 +010050 mbedtls_psa_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +010051 attributes, key_buffer, key_buffer_size,
52 alg,
53 nonce, nonce_length,
54 additional_data, additional_data_length,
55 plaintext, plaintext_length,
Ronald Cronbfe551d2021-03-23 09:33:25 +010056 ciphertext, ciphertext_size, ciphertext_length );
57 }
58
Ronald Cron7f13fa22021-04-13 12:41:34 +020059 return( mbedtls_test_driver_aead_hooks.driver_status );
Ronald Cronde822812021-03-17 16:08:20 +010060}
61
Ronald Cron7f13fa22021-04-13 12:41:34 +020062psa_status_t mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +010063 const psa_key_attributes_t *attributes,
64 const uint8_t *key_buffer, size_t key_buffer_size,
65 psa_algorithm_t alg,
66 const uint8_t *nonce, size_t nonce_length,
67 const uint8_t *additional_data, size_t additional_data_length,
68 const uint8_t *ciphertext, size_t ciphertext_length,
69 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
70{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +010071 mbedtls_test_driver_aead_hooks.hits_decrypt++;
Ronald Cronbfe551d2021-03-23 09:33:25 +010072
Ronald Cron7f13fa22021-04-13 12:41:34 +020073 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Ronald Cronbfe551d2021-03-23 09:33:25 +010074 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020075 mbedtls_test_driver_aead_hooks.driver_status =
76 mbedtls_test_driver_aead_hooks.forced_status;
Ronald Cronbfe551d2021-03-23 09:33:25 +010077 }
78 else
79 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020080 mbedtls_test_driver_aead_hooks.driver_status =
Ronald Cronbfe551d2021-03-23 09:33:25 +010081 mbedtls_psa_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +010082 attributes, key_buffer, key_buffer_size,
83 alg,
84 nonce, nonce_length,
85 additional_data, additional_data_length,
86 ciphertext, ciphertext_length,
Ronald Cronbfe551d2021-03-23 09:33:25 +010087 plaintext, plaintext_size, plaintext_length );
88 }
89
Ronald Cron7f13fa22021-04-13 12:41:34 +020090 return( mbedtls_test_driver_aead_hooks.driver_status );
Ronald Cronde822812021-03-17 16:08:20 +010091}
92
Paul Elliotta218ceb2021-05-07 15:10:31 +010093psa_status_t mbedtls_test_transparent_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +010094 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +010095 const psa_key_attributes_t *attributes,
96 const uint8_t *key_buffer, size_t key_buffer_size,
97 psa_algorithm_t alg )
98{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +010099 mbedtls_test_driver_aead_hooks.hits_encrypt_setup++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100100
Paul Elliotta218ceb2021-05-07 15:10:31 +0100101 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100102 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100103 mbedtls_test_driver_aead_hooks.driver_status =
104 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100105 }
106 else
107 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100108 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100109 mbedtls_psa_aead_encrypt_setup( operation, attributes, key_buffer,
110 key_buffer_size, alg );
111 }
112
Paul Elliotta218ceb2021-05-07 15:10:31 +0100113 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100114}
115
Paul Elliotta218ceb2021-05-07 15:10:31 +0100116psa_status_t mbedtls_test_transparent_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100117 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100118 const psa_key_attributes_t *attributes,
119 const uint8_t *key_buffer, size_t key_buffer_size,
120 psa_algorithm_t alg )
121{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100122 mbedtls_test_driver_aead_hooks.hits_decrypt_setup++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100123
Paul Elliotta218ceb2021-05-07 15:10:31 +0100124 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100125 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100126 mbedtls_test_driver_aead_hooks.driver_status =
127 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100128 }
129 else
130 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100131 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100132 mbedtls_psa_aead_decrypt_setup( operation, attributes, key_buffer,
133 key_buffer_size, alg );
134 }
135
Paul Elliotta218ceb2021-05-07 15:10:31 +0100136 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100137}
138
Paul Elliotta218ceb2021-05-07 15:10:31 +0100139psa_status_t mbedtls_test_transparent_aead_set_nonce(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100140 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100141 const uint8_t *nonce,
142 size_t nonce_length )
143{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100144 mbedtls_test_driver_aead_hooks.hits_set_nonce++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100145
Paul Elliotta218ceb2021-05-07 15:10:31 +0100146 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100147 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100148 mbedtls_test_driver_aead_hooks.driver_status =
149 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100150 }
151 else
152 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100153 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100154 mbedtls_psa_aead_set_nonce( operation, nonce, nonce_length );
155 }
156
Paul Elliotta218ceb2021-05-07 15:10:31 +0100157 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100158}
159
Paul Elliotta218ceb2021-05-07 15:10:31 +0100160psa_status_t mbedtls_test_transparent_aead_set_lengths(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100161 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100162 size_t ad_length,
163 size_t plaintext_length )
164{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100165 mbedtls_test_driver_aead_hooks.hits_set_lengths++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100166
Paul Elliotta218ceb2021-05-07 15:10:31 +0100167 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100168 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100169 mbedtls_test_driver_aead_hooks.driver_status =
170 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100171 }
172 else
173 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100174 mbedtls_test_driver_aead_hooks.driver_status =
175 mbedtls_psa_aead_set_lengths( operation, ad_length,
176 plaintext_length );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100177 }
178
Paul Elliotta218ceb2021-05-07 15:10:31 +0100179 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100180}
181
Paul Elliotta218ceb2021-05-07 15:10:31 +0100182psa_status_t mbedtls_test_transparent_aead_update_ad(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100183 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100184 const uint8_t *input,
185 size_t input_length )
186{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100187 mbedtls_test_driver_aead_hooks.hits_update_ad++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100188
Paul Elliotta218ceb2021-05-07 15:10:31 +0100189 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100190 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100191 mbedtls_test_driver_aead_hooks.driver_status =
192 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100193 }
194 else
195 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100196 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100197 mbedtls_psa_aead_update_ad( operation, input, input_length );
198 }
199
Paul Elliotta218ceb2021-05-07 15:10:31 +0100200 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100201}
202
Paul Elliotta218ceb2021-05-07 15:10:31 +0100203psa_status_t mbedtls_test_transparent_aead_update(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100204 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100205 const uint8_t *input,
206 size_t input_length,
207 uint8_t *output,
208 size_t output_size,
209 size_t *output_length )
210{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100211 mbedtls_test_driver_aead_hooks.hits_update++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100212
Paul Elliotta218ceb2021-05-07 15:10:31 +0100213 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100214 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100215 mbedtls_test_driver_aead_hooks.driver_status =
216 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100217 }
218 else
219 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100220 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100221 mbedtls_psa_aead_update( operation, input, input_length, output,
222 output_size, output_length );
223 }
224
Paul Elliotta218ceb2021-05-07 15:10:31 +0100225 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100226}
227
Paul Elliotta218ceb2021-05-07 15:10:31 +0100228psa_status_t mbedtls_test_transparent_aead_finish(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100229 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100230 uint8_t *ciphertext,
231 size_t ciphertext_size,
232 size_t *ciphertext_length,
233 uint8_t *tag,
234 size_t tag_size,
235 size_t *tag_length )
236{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100237 mbedtls_test_driver_aead_hooks.hits_finish++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100238
Paul Elliotta218ceb2021-05-07 15:10:31 +0100239 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100240 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100241 mbedtls_test_driver_aead_hooks.driver_status =
242 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100243 }
244 else
245 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100246 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100247 mbedtls_psa_aead_finish( operation, ciphertext, ciphertext_size,
Paul Elliotta218ceb2021-05-07 15:10:31 +0100248 ciphertext_length, tag, tag_size,
249 tag_length );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100250 }
251
Paul Elliotta218ceb2021-05-07 15:10:31 +0100252 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100253}
254
Paul Elliotta218ceb2021-05-07 15:10:31 +0100255psa_status_t mbedtls_test_transparent_aead_verify(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100256 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100257 uint8_t *plaintext,
258 size_t plaintext_size,
259 size_t *plaintext_length,
260 const uint8_t *tag,
261 size_t tag_length )
262{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100263 mbedtls_test_driver_aead_hooks.hits_verify++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100264
Paul Elliotta218ceb2021-05-07 15:10:31 +0100265 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100266 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100267 mbedtls_test_driver_aead_hooks.driver_status =
268 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100269 }
270 else
271 {
Paul Elliott26f4aef2021-07-22 21:47:27 +0100272 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
273 size_t check_tag_length;
274
275 mbedtls_test_driver_aead_hooks.driver_status =
276 mbedtls_psa_aead_finish( operation,
277 plaintext,
278 plaintext_size,
279 plaintext_length,
280 check_tag,
Paul Elliott0f32b7d2021-09-20 18:46:03 +0100281 sizeof( check_tag ),
Paul Elliott26f4aef2021-07-22 21:47:27 +0100282 &check_tag_length );
283
284 if( mbedtls_test_driver_aead_hooks.driver_status == PSA_SUCCESS )
285 {
286 if( tag_length != check_tag_length ||
287 mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
288 != 0 )
289 mbedtls_test_driver_aead_hooks.driver_status =
290 PSA_ERROR_INVALID_SIGNATURE;
291 }
Paul Elliott0f32b7d2021-09-20 18:46:03 +0100292
293 mbedtls_platform_zeroize( check_tag, sizeof( check_tag ) );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100294 }
295
Paul Elliotta218ceb2021-05-07 15:10:31 +0100296 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100297}
298
Paul Elliotta218ceb2021-05-07 15:10:31 +0100299psa_status_t mbedtls_test_transparent_aead_abort(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100300 mbedtls_transparent_test_driver_aead_operation_t *operation )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100301{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100302 mbedtls_test_driver_aead_hooks.hits_abort++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100303
Paul Elliotta218ceb2021-05-07 15:10:31 +0100304 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100305 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100306 mbedtls_test_driver_aead_hooks.driver_status =
307 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100308 }
309 else
310 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100311 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100312 mbedtls_psa_aead_abort( operation );
313 }
314
Paul Elliotta218ceb2021-05-07 15:10:31 +0100315 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100316}
317
Ronald Cronde822812021-03-17 16:08:20 +0100318#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */