Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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