Skip to contentSkip to conversation

Add SHA-3 hash algorithm

PATCH In review Jeff Garzik 4 participants 6 messages Oct 3, 2012 – Apr 4, 2013 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add SHA-3 hash algorithm

Whee -- SHA-3 is out! I wanted to explore the new toy a bit, and
so, here is a blatantly untested rough draft of SHA-3 kernel support.

Why rough draft? Because answers to the questions below will inform a more polished version.

Code notes and questions:

1) tcrypt setup blatantly wrong. What is the best setup here? Define a separate entry for each digest length? Is there some special string descriptor format that is desired, like "sha3-256" or "sha3(256)"?

2) Digest and block size are easily variable, as shown below... do we want hand-craft individual versions for each -- sha3_256.c, sha3_512.c, etc.?

3) Is it even feasible for struct shash_alg to have a dynamic (filled in at context init time, not driver registration time) digestsize and cra_blocksize? That would permit a single shash_alg for all sha3.

4) Original implementation from readable_keccak.tgz (link below). The official sources have a bazillion different flavors for various architectures and bit sizes, and the code is not pretty. I wanted to start small and readable, and _then_ branch out into x86[-64]-specific versions, etc. as users and use cases appear.

Commit e52113b7b4ace50ab586b426098c6d69d75c263a
Branch sha3
Repo git://git.kernel.org/pub/scm/linux/kernel/git/jgarzik/linux.git

References:

	http://keccak.noekeon.org/
	http://www.mjos.fi/dist/readable_keccak.tgz
	http://www.nist.gov/itl/csd/sha-100212.cfm
 crypto/Kconfig        |    6 +
 crypto/Makefile       |    1 
 crypto/sha3_generic.c |  280 ++++++++++++++++++++++++++++++++++++++++++++++++++
 crypto/tcrypt.c       |   14 ++
 include/crypto/sha3.h |   26 ++++
 5 files changed, 326 insertions(+), 1 deletion(-)
crypto/Kconfig +6 -0
index a323805..97f5e75 100644
@@ -457,6 +457,12 @@ config CRYPTO_SHA512
 	  This code also includes SHA-384, a 384 bit hash with 192 bits
 	  of security against collision attacks.
 
+config CRYPTO_SHA3
+	tristate "SHA3 digest algorithm"
+	select CRYPTO_HASH
+	help
+	  SHA-3 secure hash standard.
+
 config CRYPTO_TGR192
 	tristate "Tiger digest algorithms"
 	select CRYPTO_HASH
crypto/Makefile +1 -0
index 30f33d6..65150d1 100644
@@ -45,6 +45,7 @@ obj-$(CONFIG_CRYPTO_RMD320) += rmd320.o
 obj-$(CONFIG_CRYPTO_SHA1) += sha1_generic.o
 obj-$(CONFIG_CRYPTO_SHA256) += sha256_generic.o
 obj-$(CONFIG_CRYPTO_SHA512) += sha512_generic.o
+obj-$(CONFIG_CRYPTO_SHA3) += sha3_generic.o
 obj-$(CONFIG_CRYPTO_WP512) += wp512.o
 obj-$(CONFIG_CRYPTO_TGR192) += tgr192.o
 obj-$(CONFIG_CRYPTO_GF128MUL) += gf128mul.o
crypto/sha3_generic.c new file +280 -0

280 lines not on this page · raw message

crypto/tcrypt.c +13 -1
index 5cf2ccb..4fa2a4c 100644
@@ -64,7 +64,7 @@ static char *check[] = {
 	"cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
 	"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
 	"camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
-	"lzo", "cts", "zlib", NULL
+	"lzo", "cts", "zlib", "sha3", NULL
 };
 
 static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
@@ -1165,6 +1165,10 @@ static int do_test(int m)
 		ret += tcrypt_test("rfc4309(ccm(aes))");
 		break;
 
+	case 46:
+		ret += tcrypt_test("sha3");
+		break;
+
 	case 100:
 		ret += tcrypt_test("hmac(md5)");
 		break;
@@ -1434,6 +1438,10 @@ static int do_test(int m)
 		test_hash_speed("ghash-generic", sec, hash_speed_template_16);
 		if (mode > 300 && mode < 400) break;
 
+	case 319:
+		test_hash_speed("sha3", sec, generic_hash_speed_template);
+		if (mode > 300 && mode < 400) break;
+
 	case 399:
 		break;
 
@@ -1508,6 +1516,10 @@ static int do_test(int m)
 		test_ahash_speed("rmd320", sec, generic_hash_speed_template);
 		if (mode > 400 && mode < 500) break;
 
+	case 418:
+		test_ahash_speed("sha3", sec, generic_hash_speed_template);
+		if (mode > 400 && mode < 500) break;
+
 	case 499:
 		break;
 
include/crypto/sha3.h new file +26 -0
new file mode 100644
index 0000000..3004e94
@@ -0,0 +1,26 @@
+#ifndef __CRYPTO_SHA3_H__
+#define __CRYPTO_SHA3_H__
+
+#define SHA3_224_DIGEST_SIZE	(224 / 8)
+#define SHA3_224_BLOCK_SIZE	(200 - 2 * SHA3_224_DIGEST_SIZE)
+
+#define SHA3_256_DIGEST_SIZE	(256 / 8)
+#define SHA3_256_BLOCK_SIZE	(200 - 2 * SHA3_256_DIGEST_SIZE)
+
+#define SHA3_384_DIGEST_SIZE	(384 / 8)
+#define SHA3_384_BLOCK_SIZE	(200 - 2 * SHA3_384_DIGEST_SIZE)
+
+#define SHA3_512_DIGEST_SIZE	(512 / 8)
+#define SHA3_512_BLOCK_SIZE	(200 - 2 * SHA3_512_DIGEST_SIZE)
+
+struct sha3_state {
+	u64		st[25];
+	unsigned int	md_len;
+	unsigned int	rsiz;
+	unsigned int	rsizw;
+
+	unsigned int	partial;
+	u8		buf[SHA3_224_BLOCK_SIZE];
+};
+
+#endif /* __CRYPTO_SHA3_H__ */
maintainer
#1 lore raw

From: Jeff Garzik <[email protected]>
Date: Wed, 3 Oct 2012 01:45:42 -0400

Quoted 3 lines

1) tcrypt setup blatantly wrong. What is the best setup here? Define a separate entry for each digest length? Is there some special string descriptor format that is desired, like "sha3-256" or "sha3(256)"?

Good question. The base name should probably be something without dashes. Maybe "sha3_256", but yeah "sha3256" would look rediculous.

#2 lore raw
Quoted 9 lines · David Miller

From: Jeff Garzik <[email protected]>
Date: Wed, 3 Oct 2012 01:45:42 -0400

Quoted 3 lines

1) tcrypt setup blatantly wrong. What is the best setup here? Define a separate entry for each digest length? Is there some special string descriptor format that is desired, like "sha3-256" or "sha3(256)"?

Good question. The base name should probably be something without dashes. Maybe "sha3_256", but yeah "sha3256" would look rediculous.

Well, the more basic question was... what to do when the digest length is easily variable, vis a vis kernel hash APIs?

Keccak message digest size may fall anywhere within the range 8 bits - 1600 bits at runtime. You choose the digest size when you init the context. In contrast, the kernel interface appears to require a hardcoded size, chosen at driver compile time.

My patch picks sizes found in common use, consistent with existing kernel practice. However, it is valid for another Keccak user to produce a 1600 bit hash, or a 1592 bit hash, or a 1584 bit hash, etc., etc.

Maybe my patch is the best we can do in the current kernel, if dynamic digest size is not currently possible. Register "sha3_224", "sha3_256", ... as you describe, and wait for actual users to appear with unsupported digest sizes.

Jeff

#3 lore raw
Quoted 5 lines · Jeff Garzik

Maybe my patch is the best we can do in the current kernel, if dynamic digest size is not currently possible. Register "sha3_224", "sha3_256", ... as you describe, and wait for actual users to appear with unsupported digest sizes.

Let's see what people use before we do anything more fancy.

If the variants really start proliferating, we can add a template called "trunc" and then have things like "trunc(sha3,224)", etc.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#4 lore raw
Quoted 11 lines · Herbert Xu
Quoted 5 lines · Jeff Garzik

Maybe my patch is the best we can do in the current kernel, if dynamic digest size is not currently possible. Register "sha3_224", "sha3_256", ... as you describe, and wait for actual users to appear with unsupported digest sizes.

Let's see what people use before we do anything more fancy.

If the variants really start proliferating, we can add a template called "trunc" and then have things like "trunc(sha3,224)", etc.

If they start proliferating, you really just want a single "sha3(n)", one single shash_alg registered at driver init time.

Jeff

#5 lore raw
In reply to Jeff Garzik
Quoted 6 lines · Jeff Garzik

Whee -- SHA-3 is out! I wanted to explore the new toy a bit, and
so, here is a blatantly untested rough draft of SHA-3 kernel support.

Why rough draft? Because answers to the questions below will inform a more polished version.

Just to update people... this has been in a holding pattern, because apparently there are revisions to SHA-3 coming down the pipe. They want to address preimage resistance, and make things faster in hardware.

Random quote from NIST, on the NIST hash-forum, which doesn't provide detail but does summarize general feeling: "As best we can tell, continuing to pay that performance penalty for all future uses of SHA3 has no benefit. (All this is a longwinded way of saying: we were wrong, but hopefully we got better.)"

Jeff