blob: 87806be337c2c9b4616581046ac22c9a703b41a4 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020050#else
51#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020052#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#define mbedtls_free free
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020054#endif
55
Andres AG72849872017-01-19 11:24:33 +000056#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010057#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_RSA_C)
60static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 return( type == MBEDTLS_PK_RSA ||
63 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020064}
65
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +020066static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020067{
Hanno Becker6a1e7e52017-08-22 13:55:00 +010068 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
69 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020070}
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020073 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020074 const unsigned char *sig, size_t sig_len )
75{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020076 int ret;
Hanno Becker6a1e7e52017-08-22 13:55:00 +010077 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
78 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020079
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010080#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +000081 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
82 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010083#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +000084
Hanno Becker6a1e7e52017-08-22 13:55:00 +010085 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020087
Hanno Becker6a1e7e52017-08-22 13:55:00 +010088 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 MBEDTLS_RSA_PUBLIC, md_alg,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020090 (unsigned int) hash_len, hash, sig ) ) != 0 )
91 return( ret );
92
Gilles Peskine5114d3e2018-03-30 07:12:15 +020093 /* The buffer contains a valid signature followed by extra data.
94 * We have a special error code for that so that so that callers can
95 * use mbedtls_pk_verify() to check "Does the buffer start with a
96 * valid signature?" and not just "Does the buffer contain a valid
97 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +010098 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200100
101 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200102}
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200105 const unsigned char *hash, size_t hash_len,
106 unsigned char *sig, size_t *sig_len,
107 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
108{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100109 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
110
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100111#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000112 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
113 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100114#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000115
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100116 *sig_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200117
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100118 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200119 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200120}
121
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200122static int rsa_decrypt_wrap( void *ctx,
123 const unsigned char *input, size_t ilen,
124 unsigned char *output, size_t *olen, size_t osize,
125 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
126{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100127 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
128
129 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200131
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100132 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200134}
135
136static int rsa_encrypt_wrap( void *ctx,
137 const unsigned char *input, size_t ilen,
138 unsigned char *output, size_t *olen, size_t osize,
139 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
140{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100141 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
142 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200143
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100144 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100146
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100147 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
148 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200149}
150
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100151static int rsa_check_pair_wrap( const void *pub, const void *prv )
152{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
154 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100155}
156
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200157static void *rsa_alloc_wrap( void )
158{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200159 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200160
161 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200163
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200164 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200165}
166
167static void rsa_free_wrap( void *ctx )
168{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
170 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200171}
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200176 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200178
179 items++;
180
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.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200184}
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186const mbedtls_pk_info_t mbedtls_rsa_info = {
187 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200188 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200189 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200190 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200191 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200192 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200193#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200194 NULL,
195 NULL,
196#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200197 rsa_decrypt_wrap,
198 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100199 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200200 rsa_alloc_wrap,
201 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200202#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200203 NULL,
204 NULL,
205#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200206 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200207};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200211/*
212 * Generic EC key
213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200215{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( type == MBEDTLS_PK_ECKEY ||
217 type == MBEDTLS_PK_ECKEY_DH ||
218 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200219}
220
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200221static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200222{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200224}
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200227/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200229 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200230 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200233 const unsigned char *hash, size_t hash_len,
234 unsigned char *sig, size_t *sig_len,
235 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200238 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200239 const unsigned char *sig, size_t sig_len )
240{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200241 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200247 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200250
251 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200252}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200255 const unsigned char *hash, size_t hash_len,
256 unsigned char *sig, size_t *sig_len,
257 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
258{
259 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200265 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
266 f_rng, p_rng );
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200269
270 return( ret );
271}
272
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200273#if defined(MBEDTLS_ECP_RESTARTABLE)
274/* Forward declarations */
275static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
276 const unsigned char *hash, size_t hash_len,
277 const unsigned char *sig, size_t sig_len,
278 void *rs_ctx );
279
280static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
281 const unsigned char *hash, size_t hash_len,
282 unsigned char *sig, size_t *sig_len,
283 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
284 void *rs_ctx );
285
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200286/*
287 * Restart context for ECDSA operations with ECKEY context
288 *
289 * We need to store an actual ECDSA context, as we need to pass the same to
290 * the underlying ecdsa function, so we can't create it on the fly every time.
291 */
292typedef struct
293{
294 mbedtls_ecdsa_restart_ctx ecdsa_rs;
295 mbedtls_ecdsa_context ecdsa_ctx;
296} eckey_restart_ctx;
297
298static void *eckey_rs_alloc( void )
299{
300 eckey_restart_ctx *rs_ctx;
301
302 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
303
304 if( ctx != NULL )
305 {
306 rs_ctx = ctx;
307 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
308 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
309 }
310
311 return( ctx );
312}
313
314static void eckey_rs_free( void *ctx )
315{
316 eckey_restart_ctx *rs_ctx;
317
318 if( ctx == NULL)
319 return;
320
321 rs_ctx = ctx;
322 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
323 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
324
325 mbedtls_free( ctx );
326}
327
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200328static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
329 const unsigned char *hash, size_t hash_len,
330 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200331 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200332{
333 int ret;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200334 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200335
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200336 /* Should never happen */
337 if( rs == NULL )
338 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200339
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200340 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200341 if( rs->ecdsa_ctx.grp.pbits == 0 )
342 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200343
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200344 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
345 md_alg, hash, hash_len,
346 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200347
348cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200349 return( ret );
350}
351
352static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
353 const unsigned char *hash, size_t hash_len,
354 unsigned char *sig, size_t *sig_len,
355 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200356 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200357{
358 int ret;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200359 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200360
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200361 /* Should never happen */
362 if( rs == NULL )
363 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200364
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200365 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200366 if( rs->ecdsa_ctx.grp.pbits == 0 )
367 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200368
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200369 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
370 hash, hash_len, sig, sig_len,
371 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200372
373cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200374 return( ret );
375}
376#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200378
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100379static int eckey_check_pair( const void *pub, const void *prv )
380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
382 (const mbedtls_ecp_keypair *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100383}
384
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200385static void *eckey_alloc_wrap( void )
386{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200387 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200388
389 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200391
392 return( ctx );
393}
394
395static void eckey_free_wrap( void *ctx )
396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
398 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200399}
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200404 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200406}
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408const mbedtls_pk_info_t mbedtls_eckey_info = {
409 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200410 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200411 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200412 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200414 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200415 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200416#if defined(MBEDTLS_ECP_RESTARTABLE)
417 eckey_verify_rs_wrap,
418 eckey_sign_rs_wrap,
419#endif
420#else /* MBEDTLS_ECDSA_C */
421 NULL,
422 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200423#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200424 NULL,
425 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100426 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200427 eckey_alloc_wrap,
428 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200429#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200430 eckey_rs_alloc,
431 eckey_rs_free,
432#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200433 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200434};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200435
436/*
Paul Bakker75342a62014-04-08 17:35:40 +0200437 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200440{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 return( type == MBEDTLS_PK_ECKEY ||
442 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200443}
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445const mbedtls_pk_info_t mbedtls_eckeydh_info = {
446 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200447 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200448 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200449 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200450 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200451 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200452#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200453 NULL,
454 NULL,
455#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200456 NULL,
457 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100458 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200459 eckey_alloc_wrap, /* Same underlying key structure */
460 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200461#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200462 NULL,
463 NULL,
464#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200465 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200466};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_ECDSA_C)
470static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200473}
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200476 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200477 const unsigned char *sig, size_t sig_len )
478{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200479 int ret;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200480 ((void) md_alg);
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200483 hash, hash_len, sig, sig_len );
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
486 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200487
488 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200489}
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200492 const unsigned char *hash, size_t hash_len,
493 unsigned char *sig, size_t *sig_len,
494 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
495{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200497 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200498}
499
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200500#if defined(MBEDTLS_ECP_RESTARTABLE)
501static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
502 const unsigned char *hash, size_t hash_len,
503 const unsigned char *sig, size_t sig_len,
504 void *rs_ctx )
505{
506 int ret;
507 ((void) md_alg);
508
509 ret = mbedtls_ecdsa_read_signature_restartable(
510 (mbedtls_ecdsa_context *) ctx,
511 hash, hash_len, sig, sig_len,
512 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
513
514 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
515 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
516
517 return( ret );
518}
519
520static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
521 const unsigned char *hash, size_t hash_len,
522 unsigned char *sig, size_t *sig_len,
523 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
524 void *rs_ctx )
525{
526 return( mbedtls_ecdsa_write_signature_restartable(
527 (mbedtls_ecdsa_context *) ctx,
528 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
529 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
530
531}
532#endif /* MBEDTLS_ECP_RESTARTABLE */
533
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200534static void *ecdsa_alloc_wrap( void )
535{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200536 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200537
538 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200540
541 return( ctx );
542}
543
544static void ecdsa_free_wrap( void *ctx )
545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
547 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200548}
549
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200550#if defined(MBEDTLS_ECP_RESTARTABLE)
551static void *ecdsa_rs_alloc( void )
552{
553 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
554
555 if( ctx != NULL )
556 mbedtls_ecdsa_restart_init( ctx );
557
558 return( ctx );
559}
560
561static void ecdsa_rs_free( void *ctx )
562{
563 mbedtls_ecdsa_restart_free( ctx );
564 mbedtls_free( ctx );
565}
566#endif /* MBEDTLS_ECP_RESTARTABLE */
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568const mbedtls_pk_info_t mbedtls_ecdsa_info = {
569 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200570 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200571 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200572 ecdsa_can_do,
573 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200574 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200575#if defined(MBEDTLS_ECP_RESTARTABLE)
576 ecdsa_verify_rs_wrap,
577 ecdsa_sign_rs_wrap,
578#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200579 NULL,
580 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100581 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200582 ecdsa_alloc_wrap,
583 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200584#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200585 ecdsa_rs_alloc,
586 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200587#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200588 eckey_debug, /* Compatible key structures */
589};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200593/*
594 * Support for alternative RSA-private implementations
595 */
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200600}
601
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200602static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200603{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200605
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200606 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200607}
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200610 const unsigned char *hash, size_t hash_len,
611 unsigned char *sig, size_t *sig_len,
612 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
613{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200615
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100616#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000617 if( UINT_MAX < hash_len )
618 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100619#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000620
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200621 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200624 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200625}
626
627static int rsa_alt_decrypt_wrap( void *ctx,
628 const unsigned char *input, size_t ilen,
629 unsigned char *output, size_t *olen, size_t osize,
630 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200633
634 ((void) f_rng);
635 ((void) p_rng);
636
637 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200639
640 return( rsa_alt->decrypt_func( rsa_alt->key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200642}
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100645static int rsa_alt_check_pair( const void *pub, const void *prv )
646{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100648 unsigned char hash[32];
649 size_t sig_len = 0;
650 int ret;
651
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200652 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100654
655 memset( hash, 0x2a, sizeof( hash ) );
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100658 hash, sizeof( hash ),
659 sig, &sig_len, NULL, NULL ) ) != 0 )
660 {
661 return( ret );
662 }
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100665 hash, sizeof( hash ), sig, sig_len ) != 0 )
666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100668 }
669
670 return( 0 );
671}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100673
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200674static void *rsa_alt_alloc_wrap( void )
675{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200676 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200677
678 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200680
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200681 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200682}
683
684static void rsa_alt_free_wrap( void *ctx )
685{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500686 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200688}
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
691 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200692 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200693 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200694 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200695 NULL,
696 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200697#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200698 NULL,
699 NULL,
700#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200701 rsa_alt_decrypt_wrap,
702 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100704 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100705#else
706 NULL,
707#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200708 rsa_alt_alloc_wrap,
709 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200710#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200711 NULL,
712 NULL,
713#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200714 NULL,
715};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719#endif /* MBEDTLS_PK_C */