mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /**
markrad 0:cdf462088d13 2 * \file pkcs5.c
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * \brief PKCS#5 functions
markrad 0:cdf462088d13 5 *
markrad 0:cdf462088d13 6 * \author Mathias Olsson <mathias@kompetensum.com>
markrad 0:cdf462088d13 7 *
markrad 0:cdf462088d13 8 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
markrad 0:cdf462088d13 9 * SPDX-License-Identifier: Apache-2.0
markrad 0:cdf462088d13 10 *
markrad 0:cdf462088d13 11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
markrad 0:cdf462088d13 12 * not use this file except in compliance with the License.
markrad 0:cdf462088d13 13 * You may obtain a copy of the License at
markrad 0:cdf462088d13 14 *
markrad 0:cdf462088d13 15 * http://www.apache.org/licenses/LICENSE-2.0
markrad 0:cdf462088d13 16 *
markrad 0:cdf462088d13 17 * Unless required by applicable law or agreed to in writing, software
markrad 0:cdf462088d13 18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
markrad 0:cdf462088d13 19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
markrad 0:cdf462088d13 20 * See the License for the specific language governing permissions and
markrad 0:cdf462088d13 21 * limitations under the License.
markrad 0:cdf462088d13 22 *
markrad 0:cdf462088d13 23 * This file is part of mbed TLS (https://tls.mbed.org)
markrad 0:cdf462088d13 24 */
markrad 0:cdf462088d13 25 /*
markrad 0:cdf462088d13 26 * PKCS#5 includes PBKDF2 and more
markrad 0:cdf462088d13 27 *
markrad 0:cdf462088d13 28 * http://tools.ietf.org/html/rfc2898 (Specification)
markrad 0:cdf462088d13 29 * http://tools.ietf.org/html/rfc6070 (Test vectors)
markrad 0:cdf462088d13 30 */
markrad 0:cdf462088d13 31
markrad 0:cdf462088d13 32 #if !defined(MBEDTLS_CONFIG_FILE)
markrad 0:cdf462088d13 33 #include "mbedtls/config.h"
markrad 0:cdf462088d13 34 #else
markrad 0:cdf462088d13 35 #include MBEDTLS_CONFIG_FILE
markrad 0:cdf462088d13 36 #endif
markrad 0:cdf462088d13 37
markrad 0:cdf462088d13 38 #if defined(MBEDTLS_PKCS5_C)
markrad 0:cdf462088d13 39
markrad 0:cdf462088d13 40 #include "mbedtls/pkcs5.h"
markrad 0:cdf462088d13 41 #include "mbedtls/asn1.h"
markrad 0:cdf462088d13 42 #include "mbedtls/cipher.h"
markrad 0:cdf462088d13 43 #include "mbedtls/oid.h"
markrad 0:cdf462088d13 44
markrad 0:cdf462088d13 45 #include <string.h>
markrad 0:cdf462088d13 46
markrad 0:cdf462088d13 47 #if defined(MBEDTLS_PLATFORM_C)
markrad 0:cdf462088d13 48 #include "mbedtls/platform.h"
markrad 0:cdf462088d13 49 #else
markrad 0:cdf462088d13 50 #include <stdio.h>
markrad 0:cdf462088d13 51 #define mbedtls_printf printf
markrad 0:cdf462088d13 52 #endif
markrad 0:cdf462088d13 53
markrad 0:cdf462088d13 54 static int pkcs5_parse_pbkdf2_params( const mbedtls_asn1_buf *params,
markrad 0:cdf462088d13 55 mbedtls_asn1_buf *salt, int *iterations,
markrad 0:cdf462088d13 56 int *keylen, mbedtls_md_type_t *md_type )
markrad 0:cdf462088d13 57 {
markrad 0:cdf462088d13 58 int ret;
markrad 0:cdf462088d13 59 mbedtls_asn1_buf prf_alg_oid;
markrad 0:cdf462088d13 60 unsigned char *p = params->p;
markrad 0:cdf462088d13 61 const unsigned char *end = params->p + params->len;
markrad 0:cdf462088d13 62
markrad 0:cdf462088d13 63 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
markrad 0:cdf462088d13 64 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
markrad 0:cdf462088d13 65 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
markrad 0:cdf462088d13 66 /*
markrad 0:cdf462088d13 67 * PBKDF2-params ::= SEQUENCE {
markrad 0:cdf462088d13 68 * salt OCTET STRING,
markrad 0:cdf462088d13 69 * iterationCount INTEGER,
markrad 0:cdf462088d13 70 * keyLength INTEGER OPTIONAL
markrad 0:cdf462088d13 71 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
markrad 0:cdf462088d13 72 * }
markrad 0:cdf462088d13 73 *
markrad 0:cdf462088d13 74 */
markrad 0:cdf462088d13 75 if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
markrad 0:cdf462088d13 76 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
markrad 0:cdf462088d13 77
markrad 0:cdf462088d13 78 salt->p = p;
markrad 0:cdf462088d13 79 p += salt->len;
markrad 0:cdf462088d13 80
markrad 0:cdf462088d13 81 if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 )
markrad 0:cdf462088d13 82 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
markrad 0:cdf462088d13 83
markrad 0:cdf462088d13 84 if( p == end )
markrad 0:cdf462088d13 85 return( 0 );
markrad 0:cdf462088d13 86
markrad 0:cdf462088d13 87 if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 )
markrad 0:cdf462088d13 88 {
markrad 0:cdf462088d13 89 if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
markrad 0:cdf462088d13 90 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
markrad 0:cdf462088d13 91 }
markrad 0:cdf462088d13 92
markrad 0:cdf462088d13 93 if( p == end )
markrad 0:cdf462088d13 94 return( 0 );
markrad 0:cdf462088d13 95
markrad 0:cdf462088d13 96 if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
markrad 0:cdf462088d13 97 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
markrad 0:cdf462088d13 98
markrad 0:cdf462088d13 99 if( MBEDTLS_OID_CMP( MBEDTLS_OID_HMAC_SHA1, &prf_alg_oid ) != 0 )
markrad 0:cdf462088d13 100 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
markrad 0:cdf462088d13 101
markrad 0:cdf462088d13 102 *md_type = MBEDTLS_MD_SHA1;
markrad 0:cdf462088d13 103
markrad 0:cdf462088d13 104 if( p != end )
markrad 0:cdf462088d13 105 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
markrad 0:cdf462088d13 106 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
markrad 0:cdf462088d13 107
markrad 0:cdf462088d13 108 return( 0 );
markrad 0:cdf462088d13 109 }
markrad 0:cdf462088d13 110
markrad 0:cdf462088d13 111 int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
markrad 0:cdf462088d13 112 const unsigned char *pwd, size_t pwdlen,
markrad 0:cdf462088d13 113 const unsigned char *data, size_t datalen,
markrad 0:cdf462088d13 114 unsigned char *output )
markrad 0:cdf462088d13 115 {
markrad 0:cdf462088d13 116 int ret, iterations = 0, keylen = 0;
markrad 0:cdf462088d13 117 unsigned char *p, *end;
markrad 0:cdf462088d13 118 mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
markrad 0:cdf462088d13 119 mbedtls_asn1_buf salt;
markrad 0:cdf462088d13 120 mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
markrad 0:cdf462088d13 121 unsigned char key[32], iv[32];
markrad 0:cdf462088d13 122 size_t olen = 0;
markrad 0:cdf462088d13 123 const mbedtls_md_info_t *md_info;
markrad 0:cdf462088d13 124 const mbedtls_cipher_info_t *cipher_info;
markrad 0:cdf462088d13 125 mbedtls_md_context_t md_ctx;
markrad 0:cdf462088d13 126 mbedtls_cipher_type_t cipher_alg;
markrad 0:cdf462088d13 127 mbedtls_cipher_context_t cipher_ctx;
markrad 0:cdf462088d13 128
markrad 0:cdf462088d13 129 p = pbe_params->p;
markrad 0:cdf462088d13 130 end = p + pbe_params->len;
markrad 0:cdf462088d13 131
markrad 0:cdf462088d13 132 /*
markrad 0:cdf462088d13 133 * PBES2-params ::= SEQUENCE {
markrad 0:cdf462088d13 134 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
markrad 0:cdf462088d13 135 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
markrad 0:cdf462088d13 136 * }
markrad 0:cdf462088d13 137 */
markrad 0:cdf462088d13 138 if( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
markrad 0:cdf462088d13 139 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
markrad 0:cdf462088d13 140 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
markrad 0:cdf462088d13 141
markrad 0:cdf462088d13 142 if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid, &kdf_alg_params ) ) != 0 )
markrad 0:cdf462088d13 143 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
markrad 0:cdf462088d13 144
markrad 0:cdf462088d13 145 // Only PBKDF2 supported at the moment
markrad 0:cdf462088d13 146 //
markrad 0:cdf462088d13 147 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
markrad 0:cdf462088d13 148 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
markrad 0:cdf462088d13 149
markrad 0:cdf462088d13 150 if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
markrad 0:cdf462088d13 151 &salt, &iterations, &keylen,
markrad 0:cdf462088d13 152 &md_type ) ) != 0 )
markrad 0:cdf462088d13 153 {
markrad 0:cdf462088d13 154 return( ret );
markrad 0:cdf462088d13 155 }
markrad 0:cdf462088d13 156
markrad 0:cdf462088d13 157 md_info = mbedtls_md_info_from_type( md_type );
markrad 0:cdf462088d13 158 if( md_info == NULL )
markrad 0:cdf462088d13 159 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
markrad 0:cdf462088d13 160
markrad 0:cdf462088d13 161 if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
markrad 0:cdf462088d13 162 &enc_scheme_params ) ) != 0 )
markrad 0:cdf462088d13 163 {
markrad 0:cdf462088d13 164 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
markrad 0:cdf462088d13 165 }
markrad 0:cdf462088d13 166
markrad 0:cdf462088d13 167 if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
markrad 0:cdf462088d13 168 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
markrad 0:cdf462088d13 169
markrad 0:cdf462088d13 170 cipher_info = mbedtls_cipher_info_from_type( cipher_alg );
markrad 0:cdf462088d13 171 if( cipher_info == NULL )
markrad 0:cdf462088d13 172 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
markrad 0:cdf462088d13 173
markrad 0:cdf462088d13 174 /*
markrad 0:cdf462088d13 175 * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
markrad 0:cdf462088d13 176 * since it is optional and we don't know if it was set or not
markrad 0:cdf462088d13 177 */
markrad 0:cdf462088d13 178 keylen = cipher_info->key_bitlen / 8;
markrad 0:cdf462088d13 179
markrad 0:cdf462088d13 180 if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
markrad 0:cdf462088d13 181 enc_scheme_params.len != cipher_info->iv_size )
markrad 0:cdf462088d13 182 {
markrad 0:cdf462088d13 183 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
markrad 0:cdf462088d13 184 }
markrad 0:cdf462088d13 185
markrad 0:cdf462088d13 186 mbedtls_md_init( &md_ctx );
markrad 0:cdf462088d13 187 mbedtls_cipher_init( &cipher_ctx );
markrad 0:cdf462088d13 188
markrad 0:cdf462088d13 189 memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
markrad 0:cdf462088d13 190
markrad 0:cdf462088d13 191 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
markrad 0:cdf462088d13 192 goto exit;
markrad 0:cdf462088d13 193
markrad 0:cdf462088d13 194 if( ( ret = mbedtls_pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
markrad 0:cdf462088d13 195 iterations, keylen, key ) ) != 0 )
markrad 0:cdf462088d13 196 {
markrad 0:cdf462088d13 197 goto exit;
markrad 0:cdf462088d13 198 }
markrad 0:cdf462088d13 199
markrad 0:cdf462088d13 200 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
markrad 0:cdf462088d13 201 goto exit;
markrad 0:cdf462088d13 202
markrad 0:cdf462088d13 203 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
markrad 0:cdf462088d13 204 goto exit;
markrad 0:cdf462088d13 205
markrad 0:cdf462088d13 206 if( ( ret = mbedtls_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
markrad 0:cdf462088d13 207 data, datalen, output, &olen ) ) != 0 )
markrad 0:cdf462088d13 208 ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
markrad 0:cdf462088d13 209
markrad 0:cdf462088d13 210 exit:
markrad 0:cdf462088d13 211 mbedtls_md_free( &md_ctx );
markrad 0:cdf462088d13 212 mbedtls_cipher_free( &cipher_ctx );
markrad 0:cdf462088d13 213
markrad 0:cdf462088d13 214 return( ret );
markrad 0:cdf462088d13 215 }
markrad 0:cdf462088d13 216
markrad 0:cdf462088d13 217 int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *password,
markrad 0:cdf462088d13 218 size_t plen, const unsigned char *salt, size_t slen,
markrad 0:cdf462088d13 219 unsigned int iteration_count,
markrad 0:cdf462088d13 220 uint32_t key_length, unsigned char *output )
markrad 0:cdf462088d13 221 {
markrad 0:cdf462088d13 222 int ret, j;
markrad 0:cdf462088d13 223 unsigned int i;
markrad 0:cdf462088d13 224 unsigned char md1[MBEDTLS_MD_MAX_SIZE];
markrad 0:cdf462088d13 225 unsigned char work[MBEDTLS_MD_MAX_SIZE];
markrad 0:cdf462088d13 226 unsigned char md_size = mbedtls_md_get_size( ctx->md_info );
markrad 0:cdf462088d13 227 size_t use_len;
markrad 0:cdf462088d13 228 unsigned char *out_p = output;
markrad 0:cdf462088d13 229 unsigned char counter[4];
markrad 0:cdf462088d13 230
markrad 0:cdf462088d13 231 memset( counter, 0, 4 );
markrad 0:cdf462088d13 232 counter[3] = 1;
markrad 0:cdf462088d13 233
markrad 0:cdf462088d13 234 if( iteration_count > 0xFFFFFFFF )
markrad 0:cdf462088d13 235 return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
markrad 0:cdf462088d13 236
markrad 0:cdf462088d13 237 while( key_length )
markrad 0:cdf462088d13 238 {
markrad 0:cdf462088d13 239 // U1 ends up in work
markrad 0:cdf462088d13 240 //
markrad 0:cdf462088d13 241 if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
markrad 0:cdf462088d13 242 return( ret );
markrad 0:cdf462088d13 243
markrad 0:cdf462088d13 244 if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
markrad 0:cdf462088d13 245 return( ret );
markrad 0:cdf462088d13 246
markrad 0:cdf462088d13 247 if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 )
markrad 0:cdf462088d13 248 return( ret );
markrad 0:cdf462088d13 249
markrad 0:cdf462088d13 250 if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
markrad 0:cdf462088d13 251 return( ret );
markrad 0:cdf462088d13 252
markrad 0:cdf462088d13 253 memcpy( md1, work, md_size );
markrad 0:cdf462088d13 254
markrad 0:cdf462088d13 255 for( i = 1; i < iteration_count; i++ )
markrad 0:cdf462088d13 256 {
markrad 0:cdf462088d13 257 // U2 ends up in md1
markrad 0:cdf462088d13 258 //
markrad 0:cdf462088d13 259 if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
markrad 0:cdf462088d13 260 return( ret );
markrad 0:cdf462088d13 261
markrad 0:cdf462088d13 262 if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
markrad 0:cdf462088d13 263 return( ret );
markrad 0:cdf462088d13 264
markrad 0:cdf462088d13 265 if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
markrad 0:cdf462088d13 266 return( ret );
markrad 0:cdf462088d13 267
markrad 0:cdf462088d13 268 // U1 xor U2
markrad 0:cdf462088d13 269 //
markrad 0:cdf462088d13 270 for( j = 0; j < md_size; j++ )
markrad 0:cdf462088d13 271 work[j] ^= md1[j];
markrad 0:cdf462088d13 272 }
markrad 0:cdf462088d13 273
markrad 0:cdf462088d13 274 use_len = ( key_length < md_size ) ? key_length : md_size;
markrad 0:cdf462088d13 275 memcpy( out_p, work, use_len );
markrad 0:cdf462088d13 276
markrad 0:cdf462088d13 277 key_length -= (uint32_t) use_len;
markrad 0:cdf462088d13 278 out_p += use_len;
markrad 0:cdf462088d13 279
markrad 0:cdf462088d13 280 for( i = 4; i > 0; i-- )
markrad 0:cdf462088d13 281 if( ++counter[i - 1] != 0 )
markrad 0:cdf462088d13 282 break;
markrad 0:cdf462088d13 283 }
markrad 0:cdf462088d13 284
markrad 0:cdf462088d13 285 return( 0 );
markrad 0:cdf462088d13 286 }
markrad 0:cdf462088d13 287
markrad 0:cdf462088d13 288 #if defined(MBEDTLS_SELF_TEST)
markrad 0:cdf462088d13 289
markrad 0:cdf462088d13 290 #if !defined(MBEDTLS_SHA1_C)
markrad 0:cdf462088d13 291 int mbedtls_pkcs5_self_test( int verbose )
markrad 0:cdf462088d13 292 {
markrad 0:cdf462088d13 293 if( verbose != 0 )
markrad 0:cdf462088d13 294 mbedtls_printf( " PBKDF2 (SHA1): skipped\n\n" );
markrad 0:cdf462088d13 295
markrad 0:cdf462088d13 296 return( 0 );
markrad 0:cdf462088d13 297 }
markrad 0:cdf462088d13 298 #else
markrad 0:cdf462088d13 299
markrad 0:cdf462088d13 300 #define MAX_TESTS 6
markrad 0:cdf462088d13 301
markrad 0:cdf462088d13 302 static const size_t plen[MAX_TESTS] =
markrad 0:cdf462088d13 303 { 8, 8, 8, 24, 9 };
markrad 0:cdf462088d13 304
markrad 0:cdf462088d13 305 static const unsigned char password[MAX_TESTS][32] =
markrad 0:cdf462088d13 306 {
markrad 0:cdf462088d13 307 "password",
markrad 0:cdf462088d13 308 "password",
markrad 0:cdf462088d13 309 "password",
markrad 0:cdf462088d13 310 "passwordPASSWORDpassword",
markrad 0:cdf462088d13 311 "pass\0word",
markrad 0:cdf462088d13 312 };
markrad 0:cdf462088d13 313
markrad 0:cdf462088d13 314 static const size_t slen[MAX_TESTS] =
markrad 0:cdf462088d13 315 { 4, 4, 4, 36, 5 };
markrad 0:cdf462088d13 316
markrad 0:cdf462088d13 317 static const unsigned char salt[MAX_TESTS][40] =
markrad 0:cdf462088d13 318 {
markrad 0:cdf462088d13 319 "salt",
markrad 0:cdf462088d13 320 "salt",
markrad 0:cdf462088d13 321 "salt",
markrad 0:cdf462088d13 322 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
markrad 0:cdf462088d13 323 "sa\0lt",
markrad 0:cdf462088d13 324 };
markrad 0:cdf462088d13 325
markrad 0:cdf462088d13 326 static const uint32_t it_cnt[MAX_TESTS] =
markrad 0:cdf462088d13 327 { 1, 2, 4096, 4096, 4096 };
markrad 0:cdf462088d13 328
markrad 0:cdf462088d13 329 static const uint32_t key_len[MAX_TESTS] =
markrad 0:cdf462088d13 330 { 20, 20, 20, 25, 16 };
markrad 0:cdf462088d13 331
markrad 0:cdf462088d13 332 static const unsigned char result_key[MAX_TESTS][32] =
markrad 0:cdf462088d13 333 {
markrad 0:cdf462088d13 334 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
markrad 0:cdf462088d13 335 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
markrad 0:cdf462088d13 336 0x2f, 0xe0, 0x37, 0xa6 },
markrad 0:cdf462088d13 337 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
markrad 0:cdf462088d13 338 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
markrad 0:cdf462088d13 339 0xd8, 0xde, 0x89, 0x57 },
markrad 0:cdf462088d13 340 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
markrad 0:cdf462088d13 341 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
markrad 0:cdf462088d13 342 0x65, 0xa4, 0x29, 0xc1 },
markrad 0:cdf462088d13 343 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
markrad 0:cdf462088d13 344 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
markrad 0:cdf462088d13 345 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
markrad 0:cdf462088d13 346 0x38 },
markrad 0:cdf462088d13 347 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
markrad 0:cdf462088d13 348 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
markrad 0:cdf462088d13 349 };
markrad 0:cdf462088d13 350
markrad 0:cdf462088d13 351 int mbedtls_pkcs5_self_test( int verbose )
markrad 0:cdf462088d13 352 {
markrad 0:cdf462088d13 353 mbedtls_md_context_t sha1_ctx;
markrad 0:cdf462088d13 354 const mbedtls_md_info_t *info_sha1;
markrad 0:cdf462088d13 355 int ret, i;
markrad 0:cdf462088d13 356 unsigned char key[64];
markrad 0:cdf462088d13 357
markrad 0:cdf462088d13 358 mbedtls_md_init( &sha1_ctx );
markrad 0:cdf462088d13 359
markrad 0:cdf462088d13 360 info_sha1 = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
markrad 0:cdf462088d13 361 if( info_sha1 == NULL )
markrad 0:cdf462088d13 362 {
markrad 0:cdf462088d13 363 ret = 1;
markrad 0:cdf462088d13 364 goto exit;
markrad 0:cdf462088d13 365 }
markrad 0:cdf462088d13 366
markrad 0:cdf462088d13 367 if( ( ret = mbedtls_md_setup( &sha1_ctx, info_sha1, 1 ) ) != 0 )
markrad 0:cdf462088d13 368 {
markrad 0:cdf462088d13 369 ret = 1;
markrad 0:cdf462088d13 370 goto exit;
markrad 0:cdf462088d13 371 }
markrad 0:cdf462088d13 372
markrad 0:cdf462088d13 373 for( i = 0; i < MAX_TESTS; i++ )
markrad 0:cdf462088d13 374 {
markrad 0:cdf462088d13 375 if( verbose != 0 )
markrad 0:cdf462088d13 376 mbedtls_printf( " PBKDF2 (SHA1) #%d: ", i );
markrad 0:cdf462088d13 377
markrad 0:cdf462088d13 378 ret = mbedtls_pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i],
markrad 0:cdf462088d13 379 slen[i], it_cnt[i], key_len[i], key );
markrad 0:cdf462088d13 380 if( ret != 0 ||
markrad 0:cdf462088d13 381 memcmp( result_key[i], key, key_len[i] ) != 0 )
markrad 0:cdf462088d13 382 {
markrad 0:cdf462088d13 383 if( verbose != 0 )
markrad 0:cdf462088d13 384 mbedtls_printf( "failed\n" );
markrad 0:cdf462088d13 385
markrad 0:cdf462088d13 386 ret = 1;
markrad 0:cdf462088d13 387 goto exit;
markrad 0:cdf462088d13 388 }
markrad 0:cdf462088d13 389
markrad 0:cdf462088d13 390 if( verbose != 0 )
markrad 0:cdf462088d13 391 mbedtls_printf( "passed\n" );
markrad 0:cdf462088d13 392 }
markrad 0:cdf462088d13 393
markrad 0:cdf462088d13 394 if( verbose != 0 )
markrad 0:cdf462088d13 395 mbedtls_printf( "\n" );
markrad 0:cdf462088d13 396
markrad 0:cdf462088d13 397 exit:
markrad 0:cdf462088d13 398 mbedtls_md_free( &sha1_ctx );
markrad 0:cdf462088d13 399
markrad 0:cdf462088d13 400 return( ret );
markrad 0:cdf462088d13 401 }
markrad 0:cdf462088d13 402 #endif /* MBEDTLS_SHA1_C */
markrad 0:cdf462088d13 403
markrad 0:cdf462088d13 404 #endif /* MBEDTLS_SELF_TEST */
markrad 0:cdf462088d13 405
markrad 0:cdf462088d13 406 #endif /* MBEDTLS_PKCS5_C */