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