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