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 | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 128 | static int rsa_check_pair_wrap( const void *pub, const void *prv ) |
| 129 | { |
| 130 | return( rsa_check_pub_priv( (const rsa_context *) pub, |
| 131 | (const rsa_context *) prv ) ); |
| 132 | } |
| 133 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 134 | static void *rsa_alloc_wrap( void ) |
| 135 | { |
| 136 | void *ctx = polarssl_malloc( sizeof( rsa_context ) ); |
| 137 | |
| 138 | if( ctx != NULL ) |
| 139 | rsa_init( (rsa_context *) ctx, 0, 0 ); |
| 140 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 141 | return( ctx ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void rsa_free_wrap( void *ctx ) |
| 145 | { |
| 146 | rsa_free( (rsa_context *) ctx ); |
| 147 | polarssl_free( ctx ); |
| 148 | } |
| 149 | |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 150 | static void rsa_debug( const void *ctx, pk_debug_item *items ) |
| 151 | { |
| 152 | items->type = POLARSSL_PK_DEBUG_MPI; |
| 153 | items->name = "rsa.N"; |
| 154 | items->value = &( ((rsa_context *) ctx)->N ); |
| 155 | |
| 156 | items++; |
| 157 | |
| 158 | items->type = POLARSSL_PK_DEBUG_MPI; |
| 159 | items->name = "rsa.E"; |
| 160 | items->value = &( ((rsa_context *) ctx)->E ); |
| 161 | } |
| 162 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 163 | const pk_info_t rsa_info = { |
| 164 | POLARSSL_PK_RSA, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 165 | "RSA", |
| 166 | rsa_get_size, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 167 | rsa_can_do, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 168 | rsa_verify_wrap, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 169 | rsa_sign_wrap, |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 170 | rsa_decrypt_wrap, |
| 171 | rsa_encrypt_wrap, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 172 | rsa_check_pair_wrap, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 173 | rsa_alloc_wrap, |
| 174 | rsa_free_wrap, |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 175 | rsa_debug, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 176 | }; |
| 177 | #endif /* POLARSSL_RSA_C */ |
| 178 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 179 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 180 | /* |
| 181 | * Generic EC key |
| 182 | */ |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 183 | static int eckey_can_do( pk_type_t type ) |
| 184 | { |
| 185 | return( type == POLARSSL_PK_ECKEY || |
| 186 | type == POLARSSL_PK_ECKEY_DH || |
| 187 | type == POLARSSL_PK_ECDSA ); |
| 188 | } |
| 189 | |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 190 | static size_t eckey_get_size( const void *ctx ) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 191 | { |
| 192 | return( ((ecp_keypair *) ctx)->grp.pbits ); |
| 193 | } |
| 194 | |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 195 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 196 | /* Forward declarations */ |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 197 | static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg, |
| 198 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 199 | const unsigned char *sig, size_t sig_len ); |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 200 | |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 201 | static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg, |
| 202 | const unsigned char *hash, size_t hash_len, |
| 203 | unsigned char *sig, size_t *sig_len, |
| 204 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 205 | |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 206 | static int eckey_verify_wrap( void *ctx, md_type_t md_alg, |
| 207 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 208 | const unsigned char *sig, size_t sig_len ) |
| 209 | { |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 210 | int ret; |
| 211 | ecdsa_context ecdsa; |
| 212 | |
| 213 | ecdsa_init( &ecdsa ); |
| 214 | |
Manuel Pégourié-Gonnard | 583b608 | 2013-08-20 16:58:13 +0200 | [diff] [blame] | 215 | if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
| 216 | 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] | 217 | |
| 218 | ecdsa_free( &ecdsa ); |
| 219 | |
| 220 | return( ret ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 221 | } |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 222 | |
| 223 | static int eckey_sign_wrap( void *ctx, md_type_t md_alg, |
| 224 | const unsigned char *hash, size_t hash_len, |
| 225 | unsigned char *sig, size_t *sig_len, |
| 226 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 227 | { |
| 228 | int ret; |
| 229 | ecdsa_context ecdsa; |
| 230 | |
| 231 | ecdsa_init( &ecdsa ); |
| 232 | |
| 233 | if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
| 234 | ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len, |
| 235 | f_rng, p_rng ); |
| 236 | |
| 237 | ecdsa_free( &ecdsa ); |
| 238 | |
| 239 | return( ret ); |
| 240 | } |
| 241 | |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 242 | #endif /* POLARSSL_ECDSA_C */ |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 243 | |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 244 | static int eckey_check_pair( const void *pub, const void *prv ) |
| 245 | { |
| 246 | return( ecp_check_pub_priv( (const ecp_keypair *) pub, |
| 247 | (const ecp_keypair *) prv ) ); |
| 248 | } |
| 249 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 250 | static void *eckey_alloc_wrap( void ) |
| 251 | { |
| 252 | void *ctx = polarssl_malloc( sizeof( ecp_keypair ) ); |
| 253 | |
| 254 | if( ctx != NULL ) |
| 255 | ecp_keypair_init( ctx ); |
| 256 | |
| 257 | return( ctx ); |
| 258 | } |
| 259 | |
| 260 | static void eckey_free_wrap( void *ctx ) |
| 261 | { |
| 262 | ecp_keypair_free( (ecp_keypair *) ctx ); |
| 263 | polarssl_free( ctx ); |
| 264 | } |
| 265 | |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 266 | static void eckey_debug( const void *ctx, pk_debug_item *items ) |
| 267 | { |
| 268 | items->type = POLARSSL_PK_DEBUG_ECP; |
| 269 | items->name = "eckey.Q"; |
| 270 | items->value = &( ((ecp_keypair *) ctx)->Q ); |
| 271 | } |
| 272 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 273 | const pk_info_t eckey_info = { |
| 274 | POLARSSL_PK_ECKEY, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 275 | "EC", |
| 276 | eckey_get_size, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 277 | eckey_can_do, |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 278 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 279 | eckey_verify_wrap, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 280 | eckey_sign_wrap, |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 281 | #else |
| 282 | NULL, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 283 | NULL, |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 284 | #endif |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 285 | NULL, |
| 286 | NULL, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 287 | eckey_check_pair, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 288 | eckey_alloc_wrap, |
| 289 | eckey_free_wrap, |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 290 | eckey_debug, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 291 | }; |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 292 | |
| 293 | /* |
Paul Bakker | 75342a6 | 2014-04-08 17:35:40 +0200 | [diff] [blame] | 294 | * EC key restricted to ECDH |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 295 | */ |
| 296 | static int eckeydh_can_do( pk_type_t type ) |
| 297 | { |
| 298 | return( type == POLARSSL_PK_ECKEY || |
| 299 | type == POLARSSL_PK_ECKEY_DH ); |
| 300 | } |
| 301 | |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 302 | const pk_info_t eckeydh_info = { |
| 303 | POLARSSL_PK_ECKEY_DH, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 304 | "EC_DH", |
| 305 | eckey_get_size, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 306 | eckeydh_can_do, |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 307 | NULL, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 308 | NULL, |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 309 | NULL, |
| 310 | NULL, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 311 | eckey_check_pair, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 312 | eckey_alloc_wrap, /* Same underlying key structure */ |
| 313 | eckey_free_wrap, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 314 | eckey_debug, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 315 | }; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 316 | #endif /* POLARSSL_ECP_C */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 317 | |
| 318 | #if defined(POLARSSL_ECDSA_C) |
| 319 | static int ecdsa_can_do( pk_type_t type ) |
| 320 | { |
| 321 | return( type == POLARSSL_PK_ECDSA ); |
| 322 | } |
| 323 | |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 324 | static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg, |
| 325 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 326 | const unsigned char *sig, size_t sig_len ) |
| 327 | { |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 328 | int ret; |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 329 | ((void) md_alg); |
| 330 | |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 331 | ret = ecdsa_read_signature( (ecdsa_context *) ctx, |
| 332 | hash, hash_len, sig, sig_len ); |
| 333 | |
| 334 | if( ret == POLARSSL_ERR_ECP_SIG_LEN_MISMATCH ) |
| 335 | return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH ); |
| 336 | |
| 337 | return( ret ); |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 340 | static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg, |
| 341 | const unsigned char *hash, size_t hash_len, |
| 342 | unsigned char *sig, size_t *sig_len, |
| 343 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 344 | { |
Manuel Pégourié-Gonnard | 65ad3e4 | 2014-01-06 16:57:24 +0100 | [diff] [blame] | 345 | /* Use deterministic ECDSA by default if available */ |
| 346 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
| 347 | ((void) f_rng); |
| 348 | ((void) p_rng); |
| 349 | |
| 350 | return( ecdsa_write_signature_det( (ecdsa_context *) ctx, |
| 351 | hash, hash_len, sig, sig_len, md_alg ) ); |
| 352 | #else |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 353 | ((void) md_alg); |
| 354 | |
| 355 | return( ecdsa_write_signature( (ecdsa_context *) ctx, |
| 356 | hash, hash_len, sig, sig_len, f_rng, p_rng ) ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 357 | #endif /* POLARSSL_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 360 | static void *ecdsa_alloc_wrap( void ) |
| 361 | { |
| 362 | void *ctx = polarssl_malloc( sizeof( ecdsa_context ) ); |
| 363 | |
| 364 | if( ctx != NULL ) |
| 365 | ecdsa_init( (ecdsa_context *) ctx ); |
| 366 | |
| 367 | return( ctx ); |
| 368 | } |
| 369 | |
| 370 | static void ecdsa_free_wrap( void *ctx ) |
| 371 | { |
| 372 | ecdsa_free( (ecdsa_context *) ctx ); |
| 373 | polarssl_free( ctx ); |
| 374 | } |
| 375 | |
| 376 | const pk_info_t ecdsa_info = { |
| 377 | POLARSSL_PK_ECDSA, |
| 378 | "ECDSA", |
| 379 | eckey_get_size, /* Compatible key structures */ |
| 380 | ecdsa_can_do, |
| 381 | ecdsa_verify_wrap, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 382 | ecdsa_sign_wrap, |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 383 | NULL, |
| 384 | NULL, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 385 | eckey_check_pair, /* Compatible key structures */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 386 | ecdsa_alloc_wrap, |
| 387 | ecdsa_free_wrap, |
| 388 | eckey_debug, /* Compatible key structures */ |
| 389 | }; |
| 390 | #endif /* POLARSSL_ECDSA_C */ |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 391 | |
| 392 | /* |
| 393 | * Support for alternative RSA-private implementations |
| 394 | */ |
| 395 | |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 396 | static int rsa_alt_can_do( pk_type_t type ) |
| 397 | { |
| 398 | return( type == POLARSSL_PK_RSA ); |
| 399 | } |
| 400 | |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 401 | static size_t rsa_alt_get_size( const void *ctx ) |
| 402 | { |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 403 | 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] | 404 | |
Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 405 | return( 8 * rsa_alt->key_len_func( rsa_alt->key ) ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static int rsa_alt_sign_wrap( void *ctx, md_type_t md_alg, |
| 409 | const unsigned char *hash, size_t hash_len, |
| 410 | unsigned char *sig, size_t *sig_len, |
| 411 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 412 | { |
| 413 | rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx; |
| 414 | |
| 415 | *sig_len = rsa_alt->key_len_func( rsa_alt->key ); |
| 416 | |
| 417 | 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] | 418 | md_alg, (unsigned int) hash_len, hash, sig ) ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static int rsa_alt_decrypt_wrap( void *ctx, |
| 422 | const unsigned char *input, size_t ilen, |
| 423 | unsigned char *output, size_t *olen, size_t osize, |
| 424 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 425 | { |
| 426 | rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx; |
| 427 | |
| 428 | ((void) f_rng); |
| 429 | ((void) p_rng); |
| 430 | |
| 431 | if( ilen != rsa_alt->key_len_func( rsa_alt->key ) ) |
| 432 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
| 433 | |
| 434 | return( rsa_alt->decrypt_func( rsa_alt->key, |
| 435 | RSA_PRIVATE, olen, input, output, osize ) ); |
| 436 | } |
| 437 | |
| 438 | static void *rsa_alt_alloc_wrap( void ) |
| 439 | { |
| 440 | void *ctx = polarssl_malloc( sizeof( rsa_alt_context ) ); |
| 441 | |
| 442 | if( ctx != NULL ) |
| 443 | memset( ctx, 0, sizeof( rsa_alt_context ) ); |
| 444 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 445 | return( ctx ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static void rsa_alt_free_wrap( void *ctx ) |
| 449 | { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 450 | polarssl_zeroize( ctx, sizeof( rsa_alt_context ) ); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 451 | polarssl_free( ctx ); |
| 452 | } |
| 453 | |
| 454 | const pk_info_t rsa_alt_info = { |
| 455 | POLARSSL_PK_RSA_ALT, |
| 456 | "RSA-alt", |
| 457 | rsa_alt_get_size, |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 458 | rsa_alt_can_do, |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 459 | NULL, |
| 460 | rsa_alt_sign_wrap, |
| 461 | rsa_alt_decrypt_wrap, |
| 462 | NULL, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 463 | NULL, /* No public key */ |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 464 | rsa_alt_alloc_wrap, |
| 465 | rsa_alt_free_wrap, |
| 466 | NULL, |
| 467 | }; |
Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 468 | |
| 469 | #endif /* POLARSSL_PK_C */ |