| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file pkcs5.c | 
|  | 3 | * | 
|  | 4 | * \brief PKCS#5 functions | 
|  | 5 | * | 
|  | 6 | * \author Mathias Olsson <mathias@kompetensum.com> | 
|  | 7 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 8 | *  Copyright The Mbed TLS Contributors | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 9 | *  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 Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 22 | */ | 
|  | 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 Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 30 | #include "common.h" | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 31 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_PKCS5_C) | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 33 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/pkcs5.h" | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 35 | #include "mbedtls/error.h" | 
| Marcos Del Sol Vives | 8a0dfac | 2016-11-06 12:22:25 +0100 | [diff] [blame] | 36 |  | 
|  | 37 | #if defined(MBEDTLS_ASN1_PARSE_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/asn1.h" | 
|  | 39 | #include "mbedtls/cipher.h" | 
|  | 40 | #include "mbedtls/oid.h" | 
| Andres Amaya Garcia | af9a486 | 2018-03-27 20:53:07 +0100 | [diff] [blame] | 41 | #endif /* MBEDTLS_ASN1_PARSE_C */ | 
|  | 42 |  | 
|  | 43 | #include <string.h> | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 44 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 45 | #include "mbedtls/platform.h" | 
| Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 46 |  | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 47 | #include "hash_info.h" | 
|  | 48 | #include "mbedtls/psa_util.h" | 
|  | 49 |  | 
| Hanno Becker | 1ea604d | 2018-10-12 10:57:33 +0100 | [diff] [blame] | 50 | #if defined(MBEDTLS_ASN1_PARSE_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | static 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 Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 54 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 55 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | mbedtls_asn1_buf prf_alg_oid; | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 57 | unsigned char *p = params->p; | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 58 | const unsigned char *end = params->p + params->len; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 59 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 61 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, | 
|  | 62 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 63 | /* | 
|  | 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 Kurek | ee3c435 | 2019-01-10 03:10:02 -0500 | [diff] [blame] | 72 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len, | 
|  | 73 | MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 74 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 75 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 76 | salt->p = p; | 
|  | 77 | p += salt->len; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 78 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 ) | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 80 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 81 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 82 | if( p == end ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 83 | return( 0 ); | 
|  | 84 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 86 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 88 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 91 | if( p == end ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 92 | return( 0 ); | 
|  | 93 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 ) | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 95 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 96 |  | 
| Antonio Quartulli | 12ccef2 | 2017-12-20 07:03:55 +0800 | [diff] [blame] | 97 | if( mbedtls_oid_get_md_hmac( &prf_alg_oid, md_type ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 99 |  | 
| Manuel Pégourié-Gonnard | edc3ab2 | 2014-06-12 17:08:27 +0200 | [diff] [blame] | 100 | if( p != end ) | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 101 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, | 
|  | 102 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 103 |  | 
|  | 104 | return( 0 ); | 
|  | 105 | } | 
|  | 106 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode, | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 108 | 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 Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 113 | unsigned char *p, *end; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | 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 Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 117 | unsigned char key[32], iv[32]; | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 118 | size_t olen = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | const mbedtls_cipher_info_t *cipher_info; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | mbedtls_cipher_type_t cipher_alg; | 
|  | 121 | mbedtls_cipher_context_t cipher_ctx; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 122 |  | 
|  | 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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | if( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 133 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, | 
|  | 134 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 135 |  | 
| Andrzej Kurek | ee3c435 | 2019-01-10 03:10:02 -0500 | [diff] [blame] | 136 | if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid, | 
|  | 137 | &kdf_alg_params ) ) != 0 ) | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 138 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 139 |  | 
|  | 140 | // Only PBKDF2 supported at the moment | 
|  | 141 | // | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 ) | 
|  | 143 | return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 144 |  | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 145 | if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params, | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 146 | &salt, &iterations, &keylen, | 
|  | 147 | &md_type ) ) != 0 ) | 
|  | 148 | { | 
|  | 149 | return( ret ); | 
|  | 150 | } | 
|  | 151 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid, | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 153 | &enc_scheme_params ) ) != 0 ) | 
|  | 154 | { | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 155 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret ) ); | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 156 | } | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 157 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 ) | 
|  | 159 | return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 160 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | cipher_info = mbedtls_cipher_info_from_type( cipher_alg ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 162 | if( cipher_info == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 164 |  | 
| Manuel Pégourié-Gonnard | 66aca93 | 2014-06-12 13:14:55 +0200 | [diff] [blame] | 165 | /* | 
|  | 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é-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 169 | keylen = cipher_info->key_bitlen / 8; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 170 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING || | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 172 | enc_scheme_params.len != cipher_info->iv_size ) | 
|  | 173 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT ); | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 175 | } | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 176 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | mbedtls_cipher_init( &cipher_ctx ); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 178 |  | 
| Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 179 | memcpy( iv, enc_scheme_params.p, enc_scheme_params.len ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 180 |  | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 181 | if( ( ret = mbedtls_pkcs5_pbkdf2_hmac_ext( md_type, pwd, pwdlen, salt.p, | 
|  | 182 | salt.len, iterations, keylen, | 
|  | 183 | key ) ) != 0 ) | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 184 | { | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 185 | goto exit; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 188 | if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 ) | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 189 | goto exit; | 
|  | 190 |  | 
| Andrzej Kurek | ee3c435 | 2019-01-10 03:10:02 -0500 | [diff] [blame] | 191 | if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, | 
|  | 192 | (mbedtls_operation_t) mode ) ) != 0 ) | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 193 | goto exit; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 194 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | if( ( ret = mbedtls_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len, | 
| Manuel Pégourié-Gonnard | 90dac90 | 2014-06-12 17:04:24 +0200 | [diff] [blame] | 196 | data, datalen, output, &olen ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH; | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 198 |  | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 199 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | mbedtls_cipher_free( &cipher_ctx ); | 
| Paul Bakker | 4632083 | 2013-07-03 14:01:52 +0200 | [diff] [blame] | 201 |  | 
|  | 202 | return( ret ); | 
| Paul Bakker | 28144de | 2013-06-24 19:28:55 +0200 | [diff] [blame] | 203 | } | 
| Marcos Del Sol Vives | 8a0dfac | 2016-11-06 12:22:25 +0100 | [diff] [blame] | 204 | #endif /* MBEDTLS_ASN1_PARSE_C */ | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 205 |  | 
| Andrzej Kurek | f000471 | 2022-08-31 19:10:42 -0400 | [diff] [blame] | 206 | #if defined(MBEDTLS_MD_C) | 
| Andrzej Kurek | 3d0dfb9 | 2022-09-01 05:16:48 -0400 | [diff] [blame] | 207 | static 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 Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 212 | { | 
| gabor-mezei-arm | b8513fa | 2020-08-24 09:53:04 +0200 | [diff] [blame] | 213 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 214 | int j; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 215 | unsigned int i; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | unsigned char md1[MBEDTLS_MD_MAX_SIZE]; | 
|  | 217 | unsigned char work[MBEDTLS_MD_MAX_SIZE]; | 
|  | 218 | unsigned char md_size = mbedtls_md_get_size( ctx->md_info ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 219 | size_t use_len; | 
|  | 220 | unsigned char *out_p = output; | 
|  | 221 | unsigned char counter[4]; | 
|  | 222 |  | 
|  | 223 | memset( counter, 0, 4 ); | 
|  | 224 | counter[3] = 1; | 
|  | 225 |  | 
| Azim Khan | 45b79cf | 2018-05-23 16:55:16 +0100 | [diff] [blame] | 226 | #if UINT_MAX > 0xFFFFFFFF | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 227 | if( iteration_count > 0xFFFFFFFF ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA ); | 
| Azim Khan | 45b79cf | 2018-05-23 16:55:16 +0100 | [diff] [blame] | 229 | #endif | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 230 |  | 
| Jack Lloyd | 7165749 | 2019-09-23 19:15:54 -0400 | [diff] [blame] | 231 | if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 ) | 
|  | 232 | return( ret ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 233 | while( key_length ) | 
|  | 234 | { | 
|  | 235 | // U1 ends up in work | 
|  | 236 | // | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 ) | 
| gabor-mezei-arm | 4553dd4 | 2020-08-19 14:01:03 +0200 | [diff] [blame] | 238 | goto cleanup; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 239 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 ) | 
| gabor-mezei-arm | 4553dd4 | 2020-08-19 14:01:03 +0200 | [diff] [blame] | 241 | goto cleanup; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 242 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 ) | 
| gabor-mezei-arm | 4553dd4 | 2020-08-19 14:01:03 +0200 | [diff] [blame] | 244 | goto cleanup; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 245 |  | 
| Jack Lloyd | 7165749 | 2019-09-23 19:15:54 -0400 | [diff] [blame] | 246 | if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 ) | 
| gabor-mezei-arm | 4553dd4 | 2020-08-19 14:01:03 +0200 | [diff] [blame] | 247 | goto cleanup; | 
| Jack Lloyd | 7165749 | 2019-09-23 19:15:54 -0400 | [diff] [blame] | 248 |  | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 249 | memcpy( md1, work, md_size ); | 
|  | 250 |  | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 251 | for( i = 1; i < iteration_count; i++ ) | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 252 | { | 
|  | 253 | // U2 ends up in md1 | 
|  | 254 | // | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 ) | 
| gabor-mezei-arm | 4553dd4 | 2020-08-19 14:01:03 +0200 | [diff] [blame] | 256 | goto cleanup; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 257 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 258 | if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 ) | 
| gabor-mezei-arm | 4553dd4 | 2020-08-19 14:01:03 +0200 | [diff] [blame] | 259 | goto cleanup; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 260 |  | 
| Jack Lloyd | 7165749 | 2019-09-23 19:15:54 -0400 | [diff] [blame] | 261 | if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 ) | 
| gabor-mezei-arm | 4553dd4 | 2020-08-19 14:01:03 +0200 | [diff] [blame] | 262 | goto cleanup; | 
| Jack Lloyd | 7165749 | 2019-09-23 19:15:54 -0400 | [diff] [blame] | 263 |  | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 264 | // U1 xor U2 | 
|  | 265 | // | 
|  | 266 | for( j = 0; j < md_size; j++ ) | 
|  | 267 | work[j] ^= md1[j]; | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | use_len = ( key_length < md_size ) ? key_length : md_size; | 
|  | 271 | memcpy( out_p, work, use_len ); | 
|  | 272 |  | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 273 | key_length -= (uint32_t) use_len; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 274 | out_p += use_len; | 
|  | 275 |  | 
|  | 276 | for( i = 4; i > 0; i-- ) | 
|  | 277 | if( ++counter[i - 1] != 0 ) | 
|  | 278 | break; | 
|  | 279 | } | 
|  | 280 |  | 
| gabor-mezei-arm | 4553dd4 | 2020-08-19 14:01:03 +0200 | [diff] [blame] | 281 | cleanup: | 
| gabor-mezei-arm | 76749ae | 2020-07-30 16:41:25 +0200 | [diff] [blame] | 282 | /* Zeroise buffers to clear sensitive data from memory. */ | 
|  | 283 | mbedtls_platform_zeroize( work, MBEDTLS_MD_MAX_SIZE ); | 
|  | 284 | mbedtls_platform_zeroize( md1, MBEDTLS_MD_MAX_SIZE ); | 
|  | 285 |  | 
| gabor-mezei-arm | 4553dd4 | 2020-08-19 14:01:03 +0200 | [diff] [blame] | 286 | return( ret ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 287 | } | 
| Andrzej Kurek | 3d0dfb9 | 2022-09-01 05:16:48 -0400 | [diff] [blame] | 288 |  | 
|  | 289 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 290 | int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, | 
|  | 291 | const unsigned char *password, | 
|  | 292 | size_t plen, const unsigned char *salt, size_t slen, | 
|  | 293 | unsigned int iteration_count, | 
|  | 294 | uint32_t key_length, unsigned char *output ) | 
|  | 295 | { | 
|  | 296 | return( pkcs5_pbkdf2_hmac( ctx, password, plen, salt, slen, iteration_count, | 
|  | 297 | key_length, output ) ); | 
|  | 298 | } | 
|  | 299 | #endif | 
| Andrzej Kurek | f000471 | 2022-08-31 19:10:42 -0400 | [diff] [blame] | 300 | #endif /* MBEDTLS_MD_C */ | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 301 |  | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 302 | int mbedtls_pkcs5_pbkdf2_hmac_ext( mbedtls_md_type_t md_alg, | 
|  | 303 | const unsigned char *password, | 
|  | 304 | size_t plen, const unsigned char *salt, size_t slen, | 
|  | 305 | unsigned int iteration_count, | 
|  | 306 | uint32_t key_length, unsigned char *output ) | 
|  | 307 | { | 
|  | 308 | #if defined(MBEDTLS_MD_C) | 
|  | 309 | mbedtls_md_context_t md_ctx; | 
| Andrzej Kurek | e3d544c | 2022-09-01 12:33:22 -0400 | [diff] [blame] | 310 | const mbedtls_md_info_t *md_info = NULL; | 
|  | 311 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 312 |  | 
|  | 313 | md_info = mbedtls_md_info_from_type( md_alg ); | 
|  | 314 | if( md_info == NULL ) | 
|  | 315 | return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE ); | 
|  | 316 |  | 
| Andrzej Kurek | e3d544c | 2022-09-01 12:33:22 -0400 | [diff] [blame] | 317 | mbedtls_md_init( &md_ctx ); | 
|  | 318 |  | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 319 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) | 
|  | 320 | goto exit; | 
| Andrzej Kurek | 3d0dfb9 | 2022-09-01 05:16:48 -0400 | [diff] [blame] | 321 | ret = pkcs5_pbkdf2_hmac( &md_ctx, password, plen, salt, slen, | 
|  | 322 | iteration_count, key_length, output ); | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 323 | exit: | 
|  | 324 | mbedtls_md_free( &md_ctx ); | 
|  | 325 | return( ret ); | 
|  | 326 | #else | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 327 | int j; | 
|  | 328 | unsigned int i; | 
|  | 329 | unsigned char md1[PSA_HASH_MAX_SIZE]; | 
|  | 330 | unsigned char work[PSA_HASH_MAX_SIZE]; | 
| Andrzej Kurek | e3d544c | 2022-09-01 12:33:22 -0400 | [diff] [blame] | 331 | const unsigned char md_size = mbedtls_hash_info_get_size( md_alg ); | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 332 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; | 
|  | 333 |  | 
|  | 334 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Andrzej Kurek | 216baca | 2022-09-01 12:59:05 -0400 | [diff] [blame] | 335 | psa_status_t status_destruction = PSA_ERROR_CORRUPTION_DETECTED; | 
| Andrzej Kurek | e3d544c | 2022-09-01 12:33:22 -0400 | [diff] [blame] | 336 | size_t use_len, out_len; | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 337 | unsigned char *out_p = output; | 
|  | 338 | unsigned char counter[4]; | 
| Andrzej Kurek | e3d544c | 2022-09-01 12:33:22 -0400 | [diff] [blame] | 339 | mbedtls_svc_key_id_t psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 340 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Andrzej Kurek | e3d544c | 2022-09-01 12:33:22 -0400 | [diff] [blame] | 341 | const psa_algorithm_t alg = PSA_ALG_HMAC( mbedtls_hash_info_psa_from_md( md_alg ) ); | 
|  | 342 | const size_t out_size = PSA_MAC_LENGTH( PSA_KEY_TYPE_HMAC, 0, alg ); | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 343 |  | 
| Andrzej Kurek | e3d544c | 2022-09-01 12:33:22 -0400 | [diff] [blame] | 344 | memset( counter, 0, sizeof( counter ) ); | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 345 | counter[3] = 1; | 
| Andrzej Kurek | e3d544c | 2022-09-01 12:33:22 -0400 | [diff] [blame] | 346 |  | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 347 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); | 
|  | 348 | psa_set_key_algorithm( &attributes,  alg ); | 
|  | 349 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); | 
|  | 350 |  | 
|  | 351 | if( key_length == 0 ) | 
|  | 352 | return 0; | 
|  | 353 | if( ( status = psa_import_key( &attributes, | 
|  | 354 | password, plen, | 
|  | 355 | &psa_hmac_key ) ) != PSA_SUCCESS ) | 
|  | 356 | { | 
| Andrzej Kurek | 216baca | 2022-09-01 12:59:05 -0400 | [diff] [blame] | 357 | return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA; | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 358 | } | 
|  | 359 |  | 
|  | 360 | #if UINT_MAX > 0xFFFFFFFF | 
|  | 361 | if( iteration_count > 0xFFFFFFFF ) | 
|  | 362 | return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA ); | 
|  | 363 | #endif | 
|  | 364 |  | 
|  | 365 | while( key_length ) | 
|  | 366 | { | 
|  | 367 | status = psa_mac_sign_setup( &operation, psa_hmac_key, | 
|  | 368 | PSA_ALG_HMAC( alg ) ); | 
|  | 369 | if( status != PSA_SUCCESS ) | 
|  | 370 | goto cleanup; | 
|  | 371 | // U1 ends up in work | 
|  | 372 | if( ( status = psa_mac_update( &operation, salt, slen ) ) != PSA_SUCCESS ) | 
|  | 373 | goto cleanup; | 
|  | 374 |  | 
| Andrzej Kurek | e3d544c | 2022-09-01 12:33:22 -0400 | [diff] [blame] | 375 | if( ( status = psa_mac_update( &operation, counter, sizeof( counter ) ) ) != PSA_SUCCESS ) | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 376 | goto cleanup; | 
|  | 377 |  | 
|  | 378 | if( ( status = psa_mac_sign_finish( &operation, work, out_size, &out_len ) ) | 
|  | 379 | != PSA_SUCCESS ) | 
|  | 380 | goto cleanup; | 
|  | 381 |  | 
| Andrzej Kurek | e3d544c | 2022-09-01 12:33:22 -0400 | [diff] [blame] | 382 | memcpy( md1, work, out_len ); | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 383 |  | 
|  | 384 | for( i = 1; i < iteration_count; i++ ) | 
|  | 385 | { | 
|  | 386 | // U2 ends up in md1 | 
|  | 387 | // | 
|  | 388 | status = psa_mac_sign_setup( &operation, psa_hmac_key, | 
|  | 389 | PSA_ALG_HMAC( alg ) ); | 
|  | 390 | if( status != PSA_SUCCESS ) | 
|  | 391 | goto cleanup; | 
|  | 392 | if( ( status = psa_mac_update( &operation, md1, md_size ) ) != PSA_SUCCESS ) | 
|  | 393 | goto cleanup; | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 394 | if( ( status = psa_mac_sign_finish( &operation, md1, out_size, &out_len ) ) != PSA_SUCCESS ) | 
|  | 395 | goto cleanup; | 
|  | 396 |  | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 397 | // U1 xor U2 | 
|  | 398 | // | 
|  | 399 | for( j = 0; j < md_size; j++ ) | 
|  | 400 | work[j] ^= md1[j]; | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | use_len = ( key_length < md_size ) ? key_length : md_size; | 
|  | 404 | memcpy( out_p, work, use_len ); | 
|  | 405 |  | 
|  | 406 | key_length -= (uint32_t) use_len; | 
|  | 407 | out_p += use_len; | 
|  | 408 |  | 
|  | 409 | for( i = 4; i > 0; i-- ) | 
|  | 410 | if( ++counter[i - 1] != 0 ) | 
|  | 411 | break; | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | cleanup: | 
|  | 415 | /* Zeroise buffers to clear sensitive data from memory. */ | 
|  | 416 | mbedtls_platform_zeroize( work, PSA_HASH_MAX_SIZE ); | 
|  | 417 | mbedtls_platform_zeroize( md1, PSA_HASH_MAX_SIZE ); | 
| Andrzej Kurek | 216baca | 2022-09-01 12:59:05 -0400 | [diff] [blame] | 418 | status_destruction = psa_destroy_key( psa_hmac_key ); | 
|  | 419 | if( status == PSA_SUCCESS && status_destruction != PSA_SUCCESS ) | 
|  | 420 | status = status_destruction; | 
|  | 421 | status_destruction = psa_mac_abort( &operation ); | 
|  | 422 | if( status == PSA_SUCCESS && status_destruction != PSA_SUCCESS ) | 
|  | 423 | status = status_destruction; | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 424 |  | 
| Andrzej Kurek | 216baca | 2022-09-01 12:59:05 -0400 | [diff] [blame] | 425 | return( mbedtls_md_error_from_psa( status ) ); | 
| Andrzej Kurek | f000471 | 2022-08-31 19:10:42 -0400 | [diff] [blame] | 426 | #endif /* !MBEDTLS_MD_C */ | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 427 | } | 
|  | 428 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 429 | #if defined(MBEDTLS_SELF_TEST) | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 430 |  | 
| Andrzej Kurek | ed98e95 | 2022-08-31 14:57:11 -0400 | [diff] [blame] | 431 | #if !defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | int mbedtls_pkcs5_self_test( int verbose ) | 
| Manuel Pégourié-Gonnard | 2a8afa9 | 2014-06-12 12:00:44 +0200 | [diff] [blame] | 433 | { | 
|  | 434 | if( verbose != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 435 | mbedtls_printf( "  PBKDF2 (SHA1): skipped\n\n" ); | 
| Manuel Pégourié-Gonnard | 2a8afa9 | 2014-06-12 12:00:44 +0200 | [diff] [blame] | 436 |  | 
|  | 437 | return( 0 ); | 
|  | 438 | } | 
|  | 439 | #else | 
|  | 440 |  | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 441 | #define MAX_TESTS   6 | 
|  | 442 |  | 
| Michał Janiszewski | 9aeea93 | 2018-10-30 23:00:15 +0100 | [diff] [blame] | 443 | static const size_t plen_test_data[MAX_TESTS] = | 
| Manuel Pégourié-Gonnard | 73ed39d | 2015-03-10 15:46:30 +0000 | [diff] [blame] | 444 | { 8, 8, 8, 24, 9 }; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 445 |  | 
| Michał Janiszewski | 9aeea93 | 2018-10-30 23:00:15 +0100 | [diff] [blame] | 446 | static const unsigned char password_test_data[MAX_TESTS][32] = | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 447 | { | 
|  | 448 | "password", | 
|  | 449 | "password", | 
|  | 450 | "password", | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 451 | "passwordPASSWORDpassword", | 
|  | 452 | "pass\0word", | 
|  | 453 | }; | 
|  | 454 |  | 
| Michał Janiszewski | 9aeea93 | 2018-10-30 23:00:15 +0100 | [diff] [blame] | 455 | static const size_t slen_test_data[MAX_TESTS] = | 
| Manuel Pégourié-Gonnard | 73ed39d | 2015-03-10 15:46:30 +0000 | [diff] [blame] | 456 | { 4, 4, 4, 36, 5 }; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 457 |  | 
| Michał Janiszewski | 9aeea93 | 2018-10-30 23:00:15 +0100 | [diff] [blame] | 458 | static const unsigned char salt_test_data[MAX_TESTS][40] = | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 459 | { | 
|  | 460 | "salt", | 
|  | 461 | "salt", | 
|  | 462 | "salt", | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 463 | "saltSALTsaltSALTsaltSALTsaltSALTsalt", | 
|  | 464 | "sa\0lt", | 
|  | 465 | }; | 
|  | 466 |  | 
| Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 467 | static const uint32_t it_cnt_test_data[MAX_TESTS] = | 
| Manuel Pégourié-Gonnard | 73ed39d | 2015-03-10 15:46:30 +0000 | [diff] [blame] | 468 | { 1, 2, 4096, 4096, 4096 }; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 469 |  | 
| Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 470 | static const uint32_t key_len_test_data[MAX_TESTS] = | 
| Manuel Pégourié-Gonnard | 73ed39d | 2015-03-10 15:46:30 +0000 | [diff] [blame] | 471 | { 20, 20, 20, 25, 16 }; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 472 |  | 
| Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 473 | static const unsigned char result_key_test_data[MAX_TESTS][32] = | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 474 | { | 
|  | 475 | { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, | 
|  | 476 | 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, | 
|  | 477 | 0x2f, 0xe0, 0x37, 0xa6 }, | 
|  | 478 | { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, | 
|  | 479 | 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, | 
|  | 480 | 0xd8, 0xde, 0x89, 0x57 }, | 
|  | 481 | { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, | 
|  | 482 | 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, | 
|  | 483 | 0x65, 0xa4, 0x29, 0xc1 }, | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 484 | { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b, | 
|  | 485 | 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a, | 
|  | 486 | 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70, | 
|  | 487 | 0x38 }, | 
|  | 488 | { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, | 
|  | 489 | 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 }, | 
|  | 490 | }; | 
|  | 491 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | int mbedtls_pkcs5_self_test( int verbose ) | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 493 | { | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 494 | int ret, i; | 
|  | 495 | unsigned char key[64]; | 
|  | 496 |  | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 497 | for( i = 0; i < MAX_TESTS; i++ ) | 
|  | 498 | { | 
| Manuel Pégourié-Gonnard | 13a1ef8 | 2014-03-27 20:12:44 +0100 | [diff] [blame] | 499 | if( verbose != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 500 | mbedtls_printf( "  PBKDF2 (SHA1) #%d: ", i ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 501 |  | 
| Andrzej Kurek | dd36c76 | 2022-08-31 13:29:38 -0400 | [diff] [blame] | 502 | ret = mbedtls_pkcs5_pbkdf2_hmac_ext( MBEDTLS_MD_SHA1, password_test_data[i], | 
| Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 503 | plen_test_data[i], salt_test_data[i], | 
|  | 504 | slen_test_data[i], it_cnt_test_data[i], | 
|  | 505 | key_len_test_data[i], key ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 506 | if( ret != 0 || | 
| Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 507 | memcmp( result_key_test_data[i], key, key_len_test_data[i] ) != 0 ) | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 508 | { | 
|  | 509 | if( verbose != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 510 | mbedtls_printf( "failed\n" ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 511 |  | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 512 | ret = 1; | 
|  | 513 | goto exit; | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 514 | } | 
|  | 515 |  | 
|  | 516 | if( verbose != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 517 | mbedtls_printf( "passed\n" ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 518 | } | 
|  | 519 |  | 
| Paul Bakker | 4400ecc | 2016-07-19 14:41:43 +0100 | [diff] [blame] | 520 | if( verbose != 0 ) | 
|  | 521 | mbedtls_printf( "\n" ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 522 |  | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 523 | exit: | 
| Alfred Klomp | 1b4eda3 | 2014-07-14 22:07:34 +0200 | [diff] [blame] | 524 | return( ret ); | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 525 | } | 
| Andrzej Kurek | ed98e95 | 2022-08-31 14:57:11 -0400 | [diff] [blame] | 526 | #endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA */ | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 527 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | #endif /* MBEDTLS_SELF_TEST */ | 
| Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 529 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | #endif /* MBEDTLS_PKCS5_C */ |