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