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