1 /* 2 * Test driver for generating and verifying keys. 3 */ 4 /* Copyright The Mbed TLS Contributors 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. 18 */ 19 20 #ifndef PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H 21 #define PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H 22 23 #include "mbedtls/build_info.h" 24 25 #if defined(PSA_CRYPTO_DRIVER_TEST) 26 #include <psa/crypto_driver_common.h> 27 28 #define PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT 0 29 #define PSA_CRYPTO_TEST_DRIVER_BUILTIN_ECDSA_KEY_SLOT 1 30 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; 38 /* Count the amount of times one of the key management driver functions 39 * is called. */ 40 unsigned long hits; 41 } mbedtls_test_driver_key_management_hooks_t; 42 43 #define MBEDTLS_TEST_DRIVER_KEY_MANAGEMENT_INIT { NULL, 0, PSA_SUCCESS, 0 } 44 static inline mbedtls_test_driver_key_management_hooks_t mbedtls_test_driver_key_management_hooks_init(void)45 mbedtls_test_driver_key_management_hooks_init( void ) 46 { 47 const mbedtls_test_driver_key_management_hooks_t 48 v = MBEDTLS_TEST_DRIVER_KEY_MANAGEMENT_INIT; 49 return( v ); 50 } 51 52 /* 53 * In order to convert the plain text keys to Opaque, the size of the key is 54 * padded up by PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX_SIZE in addition to 55 * xor mangling the key. The pad prefix needs to be accounted for while 56 * sizing for the key. 57 */ 58 #define PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX 0xBEEFED00U 59 #define PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX_SIZE sizeof( \ 60 PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX ) 61 62 size_t mbedtls_test_opaque_size_function( 63 const psa_key_type_t key_type, 64 const size_t key_bits ); 65 66 extern mbedtls_test_driver_key_management_hooks_t 67 mbedtls_test_driver_key_management_hooks; 68 69 psa_status_t mbedtls_test_transparent_init( void ); 70 void mbedtls_test_transparent_free( void ); 71 psa_status_t mbedtls_test_opaque_init( void ); 72 void mbedtls_test_opaque_free( void ); 73 74 psa_status_t mbedtls_test_transparent_generate_key( 75 const psa_key_attributes_t *attributes, 76 uint8_t *key, size_t key_size, size_t *key_length ); 77 78 psa_status_t mbedtls_test_opaque_generate_key( 79 const psa_key_attributes_t *attributes, 80 uint8_t *key, size_t key_size, size_t *key_length ); 81 82 psa_status_t mbedtls_test_opaque_export_key( 83 const psa_key_attributes_t *attributes, 84 const uint8_t *key, size_t key_length, 85 uint8_t *data, size_t data_size, size_t *data_length ); 86 87 psa_status_t mbedtls_test_transparent_export_public_key( 88 const psa_key_attributes_t *attributes, 89 const uint8_t *key, size_t key_length, 90 uint8_t *data, size_t data_size, size_t *data_length ); 91 92 psa_status_t mbedtls_test_opaque_export_public_key( 93 const psa_key_attributes_t *attributes, 94 const uint8_t *key, size_t key_length, 95 uint8_t *data, size_t data_size, size_t *data_length ); 96 97 psa_status_t mbedtls_test_transparent_import_key( 98 const psa_key_attributes_t *attributes, 99 const uint8_t *data, 100 size_t data_length, 101 uint8_t *key_buffer, 102 size_t key_buffer_size, 103 size_t *key_buffer_length, 104 size_t *bits); 105 106 psa_status_t mbedtls_test_opaque_import_key( 107 const psa_key_attributes_t *attributes, 108 const uint8_t *data, 109 size_t data_length, 110 uint8_t *key_buffer, 111 size_t key_buffer_size, 112 size_t *key_buffer_length, 113 size_t *bits); 114 115 psa_status_t mbedtls_test_opaque_get_builtin_key( 116 psa_drv_slot_number_t slot_number, 117 psa_key_attributes_t *attributes, 118 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ); 119 120 psa_status_t mbedtls_test_opaque_copy_key( 121 psa_key_attributes_t *attributes, 122 const uint8_t *source_key, 123 size_t source_key_length, 124 uint8_t *target_key_buffer, 125 size_t target_key_buffer_size, 126 size_t *target_key_buffer_length); 127 128 #endif /* PSA_CRYPTO_DRIVER_TEST */ 129 #endif /* PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H */ 130