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