mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 17 23:23:45 2019 +0000
Revision:
0:5b88d5760320
Child:
1:9db0e321a9f4
mbed-os5 only for TYBLE16

Who changed what in which revision?

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