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