blob: 6ca03c6be880ea71eed1c8b538c64401448ddae5 [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{
Steven Cooremanc4813a62020-10-23 11:45:43 +020046 ++test_driver_key_management_hooks.hits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +020047
Steven Cooremanc4813a62020-10-23 11:45:43 +020048 if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS )
49 return( test_driver_key_management_hooks.forced_status );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020050
Steven Cooremanc4813a62020-10-23 11:45:43 +020051 if( test_driver_key_management_hooks.forced_output != NULL )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020052 {
Steven Cooremanc4813a62020-10-23 11:45:43 +020053 if( test_driver_key_management_hooks.forced_output_length > key_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020054 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremanc4813a62020-10-23 11:45:43 +020055 memcpy( key, test_driver_key_management_hooks.forced_output,
56 test_driver_key_management_hooks.forced_output_length );
57 *key_length = test_driver_key_management_hooks.forced_output_length;
Steven Cooreman2a1664c2020-07-20 15:33:08 +020058 return( PSA_SUCCESS );
59 }
60
61 /* Copied from psa_crypto.c */
62#if defined(MBEDTLS_ECP_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020063 if ( PSA_KEY_TYPE_IS_ECC( psa_get_key_type( attributes ) )
64 && PSA_KEY_TYPE_IS_KEY_PAIR( psa_get_key_type( attributes ) ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020065 {
Steven Cooremanc4813a62020-10-23 11:45:43 +020066 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
67 psa_get_key_type( attributes ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020068 mbedtls_ecp_group_id grp_id =
Steven Cooremanc4813a62020-10-23 11:45:43 +020069 mbedtls_ecc_group_of_psa(
70 curve,
71 PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020072 const mbedtls_ecp_curve_info *curve_info =
73 mbedtls_ecp_curve_info_from_grp_id( grp_id );
74 mbedtls_ecp_keypair ecp;
75 mbedtls_test_rnd_pseudo_info rnd_info;
76 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
77
78 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
79 if( attributes->domain_parameters_size != 0 )
80 return( PSA_ERROR_NOT_SUPPORTED );
81 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
82 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman56250fd2020-09-04 13:07:15 +020083 if( curve_info->bit_size != psa_get_key_bits( attributes ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020084 return( PSA_ERROR_INVALID_ARGUMENT );
85 mbedtls_ecp_keypair_init( &ecp );
86 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
87 &mbedtls_test_rnd_pseudo_rand,
88 &rnd_info );
89 if( ret != 0 )
90 {
91 mbedtls_ecp_keypair_free( &ecp );
92 return( mbedtls_to_psa_error( ret ) );
93 }
94
95 /* Make sure to use export representation */
Steven Cooreman56250fd2020-09-04 13:07:15 +020096 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020097 if( key_size < bytes )
98 {
99 mbedtls_ecp_keypair_free( &ecp );
100 return( PSA_ERROR_BUFFER_TOO_SMALL );
101 }
102 psa_status_t status = mbedtls_to_psa_error(
103 mbedtls_mpi_write_binary( &ecp.d, key, bytes ) );
104
105 if( status == PSA_SUCCESS )
106 {
107 *key_length = bytes;
108 }
109
110 mbedtls_ecp_keypair_free( &ecp );
111 return( status );
112 }
113 else
114#endif /* MBEDTLS_ECP_C */
115 return( PSA_ERROR_NOT_SUPPORTED );
116}
117
118psa_status_t test_opaque_generate_key(
119 const psa_key_attributes_t *attributes,
120 uint8_t *key, size_t key_size, size_t *key_length )
121{
122 (void) attributes;
123 (void) key;
124 (void) key_size;
125 (void) key_length;
126 return( PSA_ERROR_NOT_SUPPORTED );
127}
128
Steven Cooreman04524762020-10-13 17:43:44 +0200129psa_status_t test_transparent_validate_key(const psa_key_attributes_t *attributes,
130 const uint8_t *data,
131 size_t data_length,
132 size_t *bits)
133{
Steven Cooremanc4813a62020-10-23 11:45:43 +0200134 ++test_driver_key_management_hooks.hits;
Steven Cooreman04524762020-10-13 17:43:44 +0200135
Steven Cooremanc4813a62020-10-23 11:45:43 +0200136 if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS )
137 return( test_driver_key_management_hooks.forced_status );
Steven Cooreman04524762020-10-13 17:43:44 +0200138
139#if defined(MBEDTLS_ECP_C)
140 psa_key_type_t type = psa_get_key_type( attributes );
141 if ( PSA_KEY_TYPE_IS_ECC( type ) )
142 {
143 // Code mostly copied from psa_load_ecp_representation
144 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
145 mbedtls_ecp_group_id grp_id;
146 mbedtls_ecp_keypair ecp;
147 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
148
149 if( *bits == 0 )
150 {
151 // Attempt auto-detect of curve bit size
152 size_t curve_size = data_length;
153
154 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
155 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
156 {
157 /* A Weierstrass public key is represented as:
158 * - The byte 0x04;
159 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
160 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooremanc4813a62020-10-23 11:45:43 +0200161 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman04524762020-10-13 17:43:44 +0200162 */
163 if( ( data_length & 1 ) == 0 )
164 return( PSA_ERROR_INVALID_ARGUMENT );
165 curve_size = data_length / 2;
166
167 /* Montgomery public keys are represented in compressed format, meaning
168 * their curve_size is equal to the amount of input. */
169
170 /* Private keys are represented in uncompressed private random integer
171 * format, meaning their curve_size is equal to the amount of input. */
172 }
173
174 grp_id = mbedtls_ecc_group_of_psa( curve, curve_size );
175 }
176 else
177 {
178 grp_id = mbedtls_ecc_group_of_psa( curve,
179 PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
180 }
181
182 const mbedtls_ecp_curve_info *curve_info =
183 mbedtls_ecp_curve_info_from_grp_id( grp_id );
184
185 if( attributes->domain_parameters_size != 0 )
186 return( PSA_ERROR_NOT_SUPPORTED );
187 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
188 return( PSA_ERROR_NOT_SUPPORTED );
189
190 *bits = curve_info->bit_size;
191
192 mbedtls_ecp_keypair_init( &ecp );
193
194 status = mbedtls_to_psa_error(
195 mbedtls_ecp_group_load( &ecp.grp, grp_id ) );
196 if( status != PSA_SUCCESS )
197 goto ecp_exit;
198
199 /* Load the key material. */
200 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
201 {
202 /* Load the public value. */
203 status = mbedtls_to_psa_error(
204 mbedtls_ecp_point_read_binary( &ecp.grp, &ecp.Q,
205 data,
206 data_length ) );
207 if( status != PSA_SUCCESS )
208 goto ecp_exit;
209
210 /* Check that the point is on the curve. */
211 status = mbedtls_to_psa_error(
212 mbedtls_ecp_check_pubkey( &ecp.grp, &ecp.Q ) );
213 }
214 else
215 {
216 /* Load and validate the secret value. */
217 status = mbedtls_to_psa_error(
218 mbedtls_ecp_read_key( ecp.grp.id,
219 &ecp,
220 data,
221 data_length ) );
222 }
223
224ecp_exit:
225 mbedtls_ecp_keypair_free( &ecp );
226 return( status );
227 }
228 return( PSA_ERROR_NOT_SUPPORTED );
229#else
230 (void) data;
231 (void) data_length;
232 (void) bits;
233 return( PSA_ERROR_NOT_SUPPORTED );
234#endif /* MBEDTLS_ECP_C */
235}
236
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200237#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */