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