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