blob: 29f53b96e699dfa6996da4554b9b162b3a990921 [file] [log] [blame]
Ronald Cron00b7bfc2020-11-25 15:25:26 +01001/*
2 * PSA ECP layer on top of Mbed TLS crypto
3 */
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#include "common.h"
22
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include <psa/crypto.h>
26#include "psa_crypto_core.h"
27#include "psa_crypto_ecp.h"
Ronald Crone5ca3d82020-11-26 16:36:16 +010028#include "psa_crypto_random_impl.h"
Manuel Pégourié-Gonnardd82a9ed2022-07-18 15:21:37 +020029#include "hash_info.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030
31#include <stdlib.h>
32#include <string.h>
33#include "mbedtls/platform.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010034
Ronald Cron072722c2020-12-09 16:36:19 +010035#include <mbedtls/ecdsa.h>
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036#include <mbedtls/ecp.h>
37#include <mbedtls/error.h>
38
Ronald Cron0266cfe2021-03-13 18:50:11 +010039#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
40 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
41 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
42 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
Ronald Cronb5399a82020-12-10 09:35:33 +010043 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010044psa_status_t mbedtls_psa_ecp_load_representation(
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010045 psa_key_type_t type, size_t curve_bits,
46 const uint8_t *data, size_t data_length,
Ronald Cron00b7bfc2020-11-25 15:25:26 +010047 mbedtls_ecp_keypair **p_ecp )
48{
49 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
50 psa_status_t status;
51 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010052 size_t curve_bytes = data_length;
53 int explicit_bits = ( curve_bits != 0 );
Ronald Cron00b7bfc2020-11-25 15:25:26 +010054
55 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
56 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
57 {
58 /* A Weierstrass public key is represented as:
59 * - The byte 0x04;
60 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
61 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
62 * So its data length is 2m+1 where m is the curve size in bits.
63 */
64 if( ( data_length & 1 ) == 0 )
65 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010066 curve_bytes = data_length / 2;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010067
68 /* Montgomery public keys are represented in compressed format, meaning
Gilles Peskined88ccae2021-02-08 18:39:18 +010069 * their curve_bytes is equal to the amount of input. */
Ronald Cron00b7bfc2020-11-25 15:25:26 +010070
71 /* Private keys are represented in uncompressed private random integer
Gilles Peskined88ccae2021-02-08 18:39:18 +010072 * format, meaning their curve_bytes is equal to the amount of input. */
Ronald Cron00b7bfc2020-11-25 15:25:26 +010073 }
74
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010075 if( explicit_bits )
76 {
77 /* With an explicit bit-size, the data must have the matching length. */
78 if( curve_bytes != PSA_BITS_TO_BYTES( curve_bits ) )
79 return( PSA_ERROR_INVALID_ARGUMENT );
80 }
81 else
82 {
83 /* We need to infer the bit-size from the data. Since the only
84 * information we have is the length in bytes, the value of curve_bits
85 * at this stage is rounded up to the nearest multiple of 8. */
86 curve_bits = PSA_BYTES_TO_BITS( curve_bytes );
87 }
88
Ronald Cron00b7bfc2020-11-25 15:25:26 +010089 /* Allocate and initialize a key representation. */
90 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
91 if( ecp == NULL )
92 return( PSA_ERROR_INSUFFICIENT_MEMORY );
93 mbedtls_ecp_keypair_init( ecp );
94
95 /* Load the group. */
96 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010097 curve_bits, !explicit_bits );
Ronald Cron00b7bfc2020-11-25 15:25:26 +010098 if( grp_id == MBEDTLS_ECP_DP_NONE )
99 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100100 /* We can't distinguish between a nonsensical family/size combination
101 * (which would warrant PSA_ERROR_INVALID_ARGUMENT) and a
102 * well-regarded curve that Mbed TLS just doesn't know about (which
103 * would warrant PSA_ERROR_NOT_SUPPORTED). For uniformity with how
104 * curves that Mbed TLS knows about but for which support is disabled
105 * at build time, return NOT_SUPPORTED. */
106 status = PSA_ERROR_NOT_SUPPORTED;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100107 goto exit;
108 }
109
110 status = mbedtls_to_psa_error(
111 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
112 if( status != PSA_SUCCESS )
113 goto exit;
114
115 /* Load the key material. */
116 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
117 {
118 /* Load the public value. */
119 status = mbedtls_to_psa_error(
120 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
121 data,
122 data_length ) );
123 if( status != PSA_SUCCESS )
124 goto exit;
125
126 /* Check that the point is on the curve. */
127 status = mbedtls_to_psa_error(
128 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
129 if( status != PSA_SUCCESS )
130 goto exit;
131 }
132 else
133 {
134 /* Load and validate the secret value. */
135 status = mbedtls_to_psa_error(
136 mbedtls_ecp_read_key( ecp->grp.id,
137 ecp,
138 data,
139 data_length ) );
140 if( status != PSA_SUCCESS )
141 goto exit;
142 }
143
144 *p_ecp = ecp;
145exit:
146 if( status != PSA_SUCCESS )
147 {
148 mbedtls_ecp_keypair_free( ecp );
149 mbedtls_free( ecp );
150 }
151
152 return( status );
153}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100154#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
155 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
156 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
157 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
Ronald Cronb5399a82020-12-10 09:35:33 +0100158 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100159
Ronald Cron0266cfe2021-03-13 18:50:11 +0100160#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
161 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crond6ec3032020-11-27 18:54:57 +0100162
Ronald Cron0266cfe2021-03-13 18:50:11 +0100163psa_status_t mbedtls_psa_ecp_import_key(
Ronald Crond6ec3032020-11-27 18:54:57 +0100164 const psa_key_attributes_t *attributes,
165 const uint8_t *data, size_t data_length,
166 uint8_t *key_buffer, size_t key_buffer_size,
167 size_t *key_buffer_length, size_t *bits )
168{
169 psa_status_t status;
170 mbedtls_ecp_keypair *ecp = NULL;
171
172 /* Parse input */
173 status = mbedtls_psa_ecp_load_representation( attributes->core.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100174 attributes->core.bits,
Ronald Crond6ec3032020-11-27 18:54:57 +0100175 data,
176 data_length,
177 &ecp );
178 if( status != PSA_SUCCESS )
179 goto exit;
180
181 if( PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) ==
182 PSA_ECC_FAMILY_MONTGOMERY )
183 *bits = ecp->grp.nbits + 1;
184 else
185 *bits = ecp->grp.nbits;
186
187 /* Re-export the data to PSA export format. There is currently no support
188 * for other input formats then the export format, so this is a 1-1
189 * copy operation. */
190 status = mbedtls_psa_ecp_export_key( attributes->core.type,
191 ecp,
192 key_buffer,
193 key_buffer_size,
194 key_buffer_length );
195exit:
196 /* Always free the PK object (will also free contained ECP context) */
197 mbedtls_ecp_keypair_free( ecp );
198 mbedtls_free( ecp );
199
200 return( status );
201}
202
Ronald Crone5ca3d82020-11-26 16:36:16 +0100203psa_status_t mbedtls_psa_ecp_export_key( psa_key_type_t type,
204 mbedtls_ecp_keypair *ecp,
205 uint8_t *data,
206 size_t data_size,
207 size_t *data_length )
208{
209 psa_status_t status;
210
211 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
212 {
213 /* Check whether the public part is loaded */
214 if( mbedtls_ecp_is_zero( &ecp->Q ) )
215 {
216 /* Calculate the public key */
217 status = mbedtls_to_psa_error(
218 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
219 mbedtls_psa_get_random,
220 MBEDTLS_PSA_RANDOM_STATE ) );
221 if( status != PSA_SUCCESS )
222 return( status );
223 }
224
225 status = mbedtls_to_psa_error(
226 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
227 MBEDTLS_ECP_PF_UNCOMPRESSED,
228 data_length,
229 data,
230 data_size ) );
231 if( status != PSA_SUCCESS )
232 memset( data, 0, data_size );
233
234 return( status );
235 }
236 else
237 {
238 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
239 return( PSA_ERROR_BUFFER_TOO_SMALL );
240
241 status = mbedtls_to_psa_error(
242 mbedtls_ecp_write_key( ecp,
243 data,
244 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
245 if( status == PSA_SUCCESS )
246 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
247 else
248 memset( data, 0, data_size );
249
250 return( status );
251 }
252}
253
Ronald Cron0266cfe2021-03-13 18:50:11 +0100254psa_status_t mbedtls_psa_ecp_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100255 const psa_key_attributes_t *attributes,
256 const uint8_t *key_buffer, size_t key_buffer_size,
257 uint8_t *data, size_t data_size, size_t *data_length )
258{
259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
260 mbedtls_ecp_keypair *ecp = NULL;
261
262 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100263 attributes->core.type, attributes->core.bits,
264 key_buffer, key_buffer_size, &ecp );
Ronald Crone5ca3d82020-11-26 16:36:16 +0100265 if( status != PSA_SUCCESS )
266 return( status );
267
268 status = mbedtls_psa_ecp_export_key(
269 PSA_KEY_TYPE_ECC_PUBLIC_KEY(
270 PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) ),
271 ecp, data, data_size, data_length );
272
273 mbedtls_ecp_keypair_free( ecp );
274 mbedtls_free( ecp );
275
276 return( status );
277}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100278#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
279 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100280
Ronald Cron0266cfe2021-03-13 18:50:11 +0100281#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
282psa_status_t mbedtls_psa_ecp_generate_key(
Ronald Cron7023db52020-11-20 18:17:42 +0100283 const psa_key_attributes_t *attributes,
284 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
285{
286 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
288
289 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
290 attributes->core.type );
291 mbedtls_ecp_group_id grp_id =
292 mbedtls_ecc_group_of_psa( curve, attributes->core.bits, 0 );
293
294 const mbedtls_ecp_curve_info *curve_info =
295 mbedtls_ecp_curve_info_from_grp_id( grp_id );
296 mbedtls_ecp_keypair ecp;
297
298 if( attributes->domain_parameters_size != 0 )
299 return( PSA_ERROR_NOT_SUPPORTED );
300
301 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
302 return( PSA_ERROR_NOT_SUPPORTED );
303
304 mbedtls_ecp_keypair_init( &ecp );
305 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
306 mbedtls_psa_get_random,
307 MBEDTLS_PSA_RANDOM_STATE );
308 if( ret != 0 )
309 {
310 mbedtls_ecp_keypair_free( &ecp );
311 return( mbedtls_to_psa_error( ret ) );
312 }
313
314 status = mbedtls_to_psa_error(
315 mbedtls_ecp_write_key( &ecp, key_buffer, key_buffer_size ) );
316
317 mbedtls_ecp_keypair_free( &ecp );
318
319 if( status == PSA_SUCCESS )
320 *key_buffer_length = key_buffer_size;
321
322 return( status );
323}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100324#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron7023db52020-11-20 18:17:42 +0100325
Ronald Cron072722c2020-12-09 16:36:19 +0100326/****************************************************************/
327/* ECDSA sign/verify */
328/****************************************************************/
329
Ronald Cron0266cfe2021-03-13 18:50:11 +0100330#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
331 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
332psa_status_t mbedtls_psa_ecdsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +0100333 const psa_key_attributes_t *attributes,
334 const uint8_t *key_buffer, size_t key_buffer_size,
335 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
336 uint8_t *signature, size_t signature_size, size_t *signature_length )
337{
338 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
339 mbedtls_ecp_keypair *ecp = NULL;
340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
341 size_t curve_bytes;
342 mbedtls_mpi r, s;
343
344 status = mbedtls_psa_ecp_load_representation( attributes->core.type,
345 attributes->core.bits,
346 key_buffer,
347 key_buffer_size,
348 &ecp );
349 if( status != PSA_SUCCESS )
350 return( status );
351
352 curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
353 mbedtls_mpi_init( &r );
354 mbedtls_mpi_init( &s );
355
356 if( signature_size < 2 * curve_bytes )
357 {
358 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
359 goto cleanup;
360 }
361
Ronald Cron9103d492021-03-04 11:26:03 +0100362 if( PSA_ALG_ECDSA_IS_DETERMINISTIC( alg ) )
Ronald Cron072722c2020-12-09 16:36:19 +0100363 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100364#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Ronald Cron072722c2020-12-09 16:36:19 +0100365 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Manuel Pégourié-Gonnardd82a9ed2022-07-18 15:21:37 +0200366 mbedtls_md_type_t md_alg = mbedtls_hash_info_md_from_psa( hash_alg );
Ronald Cron072722c2020-12-09 16:36:19 +0100367 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext(
368 &ecp->grp, &r, &s,
369 &ecp->d, hash,
370 hash_length, md_alg,
371 mbedtls_psa_get_random,
372 MBEDTLS_PSA_RANDOM_STATE ) );
Ronald Cron9103d492021-03-04 11:26:03 +0100373#else
Ronald Cronbb9cbc72021-03-04 17:09:00 +0100374 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Ronald Cron9103d492021-03-04 11:26:03 +0100375 goto cleanup;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100376#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron072722c2020-12-09 16:36:19 +0100377 }
378 else
Ronald Cron072722c2020-12-09 16:36:19 +0100379 {
380 (void) alg;
381 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
382 hash, hash_length,
383 mbedtls_psa_get_random,
384 MBEDTLS_PSA_RANDOM_STATE ) );
385 }
386
387 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
388 signature,
389 curve_bytes ) );
390 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
391 signature + curve_bytes,
392 curve_bytes ) );
393cleanup:
394 mbedtls_mpi_free( &r );
395 mbedtls_mpi_free( &s );
396 if( ret == 0 )
397 *signature_length = 2 * curve_bytes;
398
399 mbedtls_ecp_keypair_free( ecp );
400 mbedtls_free( ecp );
401
402 return( mbedtls_to_psa_error( ret ) );
403}
404
Ronald Cron0266cfe2021-03-13 18:50:11 +0100405psa_status_t mbedtls_psa_ecdsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +0100406 const psa_key_attributes_t *attributes,
407 const uint8_t *key_buffer, size_t key_buffer_size,
408 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
409 const uint8_t *signature, size_t signature_length )
410{
411 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
412 mbedtls_ecp_keypair *ecp = NULL;
413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
414 size_t curve_bytes;
415 mbedtls_mpi r, s;
416
417 (void)alg;
418
419 status = mbedtls_psa_ecp_load_representation( attributes->core.type,
420 attributes->core.bits,
421 key_buffer,
422 key_buffer_size,
423 &ecp );
424 if( status != PSA_SUCCESS )
425 return( status );
426
427 curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
428 mbedtls_mpi_init( &r );
429 mbedtls_mpi_init( &s );
430
431 if( signature_length != 2 * curve_bytes )
432 {
433 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
434 goto cleanup;
435 }
436
437 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
438 signature,
439 curve_bytes ) );
440 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
441 signature + curve_bytes,
442 curve_bytes ) );
443
444 /* Check whether the public part is loaded. If not, load it. */
445 if( mbedtls_ecp_is_zero( &ecp->Q ) )
446 {
447 MBEDTLS_MPI_CHK(
448 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
449 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
450 }
451
452 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
453 &ecp->Q, &r, &s );
454
455cleanup:
456 mbedtls_mpi_free( &r );
457 mbedtls_mpi_free( &s );
458 mbedtls_ecp_keypair_free( ecp );
459 mbedtls_free( ecp );
460
461 return( mbedtls_to_psa_error( ret ) );
462}
463
Ronald Cron0266cfe2021-03-13 18:50:11 +0100464#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Ronald Cronb5399a82020-12-10 09:35:33 +0100465 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
466
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100467#endif /* MBEDTLS_PSA_CRYPTO_C */