| 1 |
// Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 1999. |
| 2 |
// Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <openssl/evp.h> |
| 6 |
|
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/hmac.h> |
| 10 |
|
| 11 |
#include "../../internal.h" |
| 12 |
#include "../service_indicator/internal.h" |
| 13 |
|
| 14 |
|
| 15 |
int PKCS5_PBKDF2_HMAC(const char *password, size_t password_len, |
| 16 |
const uint8_t *salt, size_t salt_len, uint32_t iterations, |
| 17 |
const EVP_MD *digest, size_t key_len, uint8_t *out_key) { |
| 18 |
// See RFC 8018, section 5.2. |
| 19 |
int ret = 0; |
| 20 |
size_t md_len = EVP_MD_size(digest); |
| 21 |
uint32_t i = 1; |
| 22 |
HMAC_CTX hctx; |
| 23 |
HMAC_CTX_init(&hctx); |
| 24 |
|
| 25 |
// We have to avoid the underlying SHA services updating the indicator |
| 26 |
// state, so we lock the state here. |
| 27 |
FIPS_service_indicator_lock_state(); |
| 28 |
|
| 29 |
if (!HMAC_Init_ex(&hctx, password, password_len, digest, NULL)) { |
| 30 |
goto err; |
| 31 |
} |
| 32 |
|
| 33 |
while (key_len > 0) { |
| 34 |
size_t todo = md_len; |
| 35 |
if (todo > key_len) { |
| 36 |
todo = key_len; |
| 37 |
} |
| 38 |
|
| 39 |
uint8_t i_buf[4]; |
| 40 |
i_buf[0] = (uint8_t)((i >> 24) & 0xff); |
| 41 |
i_buf[1] = (uint8_t)((i >> 16) & 0xff); |
| 42 |
i_buf[2] = (uint8_t)((i >> 8) & 0xff); |
| 43 |
i_buf[3] = (uint8_t)(i & 0xff); |
| 44 |
|
| 45 |
// Compute U_1. |
| 46 |
uint8_t digest_tmp[EVP_MAX_MD_SIZE]; |
| 47 |
if (!HMAC_Init_ex(&hctx, NULL, 0, NULL, NULL) || |
| 48 |
!HMAC_Update(&hctx, salt, salt_len) || |
| 49 |
!HMAC_Update(&hctx, i_buf, 4) || |
| 50 |
!HMAC_Final(&hctx, digest_tmp, NULL)) { |
| 51 |
goto err; |
| 52 |
} |
| 53 |
|
| 54 |
OPENSSL_memcpy(out_key, digest_tmp, todo); |
| 55 |
for (uint32_t j = 1; j < iterations; j++) { |
| 56 |
// Compute the remaining U_* values and XOR. |
| 57 |
if (!HMAC_Init_ex(&hctx, NULL, 0, NULL, NULL) || |
| 58 |
!HMAC_Update(&hctx, digest_tmp, md_len) || |
| 59 |
!HMAC_Final(&hctx, digest_tmp, NULL)) { |
| 60 |
goto err; |
| 61 |
} |
| 62 |
for (size_t k = 0; k < todo; k++) { |
| 63 |
out_key[k] ^= digest_tmp[k]; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
key_len -= todo; |
| 68 |
out_key += todo; |
| 69 |
i++; |
| 70 |
} |
| 71 |
|
| 72 |
// RFC 8018 describes iterations (c) as being a "positive integer", so a |
| 73 |
// value of 0 is an error. |
| 74 |
// |
| 75 |
// Unfortunately not all consumers of PKCS5_PBKDF2_HMAC() check their return |
| 76 |
// value, expecting it to succeed and unconditionally using |out_key|. As a |
| 77 |
// precaution for such callsites in external code, the old behavior of |
| 78 |
// iterations < 1 being treated as iterations == 1 is preserved, but |
| 79 |
// additionally an error result is returned. |
| 80 |
// |
| 81 |
// TODO(eroman): Figure out how to remove this compatibility hack, or change |
| 82 |
// the default to something more sensible like 2048. |
| 83 |
if (iterations == 0) { |
| 84 |
goto err; |
| 85 |
} |
| 86 |
|
| 87 |
ret = 1; |
| 88 |
|
| 89 |
err: |
| 90 |
FIPS_service_indicator_unlock_state(); |
| 91 |
HMAC_CTX_cleanup(&hctx); |
| 92 |
if (ret) { |
| 93 |
PBKDF2_verify_service_indicator(digest, password_len, salt_len, iterations); |
| 94 |
} |
| 95 |
return ret; |
| 96 |
} |
| 97 |
|
| 98 |
int PKCS5_PBKDF2_HMAC_SHA1(const char *password, size_t password_len, |
| 99 |
const uint8_t *salt, size_t salt_len, |
| 100 |
uint32_t iterations, size_t key_len, |
| 101 |
uint8_t *out_key) { |
| 102 |
return PKCS5_PBKDF2_HMAC(password, password_len, salt, salt_len, iterations, |
| 103 |
EVP_sha1(), key_len, out_key); |
| 104 |
} |