Skip to contentSkip to conversation

arm64: SHA-224/SHA-256 using ARMv8 Crypto Extensions

PATCH New Ard Biesheuvel 2 participants 4 messages Mar 20, 2014 – Mar 28, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: SHA-224/SHA-256 using ARMv8 Crypto Extensions

This patch adds support for the SHA-224 and SHA-256 hash algorithms using the NEON based SHA-256 instructions that were introduced in ARM v8.

---

Again, this patch depends on the FPSIMD optimization patches that I have posted to the LAKML a while ago.

 arch/arm64/crypto/Kconfig        |   5 +
 arch/arm64/crypto/Makefile       |   3 +
 arch/arm64/crypto/sha2-ce-core.S | 131 ++++++++++++++++++++++++++
 arch/arm64/crypto/sha2-ce-glue.c | 198 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 337 insertions(+)
 create mode 100644 arch/arm64/crypto/sha2-ce-core.S
 create mode 100644 arch/arm64/crypto/sha2-ce-glue.c
arch/arm64/crypto/Kconfig +5 -0
index af378bb608e8..c14e2ae98193 100644
@@ -10,4 +10,9 @@ config CRYPTO_SHA1_ARM64_CE
 	depends on ARM64 && KERNEL_MODE_NEON
 	select CRYPTO_HASH
 
+config CRYPTO_SHA2_ARM64_CE
+	tristate "SHA-224/SHA-256 digest algorithm (ARMv8 Crypto Extensions)"
+	depends on ARM64 && KERNEL_MODE_NEON
+	select CRYPTO_HASH
+
 endif
arch/arm64/crypto/Makefile +3 -0
index 18ceebc308dd..330742db69b3 100644
@@ -10,3 +10,6 @@
 
 obj-$(CONFIG_CRYPTO_SHA1_ARM64_CE) += sha1-ce.o
 sha1-ce-y := sha1-ce-glue.o sha1-ce-core.o
+
+obj-$(CONFIG_CRYPTO_SHA2_ARM64_CE) += sha2-ce.o
+sha2-ce-y := sha2-ce-glue.o sha2-ce-core.o
arch/arm64/crypto/sha2-ce-core.S new file +131 -0

131 lines not on this page · raw message

arch/arm64/crypto/sha2-ce-glue.c new file +198 -0

198 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 1/1 arm64: SHA-224/SHA-256 using ARMv8 Crypto Extensions

Quoted 5 lines · Ard Biesheuvel

This patch adds support for the SHA-224 and SHA-256 hash algorithms using the NEON based SHA-256 instructions that were introduced in ARM v8.

---

[...]

Quoted 1 line

+ * Copyright (c) Alan Smithee.

Email contact is missing here.

[...]

Review

Quoted 5 lines
(quoted diff) +5 -0
+static int sha224_init(struct shash_desc *desc)
+{
+	struct sha256_state *sctx = shash_desc_ctx(desc);
+
+	*sctx = (struct sha256_state){

This cast is interesting, I don't quite understand it. Can you please explain that to me ?

Quoted 7 lines
(quoted diff) +7 -0
+		.state = {
+			SHA224_H0, SHA224_H1, SHA224_H2, SHA224_H3,
+			SHA224_H4, SHA224_H5, SHA224_H6, SHA224_H7,
+		}
+	};
+	return 0;
+}

[...]

Review

Quoted 10 lines
(quoted diff) +10 -0
+static int sha224_final(struct shash_desc *desc, u8 *out)
+{
+	struct sha256_state *sctx = shash_desc_ctx(desc);
+	__be32 *dst = (__be32 *)out;
+	int i;
+
+	sha2_final(desc);
+
+	for (i = 0; i < SHA224_DIGEST_SIZE / sizeof(*dst); i++)
+		dst[i] = cpu_to_be32(sctx->state[i]);

Won't this cause unaligned access if *dst is not aligned to 32 bytes ?

Try the crypto tests with this patch to see if this explodes please.

crypto/testmgr.c +0 -0
index 7795550..b9b7144 100644
@@ -191,7 +191,8 @@ static int __test_hash(struct crypto_ahash *tfm, struct 

hash_testvec *template,

        const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
        unsigned int i, j, k, temp;
        struct scatterlist sg[8];
-       char result[64];
+       char _result[68];
+       char *result = _result + 1;
        struct ahash_request *req;
        struct tcrypt_result tresult;
        void *hash_buff;

[...]

#2 lore raw

PATCH 1/1 arm64: SHA-224/SHA-256 using ARMv8 Crypto Extensions

Quoted 7 lines · Marek Vasut
Quoted 5 lines · Ard Biesheuvel

This patch adds support for the SHA-224 and SHA-256 hash algorithms using the NEON based SHA-256 instructions that were introduced in ARM v8.

---

Quoted 8 lines

[...]

Quoted 1 line

+ * Copyright (c) Alan Smithee.

Email contact is missing here.

[...]

Actually, this is mostly copied from the original sha1_generic.c. In fact, my current v2 (which I will post shortly) has been reworked to such an extent that I am contemplating dropping this attribution altogether.

Quoted 9 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static int sha224_init(struct shash_desc *desc)
+{
+     struct sha256_state *sctx = shash_desc_ctx(desc);
+
+     *sctx = (struct sha256_state){

This cast is interesting, I don't quite understand it. Can you please explain that to me ?

http://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html

Quoted 10 lines
Quoted 7 lines
(quoted diff) +7 -0
+             .state = {
+                     SHA224_H0, SHA224_H1, SHA224_H2, SHA224_H3,
+                     SHA224_H4, SHA224_H5, SHA224_H6, SHA224_H7,
+             }
+     };
+     return 0;
+}

[...]

Quoted 13 lines

Review

Quoted 10 lines
(quoted diff) +10 -0
+static int sha224_final(struct shash_desc *desc, u8 *out)
+{
+     struct sha256_state *sctx = shash_desc_ctx(desc);
+     __be32 *dst = (__be32 *)out;
+     int i;
+
+     sha2_final(desc);
+
+     for (i = 0; i < SHA224_DIGEST_SIZE / sizeof(*dst); i++)
+             dst[i] = cpu_to_be32(sctx->state[i]);

Won't this cause unaligned access if *dst is not aligned to 32 bytes ?

arm64 does not care about that, but I agree it would be better (and more explicit) to use put_unaligned() here, and leave it up to the architecture to allow it or work around it.
WIll update that in v2.

Thanks for the review.

Signature · 1 line
Ard.
#3 lore raw

PATCH 1/1 arm64: SHA-224/SHA-256 using ARMv8 Crypto Extensions

Quoted 13 lines · Ard Biesheuvel
Quoted 12 lines · Marek Vasut
Quoted 6 lines · Ard Biesheuvel

This patch adds support for the SHA-224 and SHA-256 hash algorithms using the NEON based SHA-256 instructions that were introduced in ARM v8.

---

[...]

Quoted 1 line

+ * Copyright (c) Alan Smithee.

Quoted 8 lines
Quoted 3 lines

Email contact is missing here.

[...]

Actually, this is mostly copied from the original sha1_generic.c. In fact, my current v2 (which I will post shortly) has been reworked to such an extent that I am contemplating dropping this attribution altogether.

OK

Quoted 10 lines
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static int sha224_init(struct shash_desc *desc)
+{
+     struct sha256_state *sctx = shash_desc_ctx(desc);
+
+     *sctx = (struct sha256_state){

This cast is interesting, I don't quite understand it. Can you please explain that to me ?

http://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html

I have to wonder how many people will stumble across this and will wonder about the same honestly. Sure, it's a valid construct, but it's quite strange in my opinion. On the other hand, if noone else is against writing it like so, I won't push it ...

Quoted 23 lines
Quoted 10 lines
Quoted 7 lines
(quoted diff) +7 -0
+             .state = {
+                     SHA224_H0, SHA224_H1, SHA224_H2, SHA224_H3,
+                     SHA224_H4, SHA224_H5, SHA224_H6, SHA224_H7,
+             }
+     };
+     return 0;
+}

[...]

Quoted 12 lines

Review

Quoted 10 lines
(quoted diff) +10 -0
+static int sha224_final(struct shash_desc *desc, u8 *out)
+{
+     struct sha256_state *sctx = shash_desc_ctx(desc);
+     __be32 *dst = (__be32 *)out;
+     int i;
+
+     sha2_final(desc);
+
+     for (i = 0; i < SHA224_DIGEST_SIZE / sizeof(*dst); i++)
+             dst[i] = cpu_to_be32(sctx->state[i]);

Won't this cause unaligned access if *dst is not aligned to 32 bytes ?

Quoted 4 lines

arm64 does not care about that, but I agree it would be better (and more explicit) to use put_unaligned() here, and leave it up to the architecture to allow it or work around it.
WIll update that in v2.

OK, thanks!

Quoted 1 line

Thanks for the review.

Best regards,
Marek Vasut