2013-06-24 19:26:38 +02:00
|
|
|
/**
|
|
|
|
* \file pkcs5.c
|
|
|
|
*
|
|
|
|
* \brief PKCS#5 functions
|
|
|
|
*
|
|
|
|
* \author Mathias Olsson <mathias@kompetensum.com>
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-06-24 19:26:38 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-06-24 19:26:38 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-06-24 19:26:38 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2013-06-24 19:26:38 +02:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* PKCS#5 includes PBKDF2 and more
|
|
|
|
*
|
|
|
|
* http://tools.ietf.org/html/rfc2898 (Specification)
|
|
|
|
* http://tools.ietf.org/html/rfc6070 (Test vectors)
|
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PKCS5_C)
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pkcs5.h"
|
2019-11-22 14:21:35 +01:00
|
|
|
#include "mbedtls/error.h"
|
2016-11-06 12:22:25 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ASN1_PARSE_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/asn1.h"
|
|
|
|
#include "mbedtls/cipher.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2018-03-27 21:53:07 +02:00
|
|
|
#endif /* MBEDTLS_ASN1_PARSE_C */
|
|
|
|
|
|
|
|
#include <string.h>
|
2015-02-06 14:43:58 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2014-02-01 22:50:26 +01:00
|
|
|
|
2022-08-31 19:29:38 +02:00
|
|
|
#include "hash_info.h"
|
|
|
|
#include "mbedtls/psa_util.h"
|
|
|
|
|
2018-10-12 11:57:33 +02:00
|
|
|
#if defined(MBEDTLS_ASN1_PARSE_C)
|
2015-04-08 12:49:31 +02:00
|
|
|
static int pkcs5_parse_pbkdf2_params( const mbedtls_asn1_buf *params,
|
|
|
|
mbedtls_asn1_buf *salt, int *iterations,
|
|
|
|
int *keylen, mbedtls_md_type_t *md_type )
|
2013-06-24 19:28:55 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_buf prf_alg_oid;
|
2014-06-12 17:08:27 +02:00
|
|
|
unsigned char *p = params->p;
|
2013-06-29 12:16:17 +02:00
|
|
|
const unsigned char *end = params->p + params->len;
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
|
2021-04-14 13:12:09 +02:00
|
|
|
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
|
2013-06-24 19:28:55 +02:00
|
|
|
/*
|
|
|
|
* PBKDF2-params ::= SEQUENCE {
|
|
|
|
* salt OCTET STRING,
|
|
|
|
* iterationCount INTEGER,
|
|
|
|
* keyLength INTEGER OPTIONAL
|
|
|
|
* prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
2019-01-10 09:10:02 +01:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len,
|
|
|
|
MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
|
2021-04-14 13:12:09 +02:00
|
|
|
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2014-06-12 17:08:27 +02:00
|
|
|
salt->p = p;
|
|
|
|
p += salt->len;
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 )
|
2021-04-14 13:12:09 +02:00
|
|
|
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2014-06-12 17:08:27 +02:00
|
|
|
if( p == end )
|
2013-06-24 19:28:55 +02:00
|
|
|
return( 0 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 )
|
2013-06-24 19:28:55 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
2021-04-14 13:12:09 +02:00
|
|
|
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
|
2013-06-24 19:28:55 +02:00
|
|
|
}
|
|
|
|
|
2014-06-12 17:08:27 +02:00
|
|
|
if( p == end )
|
2013-06-24 19:28:55 +02:00
|
|
|
return( 0 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
|
2021-04-14 13:12:09 +02:00
|
|
|
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2017-12-20 00:03:55 +01:00
|
|
|
if( mbedtls_oid_get_md_hmac( &prf_alg_oid, md_type ) != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2014-06-12 17:08:27 +02:00
|
|
|
if( p != end )
|
2021-04-14 13:12:09 +02:00
|
|
|
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
|
2013-06-24 19:28:55 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
|
2013-06-24 19:28:55 +02:00
|
|
|
const unsigned char *pwd, size_t pwdlen,
|
|
|
|
const unsigned char *data, size_t datalen,
|
|
|
|
unsigned char *output )
|
|
|
|
{
|
|
|
|
int ret, iterations = 0, keylen = 0;
|
2013-06-29 12:16:17 +02:00
|
|
|
unsigned char *p, *end;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
|
|
|
|
mbedtls_asn1_buf salt;
|
|
|
|
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
|
2013-06-24 19:28:55 +02:00
|
|
|
unsigned char key[32], iv[32];
|
2013-06-29 12:16:17 +02:00
|
|
|
size_t olen = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
mbedtls_cipher_type_t cipher_alg;
|
|
|
|
mbedtls_cipher_context_t cipher_ctx;
|
2013-06-24 19:28:55 +02:00
|
|
|
|
|
|
|
p = pbe_params->p;
|
|
|
|
end = p + pbe_params->len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PBES2-params ::= SEQUENCE {
|
|
|
|
* keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
|
|
|
|
* encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
|
|
|
|
* }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
|
2021-04-14 13:12:09 +02:00
|
|
|
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2019-01-10 09:10:02 +01:00
|
|
|
if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid,
|
|
|
|
&kdf_alg_params ) ) != 0 )
|
2021-04-14 13:12:09 +02:00
|
|
|
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
|
2013-06-24 19:28:55 +02:00
|
|
|
|
|
|
|
// Only PBKDF2 supported at the moment
|
|
|
|
//
|
2015-04-08 12:49:31 +02:00
|
|
|
if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2013-06-29 12:16:17 +02:00
|
|
|
if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
|
2013-06-24 19:28:55 +02:00
|
|
|
&salt, &iterations, &keylen,
|
|
|
|
&md_type ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
|
2014-05-01 14:18:25 +02:00
|
|
|
&enc_scheme_params ) ) != 0 )
|
|
|
|
{
|
2021-04-14 13:12:09 +02:00
|
|
|
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) );
|
2014-05-01 14:18:25 +02:00
|
|
|
}
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type( cipher_alg );
|
2013-06-24 19:28:55 +02:00
|
|
|
if( cipher_info == NULL )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2014-06-12 13:14:55 +02:00
|
|
|
/*
|
|
|
|
* The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
|
|
|
|
* since it is optional and we don't know if it was set or not
|
|
|
|
*/
|
2015-06-18 15:28:12 +02:00
|
|
|
keylen = cipher_info->key_bitlen / 8;
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
|
2013-06-29 12:16:17 +02:00
|
|
|
enc_scheme_params.len != cipher_info->iv_size )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
|
2013-06-29 12:16:17 +02:00
|
|
|
}
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_cipher_init( &cipher_ctx );
|
2014-07-01 14:53:22 +02:00
|
|
|
|
2013-06-29 12:16:17 +02:00
|
|
|
memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2022-08-31 19:29:38 +02:00
|
|
|
if( ( ret = mbedtls_pkcs5_pbkdf2_hmac_ext( md_type, pwd, pwdlen, salt.p,
|
|
|
|
salt.len, iterations, keylen,
|
|
|
|
key ) ) != 0 )
|
2013-06-24 19:28:55 +02:00
|
|
|
{
|
2013-07-03 14:01:52 +02:00
|
|
|
goto exit;
|
2013-06-24 19:28:55 +02:00
|
|
|
}
|
|
|
|
|
2015-05-14 13:51:45 +02:00
|
|
|
if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
|
2013-07-03 14:01:52 +02:00
|
|
|
goto exit;
|
|
|
|
|
2019-01-10 09:10:02 +01:00
|
|
|
if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen,
|
|
|
|
(mbedtls_operation_t) mode ) ) != 0 )
|
2013-07-03 14:01:52 +02:00
|
|
|
goto exit;
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
|
2014-06-12 17:04:24 +02:00
|
|
|
data, datalen, output, &olen ) ) != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
|
2013-06-24 19:28:55 +02:00
|
|
|
|
2013-07-03 14:01:52 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_cipher_free( &cipher_ctx );
|
2013-07-03 14:01:52 +02:00
|
|
|
|
|
|
|
return( ret );
|
2013-06-24 19:28:55 +02:00
|
|
|
}
|
2016-11-06 12:22:25 +01:00
|
|
|
#endif /* MBEDTLS_ASN1_PARSE_C */
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2022-09-01 01:10:42 +02:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2022-09-01 11:16:48 +02:00
|
|
|
static int pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
|
|
|
|
const unsigned char *password,
|
|
|
|
size_t plen, const unsigned char *salt, size_t slen,
|
|
|
|
unsigned int iteration_count,
|
|
|
|
uint32_t key_length, unsigned char *output )
|
2013-06-24 19:26:38 +02:00
|
|
|
{
|
2020-08-24 09:53:04 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
int j;
|
2013-06-24 19:26:38 +02:00
|
|
|
unsigned int i;
|
2015-04-08 12:49:31 +02:00
|
|
|
unsigned char md1[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
unsigned char work[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
unsigned char md_size = mbedtls_md_get_size( ctx->md_info );
|
2013-06-24 19:26:38 +02:00
|
|
|
size_t use_len;
|
|
|
|
unsigned char *out_p = output;
|
|
|
|
unsigned char counter[4];
|
|
|
|
|
|
|
|
memset( counter, 0, 4 );
|
|
|
|
counter[3] = 1;
|
|
|
|
|
2018-05-23 17:55:16 +02:00
|
|
|
#if UINT_MAX > 0xFFFFFFFF
|
2013-06-24 19:26:38 +02:00
|
|
|
if( iteration_count > 0xFFFFFFFF )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
|
2018-05-23 17:55:16 +02:00
|
|
|
#endif
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2019-09-24 01:15:54 +02:00
|
|
|
if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
|
|
|
|
return( ret );
|
2013-06-24 19:26:38 +02:00
|
|
|
while( key_length )
|
|
|
|
{
|
|
|
|
// U1 ends up in work
|
|
|
|
//
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
|
2020-08-19 14:01:03 +02:00
|
|
|
goto cleanup;
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 )
|
2020-08-19 14:01:03 +02:00
|
|
|
goto cleanup;
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
|
2020-08-19 14:01:03 +02:00
|
|
|
goto cleanup;
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2019-09-24 01:15:54 +02:00
|
|
|
if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
|
2020-08-19 14:01:03 +02:00
|
|
|
goto cleanup;
|
2019-09-24 01:15:54 +02:00
|
|
|
|
2013-06-24 19:26:38 +02:00
|
|
|
memcpy( md1, work, md_size );
|
|
|
|
|
2014-06-17 16:39:18 +02:00
|
|
|
for( i = 1; i < iteration_count; i++ )
|
2013-06-24 19:26:38 +02:00
|
|
|
{
|
|
|
|
// U2 ends up in md1
|
|
|
|
//
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
|
2020-08-19 14:01:03 +02:00
|
|
|
goto cleanup;
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
|
2020-08-19 14:01:03 +02:00
|
|
|
goto cleanup;
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2019-09-24 01:15:54 +02:00
|
|
|
if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
|
2020-08-19 14:01:03 +02:00
|
|
|
goto cleanup;
|
2019-09-24 01:15:54 +02:00
|
|
|
|
2013-06-24 19:26:38 +02:00
|
|
|
// U1 xor U2
|
|
|
|
//
|
|
|
|
for( j = 0; j < md_size; j++ )
|
|
|
|
work[j] ^= md1[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
use_len = ( key_length < md_size ) ? key_length : md_size;
|
|
|
|
memcpy( out_p, work, use_len );
|
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
key_length -= (uint32_t) use_len;
|
2013-06-24 19:26:38 +02:00
|
|
|
out_p += use_len;
|
|
|
|
|
|
|
|
for( i = 4; i > 0; i-- )
|
|
|
|
if( ++counter[i - 1] != 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-08-19 14:01:03 +02:00
|
|
|
cleanup:
|
2020-07-30 16:41:25 +02:00
|
|
|
/* Zeroise buffers to clear sensitive data from memory. */
|
|
|
|
mbedtls_platform_zeroize( work, MBEDTLS_MD_MAX_SIZE );
|
|
|
|
mbedtls_platform_zeroize( md1, MBEDTLS_MD_MAX_SIZE );
|
|
|
|
|
2020-08-19 14:01:03 +02:00
|
|
|
return( ret );
|
2013-06-24 19:26:38 +02:00
|
|
|
}
|
2022-09-01 11:16:48 +02:00
|
|
|
|
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
|
|
|
int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
|
|
|
|
const unsigned char *password,
|
|
|
|
size_t plen, const unsigned char *salt, size_t slen,
|
|
|
|
unsigned int iteration_count,
|
|
|
|
uint32_t key_length, unsigned char *output )
|
|
|
|
{
|
|
|
|
return( pkcs5_pbkdf2_hmac( ctx, password, plen, salt, slen, iteration_count,
|
|
|
|
key_length, output ) );
|
|
|
|
}
|
|
|
|
#endif
|
2022-09-01 01:10:42 +02:00
|
|
|
#endif /* MBEDTLS_MD_C */
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2022-08-31 19:29:38 +02:00
|
|
|
int mbedtls_pkcs5_pbkdf2_hmac_ext( mbedtls_md_type_t md_alg,
|
|
|
|
const unsigned char *password,
|
|
|
|
size_t plen, const unsigned char *salt, size_t slen,
|
|
|
|
unsigned int iteration_count,
|
|
|
|
uint32_t key_length, unsigned char *output )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_MD_C)
|
|
|
|
mbedtls_md_context_t md_ctx;
|
2022-09-01 18:33:22 +02:00
|
|
|
const mbedtls_md_info_t *md_info = NULL;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-08-31 19:29:38 +02:00
|
|
|
|
|
|
|
md_info = mbedtls_md_info_from_type( md_alg );
|
|
|
|
if( md_info == NULL )
|
|
|
|
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
|
|
|
|
2022-09-01 18:33:22 +02:00
|
|
|
mbedtls_md_init( &md_ctx );
|
|
|
|
|
2022-08-31 19:29:38 +02:00
|
|
|
if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
|
|
|
|
goto exit;
|
2022-09-01 11:16:48 +02:00
|
|
|
ret = pkcs5_pbkdf2_hmac( &md_ctx, password, plen, salt, slen,
|
|
|
|
iteration_count, key_length, output );
|
2022-08-31 19:29:38 +02:00
|
|
|
exit:
|
|
|
|
mbedtls_md_free( &md_ctx );
|
|
|
|
return( ret );
|
|
|
|
#else
|
|
|
|
int j;
|
|
|
|
unsigned int i;
|
|
|
|
unsigned char md1[PSA_HASH_MAX_SIZE];
|
|
|
|
unsigned char work[PSA_HASH_MAX_SIZE];
|
2022-09-01 18:33:22 +02:00
|
|
|
const unsigned char md_size = mbedtls_hash_info_get_size( md_alg );
|
2022-08-31 19:29:38 +02:00
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-09-01 18:59:05 +02:00
|
|
|
psa_status_t status_destruction = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-09-01 18:33:22 +02:00
|
|
|
size_t use_len, out_len;
|
2022-08-31 19:29:38 +02:00
|
|
|
unsigned char *out_p = output;
|
|
|
|
unsigned char counter[4];
|
2022-09-01 18:33:22 +02:00
|
|
|
mbedtls_svc_key_id_t psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2022-08-31 19:29:38 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2022-09-01 18:33:22 +02:00
|
|
|
const psa_algorithm_t alg = PSA_ALG_HMAC( mbedtls_hash_info_psa_from_md( md_alg ) );
|
|
|
|
const size_t out_size = PSA_MAC_LENGTH( PSA_KEY_TYPE_HMAC, 0, alg );
|
2022-08-31 19:29:38 +02:00
|
|
|
|
2022-09-01 18:33:22 +02:00
|
|
|
memset( counter, 0, sizeof( counter ) );
|
2022-08-31 19:29:38 +02:00
|
|
|
counter[3] = 1;
|
2022-09-01 18:33:22 +02:00
|
|
|
|
2022-08-31 19:29:38 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
|
|
|
|
|
|
|
|
if( key_length == 0 )
|
|
|
|
return 0;
|
|
|
|
if( ( status = psa_import_key( &attributes,
|
|
|
|
password, plen,
|
|
|
|
&psa_hmac_key ) ) != PSA_SUCCESS )
|
|
|
|
{
|
2022-09-01 18:59:05 +02:00
|
|
|
return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
|
2022-08-31 19:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if UINT_MAX > 0xFFFFFFFF
|
|
|
|
if( iteration_count > 0xFFFFFFFF )
|
|
|
|
return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while( key_length )
|
|
|
|
{
|
|
|
|
status = psa_mac_sign_setup( &operation, psa_hmac_key,
|
|
|
|
PSA_ALG_HMAC( alg ) );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
// U1 ends up in work
|
|
|
|
if( ( status = psa_mac_update( &operation, salt, slen ) ) != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
|
2022-09-01 18:33:22 +02:00
|
|
|
if( ( status = psa_mac_update( &operation, counter, sizeof( counter ) ) ) != PSA_SUCCESS )
|
2022-08-31 19:29:38 +02:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if( ( status = psa_mac_sign_finish( &operation, work, out_size, &out_len ) )
|
|
|
|
!= PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
|
2022-09-01 18:33:22 +02:00
|
|
|
memcpy( md1, work, out_len );
|
2022-08-31 19:29:38 +02:00
|
|
|
|
|
|
|
for( i = 1; i < iteration_count; i++ )
|
|
|
|
{
|
|
|
|
// U2 ends up in md1
|
|
|
|
//
|
|
|
|
status = psa_mac_sign_setup( &operation, psa_hmac_key,
|
|
|
|
PSA_ALG_HMAC( alg ) );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
if( ( status = psa_mac_update( &operation, md1, md_size ) ) != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
if( ( status = psa_mac_sign_finish( &operation, md1, out_size, &out_len ) ) != PSA_SUCCESS )
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
// U1 xor U2
|
|
|
|
//
|
|
|
|
for( j = 0; j < md_size; j++ )
|
|
|
|
work[j] ^= md1[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
use_len = ( key_length < md_size ) ? key_length : md_size;
|
|
|
|
memcpy( out_p, work, use_len );
|
|
|
|
|
|
|
|
key_length -= (uint32_t) use_len;
|
|
|
|
out_p += use_len;
|
|
|
|
|
|
|
|
for( i = 4; i > 0; i-- )
|
|
|
|
if( ++counter[i - 1] != 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
/* Zeroise buffers to clear sensitive data from memory. */
|
|
|
|
mbedtls_platform_zeroize( work, PSA_HASH_MAX_SIZE );
|
|
|
|
mbedtls_platform_zeroize( md1, PSA_HASH_MAX_SIZE );
|
2022-09-01 18:59:05 +02:00
|
|
|
status_destruction = psa_destroy_key( psa_hmac_key );
|
|
|
|
if( status == PSA_SUCCESS && status_destruction != PSA_SUCCESS )
|
|
|
|
status = status_destruction;
|
|
|
|
status_destruction = psa_mac_abort( &operation );
|
|
|
|
if( status == PSA_SUCCESS && status_destruction != PSA_SUCCESS )
|
|
|
|
status = status_destruction;
|
|
|
|
|
|
|
|
return( mbedtls_md_error_from_psa( status ) );
|
2022-09-01 01:10:42 +02:00
|
|
|
#endif /* !MBEDTLS_MD_C */
|
2022-08-31 19:29:38 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2022-08-31 20:57:11 +02:00
|
|
|
#if !defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA)
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_pkcs5_self_test( int verbose )
|
2014-06-12 12:00:44 +02:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " PBKDF2 (SHA1): skipped\n\n" );
|
2014-06-12 12:00:44 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2013-06-24 19:26:38 +02:00
|
|
|
#define MAX_TESTS 6
|
|
|
|
|
2018-10-30 23:00:15 +01:00
|
|
|
static const size_t plen_test_data[MAX_TESTS] =
|
2015-03-10 16:46:30 +01:00
|
|
|
{ 8, 8, 8, 24, 9 };
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2018-10-30 23:00:15 +01:00
|
|
|
static const unsigned char password_test_data[MAX_TESTS][32] =
|
2013-06-24 19:26:38 +02:00
|
|
|
{
|
|
|
|
"password",
|
|
|
|
"password",
|
|
|
|
"password",
|
|
|
|
"passwordPASSWORDpassword",
|
|
|
|
"pass\0word",
|
|
|
|
};
|
|
|
|
|
2018-10-30 23:00:15 +01:00
|
|
|
static const size_t slen_test_data[MAX_TESTS] =
|
2015-03-10 16:46:30 +01:00
|
|
|
{ 4, 4, 4, 36, 5 };
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2018-10-30 23:00:15 +01:00
|
|
|
static const unsigned char salt_test_data[MAX_TESTS][40] =
|
2013-06-24 19:26:38 +02:00
|
|
|
{
|
|
|
|
"salt",
|
|
|
|
"salt",
|
|
|
|
"salt",
|
|
|
|
"saltSALTsaltSALTsaltSALTsaltSALTsalt",
|
|
|
|
"sa\0lt",
|
|
|
|
};
|
|
|
|
|
2018-10-31 20:43:05 +01:00
|
|
|
static const uint32_t it_cnt_test_data[MAX_TESTS] =
|
2015-03-10 16:46:30 +01:00
|
|
|
{ 1, 2, 4096, 4096, 4096 };
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2018-10-31 20:43:05 +01:00
|
|
|
static const uint32_t key_len_test_data[MAX_TESTS] =
|
2015-03-10 16:46:30 +01:00
|
|
|
{ 20, 20, 20, 25, 16 };
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2018-10-31 20:43:05 +01:00
|
|
|
static const unsigned char result_key_test_data[MAX_TESTS][32] =
|
2013-06-24 19:26:38 +02:00
|
|
|
{
|
|
|
|
{ 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
|
|
|
|
0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
|
|
|
|
0x2f, 0xe0, 0x37, 0xa6 },
|
|
|
|
{ 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
|
|
|
|
0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
|
|
|
|
0xd8, 0xde, 0x89, 0x57 },
|
|
|
|
{ 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
|
|
|
|
0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
|
|
|
|
0x65, 0xa4, 0x29, 0xc1 },
|
|
|
|
{ 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
|
|
|
|
0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
|
|
|
|
0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
|
|
|
|
0x38 },
|
|
|
|
{ 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
|
|
|
|
0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
|
|
|
|
};
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_pkcs5_self_test( int verbose )
|
2013-06-24 19:26:38 +02:00
|
|
|
{
|
|
|
|
int ret, i;
|
|
|
|
unsigned char key[64];
|
|
|
|
|
|
|
|
for( i = 0; i < MAX_TESTS; i++ )
|
|
|
|
{
|
2014-03-27 20:12:44 +01:00
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " PBKDF2 (SHA1) #%d: ", i );
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2022-08-31 19:29:38 +02:00
|
|
|
ret = mbedtls_pkcs5_pbkdf2_hmac_ext( MBEDTLS_MD_SHA1, password_test_data[i],
|
2018-10-31 20:43:05 +01:00
|
|
|
plen_test_data[i], salt_test_data[i],
|
|
|
|
slen_test_data[i], it_cnt_test_data[i],
|
|
|
|
key_len_test_data[i], key );
|
2013-06-24 19:26:38 +02:00
|
|
|
if( ret != 0 ||
|
2018-10-31 20:43:05 +01:00
|
|
|
memcmp( result_key_test_data[i], key, key_len_test_data[i] ) != 0 )
|
2013-06-24 19:26:38 +02:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "failed\n" );
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2014-07-01 14:53:22 +02:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2013-06-24 19:26:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "passed\n" );
|
2013-06-24 19:26:38 +02:00
|
|
|
}
|
|
|
|
|
2016-07-19 15:41:43 +02:00
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "\n" );
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2014-07-01 14:53:22 +02:00
|
|
|
exit:
|
2014-07-14 22:07:34 +02:00
|
|
|
return( ret );
|
2013-06-24 19:26:38 +02:00
|
|
|
}
|
2022-08-31 20:57:11 +02:00
|
|
|
#endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA */
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2013-06-24 19:26:38 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PKCS5_C */
|