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