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