blob: 3663fb8488572eb1df26c5383e34a1eb70ca3078 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000023#include "pk_wrap.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000024#include "mbedtls/error.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020026/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020037#endif
38
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010039#if defined(MBEDTLS_USE_PSA_CRYPTO)
40#include "mbedtls/asn1write.h"
41#endif
42
Andres Amaya Garciae32df082017-10-25 09:37:04 +010043#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050044#include "mbedtls/platform_util.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010045#endif
46
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010047#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040048#include "psa/crypto.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010049#include "mbedtls/psa_util.h"
Andrzej Kurek8b036a62018-10-31 05:16:46 -040050#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010051#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020055#else
56#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020057#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_free free
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059#endif
60
Andres AG72849872017-01-19 11:24:33 +000061#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010062#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_RSA_C)
65static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 return( type == MBEDTLS_PK_RSA ||
68 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020069}
70
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +020071static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020072{
Hanno Becker6a1e7e52017-08-22 13:55:00 +010073 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
74 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020075}
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020078 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020079 const unsigned char *sig, size_t sig_len )
80{
Janos Follath24eed8d2019-11-22 13:21:35 +000081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +010082 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
83 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020084
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010085#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +000086 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
87 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010088#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +000089
Hanno Becker6a1e7e52017-08-22 13:55:00 +010090 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020092
Hanno Becker6a1e7e52017-08-22 13:55:00 +010093 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 MBEDTLS_RSA_PUBLIC, md_alg,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020095 (unsigned int) hash_len, hash, sig ) ) != 0 )
96 return( ret );
97
Gilles Peskine5114d3e2018-03-30 07:12:15 +020098 /* The buffer contains a valid signature followed by extra data.
99 * We have a special error code for that so that so that callers can
100 * use mbedtls_pk_verify() to check "Does the buffer start with a
101 * valid signature?" and not just "Does the buffer contain a valid
102 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100103 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200105
106 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200107}
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200110 const unsigned char *hash, size_t hash_len,
111 unsigned char *sig, size_t *sig_len,
112 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
113{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100114 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
115
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100116#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000117 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
118 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100119#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000120
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100121 *sig_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200122
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100123 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200124 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200125}
126
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200127static int rsa_decrypt_wrap( void *ctx,
128 const unsigned char *input, size_t ilen,
129 unsigned char *output, size_t *olen, size_t osize,
130 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
131{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100132 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
133
134 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200136
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100137 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200139}
140
141static int rsa_encrypt_wrap( void *ctx,
142 const unsigned char *input, size_t ilen,
143 unsigned char *output, size_t *olen, size_t osize,
144 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
145{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100146 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
147 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200148
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100149 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100151
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100152 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
153 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200154}
155
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100156static int rsa_check_pair_wrap( const void *pub, const void *prv )
157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
159 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100160}
161
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200162static void *rsa_alloc_wrap( void )
163{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200164 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200165
166 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200168
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200169 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200170}
171
172static void rsa_free_wrap( void *ctx )
173{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
175 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200181 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200183
184 items++;
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200187 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200189}
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191const mbedtls_pk_info_t mbedtls_rsa_info = {
192 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200193 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200194 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200195 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200196 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200197 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200198#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200199 NULL,
200 NULL,
201#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200202 rsa_decrypt_wrap,
203 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100204 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200205 rsa_alloc_wrap,
206 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200207#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200208 NULL,
209 NULL,
210#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200211 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200212};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200216/*
217 * Generic EC key
218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 return( type == MBEDTLS_PK_ECKEY ||
222 type == MBEDTLS_PK_ECKEY_DH ||
223 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200224}
225
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200226static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200229}
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200232/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200234 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200235 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200238 const unsigned char *hash, size_t hash_len,
239 unsigned char *sig, size_t *sig_len,
240 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200243 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200244 const unsigned char *sig, size_t sig_len )
245{
Janos Follath24eed8d2019-11-22 13:21:35 +0000246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200252 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200255
256 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200257}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200260 const unsigned char *hash, size_t hash_len,
261 unsigned char *sig, size_t *sig_len,
262 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
263{
Janos Follath24eed8d2019-11-22 13:21:35 +0000264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200270 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
271 f_rng, p_rng );
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200274
275 return( ret );
276}
277
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200278#if defined(MBEDTLS_ECP_RESTARTABLE)
279/* Forward declarations */
280static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
281 const unsigned char *hash, size_t hash_len,
282 const unsigned char *sig, size_t sig_len,
283 void *rs_ctx );
284
285static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
286 const unsigned char *hash, size_t hash_len,
287 unsigned char *sig, size_t *sig_len,
288 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
289 void *rs_ctx );
290
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200291/*
292 * Restart context for ECDSA operations with ECKEY context
293 *
294 * We need to store an actual ECDSA context, as we need to pass the same to
295 * the underlying ecdsa function, so we can't create it on the fly every time.
296 */
297typedef struct
298{
299 mbedtls_ecdsa_restart_ctx ecdsa_rs;
300 mbedtls_ecdsa_context ecdsa_ctx;
301} eckey_restart_ctx;
302
303static void *eckey_rs_alloc( void )
304{
305 eckey_restart_ctx *rs_ctx;
306
307 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
308
309 if( ctx != NULL )
310 {
311 rs_ctx = ctx;
312 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
313 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
314 }
315
316 return( ctx );
317}
318
319static void eckey_rs_free( void *ctx )
320{
321 eckey_restart_ctx *rs_ctx;
322
323 if( ctx == NULL)
324 return;
325
326 rs_ctx = ctx;
327 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
328 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
329
330 mbedtls_free( ctx );
331}
332
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200333static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
334 const unsigned char *hash, size_t hash_len,
335 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200336 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200337{
Janos Follath24eed8d2019-11-22 13:21:35 +0000338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200339 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200340
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200341 /* Should never happen */
342 if( rs == NULL )
343 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200344
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200345 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200346 if( rs->ecdsa_ctx.grp.pbits == 0 )
347 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200348
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200349 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
350 md_alg, hash, hash_len,
351 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200352
353cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200354 return( ret );
355}
356
357static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
358 const unsigned char *hash, size_t hash_len,
359 unsigned char *sig, size_t *sig_len,
360 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200361 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200362{
Janos Follath24eed8d2019-11-22 13:21:35 +0000363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200364 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200365
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200366 /* Should never happen */
367 if( rs == NULL )
368 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200369
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200370 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200371 if( rs->ecdsa_ctx.grp.pbits == 0 )
372 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200373
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200374 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
375 hash, hash_len, sig, sig_len,
376 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200377
378cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200379 return( ret );
380}
381#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200383
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100384static int eckey_check_pair( const void *pub, const void *prv )
385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
387 (const mbedtls_ecp_keypair *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100388}
389
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200390static void *eckey_alloc_wrap( void )
391{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200392 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200393
394 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200396
397 return( ctx );
398}
399
400static void eckey_free_wrap( void *ctx )
401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
403 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200404}
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200409 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200411}
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413const mbedtls_pk_info_t mbedtls_eckey_info = {
414 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200415 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200416 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200417 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200419 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200420 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200421#if defined(MBEDTLS_ECP_RESTARTABLE)
422 eckey_verify_rs_wrap,
423 eckey_sign_rs_wrap,
424#endif
425#else /* MBEDTLS_ECDSA_C */
426 NULL,
427 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200428#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200429 NULL,
430 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100431 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200432 eckey_alloc_wrap,
433 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200434#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200435 eckey_rs_alloc,
436 eckey_rs_free,
437#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200438 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200439};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200440
441/*
Paul Bakker75342a62014-04-08 17:35:40 +0200442 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200445{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 return( type == MBEDTLS_PK_ECKEY ||
447 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200448}
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450const mbedtls_pk_info_t mbedtls_eckeydh_info = {
451 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200452 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200453 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200454 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200455 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200456 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200457#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200458 NULL,
459 NULL,
460#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200461 NULL,
462 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100463 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200464 eckey_alloc_wrap, /* Same underlying key structure */
465 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200466#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200467 NULL,
468 NULL,
469#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200470 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200471};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#if defined(MBEDTLS_ECDSA_C)
475static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200478}
479
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400480#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400481/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500482 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
483 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500484 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500485static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
486 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500487{
Janos Follath24eed8d2019-11-22 13:21:35 +0000488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500489 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500490
Andrzej Kurek9241d182018-11-20 05:04:35 -0500491 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500492 MBEDTLS_ASN1_INTEGER ) ) != 0 )
493 {
494 return( ret );
495 }
496
Andrzej Kurek9241d182018-11-20 05:04:35 -0500497 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500498 {
499 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500500 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500501 }
502
Andrzej Kurek9241d182018-11-20 05:04:35 -0500503 if( unpadded_len > to_len || unpadded_len == 0 )
504 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500505
Andrzej Kurek9241d182018-11-20 05:04:35 -0500506 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500507 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500508 memcpy( to + padding_len, *from, unpadded_len );
509 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500510
Andrzej Kurek9241d182018-11-20 05:04:35 -0500511 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500512}
513
514/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400515 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500516 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500517 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400518 */
519static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500520 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400521{
Janos Follath24eed8d2019-11-22 13:21:35 +0000522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500523 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500524
Andrzej Kurek9241d182018-11-20 05:04:35 -0500525 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500526 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500527 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400528
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500529 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500530 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
531 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500532 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500533 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
534 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500535
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400536 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400537}
538
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100539static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400540 const unsigned char *hash, size_t hash_len,
541 const unsigned char *sig, size_t sig_len )
542{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100543 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200546 psa_key_id_t key_id = 0;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200547 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400548 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400549 int key_len;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500550 /* see ECP_PUB_DER_MAX_BYTES in pkwrite.c */
551 unsigned char buf[30 + 2 * MBEDTLS_ECP_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000552 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400553 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700554 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100555 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100556 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100557 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
558 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700559 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400560
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500561 if( curve == 0 )
562 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
563
Hanno Beckerccf574e2019-01-29 08:26:15 +0000564 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500565 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400566 key.pk_info = &pk_info;
567 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000568 p = buf + sizeof( buf );
569 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400570 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500571 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400572
Gilles Peskined2d45c12019-05-27 14:53:13 +0200573 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100574 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200575 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500576
Gilles Peskined2d45c12019-05-27 14:53:13 +0200577 status = psa_import_key( &attributes,
578 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200579 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200580 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400581 {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200582 ret = mbedtls_psa_err_translate_pk( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400583 goto cleanup;
584 }
585
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500586 /* We don't need the exported key anymore and can
587 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500588 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500589 {
590 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
591 goto cleanup;
592 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500593
Hanno Beckera9851112019-01-25 16:37:10 +0000594 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500595 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500596 signature_part_size ) ) != 0 )
597 {
598 goto cleanup;
599 }
600
Ronald Croncf56a0a2020-08-04 09:51:30 +0200601 if( psa_verify_hash( key_id, psa_sig_md,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100602 hash, hash_len,
603 buf, 2 * signature_part_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400604 != PSA_SUCCESS )
605 {
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400606 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
607 goto cleanup;
608 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500609
610 if( p != sig + sig_len )
611 {
612 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
613 goto cleanup;
614 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400615 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400616
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500617cleanup:
Ronald Croncf56a0a2020-08-04 09:51:30 +0200618 psa_destroy_key( key_id );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400619 return( ret );
620}
621#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200623 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200624 const unsigned char *sig, size_t sig_len )
625{
Janos Follath24eed8d2019-11-22 13:21:35 +0000626 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200627 ((void) md_alg);
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200630 hash, hash_len, sig, sig_len );
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
633 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200634
635 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200636}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400637#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200640 const unsigned char *hash, size_t hash_len,
641 unsigned char *sig, size_t *sig_len,
642 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200645 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200646}
647
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200648#if defined(MBEDTLS_ECP_RESTARTABLE)
649static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
650 const unsigned char *hash, size_t hash_len,
651 const unsigned char *sig, size_t sig_len,
652 void *rs_ctx )
653{
Janos Follath24eed8d2019-11-22 13:21:35 +0000654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200655 ((void) md_alg);
656
657 ret = mbedtls_ecdsa_read_signature_restartable(
658 (mbedtls_ecdsa_context *) ctx,
659 hash, hash_len, sig, sig_len,
660 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
661
662 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
663 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
664
665 return( ret );
666}
667
668static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
669 const unsigned char *hash, size_t hash_len,
670 unsigned char *sig, size_t *sig_len,
671 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
672 void *rs_ctx )
673{
674 return( mbedtls_ecdsa_write_signature_restartable(
675 (mbedtls_ecdsa_context *) ctx,
676 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
677 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
678
679}
680#endif /* MBEDTLS_ECP_RESTARTABLE */
681
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200682static void *ecdsa_alloc_wrap( void )
683{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200684 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200685
686 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200688
689 return( ctx );
690}
691
692static void ecdsa_free_wrap( void *ctx )
693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
695 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200696}
697
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200698#if defined(MBEDTLS_ECP_RESTARTABLE)
699static void *ecdsa_rs_alloc( void )
700{
701 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
702
703 if( ctx != NULL )
704 mbedtls_ecdsa_restart_init( ctx );
705
706 return( ctx );
707}
708
709static void ecdsa_rs_free( void *ctx )
710{
711 mbedtls_ecdsa_restart_free( ctx );
712 mbedtls_free( ctx );
713}
714#endif /* MBEDTLS_ECP_RESTARTABLE */
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716const mbedtls_pk_info_t mbedtls_ecdsa_info = {
717 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200718 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200719 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200720 ecdsa_can_do,
721 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200722 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200723#if defined(MBEDTLS_ECP_RESTARTABLE)
724 ecdsa_verify_rs_wrap,
725 ecdsa_sign_rs_wrap,
726#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200727 NULL,
728 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100729 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200730 ecdsa_alloc_wrap,
731 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200732#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200733 ecdsa_rs_alloc,
734 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200735#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200736 eckey_debug, /* Compatible key structures */
737};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200741/*
742 * Support for alternative RSA-private implementations
743 */
744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200746{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200748}
749
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200750static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200751{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200753
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200754 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200755}
756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200758 const unsigned char *hash, size_t hash_len,
759 unsigned char *sig, size_t *sig_len,
760 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200763
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100764#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000765 if( UINT_MAX < hash_len )
766 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100767#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000768
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200769 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +0100770 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
771 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200774 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200775}
776
777static int rsa_alt_decrypt_wrap( void *ctx,
778 const unsigned char *input, size_t ilen,
779 unsigned char *output, size_t *olen, size_t osize,
780 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
781{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200783
784 ((void) f_rng);
785 ((void) p_rng);
786
787 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200789
790 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +0100791 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200792}
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100795static int rsa_alt_check_pair( const void *pub, const void *prv )
796{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100798 unsigned char hash[32];
799 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +0000800 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100801
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200802 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100804
805 memset( hash, 0x2a, sizeof( hash ) );
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100808 hash, sizeof( hash ),
809 sig, &sig_len, NULL, NULL ) ) != 0 )
810 {
811 return( ret );
812 }
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100815 hash, sizeof( hash ), sig, sig_len ) != 0 )
816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100818 }
819
820 return( 0 );
821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100823
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200824static void *rsa_alt_alloc_wrap( void )
825{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200826 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200827
828 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200830
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200831 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200832}
833
834static void rsa_alt_free_wrap( void *ctx )
835{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500836 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200838}
839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
841 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200842 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200843 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200844 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200845 NULL,
846 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200847#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200848 NULL,
849 NULL,
850#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200851 rsa_alt_decrypt_wrap,
852 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100854 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100855#else
856 NULL,
857#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200858 rsa_alt_alloc_wrap,
859 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200860#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200861 NULL,
862 NULL,
863#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200864 NULL,
865};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200868
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200869#if defined(MBEDTLS_USE_PSA_CRYPTO)
870
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100871static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100872{
Ronald Croncf56a0a2020-08-04 09:51:30 +0200873 void *ctx = mbedtls_calloc( 1, sizeof( psa_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100874
875 /* no _init() function to call, an calloc() already zeroized */
876
877 return( ctx );
878}
879
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100880static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100881{
Ronald Croncf56a0a2020-08-04 09:51:30 +0200882 mbedtls_platform_zeroize( ctx, sizeof( psa_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100883 mbedtls_free( ctx );
884}
885
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100886static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100887{
Ronald Croncf56a0a2020-08-04 09:51:30 +0200888 const psa_key_id_t *key = (const psa_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100889 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200890 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100891
Gilles Peskined2d45c12019-05-27 14:53:13 +0200892 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100893 return( 0 );
894
Gilles Peskined2d45c12019-05-27 14:53:13 +0200895 bits = psa_get_key_bits( &attributes );
896 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100897 return( bits );
898}
899
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100900static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100901{
902 /* For now opaque PSA keys can only wrap ECC keypairs,
903 * as checked by setup_psa().
904 * Also, ECKEY_DH does not really make sense with the current API. */
905 return( type == MBEDTLS_PK_ECKEY ||
906 type == MBEDTLS_PK_ECDSA );
907}
908
John Durkopf35069a2020-08-17 22:05:14 -0700909#if defined(MBEDTLS_ECDSA_C)
910
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100911/*
Manuel Pégourié-Gonnard509aff12018-11-15 12:17:38 +0100912 * Simultaneously convert and move raw MPI from the beginning of a buffer
913 * to an ASN.1 MPI at the end of the buffer.
914 * See also mbedtls_asn1_write_mpi().
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100915 *
916 * p: pointer to the end of the output buffer
917 * start: start of the output buffer, and also of the mpi to write at the end
Manuel Pégourié-Gonnard509aff12018-11-15 12:17:38 +0100918 * n_len: length of the mpi to read from start
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100919 */
920static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
921 size_t n_len )
922{
Janos Follath24eed8d2019-11-22 13:21:35 +0000923 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100924 size_t len = 0;
925
926 if( (size_t)( *p - start ) < n_len )
927 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
928
929 len = n_len;
930 *p -= len;
931 memmove( *p, start, len );
932
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100933 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +0100934 * Neither r nor s should be 0, but as a failsafe measure, still detect
935 * that rather than overflowing the buffer in case of a PSA error. */
936 while( len > 0 && **p == 0x00 )
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100937 {
938 ++(*p);
939 --len;
940 }
941
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +0100942 /* this is only reached if the signature was invalid */
943 if( len == 0 )
TRodziewiczb579ccd2021-04-13 14:28:28 +0200944 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +0100945
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100946 /* if the msb is 1, ASN.1 requires that we prepend a 0.
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100947 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100948 if( **p & 0x80 )
949 {
950 if( *p - start < 1 )
951 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
952
953 *--(*p) = 0x00;
954 len += 1;
955 }
956
957 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
958 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
959 MBEDTLS_ASN1_INTEGER ) );
960
961 return( (int) len );
962}
963
964/* Transcode signature from PSA format to ASN.1 sequence.
965 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
966 * MPIs, and in-place.
967 *
968 * [in/out] sig: the signature pre- and post-transcoding
969 * [in/out] sig_len: signature length pre- and post-transcoding
970 * [int] buf_len: the available size the in/out buffer
971 */
972static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
973 size_t buf_len )
974{
Janos Follath24eed8d2019-11-22 13:21:35 +0000975 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100976 size_t len = 0;
977 const size_t rs_len = *sig_len / 2;
978 unsigned char *p = sig + buf_len;
979
980 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
981 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
982
983 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
984 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
985 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
986
987 memmove( sig, p, len );
988 *sig_len = len;
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +0100989
990 return( 0 );
991}
992
John Durkopd46ede02020-08-24 09:51:00 -0700993#endif /* MBEDTLS_ECDSA_C */
John Durkopf35069a2020-08-17 22:05:14 -0700994
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100995static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
996 const unsigned char *hash, size_t hash_len,
997 unsigned char *sig, size_t *sig_len,
998 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
999{
John Durkopf35069a2020-08-17 22:05:14 -07001000#if !defined(MBEDTLS_ECDSA_C)
1001 ((void) ctx);
1002 ((void) md_alg);
1003 ((void) hash);
1004 ((void) hash_len);
1005 ((void) sig);
1006 ((void) sig_len);
1007 ((void) f_rng);
1008 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001009 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1010#else /* !MBEDTLS_ECDSA_C */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001011 const psa_key_id_t *key = (const psa_key_id_t *) ctx;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001012 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001013 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Gilles Peskined2d45c12019-05-27 14:53:13 +02001014 size_t buf_len;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001015 psa_status_t status;
1016
1017 /* PSA has its own RNG */
1018 (void) f_rng;
1019 (void) p_rng;
1020
1021 /* PSA needs an output buffer of known size, but our API doesn't provide
1022 * that information. Assume that the buffer is large enough for a
1023 * maximal-length signature with that key (otherwise the application is
1024 * buggy anyway). */
Gilles Peskined2d45c12019-05-27 14:53:13 +02001025 status = psa_get_key_attributes( *key, &attributes );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001026 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9a5a77b2018-11-16 10:15:09 +01001027 return( mbedtls_psa_err_translate_pk( status ) );
Gilles Peskined2d45c12019-05-27 14:53:13 +02001028 buf_len = MBEDTLS_ECDSA_MAX_SIG_LEN( psa_get_key_bits( &attributes ) );
1029 psa_reset_key_attributes( &attributes );
Gilles Peskine5bcb24b2019-11-08 17:33:29 +01001030 if( buf_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001031 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001032
1033 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001034 status = psa_sign_hash( *key, alg, hash, hash_len,
1035 sig, buf_len, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001036 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9a5a77b2018-11-16 10:15:09 +01001037 return( mbedtls_psa_err_translate_pk( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001038
1039 /* transcode it to ASN.1 sequence */
1040 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, buf_len ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001041#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001042}
1043
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001044const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1045 MBEDTLS_PK_OPAQUE,
1046 "Opaque",
1047 pk_opaque_get_bitlen,
1048 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001049 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001050 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001051#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1052 NULL, /* restartable verify - not relevant */
1053 NULL, /* restartable sign - not relevant */
1054#endif
1055 NULL, /* decrypt - will be done later */
1056 NULL, /* encrypt - will be done later */
1057 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001058 pk_opaque_alloc_wrap,
1059 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001060#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1061 NULL, /* restart alloc - not relevant */
1062 NULL, /* restart free - not relevant */
1063#endif
1064 NULL, /* debug - could be done later, or even left NULL */
1065};
1066
1067#endif /* MBEDTLS_USE_PSA_CRYPTO */
1068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#endif /* MBEDTLS_PK_C */