Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /*
switches 0:5c4d7b2438d3 2 * Public Key abstraction layer: wrapper functions
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
switches 0:5c4d7b2438d3 5 * SPDX-License-Identifier: Apache-2.0
switches 0:5c4d7b2438d3 6 *
switches 0:5c4d7b2438d3 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
switches 0:5c4d7b2438d3 8 * not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 9 * You may obtain a copy of the License at
switches 0:5c4d7b2438d3 10 *
switches 0:5c4d7b2438d3 11 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 12 *
switches 0:5c4d7b2438d3 13 * Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
switches 0:5c4d7b2438d3 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 16 * See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 17 * limitations under the License.
switches 0:5c4d7b2438d3 18 *
switches 0:5c4d7b2438d3 19 * This file is part of mbed TLS (https://tls.mbed.org)
switches 0:5c4d7b2438d3 20 */
switches 0:5c4d7b2438d3 21
switches 0:5c4d7b2438d3 22 #if !defined(MBEDTLS_CONFIG_FILE)
switches 0:5c4d7b2438d3 23 #include "mbedtls/config.h"
switches 0:5c4d7b2438d3 24 #else
switches 0:5c4d7b2438d3 25 #include MBEDTLS_CONFIG_FILE
switches 0:5c4d7b2438d3 26 #endif
switches 0:5c4d7b2438d3 27
switches 0:5c4d7b2438d3 28 #if defined(MBEDTLS_PK_C)
switches 0:5c4d7b2438d3 29 #include "mbedtls/pk_internal.h"
switches 0:5c4d7b2438d3 30
switches 0:5c4d7b2438d3 31 /* Even if RSA not activated, for the sake of RSA-alt */
switches 0:5c4d7b2438d3 32 #include "mbedtls/rsa.h"
switches 0:5c4d7b2438d3 33
switches 0:5c4d7b2438d3 34 #include <string.h>
switches 0:5c4d7b2438d3 35
switches 0:5c4d7b2438d3 36 #if defined(MBEDTLS_ECP_C)
switches 0:5c4d7b2438d3 37 #include "mbedtls/ecp.h"
switches 0:5c4d7b2438d3 38 #endif
switches 0:5c4d7b2438d3 39
switches 0:5c4d7b2438d3 40 #if defined(MBEDTLS_ECDSA_C)
switches 0:5c4d7b2438d3 41 #include "mbedtls/ecdsa.h"
switches 0:5c4d7b2438d3 42 #endif
switches 0:5c4d7b2438d3 43
switches 0:5c4d7b2438d3 44 #if defined(MBEDTLS_PLATFORM_C)
switches 0:5c4d7b2438d3 45 #include "mbedtls/platform.h"
switches 0:5c4d7b2438d3 46 #else
switches 0:5c4d7b2438d3 47 #include <stdlib.h>
switches 0:5c4d7b2438d3 48 #define mbedtls_calloc calloc
switches 0:5c4d7b2438d3 49 #define mbedtls_free free
switches 0:5c4d7b2438d3 50 #endif
switches 0:5c4d7b2438d3 51
switches 0:5c4d7b2438d3 52 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
switches 0:5c4d7b2438d3 53 /* Implementation that should never be optimized out by the compiler */
switches 0:5c4d7b2438d3 54 static void mbedtls_zeroize( void *v, size_t n ) {
switches 0:5c4d7b2438d3 55 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
switches 0:5c4d7b2438d3 56 }
switches 0:5c4d7b2438d3 57 #endif
switches 0:5c4d7b2438d3 58
switches 0:5c4d7b2438d3 59 #if defined(MBEDTLS_RSA_C)
switches 0:5c4d7b2438d3 60 static int rsa_can_do( mbedtls_pk_type_t type )
switches 0:5c4d7b2438d3 61 {
switches 0:5c4d7b2438d3 62 return( type == MBEDTLS_PK_RSA ||
switches 0:5c4d7b2438d3 63 type == MBEDTLS_PK_RSASSA_PSS );
switches 0:5c4d7b2438d3 64 }
switches 0:5c4d7b2438d3 65
switches 0:5c4d7b2438d3 66 static size_t rsa_get_bitlen( const void *ctx )
switches 0:5c4d7b2438d3 67 {
switches 0:5c4d7b2438d3 68 return( 8 * ((const mbedtls_rsa_context *) ctx)->len );
switches 0:5c4d7b2438d3 69 }
switches 0:5c4d7b2438d3 70
switches 0:5c4d7b2438d3 71 static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
switches 0:5c4d7b2438d3 72 const unsigned char *hash, size_t hash_len,
switches 0:5c4d7b2438d3 73 const unsigned char *sig, size_t sig_len )
switches 0:5c4d7b2438d3 74 {
switches 0:5c4d7b2438d3 75 int ret;
switches 0:5c4d7b2438d3 76
switches 0:5c4d7b2438d3 77 if( sig_len < ((mbedtls_rsa_context *) ctx)->len )
switches 0:5c4d7b2438d3 78 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
switches 0:5c4d7b2438d3 79
switches 0:5c4d7b2438d3 80 if( ( ret = mbedtls_rsa_pkcs1_verify( (mbedtls_rsa_context *) ctx, NULL, NULL,
switches 0:5c4d7b2438d3 81 MBEDTLS_RSA_PUBLIC, md_alg,
switches 0:5c4d7b2438d3 82 (unsigned int) hash_len, hash, sig ) ) != 0 )
switches 0:5c4d7b2438d3 83 return( ret );
switches 0:5c4d7b2438d3 84
switches 0:5c4d7b2438d3 85 if( sig_len > ((mbedtls_rsa_context *) ctx)->len )
switches 0:5c4d7b2438d3 86 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
switches 0:5c4d7b2438d3 87
switches 0:5c4d7b2438d3 88 return( 0 );
switches 0:5c4d7b2438d3 89 }
switches 0:5c4d7b2438d3 90
switches 0:5c4d7b2438d3 91 static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
switches 0:5c4d7b2438d3 92 const unsigned char *hash, size_t hash_len,
switches 0:5c4d7b2438d3 93 unsigned char *sig, size_t *sig_len,
switches 0:5c4d7b2438d3 94 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
switches 0:5c4d7b2438d3 95 {
switches 0:5c4d7b2438d3 96 *sig_len = ((mbedtls_rsa_context *) ctx)->len;
switches 0:5c4d7b2438d3 97
switches 0:5c4d7b2438d3 98 return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
switches 0:5c4d7b2438d3 99 md_alg, (unsigned int) hash_len, hash, sig ) );
switches 0:5c4d7b2438d3 100 }
switches 0:5c4d7b2438d3 101
switches 0:5c4d7b2438d3 102 static int rsa_decrypt_wrap( void *ctx,
switches 0:5c4d7b2438d3 103 const unsigned char *input, size_t ilen,
switches 0:5c4d7b2438d3 104 unsigned char *output, size_t *olen, size_t osize,
switches 0:5c4d7b2438d3 105 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
switches 0:5c4d7b2438d3 106 {
switches 0:5c4d7b2438d3 107 if( ilen != ((mbedtls_rsa_context *) ctx)->len )
switches 0:5c4d7b2438d3 108 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
switches 0:5c4d7b2438d3 109
switches 0:5c4d7b2438d3 110 return( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx, f_rng, p_rng,
switches 0:5c4d7b2438d3 111 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
switches 0:5c4d7b2438d3 112 }
switches 0:5c4d7b2438d3 113
switches 0:5c4d7b2438d3 114 static int rsa_encrypt_wrap( void *ctx,
switches 0:5c4d7b2438d3 115 const unsigned char *input, size_t ilen,
switches 0:5c4d7b2438d3 116 unsigned char *output, size_t *olen, size_t osize,
switches 0:5c4d7b2438d3 117 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
switches 0:5c4d7b2438d3 118 {
switches 0:5c4d7b2438d3 119 *olen = ((mbedtls_rsa_context *) ctx)->len;
switches 0:5c4d7b2438d3 120
switches 0:5c4d7b2438d3 121 if( *olen > osize )
switches 0:5c4d7b2438d3 122 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
switches 0:5c4d7b2438d3 123
switches 0:5c4d7b2438d3 124 return( mbedtls_rsa_pkcs1_encrypt( (mbedtls_rsa_context *) ctx,
switches 0:5c4d7b2438d3 125 f_rng, p_rng, MBEDTLS_RSA_PUBLIC, ilen, input, output ) );
switches 0:5c4d7b2438d3 126 }
switches 0:5c4d7b2438d3 127
switches 0:5c4d7b2438d3 128 static int rsa_check_pair_wrap( const void *pub, const void *prv )
switches 0:5c4d7b2438d3 129 {
switches 0:5c4d7b2438d3 130 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
switches 0:5c4d7b2438d3 131 (const mbedtls_rsa_context *) prv ) );
switches 0:5c4d7b2438d3 132 }
switches 0:5c4d7b2438d3 133
switches 0:5c4d7b2438d3 134 static void *rsa_alloc_wrap( void )
switches 0:5c4d7b2438d3 135 {
switches 0:5c4d7b2438d3 136 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
switches 0:5c4d7b2438d3 137
switches 0:5c4d7b2438d3 138 if( ctx != NULL )
switches 0:5c4d7b2438d3 139 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
switches 0:5c4d7b2438d3 140
switches 0:5c4d7b2438d3 141 return( ctx );
switches 0:5c4d7b2438d3 142 }
switches 0:5c4d7b2438d3 143
switches 0:5c4d7b2438d3 144 static void rsa_free_wrap( void *ctx )
switches 0:5c4d7b2438d3 145 {
switches 0:5c4d7b2438d3 146 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
switches 0:5c4d7b2438d3 147 mbedtls_free( ctx );
switches 0:5c4d7b2438d3 148 }
switches 0:5c4d7b2438d3 149
switches 0:5c4d7b2438d3 150 static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
switches 0:5c4d7b2438d3 151 {
switches 0:5c4d7b2438d3 152 items->type = MBEDTLS_PK_DEBUG_MPI;
switches 0:5c4d7b2438d3 153 items->name = "rsa.N";
switches 0:5c4d7b2438d3 154 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
switches 0:5c4d7b2438d3 155
switches 0:5c4d7b2438d3 156 items++;
switches 0:5c4d7b2438d3 157
switches 0:5c4d7b2438d3 158 items->type = MBEDTLS_PK_DEBUG_MPI;
switches 0:5c4d7b2438d3 159 items->name = "rsa.E";
switches 0:5c4d7b2438d3 160 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
switches 0:5c4d7b2438d3 161 }
switches 0:5c4d7b2438d3 162
switches 0:5c4d7b2438d3 163 const mbedtls_pk_info_t mbedtls_rsa_info = {
switches 0:5c4d7b2438d3 164 MBEDTLS_PK_RSA,
switches 0:5c4d7b2438d3 165 "RSA",
switches 0:5c4d7b2438d3 166 rsa_get_bitlen,
switches 0:5c4d7b2438d3 167 rsa_can_do,
switches 0:5c4d7b2438d3 168 rsa_verify_wrap,
switches 0:5c4d7b2438d3 169 rsa_sign_wrap,
switches 0:5c4d7b2438d3 170 rsa_decrypt_wrap,
switches 0:5c4d7b2438d3 171 rsa_encrypt_wrap,
switches 0:5c4d7b2438d3 172 rsa_check_pair_wrap,
switches 0:5c4d7b2438d3 173 rsa_alloc_wrap,
switches 0:5c4d7b2438d3 174 rsa_free_wrap,
switches 0:5c4d7b2438d3 175 rsa_debug,
switches 0:5c4d7b2438d3 176 };
switches 0:5c4d7b2438d3 177 #endif /* MBEDTLS_RSA_C */
switches 0:5c4d7b2438d3 178
switches 0:5c4d7b2438d3 179 #if defined(MBEDTLS_ECP_C)
switches 0:5c4d7b2438d3 180 /*
switches 0:5c4d7b2438d3 181 * Generic EC key
switches 0:5c4d7b2438d3 182 */
switches 0:5c4d7b2438d3 183 static int eckey_can_do( mbedtls_pk_type_t type )
switches 0:5c4d7b2438d3 184 {
switches 0:5c4d7b2438d3 185 return( type == MBEDTLS_PK_ECKEY ||
switches 0:5c4d7b2438d3 186 type == MBEDTLS_PK_ECKEY_DH ||
switches 0:5c4d7b2438d3 187 type == MBEDTLS_PK_ECDSA );
switches 0:5c4d7b2438d3 188 }
switches 0:5c4d7b2438d3 189
switches 0:5c4d7b2438d3 190 static size_t eckey_get_bitlen( const void *ctx )
switches 0:5c4d7b2438d3 191 {
switches 0:5c4d7b2438d3 192 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
switches 0:5c4d7b2438d3 193 }
switches 0:5c4d7b2438d3 194
switches 0:5c4d7b2438d3 195 #if defined(MBEDTLS_ECDSA_C)
switches 0:5c4d7b2438d3 196 /* Forward declarations */
switches 0:5c4d7b2438d3 197 static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
switches 0:5c4d7b2438d3 198 const unsigned char *hash, size_t hash_len,
switches 0:5c4d7b2438d3 199 const unsigned char *sig, size_t sig_len );
switches 0:5c4d7b2438d3 200
switches 0:5c4d7b2438d3 201 static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
switches 0:5c4d7b2438d3 202 const unsigned char *hash, size_t hash_len,
switches 0:5c4d7b2438d3 203 unsigned char *sig, size_t *sig_len,
switches 0:5c4d7b2438d3 204 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
switches 0:5c4d7b2438d3 205
switches 0:5c4d7b2438d3 206 static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
switches 0:5c4d7b2438d3 207 const unsigned char *hash, size_t hash_len,
switches 0:5c4d7b2438d3 208 const unsigned char *sig, size_t sig_len )
switches 0:5c4d7b2438d3 209 {
switches 0:5c4d7b2438d3 210 int ret;
switches 0:5c4d7b2438d3 211 mbedtls_ecdsa_context ecdsa;
switches 0:5c4d7b2438d3 212
switches 0:5c4d7b2438d3 213 mbedtls_ecdsa_init( &ecdsa );
switches 0:5c4d7b2438d3 214
switches 0:5c4d7b2438d3 215 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
switches 0:5c4d7b2438d3 216 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
switches 0:5c4d7b2438d3 217
switches 0:5c4d7b2438d3 218 mbedtls_ecdsa_free( &ecdsa );
switches 0:5c4d7b2438d3 219
switches 0:5c4d7b2438d3 220 return( ret );
switches 0:5c4d7b2438d3 221 }
switches 0:5c4d7b2438d3 222
switches 0:5c4d7b2438d3 223 static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
switches 0:5c4d7b2438d3 224 const unsigned char *hash, size_t hash_len,
switches 0:5c4d7b2438d3 225 unsigned char *sig, size_t *sig_len,
switches 0:5c4d7b2438d3 226 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
switches 0:5c4d7b2438d3 227 {
switches 0:5c4d7b2438d3 228 int ret;
switches 0:5c4d7b2438d3 229 mbedtls_ecdsa_context ecdsa;
switches 0:5c4d7b2438d3 230
switches 0:5c4d7b2438d3 231 mbedtls_ecdsa_init( &ecdsa );
switches 0:5c4d7b2438d3 232
switches 0:5c4d7b2438d3 233 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
switches 0:5c4d7b2438d3 234 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
switches 0:5c4d7b2438d3 235 f_rng, p_rng );
switches 0:5c4d7b2438d3 236
switches 0:5c4d7b2438d3 237 mbedtls_ecdsa_free( &ecdsa );
switches 0:5c4d7b2438d3 238
switches 0:5c4d7b2438d3 239 return( ret );
switches 0:5c4d7b2438d3 240 }
switches 0:5c4d7b2438d3 241
switches 0:5c4d7b2438d3 242 #endif /* MBEDTLS_ECDSA_C */
switches 0:5c4d7b2438d3 243
switches 0:5c4d7b2438d3 244 static int eckey_check_pair( const void *pub, const void *prv )
switches 0:5c4d7b2438d3 245 {
switches 0:5c4d7b2438d3 246 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
switches 0:5c4d7b2438d3 247 (const mbedtls_ecp_keypair *) prv ) );
switches 0:5c4d7b2438d3 248 }
switches 0:5c4d7b2438d3 249
switches 0:5c4d7b2438d3 250 static void *eckey_alloc_wrap( void )
switches 0:5c4d7b2438d3 251 {
switches 0:5c4d7b2438d3 252 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
switches 0:5c4d7b2438d3 253
switches 0:5c4d7b2438d3 254 if( ctx != NULL )
switches 0:5c4d7b2438d3 255 mbedtls_ecp_keypair_init( ctx );
switches 0:5c4d7b2438d3 256
switches 0:5c4d7b2438d3 257 return( ctx );
switches 0:5c4d7b2438d3 258 }
switches 0:5c4d7b2438d3 259
switches 0:5c4d7b2438d3 260 static void eckey_free_wrap( void *ctx )
switches 0:5c4d7b2438d3 261 {
switches 0:5c4d7b2438d3 262 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
switches 0:5c4d7b2438d3 263 mbedtls_free( ctx );
switches 0:5c4d7b2438d3 264 }
switches 0:5c4d7b2438d3 265
switches 0:5c4d7b2438d3 266 static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
switches 0:5c4d7b2438d3 267 {
switches 0:5c4d7b2438d3 268 items->type = MBEDTLS_PK_DEBUG_ECP;
switches 0:5c4d7b2438d3 269 items->name = "eckey.Q";
switches 0:5c4d7b2438d3 270 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
switches 0:5c4d7b2438d3 271 }
switches 0:5c4d7b2438d3 272
switches 0:5c4d7b2438d3 273 const mbedtls_pk_info_t mbedtls_eckey_info = {
switches 0:5c4d7b2438d3 274 MBEDTLS_PK_ECKEY,
switches 0:5c4d7b2438d3 275 "EC",
switches 0:5c4d7b2438d3 276 eckey_get_bitlen,
switches 0:5c4d7b2438d3 277 eckey_can_do,
switches 0:5c4d7b2438d3 278 #if defined(MBEDTLS_ECDSA_C)
switches 0:5c4d7b2438d3 279 eckey_verify_wrap,
switches 0:5c4d7b2438d3 280 eckey_sign_wrap,
switches 0:5c4d7b2438d3 281 #else
switches 0:5c4d7b2438d3 282 NULL,
switches 0:5c4d7b2438d3 283 NULL,
switches 0:5c4d7b2438d3 284 #endif
switches 0:5c4d7b2438d3 285 NULL,
switches 0:5c4d7b2438d3 286 NULL,
switches 0:5c4d7b2438d3 287 eckey_check_pair,
switches 0:5c4d7b2438d3 288 eckey_alloc_wrap,
switches 0:5c4d7b2438d3 289 eckey_free_wrap,
switches 0:5c4d7b2438d3 290 eckey_debug,
switches 0:5c4d7b2438d3 291 };
switches 0:5c4d7b2438d3 292
switches 0:5c4d7b2438d3 293 /*
switches 0:5c4d7b2438d3 294 * EC key restricted to ECDH
switches 0:5c4d7b2438d3 295 */
switches 0:5c4d7b2438d3 296 static int eckeydh_can_do( mbedtls_pk_type_t type )
switches 0:5c4d7b2438d3 297 {
switches 0:5c4d7b2438d3 298 return( type == MBEDTLS_PK_ECKEY ||
switches 0:5c4d7b2438d3 299 type == MBEDTLS_PK_ECKEY_DH );
switches 0:5c4d7b2438d3 300 }
switches 0:5c4d7b2438d3 301
switches 0:5c4d7b2438d3 302 const mbedtls_pk_info_t mbedtls_eckeydh_info = {
switches 0:5c4d7b2438d3 303 MBEDTLS_PK_ECKEY_DH,
switches 0:5c4d7b2438d3 304 "EC_DH",
switches 0:5c4d7b2438d3 305 eckey_get_bitlen, /* Same underlying key structure */
switches 0:5c4d7b2438d3 306 eckeydh_can_do,
switches 0:5c4d7b2438d3 307 NULL,
switches 0:5c4d7b2438d3 308 NULL,
switches 0:5c4d7b2438d3 309 NULL,
switches 0:5c4d7b2438d3 310 NULL,
switches 0:5c4d7b2438d3 311 eckey_check_pair,
switches 0:5c4d7b2438d3 312 eckey_alloc_wrap, /* Same underlying key structure */
switches 0:5c4d7b2438d3 313 eckey_free_wrap, /* Same underlying key structure */
switches 0:5c4d7b2438d3 314 eckey_debug, /* Same underlying key structure */
switches 0:5c4d7b2438d3 315 };
switches 0:5c4d7b2438d3 316 #endif /* MBEDTLS_ECP_C */
switches 0:5c4d7b2438d3 317
switches 0:5c4d7b2438d3 318 #if defined(MBEDTLS_ECDSA_C)
switches 0:5c4d7b2438d3 319 static int ecdsa_can_do( mbedtls_pk_type_t type )
switches 0:5c4d7b2438d3 320 {
switches 0:5c4d7b2438d3 321 return( type == MBEDTLS_PK_ECDSA );
switches 0:5c4d7b2438d3 322 }
switches 0:5c4d7b2438d3 323
switches 0:5c4d7b2438d3 324 static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
switches 0:5c4d7b2438d3 325 const unsigned char *hash, size_t hash_len,
switches 0:5c4d7b2438d3 326 const unsigned char *sig, size_t sig_len )
switches 0:5c4d7b2438d3 327 {
switches 0:5c4d7b2438d3 328 int ret;
switches 0:5c4d7b2438d3 329 ((void) md_alg);
switches 0:5c4d7b2438d3 330
switches 0:5c4d7b2438d3 331 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
switches 0:5c4d7b2438d3 332 hash, hash_len, sig, sig_len );
switches 0:5c4d7b2438d3 333
switches 0:5c4d7b2438d3 334 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
switches 0:5c4d7b2438d3 335 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
switches 0:5c4d7b2438d3 336
switches 0:5c4d7b2438d3 337 return( ret );
switches 0:5c4d7b2438d3 338 }
switches 0:5c4d7b2438d3 339
switches 0:5c4d7b2438d3 340 static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
switches 0:5c4d7b2438d3 341 const unsigned char *hash, size_t hash_len,
switches 0:5c4d7b2438d3 342 unsigned char *sig, size_t *sig_len,
switches 0:5c4d7b2438d3 343 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
switches 0:5c4d7b2438d3 344 {
switches 0:5c4d7b2438d3 345 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
switches 0:5c4d7b2438d3 346 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
switches 0:5c4d7b2438d3 347 }
switches 0:5c4d7b2438d3 348
switches 0:5c4d7b2438d3 349 static void *ecdsa_alloc_wrap( void )
switches 0:5c4d7b2438d3 350 {
switches 0:5c4d7b2438d3 351 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
switches 0:5c4d7b2438d3 352
switches 0:5c4d7b2438d3 353 if( ctx != NULL )
switches 0:5c4d7b2438d3 354 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
switches 0:5c4d7b2438d3 355
switches 0:5c4d7b2438d3 356 return( ctx );
switches 0:5c4d7b2438d3 357 }
switches 0:5c4d7b2438d3 358
switches 0:5c4d7b2438d3 359 static void ecdsa_free_wrap( void *ctx )
switches 0:5c4d7b2438d3 360 {
switches 0:5c4d7b2438d3 361 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
switches 0:5c4d7b2438d3 362 mbedtls_free( ctx );
switches 0:5c4d7b2438d3 363 }
switches 0:5c4d7b2438d3 364
switches 0:5c4d7b2438d3 365 const mbedtls_pk_info_t mbedtls_ecdsa_info = {
switches 0:5c4d7b2438d3 366 MBEDTLS_PK_ECDSA,
switches 0:5c4d7b2438d3 367 "ECDSA",
switches 0:5c4d7b2438d3 368 eckey_get_bitlen, /* Compatible key structures */
switches 0:5c4d7b2438d3 369 ecdsa_can_do,
switches 0:5c4d7b2438d3 370 ecdsa_verify_wrap,
switches 0:5c4d7b2438d3 371 ecdsa_sign_wrap,
switches 0:5c4d7b2438d3 372 NULL,
switches 0:5c4d7b2438d3 373 NULL,
switches 0:5c4d7b2438d3 374 eckey_check_pair, /* Compatible key structures */
switches 0:5c4d7b2438d3 375 ecdsa_alloc_wrap,
switches 0:5c4d7b2438d3 376 ecdsa_free_wrap,
switches 0:5c4d7b2438d3 377 eckey_debug, /* Compatible key structures */
switches 0:5c4d7b2438d3 378 };
switches 0:5c4d7b2438d3 379 #endif /* MBEDTLS_ECDSA_C */
switches 0:5c4d7b2438d3 380
switches 0:5c4d7b2438d3 381 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
switches 0:5c4d7b2438d3 382 /*
switches 0:5c4d7b2438d3 383 * Support for alternative RSA-private implementations
switches 0:5c4d7b2438d3 384 */
switches 0:5c4d7b2438d3 385
switches 0:5c4d7b2438d3 386 static int rsa_alt_can_do( mbedtls_pk_type_t type )
switches 0:5c4d7b2438d3 387 {
switches 0:5c4d7b2438d3 388 return( type == MBEDTLS_PK_RSA );
switches 0:5c4d7b2438d3 389 }
switches 0:5c4d7b2438d3 390
switches 0:5c4d7b2438d3 391 static size_t rsa_alt_get_bitlen( const void *ctx )
switches 0:5c4d7b2438d3 392 {
switches 0:5c4d7b2438d3 393 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
switches 0:5c4d7b2438d3 394
switches 0:5c4d7b2438d3 395 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
switches 0:5c4d7b2438d3 396 }
switches 0:5c4d7b2438d3 397
switches 0:5c4d7b2438d3 398 static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
switches 0:5c4d7b2438d3 399 const unsigned char *hash, size_t hash_len,
switches 0:5c4d7b2438d3 400 unsigned char *sig, size_t *sig_len,
switches 0:5c4d7b2438d3 401 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
switches 0:5c4d7b2438d3 402 {
switches 0:5c4d7b2438d3 403 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
switches 0:5c4d7b2438d3 404
switches 0:5c4d7b2438d3 405 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
switches 0:5c4d7b2438d3 406
switches 0:5c4d7b2438d3 407 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
switches 0:5c4d7b2438d3 408 md_alg, (unsigned int) hash_len, hash, sig ) );
switches 0:5c4d7b2438d3 409 }
switches 0:5c4d7b2438d3 410
switches 0:5c4d7b2438d3 411 static int rsa_alt_decrypt_wrap( void *ctx,
switches 0:5c4d7b2438d3 412 const unsigned char *input, size_t ilen,
switches 0:5c4d7b2438d3 413 unsigned char *output, size_t *olen, size_t osize,
switches 0:5c4d7b2438d3 414 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
switches 0:5c4d7b2438d3 415 {
switches 0:5c4d7b2438d3 416 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
switches 0:5c4d7b2438d3 417
switches 0:5c4d7b2438d3 418 ((void) f_rng);
switches 0:5c4d7b2438d3 419 ((void) p_rng);
switches 0:5c4d7b2438d3 420
switches 0:5c4d7b2438d3 421 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
switches 0:5c4d7b2438d3 422 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
switches 0:5c4d7b2438d3 423
switches 0:5c4d7b2438d3 424 return( rsa_alt->decrypt_func( rsa_alt->key,
switches 0:5c4d7b2438d3 425 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
switches 0:5c4d7b2438d3 426 }
switches 0:5c4d7b2438d3 427
switches 0:5c4d7b2438d3 428 #if defined(MBEDTLS_RSA_C)
switches 0:5c4d7b2438d3 429 static int rsa_alt_check_pair( const void *pub, const void *prv )
switches 0:5c4d7b2438d3 430 {
switches 0:5c4d7b2438d3 431 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
switches 0:5c4d7b2438d3 432 unsigned char hash[32];
switches 0:5c4d7b2438d3 433 size_t sig_len = 0;
switches 0:5c4d7b2438d3 434 int ret;
switches 0:5c4d7b2438d3 435
switches 0:5c4d7b2438d3 436 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
switches 0:5c4d7b2438d3 437 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
switches 0:5c4d7b2438d3 438
switches 0:5c4d7b2438d3 439 memset( hash, 0x2a, sizeof( hash ) );
switches 0:5c4d7b2438d3 440
switches 0:5c4d7b2438d3 441 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
switches 0:5c4d7b2438d3 442 hash, sizeof( hash ),
switches 0:5c4d7b2438d3 443 sig, &sig_len, NULL, NULL ) ) != 0 )
switches 0:5c4d7b2438d3 444 {
switches 0:5c4d7b2438d3 445 return( ret );
switches 0:5c4d7b2438d3 446 }
switches 0:5c4d7b2438d3 447
switches 0:5c4d7b2438d3 448 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
switches 0:5c4d7b2438d3 449 hash, sizeof( hash ), sig, sig_len ) != 0 )
switches 0:5c4d7b2438d3 450 {
switches 0:5c4d7b2438d3 451 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
switches 0:5c4d7b2438d3 452 }
switches 0:5c4d7b2438d3 453
switches 0:5c4d7b2438d3 454 return( 0 );
switches 0:5c4d7b2438d3 455 }
switches 0:5c4d7b2438d3 456 #endif /* MBEDTLS_RSA_C */
switches 0:5c4d7b2438d3 457
switches 0:5c4d7b2438d3 458 static void *rsa_alt_alloc_wrap( void )
switches 0:5c4d7b2438d3 459 {
switches 0:5c4d7b2438d3 460 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
switches 0:5c4d7b2438d3 461
switches 0:5c4d7b2438d3 462 if( ctx != NULL )
switches 0:5c4d7b2438d3 463 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
switches 0:5c4d7b2438d3 464
switches 0:5c4d7b2438d3 465 return( ctx );
switches 0:5c4d7b2438d3 466 }
switches 0:5c4d7b2438d3 467
switches 0:5c4d7b2438d3 468 static void rsa_alt_free_wrap( void *ctx )
switches 0:5c4d7b2438d3 469 {
switches 0:5c4d7b2438d3 470 mbedtls_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
switches 0:5c4d7b2438d3 471 mbedtls_free( ctx );
switches 0:5c4d7b2438d3 472 }
switches 0:5c4d7b2438d3 473
switches 0:5c4d7b2438d3 474 const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
switches 0:5c4d7b2438d3 475 MBEDTLS_PK_RSA_ALT,
switches 0:5c4d7b2438d3 476 "RSA-alt",
switches 0:5c4d7b2438d3 477 rsa_alt_get_bitlen,
switches 0:5c4d7b2438d3 478 rsa_alt_can_do,
switches 0:5c4d7b2438d3 479 NULL,
switches 0:5c4d7b2438d3 480 rsa_alt_sign_wrap,
switches 0:5c4d7b2438d3 481 rsa_alt_decrypt_wrap,
switches 0:5c4d7b2438d3 482 NULL,
switches 0:5c4d7b2438d3 483 #if defined(MBEDTLS_RSA_C)
switches 0:5c4d7b2438d3 484 rsa_alt_check_pair,
switches 0:5c4d7b2438d3 485 #else
switches 0:5c4d7b2438d3 486 NULL,
switches 0:5c4d7b2438d3 487 #endif
switches 0:5c4d7b2438d3 488 rsa_alt_alloc_wrap,
switches 0:5c4d7b2438d3 489 rsa_alt_free_wrap,
switches 0:5c4d7b2438d3 490 NULL,
switches 0:5c4d7b2438d3 491 };
switches 0:5c4d7b2438d3 492
switches 0:5c4d7b2438d3 493 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
switches 0:5c4d7b2438d3 494
switches 0:5c4d7b2438d3 495 #endif /* MBEDTLS_PK_C */