Mailing List Archive

Reject invalid HKDF key sizes
HKDF prohibits output sizes which exceed digest size * 255. See section 2.3
of RFC 5869.

In the following code, the abort() should not be reached:

#include <gcrypt.h>

#define CF_CHECK_EQ(expr, res) if ( (expr) != (res) ) { goto end; }

#define OUTSIZE ((32 * 255) + 1)

int main(void)
{
const unsigned char password[] = {0x00};
const unsigned char salt[] = {0x00};
const unsigned char info[] = {0x00};

gcry_kdf_hd_t hd = {0};
uint8_t out[OUTSIZE];
unsigned long param[1] = {OUTSIZE};

CF_CHECK_EQ(gcry_kdf_open(
&hd,
GCRY_KDF_HKDF,
GCRY_MAC_HMAC_SHA256,
param,
1,
password, sizeof(password),
NULL, 0,
salt, sizeof(salt),
info, sizeof(info)), GPG_ERR_NO_ERROR);

CF_CHECK_EQ(gcry_kdf_compute(hd, NULL), GPG_ERR_NO_ERROR);
CF_CHECK_EQ(gcry_kdf_final(hd, OUTSIZE, out), GPG_ERR_NO_ERROR);

/* Should not be reached */
abort();

end:
gcry_kdf_close(hd);
return 0;
}
Re: Reject invalid HKDF key sizes [ In reply to ]
Guido Vranken wrote:
> HKDF prohibits output sizes which exceed digest size * 255. See section 2.3
> of RFC 5869.

Thank you.

Fixed in the commit:

e0f0c788dc0f268965c0f63eb33d9f98c0575d58
--

_______________________________________________
Gcrypt-devel mailing list
Gcrypt-devel@lists.gnupg.org
https://lists.gnupg.org/mailman/listinfo/gcrypt-devel
Re: Reject invalid HKDF key sizes [ In reply to ]
Your fix introduces a memory leak. Please free 'h' before returning.

diff --git a/cipher/kdf.c b/cipher/kdf.c
index 2e5eef3..d371bdd 100644
--- a/cipher/kdf.c
+++ b/cipher/kdf.c
@@ -1699,7 +1699,10 @@ hkdf_open (gcry_kdf_hd_t *hd, int macalgo,
}

if (outlen > 255 * h->blklen)
- return GPG_ERR_INV_VALUE;
+ {
+ xfree (h);
+ return GPG_ERR_INV_VALUE;
+ }

ec = _gcry_mac_open (&h->md, macalgo, 0, NULL);
if (ec)

On Tue, Jun 21, 2022 at 7:02 AM NIIBE Yutaka <gniibe@fsij.org> wrote:

> Guido Vranken wrote:
> > HKDF prohibits output sizes which exceed digest size * 255. See section
> 2.3
> > of RFC 5869.
>
> Thank you.
>
> Fixed in the commit:
>
> e0f0c788dc0f268965c0f63eb33d9f98c0575d58
> --
>