Commit f67dd4f0 authored by Niels Möller's avatar Niels Möller
Browse files

Add meta interface for HMAC functions.

Based on patches by Daiki Ueno.
parent 9c04199d
2020-02-09 Niels Möller <nisse@lysator.liu.se>
Based on patches by Daiki Ueno.
* nettle-meta.h (_NETTLE_HMAC): New macro.
(nettle_hmac_md5, nettle_hmac_ripemd160, nettle_hmac_sha1)
(nettle_hmac_sha224, nettle_hmac_sha256, nettle_hmac_sha384)
(nettle_hmac_sha512): Declare.
(struct nettle_mac): New public struct,
* testsuite/testutils.h: ...moved from this file.
* hmac-md5-meta.c: New file.
* hmac-ripemd160-meta.c: Likewise.
* hmac-sha1-meta.c: Likewise.
* hmac-sha224-meta.c: Likewise.
* hmac-sha256-meta.c: Likewise.
* hmac-sha384-meta.c: Likewise.
* hmac-sha512-meta.c: Likewise.
* Makefile.in (nettle_SOURCES): Add new files.
* testsuite/testutils.h (_NETTLE_HMAC): Delete unused version of
this macro.
* testsuite/testutils.c (test_mac): Allow testing with smaller
digest size.
* testsuite/hmac-test.c (test_main): Use test_mac for tests using
key size == digest size.
* testsuite/cmac-test.c (nettle_cmac_aes128, nettle_cmac_aes256):
Moved to...
* cmac-aes128-meta.c: New file.
* cmac-aes256-meta.c: New file.
* Makefile.in (nettle_SOURCES): Add cmac-aes128-meta.c cmac-aes256-meta.c.
* nettle-meta.h (struct nettle_mac): New public struct,
* testsuite/testutils.h: ...moved from this file.
......
......@@ -108,6 +108,9 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \
hmac.c hmac-gosthash94.c hmac-md5.c hmac-ripemd160.c \
hmac-sha1.c hmac-sha224.c hmac-sha256.c hmac-sha384.c \
hmac-sha512.c \
hmac-md5-meta.c hmac-ripemd160-meta.c hmac-sha1-meta.c \
hmac-sha224-meta.c hmac-sha256-meta.c hmac-sha384-meta.c \
hmac-sha512-meta.c \
knuth-lfib.c hkdf.c \
md2.c md2-meta.c md4.c md4-meta.c \
md5.c md5-compress.c md5-compat.c md5-meta.c \
......
/* hmac-md5-meta.c
Copyright (C) 2020 Daiki Ueno
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "nettle-meta.h"
#include "hmac.h"
static void
hmac_md5_set_key_wrapper (void *ctx, const uint8_t *key)
{
hmac_md5_set_key (ctx, MD5_DIGEST_SIZE, key);
}
const struct nettle_mac nettle_hmac_md5
= _NETTLE_HMAC(hmac_md5, MD5);
/* hmac-ripemd160-meta.c
Copyright (C) 2020 Daiki Ueno
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "nettle-meta.h"
#include "hmac.h"
static void
hmac_ripemd160_set_key_wrapper (void *ctx, const uint8_t *key)
{
hmac_ripemd160_set_key (ctx, RIPEMD160_DIGEST_SIZE, key);
}
const struct nettle_mac nettle_hmac_ripemd160
= _NETTLE_HMAC(hmac_ripemd160, RIPEMD160);
/* hmac-sha1-meta.c
Copyright (C) 2020 Daiki Ueno
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "nettle-meta.h"
#include "hmac.h"
static void
hmac_sha1_set_key_wrapper (void *ctx, const uint8_t *key)
{
hmac_sha1_set_key (ctx, SHA1_DIGEST_SIZE, key);
}
const struct nettle_mac nettle_hmac_sha1
= _NETTLE_HMAC(hmac_sha1, SHA1);
/* hmac-sha224-meta.c
Copyright (C) 2020 Daiki Ueno
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "nettle-meta.h"
#include "hmac.h"
static void
hmac_sha224_set_key_wrapper (void *ctx, const uint8_t *key)
{
hmac_sha224_set_key (ctx, SHA224_DIGEST_SIZE, key);
}
const struct nettle_mac nettle_hmac_sha224
= _NETTLE_HMAC(hmac_sha224, SHA224);
/* hmac-sha256-meta.c
Copyright (C) 2020 Daiki Ueno
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "nettle-meta.h"
#include "hmac.h"
static void
hmac_sha256_set_key_wrapper (void *ctx, const uint8_t *key)
{
hmac_sha256_set_key (ctx, SHA256_DIGEST_SIZE, key);
}
const struct nettle_mac nettle_hmac_sha256
= _NETTLE_HMAC(hmac_sha256, SHA256);
/* hmac-sha384-meta.c
Copyright (C) 2020 Daiki Ueno
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "nettle-meta.h"
#include "hmac.h"
static void
hmac_sha384_set_key_wrapper (void *ctx, const uint8_t *key)
{
hmac_sha384_set_key (ctx, SHA384_DIGEST_SIZE, key);
}
const struct nettle_mac nettle_hmac_sha384
= _NETTLE_HMAC(hmac_sha384, SHA384);
/* hmac-sha512-meta.c
Copyright (C) 2020 Daiki Ueno
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "nettle-meta.h"
#include "hmac.h"
static void
hmac_sha512_set_key_wrapper (void *ctx, const uint8_t *key)
{
hmac_sha512_set_key (ctx, SHA512_DIGEST_SIZE, key);
}
const struct nettle_mac nettle_hmac_sha512
= _NETTLE_HMAC(hmac_sha512, SHA512);
......@@ -2,7 +2,7 @@
Information about algorithms.
Copyright (C) 2002, 2014 Niels Möller
Copyright (C) 2002, 2014, 2020 Niels Möller
This file is part of GNU Nettle.
......@@ -257,9 +257,28 @@ extern const struct nettle_armor nettle_base64;
extern const struct nettle_armor nettle_base64url;
extern const struct nettle_armor nettle_base16;
#define _NETTLE_HMAC(name, HASH) { \
#name, \
sizeof(struct name##_ctx), \
HASH##_DIGEST_SIZE, \
HASH##_DIGEST_SIZE, \
name##_set_key_wrapper, \
(nettle_hash_update_func *) name##_update, \
(nettle_hash_digest_func *) name##_digest, \
}
extern const struct nettle_mac nettle_cmac_aes128;
extern const struct nettle_mac nettle_cmac_aes256;
/* HMAC variants with key size = digest size */
extern const struct nettle_mac nettle_hmac_md5;
extern const struct nettle_mac nettle_hmac_ripemd160;
extern const struct nettle_mac nettle_hmac_sha1;
extern const struct nettle_mac nettle_hmac_sha224;
extern const struct nettle_mac nettle_hmac_sha256;
extern const struct nettle_mac nettle_hmac_sha384;
extern const struct nettle_mac nettle_hmac_sha512;
#ifdef __cplusplus
}
#endif
......
......@@ -24,12 +24,11 @@ test_main(void)
/* Test vectors for md5, from RFC-2202 */
/* md5 - 1 */
HMAC_TEST(md5,
test_mac (&nettle_hmac_md5,
SHEX("0b0b0b0b0b0b0b0b 0b0b0b0b0b0b0b0b"),
SDATA("Hi There"),
SHEX("9294727a3638bb1c 13f48ef8158bfc9d"));
/* md5 - 2 */
HMAC_TEST(md5,
SDATA("Jefe"),
......@@ -37,7 +36,7 @@ test_main(void)
SHEX("750c783e6ab0b503 eaa86e310a5db738"));
/* md5 - 3 */
HMAC_TEST(md5,
test_mac(&nettle_hmac_md5,
SHEX("aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"),
SHEX("dddddddddddddddd dddddddddddddddd"
"dddddddddddddddd dddddddddddddddd"
......@@ -56,7 +55,7 @@ test_main(void)
SHEX("697eaf0aca3a3aea 3a75164746ffaa79"));
/* md5 - 5 */
HMAC_TEST(md5,
test_mac(&nettle_hmac_md5,
SHEX("0c0c0c0c0c0c0c0c 0c0c0c0c0c0c0c0c"),
SDATA("Test With Truncation"),
SHEX("56461ef2342edc00 f9bab995"));
......@@ -125,73 +124,73 @@ test_main(void)
/* Test vectors for ripemd160, from
http://homes.esat.kuleuven.be/~bosselae/ripemd160.html */
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("00112233445566778899aabbccddeeff01234567"),
SDATA(""),
SHEX("cf387677bfda8483e63b57e06c3b5ecd8b7fc055"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("00112233445566778899aabbccddeeff01234567"),
SDATA("a"),
SHEX("0d351d71b78e36dbb7391c810a0d2b6240ddbafc"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("00112233445566778899aabbccddeeff01234567"),
SDATA("abc"),
SHEX("f7ef288cb1bbcc6160d76507e0a3bbf712fb67d6"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("00112233445566778899aabbccddeeff01234567"),
SDATA("message digest"),
SHEX("f83662cc8d339c227e600fcd636c57d2571b1c34"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("00112233445566778899aabbccddeeff01234567"),
SDATA("abcdefghijklmnopqrstuvwxyz"),
SHEX("843d1c4eb880ac8ac0c9c95696507957d0155ddb"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("00112233445566778899aabbccddeeff01234567"),
SDATA("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
SHEX("60f5ef198a2dd5745545c1f0c47aa3fb5776f881"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("00112233445566778899aabbccddeeff01234567"),
SDATA("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"),
SHEX("e49c136a9e5627e0681b808a3b97e6a6e661ae79"));
/* Other key */
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("0123456789abcdeffedcba987654321000112233"),
SDATA(""),
SHEX("fe69a66c7423eea9c8fa2eff8d9dafb4f17a62f5"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("0123456789abcdeffedcba987654321000112233"),
SDATA("a"),
SHEX("85743e899bc82dbfa36faaa7a25b7cfd372432cd"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("0123456789abcdeffedcba987654321000112233"),
SDATA("abc"),
SHEX("6e4afd501fa6b4a1823ca3b10bd9aa0ba97ba182"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("0123456789abcdeffedcba987654321000112233"),
SDATA("message digest"),
SHEX("2e066e624badb76a184c8f90fba053330e650e92"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("0123456789abcdeffedcba987654321000112233"),
SDATA("abcdefghijklmnopqrstuvwxyz"),
SHEX("07e942aa4e3cd7c04dedc1d46e2e8cc4c741b3d9"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("0123456789abcdeffedcba987654321000112233"),
SDATA("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
SHEX("b6582318ddcfb67a53a67d676b8ad869aded629a"));
HMAC_TEST(ripemd160,
test_mac(&nettle_hmac_ripemd160,
SHEX("0123456789abcdeffedcba987654321000112233"),
SDATA("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"),
SHEX("f1be3ee877703140d34f97ea1ab3a07c141333e2"));
......@@ -199,7 +198,7 @@ test_main(void)
/* Test vectors for sha1, from RFC-2202 */
/* sha1 - 1 */
HMAC_TEST(sha1,
test_mac(&nettle_hmac_sha1,
SHEX("0b0b0b0b0b0b0b0b 0b0b0b0b0b0b0b0b 0b0b0b0b"),
SDATA("Hi There"),
SHEX("b617318655057264 e28bc0b6fb378c8e f146be00"));
......@@ -211,7 +210,7 @@ test_main(void)
SHEX("effcdf6ae5eb2fa2 d27416d5f184df9c 259a7c79"));
/* sha1 - 3 */
HMAC_TEST(sha1,
test_mac(&nettle_hmac_sha1,
SHEX("aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaa"),
SHEX("dddddddddddddddd dddddddddddddddd"
"dddddddddddddddd dddddddddddddddd"
......@@ -230,7 +229,7 @@ test_main(void)
SHEX("4c9007f4026250c6 bc8414f9bf50c86c 2d7235da"));
/* sha1 - 5 */
HMAC_TEST(sha1,
test_mac(&nettle_hmac_sha1,
SHEX("0c0c0c0c0c0c0c0c 0c0c0c0c0c0c0c0c 0c0c0c0c"),
SDATA("Test With Truncation"),
SHEX("4c1a03424b55e07f e7f27be1"));
......@@ -256,47 +255,6 @@ test_main(void)
"Than One Block-Size Data"),
SHEX("e8e99d0f45237d78 6d6bbaa7965c7808 bbff1a91"));
/* Additional test vectors, from Daniel Kahn Gillmor */
HMAC_TEST(md5,
SDATA("monkey monkey monkey monkey"),
SDATA(""),
SHEX("e84db42a188813f30a15e611d64c7869"));
HMAC_TEST(md5,
SDATA("monkey monkey monkey monkey"),
SDATA("a"),
SHEX("123662062e67c2aab371cc49db0df134"));
HMAC_TEST(md5,
SDATA("monkey monkey monkey monkey"),
SDATA("38"),
SHEX("0a46cc10a49d4b7025c040c597bf5d76"));
HMAC_TEST(md5,
SDATA("monkey monkey monkey monkey"),
SDATA("abc"),
SHEX("d1f4d89f0e8b2b6ed0623c99ec298310"));
HMAC_TEST(md5,
SDATA("monkey monkey monkey monkey"),
SDATA("message digest"),
SHEX("1627207b9bed5009a4f6e9ca8d2ca01e"));
HMAC_TEST(md5,
SDATA("monkey monkey monkey monkey"),
SDATA("abcdefghijklmnopqrstuvwxyz"),
SHEX("922aae6ab3b3a29202e21ce5f916ae9a"));
HMAC_TEST(md5,
SDATA("monkey monkey monkey monkey"),
SDATA("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"),
SHEX("ede9cb83679ba82d88fbeae865b3f8fc"));
HMAC_TEST(md5,
SDATA("monkey monkey monkey monkey"),
SDATA("12345678901234567890123456789012345678901234567890123456789012345678901234567890"),
SHEX("939dd45512ee3a594b6654f6b8de27f7"));
/* Test vectors for sha224, from RFC 4231 */
HMAC_TEST(sha224,
SHEX("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"
......@@ -490,7 +448,7 @@ test_main(void)
draft-ietf-ipsec-ciph-sha-256-01.txt */
/* Test Case #1: HMAC-SHA-256 with 3-byte input and 32-byte key */
HMAC_TEST(sha256,
test_mac(&nettle_hmac_sha256,
SHEX("0102030405060708 090a0b0c0d0e0f10"
"1112131415161718 191a1b1c1d1e1f20"),
SDATA("abc"),
......@@ -498,7 +456,7 @@ test_main(void)
"7f98cc131cb16a66 92759021cfab8181"));
/* Test Case #2: HMAC-SHA-256 with 56-byte input and 32-byte key */
HMAC_TEST(sha256,
test_mac(&nettle_hmac_sha256,
SHEX("0102030405060708 090a0b0c0d0e0f10"
"1112131415161718 191a1b1c1d1e1f20"),
SDATA("abcdbcdecdefdefgefghfghighijhijk"
......@@ -508,7 +466,7 @@ test_main(void)
/* Test Case #3: HMAC-SHA-256 with 112-byte (multi-block) input
and 32-byte key */
HMAC_TEST(sha256,
test_mac(&nettle_hmac_sha256,
SHEX("0102030405060708 090a0b0c0d0e0f10"
"1112131415161718 191a1b1c1d1e1f20"),
SDATA("abcdbcdecdefdefgefghfghighijhijk"
......@@ -519,7 +477,7 @@ test_main(void)
"73acf0fd060447a5 eb4595bf33a9d1a3"));
/* Test Case #4: HMAC-SHA-256 with 8-byte input and 32-byte key */
HMAC_TEST(sha256,