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