mbed TLS Build
Dependents: Encrypt_Decrypt1 mbed_blink_tls encrypt encrypt
library/pk.c@0:cdf462088d13, 2017-01-05 (annotated)
- Committer:
- markrad
- Date:
- Thu Jan 05 00:18:44 2017 +0000
- Revision:
- 0:cdf462088d13
Initial commit
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 | |
markrad | 0:cdf462088d13 | 32 | #if defined(MBEDTLS_RSA_C) |
markrad | 0:cdf462088d13 | 33 | #include "mbedtls/rsa.h" |
markrad | 0:cdf462088d13 | 34 | #endif |
markrad | 0:cdf462088d13 | 35 | #if defined(MBEDTLS_ECP_C) |
markrad | 0:cdf462088d13 | 36 | #include "mbedtls/ecp.h" |
markrad | 0:cdf462088d13 | 37 | #endif |
markrad | 0:cdf462088d13 | 38 | #if defined(MBEDTLS_ECDSA_C) |
markrad | 0:cdf462088d13 | 39 | #include "mbedtls/ecdsa.h" |
markrad | 0:cdf462088d13 | 40 | #endif |
markrad | 0:cdf462088d13 | 41 | |
markrad | 0:cdf462088d13 | 42 | /* Implementation that should never be optimized out by the compiler */ |
markrad | 0:cdf462088d13 | 43 | static void mbedtls_zeroize( void *v, size_t n ) { |
markrad | 0:cdf462088d13 | 44 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
markrad | 0:cdf462088d13 | 45 | } |
markrad | 0:cdf462088d13 | 46 | |
markrad | 0:cdf462088d13 | 47 | /* |
markrad | 0:cdf462088d13 | 48 | * Initialise a mbedtls_pk_context |
markrad | 0:cdf462088d13 | 49 | */ |
markrad | 0:cdf462088d13 | 50 | void mbedtls_pk_init( mbedtls_pk_context *ctx ) |
markrad | 0:cdf462088d13 | 51 | { |
markrad | 0:cdf462088d13 | 52 | if( ctx == NULL ) |
markrad | 0:cdf462088d13 | 53 | return; |
markrad | 0:cdf462088d13 | 54 | |
markrad | 0:cdf462088d13 | 55 | ctx->pk_info = NULL; |
markrad | 0:cdf462088d13 | 56 | ctx->pk_ctx = NULL; |
markrad | 0:cdf462088d13 | 57 | } |
markrad | 0:cdf462088d13 | 58 | |
markrad | 0:cdf462088d13 | 59 | /* |
markrad | 0:cdf462088d13 | 60 | * Free (the components of) a mbedtls_pk_context |
markrad | 0:cdf462088d13 | 61 | */ |
markrad | 0:cdf462088d13 | 62 | void mbedtls_pk_free( mbedtls_pk_context *ctx ) |
markrad | 0:cdf462088d13 | 63 | { |
markrad | 0:cdf462088d13 | 64 | if( ctx == NULL || ctx->pk_info == NULL ) |
markrad | 0:cdf462088d13 | 65 | return; |
markrad | 0:cdf462088d13 | 66 | |
markrad | 0:cdf462088d13 | 67 | ctx->pk_info->ctx_free_func( ctx->pk_ctx ); |
markrad | 0:cdf462088d13 | 68 | |
markrad | 0:cdf462088d13 | 69 | mbedtls_zeroize( ctx, sizeof( mbedtls_pk_context ) ); |
markrad | 0:cdf462088d13 | 70 | } |
markrad | 0:cdf462088d13 | 71 | |
markrad | 0:cdf462088d13 | 72 | /* |
markrad | 0:cdf462088d13 | 73 | * Get pk_info structure from type |
markrad | 0:cdf462088d13 | 74 | */ |
markrad | 0:cdf462088d13 | 75 | const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type ) |
markrad | 0:cdf462088d13 | 76 | { |
markrad | 0:cdf462088d13 | 77 | switch( pk_type ) { |
markrad | 0:cdf462088d13 | 78 | #if defined(MBEDTLS_RSA_C) |
markrad | 0:cdf462088d13 | 79 | case MBEDTLS_PK_RSA: |
markrad | 0:cdf462088d13 | 80 | return( &mbedtls_rsa_info ); |
markrad | 0:cdf462088d13 | 81 | #endif |
markrad | 0:cdf462088d13 | 82 | #if defined(MBEDTLS_ECP_C) |
markrad | 0:cdf462088d13 | 83 | case MBEDTLS_PK_ECKEY: |
markrad | 0:cdf462088d13 | 84 | return( &mbedtls_eckey_info ); |
markrad | 0:cdf462088d13 | 85 | case MBEDTLS_PK_ECKEY_DH: |
markrad | 0:cdf462088d13 | 86 | return( &mbedtls_eckeydh_info ); |
markrad | 0:cdf462088d13 | 87 | #endif |
markrad | 0:cdf462088d13 | 88 | #if defined(MBEDTLS_ECDSA_C) |
markrad | 0:cdf462088d13 | 89 | case MBEDTLS_PK_ECDSA: |
markrad | 0:cdf462088d13 | 90 | return( &mbedtls_ecdsa_info ); |
markrad | 0:cdf462088d13 | 91 | #endif |
markrad | 0:cdf462088d13 | 92 | /* MBEDTLS_PK_RSA_ALT omitted on purpose */ |
markrad | 0:cdf462088d13 | 93 | default: |
markrad | 0:cdf462088d13 | 94 | return( NULL ); |
markrad | 0:cdf462088d13 | 95 | } |
markrad | 0:cdf462088d13 | 96 | } |
markrad | 0:cdf462088d13 | 97 | |
markrad | 0:cdf462088d13 | 98 | /* |
markrad | 0:cdf462088d13 | 99 | * Initialise context |
markrad | 0:cdf462088d13 | 100 | */ |
markrad | 0:cdf462088d13 | 101 | int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info ) |
markrad | 0:cdf462088d13 | 102 | { |
markrad | 0:cdf462088d13 | 103 | if( ctx == NULL || info == NULL || ctx->pk_info != NULL ) |
markrad | 0:cdf462088d13 | 104 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 105 | |
markrad | 0:cdf462088d13 | 106 | if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) |
markrad | 0:cdf462088d13 | 107 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); |
markrad | 0:cdf462088d13 | 108 | |
markrad | 0:cdf462088d13 | 109 | ctx->pk_info = info; |
markrad | 0:cdf462088d13 | 110 | |
markrad | 0:cdf462088d13 | 111 | return( 0 ); |
markrad | 0:cdf462088d13 | 112 | } |
markrad | 0:cdf462088d13 | 113 | |
markrad | 0:cdf462088d13 | 114 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
markrad | 0:cdf462088d13 | 115 | /* |
markrad | 0:cdf462088d13 | 116 | * Initialize an RSA-alt context |
markrad | 0:cdf462088d13 | 117 | */ |
markrad | 0:cdf462088d13 | 118 | int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key, |
markrad | 0:cdf462088d13 | 119 | mbedtls_pk_rsa_alt_decrypt_func decrypt_func, |
markrad | 0:cdf462088d13 | 120 | mbedtls_pk_rsa_alt_sign_func sign_func, |
markrad | 0:cdf462088d13 | 121 | mbedtls_pk_rsa_alt_key_len_func key_len_func ) |
markrad | 0:cdf462088d13 | 122 | { |
markrad | 0:cdf462088d13 | 123 | mbedtls_rsa_alt_context *rsa_alt; |
markrad | 0:cdf462088d13 | 124 | const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info; |
markrad | 0:cdf462088d13 | 125 | |
markrad | 0:cdf462088d13 | 126 | if( ctx == NULL || ctx->pk_info != NULL ) |
markrad | 0:cdf462088d13 | 127 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 128 | |
markrad | 0:cdf462088d13 | 129 | if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) |
markrad | 0:cdf462088d13 | 130 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); |
markrad | 0:cdf462088d13 | 131 | |
markrad | 0:cdf462088d13 | 132 | ctx->pk_info = info; |
markrad | 0:cdf462088d13 | 133 | |
markrad | 0:cdf462088d13 | 134 | rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx; |
markrad | 0:cdf462088d13 | 135 | |
markrad | 0:cdf462088d13 | 136 | rsa_alt->key = key; |
markrad | 0:cdf462088d13 | 137 | rsa_alt->decrypt_func = decrypt_func; |
markrad | 0:cdf462088d13 | 138 | rsa_alt->sign_func = sign_func; |
markrad | 0:cdf462088d13 | 139 | rsa_alt->key_len_func = key_len_func; |
markrad | 0:cdf462088d13 | 140 | |
markrad | 0:cdf462088d13 | 141 | return( 0 ); |
markrad | 0:cdf462088d13 | 142 | } |
markrad | 0:cdf462088d13 | 143 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
markrad | 0:cdf462088d13 | 144 | |
markrad | 0:cdf462088d13 | 145 | /* |
markrad | 0:cdf462088d13 | 146 | * Tell if a PK can do the operations of the given type |
markrad | 0:cdf462088d13 | 147 | */ |
markrad | 0:cdf462088d13 | 148 | int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type ) |
markrad | 0:cdf462088d13 | 149 | { |
markrad | 0:cdf462088d13 | 150 | /* null or NONE context can't do anything */ |
markrad | 0:cdf462088d13 | 151 | if( ctx == NULL || ctx->pk_info == NULL ) |
markrad | 0:cdf462088d13 | 152 | return( 0 ); |
markrad | 0:cdf462088d13 | 153 | |
markrad | 0:cdf462088d13 | 154 | return( ctx->pk_info->can_do( type ) ); |
markrad | 0:cdf462088d13 | 155 | } |
markrad | 0:cdf462088d13 | 156 | |
markrad | 0:cdf462088d13 | 157 | /* |
markrad | 0:cdf462088d13 | 158 | * Helper for mbedtls_pk_sign and mbedtls_pk_verify |
markrad | 0:cdf462088d13 | 159 | */ |
markrad | 0:cdf462088d13 | 160 | static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len ) |
markrad | 0:cdf462088d13 | 161 | { |
markrad | 0:cdf462088d13 | 162 | const mbedtls_md_info_t *md_info; |
markrad | 0:cdf462088d13 | 163 | |
markrad | 0:cdf462088d13 | 164 | if( *hash_len != 0 ) |
markrad | 0:cdf462088d13 | 165 | return( 0 ); |
markrad | 0:cdf462088d13 | 166 | |
markrad | 0:cdf462088d13 | 167 | if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL ) |
markrad | 0:cdf462088d13 | 168 | return( -1 ); |
markrad | 0:cdf462088d13 | 169 | |
markrad | 0:cdf462088d13 | 170 | *hash_len = mbedtls_md_get_size( md_info ); |
markrad | 0:cdf462088d13 | 171 | return( 0 ); |
markrad | 0:cdf462088d13 | 172 | } |
markrad | 0:cdf462088d13 | 173 | |
markrad | 0:cdf462088d13 | 174 | /* |
markrad | 0:cdf462088d13 | 175 | * Verify a signature |
markrad | 0:cdf462088d13 | 176 | */ |
markrad | 0:cdf462088d13 | 177 | int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 178 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 179 | const unsigned char *sig, size_t sig_len ) |
markrad | 0:cdf462088d13 | 180 | { |
markrad | 0:cdf462088d13 | 181 | if( ctx == NULL || ctx->pk_info == NULL || |
markrad | 0:cdf462088d13 | 182 | pk_hashlen_helper( md_alg, &hash_len ) != 0 ) |
markrad | 0:cdf462088d13 | 183 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 184 | |
markrad | 0:cdf462088d13 | 185 | if( ctx->pk_info->verify_func == NULL ) |
markrad | 0:cdf462088d13 | 186 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
markrad | 0:cdf462088d13 | 187 | |
markrad | 0:cdf462088d13 | 188 | return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len, |
markrad | 0:cdf462088d13 | 189 | sig, sig_len ) ); |
markrad | 0:cdf462088d13 | 190 | } |
markrad | 0:cdf462088d13 | 191 | |
markrad | 0:cdf462088d13 | 192 | /* |
markrad | 0:cdf462088d13 | 193 | * Verify a signature with options |
markrad | 0:cdf462088d13 | 194 | */ |
markrad | 0:cdf462088d13 | 195 | int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options, |
markrad | 0:cdf462088d13 | 196 | mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 197 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 198 | const unsigned char *sig, size_t sig_len ) |
markrad | 0:cdf462088d13 | 199 | { |
markrad | 0:cdf462088d13 | 200 | if( ctx == NULL || ctx->pk_info == NULL ) |
markrad | 0:cdf462088d13 | 201 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 202 | |
markrad | 0:cdf462088d13 | 203 | if( ! mbedtls_pk_can_do( ctx, type ) ) |
markrad | 0:cdf462088d13 | 204 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
markrad | 0:cdf462088d13 | 205 | |
markrad | 0:cdf462088d13 | 206 | if( type == MBEDTLS_PK_RSASSA_PSS ) |
markrad | 0:cdf462088d13 | 207 | { |
markrad | 0:cdf462088d13 | 208 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) |
markrad | 0:cdf462088d13 | 209 | int ret; |
markrad | 0:cdf462088d13 | 210 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
markrad | 0:cdf462088d13 | 211 | |
markrad | 0:cdf462088d13 | 212 | if( options == NULL ) |
markrad | 0:cdf462088d13 | 213 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 214 | |
markrad | 0:cdf462088d13 | 215 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) options; |
markrad | 0:cdf462088d13 | 216 | |
markrad | 0:cdf462088d13 | 217 | if( sig_len < mbedtls_pk_get_len( ctx ) ) |
markrad | 0:cdf462088d13 | 218 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
markrad | 0:cdf462088d13 | 219 | |
markrad | 0:cdf462088d13 | 220 | ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ), |
markrad | 0:cdf462088d13 | 221 | NULL, NULL, MBEDTLS_RSA_PUBLIC, |
markrad | 0:cdf462088d13 | 222 | md_alg, (unsigned int) hash_len, hash, |
markrad | 0:cdf462088d13 | 223 | pss_opts->mgf1_hash_id, |
markrad | 0:cdf462088d13 | 224 | pss_opts->expected_salt_len, |
markrad | 0:cdf462088d13 | 225 | sig ); |
markrad | 0:cdf462088d13 | 226 | if( ret != 0 ) |
markrad | 0:cdf462088d13 | 227 | return( ret ); |
markrad | 0:cdf462088d13 | 228 | |
markrad | 0:cdf462088d13 | 229 | if( sig_len > mbedtls_pk_get_len( ctx ) ) |
markrad | 0:cdf462088d13 | 230 | return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH ); |
markrad | 0:cdf462088d13 | 231 | |
markrad | 0:cdf462088d13 | 232 | return( 0 ); |
markrad | 0:cdf462088d13 | 233 | #else |
markrad | 0:cdf462088d13 | 234 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); |
markrad | 0:cdf462088d13 | 235 | #endif |
markrad | 0:cdf462088d13 | 236 | } |
markrad | 0:cdf462088d13 | 237 | |
markrad | 0:cdf462088d13 | 238 | /* General case: no options */ |
markrad | 0:cdf462088d13 | 239 | if( options != NULL ) |
markrad | 0:cdf462088d13 | 240 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 241 | |
markrad | 0:cdf462088d13 | 242 | return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) ); |
markrad | 0:cdf462088d13 | 243 | } |
markrad | 0:cdf462088d13 | 244 | |
markrad | 0:cdf462088d13 | 245 | /* |
markrad | 0:cdf462088d13 | 246 | * Make a signature |
markrad | 0:cdf462088d13 | 247 | */ |
markrad | 0:cdf462088d13 | 248 | int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
markrad | 0:cdf462088d13 | 249 | const unsigned char *hash, size_t hash_len, |
markrad | 0:cdf462088d13 | 250 | unsigned char *sig, size_t *sig_len, |
markrad | 0:cdf462088d13 | 251 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
markrad | 0:cdf462088d13 | 252 | { |
markrad | 0:cdf462088d13 | 253 | if( ctx == NULL || ctx->pk_info == NULL || |
markrad | 0:cdf462088d13 | 254 | pk_hashlen_helper( md_alg, &hash_len ) != 0 ) |
markrad | 0:cdf462088d13 | 255 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 256 | |
markrad | 0:cdf462088d13 | 257 | if( ctx->pk_info->sign_func == NULL ) |
markrad | 0:cdf462088d13 | 258 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
markrad | 0:cdf462088d13 | 259 | |
markrad | 0:cdf462088d13 | 260 | return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len, |
markrad | 0:cdf462088d13 | 261 | sig, sig_len, f_rng, p_rng ) ); |
markrad | 0:cdf462088d13 | 262 | } |
markrad | 0:cdf462088d13 | 263 | |
markrad | 0:cdf462088d13 | 264 | /* |
markrad | 0:cdf462088d13 | 265 | * Decrypt message |
markrad | 0:cdf462088d13 | 266 | */ |
markrad | 0:cdf462088d13 | 267 | int mbedtls_pk_decrypt( mbedtls_pk_context *ctx, |
markrad | 0:cdf462088d13 | 268 | const unsigned char *input, size_t ilen, |
markrad | 0:cdf462088d13 | 269 | unsigned char *output, size_t *olen, size_t osize, |
markrad | 0:cdf462088d13 | 270 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
markrad | 0:cdf462088d13 | 271 | { |
markrad | 0:cdf462088d13 | 272 | if( ctx == NULL || ctx->pk_info == NULL ) |
markrad | 0:cdf462088d13 | 273 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 274 | |
markrad | 0:cdf462088d13 | 275 | if( ctx->pk_info->decrypt_func == NULL ) |
markrad | 0:cdf462088d13 | 276 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
markrad | 0:cdf462088d13 | 277 | |
markrad | 0:cdf462088d13 | 278 | return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen, |
markrad | 0:cdf462088d13 | 279 | output, olen, osize, f_rng, p_rng ) ); |
markrad | 0:cdf462088d13 | 280 | } |
markrad | 0:cdf462088d13 | 281 | |
markrad | 0:cdf462088d13 | 282 | /* |
markrad | 0:cdf462088d13 | 283 | * Encrypt message |
markrad | 0:cdf462088d13 | 284 | */ |
markrad | 0:cdf462088d13 | 285 | int mbedtls_pk_encrypt( mbedtls_pk_context *ctx, |
markrad | 0:cdf462088d13 | 286 | const unsigned char *input, size_t ilen, |
markrad | 0:cdf462088d13 | 287 | unsigned char *output, size_t *olen, size_t osize, |
markrad | 0:cdf462088d13 | 288 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
markrad | 0:cdf462088d13 | 289 | { |
markrad | 0:cdf462088d13 | 290 | if( ctx == NULL || ctx->pk_info == NULL ) |
markrad | 0:cdf462088d13 | 291 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 292 | |
markrad | 0:cdf462088d13 | 293 | if( ctx->pk_info->encrypt_func == NULL ) |
markrad | 0:cdf462088d13 | 294 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
markrad | 0:cdf462088d13 | 295 | |
markrad | 0:cdf462088d13 | 296 | return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen, |
markrad | 0:cdf462088d13 | 297 | output, olen, osize, f_rng, p_rng ) ); |
markrad | 0:cdf462088d13 | 298 | } |
markrad | 0:cdf462088d13 | 299 | |
markrad | 0:cdf462088d13 | 300 | /* |
markrad | 0:cdf462088d13 | 301 | * Check public-private key pair |
markrad | 0:cdf462088d13 | 302 | */ |
markrad | 0:cdf462088d13 | 303 | int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv ) |
markrad | 0:cdf462088d13 | 304 | { |
markrad | 0:cdf462088d13 | 305 | if( pub == NULL || pub->pk_info == NULL || |
markrad | 0:cdf462088d13 | 306 | prv == NULL || prv->pk_info == NULL || |
markrad | 0:cdf462088d13 | 307 | prv->pk_info->check_pair_func == NULL ) |
markrad | 0:cdf462088d13 | 308 | { |
markrad | 0:cdf462088d13 | 309 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 310 | } |
markrad | 0:cdf462088d13 | 311 | |
markrad | 0:cdf462088d13 | 312 | if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT ) |
markrad | 0:cdf462088d13 | 313 | { |
markrad | 0:cdf462088d13 | 314 | if( pub->pk_info->type != MBEDTLS_PK_RSA ) |
markrad | 0:cdf462088d13 | 315 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
markrad | 0:cdf462088d13 | 316 | } |
markrad | 0:cdf462088d13 | 317 | else |
markrad | 0:cdf462088d13 | 318 | { |
markrad | 0:cdf462088d13 | 319 | if( pub->pk_info != prv->pk_info ) |
markrad | 0:cdf462088d13 | 320 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
markrad | 0:cdf462088d13 | 321 | } |
markrad | 0:cdf462088d13 | 322 | |
markrad | 0:cdf462088d13 | 323 | return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) ); |
markrad | 0:cdf462088d13 | 324 | } |
markrad | 0:cdf462088d13 | 325 | |
markrad | 0:cdf462088d13 | 326 | /* |
markrad | 0:cdf462088d13 | 327 | * Get key size in bits |
markrad | 0:cdf462088d13 | 328 | */ |
markrad | 0:cdf462088d13 | 329 | size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx ) |
markrad | 0:cdf462088d13 | 330 | { |
markrad | 0:cdf462088d13 | 331 | if( ctx == NULL || ctx->pk_info == NULL ) |
markrad | 0:cdf462088d13 | 332 | return( 0 ); |
markrad | 0:cdf462088d13 | 333 | |
markrad | 0:cdf462088d13 | 334 | return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) ); |
markrad | 0:cdf462088d13 | 335 | } |
markrad | 0:cdf462088d13 | 336 | |
markrad | 0:cdf462088d13 | 337 | /* |
markrad | 0:cdf462088d13 | 338 | * Export debug information |
markrad | 0:cdf462088d13 | 339 | */ |
markrad | 0:cdf462088d13 | 340 | int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items ) |
markrad | 0:cdf462088d13 | 341 | { |
markrad | 0:cdf462088d13 | 342 | if( ctx == NULL || ctx->pk_info == NULL ) |
markrad | 0:cdf462088d13 | 343 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 344 | |
markrad | 0:cdf462088d13 | 345 | if( ctx->pk_info->debug_func == NULL ) |
markrad | 0:cdf462088d13 | 346 | return( MBEDTLS_ERR_PK_TYPE_MISMATCH ); |
markrad | 0:cdf462088d13 | 347 | |
markrad | 0:cdf462088d13 | 348 | ctx->pk_info->debug_func( ctx->pk_ctx, items ); |
markrad | 0:cdf462088d13 | 349 | return( 0 ); |
markrad | 0:cdf462088d13 | 350 | } |
markrad | 0:cdf462088d13 | 351 | |
markrad | 0:cdf462088d13 | 352 | /* |
markrad | 0:cdf462088d13 | 353 | * Access the PK type name |
markrad | 0:cdf462088d13 | 354 | */ |
markrad | 0:cdf462088d13 | 355 | const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx ) |
markrad | 0:cdf462088d13 | 356 | { |
markrad | 0:cdf462088d13 | 357 | if( ctx == NULL || ctx->pk_info == NULL ) |
markrad | 0:cdf462088d13 | 358 | return( "invalid PK" ); |
markrad | 0:cdf462088d13 | 359 | |
markrad | 0:cdf462088d13 | 360 | return( ctx->pk_info->name ); |
markrad | 0:cdf462088d13 | 361 | } |
markrad | 0:cdf462088d13 | 362 | |
markrad | 0:cdf462088d13 | 363 | /* |
markrad | 0:cdf462088d13 | 364 | * Access the PK type |
markrad | 0:cdf462088d13 | 365 | */ |
markrad | 0:cdf462088d13 | 366 | mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx ) |
markrad | 0:cdf462088d13 | 367 | { |
markrad | 0:cdf462088d13 | 368 | if( ctx == NULL || ctx->pk_info == NULL ) |
markrad | 0:cdf462088d13 | 369 | return( MBEDTLS_PK_NONE ); |
markrad | 0:cdf462088d13 | 370 | |
markrad | 0:cdf462088d13 | 371 | return( ctx->pk_info->type ); |
markrad | 0:cdf462088d13 | 372 | } |
markrad | 0:cdf462088d13 | 373 | |
markrad | 0:cdf462088d13 | 374 | #endif /* MBEDTLS_PK_C */ |