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