1 /* 2 * Test driver for retrieving key context size. 3 * Only used by opaque drivers. 4 */ 5 /* Copyright The Mbed TLS Contributors 6 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 7 */ 8 9 #if !defined(MBEDTLS_CONFIG_FILE) 10 #include "mbedtls/config.h" 11 #else 12 #include MBEDTLS_CONFIG_FILE 13 #endif 14 15 #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST) 16 17 #include "test/drivers/size.h" 18 #include "psa/crypto.h" 19 20 typedef struct { 21 unsigned int context; 22 } test_driver_key_context_t; 23 24 /* 25 * This macro returns the base size for the key context. It is the size of the 26 * driver specific information stored in each key context. 27 */ 28 #define TEST_DRIVER_KEY_CONTEXT_BASE_SIZE sizeof(test_driver_key_context_t) 29 30 /* 31 * Number of bytes included in every key context for a key pair. 32 * 33 * This pair size is for an ECC 256-bit private/public key pair. 34 * Based on this value, the size of the private key can be derived by 35 * subtracting the public key size below from this one. 36 */ 37 #define TEST_DRIVER_KEY_CONTEXT_KEY_PAIR_SIZE 65 38 39 /* 40 * Number of bytes included in every key context for a public key. 41 * 42 * For ECC public keys, it needs 257 bits so 33 bytes. 43 */ 44 #define TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE 33 45 46 /* 47 * Every key context for a symmetric key includes this many times the key size. 48 */ 49 #define TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR 0 50 51 /* 52 * If this is true for a key pair, the key context includes space for the public key. 53 * If this is false, no additional space is added for the public key. 54 * 55 * For this instance, store the public key with the private one. 56 */ 57 #define TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY 1 58 mbedtls_test_size_function(const psa_key_type_t key_type,const size_t key_bits)59size_t mbedtls_test_size_function( 60 const psa_key_type_t key_type, 61 const size_t key_bits) 62 { 63 size_t key_buffer_size = 0; 64 65 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_type)) { 66 int public_key_overhead = 67 ((TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1) 68 ? PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) : 0); 69 key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE + 70 TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE + 71 public_key_overhead; 72 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(key_type)) { 73 key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE + 74 TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE; 75 } else if (!PSA_KEY_TYPE_IS_KEY_PAIR(key_type) && 76 !PSA_KEY_TYPE_IS_PUBLIC_KEY(key_type)) { 77 key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE + 78 (TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR * 79 ((key_bits + 7) / 8)); 80 } 81 82 return key_buffer_size; 83 } 84 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ 85