Skip to contentSkip to conversation

crypto/arm: accelerated SHA-512 using ARM generic ASM and NEON

PATCH Superseded Ard Biesheuvel 3 participants 10 messages Apr 10, 2015 – Apr 13, 2015

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

PATCH v2 1/1 crypto/arm: accelerated SHA-512 using ARM generic ASM and NEON

This replaces the SHA-512 NEON module with the faster and more versatile implementation from the OpenSSL project. It consists of both a NEON and a generic ASM version of the core SHA-512 transform, where the NEON version reverts to the ASM version when invoked in non-process context.

This patch is based on the OpenSSL upstream version b1a5d1c65208 of sha512-armv4.pl, which can be found here:

https://git.openssl.org/gitweb/?p=openssl.git;h=b1a5d1c65208

Performance relative to the generic implementation (measured using tcrypt.ko mode=306 sec=1 running on a Cortex-A57 under KVM):

input size block size asm neon old neon

16 16 1.39 2.54 2.21
64 16 1.32 2.33 2.09
64 64 1.38 2.53 2.19
256 16 1.31 2.28 2.06
256 64 1.38 2.54 2.25
256 256 1.40 2.77 2.39
1024 16 1.29 2.22 2.01
1024 256 1.40 2.82 2.45
1024 1024 1.41 2.93 2.53
2048 16 1.33 2.21 2.00
2048 256 1.40 2.84 2.46
2048 1024 1.41 2.96 2.55
2048 2048 1.41 2.98 2.56
4096 16 1.34 2.20 1.99
4096 256 1.40 2.84 2.46
4096 1024 1.41 2.97 2.56
4096 4096 1.41 3.01 2.58
8192 16 1.34 2.19 1.99
8192 256 1.40 2.85 2.47
8192 1024 1.41 2.98 2.56
8192 4096 1.41 2.71 2.59
8192 8192 1.51 3.51 2.69

---

Can also be found here (rebased onto latest cryptodev):
https://git.linaro.org/people/ard.biesheuvel/linux-arm.git/shortlog/refs/heads/arm-crypto

 arch/arm/crypto/Kconfig               |   15 +-
 arch/arm/crypto/Makefile              |   10 +-
 arch/arm/crypto/sha512-armv4.pl       |  649 ++++++++++++
 arch/arm/crypto/sha512-armv7-neon.S   |  455 --------
 arch/arm/crypto/sha512-core.S_shipped | 1861 +++++++++++++++++++++++++++++++++
 arch/arm/crypto/sha512-glue.c         |  121 +++
 arch/arm/crypto/sha512-neon-glue.c    |   98 ++
 arch/arm/crypto/sha512.h              |    8 +
 arch/arm/crypto/sha512_neon_glue.c    |  305 ------
 9 files changed, 2748 insertions(+), 774 deletions(-)
 create mode 100644 arch/arm/crypto/sha512-armv4.pl
 delete mode 100644 arch/arm/crypto/sha512-armv7-neon.S
 create mode 100644 arch/arm/crypto/sha512-core.S_shipped
 create mode 100644 arch/arm/crypto/sha512-glue.c
 create mode 100644 arch/arm/crypto/sha512-neon-glue.c
 create mode 100644 arch/arm/crypto/sha512.h
 delete mode 100644 arch/arm/crypto/sha512_neon_glue.c
arch/arm/crypto/Kconfig +4 -11
index a267529d9577..9f4cb0e9130c 100644
@@ -52,20 +52,13 @@ config CRYPTO_SHA256_ARM
 	  SHA-256 secure hash standard (DFIPS 180-2) implemented
 	  using optimized ARM assembler and NEON, when available.
 
-config CRYPTO_SHA512_ARM_NEON
-	tristate "SHA384 and SHA512 digest algorithm (ARM NEON)"
-	depends on KERNEL_MODE_NEON
-	select CRYPTO_SHA512
+config CRYPTO_SHA512_ARM
+	tristate "SHA-384/512 digest algorithm (ARM-asm and NEON)"
 	select CRYPTO_HASH
+	select CRYPTO_SHA512_BASE
 	help
 	  SHA-512 secure hash standard (DFIPS 180-2) implemented
-	  using ARM NEON instructions, when available.
-
-	  This version of SHA implements a 512 bit hash with 256 bits of
-	  security against collision attacks.
-
-	  This code also includes SHA-384, a 384 bit hash with 192 bits
-	  of security against collision attacks.
+	  using optimized ARM assembler and NEON, when available.
 
 config CRYPTO_AES_ARM
 	tristate "AES cipher algorithms (ARM-asm)"
arch/arm/crypto/Makefile +7 -3
index ef46e898f98b..b37597ad979c 100644
@@ -8,7 +8,7 @@ obj-$(CONFIG_CRYPTO_AES_ARM_CE) += aes-arm-ce.o
 obj-$(CONFIG_CRYPTO_SHA1_ARM) += sha1-arm.o
 obj-$(CONFIG_CRYPTO_SHA1_ARM_NEON) += sha1-arm-neon.o
 obj-$(CONFIG_CRYPTO_SHA256_ARM) += sha256-arm.o
-obj-$(CONFIG_CRYPTO_SHA512_ARM_NEON) += sha512-arm-neon.o
+obj-$(CONFIG_CRYPTO_SHA512_ARM) += sha512-arm.o
 obj-$(CONFIG_CRYPTO_SHA1_ARM_CE) += sha1-arm-ce.o
 obj-$(CONFIG_CRYPTO_SHA2_ARM_CE) += sha2-arm-ce.o
 obj-$(CONFIG_CRYPTO_GHASH_ARM_CE) += ghash-arm-ce.o
@@ -19,7 +19,8 @@ sha1-arm-y	:= sha1-armv4-large.o sha1_glue.o
 sha1-arm-neon-y	:= sha1-armv7-neon.o sha1_neon_glue.o
 sha256-arm-neon-$(CONFIG_KERNEL_MODE_NEON) := sha256_neon_glue.o
 sha256-arm-y	:= sha256-core.o sha256_glue.o $(sha256-arm-neon-y)
-sha512-arm-neon-y := sha512-armv7-neon.o sha512_neon_glue.o
+sha512-arm-neon-$(CONFIG_KERNEL_MODE_NEON) := sha512-neon-glue.o
+sha512-arm-y	:= sha512-core.o sha512-glue.o $(sha512-arm-neon-y)
 sha1-arm-ce-y	:= sha1-ce-core.o sha1-ce-glue.o
 sha2-arm-ce-y	:= sha2-ce-core.o sha2-ce-glue.o
 aes-arm-ce-y	:= aes-ce-core.o aes-ce-glue.o
@@ -34,4 +35,7 @@ $(src)/aesbs-core.S_shipped: $(src)/bsaes-armv7.pl
 $(src)/sha256-core.S_shipped: $(src)/sha256-armv4.pl
 	$(call cmd,perl)
 
-.PRECIOUS: $(obj)/aesbs-core.S $(obj)/sha256-core.S
+$(src)/sha512-core.S_shipped: $(src)/sha512-armv4.pl
+	$(call cmd,perl)
+
+.PRECIOUS: $(obj)/aesbs-core.S $(obj)/sha256-core.S $(obj)/sha512-core.S
arch/arm/crypto/sha512-armv4.pl new file +649 -0

649 lines not on this page · raw message

arch/arm/crypto/sha512-armv7-neon.S deleted +0 -455

455 lines not on this page · raw message

arch/arm/crypto/sha512-core.S_shipped new file +1861 -0

1,847 lines not on this page · raw message

arch/arm/crypto/sha512-glue.c new file +121 -0

121 lines not on this page · raw message

arch/arm/crypto/sha512-neon-glue.c new file +98 -0

98 lines not on this page · raw message

arch/arm/crypto/sha512.h new file +8 -0
new file mode 100644
index 000000000000..a75d9a82988a

Lines of this file are not shown. View the raw message.

arch/arm/crypto/sha512_neon_glue.c deleted +0 -305

305 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.8.3.2

Part of the diff is not shown here. View the raw message.

#1 lore raw

PATCH v2 1/1 crypto/arm: accelerated SHA-512 using ARM generic ASM and NEON

arch/arm/crypto/sha512-armv4.pl line 588

Quoted 5 lines · On Friday 10 April 2015 16:29:08 Ard Biesheuvel wrote:
arch/arm/crypto/sha512-armv4.pl new file +4 -0
+#if __ARM_MAX_ARCH__>=7
+.arch  armv7-a
+.fpu   neon
+

This will cause a build failure on an ARMv7-M build, which is incompatible
with .arch armv7-a and .fpu neon.

Arnd

#2 lore raw

PATCH v2 1/1 crypto/arm: accelerated SHA-512 using ARM generic ASM and NEON

Quoted 12 lines · Arnd Bergmann

arch/arm/crypto/sha512-armv4.pl line 588

Quoted 6 lines · On Friday 10 April 2015 16:29:08 Ard Biesheuvel wrote:
arch/arm/crypto/sha512-armv4.pl new file +4 -0
+#if __ARM_MAX_ARCH__>=7
+.arch  armv7-a
+.fpu   neon
+

This will cause a build failure on an ARMv7-M build, which is incompatible
with .arch armv7-a and .fpu neon.

The neon part depends on CONFIG_KERNEL_MODE_NEON, which would never be set for that platform, I suppose

#3 lore raw

PATCH v2 1/1 crypto/arm: accelerated SHA-512 using ARM generic ASM and NEON

Quoted 13 lines · Ard Biesheuvel
Quoted 12 lines · Arnd Bergmann

arch/arm/crypto/sha512-armv4.pl line 588

Quoted 6 lines · On Friday 10 April 2015 16:29:08 Ard Biesheuvel wrote:
arch/arm/crypto/sha512-armv4.pl new file +4 -0
+#if __ARM_MAX_ARCH__>=7
+.arch  armv7-a
+.fpu   neon
+

This will cause a build failure on an ARMv7-M build, which is incompatible
with .arch armv7-a and .fpu neon.

Quoted 1 line

The neon part depends on CONFIG_KERNEL_MODE_NEON, which would never be set for that platform, I suppose

On second thought, that is not entirely true, but I still don't think there is problem here:
the .arch/.fpu declarations are understood perfectly fine by GAS when targeting ARMv7-M. Only, it will emit code that is incompatible with it. However, this code is invoked at runtime only if a NEON unit has been detected, so it will just be ignored on ARMv7-M

Signature · 1 line
Ard.
#4 lore raw

PATCH v2 1/1 crypto/arm: accelerated SHA-512 using ARM generic ASM and NEON

Quoted 14 lines · On Saturday 11 April 2015 09:35:15 Ard Biesheuvel wrote:
Quoted 13 lines · Ard Biesheuvel
Quoted 12 lines · Arnd Bergmann

Review

Quoted 6 lines · On Friday 10 April 2015 16:29:08 Ard Biesheuvel wrote:
(quoted diff) +4 -0
+#if __ARM_MAX_ARCH__>=7
+.arch  armv7-a
+.fpu   neon
+

This will cause a build failure on an ARMv7-M build, which is incompatible
with .arch armv7-a and .fpu neon.

Quoted 8 lines
Quoted 1 line

The neon part depends on CONFIG_KERNEL_MODE_NEON, which would never be set for that platform, I suppose

On second thought, that is not entirely true, but I still don't think there is problem here:
the .arch/.fpu declarations are understood perfectly fine by GAS when targeting ARMv7-M. Only, it will emit code that is incompatible with it. However, this code is invoked at runtime only if a NEON unit has been detected, so it will just be ignored on ARMv7-M

Sorry, I should have collected my findings better when replying to your patch. What I remembered was that I saw a problem in this area in linux-next with randconfig builds, but I did not notice that it was for a different file, and I had not double-checked that patch yet in order to send it out.

See below for the patch I'm currently using for my randconfig builder. Before you apply this, please check again which files are affected, as it's possible that there are other modules that suffer from the same problem.

Arnd

8<-----------
Subject: [PATCH] ARM: crypto: avoid sha256 code on ARMv7-M

The sha256 assembly implementation can deal with all architecture levels from ARMv4 to ARMv7-A, but not with ARMv7-M. Enabling it in an ARMv7-M kernel results in this build failure:

arm-linux-gnueabi-ld: error: arch/arm/crypto/sha256_glue.o: Conflicting architecture profiles M/A
arm-linux-gnueabi-ld: failed to merge target specific data of file arch/arm/crypto/sha256_glue.o

This adds a Kconfig dependency to prevent the code from being disabled for ARMv7-M.

arch/arm/crypto/Kconfig +1 -0
index 458729d2ce22..76463da22f81 100644
@@ -49,6 +49,7 @@ config CRYPTO_SHA2_ARM_CE
 config CRYPTO_SHA256_ARM
 	tristate "SHA-224/256 digest algorithm (ARM-asm and NEON)"
 	select CRYPTO_HASH
+	depends on !CPU_V7M
 	help
 	  SHA-256 secure hash standard (DFIPS 180-2) implemented
 	  using optimized ARM assembler and NEON, when available.
#5 lore raw

PATCH v2 1/1 crypto/arm: accelerated SHA-512 using ARM generic ASM and NEON

Quoted 32 lines · Arnd Bergmann
Quoted 15 lines · On Saturday 11 April 2015 09:35:15 Ard Biesheuvel wrote:
Quoted 13 lines · Ard Biesheuvel
Quoted 11 lines

Review

Quoted 8 lines · Arnd Bergmann

On Friday 10 April 2015 16:29:08 Ard Biesheuvel wrote:

(quoted diff) +4 -0
+#if __ARM_MAX_ARCH__>=7
+.arch  armv7-a
+.fpu   neon
+

This will cause a build failure on an ARMv7-M build, which is incompatible
with .arch armv7-a and .fpu neon.

Quoted 8 lines
Quoted 1 line

The neon part depends on CONFIG_KERNEL_MODE_NEON, which would never be set for that platform, I suppose

On second thought, that is not entirely true, but I still don't think there is problem here:
the .arch/.fpu declarations are understood perfectly fine by GAS when targeting ARMv7-M. Only, it will emit code that is incompatible with it. However, this code is invoked at runtime only if a NEON unit has been detected, so it will just be ignored on ARMv7-M

Sorry, I should have collected my findings better when replying to your patch. What I remembered was that I saw a problem in this area in linux-next with randconfig builds, but I did not notice that it was for a different file, and I had not double-checked that patch yet in order to send it out.

Quoted 5 lines

See below for the patch I'm currently using for my randconfig builder. Before you apply this, please check again which files are affected, as it's possible that there are other modules that suffer from the same problem.

Ah i see it now. The new Sha256 module as well as the Sha512 i am proposing here both use a single .o containing the !neon and neon implementations, and only expose the latter if KERNEL_MODE_NEON. This way, we can use the exact same .S file ad OpenSSL, which should mean less maintenance burden.

So your fix seems the most appropriate, even if it means v7m won't be able to use the !neon part either.

Quoted 30 lines

Arnd

8<-----------
Subject: [PATCH] ARM: crypto: avoid sha256 code on ARMv7-M

The sha256 assembly implementation can deal with all architecture levels from ARMv4 to ARMv7-A, but not with ARMv7-M. Enabling it in an ARMv7-M kernel results in this build failure:

arm-linux-gnueabi-ld: error: arch/arm/crypto/sha256_glue.o: Conflicting architecture profiles M/A
arm-linux-gnueabi-ld: failed to merge target specific data of file arch/arm/crypto/sha256_glue.o

This adds a Kconfig dependency to prevent the code from being disabled for ARMv7-M.

arch/arm/crypto/Kconfig +0 -0
index 458729d2ce22..76463da22f81 100644
@@ -49,6 +49,7 @@ config CRYPTO_SHA2_ARM_CE

config CRYPTO_SHA256_ARM
tristate "SHA-224/256 digest algorithm (ARM-asm and NEON)" select CRYPTO_HASH
+ depends on !CPU_V7M
help
SHA-256 secure hash standard (DFIPS 180-2) implemented using optimized ARM assembler and NEON, when available.

#6 lore raw

PATCH v2 1/1 crypto/arm: accelerated SHA-512 using ARM generic ASM and NEON

Quoted 6 lines · On Saturday 11 April 2015 12:27:18 Ard Biesheuvel wrote:

Ah i see it now. The new Sha256 module as well as the Sha512 i am proposing here both use a single .o containing the !neon and neon implementations, and only expose the latter if KERNEL_MODE_NEON. This way, we can use the exact same .S file ad OpenSSL, which should mean less maintenance burden.

So your fix seems the most appropriate, even if it means v7m won't be able to use the !neon part either.

Ok, sounds good. If someone wants to change that code to work on ARMv7-M, they probably want that fix in the openssl version as well, and then we can update both.

Arnd

#7 lore raw

PATCH v2 1/1 crypto/arm: accelerated SHA-512 using ARM generic ASM and NEON

In reply to Arnd Bergmann
Quoted 40 lines · Arnd Bergmann
Quoted 14 lines · On Saturday 11 April 2015 09:35:15 Ard Biesheuvel wrote:
Quoted 13 lines · Ard Biesheuvel
Quoted 12 lines · Arnd Bergmann

Review

Quoted 6 lines · On Friday 10 April 2015 16:29:08 Ard Biesheuvel wrote:
(quoted diff) +4 -0
+#if __ARM_MAX_ARCH__>=7
+.arch  armv7-a
+.fpu   neon
+

This will cause a build failure on an ARMv7-M build, which is incompatible
with .arch armv7-a and .fpu neon.

Quoted 8 lines
Quoted 1 line

The neon part depends on CONFIG_KERNEL_MODE_NEON, which would never be set for that platform, I suppose

On second thought, that is not entirely true, but I still don't think there is problem here:
the .arch/.fpu declarations are understood perfectly fine by GAS when targeting ARMv7-M. Only, it will emit code that is incompatible with it. However, this code is invoked at runtime only if a NEON unit has been detected, so it will just be ignored on ARMv7-M

Sorry, I should have collected my findings better when replying to your patch. What I remembered was that I saw a problem in this area in linux-next with randconfig builds, but I did not notice that it was for a different file, and I had not double-checked that patch yet in order to send it out.

See below for the patch I'm currently using for my randconfig builder. Before you apply this, please check again which files are affected, as it's possible that there are other modules that suffer from the same problem.

Arnd

8<-----------
Subject: [PATCH] ARM: crypto: avoid sha256 code on ARMv7-M

Quoted 8 lines

The sha256 assembly implementation can deal with all architecture levels from ARMv4 to ARMv7-A, but not with ARMv7-M. Enabling it in an ARMv7-M kernel results in this build failure:

arm-linux-gnueabi-ld: error: arch/arm/crypto/sha256_glue.o: Conflicting architecture profiles M/A
arm-linux-gnueabi-ld: failed to merge target specific data of file arch/arm/crypto/sha256_glue.o

This adds a Kconfig dependency to prevent the code from being disabled

... enabled?

Quoted 10 lines

for ARMv7-M.

arch/arm/crypto/Kconfig +0 -0
index 458729d2ce22..76463da22f81 100644

arch/arm/crypto/Kconfig config CRYPTO_SHA2_ARM_CE · line 55

Quoted 7 lines
arch/arm/crypto/Kconfig +1 -0
@@ -49,6 +49,7 @@ config CRYPTO_SHA2_ARM_CE
 config CRYPTO_SHA256_ARM
        tristate "SHA-224/256 digest algorithm (ARM-asm and NEON)"
        select CRYPTO_HASH
+       depends on !CPU_V7M
        help
          SHA-256 secure hash standard (DFIPS 180-2) implemented
          using optimized ARM assembler and NEON, when available.

@Herbert: could you please apply this onto cryptodev before sending out your pull request for v4.1?
And please disregard $subject, I will post a v3 with a similar 'depends on' added (unless you're ok to add it yourself)

Thanks,
Ard.

maintainer
#8 lore raw

PATCH v2 1/1 crypto/arm: accelerated SHA-512 using ARM generic ASM and NEON

Quoted 3 lines · Ard Biesheuvel

@Herbert: could you please apply this onto cryptodev before sending out your pull request for v4.1?

Done.

Quoted 2 lines

And please disregard $subject, I will post a v3 with a similar 'depends on' added (unless you're ok to add it yourself)

Please resend the patch. But I'll process it after the merge window closes so no hurry.

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

PATCH v2 1/1 crypto/arm: accelerated SHA-512 using ARM generic ASM and NEON

Quoted 5 lines · Herbert Xu
Quoted 3 lines · Ard Biesheuvel

@Herbert: could you please apply this onto cryptodev before sending out your pull request for v4.1?

Quoted 8 lines

Done.

Quoted 2 lines

And please disregard $subject, I will post a v3 with a similar 'depends on' added (unless you're ok to add it yourself)

Please resend the patch. But I'll process it after the merge window closes so no hurry.

OK, all fine.

Thanks Herbert!