Skip to content

Commit 15c5fcc

Browse files
authored
Merge pull request #352 from ColtonWilley/wp_tls13_kdf_fips
Update TLS 1.3 KDF to use proper wolfcrypt FIPS APIs
2 parents 58e0a3d + 3383fc7 commit 15c5fcc

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

src/wp_hkdf.c

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <wolfprovider/alg_funcs.h>
3131
#include <wolfprovider/internal.h>
32+
#include <wolfssl/wolfcrypt/kdf.h>
3233

3334
/**
3435
* Define WP_HKDF_NULL_SALT_ALLOWED for OpenSSL versions that allow NULL salt in HKDF.
@@ -571,7 +572,6 @@ static int wp_tls13_hkdf_expand(wp_HkdfCtx* ctx, unsigned char* inKey,
571572
size_t keyLen)
572573
{
573574
int ok = 1;
574-
size_t idx = 0;
575575
int rc;
576576

577577
WOLFPROV_ENTER(WP_LOG_COMP_HKDF, "wp_tls13_hkdf_expand");
@@ -582,32 +582,17 @@ static int wp_tls13_hkdf_expand(wp_HkdfCtx* ctx, unsigned char* inKey,
582582
"TLS1.3 HKDF expand: prefixLen=%zu, labelLen=%zu",
583583
ctx->prefixLen, ctx->labelLen);
584584

585-
/* Construct info to expand from:
586-
* - output key length
587-
* - label
588-
* - prefix/protocol
589-
* - data
590-
*/
591-
ctx->info[idx++] = (byte)(keyLen >> 8);
592-
ctx->info[idx++] = (byte)keyLen;
593-
ctx->info[idx++] = (byte)(ctx->prefixLen + ctx->labelLen);
594-
XMEMCPY(ctx->info + idx, ctx->prefix, ctx->prefixLen);
595-
idx += ctx->prefixLen;
596-
XMEMCPY(ctx->info + idx, ctx->label, ctx->labelLen);
597-
idx += ctx->labelLen;
598-
ctx->info[idx++] = (byte)(dataLen);
599-
if (dataLen > 0) {
600-
XMEMCPY(ctx->info + idx, data, dataLen);
601-
idx += dataLen;
602-
}
603-
ctx->infoSz = idx;
604-
605585
PRIVATE_KEY_UNLOCK();
606-
rc = wc_HKDF_Expand(ctx->mdType, inKey, (word32)inKeyLen, ctx->info,
607-
(word32)ctx->infoSz, key, (word32)keyLen);
586+
rc = wc_Tls13_HKDF_Expand_Label(key, (word32)keyLen,
587+
inKey, (word32)inKeyLen,
588+
ctx->prefix, (word32)ctx->prefixLen,
589+
ctx->label, (word32)ctx->labelLen,
590+
data, (word32)dataLen,
591+
ctx->mdType);
608592
PRIVATE_KEY_LOCK();
609593
if (rc != 0) {
610-
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_HKDF_Expand", rc);
594+
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG,
595+
"wc_Tls13_HKDF_Expand_Label", rc);
611596
ok = 0;
612597
}
613598

@@ -674,16 +659,17 @@ static int wp_tls13_hkdf_extract(wp_HkdfCtx* ctx, unsigned char* key,
674659
(void)keyLen;
675660
PRIVATE_KEY_UNLOCK();
676661
if (saltLen == 0) {
677-
rc = wc_HKDF_Extract(ctx->mdType, NULL, 0, inKey,
678-
(word32)inKeyLen, key);
662+
rc = wc_Tls13_HKDF_Extract(key, NULL, 0, inKey,
663+
(word32)inKeyLen, ctx->mdType);
679664
}
680665
else {
681-
rc = wc_HKDF_Extract(ctx->mdType, salt, (word32)saltLen, inKey,
682-
(word32)inKeyLen, key);
666+
rc = wc_Tls13_HKDF_Extract(key, salt, (word32)saltLen, inKey,
667+
(word32)inKeyLen, ctx->mdType);
683668
}
684669
PRIVATE_KEY_LOCK();
685670
if (rc != 0) {
686-
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_HKDF_Extract", rc);
671+
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG,
672+
"wc_Tls13_HKDF_Extract", rc);
687673
ok = 0;
688674
}
689675
}

0 commit comments

Comments
 (0)