Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 1 | /* |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 2 | * Test driver for generating and verifying keys. |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 3 | */ |
Steven Cooreman | 2c7b2f8 | 2020-09-02 13:43: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 | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 8 | #ifndef PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H |
| 9 | #define PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 10 | |
| 11 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 12 | #include "mbedtls/config.h" |
| 13 | #else |
| 14 | #include MBEDTLS_CONFIG_FILE |
| 15 | #endif |
| 16 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 17 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 18 | #include <psa/crypto_driver_common.h> |
| 19 | |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 20 | #define PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT 0 |
| 21 | #define PSA_CRYPTO_TEST_DRIVER_BUILTIN_ECDSA_KEY_SLOT 1 |
Steven Cooreman | 437fcfc | 2021-02-22 12:44:15 +0100 | [diff] [blame] | 22 | |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +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 | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 30 | /* Count the amount of times one of the key management driver functions |
| 31 | * is called. */ |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 32 | unsigned long hits; |
Pengyu Lv | d3590a8 | 2023-12-07 16:11:53 +0800 | [diff] [blame^] | 33 | /* Subset of hits which only counts public key export operations */ |
| 34 | unsigned long hits_export_public_key; |
| 35 | /* Subset of hits which only counts key generation operations */ |
| 36 | unsigned long hits_generate_key; |
Andrzej Kurek | 28a7c06 | 2022-02-04 09:04:20 -0500 | [diff] [blame] | 37 | /* Location of the last key management driver called to import a key. */ |
| 38 | psa_key_location_t location; |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 39 | } mbedtls_test_driver_key_management_hooks_t; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 40 | |
Andrzej Kurek | 981a0ce | 2022-02-03 09:42:47 -0500 | [diff] [blame] | 41 | /* The location is initialized to the invalid value 0x800000. Invalid in the |
| 42 | * sense that no PSA specification will assign a meaning to this location |
| 43 | * (stated first in version 1.0.1 of the specification) and that it is not |
| 44 | * used as a location of an opaque test drivers. */ |
Pengyu Lv | d3590a8 | 2023-12-07 16:11:53 +0800 | [diff] [blame^] | 45 | #define MBEDTLS_TEST_DRIVER_KEY_MANAGEMENT_INIT { NULL, 0, PSA_SUCCESS, 0, 0, 0, 0x800000 } |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 46 | static inline mbedtls_test_driver_key_management_hooks_t |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 47 | mbedtls_test_driver_key_management_hooks_init(void) |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 48 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 49 | const mbedtls_test_driver_key_management_hooks_t |
| 50 | v = MBEDTLS_TEST_DRIVER_KEY_MANAGEMENT_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 51 | return v; |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 54 | extern mbedtls_test_driver_key_management_hooks_t |
| 55 | mbedtls_test_driver_key_management_hooks; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 56 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 57 | psa_status_t mbedtls_test_transparent_init(void); |
| 58 | void mbedtls_test_transparent_free(void); |
| 59 | psa_status_t mbedtls_test_opaque_init(void); |
| 60 | void mbedtls_test_opaque_free(void); |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 61 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 62 | psa_status_t mbedtls_test_transparent_generate_key( |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 63 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | uint8_t *key, size_t key_size, size_t *key_length); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 65 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 66 | psa_status_t mbedtls_test_opaque_generate_key( |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 67 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 68 | uint8_t *key, size_t key_size, size_t *key_length); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 69 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 70 | psa_status_t mbedtls_test_opaque_export_key( |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 71 | const psa_key_attributes_t *attributes, |
| 72 | const uint8_t *key, size_t key_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 73 | uint8_t *data, size_t data_size, size_t *data_length); |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 74 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 75 | psa_status_t mbedtls_test_transparent_export_public_key( |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 76 | const psa_key_attributes_t *attributes, |
| 77 | const uint8_t *key, size_t key_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 78 | uint8_t *data, size_t data_size, size_t *data_length); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 79 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 80 | psa_status_t mbedtls_test_opaque_export_public_key( |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 81 | const psa_key_attributes_t *attributes, |
| 82 | const uint8_t *key, size_t key_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 83 | uint8_t *data, size_t data_size, size_t *data_length); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 84 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 85 | psa_status_t mbedtls_test_transparent_import_key( |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 86 | const psa_key_attributes_t *attributes, |
| 87 | const uint8_t *data, |
| 88 | size_t data_length, |
| 89 | uint8_t *key_buffer, |
| 90 | size_t key_buffer_size, |
| 91 | size_t *key_buffer_length, |
| 92 | size_t *bits); |
| 93 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 94 | psa_status_t mbedtls_test_opaque_get_builtin_key( |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 95 | psa_drv_slot_number_t slot_number, |
| 96 | psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 97 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length); |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 98 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 99 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 100 | #endif /* PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H */ |