blob: a6bc1b55c7be199e42676f3e03fd988e36342c25 [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é-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010046
Andrzej Kurekdd36c762022-08-31 13:29:38 -040047#include "hash_info.h"
48#include "mbedtls/psa_util.h"
49
Hanno Becker1ea604d2018-10-12 10:57:33 +010050#if defined(MBEDTLS_ASN1_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051static int pkcs5_parse_pbkdf2_params( const mbedtls_asn1_buf *params,
52 mbedtls_asn1_buf *salt, int *iterations,
53 int *keylen, mbedtls_md_type_t *md_type )
Paul Bakker28144de2013-06-24 19:28:55 +020054{
Janos Follath24eed8d2019-11-22 13:21:35 +000055 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_asn1_buf prf_alg_oid;
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020057 unsigned char *p = params->p;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020058 const unsigned char *end = params->p + params->len;
Paul Bakker28144de2013-06-24 19:28:55 +020059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +010061 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
62 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker28144de2013-06-24 19:28:55 +020063 /*
64 * PBKDF2-params ::= SEQUENCE {
65 * salt OCTET STRING,
66 * iterationCount INTEGER,
67 * keyLength INTEGER OPTIONAL
68 * prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
69 * }
70 *
71 */
Andrzej Kurekee3c4352019-01-10 03:10:02 -050072 if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len,
73 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010074 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakker28144de2013-06-24 19:28:55 +020075
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020076 salt->p = p;
77 p += salt->len;
Paul Bakker28144de2013-06-24 19:28:55 +020078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010080 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakker28144de2013-06-24 19:28:55 +020081
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020082 if( p == end )
Paul Bakker28144de2013-06-24 19:28:55 +020083 return( 0 );
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +020086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +010088 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakker28144de2013-06-24 19:28:55 +020089 }
90
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +020091 if( p == end )
Paul Bakker28144de2013-06-24 19:28:55 +020092 return( 0 );
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010095 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakker28144de2013-06-24 19:28:55 +020096
Antonio Quartulli12ccef22017-12-20 07:03:55 +080097 if( mbedtls_oid_get_md_hmac( &prf_alg_oid, md_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +020099
Manuel Pégourié-Gonnardedc3ab22014-06-12 17:08:27 +0200100 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100101 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
102 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker28144de2013-06-24 19:28:55 +0200103
104 return( 0 );
105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakker28144de2013-06-24 19:28:55 +0200108 const unsigned char *pwd, size_t pwdlen,
109 const unsigned char *data, size_t datalen,
110 unsigned char *output )
111{
112 int ret, iterations = 0, keylen = 0;
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200113 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
115 mbedtls_asn1_buf salt;
116 mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
Paul Bakker28144de2013-06-24 19:28:55 +0200117 unsigned char key[32], iv[32];
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200118 size_t olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_cipher_type_t cipher_alg;
121 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker28144de2013-06-24 19:28:55 +0200122
123 p = pbe_params->p;
124 end = p + pbe_params->len;
125
126 /*
127 * PBES2-params ::= SEQUENCE {
128 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
129 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
130 * }
131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100133 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
134 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200135
Andrzej Kurekee3c4352019-01-10 03:10:02 -0500136 if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid,
137 &kdf_alg_params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100138 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakker28144de2013-06-24 19:28:55 +0200139
140 // Only PBKDF2 supported at the moment
141 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
143 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200144
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200145 if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
Paul Bakker28144de2013-06-24 19:28:55 +0200146 &salt, &iterations, &keylen,
147 &md_type ) ) != 0 )
148 {
149 return( ret );
150 }
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200153 &enc_scheme_params ) ) != 0 )
154 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100155 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200156 }
Paul Bakker28144de2013-06-24 19:28:55 +0200157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
159 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
Paul Bakker28144de2013-06-24 19:28:55 +0200160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 cipher_info = mbedtls_cipher_info_from_type( cipher_alg );
Paul Bakker28144de2013-06-24 19:28:55 +0200162 if( cipher_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é-Gonnard66aca932014-06-12 13:14:55 +0200165 /*
166 * The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
167 * since it is optional and we don't know if it was set or not
168 */
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200169 keylen = cipher_info->key_bitlen / 8;
Paul Bakker28144de2013-06-24 19:28:55 +0200170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200172 enc_scheme_params.len != cipher_info->iv_size )
173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200175 }
Paul Bakker28144de2013-06-24 19:28:55 +0200176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_cipher_init( &cipher_ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200178
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200179 memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
Paul Bakker28144de2013-06-24 19:28:55 +0200180
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400181 if( ( ret = mbedtls_pkcs5_pbkdf2_hmac_ext( md_type, pwd, pwdlen, salt.p,
182 salt.len, iterations, keylen,
183 key ) ) != 0 )
Paul Bakker28144de2013-06-24 19:28:55 +0200184 {
Paul Bakker46320832013-07-03 14:01:52 +0200185 goto exit;
Paul Bakker28144de2013-06-24 19:28:55 +0200186 }
187
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200188 if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakker46320832013-07-03 14:01:52 +0200189 goto exit;
190
Andrzej Kurekee3c4352019-01-10 03:10:02 -0500191 if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen,
192 (mbedtls_operation_t) mode ) ) != 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_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
Manuel Pégourié-Gonnard90dac902014-06-12 17:04:24 +0200196 data, datalen, output, &olen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
Paul Bakker28144de2013-06-24 19:28:55 +0200198
Paul Bakker46320832013-07-03 14:01:52 +0200199exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_cipher_free( &cipher_ctx );
Paul Bakker46320832013-07-03 14:01:52 +0200201
202 return( ret );
Paul Bakker28144de2013-06-24 19:28:55 +0200203}
Marcos Del Sol Vives8a0dfac2016-11-06 12:22:25 +0100204#endif /* MBEDTLS_ASN1_PARSE_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200205
Andrzej Kurekf0004712022-08-31 19:10:42 -0400206#if defined(MBEDTLS_MD_C)
Andrzej Kurek3d0dfb92022-09-01 05:16:48 -0400207static int pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
208 const unsigned char *password,
209 size_t plen, const unsigned char *salt, size_t slen,
210 unsigned int iteration_count,
211 uint32_t key_length, unsigned char *output )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200212{
gabor-mezei-armb8513fa2020-08-24 09:53:04 +0200213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200214 unsigned int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 unsigned char md1[MBEDTLS_MD_MAX_SIZE];
216 unsigned char work[MBEDTLS_MD_MAX_SIZE];
217 unsigned char md_size = mbedtls_md_get_size( ctx->md_info );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200218 size_t use_len;
219 unsigned char *out_p = output;
220 unsigned char counter[4];
221
222 memset( counter, 0, 4 );
223 counter[3] = 1;
224
Azim Khan45b79cf2018-05-23 16:55:16 +0100225#if UINT_MAX > 0xFFFFFFFF
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200226 if( iteration_count > 0xFFFFFFFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
Azim Khan45b79cf2018-05-23 16:55:16 +0100228#endif
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200229
Jack Lloyd71657492019-09-23 19:15:54 -0400230 if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
231 return( ret );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200232 while( key_length )
233 {
234 // U1 ends up in work
235 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200237 goto cleanup;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200240 goto cleanup;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200243 goto cleanup;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200244
Jack Lloyd71657492019-09-23 19:15:54 -0400245 if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200246 goto cleanup;
Jack Lloyd71657492019-09-23 19:15:54 -0400247
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200248 memcpy( md1, work, md_size );
249
Paul Bakker66d5d072014-06-17 16:39:18 +0200250 for( i = 1; i < iteration_count; i++ )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200251 {
252 // U2 ends up in md1
253 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200255 goto cleanup;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200258 goto cleanup;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200259
Jack Lloyd71657492019-09-23 19:15:54 -0400260 if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200261 goto cleanup;
Jack Lloyd71657492019-09-23 19:15:54 -0400262
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200263 // U1 xor U2
264 //
Dave Rodgman74b345f2022-11-22 16:56:59 +0000265 mbedtls_xor( work, work, md1, md_size );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200266 }
267
268 use_len = ( key_length < md_size ) ? key_length : md_size;
269 memcpy( out_p, work, use_len );
270
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200271 key_length -= (uint32_t) use_len;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200272 out_p += use_len;
273
274 for( i = 4; i > 0; i-- )
275 if( ++counter[i - 1] != 0 )
276 break;
277 }
278
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200279cleanup:
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200280 /* Zeroise buffers to clear sensitive data from memory. */
281 mbedtls_platform_zeroize( work, MBEDTLS_MD_MAX_SIZE );
282 mbedtls_platform_zeroize( md1, MBEDTLS_MD_MAX_SIZE );
283
gabor-mezei-arm4553dd42020-08-19 14:01:03 +0200284 return( ret );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200285}
Andrzej Kurek3d0dfb92022-09-01 05:16:48 -0400286
287#if !defined(MBEDTLS_DEPRECATED_REMOVED)
288int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
289 const unsigned char *password,
290 size_t plen, const unsigned char *salt, size_t slen,
291 unsigned int iteration_count,
292 uint32_t key_length, unsigned char *output )
293{
294 return( pkcs5_pbkdf2_hmac( ctx, password, plen, salt, slen, iteration_count,
295 key_length, output ) );
296}
297#endif
Andrzej Kurekf0004712022-08-31 19:10:42 -0400298#endif /* MBEDTLS_MD_C */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200299
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400300int mbedtls_pkcs5_pbkdf2_hmac_ext( mbedtls_md_type_t md_alg,
301 const unsigned char *password,
302 size_t plen, const unsigned char *salt, size_t slen,
303 unsigned int iteration_count,
304 uint32_t key_length, unsigned char *output )
305{
306#if defined(MBEDTLS_MD_C)
307 mbedtls_md_context_t md_ctx;
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400308 const mbedtls_md_info_t *md_info = NULL;
309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400310
311 md_info = mbedtls_md_info_from_type( md_alg );
312 if( md_info == NULL )
313 return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
314
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400315 mbedtls_md_init( &md_ctx );
316
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400317 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
318 goto exit;
Andrzej Kurek3d0dfb92022-09-01 05:16:48 -0400319 ret = pkcs5_pbkdf2_hmac( &md_ctx, password, plen, salt, slen,
320 iteration_count, key_length, output );
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400321exit:
322 mbedtls_md_free( &md_ctx );
323 return( ret );
324#else
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400325 int j;
326 unsigned int i;
327 unsigned char md1[PSA_HASH_MAX_SIZE];
328 unsigned char work[PSA_HASH_MAX_SIZE];
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400329 const unsigned char md_size = mbedtls_hash_info_get_size( md_alg );
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400330 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
331
332 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek216baca2022-09-01 12:59:05 -0400333 psa_status_t status_destruction = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400334 size_t use_len, out_len;
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400335 unsigned char *out_p = output;
336 unsigned char counter[4];
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400337 mbedtls_svc_key_id_t psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400339 const psa_algorithm_t alg = PSA_ALG_HMAC( mbedtls_hash_info_psa_from_md( md_alg ) );
340 const size_t out_size = PSA_MAC_LENGTH( PSA_KEY_TYPE_HMAC, 0, alg );
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400341
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400342 memset( counter, 0, sizeof( counter ) );
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400343 counter[3] = 1;
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400344
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400345 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
346 psa_set_key_algorithm( &attributes, alg );
347 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
348
349 if( key_length == 0 )
350 return 0;
351 if( ( status = psa_import_key( &attributes,
352 password, plen,
353 &psa_hmac_key ) ) != PSA_SUCCESS )
354 {
Andrzej Kurek216baca2022-09-01 12:59:05 -0400355 return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400356 }
357
358#if UINT_MAX > 0xFFFFFFFF
359 if( iteration_count > 0xFFFFFFFF )
360 return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
361#endif
362
363 while( key_length )
364 {
365 status = psa_mac_sign_setup( &operation, psa_hmac_key,
366 PSA_ALG_HMAC( alg ) );
367 if( status != PSA_SUCCESS )
368 goto cleanup;
369 // U1 ends up in work
370 if( ( status = psa_mac_update( &operation, salt, slen ) ) != PSA_SUCCESS )
371 goto cleanup;
372
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400373 if( ( status = psa_mac_update( &operation, counter, sizeof( counter ) ) ) != PSA_SUCCESS )
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400374 goto cleanup;
375
376 if( ( status = psa_mac_sign_finish( &operation, work, out_size, &out_len ) )
377 != PSA_SUCCESS )
378 goto cleanup;
379
Andrzej Kureke3d544c2022-09-01 12:33:22 -0400380 memcpy( md1, work, out_len );
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400381
382 for( i = 1; i < iteration_count; i++ )
383 {
384 // U2 ends up in md1
385 //
386 status = psa_mac_sign_setup( &operation, psa_hmac_key,
387 PSA_ALG_HMAC( alg ) );
388 if( status != PSA_SUCCESS )
389 goto cleanup;
390 if( ( status = psa_mac_update( &operation, md1, md_size ) ) != PSA_SUCCESS )
391 goto cleanup;
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400392 if( ( status = psa_mac_sign_finish( &operation, md1, out_size, &out_len ) ) != PSA_SUCCESS )
393 goto cleanup;
394
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400395 // U1 xor U2
396 //
Dave Rodgman74b345f2022-11-22 16:56:59 +0000397 mbedtls_xor( work, work, md1, md_size );
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400398 }
399
400 use_len = ( key_length < md_size ) ? key_length : md_size;
401 memcpy( out_p, work, use_len );
402
403 key_length -= (uint32_t) use_len;
404 out_p += use_len;
405
406 for( i = 4; i > 0; i-- )
407 if( ++counter[i - 1] != 0 )
408 break;
409 }
410
411cleanup:
412 /* Zeroise buffers to clear sensitive data from memory. */
413 mbedtls_platform_zeroize( work, PSA_HASH_MAX_SIZE );
414 mbedtls_platform_zeroize( md1, PSA_HASH_MAX_SIZE );
Andrzej Kurek216baca2022-09-01 12:59:05 -0400415 status_destruction = psa_destroy_key( psa_hmac_key );
416 if( status == PSA_SUCCESS && status_destruction != PSA_SUCCESS )
417 status = status_destruction;
418 status_destruction = psa_mac_abort( &operation );
419 if( status == PSA_SUCCESS && status_destruction != PSA_SUCCESS )
420 status = status_destruction;
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400421
Andrzej Kurek216baca2022-09-01 12:59:05 -0400422 return( mbedtls_md_error_from_psa( status ) );
Andrzej Kurekf0004712022-08-31 19:10:42 -0400423#endif /* !MBEDTLS_MD_C */
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400424}
425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_SELF_TEST)
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200427
Andrzej Kureked98e952022-08-31 14:57:11 -0400428#if !defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429int mbedtls_pkcs5_self_test( int verbose )
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200430{
431 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_printf( " PBKDF2 (SHA1): skipped\n\n" );
Manuel Pégourié-Gonnard2a8afa92014-06-12 12:00:44 +0200433
434 return( 0 );
435}
436#else
437
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200438#define MAX_TESTS 6
439
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100440static const size_t plen_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000441 { 8, 8, 8, 24, 9 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200442
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100443static const unsigned char password_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200444{
445 "password",
446 "password",
447 "password",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200448 "passwordPASSWORDpassword",
449 "pass\0word",
450};
451
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100452static const size_t slen_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000453 { 4, 4, 4, 36, 5 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200454
Michał Janiszewski9aeea932018-10-30 23:00:15 +0100455static const unsigned char salt_test_data[MAX_TESTS][40] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200456{
457 "salt",
458 "salt",
459 "salt",
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200460 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
461 "sa\0lt",
462};
463
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100464static const uint32_t it_cnt_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000465 { 1, 2, 4096, 4096, 4096 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200466
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100467static const uint32_t key_len_test_data[MAX_TESTS] =
Manuel Pégourié-Gonnard73ed39d2015-03-10 15:46:30 +0000468 { 20, 20, 20, 25, 16 };
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200469
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100470static const unsigned char result_key_test_data[MAX_TESTS][32] =
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200471{
472 { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
473 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
474 0x2f, 0xe0, 0x37, 0xa6 },
475 { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
476 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
477 0xd8, 0xde, 0x89, 0x57 },
478 { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
479 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
480 0x65, 0xa4, 0x29, 0xc1 },
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200481 { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
482 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
483 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
484 0x38 },
485 { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
486 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
487};
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489int mbedtls_pkcs5_self_test( int verbose )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200490{
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200491 int ret, i;
492 unsigned char key[64];
493
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200494 for( i = 0; i < MAX_TESTS; i++ )
495 {
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100496 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_printf( " PBKDF2 (SHA1) #%d: ", i );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200498
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400499 ret = mbedtls_pkcs5_pbkdf2_hmac_ext( MBEDTLS_MD_SHA1, password_test_data[i],
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100500 plen_test_data[i], salt_test_data[i],
501 slen_test_data[i], it_cnt_test_data[i],
502 key_len_test_data[i], key );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200503 if( ret != 0 ||
Michał Janiszewskic79e92b2018-10-31 20:43:05 +0100504 memcmp( result_key_test_data[i], key, key_len_test_data[i] ) != 0 )
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200505 {
506 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_printf( "failed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200508
Paul Bakker84bbeb52014-07-01 14:53:22 +0200509 ret = 1;
510 goto exit;
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200511 }
512
513 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_printf( "passed\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200515 }
516
Paul Bakker4400ecc2016-07-19 14:41:43 +0100517 if( verbose != 0 )
518 mbedtls_printf( "\n" );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200519
Paul Bakker84bbeb52014-07-01 14:53:22 +0200520exit:
Alfred Klomp1b4eda32014-07-14 22:07:34 +0200521 return( ret );
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200522}
Andrzej Kureked98e952022-08-31 14:57:11 -0400523#endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#endif /* MBEDTLS_SELF_TEST */
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#endif /* MBEDTLS_PKCS5_C */