blob: 84fc98a371fedae5a06adcd48634251a01508096 [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 Cooreman0d7c64d2020-09-07 16:17:55 +020033#include "test/drivers/keygen.h"
Steven Cooreman2a1664c2020-07-20 15:33:08 +020034
35#include "test/random.h"
36
37#include <string.h>
38
Steven Cooreman831c6952020-09-07 12:58:16 +020039test_driver_keygen_hooks_t test_driver_keygen_hooks = TEST_DRIVER_KEYGEN_INIT;
Steven Cooreman2a1664c2020-07-20 15:33:08 +020040
41psa_status_t test_transparent_generate_key(
42 const psa_key_attributes_t *attributes,
43 uint8_t *key, size_t key_size, size_t *key_length )
44{
Steven Cooreman831c6952020-09-07 12:58:16 +020045 ++test_driver_keygen_hooks.hits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +020046
Steven Cooreman831c6952020-09-07 12:58:16 +020047 if( test_driver_keygen_hooks.forced_status != PSA_SUCCESS )
48 return( test_driver_keygen_hooks.forced_status );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020049
Steven Cooreman831c6952020-09-07 12:58:16 +020050 if( test_driver_keygen_hooks.forced_output != NULL )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020051 {
Steven Cooreman831c6952020-09-07 12:58:16 +020052 if( test_driver_keygen_hooks.forced_output_length > key_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020053 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooreman831c6952020-09-07 12:58:16 +020054 memcpy( key, test_driver_keygen_hooks.forced_output,
55 test_driver_keygen_hooks.forced_output_length );
56 *key_length = test_driver_keygen_hooks.forced_output_length;
Steven Cooreman2a1664c2020-07-20 15:33:08 +020057 return( PSA_SUCCESS );
58 }
59
60 /* Copied from psa_crypto.c */
61#if defined(MBEDTLS_ECP_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020062 if ( PSA_KEY_TYPE_IS_ECC( psa_get_key_type( attributes ) )
63 && PSA_KEY_TYPE_IS_KEY_PAIR( psa_get_key_type( attributes ) ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020064 {
Steven Cooreman56250fd2020-09-04 13:07:15 +020065 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( psa_get_key_type( attributes ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020066 mbedtls_ecp_group_id grp_id =
Steven Cooreman56250fd2020-09-04 13:07:15 +020067 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020068 const mbedtls_ecp_curve_info *curve_info =
69 mbedtls_ecp_curve_info_from_grp_id( grp_id );
70 mbedtls_ecp_keypair ecp;
71 mbedtls_test_rnd_pseudo_info rnd_info;
72 memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) );
73
74 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
75 if( attributes->domain_parameters_size != 0 )
76 return( PSA_ERROR_NOT_SUPPORTED );
77 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
78 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman56250fd2020-09-04 13:07:15 +020079 if( curve_info->bit_size != psa_get_key_bits( attributes ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +020080 return( PSA_ERROR_INVALID_ARGUMENT );
81 mbedtls_ecp_keypair_init( &ecp );
82 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
83 &mbedtls_test_rnd_pseudo_rand,
84 &rnd_info );
85 if( ret != 0 )
86 {
87 mbedtls_ecp_keypair_free( &ecp );
88 return( mbedtls_to_psa_error( ret ) );
89 }
90
91 /* Make sure to use export representation */
Steven Cooreman56250fd2020-09-04 13:07:15 +020092 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +020093 if( key_size < bytes )
94 {
95 mbedtls_ecp_keypair_free( &ecp );
96 return( PSA_ERROR_BUFFER_TOO_SMALL );
97 }
98 psa_status_t status = mbedtls_to_psa_error(
99 mbedtls_mpi_write_binary( &ecp.d, key, bytes ) );
100
101 if( status == PSA_SUCCESS )
102 {
103 *key_length = bytes;
104 }
105
106 mbedtls_ecp_keypair_free( &ecp );
107 return( status );
108 }
109 else
110#endif /* MBEDTLS_ECP_C */
111 return( PSA_ERROR_NOT_SUPPORTED );
112}
113
114psa_status_t test_opaque_generate_key(
115 const psa_key_attributes_t *attributes,
116 uint8_t *key, size_t key_size, size_t *key_length )
117{
118 (void) attributes;
119 (void) key;
120 (void) key_size;
121 (void) key_length;
122 return( PSA_ERROR_NOT_SUPPORTED );
123}
124
Steven Cooreman04524762020-10-13 17:43:44 +0200125psa_status_t test_transparent_validate_key(const psa_key_attributes_t *attributes,
126 const uint8_t *data,
127 size_t data_length,
128 size_t *bits)
129{
130 ++test_driver_keygen_hooks.hits;
131
132 if( test_driver_keygen_hooks.forced_status != PSA_SUCCESS )
133 return( test_driver_keygen_hooks.forced_status );
134
135#if defined(MBEDTLS_ECP_C)
136 psa_key_type_t type = psa_get_key_type( attributes );
137 if ( PSA_KEY_TYPE_IS_ECC( type ) )
138 {
139 // Code mostly copied from psa_load_ecp_representation
140 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
141 mbedtls_ecp_group_id grp_id;
142 mbedtls_ecp_keypair ecp;
143 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
144
145 if( *bits == 0 )
146 {
147 // Attempt auto-detect of curve bit size
148 size_t curve_size = data_length;
149
150 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
151 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
152 {
153 /* A Weierstrass public key is represented as:
154 * - The byte 0x04;
155 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
156 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
157 * So its data length is 2m+1 where n is the key size in bits.
158 */
159 if( ( data_length & 1 ) == 0 )
160 return( PSA_ERROR_INVALID_ARGUMENT );
161 curve_size = data_length / 2;
162
163 /* Montgomery public keys are represented in compressed format, meaning
164 * their curve_size is equal to the amount of input. */
165
166 /* Private keys are represented in uncompressed private random integer
167 * format, meaning their curve_size is equal to the amount of input. */
168 }
169
170 grp_id = mbedtls_ecc_group_of_psa( curve, curve_size );
171 }
172 else
173 {
174 grp_id = mbedtls_ecc_group_of_psa( curve,
175 PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) ) );
176 }
177
178 const mbedtls_ecp_curve_info *curve_info =
179 mbedtls_ecp_curve_info_from_grp_id( grp_id );
180
181 if( attributes->domain_parameters_size != 0 )
182 return( PSA_ERROR_NOT_SUPPORTED );
183 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
184 return( PSA_ERROR_NOT_SUPPORTED );
185
186 *bits = curve_info->bit_size;
187
188 mbedtls_ecp_keypair_init( &ecp );
189
190 status = mbedtls_to_psa_error(
191 mbedtls_ecp_group_load( &ecp.grp, grp_id ) );
192 if( status != PSA_SUCCESS )
193 goto ecp_exit;
194
195 /* Load the key material. */
196 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
197 {
198 /* Load the public value. */
199 status = mbedtls_to_psa_error(
200 mbedtls_ecp_point_read_binary( &ecp.grp, &ecp.Q,
201 data,
202 data_length ) );
203 if( status != PSA_SUCCESS )
204 goto ecp_exit;
205
206 /* Check that the point is on the curve. */
207 status = mbedtls_to_psa_error(
208 mbedtls_ecp_check_pubkey( &ecp.grp, &ecp.Q ) );
209 }
210 else
211 {
212 /* Load and validate the secret value. */
213 status = mbedtls_to_psa_error(
214 mbedtls_ecp_read_key( ecp.grp.id,
215 &ecp,
216 data,
217 data_length ) );
218 }
219
220ecp_exit:
221 mbedtls_ecp_keypair_free( &ecp );
222 return( status );
223 }
224 return( PSA_ERROR_NOT_SUPPORTED );
225#else
226 (void) data;
227 (void) data_length;
228 (void) bits;
229 return( PSA_ERROR_NOT_SUPPORTED );
230#endif /* MBEDTLS_ECP_C */
231}
232
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200233#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */