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 | dc13725 | 2021-12-07 19:06:51 +0100 | [diff] [blame^] | 41 | /* Record the source of the function call. */ |
| 42 | psa_key_location_t source; |
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 | dc13725 | 2021-12-07 19:06:51 +0100 | [diff] [blame^] | 45 | /* 0x800000 is a vendor-specific location, unused by the PSA, overwritten |
| 46 | * in tests that expect a different value. */ |
| 47 | #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] | 48 | static inline mbedtls_test_driver_key_management_hooks_t |
| 49 | mbedtls_test_driver_key_management_hooks_init( void ) |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 50 | { |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 51 | const mbedtls_test_driver_key_management_hooks_t |
| 52 | v = MBEDTLS_TEST_DRIVER_KEY_MANAGEMENT_INIT; |
Steven Cooreman | 831c695 | 2020-09-07 12:58:16 +0200 | [diff] [blame] | 53 | return( v ); |
| 54 | } |
| 55 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 56 | /* |
| 57 | * 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] | 58 | * padded up by PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX_SIZE in addition to |
| 59 | * xor mangling the key. The pad prefix needs to be accounted for while |
| 60 | * sizing for the key. |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 61 | */ |
| 62 | #define PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX 0xBEEFED00U |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 63 | #define PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX_SIZE sizeof( \ |
| 64 | PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX ) |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 65 | |
| 66 | size_t mbedtls_test_opaque_size_function( |
| 67 | const psa_key_type_t key_type, |
| 68 | const size_t key_bits ); |
| 69 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 70 | extern mbedtls_test_driver_key_management_hooks_t |
| 71 | mbedtls_test_driver_key_management_hooks; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 72 | |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 73 | psa_status_t mbedtls_test_transparent_init( void ); |
| 74 | void mbedtls_test_transparent_free( void ); |
| 75 | psa_status_t mbedtls_test_opaque_init( void ); |
| 76 | void mbedtls_test_opaque_free( void ); |
| 77 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 78 | psa_status_t mbedtls_test_transparent_generate_key( |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 79 | const psa_key_attributes_t *attributes, |
| 80 | uint8_t *key, size_t key_size, size_t *key_length ); |
| 81 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 82 | psa_status_t mbedtls_test_opaque_generate_key( |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 83 | const psa_key_attributes_t *attributes, |
| 84 | uint8_t *key, size_t key_size, size_t *key_length ); |
| 85 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 86 | psa_status_t mbedtls_test_opaque_export_key( |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 87 | const psa_key_attributes_t *attributes, |
| 88 | const uint8_t *key, size_t key_length, |
| 89 | uint8_t *data, size_t data_size, size_t *data_length ); |
| 90 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 91 | psa_status_t mbedtls_test_transparent_export_public_key( |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 92 | const psa_key_attributes_t *attributes, |
| 93 | const uint8_t *key, size_t key_length, |
| 94 | uint8_t *data, size_t data_size, size_t *data_length ); |
| 95 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 96 | psa_status_t mbedtls_test_opaque_export_public_key( |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 97 | const psa_key_attributes_t *attributes, |
| 98 | const uint8_t *key, size_t key_length, |
| 99 | uint8_t *data, size_t data_size, size_t *data_length ); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 100 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 101 | psa_status_t mbedtls_test_transparent_import_key( |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 102 | const psa_key_attributes_t *attributes, |
| 103 | const uint8_t *data, |
| 104 | size_t data_length, |
| 105 | uint8_t *key_buffer, |
| 106 | size_t key_buffer_size, |
| 107 | size_t *key_buffer_length, |
| 108 | size_t *bits); |
| 109 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 110 | psa_status_t mbedtls_test_opaque_import_key( |
| 111 | const psa_key_attributes_t *attributes, |
| 112 | const uint8_t *data, |
| 113 | size_t data_length, |
| 114 | uint8_t *key_buffer, |
| 115 | size_t key_buffer_size, |
| 116 | size_t *key_buffer_length, |
| 117 | size_t *bits); |
| 118 | |
Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 119 | psa_status_t mbedtls_test_opaque_get_builtin_key( |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 120 | psa_drv_slot_number_t slot_number, |
| 121 | psa_key_attributes_t *attributes, |
| 122 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ); |
| 123 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 124 | psa_status_t mbedtls_test_opaque_copy_key( |
| 125 | psa_key_attributes_t *attributes, |
| 126 | const uint8_t *source_key, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 127 | size_t source_key_length, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 128 | uint8_t *target_key_buffer, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 129 | size_t target_key_buffer_size, |
| 130 | size_t *target_key_buffer_length); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 131 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 132 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | c4813a6 | 2020-10-23 11:45:43 +0200 | [diff] [blame] | 133 | #endif /* PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H */ |