Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key abstraction layer |
| 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 | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +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 | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/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 | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 29 | #if defined(POLARSSL_PK_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/pk.h" |
| 31 | #include "mbedtls/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) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/rsa.h" |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 35 | #endif |
| 36 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/ecp.h" |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 38 | #endif |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 39 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/ecdsa.h" |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 41 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 42 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 43 | /* Implementation that should never be optimized out by the compiler */ |
| 44 | static void polarssl_zeroize( void *v, size_t n ) { |
| 45 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 46 | } |
| 47 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 48 | /* |
| 49 | * Initialise a pk_context |
| 50 | */ |
| 51 | void pk_init( pk_context *ctx ) |
| 52 | { |
| 53 | if( ctx == NULL ) |
| 54 | return; |
| 55 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 56 | ctx->pk_info = NULL; |
| 57 | ctx->pk_ctx = NULL; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Free (the components of) a pk_context |
| 62 | */ |
| 63 | void pk_free( pk_context *ctx ) |
| 64 | { |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 65 | if( ctx == NULL || ctx->pk_info == NULL ) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 66 | return; |
| 67 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 68 | ctx->pk_info->ctx_free_func( ctx->pk_ctx ); |
Manuel Pégourié-Gonnard | 1f73a65 | 2013-07-09 10:26:41 +0200 | [diff] [blame] | 69 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 70 | polarssl_zeroize( ctx, sizeof( pk_context ) ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Get pk_info structure from type |
| 75 | */ |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 76 | 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] | 77 | { |
| 78 | switch( pk_type ) { |
| 79 | #if defined(POLARSSL_RSA_C) |
| 80 | case POLARSSL_PK_RSA: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 81 | return( &rsa_info ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 82 | #endif |
| 83 | #if defined(POLARSSL_ECP_C) |
| 84 | case POLARSSL_PK_ECKEY: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 85 | return( &eckey_info ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 86 | case POLARSSL_PK_ECKEY_DH: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 87 | return( &eckeydh_info ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 88 | #endif |
| 89 | #if defined(POLARSSL_ECDSA_C) |
| 90 | case POLARSSL_PK_ECDSA: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 91 | return( &ecdsa_info ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 92 | #endif |
Paul Bakker | 75342a6 | 2014-04-08 17:35:40 +0200 | [diff] [blame] | 93 | /* POLARSSL_PK_RSA_ALT omitted on purpose */ |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 94 | default: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 95 | return( NULL ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 96 | } |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 100 | * Initialise context |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 101 | */ |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 102 | 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] | 103 | { |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 104 | if( ctx == NULL || info == NULL || ctx->pk_info != NULL ) |
Manuel Pégourié-Gonnard | 1569938 | 2013-08-14 19:22:48 +0200 | [diff] [blame] | 105 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 106 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 107 | if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 108 | return( POLARSSL_ERR_PK_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 109 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 110 | ctx->pk_info = info; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 111 | |
| 112 | return( 0 ); |
| 113 | } |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 114 | |
Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 115 | #if defined(POLARSSL_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | b4fae57 | 2014-01-20 11:22:25 +0100 | [diff] [blame] | 116 | /* |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 117 | * Initialize an RSA-alt context |
| 118 | */ |
| 119 | int pk_init_ctx_rsa_alt( pk_context *ctx, void * key, |
| 120 | pk_rsa_alt_decrypt_func decrypt_func, |
| 121 | pk_rsa_alt_sign_func sign_func, |
| 122 | pk_rsa_alt_key_len_func key_len_func ) |
| 123 | { |
| 124 | rsa_alt_context *rsa_alt; |
| 125 | const pk_info_t *info = &rsa_alt_info; |
| 126 | |
| 127 | if( ctx == NULL || ctx->pk_info != NULL ) |
| 128 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 129 | |
| 130 | if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) |
| 131 | return( POLARSSL_ERR_PK_MALLOC_FAILED ); |
| 132 | |
| 133 | ctx->pk_info = info; |
| 134 | |
| 135 | rsa_alt = (rsa_alt_context *) ctx->pk_ctx; |
| 136 | |
| 137 | rsa_alt->key = key; |
| 138 | rsa_alt->decrypt_func = decrypt_func; |
| 139 | rsa_alt->sign_func = sign_func; |
| 140 | rsa_alt->key_len_func = key_len_func; |
| 141 | |
| 142 | return( 0 ); |
| 143 | } |
Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 144 | #endif /* POLARSSL_PK_RSA_ALT_SUPPORT */ |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 145 | |
| 146 | /* |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 147 | * Tell if a PK can do the operations of the given type |
| 148 | */ |
Manuel Pégourié-Gonnard | 0db107e | 2015-03-19 14:01:57 +0000 | [diff] [blame] | 149 | int pk_can_do( const pk_context *ctx, pk_type_t type ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 150 | { |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 151 | /* null or NONE context can't do anything */ |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 152 | if( ctx == NULL || ctx->pk_info == NULL ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 153 | return( 0 ); |
| 154 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 155 | return( ctx->pk_info->can_do( type ) ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 159 | * Helper for pk_sign and pk_verify |
| 160 | */ |
| 161 | static inline int pk_hashlen_helper( md_type_t md_alg, size_t *hash_len ) |
| 162 | { |
| 163 | const md_info_t *md_info; |
| 164 | |
| 165 | if( *hash_len != 0 ) |
| 166 | return( 0 ); |
| 167 | |
| 168 | if( ( md_info = md_info_from_type( md_alg ) ) == NULL ) |
| 169 | return( -1 ); |
| 170 | |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 171 | *hash_len = md_get_size( md_info ); |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 172 | return( 0 ); |
| 173 | } |
| 174 | |
| 175 | /* |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 176 | * Verify a signature |
| 177 | */ |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 178 | int pk_verify( pk_context *ctx, md_type_t md_alg, |
| 179 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 180 | const unsigned char *sig, size_t sig_len ) |
| 181 | { |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 182 | if( ctx == NULL || ctx->pk_info == NULL || |
| 183 | pk_hashlen_helper( md_alg, &hash_len ) != 0 ) |
Manuel Pégourié-Gonnard | 1569938 | 2013-08-14 19:22:48 +0200 | [diff] [blame] | 184 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 185 | |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 186 | if( ctx->pk_info->verify_func == NULL ) |
| 187 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 188 | |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 189 | 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] | 190 | sig, sig_len ) ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 194 | * Verify a signature with options |
| 195 | */ |
| 196 | int pk_verify_ext( pk_type_t type, const void *options, |
| 197 | pk_context *ctx, md_type_t md_alg, |
| 198 | const unsigned char *hash, size_t hash_len, |
| 199 | const unsigned char *sig, size_t sig_len ) |
| 200 | { |
| 201 | if( ctx == NULL || ctx->pk_info == NULL ) |
| 202 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 203 | |
| 204 | if( ! pk_can_do( ctx, type ) ) |
| 205 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 206 | |
| 207 | if( type == POLARSSL_PK_RSASSA_PSS ) |
| 208 | { |
| 209 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_PKCS1_V21) |
| 210 | int ret; |
| 211 | const pk_rsassa_pss_options *pss_opts; |
| 212 | |
| 213 | if( options == NULL ) |
| 214 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 215 | |
| 216 | pss_opts = (const pk_rsassa_pss_options *) options; |
| 217 | |
| 218 | if( sig_len < pk_get_len( ctx ) ) |
| 219 | return( POLARSSL_ERR_RSA_VERIFY_FAILED ); |
| 220 | |
| 221 | ret = rsa_rsassa_pss_verify_ext( pk_rsa( *ctx ), |
| 222 | NULL, NULL, RSA_PUBLIC, |
Sander Niemeijer | ef5087d | 2014-08-16 12:45:52 +0200 | [diff] [blame] | 223 | md_alg, (unsigned int) hash_len, hash, |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 224 | pss_opts->mgf1_hash_id, |
| 225 | pss_opts->expected_salt_len, |
| 226 | sig ); |
| 227 | if( ret != 0 ) |
| 228 | return( ret ); |
| 229 | |
| 230 | if( sig_len > pk_get_len( ctx ) ) |
| 231 | return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH ); |
| 232 | |
| 233 | return( 0 ); |
| 234 | #else |
| 235 | return( POLARSSL_ERR_PK_FEATURE_UNAVAILABLE ); |
| 236 | #endif |
| 237 | } |
| 238 | |
| 239 | /* General case: no options */ |
| 240 | if( options != NULL ) |
| 241 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 242 | |
| 243 | return( pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) ); |
| 244 | } |
| 245 | |
| 246 | /* |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 247 | * Make a signature |
| 248 | */ |
| 249 | int pk_sign( pk_context *ctx, md_type_t md_alg, |
| 250 | const unsigned char *hash, size_t hash_len, |
| 251 | unsigned char *sig, size_t *sig_len, |
| 252 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 253 | { |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 254 | if( ctx == NULL || ctx->pk_info == NULL || |
| 255 | pk_hashlen_helper( md_alg, &hash_len ) != 0 ) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 256 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 257 | |
| 258 | if( ctx->pk_info->sign_func == NULL ) |
| 259 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 260 | |
| 261 | return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len, |
| 262 | sig, sig_len, f_rng, p_rng ) ); |
| 263 | } |
| 264 | |
| 265 | /* |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 266 | * Decrypt message |
| 267 | */ |
| 268 | int pk_decrypt( pk_context *ctx, |
| 269 | const unsigned char *input, size_t ilen, |
| 270 | unsigned char *output, size_t *olen, size_t osize, |
| 271 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 272 | { |
| 273 | if( ctx == NULL || ctx->pk_info == NULL ) |
| 274 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 275 | |
| 276 | if( ctx->pk_info->decrypt_func == NULL ) |
| 277 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 278 | |
| 279 | return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen, |
| 280 | output, olen, osize, f_rng, p_rng ) ); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Encrypt message |
| 285 | */ |
| 286 | int pk_encrypt( pk_context *ctx, |
| 287 | const unsigned char *input, size_t ilen, |
| 288 | unsigned char *output, size_t *olen, size_t osize, |
| 289 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 290 | { |
| 291 | if( ctx == NULL || ctx->pk_info == NULL ) |
| 292 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 293 | |
| 294 | if( ctx->pk_info->encrypt_func == NULL ) |
| 295 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 296 | |
| 297 | return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen, |
| 298 | output, olen, osize, f_rng, p_rng ) ); |
| 299 | } |
| 300 | |
| 301 | /* |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 302 | * Check public-private key pair |
| 303 | */ |
| 304 | int pk_check_pair( const pk_context *pub, const pk_context *prv ) |
| 305 | { |
| 306 | if( pub == NULL || pub->pk_info == NULL || |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 307 | prv == NULL || prv->pk_info == NULL || |
| 308 | prv->pk_info->check_pair_func == NULL ) |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 309 | { |
| 310 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
| 311 | } |
| 312 | |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 313 | if( prv->pk_info->type == POLARSSL_PK_RSA_ALT ) |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 314 | { |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 315 | if( pub->pk_info->type != POLARSSL_PK_RSA ) |
| 316 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | if( pub->pk_info != prv->pk_info ) |
| 321 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 324 | return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) ); |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 328 | * Get key size in bits |
| 329 | */ |
| 330 | size_t pk_get_size( const pk_context *ctx ) |
| 331 | { |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 332 | if( ctx == NULL || ctx->pk_info == NULL ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 333 | return( 0 ); |
| 334 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 335 | return( ctx->pk_info->get_size( ctx->pk_ctx ) ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 336 | } |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 337 | |
| 338 | /* |
| 339 | * Export debug information |
| 340 | */ |
| 341 | int pk_debug( const pk_context *ctx, pk_debug_item *items ) |
| 342 | { |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 343 | if( ctx == NULL || ctx->pk_info == NULL ) |
Manuel Pégourié-Gonnard | 1569938 | 2013-08-14 19:22:48 +0200 | [diff] [blame] | 344 | return( POLARSSL_ERR_PK_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 345 | |
Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 346 | if( ctx->pk_info->debug_func == NULL ) |
| 347 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 348 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 349 | ctx->pk_info->debug_func( ctx->pk_ctx, items ); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 350 | return( 0 ); |
| 351 | } |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 352 | |
| 353 | /* |
| 354 | * Access the PK type name |
| 355 | */ |
Manuel Pégourié-Gonnard | 0db107e | 2015-03-19 14:01:57 +0000 | [diff] [blame] | 356 | const char *pk_get_name( const pk_context *ctx ) |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 357 | { |
| 358 | if( ctx == NULL || ctx->pk_info == NULL ) |
| 359 | return( "invalid PK" ); |
| 360 | |
| 361 | return( ctx->pk_info->name ); |
| 362 | } |
Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 363 | |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 364 | /* |
| 365 | * Access the PK type |
| 366 | */ |
| 367 | pk_type_t pk_get_type( const pk_context *ctx ) |
| 368 | { |
| 369 | if( ctx == NULL || ctx->pk_info == NULL ) |
| 370 | return( POLARSSL_PK_NONE ); |
| 371 | |
| 372 | return( ctx->pk_info->type ); |
| 373 | } |
| 374 | |
Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 375 | #endif /* POLARSSL_PK_C */ |