Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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