blob: d27ada294d48a2a4ce6b8f5aeab86370deb61f1d [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 Elliott325d3742021-09-27 17:56:28 +0100174 /* No mbedtls_psa_aead_set_lengths, everything is done in PSA Core. */
175 mbedtls_test_driver_aead_hooks.driver_status = PSA_SUCCESS;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100176 }
177
Paul Elliotta218ceb2021-05-07 15:10:31 +0100178 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100179}
180
Paul Elliotta218ceb2021-05-07 15:10:31 +0100181psa_status_t mbedtls_test_transparent_aead_update_ad(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100182 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100183 const uint8_t *input,
184 size_t input_length )
185{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100186 mbedtls_test_driver_aead_hooks.hits_update_ad++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100187
Paul Elliotta218ceb2021-05-07 15:10:31 +0100188 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100189 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100190 mbedtls_test_driver_aead_hooks.driver_status =
191 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100192 }
193 else
194 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100195 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100196 mbedtls_psa_aead_update_ad( operation, input, input_length );
197 }
198
Paul Elliotta218ceb2021-05-07 15:10:31 +0100199 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100200}
201
Paul Elliotta218ceb2021-05-07 15:10:31 +0100202psa_status_t mbedtls_test_transparent_aead_update(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100203 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100204 const uint8_t *input,
205 size_t input_length,
206 uint8_t *output,
207 size_t output_size,
208 size_t *output_length )
209{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100210 mbedtls_test_driver_aead_hooks.hits_update++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100211
Paul Elliotta218ceb2021-05-07 15:10:31 +0100212 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100213 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100214 mbedtls_test_driver_aead_hooks.driver_status =
215 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100216 }
217 else
218 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100219 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100220 mbedtls_psa_aead_update( operation, input, input_length, output,
221 output_size, output_length );
222 }
223
Paul Elliotta218ceb2021-05-07 15:10:31 +0100224 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100225}
226
Paul Elliotta218ceb2021-05-07 15:10:31 +0100227psa_status_t mbedtls_test_transparent_aead_finish(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100228 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100229 uint8_t *ciphertext,
230 size_t ciphertext_size,
231 size_t *ciphertext_length,
232 uint8_t *tag,
233 size_t tag_size,
234 size_t *tag_length )
235{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100236 mbedtls_test_driver_aead_hooks.hits_finish++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100237
Paul Elliotta218ceb2021-05-07 15:10:31 +0100238 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100239 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100240 mbedtls_test_driver_aead_hooks.driver_status =
241 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100242 }
243 else
244 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100245 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100246 mbedtls_psa_aead_finish( operation, ciphertext, ciphertext_size,
Paul Elliotta218ceb2021-05-07 15:10:31 +0100247 ciphertext_length, tag, tag_size,
248 tag_length );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100249 }
250
Paul Elliotta218ceb2021-05-07 15:10:31 +0100251 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100252}
253
Paul Elliotta218ceb2021-05-07 15:10:31 +0100254psa_status_t mbedtls_test_transparent_aead_verify(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100255 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100256 uint8_t *plaintext,
257 size_t plaintext_size,
258 size_t *plaintext_length,
259 const uint8_t *tag,
260 size_t tag_length )
261{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100262 mbedtls_test_driver_aead_hooks.hits_verify++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100263
Paul Elliotta218ceb2021-05-07 15:10:31 +0100264 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100265 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100266 mbedtls_test_driver_aead_hooks.driver_status =
267 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100268 }
269 else
270 {
Paul Elliott26f4aef2021-07-22 21:47:27 +0100271 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
272 size_t check_tag_length;
273
274 mbedtls_test_driver_aead_hooks.driver_status =
275 mbedtls_psa_aead_finish( operation,
276 plaintext,
277 plaintext_size,
278 plaintext_length,
279 check_tag,
Paul Elliott0f32b7d2021-09-20 18:46:03 +0100280 sizeof( check_tag ),
Paul Elliott26f4aef2021-07-22 21:47:27 +0100281 &check_tag_length );
282
283 if( mbedtls_test_driver_aead_hooks.driver_status == PSA_SUCCESS )
284 {
285 if( tag_length != check_tag_length ||
286 mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
287 != 0 )
288 mbedtls_test_driver_aead_hooks.driver_status =
289 PSA_ERROR_INVALID_SIGNATURE;
290 }
Paul Elliott0f32b7d2021-09-20 18:46:03 +0100291
292 mbedtls_platform_zeroize( check_tag, sizeof( check_tag ) );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100293 }
294
Paul Elliotta218ceb2021-05-07 15:10:31 +0100295 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100296}
297
Paul Elliotta218ceb2021-05-07 15:10:31 +0100298psa_status_t mbedtls_test_transparent_aead_abort(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100299 mbedtls_transparent_test_driver_aead_operation_t *operation )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100300{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100301 mbedtls_test_driver_aead_hooks.hits_abort++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100302
Paul Elliotta218ceb2021-05-07 15:10:31 +0100303 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100304 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100305 mbedtls_test_driver_aead_hooks.driver_status =
306 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100307 }
308 else
309 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100310 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100311 mbedtls_psa_aead_abort( operation );
312 }
313
Paul Elliotta218ceb2021-05-07 15:10:31 +0100314 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100315}
316
Ronald Cronde822812021-03-17 16:08:20 +0100317#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */