blob: 1d43a3a7372efec56831d3420b56a728389b800e [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 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
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 */
23/*
24 * PKCS#5 includes PBKDF2 and more
25 *
26 * http://tools.ietf.org/html/rfc2898 (Specification)
27 * http://tools.ietf.org/html/rfc6070 (Test vectors)
28 */
29
Gilles Peskinedb09ef62020-06-03 01:43:33 +020030#include "common.h"
Paul Bakkerb0c19a42013-06-24 19:26:38 +020031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_PKCS5_C)
Paul Bakkerb0c19a42013-06-24 19:26:38 +020033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/pkcs5.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000035#include "mbedtls/error.h"
Marcos Del Sol Vives8a0dfac2016-11-06 12:22:25 +010036
37#if defined(MBEDTLS_ASN1_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/asn1.h"
39#include "mbedtls/cipher.h"
40#include "mbedtls/oid.h"
Andres Amaya Garciaaf9a4862018-03-27 20:53:07 +010041#endif /* MBEDTLS_ASN1_PARSE_C */
42
43#include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010047#else
Rich Evans00ab4702015-02-06 13:43:58 +000048#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define mbedtls_printf printf
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#endif
51
Andrzej Kurekdd36c762022-08-31 13:29:38 -040052#include "hash_info.h"
53#include "mbedtls/psa_util.h"
54
Hanno Becker1ea604d2018-10-12 10:57:33 +010055#if defined(MBEDTLS_ASN1_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056static int pkcs5_parse_pbkdf2_params( const mbedtls_asn1_buf *params,
57 mbedtls_asn1_buf *salt, int *iterations,
58 int *keylen, mbedtls_md_type_t *md_type )
Paul Bakker28144de2013-06-24 19:28:55 +020059{
Janos Follath24eed8d2019-11-22 13:21:35 +000060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_asn1_buf prf_alg_oid;
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020062 unsigned char *p = params->p;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020063 const unsigned char *end = params->p + params->len;
Paul Bakker28144de2013-06-24 19:28:55 +020064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +010066 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
67 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker28144de2013-06-24 19:28:55 +020068 /*
69 * PBKDF2-params ::= SEQUENCE {
70 * salt OCTET STRING,
71 * iterationCount INTEGER,
72 * keyLength INTEGER OPTIONAL
73 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
74 * }
75 *
76 */
Andrzej Kurekee3c4352019-01-10 03:10:02 -050077 if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len,
78 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010079 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakker28144de2013-06-24 19:28:55 +020080
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020081 salt->p = p;
82 p += salt->len;
Paul Bakker28144de2013-06-24 19:28:55 +020083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010085 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakker28144de2013-06-24 19:28:55 +020086
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020087 if( p == end )
Paul Bakker28144de2013-06-24 19:28:55 +020088 return( 0 );
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +020091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +010093 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakker28144de2013-06-24 19:28:55 +020094 }
95
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020096 if( p == end )
Paul Bakker28144de2013-06-24 19:28:55 +020097 return( 0 );
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100100 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakker28144de2013-06-24 19:28:55 +0200101
Antonio Quartulli12ccef22017-12-20 07:03:55 +0800102 if( mbedtls_oid_get_md_hmac( &prf_alg_oid, md_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200104
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +0200105 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100106 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
107 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker28144de2013-06-24 19:28:55 +0200108
109 return( 0 );
110}
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakker28144de2013-06-24 19:28:55 +0200113 const unsigned char *pwd, size_t pwdlen,
114 const unsigned char *data, size_t datalen,
115 unsigned char *output )
116{
117 int ret, iterations = 0, keylen = 0;
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200118 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
120 mbedtls_asn1_buf salt;
121 mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
Paul Bakker28144de2013-06-24 19:28:55 +0200122 unsigned char key[32], iv[32];
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200123 size_t olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_cipher_type_t cipher_alg;
126 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker28144de2013-06-24 19:28:55 +0200127
128 p = pbe_params->p;
129 end = p + pbe_params->len;
130
131 /*
132 * PBES2-params ::= SEQUENCE {
133 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
134 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
135 * }
136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 if( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100138 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
139 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200140
Andrzej Kurekee3c4352019-01-10 03:10:02 -0500141 if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid,
142 &kdf_alg_params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100143 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakker28144de2013-06-24 19:28:55 +0200144
145 // Only PBKDF2 supported at the moment
146 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
148 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200149
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200150 if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
Paul Bakker28144de2013-06-24 19:28:55 +0200151 &salt, &iterations, &keylen,
152 &md_type ) ) != 0 )
153 {
154 return( ret );
155 }
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200158 &enc_scheme_params ) ) != 0 )
159 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100160 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200161 }
Paul Bakker28144de2013-06-24 19:28:55 +0200162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
164 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 cipher_info = mbedtls_cipher_info_from_type( cipher_alg );
Paul Bakker28144de2013-06-24 19:28:55 +0200167 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200169
Manuel Pégourié-Gonnard66aca932014-06-12 13:14:55 +0200170 /*
171 * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
172 * since it is optional and we don't know if it was set or not
173 */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200174 keylen = cipher_info->key_bitlen / 8;
Paul Bakker28144de2013-06-24 19:28:55 +0200175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200177 enc_scheme_params.len != cipher_info->iv_size )
178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200180 }
Paul Bakker28144de2013-06-24 19:28:55 +0200181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200183
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200184 memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
Paul Bakker28144de2013-06-24 19:28:55 +0200185
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400186 if( ( ret = mbedtls_pkcs5_pbkdf2_hmac_ext( md_type, pwd, pwdlen, salt.p,
187 salt.len, iterations, keylen,
188 key ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +0200189 {
Paul Bakker46320832013-07-03 14:01:52 +0200190 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200191 }
192
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200193 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200194 goto exit;
195
Andrzej Kurekee3c4352019-01-10 03:10:02 -0500196 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen,
197 (mbedtls_operation_t) mode ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200198 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 if( ( ret = mbedtls_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
Manuel Pégourié-Gonnard90dac902014-06-12 17:04:24 +0200201 data, datalen, output, &olen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
Paul Bakker28144de2013-06-24 19:28:55 +0200203
Paul Bakker46320832013-07-03 14:01:52 +0200204exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_cipher_free( &cipher_ctx );
Paul Bakker46320832013-07-03 14:01:52 +0200206
207 return( ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200208}
Marcos Del Sol Vives8a0dfac2016-11-06 12:22:25 +0100209#endif /* MBEDTLS_ASN1_PARSE_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200210
Andrzej Kurekee3c4352019-01-10 03:10:02 -0500211int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
212 const unsigned char *password,
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200213 size_t plen, const unsigned char *salt, size_t slen,
214 unsigned int iteration_count,
215 uint32_t key_length, unsigned char *output )
216{
gabor-mezei-armb8513fa2020-08-24 09:53:04 +0200217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
218 int j;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200219 unsigned int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 unsigned char md1[MBEDTLS_MD_MAX_SIZE];
221 unsigned char work[MBEDTLS_MD_MAX_SIZE];
222 unsigned char md_size = mbedtls_md_get_size( ctx->md_info );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200223 size_t use_len;
224 unsigned char *out_p = output;
225 unsigned char counter[4];
226
227 memset( counter, 0, 4 );
228 counter[3] = 1;
229
Azim Khan45b79cf2018-05-23 16:55:16 +0100230#if UINT_MAX > 0xFFFFFFFF
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200231 if( iteration_count > 0xFFFFFFFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
Azim Khan45b79cf2018-05-23 16:55:16 +0100233#endif
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200234
Jack Lloyd71657492019-09-23 19:15:54 -0400235 if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
236 return( ret );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200237 while( key_length )
238 {
239 // U1 ends up in work
240 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200242 goto cleanup;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200245 goto cleanup;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200248 goto cleanup;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200249
Jack Lloyd71657492019-09-23 19:15:54 -0400250 if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200251 goto cleanup;
Jack Lloyd71657492019-09-23 19:15:54 -0400252
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200253 memcpy( md1, work, md_size );
254
Paul Bakker66d5d072014-06-17 16:39:18 +0200255 for( i = 1; i < iteration_count; i++ )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200256 {
257 // U2 ends up in md1
258 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200260 goto cleanup;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200263 goto cleanup;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200264
Jack Lloyd71657492019-09-23 19:15:54 -0400265 if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200266 goto cleanup;
Jack Lloyd71657492019-09-23 19:15:54 -0400267
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200268 // U1 xor U2
269 //
270 for( j = 0; j < md_size; j++ )
271 work[j] ^= md1[j];
272 }
273
274 use_len = ( key_length < md_size ) ? key_length : md_size;
275 memcpy( out_p, work, use_len );
276
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200277 key_length -= (uint32_t) use_len;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200278 out_p += use_len;
279
280 for( i = 4; i > 0; i-- )
281 if( ++counter[i - 1] != 0 )
282 break;
283 }
284
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200285cleanup:
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200286 /* Zeroise buffers to clear sensitive data from memory. */
287 mbedtls_platform_zeroize( work, MBEDTLS_MD_MAX_SIZE );
288 mbedtls_platform_zeroize( md1, MBEDTLS_MD_MAX_SIZE );
289
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200290 return( ret );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200291}
292
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400293int mbedtls_pkcs5_pbkdf2_hmac_ext( mbedtls_md_type_t md_alg,
294 const unsigned char *password,
295 size_t plen, const unsigned char *salt, size_t slen,
296 unsigned int iteration_count,
297 uint32_t key_length, unsigned char *output )
298{
299#if defined(MBEDTLS_MD_C)
300 mbedtls_md_context_t md_ctx;
301 const mbedtls_md_info_t *md_info;
302 int ret;
303
304 mbedtls_md_init( &md_ctx );
305
306 md_info = mbedtls_md_info_from_type( md_alg );
307 if( md_info == NULL )
308 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
309
310 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
311 goto exit;
312 ret = mbedtls_pkcs5_pbkdf2_hmac( &md_ctx, password, plen, salt, slen,
313 iteration_count, key_length, output );
314exit:
315 mbedtls_md_free( &md_ctx );
316 return( ret );
317#else
318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
319 int j;
320 unsigned int i;
321 unsigned char md1[PSA_HASH_MAX_SIZE];
322 unsigned char work[PSA_HASH_MAX_SIZE];
323 unsigned char md_size = mbedtls_hash_info_get_size( md_alg );
324 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
325
326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
327 size_t use_len, out_len, out_size;
328 unsigned char *out_p = output;
329 unsigned char counter[4];
330 mbedtls_svc_key_id_t psa_hmac_key;
331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
332
333
334 memset( counter, 0, 4 );
335 counter[3] = 1;
336 psa_algorithm_t alg = PSA_ALG_HMAC( mbedtls_hash_info_psa_from_md( md_alg ) );
337 out_size = PSA_MAC_LENGTH( PSA_KEY_TYPE_HMAC, 0, alg );
338 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
339 psa_set_key_algorithm( &attributes, alg );
340 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
341
342 if( key_length == 0 )
343 return 0;
344 if( ( status = psa_import_key( &attributes,
345 password, plen,
346 &psa_hmac_key ) ) != PSA_SUCCESS )
347 {
348 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
349 }
350
351#if UINT_MAX > 0xFFFFFFFF
352 if( iteration_count > 0xFFFFFFFF )
353 return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
354#endif
355
356 while( key_length )
357 {
358 status = psa_mac_sign_setup( &operation, psa_hmac_key,
359 PSA_ALG_HMAC( alg ) );
360 if( status != PSA_SUCCESS )
361 goto cleanup;
362 // U1 ends up in work
363 if( ( status = psa_mac_update( &operation, salt, slen ) ) != PSA_SUCCESS )
364 goto cleanup;
365
366 if( ( status = psa_mac_update( &operation, counter, 4 ) ) != PSA_SUCCESS )
367 goto cleanup;
368
369 if( ( status = psa_mac_sign_finish( &operation, work, out_size, &out_len ) )
370 != PSA_SUCCESS )
371 goto cleanup;
372
373 memcpy( md1, work, md_size );
374
375 for( i = 1; i < iteration_count; i++ )
376 {
377 // U2 ends up in md1
378 //
379 status = psa_mac_sign_setup( &operation, psa_hmac_key,
380 PSA_ALG_HMAC( alg ) );
381 if( status != PSA_SUCCESS )
382 goto cleanup;
383 if( ( status = psa_mac_update( &operation, md1, md_size ) ) != PSA_SUCCESS )
384 goto cleanup;
385
386 if( ( status = psa_mac_sign_finish( &operation, md1, out_size, &out_len ) ) != PSA_SUCCESS )
387 goto cleanup;
388
389
390 // U1 xor U2
391 //
392 for( j = 0; j < md_size; j++ )
393 work[j] ^= md1[j];
394 }
395
396 use_len = ( key_length < md_size ) ? key_length : md_size;
397 memcpy( out_p, work, use_len );
398
399 key_length -= (uint32_t) use_len;
400 out_p += use_len;
401
402 for( i = 4; i > 0; i-- )
403 if( ++counter[i - 1] != 0 )
404 break;
405 }
406
407cleanup:
408 /* Zeroise buffers to clear sensitive data from memory. */
409 mbedtls_platform_zeroize( work, PSA_HASH_MAX_SIZE );
410 mbedtls_platform_zeroize( md1, PSA_HASH_MAX_SIZE );
411 psa_destroy_key( psa_hmac_key );
412 ret = (status != PSA_SUCCESS? MBEDTLS_ERR_ERROR_GENERIC_ERROR: 0);
413 status = psa_mac_abort( &operation );
414 if( ret == 0 && status != PSA_SUCCESS )
415 ret = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
416
417 return ( ret );
418#endif
419}
420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#if defined(MBEDTLS_SELF_TEST)
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423#if !defined(MBEDTLS_SHA1_C)
424int mbedtls_pkcs5_self_test( int verbose )
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200425{
426 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_printf( " PBKDF2 (SHA1): skipped\n\n" );
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200428
429 return( 0 );
430}
431#else
432
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200433#define MAX_TESTS 6
434
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100435static const size_t plen_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000436 { 8, 8, 8, 24, 9 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200437
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100438static const unsigned char password_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200439{
440 "password",
441 "password",
442 "password",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200443 "passwordPASSWORDpassword",
444 "pass\0word",
445};
446
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100447static const size_t slen_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000448 { 4, 4, 4, 36, 5 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200449
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100450static const unsigned char salt_test_data[MAX_TESTS][40] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200451{
452 "salt",
453 "salt",
454 "salt",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200455 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
456 "sa\0lt",
457};
458
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100459static const uint32_t it_cnt_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000460 { 1, 2, 4096, 4096, 4096 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200461
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100462static const uint32_t key_len_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000463 { 20, 20, 20, 25, 16 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200464
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100465static const unsigned char result_key_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200466{
467 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
468 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
469 0x2f, 0xe0, 0x37, 0xa6 },
470 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
471 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
472 0xd8, 0xde, 0x89, 0x57 },
473 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
474 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
475 0x65, 0xa4, 0x29, 0xc1 },
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200476 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
477 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
478 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
479 0x38 },
480 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
481 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
482};
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484int mbedtls_pkcs5_self_test( int verbose )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200485{
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200486 int ret, i;
487 unsigned char key[64];
488
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200489 for( i = 0; i < MAX_TESTS; i++ )
490 {
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100491 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_printf( " PBKDF2 (SHA1) #%d: ", i );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200493
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400494 ret = mbedtls_pkcs5_pbkdf2_hmac_ext( MBEDTLS_MD_SHA1, password_test_data[i],
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100495 plen_test_data[i], salt_test_data[i],
496 slen_test_data[i], it_cnt_test_data[i],
497 key_len_test_data[i], key );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200498 if( ret != 0 ||
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100499 memcmp( result_key_test_data[i], key, key_len_test_data[i] ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200500 {
501 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_printf( "failed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200503
Paul Bakker84bbeb52014-07-01 14:53:22 +0200504 ret = 1;
505 goto exit;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200506 }
507
508 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_printf( "passed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200510 }
511
Paul Bakker4400ecc2016-07-19 14:41:43 +0100512 if( verbose != 0 )
513 mbedtls_printf( "\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200514
Paul Bakker84bbeb52014-07-01 14:53:22 +0200515exit:
Alfred Klomp1b4eda32014-07-14 22:07:34 +0200516 return( ret );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#endif /* MBEDTLS_SHA1_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#endif /* MBEDTLS_SELF_TEST */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#endif /* MBEDTLS_PKCS5_C */