blob: 4a74621fcde818e4dc924195c747a65d07f84af4 [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{
545 int ret;
546 size_t len_signature;
547 size_t len_partial;
548 int tag_type;
549 if( ( end - *p ) < 1 )
Andrzej Kurek6376d632018-11-06 08:50:04 -0500550 {
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400551 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
Andrzej Kurek6376d632018-11-06 08:50:04 -0500552 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
553 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400554 tag_type = **p;
555
Andrzej Kurek6376d632018-11-06 08:50:04 -0500556 if( ( ret = mbedtls_asn1_get_tag( p, end, &len_partial,
557 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
558 {
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400559 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
560 }
561
562 if( ( ret = mbedtls_asn1_get_tag( p, end, &len_partial, MBEDTLS_ASN1_INTEGER ) )
563 != 0 )
564 return( ret );
565
Andrzej Kurek6376d632018-11-06 08:50:04 -0500566 if( **p == '\0' )
567 {
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400568 ( *p )++;
569 len_partial--;
570 }
571
572 sig->p = mbedtls_calloc( 2, len_partial );
Andrzej Kurek6376d632018-11-06 08:50:04 -0500573 if( sig->p == NULL )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400574 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400575
576 memcpy( sig->p, *p, len_partial );
577 len_signature = len_partial;
578 ( *p ) += len_partial;
579 if( ( ret = mbedtls_asn1_get_tag( p, end, &len_partial, MBEDTLS_ASN1_INTEGER ) )
580 != 0 )
581 {
582 mbedtls_free( sig->p );
583 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
592 memcpy( sig->p + len_partial, *p, len_partial );
593 len_signature += len_partial;
594 sig->tag = tag_type;
595 sig->len = len_signature;
596 ( *p ) += len_partial;
597 return( 0 );
598}
599
600static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
601{
602 switch( grpid )
603 {
604#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
605 case MBEDTLS_ECP_DP_SECP192R1:
606 return( PSA_ECC_CURVE_SECP192R1 );
607#endif
608#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
609 case MBEDTLS_ECP_DP_SECP224R1:
610 return( PSA_ECC_CURVE_SECP224R1 );
611#endif
612#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
613 case MBEDTLS_ECP_DP_SECP256R1:
614 return( PSA_ECC_CURVE_SECP256R1 );
615#endif
616#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
617 case MBEDTLS_ECP_DP_SECP384R1:
618 return( PSA_ECC_CURVE_SECP384R1 );
619#endif
620#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
621 case MBEDTLS_ECP_DP_SECP521R1:
622 return( PSA_ECC_CURVE_SECP521R1 );
623#endif
624#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
625 case MBEDTLS_ECP_DP_BP256R1:
626 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
627#endif
628#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
629 case MBEDTLS_ECP_DP_BP384R1:
630 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
631#endif
632#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
633 case MBEDTLS_ECP_DP_BP512R1:
634 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
635#endif
636#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
637 case MBEDTLS_ECP_DP_CURVE25519:
638 return( PSA_ECC_CURVE_CURVE25519 );
639#endif
640#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
641 case MBEDTLS_ECP_DP_SECP192K1:
642 return( PSA_ECC_CURVE_SECP192K1 );
643#endif
644#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
645 case MBEDTLS_ECP_DP_SECP224K1:
646 return( PSA_ECC_CURVE_SECP224K1 );
647#endif
648#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
649 case MBEDTLS_ECP_DP_SECP256K1:
650 return( PSA_ECC_CURVE_SECP256K1 );
651#endif
652#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
653 case MBEDTLS_ECP_DP_CURVE448:
654 return( PSA_ECC_CURVE_CURVE448 );
655#endif
656 default:
657 return( 0 );
658 }
659}
660
661static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
662 const unsigned char *hash, size_t hash_len,
663 const unsigned char *sig, size_t sig_len )
664{
665 int ret;
666 psa_key_slot_t key_slot;
667 psa_key_policy_t policy;
668 psa_key_type_t psa_type;
669 mbedtls_pk_context key;
670 mbedtls_asn1_buf signature;
671 int key_len;
Andrzej Kurek6376d632018-11-06 08:50:04 -0500672 const int buf_len = 30 + 2 * MBEDTLS_ECP_MAX_BYTES; // Equivalent of ECP_PUB_DER_MAX_BYTES
673 unsigned char buf[buf_len];
674 unsigned char *p = (unsigned char*) sig;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400675 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
676 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA( translate_md_to_psa( md_alg ) );
677 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa ( ( (mbedtls_ecdsa_context *) ctx )->grp.id );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400678
679 memset( &signature, 0, sizeof( mbedtls_asn1_buf ) );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400680 key.pk_info = &pk_info;
681 key.pk_ctx = ctx;
682 psa_crypto_init();
683
684 psa_type = PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve );
685
686 if( extract_ecdsa_sig( &p, p + sig_len, &signature ) != 0 )
687 {
688 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
689 goto cleanup;
690 }
691
Andrzej Kurek6376d632018-11-06 08:50:04 -0500692 key_len = mbedtls_pk_write_pubkey_der( &key, buf, buf_len );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400693 if( key_len <= 0 )
694 {
695 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
696 goto cleanup;
697 }
698
699 if( mbedtls_psa_get_free_key_slot( &key_slot ) != PSA_SUCCESS )
700 {
701 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
702 goto cleanup;
703 }
704 psa_key_policy_init( &policy );
705 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, psa_sig_md );
706 if( psa_set_key_policy( key_slot, &policy ) != PSA_SUCCESS )
707 {
708 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
709 goto cleanup;
710 }
711
Andrzej Kurek6376d632018-11-06 08:50:04 -0500712 if( psa_import_key( key_slot, psa_type, buf+buf_len-key_len, key_len )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400713 != PSA_SUCCESS )
714 {
715 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
716 goto cleanup;
717 }
718
719 if( psa_asymmetric_verify( key_slot, psa_sig_md,
720 hash, hash_len,
721 signature.p, signature.len )
722 != PSA_SUCCESS )
723 {
724 psa_destroy_key( key_slot );
725 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
726 goto cleanup;
727 }
728 ret = 0;
729 psa_destroy_key( key_slot );
730
731 cleanup:
732 mbedtls_free( signature.p );
733 return( ret );
734}
735#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200737 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200738 const unsigned char *sig, size_t sig_len )
739{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200740 int ret;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200741 ((void) md_alg);
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200744 hash, hash_len, sig, sig_len );
745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
747 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200748
749 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200750}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400751#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200754 const unsigned char *hash, size_t hash_len,
755 unsigned char *sig, size_t *sig_len,
756 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
757{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200759 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200760}
761
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200762#if defined(MBEDTLS_ECP_RESTARTABLE)
763static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
764 const unsigned char *hash, size_t hash_len,
765 const unsigned char *sig, size_t sig_len,
766 void *rs_ctx )
767{
768 int ret;
769 ((void) md_alg);
770
771 ret = mbedtls_ecdsa_read_signature_restartable(
772 (mbedtls_ecdsa_context *) ctx,
773 hash, hash_len, sig, sig_len,
774 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
775
776 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
777 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
778
779 return( ret );
780}
781
782static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
783 const unsigned char *hash, size_t hash_len,
784 unsigned char *sig, size_t *sig_len,
785 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
786 void *rs_ctx )
787{
788 return( mbedtls_ecdsa_write_signature_restartable(
789 (mbedtls_ecdsa_context *) ctx,
790 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
791 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
792
793}
794#endif /* MBEDTLS_ECP_RESTARTABLE */
795
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200796static void *ecdsa_alloc_wrap( void )
797{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200798 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200799
800 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200802
803 return( ctx );
804}
805
806static void ecdsa_free_wrap( void *ctx )
807{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
809 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200810}
811
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200812#if defined(MBEDTLS_ECP_RESTARTABLE)
813static void *ecdsa_rs_alloc( void )
814{
815 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
816
817 if( ctx != NULL )
818 mbedtls_ecdsa_restart_init( ctx );
819
820 return( ctx );
821}
822
823static void ecdsa_rs_free( void *ctx )
824{
825 mbedtls_ecdsa_restart_free( ctx );
826 mbedtls_free( ctx );
827}
828#endif /* MBEDTLS_ECP_RESTARTABLE */
829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830const mbedtls_pk_info_t mbedtls_ecdsa_info = {
831 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200832 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200833 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200834 ecdsa_can_do,
835 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200836 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200837#if defined(MBEDTLS_ECP_RESTARTABLE)
838 ecdsa_verify_rs_wrap,
839 ecdsa_sign_rs_wrap,
840#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200841 NULL,
842 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100843 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200844 ecdsa_alloc_wrap,
845 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200846#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200847 ecdsa_rs_alloc,
848 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200849#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200850 eckey_debug, /* Compatible key structures */
851};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200855/*
856 * Support for alternative RSA-private implementations
857 */
858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200860{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200862}
863
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200864static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200865{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200867
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200868 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200869}
870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200872 const unsigned char *hash, size_t hash_len,
873 unsigned char *sig, size_t *sig_len,
874 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
875{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200877
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100878#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000879 if( UINT_MAX < hash_len )
880 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100881#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000882
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200883 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200886 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200887}
888
889static int rsa_alt_decrypt_wrap( void *ctx,
890 const unsigned char *input, size_t ilen,
891 unsigned char *output, size_t *olen, size_t osize,
892 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
893{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200895
896 ((void) f_rng);
897 ((void) p_rng);
898
899 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200901
902 return( rsa_alt->decrypt_func( rsa_alt->key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200904}
905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100907static int rsa_alt_check_pair( const void *pub, const void *prv )
908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100910 unsigned char hash[32];
911 size_t sig_len = 0;
912 int ret;
913
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200914 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100916
917 memset( hash, 0x2a, sizeof( hash ) );
918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100920 hash, sizeof( hash ),
921 sig, &sig_len, NULL, NULL ) ) != 0 )
922 {
923 return( ret );
924 }
925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100927 hash, sizeof( hash ), sig, sig_len ) != 0 )
928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100930 }
931
932 return( 0 );
933}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100935
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200936static void *rsa_alt_alloc_wrap( void )
937{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200938 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200939
940 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200942
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200943 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200944}
945
946static void rsa_alt_free_wrap( void *ctx )
947{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500948 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200950}
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
953 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200954 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200955 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200956 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200957 NULL,
958 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200959#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200960 NULL,
961 NULL,
962#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200963 rsa_alt_decrypt_wrap,
964 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100966 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100967#else
968 NULL,
969#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200970 rsa_alt_alloc_wrap,
971 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200972#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200973 NULL,
974 NULL,
975#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200976 NULL,
977};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#endif /* MBEDTLS_PK_C */