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 |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef PSA_CRYPTO_TEST_DRIVERS_CIPHER_H |
| 9 | #define PSA_CRYPTO_TEST_DRIVERS_CIPHER_H |
| 10 | |
| 11 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 12 | #include "mbedtls/config.h" |
| 13 | #else |
| 14 | #include MBEDTLS_CONFIG_FILE |
| 15 | #endif |
| 16 | |
| 17 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 18 | #include <psa/crypto_driver_common.h> |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 19 | #include <psa/crypto.h> |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 20 | |
| 21 | #include "mbedtls/cipher.h" |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 22 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 23 | typedef struct { |
| 24 | /* If non-null, on success, copy this to the output. */ |
| 25 | void *forced_output; |
| 26 | size_t forced_output_length; |
| 27 | /* If not PSA_SUCCESS, return this error code instead of processing the |
| 28 | * function call. */ |
| 29 | psa_status_t forced_status; |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 30 | /* 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] | 31 | unsigned long hits; |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 32 | } mbedtls_test_driver_cipher_hooks_t; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 33 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 34 | #define MBEDTLS_TEST_DRIVER_CIPHER_INIT { NULL, 0, PSA_SUCCESS, 0 } |
| 35 | static inline mbedtls_test_driver_cipher_hooks_t |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 36 | mbedtls_test_driver_cipher_hooks_init(void) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 37 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 38 | const mbedtls_test_driver_cipher_hooks_t v = MBEDTLS_TEST_DRIVER_CIPHER_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 39 | return v; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 42 | extern mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 43 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 44 | psa_status_t mbedtls_test_transparent_cipher_encrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 45 | const psa_key_attributes_t *attributes, |
| 46 | const uint8_t *key, size_t key_length, |
| 47 | psa_algorithm_t alg, |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 48 | const uint8_t *iv, size_t iv_length, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 49 | const uint8_t *input, size_t input_length, |
| 50 | uint8_t *output, size_t output_size, size_t *output_length); |
| 51 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 52 | psa_status_t mbedtls_test_transparent_cipher_decrypt( |
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 | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 59 | psa_status_t mbedtls_test_transparent_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 60 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 61 | const psa_key_attributes_t *attributes, |
| 62 | const uint8_t *key, size_t key_length, |
| 63 | psa_algorithm_t alg); |
| 64 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 65 | psa_status_t mbedtls_test_transparent_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 66 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 67 | const psa_key_attributes_t *attributes, |
| 68 | const uint8_t *key, size_t key_length, |
| 69 | psa_algorithm_t alg); |
| 70 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 71 | psa_status_t mbedtls_test_transparent_cipher_abort( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 72 | mbedtls_transparent_test_driver_cipher_operation_t *operation); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 73 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 74 | psa_status_t mbedtls_test_transparent_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 75 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
| 76 | const uint8_t *iv, size_t iv_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 77 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 78 | psa_status_t mbedtls_test_transparent_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 79 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
| 80 | const uint8_t *input, size_t input_length, |
| 81 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 82 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 83 | psa_status_t mbedtls_test_transparent_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 84 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
| 85 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * opaque versions |
| 89 | */ |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 90 | psa_status_t mbedtls_test_opaque_cipher_encrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 91 | const psa_key_attributes_t *attributes, |
| 92 | const uint8_t *key, size_t key_length, |
| 93 | psa_algorithm_t alg, |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 94 | const uint8_t *iv, size_t iv_length, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 95 | const uint8_t *input, size_t input_length, |
| 96 | uint8_t *output, size_t output_size, size_t *output_length); |
| 97 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 98 | psa_status_t mbedtls_test_opaque_cipher_decrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 99 | const psa_key_attributes_t *attributes, |
| 100 | const uint8_t *key, size_t key_length, |
| 101 | psa_algorithm_t alg, |
| 102 | const uint8_t *input, size_t input_length, |
| 103 | uint8_t *output, size_t output_size, size_t *output_length); |
| 104 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 105 | psa_status_t mbedtls_test_opaque_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 106 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 107 | const psa_key_attributes_t *attributes, |
| 108 | const uint8_t *key, size_t key_length, |
| 109 | psa_algorithm_t alg); |
| 110 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 111 | psa_status_t mbedtls_test_opaque_cipher_decrypt_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 | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 117 | psa_status_t mbedtls_test_opaque_cipher_abort( |
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 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 120 | psa_status_t mbedtls_test_opaque_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 121 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
| 122 | const uint8_t *iv, size_t iv_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 123 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 124 | psa_status_t mbedtls_test_opaque_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 125 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
| 126 | const uint8_t *input, size_t input_length, |
| 127 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 128 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 129 | psa_status_t mbedtls_test_opaque_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 130 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
| 131 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 132 | |
| 133 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 134 | #endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */ |