mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /**
ansond 0:137634ff4186 2 * \file pkcs5.c
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * \brief PKCS#5 functions
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * \author Mathias Olsson <mathias@kompetensum.com>
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 9 *
ansond 0:137634ff4186 10 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 11 *
ansond 0:137634ff4186 12 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 13 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 14 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 15 * (at your option) any later version.
ansond 0:137634ff4186 16 *
ansond 0:137634ff4186 17 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 20 * GNU General Public License for more details.
ansond 0:137634ff4186 21 *
ansond 0:137634ff4186 22 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 23 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 25 */
ansond 0:137634ff4186 26 /*
ansond 0:137634ff4186 27 * PKCS#5 includes PBKDF2 and more
ansond 0:137634ff4186 28 *
ansond 0:137634ff4186 29 * http://tools.ietf.org/html/rfc2898 (Specification)
ansond 0:137634ff4186 30 * http://tools.ietf.org/html/rfc6070 (Test vectors)
ansond 0:137634ff4186 31 */
ansond 0:137634ff4186 32
ansond 0:137634ff4186 33 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 34 #include "polarssl/config.h"
ansond 0:137634ff4186 35 #else
ansond 0:137634ff4186 36 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 37 #endif
ansond 0:137634ff4186 38
ansond 0:137634ff4186 39 #if defined(POLARSSL_PKCS5_C)
ansond 0:137634ff4186 40
ansond 0:137634ff4186 41 #include "polarssl/pkcs5.h"
ansond 0:137634ff4186 42 #include "polarssl/asn1.h"
ansond 0:137634ff4186 43 #include "polarssl/cipher.h"
ansond 0:137634ff4186 44 #include "polarssl/oid.h"
ansond 0:137634ff4186 45
ansond 0:137634ff4186 46 #include <string.h>
ansond 0:137634ff4186 47
ansond 0:137634ff4186 48 #if defined(POLARSSL_PLATFORM_C)
ansond 0:137634ff4186 49 #include "polarssl/platform.h"
ansond 0:137634ff4186 50 #else
ansond 0:137634ff4186 51 #include <stdio.h>
ansond 0:137634ff4186 52 #define polarssl_printf printf
ansond 0:137634ff4186 53 #endif
ansond 0:137634ff4186 54
ansond 0:137634ff4186 55 static int pkcs5_parse_pbkdf2_params( const asn1_buf *params,
ansond 0:137634ff4186 56 asn1_buf *salt, int *iterations,
ansond 0:137634ff4186 57 int *keylen, md_type_t *md_type )
ansond 0:137634ff4186 58 {
ansond 0:137634ff4186 59 int ret;
ansond 0:137634ff4186 60 asn1_buf prf_alg_oid;
ansond 0:137634ff4186 61 unsigned char *p = params->p;
ansond 0:137634ff4186 62 const unsigned char *end = params->p + params->len;
ansond 0:137634ff4186 63
ansond 0:137634ff4186 64 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
ansond 0:137634ff4186 65 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
ansond 0:137634ff4186 66 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
ansond 0:137634ff4186 67 /*
ansond 0:137634ff4186 68 * PBKDF2-params ::= SEQUENCE {
ansond 0:137634ff4186 69 * salt OCTET STRING,
ansond 0:137634ff4186 70 * iterationCount INTEGER,
ansond 0:137634ff4186 71 * keyLength INTEGER OPTIONAL
ansond 0:137634ff4186 72 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
ansond 0:137634ff4186 73 * }
ansond 0:137634ff4186 74 *
ansond 0:137634ff4186 75 */
ansond 0:137634ff4186 76 if( ( ret = asn1_get_tag( &p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
ansond 0:137634ff4186 77 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
ansond 0:137634ff4186 78
ansond 0:137634ff4186 79 salt->p = p;
ansond 0:137634ff4186 80 p += salt->len;
ansond 0:137634ff4186 81
ansond 0:137634ff4186 82 if( ( ret = asn1_get_int( &p, end, iterations ) ) != 0 )
ansond 0:137634ff4186 83 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
ansond 0:137634ff4186 84
ansond 0:137634ff4186 85 if( p == end )
ansond 0:137634ff4186 86 return( 0 );
ansond 0:137634ff4186 87
ansond 0:137634ff4186 88 if( ( ret = asn1_get_int( &p, end, keylen ) ) != 0 )
ansond 0:137634ff4186 89 {
ansond 0:137634ff4186 90 if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
ansond 0:137634ff4186 91 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
ansond 0:137634ff4186 92 }
ansond 0:137634ff4186 93
ansond 0:137634ff4186 94 if( p == end )
ansond 0:137634ff4186 95 return( 0 );
ansond 0:137634ff4186 96
ansond 0:137634ff4186 97 if( ( ret = asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
ansond 0:137634ff4186 98 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
ansond 0:137634ff4186 99
ansond 0:137634ff4186 100 if( !OID_CMP( OID_HMAC_SHA1, &prf_alg_oid ) )
ansond 0:137634ff4186 101 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
ansond 0:137634ff4186 102
ansond 0:137634ff4186 103 *md_type = POLARSSL_MD_SHA1;
ansond 0:137634ff4186 104
ansond 0:137634ff4186 105 if( p != end )
ansond 0:137634ff4186 106 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
ansond 0:137634ff4186 107 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
ansond 0:137634ff4186 108
ansond 0:137634ff4186 109 return( 0 );
ansond 0:137634ff4186 110 }
ansond 0:137634ff4186 111
ansond 0:137634ff4186 112 int pkcs5_pbes2( asn1_buf *pbe_params, int mode,
ansond 0:137634ff4186 113 const unsigned char *pwd, size_t pwdlen,
ansond 0:137634ff4186 114 const unsigned char *data, size_t datalen,
ansond 0:137634ff4186 115 unsigned char *output )
ansond 0:137634ff4186 116 {
ansond 0:137634ff4186 117 int ret, iterations = 0, keylen = 0;
ansond 0:137634ff4186 118 unsigned char *p, *end;
ansond 0:137634ff4186 119 asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
ansond 0:137634ff4186 120 asn1_buf salt;
ansond 0:137634ff4186 121 md_type_t md_type = POLARSSL_MD_SHA1;
ansond 0:137634ff4186 122 unsigned char key[32], iv[32];
ansond 0:137634ff4186 123 size_t olen = 0;
ansond 0:137634ff4186 124 const md_info_t *md_info;
ansond 0:137634ff4186 125 const cipher_info_t *cipher_info;
ansond 0:137634ff4186 126 md_context_t md_ctx;
ansond 0:137634ff4186 127 cipher_type_t cipher_alg;
ansond 0:137634ff4186 128 cipher_context_t cipher_ctx;
ansond 0:137634ff4186 129
ansond 0:137634ff4186 130 p = pbe_params->p;
ansond 0:137634ff4186 131 end = p + pbe_params->len;
ansond 0:137634ff4186 132
ansond 0:137634ff4186 133 /*
ansond 0:137634ff4186 134 * PBES2-params ::= SEQUENCE {
ansond 0:137634ff4186 135 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
ansond 0:137634ff4186 136 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
ansond 0:137634ff4186 137 * }
ansond 0:137634ff4186 138 */
ansond 0:137634ff4186 139 if( pbe_params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
ansond 0:137634ff4186 140 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
ansond 0:137634ff4186 141 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
ansond 0:137634ff4186 142
ansond 0:137634ff4186 143 if( ( ret = asn1_get_alg( &p, end, &kdf_alg_oid, &kdf_alg_params ) ) != 0 )
ansond 0:137634ff4186 144 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
ansond 0:137634ff4186 145
ansond 0:137634ff4186 146 // Only PBKDF2 supported at the moment
ansond 0:137634ff4186 147 //
ansond 0:137634ff4186 148 if( !OID_CMP( OID_PKCS5_PBKDF2, &kdf_alg_oid ) )
ansond 0:137634ff4186 149 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
ansond 0:137634ff4186 150
ansond 0:137634ff4186 151 if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
ansond 0:137634ff4186 152 &salt, &iterations, &keylen,
ansond 0:137634ff4186 153 &md_type ) ) != 0 )
ansond 0:137634ff4186 154 {
ansond 0:137634ff4186 155 return( ret );
ansond 0:137634ff4186 156 }
ansond 0:137634ff4186 157
ansond 0:137634ff4186 158 md_info = md_info_from_type( md_type );
ansond 0:137634ff4186 159 if( md_info == NULL )
ansond 0:137634ff4186 160 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
ansond 0:137634ff4186 161
ansond 0:137634ff4186 162 if( ( ret = asn1_get_alg( &p, end, &enc_scheme_oid,
ansond 0:137634ff4186 163 &enc_scheme_params ) ) != 0 )
ansond 0:137634ff4186 164 {
ansond 0:137634ff4186 165 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
ansond 0:137634ff4186 166 }
ansond 0:137634ff4186 167
ansond 0:137634ff4186 168 if( oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
ansond 0:137634ff4186 169 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
ansond 0:137634ff4186 170
ansond 0:137634ff4186 171 cipher_info = cipher_info_from_type( cipher_alg );
ansond 0:137634ff4186 172 if( cipher_info == NULL )
ansond 0:137634ff4186 173 return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
ansond 0:137634ff4186 174
ansond 0:137634ff4186 175 /*
ansond 0:137634ff4186 176 * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
ansond 0:137634ff4186 177 * since it is optional and we don't know if it was set or not
ansond 0:137634ff4186 178 */
ansond 0:137634ff4186 179 keylen = cipher_info->key_length / 8;
ansond 0:137634ff4186 180
ansond 0:137634ff4186 181 if( enc_scheme_params.tag != ASN1_OCTET_STRING ||
ansond 0:137634ff4186 182 enc_scheme_params.len != cipher_info->iv_size )
ansond 0:137634ff4186 183 {
ansond 0:137634ff4186 184 return( POLARSSL_ERR_PKCS5_INVALID_FORMAT );
ansond 0:137634ff4186 185 }
ansond 0:137634ff4186 186
ansond 0:137634ff4186 187 md_init( &md_ctx );
ansond 0:137634ff4186 188 cipher_init( &cipher_ctx );
ansond 0:137634ff4186 189
ansond 0:137634ff4186 190 memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
ansond 0:137634ff4186 191
ansond 0:137634ff4186 192 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
ansond 0:137634ff4186 193 goto exit;
ansond 0:137634ff4186 194
ansond 0:137634ff4186 195 if( ( ret = pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
ansond 0:137634ff4186 196 iterations, keylen, key ) ) != 0 )
ansond 0:137634ff4186 197 {
ansond 0:137634ff4186 198 goto exit;
ansond 0:137634ff4186 199 }
ansond 0:137634ff4186 200
ansond 0:137634ff4186 201 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
ansond 0:137634ff4186 202 goto exit;
ansond 0:137634ff4186 203
ansond 0:137634ff4186 204 if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, (operation_t) mode ) ) != 0 )
ansond 0:137634ff4186 205 goto exit;
ansond 0:137634ff4186 206
ansond 0:137634ff4186 207 if( ( ret = cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
ansond 0:137634ff4186 208 data, datalen, output, &olen ) ) != 0 )
ansond 0:137634ff4186 209 ret = POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH;
ansond 0:137634ff4186 210
ansond 0:137634ff4186 211 exit:
ansond 0:137634ff4186 212 md_free( &md_ctx );
ansond 0:137634ff4186 213 cipher_free( &cipher_ctx );
ansond 0:137634ff4186 214
ansond 0:137634ff4186 215 return( ret );
ansond 0:137634ff4186 216 }
ansond 0:137634ff4186 217
ansond 0:137634ff4186 218 int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
ansond 0:137634ff4186 219 size_t plen, const unsigned char *salt, size_t slen,
ansond 0:137634ff4186 220 unsigned int iteration_count,
ansond 0:137634ff4186 221 uint32_t key_length, unsigned char *output )
ansond 0:137634ff4186 222 {
ansond 0:137634ff4186 223 int ret, j;
ansond 0:137634ff4186 224 unsigned int i;
ansond 0:137634ff4186 225 unsigned char md1[POLARSSL_MD_MAX_SIZE];
ansond 0:137634ff4186 226 unsigned char work[POLARSSL_MD_MAX_SIZE];
ansond 0:137634ff4186 227 unsigned char md_size = md_get_size( ctx->md_info );
ansond 0:137634ff4186 228 size_t use_len;
ansond 0:137634ff4186 229 unsigned char *out_p = output;
ansond 0:137634ff4186 230 unsigned char counter[4];
ansond 0:137634ff4186 231
ansond 0:137634ff4186 232 memset( counter, 0, 4 );
ansond 0:137634ff4186 233 counter[3] = 1;
ansond 0:137634ff4186 234
ansond 0:137634ff4186 235 if( iteration_count > 0xFFFFFFFF )
ansond 0:137634ff4186 236 return( POLARSSL_ERR_PKCS5_BAD_INPUT_DATA );
ansond 0:137634ff4186 237
ansond 0:137634ff4186 238 while( key_length )
ansond 0:137634ff4186 239 {
ansond 0:137634ff4186 240 // U1 ends up in work
ansond 0:137634ff4186 241 //
ansond 0:137634ff4186 242 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
ansond 0:137634ff4186 243 return( ret );
ansond 0:137634ff4186 244
ansond 0:137634ff4186 245 if( ( ret = md_hmac_update( ctx, salt, slen ) ) != 0 )
ansond 0:137634ff4186 246 return( ret );
ansond 0:137634ff4186 247
ansond 0:137634ff4186 248 if( ( ret = md_hmac_update( ctx, counter, 4 ) ) != 0 )
ansond 0:137634ff4186 249 return( ret );
ansond 0:137634ff4186 250
ansond 0:137634ff4186 251 if( ( ret = md_hmac_finish( ctx, work ) ) != 0 )
ansond 0:137634ff4186 252 return( ret );
ansond 0:137634ff4186 253
ansond 0:137634ff4186 254 memcpy( md1, work, md_size );
ansond 0:137634ff4186 255
ansond 0:137634ff4186 256 for( i = 1; i < iteration_count; i++ )
ansond 0:137634ff4186 257 {
ansond 0:137634ff4186 258 // U2 ends up in md1
ansond 0:137634ff4186 259 //
ansond 0:137634ff4186 260 if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
ansond 0:137634ff4186 261 return( ret );
ansond 0:137634ff4186 262
ansond 0:137634ff4186 263 if( ( ret = md_hmac_update( ctx, md1, md_size ) ) != 0 )
ansond 0:137634ff4186 264 return( ret );
ansond 0:137634ff4186 265
ansond 0:137634ff4186 266 if( ( ret = md_hmac_finish( ctx, md1 ) ) != 0 )
ansond 0:137634ff4186 267 return( ret );
ansond 0:137634ff4186 268
ansond 0:137634ff4186 269 // U1 xor U2
ansond 0:137634ff4186 270 //
ansond 0:137634ff4186 271 for( j = 0; j < md_size; j++ )
ansond 0:137634ff4186 272 work[j] ^= md1[j];
ansond 0:137634ff4186 273 }
ansond 0:137634ff4186 274
ansond 0:137634ff4186 275 use_len = ( key_length < md_size ) ? key_length : md_size;
ansond 0:137634ff4186 276 memcpy( out_p, work, use_len );
ansond 0:137634ff4186 277
ansond 0:137634ff4186 278 key_length -= (uint32_t) use_len;
ansond 0:137634ff4186 279 out_p += use_len;
ansond 0:137634ff4186 280
ansond 0:137634ff4186 281 for( i = 4; i > 0; i-- )
ansond 0:137634ff4186 282 if( ++counter[i - 1] != 0 )
ansond 0:137634ff4186 283 break;
ansond 0:137634ff4186 284 }
ansond 0:137634ff4186 285
ansond 0:137634ff4186 286 return( 0 );
ansond 0:137634ff4186 287 }
ansond 0:137634ff4186 288
ansond 0:137634ff4186 289 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 290
ansond 0:137634ff4186 291 #if !defined(POLARSSL_SHA1_C)
ansond 0:137634ff4186 292 int pkcs5_self_test( int verbose )
ansond 0:137634ff4186 293 {
ansond 0:137634ff4186 294 if( verbose != 0 )
ansond 0:137634ff4186 295 polarssl_printf( " PBKDF2 (SHA1): skipped\n\n" );
ansond 0:137634ff4186 296
ansond 0:137634ff4186 297 return( 0 );
ansond 0:137634ff4186 298 }
ansond 0:137634ff4186 299 #else
ansond 0:137634ff4186 300
ansond 0:137634ff4186 301 #define MAX_TESTS 6
ansond 0:137634ff4186 302
ansond 0:137634ff4186 303 static const size_t plen[MAX_TESTS] =
ansond 0:137634ff4186 304 { 8, 8, 8, 24, 9 };
ansond 0:137634ff4186 305
ansond 0:137634ff4186 306 static const unsigned char password[MAX_TESTS][32] =
ansond 0:137634ff4186 307 {
ansond 0:137634ff4186 308 "password",
ansond 0:137634ff4186 309 "password",
ansond 0:137634ff4186 310 "password",
ansond 0:137634ff4186 311 "passwordPASSWORDpassword",
ansond 0:137634ff4186 312 "pass\0word",
ansond 0:137634ff4186 313 };
ansond 0:137634ff4186 314
ansond 0:137634ff4186 315 static const size_t slen[MAX_TESTS] =
ansond 0:137634ff4186 316 { 4, 4, 4, 36, 5 };
ansond 0:137634ff4186 317
ansond 0:137634ff4186 318 static const unsigned char salt[MAX_TESTS][40] =
ansond 0:137634ff4186 319 {
ansond 0:137634ff4186 320 "salt",
ansond 0:137634ff4186 321 "salt",
ansond 0:137634ff4186 322 "salt",
ansond 0:137634ff4186 323 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
ansond 0:137634ff4186 324 "sa\0lt",
ansond 0:137634ff4186 325 };
ansond 0:137634ff4186 326
ansond 0:137634ff4186 327 static const uint32_t it_cnt[MAX_TESTS] =
ansond 0:137634ff4186 328 { 1, 2, 4096, 4096, 4096 };
ansond 0:137634ff4186 329
ansond 0:137634ff4186 330 static const uint32_t key_len[MAX_TESTS] =
ansond 0:137634ff4186 331 { 20, 20, 20, 25, 16 };
ansond 0:137634ff4186 332
ansond 0:137634ff4186 333 static const unsigned char result_key[MAX_TESTS][32] =
ansond 0:137634ff4186 334 {
ansond 0:137634ff4186 335 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
ansond 0:137634ff4186 336 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
ansond 0:137634ff4186 337 0x2f, 0xe0, 0x37, 0xa6 },
ansond 0:137634ff4186 338 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
ansond 0:137634ff4186 339 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
ansond 0:137634ff4186 340 0xd8, 0xde, 0x89, 0x57 },
ansond 0:137634ff4186 341 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
ansond 0:137634ff4186 342 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
ansond 0:137634ff4186 343 0x65, 0xa4, 0x29, 0xc1 },
ansond 0:137634ff4186 344 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
ansond 0:137634ff4186 345 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
ansond 0:137634ff4186 346 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
ansond 0:137634ff4186 347 0x38 },
ansond 0:137634ff4186 348 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
ansond 0:137634ff4186 349 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
ansond 0:137634ff4186 350 };
ansond 0:137634ff4186 351
ansond 0:137634ff4186 352 int pkcs5_self_test( int verbose )
ansond 0:137634ff4186 353 {
ansond 0:137634ff4186 354 md_context_t sha1_ctx;
ansond 0:137634ff4186 355 const md_info_t *info_sha1;
ansond 0:137634ff4186 356 int ret, i;
ansond 0:137634ff4186 357 unsigned char key[64];
ansond 0:137634ff4186 358
ansond 0:137634ff4186 359 md_init( &sha1_ctx );
ansond 0:137634ff4186 360
ansond 0:137634ff4186 361 info_sha1 = md_info_from_type( POLARSSL_MD_SHA1 );
ansond 0:137634ff4186 362 if( info_sha1 == NULL )
ansond 0:137634ff4186 363 {
ansond 0:137634ff4186 364 ret = 1;
ansond 0:137634ff4186 365 goto exit;
ansond 0:137634ff4186 366 }
ansond 0:137634ff4186 367
ansond 0:137634ff4186 368 if( ( ret = md_init_ctx( &sha1_ctx, info_sha1 ) ) != 0 )
ansond 0:137634ff4186 369 {
ansond 0:137634ff4186 370 ret = 1;
ansond 0:137634ff4186 371 goto exit;
ansond 0:137634ff4186 372 }
ansond 0:137634ff4186 373
ansond 0:137634ff4186 374 for( i = 0; i < MAX_TESTS; i++ )
ansond 0:137634ff4186 375 {
ansond 0:137634ff4186 376 if( verbose != 0 )
ansond 0:137634ff4186 377 polarssl_printf( " PBKDF2 (SHA1) #%d: ", i );
ansond 0:137634ff4186 378
ansond 0:137634ff4186 379 ret = pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i],
ansond 0:137634ff4186 380 slen[i], it_cnt[i], key_len[i], key );
ansond 0:137634ff4186 381 if( ret != 0 ||
ansond 0:137634ff4186 382 memcmp( result_key[i], key, key_len[i] ) != 0 )
ansond 0:137634ff4186 383 {
ansond 0:137634ff4186 384 if( verbose != 0 )
ansond 0:137634ff4186 385 polarssl_printf( "failed\n" );
ansond 0:137634ff4186 386
ansond 0:137634ff4186 387 ret = 1;
ansond 0:137634ff4186 388 goto exit;
ansond 0:137634ff4186 389 }
ansond 0:137634ff4186 390
ansond 0:137634ff4186 391 if( verbose != 0 )
ansond 0:137634ff4186 392 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 393 }
ansond 0:137634ff4186 394
ansond 0:137634ff4186 395 polarssl_printf( "\n" );
ansond 0:137634ff4186 396
ansond 0:137634ff4186 397 exit:
ansond 0:137634ff4186 398 md_free( &sha1_ctx );
ansond 0:137634ff4186 399
ansond 0:137634ff4186 400 return( ret );
ansond 0:137634ff4186 401 }
ansond 0:137634ff4186 402 #endif /* POLARSSL_SHA1_C */
ansond 0:137634ff4186 403
ansond 0:137634ff4186 404 #endif /* POLARSSL_SELF_TEST */
ansond 0:137634ff4186 405
ansond 0:137634ff4186 406 #endif /* POLARSSL_PKCS5_C */
ansond 0:137634ff4186 407