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 | |
| 23 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 24 | #include "mbedtls/config.h" |
| 25 | #else |
| 26 | #include MBEDTLS_CONFIG_FILE |
| 27 | #endif |
| 28 | |
| 29 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 30 | #include <psa/crypto_driver_common.h> |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame^] | 31 | #include <psa/crypto.h> |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 32 | |
| 33 | #include "mbedtls/cipher.h" |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame^] | 34 | typedef psa_cipher_operation_t test_transparent_cipher_operation_t; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 35 | |
| 36 | typedef struct{ |
| 37 | unsigned int initialised : 1; |
| 38 | test_transparent_cipher_operation_t ctx; |
| 39 | } test_opaque_cipher_operation_t; |
| 40 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 41 | typedef struct { |
| 42 | /* If non-null, on success, copy this to the output. */ |
| 43 | void *forced_output; |
| 44 | size_t forced_output_length; |
| 45 | /* If not PSA_SUCCESS, return this error code instead of processing the |
| 46 | * function call. */ |
| 47 | psa_status_t forced_status; |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 48 | /* Count the amount of times one of the cipher driver functions is called. */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 49 | unsigned long hits; |
| 50 | } test_driver_cipher_hooks_t; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 51 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 52 | #define TEST_DRIVER_CIPHER_INIT { NULL, 0, PSA_SUCCESS, 0 } |
| 53 | static inline test_driver_cipher_hooks_t test_driver_cipher_hooks_init( void ) |
| 54 | { |
| 55 | const test_driver_cipher_hooks_t v = TEST_DRIVER_CIPHER_INIT; |
| 56 | return( v ); |
| 57 | } |
| 58 | |
| 59 | extern test_driver_cipher_hooks_t test_driver_cipher_hooks; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 60 | |
| 61 | psa_status_t test_transparent_cipher_encrypt( |
| 62 | const psa_key_attributes_t *attributes, |
| 63 | const uint8_t *key, size_t key_length, |
| 64 | psa_algorithm_t alg, |
| 65 | const uint8_t *input, size_t input_length, |
| 66 | uint8_t *output, size_t output_size, size_t *output_length); |
| 67 | |
| 68 | psa_status_t test_transparent_cipher_decrypt( |
| 69 | const psa_key_attributes_t *attributes, |
| 70 | const uint8_t *key, size_t key_length, |
| 71 | psa_algorithm_t alg, |
| 72 | const uint8_t *input, size_t input_length, |
| 73 | uint8_t *output, size_t output_size, size_t *output_length); |
| 74 | |
| 75 | psa_status_t test_transparent_cipher_encrypt_setup( |
| 76 | test_transparent_cipher_operation_t *operation, |
| 77 | const psa_key_attributes_t *attributes, |
| 78 | const uint8_t *key, size_t key_length, |
| 79 | psa_algorithm_t alg); |
| 80 | |
| 81 | psa_status_t test_transparent_cipher_decrypt_setup( |
| 82 | test_transparent_cipher_operation_t *operation, |
| 83 | const psa_key_attributes_t *attributes, |
| 84 | const uint8_t *key, size_t key_length, |
| 85 | psa_algorithm_t alg); |
| 86 | |
| 87 | psa_status_t test_transparent_cipher_abort( |
| 88 | test_transparent_cipher_operation_t *operation); |
| 89 | |
| 90 | psa_status_t test_transparent_cipher_generate_iv( |
| 91 | test_transparent_cipher_operation_t *operation, |
| 92 | uint8_t *iv, |
| 93 | size_t iv_size, |
| 94 | size_t *iv_length); |
| 95 | |
| 96 | psa_status_t test_transparent_cipher_set_iv( |
| 97 | test_transparent_cipher_operation_t *operation, |
| 98 | const uint8_t *iv, |
| 99 | size_t iv_length); |
| 100 | |
| 101 | psa_status_t test_transparent_cipher_update( |
| 102 | test_transparent_cipher_operation_t *operation, |
| 103 | const uint8_t *input, |
| 104 | size_t input_length, |
| 105 | uint8_t *output, |
| 106 | size_t output_size, |
| 107 | size_t *output_length); |
| 108 | |
| 109 | psa_status_t test_transparent_cipher_finish( |
| 110 | test_transparent_cipher_operation_t *operation, |
| 111 | uint8_t *output, |
| 112 | size_t output_size, |
| 113 | size_t *output_length); |
| 114 | |
| 115 | /* |
| 116 | * opaque versions |
| 117 | */ |
| 118 | psa_status_t test_opaque_cipher_encrypt( |
| 119 | const psa_key_attributes_t *attributes, |
| 120 | const uint8_t *key, size_t key_length, |
| 121 | psa_algorithm_t alg, |
| 122 | const uint8_t *input, size_t input_length, |
| 123 | uint8_t *output, size_t output_size, size_t *output_length); |
| 124 | |
| 125 | psa_status_t test_opaque_cipher_decrypt( |
| 126 | const psa_key_attributes_t *attributes, |
| 127 | const uint8_t *key, size_t key_length, |
| 128 | psa_algorithm_t alg, |
| 129 | const uint8_t *input, size_t input_length, |
| 130 | uint8_t *output, size_t output_size, size_t *output_length); |
| 131 | |
| 132 | psa_status_t test_opaque_cipher_encrypt_setup( |
| 133 | test_opaque_cipher_operation_t *operation, |
| 134 | const psa_key_attributes_t *attributes, |
| 135 | const uint8_t *key, size_t key_length, |
| 136 | psa_algorithm_t alg); |
| 137 | |
| 138 | psa_status_t test_opaque_cipher_decrypt_setup( |
| 139 | test_opaque_cipher_operation_t *operation, |
| 140 | const psa_key_attributes_t *attributes, |
| 141 | const uint8_t *key, size_t key_length, |
| 142 | psa_algorithm_t alg); |
| 143 | |
| 144 | psa_status_t test_opaque_cipher_abort( |
| 145 | test_opaque_cipher_operation_t *operation); |
| 146 | |
| 147 | psa_status_t test_opaque_cipher_generate_iv( |
| 148 | test_opaque_cipher_operation_t *operation, |
| 149 | uint8_t *iv, |
| 150 | size_t iv_size, |
| 151 | size_t *iv_length); |
| 152 | |
| 153 | psa_status_t test_opaque_cipher_set_iv( |
| 154 | test_opaque_cipher_operation_t *operation, |
| 155 | const uint8_t *iv, |
| 156 | size_t iv_length); |
| 157 | |
| 158 | psa_status_t test_opaque_cipher_update( |
| 159 | test_opaque_cipher_operation_t *operation, |
| 160 | const uint8_t *input, |
| 161 | size_t input_length, |
| 162 | uint8_t *output, |
| 163 | size_t output_size, |
| 164 | size_t *output_length); |
| 165 | |
| 166 | psa_status_t test_opaque_cipher_finish( |
| 167 | test_opaque_cipher_operation_t *operation, |
| 168 | uint8_t *output, |
| 169 | size_t output_size, |
| 170 | size_t *output_length); |
| 171 | |
| 172 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 173 | #endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */ |