Mark Radbourne / mbedtls

Dependents:   Encrypt_Decrypt1 mbed_blink_tls encrypt encrypt

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pk.c Source File

pk.c

00001 /*
00002  *  Public Key abstraction layer
00003  *
00004  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  *  This file is part of mbed TLS (https://tls.mbed.org)
00020  */
00021 
00022 #if !defined(MBEDTLS_CONFIG_FILE)
00023 #include "mbedtls/config.h"
00024 #else
00025 #include MBEDTLS_CONFIG_FILE
00026 #endif
00027 
00028 #if defined(MBEDTLS_PK_C)
00029 #include "mbedtls/pk.h"
00030 #include "mbedtls/pk_internal.h"
00031 
00032 #if defined(MBEDTLS_RSA_C)
00033 #include "mbedtls/rsa.h"
00034 #endif
00035 #if defined(MBEDTLS_ECP_C)
00036 #include "mbedtls/ecp.h"
00037 #endif
00038 #if defined(MBEDTLS_ECDSA_C)
00039 #include "mbedtls/ecdsa.h"
00040 #endif
00041 
00042 /* Implementation that should never be optimized out by the compiler */
00043 static void mbedtls_zeroize( void *v, size_t n ) {
00044     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
00045 }
00046 
00047 /*
00048  * Initialise a mbedtls_pk_context
00049  */
00050 void mbedtls_pk_init( mbedtls_pk_context *ctx )
00051 {
00052     if( ctx == NULL )
00053         return;
00054 
00055     ctx->pk_info = NULL;
00056     ctx->pk_ctx = NULL;
00057 }
00058 
00059 /*
00060  * Free (the components of) a mbedtls_pk_context
00061  */
00062 void mbedtls_pk_free( mbedtls_pk_context *ctx )
00063 {
00064     if( ctx == NULL || ctx->pk_info == NULL )
00065         return;
00066 
00067     ctx->pk_info->ctx_free_func( ctx->pk_ctx );
00068 
00069     mbedtls_zeroize( ctx, sizeof( mbedtls_pk_context ) );
00070 }
00071 
00072 /*
00073  * Get pk_info structure from type
00074  */
00075 const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
00076 {
00077     switch( pk_type ) {
00078 #if defined(MBEDTLS_RSA_C)
00079         case MBEDTLS_PK_RSA:
00080             return( &mbedtls_rsa_info );
00081 #endif
00082 #if defined(MBEDTLS_ECP_C)
00083         case MBEDTLS_PK_ECKEY:
00084             return( &mbedtls_eckey_info );
00085         case MBEDTLS_PK_ECKEY_DH:
00086             return( &mbedtls_eckeydh_info );
00087 #endif
00088 #if defined(MBEDTLS_ECDSA_C)
00089         case MBEDTLS_PK_ECDSA:
00090             return( &mbedtls_ecdsa_info );
00091 #endif
00092         /* MBEDTLS_PK_RSA_ALT omitted on purpose */
00093         default:
00094             return( NULL );
00095     }
00096 }
00097 
00098 /*
00099  * Initialise context
00100  */
00101 int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
00102 {
00103     if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
00104         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00105 
00106     if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
00107         return( MBEDTLS_ERR_PK_ALLOC_FAILED );
00108 
00109     ctx->pk_info = info;
00110 
00111     return( 0 );
00112 }
00113 
00114 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
00115 /*
00116  * Initialize an RSA-alt context
00117  */
00118 int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
00119                          mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
00120                          mbedtls_pk_rsa_alt_sign_func sign_func,
00121                          mbedtls_pk_rsa_alt_key_len_func key_len_func )
00122 {
00123     mbedtls_rsa_alt_context *rsa_alt;
00124     const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
00125 
00126     if( ctx == NULL || ctx->pk_info != NULL )
00127         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00128 
00129     if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
00130         return( MBEDTLS_ERR_PK_ALLOC_FAILED );
00131 
00132     ctx->pk_info = info;
00133 
00134     rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
00135 
00136     rsa_alt->key = key;
00137     rsa_alt->decrypt_func = decrypt_func;
00138     rsa_alt->sign_func = sign_func;
00139     rsa_alt->key_len_func = key_len_func;
00140 
00141     return( 0 );
00142 }
00143 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
00144 
00145 /*
00146  * Tell if a PK can do the operations of the given type
00147  */
00148 int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
00149 {
00150     /* null or NONE context can't do anything */
00151     if( ctx == NULL || ctx->pk_info == NULL )
00152         return( 0 );
00153 
00154     return( ctx->pk_info->can_do( type ) );
00155 }
00156 
00157 /*
00158  * Helper for mbedtls_pk_sign and mbedtls_pk_verify
00159  */
00160 static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
00161 {
00162     const mbedtls_md_info_t *md_info;
00163 
00164     if( *hash_len != 0 )
00165         return( 0 );
00166 
00167     if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
00168         return( -1 );
00169 
00170     *hash_len = mbedtls_md_get_size( md_info );
00171     return( 0 );
00172 }
00173 
00174 /*
00175  * Verify a signature
00176  */
00177 int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
00178                const unsigned char *hash, size_t hash_len,
00179                const unsigned char *sig, size_t sig_len )
00180 {
00181     if( ctx == NULL || ctx->pk_info == NULL ||
00182         pk_hashlen_helper( md_alg, &hash_len ) != 0 )
00183         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00184 
00185     if( ctx->pk_info->verify_func == NULL )
00186         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00187 
00188     return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
00189                                        sig, sig_len ) );
00190 }
00191 
00192 /*
00193  * Verify a signature with options
00194  */
00195 int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
00196                    mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
00197                    const unsigned char *hash, size_t hash_len,
00198                    const unsigned char *sig, size_t sig_len )
00199 {
00200     if( ctx == NULL || ctx->pk_info == NULL )
00201         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00202 
00203     if( ! mbedtls_pk_can_do( ctx, type ) )
00204         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00205 
00206     if( type == MBEDTLS_PK_RSASSA_PSS )
00207     {
00208 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
00209         int ret;
00210         const mbedtls_pk_rsassa_pss_options *pss_opts;
00211 
00212         if( options == NULL )
00213             return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00214 
00215         pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
00216 
00217         if( sig_len < mbedtls_pk_get_len( ctx ) )
00218             return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
00219 
00220         ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
00221                 NULL, NULL, MBEDTLS_RSA_PUBLIC,
00222                 md_alg, (unsigned int) hash_len, hash,
00223                 pss_opts->mgf1_hash_id,
00224                 pss_opts->expected_salt_len,
00225                 sig );
00226         if( ret != 0 )
00227             return( ret );
00228 
00229         if( sig_len > mbedtls_pk_get_len( ctx ) )
00230             return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
00231 
00232         return( 0 );
00233 #else
00234         return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
00235 #endif
00236     }
00237 
00238     /* General case: no options */
00239     if( options != NULL )
00240         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00241 
00242     return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
00243 }
00244 
00245 /*
00246  * Make a signature
00247  */
00248 int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
00249              const unsigned char *hash, size_t hash_len,
00250              unsigned char *sig, size_t *sig_len,
00251              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00252 {
00253     if( ctx == NULL || ctx->pk_info == NULL ||
00254         pk_hashlen_helper( md_alg, &hash_len ) != 0 )
00255         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00256 
00257     if( ctx->pk_info->sign_func == NULL )
00258         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00259 
00260     return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
00261                                      sig, sig_len, f_rng, p_rng ) );
00262 }
00263 
00264 /*
00265  * Decrypt message
00266  */
00267 int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
00268                 const unsigned char *input, size_t ilen,
00269                 unsigned char *output, size_t *olen, size_t osize,
00270                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00271 {
00272     if( ctx == NULL || ctx->pk_info == NULL )
00273         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00274 
00275     if( ctx->pk_info->decrypt_func == NULL )
00276         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00277 
00278     return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
00279                 output, olen, osize, f_rng, p_rng ) );
00280 }
00281 
00282 /*
00283  * Encrypt message
00284  */
00285 int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
00286                 const unsigned char *input, size_t ilen,
00287                 unsigned char *output, size_t *olen, size_t osize,
00288                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00289 {
00290     if( ctx == NULL || ctx->pk_info == NULL )
00291         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00292 
00293     if( ctx->pk_info->encrypt_func == NULL )
00294         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00295 
00296     return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
00297                 output, olen, osize, f_rng, p_rng ) );
00298 }
00299 
00300 /*
00301  * Check public-private key pair
00302  */
00303 int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
00304 {
00305     if( pub == NULL || pub->pk_info == NULL ||
00306         prv == NULL || prv->pk_info == NULL ||
00307         prv->pk_info->check_pair_func == NULL )
00308     {
00309         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00310     }
00311 
00312     if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
00313     {
00314         if( pub->pk_info->type != MBEDTLS_PK_RSA )
00315             return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00316     }
00317     else
00318     {
00319         if( pub->pk_info != prv->pk_info )
00320             return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00321     }
00322 
00323     return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
00324 }
00325 
00326 /*
00327  * Get key size in bits
00328  */
00329 size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
00330 {
00331     if( ctx == NULL || ctx->pk_info == NULL )
00332         return( 0 );
00333 
00334     return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
00335 }
00336 
00337 /*
00338  * Export debug information
00339  */
00340 int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
00341 {
00342     if( ctx == NULL || ctx->pk_info == NULL )
00343         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00344 
00345     if( ctx->pk_info->debug_func == NULL )
00346         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00347 
00348     ctx->pk_info->debug_func( ctx->pk_ctx, items );
00349     return( 0 );
00350 }
00351 
00352 /*
00353  * Access the PK type name
00354  */
00355 const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
00356 {
00357     if( ctx == NULL || ctx->pk_info == NULL )
00358         return( "invalid PK" );
00359 
00360     return( ctx->pk_info->name );
00361 }
00362 
00363 /*
00364  * Access the PK type
00365  */
00366 mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
00367 {
00368     if( ctx == NULL || ctx->pk_info == NULL )
00369         return( MBEDTLS_PK_NONE );
00370 
00371     return( ctx->pk_info->type );
00372 }
00373 
00374 #endif /* MBEDTLS_PK_C */