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 |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +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 | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 20 | #ifndef PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H |
| 21 | #define PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 22 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 23 | #include "mbedtls/build_info.h" |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 24 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 25 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 26 | #include <psa/crypto_driver_common.h> |
| 27 | |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 28 | #define PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT 0 |
| 29 | #define PSA_CRYPTO_TEST_DRIVER_BUILTIN_ECDSA_KEY_SLOT 1 |
Steven Cooreman | 437fcfc | 2021-02-22 12:44:15 +0100 | [diff] [blame] | 30 | |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +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 | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 38 | /* Count the amount of times one of the key management driver functions |
| 39 | * is called. */ |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 40 | unsigned long hits; |
Andrzej Kurek | ba4cade | 2022-02-03 10:27:01 -0500 | [diff] [blame] | 41 | /* Location of the last key management driver called to import a key. */ |
| 42 | psa_key_location_t location; |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 43 | } mbedtls_test_driver_key_management_hooks_t; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 44 | |
Andrzej Kurek | 333e0fa | 2022-02-03 09:42:47 -0500 | [diff] [blame] | 45 | /* The location is initialized to the invalid value 0x800000. Invalid in the |
| 46 | * sense that no PSA specification will assign a meaning to this location |
| 47 | * (stated first in version 1.0.1 of the specification) and that it is not |
| 48 | * used as a location of an opaque test drivers. */ |
Andrzej Kurek | dc13725 | 2021-12-07 19:06:51 +0100 | [diff] [blame] | 49 | #define MBEDTLS_TEST_DRIVER_KEY_MANAGEMENT_INIT { NULL, 0, PSA_SUCCESS, 0, 0x800000 } |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 50 | static inline mbedtls_test_driver_key_management_hooks_t |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | mbedtls_test_driver_key_management_hooks_init(void) |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 52 | { |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 53 | const mbedtls_test_driver_key_management_hooks_t |
| 54 | v = MBEDTLS_TEST_DRIVER_KEY_MANAGEMENT_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | return v; |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 58 | /* |
| 59 | * In order to convert the plain text keys to Opaque, the size of the key is |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 60 | * padded up by PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX_SIZE in addition to |
| 61 | * xor mangling the key. The pad prefix needs to be accounted for while |
| 62 | * sizing for the key. |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 63 | */ |
| 64 | #define PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX 0xBEEFED00U |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 65 | #define PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX_SIZE sizeof( \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX) |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 67 | |
| 68 | size_t mbedtls_test_opaque_size_function( |
| 69 | const psa_key_type_t key_type, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 70 | const size_t key_bits); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 71 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 72 | extern mbedtls_test_driver_key_management_hooks_t |
| 73 | mbedtls_test_driver_key_management_hooks; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 74 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | psa_status_t mbedtls_test_transparent_init(void); |
| 76 | void mbedtls_test_transparent_free(void); |
| 77 | psa_status_t mbedtls_test_opaque_init(void); |
| 78 | void mbedtls_test_opaque_free(void); |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 79 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 80 | psa_status_t mbedtls_test_transparent_generate_key( |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 81 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | uint8_t *key, size_t key_size, size_t *key_length); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 83 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 84 | psa_status_t mbedtls_test_opaque_generate_key( |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 85 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | uint8_t *key, size_t key_size, size_t *key_length); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 87 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 88 | psa_status_t mbedtls_test_opaque_export_key( |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 89 | const psa_key_attributes_t *attributes, |
| 90 | const uint8_t *key, size_t key_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | uint8_t *data, size_t data_size, size_t *data_length); |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 92 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 93 | psa_status_t mbedtls_test_transparent_export_public_key( |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 94 | const psa_key_attributes_t *attributes, |
| 95 | const uint8_t *key, size_t key_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | uint8_t *data, size_t data_size, size_t *data_length); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 97 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 98 | psa_status_t mbedtls_test_opaque_export_public_key( |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 99 | const psa_key_attributes_t *attributes, |
| 100 | const uint8_t *key, size_t key_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | uint8_t *data, size_t data_size, size_t *data_length); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 102 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 103 | psa_status_t mbedtls_test_transparent_import_key( |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 104 | const psa_key_attributes_t *attributes, |
| 105 | const uint8_t *data, |
| 106 | size_t data_length, |
| 107 | uint8_t *key_buffer, |
| 108 | size_t key_buffer_size, |
| 109 | size_t *key_buffer_length, |
| 110 | size_t *bits); |
| 111 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 112 | psa_status_t mbedtls_test_opaque_import_key( |
| 113 | const psa_key_attributes_t *attributes, |
| 114 | const uint8_t *data, |
| 115 | size_t data_length, |
| 116 | uint8_t *key_buffer, |
| 117 | size_t key_buffer_size, |
| 118 | size_t *key_buffer_length, |
| 119 | size_t *bits); |
| 120 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 121 | psa_status_t mbedtls_test_opaque_get_builtin_key( |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 122 | psa_drv_slot_number_t slot_number, |
| 123 | psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | 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] | 125 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 126 | psa_status_t mbedtls_test_opaque_copy_key( |
| 127 | psa_key_attributes_t *attributes, |
| 128 | const uint8_t *source_key, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 129 | size_t source_key_length, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 130 | uint8_t *target_key_buffer, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 131 | size_t target_key_buffer_size, |
| 132 | size_t *target_key_buffer_length); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 133 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 134 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 135 | #endif /* PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H */ |