Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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