blob: 5928e0e010bd0da4cf418d16436cd33443fbc6a8 [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{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +010043 mbedtls_test_driver_aead_hooks.hits_encrypt++;
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{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +010074 mbedtls_test_driver_aead_hooks.hits_decrypt++;
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 Elliotta218ceb2021-05-07 15:10:31 +010096psa_status_t mbedtls_test_transparent_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +010097 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +010098 const psa_key_attributes_t *attributes,
99 const uint8_t *key_buffer, size_t key_buffer_size,
100 psa_algorithm_t alg )
101{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100102 mbedtls_test_driver_aead_hooks.hits_encrypt_setup++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100103
Paul Elliotta218ceb2021-05-07 15:10:31 +0100104 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100105 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100106 mbedtls_test_driver_aead_hooks.driver_status =
107 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100108 }
109 else
110 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100111 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100112 mbedtls_psa_aead_encrypt_setup( operation, attributes, key_buffer,
113 key_buffer_size, alg );
114 }
115
Paul Elliotta218ceb2021-05-07 15:10:31 +0100116 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100117}
118
Paul Elliotta218ceb2021-05-07 15:10:31 +0100119psa_status_t mbedtls_test_transparent_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100120 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100121 const psa_key_attributes_t *attributes,
122 const uint8_t *key_buffer, size_t key_buffer_size,
123 psa_algorithm_t alg )
124{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100125 mbedtls_test_driver_aead_hooks.hits_decrypt_setup++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100126
Paul Elliotta218ceb2021-05-07 15:10:31 +0100127 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100128 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100129 mbedtls_test_driver_aead_hooks.driver_status =
130 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100131 }
132 else
133 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100134 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100135 mbedtls_psa_aead_decrypt_setup( operation, attributes, key_buffer,
136 key_buffer_size, alg );
137 }
138
Paul Elliotta218ceb2021-05-07 15:10:31 +0100139 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100140}
141
Paul Elliotta218ceb2021-05-07 15:10:31 +0100142psa_status_t mbedtls_test_transparent_aead_set_nonce(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100143 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100144 const uint8_t *nonce,
145 size_t nonce_length )
146{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100147 mbedtls_test_driver_aead_hooks.hits_set_nonce++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100148
Paul Elliotta218ceb2021-05-07 15:10:31 +0100149 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100150 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100151 mbedtls_test_driver_aead_hooks.driver_status =
152 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100153 }
154 else
155 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100156 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100157 mbedtls_psa_aead_set_nonce( operation, nonce, nonce_length );
158 }
159
Paul Elliotta218ceb2021-05-07 15:10:31 +0100160 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100161}
162
Paul Elliotta218ceb2021-05-07 15:10:31 +0100163psa_status_t mbedtls_test_transparent_aead_set_lengths(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100164 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100165 size_t ad_length,
166 size_t plaintext_length )
167{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100168 mbedtls_test_driver_aead_hooks.hits_set_lengths++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100169
Paul Elliotta218ceb2021-05-07 15:10:31 +0100170 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100171 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100172 mbedtls_test_driver_aead_hooks.driver_status =
173 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100174 }
175 else
176 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100177 mbedtls_test_driver_aead_hooks.driver_status =
178 mbedtls_psa_aead_set_lengths( operation, ad_length,
179 plaintext_length );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100180 }
181
Paul Elliotta218ceb2021-05-07 15:10:31 +0100182 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100183}
184
Paul Elliotta218ceb2021-05-07 15:10:31 +0100185psa_status_t mbedtls_test_transparent_aead_update_ad(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100186 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100187 const uint8_t *input,
188 size_t input_length )
189{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100190 mbedtls_test_driver_aead_hooks.hits_update_ad++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100191
Paul Elliotta218ceb2021-05-07 15:10:31 +0100192 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100193 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100194 mbedtls_test_driver_aead_hooks.driver_status =
195 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100196 }
197 else
198 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100199 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100200 mbedtls_psa_aead_update_ad( operation, input, input_length );
201 }
202
Paul Elliotta218ceb2021-05-07 15:10:31 +0100203 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100204}
205
Paul Elliotta218ceb2021-05-07 15:10:31 +0100206psa_status_t mbedtls_test_transparent_aead_update(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100207 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100208 const uint8_t *input,
209 size_t input_length,
210 uint8_t *output,
211 size_t output_size,
212 size_t *output_length )
213{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100214 mbedtls_test_driver_aead_hooks.hits_update++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100215
Paul Elliotta218ceb2021-05-07 15:10:31 +0100216 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100217 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100218 mbedtls_test_driver_aead_hooks.driver_status =
219 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100220 }
221 else
222 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100223 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100224 mbedtls_psa_aead_update( operation, input, input_length, output,
225 output_size, output_length );
226 }
227
Paul Elliotta218ceb2021-05-07 15:10:31 +0100228 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100229}
230
Paul Elliotta218ceb2021-05-07 15:10:31 +0100231psa_status_t mbedtls_test_transparent_aead_finish(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100232 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100233 uint8_t *ciphertext,
234 size_t ciphertext_size,
235 size_t *ciphertext_length,
236 uint8_t *tag,
237 size_t tag_size,
238 size_t *tag_length )
239{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100240 mbedtls_test_driver_aead_hooks.hits_finish++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100241
Paul Elliotta218ceb2021-05-07 15:10:31 +0100242 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100243 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100244 mbedtls_test_driver_aead_hooks.driver_status =
245 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100246 }
247 else
248 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100249 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100250 mbedtls_psa_aead_finish( operation, ciphertext, ciphertext_size,
Paul Elliotta218ceb2021-05-07 15:10:31 +0100251 ciphertext_length, tag, tag_size,
252 tag_length );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100253 }
254
Paul Elliotta218ceb2021-05-07 15:10:31 +0100255 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100256}
257
Paul Elliotta218ceb2021-05-07 15:10:31 +0100258psa_status_t mbedtls_test_transparent_aead_verify(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100259 mbedtls_transparent_test_driver_aead_operation_t *operation,
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100260 uint8_t *plaintext,
261 size_t plaintext_size,
262 size_t *plaintext_length,
263 const uint8_t *tag,
264 size_t tag_length )
265{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100266 mbedtls_test_driver_aead_hooks.hits_verify++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100267
Paul Elliotta218ceb2021-05-07 15:10:31 +0100268 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100269 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100270 mbedtls_test_driver_aead_hooks.driver_status =
271 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100272 }
273 else
274 {
Paul Elliott26f4aef2021-07-22 21:47:27 +0100275 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
276 size_t check_tag_length;
277
278 mbedtls_test_driver_aead_hooks.driver_status =
279 mbedtls_psa_aead_finish( operation,
280 plaintext,
281 plaintext_size,
282 plaintext_length,
283 check_tag,
284 tag_length,
285 &check_tag_length );
286
287 if( mbedtls_test_driver_aead_hooks.driver_status == PSA_SUCCESS )
288 {
289 if( tag_length != check_tag_length ||
290 mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
291 != 0 )
292 mbedtls_test_driver_aead_hooks.driver_status =
293 PSA_ERROR_INVALID_SIGNATURE;
294 }
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100295 }
296
Paul Elliotta218ceb2021-05-07 15:10:31 +0100297 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100298}
299
Paul Elliotta218ceb2021-05-07 15:10:31 +0100300psa_status_t mbedtls_test_transparent_aead_abort(
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100301 mbedtls_transparent_test_driver_aead_operation_t *operation )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100302{
Paul Elliottfcb5cdc2021-06-23 09:40:12 +0100303 mbedtls_test_driver_aead_hooks.hits_abort++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100304
Paul Elliotta218ceb2021-05-07 15:10:31 +0100305 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100306 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100307 mbedtls_test_driver_aead_hooks.driver_status =
308 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100309 }
310 else
311 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100312 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100313 mbedtls_psa_aead_abort( operation );
314 }
315
Paul Elliotta218ceb2021-05-07 15:10:31 +0100316 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100317}
318
Ronald Cronde822812021-03-17 16:08:20 +0100319#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */