mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 1:24750b9ad5ef 1 /*
Christopher Haster 1:24750b9ad5ef 2 * Public Key abstraction layer
Christopher Haster 1:24750b9ad5ef 3 *
Christopher Haster 1:24750b9ad5ef 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Christopher Haster 1:24750b9ad5ef 5 * SPDX-License-Identifier: Apache-2.0
Christopher Haster 1:24750b9ad5ef 6 *
Christopher Haster 1:24750b9ad5ef 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Christopher Haster 1:24750b9ad5ef 8 * not use this file except in compliance with the License.
Christopher Haster 1:24750b9ad5ef 9 * You may obtain a copy of the License at
Christopher Haster 1:24750b9ad5ef 10 *
Christopher Haster 1:24750b9ad5ef 11 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 1:24750b9ad5ef 12 *
Christopher Haster 1:24750b9ad5ef 13 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 1:24750b9ad5ef 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Christopher Haster 1:24750b9ad5ef 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 1:24750b9ad5ef 16 * See the License for the specific language governing permissions and
Christopher Haster 1:24750b9ad5ef 17 * limitations under the License.
Christopher Haster 1:24750b9ad5ef 18 *
Christopher Haster 1:24750b9ad5ef 19 * This file is part of mbed TLS (https://tls.mbed.org)
Christopher Haster 1:24750b9ad5ef 20 */
Christopher Haster 1:24750b9ad5ef 21
Christopher Haster 1:24750b9ad5ef 22 #if !defined(MBEDTLS_CONFIG_FILE)
Christopher Haster 1:24750b9ad5ef 23 #include "mbedtls/config.h"
Christopher Haster 1:24750b9ad5ef 24 #else
Christopher Haster 1:24750b9ad5ef 25 #include MBEDTLS_CONFIG_FILE
Christopher Haster 1:24750b9ad5ef 26 #endif
Christopher Haster 1:24750b9ad5ef 27
Christopher Haster 1:24750b9ad5ef 28 #if defined(MBEDTLS_PK_C)
Christopher Haster 1:24750b9ad5ef 29 #include "mbedtls/pk.h"
Christopher Haster 1:24750b9ad5ef 30 #include "mbedtls/pk_internal.h"
Christopher Haster 1:24750b9ad5ef 31
Christopher Haster 1:24750b9ad5ef 32 #if defined(MBEDTLS_RSA_C)
Christopher Haster 1:24750b9ad5ef 33 #include "mbedtls/rsa.h"
Christopher Haster 1:24750b9ad5ef 34 #endif
Christopher Haster 1:24750b9ad5ef 35 #if defined(MBEDTLS_ECP_C)
Christopher Haster 1:24750b9ad5ef 36 #include "mbedtls/ecp.h"
Christopher Haster 1:24750b9ad5ef 37 #endif
Christopher Haster 1:24750b9ad5ef 38 #if defined(MBEDTLS_ECDSA_C)
Christopher Haster 1:24750b9ad5ef 39 #include "mbedtls/ecdsa.h"
Christopher Haster 1:24750b9ad5ef 40 #endif
Christopher Haster 1:24750b9ad5ef 41
Christopher Haster 1:24750b9ad5ef 42 /* Implementation that should never be optimized out by the compiler */
Christopher Haster 1:24750b9ad5ef 43 static void mbedtls_zeroize( void *v, size_t n ) {
Christopher Haster 1:24750b9ad5ef 44 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
Christopher Haster 1:24750b9ad5ef 45 }
Christopher Haster 1:24750b9ad5ef 46
Christopher Haster 1:24750b9ad5ef 47 /*
Christopher Haster 1:24750b9ad5ef 48 * Initialise a mbedtls_pk_context
Christopher Haster 1:24750b9ad5ef 49 */
Christopher Haster 1:24750b9ad5ef 50 void mbedtls_pk_init( mbedtls_pk_context *ctx )
Christopher Haster 1:24750b9ad5ef 51 {
Christopher Haster 1:24750b9ad5ef 52 if( ctx == NULL )
Christopher Haster 1:24750b9ad5ef 53 return;
Christopher Haster 1:24750b9ad5ef 54
Christopher Haster 1:24750b9ad5ef 55 ctx->pk_info = NULL;
Christopher Haster 1:24750b9ad5ef 56 ctx->pk_ctx = NULL;
Christopher Haster 1:24750b9ad5ef 57 }
Christopher Haster 1:24750b9ad5ef 58
Christopher Haster 1:24750b9ad5ef 59 /*
Christopher Haster 1:24750b9ad5ef 60 * Free (the components of) a mbedtls_pk_context
Christopher Haster 1:24750b9ad5ef 61 */
Christopher Haster 1:24750b9ad5ef 62 void mbedtls_pk_free( mbedtls_pk_context *ctx )
Christopher Haster 1:24750b9ad5ef 63 {
Christopher Haster 1:24750b9ad5ef 64 if( ctx == NULL || ctx->pk_info == NULL )
Christopher Haster 1:24750b9ad5ef 65 return;
Christopher Haster 1:24750b9ad5ef 66
Christopher Haster 1:24750b9ad5ef 67 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Christopher Haster 1:24750b9ad5ef 68
Christopher Haster 1:24750b9ad5ef 69 mbedtls_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Christopher Haster 1:24750b9ad5ef 70 }
Christopher Haster 1:24750b9ad5ef 71
Christopher Haster 1:24750b9ad5ef 72 /*
Christopher Haster 1:24750b9ad5ef 73 * Get pk_info structure from type
Christopher Haster 1:24750b9ad5ef 74 */
Christopher Haster 1:24750b9ad5ef 75 const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Christopher Haster 1:24750b9ad5ef 76 {
Christopher Haster 1:24750b9ad5ef 77 switch( pk_type ) {
Christopher Haster 1:24750b9ad5ef 78 #if defined(MBEDTLS_RSA_C)
Christopher Haster 1:24750b9ad5ef 79 case MBEDTLS_PK_RSA:
Christopher Haster 1:24750b9ad5ef 80 return( &mbedtls_rsa_info );
Christopher Haster 1:24750b9ad5ef 81 #endif
Christopher Haster 1:24750b9ad5ef 82 #if defined(MBEDTLS_ECP_C)
Christopher Haster 1:24750b9ad5ef 83 case MBEDTLS_PK_ECKEY:
Christopher Haster 1:24750b9ad5ef 84 return( &mbedtls_eckey_info );
Christopher Haster 1:24750b9ad5ef 85 case MBEDTLS_PK_ECKEY_DH:
Christopher Haster 1:24750b9ad5ef 86 return( &mbedtls_eckeydh_info );
Christopher Haster 1:24750b9ad5ef 87 #endif
Christopher Haster 1:24750b9ad5ef 88 #if defined(MBEDTLS_ECDSA_C)
Christopher Haster 1:24750b9ad5ef 89 case MBEDTLS_PK_ECDSA:
Christopher Haster 1:24750b9ad5ef 90 return( &mbedtls_ecdsa_info );
Christopher Haster 1:24750b9ad5ef 91 #endif
Christopher Haster 1:24750b9ad5ef 92 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Christopher Haster 1:24750b9ad5ef 93 default:
Christopher Haster 1:24750b9ad5ef 94 return( NULL );
Christopher Haster 1:24750b9ad5ef 95 }
Christopher Haster 1:24750b9ad5ef 96 }
Christopher Haster 1:24750b9ad5ef 97
Christopher Haster 1:24750b9ad5ef 98 /*
Christopher Haster 1:24750b9ad5ef 99 * Initialise context
Christopher Haster 1:24750b9ad5ef 100 */
Christopher Haster 1:24750b9ad5ef 101 int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Christopher Haster 1:24750b9ad5ef 102 {
Christopher Haster 1:24750b9ad5ef 103 if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
Christopher Haster 1:24750b9ad5ef 104 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Christopher Haster 1:24750b9ad5ef 105
Christopher Haster 1:24750b9ad5ef 106 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Christopher Haster 1:24750b9ad5ef 107 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Christopher Haster 1:24750b9ad5ef 108
Christopher Haster 1:24750b9ad5ef 109 ctx->pk_info = info;
Christopher Haster 1:24750b9ad5ef 110
Christopher Haster 1:24750b9ad5ef 111 return( 0 );
Christopher Haster 1:24750b9ad5ef 112 }
Christopher Haster 1:24750b9ad5ef 113
Christopher Haster 1:24750b9ad5ef 114 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Christopher Haster 1:24750b9ad5ef 115 /*
Christopher Haster 1:24750b9ad5ef 116 * Initialize an RSA-alt context
Christopher Haster 1:24750b9ad5ef 117 */
Christopher Haster 1:24750b9ad5ef 118 int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Christopher Haster 1:24750b9ad5ef 119 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
Christopher Haster 1:24750b9ad5ef 120 mbedtls_pk_rsa_alt_sign_func sign_func,
Christopher Haster 1:24750b9ad5ef 121 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Christopher Haster 1:24750b9ad5ef 122 {
Christopher Haster 1:24750b9ad5ef 123 mbedtls_rsa_alt_context *rsa_alt;
Christopher Haster 1:24750b9ad5ef 124 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Christopher Haster 1:24750b9ad5ef 125
Christopher Haster 1:24750b9ad5ef 126 if( ctx == NULL || ctx->pk_info != NULL )
Christopher Haster 1:24750b9ad5ef 127 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Christopher Haster 1:24750b9ad5ef 128
Christopher Haster 1:24750b9ad5ef 129 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Christopher Haster 1:24750b9ad5ef 130 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Christopher Haster 1:24750b9ad5ef 131
Christopher Haster 1:24750b9ad5ef 132 ctx->pk_info = info;
Christopher Haster 1:24750b9ad5ef 133
Christopher Haster 1:24750b9ad5ef 134 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Christopher Haster 1:24750b9ad5ef 135
Christopher Haster 1:24750b9ad5ef 136 rsa_alt->key = key;
Christopher Haster 1:24750b9ad5ef 137 rsa_alt->decrypt_func = decrypt_func;
Christopher Haster 1:24750b9ad5ef 138 rsa_alt->sign_func = sign_func;
Christopher Haster 1:24750b9ad5ef 139 rsa_alt->key_len_func = key_len_func;
Christopher Haster 1:24750b9ad5ef 140
Christopher Haster 1:24750b9ad5ef 141 return( 0 );
Christopher Haster 1:24750b9ad5ef 142 }
Christopher Haster 1:24750b9ad5ef 143 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Christopher Haster 1:24750b9ad5ef 144
Christopher Haster 1:24750b9ad5ef 145 /*
Christopher Haster 1:24750b9ad5ef 146 * Tell if a PK can do the operations of the given type
Christopher Haster 1:24750b9ad5ef 147 */
Christopher Haster 1:24750b9ad5ef 148 int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Christopher Haster 1:24750b9ad5ef 149 {
Christopher Haster 1:24750b9ad5ef 150 /* null or NONE context can't do anything */
Christopher Haster 1:24750b9ad5ef 151 if( ctx == NULL || ctx->pk_info == NULL )
Christopher Haster 1:24750b9ad5ef 152 return( 0 );
Christopher Haster 1:24750b9ad5ef 153
Christopher Haster 1:24750b9ad5ef 154 return( ctx->pk_info->can_do( type ) );
Christopher Haster 1:24750b9ad5ef 155 }
Christopher Haster 1:24750b9ad5ef 156
Christopher Haster 1:24750b9ad5ef 157 /*
Christopher Haster 1:24750b9ad5ef 158 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Christopher Haster 1:24750b9ad5ef 159 */
Christopher Haster 1:24750b9ad5ef 160 static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Christopher Haster 1:24750b9ad5ef 161 {
Christopher Haster 1:24750b9ad5ef 162 const mbedtls_md_info_t *md_info;
Christopher Haster 1:24750b9ad5ef 163
Christopher Haster 1:24750b9ad5ef 164 if( *hash_len != 0 )
Christopher Haster 1:24750b9ad5ef 165 return( 0 );
Christopher Haster 1:24750b9ad5ef 166
Christopher Haster 1:24750b9ad5ef 167 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Christopher Haster 1:24750b9ad5ef 168 return( -1 );
Christopher Haster 1:24750b9ad5ef 169
Christopher Haster 1:24750b9ad5ef 170 *hash_len = mbedtls_md_get_size( md_info );
Christopher Haster 1:24750b9ad5ef 171 return( 0 );
Christopher Haster 1:24750b9ad5ef 172 }
Christopher Haster 1:24750b9ad5ef 173
Christopher Haster 1:24750b9ad5ef 174 /*
Christopher Haster 1:24750b9ad5ef 175 * Verify a signature
Christopher Haster 1:24750b9ad5ef 176 */
Christopher Haster 1:24750b9ad5ef 177 int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Christopher Haster 1:24750b9ad5ef 178 const unsigned char *hash, size_t hash_len,
Christopher Haster 1:24750b9ad5ef 179 const unsigned char *sig, size_t sig_len )
Christopher Haster 1:24750b9ad5ef 180 {
Christopher Haster 1:24750b9ad5ef 181 if( ctx == NULL || ctx->pk_info == NULL ||
Christopher Haster 1:24750b9ad5ef 182 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Christopher Haster 1:24750b9ad5ef 183 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Christopher Haster 1:24750b9ad5ef 184
Christopher Haster 1:24750b9ad5ef 185 if( ctx->pk_info->verify_func == NULL )
Christopher Haster 1:24750b9ad5ef 186 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Christopher Haster 1:24750b9ad5ef 187
Christopher Haster 1:24750b9ad5ef 188 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Christopher Haster 1:24750b9ad5ef 189 sig, sig_len ) );
Christopher Haster 1:24750b9ad5ef 190 }
Christopher Haster 1:24750b9ad5ef 191
Christopher Haster 1:24750b9ad5ef 192 /*
Christopher Haster 1:24750b9ad5ef 193 * Verify a signature with options
Christopher Haster 1:24750b9ad5ef 194 */
Christopher Haster 1:24750b9ad5ef 195 int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
Christopher Haster 1:24750b9ad5ef 196 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Christopher Haster 1:24750b9ad5ef 197 const unsigned char *hash, size_t hash_len,
Christopher Haster 1:24750b9ad5ef 198 const unsigned char *sig, size_t sig_len )
Christopher Haster 1:24750b9ad5ef 199 {
Christopher Haster 1:24750b9ad5ef 200 if( ctx == NULL || ctx->pk_info == NULL )
Christopher Haster 1:24750b9ad5ef 201 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Christopher Haster 1:24750b9ad5ef 202
Christopher Haster 1:24750b9ad5ef 203 if( ! mbedtls_pk_can_do( ctx, type ) )
Christopher Haster 1:24750b9ad5ef 204 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Christopher Haster 1:24750b9ad5ef 205
Christopher Haster 1:24750b9ad5ef 206 if( type == MBEDTLS_PK_RSASSA_PSS )
Christopher Haster 1:24750b9ad5ef 207 {
Christopher Haster 1:24750b9ad5ef 208 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Christopher Haster 1:24750b9ad5ef 209 int ret;
Christopher Haster 1:24750b9ad5ef 210 const mbedtls_pk_rsassa_pss_options *pss_opts;
Christopher Haster 1:24750b9ad5ef 211
Christopher Haster 1:24750b9ad5ef 212 if( options == NULL )
Christopher Haster 1:24750b9ad5ef 213 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Christopher Haster 1:24750b9ad5ef 214
Christopher Haster 1:24750b9ad5ef 215 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Christopher Haster 1:24750b9ad5ef 216
Christopher Haster 1:24750b9ad5ef 217 if( sig_len < mbedtls_pk_get_len( ctx ) )
Christopher Haster 1:24750b9ad5ef 218 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Christopher Haster 1:24750b9ad5ef 219
Christopher Haster 1:24750b9ad5ef 220 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
Christopher Haster 1:24750b9ad5ef 221 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Christopher Haster 1:24750b9ad5ef 222 md_alg, (unsigned int) hash_len, hash,
Christopher Haster 1:24750b9ad5ef 223 pss_opts->mgf1_hash_id,
Christopher Haster 1:24750b9ad5ef 224 pss_opts->expected_salt_len,
Christopher Haster 1:24750b9ad5ef 225 sig );
Christopher Haster 1:24750b9ad5ef 226 if( ret != 0 )
Christopher Haster 1:24750b9ad5ef 227 return( ret );
Christopher Haster 1:24750b9ad5ef 228
Christopher Haster 1:24750b9ad5ef 229 if( sig_len > mbedtls_pk_get_len( ctx ) )
Christopher Haster 1:24750b9ad5ef 230 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Christopher Haster 1:24750b9ad5ef 231
Christopher Haster 1:24750b9ad5ef 232 return( 0 );
Christopher Haster 1:24750b9ad5ef 233 #else
Christopher Haster 1:24750b9ad5ef 234 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Christopher Haster 1:24750b9ad5ef 235 #endif
Christopher Haster 1:24750b9ad5ef 236 }
Christopher Haster 1:24750b9ad5ef 237
Christopher Haster 1:24750b9ad5ef 238 /* General case: no options */
Christopher Haster 1:24750b9ad5ef 239 if( options != NULL )
Christopher Haster 1:24750b9ad5ef 240 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Christopher Haster 1:24750b9ad5ef 241
Christopher Haster 1:24750b9ad5ef 242 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Christopher Haster 1:24750b9ad5ef 243 }
Christopher Haster 1:24750b9ad5ef 244
Christopher Haster 1:24750b9ad5ef 245 /*
Christopher Haster 1:24750b9ad5ef 246 * Make a signature
Christopher Haster 1:24750b9ad5ef 247 */
Christopher Haster 1:24750b9ad5ef 248 int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Christopher Haster 1:24750b9ad5ef 249 const unsigned char *hash, size_t hash_len,
Christopher Haster 1:24750b9ad5ef 250 unsigned char *sig, size_t *sig_len,
Christopher Haster 1:24750b9ad5ef 251 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Christopher Haster 1:24750b9ad5ef 252 {
Christopher Haster 1:24750b9ad5ef 253 if( ctx == NULL || ctx->pk_info == NULL ||
Christopher Haster 1:24750b9ad5ef 254 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Christopher Haster 1:24750b9ad5ef 255 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Christopher Haster 1:24750b9ad5ef 256
Christopher Haster 1:24750b9ad5ef 257 if( ctx->pk_info->sign_func == NULL )
Christopher Haster 1:24750b9ad5ef 258 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Christopher Haster 1:24750b9ad5ef 259
Christopher Haster 1:24750b9ad5ef 260 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
Christopher Haster 1:24750b9ad5ef 261 sig, sig_len, f_rng, p_rng ) );
Christopher Haster 1:24750b9ad5ef 262 }
Christopher Haster 1:24750b9ad5ef 263
Christopher Haster 1:24750b9ad5ef 264 /*
Christopher Haster 1:24750b9ad5ef 265 * Decrypt message
Christopher Haster 1:24750b9ad5ef 266 */
Christopher Haster 1:24750b9ad5ef 267 int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Christopher Haster 1:24750b9ad5ef 268 const unsigned char *input, size_t ilen,
Christopher Haster 1:24750b9ad5ef 269 unsigned char *output, size_t *olen, size_t osize,
Christopher Haster 1:24750b9ad5ef 270 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Christopher Haster 1:24750b9ad5ef 271 {
Christopher Haster 1:24750b9ad5ef 272 if( ctx == NULL || ctx->pk_info == NULL )
Christopher Haster 1:24750b9ad5ef 273 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Christopher Haster 1:24750b9ad5ef 274
Christopher Haster 1:24750b9ad5ef 275 if( ctx->pk_info->decrypt_func == NULL )
Christopher Haster 1:24750b9ad5ef 276 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Christopher Haster 1:24750b9ad5ef 277
Christopher Haster 1:24750b9ad5ef 278 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
Christopher Haster 1:24750b9ad5ef 279 output, olen, osize, f_rng, p_rng ) );
Christopher Haster 1:24750b9ad5ef 280 }
Christopher Haster 1:24750b9ad5ef 281
Christopher Haster 1:24750b9ad5ef 282 /*
Christopher Haster 1:24750b9ad5ef 283 * Encrypt message
Christopher Haster 1:24750b9ad5ef 284 */
Christopher Haster 1:24750b9ad5ef 285 int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Christopher Haster 1:24750b9ad5ef 286 const unsigned char *input, size_t ilen,
Christopher Haster 1:24750b9ad5ef 287 unsigned char *output, size_t *olen, size_t osize,
Christopher Haster 1:24750b9ad5ef 288 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Christopher Haster 1:24750b9ad5ef 289 {
Christopher Haster 1:24750b9ad5ef 290 if( ctx == NULL || ctx->pk_info == NULL )
Christopher Haster 1:24750b9ad5ef 291 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Christopher Haster 1:24750b9ad5ef 292
Christopher Haster 1:24750b9ad5ef 293 if( ctx->pk_info->encrypt_func == NULL )
Christopher Haster 1:24750b9ad5ef 294 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Christopher Haster 1:24750b9ad5ef 295
Christopher Haster 1:24750b9ad5ef 296 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
Christopher Haster 1:24750b9ad5ef 297 output, olen, osize, f_rng, p_rng ) );
Christopher Haster 1:24750b9ad5ef 298 }
Christopher Haster 1:24750b9ad5ef 299
Christopher Haster 1:24750b9ad5ef 300 /*
Christopher Haster 1:24750b9ad5ef 301 * Check public-private key pair
Christopher Haster 1:24750b9ad5ef 302 */
Christopher Haster 1:24750b9ad5ef 303 int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Christopher Haster 1:24750b9ad5ef 304 {
Christopher Haster 1:24750b9ad5ef 305 if( pub == NULL || pub->pk_info == NULL ||
Christopher Haster 1:24750b9ad5ef 306 prv == NULL || prv->pk_info == NULL ||
Christopher Haster 1:24750b9ad5ef 307 prv->pk_info->check_pair_func == NULL )
Christopher Haster 1:24750b9ad5ef 308 {
Christopher Haster 1:24750b9ad5ef 309 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Christopher Haster 1:24750b9ad5ef 310 }
Christopher Haster 1:24750b9ad5ef 311
Christopher Haster 1:24750b9ad5ef 312 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Christopher Haster 1:24750b9ad5ef 313 {
Christopher Haster 1:24750b9ad5ef 314 if( pub->pk_info->type != MBEDTLS_PK_RSA )
Christopher Haster 1:24750b9ad5ef 315 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Christopher Haster 1:24750b9ad5ef 316 }
Christopher Haster 1:24750b9ad5ef 317 else
Christopher Haster 1:24750b9ad5ef 318 {
Christopher Haster 1:24750b9ad5ef 319 if( pub->pk_info != prv->pk_info )
Christopher Haster 1:24750b9ad5ef 320 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Christopher Haster 1:24750b9ad5ef 321 }
Christopher Haster 1:24750b9ad5ef 322
Christopher Haster 1:24750b9ad5ef 323 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Christopher Haster 1:24750b9ad5ef 324 }
Christopher Haster 1:24750b9ad5ef 325
Christopher Haster 1:24750b9ad5ef 326 /*
Christopher Haster 1:24750b9ad5ef 327 * Get key size in bits
Christopher Haster 1:24750b9ad5ef 328 */
Christopher Haster 1:24750b9ad5ef 329 size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Christopher Haster 1:24750b9ad5ef 330 {
Christopher Haster 1:24750b9ad5ef 331 if( ctx == NULL || ctx->pk_info == NULL )
Christopher Haster 1:24750b9ad5ef 332 return( 0 );
Christopher Haster 1:24750b9ad5ef 333
Christopher Haster 1:24750b9ad5ef 334 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Christopher Haster 1:24750b9ad5ef 335 }
Christopher Haster 1:24750b9ad5ef 336
Christopher Haster 1:24750b9ad5ef 337 /*
Christopher Haster 1:24750b9ad5ef 338 * Export debug information
Christopher Haster 1:24750b9ad5ef 339 */
Christopher Haster 1:24750b9ad5ef 340 int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Christopher Haster 1:24750b9ad5ef 341 {
Christopher Haster 1:24750b9ad5ef 342 if( ctx == NULL || ctx->pk_info == NULL )
Christopher Haster 1:24750b9ad5ef 343 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Christopher Haster 1:24750b9ad5ef 344
Christopher Haster 1:24750b9ad5ef 345 if( ctx->pk_info->debug_func == NULL )
Christopher Haster 1:24750b9ad5ef 346 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Christopher Haster 1:24750b9ad5ef 347
Christopher Haster 1:24750b9ad5ef 348 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Christopher Haster 1:24750b9ad5ef 349 return( 0 );
Christopher Haster 1:24750b9ad5ef 350 }
Christopher Haster 1:24750b9ad5ef 351
Christopher Haster 1:24750b9ad5ef 352 /*
Christopher Haster 1:24750b9ad5ef 353 * Access the PK type name
Christopher Haster 1:24750b9ad5ef 354 */
Christopher Haster 1:24750b9ad5ef 355 const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Christopher Haster 1:24750b9ad5ef 356 {
Christopher Haster 1:24750b9ad5ef 357 if( ctx == NULL || ctx->pk_info == NULL )
Christopher Haster 1:24750b9ad5ef 358 return( "invalid PK" );
Christopher Haster 1:24750b9ad5ef 359
Christopher Haster 1:24750b9ad5ef 360 return( ctx->pk_info->name );
Christopher Haster 1:24750b9ad5ef 361 }
Christopher Haster 1:24750b9ad5ef 362
Christopher Haster 1:24750b9ad5ef 363 /*
Christopher Haster 1:24750b9ad5ef 364 * Access the PK type
Christopher Haster 1:24750b9ad5ef 365 */
Christopher Haster 1:24750b9ad5ef 366 mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Christopher Haster 1:24750b9ad5ef 367 {
Christopher Haster 1:24750b9ad5ef 368 if( ctx == NULL || ctx->pk_info == NULL )
Christopher Haster 1:24750b9ad5ef 369 return( MBEDTLS_PK_NONE );
Christopher Haster 1:24750b9ad5ef 370
Christopher Haster 1:24750b9ad5ef 371 return( ctx->pk_info->type );
Christopher Haster 1:24750b9ad5ef 372 }
Christopher Haster 1:24750b9ad5ef 373
Christopher Haster 1:24750b9ad5ef 374 #endif /* MBEDTLS_PK_C */