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