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