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