1 /*
2  * Test driver for generating and verifying keys.
3  * Currently only supports generating and verifying ECC keys.
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 #include "psa/crypto.h"
29 #include "psa_crypto_core.h"
30 #include "mbedtls/ecp.h"
31 #include "mbedtls/error.h"
32 
33 #include "test/drivers/key_management.h"
34 
35 #include "test/random.h"
36 
37 #include <string.h>
38 
39 test_driver_key_management_hooks_t test_driver_key_management_hooks =
40     TEST_DRIVER_KEY_MANAGEMENT_INIT;
41 
test_transparent_generate_key(const psa_key_attributes_t * attributes,uint8_t * key,size_t key_size,size_t * key_length)42 psa_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 {
46 #if !defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) && \
47     !defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
48     (void)attributes;
49 #endif /* !MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR &&
50         * !MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY */
51     ++test_driver_key_management_hooks.hits;
52 
53     if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS )
54         return( test_driver_key_management_hooks.forced_status );
55 
56     if( test_driver_key_management_hooks.forced_output != NULL )
57     {
58         if( test_driver_key_management_hooks.forced_output_length > key_size )
59             return( PSA_ERROR_BUFFER_TOO_SMALL );
60         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;
63         return( PSA_SUCCESS );
64     }
65 
66     /* Copied from psa_crypto.c */
67 #if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
68     defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
69     if ( PSA_KEY_TYPE_IS_ECC( psa_get_key_type( attributes ) )
70          && PSA_KEY_TYPE_IS_KEY_PAIR( psa_get_key_type( attributes ) ) )
71     {
72         psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
73             psa_get_key_type( attributes ) );
74         mbedtls_ecp_group_id grp_id =
75             mbedtls_ecc_group_of_psa(
76                 curve,
77                 PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
78         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 );
89         if( curve_info->bit_size != psa_get_key_bits( attributes ) )
90             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 */
102         size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) );
103         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         }
115         else
116         {
117             memset( key, 0, bytes );
118         }
119 
120         mbedtls_ecp_keypair_free( &ecp );
121         return( status );
122     }
123     else
124 #endif /* MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR ||
125         * MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY */
126     return( PSA_ERROR_NOT_SUPPORTED );
127 }
128 
test_opaque_generate_key(const psa_key_attributes_t * attributes,uint8_t * key,size_t key_size,size_t * key_length)129 psa_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 
test_transparent_validate_key(const psa_key_attributes_t * attributes,const uint8_t * data,size_t data_length,size_t * bits)140 psa_status_t test_transparent_validate_key(
141     const psa_key_attributes_t *attributes,
142     const uint8_t *data,
143     size_t data_length,
144     size_t *bits )
145 {
146     ++test_driver_key_management_hooks.hits;
147 
148     if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS )
149         return( test_driver_key_management_hooks.forced_status );
150 
151 #if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
152     defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
153     psa_key_type_t type = psa_get_key_type( attributes );
154     if ( PSA_KEY_TYPE_IS_ECC( type ) )
155     {
156         // Code mostly copied from psa_load_ecp_representation
157         psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
158         mbedtls_ecp_group_id grp_id;
159         mbedtls_ecp_keypair ecp;
160         psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
161 
162         if( psa_get_key_bits( attributes ) == 0 )
163         {
164             // Attempt auto-detect of curve bit size
165             size_t curve_size = data_length;
166 
167             if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
168                 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
169             {
170                 /* A Weierstrass public key is represented as:
171                  * - The byte 0x04;
172                  * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
173                  * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
174                  * So its data length is 2m+1 where m is the curve size in bits.
175                  */
176                 if( ( data_length & 1 ) == 0 )
177                     return( PSA_ERROR_INVALID_ARGUMENT );
178                 curve_size = data_length / 2;
179 
180                 /* Montgomery public keys are represented in compressed format, meaning
181                  * their curve_size is equal to the amount of input. */
182 
183                 /* Private keys are represented in uncompressed private random integer
184                  * format, meaning their curve_size is equal to the amount of input. */
185             }
186 
187             grp_id = mbedtls_ecc_group_of_psa( curve, curve_size );
188         }
189         else
190         {
191             grp_id = mbedtls_ecc_group_of_psa( curve,
192                 PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
193         }
194 
195         const mbedtls_ecp_curve_info *curve_info =
196             mbedtls_ecp_curve_info_from_grp_id( grp_id );
197 
198         if( attributes->domain_parameters_size != 0 )
199             return( PSA_ERROR_NOT_SUPPORTED );
200         if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
201             return( PSA_ERROR_NOT_SUPPORTED );
202 
203         *bits = curve_info->bit_size;
204 
205         mbedtls_ecp_keypair_init( &ecp );
206 
207         status = mbedtls_to_psa_error(
208                     mbedtls_ecp_group_load( &ecp.grp, grp_id ) );
209         if( status != PSA_SUCCESS )
210             goto ecp_exit;
211 
212         /* Load the key material. */
213         if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
214         {
215             /* Load the public value. */
216             status = mbedtls_to_psa_error(
217                 mbedtls_ecp_point_read_binary( &ecp.grp, &ecp.Q,
218                                                data,
219                                                data_length ) );
220             if( status != PSA_SUCCESS )
221                 goto ecp_exit;
222 
223             /* Check that the point is on the curve. */
224             status = mbedtls_to_psa_error(
225                 mbedtls_ecp_check_pubkey( &ecp.grp, &ecp.Q ) );
226         }
227         else
228         {
229             /* Load and validate the secret value. */
230             status = mbedtls_to_psa_error(
231                 mbedtls_ecp_read_key( ecp.grp.id,
232                                       &ecp,
233                                       data,
234                                       data_length ) );
235         }
236 
237 ecp_exit:
238         mbedtls_ecp_keypair_free( &ecp );
239         return( status );
240     }
241     return( PSA_ERROR_NOT_SUPPORTED );
242 #else
243     (void) attributes;
244     (void) data;
245     (void) data_length;
246     (void) bits;
247     return( PSA_ERROR_NOT_SUPPORTED );
248 #endif /* MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR ||
249         * MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY */
250 }
251 
test_transparent_export_public_key(const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,uint8_t * data,size_t data_size,size_t * data_length)252 psa_status_t test_transparent_export_public_key(
253     const psa_key_attributes_t *attributes,
254     const uint8_t *key, size_t key_length,
255     uint8_t *data, size_t data_size, size_t *data_length )
256 {
257     ++test_driver_key_management_hooks.hits;
258 
259     if( test_driver_key_management_hooks.forced_status != PSA_SUCCESS )
260         return( test_driver_key_management_hooks.forced_status );
261 
262     if( test_driver_key_management_hooks.forced_output != NULL )
263     {
264         if( test_driver_key_management_hooks.forced_output_length > data_size )
265             return( PSA_ERROR_BUFFER_TOO_SMALL );
266         memcpy( data, test_driver_key_management_hooks.forced_output,
267                 test_driver_key_management_hooks.forced_output_length );
268         *data_length = test_driver_key_management_hooks.forced_output_length;
269         return( PSA_SUCCESS );
270     }
271 
272     if( key == NULL || key_length == 0 )
273         return( PSA_ERROR_INVALID_ARGUMENT );
274 
275     psa_key_type_t keytype = psa_get_key_type( attributes );
276     (void) keytype;
277 
278 #if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
279     defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
280     if( PSA_KEY_TYPE_IS_ECC( keytype ) )
281     {
282         if( !PSA_KEY_TYPE_IS_KEY_PAIR( keytype ) )
283             return( PSA_ERROR_INVALID_ARGUMENT );
284 
285         /* Mostly copied from psa_crypto.c */
286         mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
287         psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
288         mbedtls_ecp_keypair ecp;
289         mbedtls_test_rnd_pseudo_info rnd_info;
290         memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
291 
292         if( attributes->domain_parameters_size != 0 )
293             return( PSA_ERROR_NOT_SUPPORTED );
294 
295         grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( keytype ),
296                                            PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
297         if( grp_id == MBEDTLS_ECP_DP_NONE )
298             return( PSA_ERROR_NOT_SUPPORTED );
299 
300         mbedtls_ecp_keypair_init( &ecp );
301 
302         status = mbedtls_to_psa_error(
303                     mbedtls_ecp_group_load( &ecp.grp, grp_id ) );
304         if( status != PSA_SUCCESS )
305             goto ecp_exit;
306 
307         status = mbedtls_to_psa_error(
308             mbedtls_ecp_read_key( ecp.grp.id,
309                                   &ecp,
310                                   key,
311                                   key_length ) );
312         if( status != PSA_SUCCESS )
313             goto ecp_exit;
314 
315         /* Calculate the public key */
316         status = mbedtls_to_psa_error(
317             mbedtls_ecp_mul( &ecp.grp, &ecp.Q, &ecp.d, &ecp.grp.G,
318                              &mbedtls_test_rnd_pseudo_rand,
319                              &rnd_info ) );
320         if( status != PSA_SUCCESS )
321             goto ecp_exit;
322 
323         status = mbedtls_to_psa_error(
324                     mbedtls_ecp_point_write_binary( &ecp.grp, &ecp.Q,
325                                                     MBEDTLS_ECP_PF_UNCOMPRESSED,
326                                                     data_length,
327                                                     data,
328                                                     data_size ) );
329         if( status != PSA_SUCCESS )
330             memset( data, 0, data_size );
331 ecp_exit:
332         mbedtls_ecp_keypair_free( &ecp );
333         return( status );
334     }
335 #endif /* MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR ||
336         * MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY */
337 
338     return( PSA_ERROR_NOT_SUPPORTED );
339 }
340 
test_opaque_export_public_key(const psa_key_attributes_t * attributes,const uint8_t * key,size_t key_length,uint8_t * data,size_t data_size,size_t * data_length)341 psa_status_t test_opaque_export_public_key(
342     const psa_key_attributes_t *attributes,
343     const uint8_t *key, size_t key_length,
344     uint8_t *data, size_t data_size, size_t *data_length )
345 {
346     (void) attributes;
347     (void) key;
348     (void) key_length;
349     (void) data;
350     (void) data_size;
351     (void) data_length;
352     return( PSA_ERROR_NOT_SUPPORTED );
353 }
354 
355 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
356