Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 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 | #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 | |
Przemek Stekiel | f886241 | 2022-10-02 21:01:23 +0200 | [diff] [blame] | 31 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) |
| 32 | #include "libtestdriver1/library/psa_crypto_aead.h" |
| 33 | #endif |
| 34 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 35 | mbedtls_test_driver_aead_hooks_t |
| 36 | mbedtls_test_driver_aead_hooks = MBEDTLS_TEST_DRIVER_AEAD_INIT; |
Ronald Cron | bfe551d | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 37 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 38 | psa_status_t mbedtls_test_transparent_aead_encrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 39 | const psa_key_attributes_t *attributes, |
| 40 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 41 | psa_algorithm_t alg, |
| 42 | const uint8_t *nonce, size_t nonce_length, |
| 43 | const uint8_t *additional_data, size_t additional_data_length, |
| 44 | const uint8_t *plaintext, size_t plaintext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 45 | uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length) |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 46 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 47 | mbedtls_test_driver_aead_hooks.hits++; |
Ronald Cron | bfe551d | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 48 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 49 | if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) { |
| 50 | mbedtls_test_driver_aead_hooks.driver_status = |
| 51 | mbedtls_test_driver_aead_hooks.forced_status; |
| 52 | } else { |
Przemek Stekiel | f886241 | 2022-10-02 21:01:23 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 54 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD) |
Przemek Stekiel | f886241 | 2022-10-02 21:01:23 +0200 | [diff] [blame] | 55 | mbedtls_test_driver_aead_hooks.driver_status = |
| 56 | libtestdriver1_mbedtls_psa_aead_encrypt( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 57 | (const libtestdriver1_psa_key_attributes_t *) attributes, |
Przemek Stekiel | f886241 | 2022-10-02 21:01:23 +0200 | [diff] [blame] | 58 | key_buffer, key_buffer_size, |
| 59 | alg, |
| 60 | nonce, nonce_length, |
| 61 | additional_data, additional_data_length, |
| 62 | plaintext, plaintext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 63 | ciphertext, ciphertext_size, ciphertext_length); |
Przemek Stekiel | f886241 | 2022-10-02 21:01:23 +0200 | [diff] [blame] | 64 | #elif defined(MBEDTLS_PSA_BUILTIN_AEAD) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 65 | mbedtls_test_driver_aead_hooks.driver_status = |
Ronald Cron | bfe551d | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 66 | mbedtls_psa_aead_encrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 67 | attributes, key_buffer, key_buffer_size, |
| 68 | alg, |
| 69 | nonce, nonce_length, |
| 70 | additional_data, additional_data_length, |
| 71 | plaintext, plaintext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 72 | ciphertext, ciphertext_size, ciphertext_length); |
Przemek Stekiel | f886241 | 2022-10-02 21:01:23 +0200 | [diff] [blame] | 73 | #else |
| 74 | (void) attributes; |
| 75 | (void) key_buffer; |
| 76 | (void) key_buffer_size; |
| 77 | (void) alg; |
| 78 | (void) nonce; |
| 79 | (void) nonce_length; |
| 80 | (void) additional_data; |
| 81 | (void) additional_data_length; |
| 82 | (void) plaintext; |
| 83 | (void) plaintext_length; |
| 84 | (void) ciphertext; |
| 85 | (void) ciphertext_size; |
| 86 | (void) ciphertext_length; |
| 87 | mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 88 | #endif |
| 89 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 90 | return mbedtls_test_driver_aead_hooks.driver_status; |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 93 | psa_status_t mbedtls_test_transparent_aead_decrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 94 | const psa_key_attributes_t *attributes, |
| 95 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 96 | psa_algorithm_t alg, |
| 97 | const uint8_t *nonce, size_t nonce_length, |
| 98 | const uint8_t *additional_data, size_t additional_data_length, |
| 99 | const uint8_t *ciphertext, size_t ciphertext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 100 | uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length) |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 101 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 102 | mbedtls_test_driver_aead_hooks.hits++; |
Ronald Cron | bfe551d | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 103 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 104 | if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) { |
| 105 | mbedtls_test_driver_aead_hooks.driver_status = |
| 106 | mbedtls_test_driver_aead_hooks.forced_status; |
| 107 | } else { |
Przemek Stekiel | f886241 | 2022-10-02 21:01:23 +0200 | [diff] [blame] | 108 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 109 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD) |
Przemek Stekiel | f886241 | 2022-10-02 21:01:23 +0200 | [diff] [blame] | 110 | mbedtls_test_driver_aead_hooks.driver_status = |
| 111 | libtestdriver1_mbedtls_psa_aead_decrypt( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 112 | (const libtestdriver1_psa_key_attributes_t *) attributes, |
Przemek Stekiel | f886241 | 2022-10-02 21:01:23 +0200 | [diff] [blame] | 113 | key_buffer, key_buffer_size, |
| 114 | alg, |
| 115 | nonce, nonce_length, |
| 116 | additional_data, additional_data_length, |
| 117 | ciphertext, ciphertext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 118 | plaintext, plaintext_size, plaintext_length); |
Przemek Stekiel | f886241 | 2022-10-02 21:01:23 +0200 | [diff] [blame] | 119 | #elif defined(MBEDTLS_PSA_BUILTIN_AEAD) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 120 | mbedtls_test_driver_aead_hooks.driver_status = |
Ronald Cron | bfe551d | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 121 | mbedtls_psa_aead_decrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 122 | attributes, key_buffer, key_buffer_size, |
| 123 | alg, |
| 124 | nonce, nonce_length, |
| 125 | additional_data, additional_data_length, |
| 126 | ciphertext, ciphertext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 127 | plaintext, plaintext_size, plaintext_length); |
Przemek Stekiel | f886241 | 2022-10-02 21:01:23 +0200 | [diff] [blame] | 128 | #else |
| 129 | (void) attributes; |
| 130 | (void) key_buffer; |
| 131 | (void) key_buffer_size; |
| 132 | (void) alg; |
| 133 | (void) nonce; |
| 134 | (void) nonce_length; |
| 135 | (void) additional_data; |
| 136 | (void) additional_data_length; |
| 137 | (void) ciphertext; |
| 138 | (void) ciphertext_length; |
| 139 | (void) plaintext; |
| 140 | (void) plaintext_size; |
| 141 | (void) plaintext_length; |
| 142 | mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 143 | #endif |
| 144 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 145 | return mbedtls_test_driver_aead_hooks.driver_status; |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |