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