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 | 16799db | 2023-11-02 19:47:20 +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 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 11 | #include "mbedtls/build_info.h" |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 12 | |
| 13 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 14 | #include <psa/crypto_driver_common.h> |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 15 | #include <psa/crypto.h> |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 16 | |
| 17 | #include "mbedtls/cipher.h" |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 18 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 19 | typedef struct { |
| 20 | /* If non-null, on success, copy this to the output. */ |
| 21 | void *forced_output; |
| 22 | size_t forced_output_length; |
| 23 | /* If not PSA_SUCCESS, return this error code instead of processing the |
| 24 | * function call. */ |
| 25 | psa_status_t forced_status; |
Valerio Setti | 829ce0f | 2023-11-27 12:27:46 +0100 | [diff] [blame^] | 26 | psa_status_t forced_status_encrypt; |
| 27 | psa_status_t forced_status_set_iv; |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 28 | /* 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] | 29 | unsigned long hits; |
Valerio Setti | 829ce0f | 2023-11-27 12:27:46 +0100 | [diff] [blame^] | 30 | unsigned long hits_encrypt; |
| 31 | unsigned long hits_set_iv; |
Ronald Cron | 7f13fa2 | 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 | |
Valerio Setti | 829ce0f | 2023-11-27 12:27:46 +0100 | [diff] [blame^] | 34 | #define MBEDTLS_TEST_DRIVER_CIPHER_INIT { NULL, 0, \ |
| 35 | PSA_SUCCESS, PSA_SUCCESS, PSA_SUCCESS, \ |
| 36 | 0, 0, 0 } |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 37 | static inline mbedtls_test_driver_cipher_hooks_t |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | mbedtls_test_driver_cipher_hooks_init(void) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 39 | { |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 40 | const mbedtls_test_driver_cipher_hooks_t v = MBEDTLS_TEST_DRIVER_CIPHER_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 41 | return v; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 44 | extern mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks; |
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 | psa_status_t mbedtls_test_transparent_cipher_encrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 47 | const psa_key_attributes_t *attributes, |
| 48 | const uint8_t *key, size_t key_length, |
| 49 | psa_algorithm_t alg, |
Ronald Cron | 9b67428 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 50 | const uint8_t *iv, size_t iv_length, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 51 | const uint8_t *input, size_t input_length, |
| 52 | uint8_t *output, size_t output_size, size_t *output_length); |
| 53 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 54 | psa_status_t mbedtls_test_transparent_cipher_decrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 55 | const psa_key_attributes_t *attributes, |
| 56 | const uint8_t *key, size_t key_length, |
| 57 | psa_algorithm_t alg, |
| 58 | const uint8_t *input, size_t input_length, |
| 59 | uint8_t *output, size_t output_size, size_t *output_length); |
| 60 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 61 | psa_status_t mbedtls_test_transparent_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 62 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 63 | const psa_key_attributes_t *attributes, |
| 64 | const uint8_t *key, size_t key_length, |
| 65 | psa_algorithm_t alg); |
| 66 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 67 | psa_status_t mbedtls_test_transparent_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 68 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 69 | const psa_key_attributes_t *attributes, |
| 70 | const uint8_t *key, size_t key_length, |
| 71 | psa_algorithm_t alg); |
| 72 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 73 | psa_status_t mbedtls_test_transparent_cipher_abort( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | mbedtls_transparent_test_driver_cipher_operation_t *operation); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 75 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 76 | psa_status_t mbedtls_test_transparent_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 77 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
| 78 | const uint8_t *iv, size_t iv_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 79 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 80 | psa_status_t mbedtls_test_transparent_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 81 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
| 82 | const uint8_t *input, size_t input_length, |
| 83 | uint8_t *output, size_t output_size, size_t *output_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_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 86 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
| 87 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * opaque versions |
| 91 | */ |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 92 | psa_status_t mbedtls_test_opaque_cipher_encrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 93 | const psa_key_attributes_t *attributes, |
| 94 | const uint8_t *key, size_t key_length, |
| 95 | psa_algorithm_t alg, |
Ronald Cron | 9b67428 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 96 | const uint8_t *iv, size_t iv_length, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 97 | const uint8_t *input, size_t input_length, |
| 98 | uint8_t *output, size_t output_size, size_t *output_length); |
| 99 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 100 | psa_status_t mbedtls_test_opaque_cipher_decrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 101 | const psa_key_attributes_t *attributes, |
| 102 | const uint8_t *key, size_t key_length, |
| 103 | psa_algorithm_t alg, |
| 104 | const uint8_t *input, size_t input_length, |
| 105 | uint8_t *output, size_t output_size, size_t *output_length); |
| 106 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 107 | psa_status_t mbedtls_test_opaque_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 108 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
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 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 113 | psa_status_t mbedtls_test_opaque_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 114 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 115 | const psa_key_attributes_t *attributes, |
| 116 | const uint8_t *key, size_t key_length, |
| 117 | psa_algorithm_t alg); |
| 118 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 119 | psa_status_t mbedtls_test_opaque_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 120 | mbedtls_opaque_test_driver_cipher_operation_t *operation); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 121 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 122 | psa_status_t mbedtls_test_opaque_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 123 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
| 124 | const uint8_t *iv, size_t iv_length); |
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_update( |
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 *input, size_t input_length, |
| 129 | uint8_t *output, size_t output_size, size_t *output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 130 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 131 | psa_status_t mbedtls_test_opaque_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 132 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
| 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 | |
| 135 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 136 | #endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */ |