blob: fc52248834a7e3e7dc5afe6e39b96e96aaf5ee05 [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakkerb0c19a42013-06-24 19:26:38 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PKCS5_C)
Paul Bakkerb0c19a42013-06-24 19:26:38 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/pkcs5.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000037#include "mbedtls/error.h"
Marcos Del Sol Vives8a0dfac2016-11-06 12:22:25 +010038
39#if defined(MBEDTLS_ASN1_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/asn1.h"
41#include "mbedtls/cipher.h"
42#include "mbedtls/oid.h"
Andres Amaya Garciaaf9a4862018-03-27 20:53:07 +010043#endif /* MBEDTLS_ASN1_PARSE_C */
44
45#include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#else
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#define mbedtls_printf printf
Paul Bakker7dc4c442014-02-01 22:50:26 +010052#endif
53
Hanno Becker1ea604d2018-10-12 10:57:33 +010054#if defined(MBEDTLS_ASN1_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055static int pkcs5_parse_pbkdf2_params( const mbedtls_asn1_buf *params,
56 mbedtls_asn1_buf *salt, int *iterations,
57 int *keylen, mbedtls_md_type_t *md_type )
Paul Bakker28144de2013-06-24 19:28:55 +020058{
Janos Follath24eed8d2019-11-22 13:21:35 +000059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_asn1_buf prf_alg_oid;
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020061 unsigned char *p = params->p;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020062 const unsigned char *end = params->p + params->len;
Paul Bakker28144de2013-06-24 19:28:55 +020063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
65 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
66 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker28144de2013-06-24 19:28:55 +020067 /*
68 * PBKDF2-params ::= SEQUENCE {
69 * salt OCTET STRING,
70 * iterationCount INTEGER,
71 * keyLength INTEGER OPTIONAL
72 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
73 * }
74 *
75 */
Andrzej Kurekee3c4352019-01-10 03:10:02 -050076 if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len,
77 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +020079
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020080 salt->p = p;
81 p += salt->len;
Paul Bakker28144de2013-06-24 19:28:55 +020082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 )
84 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +020085
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020086 if( p == end )
Paul Bakker28144de2013-06-24 19:28:55 +020087 return( 0 );
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +020090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
92 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +020093 }
94
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020095 if( p == end )
Paul Bakker28144de2013-06-24 19:28:55 +020096 return( 0 );
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
99 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200100
Antonio Quartulli12ccef22017-12-20 07:03:55 +0800101 if( mbedtls_oid_get_md_hmac( &prf_alg_oid, md_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200103
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +0200104 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
106 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker28144de2013-06-24 19:28:55 +0200107
108 return( 0 );
109}
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakker28144de2013-06-24 19:28:55 +0200112 const unsigned char *pwd, size_t pwdlen,
113 const unsigned char *data, size_t datalen,
114 unsigned char *output )
115{
116 int ret, iterations = 0, keylen = 0;
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200117 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
119 mbedtls_asn1_buf salt;
120 mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
Paul Bakker28144de2013-06-24 19:28:55 +0200121 unsigned char key[32], iv[32];
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200122 size_t olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 const mbedtls_md_info_t *md_info;
124 const mbedtls_cipher_info_t *cipher_info;
125 mbedtls_md_context_t md_ctx;
126 mbedtls_cipher_type_t cipher_alg;
127 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker28144de2013-06-24 19:28:55 +0200128
129 p = pbe_params->p;
130 end = p + pbe_params->len;
131
132 /*
133 * PBES2-params ::= SEQUENCE {
134 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
135 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
136 * }
137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 if( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
139 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
140 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200141
Andrzej Kurekee3c4352019-01-10 03:10:02 -0500142 if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid,
143 &kdf_alg_params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200145
146 // Only PBKDF2 supported at the moment
147 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
149 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200150
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200151 if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
Paul Bakker28144de2013-06-24 19:28:55 +0200152 &salt, &iterations, &keylen,
153 &md_type ) ) != 0 )
154 {
155 return( ret );
156 }
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 md_info = mbedtls_md_info_from_type( md_type );
Paul Bakker28144de2013-06-24 19:28:55 +0200159 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200163 &enc_scheme_params ) ) != 0 )
164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200166 }
Paul Bakker28144de2013-06-24 19:28:55 +0200167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
169 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 cipher_info = mbedtls_cipher_info_from_type( cipher_alg );
Paul Bakker28144de2013-06-24 19:28:55 +0200172 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200174
Manuel Pégourié-Gonnard66aca932014-06-12 13:14:55 +0200175 /*
176 * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
177 * since it is optional and we don't know if it was set or not
178 */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200179 keylen = cipher_info->key_bitlen / 8;
Paul Bakker28144de2013-06-24 19:28:55 +0200180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200182 enc_scheme_params.len != cipher_info->iv_size )
183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200185 }
Paul Bakker28144de2013-06-24 19:28:55 +0200186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_md_init( &md_ctx );
188 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200189
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200190 memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
Paul Bakker28144de2013-06-24 19:28:55 +0200191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200193 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 if( ( ret = mbedtls_pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
Paul Bakker66d5d072014-06-17 16:39:18 +0200196 iterations, keylen, key ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +0200197 {
Paul Bakker46320832013-07-03 14:01:52 +0200198 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200199 }
200
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200201 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200202 goto exit;
203
Andrzej Kurekee3c4352019-01-10 03:10:02 -0500204 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen,
205 (mbedtls_operation_t) mode ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200206 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( ( ret = mbedtls_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
Manuel Pégourié-Gonnard90dac902014-06-12 17:04:24 +0200209 data, datalen, output, &olen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
Paul Bakker28144de2013-06-24 19:28:55 +0200211
Paul Bakker46320832013-07-03 14:01:52 +0200212exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_md_free( &md_ctx );
214 mbedtls_cipher_free( &cipher_ctx );
Paul Bakker46320832013-07-03 14:01:52 +0200215
216 return( ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200217}
Marcos Del Sol Vives8a0dfac2016-11-06 12:22:25 +0100218#endif /* MBEDTLS_ASN1_PARSE_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200219
Andrzej Kurekee3c4352019-01-10 03:10:02 -0500220int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
221 const unsigned char *password,
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200222 size_t plen, const unsigned char *salt, size_t slen,
223 unsigned int iteration_count,
224 uint32_t key_length, unsigned char *output )
225{
226 int ret, j;
227 unsigned int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 unsigned char md1[MBEDTLS_MD_MAX_SIZE];
229 unsigned char work[MBEDTLS_MD_MAX_SIZE];
230 unsigned char md_size = mbedtls_md_get_size( ctx->md_info );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200231 size_t use_len;
232 unsigned char *out_p = output;
233 unsigned char counter[4];
234
235 memset( counter, 0, 4 );
236 counter[3] = 1;
237
Azim Khan45b79cf2018-05-23 16:55:16 +0100238#if UINT_MAX > 0xFFFFFFFF
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200239 if( iteration_count > 0xFFFFFFFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
Azim Khan45b79cf2018-05-23 16:55:16 +0100241#endif
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200242
Jack Lloyd71657492019-09-23 19:15:54 -0400243 if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
244 return( ret );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200245 while( key_length )
246 {
247 // U1 ends up in work
248 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200250 return( ret );
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200253 return( ret );
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200256 return( ret );
257
Jack Lloyd71657492019-09-23 19:15:54 -0400258 if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
259 return( ret );
260
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200261 memcpy( md1, work, md_size );
262
Paul Bakker66d5d072014-06-17 16:39:18 +0200263 for( i = 1; i < iteration_count; i++ )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200264 {
265 // U2 ends up in md1
266 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200268 return( ret );
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200271 return( ret );
272
Jack Lloyd71657492019-09-23 19:15:54 -0400273 if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
274 return( ret );
275
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200276 // U1 xor U2
277 //
278 for( j = 0; j < md_size; j++ )
279 work[j] ^= md1[j];
280 }
281
282 use_len = ( key_length < md_size ) ? key_length : md_size;
283 memcpy( out_p, work, use_len );
284
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200285 key_length -= (uint32_t) use_len;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200286 out_p += use_len;
287
288 for( i = 4; i > 0; i-- )
289 if( ++counter[i - 1] != 0 )
290 break;
291 }
292
293 return( 0 );
294}
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_SELF_TEST)
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#if !defined(MBEDTLS_SHA1_C)
299int mbedtls_pkcs5_self_test( int verbose )
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200300{
301 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_printf( " PBKDF2 (SHA1): skipped\n\n" );
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200303
304 return( 0 );
305}
306#else
307
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200308#define MAX_TESTS 6
309
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100310static const size_t plen_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000311 { 8, 8, 8, 24, 9 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200312
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100313static const unsigned char password_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200314{
315 "password",
316 "password",
317 "password",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200318 "passwordPASSWORDpassword",
319 "pass\0word",
320};
321
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100322static const size_t slen_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000323 { 4, 4, 4, 36, 5 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200324
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100325static const unsigned char salt_test_data[MAX_TESTS][40] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200326{
327 "salt",
328 "salt",
329 "salt",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200330 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
331 "sa\0lt",
332};
333
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100334static const uint32_t it_cnt_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000335 { 1, 2, 4096, 4096, 4096 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200336
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100337static const uint32_t key_len_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000338 { 20, 20, 20, 25, 16 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200339
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100340static const unsigned char result_key_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200341{
342 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
343 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
344 0x2f, 0xe0, 0x37, 0xa6 },
345 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
346 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
347 0xd8, 0xde, 0x89, 0x57 },
348 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
349 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
350 0x65, 0xa4, 0x29, 0xc1 },
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200351 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
352 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
353 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
354 0x38 },
355 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
356 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
357};
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359int mbedtls_pkcs5_self_test( int verbose )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_md_context_t sha1_ctx;
362 const mbedtls_md_info_t *info_sha1;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200363 int ret, i;
364 unsigned char key[64];
365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_md_init( &sha1_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 info_sha1 = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200369 if( info_sha1 == NULL )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200370 {
371 ret = 1;
372 goto exit;
373 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 if( ( ret = mbedtls_md_setup( &sha1_ctx, info_sha1, 1 ) ) != 0 )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200376 {
377 ret = 1;
378 goto exit;
379 }
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200380
381 for( i = 0; i < MAX_TESTS; i++ )
382 {
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100383 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_printf( " PBKDF2 (SHA1) #%d: ", i );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200385
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100386 ret = mbedtls_pkcs5_pbkdf2_hmac( &sha1_ctx, password_test_data[i],
387 plen_test_data[i], salt_test_data[i],
388 slen_test_data[i], it_cnt_test_data[i],
389 key_len_test_data[i], key );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200390 if( ret != 0 ||
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100391 memcmp( result_key_test_data[i], key, key_len_test_data[i] ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200392 {
393 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_printf( "failed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200395
Paul Bakker84bbeb52014-07-01 14:53:22 +0200396 ret = 1;
397 goto exit;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200398 }
399
400 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_printf( "passed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200402 }
403
Paul Bakker4400ecc2016-07-19 14:41:43 +0100404 if( verbose != 0 )
405 mbedtls_printf( "\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200406
Paul Bakker84bbeb52014-07-01 14:53:22 +0200407exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_md_free( &sha1_ctx );
Paul Bakkerf8634852013-07-03 13:31:52 +0200409
Alfred Klomp1b4eda32014-07-14 22:07:34 +0200410 return( ret );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_SHA1_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414#endif /* MBEDTLS_SELF_TEST */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#endif /* MBEDTLS_PKCS5_C */