[PATCH 3/3] crypto: Crypto API implementation of Ascon-Hash256

Rusydi H. Makarim posted 3 patches 1 day, 20 hours ago
[PATCH 3/3] crypto: Crypto API implementation of Ascon-Hash256
Posted by Rusydi H. Makarim 1 day, 20 hours ago
	This commit implements Ascon-Hash256 for Crypto API

Signed-off-by: Rusydi H. Makarim <rusydi.makarim@kriptograf.id>
---
 crypto/Kconfig      |  7 +++++
 crypto/Makefile     |  1 +
 crypto/ascon_hash.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+)

diff --git a/crypto/Kconfig b/crypto/Kconfig
index 2e5b195b1b06..e671b5575535 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -1000,6 +1000,13 @@ config CRYPTO_SHA3
 	help
 	  SHA-3 secure hash algorithms (FIPS 202, ISO/IEC 10118-3)
 
+config CRYPTO_ASCON_HASH
+	tristate "Ascon-Hash"
+	select CRYPTO_HASH
+	select CRYPTO_LIB_ASCON_HASH
+	help
+	  Ascon-Hash secure hash algorithms (NIST SP 800-232)
+
 config CRYPTO_SM3_GENERIC
 	tristate "SM3 (ShangMi 3)"
 	select CRYPTO_HASH
diff --git a/crypto/Makefile b/crypto/Makefile
index 16a35649dd91..a697a92d2092 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -82,6 +82,7 @@ obj-$(CONFIG_CRYPTO_SHA3) += sha3.o
 obj-$(CONFIG_CRYPTO_SM3_GENERIC) += sm3_generic.o
 obj-$(CONFIG_CRYPTO_STREEBOG) += streebog_generic.o
 obj-$(CONFIG_CRYPTO_WP512) += wp512.o
+obj-$(CONFIG_CRYPTO_ASCON_HASH) += ascon_hash.o
 CFLAGS_wp512.o := $(call cc-option,-fno-schedule-insns)  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79149
 obj-$(CONFIG_CRYPTO_BLAKE2B) += blake2b.o
 obj-$(CONFIG_CRYPTO_ECB) += ecb.o
diff --git a/crypto/ascon_hash.c b/crypto/ascon_hash.c
new file mode 100644
index 000000000000..2fa5e762fbc1
--- /dev/null
+++ b/crypto/ascon_hash.c
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Crypto API support for Ascon-Hash256
+ * (https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-232.pdf)
+ *
+ * Copyright (C) Rusydi H. Makarim <rusydi.makarim@kriptograf.id>
+ */
+
+#include <crypto/internal/hash.h>
+#include <crypto/ascon_hash.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#define ASCON_HASH256_CTX(desc) ((struct ascon_hash256_ctx *)shash_desc_ctx(desc))
+
+static int crypto_ascon_hash256_init(struct shash_desc *desc)
+{
+	ascon_hash256_init(ASCON_HASH256_CTX(desc));
+	return 0;
+}
+
+static int crypto_ascon_hash256_update(struct shash_desc *desc, const u8 *data,
+				       unsigned int len)
+{
+	ascon_hash256_update(ASCON_HASH256_CTX(desc), data, len);
+	return 0;
+}
+
+static int crypto_ascon_hash256_final(struct shash_desc *desc, u8 *out)
+{
+	ascon_hash256_final(ASCON_HASH256_CTX(desc), out);
+	return 0;
+}
+
+static int crypto_ascon_hash256_digest(struct shash_desc *desc, const u8 *data,
+				       unsigned int len, u8 *out)
+{
+	ascon_hash256(data, len, out);
+	return 0;
+}
+
+static int crypto_ascon_hash256_export_core(struct shash_desc *desc, void *out)
+{
+	memcpy(out, ASCON_HASH256_CTX(desc), sizeof(struct ascon_hash256_ctx));
+	return 0;
+}
+
+static int crypto_ascon_hash256_import_core(struct shash_desc *desc,
+					    const void *in)
+{
+	memcpy(ASCON_HASH256_CTX(desc), in, sizeof(struct ascon_hash256_ctx));
+	return 0;
+}
+
+static struct shash_alg algs[] = { {
+	.digestsize = ASCON_HASH256_DIGEST_SIZE,
+	.init = crypto_ascon_hash256_init,
+	.update = crypto_ascon_hash256_update,
+	.final = crypto_ascon_hash256_final,
+	.digest = crypto_ascon_hash256_digest,
+	.export_core = crypto_ascon_hash256_export_core,
+	.import_core = crypto_ascon_hash256_import_core,
+	.descsize = sizeof(struct ascon_hash256_ctx),
+	.base.cra_name = "ascon-hash256",
+	.base.cra_driver_name = "ascon-hash256-lib",
+	.base.cra_blocksize = ASCON_HASH256_BLOCK_SIZE,
+	.base.cra_module = THIS_MODULE,
+} };
+
+static int __init crypto_ascon_hash256_mod_init(void)
+{
+	return crypto_register_shashes(algs, ARRAY_SIZE(algs));
+}
+module_init(crypto_ascon_hash256_mod_init);
+
+static void __exit crypto_ascon_hash256_mod_exit(void)
+{
+	crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
+}
+module_exit(crypto_ascon_hash256_mod_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Crypto API support for Ascon-Hash256");
+
+MODULE_ALIAS_CRYPTO("ascon-hash256");
+MODULE_ALIAS_CRYPTO("ascon-hash256-lib");

-- 
2.52.0