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" |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 34 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 35 | typedef struct { |
| 36 | /* If non-null, on success, copy this to the output. */ |
| 37 | void *forced_output; |
| 38 | size_t forced_output_length; |
| 39 | /* If not PSA_SUCCESS, return this error code instead of processing the |
| 40 | * function call. */ |
| 41 | psa_status_t forced_status; |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 42 | /* 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] | 43 | unsigned long hits; |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 44 | } mbedtls_test_driver_cipher_hooks_t; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 45 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 46 | #define MBEDTLS_TEST_DRIVER_CIPHER_INIT { NULL, 0, PSA_SUCCESS, 0 } |
| 47 | static inline mbedtls_test_driver_cipher_hooks_t |
| 48 | mbedtls_test_driver_cipher_hooks_init( void ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 49 | { |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 50 | const mbedtls_test_driver_cipher_hooks_t v = MBEDTLS_TEST_DRIVER_CIPHER_INIT; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 51 | return( v ); |
| 52 | } |
| 53 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 54 | extern mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 55 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 56 | psa_status_t mbedtls_test_transparent_cipher_encrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 57 | const psa_key_attributes_t *attributes, |
| 58 | const uint8_t *key, size_t key_length, |
| 59 | psa_algorithm_t alg, |
| 60 | const uint8_t *input, size_t input_length, |
| 61 | uint8_t *output, size_t output_size, size_t *output_length); |
| 62 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 63 | psa_status_t mbedtls_test_transparent_cipher_decrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 64 | const psa_key_attributes_t *attributes, |
| 65 | const uint8_t *key, size_t key_length, |
| 66 | psa_algorithm_t alg, |
| 67 | const uint8_t *input, size_t input_length, |
| 68 | uint8_t *output, size_t output_size, size_t *output_length); |
| 69 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 70 | psa_status_t mbedtls_test_transparent_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 71 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 72 | const psa_key_attributes_t *attributes, |
| 73 | const uint8_t *key, size_t key_length, |
| 74 | psa_algorithm_t alg); |
| 75 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 76 | psa_status_t mbedtls_test_transparent_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 77 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 78 | const psa_key_attributes_t *attributes, |
| 79 | const uint8_t *key, size_t key_length, |
| 80 | psa_algorithm_t alg); |
| 81 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 82 | psa_status_t mbedtls_test_transparent_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 83 | mbedtls_transparent_test_driver_cipher_operation_t *operation ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 84 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 85 | psa_status_t mbedtls_test_transparent_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 86 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
| 87 | const uint8_t *iv, size_t iv_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 88 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 89 | psa_status_t mbedtls_test_transparent_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 90 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
| 91 | const uint8_t *input, size_t input_length, |
| 92 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 93 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 94 | psa_status_t mbedtls_test_transparent_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 95 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
| 96 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * opaque versions |
| 100 | */ |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 101 | psa_status_t mbedtls_test_opaque_cipher_encrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 102 | const psa_key_attributes_t *attributes, |
| 103 | const uint8_t *key, size_t key_length, |
| 104 | psa_algorithm_t alg, |
| 105 | const uint8_t *input, size_t input_length, |
| 106 | uint8_t *output, size_t output_size, size_t *output_length); |
| 107 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 108 | psa_status_t mbedtls_test_opaque_cipher_decrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 109 | const psa_key_attributes_t *attributes, |
| 110 | const uint8_t *key, size_t key_length, |
| 111 | psa_algorithm_t alg, |
| 112 | const uint8_t *input, size_t input_length, |
| 113 | uint8_t *output, size_t output_size, size_t *output_length); |
| 114 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 115 | psa_status_t mbedtls_test_opaque_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 116 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 117 | const psa_key_attributes_t *attributes, |
| 118 | const uint8_t *key, size_t key_length, |
| 119 | psa_algorithm_t alg); |
| 120 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 121 | psa_status_t mbedtls_test_opaque_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 122 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 123 | const psa_key_attributes_t *attributes, |
| 124 | const uint8_t *key, size_t key_length, |
| 125 | psa_algorithm_t alg); |
| 126 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 127 | psa_status_t mbedtls_test_opaque_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 128 | mbedtls_opaque_test_driver_cipher_operation_t *operation); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 129 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 130 | psa_status_t mbedtls_test_opaque_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 131 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
| 132 | const uint8_t *iv, size_t iv_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 133 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 134 | psa_status_t mbedtls_test_opaque_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 135 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
| 136 | const uint8_t *input, size_t input_length, |
| 137 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 138 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame^] | 139 | psa_status_t mbedtls_test_opaque_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 140 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
| 141 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 142 | |
| 143 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 144 | #endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */ |