blob: f35abf21a46ac3d626e0bd6008327106fcc060e8 [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)
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020023#include "mbedtls/pk_internal.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é-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020054
Andres AG72849872017-01-19 11:24:33 +000055#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010056#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_RSA_C)
59static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 return( type == MBEDTLS_PK_RSA ||
62 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020063}
64
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +020065static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020066{
Hanno Becker6a1e7e52017-08-22 13:55:00 +010067 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
68 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020069}
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020072 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020073 const unsigned char *sig, size_t sig_len )
74{
Janos Follath24eed8d2019-11-22 13:21:35 +000075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +010076 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
77 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020078
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010079#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +000080 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
81 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010082#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +000083
Hanno Becker6a1e7e52017-08-22 13:55:00 +010084 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020086
Hanno Becker6a1e7e52017-08-22 13:55:00 +010087 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 MBEDTLS_RSA_PUBLIC, md_alg,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020089 (unsigned int) hash_len, hash, sig ) ) != 0 )
90 return( ret );
91
Gilles Peskine5114d3e2018-03-30 07:12:15 +020092 /* The buffer contains a valid signature followed by extra data.
93 * We have a special error code for that so that so that callers can
94 * use mbedtls_pk_verify() to check "Does the buffer start with a
95 * valid signature?" and not just "Does the buffer contain a valid
96 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +010097 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020099
100 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200101}
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200104 const unsigned char *hash, size_t hash_len,
105 unsigned char *sig, size_t *sig_len,
106 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
107{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100108 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
109
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100110#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000111 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
112 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100113#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000114
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100115 *sig_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200116
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100117 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200118 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200119}
120
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200121static int rsa_decrypt_wrap( void *ctx,
122 const unsigned char *input, size_t ilen,
123 unsigned char *output, size_t *olen, size_t osize,
124 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
125{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100126 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
127
128 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200130
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100131 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200133}
134
135static int rsa_encrypt_wrap( void *ctx,
136 const unsigned char *input, size_t ilen,
137 unsigned char *output, size_t *olen, size_t osize,
138 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
139{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100140 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
141 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200142
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100143 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100145
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100146 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
147 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200148}
149
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100150static int rsa_check_pair_wrap( const void *pub, const void *prv )
151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
153 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100154}
155
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200156static void *rsa_alloc_wrap( void )
157{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200158 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200159
160 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200162
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200163 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200164}
165
166static void rsa_free_wrap( void *ctx )
167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
169 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200170}
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200173{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200175 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200177
178 items++;
179
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.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200183}
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185const mbedtls_pk_info_t mbedtls_rsa_info = {
186 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200187 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200188 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200189 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200190 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200191 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200192#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200193 NULL,
194 NULL,
195#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200196 rsa_decrypt_wrap,
197 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100198 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200199 rsa_alloc_wrap,
200 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200201#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200202 NULL,
203 NULL,
204#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200205 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200206};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200210/*
211 * Generic EC key
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200214{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return( type == MBEDTLS_PK_ECKEY ||
216 type == MBEDTLS_PK_ECKEY_DH ||
217 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200218}
219
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200220static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200223}
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200226/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200228 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200229 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200232 const unsigned char *hash, size_t hash_len,
233 unsigned char *sig, size_t *sig_len,
234 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200237 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200238 const unsigned char *sig, size_t sig_len )
239{
Janos Follath24eed8d2019-11-22 13:21:35 +0000240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200246 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200249
250 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200251}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200254 const unsigned char *hash, size_t hash_len,
255 unsigned char *sig, size_t *sig_len,
256 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
257{
Janos Follath24eed8d2019-11-22 13:21:35 +0000258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200264 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
265 f_rng, p_rng );
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200268
269 return( ret );
270}
271
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200272#if defined(MBEDTLS_ECP_RESTARTABLE)
273/* Forward declarations */
274static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
275 const unsigned char *hash, size_t hash_len,
276 const unsigned char *sig, size_t sig_len,
277 void *rs_ctx );
278
279static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
280 const unsigned char *hash, size_t hash_len,
281 unsigned char *sig, size_t *sig_len,
282 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
283 void *rs_ctx );
284
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200285/*
286 * Restart context for ECDSA operations with ECKEY context
287 *
288 * We need to store an actual ECDSA context, as we need to pass the same to
289 * the underlying ecdsa function, so we can't create it on the fly every time.
290 */
291typedef struct
292{
293 mbedtls_ecdsa_restart_ctx ecdsa_rs;
294 mbedtls_ecdsa_context ecdsa_ctx;
295} eckey_restart_ctx;
296
297static void *eckey_rs_alloc( void )
298{
299 eckey_restart_ctx *rs_ctx;
300
301 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
302
303 if( ctx != NULL )
304 {
305 rs_ctx = ctx;
306 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
307 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
308 }
309
310 return( ctx );
311}
312
313static void eckey_rs_free( void *ctx )
314{
315 eckey_restart_ctx *rs_ctx;
316
317 if( ctx == NULL)
318 return;
319
320 rs_ctx = ctx;
321 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
322 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
323
324 mbedtls_free( ctx );
325}
326
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200327static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
328 const unsigned char *hash, size_t hash_len,
329 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200330 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200331{
Janos Follath24eed8d2019-11-22 13:21:35 +0000332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200333 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200334
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200335 /* Should never happen */
336 if( rs == NULL )
337 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200338
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200339 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200340 if( rs->ecdsa_ctx.grp.pbits == 0 )
341 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200342
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200343 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
344 md_alg, hash, hash_len,
345 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200346
347cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200348 return( ret );
349}
350
351static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
352 const unsigned char *hash, size_t hash_len,
353 unsigned char *sig, size_t *sig_len,
354 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200355 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200356{
Janos Follath24eed8d2019-11-22 13:21:35 +0000357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200358 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200359
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200360 /* Should never happen */
361 if( rs == NULL )
362 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200363
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200364 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200365 if( rs->ecdsa_ctx.grp.pbits == 0 )
366 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200367
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200368 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
369 hash, hash_len, sig, sig_len,
370 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200371
372cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200373 return( ret );
374}
375#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200377
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100378static int eckey_check_pair( const void *pub, const void *prv )
379{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
381 (const mbedtls_ecp_keypair *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100382}
383
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200384static void *eckey_alloc_wrap( void )
385{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200386 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200387
388 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200390
391 return( ctx );
392}
393
394static void eckey_free_wrap( void *ctx )
395{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
397 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200398}
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200403 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200405}
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407const mbedtls_pk_info_t mbedtls_eckey_info = {
408 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200409 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200410 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200411 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200413 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200414 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200415#if defined(MBEDTLS_ECP_RESTARTABLE)
416 eckey_verify_rs_wrap,
417 eckey_sign_rs_wrap,
418#endif
419#else /* MBEDTLS_ECDSA_C */
420 NULL,
421 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200422#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200423 NULL,
424 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100425 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200426 eckey_alloc_wrap,
427 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200428#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200429 eckey_rs_alloc,
430 eckey_rs_free,
431#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200432 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200433};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200434
435/*
Paul Bakker75342a62014-04-08 17:35:40 +0200436 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200439{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 return( type == MBEDTLS_PK_ECKEY ||
441 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200442}
443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444const mbedtls_pk_info_t mbedtls_eckeydh_info = {
445 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200446 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200447 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200448 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200449 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200450 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200451#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200452 NULL,
453 NULL,
454#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200455 NULL,
456 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100457 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200458 eckey_alloc_wrap, /* Same underlying key structure */
459 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200460#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200461 NULL,
462 NULL,
463#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200464 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200465};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468#if defined(MBEDTLS_ECDSA_C)
469static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200472}
473
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400474#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400475/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500476 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
477 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500478 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500479static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
480 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500481{
Janos Follath24eed8d2019-11-22 13:21:35 +0000482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500483 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500484
Andrzej Kurek9241d182018-11-20 05:04:35 -0500485 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500486 MBEDTLS_ASN1_INTEGER ) ) != 0 )
487 {
488 return( ret );
489 }
490
Andrzej Kurek9241d182018-11-20 05:04:35 -0500491 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500492 {
493 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500494 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500495 }
496
Andrzej Kurek9241d182018-11-20 05:04:35 -0500497 if( unpadded_len > to_len || unpadded_len == 0 )
498 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500499
Andrzej Kurek9241d182018-11-20 05:04:35 -0500500 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500501 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500502 memcpy( to + padding_len, *from, unpadded_len );
503 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500504
Andrzej Kurek9241d182018-11-20 05:04:35 -0500505 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500506}
507
508/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400509 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500510 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500511 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400512 */
513static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500514 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400515{
Janos Follath24eed8d2019-11-22 13:21:35 +0000516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500517 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500518
Andrzej Kurek9241d182018-11-20 05:04:35 -0500519 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500520 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500521 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400522
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500523 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500524 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
525 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500526 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500527 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
528 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500529
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400530 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400531}
532
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100533static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400534 const unsigned char *hash, size_t hash_len,
535 const unsigned char *sig, size_t sig_len )
536{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100537 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200540 psa_key_id_t key_id = 0;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200541 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400542 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400543 int key_len;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500544 /* see ECP_PUB_DER_MAX_BYTES in pkwrite.c */
545 unsigned char buf[30 + 2 * MBEDTLS_ECP_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000546 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400547 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700548 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100549 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100550 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100551 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
552 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700553 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400554
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500555 if( curve == 0 )
556 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
557
Hanno Beckerccf574e2019-01-29 08:26:15 +0000558 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500559 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400560 key.pk_info = &pk_info;
561 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000562 p = buf + sizeof( buf );
563 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400564 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500565 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400566
Gilles Peskined2d45c12019-05-27 14:53:13 +0200567 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100568 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200569 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500570
Gilles Peskined2d45c12019-05-27 14:53:13 +0200571 status = psa_import_key( &attributes,
572 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200573 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200574 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400575 {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200576 ret = mbedtls_psa_err_translate_pk( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400577 goto cleanup;
578 }
579
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500580 /* We don't need the exported key anymore and can
581 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500582 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500583 {
584 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
585 goto cleanup;
586 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500587
Hanno Beckera9851112019-01-25 16:37:10 +0000588 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500589 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500590 signature_part_size ) ) != 0 )
591 {
592 goto cleanup;
593 }
594
Ronald Croncf56a0a2020-08-04 09:51:30 +0200595 if( psa_verify_hash( key_id, psa_sig_md,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100596 hash, hash_len,
597 buf, 2 * signature_part_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400598 != PSA_SUCCESS )
599 {
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400600 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
601 goto cleanup;
602 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500603
604 if( p != sig + sig_len )
605 {
606 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
607 goto cleanup;
608 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400609 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400610
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500611cleanup:
Ronald Croncf56a0a2020-08-04 09:51:30 +0200612 psa_destroy_key( key_id );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400613 return( ret );
614}
615#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200617 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200618 const unsigned char *sig, size_t sig_len )
619{
Janos Follath24eed8d2019-11-22 13:21:35 +0000620 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200621 ((void) md_alg);
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200624 hash, hash_len, sig, sig_len );
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
627 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200628
629 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200630}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400631#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200634 const unsigned char *hash, size_t hash_len,
635 unsigned char *sig, size_t *sig_len,
636 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200639 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200640}
641
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200642#if defined(MBEDTLS_ECP_RESTARTABLE)
643static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
644 const unsigned char *hash, size_t hash_len,
645 const unsigned char *sig, size_t sig_len,
646 void *rs_ctx )
647{
Janos Follath24eed8d2019-11-22 13:21:35 +0000648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200649 ((void) md_alg);
650
651 ret = mbedtls_ecdsa_read_signature_restartable(
652 (mbedtls_ecdsa_context *) ctx,
653 hash, hash_len, sig, sig_len,
654 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
655
656 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
657 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
658
659 return( ret );
660}
661
662static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
663 const unsigned char *hash, size_t hash_len,
664 unsigned char *sig, size_t *sig_len,
665 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
666 void *rs_ctx )
667{
668 return( mbedtls_ecdsa_write_signature_restartable(
669 (mbedtls_ecdsa_context *) ctx,
670 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
671 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
672
673}
674#endif /* MBEDTLS_ECP_RESTARTABLE */
675
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200676static void *ecdsa_alloc_wrap( void )
677{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200678 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200679
680 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200682
683 return( ctx );
684}
685
686static void ecdsa_free_wrap( void *ctx )
687{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
689 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200690}
691
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200692#if defined(MBEDTLS_ECP_RESTARTABLE)
693static void *ecdsa_rs_alloc( void )
694{
695 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
696
697 if( ctx != NULL )
698 mbedtls_ecdsa_restart_init( ctx );
699
700 return( ctx );
701}
702
703static void ecdsa_rs_free( void *ctx )
704{
705 mbedtls_ecdsa_restart_free( ctx );
706 mbedtls_free( ctx );
707}
708#endif /* MBEDTLS_ECP_RESTARTABLE */
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710const mbedtls_pk_info_t mbedtls_ecdsa_info = {
711 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200712 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200713 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200714 ecdsa_can_do,
715 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200716 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200717#if defined(MBEDTLS_ECP_RESTARTABLE)
718 ecdsa_verify_rs_wrap,
719 ecdsa_sign_rs_wrap,
720#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200721 NULL,
722 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100723 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200724 ecdsa_alloc_wrap,
725 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200726#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200727 ecdsa_rs_alloc,
728 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200729#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200730 eckey_debug, /* Compatible key structures */
731};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200735/*
736 * Support for alternative RSA-private implementations
737 */
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200742}
743
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200744static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200745{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200747
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200748 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200749}
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200752 const unsigned char *hash, size_t hash_len,
753 unsigned char *sig, size_t *sig_len,
754 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
755{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200757
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100758#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000759 if( UINT_MAX < hash_len )
760 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100761#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000762
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200763 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +0100764 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
765 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200768 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200769}
770
771static int rsa_alt_decrypt_wrap( void *ctx,
772 const unsigned char *input, size_t ilen,
773 unsigned char *output, size_t *olen, size_t osize,
774 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200777
778 ((void) f_rng);
779 ((void) p_rng);
780
781 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200783
784 return( rsa_alt->decrypt_func( rsa_alt->key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200786}
787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100789static int rsa_alt_check_pair( const void *pub, const void *prv )
790{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100792 unsigned char hash[32];
793 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +0000794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100795
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200796 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100798
799 memset( hash, 0x2a, sizeof( hash ) );
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100802 hash, sizeof( hash ),
803 sig, &sig_len, NULL, NULL ) ) != 0 )
804 {
805 return( ret );
806 }
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100809 hash, sizeof( hash ), sig, sig_len ) != 0 )
810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100812 }
813
814 return( 0 );
815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100817
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200818static void *rsa_alt_alloc_wrap( void )
819{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200820 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200821
822 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200824
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200825 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200826}
827
828static void rsa_alt_free_wrap( void *ctx )
829{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500830 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200832}
833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
835 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200836 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200837 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200838 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200839 NULL,
840 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200841#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200842 NULL,
843 NULL,
844#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200845 rsa_alt_decrypt_wrap,
846 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100848 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100849#else
850 NULL,
851#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200852 rsa_alt_alloc_wrap,
853 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200854#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200855 NULL,
856 NULL,
857#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200858 NULL,
859};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200862
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200863#if defined(MBEDTLS_USE_PSA_CRYPTO)
864
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100865static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100866{
Ronald Croncf56a0a2020-08-04 09:51:30 +0200867 void *ctx = mbedtls_calloc( 1, sizeof( psa_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100868
Tom Cosgrove5205c972022-07-28 06:12:08 +0100869 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100870
871 return( ctx );
872}
873
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100874static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100875{
Ronald Croncf56a0a2020-08-04 09:51:30 +0200876 mbedtls_platform_zeroize( ctx, sizeof( psa_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100877 mbedtls_free( ctx );
878}
879
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100880static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100881{
Ronald Croncf56a0a2020-08-04 09:51:30 +0200882 const psa_key_id_t *key = (const psa_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100883 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200884 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100885
Gilles Peskined2d45c12019-05-27 14:53:13 +0200886 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100887 return( 0 );
888
Gilles Peskined2d45c12019-05-27 14:53:13 +0200889 bits = psa_get_key_bits( &attributes );
890 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100891 return( bits );
892}
893
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100894static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100895{
896 /* For now opaque PSA keys can only wrap ECC keypairs,
897 * as checked by setup_psa().
898 * Also, ECKEY_DH does not really make sense with the current API. */
899 return( type == MBEDTLS_PK_ECKEY ||
900 type == MBEDTLS_PK_ECDSA );
901}
902
John Durkopf35069a2020-08-17 22:05:14 -0700903#if defined(MBEDTLS_ECDSA_C)
904
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100905/*
Manuel Pégourié-Gonnard509aff12018-11-15 12:17:38 +0100906 * Simultaneously convert and move raw MPI from the beginning of a buffer
907 * to an ASN.1 MPI at the end of the buffer.
908 * See also mbedtls_asn1_write_mpi().
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100909 *
910 * p: pointer to the end of the output buffer
911 * 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 +0100912 * n_len: length of the mpi to read from start
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100913 */
914static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
915 size_t n_len )
916{
Janos Follath24eed8d2019-11-22 13:21:35 +0000917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100918 size_t len = 0;
919
920 if( (size_t)( *p - start ) < n_len )
921 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
922
923 len = n_len;
924 *p -= len;
925 memmove( *p, start, len );
926
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100927 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +0100928 * Neither r nor s should be 0, but as a failsafe measure, still detect
929 * that rather than overflowing the buffer in case of a PSA error. */
930 while( len > 0 && **p == 0x00 )
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100931 {
932 ++(*p);
933 --len;
934 }
935
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +0100936 /* this is only reached if the signature was invalid */
937 if( len == 0 )
938 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
939
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100940 /* if the msb is 1, ASN.1 requires that we prepend a 0.
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100941 * 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 +0100942 if( **p & 0x80 )
943 {
944 if( *p - start < 1 )
945 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
946
947 *--(*p) = 0x00;
948 len += 1;
949 }
950
951 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
952 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
953 MBEDTLS_ASN1_INTEGER ) );
954
955 return( (int) len );
956}
957
958/* Transcode signature from PSA format to ASN.1 sequence.
959 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
960 * MPIs, and in-place.
961 *
962 * [in/out] sig: the signature pre- and post-transcoding
963 * [in/out] sig_len: signature length pre- and post-transcoding
964 * [int] buf_len: the available size the in/out buffer
965 */
966static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
967 size_t buf_len )
968{
Janos Follath24eed8d2019-11-22 13:21:35 +0000969 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100970 size_t len = 0;
971 const size_t rs_len = *sig_len / 2;
972 unsigned char *p = sig + buf_len;
973
974 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
975 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
976
977 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
978 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
979 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
980
981 memmove( sig, p, len );
982 *sig_len = len;
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +0100983
984 return( 0 );
985}
986
John Durkopd46ede02020-08-24 09:51:00 -0700987#endif /* MBEDTLS_ECDSA_C */
John Durkopf35069a2020-08-17 22:05:14 -0700988
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100989static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
990 const unsigned char *hash, size_t hash_len,
991 unsigned char *sig, size_t *sig_len,
992 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
993{
John Durkopf35069a2020-08-17 22:05:14 -0700994#if !defined(MBEDTLS_ECDSA_C)
995 ((void) ctx);
996 ((void) md_alg);
997 ((void) hash);
998 ((void) hash_len);
999 ((void) sig);
1000 ((void) sig_len);
1001 ((void) f_rng);
1002 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001003 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1004#else /* !MBEDTLS_ECDSA_C */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001005 const psa_key_id_t *key = (const psa_key_id_t *) ctx;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001007 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Gilles Peskined2d45c12019-05-27 14:53:13 +02001008 size_t buf_len;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001009 psa_status_t status;
1010
1011 /* PSA has its own RNG */
1012 (void) f_rng;
1013 (void) p_rng;
1014
1015 /* PSA needs an output buffer of known size, but our API doesn't provide
1016 * that information. Assume that the buffer is large enough for a
1017 * maximal-length signature with that key (otherwise the application is
1018 * buggy anyway). */
Gilles Peskined2d45c12019-05-27 14:53:13 +02001019 status = psa_get_key_attributes( *key, &attributes );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001020 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9a5a77b2018-11-16 10:15:09 +01001021 return( mbedtls_psa_err_translate_pk( status ) );
Gilles Peskined2d45c12019-05-27 14:53:13 +02001022 buf_len = MBEDTLS_ECDSA_MAX_SIG_LEN( psa_get_key_bits( &attributes ) );
1023 psa_reset_key_attributes( &attributes );
Gilles Peskine5bcb24b2019-11-08 17:33:29 +01001024 if( buf_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001025 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001026
1027 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001028 status = psa_sign_hash( *key, alg, hash, hash_len,
1029 sig, buf_len, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001030 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9a5a77b2018-11-16 10:15:09 +01001031 return( mbedtls_psa_err_translate_pk( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001032
1033 /* transcode it to ASN.1 sequence */
1034 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, buf_len ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001035#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001036}
1037
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001038const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1039 MBEDTLS_PK_OPAQUE,
1040 "Opaque",
1041 pk_opaque_get_bitlen,
1042 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001043 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001044 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001045#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1046 NULL, /* restartable verify - not relevant */
1047 NULL, /* restartable sign - not relevant */
1048#endif
1049 NULL, /* decrypt - will be done later */
1050 NULL, /* encrypt - will be done later */
1051 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001052 pk_opaque_alloc_wrap,
1053 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001054#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1055 NULL, /* restart alloc - not relevant */
1056 NULL, /* restart free - not relevant */
1057#endif
1058 NULL, /* debug - could be done later, or even left NULL */
1059};
1060
1061#endif /* MBEDTLS_USE_PSA_CRYPTO */
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_PK_C */