BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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