Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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