blob: 34bbc51ab29c3b0207609190cbfc619a55152304 [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 Elliotta218ceb2021-05-07 15:10:31 +010096psa_status_t mbedtls_test_transparent_aead_encrypt_setup(
Paul Elliott4bbe82b2021-04-27 12:11:56 +010097 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{
Paul Elliotta218ceb2021-05-07 15:10:31 +0100102 mbedtls_test_driver_aead_hooks.hits++;
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 Elliott4bbe82b2021-04-27 12:11:56 +0100120 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{
Paul Elliotta218ceb2021-05-07 15:10:31 +0100125 mbedtls_test_driver_aead_hooks.hits++;
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 Elliott4bbe82b2021-04-27 12:11:56 +0100143 psa_aead_operation_t *operation,
144 const uint8_t *nonce,
145 size_t nonce_length )
146{
Paul Elliotta218ceb2021-05-07 15:10:31 +0100147 mbedtls_test_driver_aead_hooks.hits++;
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 Elliott4bbe82b2021-04-27 12:11:56 +0100164 psa_aead_operation_t *operation,
165 size_t ad_length,
166 size_t plaintext_length )
167{
Paul Elliotta218ceb2021-05-07 15:10:31 +0100168 mbedtls_test_driver_aead_hooks.hits++;
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 Elliott4bbe82b2021-04-27 12:11:56 +0100186 psa_aead_operation_t *operation,
187 const uint8_t *input,
188 size_t input_length )
189{
Paul Elliotta218ceb2021-05-07 15:10:31 +0100190 mbedtls_test_driver_aead_hooks.hits++;
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 Elliott4bbe82b2021-04-27 12:11:56 +0100207 psa_aead_operation_t *operation,
208 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 Elliotta218ceb2021-05-07 15:10:31 +0100214 mbedtls_test_driver_aead_hooks.hits++;
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 Elliott4bbe82b2021-04-27 12:11:56 +0100232 psa_aead_operation_t *operation,
233 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 Elliotta218ceb2021-05-07 15:10:31 +0100240 mbedtls_test_driver_aead_hooks.hits++;
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 Elliott4bbe82b2021-04-27 12:11:56 +0100259 psa_aead_operation_t *operation,
260 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 Elliotta218ceb2021-05-07 15:10:31 +0100266 mbedtls_test_driver_aead_hooks.hits++;
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 Elliotta218ceb2021-05-07 15:10:31 +0100275 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100276 mbedtls_psa_aead_verify( operation, plaintext, plaintext_size,
277 plaintext_length, tag, tag_length );
278 }
279
Paul Elliotta218ceb2021-05-07 15:10:31 +0100280 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100281}
282
Paul Elliotta218ceb2021-05-07 15:10:31 +0100283psa_status_t mbedtls_test_transparent_aead_abort(
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100284 psa_aead_operation_t *operation )
285{
Paul Elliotta218ceb2021-05-07 15:10:31 +0100286 mbedtls_test_driver_aead_hooks.hits++;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100287
Paul Elliotta218ceb2021-05-07 15:10:31 +0100288 if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100289 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100290 mbedtls_test_driver_aead_hooks.driver_status =
291 mbedtls_test_driver_aead_hooks.forced_status;
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100292 }
293 else
294 {
Paul Elliotta218ceb2021-05-07 15:10:31 +0100295 mbedtls_test_driver_aead_hooks.driver_status =
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100296 mbedtls_psa_aead_abort( operation );
297 }
298
Paul Elliotta218ceb2021-05-07 15:10:31 +0100299 return( mbedtls_test_driver_aead_hooks.driver_status );
Paul Elliott4bbe82b2021-04-27 12:11:56 +0100300}
301
Ronald Cronde822812021-03-17 16:08:20 +0100302#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */