mbed TLS upgraded to 2.6.0
Fork of mbedtls by
library/pk_wrap.c@2:bbdeda018a3c, 2017-09-29 (annotated)
- Committer:
- Jasper Wallace
- Date:
- Fri Sep 29 19:50:30 2017 +0100
- Revision:
- 2:bbdeda018a3c
- Parent:
- 1:9ebc941037d5
Update to mbedtls 2.6.0, many changes.
Changes to mbedtls sources made:
in include/mbedtls/config.h comment out:
#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C
uncomment:
#define MBEDTLS_NO_PLATFORM_ENTROPY
remove the following directorys:
programs
yotta
visualc
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" |
Jasper Wallace |
1:9ebc941037d5 | 33 | #include "mbedtls/bignum.h" |
markrad | 0:cdf462088d13 | 34 | |
markrad | 0:cdf462088d13 | 35 | #include <string.h> |
markrad | 0:cdf462088d13 | 36 | |
markrad | 0:cdf462088d13 | 37 | #if defined(MBEDTLS_ECP_C) |
markrad | 0:cdf462088d13 | 38 | #include "mbedtls/ecp.h" |
markrad | 0:cdf462088d13 | 39 | #endif |
markrad | 0:cdf462088d13 | 40 | |
markrad | 0:cdf462088d13 | 41 | #if defined(MBEDTLS_ECDSA_C) |
markrad | 0:cdf462088d13 | 42 | #include "mbedtls/ecdsa.h" |
markrad | 0:cdf462088d13 | 43 | #endif |
markrad | 0:cdf462088d13 | 44 | |
markrad | 0:cdf462088d13 | 45 | #if defined(MBEDTLS_PLATFORM_C) |
markrad | 0:cdf462088d13 | 46 | #include "mbedtls/platform.h" |
markrad | 0:cdf462088d13 | 47 | #else |
markrad | 0:cdf462088d13 | 48 | #include <stdlib.h> |
markrad | 0:cdf462088d13 | 49 | #define mbedtls_calloc calloc |
markrad | 0:cdf462088d13 | 50 | #define mbedtls_free free |
markrad | 0:cdf462088d13 | 51 | #endif |
markrad | 0:cdf462088d13 | 52 | |
Jasper Wallace |
1:9ebc941037d5 | 53 | #include <limits.h> |
Jasper Wallace |
1:9ebc941037d5 | 54 | |
markrad | 0:cdf462088d13 | 55 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
markrad | 0:cdf462088d13 | 56 | /* Implementation that should never be optimized out by the compiler */ |
markrad | 0:cdf462088d13 | 57 | static void mbedtls_zeroize( void *v, size_t n ) { |
markrad | 0:cdf462088d13 | 58 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
markrad | 0:cdf462088d13 | 59 | } |
markrad | 0:cdf462088d13 | 60 | #endif |
markrad | 0:cdf462088d13 | 61 | |
markrad | 0:cdf462088d13 | 62 | #if defined(MBEDTLS_RSA_C) |
markrad | 0:cdf462088d13 | 63 | static int rsa_can_do( mbedtls_pk_type_t type ) |
markrad | 0:cdf462088d13 | 64 | { |
markrad | 0:cdf462088d13 | 65 | return( type == MBEDTLS_PK_RSA || |
markrad | 0:cdf462088d13 | 66 | type == MBEDTLS_PK_RSASSA_PSS ); |
markrad | 0:cdf462088d13 | 67 | } |
markrad | 0:cdf462088d13 | 68 | |
markrad | 0:cdf462088d13 | 69 | static size_t rsa_get_bitlen( const void *ctx ) |
markrad | 0:cdf462088d13 | 70 | { |
markrad | 0:cdf462088d13 | 71 | return( 8 * ((const mbedtls_rsa_context *) ctx)->len ); |
markrad | 0:cdf462088d13 | 72 | } |
markrad | 0:cdf462088d13 | 73 | |
markrad | 0:cdf462088d13 | 74 | static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 75 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 76 | const unsigned char *sig, size_t sig_len ) |
markrad | 0:cdf462088d13 | 77 | { |
markrad | 0:cdf462088d13 | 78 | int ret; |
markrad | 0:cdf462088d13 | 79 | |
Jasper Wallace |
1:9ebc941037d5 | 80 | #if defined(MBEDTLS_HAVE_INT64) |
Jasper Wallace |
1:9ebc941037d5 | 81 | if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) |
Jasper Wallace |
1:9ebc941037d5 | 82 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Jasper Wallace |
1:9ebc941037d5 | 83 | #endif /* MBEDTLS_HAVE_INT64 */ |
Jasper Wallace |
1:9ebc941037d5 | 84 | |
markrad | 0:cdf462088d13 | 85 | if( sig_len < ((mbedtls_rsa_context *) ctx)->len ) |
markrad | 0:cdf462088d13 | 86 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
markrad | 0:cdf462088d13 | 87 | |
markrad | 0:cdf462088d13 | 88 | if( ( ret = mbedtls_rsa_pkcs1_verify( (mbedtls_rsa_context *) ctx, NULL, NULL, |
markrad | 0:cdf462088d13 | 89 | MBEDTLS_RSA_PUBLIC, md_alg, |
markrad | 0:cdf462088d13 | 90 | (unsigned int) hash_len, hash, sig ) ) != 0 ) |
markrad | 0:cdf462088d13 | 91 | return( ret ); |
markrad | 0:cdf462088d13 | 92 | |
markrad | 0:cdf462088d13 | 93 | if( sig_len > ((mbedtls_rsa_context *) ctx)->len ) |
markrad | 0:cdf462088d13 | 94 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); |
markrad | 0:cdf462088d13 | 95 | |
markrad | 0:cdf462088d13 | 96 | return( 0 ); |
markrad | 0:cdf462088d13 | 97 | } |
markrad | 0:cdf462088d13 | 98 | |
markrad | 0:cdf462088d13 | 99 | static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 100 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 101 | unsigned char *sig, size_t *sig_len, |
markrad | 0:cdf462088d13 | 102 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
markrad | 0:cdf462088d13 | 103 | { |
Jasper Wallace |
1:9ebc941037d5 | 104 | #if defined(MBEDTLS_HAVE_INT64) |
Jasper Wallace |
1:9ebc941037d5 | 105 | if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len ) |
Jasper Wallace |
1:9ebc941037d5 | 106 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Jasper Wallace |
1:9ebc941037d5 | 107 | #endif /* MBEDTLS_HAVE_INT64 */ |
Jasper Wallace |
1:9ebc941037d5 | 108 | |
markrad | 0:cdf462088d13 | 109 | *sig_len = ((mbedtls_rsa_context *) ctx)->len; |
markrad | 0:cdf462088d13 | 110 | |
markrad | 0:cdf462088d13 | 111 | return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, |
markrad | 0:cdf462088d13 | 112 | md_alg, (unsigned int) hash_len, hash, sig ) ); |
markrad | 0:cdf462088d13 | 113 | } |
markrad | 0:cdf462088d13 | 114 | |
markrad | 0:cdf462088d13 | 115 | static int rsa_decrypt_wrap( void *ctx, |
markrad | 0:cdf462088d13 | 116 | const unsigned char *input, size_t ilen, |
markrad | 0:cdf462088d13 | 117 | unsigned char *output, size_t *olen, size_t osize, |
markrad | 0:cdf462088d13 | 118 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
markrad | 0:cdf462088d13 | 119 | { |
markrad | 0:cdf462088d13 | 120 | if( ilen != ((mbedtls_rsa_context *) ctx)->len ) |
markrad | 0:cdf462088d13 | 121 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 122 | |
markrad | 0:cdf462088d13 | 123 | return( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx, f_rng, p_rng, |
markrad | 0:cdf462088d13 | 124 | MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) ); |
markrad | 0:cdf462088d13 | 125 | } |
markrad | 0:cdf462088d13 | 126 | |
markrad | 0:cdf462088d13 | 127 | static int rsa_encrypt_wrap( void *ctx, |
markrad | 0:cdf462088d13 | 128 | const unsigned char *input, size_t ilen, |
markrad | 0:cdf462088d13 | 129 | unsigned char *output, size_t *olen, size_t osize, |
markrad | 0:cdf462088d13 | 130 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
markrad | 0:cdf462088d13 | 131 | { |
markrad | 0:cdf462088d13 | 132 | *olen = ((mbedtls_rsa_context *) ctx)->len; |
markrad | 0:cdf462088d13 | 133 | |
markrad | 0:cdf462088d13 | 134 | if( *olen > osize ) |
markrad | 0:cdf462088d13 | 135 | return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE ); |
markrad | 0:cdf462088d13 | 136 | |
markrad | 0:cdf462088d13 | 137 | return( mbedtls_rsa_pkcs1_encrypt( (mbedtls_rsa_context *) ctx, |
markrad | 0:cdf462088d13 | 138 | f_rng, p_rng, MBEDTLS_RSA_PUBLIC, ilen, input, output ) ); |
markrad | 0:cdf462088d13 | 139 | } |
markrad | 0:cdf462088d13 | 140 | |
markrad | 0:cdf462088d13 | 141 | static int rsa_check_pair_wrap( const void *pub, const void *prv ) |
markrad | 0:cdf462088d13 | 142 | { |
markrad | 0:cdf462088d13 | 143 | return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub, |
markrad | 0:cdf462088d13 | 144 | (const mbedtls_rsa_context *) prv ) ); |
markrad | 0:cdf462088d13 | 145 | } |
markrad | 0:cdf462088d13 | 146 | |
markrad | 0:cdf462088d13 | 147 | static void *rsa_alloc_wrap( void ) |
markrad | 0:cdf462088d13 | 148 | { |
markrad | 0:cdf462088d13 | 149 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) ); |
markrad | 0:cdf462088d13 | 150 | |
markrad | 0:cdf462088d13 | 151 | if( ctx != NULL ) |
markrad | 0:cdf462088d13 | 152 | mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 ); |
markrad | 0:cdf462088d13 | 153 | |
markrad | 0:cdf462088d13 | 154 | return( ctx ); |
markrad | 0:cdf462088d13 | 155 | } |
markrad | 0:cdf462088d13 | 156 | |
markrad | 0:cdf462088d13 | 157 | static void rsa_free_wrap( void *ctx ) |
markrad | 0:cdf462088d13 | 158 | { |
markrad | 0:cdf462088d13 | 159 | mbedtls_rsa_free( (mbedtls_rsa_context *) ctx ); |
markrad | 0:cdf462088d13 | 160 | mbedtls_free( ctx ); |
markrad | 0:cdf462088d13 | 161 | } |
markrad | 0:cdf462088d13 | 162 | |
markrad | 0:cdf462088d13 | 163 | static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items ) |
markrad | 0:cdf462088d13 | 164 | { |
markrad | 0:cdf462088d13 | 165 | items->type = MBEDTLS_PK_DEBUG_MPI; |
markrad | 0:cdf462088d13 | 166 | items->name = "rsa.N"; |
markrad | 0:cdf462088d13 | 167 | items->value = &( ((mbedtls_rsa_context *) ctx)->N ); |
markrad | 0:cdf462088d13 | 168 | |
markrad | 0:cdf462088d13 | 169 | items++; |
markrad | 0:cdf462088d13 | 170 | |
markrad | 0:cdf462088d13 | 171 | items->type = MBEDTLS_PK_DEBUG_MPI; |
markrad | 0:cdf462088d13 | 172 | items->name = "rsa.E"; |
markrad | 0:cdf462088d13 | 173 | items->value = &( ((mbedtls_rsa_context *) ctx)->E ); |
markrad | 0:cdf462088d13 | 174 | } |
markrad | 0:cdf462088d13 | 175 | |
markrad | 0:cdf462088d13 | 176 | const mbedtls_pk_info_t mbedtls_rsa_info = { |
markrad | 0:cdf462088d13 | 177 | MBEDTLS_PK_RSA, |
markrad | 0:cdf462088d13 | 178 | "RSA", |
markrad | 0:cdf462088d13 | 179 | rsa_get_bitlen, |
markrad | 0:cdf462088d13 | 180 | rsa_can_do, |
markrad | 0:cdf462088d13 | 181 | rsa_verify_wrap, |
markrad | 0:cdf462088d13 | 182 | rsa_sign_wrap, |
markrad | 0:cdf462088d13 | 183 | rsa_decrypt_wrap, |
markrad | 0:cdf462088d13 | 184 | rsa_encrypt_wrap, |
markrad | 0:cdf462088d13 | 185 | rsa_check_pair_wrap, |
markrad | 0:cdf462088d13 | 186 | rsa_alloc_wrap, |
markrad | 0:cdf462088d13 | 187 | rsa_free_wrap, |
markrad | 0:cdf462088d13 | 188 | rsa_debug, |
markrad | 0:cdf462088d13 | 189 | }; |
markrad | 0:cdf462088d13 | 190 | #endif /* MBEDTLS_RSA_C */ |
markrad | 0:cdf462088d13 | 191 | |
markrad | 0:cdf462088d13 | 192 | #if defined(MBEDTLS_ECP_C) |
markrad | 0:cdf462088d13 | 193 | /* |
markrad | 0:cdf462088d13 | 194 | * Generic EC key |
markrad | 0:cdf462088d13 | 195 | */ |
markrad | 0:cdf462088d13 | 196 | static int eckey_can_do( mbedtls_pk_type_t type ) |
markrad | 0:cdf462088d13 | 197 | { |
markrad | 0:cdf462088d13 | 198 | return( type == MBEDTLS_PK_ECKEY || |
markrad | 0:cdf462088d13 | 199 | type == MBEDTLS_PK_ECKEY_DH || |
markrad | 0:cdf462088d13 | 200 | type == MBEDTLS_PK_ECDSA ); |
markrad | 0:cdf462088d13 | 201 | } |
markrad | 0:cdf462088d13 | 202 | |
markrad | 0:cdf462088d13 | 203 | static size_t eckey_get_bitlen( const void *ctx ) |
markrad | 0:cdf462088d13 | 204 | { |
markrad | 0:cdf462088d13 | 205 | return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits ); |
markrad | 0:cdf462088d13 | 206 | } |
markrad | 0:cdf462088d13 | 207 | |
markrad | 0:cdf462088d13 | 208 | #if defined(MBEDTLS_ECDSA_C) |
markrad | 0:cdf462088d13 | 209 | /* Forward declarations */ |
markrad | 0:cdf462088d13 | 210 | static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 211 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 212 | const unsigned char *sig, size_t sig_len ); |
markrad | 0:cdf462088d13 | 213 | |
markrad | 0:cdf462088d13 | 214 | static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 215 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 216 | unsigned char *sig, size_t *sig_len, |
markrad | 0:cdf462088d13 | 217 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
markrad | 0:cdf462088d13 | 218 | |
markrad | 0:cdf462088d13 | 219 | static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 220 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 221 | const unsigned char *sig, size_t sig_len ) |
markrad | 0:cdf462088d13 | 222 | { |
markrad | 0:cdf462088d13 | 223 | int ret; |
markrad | 0:cdf462088d13 | 224 | mbedtls_ecdsa_context ecdsa; |
markrad | 0:cdf462088d13 | 225 | |
markrad | 0:cdf462088d13 | 226 | mbedtls_ecdsa_init( &ecdsa ); |
markrad | 0:cdf462088d13 | 227 | |
markrad | 0:cdf462088d13 | 228 | if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
markrad | 0:cdf462088d13 | 229 | ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len ); |
markrad | 0:cdf462088d13 | 230 | |
markrad | 0:cdf462088d13 | 231 | mbedtls_ecdsa_free( &ecdsa ); |
markrad | 0:cdf462088d13 | 232 | |
markrad | 0:cdf462088d13 | 233 | return( ret ); |
markrad | 0:cdf462088d13 | 234 | } |
markrad | 0:cdf462088d13 | 235 | |
markrad | 0:cdf462088d13 | 236 | static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 237 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 238 | unsigned char *sig, size_t *sig_len, |
markrad | 0:cdf462088d13 | 239 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
markrad | 0:cdf462088d13 | 240 | { |
markrad | 0:cdf462088d13 | 241 | int ret; |
markrad | 0:cdf462088d13 | 242 | mbedtls_ecdsa_context ecdsa; |
markrad | 0:cdf462088d13 | 243 | |
markrad | 0:cdf462088d13 | 244 | mbedtls_ecdsa_init( &ecdsa ); |
markrad | 0:cdf462088d13 | 245 | |
markrad | 0:cdf462088d13 | 246 | if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
markrad | 0:cdf462088d13 | 247 | ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len, |
markrad | 0:cdf462088d13 | 248 | f_rng, p_rng ); |
markrad | 0:cdf462088d13 | 249 | |
markrad | 0:cdf462088d13 | 250 | mbedtls_ecdsa_free( &ecdsa ); |
markrad | 0:cdf462088d13 | 251 | |
markrad | 0:cdf462088d13 | 252 | return( ret ); |
markrad | 0:cdf462088d13 | 253 | } |
markrad | 0:cdf462088d13 | 254 | |
markrad | 0:cdf462088d13 | 255 | #endif /* MBEDTLS_ECDSA_C */ |
markrad | 0:cdf462088d13 | 256 | |
markrad | 0:cdf462088d13 | 257 | static int eckey_check_pair( const void *pub, const void *prv ) |
markrad | 0:cdf462088d13 | 258 | { |
markrad | 0:cdf462088d13 | 259 | return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub, |
markrad | 0:cdf462088d13 | 260 | (const mbedtls_ecp_keypair *) prv ) ); |
markrad | 0:cdf462088d13 | 261 | } |
markrad | 0:cdf462088d13 | 262 | |
markrad | 0:cdf462088d13 | 263 | static void *eckey_alloc_wrap( void ) |
markrad | 0:cdf462088d13 | 264 | { |
markrad | 0:cdf462088d13 | 265 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
markrad | 0:cdf462088d13 | 266 | |
markrad | 0:cdf462088d13 | 267 | if( ctx != NULL ) |
markrad | 0:cdf462088d13 | 268 | mbedtls_ecp_keypair_init( ctx ); |
markrad | 0:cdf462088d13 | 269 | |
markrad | 0:cdf462088d13 | 270 | return( ctx ); |
markrad | 0:cdf462088d13 | 271 | } |
markrad | 0:cdf462088d13 | 272 | |
markrad | 0:cdf462088d13 | 273 | static void eckey_free_wrap( void *ctx ) |
markrad | 0:cdf462088d13 | 274 | { |
markrad | 0:cdf462088d13 | 275 | mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx ); |
markrad | 0:cdf462088d13 | 276 | mbedtls_free( ctx ); |
markrad | 0:cdf462088d13 | 277 | } |
markrad | 0:cdf462088d13 | 278 | |
markrad | 0:cdf462088d13 | 279 | static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items ) |
markrad | 0:cdf462088d13 | 280 | { |
markrad | 0:cdf462088d13 | 281 | items->type = MBEDTLS_PK_DEBUG_ECP; |
markrad | 0:cdf462088d13 | 282 | items->name = "eckey.Q"; |
markrad | 0:cdf462088d13 | 283 | items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q ); |
markrad | 0:cdf462088d13 | 284 | } |
markrad | 0:cdf462088d13 | 285 | |
markrad | 0:cdf462088d13 | 286 | const mbedtls_pk_info_t mbedtls_eckey_info = { |
markrad | 0:cdf462088d13 | 287 | MBEDTLS_PK_ECKEY, |
markrad | 0:cdf462088d13 | 288 | "EC", |
markrad | 0:cdf462088d13 | 289 | eckey_get_bitlen, |
markrad | 0:cdf462088d13 | 290 | eckey_can_do, |
markrad | 0:cdf462088d13 | 291 | #if defined(MBEDTLS_ECDSA_C) |
markrad | 0:cdf462088d13 | 292 | eckey_verify_wrap, |
markrad | 0:cdf462088d13 | 293 | eckey_sign_wrap, |
markrad | 0:cdf462088d13 | 294 | #else |
markrad | 0:cdf462088d13 | 295 | NULL, |
markrad | 0:cdf462088d13 | 296 | NULL, |
markrad | 0:cdf462088d13 | 297 | #endif |
markrad | 0:cdf462088d13 | 298 | NULL, |
markrad | 0:cdf462088d13 | 299 | NULL, |
markrad | 0:cdf462088d13 | 300 | eckey_check_pair, |
markrad | 0:cdf462088d13 | 301 | eckey_alloc_wrap, |
markrad | 0:cdf462088d13 | 302 | eckey_free_wrap, |
markrad | 0:cdf462088d13 | 303 | eckey_debug, |
markrad | 0:cdf462088d13 | 304 | }; |
markrad | 0:cdf462088d13 | 305 | |
markrad | 0:cdf462088d13 | 306 | /* |
markrad | 0:cdf462088d13 | 307 | * EC key restricted to ECDH |
markrad | 0:cdf462088d13 | 308 | */ |
markrad | 0:cdf462088d13 | 309 | static int eckeydh_can_do( mbedtls_pk_type_t type ) |
markrad | 0:cdf462088d13 | 310 | { |
markrad | 0:cdf462088d13 | 311 | return( type == MBEDTLS_PK_ECKEY || |
markrad | 0:cdf462088d13 | 312 | type == MBEDTLS_PK_ECKEY_DH ); |
markrad | 0:cdf462088d13 | 313 | } |
markrad | 0:cdf462088d13 | 314 | |
markrad | 0:cdf462088d13 | 315 | const mbedtls_pk_info_t mbedtls_eckeydh_info = { |
markrad | 0:cdf462088d13 | 316 | MBEDTLS_PK_ECKEY_DH, |
markrad | 0:cdf462088d13 | 317 | "EC_DH", |
markrad | 0:cdf462088d13 | 318 | eckey_get_bitlen, /* Same underlying key structure */ |
markrad | 0:cdf462088d13 | 319 | eckeydh_can_do, |
markrad | 0:cdf462088d13 | 320 | NULL, |
markrad | 0:cdf462088d13 | 321 | NULL, |
markrad | 0:cdf462088d13 | 322 | NULL, |
markrad | 0:cdf462088d13 | 323 | NULL, |
markrad | 0:cdf462088d13 | 324 | eckey_check_pair, |
markrad | 0:cdf462088d13 | 325 | eckey_alloc_wrap, /* Same underlying key structure */ |
markrad | 0:cdf462088d13 | 326 | eckey_free_wrap, /* Same underlying key structure */ |
markrad | 0:cdf462088d13 | 327 | eckey_debug, /* Same underlying key structure */ |
markrad | 0:cdf462088d13 | 328 | }; |
markrad | 0:cdf462088d13 | 329 | #endif /* MBEDTLS_ECP_C */ |
markrad | 0:cdf462088d13 | 330 | |
markrad | 0:cdf462088d13 | 331 | #if defined(MBEDTLS_ECDSA_C) |
markrad | 0:cdf462088d13 | 332 | static int ecdsa_can_do( mbedtls_pk_type_t type ) |
markrad | 0:cdf462088d13 | 333 | { |
markrad | 0:cdf462088d13 | 334 | return( type == MBEDTLS_PK_ECDSA ); |
markrad | 0:cdf462088d13 | 335 | } |
markrad | 0:cdf462088d13 | 336 | |
markrad | 0:cdf462088d13 | 337 | static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 338 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 339 | const unsigned char *sig, size_t sig_len ) |
markrad | 0:cdf462088d13 | 340 | { |
markrad | 0:cdf462088d13 | 341 | int ret; |
markrad | 0:cdf462088d13 | 342 | ((void) md_alg); |
markrad | 0:cdf462088d13 | 343 | |
markrad | 0:cdf462088d13 | 344 | ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx, |
markrad | 0:cdf462088d13 | 345 | hash, hash_len, sig, sig_len ); |
markrad | 0:cdf462088d13 | 346 | |
markrad | 0:cdf462088d13 | 347 | if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH ) |
markrad | 0:cdf462088d13 | 348 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); |
markrad | 0:cdf462088d13 | 349 | |
markrad | 0:cdf462088d13 | 350 | return( ret ); |
markrad | 0:cdf462088d13 | 351 | } |
markrad | 0:cdf462088d13 | 352 | |
markrad | 0:cdf462088d13 | 353 | static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 354 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 355 | unsigned char *sig, size_t *sig_len, |
markrad | 0:cdf462088d13 | 356 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
markrad | 0:cdf462088d13 | 357 | { |
markrad | 0:cdf462088d13 | 358 | return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx, |
markrad | 0:cdf462088d13 | 359 | md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) ); |
markrad | 0:cdf462088d13 | 360 | } |
markrad | 0:cdf462088d13 | 361 | |
markrad | 0:cdf462088d13 | 362 | static void *ecdsa_alloc_wrap( void ) |
markrad | 0:cdf462088d13 | 363 | { |
markrad | 0:cdf462088d13 | 364 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) ); |
markrad | 0:cdf462088d13 | 365 | |
markrad | 0:cdf462088d13 | 366 | if( ctx != NULL ) |
markrad | 0:cdf462088d13 | 367 | mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx ); |
markrad | 0:cdf462088d13 | 368 | |
markrad | 0:cdf462088d13 | 369 | return( ctx ); |
markrad | 0:cdf462088d13 | 370 | } |
markrad | 0:cdf462088d13 | 371 | |
markrad | 0:cdf462088d13 | 372 | static void ecdsa_free_wrap( void *ctx ) |
markrad | 0:cdf462088d13 | 373 | { |
markrad | 0:cdf462088d13 | 374 | mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx ); |
markrad | 0:cdf462088d13 | 375 | mbedtls_free( ctx ); |
markrad | 0:cdf462088d13 | 376 | } |
markrad | 0:cdf462088d13 | 377 | |
markrad | 0:cdf462088d13 | 378 | const mbedtls_pk_info_t mbedtls_ecdsa_info = { |
markrad | 0:cdf462088d13 | 379 | MBEDTLS_PK_ECDSA, |
markrad | 0:cdf462088d13 | 380 | "ECDSA", |
markrad | 0:cdf462088d13 | 381 | eckey_get_bitlen, /* Compatible key structures */ |
markrad | 0:cdf462088d13 | 382 | ecdsa_can_do, |
markrad | 0:cdf462088d13 | 383 | ecdsa_verify_wrap, |
markrad | 0:cdf462088d13 | 384 | ecdsa_sign_wrap, |
markrad | 0:cdf462088d13 | 385 | NULL, |
markrad | 0:cdf462088d13 | 386 | NULL, |
markrad | 0:cdf462088d13 | 387 | eckey_check_pair, /* Compatible key structures */ |
markrad | 0:cdf462088d13 | 388 | ecdsa_alloc_wrap, |
markrad | 0:cdf462088d13 | 389 | ecdsa_free_wrap, |
markrad | 0:cdf462088d13 | 390 | eckey_debug, /* Compatible key structures */ |
markrad | 0:cdf462088d13 | 391 | }; |
markrad | 0:cdf462088d13 | 392 | #endif /* MBEDTLS_ECDSA_C */ |
markrad | 0:cdf462088d13 | 393 | |
markrad | 0:cdf462088d13 | 394 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
markrad | 0:cdf462088d13 | 395 | /* |
markrad | 0:cdf462088d13 | 396 | * Support for alternative RSA-private implementations |
markrad | 0:cdf462088d13 | 397 | */ |
markrad | 0:cdf462088d13 | 398 | |
markrad | 0:cdf462088d13 | 399 | static int rsa_alt_can_do( mbedtls_pk_type_t type ) |
markrad | 0:cdf462088d13 | 400 | { |
markrad | 0:cdf462088d13 | 401 | return( type == MBEDTLS_PK_RSA ); |
markrad | 0:cdf462088d13 | 402 | } |
markrad | 0:cdf462088d13 | 403 | |
markrad | 0:cdf462088d13 | 404 | static size_t rsa_alt_get_bitlen( const void *ctx ) |
markrad | 0:cdf462088d13 | 405 | { |
markrad | 0:cdf462088d13 | 406 | const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx; |
markrad | 0:cdf462088d13 | 407 | |
markrad | 0:cdf462088d13 | 408 | return( 8 * rsa_alt->key_len_func( rsa_alt->key ) ); |
markrad | 0:cdf462088d13 | 409 | } |
markrad | 0:cdf462088d13 | 410 | |
markrad | 0:cdf462088d13 | 411 | static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 412 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 413 | unsigned char *sig, size_t *sig_len, |
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 | |
Jasper Wallace |
1:9ebc941037d5 | 418 | #if defined(MBEDTLS_HAVE_INT64) |
Jasper Wallace |
1:9ebc941037d5 | 419 | if( UINT_MAX < hash_len ) |
Jasper Wallace |
1:9ebc941037d5 | 420 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
Jasper Wallace |
1:9ebc941037d5 | 421 | #endif /* MBEDTLS_HAVE_INT64 */ |
Jasper Wallace |
1:9ebc941037d5 | 422 | |
markrad | 0:cdf462088d13 | 423 | *sig_len = rsa_alt->key_len_func( rsa_alt->key ); |
markrad | 0:cdf462088d13 | 424 | |
markrad | 0:cdf462088d13 | 425 | return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, |
markrad | 0:cdf462088d13 | 426 | md_alg, (unsigned int) hash_len, hash, sig ) ); |
markrad | 0:cdf462088d13 | 427 | } |
markrad | 0:cdf462088d13 | 428 | |
markrad | 0:cdf462088d13 | 429 | static int rsa_alt_decrypt_wrap( void *ctx, |
markrad | 0:cdf462088d13 | 430 | const unsigned char *input, size_t ilen, |
markrad | 0:cdf462088d13 | 431 | unsigned char *output, size_t *olen, size_t osize, |
markrad | 0:cdf462088d13 | 432 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
markrad | 0:cdf462088d13 | 433 | { |
markrad | 0:cdf462088d13 | 434 | mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; |
markrad | 0:cdf462088d13 | 435 | |
markrad | 0:cdf462088d13 | 436 | ((void) f_rng); |
markrad | 0:cdf462088d13 | 437 | ((void) p_rng); |
markrad | 0:cdf462088d13 | 438 | |
markrad | 0:cdf462088d13 | 439 | if( ilen != rsa_alt->key_len_func( rsa_alt->key ) ) |
markrad | 0:cdf462088d13 | 440 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 441 | |
markrad | 0:cdf462088d13 | 442 | return( rsa_alt->decrypt_func( rsa_alt->key, |
markrad | 0:cdf462088d13 | 443 | MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) ); |
markrad | 0:cdf462088d13 | 444 | } |
markrad | 0:cdf462088d13 | 445 | |
markrad | 0:cdf462088d13 | 446 | #if defined(MBEDTLS_RSA_C) |
markrad | 0:cdf462088d13 | 447 | static int rsa_alt_check_pair( const void *pub, const void *prv ) |
markrad | 0:cdf462088d13 | 448 | { |
markrad | 0:cdf462088d13 | 449 | unsigned char sig[MBEDTLS_MPI_MAX_SIZE]; |
markrad | 0:cdf462088d13 | 450 | unsigned char hash[32]; |
markrad | 0:cdf462088d13 | 451 | size_t sig_len = 0; |
markrad | 0:cdf462088d13 | 452 | int ret; |
markrad | 0:cdf462088d13 | 453 | |
markrad | 0:cdf462088d13 | 454 | if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) ) |
markrad | 0:cdf462088d13 | 455 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
markrad | 0:cdf462088d13 | 456 | |
markrad | 0:cdf462088d13 | 457 | memset( hash, 0x2a, sizeof( hash ) ); |
markrad | 0:cdf462088d13 | 458 | |
markrad | 0:cdf462088d13 | 459 | if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE, |
markrad | 0:cdf462088d13 | 460 | hash, sizeof( hash ), |
markrad | 0:cdf462088d13 | 461 | sig, &sig_len, NULL, NULL ) ) != 0 ) |
markrad | 0:cdf462088d13 | 462 | { |
markrad | 0:cdf462088d13 | 463 | return( ret ); |
markrad | 0:cdf462088d13 | 464 | } |
markrad | 0:cdf462088d13 | 465 | |
markrad | 0:cdf462088d13 | 466 | if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE, |
markrad | 0:cdf462088d13 | 467 | hash, sizeof( hash ), sig, sig_len ) != 0 ) |
markrad | 0:cdf462088d13 | 468 | { |
markrad | 0:cdf462088d13 | 469 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
markrad | 0:cdf462088d13 | 470 | } |
markrad | 0:cdf462088d13 | 471 | |
markrad | 0:cdf462088d13 | 472 | return( 0 ); |
markrad | 0:cdf462088d13 | 473 | } |
markrad | 0:cdf462088d13 | 474 | #endif /* MBEDTLS_RSA_C */ |
markrad | 0:cdf462088d13 | 475 | |
markrad | 0:cdf462088d13 | 476 | static void *rsa_alt_alloc_wrap( void ) |
markrad | 0:cdf462088d13 | 477 | { |
markrad | 0:cdf462088d13 | 478 | void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) ); |
markrad | 0:cdf462088d13 | 479 | |
markrad | 0:cdf462088d13 | 480 | if( ctx != NULL ) |
markrad | 0:cdf462088d13 | 481 | memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) ); |
markrad | 0:cdf462088d13 | 482 | |
markrad | 0:cdf462088d13 | 483 | return( ctx ); |
markrad | 0:cdf462088d13 | 484 | } |
markrad | 0:cdf462088d13 | 485 | |
markrad | 0:cdf462088d13 | 486 | static void rsa_alt_free_wrap( void *ctx ) |
markrad | 0:cdf462088d13 | 487 | { |
markrad | 0:cdf462088d13 | 488 | mbedtls_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) ); |
markrad | 0:cdf462088d13 | 489 | mbedtls_free( ctx ); |
markrad | 0:cdf462088d13 | 490 | } |
markrad | 0:cdf462088d13 | 491 | |
markrad | 0:cdf462088d13 | 492 | const mbedtls_pk_info_t mbedtls_rsa_alt_info = { |
markrad | 0:cdf462088d13 | 493 | MBEDTLS_PK_RSA_ALT, |
markrad | 0:cdf462088d13 | 494 | "RSA-alt", |
markrad | 0:cdf462088d13 | 495 | rsa_alt_get_bitlen, |
markrad | 0:cdf462088d13 | 496 | rsa_alt_can_do, |
markrad | 0:cdf462088d13 | 497 | NULL, |
markrad | 0:cdf462088d13 | 498 | rsa_alt_sign_wrap, |
markrad | 0:cdf462088d13 | 499 | rsa_alt_decrypt_wrap, |
markrad | 0:cdf462088d13 | 500 | NULL, |
markrad | 0:cdf462088d13 | 501 | #if defined(MBEDTLS_RSA_C) |
markrad | 0:cdf462088d13 | 502 | rsa_alt_check_pair, |
markrad | 0:cdf462088d13 | 503 | #else |
markrad | 0:cdf462088d13 | 504 | NULL, |
markrad | 0:cdf462088d13 | 505 | #endif |
markrad | 0:cdf462088d13 | 506 | rsa_alt_alloc_wrap, |
markrad | 0:cdf462088d13 | 507 | rsa_alt_free_wrap, |
markrad | 0:cdf462088d13 | 508 | NULL, |
markrad | 0:cdf462088d13 | 509 | }; |
markrad | 0:cdf462088d13 | 510 | |
markrad | 0:cdf462088d13 | 511 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
markrad | 0:cdf462088d13 | 512 | |
markrad | 0:cdf462088d13 | 513 | #endif /* MBEDTLS_PK_C */ |