• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include <test/helpers.h>
21 
22 #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
23 #include "psa_crypto_aead.h"
24 #include "psa_crypto_core.h"
25 
26 #include "test/drivers/aead.h"
27 
28 mbedtls_test_driver_aead_hooks_t
29     mbedtls_test_driver_aead_hooks = MBEDTLS_TEST_DRIVER_AEAD_INIT;
30 
mbedtls_test_transparent_aead_encrypt(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * nonce,size_t nonce_length,const uint8_t * additional_data,size_t additional_data_length,const uint8_t * plaintext,size_t plaintext_length,uint8_t * ciphertext,size_t ciphertext_size,size_t * ciphertext_length)31 psa_status_t mbedtls_test_transparent_aead_encrypt(
32     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 {
40     mbedtls_test_driver_aead_hooks.hits_encrypt++;
41 
42     if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
43     {
44          mbedtls_test_driver_aead_hooks.driver_status =
45              mbedtls_test_driver_aead_hooks.forced_status;
46     }
47     else
48     {
49 #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
50         mbedtls_test_driver_aead_hooks.driver_status =
51             mbedtls_psa_aead_encrypt(
52                 attributes, key_buffer, key_buffer_size,
53                 alg,
54                 nonce, nonce_length,
55                 additional_data, additional_data_length,
56                 plaintext, plaintext_length,
57                 ciphertext, ciphertext_size, ciphertext_length );
58 #else
59         (void) attributes;
60         (void) key_buffer;
61         (void) key_buffer_size;
62         (void) alg;
63         (void) nonce;
64         (void) nonce_length;
65         (void) additional_data;
66         (void) additional_data_length;
67         (void) plaintext;
68         (void) plaintext_length;
69         (void) ciphertext;
70         (void) ciphertext_size;
71         (void) ciphertext_length;
72         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
73 #endif
74     }
75 
76     return( mbedtls_test_driver_aead_hooks.driver_status );
77 }
78 
mbedtls_test_transparent_aead_decrypt(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * nonce,size_t nonce_length,const uint8_t * additional_data,size_t additional_data_length,const uint8_t * ciphertext,size_t ciphertext_length,uint8_t * plaintext,size_t plaintext_size,size_t * plaintext_length)79 psa_status_t mbedtls_test_transparent_aead_decrypt(
80     const psa_key_attributes_t *attributes,
81     const uint8_t *key_buffer, size_t key_buffer_size,
82     psa_algorithm_t alg,
83     const uint8_t *nonce, size_t nonce_length,
84     const uint8_t *additional_data, size_t additional_data_length,
85     const uint8_t *ciphertext, size_t ciphertext_length,
86     uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
87 {
88     mbedtls_test_driver_aead_hooks.hits_decrypt++;
89 
90     if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
91     {
92          mbedtls_test_driver_aead_hooks.driver_status =
93              mbedtls_test_driver_aead_hooks.forced_status;
94     }
95     else
96     {
97 #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
98         mbedtls_test_driver_aead_hooks.driver_status =
99             mbedtls_psa_aead_decrypt(
100                 attributes, key_buffer, key_buffer_size,
101                 alg,
102                 nonce, nonce_length,
103                 additional_data, additional_data_length,
104                 ciphertext, ciphertext_length,
105                 plaintext, plaintext_size, plaintext_length );
106 #else
107         (void) attributes;
108         (void) key_buffer;
109         (void) key_buffer_size;
110         (void) alg;
111         (void) nonce;
112         (void) nonce_length;
113         (void) additional_data;
114         (void) additional_data_length;
115         (void) ciphertext;
116         (void) ciphertext_length;
117         (void) plaintext;
118         (void) plaintext_size;
119         (void) plaintext_length;
120         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
121 #endif
122     }
123 
124     return( mbedtls_test_driver_aead_hooks.driver_status );
125 }
126 
mbedtls_test_transparent_aead_encrypt_setup(mbedtls_transparent_test_driver_aead_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg)127 psa_status_t mbedtls_test_transparent_aead_encrypt_setup(
128     mbedtls_transparent_test_driver_aead_operation_t *operation,
129     const psa_key_attributes_t *attributes,
130     const uint8_t *key_buffer, size_t key_buffer_size,
131     psa_algorithm_t alg )
132 {
133     mbedtls_test_driver_aead_hooks.hits_encrypt_setup++;
134 
135     if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
136     {
137          mbedtls_test_driver_aead_hooks.driver_status =
138              mbedtls_test_driver_aead_hooks.forced_status;
139     }
140     else
141     {
142 #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
143         mbedtls_test_driver_aead_hooks.driver_status =
144             mbedtls_psa_aead_encrypt_setup( operation, attributes, key_buffer,
145                                             key_buffer_size, alg );
146 #else
147         (void) operation;
148         (void) attributes;
149         (void) key_buffer;
150         (void) key_buffer_size;
151         (void) alg;
152         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
153 #endif
154     }
155 
156     return( mbedtls_test_driver_aead_hooks.driver_status );
157 }
158 
mbedtls_test_transparent_aead_decrypt_setup(mbedtls_transparent_test_driver_aead_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg)159 psa_status_t mbedtls_test_transparent_aead_decrypt_setup(
160     mbedtls_transparent_test_driver_aead_operation_t *operation,
161     const psa_key_attributes_t *attributes,
162     const uint8_t *key_buffer, size_t key_buffer_size,
163     psa_algorithm_t alg )
164 {
165     mbedtls_test_driver_aead_hooks.hits_decrypt_setup++;
166 
167     if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
168     {
169          mbedtls_test_driver_aead_hooks.driver_status =
170              mbedtls_test_driver_aead_hooks.forced_status;
171     }
172     else
173     {
174 #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
175         mbedtls_test_driver_aead_hooks.driver_status =
176             mbedtls_psa_aead_decrypt_setup( operation, attributes, key_buffer,
177                                             key_buffer_size, alg );
178 #else
179         (void) operation;
180         (void) attributes;
181         (void) key_buffer;
182         (void) key_buffer_size;
183         (void) alg;
184         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
185 #endif
186     }
187 
188     return( mbedtls_test_driver_aead_hooks.driver_status );
189 }
190 
mbedtls_test_transparent_aead_set_nonce(mbedtls_transparent_test_driver_aead_operation_t * operation,const uint8_t * nonce,size_t nonce_length)191 psa_status_t mbedtls_test_transparent_aead_set_nonce(
192     mbedtls_transparent_test_driver_aead_operation_t *operation,
193     const uint8_t *nonce,
194     size_t nonce_length )
195 {
196     mbedtls_test_driver_aead_hooks.hits_set_nonce++;
197 
198     if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
199     {
200          mbedtls_test_driver_aead_hooks.driver_status =
201              mbedtls_test_driver_aead_hooks.forced_status;
202     }
203     else
204     {
205 #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
206         mbedtls_test_driver_aead_hooks.driver_status =
207             mbedtls_psa_aead_set_nonce( operation, nonce, nonce_length );
208 #else
209         (void) operation;
210         (void) nonce;
211         (void) nonce_length;
212         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
213 #endif
214     }
215 
216     return( mbedtls_test_driver_aead_hooks.driver_status );
217 }
218 
mbedtls_test_transparent_aead_set_lengths(mbedtls_transparent_test_driver_aead_operation_t * operation,size_t ad_length,size_t plaintext_length)219 psa_status_t mbedtls_test_transparent_aead_set_lengths(
220     mbedtls_transparent_test_driver_aead_operation_t *operation,
221     size_t ad_length,
222     size_t plaintext_length )
223 {
224     mbedtls_test_driver_aead_hooks.hits_set_lengths++;
225 
226     if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
227     {
228          mbedtls_test_driver_aead_hooks.driver_status =
229              mbedtls_test_driver_aead_hooks.forced_status;
230     }
231     else
232     {
233 #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
234         mbedtls_test_driver_aead_hooks.driver_status =
235             mbedtls_psa_aead_set_lengths( operation, ad_length,
236                                           plaintext_length );
237 #else
238         (void) operation;
239         (void) ad_length;
240         (void) plaintext_length;
241         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
242 #endif
243     }
244 
245     return( mbedtls_test_driver_aead_hooks.driver_status );
246 }
247 
mbedtls_test_transparent_aead_update_ad(mbedtls_transparent_test_driver_aead_operation_t * operation,const uint8_t * input,size_t input_length)248 psa_status_t mbedtls_test_transparent_aead_update_ad(
249     mbedtls_transparent_test_driver_aead_operation_t *operation,
250     const uint8_t *input,
251     size_t input_length )
252 {
253     mbedtls_test_driver_aead_hooks.hits_update_ad++;
254 
255     if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
256     {
257          mbedtls_test_driver_aead_hooks.driver_status =
258              mbedtls_test_driver_aead_hooks.forced_status;
259     }
260     else
261     {
262 #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
263         mbedtls_test_driver_aead_hooks.driver_status =
264             mbedtls_psa_aead_update_ad( operation, input, input_length );
265 #else
266         (void) operation;
267         (void) input;
268         (void) input_length;
269         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
270 #endif
271     }
272 
273     return( mbedtls_test_driver_aead_hooks.driver_status );
274 }
275 
mbedtls_test_transparent_aead_update(mbedtls_transparent_test_driver_aead_operation_t * operation,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)276 psa_status_t mbedtls_test_transparent_aead_update(
277    mbedtls_transparent_test_driver_aead_operation_t *operation,
278    const uint8_t *input,
279    size_t input_length,
280    uint8_t *output,
281    size_t output_size,
282    size_t *output_length )
283 {
284     mbedtls_test_driver_aead_hooks.hits_update++;
285 
286     if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
287     {
288          mbedtls_test_driver_aead_hooks.driver_status =
289              mbedtls_test_driver_aead_hooks.forced_status;
290     }
291     else
292     {
293 #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
294         mbedtls_test_driver_aead_hooks.driver_status =
295             mbedtls_psa_aead_update( operation, input, input_length, output,
296                                     output_size, output_length );
297 #else
298         (void) operation;
299         (void) input;
300         (void) input_length;
301         (void) output;
302         (void) output_size;
303         (void) output_length;
304         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
305 #endif
306     }
307 
308     return( mbedtls_test_driver_aead_hooks.driver_status );
309 }
310 
mbedtls_test_transparent_aead_finish(mbedtls_transparent_test_driver_aead_operation_t * operation,uint8_t * ciphertext,size_t ciphertext_size,size_t * ciphertext_length,uint8_t * tag,size_t tag_size,size_t * tag_length)311 psa_status_t mbedtls_test_transparent_aead_finish(
312    mbedtls_transparent_test_driver_aead_operation_t *operation,
313    uint8_t *ciphertext,
314    size_t ciphertext_size,
315    size_t *ciphertext_length,
316    uint8_t *tag,
317    size_t tag_size,
318    size_t *tag_length )
319 {
320    mbedtls_test_driver_aead_hooks.hits_finish++;
321 
322     if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
323     {
324          mbedtls_test_driver_aead_hooks.driver_status =
325              mbedtls_test_driver_aead_hooks.forced_status;
326     }
327     else
328     {
329 #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
330         mbedtls_test_driver_aead_hooks.driver_status =
331             mbedtls_psa_aead_finish( operation, ciphertext, ciphertext_size,
332                                      ciphertext_length, tag, tag_size,
333                                      tag_length );
334 #else
335         (void) operation;
336         (void) ciphertext;
337         (void) ciphertext_size;
338         (void) ciphertext_length;
339         (void) tag;
340         (void) tag_size;
341         (void) tag_length;
342         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
343 #endif
344     }
345 
346     return( mbedtls_test_driver_aead_hooks.driver_status );
347 }
348 
mbedtls_test_transparent_aead_verify(mbedtls_transparent_test_driver_aead_operation_t * operation,uint8_t * plaintext,size_t plaintext_size,size_t * plaintext_length,const uint8_t * tag,size_t tag_length)349 psa_status_t mbedtls_test_transparent_aead_verify(
350    mbedtls_transparent_test_driver_aead_operation_t *operation,
351    uint8_t *plaintext,
352    size_t plaintext_size,
353    size_t *plaintext_length,
354    const uint8_t *tag,
355    size_t tag_length )
356 {
357    mbedtls_test_driver_aead_hooks.hits_verify++;
358 
359     if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
360     {
361          mbedtls_test_driver_aead_hooks.driver_status =
362              mbedtls_test_driver_aead_hooks.forced_status;
363     }
364     else
365     {
366        uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
367        size_t check_tag_length;
368 
369 #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
370        mbedtls_test_driver_aead_hooks.driver_status =
371           mbedtls_psa_aead_finish( operation,
372                                    plaintext,
373                                    plaintext_size,
374                                    plaintext_length,
375                                    check_tag,
376                                    sizeof( check_tag ),
377                                    &check_tag_length );
378 #else
379         (void) operation;
380         (void) plaintext;
381         (void) plaintext_size;
382         (void) plaintext_length;
383         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
384 #endif
385 
386        if( mbedtls_test_driver_aead_hooks.driver_status == PSA_SUCCESS )
387        {
388           if( tag_length != check_tag_length ||
389               mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
390               != 0 )
391              mbedtls_test_driver_aead_hooks.driver_status =
392                                                     PSA_ERROR_INVALID_SIGNATURE;
393        }
394 
395        mbedtls_platform_zeroize( check_tag, sizeof( check_tag ) );
396     }
397 
398     return( mbedtls_test_driver_aead_hooks.driver_status );
399 }
400 
mbedtls_test_transparent_aead_abort(mbedtls_transparent_test_driver_aead_operation_t * operation)401 psa_status_t mbedtls_test_transparent_aead_abort(
402    mbedtls_transparent_test_driver_aead_operation_t *operation )
403 {
404    mbedtls_test_driver_aead_hooks.hits_abort++;
405 
406     if( mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS )
407     {
408          mbedtls_test_driver_aead_hooks.driver_status =
409              mbedtls_test_driver_aead_hooks.forced_status;
410     }
411     else
412     {
413 #if defined(MBEDTLS_PSA_BUILTIN_AEAD)
414         mbedtls_test_driver_aead_hooks.driver_status =
415             mbedtls_psa_aead_abort( operation );
416 #else
417         (void) operation;
418         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
419 #endif
420     }
421 
422     return( mbedtls_test_driver_aead_hooks.driver_status );
423 }
424 
425 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
426