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