blob: e7d805c2c6e4e88c1b51b5a4a549154bfd15aabb [file] [log] [blame]
Paul Bakkerb0c19a42013-06-24 19:26:38 +02001/**
2 * \file pkcs5.c
3 *
4 * \brief PKCS#5 functions
5 *
6 * \author Mathias Olsson <mathias@kompetensum.com>
7 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
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.
Paul Bakkerb0c19a42013-06-24 19:26:38 +020022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb0c19a42013-06-24 19:26:38 +020024 */
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakkerb0c19a42013-06-24 19:26:38 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_PKCS5_C)
Paul Bakkerb0c19a42013-06-24 19:26:38 +020039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/pkcs5.h"
Marcos Del Sol Vives8a0dfac2016-11-06 12:22:25 +010041
42#if defined(MBEDTLS_ASN1_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/asn1.h"
44#include "mbedtls/cipher.h"
45#include "mbedtls/oid.h"
Andres Amaya Garciaaf9a4862018-03-27 20:53:07 +010046#endif /* MBEDTLS_ASN1_PARSE_C */
47
48#include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010052#else
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_printf printf
Paul Bakker7dc4c442014-02-01 22:50:26 +010055#endif
56
Hanno Becker1ea604d2018-10-12 10:57:33 +010057#if defined(MBEDTLS_ASN1_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058static 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 )
Paul Bakker28144de2013-06-24 19:28:55 +020061{
62 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_asn1_buf prf_alg_oid;
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020064 unsigned char *p = params->p;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020065 const unsigned char *end = params->p + params->len;
Paul Bakker28144de2013-06-24 19:28:55 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
68 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
69 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker28144de2013-06-24 19:28:55 +020070 /*
71 * PBKDF2-params ::= SEQUENCE {
72 * salt OCTET STRING,
73 * iterationCount INTEGER,
74 * keyLength INTEGER OPTIONAL
75 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
76 * }
77 *
78 */
Andrzej Kurekee3c4352019-01-10 03:10:02 -050079 if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len,
80 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +020082
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020083 salt->p = p;
84 p += salt->len;
Paul Bakker28144de2013-06-24 19:28:55 +020085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 )
87 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +020088
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020089 if( p == end )
Paul Bakker28144de2013-06-24 19:28:55 +020090 return( 0 );
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +020093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
95 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +020096 }
97
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020098 if( p == end )
Paul Bakker28144de2013-06-24 19:28:55 +020099 return( 0 );
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
102 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200103
Antonio Quartulli12ccef22017-12-20 07:03:55 +0800104 if( mbedtls_oid_get_md_hmac( &prf_alg_oid, md_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200106
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +0200107 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
109 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker28144de2013-06-24 19:28:55 +0200110
111 return( 0 );
112}
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakker28144de2013-06-24 19:28:55 +0200115 const unsigned char *pwd, size_t pwdlen,
116 const unsigned char *data, size_t datalen,
117 unsigned char *output )
118{
119 int ret, iterations = 0, keylen = 0;
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200120 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
122 mbedtls_asn1_buf salt;
123 mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
Paul Bakker28144de2013-06-24 19:28:55 +0200124 unsigned char key[32], iv[32];
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200125 size_t olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 const mbedtls_md_info_t *md_info;
127 const mbedtls_cipher_info_t *cipher_info;
128 mbedtls_md_context_t md_ctx;
129 mbedtls_cipher_type_t cipher_alg;
130 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker28144de2013-06-24 19:28:55 +0200131
132 p = pbe_params->p;
133 end = p + pbe_params->len;
134
135 /*
136 * PBES2-params ::= SEQUENCE {
137 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
138 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
139 * }
140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 if( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
142 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
143 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200144
Andrzej Kurekee3c4352019-01-10 03:10:02 -0500145 if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid,
146 &kdf_alg_params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200148
149 // Only PBKDF2 supported at the moment
150 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
152 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200153
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200154 if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
Paul Bakker28144de2013-06-24 19:28:55 +0200155 &salt, &iterations, &keylen,
156 &md_type ) ) != 0 )
157 {
158 return( ret );
159 }
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakker28144de2013-06-24 19:28:55 +0200162 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200166 &enc_scheme_params ) ) != 0 )
167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200169 }
Paul Bakker28144de2013-06-24 19:28:55 +0200170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
172 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 cipher_info = mbedtls_cipher_info_from_type( cipher_alg );
Paul Bakker28144de2013-06-24 19:28:55 +0200175 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200177
Manuel Pégourié-Gonnard66aca932014-06-12 13:14:55 +0200178 /*
179 * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
180 * since it is optional and we don't know if it was set or not
181 */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200182 keylen = cipher_info->key_bitlen / 8;
Paul Bakker28144de2013-06-24 19:28:55 +0200183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200185 enc_scheme_params.len != cipher_info->iv_size )
186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200188 }
Paul Bakker28144de2013-06-24 19:28:55 +0200189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_md_init( &md_ctx );
191 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200192
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200193 memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
Paul Bakker28144de2013-06-24 19:28:55 +0200194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200196 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 if( ( ret = mbedtls_pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
Paul Bakker66d5d072014-06-17 16:39:18 +0200199 iterations, keylen, key ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +0200200 {
Paul Bakker46320832013-07-03 14:01:52 +0200201 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200202 }
203
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200204 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200205 goto exit;
206
Andrzej Kurekee3c4352019-01-10 03:10:02 -0500207 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen,
208 (mbedtls_operation_t) mode ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200209 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ( ret = mbedtls_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
Manuel Pégourié-Gonnard90dac902014-06-12 17:04:24 +0200212 data, datalen, output, &olen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
Paul Bakker28144de2013-06-24 19:28:55 +0200214
Paul Bakker46320832013-07-03 14:01:52 +0200215exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_md_free( &md_ctx );
217 mbedtls_cipher_free( &cipher_ctx );
Paul Bakker46320832013-07-03 14:01:52 +0200218
219 return( ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200220}
Marcos Del Sol Vives8a0dfac2016-11-06 12:22:25 +0100221#endif /* MBEDTLS_ASN1_PARSE_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200222
Andrzej Kurekee3c4352019-01-10 03:10:02 -0500223int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
224 const unsigned char *password,
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200225 size_t plen, const unsigned char *salt, size_t slen,
226 unsigned int iteration_count,
227 uint32_t key_length, unsigned char *output )
228{
229 int ret, j;
230 unsigned int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 unsigned char md1[MBEDTLS_MD_MAX_SIZE];
232 unsigned char work[MBEDTLS_MD_MAX_SIZE];
233 unsigned char md_size = mbedtls_md_get_size( ctx->md_info );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200234 size_t use_len;
235 unsigned char *out_p = output;
236 unsigned char counter[4];
237
238 memset( counter, 0, 4 );
239 counter[3] = 1;
240
Azim Khan45b79cf2018-05-23 16:55:16 +0100241#if UINT_MAX > 0xFFFFFFFF
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200242 if( iteration_count > 0xFFFFFFFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
Azim Khan45b79cf2018-05-23 16:55:16 +0100244#endif
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200245
246 while( key_length )
247 {
248 // U1 ends up in work
249 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200251 return( ret );
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200254 return( ret );
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200257 return( ret );
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200260 return( ret );
261
262 memcpy( md1, work, md_size );
263
Paul Bakker66d5d072014-06-17 16:39:18 +0200264 for( i = 1; i < iteration_count; i++ )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200265 {
266 // U2 ends up in md1
267 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200269 return( ret );
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200272 return( ret );
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200275 return( ret );
276
277 // U1 xor U2
278 //
279 for( j = 0; j < md_size; j++ )
280 work[j] ^= md1[j];
281 }
282
283 use_len = ( key_length < md_size ) ? key_length : md_size;
284 memcpy( out_p, work, use_len );
285
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200286 key_length -= (uint32_t) use_len;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200287 out_p += use_len;
288
289 for( i = 4; i > 0; i-- )
290 if( ++counter[i - 1] != 0 )
291 break;
292 }
293
294 return( 0 );
295}
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_SELF_TEST)
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#if !defined(MBEDTLS_SHA1_C)
300int mbedtls_pkcs5_self_test( int verbose )
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200301{
302 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_printf( " PBKDF2 (SHA1): skipped\n\n" );
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200304
305 return( 0 );
306}
307#else
308
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200309#define MAX_TESTS 6
310
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100311static const size_t plen_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000312 { 8, 8, 8, 24, 9 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200313
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100314static const unsigned char password_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200315{
316 "password",
317 "password",
318 "password",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200319 "passwordPASSWORDpassword",
320 "pass\0word",
321};
322
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100323static const size_t slen_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000324 { 4, 4, 4, 36, 5 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200325
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100326static const unsigned char salt_test_data[MAX_TESTS][40] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200327{
328 "salt",
329 "salt",
330 "salt",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200331 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
332 "sa\0lt",
333};
334
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100335static const uint32_t it_cnt_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000336 { 1, 2, 4096, 4096, 4096 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200337
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100338static const uint32_t key_len_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000339 { 20, 20, 20, 25, 16 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200340
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100341static const unsigned char result_key_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200342{
343 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
344 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
345 0x2f, 0xe0, 0x37, 0xa6 },
346 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
347 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
348 0xd8, 0xde, 0x89, 0x57 },
349 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
350 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
351 0x65, 0xa4, 0x29, 0xc1 },
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200352 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
353 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
354 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
355 0x38 },
356 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
357 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
358};
359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360int mbedtls_pkcs5_self_test( int verbose )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200361{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_md_context_t sha1_ctx;
363 const mbedtls_md_info_t *info_sha1;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200364 int ret, i;
365 unsigned char key[64];
366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_md_init( &sha1_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 info_sha1 = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200370 if( info_sha1 == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200371 {
372 ret = 1;
373 goto exit;
374 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( ( ret = mbedtls_md_setup( &sha1_ctx, info_sha1, 1 ) ) != 0 )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200377 {
378 ret = 1;
379 goto exit;
380 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200381
382 for( i = 0; i < MAX_TESTS; i++ )
383 {
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100384 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_printf( " PBKDF2 (SHA1) #%d: ", i );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200386
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100387 ret = mbedtls_pkcs5_pbkdf2_hmac( &sha1_ctx, password_test_data[i],
388 plen_test_data[i], salt_test_data[i],
389 slen_test_data[i], it_cnt_test_data[i],
390 key_len_test_data[i], key );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200391 if( ret != 0 ||
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100392 memcmp( result_key_test_data[i], key, key_len_test_data[i] ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200393 {
394 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_printf( "failed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200396
Paul Bakker84bbeb52014-07-01 14:53:22 +0200397 ret = 1;
398 goto exit;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200399 }
400
401 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_printf( "passed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200403 }
404
Paul Bakker4400ecc2016-07-19 14:41:43 +0100405 if( verbose != 0 )
406 mbedtls_printf( "\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200407
Paul Bakker84bbeb52014-07-01 14:53:22 +0200408exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_free( &sha1_ctx );
Paul Bakkerf8634852013-07-03 13:31:52 +0200410
Alfred Klomp1b4eda32014-07-14 22:07:34 +0200411 return( ret );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_SHA1_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#endif /* MBEDTLS_SELF_TEST */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_PKCS5_C */