1
#pragma clang diagnostic ignored "-Wunknown-warning-option"
2
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
3
#pragma clang diagnostic ignored "-Wsign-conversion"
5
#pragma GCC diagnostic ignored "-Wconversion"
7
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
8
#pragma clang diagnostic ignored "-Wsign-conversion"
10
#pragma GCC diagnostic ignored "-Wconversion"
12
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
13
#pragma clang diagnostic ignored "-Wsign-conversion"
17
* \brief PKCS#5 functions
19
* \author Mathias Olsson <mathias@kompetensum.com>
21
* Copyright The Mbed TLS Contributors
22
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
24
* This file is provided under the Apache License 2.0, or the
25
* GNU General Public License v2.0 or later.
30
* Licensed under the Apache License, Version 2.0 (the "License"); you may
31
* not use this file except in compliance with the License.
32
* You may obtain a copy of the License at
34
* http://www.apache.org/licenses/LICENSE-2.0
36
* Unless required by applicable law or agreed to in writing, software
37
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
38
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39
* See the License for the specific language governing permissions and
40
* limitations under the License.
45
* GNU General Public License v2.0 or later:
47
* This program is free software; you can redistribute it and/or modify
48
* it under the terms of the GNU General Public License as published by
49
* the Free Software Foundation; either version 2 of the License, or
50
* (at your option) any later version.
52
* This program is distributed in the hope that it will be useful,
53
* but WITHOUT ANY WARRANTY; without even the implied warranty of
54
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55
* GNU General Public License for more details.
57
* You should have received a copy of the GNU General Public License along
58
* with this program; if not, write to the Free Software Foundation, Inc.,
59
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
64
* PKCS#5 includes PBKDF2 and more
66
* http://tools.ietf.org/html/rfc2898 (Specification)
67
* http://tools.ietf.org/html/rfc6070 (Test vectors)
70
#if !defined(MBEDTLS_CONFIG_FILE)
71
#include "mbedtls/config.h"
73
#include MBEDTLS_CONFIG_FILE
76
#if defined(MBEDTLS_PKCS5_C)
78
#include "mbedtls/pkcs5.h"
80
#if defined(MBEDTLS_ASN1_PARSE_C)
81
#include "mbedtls/asn1.h"
82
#include "mbedtls/cipher.h"
83
#include "mbedtls/oid.h"
84
#endif /* MBEDTLS_ASN1_PARSE_C */
88
#if defined(MBEDTLS_PLATFORM_C)
89
#include "mbedtls/platform.h"
92
#define mbedtls_printf printf
95
#if defined(MBEDTLS_ASN1_PARSE_C)
96
static int pkcs5_parse_pbkdf2_params( const mbedtls_asn1_buf *params,
97
mbedtls_asn1_buf *salt, int *iterations,
98
int *keylen, mbedtls_md_type_t *md_type )
101
mbedtls_asn1_buf prf_alg_oid;
102
unsigned char *p = params->p;
103
const unsigned char *end = params->p + params->len;
105
if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
106
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
107
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
109
* PBKDF2-params ::= SEQUENCE {
111
* iterationCount INTEGER,
112
* keyLength INTEGER OPTIONAL
113
* prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
117
if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
118
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
123
if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 )
124
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
129
if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 )
131
if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
132
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
138
if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
139
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
141
if( mbedtls_oid_get_md_hmac( &prf_alg_oid, md_type ) != 0 )
142
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
145
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
146
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
151
int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
152
const unsigned char *pwd, size_t pwdlen,
153
const unsigned char *data, size_t datalen,
154
unsigned char *output )
156
int ret, iterations = 0, keylen = 0;
157
unsigned char *p, *end;
158
mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
159
mbedtls_asn1_buf salt;
160
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
161
unsigned char key[32], iv[32];
163
const mbedtls_md_info_t *md_info;
164
const mbedtls_cipher_info_t *cipher_info;
165
mbedtls_md_context_t md_ctx;
166
mbedtls_cipher_type_t cipher_alg;
167
mbedtls_cipher_context_t cipher_ctx;
170
end = p + pbe_params->len;
173
* PBES2-params ::= SEQUENCE {
174
* keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
175
* encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
178
if( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
179
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
180
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
182
if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid, &kdf_alg_params ) ) != 0 )
183
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
185
// Only PBKDF2 supported at the moment
187
if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
188
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
190
if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
191
&salt, &iterations, &keylen,
197
md_info = mbedtls_md_info_from_type( md_type );
198
if( md_info == NULL )
199
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
201
if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
202
&enc_scheme_params ) ) != 0 )
204
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
207
if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
208
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
210
cipher_info = mbedtls_cipher_info_from_type( cipher_alg );
211
if( cipher_info == NULL )
212
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
215
* The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
216
* since it is optional and we don't know if it was set or not
218
keylen = cipher_info->key_bitlen / 8;
220
if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
221
enc_scheme_params.len != cipher_info->iv_size )
223
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
226
mbedtls_md_init( &md_ctx );
227
mbedtls_cipher_init( &cipher_ctx );
229
memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
231
if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
234
if( ( ret = mbedtls_pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
235
iterations, keylen, key ) ) != 0 )
240
if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
243
if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
246
if( ( ret = mbedtls_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
247
data, datalen, output, &olen ) ) != 0 )
248
ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
251
mbedtls_md_free( &md_ctx );
252
mbedtls_cipher_free( &cipher_ctx );
256
#endif /* MBEDTLS_ASN1_PARSE_C */
258
int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *password,
259
size_t plen, const unsigned char *salt, size_t slen,
260
unsigned int iteration_count,
261
uint32_t key_length, unsigned char *output )
265
unsigned char md1[MBEDTLS_MD_MAX_SIZE];
266
unsigned char work[MBEDTLS_MD_MAX_SIZE];
267
unsigned char md_size = mbedtls_md_get_size( ctx->md_info );
269
unsigned char *out_p = output;
270
unsigned char counter[4];
272
memset( counter, 0, 4 );
275
#if UINT_MAX > 0xFFFFFFFF
276
if( iteration_count > 0xFFFFFFFF )
277
return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
282
// U1 ends up in work
284
if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
287
if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
290
if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 )
293
if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
296
memcpy( md1, work, md_size );
298
for( i = 1; i < iteration_count; i++ )
302
if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
305
if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
308
if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
313
for( j = 0; j < md_size; j++ )
317
use_len = ( key_length < md_size ) ? key_length : md_size;
318
memcpy( out_p, work, use_len );
320
key_length -= (uint32_t) use_len;
323
for( i = 4; i > 0; i-- )
324
if( ++counter[i - 1] != 0 )
329
/* Zeroise buffers to clear sensitive data from memory. */
330
mbedtls_platform_zeroize( work, MBEDTLS_MD_MAX_SIZE );
331
mbedtls_platform_zeroize( md1, MBEDTLS_MD_MAX_SIZE );
336
#if defined(MBEDTLS_SELF_TEST)
338
#if !defined(MBEDTLS_SHA1_C)
339
int mbedtls_pkcs5_self_test( int verbose )
342
mbedtls_printf( " PBKDF2 (SHA1): skipped\n\n" );
350
static const size_t plen[MAX_TESTS] =
353
static const unsigned char password[MAX_TESTS][32] =
358
"passwordPASSWORDpassword",
362
static const size_t slen[MAX_TESTS] =
365
static const unsigned char salt[MAX_TESTS][40] =
370
"saltSALTsaltSALTsaltSALTsaltSALTsalt",
374
static const uint32_t it_cnt[MAX_TESTS] =
375
{ 1, 2, 4096, 4096, 4096 };
377
static const uint32_t key_len[MAX_TESTS] =
378
{ 20, 20, 20, 25, 16 };
380
static const unsigned char result_key[MAX_TESTS][32] =
382
{ 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
383
0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
384
0x2f, 0xe0, 0x37, 0xa6 },
385
{ 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
386
0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
387
0xd8, 0xde, 0x89, 0x57 },
388
{ 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
389
0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
390
0x65, 0xa4, 0x29, 0xc1 },
391
{ 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
392
0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
393
0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
395
{ 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
396
0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
399
int mbedtls_pkcs5_self_test( int verbose )
401
mbedtls_md_context_t sha1_ctx;
402
const mbedtls_md_info_t *info_sha1;
404
unsigned char key[64];
406
mbedtls_md_init( &sha1_ctx );
408
info_sha1 = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
409
if( info_sha1 == NULL )
415
if( ( ret = mbedtls_md_setup( &sha1_ctx, info_sha1, 1 ) ) != 0 )
421
for( i = 0; i < MAX_TESTS; i++ )
424
mbedtls_printf( " PBKDF2 (SHA1) #%d: ", i );
426
ret = mbedtls_pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i],
427
slen[i], it_cnt[i], key_len[i], key );
429
memcmp( result_key[i], key, key_len[i] ) != 0 )
432
mbedtls_printf( "failed\n" );
439
mbedtls_printf( "passed\n" );
443
mbedtls_printf( "\n" );
446
mbedtls_md_free( &sha1_ctx );
450
#endif /* MBEDTLS_SHA1_C */
452
#endif /* MBEDTLS_SELF_TEST */
454
#endif /* MBEDTLS_PKCS5_C */