Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/library/pk_wrap.c@0:796d0f61a05b, 2018-09-27 (annotated)
- Committer:
- HannesTschofenig
- Date:
- Thu Sep 27 06:34:22 2018 +0000
- Revision:
- 0:796d0f61a05b
Example AES-GCM test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /* |
HannesTschofenig | 0:796d0f61a05b | 2 | * Public Key abstraction layer: wrapper functions |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * Copyright (C) 2006-2014, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 8 | * |
HannesTschofenig | 0:796d0f61a05b | 9 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 12 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 13 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 14 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 15 | * |
HannesTschofenig | 0:796d0f61a05b | 16 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 19 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 20 | * |
HannesTschofenig | 0:796d0f61a05b | 21 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 24 | */ |
HannesTschofenig | 0:796d0f61a05b | 25 | |
HannesTschofenig | 0:796d0f61a05b | 26 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 27 | #include "polarssl/config.h" |
HannesTschofenig | 0:796d0f61a05b | 28 | #else |
HannesTschofenig | 0:796d0f61a05b | 29 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 30 | #endif |
HannesTschofenig | 0:796d0f61a05b | 31 | |
HannesTschofenig | 0:796d0f61a05b | 32 | #if defined(POLARSSL_PK_C) |
HannesTschofenig | 0:796d0f61a05b | 33 | |
HannesTschofenig | 0:796d0f61a05b | 34 | #include "polarssl/pk_wrap.h" |
HannesTschofenig | 0:796d0f61a05b | 35 | |
HannesTschofenig | 0:796d0f61a05b | 36 | /* Even if RSA not activated, for the sake of RSA-alt */ |
HannesTschofenig | 0:796d0f61a05b | 37 | #include "polarssl/rsa.h" |
HannesTschofenig | 0:796d0f61a05b | 38 | |
HannesTschofenig | 0:796d0f61a05b | 39 | #if defined(POLARSSL_ECP_C) |
HannesTschofenig | 0:796d0f61a05b | 40 | #include "polarssl/ecp.h" |
HannesTschofenig | 0:796d0f61a05b | 41 | #endif |
HannesTschofenig | 0:796d0f61a05b | 42 | |
HannesTschofenig | 0:796d0f61a05b | 43 | #if defined(POLARSSL_ECDSA_C) |
HannesTschofenig | 0:796d0f61a05b | 44 | #include "polarssl/ecdsa.h" |
HannesTschofenig | 0:796d0f61a05b | 45 | #endif |
HannesTschofenig | 0:796d0f61a05b | 46 | |
HannesTschofenig | 0:796d0f61a05b | 47 | #if defined(POLARSSL_PLATFORM_C) |
HannesTschofenig | 0:796d0f61a05b | 48 | #include "polarssl/platform.h" |
HannesTschofenig | 0:796d0f61a05b | 49 | #else |
HannesTschofenig | 0:796d0f61a05b | 50 | #include <stdlib.h> |
HannesTschofenig | 0:796d0f61a05b | 51 | #define polarssl_malloc malloc |
HannesTschofenig | 0:796d0f61a05b | 52 | #define polarssl_free free |
HannesTschofenig | 0:796d0f61a05b | 53 | #endif |
HannesTschofenig | 0:796d0f61a05b | 54 | |
HannesTschofenig | 0:796d0f61a05b | 55 | /* Used by RSA-alt too */ |
HannesTschofenig | 0:796d0f61a05b | 56 | static int rsa_can_do( pk_type_t type ) |
HannesTschofenig | 0:796d0f61a05b | 57 | { |
HannesTschofenig | 0:796d0f61a05b | 58 | return( type == POLARSSL_PK_RSA ); |
HannesTschofenig | 0:796d0f61a05b | 59 | } |
HannesTschofenig | 0:796d0f61a05b | 60 | |
HannesTschofenig | 0:796d0f61a05b | 61 | #if defined(POLARSSL_RSA_C) |
HannesTschofenig | 0:796d0f61a05b | 62 | static size_t rsa_get_size( const void *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 63 | { |
HannesTschofenig | 0:796d0f61a05b | 64 | return( 8 * ((const rsa_context *) ctx)->len ); |
HannesTschofenig | 0:796d0f61a05b | 65 | } |
HannesTschofenig | 0:796d0f61a05b | 66 | |
HannesTschofenig | 0:796d0f61a05b | 67 | static int rsa_verify_wrap( void *ctx, md_type_t md_alg, |
HannesTschofenig | 0:796d0f61a05b | 68 | const unsigned char *hash, size_t hash_len, |
HannesTschofenig | 0:796d0f61a05b | 69 | const unsigned char *sig, size_t sig_len ) |
HannesTschofenig | 0:796d0f61a05b | 70 | { |
HannesTschofenig | 0:796d0f61a05b | 71 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 72 | |
HannesTschofenig | 0:796d0f61a05b | 73 | if( sig_len < ((rsa_context *) ctx)->len ) |
HannesTschofenig | 0:796d0f61a05b | 74 | return( POLARSSL_ERR_RSA_VERIFY_FAILED ); |
HannesTschofenig | 0:796d0f61a05b | 75 | |
HannesTschofenig | 0:796d0f61a05b | 76 | if( ( ret = rsa_pkcs1_verify( (rsa_context *) ctx, NULL, NULL, |
HannesTschofenig | 0:796d0f61a05b | 77 | RSA_PUBLIC, md_alg, |
HannesTschofenig | 0:796d0f61a05b | 78 | (unsigned int) hash_len, hash, sig ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 79 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 80 | |
HannesTschofenig | 0:796d0f61a05b | 81 | if( sig_len > ((rsa_context *) ctx)->len ) |
HannesTschofenig | 0:796d0f61a05b | 82 | return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH ); |
HannesTschofenig | 0:796d0f61a05b | 83 | |
HannesTschofenig | 0:796d0f61a05b | 84 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 85 | } |
HannesTschofenig | 0:796d0f61a05b | 86 | |
HannesTschofenig | 0:796d0f61a05b | 87 | static int rsa_sign_wrap( void *ctx, md_type_t md_alg, |
HannesTschofenig | 0:796d0f61a05b | 88 | const unsigned char *hash, size_t hash_len, |
HannesTschofenig | 0:796d0f61a05b | 89 | unsigned char *sig, size_t *sig_len, |
HannesTschofenig | 0:796d0f61a05b | 90 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
HannesTschofenig | 0:796d0f61a05b | 91 | { |
HannesTschofenig | 0:796d0f61a05b | 92 | *sig_len = ((rsa_context *) ctx)->len; |
HannesTschofenig | 0:796d0f61a05b | 93 | |
HannesTschofenig | 0:796d0f61a05b | 94 | return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, RSA_PRIVATE, |
HannesTschofenig | 0:796d0f61a05b | 95 | md_alg, (unsigned int) hash_len, hash, sig ) ); |
HannesTschofenig | 0:796d0f61a05b | 96 | } |
HannesTschofenig | 0:796d0f61a05b | 97 | |
HannesTschofenig | 0:796d0f61a05b | 98 | static int rsa_decrypt_wrap( void *ctx, |
HannesTschofenig | 0:796d0f61a05b | 99 | const unsigned char *input, size_t ilen, |
HannesTschofenig | 0:796d0f61a05b | 100 | unsigned char *output, size_t *olen, size_t osize, |
HannesTschofenig | 0:796d0f61a05b | 101 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
HannesTschofenig | 0:796d0f61a05b | 102 | { |
HannesTschofenig | 0:796d0f61a05b | 103 | if( ilen != ((rsa_context *) ctx)->len ) |
HannesTschofenig | 0:796d0f61a05b | 104 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 105 | |
HannesTschofenig | 0:796d0f61a05b | 106 | return( rsa_pkcs1_decrypt( (rsa_context *) ctx, f_rng, p_rng, |
HannesTschofenig | 0:796d0f61a05b | 107 | RSA_PRIVATE, olen, input, output, osize ) ); |
HannesTschofenig | 0:796d0f61a05b | 108 | } |
HannesTschofenig | 0:796d0f61a05b | 109 | |
HannesTschofenig | 0:796d0f61a05b | 110 | static int rsa_encrypt_wrap( void *ctx, |
HannesTschofenig | 0:796d0f61a05b | 111 | const unsigned char *input, size_t ilen, |
HannesTschofenig | 0:796d0f61a05b | 112 | unsigned char *output, size_t *olen, size_t osize, |
HannesTschofenig | 0:796d0f61a05b | 113 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
HannesTschofenig | 0:796d0f61a05b | 114 | { |
HannesTschofenig | 0:796d0f61a05b | 115 | ((void) osize); |
HannesTschofenig | 0:796d0f61a05b | 116 | |
HannesTschofenig | 0:796d0f61a05b | 117 | *olen = ((rsa_context *) ctx)->len; |
HannesTschofenig | 0:796d0f61a05b | 118 | |
HannesTschofenig | 0:796d0f61a05b | 119 | return( rsa_pkcs1_encrypt( (rsa_context *) ctx, |
HannesTschofenig | 0:796d0f61a05b | 120 | f_rng, p_rng, RSA_PUBLIC, ilen, input, output ) ); |
HannesTschofenig | 0:796d0f61a05b | 121 | } |
HannesTschofenig | 0:796d0f61a05b | 122 | |
HannesTschofenig | 0:796d0f61a05b | 123 | static void *rsa_alloc_wrap( void ) |
HannesTschofenig | 0:796d0f61a05b | 124 | { |
HannesTschofenig | 0:796d0f61a05b | 125 | void *ctx = polarssl_malloc( sizeof( rsa_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 126 | |
HannesTschofenig | 0:796d0f61a05b | 127 | if( ctx != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 128 | rsa_init( (rsa_context *) ctx, 0, 0 ); |
HannesTschofenig | 0:796d0f61a05b | 129 | |
HannesTschofenig | 0:796d0f61a05b | 130 | return ctx; |
HannesTschofenig | 0:796d0f61a05b | 131 | } |
HannesTschofenig | 0:796d0f61a05b | 132 | |
HannesTschofenig | 0:796d0f61a05b | 133 | static void rsa_free_wrap( void *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 134 | { |
HannesTschofenig | 0:796d0f61a05b | 135 | rsa_free( (rsa_context *) ctx ); |
HannesTschofenig | 0:796d0f61a05b | 136 | polarssl_free( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 137 | } |
HannesTschofenig | 0:796d0f61a05b | 138 | |
HannesTschofenig | 0:796d0f61a05b | 139 | static void rsa_debug( const void *ctx, pk_debug_item *items ) |
HannesTschofenig | 0:796d0f61a05b | 140 | { |
HannesTschofenig | 0:796d0f61a05b | 141 | items->type = POLARSSL_PK_DEBUG_MPI; |
HannesTschofenig | 0:796d0f61a05b | 142 | items->name = "rsa.N"; |
HannesTschofenig | 0:796d0f61a05b | 143 | items->value = &( ((rsa_context *) ctx)->N ); |
HannesTschofenig | 0:796d0f61a05b | 144 | |
HannesTschofenig | 0:796d0f61a05b | 145 | items++; |
HannesTschofenig | 0:796d0f61a05b | 146 | |
HannesTschofenig | 0:796d0f61a05b | 147 | items->type = POLARSSL_PK_DEBUG_MPI; |
HannesTschofenig | 0:796d0f61a05b | 148 | items->name = "rsa.E"; |
HannesTschofenig | 0:796d0f61a05b | 149 | items->value = &( ((rsa_context *) ctx)->E ); |
HannesTschofenig | 0:796d0f61a05b | 150 | } |
HannesTschofenig | 0:796d0f61a05b | 151 | |
HannesTschofenig | 0:796d0f61a05b | 152 | const pk_info_t rsa_info = { |
HannesTschofenig | 0:796d0f61a05b | 153 | POLARSSL_PK_RSA, |
HannesTschofenig | 0:796d0f61a05b | 154 | "RSA", |
HannesTschofenig | 0:796d0f61a05b | 155 | rsa_get_size, |
HannesTschofenig | 0:796d0f61a05b | 156 | rsa_can_do, |
HannesTschofenig | 0:796d0f61a05b | 157 | rsa_verify_wrap, |
HannesTschofenig | 0:796d0f61a05b | 158 | rsa_sign_wrap, |
HannesTschofenig | 0:796d0f61a05b | 159 | rsa_decrypt_wrap, |
HannesTschofenig | 0:796d0f61a05b | 160 | rsa_encrypt_wrap, |
HannesTschofenig | 0:796d0f61a05b | 161 | rsa_alloc_wrap, |
HannesTschofenig | 0:796d0f61a05b | 162 | rsa_free_wrap, |
HannesTschofenig | 0:796d0f61a05b | 163 | rsa_debug, |
HannesTschofenig | 0:796d0f61a05b | 164 | }; |
HannesTschofenig | 0:796d0f61a05b | 165 | #endif /* POLARSSL_RSA_C */ |
HannesTschofenig | 0:796d0f61a05b | 166 | |
HannesTschofenig | 0:796d0f61a05b | 167 | #if defined(POLARSSL_ECP_C) |
HannesTschofenig | 0:796d0f61a05b | 168 | /* |
HannesTschofenig | 0:796d0f61a05b | 169 | * Generic EC key |
HannesTschofenig | 0:796d0f61a05b | 170 | */ |
HannesTschofenig | 0:796d0f61a05b | 171 | static int eckey_can_do( pk_type_t type ) |
HannesTschofenig | 0:796d0f61a05b | 172 | { |
HannesTschofenig | 0:796d0f61a05b | 173 | return( type == POLARSSL_PK_ECKEY || |
HannesTschofenig | 0:796d0f61a05b | 174 | type == POLARSSL_PK_ECKEY_DH || |
HannesTschofenig | 0:796d0f61a05b | 175 | type == POLARSSL_PK_ECDSA ); |
HannesTschofenig | 0:796d0f61a05b | 176 | } |
HannesTschofenig | 0:796d0f61a05b | 177 | |
HannesTschofenig | 0:796d0f61a05b | 178 | static size_t eckey_get_size( const void *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 179 | { |
HannesTschofenig | 0:796d0f61a05b | 180 | return( ((ecp_keypair *) ctx)->grp.pbits ); |
HannesTschofenig | 0:796d0f61a05b | 181 | } |
HannesTschofenig | 0:796d0f61a05b | 182 | |
HannesTschofenig | 0:796d0f61a05b | 183 | #if defined(POLARSSL_ECDSA_C) |
HannesTschofenig | 0:796d0f61a05b | 184 | /* Forward declarations */ |
HannesTschofenig | 0:796d0f61a05b | 185 | static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg, |
HannesTschofenig | 0:796d0f61a05b | 186 | const unsigned char *hash, size_t hash_len, |
HannesTschofenig | 0:796d0f61a05b | 187 | const unsigned char *sig, size_t sig_len ); |
HannesTschofenig | 0:796d0f61a05b | 188 | |
HannesTschofenig | 0:796d0f61a05b | 189 | static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg, |
HannesTschofenig | 0:796d0f61a05b | 190 | const unsigned char *hash, size_t hash_len, |
HannesTschofenig | 0:796d0f61a05b | 191 | unsigned char *sig, size_t *sig_len, |
HannesTschofenig | 0:796d0f61a05b | 192 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
HannesTschofenig | 0:796d0f61a05b | 193 | |
HannesTschofenig | 0:796d0f61a05b | 194 | static int eckey_verify_wrap( void *ctx, md_type_t md_alg, |
HannesTschofenig | 0:796d0f61a05b | 195 | const unsigned char *hash, size_t hash_len, |
HannesTschofenig | 0:796d0f61a05b | 196 | const unsigned char *sig, size_t sig_len ) |
HannesTschofenig | 0:796d0f61a05b | 197 | { |
HannesTschofenig | 0:796d0f61a05b | 198 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 199 | ecdsa_context ecdsa; |
HannesTschofenig | 0:796d0f61a05b | 200 | |
HannesTschofenig | 0:796d0f61a05b | 201 | ecdsa_init( &ecdsa ); |
HannesTschofenig | 0:796d0f61a05b | 202 | |
HannesTschofenig | 0:796d0f61a05b | 203 | if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 204 | ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len ); |
HannesTschofenig | 0:796d0f61a05b | 205 | |
HannesTschofenig | 0:796d0f61a05b | 206 | ecdsa_free( &ecdsa ); |
HannesTschofenig | 0:796d0f61a05b | 207 | |
HannesTschofenig | 0:796d0f61a05b | 208 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 209 | } |
HannesTschofenig | 0:796d0f61a05b | 210 | |
HannesTschofenig | 0:796d0f61a05b | 211 | static int eckey_sign_wrap( void *ctx, md_type_t md_alg, |
HannesTschofenig | 0:796d0f61a05b | 212 | const unsigned char *hash, size_t hash_len, |
HannesTschofenig | 0:796d0f61a05b | 213 | unsigned char *sig, size_t *sig_len, |
HannesTschofenig | 0:796d0f61a05b | 214 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
HannesTschofenig | 0:796d0f61a05b | 215 | { |
HannesTschofenig | 0:796d0f61a05b | 216 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 217 | ecdsa_context ecdsa; |
HannesTschofenig | 0:796d0f61a05b | 218 | |
HannesTschofenig | 0:796d0f61a05b | 219 | ecdsa_init( &ecdsa ); |
HannesTschofenig | 0:796d0f61a05b | 220 | |
HannesTschofenig | 0:796d0f61a05b | 221 | if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 222 | ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len, |
HannesTschofenig | 0:796d0f61a05b | 223 | f_rng, p_rng ); |
HannesTschofenig | 0:796d0f61a05b | 224 | |
HannesTschofenig | 0:796d0f61a05b | 225 | ecdsa_free( &ecdsa ); |
HannesTschofenig | 0:796d0f61a05b | 226 | |
HannesTschofenig | 0:796d0f61a05b | 227 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 228 | } |
HannesTschofenig | 0:796d0f61a05b | 229 | |
HannesTschofenig | 0:796d0f61a05b | 230 | #endif /* POLARSSL_ECDSA_C */ |
HannesTschofenig | 0:796d0f61a05b | 231 | |
HannesTschofenig | 0:796d0f61a05b | 232 | static void *eckey_alloc_wrap( void ) |
HannesTschofenig | 0:796d0f61a05b | 233 | { |
HannesTschofenig | 0:796d0f61a05b | 234 | void *ctx = polarssl_malloc( sizeof( ecp_keypair ) ); |
HannesTschofenig | 0:796d0f61a05b | 235 | |
HannesTschofenig | 0:796d0f61a05b | 236 | if( ctx != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 237 | ecp_keypair_init( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 238 | |
HannesTschofenig | 0:796d0f61a05b | 239 | return( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 240 | } |
HannesTschofenig | 0:796d0f61a05b | 241 | |
HannesTschofenig | 0:796d0f61a05b | 242 | static void eckey_free_wrap( void *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 243 | { |
HannesTschofenig | 0:796d0f61a05b | 244 | ecp_keypair_free( (ecp_keypair *) ctx ); |
HannesTschofenig | 0:796d0f61a05b | 245 | polarssl_free( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 246 | } |
HannesTschofenig | 0:796d0f61a05b | 247 | |
HannesTschofenig | 0:796d0f61a05b | 248 | static void eckey_debug( const void *ctx, pk_debug_item *items ) |
HannesTschofenig | 0:796d0f61a05b | 249 | { |
HannesTschofenig | 0:796d0f61a05b | 250 | items->type = POLARSSL_PK_DEBUG_ECP; |
HannesTschofenig | 0:796d0f61a05b | 251 | items->name = "eckey.Q"; |
HannesTschofenig | 0:796d0f61a05b | 252 | items->value = &( ((ecp_keypair *) ctx)->Q ); |
HannesTschofenig | 0:796d0f61a05b | 253 | } |
HannesTschofenig | 0:796d0f61a05b | 254 | |
HannesTschofenig | 0:796d0f61a05b | 255 | const pk_info_t eckey_info = { |
HannesTschofenig | 0:796d0f61a05b | 256 | POLARSSL_PK_ECKEY, |
HannesTschofenig | 0:796d0f61a05b | 257 | "EC", |
HannesTschofenig | 0:796d0f61a05b | 258 | eckey_get_size, |
HannesTschofenig | 0:796d0f61a05b | 259 | eckey_can_do, |
HannesTschofenig | 0:796d0f61a05b | 260 | #if defined(POLARSSL_ECDSA_C) |
HannesTschofenig | 0:796d0f61a05b | 261 | eckey_verify_wrap, |
HannesTschofenig | 0:796d0f61a05b | 262 | eckey_sign_wrap, |
HannesTschofenig | 0:796d0f61a05b | 263 | #else |
HannesTschofenig | 0:796d0f61a05b | 264 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 265 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 266 | #endif |
HannesTschofenig | 0:796d0f61a05b | 267 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 268 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 269 | eckey_alloc_wrap, |
HannesTschofenig | 0:796d0f61a05b | 270 | eckey_free_wrap, |
HannesTschofenig | 0:796d0f61a05b | 271 | eckey_debug, |
HannesTschofenig | 0:796d0f61a05b | 272 | }; |
HannesTschofenig | 0:796d0f61a05b | 273 | |
HannesTschofenig | 0:796d0f61a05b | 274 | /* |
HannesTschofenig | 0:796d0f61a05b | 275 | * EC key restricted to ECDH |
HannesTschofenig | 0:796d0f61a05b | 276 | */ |
HannesTschofenig | 0:796d0f61a05b | 277 | static int eckeydh_can_do( pk_type_t type ) |
HannesTschofenig | 0:796d0f61a05b | 278 | { |
HannesTschofenig | 0:796d0f61a05b | 279 | return( type == POLARSSL_PK_ECKEY || |
HannesTschofenig | 0:796d0f61a05b | 280 | type == POLARSSL_PK_ECKEY_DH ); |
HannesTschofenig | 0:796d0f61a05b | 281 | } |
HannesTschofenig | 0:796d0f61a05b | 282 | |
HannesTschofenig | 0:796d0f61a05b | 283 | const pk_info_t eckeydh_info = { |
HannesTschofenig | 0:796d0f61a05b | 284 | POLARSSL_PK_ECKEY_DH, |
HannesTschofenig | 0:796d0f61a05b | 285 | "EC_DH", |
HannesTschofenig | 0:796d0f61a05b | 286 | eckey_get_size, /* Same underlying key structure */ |
HannesTschofenig | 0:796d0f61a05b | 287 | eckeydh_can_do, |
HannesTschofenig | 0:796d0f61a05b | 288 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 289 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 290 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 291 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 292 | eckey_alloc_wrap, /* Same underlying key structure */ |
HannesTschofenig | 0:796d0f61a05b | 293 | eckey_free_wrap, /* Same underlying key structure */ |
HannesTschofenig | 0:796d0f61a05b | 294 | eckey_debug, /* Same underlying key structure */ |
HannesTschofenig | 0:796d0f61a05b | 295 | }; |
HannesTschofenig | 0:796d0f61a05b | 296 | #endif /* POLARSSL_ECP_C */ |
HannesTschofenig | 0:796d0f61a05b | 297 | |
HannesTschofenig | 0:796d0f61a05b | 298 | #if defined(POLARSSL_ECDSA_C) |
HannesTschofenig | 0:796d0f61a05b | 299 | static int ecdsa_can_do( pk_type_t type ) |
HannesTschofenig | 0:796d0f61a05b | 300 | { |
HannesTschofenig | 0:796d0f61a05b | 301 | return( type == POLARSSL_PK_ECDSA ); |
HannesTschofenig | 0:796d0f61a05b | 302 | } |
HannesTschofenig | 0:796d0f61a05b | 303 | |
HannesTschofenig | 0:796d0f61a05b | 304 | static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg, |
HannesTschofenig | 0:796d0f61a05b | 305 | const unsigned char *hash, size_t hash_len, |
HannesTschofenig | 0:796d0f61a05b | 306 | const unsigned char *sig, size_t sig_len ) |
HannesTschofenig | 0:796d0f61a05b | 307 | { |
HannesTschofenig | 0:796d0f61a05b | 308 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 309 | ((void) md_alg); |
HannesTschofenig | 0:796d0f61a05b | 310 | |
HannesTschofenig | 0:796d0f61a05b | 311 | ret = ecdsa_read_signature( (ecdsa_context *) ctx, |
HannesTschofenig | 0:796d0f61a05b | 312 | hash, hash_len, sig, sig_len ); |
HannesTschofenig | 0:796d0f61a05b | 313 | |
HannesTschofenig | 0:796d0f61a05b | 314 | if( ret == POLARSSL_ERR_ECP_SIG_LEN_MISMATCH ) |
HannesTschofenig | 0:796d0f61a05b | 315 | return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH ); |
HannesTschofenig | 0:796d0f61a05b | 316 | |
HannesTschofenig | 0:796d0f61a05b | 317 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 318 | } |
HannesTschofenig | 0:796d0f61a05b | 319 | |
HannesTschofenig | 0:796d0f61a05b | 320 | static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg, |
HannesTschofenig | 0:796d0f61a05b | 321 | const unsigned char *hash, size_t hash_len, |
HannesTschofenig | 0:796d0f61a05b | 322 | unsigned char *sig, size_t *sig_len, |
HannesTschofenig | 0:796d0f61a05b | 323 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
HannesTschofenig | 0:796d0f61a05b | 324 | { |
HannesTschofenig | 0:796d0f61a05b | 325 | /* Use deterministic ECDSA by default if available */ |
HannesTschofenig | 0:796d0f61a05b | 326 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
HannesTschofenig | 0:796d0f61a05b | 327 | ((void) f_rng); |
HannesTschofenig | 0:796d0f61a05b | 328 | ((void) p_rng); |
HannesTschofenig | 0:796d0f61a05b | 329 | |
HannesTschofenig | 0:796d0f61a05b | 330 | return( ecdsa_write_signature_det( (ecdsa_context *) ctx, |
HannesTschofenig | 0:796d0f61a05b | 331 | hash, hash_len, sig, sig_len, md_alg ) ); |
HannesTschofenig | 0:796d0f61a05b | 332 | #else |
HannesTschofenig | 0:796d0f61a05b | 333 | ((void) md_alg); |
HannesTschofenig | 0:796d0f61a05b | 334 | |
HannesTschofenig | 0:796d0f61a05b | 335 | return( ecdsa_write_signature( (ecdsa_context *) ctx, |
HannesTschofenig | 0:796d0f61a05b | 336 | hash, hash_len, sig, sig_len, f_rng, p_rng ) ); |
HannesTschofenig | 0:796d0f61a05b | 337 | #endif /* POLARSSL_ECDSA_DETERMINISTIC */ |
HannesTschofenig | 0:796d0f61a05b | 338 | } |
HannesTschofenig | 0:796d0f61a05b | 339 | |
HannesTschofenig | 0:796d0f61a05b | 340 | static void *ecdsa_alloc_wrap( void ) |
HannesTschofenig | 0:796d0f61a05b | 341 | { |
HannesTschofenig | 0:796d0f61a05b | 342 | void *ctx = polarssl_malloc( sizeof( ecdsa_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 343 | |
HannesTschofenig | 0:796d0f61a05b | 344 | if( ctx != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 345 | ecdsa_init( (ecdsa_context *) ctx ); |
HannesTschofenig | 0:796d0f61a05b | 346 | |
HannesTschofenig | 0:796d0f61a05b | 347 | return( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 348 | } |
HannesTschofenig | 0:796d0f61a05b | 349 | |
HannesTschofenig | 0:796d0f61a05b | 350 | static void ecdsa_free_wrap( void *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 351 | { |
HannesTschofenig | 0:796d0f61a05b | 352 | ecdsa_free( (ecdsa_context *) ctx ); |
HannesTschofenig | 0:796d0f61a05b | 353 | polarssl_free( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 354 | } |
HannesTschofenig | 0:796d0f61a05b | 355 | |
HannesTschofenig | 0:796d0f61a05b | 356 | const pk_info_t ecdsa_info = { |
HannesTschofenig | 0:796d0f61a05b | 357 | POLARSSL_PK_ECDSA, |
HannesTschofenig | 0:796d0f61a05b | 358 | "ECDSA", |
HannesTschofenig | 0:796d0f61a05b | 359 | eckey_get_size, /* Compatible key structures */ |
HannesTschofenig | 0:796d0f61a05b | 360 | ecdsa_can_do, |
HannesTschofenig | 0:796d0f61a05b | 361 | ecdsa_verify_wrap, |
HannesTschofenig | 0:796d0f61a05b | 362 | ecdsa_sign_wrap, |
HannesTschofenig | 0:796d0f61a05b | 363 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 364 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 365 | ecdsa_alloc_wrap, |
HannesTschofenig | 0:796d0f61a05b | 366 | ecdsa_free_wrap, |
HannesTschofenig | 0:796d0f61a05b | 367 | eckey_debug, /* Compatible key structures */ |
HannesTschofenig | 0:796d0f61a05b | 368 | }; |
HannesTschofenig | 0:796d0f61a05b | 369 | #endif /* POLARSSL_ECDSA_C */ |
HannesTschofenig | 0:796d0f61a05b | 370 | |
HannesTschofenig | 0:796d0f61a05b | 371 | /* |
HannesTschofenig | 0:796d0f61a05b | 372 | * Support for alternative RSA-private implementations |
HannesTschofenig | 0:796d0f61a05b | 373 | */ |
HannesTschofenig | 0:796d0f61a05b | 374 | |
HannesTschofenig | 0:796d0f61a05b | 375 | static size_t rsa_alt_get_size( const void *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 376 | { |
HannesTschofenig | 0:796d0f61a05b | 377 | const rsa_alt_context *rsa_alt = (const rsa_alt_context *) ctx; |
HannesTschofenig | 0:796d0f61a05b | 378 | |
HannesTschofenig | 0:796d0f61a05b | 379 | return( 8 * rsa_alt->key_len_func( rsa_alt->key ) ); |
HannesTschofenig | 0:796d0f61a05b | 380 | } |
HannesTschofenig | 0:796d0f61a05b | 381 | |
HannesTschofenig | 0:796d0f61a05b | 382 | static int rsa_alt_sign_wrap( void *ctx, md_type_t md_alg, |
HannesTschofenig | 0:796d0f61a05b | 383 | const unsigned char *hash, size_t hash_len, |
HannesTschofenig | 0:796d0f61a05b | 384 | unsigned char *sig, size_t *sig_len, |
HannesTschofenig | 0:796d0f61a05b | 385 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
HannesTschofenig | 0:796d0f61a05b | 386 | { |
HannesTschofenig | 0:796d0f61a05b | 387 | rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx; |
HannesTschofenig | 0:796d0f61a05b | 388 | |
HannesTschofenig | 0:796d0f61a05b | 389 | *sig_len = rsa_alt->key_len_func( rsa_alt->key ); |
HannesTschofenig | 0:796d0f61a05b | 390 | |
HannesTschofenig | 0:796d0f61a05b | 391 | return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, RSA_PRIVATE, |
HannesTschofenig | 0:796d0f61a05b | 392 | md_alg, (unsigned int) hash_len, hash, sig ) ); |
HannesTschofenig | 0:796d0f61a05b | 393 | } |
HannesTschofenig | 0:796d0f61a05b | 394 | |
HannesTschofenig | 0:796d0f61a05b | 395 | static int rsa_alt_decrypt_wrap( void *ctx, |
HannesTschofenig | 0:796d0f61a05b | 396 | const unsigned char *input, size_t ilen, |
HannesTschofenig | 0:796d0f61a05b | 397 | unsigned char *output, size_t *olen, size_t osize, |
HannesTschofenig | 0:796d0f61a05b | 398 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
HannesTschofenig | 0:796d0f61a05b | 399 | { |
HannesTschofenig | 0:796d0f61a05b | 400 | rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx; |
HannesTschofenig | 0:796d0f61a05b | 401 | |
HannesTschofenig | 0:796d0f61a05b | 402 | ((void) f_rng); |
HannesTschofenig | 0:796d0f61a05b | 403 | ((void) p_rng); |
HannesTschofenig | 0:796d0f61a05b | 404 | |
HannesTschofenig | 0:796d0f61a05b | 405 | if( ilen != rsa_alt->key_len_func( rsa_alt->key ) ) |
HannesTschofenig | 0:796d0f61a05b | 406 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 407 | |
HannesTschofenig | 0:796d0f61a05b | 408 | return( rsa_alt->decrypt_func( rsa_alt->key, |
HannesTschofenig | 0:796d0f61a05b | 409 | RSA_PRIVATE, olen, input, output, osize ) ); |
HannesTschofenig | 0:796d0f61a05b | 410 | } |
HannesTschofenig | 0:796d0f61a05b | 411 | |
HannesTschofenig | 0:796d0f61a05b | 412 | static void *rsa_alt_alloc_wrap( void ) |
HannesTschofenig | 0:796d0f61a05b | 413 | { |
HannesTschofenig | 0:796d0f61a05b | 414 | void *ctx = polarssl_malloc( sizeof( rsa_alt_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 415 | |
HannesTschofenig | 0:796d0f61a05b | 416 | if( ctx != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 417 | memset( ctx, 0, sizeof( rsa_alt_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 418 | |
HannesTschofenig | 0:796d0f61a05b | 419 | return ctx; |
HannesTschofenig | 0:796d0f61a05b | 420 | } |
HannesTschofenig | 0:796d0f61a05b | 421 | |
HannesTschofenig | 0:796d0f61a05b | 422 | static void rsa_alt_free_wrap( void *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 423 | { |
HannesTschofenig | 0:796d0f61a05b | 424 | polarssl_free( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 425 | } |
HannesTschofenig | 0:796d0f61a05b | 426 | |
HannesTschofenig | 0:796d0f61a05b | 427 | const pk_info_t rsa_alt_info = { |
HannesTschofenig | 0:796d0f61a05b | 428 | POLARSSL_PK_RSA_ALT, |
HannesTschofenig | 0:796d0f61a05b | 429 | "RSA-alt", |
HannesTschofenig | 0:796d0f61a05b | 430 | rsa_alt_get_size, |
HannesTschofenig | 0:796d0f61a05b | 431 | rsa_can_do, |
HannesTschofenig | 0:796d0f61a05b | 432 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 433 | rsa_alt_sign_wrap, |
HannesTschofenig | 0:796d0f61a05b | 434 | rsa_alt_decrypt_wrap, |
HannesTschofenig | 0:796d0f61a05b | 435 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 436 | rsa_alt_alloc_wrap, |
HannesTschofenig | 0:796d0f61a05b | 437 | rsa_alt_free_wrap, |
HannesTschofenig | 0:796d0f61a05b | 438 | NULL, |
HannesTschofenig | 0:796d0f61a05b | 439 | }; |
HannesTschofenig | 0:796d0f61a05b | 440 | |
HannesTschofenig | 0:796d0f61a05b | 441 | #endif /* POLARSSL_PK_C */ |
HannesTschofenig | 0:796d0f61a05b | 442 | |
HannesTschofenig | 0:796d0f61a05b | 443 |