blob: 21a7a33d82de88a7a41bdef0dcaba0a78dd19d01 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020056#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020057
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020058/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020060
Rich Evans00ab4702015-02-06 13:43:58 +000061#include <string.h>
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020065#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020069#endif
70
Andres Amaya Garciae32df082017-10-25 09:37:04 +010071#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050072#include "mbedtls/platform_util.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010073#endif
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020077#else
78#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020079#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#define mbedtls_free free
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020081#endif
82
Andres AG72849872017-01-19 11:24:33 +000083#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010084#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_RSA_C)
87static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 return( type == MBEDTLS_PK_RSA ||
90 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020091}
92
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +020093static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020094{
Hanno Becker6a1e7e52017-08-22 13:55:00 +010095 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
96 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020097}
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200100 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200101 const unsigned char *sig, size_t sig_len )
102{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200103 int ret;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100104 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
105 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200106
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100107#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000108 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
109 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100110#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000111
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100112 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200114
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100115 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 MBEDTLS_RSA_PUBLIC, md_alg,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200117 (unsigned int) hash_len, hash, sig ) ) != 0 )
118 return( ret );
119
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200120 /* The buffer contains a valid signature followed by extra data.
121 * We have a special error code for that so that so that callers can
122 * use mbedtls_pk_verify() to check "Does the buffer start with a
123 * valid signature?" and not just "Does the buffer contain a valid
124 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100125 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200127
128 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200129}
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200132 const unsigned char *hash, size_t hash_len,
133 unsigned char *sig, size_t *sig_len,
134 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
135{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100136 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
137
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100138#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000139 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
140 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100141#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000142
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100143 *sig_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200144
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100145 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200146 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200147}
148
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200149static int rsa_decrypt_wrap( void *ctx,
150 const unsigned char *input, size_t ilen,
151 unsigned char *output, size_t *olen, size_t osize,
152 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
153{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100154 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
155
156 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200158
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100159 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200161}
162
163static int rsa_encrypt_wrap( void *ctx,
164 const unsigned char *input, size_t ilen,
165 unsigned char *output, size_t *olen, size_t osize,
166 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
167{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100168 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
169 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200170
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100171 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100173
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100174 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
175 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200176}
177
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100178static int rsa_check_pair_wrap( const void *pub, const void *prv )
179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
181 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100182}
183
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200184static void *rsa_alloc_wrap( void )
185{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200186 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200187
188 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200190
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200191 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200192}
193
194static void rsa_free_wrap( void *ctx )
195{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
197 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200198}
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200203 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200205
206 items++;
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200209 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200211}
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213const mbedtls_pk_info_t mbedtls_rsa_info = {
214 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200215 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200216 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200217 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200218 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200219 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200220#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200221 NULL,
222 NULL,
223#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200224 rsa_decrypt_wrap,
225 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100226 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200227 rsa_alloc_wrap,
228 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200229#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200230 NULL,
231 NULL,
232#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200233 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200234};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200238/*
239 * Generic EC key
240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return( type == MBEDTLS_PK_ECKEY ||
244 type == MBEDTLS_PK_ECKEY_DH ||
245 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200246}
247
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200248static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200249{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200251}
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200254/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200256 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200257 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200260 const unsigned char *hash, size_t hash_len,
261 unsigned char *sig, size_t *sig_len,
262 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200265 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200266 const unsigned char *sig, size_t sig_len )
267{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200268 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200274 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200277
278 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200279}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200282 const unsigned char *hash, size_t hash_len,
283 unsigned char *sig, size_t *sig_len,
284 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
285{
286 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200292 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
293 f_rng, p_rng );
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200296
297 return( ret );
298}
299
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200300#if defined(MBEDTLS_ECP_RESTARTABLE)
301/* Forward declarations */
302static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
303 const unsigned char *hash, size_t hash_len,
304 const unsigned char *sig, size_t sig_len,
305 void *rs_ctx );
306
307static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
308 const unsigned char *hash, size_t hash_len,
309 unsigned char *sig, size_t *sig_len,
310 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
311 void *rs_ctx );
312
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200313/*
314 * Restart context for ECDSA operations with ECKEY context
315 *
316 * We need to store an actual ECDSA context, as we need to pass the same to
317 * the underlying ecdsa function, so we can't create it on the fly every time.
318 */
319typedef struct
320{
321 mbedtls_ecdsa_restart_ctx ecdsa_rs;
322 mbedtls_ecdsa_context ecdsa_ctx;
323} eckey_restart_ctx;
324
325static void *eckey_rs_alloc( void )
326{
327 eckey_restart_ctx *rs_ctx;
328
329 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
330
331 if( ctx != NULL )
332 {
333 rs_ctx = ctx;
334 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
335 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
336 }
337
338 return( ctx );
339}
340
341static void eckey_rs_free( void *ctx )
342{
343 eckey_restart_ctx *rs_ctx;
344
345 if( ctx == NULL)
346 return;
347
348 rs_ctx = ctx;
349 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
350 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
351
352 mbedtls_free( ctx );
353}
354
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200355static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
356 const unsigned char *hash, size_t hash_len,
357 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200358 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200359{
360 int ret;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200361 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200362
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200363 /* Should never happen */
364 if( rs == NULL )
365 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200366
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200367 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200368 if( rs->ecdsa_ctx.grp.pbits == 0 )
369 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200370
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200371 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
372 md_alg, hash, hash_len,
373 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200374
375cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200376 return( ret );
377}
378
379static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
380 const unsigned char *hash, size_t hash_len,
381 unsigned char *sig, size_t *sig_len,
382 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200383 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200384{
385 int ret;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200386 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200387
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200388 /* Should never happen */
389 if( rs == NULL )
390 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200391
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200392 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200393 if( rs->ecdsa_ctx.grp.pbits == 0 )
394 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200395
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200396 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
397 hash, hash_len, sig, sig_len,
398 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200399
400cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200401 return( ret );
402}
403#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200405
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100406static int eckey_check_pair( const void *pub, const void *prv )
407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
409 (const mbedtls_ecp_keypair *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100410}
411
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200412static void *eckey_alloc_wrap( void )
413{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200414 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200415
416 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200418
419 return( ctx );
420}
421
422static void eckey_free_wrap( void *ctx )
423{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
425 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200426}
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200429{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200431 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200433}
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435const mbedtls_pk_info_t mbedtls_eckey_info = {
436 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200437 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200438 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200439 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200441 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200442 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200443#if defined(MBEDTLS_ECP_RESTARTABLE)
444 eckey_verify_rs_wrap,
445 eckey_sign_rs_wrap,
446#endif
447#else /* MBEDTLS_ECDSA_C */
448 NULL,
449 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200450#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200451 NULL,
452 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100453 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200454 eckey_alloc_wrap,
455 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200456#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200457 eckey_rs_alloc,
458 eckey_rs_free,
459#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200460 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200461};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200462
463/*
Paul Bakker75342a62014-04-08 17:35:40 +0200464 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200467{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 return( type == MBEDTLS_PK_ECKEY ||
469 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200470}
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472const mbedtls_pk_info_t mbedtls_eckeydh_info = {
473 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200474 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200475 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200476 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200477 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200478 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200479#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200480 NULL,
481 NULL,
482#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200483 NULL,
484 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100485 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200486 eckey_alloc_wrap, /* Same underlying key structure */
487 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200488#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200489 NULL,
490 NULL,
491#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200492 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200493};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#if defined(MBEDTLS_ECDSA_C)
497static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200498{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200500}
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200503 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200504 const unsigned char *sig, size_t sig_len )
505{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200506 int ret;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200507 ((void) md_alg);
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200510 hash, hash_len, sig, sig_len );
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
513 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200514
515 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200516}
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200519 const unsigned char *hash, size_t hash_len,
520 unsigned char *sig, size_t *sig_len,
521 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
522{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200524 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200525}
526
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200527#if defined(MBEDTLS_ECP_RESTARTABLE)
528static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
529 const unsigned char *hash, size_t hash_len,
530 const unsigned char *sig, size_t sig_len,
531 void *rs_ctx )
532{
533 int ret;
534 ((void) md_alg);
535
536 ret = mbedtls_ecdsa_read_signature_restartable(
537 (mbedtls_ecdsa_context *) ctx,
538 hash, hash_len, sig, sig_len,
539 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
540
541 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
542 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
543
544 return( ret );
545}
546
547static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
548 const unsigned char *hash, size_t hash_len,
549 unsigned char *sig, size_t *sig_len,
550 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
551 void *rs_ctx )
552{
553 return( mbedtls_ecdsa_write_signature_restartable(
554 (mbedtls_ecdsa_context *) ctx,
555 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
556 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
557
558}
559#endif /* MBEDTLS_ECP_RESTARTABLE */
560
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200561static void *ecdsa_alloc_wrap( void )
562{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200563 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200564
565 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200567
568 return( ctx );
569}
570
571static void ecdsa_free_wrap( void *ctx )
572{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
574 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200575}
576
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200577#if defined(MBEDTLS_ECP_RESTARTABLE)
578static void *ecdsa_rs_alloc( void )
579{
580 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
581
582 if( ctx != NULL )
583 mbedtls_ecdsa_restart_init( ctx );
584
585 return( ctx );
586}
587
588static void ecdsa_rs_free( void *ctx )
589{
590 mbedtls_ecdsa_restart_free( ctx );
591 mbedtls_free( ctx );
592}
593#endif /* MBEDTLS_ECP_RESTARTABLE */
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595const mbedtls_pk_info_t mbedtls_ecdsa_info = {
596 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200597 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200598 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200599 ecdsa_can_do,
600 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200601 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200602#if defined(MBEDTLS_ECP_RESTARTABLE)
603 ecdsa_verify_rs_wrap,
604 ecdsa_sign_rs_wrap,
605#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200606 NULL,
607 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100608 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200609 ecdsa_alloc_wrap,
610 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200611#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200612 ecdsa_rs_alloc,
613 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200614#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200615 eckey_debug, /* Compatible key structures */
616};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200620/*
621 * Support for alternative RSA-private implementations
622 */
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200627}
628
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200629static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200630{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200632
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200633 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200634}
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200637 const unsigned char *hash, size_t hash_len,
638 unsigned char *sig, size_t *sig_len,
639 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200642
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100643#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000644 if( UINT_MAX < hash_len )
645 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100646#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000647
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200648 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200651 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200652}
653
654static int rsa_alt_decrypt_wrap( void *ctx,
655 const unsigned char *input, size_t ilen,
656 unsigned char *output, size_t *olen, size_t osize,
657 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
658{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200660
661 ((void) f_rng);
662 ((void) p_rng);
663
664 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200666
667 return( rsa_alt->decrypt_func( rsa_alt->key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200669}
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100672static int rsa_alt_check_pair( const void *pub, const void *prv )
673{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100675 unsigned char hash[32];
676 size_t sig_len = 0;
677 int ret;
678
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200679 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100681
682 memset( hash, 0x2a, sizeof( hash ) );
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100685 hash, sizeof( hash ),
686 sig, &sig_len, NULL, NULL ) ) != 0 )
687 {
688 return( ret );
689 }
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100692 hash, sizeof( hash ), sig, sig_len ) != 0 )
693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100695 }
696
697 return( 0 );
698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100700
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200701static void *rsa_alt_alloc_wrap( void )
702{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200703 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200704
705 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200707
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200708 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200709}
710
711static void rsa_alt_free_wrap( void *ctx )
712{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500713 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200715}
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
718 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200719 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200720 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200721 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200722 NULL,
723 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200724#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200725 NULL,
726 NULL,
727#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200728 rsa_alt_decrypt_wrap,
729 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100731 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100732#else
733 NULL,
734#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200735 rsa_alt_alloc_wrap,
736 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200737#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200738 NULL,
739 NULL,
740#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200741 NULL,
742};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#endif /* MBEDTLS_PK_C */