Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Test driver for cipher functions |
| 3 | */ |
Steven Cooreman | 3ec4018 | 2020-09-02 16:27:46 +0200 | [diff] [blame] | 4 | /* Copyright The Mbed TLS Contributors |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 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. |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #ifndef PSA_CRYPTO_TEST_DRIVERS_CIPHER_H |
| 21 | #define PSA_CRYPTO_TEST_DRIVERS_CIPHER_H |
| 22 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 23 | #include "mbedtls/build_info.h" |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 24 | |
| 25 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 26 | # include <psa/crypto_driver_common.h> |
| 27 | # include <psa/crypto.h> |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 28 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 29 | # include "mbedtls/cipher.h" |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 30 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 31 | typedef struct { |
| 32 | /* If non-null, on success, copy this to the output. */ |
| 33 | void *forced_output; |
| 34 | size_t forced_output_length; |
| 35 | /* If not PSA_SUCCESS, return this error code instead of processing the |
| 36 | * function call. */ |
| 37 | psa_status_t forced_status; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 38 | /* Count the amount of times one of the cipher driver functions is called. |
| 39 | */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 40 | unsigned long hits; |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 41 | } mbedtls_test_driver_cipher_hooks_t; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 42 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 43 | # define MBEDTLS_TEST_DRIVER_CIPHER_INIT \ |
| 44 | { \ |
| 45 | NULL, 0, PSA_SUCCESS, 0 \ |
| 46 | } |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 47 | static inline mbedtls_test_driver_cipher_hooks_t |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 48 | mbedtls_test_driver_cipher_hooks_init(void) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 49 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 50 | const mbedtls_test_driver_cipher_hooks_t v = |
| 51 | MBEDTLS_TEST_DRIVER_CIPHER_INIT; |
| 52 | return v; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 55 | extern mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 56 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 57 | psa_status_t |
| 58 | mbedtls_test_transparent_cipher_encrypt(const psa_key_attributes_t *attributes, |
| 59 | const uint8_t *key, |
| 60 | size_t key_length, |
| 61 | psa_algorithm_t alg, |
| 62 | const uint8_t *input, |
| 63 | size_t input_length, |
| 64 | uint8_t *output, |
| 65 | size_t output_size, |
| 66 | size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 67 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 68 | psa_status_t |
| 69 | mbedtls_test_transparent_cipher_decrypt(const psa_key_attributes_t *attributes, |
| 70 | const uint8_t *key, |
| 71 | size_t key_length, |
| 72 | psa_algorithm_t alg, |
| 73 | const uint8_t *input, |
| 74 | size_t input_length, |
| 75 | uint8_t *output, |
| 76 | size_t output_size, |
| 77 | size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 78 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 79 | psa_status_t mbedtls_test_transparent_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 80 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 81 | const psa_key_attributes_t *attributes, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 82 | const uint8_t *key, |
| 83 | size_t key_length, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 84 | psa_algorithm_t alg); |
| 85 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 86 | psa_status_t mbedtls_test_transparent_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 87 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 88 | const psa_key_attributes_t *attributes, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 89 | const uint8_t *key, |
| 90 | size_t key_length, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 91 | psa_algorithm_t alg); |
| 92 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 93 | psa_status_t mbedtls_test_transparent_cipher_abort( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 94 | mbedtls_transparent_test_driver_cipher_operation_t *operation); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 95 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 96 | psa_status_t mbedtls_test_transparent_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 97 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 98 | const uint8_t *iv, |
| 99 | size_t iv_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 100 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 101 | psa_status_t mbedtls_test_transparent_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 102 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 103 | const uint8_t *input, |
| 104 | size_t input_length, |
| 105 | uint8_t *output, |
| 106 | size_t output_size, |
| 107 | size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 108 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 109 | psa_status_t mbedtls_test_transparent_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 110 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 111 | uint8_t *output, |
| 112 | size_t output_size, |
| 113 | size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * opaque versions |
| 117 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 118 | psa_status_t |
| 119 | mbedtls_test_opaque_cipher_encrypt(const psa_key_attributes_t *attributes, |
| 120 | const uint8_t *key, |
| 121 | size_t key_length, |
| 122 | psa_algorithm_t alg, |
| 123 | const uint8_t *input, |
| 124 | size_t input_length, |
| 125 | uint8_t *output, |
| 126 | size_t output_size, |
| 127 | size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 128 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 129 | psa_status_t |
| 130 | mbedtls_test_opaque_cipher_decrypt(const psa_key_attributes_t *attributes, |
| 131 | const uint8_t *key, |
| 132 | size_t key_length, |
| 133 | psa_algorithm_t alg, |
| 134 | const uint8_t *input, |
| 135 | size_t input_length, |
| 136 | uint8_t *output, |
| 137 | size_t output_size, |
| 138 | size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 139 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 140 | psa_status_t mbedtls_test_opaque_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 141 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 142 | const psa_key_attributes_t *attributes, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 143 | const uint8_t *key, |
| 144 | size_t key_length, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 145 | psa_algorithm_t alg); |
| 146 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 147 | psa_status_t mbedtls_test_opaque_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 148 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 149 | const psa_key_attributes_t *attributes, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 150 | const uint8_t *key, |
| 151 | size_t key_length, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 152 | psa_algorithm_t alg); |
| 153 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 154 | psa_status_t mbedtls_test_opaque_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 155 | mbedtls_opaque_test_driver_cipher_operation_t *operation); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 156 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 157 | psa_status_t mbedtls_test_opaque_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 158 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 159 | const uint8_t *iv, |
| 160 | size_t iv_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 161 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 162 | psa_status_t mbedtls_test_opaque_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 163 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 164 | const uint8_t *input, |
| 165 | size_t input_length, |
| 166 | uint8_t *output, |
| 167 | size_t output_size, |
| 168 | size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 169 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 170 | psa_status_t mbedtls_test_opaque_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 171 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 172 | uint8_t *output, |
| 173 | size_t output_size, |
| 174 | size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 175 | |
| 176 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 177 | #endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */ |