blob: a788934fa118e310856dac69680a76eeb7590f61 [file] [log] [blame]
Steven Cooreman2a1664c2020-07-20 15:33:08 +02001/*
Steven Cooreman04524762020-10-13 17:43:44 +02002 * Test driver for generating and verifying keys.
3 * Currently only supports generating and verifying ECC keys.
Steven Cooreman2a1664c2020-07-20 15:33:08 +02004 */
Steven Cooreman2c7b2f82020-09-02 13:43:46 +02005/* Copyright The Mbed TLS Contributors
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006 * 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.
Steven Cooreman2a1664c2020-07-20 15:33:08 +020019 */
20
21#if !defined(MBEDTLS_CONFIG_FILE)
22#include "mbedtls/config.h"
23#else
24#include MBEDTLS_CONFIG_FILE
25#endif
26
Steven Cooremanf1720ea2020-07-24 18:41:58 +020027#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +020028#include "psa/crypto.h"
Steven Cooreman15f58d22020-09-04 13:05:23 +020029#include "psa_crypto_core.h"
Steven Cooreman2a1664c2020-07-20 15:33:08 +020030#include "mbedtls/ecp.h"
31#include "mbedtls/error.h"
32
Steven Cooremanc4813a62020-10-23 11:45:43 +020033#include "test/drivers/key_management.h"
Steven Cooreman2a1664c2020-07-20 15:33:08 +020034
35#include "test/random.h"
36
37#include <string.h>
38
Steven Cooremanc4813a62020-10-23 11:45:43 +020039test_driver_key_management_hooks_t test_driver_key_management_hooks =
40 TEST_DRIVER_KEY_MANAGEMENT_INIT;
Steven Cooreman2a1664c2020-07-20 15:33:08 +020041
42psa_status_t test_transparent_generate_key(
43 const psa_key_attributes_t *attributes,
44 uint8_t *key, size_t key_size, size_t *key_length )
45{
John Durkop9814fa22020-11-04 12:28:15 -080046#if !defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) && \
47 !defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
John Durkop0e005192020-10-31 22:06:54 -070048 (void)attributes;
49#endif /* !MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR && !MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY */
Steven Cooremanc4813a62020-10-23 11:45:43 +020050 ++test_driver_key_management_hooks.hits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +020051
Steven Cooremanc4813a62020-10-23 11:45:43 +020052 if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS )
53 return( test_driver_key_management_hooks.forced_status );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020054
Steven Cooremanc4813a62020-10-23 11:45:43 +020055 if( test_driver_key_management_hooks.forced_output != NULL )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020056 {
Steven Cooremanc4813a62020-10-23 11:45:43 +020057 if( test_driver_key_management_hooks.forced_output_length > key_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020058 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremanc4813a62020-10-23 11:45:43 +020059 memcpy( key, test_driver_key_management_hooks.forced_output,
60 test_driver_key_management_hooks.forced_output_length );
61 *key_length = test_driver_key_management_hooks.forced_output_length;
Steven Cooreman2a1664c2020-07-20 15:33:08 +020062 return( PSA_SUCCESS );
63 }
64
65 /* Copied from psa_crypto.c */
John Durkop9814fa22020-11-04 12:28:15 -080066#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
67 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman56250fd2020-09-04 13:07:15 +020068 if ( PSA_KEY_TYPE_IS_ECC( psa_get_key_type( attributes ) )
69 && PSA_KEY_TYPE_IS_KEY_PAIR( psa_get_key_type( attributes ) ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020070 {
Steven Cooremanc4813a62020-10-23 11:45:43 +020071 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
72 psa_get_key_type( attributes ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020073 mbedtls_ecp_group_id grp_id =
Steven Cooremanc4813a62020-10-23 11:45:43 +020074 mbedtls_ecc_group_of_psa(
75 curve,
76 PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020077 const mbedtls_ecp_curve_info *curve_info =
78 mbedtls_ecp_curve_info_from_grp_id( grp_id );
79 mbedtls_ecp_keypair ecp;
80 mbedtls_test_rnd_pseudo_info rnd_info;
81 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
82
83 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
84 if( attributes->domain_parameters_size != 0 )
85 return( PSA_ERROR_NOT_SUPPORTED );
86 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
87 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman56250fd2020-09-04 13:07:15 +020088 if( curve_info->bit_size != psa_get_key_bits( attributes ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020089 return( PSA_ERROR_INVALID_ARGUMENT );
90 mbedtls_ecp_keypair_init( &ecp );
91 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
92 &mbedtls_test_rnd_pseudo_rand,
93 &rnd_info );
94 if( ret != 0 )
95 {
96 mbedtls_ecp_keypair_free( &ecp );
97 return( mbedtls_to_psa_error( ret ) );
98 }
99
100 /* Make sure to use export representation */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200101 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200102 if( key_size < bytes )
103 {
104 mbedtls_ecp_keypair_free( &ecp );
105 return( PSA_ERROR_BUFFER_TOO_SMALL );
106 }
107 psa_status_t status = mbedtls_to_psa_error(
108 mbedtls_mpi_write_binary( &ecp.d, key, bytes ) );
109
110 if( status == PSA_SUCCESS )
111 {
112 *key_length = bytes;
113 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100114 else
115 {
116 memset( key, 0, bytes );
117 }
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200118
119 mbedtls_ecp_keypair_free( &ecp );
120 return( status );
121 }
122 else
John Durkop9814fa22020-11-04 12:28:15 -0800123#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR || MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200124 return( PSA_ERROR_NOT_SUPPORTED );
125}
126
127psa_status_t test_opaque_generate_key(
128 const psa_key_attributes_t *attributes,
129 uint8_t *key, size_t key_size, size_t *key_length )
130{
131 (void) attributes;
132 (void) key;
133 (void) key_size;
134 (void) key_length;
135 return( PSA_ERROR_NOT_SUPPORTED );
136}
137
Steven Cooreman04524762020-10-13 17:43:44 +0200138psa_status_t test_transparent_validate_key(const psa_key_attributes_t *attributes,
139 const uint8_t *data,
140 size_t data_length,
141 size_t *bits)
142{
Steven Cooremanc4813a62020-10-23 11:45:43 +0200143 ++test_driver_key_management_hooks.hits;
Steven Cooreman04524762020-10-13 17:43:44 +0200144
Steven Cooremanc4813a62020-10-23 11:45:43 +0200145 if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS )
146 return( test_driver_key_management_hooks.forced_status );
Steven Cooreman04524762020-10-13 17:43:44 +0200147
John Durkop9814fa22020-11-04 12:28:15 -0800148#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
149 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman04524762020-10-13 17:43:44 +0200150 psa_key_type_t type = psa_get_key_type( attributes );
151 if ( PSA_KEY_TYPE_IS_ECC( type ) )
152 {
153 // Code mostly copied from psa_load_ecp_representation
154 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
155 mbedtls_ecp_group_id grp_id;
156 mbedtls_ecp_keypair ecp;
157 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
158
Steven Cooreman40120f62020-10-29 11:42:22 +0100159 if( psa_get_key_bits( attributes ) == 0 )
Steven Cooreman04524762020-10-13 17:43:44 +0200160 {
161 // Attempt auto-detect of curve bit size
162 size_t curve_size = data_length;
163
164 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
165 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
166 {
167 /* A Weierstrass public key is represented as:
168 * - The byte 0x04;
169 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
170 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooremanc4813a62020-10-23 11:45:43 +0200171 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman04524762020-10-13 17:43:44 +0200172 */
173 if( ( data_length & 1 ) == 0 )
174 return( PSA_ERROR_INVALID_ARGUMENT );
175 curve_size = data_length / 2;
176
177 /* Montgomery public keys are represented in compressed format, meaning
178 * their curve_size is equal to the amount of input. */
179
180 /* Private keys are represented in uncompressed private random integer
181 * format, meaning their curve_size is equal to the amount of input. */
182 }
183
184 grp_id = mbedtls_ecc_group_of_psa( curve, curve_size );
185 }
186 else
187 {
188 grp_id = mbedtls_ecc_group_of_psa( curve,
189 PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
190 }
191
192 const mbedtls_ecp_curve_info *curve_info =
193 mbedtls_ecp_curve_info_from_grp_id( grp_id );
194
195 if( attributes->domain_parameters_size != 0 )
196 return( PSA_ERROR_NOT_SUPPORTED );
197 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
198 return( PSA_ERROR_NOT_SUPPORTED );
199
200 *bits = curve_info->bit_size;
201
202 mbedtls_ecp_keypair_init( &ecp );
203
204 status = mbedtls_to_psa_error(
205 mbedtls_ecp_group_load( &ecp.grp, grp_id ) );
206 if( status != PSA_SUCCESS )
207 goto ecp_exit;
208
209 /* Load the key material. */
210 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
211 {
212 /* Load the public value. */
213 status = mbedtls_to_psa_error(
214 mbedtls_ecp_point_read_binary( &ecp.grp, &ecp.Q,
215 data,
216 data_length ) );
217 if( status != PSA_SUCCESS )
218 goto ecp_exit;
219
220 /* Check that the point is on the curve. */
221 status = mbedtls_to_psa_error(
222 mbedtls_ecp_check_pubkey( &ecp.grp, &ecp.Q ) );
223 }
224 else
225 {
226 /* Load and validate the secret value. */
227 status = mbedtls_to_psa_error(
228 mbedtls_ecp_read_key( ecp.grp.id,
229 &ecp,
230 data,
231 data_length ) );
232 }
233
234ecp_exit:
235 mbedtls_ecp_keypair_free( &ecp );
236 return( status );
237 }
238 return( PSA_ERROR_NOT_SUPPORTED );
239#else
John Durkop9814fa22020-11-04 12:28:15 -0800240 (void) attributes;
Steven Cooreman04524762020-10-13 17:43:44 +0200241 (void) data;
242 (void) data_length;
243 (void) bits;
244 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -0800245#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR || MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY */
Steven Cooreman04524762020-10-13 17:43:44 +0200246}
247
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200248#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */