blob: 2e22ec9d3094c832b1196957b825266ac50d2fc9 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020029#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020030
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020031/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <string.h>
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020038#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020042#endif
43
Andres Amaya Garciae32df082017-10-25 09:37:04 +010044#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010046#endif
47
Andrzej Kurek8b036a62018-10-31 05:16:46 -040048#if defined(MBEDTLS_USE_PSA_CRYPTO)
49#include "psa/crypto.h"
50#include "mbedtls/x509.h"
51#include "mbedtls/asn1.h"
52#endif
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020056#else
57#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_free free
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020060#endif
61
Andres AG72849872017-01-19 11:24:33 +000062#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010063#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_RSA_C)
66static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 return( type == MBEDTLS_PK_RSA ||
69 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020070}
71
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +020072static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020073{
Hanno Becker6a1e7e52017-08-22 13:55:00 +010074 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
75 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020076}
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020079 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020080 const unsigned char *sig, size_t sig_len )
81{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020082 int ret;
Hanno Becker6a1e7e52017-08-22 13:55:00 +010083 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
84 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020085
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010086#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +000087 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
88 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010089#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +000090
Hanno Becker6a1e7e52017-08-22 13:55:00 +010091 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020093
Hanno Becker6a1e7e52017-08-22 13:55:00 +010094 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 MBEDTLS_RSA_PUBLIC, md_alg,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020096 (unsigned int) hash_len, hash, sig ) ) != 0 )
97 return( ret );
98
Gilles Peskine5114d3e2018-03-30 07:12:15 +020099 /* The buffer contains a valid signature followed by extra data.
100 * We have a special error code for that so that so that callers can
101 * use mbedtls_pk_verify() to check "Does the buffer start with a
102 * valid signature?" and not just "Does the buffer contain a valid
103 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100104 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200106
107 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200108}
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200111 const unsigned char *hash, size_t hash_len,
112 unsigned char *sig, size_t *sig_len,
113 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
114{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100115 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
116
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100117#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000118 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
119 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100120#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000121
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100122 *sig_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200123
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100124 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200125 md_alg, (unsigned int) hash_len, 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,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 MBEDTLS_RSA_PRIVATE, 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
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100153 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
154 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 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200182 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200184
185 items++;
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200188 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200190}
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192const mbedtls_pk_info_t mbedtls_rsa_info = {
193 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200194 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200195 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200196 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200197 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200198 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200199#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200200 NULL,
201 NULL,
202#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200203 rsa_decrypt_wrap,
204 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100205 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200206 rsa_alloc_wrap,
207 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200208#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200209 NULL,
210 NULL,
211#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200212 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200213};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200217/*
218 * Generic EC key
219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 return( type == MBEDTLS_PK_ECKEY ||
223 type == MBEDTLS_PK_ECKEY_DH ||
224 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200225}
226
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200227static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200228{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200230}
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200233/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200235 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200236 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200239 const unsigned char *hash, size_t hash_len,
240 unsigned char *sig, size_t *sig_len,
241 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200244 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200245 const unsigned char *sig, size_t sig_len )
246{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200247 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200253 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200256
257 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200258}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200261 const unsigned char *hash, size_t hash_len,
262 unsigned char *sig, size_t *sig_len,
263 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
264{
265 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200271 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
272 f_rng, p_rng );
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200275
276 return( ret );
277}
278
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200279#if defined(MBEDTLS_ECP_RESTARTABLE)
280/* Forward declarations */
281static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
282 const unsigned char *hash, size_t hash_len,
283 const unsigned char *sig, size_t sig_len,
284 void *rs_ctx );
285
286static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
287 const unsigned char *hash, size_t hash_len,
288 unsigned char *sig, size_t *sig_len,
289 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
290 void *rs_ctx );
291
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200292/*
293 * Restart context for ECDSA operations with ECKEY context
294 *
295 * We need to store an actual ECDSA context, as we need to pass the same to
296 * the underlying ecdsa function, so we can't create it on the fly every time.
297 */
298typedef struct
299{
300 mbedtls_ecdsa_restart_ctx ecdsa_rs;
301 mbedtls_ecdsa_context ecdsa_ctx;
302} eckey_restart_ctx;
303
304static void *eckey_rs_alloc( void )
305{
306 eckey_restart_ctx *rs_ctx;
307
308 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
309
310 if( ctx != NULL )
311 {
312 rs_ctx = ctx;
313 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
314 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
315 }
316
317 return( ctx );
318}
319
320static void eckey_rs_free( void *ctx )
321{
322 eckey_restart_ctx *rs_ctx;
323
324 if( ctx == NULL)
325 return;
326
327 rs_ctx = ctx;
328 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
329 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
330
331 mbedtls_free( ctx );
332}
333
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200334static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
335 const unsigned char *hash, size_t hash_len,
336 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200337 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200338{
339 int ret;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200340 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200341
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200342 /* Should never happen */
343 if( rs == NULL )
344 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200345
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200346 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200347 if( rs->ecdsa_ctx.grp.pbits == 0 )
348 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200349
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200350 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
351 md_alg, hash, hash_len,
352 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200353
354cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200355 return( ret );
356}
357
358static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
359 const unsigned char *hash, size_t hash_len,
360 unsigned char *sig, size_t *sig_len,
361 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200362 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200363{
364 int ret;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200365 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200366
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200367 /* Should never happen */
368 if( rs == NULL )
369 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200370
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200371 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200372 if( rs->ecdsa_ctx.grp.pbits == 0 )
373 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200374
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200375 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
376 hash, hash_len, sig, sig_len,
377 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200378
379cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200380 return( ret );
381}
382#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200384
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100385static int eckey_check_pair( const void *pub, const void *prv )
386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
388 (const mbedtls_ecp_keypair *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100389}
390
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200391static void *eckey_alloc_wrap( void )
392{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200393 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200394
395 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200397
398 return( ctx );
399}
400
401static void eckey_free_wrap( void *ctx )
402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
404 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200405}
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200410 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200412}
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414const mbedtls_pk_info_t mbedtls_eckey_info = {
415 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200416 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200417 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200418 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200420 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200421 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200422#if defined(MBEDTLS_ECP_RESTARTABLE)
423 eckey_verify_rs_wrap,
424 eckey_sign_rs_wrap,
425#endif
426#else /* MBEDTLS_ECDSA_C */
427 NULL,
428 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200429#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200430 NULL,
431 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100432 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200433 eckey_alloc_wrap,
434 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200435#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200436 eckey_rs_alloc,
437 eckey_rs_free,
438#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200439 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200440};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200441
442/*
Paul Bakker75342a62014-04-08 17:35:40 +0200443 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200446{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 return( type == MBEDTLS_PK_ECKEY ||
448 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200449}
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451const mbedtls_pk_info_t mbedtls_eckeydh_info = {
452 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200453 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200454 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200455 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200456 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200457 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200458#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200459 NULL,
460 NULL,
461#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200462 NULL,
463 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100464 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200465 eckey_alloc_wrap, /* Same underlying key structure */
466 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200467#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200468 NULL,
469 NULL,
470#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200471 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200472};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#if defined(MBEDTLS_ECDSA_C)
476static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200477{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200479}
480
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400481#if defined(MBEDTLS_USE_PSA_CRYPTO)
482static psa_status_t mbedtls_psa_get_free_key_slot( psa_key_slot_t *key )
483{
484 for( psa_key_slot_t slot = 1; slot <= 32; slot++ )
485 {
486 if( psa_get_key_information( slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT )
487 {
488 *key = slot;
489 return( PSA_SUCCESS );
490 }
491 }
492 return( PSA_ERROR_INSUFFICIENT_MEMORY );
493}
494
495static psa_algorithm_t translate_md_to_psa( mbedtls_md_type_t md_alg )
496{
497 switch( md_alg )
498 {
499#if defined(MBEDTLS_MD2_C)
500 case MBEDTLS_MD_MD2:
501 return( PSA_ALG_MD2 );
502#endif
503#if defined(MBEDTLS_MD4_C)
504 case MBEDTLS_MD_MD4:
505 return( PSA_ALG_MD4 );
506#endif
507#if defined(MBEDTLS_MD5_C)
508 case MBEDTLS_MD_MD5:
509 return( PSA_ALG_MD5 );
510#endif
511#if defined(MBEDTLS_SHA1_C)
512 case MBEDTLS_MD_SHA1:
513 return( PSA_ALG_SHA_1 );
514#endif
515#if defined(MBEDTLS_SHA256_C)
516 case MBEDTLS_MD_SHA224:
517 return( PSA_ALG_SHA_224 );
518 case MBEDTLS_MD_SHA256:
519 return( PSA_ALG_SHA_256 );
520#endif
521#if defined(MBEDTLS_SHA512_C)
522 case MBEDTLS_MD_SHA384:
523 return( PSA_ALG_SHA_384 );
524 case MBEDTLS_MD_SHA512:
525 return( PSA_ALG_SHA_512 );
526#endif
527#if defined(MBEDTLS_RIPEMD160_C)
528 case MBEDTLS_MD_RIPEMD160:
529 return( PSA_ALG_RIPEMD160 );
530#endif
531 case MBEDTLS_MD_NONE: // Intentional fallthrough
532 default:
533 return( 0 );
534 }
535}
536
537/*
538 * Convert a signature from an ASN.1 sequence of two integers
539 * to a raw {r,s} buffer. Note: upon a successful call, the caller
540 * takes ownership of the sig->p buffer.
541 */
542static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
543 mbedtls_asn1_buf *sig )
544{
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500545 int ret, tag_type;
546 size_t len_signature, len_partial;
547
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400548 if( ( end - *p ) < 1 )
Andrzej Kurek6376d632018-11-06 08:50:04 -0500549 {
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400550 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
Andrzej Kurek6376d632018-11-06 08:50:04 -0500551 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
552 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400553 tag_type = **p;
554
Andrzej Kurek6376d632018-11-06 08:50:04 -0500555 if( ( ret = mbedtls_asn1_get_tag( p, end, &len_partial,
556 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
557 {
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400558 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
559 }
560
561 if( ( ret = mbedtls_asn1_get_tag( p, end, &len_partial, MBEDTLS_ASN1_INTEGER ) )
562 != 0 )
563 return( ret );
564
Andrzej Kurek6376d632018-11-06 08:50:04 -0500565 if( **p == '\0' )
566 {
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400567 ( *p )++;
568 len_partial--;
569 }
570
571 sig->p = mbedtls_calloc( 2, len_partial );
Andrzej Kurek6376d632018-11-06 08:50:04 -0500572 if( sig->p == NULL )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400573 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400574
575 memcpy( sig->p, *p, len_partial );
576 len_signature = len_partial;
577 ( *p ) += len_partial;
Andrzej Kurekea842332018-11-07 03:19:08 -0500578 if( ( ret = mbedtls_asn1_get_tag( p, end, &len_partial,
579 MBEDTLS_ASN1_INTEGER ) ) != 0 )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400580 {
581 mbedtls_free( sig->p );
Andrzej Kurekea842332018-11-07 03:19:08 -0500582 sig->p = NULL;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400583 return( ret );
584 }
585
Andrzej Kurek6376d632018-11-06 08:50:04 -0500586 if( **p == '\0' )
587 {
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400588 ( *p )++;
589 len_partial--;
590 }
591
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500592 // Check if both parts are of the same size
593 if( len_partial != len_signature )
594 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE );
595
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400596 memcpy( sig->p + len_partial, *p, len_partial );
597 len_signature += len_partial;
598 sig->tag = tag_type;
599 sig->len = len_signature;
600 ( *p ) += len_partial;
601 return( 0 );
602}
603
604static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
605{
606 switch( grpid )
607 {
608#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
609 case MBEDTLS_ECP_DP_SECP192R1:
610 return( PSA_ECC_CURVE_SECP192R1 );
611#endif
612#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
613 case MBEDTLS_ECP_DP_SECP224R1:
614 return( PSA_ECC_CURVE_SECP224R1 );
615#endif
616#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
617 case MBEDTLS_ECP_DP_SECP256R1:
618 return( PSA_ECC_CURVE_SECP256R1 );
619#endif
620#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
621 case MBEDTLS_ECP_DP_SECP384R1:
622 return( PSA_ECC_CURVE_SECP384R1 );
623#endif
624#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
625 case MBEDTLS_ECP_DP_SECP521R1:
626 return( PSA_ECC_CURVE_SECP521R1 );
627#endif
628#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
629 case MBEDTLS_ECP_DP_BP256R1:
630 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
631#endif
632#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
633 case MBEDTLS_ECP_DP_BP384R1:
634 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
635#endif
636#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
637 case MBEDTLS_ECP_DP_BP512R1:
638 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
639#endif
640#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
641 case MBEDTLS_ECP_DP_CURVE25519:
642 return( PSA_ECC_CURVE_CURVE25519 );
643#endif
644#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
645 case MBEDTLS_ECP_DP_SECP192K1:
646 return( PSA_ECC_CURVE_SECP192K1 );
647#endif
648#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
649 case MBEDTLS_ECP_DP_SECP224K1:
650 return( PSA_ECC_CURVE_SECP224K1 );
651#endif
652#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
653 case MBEDTLS_ECP_DP_SECP256K1:
654 return( PSA_ECC_CURVE_SECP256K1 );
655#endif
656#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
657 case MBEDTLS_ECP_DP_CURVE448:
658 return( PSA_ECC_CURVE_CURVE448 );
659#endif
660 default:
661 return( 0 );
662 }
663}
664
665static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
666 const unsigned char *hash, size_t hash_len,
667 const unsigned char *sig, size_t sig_len )
668{
669 int ret;
670 psa_key_slot_t key_slot;
671 psa_key_policy_t policy;
672 psa_key_type_t psa_type;
673 mbedtls_pk_context key;
674 mbedtls_asn1_buf signature;
675 int key_len;
Andrzej Kurek6376d632018-11-06 08:50:04 -0500676 const int buf_len = 30 + 2 * MBEDTLS_ECP_MAX_BYTES; // Equivalent of ECP_PUB_DER_MAX_BYTES
677 unsigned char buf[buf_len];
678 unsigned char *p = (unsigned char*) sig;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400679 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
680 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA( translate_md_to_psa( md_alg ) );
681 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa ( ( (mbedtls_ecdsa_context *) ctx )->grp.id );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400682
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500683 if( curve == 0 )
684 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
685
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400686 memset( &signature, 0, sizeof( mbedtls_asn1_buf ) );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400687 key.pk_info = &pk_info;
688 key.pk_ctx = ctx;
689 psa_crypto_init();
690
691 psa_type = PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve );
692
693 if( extract_ecdsa_sig( &p, p + sig_len, &signature ) != 0 )
Andrzej Kurekea842332018-11-07 03:19:08 -0500694 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400695
Andrzej Kurek6376d632018-11-06 08:50:04 -0500696 key_len = mbedtls_pk_write_pubkey_der( &key, buf, buf_len );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400697 if( key_len <= 0 )
698 {
699 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
700 goto cleanup;
701 }
702
703 if( mbedtls_psa_get_free_key_slot( &key_slot ) != PSA_SUCCESS )
704 {
705 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
706 goto cleanup;
707 }
708 psa_key_policy_init( &policy );
709 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, psa_sig_md );
710 if( psa_set_key_policy( key_slot, &policy ) != PSA_SUCCESS )
711 {
712 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
713 goto cleanup;
714 }
715
Andrzej Kurek6376d632018-11-06 08:50:04 -0500716 if( psa_import_key( key_slot, psa_type, buf+buf_len-key_len, key_len )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400717 != PSA_SUCCESS )
718 {
719 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
720 goto cleanup;
721 }
722
723 if( psa_asymmetric_verify( key_slot, psa_sig_md,
724 hash, hash_len,
725 signature.p, signature.len )
726 != PSA_SUCCESS )
727 {
728 psa_destroy_key( key_slot );
729 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
730 goto cleanup;
731 }
732 ret = 0;
733 psa_destroy_key( key_slot );
734
735 cleanup:
736 mbedtls_free( signature.p );
737 return( ret );
738}
739#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200741 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200742 const unsigned char *sig, size_t sig_len )
743{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200744 int ret;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200745 ((void) md_alg);
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200748 hash, hash_len, sig, sig_len );
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
751 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200752
753 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200754}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400755#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200758 const unsigned char *hash, size_t hash_len,
759 unsigned char *sig, size_t *sig_len,
760 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200763 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200764}
765
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200766#if defined(MBEDTLS_ECP_RESTARTABLE)
767static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
768 const unsigned char *hash, size_t hash_len,
769 const unsigned char *sig, size_t sig_len,
770 void *rs_ctx )
771{
772 int ret;
773 ((void) md_alg);
774
775 ret = mbedtls_ecdsa_read_signature_restartable(
776 (mbedtls_ecdsa_context *) ctx,
777 hash, hash_len, sig, sig_len,
778 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
779
780 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
781 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
782
783 return( ret );
784}
785
786static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
787 const unsigned char *hash, size_t hash_len,
788 unsigned char *sig, size_t *sig_len,
789 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
790 void *rs_ctx )
791{
792 return( mbedtls_ecdsa_write_signature_restartable(
793 (mbedtls_ecdsa_context *) ctx,
794 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
795 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
796
797}
798#endif /* MBEDTLS_ECP_RESTARTABLE */
799
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200800static void *ecdsa_alloc_wrap( void )
801{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200802 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200803
804 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200806
807 return( ctx );
808}
809
810static void ecdsa_free_wrap( void *ctx )
811{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
813 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200814}
815
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200816#if defined(MBEDTLS_ECP_RESTARTABLE)
817static void *ecdsa_rs_alloc( void )
818{
819 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
820
821 if( ctx != NULL )
822 mbedtls_ecdsa_restart_init( ctx );
823
824 return( ctx );
825}
826
827static void ecdsa_rs_free( void *ctx )
828{
829 mbedtls_ecdsa_restart_free( ctx );
830 mbedtls_free( ctx );
831}
832#endif /* MBEDTLS_ECP_RESTARTABLE */
833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834const mbedtls_pk_info_t mbedtls_ecdsa_info = {
835 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200836 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200837 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200838 ecdsa_can_do,
839 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200840 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200841#if defined(MBEDTLS_ECP_RESTARTABLE)
842 ecdsa_verify_rs_wrap,
843 ecdsa_sign_rs_wrap,
844#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200845 NULL,
846 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100847 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200848 ecdsa_alloc_wrap,
849 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200850#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200851 ecdsa_rs_alloc,
852 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200853#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200854 eckey_debug, /* Compatible key structures */
855};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200859/*
860 * Support for alternative RSA-private implementations
861 */
862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200864{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200866}
867
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200868static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200869{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200871
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200872 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200873}
874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200876 const unsigned char *hash, size_t hash_len,
877 unsigned char *sig, size_t *sig_len,
878 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200881
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100882#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000883 if( UINT_MAX < hash_len )
884 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100885#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000886
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200887 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200890 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200891}
892
893static int rsa_alt_decrypt_wrap( void *ctx,
894 const unsigned char *input, size_t ilen,
895 unsigned char *output, size_t *olen, size_t osize,
896 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200899
900 ((void) f_rng);
901 ((void) p_rng);
902
903 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200905
906 return( rsa_alt->decrypt_func( rsa_alt->key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200908}
909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100911static int rsa_alt_check_pair( const void *pub, const void *prv )
912{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100914 unsigned char hash[32];
915 size_t sig_len = 0;
916 int ret;
917
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200918 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100920
921 memset( hash, 0x2a, sizeof( hash ) );
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100924 hash, sizeof( hash ),
925 sig, &sig_len, NULL, NULL ) ) != 0 )
926 {
927 return( ret );
928 }
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100931 hash, sizeof( hash ), sig, sig_len ) != 0 )
932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100934 }
935
936 return( 0 );
937}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100939
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200940static void *rsa_alt_alloc_wrap( void )
941{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200942 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200943
944 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200946
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200947 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200948}
949
950static void rsa_alt_free_wrap( void *ctx )
951{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500952 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200954}
955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
957 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200958 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200959 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200960 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200961 NULL,
962 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200963#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200964 NULL,
965 NULL,
966#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200967 rsa_alt_decrypt_wrap,
968 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100970 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100971#else
972 NULL,
973#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200974 rsa_alt_alloc_wrap,
975 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200976#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200977 NULL,
978 NULL,
979#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200980 NULL,
981};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#endif /* MBEDTLS_PK_C */