blob: 3fdcdb267899d4a3d8ae214107f0c772c375f23f [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"
Steven Cooreman5f88e772021-03-15 11:07:12 +010029#include "psa_crypto_hash.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030
31#include <stdlib.h>
32#include <string.h>
33#include "mbedtls/platform.h"
34#if !defined(MBEDTLS_PLATFORM_C)
35#define mbedtls_calloc calloc
36#define mbedtls_free free
37#endif
38
Ronald Cron072722c2020-12-09 16:36:19 +010039#include <mbedtls/ecdsa.h>
Ronald Cron00b7bfc2020-11-25 15:25:26 +010040#include <mbedtls/ecp.h>
41#include <mbedtls/error.h>
42
Ronald Cronf1057d32020-11-26 19:19:10 +010043#if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
44 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
Ronald Cron2091eed2021-04-09 11:09:54 +020045 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Cronf1057d32020-11-26 19:19:10 +010046 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ) )
47#define BUILTIN_KEY_TYPE_ECC_KEY_PAIR 1
48#endif
49
Ronald Cron2091eed2021-04-09 11:09:54 +020050#if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
51 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
52 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Cronf1057d32020-11-26 19:19:10 +010053 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) ) )
54#define BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY 1
55#endif
56
Ronald Cronb5399a82020-12-10 09:35:33 +010057#if ( defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
58 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
Ronald Cron2091eed2021-04-09 11:09:54 +020059 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Cronb5399a82020-12-10 09:35:33 +010060 defined(MBEDTLS_PSA_ACCEL_ALG_ECDSA) && \
61 defined(MBEDTLS_ECDSA_C) ) )
62#define BUILTIN_ALG_ECDSA 1
63#endif
64
65#if ( defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
66 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
Ronald Cron2091eed2021-04-09 11:09:54 +020067 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Cronb5399a82020-12-10 09:35:33 +010068 defined(MBEDTLS_PSA_ACCEL_ALG_DETERMINISTIC_ECDSA) && \
69 defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECDSA_DETERMINISTIC) ) )
70#define BUILTIN_ALG_DETERMINISTIC_ECDSA 1
71#endif
72
Ronald Cronf1057d32020-11-26 19:19:10 +010073#if defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
74 defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cronb5399a82020-12-10 09:35:33 +010075 defined(BUILTIN_ALG_ECDSA) || \
76 defined(BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010078psa_status_t mbedtls_psa_ecp_load_representation(
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010079 psa_key_type_t type, size_t curve_bits,
80 const uint8_t *data, size_t data_length,
Ronald Cron00b7bfc2020-11-25 15:25:26 +010081 mbedtls_ecp_keypair **p_ecp )
82{
83 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
84 psa_status_t status;
85 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010086 size_t curve_bytes = data_length;
87 int explicit_bits = ( curve_bits != 0 );
Ronald Cron00b7bfc2020-11-25 15:25:26 +010088
89 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
90 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
91 {
92 /* A Weierstrass public key is represented as:
93 * - The byte 0x04;
94 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
95 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
96 * So its data length is 2m+1 where m is the curve size in bits.
97 */
98 if( ( data_length & 1 ) == 0 )
99 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100100 curve_bytes = data_length / 2;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100101
102 /* Montgomery public keys are represented in compressed format, meaning
Gilles Peskined88ccae2021-02-08 18:39:18 +0100103 * their curve_bytes is equal to the amount of input. */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100104
105 /* Private keys are represented in uncompressed private random integer
Gilles Peskined88ccae2021-02-08 18:39:18 +0100106 * format, meaning their curve_bytes is equal to the amount of input. */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100107 }
108
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100109 if( explicit_bits )
110 {
111 /* With an explicit bit-size, the data must have the matching length. */
112 if( curve_bytes != PSA_BITS_TO_BYTES( curve_bits ) )
113 return( PSA_ERROR_INVALID_ARGUMENT );
114 }
115 else
116 {
117 /* We need to infer the bit-size from the data. Since the only
118 * information we have is the length in bytes, the value of curve_bits
119 * at this stage is rounded up to the nearest multiple of 8. */
120 curve_bits = PSA_BYTES_TO_BITS( curve_bytes );
121 }
122
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100123 /* Allocate and initialize a key representation. */
124 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
125 if( ecp == NULL )
126 return( PSA_ERROR_INSUFFICIENT_MEMORY );
127 mbedtls_ecp_keypair_init( ecp );
128
129 /* Load the group. */
130 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100131 curve_bits, !explicit_bits );
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100132 if( grp_id == MBEDTLS_ECP_DP_NONE )
133 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100134 /* We can't distinguish between a nonsensical family/size combination
135 * (which would warrant PSA_ERROR_INVALID_ARGUMENT) and a
136 * well-regarded curve that Mbed TLS just doesn't know about (which
137 * would warrant PSA_ERROR_NOT_SUPPORTED). For uniformity with how
138 * curves that Mbed TLS knows about but for which support is disabled
139 * at build time, return NOT_SUPPORTED. */
140 status = PSA_ERROR_NOT_SUPPORTED;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100141 goto exit;
142 }
143
144 status = mbedtls_to_psa_error(
145 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
146 if( status != PSA_SUCCESS )
147 goto exit;
148
149 /* Load the key material. */
150 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
151 {
152 /* Load the public value. */
153 status = mbedtls_to_psa_error(
154 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
155 data,
156 data_length ) );
157 if( status != PSA_SUCCESS )
158 goto exit;
159
160 /* Check that the point is on the curve. */
161 status = mbedtls_to_psa_error(
162 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
163 if( status != PSA_SUCCESS )
164 goto exit;
165 }
166 else
167 {
168 /* Load and validate the secret value. */
169 status = mbedtls_to_psa_error(
170 mbedtls_ecp_read_key( ecp->grp.id,
171 ecp,
172 data,
173 data_length ) );
174 if( status != PSA_SUCCESS )
175 goto exit;
176 }
177
178 *p_ecp = ecp;
179exit:
180 if( status != PSA_SUCCESS )
181 {
182 mbedtls_ecp_keypair_free( ecp );
183 mbedtls_free( ecp );
184 }
185
186 return( status );
187}
Ronald Cronf1057d32020-11-26 19:19:10 +0100188#endif /* defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
189 * defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
Ronald Cronb5399a82020-12-10 09:35:33 +0100190 * defined(BUILTIN_ALG_ECDSA) ||
191 * defined(BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
192 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100193
Ronald Cronf1057d32020-11-26 19:19:10 +0100194#if defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
195 defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crond6ec3032020-11-27 18:54:57 +0100196
Ronald Cron784fb322020-11-30 13:55:05 +0100197static psa_status_t ecp_import_key(
Ronald Crond6ec3032020-11-27 18:54:57 +0100198 const psa_key_attributes_t *attributes,
199 const uint8_t *data, size_t data_length,
200 uint8_t *key_buffer, size_t key_buffer_size,
201 size_t *key_buffer_length, size_t *bits )
202{
203 psa_status_t status;
204 mbedtls_ecp_keypair *ecp = NULL;
205
206 /* Parse input */
207 status = mbedtls_psa_ecp_load_representation( attributes->core.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100208 attributes->core.bits,
Ronald Crond6ec3032020-11-27 18:54:57 +0100209 data,
210 data_length,
211 &ecp );
212 if( status != PSA_SUCCESS )
213 goto exit;
214
215 if( PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) ==
216 PSA_ECC_FAMILY_MONTGOMERY )
217 *bits = ecp->grp.nbits + 1;
218 else
219 *bits = ecp->grp.nbits;
220
221 /* Re-export the data to PSA export format. There is currently no support
222 * for other input formats then the export format, so this is a 1-1
223 * copy operation. */
224 status = mbedtls_psa_ecp_export_key( attributes->core.type,
225 ecp,
226 key_buffer,
227 key_buffer_size,
228 key_buffer_length );
229exit:
230 /* Always free the PK object (will also free contained ECP context) */
231 mbedtls_ecp_keypair_free( ecp );
232 mbedtls_free( ecp );
233
234 return( status );
235}
236
Ronald Crone5ca3d82020-11-26 16:36:16 +0100237psa_status_t mbedtls_psa_ecp_export_key( psa_key_type_t type,
238 mbedtls_ecp_keypair *ecp,
239 uint8_t *data,
240 size_t data_size,
241 size_t *data_length )
242{
243 psa_status_t status;
244
245 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
246 {
247 /* Check whether the public part is loaded */
248 if( mbedtls_ecp_is_zero( &ecp->Q ) )
249 {
250 /* Calculate the public key */
251 status = mbedtls_to_psa_error(
252 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
253 mbedtls_psa_get_random,
254 MBEDTLS_PSA_RANDOM_STATE ) );
255 if( status != PSA_SUCCESS )
256 return( status );
257 }
258
259 status = mbedtls_to_psa_error(
260 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
261 MBEDTLS_ECP_PF_UNCOMPRESSED,
262 data_length,
263 data,
264 data_size ) );
265 if( status != PSA_SUCCESS )
266 memset( data, 0, data_size );
267
268 return( status );
269 }
270 else
271 {
272 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
273 return( PSA_ERROR_BUFFER_TOO_SMALL );
274
275 status = mbedtls_to_psa_error(
276 mbedtls_ecp_write_key( ecp,
277 data,
278 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
279 if( status == PSA_SUCCESS )
280 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
281 else
282 memset( data, 0, data_size );
283
284 return( status );
285 }
286}
287
Ronald Cronf1057d32020-11-26 19:19:10 +0100288static psa_status_t ecp_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100289 const psa_key_attributes_t *attributes,
290 const uint8_t *key_buffer, size_t key_buffer_size,
291 uint8_t *data, size_t data_size, size_t *data_length )
292{
293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
294 mbedtls_ecp_keypair *ecp = NULL;
295
296 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100297 attributes->core.type, attributes->core.bits,
298 key_buffer, key_buffer_size, &ecp );
Ronald Crone5ca3d82020-11-26 16:36:16 +0100299 if( status != PSA_SUCCESS )
300 return( status );
301
302 status = mbedtls_psa_ecp_export_key(
303 PSA_KEY_TYPE_ECC_PUBLIC_KEY(
304 PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) ),
305 ecp, data, data_size, data_length );
306
307 mbedtls_ecp_keypair_free( ecp );
308 mbedtls_free( ecp );
309
310 return( status );
311}
Ronald Cronf1057d32020-11-26 19:19:10 +0100312#endif /* defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
313 * defined(BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
314
Ronald Cron7023db52020-11-20 18:17:42 +0100315#if defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100316static psa_status_t ecp_generate_key(
Ronald Cron7023db52020-11-20 18:17:42 +0100317 const psa_key_attributes_t *attributes,
318 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
319{
320 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
322
323 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
324 attributes->core.type );
325 mbedtls_ecp_group_id grp_id =
326 mbedtls_ecc_group_of_psa( curve, attributes->core.bits, 0 );
327
328 const mbedtls_ecp_curve_info *curve_info =
329 mbedtls_ecp_curve_info_from_grp_id( grp_id );
330 mbedtls_ecp_keypair ecp;
331
332 if( attributes->domain_parameters_size != 0 )
333 return( PSA_ERROR_NOT_SUPPORTED );
334
335 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
336 return( PSA_ERROR_NOT_SUPPORTED );
337
338 mbedtls_ecp_keypair_init( &ecp );
339 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
340 mbedtls_psa_get_random,
341 MBEDTLS_PSA_RANDOM_STATE );
342 if( ret != 0 )
343 {
344 mbedtls_ecp_keypair_free( &ecp );
345 return( mbedtls_to_psa_error( ret ) );
346 }
347
348 status = mbedtls_to_psa_error(
349 mbedtls_ecp_write_key( &ecp, key_buffer, key_buffer_size ) );
350
351 mbedtls_ecp_keypair_free( &ecp );
352
353 if( status == PSA_SUCCESS )
354 *key_buffer_length = key_buffer_size;
355
356 return( status );
357}
358#endif /* defined(BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
359
Ronald Cron072722c2020-12-09 16:36:19 +0100360/****************************************************************/
361/* ECDSA sign/verify */
362/****************************************************************/
363
Ronald Cronb5399a82020-12-10 09:35:33 +0100364#if defined(BUILTIN_ALG_ECDSA) || \
365 defined(BUILTIN_ALG_DETERMINISTIC_ECDSA)
366static psa_status_t ecdsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +0100367 const psa_key_attributes_t *attributes,
368 const uint8_t *key_buffer, size_t key_buffer_size,
369 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
370 uint8_t *signature, size_t signature_size, size_t *signature_length )
371{
372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
373 mbedtls_ecp_keypair *ecp = NULL;
374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
375 size_t curve_bytes;
376 mbedtls_mpi r, s;
377
378 status = mbedtls_psa_ecp_load_representation( attributes->core.type,
379 attributes->core.bits,
380 key_buffer,
381 key_buffer_size,
382 &ecp );
383 if( status != PSA_SUCCESS )
384 return( status );
385
386 curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
387 mbedtls_mpi_init( &r );
388 mbedtls_mpi_init( &s );
389
390 if( signature_size < 2 * curve_bytes )
391 {
392 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
393 goto cleanup;
394 }
395
Ronald Cron9103d492021-03-04 11:26:03 +0100396 if( PSA_ALG_ECDSA_IS_DETERMINISTIC( alg ) )
Ronald Cron072722c2020-12-09 16:36:19 +0100397 {
Ronald Cron9103d492021-03-04 11:26:03 +0100398#if defined(BUILTIN_ALG_DETERMINISTIC_ECDSA)
Ronald Cron072722c2020-12-09 16:36:19 +0100399 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
400 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
401 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
402 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext(
403 &ecp->grp, &r, &s,
404 &ecp->d, hash,
405 hash_length, md_alg,
406 mbedtls_psa_get_random,
407 MBEDTLS_PSA_RANDOM_STATE ) );
Ronald Cron9103d492021-03-04 11:26:03 +0100408#else
Ronald Cronbb9cbc72021-03-04 17:09:00 +0100409 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Ronald Cron9103d492021-03-04 11:26:03 +0100410 goto cleanup;
411#endif /* defined(BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron072722c2020-12-09 16:36:19 +0100412 }
413 else
Ronald Cron072722c2020-12-09 16:36:19 +0100414 {
415 (void) alg;
416 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
417 hash, hash_length,
418 mbedtls_psa_get_random,
419 MBEDTLS_PSA_RANDOM_STATE ) );
420 }
421
422 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
423 signature,
424 curve_bytes ) );
425 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
426 signature + curve_bytes,
427 curve_bytes ) );
428cleanup:
429 mbedtls_mpi_free( &r );
430 mbedtls_mpi_free( &s );
431 if( ret == 0 )
432 *signature_length = 2 * curve_bytes;
433
434 mbedtls_ecp_keypair_free( ecp );
435 mbedtls_free( ecp );
436
437 return( mbedtls_to_psa_error( ret ) );
438}
439
Ronald Cronb5399a82020-12-10 09:35:33 +0100440static psa_status_t ecdsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +0100441 const psa_key_attributes_t *attributes,
442 const uint8_t *key_buffer, size_t key_buffer_size,
443 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
444 const uint8_t *signature, size_t signature_length )
445{
446 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
447 mbedtls_ecp_keypair *ecp = NULL;
448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
449 size_t curve_bytes;
450 mbedtls_mpi r, s;
451
452 (void)alg;
453
454 status = mbedtls_psa_ecp_load_representation( attributes->core.type,
455 attributes->core.bits,
456 key_buffer,
457 key_buffer_size,
458 &ecp );
459 if( status != PSA_SUCCESS )
460 return( status );
461
462 curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
463 mbedtls_mpi_init( &r );
464 mbedtls_mpi_init( &s );
465
466 if( signature_length != 2 * curve_bytes )
467 {
468 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
469 goto cleanup;
470 }
471
472 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
473 signature,
474 curve_bytes ) );
475 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
476 signature + curve_bytes,
477 curve_bytes ) );
478
479 /* Check whether the public part is loaded. If not, load it. */
480 if( mbedtls_ecp_is_zero( &ecp->Q ) )
481 {
482 MBEDTLS_MPI_CHK(
483 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
484 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
485 }
486
487 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
488 &ecp->Q, &r, &s );
489
490cleanup:
491 mbedtls_mpi_free( &r );
492 mbedtls_mpi_free( &s );
493 mbedtls_ecp_keypair_free( ecp );
494 mbedtls_free( ecp );
495
496 return( mbedtls_to_psa_error( ret ) );
497}
498
Ronald Cronb5399a82020-12-10 09:35:33 +0100499#endif /* defined(BUILTIN_ALG_ECDSA) || \
500 * defined(BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron072722c2020-12-09 16:36:19 +0100501
Ronald Cronf1057d32020-11-26 19:19:10 +0100502#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
503 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
504
Ronald Cron784fb322020-11-30 13:55:05 +0100505psa_status_t mbedtls_psa_ecp_import_key(
506 const psa_key_attributes_t *attributes,
507 const uint8_t *data, size_t data_length,
508 uint8_t *key_buffer, size_t key_buffer_size,
509 size_t *key_buffer_length, size_t *bits )
510{
511 return( ecp_import_key( attributes, data, data_length,
512 key_buffer, key_buffer_size,
513 key_buffer_length, bits ) );
514}
515
Ronald Cronf1057d32020-11-26 19:19:10 +0100516psa_status_t mbedtls_psa_ecp_export_public_key(
517 const psa_key_attributes_t *attributes,
518 const uint8_t *key_buffer, size_t key_buffer_size,
519 uint8_t *data, size_t data_size, size_t *data_length )
520{
521 return( ecp_export_public_key( attributes, key_buffer, key_buffer_size,
522 data, data_size, data_length ) );
523}
524
Ronald Crone5ca3d82020-11-26 16:36:16 +0100525#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
526 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
527
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100528#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
529psa_status_t mbedtls_psa_ecp_generate_key(
530 const psa_key_attributes_t *attributes,
531 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
532{
533 return( ecp_generate_key( attributes, key_buffer, key_buffer_size,
534 key_buffer_length ) );
535}
536#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
537
Ronald Cronb5399a82020-12-10 09:35:33 +0100538
539#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
540 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
541
542psa_status_t mbedtls_psa_ecdsa_sign_hash(
543 const psa_key_attributes_t *attributes,
544 const uint8_t *key_buffer, size_t key_buffer_size,
545 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
546 uint8_t *signature, size_t signature_size, size_t *signature_length )
547{
548
549 return( ecdsa_sign_hash( attributes,
550 key_buffer, key_buffer_size,
551 alg, hash, hash_length,
552 signature, signature_size, signature_length ) );
553}
554
555psa_status_t mbedtls_psa_ecdsa_verify_hash(
556 const psa_key_attributes_t *attributes,
557 const uint8_t *key_buffer, size_t key_buffer_size,
558 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
559 const uint8_t *signature, size_t signature_length )
560{
561 return( ecdsa_verify_hash( attributes,
562 key_buffer, key_buffer_size,
563 alg, hash, hash_length,
564 signature, signature_length ) );
565}
566
567#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
568 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
569
Ronald Cronf1057d32020-11-26 19:19:10 +0100570/*
571 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
572 */
573
Ronald Cron2091eed2021-04-09 11:09:54 +0200574#if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cronf1057d32020-11-26 19:19:10 +0100575
576#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
577 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron784fb322020-11-30 13:55:05 +0100578
Ronald Cron7b7854e2021-03-13 18:19:08 +0100579psa_status_t libtestdriver1_mbedtls_psa_ecp_import_key(
Ronald Cron784fb322020-11-30 13:55:05 +0100580 const psa_key_attributes_t *attributes,
581 const uint8_t *data, size_t data_length,
582 uint8_t *key_buffer, size_t key_buffer_size,
583 size_t *key_buffer_length, size_t *bits )
584{
585 return( ecp_import_key( attributes, data, data_length,
586 key_buffer, key_buffer_size,
587 key_buffer_length, bits ) );
588}
589
Ronald Cron7b7854e2021-03-13 18:19:08 +0100590psa_status_t libtestdriver1_mbedtls_psa_ecp_export_public_key(
Ronald Cronf1057d32020-11-26 19:19:10 +0100591 const psa_key_attributes_t *attributes,
592 const uint8_t *key_buffer, size_t key_buffer_size,
593 uint8_t *data, size_t data_size, size_t *data_length )
594{
595 return( ecp_export_public_key( attributes, key_buffer, key_buffer_size,
596 data, data_size, data_length ) );
597}
Ronald Cron784fb322020-11-30 13:55:05 +0100598
Ronald Cronf1057d32020-11-26 19:19:10 +0100599#endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
600 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
601
Ronald Cron95391262021-02-08 09:54:03 +0100602#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) && \
603 defined(MBEDTLS_GENPRIME)
Ronald Cron7b7854e2021-03-13 18:19:08 +0100604psa_status_t libtestdriver1_mbedtls_psa_ecp_generate_key(
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100605 const psa_key_attributes_t *attributes,
606 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
607{
608 return( ecp_generate_key( attributes, key_buffer, key_buffer_size,
609 key_buffer_length ) );
610}
Ronald Cron95391262021-02-08 09:54:03 +0100611#endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) &&
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100612 defined(MBEDTLS_GENPRIME) */
613
Ronald Cronb5399a82020-12-10 09:35:33 +0100614#if defined(MBEDTLS_PSA_ACCEL_ALG_ECDSA) || \
615 defined(MBEDTLS_PSA_ACCEL_ALG_DETERMINISTIC_ECDSA)
616
Ronald Cron7b7854e2021-03-13 18:19:08 +0100617psa_status_t libtestdriver1_mbedtls_psa_ecdsa_sign_hash(
Ronald Cronb5399a82020-12-10 09:35:33 +0100618 const psa_key_attributes_t *attributes,
619 const uint8_t *key_buffer, size_t key_buffer_size,
620 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
621 uint8_t *signature, size_t signature_size, size_t *signature_length )
622{
623
624#if defined(MBEDTLS_ECDSA_C)
625 return( ecdsa_sign_hash( attributes,
626 key_buffer, key_buffer_size,
627 alg, hash, hash_length,
628 signature, signature_size, signature_length ) );
629#else
630 (void)attributes;
631 (void)key_buffer;
632 (void)key_buffer_size;
633 (void)alg;
634 (void)hash;
635 (void)hash_length;
636 (void)signature;
637 (void)signature_size;
638 (void)signature_length;
639 return( PSA_ERROR_NOT_SUPPORTED );
640#endif
641}
642
Ronald Cron7b7854e2021-03-13 18:19:08 +0100643psa_status_t libtestdriver1_mbedtls_psa_ecdsa_verify_hash(
Ronald Cronb5399a82020-12-10 09:35:33 +0100644 const psa_key_attributes_t *attributes,
645 const uint8_t *key_buffer, size_t key_buffer_size,
646 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
647 const uint8_t *signature, size_t signature_length )
648{
649#if defined(MBEDTLS_ECDSA_C)
650 return( ecdsa_verify_hash( attributes,
651 key_buffer, key_buffer_size,
652 alg, hash, hash_length,
653 signature, signature_length ) );
654#else
655 (void)attributes;
656 (void)key_buffer;
657 (void)key_buffer_size;
658 (void)alg;
659 (void)hash;
660 (void)hash_length;
661 (void)signature;
662 (void)signature_length;
663 return( PSA_ERROR_NOT_SUPPORTED );
664#endif
665}
666
667#endif /* defined(MBEDTLS_PSA_ACCEL_ALG_ECDSA) ||
668 * defined(MBEDTLS_PSA_ACCEL_ALG_DETERMINISTIC_ECDSA) */
669
Ronald Cron2091eed2021-04-09 11:09:54 +0200670#endif /* PSA_CRYPTO_DRIVER_TEST && MBEDTLS_PSA_CRYPTO_CONFIG */
Ronald Cronf1057d32020-11-26 19:19:10 +0100671
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100672#endif /* MBEDTLS_PSA_CRYPTO_C */