2016-07-17 09:51:22 +02:00
|
|
|
/*
|
|
|
|
* HKDF implementation -- RFC 5869
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2016-07-17 09:51:22 +02:00
|
|
|
*/
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2016-07-17 09:51:22 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_HKDF_C)
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "mbedtls/hkdf.h"
|
|
|
|
#include "mbedtls/platform_util.h"
|
2019-11-22 14:21:35 +01:00
|
|
|
#include "mbedtls/error.h"
|
2016-07-17 09:51:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_hkdf(const mbedtls_md_info_t *md, const unsigned char *salt,
|
|
|
|
size_t salt_len, const unsigned char *ikm, size_t ikm_len,
|
|
|
|
const unsigned char *info, size_t info_len,
|
|
|
|
unsigned char *okm, size_t okm_len)
|
2016-07-17 09:51:22 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2016-07-17 09:51:22 +02:00
|
|
|
unsigned char prk[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_hkdf_extract(md, salt, salt_len, ikm, ikm_len, prk);
|
2016-07-17 09:51:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
ret = mbedtls_hkdf_expand(md, prk, mbedtls_md_get_size(md),
|
|
|
|
info, info_len, okm, okm_len);
|
2016-07-17 09:51:22 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(prk, sizeof(prk));
|
2016-07-17 09:51:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2016-07-17 09:51:22 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_hkdf_extract(const mbedtls_md_info_t *md,
|
|
|
|
const unsigned char *salt, size_t salt_len,
|
|
|
|
const unsigned char *ikm, size_t ikm_len,
|
|
|
|
unsigned char *prk)
|
2016-07-17 09:51:22 +02:00
|
|
|
{
|
|
|
|
unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (salt == NULL) {
|
2016-07-17 09:51:22 +02:00
|
|
|
size_t hash_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (salt_len != 0) {
|
2018-07-06 19:03:58 +02:00
|
|
|
return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_len = mbedtls_md_get_size(md);
|
2016-07-17 09:51:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hash_len == 0) {
|
2016-07-17 09:51:22 +02:00
|
|
|
return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
salt = null_salt;
|
|
|
|
salt_len = hash_len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_md_hmac(md, salt, salt_len, ikm, ikm_len, prk);
|
2016-07-17 09:51:22 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk,
|
|
|
|
size_t prk_len, const unsigned char *info,
|
|
|
|
size_t info_len, unsigned char *okm, size_t okm_len)
|
2016-07-17 09:51:22 +02:00
|
|
|
{
|
|
|
|
size_t hash_len;
|
|
|
|
size_t where = 0;
|
|
|
|
size_t n;
|
|
|
|
size_t t_len = 0;
|
|
|
|
size_t i;
|
|
|
|
int ret = 0;
|
|
|
|
mbedtls_md_context_t ctx;
|
|
|
|
unsigned char t[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (okm == NULL) {
|
|
|
|
return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
|
2016-07-17 09:51:22 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_len = mbedtls_md_get_size(md);
|
2016-07-17 09:51:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (prk_len < hash_len || hash_len == 0) {
|
|
|
|
return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
|
2016-07-17 09:51:22 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (info == NULL) {
|
2016-07-17 09:51:22 +02:00
|
|
|
info = (const unsigned char *) "";
|
|
|
|
info_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = okm_len / hash_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (okm_len % hash_len != 0) {
|
2016-07-17 09:51:22 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2018-07-06 19:02:39 +02:00
|
|
|
/*
|
|
|
|
* Per RFC 5869 Section 2.3, okm_len must not exceed
|
|
|
|
* 255 times the hash length
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (n > 255) {
|
|
|
|
return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
|
2016-07-17 09:51:22 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&ctx);
|
2016-07-17 09:51:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_md_setup(&ctx, md, 1)) != 0) {
|
2016-07-17 09:51:22 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(t, 0, hash_len);
|
2020-04-02 19:51:05 +02:00
|
|
|
|
2018-07-06 19:02:39 +02:00
|
|
|
/*
|
|
|
|
* Compute T = T(1) | T(2) | T(3) | ... | T(N)
|
|
|
|
* Where T(N) is defined in RFC 5869 Section 2.3
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 1; i <= n; i++) {
|
2016-07-17 09:51:22 +02:00
|
|
|
size_t num_to_copy;
|
|
|
|
unsigned char c = i & 0xff;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md_hmac_starts(&ctx, prk, prk_len);
|
|
|
|
if (ret != 0) {
|
2016-07-17 09:51:22 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md_hmac_update(&ctx, t, t_len);
|
|
|
|
if (ret != 0) {
|
2016-07-17 09:51:22 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md_hmac_update(&ctx, info, info_len);
|
|
|
|
if (ret != 0) {
|
2016-07-17 09:51:22 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2018-07-06 19:02:39 +02:00
|
|
|
/* The constant concatenated to the end of each T(n) is a single octet.
|
2016-07-17 09:51:22 +02:00
|
|
|
* */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md_hmac_update(&ctx, &c, 1);
|
|
|
|
if (ret != 0) {
|
2016-07-17 09:51:22 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md_hmac_finish(&ctx, t);
|
|
|
|
if (ret != 0) {
|
2016-07-17 09:51:22 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_to_copy = i != n ? hash_len : okm_len - where;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(okm + where, t, num_to_copy);
|
2016-07-17 09:51:22 +02:00
|
|
|
where += hash_len;
|
|
|
|
t_len = hash_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&ctx);
|
|
|
|
mbedtls_platform_zeroize(t, sizeof(t));
|
2016-07-17 09:51:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2016-07-17 09:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_HKDF_C */
|