Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pkcs5.c Source File

pkcs5.c

Go to the documentation of this file.
00001 /**
00002  * \file pkcs5.c
00003  *
00004  * \brief PKCS#5 functions
00005  *
00006  * \author Mathias Olsson <mathias@kompetensum.com>
00007  *
00008  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00009  *  SPDX-License-Identifier: Apache-2.0
00010  *
00011  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00012  *  not use this file except in compliance with the License.
00013  *  You may obtain a copy of the License at
00014  *
00015  *  http://www.apache.org/licenses/LICENSE-2.0
00016  *
00017  *  Unless required by applicable law or agreed to in writing, software
00018  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00019  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020  *  See the License for the specific language governing permissions and
00021  *  limitations under the License.
00022  *
00023  *  This file is part of mbed TLS (https://tls.mbed.org)
00024  */
00025 /*
00026  * PKCS#5 includes PBKDF2 and more
00027  *
00028  * http://tools.ietf.org/html/rfc2898 (Specification)
00029  * http://tools.ietf.org/html/rfc6070 (Test vectors)
00030  */
00031 
00032 #if !defined(MBEDTLS_CONFIG_FILE)
00033 #include "mbedtls/config.h"
00034 #else
00035 #include MBEDTLS_CONFIG_FILE
00036 #endif
00037 
00038 #if defined(MBEDTLS_PKCS5_C)
00039 
00040 #include "mbedtls/pkcs5.h"
00041 
00042 #if defined(MBEDTLS_ASN1_PARSE_C)
00043 #include "mbedtls/asn1.h"
00044 #include "mbedtls/cipher.h"
00045 #include "mbedtls/oid.h"
00046 #endif /* MBEDTLS_ASN1_PARSE_C */
00047 
00048 #include <string.h>
00049 
00050 #if defined(MBEDTLS_PLATFORM_C)
00051 #include "mbedtls/platform.h"
00052 #else
00053 #include <stdio.h>
00054 #define mbedtls_printf printf
00055 #endif
00056 
00057 #if defined(MBEDTLS_ASN1_PARSE_C)
00058 static int pkcs5_parse_pbkdf2_params( const mbedtls_asn1_buf *params,
00059                                       mbedtls_asn1_buf *salt, int *iterations,
00060                                       int *keylen, mbedtls_md_type_t *md_type )
00061 {
00062     int ret;
00063     mbedtls_asn1_buf prf_alg_oid;
00064     unsigned char *p = params->p;
00065     const unsigned char *end = params->p + params->len;
00066 
00067     if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
00068         return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
00069                 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
00070     /*
00071      *  PBKDF2-params ::= SEQUENCE {
00072      *    salt              OCTET STRING,
00073      *    iterationCount    INTEGER,
00074      *    keyLength         INTEGER OPTIONAL
00075      *    prf               AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
00076      *  }
00077      *
00078      */
00079     if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len,
00080                                       MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
00081         return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
00082 
00083     salt->p = p;
00084     p += salt->len;
00085 
00086     if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 )
00087         return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
00088 
00089     if( p == end )
00090         return( 0 );
00091 
00092     if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 )
00093     {
00094         if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
00095             return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
00096     }
00097 
00098     if( p == end )
00099         return( 0 );
00100 
00101     if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
00102         return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
00103 
00104     if( mbedtls_oid_get_md_hmac( &prf_alg_oid, md_type ) != 0 )
00105         return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
00106 
00107     if( p != end )
00108         return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
00109                 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
00110 
00111     return( 0 );
00112 }
00113 
00114 int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
00115                  const unsigned char *pwd,  size_t pwdlen,
00116                  const unsigned char *data, size_t datalen,
00117                  unsigned char *output )
00118 {
00119     int ret, iterations = 0, keylen = 0;
00120     unsigned char *p, *end;
00121     mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
00122     mbedtls_asn1_buf salt;
00123     mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
00124     unsigned char key[32], iv[32];
00125     size_t olen = 0;
00126     const mbedtls_md_info_t *md_info;
00127     const mbedtls_cipher_info_t *cipher_info;
00128     mbedtls_md_context_t md_ctx;
00129     mbedtls_cipher_type_t cipher_alg;
00130     mbedtls_cipher_context_t cipher_ctx;
00131 
00132     p = pbe_params->p;
00133     end = p + pbe_params->len;
00134 
00135     /*
00136      *  PBES2-params ::= SEQUENCE {
00137      *    keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
00138      *    encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
00139      *  }
00140      */
00141     if( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
00142         return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
00143                 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
00144 
00145     if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid,
00146                                       &kdf_alg_params ) ) != 0 )
00147         return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
00148 
00149     // Only PBKDF2 supported at the moment
00150     //
00151     if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
00152         return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
00153 
00154     if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
00155                                            &salt, &iterations, &keylen,
00156                                            &md_type ) ) != 0 )
00157     {
00158         return( ret );
00159     }
00160 
00161     md_info = mbedtls_md_info_from_type( md_type );
00162     if( md_info == NULL )
00163         return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
00164 
00165     if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
00166                               &enc_scheme_params ) ) != 0 )
00167     {
00168         return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
00169     }
00170 
00171     if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
00172         return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
00173 
00174     cipher_info = mbedtls_cipher_info_from_type( cipher_alg );
00175     if( cipher_info == NULL )
00176         return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
00177 
00178     /*
00179      * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
00180      * since it is optional and we don't know if it was set or not
00181      */
00182     keylen = cipher_info->key_bitlen / 8;
00183 
00184     if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
00185         enc_scheme_params.len != cipher_info->iv_size )
00186     {
00187         return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
00188     }
00189 
00190     mbedtls_md_init( &md_ctx );
00191     mbedtls_cipher_init( &cipher_ctx );
00192 
00193     memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
00194 
00195     if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
00196         goto exit;
00197 
00198     if( ( ret = mbedtls_pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
00199                                    iterations, keylen, key ) ) != 0 )
00200     {
00201         goto exit;
00202     }
00203 
00204     if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
00205         goto exit;
00206 
00207     if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen,
00208                                        (mbedtls_operation_t) mode ) ) != 0 )
00209         goto exit;
00210 
00211     if( ( ret = mbedtls_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
00212                               data, datalen, output, &olen ) ) != 0 )
00213         ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
00214 
00215 exit:
00216     mbedtls_md_free( &md_ctx );
00217     mbedtls_cipher_free( &cipher_ctx );
00218 
00219     return( ret );
00220 }
00221 #endif /* MBEDTLS_ASN1_PARSE_C */
00222 
00223 int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
00224                        const unsigned char *password,
00225                        size_t plen, const unsigned char *salt, size_t slen,
00226                        unsigned int iteration_count,
00227                        uint32_t key_length, unsigned char *output )
00228 {
00229     int ret, j;
00230     unsigned int i;
00231     unsigned char md1[MBEDTLS_MD_MAX_SIZE];
00232     unsigned char work[MBEDTLS_MD_MAX_SIZE];
00233     unsigned char md_size = mbedtls_md_get_size( ctx->md_info );
00234     size_t use_len;
00235     unsigned char *out_p = output;
00236     unsigned char counter[4];
00237 
00238     memset( counter, 0, 4 );
00239     counter[3] = 1;
00240 
00241 #if UINT_MAX > 0xFFFFFFFF
00242     if( iteration_count > 0xFFFFFFFF )
00243         return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
00244 #endif
00245 
00246     if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
00247         return( ret );
00248     while( key_length )
00249     {
00250         // U1 ends up in work
00251         //
00252         if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
00253             return( ret );
00254 
00255         if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 )
00256             return( ret );
00257 
00258         if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
00259             return( ret );
00260 
00261         if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
00262            return( ret );
00263 
00264         memcpy( md1, work, md_size );
00265 
00266         for( i = 1; i < iteration_count; i++ )
00267         {
00268             // U2 ends up in md1
00269             //
00270             if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
00271                 return( ret );
00272 
00273             if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
00274                 return( ret );
00275 
00276             if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
00277                 return( ret );
00278 
00279             // U1 xor U2
00280             //
00281             for( j = 0; j < md_size; j++ )
00282                 work[j] ^= md1[j];
00283         }
00284 
00285         use_len = ( key_length < md_size ) ? key_length : md_size;
00286         memcpy( out_p, work, use_len );
00287 
00288         key_length -= (uint32_t) use_len;
00289         out_p += use_len;
00290 
00291         for( i = 4; i > 0; i-- )
00292             if( ++counter[i - 1] != 0 )
00293                 break;
00294     }
00295 
00296     return( 0 );
00297 }
00298 
00299 #if defined(MBEDTLS_SELF_TEST)
00300 
00301 #if !defined(MBEDTLS_SHA1_C)
00302 int mbedtls_pkcs5_self_test( int verbose )
00303 {
00304     if( verbose != 0 )
00305         mbedtls_printf( "  PBKDF2 (SHA1): skipped\n\n" );
00306 
00307     return( 0 );
00308 }
00309 #else
00310 
00311 #define MAX_TESTS   6
00312 
00313 static const size_t plen_test_data[MAX_TESTS] =
00314     { 8, 8, 8, 24, 9 };
00315 
00316 static const unsigned char password_test_data[MAX_TESTS][32] =
00317 {
00318     "password",
00319     "password",
00320     "password",
00321     "passwordPASSWORDpassword",
00322     "pass\0word",
00323 };
00324 
00325 static const size_t slen_test_data[MAX_TESTS] =
00326     { 4, 4, 4, 36, 5 };
00327 
00328 static const unsigned char salt_test_data[MAX_TESTS][40] =
00329 {
00330     "salt",
00331     "salt",
00332     "salt",
00333     "saltSALTsaltSALTsaltSALTsaltSALTsalt",
00334     "sa\0lt",
00335 };
00336 
00337 static const uint32_t it_cnt_test_data[MAX_TESTS] =
00338     { 1, 2, 4096, 4096, 4096 };
00339 
00340 static const uint32_t key_len_test_data[MAX_TESTS] =
00341     { 20, 20, 20, 25, 16 };
00342 
00343 static const unsigned char result_key_test_data[MAX_TESTS][32] =
00344 {
00345     { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
00346       0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
00347       0x2f, 0xe0, 0x37, 0xa6 },
00348     { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
00349       0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
00350       0xd8, 0xde, 0x89, 0x57 },
00351     { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
00352       0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
00353       0x65, 0xa4, 0x29, 0xc1 },
00354     { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
00355       0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
00356       0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
00357       0x38 },
00358     { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
00359       0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
00360 };
00361 
00362 int mbedtls_pkcs5_self_test( int verbose )
00363 {
00364     mbedtls_md_context_t sha1_ctx;
00365     const mbedtls_md_info_t *info_sha1;
00366     int ret, i;
00367     unsigned char key[64];
00368 
00369     mbedtls_md_init( &sha1_ctx );
00370 
00371     info_sha1 = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
00372     if( info_sha1 == NULL )
00373     {
00374         ret = 1;
00375         goto exit;
00376     }
00377 
00378     if( ( ret = mbedtls_md_setup( &sha1_ctx, info_sha1, 1 ) ) != 0 )
00379     {
00380         ret = 1;
00381         goto exit;
00382     }
00383 
00384     for( i = 0; i < MAX_TESTS; i++ )
00385     {
00386         if( verbose != 0 )
00387             mbedtls_printf( "  PBKDF2 (SHA1) #%d: ", i );
00388 
00389         ret = mbedtls_pkcs5_pbkdf2_hmac( &sha1_ctx, password_test_data[i],
00390                                          plen_test_data[i], salt_test_data[i],
00391                                          slen_test_data[i], it_cnt_test_data[i],
00392                                          key_len_test_data[i], key );
00393         if( ret != 0 ||
00394             memcmp( result_key_test_data[i], key, key_len_test_data[i] ) != 0 )
00395         {
00396             if( verbose != 0 )
00397                 mbedtls_printf( "failed\n" );
00398 
00399             ret = 1;
00400             goto exit;
00401         }
00402 
00403         if( verbose != 0 )
00404             mbedtls_printf( "passed\n" );
00405     }
00406 
00407     if( verbose != 0 )
00408         mbedtls_printf( "\n" );
00409 
00410 exit:
00411     mbedtls_md_free( &sha1_ctx );
00412 
00413     return( ret );
00414 }
00415 #endif /* MBEDTLS_SHA1_C */
00416 
00417 #endif /* MBEDTLS_SELF_TEST */
00418 
00419 #endif /* MBEDTLS_PKCS5_C */