Skip to contentSkip to conversation

crypto: streebog - add Streebog hash function

PATCH Superseded Vitaly Chikunov 3 participants 12 messages Oct 10, 2018 – Nov 5, 2018

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover crypto: streebog - add Streebog hash function

Add GOST/IETF Streebog hash function (GOST R 34.11-2012, RFC 6986) generic hash transformation with appropriate testmgr and tcrypt tests and vectors.

Streebog Hash Function (GOST R 34.11-2012, RFC 6986) is one of the Russian cryptographic standard algorithms (called GOST algorithms).

References:
https://tc26.ru/upload/iblock/fed/feddbb4d26b685903faa2ba11aea43f6.pdf
https://tools.ietf.org/html/rfc6986

Tested using testmgr and tcrypt on x86_64, arm, arm64, and s390x on qemu.

Changes from v1:
- Fix of big-endian problem in add512.
- More architectures are tested, including big-endian and 32-bit.

Vitaly Chikunov (2):
crypto: streebog - add Streebog hash function
crypto: streebog - add Streebog test vectors

 crypto/Kconfig            |   12 +
 crypto/Makefile           |    1 +
 crypto/streebog_generic.c | 1142 +++++++++++++++++++++++++++++++++++++++++++++
 crypto/tcrypt.c           |   27 ++
 crypto/testmgr.c          |   12 +
 crypto/testmgr.h          |   71 +++
 include/crypto/streebog.h |   34 ++
 7 files changed, 1299 insertions(+)
 create mode 100644 crypto/streebog_generic.c
 create mode 100644 include/crypto/streebog.h
Signature · 1 line
2.11.0
#1 lore raw

PATCH v2 1/2 crypto: streebog - add Streebog hash function

Add GOST/IETF Streebog hash function (GOST R 34.11-2012, RFC 6986) generic hash transformation.

---
 crypto/Kconfig            |   12 +
 crypto/Makefile           |    1 +
 crypto/streebog_generic.c | 1142 +++++++++++++++++++++++++++++++++++++++++++++
 include/crypto/streebog.h |   34 ++
 4 files changed, 1189 insertions(+)
 create mode 100644 crypto/streebog_generic.c
 create mode 100644 include/crypto/streebog.h
crypto/Kconfig +12 -0
index f3e40ac56d93..587c32f65cfe 100644
@@ -986,6 +986,18 @@ config CRYPTO_SM3
 	  http://www.oscca.gov.cn/UpFile/20101222141857786.pdf
 	  https://datatracker.ietf.org/doc/html/draft-shen-sm3-hash
 
+config CRYPTO_STREEBOG
+	tristate "Streebog Hash Function"
+	select CRYPTO_HASH
+	help
+	  Streebog Hash Function (GOST R 34.11-2012, RFC 6986) is one of the Russian
+	  cryptographic standard algorithms (called GOST algorithms).
+	  This setting enables two hash algorithms with 256 and 512 bits output.
+
+	  References:
+	  https://tc26.ru/upload/iblock/fed/feddbb4d26b685903faa2ba11aea43f6.pdf
+	  https://tools.ietf.org/html/rfc6986
+
 config CRYPTO_TGR192
 	tristate "Tiger digest algorithms"
 	select CRYPTO_HASH
crypto/Makefile +1 -0
index 6d1d40eeb964..65be945da35c 100644
@@ -70,6 +70,7 @@ obj-$(CONFIG_CRYPTO_SHA256) += sha256_generic.o
 obj-$(CONFIG_CRYPTO_SHA512) += sha512_generic.o
 obj-$(CONFIG_CRYPTO_SHA3) += sha3_generic.o
 obj-$(CONFIG_CRYPTO_SM3) += sm3_generic.o
+obj-$(CONFIG_CRYPTO_STREEBOG) += streebog_generic.o
 obj-$(CONFIG_CRYPTO_WP512) += wp512.o
 CFLAGS_wp512.o := $(call cc-option,-fno-schedule-insns)  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79149
 obj-$(CONFIG_CRYPTO_TGR192) += tgr192.o
crypto/streebog_generic.c new file +1142 -0

1,142 lines not on this page · raw message

include/crypto/streebog.h new file +34 -0
new file mode 100644
index 000000000000..8d5c676225b0
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause */
+/*
+ * Copyright (c) 2013 Alexey Degtyarev <[email protected]>
+ * Copyright (c) 2018 Vitaly Chikunov <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of 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.
+ */
+
+#ifndef _CRYPTO_STREEBOG_H_
+#define _CRYPTO_STREEBOG_H_
+
+#include <linux/types.h>
+
+#define STREEBOG256_DIGEST_SIZE	32
+#define STREEBOG512_DIGEST_SIZE	64
+#define STREEBOG_BLOCK_SIZE	64
+
+struct streebog_uint512 {
+	u64 qword[8];
+} __aligned(16);
+
+struct streebog_state {
+	u8 buffer[64] __aligned(16);
+	struct streebog_uint512 hash;
+	struct streebog_uint512 h;
+	struct streebog_uint512 N;
+	struct streebog_uint512 Sigma;
+	size_t bufsize;
+};
+
+#endif /* !_CRYPTO_STREEBOG_H_ */
Signature · 1 line
2.11.0
#2 lore raw
Quoted 60 lines · Vitaly Chikunov

Add GOST/IETF Streebog hash function (GOST R 34.11-2012, RFC 6986) generic hash transformation.

---
 crypto/Kconfig            |   12 +
 crypto/Makefile           |    1 +
 crypto/streebog_generic.c | 1142 +++++++++++++++++++++++++++++++++++++++++++++
 include/crypto/streebog.h |   34 ++
 4 files changed, 1189 insertions(+)
 create mode 100644 crypto/streebog_generic.c
 create mode 100644 include/crypto/streebog.h
crypto/streebog_generic.c +43 -0
...
+#define XLPS(x, y, data) { \
+	register u64 r0, r1, r2, r3, r4, r5, r6, r7; \
+	int _i; \
+	\
+	r0 = le64_to_cpu((x)->qword[0] ^ (y)->qword[0]); \
+	r1 = le64_to_cpu((x)->qword[1] ^ (y)->qword[1]); \
+	r2 = le64_to_cpu((x)->qword[2] ^ (y)->qword[2]); \
+	r3 = le64_to_cpu((x)->qword[3] ^ (y)->qword[3]); \
+	r4 = le64_to_cpu((x)->qword[4] ^ (y)->qword[4]); \
+	r5 = le64_to_cpu((x)->qword[5] ^ (y)->qword[5]); \
+	r6 = le64_to_cpu((x)->qword[6] ^ (y)->qword[6]); \
+	r7 = le64_to_cpu((x)->qword[7] ^ (y)->qword[7]); \
+	\
+	for (_i = 0; _i <= 7; _i++) { \
+		(data)->qword[_i]  = cpu_to_le64(Ax[0][r0 & 0xFF]); \
+		(data)->qword[_i] ^= cpu_to_le64(Ax[1][r1 & 0xFF]); \
+		(data)->qword[_i] ^= cpu_to_le64(Ax[2][r2 & 0xFF]); \
+		(data)->qword[_i] ^= cpu_to_le64(Ax[3][r3 & 0xFF]); \
+		(data)->qword[_i] ^= cpu_to_le64(Ax[4][r4 & 0xFF]); \
+		(data)->qword[_i] ^= cpu_to_le64(Ax[5][r5 & 0xFF]); \
+		(data)->qword[_i] ^= cpu_to_le64(Ax[6][r6 & 0xFF]); \
+		(data)->qword[_i] ^= cpu_to_le64(Ax[7][r7 & 0xFF]); \
+		r0 >>= 8; \
+		r1 >>= 8; \
+		r2 >>= 8; \
+		r3 >>= 8; \
+		r4 >>= 8; \
+		r5 >>= 8; \
+		r6 >>= 8; \
+		r7 >>= 8; \
+	} \
+}
...
+static const struct streebog_uint512 C[12] = {
+	{ {
+		 cpu_to_le64(0xdd806559f2a64507ULL),
+		 cpu_to_le64(0x05767436cc744d23ULL),
+		 cpu_to_le64(0xa2422a08a460d315ULL),
+		 cpu_to_le64(0x4b7ce09192676901ULL),
+		 cpu_to_le64(0x714eb88d7585c4fcULL),
+		 cpu_to_le64(0x2f6a76432e45d016ULL),
+		 cpu_to_le64(0xebcb2f81c0657c1fULL),
+		 cpu_to_le64(0xb1085bda1ecadae9ULL)
...
+static const unsigned long long Ax[8][256] __aligned(16) = {

crypto/streebog_generic.c

Quoted 10 lines
crypto/streebog_generic.c +9 -0
+	{
+	0xd01f715b5c7ef8e6ULL, 0x16fa240980778325ULL, 0xa8a42e857ee049c8ULL,
+	0x6ac1068fa186465bULL, 0x6e417bd7a2e9320bULL, 0x665c8167a437daabULL,
+	0x7666681aa89617f6ULL, 0x4b959163700bdcf5ULL, 0xf14be6b78df36248ULL,
+	0xc585bd689a625cffULL, 0x9557d7fca67d82cbULL, 0x89f0b969af6dd366ULL,
+	0xb0833d48749f6c35ULL, 0xa1998c23b1ecbc7cULL, 0x8d70c431ac02a736ULL,
+	0xd6dfbc2fd0a8b69eULL, 0x37aeb3e551fa198bULL, 0x0b7d128a40b5cf9cULL,
+	0x5a8f2008b5780cbcULL, 0xedec882284e333e5ULL, 0xd25fc177d3c7c2ceULL,
+	0x5e0f5d50b61778ecULL, 0x1d873683c0c24cb9ULL, 0xad040bcbb45d208cULL,
...

It's possible to move cpu_to_le64 from XLPS into each value of Ax[]. This would increase source size but slightly improve performance. Does it worth the change? My opinion is to leave it as is since big-endian architectures are not main targets.

Thanks,

#3 lore raw

PATCH v2 2/2 crypto: streebog - add Streebog test vectors

In reply to Vitaly Chikunov

Add testmgr and tcrypt tests and vectors for Streebog hash function from RFC 6986 and GOST R 34.11-2012.

---
 crypto/tcrypt.c  | 27 +++++++++++++++++++++
 crypto/testmgr.c | 12 ++++++++++
 crypto/testmgr.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+)
crypto/tcrypt.c +27 -0
index bdde95e8d369..d66cb167daa2 100644
@@ -77,6 +77,7 @@ static char *check[] = {
 	"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
 	"camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
 	"lzo", "cts", "zlib", "sha3-224", "sha3-256", "sha3-384", "sha3-512",
+	"sb256", "sb512",
 	NULL
 };
 
@@ -1914,6 +1915,14 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb)
 		ret += tcrypt_test("sm3");
 		break;
 
+	case 53:
+		ret += tcrypt_test("sb256");
+		break;
+
+	case 54:
+		ret += tcrypt_test("sb512");
+		break;
+
 	case 100:
 		ret += tcrypt_test("hmac(md5)");
 		break;
@@ -2392,6 +2401,14 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb)
 		test_hash_speed("sm3", sec, generic_hash_speed_template);
 		if (mode > 300 && mode < 400) break;
 		/* fall through */
+	case 327:
+		test_hash_speed("sb256", sec, generic_hash_speed_template);
+		if (mode > 300 && mode < 400) break;
+		/* fall through */
+	case 328:
+		test_hash_speed("sb512", sec, generic_hash_speed_template);
+		if (mode > 300 && mode < 400) break;
+		/* fall through */
 	case 399:
 		break;
 
@@ -2505,6 +2522,16 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb)
 				    num_mb);
 		if (mode > 400 && mode < 500) break;
 		/* fall through */
+	case 426:
+		test_mb_ahash_speed("sb256", sec, generic_hash_speed_template,
+				    num_mb);
+		if (mode > 400 && mode < 500) break;
+		/* fall through */
+	case 427:
+		test_mb_ahash_speed("sb512", sec, generic_hash_speed_template,
+				    num_mb);
+		if (mode > 400 && mode < 500) break;
+		/* fall through */
 	case 499:
 		break;
 
crypto/testmgr.c +12 -0
index a1d42245082a..6efb0a218b41 100644
@@ -3428,6 +3428,18 @@ static const struct alg_test_desc alg_test_descs[] = {
 			.cipher = __VECS(salsa20_stream_tv_template)
 		}
 	}, {
+		.alg = "sb256",
+		.test = alg_test_hash,
+		.suite = {
+			.hash = __VECS(sb256_tv_template)
+		}
+	}, {
+		.alg = "sb512",
+		.test = alg_test_hash,
+		.suite = {
+			.hash = __VECS(sb512_tv_template)
+		}
+	}, {
 		.alg = "sha1",
 		.test = alg_test_hash,
 		.fips_allowed = 1,
crypto/testmgr.h +71 -0

77 lines not on this page · raw message

Signature · 1 line
2.11.0
#4 lore raw

Re: [PATCH v2 2/2] crypto: streebog - add Streebog test vectors

Quoted 16 lines · Vitaly Chikunov

Add testmgr and tcrypt tests and vectors for Streebog hash function from RFC 6986 and GOST R 34.11-2012.

---
 crypto/tcrypt.c  | 27 +++++++++++++++++++++
 crypto/testmgr.c | 12 ++++++++++
 crypto/testmgr.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+)
crypto/tcrypt.c +0 -0
index bdde95e8d369..d66cb167daa2 100644
@@ -1914,6 +1915,14 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb)
 		ret += tcrypt_test("sm3");

crypto/tcrypt.c static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb) · line 1925

Quoted 10 lines
crypto/tcrypt.c +8 -0
 		break;
 
+	case 53:
+		ret += tcrypt_test("sb256");
+		break;
+
+	case 54:
+		ret += tcrypt_test("sb512");
+		break;
+

I am requested by Dmitry Eremin-Solenikov off-list to also add hmac(sb256) and hmac(sb512) tests. I think this is a reasonable request since IMA have such a mode of use and IMA is planned as one of the main users of this hash. I may resend v3 with this, or as follow-up PATCH as maintainers would prefer.

Thanks,

maintainer
#5 lore raw
In reply to Vitaly Chikunov
Quoted 4 lines · Vitaly Chikunov

Add GOST/IETF Streebog hash function (GOST R 34.11-2012, RFC 6986) generic hash transformation with appropriate testmgr and tcrypt tests and vectors.

Quoted 8 lines

Streebog Hash Function (GOST R 34.11-2012, RFC 6986) is one of the Russian cryptographic standard algorithms (called GOST algorithms).

References:
https://tc26.ru/upload/iblock/fed/feddbb4d26b685903faa2ba11aea43f6.pdf
https://tools.ietf.org/html/rfc6986

Tested using testmgr and tcrypt on x86_64, arm, arm64, and s390x on qemu.

We don't add algorithms purely used by user-space.

So please clarify who is going to be using this algorithm *within* the kernel.

Thanks,

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
#6 lore raw
Quoted 12 lines · Herbert Xu
Quoted 11 lines · Vitaly Chikunov

Add GOST/IETF Streebog hash function (GOST R 34.11-2012, RFC 6986) generic hash transformation with appropriate testmgr and tcrypt tests and vectors.

Streebog Hash Function (GOST R 34.11-2012, RFC 6986) is one of the Russian cryptographic standard algorithms (called GOST algorithms).

References:
https://tc26.ru/upload/iblock/fed/feddbb4d26b685903faa2ba11aea43f6.pdf
https://tools.ietf.org/html/rfc6986

Quoted 6 lines
Quoted 1 line

Tested using testmgr and tcrypt on x86_64, arm, arm64, and s390x on qemu.

We don't add algorithms purely used by user-space.

So please clarify who is going to be using this algorithm *within* the kernel.

It is the first part of attempts to add to the Integrity subsystem ability of verifying file and module signatures by Russian GOST algorithms.

Thanks,

maintainer
#7 lore raw
Quoted 4 lines · Vitaly Chikunov

It is the first part of attempts to add to the Integrity subsystem ability of verifying file and module signatures by Russian GOST algorithms.

It would be better if these patches are posted together. That way we don't end up with a situation where the algorithm goes into the kernel but the ultimate user is rejected.

Thanks,

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
#8 lore raw

Herbert,

Quoted 9 lines · Herbert Xu
Quoted 4 lines · Vitaly Chikunov

It is the first part of attempts to add to the Integrity subsystem ability of verifying file and module signatures by Russian GOST algorithms.

It would be better if these patches are posted together. That way we don't end up with a situation where the algorithm goes into the kernel but the ultimate user is rejected.

IMA does not need any particular patching inside of their subtree to support new hash, because it is using any hash registered in Hash Info which is still under crypto subtree. I added appropriate patch into v3 which is already posted a week ago, and tested it to work correctly with ima_appraise=fix.

Thanks,

maintainer
#9 lore raw
Quoted 13 lines · Vitaly Chikunov

Herbert,

Quoted 9 lines · Herbert Xu
Quoted 4 lines · Vitaly Chikunov

It is the first part of attempts to add to the Integrity subsystem ability of verifying file and module signatures by Russian GOST algorithms.

It would be better if these patches are posted together. That way we don't end up with a situation where the algorithm goes into the kernel but the ultimate user is rejected.

Quoted 5 lines

IMA does not need any particular patching inside of their subtree to support new hash, because it is using any hash registered in Hash Info which is still under crypto subtree. I added appropriate patch into v3 which is already posted a week ago, and tested it to work correctly with ima_appraise=fix.

That's true, but the target subsystem should be made aware of the new usage.

Thanks, Herbert.

Mimi

#10 lore raw

Mimi,

Quoted 12 lines · Mimi Zohar
Quoted 11 lines · Vitaly Chikunov
Quoted 9 lines · Herbert Xu
Quoted 4 lines · Vitaly Chikunov

It is the first part of attempts to add to the Integrity subsystem ability of verifying file and module signatures by Russian GOST algorithms.

It would be better if these patches are posted together. That way we don't end up with a situation where the algorithm goes into the kernel but the ultimate user is rejected.

Quoted 8 lines
Quoted 5 lines

IMA does not need any particular patching inside of their subtree to support new hash, because it is using any hash registered in Hash Info which is still under crypto subtree. I added appropriate patch into v3 which is already posted a week ago, and tested it to work correctly with ima_appraise=fix.

That's true, but the target subsystem should be made aware of the new usage.

Did you mean by re-sending the patch with Cc to linux-integrity? Or something more?

Thanks,

maintainer
#11 lore raw
Quoted 21 lines · Vitaly Chikunov

Mimi,

Quoted 18 lines · Mimi Zohar
Quoted 16 lines · Vitaly Chikunov
Quoted 9 lines · Herbert Xu
Quoted 4 lines · Vitaly Chikunov

It is the first part of attempts to add to the Integrity subsystem ability of verifying file and module signatures by Russian GOST algorithms.

It would be better if these patches are posted together. That way we don't end up with a situation where the algorithm goes into the kernel but the ultimate user is rejected.

IMA does not need any particular patching inside of their subtree to support new hash, because it is using any hash registered in Hash Info which is still under crypto subtree. I added appropriate patch into v3 which is already posted a week ago, and tested it to work correctly with ima_appraise=fix.

Quoted 5 lines
Quoted 2 lines

That's true, but the target subsystem should be made aware of the new usage.

Did you mean by re-sending the patch with Cc to linux-integrity? Or something more?

Just as future reference.  For now, please feel free to add my Reviewed-by on the "crypto: streebog - register Streebog in hash info for IMA" patch.

Mimi