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