blob: ab3210b71cfc3e91808a8ddc9a1b34a8b30d708a [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 Durkop6ba40d12020-11-10 08:50:04 -080046#if !defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) && \
47 !defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
John Durkop0e005192020-10-31 22:06:54 -070048 (void)attributes;
John Durkop6ba40d12020-11-10 08:50:04 -080049#endif /* !MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR &&
50 * !MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY */
Steven Cooremanc4813a62020-10-23 11:45:43 +020051 ++test_driver_key_management_hooks.hits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +020052
Steven Cooremanc4813a62020-10-23 11:45:43 +020053 if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS )
54 return( test_driver_key_management_hooks.forced_status );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020055
Steven Cooremanc4813a62020-10-23 11:45:43 +020056 if( test_driver_key_management_hooks.forced_output != NULL )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020057 {
Steven Cooremanc4813a62020-10-23 11:45:43 +020058 if( test_driver_key_management_hooks.forced_output_length > key_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020059 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremanc4813a62020-10-23 11:45:43 +020060 memcpy( key, test_driver_key_management_hooks.forced_output,
61 test_driver_key_management_hooks.forced_output_length );
62 *key_length = test_driver_key_management_hooks.forced_output_length;
Steven Cooreman2a1664c2020-07-20 15:33:08 +020063 return( PSA_SUCCESS );
64 }
65
66 /* Copied from psa_crypto.c */
John Durkop6ba40d12020-11-10 08:50:04 -080067#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
68 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman56250fd2020-09-04 13:07:15 +020069 if ( PSA_KEY_TYPE_IS_ECC( psa_get_key_type( attributes ) )
70 && PSA_KEY_TYPE_IS_KEY_PAIR( psa_get_key_type( attributes ) ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020071 {
Steven Cooremanc4813a62020-10-23 11:45:43 +020072 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
73 psa_get_key_type( attributes ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020074 mbedtls_ecp_group_id grp_id =
Steven Cooremanc4813a62020-10-23 11:45:43 +020075 mbedtls_ecc_group_of_psa(
76 curve,
77 PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020078 const mbedtls_ecp_curve_info *curve_info =
79 mbedtls_ecp_curve_info_from_grp_id( grp_id );
80 mbedtls_ecp_keypair ecp;
81 mbedtls_test_rnd_pseudo_info rnd_info;
82 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
83
84 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
85 if( attributes->domain_parameters_size != 0 )
86 return( PSA_ERROR_NOT_SUPPORTED );
87 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
88 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman56250fd2020-09-04 13:07:15 +020089 if( curve_info->bit_size != psa_get_key_bits( attributes ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020090 return( PSA_ERROR_INVALID_ARGUMENT );
91 mbedtls_ecp_keypair_init( &ecp );
92 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
93 &mbedtls_test_rnd_pseudo_rand,
94 &rnd_info );
95 if( ret != 0 )
96 {
97 mbedtls_ecp_keypair_free( &ecp );
98 return( mbedtls_to_psa_error( ret ) );
99 }
100
101 /* Make sure to use export representation */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200102 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200103 if( key_size < bytes )
104 {
105 mbedtls_ecp_keypair_free( &ecp );
106 return( PSA_ERROR_BUFFER_TOO_SMALL );
107 }
108 psa_status_t status = mbedtls_to_psa_error(
109 mbedtls_mpi_write_binary( &ecp.d, key, bytes ) );
110
111 if( status == PSA_SUCCESS )
112 {
113 *key_length = bytes;
114 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100115 else
116 {
117 memset( key, 0, bytes );
118 }
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200119
120 mbedtls_ecp_keypair_free( &ecp );
121 return( status );
122 }
123 else
John Durkop6ba40d12020-11-10 08:50:04 -0800124#endif /* MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR ||
125 * MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200126 return( PSA_ERROR_NOT_SUPPORTED );
127}
128
129psa_status_t test_opaque_generate_key(
130 const psa_key_attributes_t *attributes,
131 uint8_t *key, size_t key_size, size_t *key_length )
132{
133 (void) attributes;
134 (void) key;
135 (void) key_size;
136 (void) key_length;
137 return( PSA_ERROR_NOT_SUPPORTED );
138}
139
Ronald Cron83282872020-11-22 14:02:39 +0100140psa_status_t test_transparent_import_key(
Steven Cooremanb9b84422020-10-14 14:39:20 +0200141 const psa_key_attributes_t *attributes,
142 const uint8_t *data,
143 size_t data_length,
Ronald Cron83282872020-11-22 14:02:39 +0100144 uint8_t *key_buffer,
145 size_t key_buffer_size,
146 size_t *key_buffer_length,
147 size_t *bits)
Steven Cooreman04524762020-10-13 17:43:44 +0200148{
Steven Cooremanc4813a62020-10-23 11:45:43 +0200149 ++test_driver_key_management_hooks.hits;
Steven Cooreman04524762020-10-13 17:43:44 +0200150
Steven Cooremanc4813a62020-10-23 11:45:43 +0200151 if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS )
152 return( test_driver_key_management_hooks.forced_status );
Steven Cooreman04524762020-10-13 17:43:44 +0200153
John Durkop6ba40d12020-11-10 08:50:04 -0800154#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
155 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman04524762020-10-13 17:43:44 +0200156 psa_key_type_t type = psa_get_key_type( attributes );
157 if ( PSA_KEY_TYPE_IS_ECC( type ) )
158 {
159 // Code mostly copied from psa_load_ecp_representation
160 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
161 mbedtls_ecp_group_id grp_id;
162 mbedtls_ecp_keypair ecp;
163 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
164
Steven Cooreman40120f62020-10-29 11:42:22 +0100165 if( psa_get_key_bits( attributes ) == 0 )
Steven Cooreman04524762020-10-13 17:43:44 +0200166 {
167 // Attempt auto-detect of curve bit size
168 size_t curve_size = data_length;
169
170 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
171 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
172 {
173 /* A Weierstrass public key is represented as:
174 * - The byte 0x04;
175 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
176 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooremanc4813a62020-10-23 11:45:43 +0200177 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman04524762020-10-13 17:43:44 +0200178 */
179 if( ( data_length & 1 ) == 0 )
180 return( PSA_ERROR_INVALID_ARGUMENT );
181 curve_size = data_length / 2;
182
183 /* Montgomery public keys are represented in compressed format, meaning
184 * their curve_size is equal to the amount of input. */
185
186 /* Private keys are represented in uncompressed private random integer
187 * format, meaning their curve_size is equal to the amount of input. */
188 }
189
190 grp_id = mbedtls_ecc_group_of_psa( curve, curve_size );
191 }
192 else
193 {
194 grp_id = mbedtls_ecc_group_of_psa( curve,
195 PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
196 }
197
198 const mbedtls_ecp_curve_info *curve_info =
199 mbedtls_ecp_curve_info_from_grp_id( grp_id );
200
201 if( attributes->domain_parameters_size != 0 )
202 return( PSA_ERROR_NOT_SUPPORTED );
203 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
204 return( PSA_ERROR_NOT_SUPPORTED );
205
206 *bits = curve_info->bit_size;
207
208 mbedtls_ecp_keypair_init( &ecp );
209
210 status = mbedtls_to_psa_error(
211 mbedtls_ecp_group_load( &ecp.grp, grp_id ) );
212 if( status != PSA_SUCCESS )
213 goto ecp_exit;
214
215 /* Load the key material. */
216 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
217 {
218 /* Load the public value. */
219 status = mbedtls_to_psa_error(
220 mbedtls_ecp_point_read_binary( &ecp.grp, &ecp.Q,
221 data,
222 data_length ) );
223 if( status != PSA_SUCCESS )
224 goto ecp_exit;
225
226 /* Check that the point is on the curve. */
227 status = mbedtls_to_psa_error(
228 mbedtls_ecp_check_pubkey( &ecp.grp, &ecp.Q ) );
229 }
230 else
231 {
232 /* Load and validate the secret value. */
233 status = mbedtls_to_psa_error(
234 mbedtls_ecp_read_key( ecp.grp.id,
235 &ecp,
236 data,
237 data_length ) );
238 }
239
240ecp_exit:
241 mbedtls_ecp_keypair_free( &ecp );
242 return( status );
243 }
244 return( PSA_ERROR_NOT_SUPPORTED );
245#else
John Durkop9814fa22020-11-04 12:28:15 -0800246 (void) attributes;
Steven Cooreman04524762020-10-13 17:43:44 +0200247 (void) data;
248 (void) data_length;
249 (void) bits;
250 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop6ba40d12020-11-10 08:50:04 -0800251#endif /* MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR ||
252 * MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY */
Steven Cooreman04524762020-10-13 17:43:44 +0200253}
254
Steven Cooremanb9b84422020-10-14 14:39:20 +0200255psa_status_t test_transparent_export_public_key(
256 const psa_key_attributes_t *attributes,
257 const uint8_t *key, size_t key_length,
258 uint8_t *data, size_t data_size, size_t *data_length )
259{
Gilles Peskinec2402362020-11-22 18:47:43 +0100260 ++test_driver_key_management_hooks.hits;
Steven Cooremanb9b84422020-10-14 14:39:20 +0200261
Gilles Peskinec2402362020-11-22 18:47:43 +0100262 if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS )
263 return( test_driver_key_management_hooks.forced_status );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200264
Gilles Peskinec2402362020-11-22 18:47:43 +0100265 if( test_driver_key_management_hooks.forced_output != NULL )
Steven Cooremanb9b84422020-10-14 14:39:20 +0200266 {
Gilles Peskinec2402362020-11-22 18:47:43 +0100267 if( test_driver_key_management_hooks.forced_output_length > data_size )
Steven Cooremanb9b84422020-10-14 14:39:20 +0200268 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinec2402362020-11-22 18:47:43 +0100269 memcpy( data, test_driver_key_management_hooks.forced_output,
270 test_driver_key_management_hooks.forced_output_length );
271 *data_length = test_driver_key_management_hooks.forced_output_length;
Steven Cooremanb9b84422020-10-14 14:39:20 +0200272 return( PSA_SUCCESS );
273 }
274
275 if( key == NULL || key_length == 0 )
276 return( PSA_ERROR_INVALID_ARGUMENT );
277
278 psa_key_type_t keytype = psa_get_key_type( attributes );
Gilles Peskinee13fb812020-11-22 19:33:11 +0100279 (void) keytype;
Steven Cooremanb9b84422020-10-14 14:39:20 +0200280
281#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
282 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
283 if( PSA_KEY_TYPE_IS_ECC( keytype ) )
284 {
285 if( !PSA_KEY_TYPE_IS_KEY_PAIR( keytype ) )
286 return( PSA_ERROR_INVALID_ARGUMENT );
287
288 /* Mostly copied from psa_crypto.c */
289 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
290 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
291 mbedtls_ecp_keypair ecp;
292 mbedtls_test_rnd_pseudo_info rnd_info;
293 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
294
295 if( attributes->domain_parameters_size != 0 )
296 return( PSA_ERROR_NOT_SUPPORTED );
297
298 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( keytype ),
299 PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
300 if( grp_id == MBEDTLS_ECP_DP_NONE )
301 return( PSA_ERROR_NOT_SUPPORTED );
302
303 mbedtls_ecp_keypair_init( &ecp );
304
305 status = mbedtls_to_psa_error(
306 mbedtls_ecp_group_load( &ecp.grp, grp_id ) );
307 if( status != PSA_SUCCESS )
308 goto ecp_exit;
309
310 status = mbedtls_to_psa_error(
311 mbedtls_ecp_read_key( ecp.grp.id,
312 &ecp,
313 key,
314 key_length ) );
315 if( status != PSA_SUCCESS )
316 goto ecp_exit;
317
318 /* Calculate the public key */
319 status = mbedtls_to_psa_error(
320 mbedtls_ecp_mul( &ecp.grp, &ecp.Q, &ecp.d, &ecp.grp.G,
321 &mbedtls_test_rnd_pseudo_rand,
322 &rnd_info ) );
323 if( status != PSA_SUCCESS )
324 goto ecp_exit;
325
326 status = mbedtls_to_psa_error(
327 mbedtls_ecp_point_write_binary( &ecp.grp, &ecp.Q,
328 MBEDTLS_ECP_PF_UNCOMPRESSED,
329 data_length,
330 data,
331 data_size ) );
332 if( status != PSA_SUCCESS )
333 memset( data, 0, data_size );
334ecp_exit:
335 mbedtls_ecp_keypair_free( &ecp );
336 return( status );
337 }
338#endif /* MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR ||
339 * MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY */
340
341 return( PSA_ERROR_NOT_SUPPORTED );
342}
343
344psa_status_t test_opaque_export_public_key(
345 const psa_key_attributes_t *attributes,
346 const uint8_t *key, size_t key_length,
347 uint8_t *data, size_t data_size, size_t *data_length )
348{
349 (void) attributes;
350 (void) key;
351 (void) key_length;
352 (void) data;
353 (void) data_size;
354 (void) data_length;
355 return( PSA_ERROR_NOT_SUPPORTED );
356}
357
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200358#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */