blob: 393fdebe02cd43928b065825bb4cf73e7e1a95ed [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)
Gilles Peskine5cc7bc52017-11-03 11:58:25 +010029#include "mbedtls/pk_info.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020030#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020031
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020032/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.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>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010054#include <stdint.h>
Andres AG72849872017-01-19 11:24:33 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Paul Bakker34617722014-06-13 17:20:13 +020057/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020059 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
60}
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +020061#endif
Paul Bakker34617722014-06-13 17:20:13 +020062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_RSA_C)
Gilles Peskine373deea2017-10-26 12:03:35 +020064static int rsa_can_do( const void *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020065{
Gilles Peskine373deea2017-10-26 12:03:35 +020066 (void) ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 return( type == MBEDTLS_PK_RSA ||
68 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020069}
70
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +020071static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020072{
Hanno Becker6a1e7e52017-08-22 13:55:00 +010073 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
74 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020075}
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020078 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020079 const unsigned char *sig, size_t sig_len )
80{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020081 int ret;
Hanno Becker6a1e7e52017-08-22 13:55:00 +010082 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
83 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020084
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010085#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +000086 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
87 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010088#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +000089
Hanno Becker6a1e7e52017-08-22 13:55:00 +010090 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020092
Hanno Becker6a1e7e52017-08-22 13:55:00 +010093 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 MBEDTLS_RSA_PUBLIC, md_alg,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020095 (unsigned int) hash_len, hash, sig ) ) != 0 )
96 return( ret );
97
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
Gilles Peskine02768b42017-11-03 19:20:27 +0100151static int rsa_check_pair_wrap( const mbedtls_pk_context *pub, const void *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100152{
Gilles Peskine02768b42017-11-03 19:20:27 +0100153 return( mbedtls_rsa_check_pub_priv( pub->pk_ctx, prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100154}
155
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200156static void *rsa_alloc_wrap( void )
157{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200158 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200159
160 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200162
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200163 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200164}
165
166static void rsa_free_wrap( void *ctx )
167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
169 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200170}
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200173{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200175 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200177
178 items++;
179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200181 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200183}
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185const mbedtls_pk_info_t mbedtls_rsa_info = {
186 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200187 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200188 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200189 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200190 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200191 rsa_sign_wrap,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200192 rsa_decrypt_wrap,
193 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100194 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200195 rsa_alloc_wrap,
196 rsa_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200197 rsa_debug,
Gilles Peskinecd062d82017-11-02 17:16:43 +0100198 NULL,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200199};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200203/*
204 * Generic EC key
205 */
Gilles Peskine373deea2017-10-26 12:03:35 +0200206static int eckey_can_do( const void *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200207{
Gilles Peskine373deea2017-10-26 12:03:35 +0200208 (void) ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 return( type == MBEDTLS_PK_ECKEY ||
210 type == MBEDTLS_PK_ECKEY_DH ||
211 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200212}
213
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200214static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200215{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200217}
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200220/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200222 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200223 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200226 const unsigned char *hash, size_t hash_len,
227 unsigned char *sig, size_t *sig_len,
228 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200231 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200232 const unsigned char *sig, size_t sig_len )
233{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200234 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200240 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200243
244 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200245}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200248 const unsigned char *hash, size_t hash_len,
249 unsigned char *sig, size_t *sig_len,
250 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
251{
252 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200258 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
259 f_rng, p_rng );
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200262
263 return( ret );
264}
265
Gilles Peskinecd062d82017-11-02 17:16:43 +0100266static size_t ecdsa_signature_size( const void *ctx_arg )
267{
268 const mbedtls_ecp_keypair *ctx = ctx_arg;
269 return( MBEDTLS_ECDSA_MAX_SIG_LEN( ctx->grp.pbits ) );
270}
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200273
Gilles Peskine02768b42017-11-03 19:20:27 +0100274static int eckey_check_pair( const mbedtls_pk_context *pub, const void *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100275{
Gilles Peskine02768b42017-11-03 19:20:27 +0100276 return( mbedtls_ecp_check_pub_priv( pub->pk_ctx, prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100277}
278
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200279static void *eckey_alloc_wrap( void )
280{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200281 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200282
283 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200285
286 return( ctx );
287}
288
289static void eckey_free_wrap( void *ctx )
290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
292 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200293}
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200296{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200298 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200300}
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302const mbedtls_pk_info_t mbedtls_eckey_info = {
303 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200304 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200305 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200306 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200308 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200309 eckey_sign_wrap,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200310#else
311 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200312 NULL,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200313#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200314 NULL,
315 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100316 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200317 eckey_alloc_wrap,
318 eckey_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200319 eckey_debug,
Gilles Peskinecd062d82017-11-02 17:16:43 +0100320#if defined(MBEDTLS_ECDSA_C)
321 ecdsa_signature_size,
322#else
323 NULL,
324#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200325};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200326
327/*
Paul Bakker75342a62014-04-08 17:35:40 +0200328 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200329 */
Gilles Peskine373deea2017-10-26 12:03:35 +0200330static int eckeydh_can_do( const void *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200331{
Gilles Peskine373deea2017-10-26 12:03:35 +0200332 (void) ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 return( type == MBEDTLS_PK_ECKEY ||
334 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200335}
336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337const mbedtls_pk_info_t mbedtls_eckeydh_info = {
338 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200339 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200340 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200341 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200342 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200343 NULL,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200344 NULL,
345 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100346 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200347 eckey_alloc_wrap, /* Same underlying key structure */
348 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200349 eckey_debug, /* Same underlying key structure */
Gilles Peskinecd062d82017-11-02 17:16:43 +0100350 NULL,
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200351};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine373deea2017-10-26 12:03:35 +0200355static int ecdsa_can_do( const void *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200356{
Gilles Peskine373deea2017-10-26 12:03:35 +0200357 (void) ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200359}
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200362 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200363 const unsigned char *sig, size_t sig_len )
364{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200365 int ret;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200366 ((void) md_alg);
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200369 hash, hash_len, sig, sig_len );
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
372 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200373
374 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200375}
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200378 const unsigned char *hash, size_t hash_len,
379 unsigned char *sig, size_t *sig_len,
380 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200383 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200384}
385
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200386static void *ecdsa_alloc_wrap( void )
387{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200388 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200389
390 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200392
393 return( ctx );
394}
395
396static void ecdsa_free_wrap( void *ctx )
397{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
399 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200400}
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402const mbedtls_pk_info_t mbedtls_ecdsa_info = {
403 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200404 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200405 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200406 ecdsa_can_do,
407 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200408 ecdsa_sign_wrap,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200409 NULL,
410 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100411 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200412 ecdsa_alloc_wrap,
413 ecdsa_free_wrap,
414 eckey_debug, /* Compatible key structures */
Gilles Peskinecd062d82017-11-02 17:16:43 +0100415 ecdsa_signature_size,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200416};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200420/*
421 * Support for alternative RSA-private implementations
422 */
423
Gilles Peskine373deea2017-10-26 12:03:35 +0200424static int rsa_alt_can_do( const void *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200425{
Gilles Peskine373deea2017-10-26 12:03:35 +0200426 (void) ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200428}
429
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200430static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200431{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200433
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200434 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200435}
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200438 const unsigned char *hash, size_t hash_len,
439 unsigned char *sig, size_t *sig_len,
440 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
441{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200443
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100444#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000445 if( UINT_MAX < hash_len )
446 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100447#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000448
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200449 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200452 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200453}
454
455static int rsa_alt_decrypt_wrap( void *ctx,
456 const unsigned char *input, size_t ilen,
457 unsigned char *output, size_t *olen, size_t osize,
458 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200461
462 ((void) f_rng);
463 ((void) p_rng);
464
465 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200467
468 return( rsa_alt->decrypt_func( rsa_alt->key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200470}
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#if defined(MBEDTLS_RSA_C)
Gilles Peskine02768b42017-11-03 19:20:27 +0100473static int rsa_alt_check_pair( const mbedtls_pk_context *pub, const void *prv )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100474{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100476 unsigned char hash[32];
477 size_t sig_len = 0;
478 int ret;
479
Gilles Peskine02768b42017-11-03 19:20:27 +0100480 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub->pk_ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100482
483 memset( hash, 0x2a, sizeof( hash ) );
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100486 hash, sizeof( hash ),
487 sig, &sig_len, NULL, NULL ) ) != 0 )
488 {
489 return( ret );
490 }
491
Gilles Peskine02768b42017-11-03 19:20:27 +0100492 if( rsa_verify_wrap( pub->pk_ctx, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100493 hash, sizeof( hash ), sig, sig_len ) != 0 )
494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100496 }
497
498 return( 0 );
499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100501
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200502static void *rsa_alt_alloc_wrap( void )
503{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200504 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200505
506 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200508
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200509 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200510}
511
512static void rsa_alt_free_wrap( void *ctx )
513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
515 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200516}
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
519 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200520 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200521 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200522 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200523 NULL,
524 rsa_alt_sign_wrap,
525 rsa_alt_decrypt_wrap,
526 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100528 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100529#else
530 NULL,
531#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200532 rsa_alt_alloc_wrap,
533 rsa_alt_free_wrap,
534 NULL,
Gilles Peskinecd062d82017-11-02 17:16:43 +0100535 NULL,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200536};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#endif /* MBEDTLS_PK_C */