blob: 7c317c52d352a2ad5d098e4db3688cb23a5e3f92 [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
Thomas Daubney68d9cbc2021-05-18 18:45:09 +010093 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
94 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +010095 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020096 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
Thomas Daubney140184d2021-05-18 16:04:07 +0100123 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
124 md_alg, (unsigned int) hash_len,
125 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200126}
127
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200128static int rsa_decrypt_wrap( void *ctx,
129 const unsigned char *input, size_t ilen,
130 unsigned char *output, size_t *olen, size_t osize,
131 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
132{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100133 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
134
135 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200137
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100138 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100139 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200140}
141
142static int rsa_encrypt_wrap( void *ctx,
143 const unsigned char *input, size_t ilen,
144 unsigned char *output, size_t *olen, size_t osize,
145 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
146{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100147 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
148 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200149
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100150 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100152
Thomas Daubney21772772021-05-13 17:30:32 +0100153 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100154 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200155}
156
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100157static int rsa_check_pair_wrap( const void *pub, const void *prv )
158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
160 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100161}
162
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200163static void *rsa_alloc_wrap( void )
164{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200165 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200166
167 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200168 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200169
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200170 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200171}
172
173static void rsa_free_wrap( void *ctx )
174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
176 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200177}
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200180{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200181#if defined(MBEDTLS_RSA_ALT)
182 /* Not supported */
183 (void) ctx;
184 (void) items;
185#else
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.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200189
190 items++;
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200193 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200195#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200196}
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198const mbedtls_pk_info_t mbedtls_rsa_info = {
199 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200200 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200201 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200202 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200203 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200204 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200205#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200206 NULL,
207 NULL,
208#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200209 rsa_decrypt_wrap,
210 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100211 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200212 rsa_alloc_wrap,
213 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200214#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200215 NULL,
216 NULL,
217#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200218 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200219};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200223/*
224 * Generic EC key
225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 return( type == MBEDTLS_PK_ECKEY ||
229 type == MBEDTLS_PK_ECKEY_DH ||
230 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200231}
232
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200233static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200236}
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200239/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200241 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200242 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200245 const unsigned char *hash, size_t hash_len,
246 unsigned char *sig, size_t *sig_len,
247 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200250 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200251 const unsigned char *sig, size_t sig_len )
252{
Janos Follath24eed8d2019-11-22 13:21:35 +0000253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200259 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200262
263 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200264}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200267 const unsigned char *hash, size_t hash_len,
268 unsigned char *sig, size_t *sig_len,
269 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
270{
Janos Follath24eed8d2019-11-22 13:21:35 +0000271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200277 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
278 f_rng, p_rng );
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200281
282 return( ret );
283}
284
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200285#if defined(MBEDTLS_ECP_RESTARTABLE)
286/* Forward declarations */
287static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
288 const unsigned char *hash, size_t hash_len,
289 const unsigned char *sig, size_t sig_len,
290 void *rs_ctx );
291
292static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
293 const unsigned char *hash, size_t hash_len,
294 unsigned char *sig, size_t *sig_len,
295 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
296 void *rs_ctx );
297
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200298/*
299 * Restart context for ECDSA operations with ECKEY context
300 *
301 * We need to store an actual ECDSA context, as we need to pass the same to
302 * the underlying ecdsa function, so we can't create it on the fly every time.
303 */
304typedef struct
305{
306 mbedtls_ecdsa_restart_ctx ecdsa_rs;
307 mbedtls_ecdsa_context ecdsa_ctx;
308} eckey_restart_ctx;
309
310static void *eckey_rs_alloc( void )
311{
312 eckey_restart_ctx *rs_ctx;
313
314 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
315
316 if( ctx != NULL )
317 {
318 rs_ctx = ctx;
319 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
320 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
321 }
322
323 return( ctx );
324}
325
326static void eckey_rs_free( void *ctx )
327{
328 eckey_restart_ctx *rs_ctx;
329
330 if( ctx == NULL)
331 return;
332
333 rs_ctx = ctx;
334 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
335 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
336
337 mbedtls_free( ctx );
338}
339
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200340static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
341 const unsigned char *hash, size_t hash_len,
342 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200343 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200344{
Janos Follath24eed8d2019-11-22 13:21:35 +0000345 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200346 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200347
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200348 /* Should never happen */
349 if( rs == NULL )
350 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200351
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200352 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200353 if( rs->ecdsa_ctx.grp.pbits == 0 )
354 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200355
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200356 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
357 md_alg, hash, hash_len,
358 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200359
360cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200361 return( ret );
362}
363
364static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
365 const unsigned char *hash, size_t hash_len,
366 unsigned char *sig, size_t *sig_len,
367 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200368 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200369{
Janos Follath24eed8d2019-11-22 13:21:35 +0000370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200371 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200372
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200373 /* Should never happen */
374 if( rs == NULL )
375 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200376
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200377 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200378 if( rs->ecdsa_ctx.grp.pbits == 0 )
379 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200380
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200381 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
382 hash, hash_len, sig, sig_len,
383 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200384
385cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200386 return( ret );
387}
388#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200390
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100391static int eckey_check_pair( const void *pub, const void *prv )
392{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
394 (const mbedtls_ecp_keypair *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100395}
396
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200397static void *eckey_alloc_wrap( void )
398{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200399 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200400
401 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200403
404 return( ctx );
405}
406
407static void eckey_free_wrap( void *ctx )
408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
410 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200411}
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200416 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200418}
419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420const mbedtls_pk_info_t mbedtls_eckey_info = {
421 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200422 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200423 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200424 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200426 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200427 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200428#if defined(MBEDTLS_ECP_RESTARTABLE)
429 eckey_verify_rs_wrap,
430 eckey_sign_rs_wrap,
431#endif
432#else /* MBEDTLS_ECDSA_C */
433 NULL,
434 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200435#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200436 NULL,
437 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100438 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200439 eckey_alloc_wrap,
440 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200441#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200442 eckey_rs_alloc,
443 eckey_rs_free,
444#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200445 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200446};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200447
448/*
Paul Bakker75342a62014-04-08 17:35:40 +0200449 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200452{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 return( type == MBEDTLS_PK_ECKEY ||
454 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200455}
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457const mbedtls_pk_info_t mbedtls_eckeydh_info = {
458 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200459 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200460 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200461 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200462 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200463 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200464#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200465 NULL,
466 NULL,
467#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200468 NULL,
469 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100470 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200471 eckey_alloc_wrap, /* Same underlying key structure */
472 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200473#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200474 NULL,
475 NULL,
476#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200477 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200478};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#if defined(MBEDTLS_ECDSA_C)
482static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200485}
486
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400487#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400488/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500489 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
490 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500491 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500492static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
493 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500494{
Janos Follath24eed8d2019-11-22 13:21:35 +0000495 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500496 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500497
Andrzej Kurek9241d182018-11-20 05:04:35 -0500498 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500499 MBEDTLS_ASN1_INTEGER ) ) != 0 )
500 {
501 return( ret );
502 }
503
Andrzej Kurek9241d182018-11-20 05:04:35 -0500504 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500505 {
506 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500507 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500508 }
509
Andrzej Kurek9241d182018-11-20 05:04:35 -0500510 if( unpadded_len > to_len || unpadded_len == 0 )
511 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500512
Andrzej Kurek9241d182018-11-20 05:04:35 -0500513 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500514 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500515 memcpy( to + padding_len, *from, unpadded_len );
516 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500517
Andrzej Kurek9241d182018-11-20 05:04:35 -0500518 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500519}
520
521/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400522 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500523 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500524 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400525 */
526static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500527 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400528{
Janos Follath24eed8d2019-11-22 13:21:35 +0000529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500530 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500531
Andrzej Kurek9241d182018-11-20 05:04:35 -0500532 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500533 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500534 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400535
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500536 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500537 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
538 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500539 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500540 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
541 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500542
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400543 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400544}
545
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100546static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400547 const unsigned char *hash, size_t hash_len,
548 const unsigned char *sig, size_t sig_len )
549{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100550 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200552 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200553 psa_key_id_t key_id = 0;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200554 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400555 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400556 int key_len;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500557 /* see ECP_PUB_DER_MAX_BYTES in pkwrite.c */
558 unsigned char buf[30 + 2 * MBEDTLS_ECP_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000559 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400560 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700561 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100562 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100563 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100564 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
565 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700566 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400567
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500568 if( curve == 0 )
569 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
570
Hanno Beckerccf574e2019-01-29 08:26:15 +0000571 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500572 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400573 key.pk_info = &pk_info;
574 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000575 p = buf + sizeof( buf );
576 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400577 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500578 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400579
Gilles Peskined2d45c12019-05-27 14:53:13 +0200580 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100581 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200582 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500583
Gilles Peskined2d45c12019-05-27 14:53:13 +0200584 status = psa_import_key( &attributes,
585 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200586 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200587 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400588 {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200589 ret = mbedtls_psa_err_translate_pk( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400590 goto cleanup;
591 }
592
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500593 /* We don't need the exported key anymore and can
594 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500595 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500596 {
597 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
598 goto cleanup;
599 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500600
Hanno Beckera9851112019-01-25 16:37:10 +0000601 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500602 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500603 signature_part_size ) ) != 0 )
604 {
605 goto cleanup;
606 }
607
Ronald Croncf56a0a2020-08-04 09:51:30 +0200608 if( psa_verify_hash( key_id, psa_sig_md,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100609 hash, hash_len,
610 buf, 2 * signature_part_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400611 != PSA_SUCCESS )
612 {
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400613 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
614 goto cleanup;
615 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500616
617 if( p != sig + sig_len )
618 {
619 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
620 goto cleanup;
621 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400622 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400623
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500624cleanup:
Ronald Croncf56a0a2020-08-04 09:51:30 +0200625 psa_destroy_key( key_id );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400626 return( ret );
627}
628#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200630 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200631 const unsigned char *sig, size_t sig_len )
632{
Janos Follath24eed8d2019-11-22 13:21:35 +0000633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200634 ((void) md_alg);
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200637 hash, hash_len, sig, sig_len );
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
640 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200641
642 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200643}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400644#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200647 const unsigned char *hash, size_t hash_len,
648 unsigned char *sig, size_t *sig_len,
649 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
650{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200652 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200653}
654
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200655#if defined(MBEDTLS_ECP_RESTARTABLE)
656static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
657 const unsigned char *hash, size_t hash_len,
658 const unsigned char *sig, size_t sig_len,
659 void *rs_ctx )
660{
Janos Follath24eed8d2019-11-22 13:21:35 +0000661 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200662 ((void) md_alg);
663
664 ret = mbedtls_ecdsa_read_signature_restartable(
665 (mbedtls_ecdsa_context *) ctx,
666 hash, hash_len, sig, sig_len,
667 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
668
669 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
670 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
671
672 return( ret );
673}
674
675static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
676 const unsigned char *hash, size_t hash_len,
677 unsigned char *sig, size_t *sig_len,
678 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
679 void *rs_ctx )
680{
681 return( mbedtls_ecdsa_write_signature_restartable(
682 (mbedtls_ecdsa_context *) ctx,
683 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
684 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
685
686}
687#endif /* MBEDTLS_ECP_RESTARTABLE */
688
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200689static void *ecdsa_alloc_wrap( void )
690{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200691 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200692
693 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200695
696 return( ctx );
697}
698
699static void ecdsa_free_wrap( void *ctx )
700{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
702 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200703}
704
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200705#if defined(MBEDTLS_ECP_RESTARTABLE)
706static void *ecdsa_rs_alloc( void )
707{
708 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
709
710 if( ctx != NULL )
711 mbedtls_ecdsa_restart_init( ctx );
712
713 return( ctx );
714}
715
716static void ecdsa_rs_free( void *ctx )
717{
718 mbedtls_ecdsa_restart_free( ctx );
719 mbedtls_free( ctx );
720}
721#endif /* MBEDTLS_ECP_RESTARTABLE */
722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723const mbedtls_pk_info_t mbedtls_ecdsa_info = {
724 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200725 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200726 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200727 ecdsa_can_do,
728 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200729 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200730#if defined(MBEDTLS_ECP_RESTARTABLE)
731 ecdsa_verify_rs_wrap,
732 ecdsa_sign_rs_wrap,
733#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200734 NULL,
735 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100736 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200737 ecdsa_alloc_wrap,
738 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200739#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200740 ecdsa_rs_alloc,
741 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200742#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200743 eckey_debug, /* Compatible key structures */
744};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200748/*
749 * Support for alternative RSA-private implementations
750 */
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200755}
756
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200757static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200760
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200761 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200762}
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200765 const unsigned char *hash, size_t hash_len,
766 unsigned char *sig, size_t *sig_len,
767 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
768{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200770
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100771#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000772 if( UINT_MAX < hash_len )
773 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100774#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000775
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200776 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +0100777 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
778 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200779
Thomas Daubneyfa1581e2021-05-18 12:38:33 +0100780 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200781 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200782}
783
784static int rsa_alt_decrypt_wrap( void *ctx,
785 const unsigned char *input, size_t ilen,
786 unsigned char *output, size_t *olen, size_t osize,
787 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
788{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200790
791 ((void) f_rng);
792 ((void) p_rng);
793
794 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200796
797 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +0100798 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200799}
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100802static int rsa_alt_check_pair( const void *pub, const void *prv )
803{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100805 unsigned char hash[32];
806 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +0000807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100808
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200809 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100811
812 memset( hash, 0x2a, sizeof( hash ) );
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100815 hash, sizeof( hash ),
816 sig, &sig_len, NULL, NULL ) ) != 0 )
817 {
818 return( ret );
819 }
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100822 hash, sizeof( hash ), sig, sig_len ) != 0 )
823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100825 }
826
827 return( 0 );
828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100830
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200831static void *rsa_alt_alloc_wrap( void )
832{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200833 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200834
835 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200837
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200838 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200839}
840
841static void rsa_alt_free_wrap( void *ctx )
842{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500843 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200845}
846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
848 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200849 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200850 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200851 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200852 NULL,
853 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200854#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200855 NULL,
856 NULL,
857#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200858 rsa_alt_decrypt_wrap,
859 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100861 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100862#else
863 NULL,
864#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200865 rsa_alt_alloc_wrap,
866 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200867#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200868 NULL,
869 NULL,
870#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200871 NULL,
872};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200875
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200876#if defined(MBEDTLS_USE_PSA_CRYPTO)
877
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100878static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100879{
Ronald Croncf56a0a2020-08-04 09:51:30 +0200880 void *ctx = mbedtls_calloc( 1, sizeof( psa_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100881
882 /* no _init() function to call, an calloc() already zeroized */
883
884 return( ctx );
885}
886
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100887static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100888{
Ronald Croncf56a0a2020-08-04 09:51:30 +0200889 mbedtls_platform_zeroize( ctx, sizeof( psa_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100890 mbedtls_free( ctx );
891}
892
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100893static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100894{
Ronald Croncf56a0a2020-08-04 09:51:30 +0200895 const psa_key_id_t *key = (const psa_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100896 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100898
Gilles Peskined2d45c12019-05-27 14:53:13 +0200899 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100900 return( 0 );
901
Gilles Peskined2d45c12019-05-27 14:53:13 +0200902 bits = psa_get_key_bits( &attributes );
903 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100904 return( bits );
905}
906
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100907static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100908{
909 /* For now opaque PSA keys can only wrap ECC keypairs,
910 * as checked by setup_psa().
911 * Also, ECKEY_DH does not really make sense with the current API. */
912 return( type == MBEDTLS_PK_ECKEY ||
913 type == MBEDTLS_PK_ECDSA );
914}
915
John Durkopf35069a2020-08-17 22:05:14 -0700916#if defined(MBEDTLS_ECDSA_C)
917
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100918/*
Manuel Pégourié-Gonnard509aff12018-11-15 12:17:38 +0100919 * Simultaneously convert and move raw MPI from the beginning of a buffer
920 * to an ASN.1 MPI at the end of the buffer.
921 * See also mbedtls_asn1_write_mpi().
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100922 *
923 * p: pointer to the end of the output buffer
924 * 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 +0100925 * n_len: length of the mpi to read from start
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100926 */
927static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
928 size_t n_len )
929{
Janos Follath24eed8d2019-11-22 13:21:35 +0000930 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100931 size_t len = 0;
932
933 if( (size_t)( *p - start ) < n_len )
934 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
935
936 len = n_len;
937 *p -= len;
938 memmove( *p, start, len );
939
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100940 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +0100941 * Neither r nor s should be 0, but as a failsafe measure, still detect
942 * that rather than overflowing the buffer in case of a PSA error. */
943 while( len > 0 && **p == 0x00 )
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100944 {
945 ++(*p);
946 --len;
947 }
948
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +0100949 /* this is only reached if the signature was invalid */
950 if( len == 0 )
TRodziewiczb579ccd2021-04-13 14:28:28 +0200951 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +0100952
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100953 /* if the msb is 1, ASN.1 requires that we prepend a 0.
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100954 * 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 +0100955 if( **p & 0x80 )
956 {
957 if( *p - start < 1 )
958 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
959
960 *--(*p) = 0x00;
961 len += 1;
962 }
963
964 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
965 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
966 MBEDTLS_ASN1_INTEGER ) );
967
968 return( (int) len );
969}
970
971/* Transcode signature from PSA format to ASN.1 sequence.
972 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
973 * MPIs, and in-place.
974 *
975 * [in/out] sig: the signature pre- and post-transcoding
976 * [in/out] sig_len: signature length pre- and post-transcoding
977 * [int] buf_len: the available size the in/out buffer
978 */
979static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
980 size_t buf_len )
981{
Janos Follath24eed8d2019-11-22 13:21:35 +0000982 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100983 size_t len = 0;
984 const size_t rs_len = *sig_len / 2;
985 unsigned char *p = sig + buf_len;
986
987 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
988 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
989
990 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
991 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
992 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
993
994 memmove( sig, p, len );
995 *sig_len = len;
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +0100996
997 return( 0 );
998}
999
John Durkopd46ede02020-08-24 09:51:00 -07001000#endif /* MBEDTLS_ECDSA_C */
John Durkopf35069a2020-08-17 22:05:14 -07001001
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001002static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1003 const unsigned char *hash, size_t hash_len,
1004 unsigned char *sig, size_t *sig_len,
1005 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1006{
John Durkopf35069a2020-08-17 22:05:14 -07001007#if !defined(MBEDTLS_ECDSA_C)
1008 ((void) ctx);
1009 ((void) md_alg);
1010 ((void) hash);
1011 ((void) hash_len);
1012 ((void) sig);
1013 ((void) sig_len);
1014 ((void) f_rng);
1015 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001016 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1017#else /* !MBEDTLS_ECDSA_C */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001018 const psa_key_id_t *key = (const psa_key_id_t *) ctx;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001019 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001020 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Gilles Peskined2d45c12019-05-27 14:53:13 +02001021 size_t buf_len;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001022 psa_status_t status;
1023
1024 /* PSA has its own RNG */
1025 (void) f_rng;
1026 (void) p_rng;
1027
1028 /* PSA needs an output buffer of known size, but our API doesn't provide
1029 * that information. Assume that the buffer is large enough for a
1030 * maximal-length signature with that key (otherwise the application is
1031 * buggy anyway). */
Gilles Peskined2d45c12019-05-27 14:53:13 +02001032 status = psa_get_key_attributes( *key, &attributes );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001033 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9a5a77b2018-11-16 10:15:09 +01001034 return( mbedtls_psa_err_translate_pk( status ) );
Gilles Peskined2d45c12019-05-27 14:53:13 +02001035 buf_len = MBEDTLS_ECDSA_MAX_SIG_LEN( psa_get_key_bits( &attributes ) );
1036 psa_reset_key_attributes( &attributes );
Gilles Peskine5bcb24b2019-11-08 17:33:29 +01001037 if( buf_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001038 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001039
1040 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001041 status = psa_sign_hash( *key, alg, hash, hash_len,
1042 sig, buf_len, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001043 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9a5a77b2018-11-16 10:15:09 +01001044 return( mbedtls_psa_err_translate_pk( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001045
1046 /* transcode it to ASN.1 sequence */
1047 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, buf_len ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001048#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001049}
1050
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001051const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1052 MBEDTLS_PK_OPAQUE,
1053 "Opaque",
1054 pk_opaque_get_bitlen,
1055 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001056 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001057 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001058#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1059 NULL, /* restartable verify - not relevant */
1060 NULL, /* restartable sign - not relevant */
1061#endif
1062 NULL, /* decrypt - will be done later */
1063 NULL, /* encrypt - will be done later */
1064 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001065 pk_opaque_alloc_wrap,
1066 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001067#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1068 NULL, /* restart alloc - not relevant */
1069 NULL, /* restart free - not relevant */
1070#endif
1071 NULL, /* debug - could be done later, or even left NULL */
1072};
1073
1074#endif /* MBEDTLS_USE_PSA_CRYPTO */
1075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#endif /* MBEDTLS_PK_C */