From 8415362cbc1de8b22a220f4bfefe876aa67fa1bc Mon Sep 17 00:00:00 2001
From: Nikos Mavrogiannopoulos <nmav@redhat.com>
Date: Wed, 26 Aug 2015 09:33:59 +0200
Subject: [PATCH] Added helper functions for HMAC-SHA3

---
 Makefile.in     |  1 +
 hmac-sha3-224.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++
 hmac-sha3-256.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++
 hmac-sha3-384.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++
 hmac-sha3-512.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++
 hmac.h          | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 313 insertions(+)
 create mode 100644 hmac-sha3-224.c
 create mode 100644 hmac-sha3-256.c
 create mode 100644 hmac-sha3-384.c
 create mode 100644 hmac-sha3-512.c

diff --git a/Makefile.in b/Makefile.in
index 1bb750d..cd2a491 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -106,6 +106,7 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \
 		 gosthash94.c gosthash94-meta.c \
 		 hmac.c hmac-md5.c hmac-ripemd160.c hmac-sha1.c \
 		 hmac-sha224.c hmac-sha256.c hmac-sha384.c hmac-sha512.c \
+		 hmac-sha3-224.c hmac-sha3-256.c hmac-sha3-384.c hmac-sha3-512.c \
 		 knuth-lfib.c \
 		 md2.c md2-meta.c md4.c md4-meta.c \
 		 md5.c md5-compress.c md5-compat.c md5-meta.c \
diff --git a/hmac-sha3-224.c b/hmac-sha3-224.c
new file mode 100644
index 0000000..47a0289
--- /dev/null
+++ b/hmac-sha3-224.c
@@ -0,0 +1,60 @@
+/* hmac-sha3-224.c
+
+   HMAC-SHA3-224 message authentication code.
+
+   Copyright (C) 2015 Red Hat, Inc.
+
+   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 "hmac.h"
+
+void
+hmac_sha3_224_set_key(struct hmac_sha3_224_ctx *ctx,
+		    size_t key_length, const uint8_t *key)
+{
+  HMAC_SET_KEY(ctx, &nettle_sha3_224, key_length, key);
+}
+
+void
+hmac_sha3_224_digest(struct hmac_sha3_224_ctx *ctx,
+		   size_t length, uint8_t *digest)
+{
+  HMAC_DIGEST(ctx, &nettle_sha3_224, length, digest);
+}
+
+void
+hmac_sha3_224_update(struct hmac_sha3_224_ctx *ctx,
+		   size_t length, const uint8_t *data)
+{
+  sha3_224_update(&ctx->state, length, data);
+}
+
diff --git a/hmac-sha3-256.c b/hmac-sha3-256.c
new file mode 100644
index 0000000..9dc4932
--- /dev/null
+++ b/hmac-sha3-256.c
@@ -0,0 +1,60 @@
+/* hmac-sha3-256.c
+
+   HMAC-SHA3-256 message authentication code.
+
+   Copyright (C) 2015 Red Hat, Inc.
+
+   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 "hmac.h"
+
+void
+hmac_sha3_256_set_key(struct hmac_sha3_256_ctx *ctx,
+		    size_t key_length, const uint8_t *key)
+{
+  HMAC_SET_KEY(ctx, &nettle_sha3_256, key_length, key);
+}
+
+void
+hmac_sha3_256_digest(struct hmac_sha3_256_ctx *ctx,
+		   size_t length, uint8_t *digest)
+{
+  HMAC_DIGEST(ctx, &nettle_sha3_256, length, digest);
+}
+
+void
+hmac_sha3_256_update(struct hmac_sha3_256_ctx *ctx,
+		   size_t length, const uint8_t *data)
+{
+  sha3_256_update(&ctx->state, length, data);
+}
+
diff --git a/hmac-sha3-384.c b/hmac-sha3-384.c
new file mode 100644
index 0000000..b010f41
--- /dev/null
+++ b/hmac-sha3-384.c
@@ -0,0 +1,60 @@
+/* hmac-sha3-384.c
+
+   HMAC-SHA3-384 message authentication code.
+
+   Copyright (C) 2015 Red Hat, Inc.
+
+   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 "hmac.h"
+
+void
+hmac_sha3_384_set_key(struct hmac_sha3_384_ctx *ctx,
+		    size_t key_length, const uint8_t *key)
+{
+  HMAC_SET_KEY(ctx, &nettle_sha3_384, key_length, key);
+}
+
+void
+hmac_sha3_384_digest(struct hmac_sha3_384_ctx *ctx,
+		   size_t length, uint8_t *digest)
+{
+  HMAC_DIGEST(ctx, &nettle_sha3_384, length, digest);
+}
+
+void
+hmac_sha3_384_update(struct hmac_sha3_384_ctx *ctx,
+		   size_t length, const uint8_t *data)
+{
+  sha3_384_update(&ctx->state, length, data);
+}
+
diff --git a/hmac-sha3-512.c b/hmac-sha3-512.c
new file mode 100644
index 0000000..e4dc75b
--- /dev/null
+++ b/hmac-sha3-512.c
@@ -0,0 +1,60 @@
+/* hmac-sha3-512.c
+
+   HMAC-SHA3-512 message authentication code.
+
+   Copyright (C) 2015 Red Hat, Inc.
+
+   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 "hmac.h"
+
+void
+hmac_sha3_512_set_key(struct hmac_sha3_512_ctx *ctx,
+		    size_t key_length, const uint8_t *key)
+{
+  HMAC_SET_KEY(ctx, &nettle_sha3_512, key_length, key);
+}
+
+void
+hmac_sha3_512_digest(struct hmac_sha3_512_ctx *ctx,
+		   size_t length, uint8_t *digest)
+{
+  HMAC_DIGEST(ctx, &nettle_sha3_512, length, digest);
+}
+
+void
+hmac_sha3_512_update(struct hmac_sha3_512_ctx *ctx,
+		   size_t length, const uint8_t *data)
+{
+  sha3_512_update(&ctx->state, length, data);
+}
+
diff --git a/hmac.h b/hmac.h
index 40a8e77..6f04466 100644
--- a/hmac.h
+++ b/hmac.h
@@ -40,6 +40,7 @@
 #include "ripemd160.h"
 #include "sha1.h"
 #include "sha2.h"
+#include "sha3.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -68,6 +69,16 @@ extern "C" {
 #define hmac_sha512_set_key nettle_hmac_sha512_set_key
 #define hmac_sha512_update nettle_hmac_sha512_update
 #define hmac_sha512_digest nettle_hmac_sha512_digest
+#define hmac_sha3_224_set_key nettle_hmac_sha3_224_set_key
+#define hmac_sha3_224_digest nettle_hmac_sha3_224_digest
+#define hmac_sha3_256_set_key nettle_hmac_sha3_256_set_key
+#define hmac_sha3_256_update nettle_hmac_sha3_256_update
+#define hmac_sha3_256_digest nettle_hmac_sha3_256_digest
+#define hmac_sha3_384_set_key nettle_hmac_sha3_384_set_key
+#define hmac_sha3_384_digest nettle_hmac_sha3_384_digest
+#define hmac_sha3_512_set_key nettle_hmac_sha3_512_set_key
+#define hmac_sha3_512_update nettle_hmac_sha3_512_update
+#define hmac_sha3_512_digest nettle_hmac_sha3_512_digest
 
 void
 hmac_set_key(void *outer, void *inner, void *state,
@@ -203,6 +214,67 @@ void
 hmac_sha384_digest(struct hmac_sha512_ctx *ctx,
 		   size_t length, uint8_t *digest);
 
+/* SHA-3 */
+
+struct hmac_sha3_256_ctx HMAC_CTX(struct sha3_256_ctx);
+
+void
+hmac_sha3_256_set_key(struct hmac_sha3_256_ctx *ctx,
+		    size_t key_length, const uint8_t *key);
+
+void
+hmac_sha3_256_update(struct hmac_sha3_256_ctx *ctx,
+		   size_t length, const uint8_t *data);
+
+void
+hmac_sha3_256_digest(struct hmac_sha3_256_ctx *ctx,
+		   size_t length, uint8_t *digest);
+
+/* hmac-sha224 */
+struct hmac_sha3_224_ctx HMAC_CTX(struct sha3_224_ctx);
+
+void
+hmac_sha3_224_set_key(struct hmac_sha3_224_ctx *ctx,
+		    size_t key_length, const uint8_t *key);
+
+void
+hmac_sha3_224_update(struct hmac_sha3_224_ctx *ctx,
+		   size_t length, const uint8_t *data);
+
+void
+hmac_sha3_224_digest(struct hmac_sha3_224_ctx *ctx,
+		   size_t length, uint8_t *digest);
+
+/* hmac-sha512 */
+struct hmac_sha3_512_ctx HMAC_CTX(struct sha3_512_ctx);
+
+void
+hmac_sha3_512_set_key(struct hmac_sha3_512_ctx *ctx,
+		    size_t key_length, const uint8_t *key);
+
+void
+hmac_sha3_512_update(struct hmac_sha3_512_ctx *ctx,
+		   size_t length, const uint8_t *data);
+
+void
+hmac_sha3_512_digest(struct hmac_sha3_512_ctx *ctx,
+		   size_t length, uint8_t *digest);
+
+/* hmac-sha384 */
+struct hmac_sha3_384_ctx HMAC_CTX(struct sha3_384_ctx);
+
+void
+hmac_sha3_384_set_key(struct hmac_sha3_384_ctx *ctx,
+		    size_t key_length, const uint8_t *key);
+
+void
+hmac_sha3_384_update(struct hmac_sha3_384_ctx *ctx,
+		   size_t length, const uint8_t *data);
+
+void
+hmac_sha3_384_digest(struct hmac_sha3_384_ctx *ctx,
+		   size_t length, uint8_t *digest);
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.4.3

