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