Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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