Nicolas Borla / Mbed OS BBR_1Ebene
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 #include <limits.h>
00043 #include <stdint.h>
00044 
00045 /* Implementation that should never be optimized out by the compiler */
00046 static void mbedtls_zeroize( void *v, size_t n ) {
00047     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
00048 }
00049 
00050 /*
00051  * Initialise a mbedtls_pk_context
00052  */
00053 void mbedtls_pk_init( mbedtls_pk_context *ctx )
00054 {
00055     if( ctx == NULL )
00056         return;
00057 
00058     ctx->pk_info = NULL;
00059     ctx->pk_ctx = NULL;
00060 }
00061 
00062 /*
00063  * Free (the components of) a mbedtls_pk_context
00064  */
00065 void mbedtls_pk_free( mbedtls_pk_context *ctx )
00066 {
00067     if( ctx == NULL || ctx->pk_info == NULL )
00068         return;
00069 
00070     ctx->pk_info->ctx_free_func( ctx->pk_ctx );
00071 
00072     mbedtls_zeroize( ctx, sizeof( mbedtls_pk_context ) );
00073 }
00074 
00075 /*
00076  * Get pk_info structure from type
00077  */
00078 const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
00079 {
00080     switch( pk_type ) {
00081 #if defined(MBEDTLS_RSA_C)
00082         case MBEDTLS_PK_RSA:
00083             return( &mbedtls_rsa_info );
00084 #endif
00085 #if defined(MBEDTLS_ECP_C)
00086         case MBEDTLS_PK_ECKEY:
00087             return( &mbedtls_eckey_info );
00088         case MBEDTLS_PK_ECKEY_DH:
00089             return( &mbedtls_eckeydh_info );
00090 #endif
00091 #if defined(MBEDTLS_ECDSA_C)
00092         case MBEDTLS_PK_ECDSA:
00093             return( &mbedtls_ecdsa_info );
00094 #endif
00095         /* MBEDTLS_PK_RSA_ALT omitted on purpose */
00096         default:
00097             return( NULL );
00098     }
00099 }
00100 
00101 /*
00102  * Initialise context
00103  */
00104 int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
00105 {
00106     if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
00107         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00108 
00109     if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
00110         return( MBEDTLS_ERR_PK_ALLOC_FAILED );
00111 
00112     ctx->pk_info = info;
00113 
00114     return( 0 );
00115 }
00116 
00117 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
00118 /*
00119  * Initialize an RSA-alt context
00120  */
00121 int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
00122                          mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
00123                          mbedtls_pk_rsa_alt_sign_func sign_func,
00124                          mbedtls_pk_rsa_alt_key_len_func key_len_func )
00125 {
00126     mbedtls_rsa_alt_context *rsa_alt;
00127     const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
00128 
00129     if( ctx == NULL || ctx->pk_info != NULL )
00130         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00131 
00132     if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
00133         return( MBEDTLS_ERR_PK_ALLOC_FAILED );
00134 
00135     ctx->pk_info = info;
00136 
00137     rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
00138 
00139     rsa_alt->key = key;
00140     rsa_alt->decrypt_func = decrypt_func;
00141     rsa_alt->sign_func = sign_func;
00142     rsa_alt->key_len_func = key_len_func;
00143 
00144     return( 0 );
00145 }
00146 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
00147 
00148 /*
00149  * Tell if a PK can do the operations of the given type
00150  */
00151 int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
00152 {
00153     /* null or NONE context can't do anything */
00154     if( ctx == NULL || ctx->pk_info == NULL )
00155         return( 0 );
00156 
00157     return( ctx->pk_info->can_do( type ) );
00158 }
00159 
00160 /*
00161  * Helper for mbedtls_pk_sign and mbedtls_pk_verify
00162  */
00163 static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
00164 {
00165     const mbedtls_md_info_t *md_info;
00166 
00167     if( *hash_len != 0 )
00168         return( 0 );
00169 
00170     if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
00171         return( -1 );
00172 
00173     *hash_len = mbedtls_md_get_size( md_info );
00174     return( 0 );
00175 }
00176 
00177 /*
00178  * Verify a signature
00179  */
00180 int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
00181                const unsigned char *hash, size_t hash_len,
00182                const unsigned char *sig, size_t sig_len )
00183 {
00184     if( ctx == NULL || ctx->pk_info == NULL ||
00185         pk_hashlen_helper( md_alg, &hash_len ) != 0 )
00186         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00187 
00188     if( ctx->pk_info->verify_func == NULL )
00189         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00190 
00191     return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
00192                                        sig, sig_len ) );
00193 }
00194 
00195 /*
00196  * Verify a signature with options
00197  */
00198 int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
00199                    mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
00200                    const unsigned char *hash, size_t hash_len,
00201                    const unsigned char *sig, size_t sig_len )
00202 {
00203     if( ctx == NULL || ctx->pk_info == NULL )
00204         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00205 
00206     if( ! mbedtls_pk_can_do( ctx, type ) )
00207         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00208 
00209     if( type == MBEDTLS_PK_RSASSA_PSS )
00210     {
00211 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
00212         int ret;
00213         const mbedtls_pk_rsassa_pss_options *pss_opts;
00214 
00215 #if SIZE_MAX > UINT_MAX
00216         if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
00217             return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00218 #endif /* SIZE_MAX > UINT_MAX */
00219 
00220         if( options == NULL )
00221             return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00222 
00223         pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
00224 
00225         if( sig_len < mbedtls_pk_get_len( ctx ) )
00226             return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
00227 
00228         ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
00229                 NULL, NULL, MBEDTLS_RSA_PUBLIC,
00230                 md_alg, (unsigned int) hash_len, hash,
00231                 pss_opts->mgf1_hash_id,
00232                 pss_opts->expected_salt_len,
00233                 sig );
00234         if( ret != 0 )
00235             return( ret );
00236 
00237         if( sig_len > mbedtls_pk_get_len( ctx ) )
00238             return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
00239 
00240         return( 0 );
00241 #else
00242         return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
00243 #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
00244     }
00245 
00246     /* General case: no options */
00247     if( options != NULL )
00248         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00249 
00250     return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
00251 }
00252 
00253 /*
00254  * Make a signature
00255  */
00256 int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
00257              const unsigned char *hash, size_t hash_len,
00258              unsigned char *sig, size_t *sig_len,
00259              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00260 {
00261     if( ctx == NULL || ctx->pk_info == NULL ||
00262         pk_hashlen_helper( md_alg, &hash_len ) != 0 )
00263         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00264 
00265     if( ctx->pk_info->sign_func == NULL )
00266         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00267 
00268     return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
00269                                      sig, sig_len, f_rng, p_rng ) );
00270 }
00271 
00272 /*
00273  * Decrypt message
00274  */
00275 int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
00276                 const unsigned char *input, size_t ilen,
00277                 unsigned char *output, size_t *olen, size_t osize,
00278                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00279 {
00280     if( ctx == NULL || ctx->pk_info == NULL )
00281         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00282 
00283     if( ctx->pk_info->decrypt_func == NULL )
00284         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00285 
00286     return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
00287                 output, olen, osize, f_rng, p_rng ) );
00288 }
00289 
00290 /*
00291  * Encrypt message
00292  */
00293 int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
00294                 const unsigned char *input, size_t ilen,
00295                 unsigned char *output, size_t *olen, size_t osize,
00296                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00297 {
00298     if( ctx == NULL || ctx->pk_info == NULL )
00299         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00300 
00301     if( ctx->pk_info->encrypt_func == NULL )
00302         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00303 
00304     return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
00305                 output, olen, osize, f_rng, p_rng ) );
00306 }
00307 
00308 /*
00309  * Check public-private key pair
00310  */
00311 int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
00312 {
00313     if( pub == NULL || pub->pk_info == NULL ||
00314         prv == NULL || prv->pk_info == NULL ||
00315         prv->pk_info->check_pair_func == NULL )
00316     {
00317         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00318     }
00319 
00320     if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
00321     {
00322         if( pub->pk_info->type != MBEDTLS_PK_RSA )
00323             return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00324     }
00325     else
00326     {
00327         if( pub->pk_info != prv->pk_info )
00328             return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00329     }
00330 
00331     return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
00332 }
00333 
00334 /*
00335  * Get key size in bits
00336  */
00337 size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
00338 {
00339     if( ctx == NULL || ctx->pk_info == NULL )
00340         return( 0 );
00341 
00342     return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
00343 }
00344 
00345 /*
00346  * Export debug information
00347  */
00348 int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
00349 {
00350     if( ctx == NULL || ctx->pk_info == NULL )
00351         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
00352 
00353     if( ctx->pk_info->debug_func == NULL )
00354         return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
00355 
00356     ctx->pk_info->debug_func( ctx->pk_ctx, items );
00357     return( 0 );
00358 }
00359 
00360 /*
00361  * Access the PK type name
00362  */
00363 const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
00364 {
00365     if( ctx == NULL || ctx->pk_info == NULL )
00366         return( "invalid PK" );
00367 
00368     return( ctx->pk_info->name );
00369 }
00370 
00371 /*
00372  * Access the PK type
00373  */
00374 mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
00375 {
00376     if( ctx == NULL || ctx->pk_info == NULL )
00377         return( MBEDTLS_PK_NONE );
00378 
00379     return( ctx->pk_info->type );
00380 }
00381 
00382 #endif /* MBEDTLS_PK_C */