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) |
| 26 | #include <psa/crypto_driver_common.h> |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 27 | #include <psa/crypto.h> |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 28 | |
| 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; |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 38 | /* 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] | 39 | unsigned long hits; |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 40 | } mbedtls_test_driver_cipher_hooks_t; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 41 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 42 | #define MBEDTLS_TEST_DRIVER_CIPHER_INIT { NULL, 0, PSA_SUCCESS, 0 } |
| 43 | static inline mbedtls_test_driver_cipher_hooks_t |
| 44 | mbedtls_test_driver_cipher_hooks_init( void ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 45 | { |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 46 | const mbedtls_test_driver_cipher_hooks_t v = MBEDTLS_TEST_DRIVER_CIPHER_INIT; |
Mateusz Starzyk | e36f5b1 | 2021-07-22 16:43:35 +0200 | [diff] [blame^] | 47 | return v ; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 50 | extern mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 51 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 52 | psa_status_t mbedtls_test_transparent_cipher_encrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 53 | const psa_key_attributes_t *attributes, |
| 54 | const uint8_t *key, size_t key_length, |
| 55 | psa_algorithm_t alg, |
| 56 | const uint8_t *input, size_t input_length, |
| 57 | uint8_t *output, size_t output_size, size_t *output_length); |
| 58 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 59 | psa_status_t mbedtls_test_transparent_cipher_decrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 60 | const psa_key_attributes_t *attributes, |
| 61 | const uint8_t *key, size_t key_length, |
| 62 | psa_algorithm_t alg, |
| 63 | const uint8_t *input, size_t input_length, |
| 64 | uint8_t *output, size_t output_size, size_t *output_length); |
| 65 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 66 | psa_status_t mbedtls_test_transparent_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 67 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 68 | const psa_key_attributes_t *attributes, |
| 69 | const uint8_t *key, size_t key_length, |
| 70 | psa_algorithm_t alg); |
| 71 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 72 | psa_status_t mbedtls_test_transparent_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 73 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 74 | const psa_key_attributes_t *attributes, |
| 75 | const uint8_t *key, size_t key_length, |
| 76 | psa_algorithm_t alg); |
| 77 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 78 | psa_status_t mbedtls_test_transparent_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 79 | mbedtls_transparent_test_driver_cipher_operation_t *operation ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 80 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 81 | psa_status_t mbedtls_test_transparent_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 82 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
| 83 | const uint8_t *iv, size_t iv_length); |
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_update( |
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 *input, size_t input_length, |
| 88 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 89 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 90 | psa_status_t mbedtls_test_transparent_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 91 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
| 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 | |
| 94 | /* |
| 95 | * opaque versions |
| 96 | */ |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 97 | psa_status_t mbedtls_test_opaque_cipher_encrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 98 | const psa_key_attributes_t *attributes, |
| 99 | const uint8_t *key, size_t key_length, |
| 100 | psa_algorithm_t alg, |
| 101 | const uint8_t *input, size_t input_length, |
| 102 | uint8_t *output, size_t output_size, size_t *output_length); |
| 103 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 104 | psa_status_t mbedtls_test_opaque_cipher_decrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 105 | const psa_key_attributes_t *attributes, |
| 106 | const uint8_t *key, size_t key_length, |
| 107 | psa_algorithm_t alg, |
| 108 | const uint8_t *input, size_t input_length, |
| 109 | uint8_t *output, size_t output_size, size_t *output_length); |
| 110 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 111 | psa_status_t mbedtls_test_opaque_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 112 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 113 | const psa_key_attributes_t *attributes, |
| 114 | const uint8_t *key, size_t key_length, |
| 115 | psa_algorithm_t alg); |
| 116 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 117 | psa_status_t mbedtls_test_opaque_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 118 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 119 | const psa_key_attributes_t *attributes, |
| 120 | const uint8_t *key, size_t key_length, |
| 121 | psa_algorithm_t alg); |
| 122 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 123 | psa_status_t mbedtls_test_opaque_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 124 | mbedtls_opaque_test_driver_cipher_operation_t *operation); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 125 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 126 | psa_status_t mbedtls_test_opaque_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 127 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
| 128 | const uint8_t *iv, size_t iv_length); |
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_update( |
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 *input, size_t input_length, |
| 133 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 134 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 135 | psa_status_t mbedtls_test_opaque_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 136 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
| 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 | |
| 139 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 140 | #endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */ |