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