This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

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