Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key abstraction layer |
| 3 | * |
| 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 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 | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 30 | #include "polarssl/pk.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 31 | #include "polarssl/pk_wrap.h" |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 33 | #if defined(POLARSSL_RSA_C) |
| 34 | #include "polarssl/rsa.h" |
| 35 | #endif |
| 36 | #if defined(POLARSSL_ECP_C) |
| 37 | #include "polarssl/ecp.h" |
| 38 | #endif |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 39 | #if defined(POLARSSL_ECDSA_C) |
| 40 | #include "polarssl/ecdsa.h" |
| 41 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 42 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 43 | /* |
| 44 | * Initialise a pk_context |
| 45 | */ |
| 46 | void pk_init( pk_context *ctx ) |
| 47 | { |
| 48 | if( ctx == NULL ) |
| 49 | return; |
| 50 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 51 | ctx->pk_info = NULL; |
| 52 | ctx->pk_ctx = NULL; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Free (the components of) a pk_context |
| 57 | */ |
| 58 | void pk_free( pk_context *ctx ) |
| 59 | { |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 60 | if( ctx == NULL || ctx->pk_info == NULL) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 61 | return; |
| 62 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 63 | ctx->pk_info->ctx_free_func( ctx->pk_ctx ); |
| 64 | ctx->pk_ctx = NULL; |
Manuel Pégourié-Gonnard | 1f73a65 | 2013-07-09 10:26:41 +0200 | [diff] [blame] | 65 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 66 | ctx->pk_info = NULL; |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Get pk_info structure from type |
| 71 | */ |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 72 | const pk_info_t * pk_info_from_type( pk_type_t pk_type ) |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 73 | { |
| 74 | switch( pk_type ) { |
| 75 | #if defined(POLARSSL_RSA_C) |
| 76 | case POLARSSL_PK_RSA: |
| 77 | return &rsa_info; |
| 78 | #endif |
| 79 | #if defined(POLARSSL_ECP_C) |
| 80 | case POLARSSL_PK_ECKEY: |
| 81 | return &eckey_info; |
| 82 | case POLARSSL_PK_ECKEY_DH: |
| 83 | return &eckeydh_info; |
| 84 | #endif |
| 85 | #if defined(POLARSSL_ECDSA_C) |
| 86 | case POLARSSL_PK_ECDSA: |
| 87 | return &ecdsa_info; |
| 88 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 89 | /* POLARSSL_PK_RSA_ALT ommited on purpose */ |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 90 | default: |
| 91 | return NULL; |
| 92 | } |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 96 | * Initialise context |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 97 | */ |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 98 | int pk_init_ctx( pk_context *ctx, const pk_info_t *info ) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 99 | { |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 100 | if( ctx == NULL || info == NULL || ctx->pk_info != NULL ) |
Manuel Pégourié-Gonnard | 1569938 | 2013-08-14 19:22:48 +0200 | [diff] [blame] | 101 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 102 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 103 | if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 104 | return( POLARSSL_ERR_PK_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 105 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 106 | ctx->pk_info = info; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 107 | |
| 108 | return( 0 ); |
| 109 | } |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | b4fae57 | 2014-01-20 11:22:25 +0100 | [diff] [blame] | 111 | /* |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 112 | * Initialize an RSA-alt context |
| 113 | */ |
| 114 | int pk_init_ctx_rsa_alt( pk_context *ctx, void * key, |
| 115 | pk_rsa_alt_decrypt_func decrypt_func, |
| 116 | pk_rsa_alt_sign_func sign_func, |
| 117 | pk_rsa_alt_key_len_func key_len_func ) |
| 118 | { |
| 119 | rsa_alt_context *rsa_alt; |
| 120 | const pk_info_t *info = &rsa_alt_info; |
| 121 | |
| 122 | if( ctx == NULL || ctx->pk_info != NULL ) |
| 123 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 124 | |
| 125 | if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) |
| 126 | return( POLARSSL_ERR_PK_MALLOC_FAILED ); |
| 127 | |
| 128 | ctx->pk_info = info; |
| 129 | |
| 130 | rsa_alt = (rsa_alt_context *) ctx->pk_ctx; |
| 131 | |
| 132 | rsa_alt->key = key; |
| 133 | rsa_alt->decrypt_func = decrypt_func; |
| 134 | rsa_alt->sign_func = sign_func; |
| 135 | rsa_alt->key_len_func = key_len_func; |
| 136 | |
| 137 | return( 0 ); |
| 138 | } |
| 139 | |
| 140 | /* |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 141 | * Tell if a PK can do the operations of the given type |
| 142 | */ |
| 143 | int pk_can_do( pk_context *ctx, pk_type_t type ) |
| 144 | { |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 145 | /* null or NONE context can't do anything */ |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 146 | if( ctx == NULL || ctx->pk_info == NULL ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 147 | return( 0 ); |
| 148 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 149 | return( ctx->pk_info->can_do( type ) ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 153 | * Helper for pk_sign and pk_verify |
| 154 | */ |
| 155 | static inline int pk_hashlen_helper( md_type_t md_alg, size_t *hash_len ) |
| 156 | { |
| 157 | const md_info_t *md_info; |
| 158 | |
| 159 | if( *hash_len != 0 ) |
| 160 | return( 0 ); |
| 161 | |
| 162 | if( ( md_info = md_info_from_type( md_alg ) ) == NULL ) |
| 163 | return( -1 ); |
| 164 | |
| 165 | *hash_len = md_info->size; |
| 166 | return( 0 ); |
| 167 | } |
| 168 | |
| 169 | /* |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 170 | * Verify a signature |
| 171 | */ |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 172 | int pk_verify( pk_context *ctx, md_type_t md_alg, |
| 173 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 174 | const unsigned char *sig, size_t sig_len ) |
| 175 | { |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 176 | if( ctx == NULL || ctx->pk_info == NULL || |
| 177 | pk_hashlen_helper( md_alg, &hash_len ) != 0 ) |
Manuel Pégourié-Gonnard | 1569938 | 2013-08-14 19:22:48 +0200 | [diff] [blame] | 178 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 179 | |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 180 | if( ctx->pk_info->verify_func == NULL ) |
| 181 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 182 | |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 183 | return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len, |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 184 | sig, sig_len ) ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 188 | * Make a signature |
| 189 | */ |
| 190 | int pk_sign( pk_context *ctx, md_type_t md_alg, |
| 191 | const unsigned char *hash, size_t hash_len, |
| 192 | unsigned char *sig, size_t *sig_len, |
| 193 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 194 | { |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 195 | if( ctx == NULL || ctx->pk_info == NULL || |
| 196 | pk_hashlen_helper( md_alg, &hash_len ) != 0 ) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 197 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 198 | |
| 199 | if( ctx->pk_info->sign_func == NULL ) |
| 200 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 201 | |
| 202 | return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len, |
| 203 | sig, sig_len, f_rng, p_rng ) ); |
| 204 | } |
| 205 | |
| 206 | /* |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 207 | * Decrypt message |
| 208 | */ |
| 209 | int pk_decrypt( pk_context *ctx, |
| 210 | const unsigned char *input, size_t ilen, |
| 211 | unsigned char *output, size_t *olen, size_t osize, |
| 212 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 213 | { |
| 214 | if( ctx == NULL || ctx->pk_info == NULL ) |
| 215 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 216 | |
| 217 | if( ctx->pk_info->decrypt_func == NULL ) |
| 218 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 219 | |
| 220 | return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen, |
| 221 | output, olen, osize, f_rng, p_rng ) ); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Encrypt message |
| 226 | */ |
| 227 | int pk_encrypt( pk_context *ctx, |
| 228 | const unsigned char *input, size_t ilen, |
| 229 | unsigned char *output, size_t *olen, size_t osize, |
| 230 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 231 | { |
| 232 | if( ctx == NULL || ctx->pk_info == NULL ) |
| 233 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 234 | |
| 235 | if( ctx->pk_info->encrypt_func == NULL ) |
| 236 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 237 | |
| 238 | return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen, |
| 239 | output, olen, osize, f_rng, p_rng ) ); |
| 240 | } |
| 241 | |
| 242 | /* |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 243 | * Get key size in bits |
| 244 | */ |
| 245 | size_t pk_get_size( const pk_context *ctx ) |
| 246 | { |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 247 | if( ctx == NULL || ctx->pk_info == NULL ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 248 | return( 0 ); |
| 249 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 250 | return( ctx->pk_info->get_size( ctx->pk_ctx ) ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 251 | } |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * Export debug information |
| 255 | */ |
| 256 | int pk_debug( const pk_context *ctx, pk_debug_item *items ) |
| 257 | { |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 258 | if( ctx == NULL || ctx->pk_info == NULL ) |
Manuel Pégourié-Gonnard | 1569938 | 2013-08-14 19:22:48 +0200 | [diff] [blame] | 259 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 260 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 261 | ctx->pk_info->debug_func( ctx->pk_ctx, items ); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 262 | return( 0 ); |
| 263 | } |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * Access the PK type name |
| 267 | */ |
| 268 | const char * pk_get_name( const pk_context *ctx ) |
| 269 | { |
| 270 | if( ctx == NULL || ctx->pk_info == NULL ) |
| 271 | return( "invalid PK" ); |
| 272 | |
| 273 | return( ctx->pk_info->name ); |
| 274 | } |
Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 275 | |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 276 | /* |
| 277 | * Access the PK type |
| 278 | */ |
| 279 | pk_type_t pk_get_type( const pk_context *ctx ) |
| 280 | { |
| 281 | if( ctx == NULL || ctx->pk_info == NULL ) |
| 282 | return( POLARSSL_PK_NONE ); |
| 283 | |
| 284 | return( ctx->pk_info->type ); |
| 285 | } |
| 286 | |
Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 287 | #endif /* POLARSSL_PK_C */ |