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