blob: 9ca5556725d9c96c99fd0aadda4f85d4e4a876e1 [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é-Gonnardaaa98142017-08-18 17:30:37 +0200183#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200184 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é-Gonnardaaa98142017-08-18 17:30:37 +0200192#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200193 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
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200276/*
277 * Restart context for ECDSA operations with ECKEY context
278 *
279 * We need to store an actual ECDSA context, as we need to pass the same to
280 * the underlying ecdsa function, so we can't create it on the fly every time.
281 */
282typedef struct
283{
284 mbedtls_ecdsa_restart_ctx ecdsa_rs;
285 mbedtls_ecdsa_context ecdsa_ctx;
286} eckey_restart_ctx;
287
288static void *eckey_rs_alloc( void )
289{
290 eckey_restart_ctx *rs_ctx;
291
292 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
293
294 if( ctx != NULL )
295 {
296 rs_ctx = ctx;
297 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
298 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
299 }
300
301 return( ctx );
302}
303
304static void eckey_rs_free( void *ctx )
305{
306 eckey_restart_ctx *rs_ctx;
307
308 if( ctx == NULL)
309 return;
310
311 rs_ctx = ctx;
312 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
313 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
314
315 mbedtls_free( ctx );
316}
317
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200318static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
319 const unsigned char *hash, size_t hash_len,
320 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200321 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200322{
323 int ret;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200324 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200325
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200326 /* Should never happen */
327 if( rs == NULL )
328 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200329
330 /* set up our own sub-context if needed */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200331 if( rs->ecdsa_ctx.grp.pbits == 0 )
332 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200333
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200334 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
335 md_alg, hash, hash_len,
336 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200337
338cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200339 return( ret );
340}
341
342static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
343 const unsigned char *hash, size_t hash_len,
344 unsigned char *sig, size_t *sig_len,
345 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200346 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200347{
348 int ret;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200349 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200350
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200351 /* Should never happen */
352 if( rs == NULL )
353 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200354
355 /* set up our own sub-context if needed */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200356 if( rs->ecdsa_ctx.grp.pbits == 0 )
357 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200358
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200359 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
360 hash, hash_len, sig, sig_len,
361 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200362
363cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200364 return( ret );
365}
366#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200368
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100369static int eckey_check_pair( const void *pub, const void *prv )
370{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
372 (const mbedtls_ecp_keypair *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100373}
374
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200375static void *eckey_alloc_wrap( void )
376{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200377 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200378
379 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200381
382 return( ctx );
383}
384
385static void eckey_free_wrap( void *ctx )
386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
388 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200389}
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200392{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200394 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200396}
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398const mbedtls_pk_info_t mbedtls_eckey_info = {
399 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200400 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200401 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200402 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200404 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200405 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200406#if defined(MBEDTLS_ECP_RESTARTABLE)
407 eckey_verify_rs_wrap,
408 eckey_sign_rs_wrap,
409#endif
410#else /* MBEDTLS_ECDSA_C */
411 NULL,
412 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200413#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200414 NULL,
415 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100416 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200417 eckey_alloc_wrap,
418 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200419#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200420 eckey_rs_alloc,
421 eckey_rs_free,
422#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200423 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200424};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200425
426/*
Paul Bakker75342a62014-04-08 17:35:40 +0200427 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200430{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 return( type == MBEDTLS_PK_ECKEY ||
432 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200433}
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435const mbedtls_pk_info_t mbedtls_eckeydh_info = {
436 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200437 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200438 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200439 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200440 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200441 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200442#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200443 NULL,
444 NULL,
445#endif
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, /* Same underlying key structure */
450 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200451#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200452 NULL,
453 NULL,
454#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200455 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200456};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#if defined(MBEDTLS_ECDSA_C)
460static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200463}
464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200466 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200467 const unsigned char *sig, size_t sig_len )
468{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200469 int ret;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200470 ((void) md_alg);
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200473 hash, hash_len, sig, sig_len );
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
476 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200477
478 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200479}
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200482 const unsigned char *hash, size_t hash_len,
483 unsigned char *sig, size_t *sig_len,
484 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
485{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200487 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200488}
489
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200490#if defined(MBEDTLS_ECP_RESTARTABLE)
491static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
492 const unsigned char *hash, size_t hash_len,
493 const unsigned char *sig, size_t sig_len,
494 void *rs_ctx )
495{
496 int ret;
497 ((void) md_alg);
498
499 ret = mbedtls_ecdsa_read_signature_restartable(
500 (mbedtls_ecdsa_context *) ctx,
501 hash, hash_len, sig, sig_len,
502 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
503
504 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
505 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
506
507 return( ret );
508}
509
510static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
511 const unsigned char *hash, size_t hash_len,
512 unsigned char *sig, size_t *sig_len,
513 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
514 void *rs_ctx )
515{
516 return( mbedtls_ecdsa_write_signature_restartable(
517 (mbedtls_ecdsa_context *) ctx,
518 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
519 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
520
521}
522#endif /* MBEDTLS_ECP_RESTARTABLE */
523
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200524static void *ecdsa_alloc_wrap( void )
525{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200526 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200527
528 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200530
531 return( ctx );
532}
533
534static void ecdsa_free_wrap( void *ctx )
535{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
537 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200538}
539
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200540#if defined(MBEDTLS_ECP_RESTARTABLE)
541static void *ecdsa_rs_alloc( void )
542{
543 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
544
545 if( ctx != NULL )
546 mbedtls_ecdsa_restart_init( ctx );
547
548 return( ctx );
549}
550
551static void ecdsa_rs_free( void *ctx )
552{
553 mbedtls_ecdsa_restart_free( ctx );
554 mbedtls_free( ctx );
555}
556#endif /* MBEDTLS_ECP_RESTARTABLE */
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558const mbedtls_pk_info_t mbedtls_ecdsa_info = {
559 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200560 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200561 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200562 ecdsa_can_do,
563 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200564 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200565#if defined(MBEDTLS_ECP_RESTARTABLE)
566 ecdsa_verify_rs_wrap,
567 ecdsa_sign_rs_wrap,
568#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200569 NULL,
570 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100571 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200572 ecdsa_alloc_wrap,
573 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200574#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200575 ecdsa_rs_alloc,
576 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200577#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200578 eckey_debug, /* Compatible key structures */
579};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200583/*
584 * Support for alternative RSA-private implementations
585 */
586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200590}
591
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200592static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200593{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200595
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200596 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200597}
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200600 const unsigned char *hash, size_t hash_len,
601 unsigned char *sig, size_t *sig_len,
602 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
603{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200605
Andres AG72849872017-01-19 11:24:33 +0000606#if defined(MBEDTLS_HAVE_INT64)
607 if( UINT_MAX < hash_len )
608 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
609#endif /* MBEDTLS_HAVE_INT64 */
610
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200611 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200614 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200615}
616
617static int rsa_alt_decrypt_wrap( void *ctx,
618 const unsigned char *input, size_t ilen,
619 unsigned char *output, size_t *olen, size_t osize,
620 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
621{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200623
624 ((void) f_rng);
625 ((void) p_rng);
626
627 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200629
630 return( rsa_alt->decrypt_func( rsa_alt->key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200632}
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100635static int rsa_alt_check_pair( const void *pub, const void *prv )
636{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100638 unsigned char hash[32];
639 size_t sig_len = 0;
640 int ret;
641
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200642 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100644
645 memset( hash, 0x2a, sizeof( hash ) );
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100648 hash, sizeof( hash ),
649 sig, &sig_len, NULL, NULL ) ) != 0 )
650 {
651 return( ret );
652 }
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100655 hash, sizeof( hash ), sig, sig_len ) != 0 )
656 {
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
660 return( 0 );
661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100663
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200664static void *rsa_alt_alloc_wrap( void )
665{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200666 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200667
668 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200670
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200672}
673
674static void rsa_alt_free_wrap( void *ctx )
675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
677 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200678}
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
681 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200682 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200683 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200684 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200685 NULL,
686 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200687#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200688 NULL,
689 NULL,
690#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200691 rsa_alt_decrypt_wrap,
692 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100694 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100695#else
696 NULL,
697#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200698 rsa_alt_alloc_wrap,
699 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200700#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200701 NULL,
702 NULL,
703#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200704 NULL,
705};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#endif /* MBEDTLS_PK_C */