Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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