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
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 #if !defined(MBEDTLS_CONFIG_FILE)
22 #include "mbedtls/config.h"
23 #else
24 #include MBEDTLS_CONFIG_FILE
25 #endif
26
27 #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
28
29 #include "test/drivers/size.h"
30 #include "psa/crypto.h"
31
32 typedef struct {
33 unsigned int context;
34 } test_driver_key_context_t;
35
36 /*
37 * This macro returns the base size for the key context. It is the size of the
38 * driver specific information stored in each key context.
39 */
40 #define TEST_DRIVER_KEY_CONTEXT_BASE_SIZE sizeof( test_driver_key_context_t )
41
42 /*
43 * Number of bytes included in every key context for a key pair.
44 *
45 * This pair size is for an ECC 256-bit private/public key pair.
46 * Based on this value, the size of the private key can be derived by
47 * subtracting the public key size below from this one.
48 */
49 #define TEST_DRIVER_KEY_CONTEXT_KEY_PAIR_SIZE 65
50
51 /*
52 * Number of bytes included in every key context for a public key.
53 *
54 * For ECC public keys, it needs 257 bits so 33 bytes.
55 */
56 #define TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE 33
57
58 /*
59 * Every key context for a symmetric key includes this many times the key size.
60 */
61 #define TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR 0
62
63 /*
64 * If this is true for a key pair, the key context includes space for the public key.
65 * If this is false, no additional space is added for the public key.
66 *
67 * For this instance, store the public key with the private one.
68 */
69 #define TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY 1
70
mbedtls_test_size_function(const psa_key_type_t key_type,const size_t key_bits)71 size_t mbedtls_test_size_function(
72 const psa_key_type_t key_type,
73 const size_t key_bits )
74 {
75 size_t key_buffer_size = 0;
76
77 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
78 {
79 int public_key_overhead =
80 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 )
81 ? PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
82 key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE +
83 TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE +
84 public_key_overhead;
85 }
86 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
87 {
88 key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE +
89 TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
90 }
91 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
92 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
93 {
94 key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE +
95 ( TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR *
96 ( ( key_bits + 7 ) / 8 ) );
97 }
98
99 return( key_buffer_size );
100 }
101 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
102