blob: 0f935b2ad900fb0042eee27dfe40fb8e4810a7c2 [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"
Andres AG72849872017-01-19 11:24:33 +000033#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020047#else
48#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020049#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#define mbedtls_free free
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020051#endif
52
Andres AG72849872017-01-19 11:24:33 +000053#include <limits.h>
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Paul Bakker34617722014-06-13 17:20:13 +020056/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020058 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
59}
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +020060#endif
Paul Bakker34617722014-06-13 17:20:13 +020061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_RSA_C)
63static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 return( type == MBEDTLS_PK_RSA ||
66 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020067}
68
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +020069static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 return( 8 * ((const mbedtls_rsa_context *) ctx)->len );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020072}
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020075 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020076 const unsigned char *sig, size_t sig_len )
77{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020078 int ret;
79
Andres AG72849872017-01-19 11:24:33 +000080#if defined(MBEDTLS_HAVE_INT64)
81 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
82 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
83#endif /* MBEDTLS_HAVE_INT64 */
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 if( sig_len < ((mbedtls_rsa_context *) ctx)->len )
86 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 if( ( ret = mbedtls_rsa_pkcs1_verify( (mbedtls_rsa_context *) ctx, NULL, NULL,
89 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 if( sig_len > ((mbedtls_rsa_context *) ctx)->len )
94 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020095
96 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020097}
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200100 const unsigned char *hash, size_t hash_len,
101 unsigned char *sig, size_t *sig_len,
102 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
103{
Andres AG72849872017-01-19 11:24:33 +0000104#if defined(MBEDTLS_HAVE_INT64)
105 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
106 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
107#endif /* MBEDTLS_HAVE_INT64 */
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 *sig_len = ((mbedtls_rsa_context *) ctx)->len;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200112 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200113}
114
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200115static int rsa_decrypt_wrap( void *ctx,
116 const unsigned char *input, size_t ilen,
117 unsigned char *output, size_t *olen, size_t osize,
118 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( ilen != ((mbedtls_rsa_context *) ctx)->len )
121 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 return( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx, f_rng, p_rng,
124 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200125}
126
127static int rsa_encrypt_wrap( void *ctx,
128 const unsigned char *input, size_t ilen,
129 unsigned char *output, size_t *olen, size_t osize,
130 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 *olen = ((mbedtls_rsa_context *) ctx)->len;
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200133
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100134 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 return( mbedtls_rsa_pkcs1_encrypt( (mbedtls_rsa_context *) ctx,
138 f_rng, p_rng, MBEDTLS_RSA_PUBLIC, ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200139}
140
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100141static int rsa_check_pair_wrap( const void *pub, const void *prv )
142{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
144 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100145}
146
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200147static void *rsa_alloc_wrap( void )
148{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200149 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200150
151 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200153
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200154 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200155}
156
157static void rsa_free_wrap( void *ctx )
158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
160 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200161}
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200166 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200168
169 items++;
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200172 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200174}
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176const mbedtls_pk_info_t mbedtls_rsa_info = {
177 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200178 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200179 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200180 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200181 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200182 rsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200183#if defined(MBEDTLS_ECP_RESTARTABLE)
184 NULL,
185 NULL,
186#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200187 rsa_decrypt_wrap,
188 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100189 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200190 rsa_alloc_wrap,
191 rsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200192#if defined(MBEDTLS_ECP_RESTARTABLE)
193 NULL,
194 NULL,
195#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200196 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200197};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200201/*
202 * Generic EC key
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 return( type == MBEDTLS_PK_ECKEY ||
207 type == MBEDTLS_PK_ECKEY_DH ||
208 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200209}
210
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200211static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200214}
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200217/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200219 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200220 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200223 const unsigned char *hash, size_t hash_len,
224 unsigned char *sig, size_t *sig_len,
225 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200228 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200229 const unsigned char *sig, size_t sig_len )
230{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200231 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200237 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200240
241 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200242}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200245 const unsigned char *hash, size_t hash_len,
246 unsigned char *sig, size_t *sig_len,
247 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
248{
249 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200255 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
256 f_rng, p_rng );
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200259
260 return( ret );
261}
262
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200263#if defined(MBEDTLS_ECP_RESTARTABLE)
264/* Forward declarations */
265static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
266 const unsigned char *hash, size_t hash_len,
267 const unsigned char *sig, size_t sig_len,
268 void *rs_ctx );
269
270static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
271 const unsigned char *hash, size_t hash_len,
272 unsigned char *sig, size_t *sig_len,
273 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
274 void *rs_ctx );
275
276static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
277 const unsigned char *hash, size_t hash_len,
278 const unsigned char *sig, size_t sig_len,
279 void *p_rs_ctx )
280{
281 int ret;
282 mbedtls_ecdsa_context ecdsa, *p_ecdsa = &ecdsa;
283 mbedtls_ecdsa_restart_ctx *rs_ctx = p_rs_ctx;
284
285 mbedtls_ecdsa_init( &ecdsa );
286
287 /* set up our own sub-context if needed */
288 if( mbedtls_ecp_restart_enabled() &&
289 rs_ctx != NULL && rs_ctx->ecdsa == NULL )
290 {
291 rs_ctx->ecdsa = mbedtls_calloc( 1, sizeof( *rs_ctx->ecdsa ) );
292 if( rs_ctx->ecdsa == NULL )
293 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
294
295 mbedtls_ecdsa_init( rs_ctx->ecdsa );
296 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( rs_ctx->ecdsa, ctx ) );
297 }
298
299 if( rs_ctx != NULL && rs_ctx->ecdsa != NULL )
300 {
301 /* redirect to our context */
302 p_ecdsa = rs_ctx->ecdsa;
303 }
304 else
305 {
306 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( p_ecdsa, ctx ) );
307 }
308
309 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( p_ecdsa, md_alg, hash, hash_len,
310 sig, sig_len, rs_ctx ) );
311
312cleanup:
313 /* clear our sub-context when not in progress (done or error) */
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200314 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
315 {
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200316 mbedtls_ecdsa_free( rs_ctx->ecdsa );
317 mbedtls_free( rs_ctx->ecdsa );
318 rs_ctx->ecdsa = NULL;
319 }
320
321 mbedtls_ecdsa_free( &ecdsa );
322
323 return( ret );
324}
325
326static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
327 const unsigned char *hash, size_t hash_len,
328 unsigned char *sig, size_t *sig_len,
329 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
330 void *p_rs_ctx )
331{
332 int ret;
333 mbedtls_ecdsa_context ecdsa, *p_ecdsa = &ecdsa;
334 mbedtls_ecdsa_restart_ctx *rs_ctx = p_rs_ctx;
335
336 mbedtls_ecdsa_init( &ecdsa );
337
338 /* set up our own sub-context if needed */
339 if( mbedtls_ecp_restart_enabled() &&
340 rs_ctx != NULL && rs_ctx->ecdsa == NULL )
341 {
342 rs_ctx->ecdsa = mbedtls_calloc( 1, sizeof( *rs_ctx->ecdsa ) );
343 if( rs_ctx->ecdsa == NULL )
344 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
345
346 mbedtls_ecdsa_init( rs_ctx->ecdsa );
347 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( rs_ctx->ecdsa, ctx ) );
348 }
349
350 if( rs_ctx != NULL && rs_ctx->ecdsa != NULL )
351 {
352 /* redirect to our context */
353 p_ecdsa = rs_ctx->ecdsa;
354 }
355 else
356 {
357 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( p_ecdsa, ctx ) );
358 }
359
360 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( p_ecdsa, md_alg, hash, hash_len,
361 sig, sig_len, f_rng, p_rng, rs_ctx ) );
362
363cleanup:
364 /* clear our sub-context when not in progress (done or error) */
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200365 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
366 {
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200367 mbedtls_ecdsa_free( rs_ctx->ecdsa );
368 mbedtls_free( rs_ctx->ecdsa );
369 rs_ctx->ecdsa = NULL;
370 }
371
372 mbedtls_ecdsa_free( &ecdsa );
373
374 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é-Gonnard0bbc66c2017-08-18 16:22:06 +0200408#if defined(MBEDTLS_ECP_RESTARTABLE)
409static void *eckey_rs_alloc( void )
410{
411 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
412
413 if( ctx != NULL )
414 mbedtls_ecdsa_restart_init( ctx );
415
416 return( ctx );
417}
418
419static void eckey_rs_free( void *ctx )
420{
421 mbedtls_ecdsa_restart_free( ctx );
422 mbedtls_free( ctx );
423}
424#endif /* MBEDTLS_ECP_RESTARTABLE */
425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426const mbedtls_pk_info_t mbedtls_eckey_info = {
427 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200428 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200429 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200430 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200432 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200433 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200434#if defined(MBEDTLS_ECP_RESTARTABLE)
435 eckey_verify_rs_wrap,
436 eckey_sign_rs_wrap,
437#endif
438#else /* MBEDTLS_ECDSA_C */
439 NULL,
440 NULL,
441#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200442 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200443 NULL,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200444#endif
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200445#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200446 NULL,
447 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100448 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200449 eckey_alloc_wrap,
450 eckey_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200451#if defined(MBEDTLS_ECP_RESTARTABLE)
452 eckey_rs_alloc,
453 eckey_rs_free,
454#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200455 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200456};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200457
458/*
Paul Bakker75342a62014-04-08 17:35:40 +0200459 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200462{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 return( type == MBEDTLS_PK_ECKEY ||
464 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200465}
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467const mbedtls_pk_info_t mbedtls_eckeydh_info = {
468 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200469 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200470 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200471 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200472 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200473 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200474#if defined(MBEDTLS_ECP_RESTARTABLE)
475 NULL,
476 NULL,
477#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200478 NULL,
479 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100480 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200481 eckey_alloc_wrap, /* Same underlying key structure */
482 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200483#if defined(MBEDTLS_ECP_RESTARTABLE)
484 NULL,
485 NULL,
486#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200487 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200488};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#if defined(MBEDTLS_ECDSA_C)
492static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200493{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200495}
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200498 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200499 const unsigned char *sig, size_t sig_len )
500{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200501 int ret;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200502 ((void) md_alg);
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200505 hash, hash_len, sig, sig_len );
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
508 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200509
510 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200511}
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200514 const unsigned char *hash, size_t hash_len,
515 unsigned char *sig, size_t *sig_len,
516 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200519 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200520}
521
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200522#if defined(MBEDTLS_ECP_RESTARTABLE)
523static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
524 const unsigned char *hash, size_t hash_len,
525 const unsigned char *sig, size_t sig_len,
526 void *rs_ctx )
527{
528 int ret;
529 ((void) md_alg);
530
531 ret = mbedtls_ecdsa_read_signature_restartable(
532 (mbedtls_ecdsa_context *) ctx,
533 hash, hash_len, sig, sig_len,
534 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
535
536 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
537 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
538
539 return( ret );
540}
541
542static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
543 const unsigned char *hash, size_t hash_len,
544 unsigned char *sig, size_t *sig_len,
545 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
546 void *rs_ctx )
547{
548 return( mbedtls_ecdsa_write_signature_restartable(
549 (mbedtls_ecdsa_context *) ctx,
550 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
551 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
552
553}
554#endif /* MBEDTLS_ECP_RESTARTABLE */
555
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200556static void *ecdsa_alloc_wrap( void )
557{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200558 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200559
560 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200562
563 return( ctx );
564}
565
566static void ecdsa_free_wrap( void *ctx )
567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
569 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200570}
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572const mbedtls_pk_info_t mbedtls_ecdsa_info = {
573 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200574 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200575 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200576 ecdsa_can_do,
577 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200578 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200579#if defined(MBEDTLS_ECP_RESTARTABLE)
580 ecdsa_verify_rs_wrap,
581 ecdsa_sign_rs_wrap,
582#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200583 NULL,
584 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100585 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200586 ecdsa_alloc_wrap,
587 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200588#if defined(MBEDTLS_ECP_RESTARTABLE)
589 eckey_rs_alloc,
590 eckey_rs_free,
591#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200592 eckey_debug, /* Compatible key structures */
593};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200597/*
598 * Support for alternative RSA-private implementations
599 */
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200604}
605
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200606static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200607{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200609
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200610 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200611}
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200614 const unsigned char *hash, size_t hash_len,
615 unsigned char *sig, size_t *sig_len,
616 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
617{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200619
Andres AG72849872017-01-19 11:24:33 +0000620#if defined(MBEDTLS_HAVE_INT64)
621 if( UINT_MAX < hash_len )
622 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
623#endif /* MBEDTLS_HAVE_INT64 */
624
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200625 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200628 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200629}
630
631static int rsa_alt_decrypt_wrap( void *ctx,
632 const unsigned char *input, size_t ilen,
633 unsigned char *output, size_t *olen, size_t osize,
634 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200637
638 ((void) f_rng);
639 ((void) p_rng);
640
641 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200643
644 return( rsa_alt->decrypt_func( rsa_alt->key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200646}
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100649static int rsa_alt_check_pair( const void *pub, const void *prv )
650{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100652 unsigned char hash[32];
653 size_t sig_len = 0;
654 int ret;
655
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200656 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100658
659 memset( hash, 0x2a, sizeof( hash ) );
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100662 hash, sizeof( hash ),
663 sig, &sig_len, NULL, NULL ) ) != 0 )
664 {
665 return( ret );
666 }
667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100669 hash, sizeof( hash ), sig, sig_len ) != 0 )
670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100672 }
673
674 return( 0 );
675}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100677
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200678static void *rsa_alt_alloc_wrap( void )
679{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200680 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200681
682 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200684
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200685 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200686}
687
688static void rsa_alt_free_wrap( void *ctx )
689{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
691 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200692}
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
695 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200696 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200697 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200698 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200699 NULL,
700 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200701#if defined(MBEDTLS_ECP_RESTARTABLE)
702 NULL,
703 NULL,
704#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200705 rsa_alt_decrypt_wrap,
706 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100708 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100709#else
710 NULL,
711#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200712 rsa_alt_alloc_wrap,
713 rsa_alt_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200714#if defined(MBEDTLS_ECP_RESTARTABLE)
715 NULL,
716 NULL,
717#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200718 NULL,
719};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#endif /* MBEDTLS_PK_C */