x86-optimized SHA1 hash for CryptoAPI
RFC PATCH 1/1 x86-optimized SHA1 hash for CryptoAPI
This is a new CryptoAPI module containing an x86-optimized implementation of SHA1, taken from Nettle.
On my box (Pentium IV), tcrypt's performance tester reports the following cycles-per-byte figures (average of 5 trials):
Test# Bytes/ Bytes/ Unopt Opt
block update cycles/B cycles/B
0 16 16 209 150
1 64 16 130 84
2 64 64 91 45
3 256 16 95 69
4 256 64 53 28
5 256 256 40 18
6 1024 16 88 66
7 1024 256 32 15
8 1024 1024 28 12
9 2048 16 91 65
10 2048 256 36 16
11 2048 1024 27 12
12 2048 2048 27 13
13 4096 16 92 65
14 4096 256 30 14
15 4096 1024 27 11
16 4096 4096 26 11
17 8192 16 85 65
18 8192 256 30 14
19 8192 1024 27 11
20 8192 4096 26 10
21 8192 8192 26 10so the x86-optimized version is more than twice as fast for large updates and still somewhat faster than the current version for small updates.
Now for my question. The attached patch is a fairly straightforward port from the Nettle asm and C code. However, there are (at least) two other options:
1. Recognizing that arch/i386/crypto/sha1.c contains almost the same functionality as crypto/sha1.c, I could abstract out common code between the two implementations. However, given the length of the files in question, I suspect the complexity isn't worth it. In-tree, AES uses duplicated code between the asm and C versions, while Twofish pulls out common code into a separate module.
2. Since the optimized part of the code is the SHA1 compression function, and since crypto/sha1.c is just a wrapper around the compression function in lib/sha1.c, I could eliminate the separate CryptoAPI module and switch the kernel between the C and asm compression functions based on a config option -- so that all SHA1 users get the optimized version, instead of just CryptoAPI. There is an API issue though: the optimized code needs to allocate its workspace on the stack for register-allocation reasons, and the C version expects the caller to allocate a temporary buffer (to minimize the number of times the workspace needs to be cleared when hashing several times in a row). If the optimized code was used with the current API, the caller would be doing a bunch of allocation and clearing for nothing. (Note: the only direct users of lib/sha1.c right now are /dev/random and syncookies.)
Which (if any) is the preferred approach?
Suggestions and comments appreciated.
--Benjamin Gilbert
--- arch/i386/crypto/Makefile | 2 arch/i386/crypto/sha1-i586-asm.S | 330 ++++++++++++++++++++++++++++++++++++++ arch/i386/crypto/sha1.c | 161 +++++++++++++++++++ crypto/Kconfig | 8 + 4 files changed, 501 insertions(+), 0 deletions(-)
arch/i386/crypto/Makefile +2 -0
@@ -6,7 +6,9 @@ # obj-$(CONFIG_CRYPTO_AES_586) += aes-i586.o obj-$(CONFIG_CRYPTO_TWOFISH_586) += twofish-i586.o +obj-$(CONFIG_CRYPTO_SHA1_586) += sha1-i586.o aes-i586-y := aes-i586-asm.o aes.o twofish-i586-y := twofish-i586-asm.o twofish.o +sha1-i586-y := sha1-i586-asm.o sha1.o
arch/i386/crypto/sha1-i586-asm.S new file +330 -0
330 lines not on this page · raw message
arch/i386/crypto/sha1.c new file +161 -0
161 lines not on this page · raw message
crypto/Kconfig +8 -0
@@ -63,6 +63,14 @@ config CRYPTO_SHA1 help SHA-1 secure hash standard (FIPS 180-1/DFIPS 180-2). +config CRYPTO_SHA1_586 + tristate "SHA1 digest algorithm (i586)" + depends on (X86 || UML_X86) && !64BIT + select CRYPTO_ALGAPI + help + SHA-1 secure hash standard (FIPS 180-1/DFIPS 180-2). This is the + i586-optimized version. + config CRYPTO_SHA1_S390 tristate "SHA1 digest algorithm (s390)" depends on S390
Herbert Xu
maintainerHi Bejamin:
Quoted 2 lines · Benjamin Gilbert
This is a new CryptoAPI module containing an x86-optimized implementation of SHA1, taken from Nettle.
Thanks for the patch! Sorry for the very late response. I just discovered this while cleaning up my mailbox :)
Quoted 6 lines
1. Recognizing that
arch/i386/crypto/sha1.ccontains almost the same functionality ascrypto/sha1.c, I could abstract out common code between the two implementations. However, given the length of the files in question, I suspect the complexity isn't worth it. In-tree, AES uses duplicated code between the asm and C versions, while Twofish pulls out common code into a separate module.
Considering the simplicity of sha1.c, I'd say that you should just duplicate it.
Quoted 13 lines
2. Since the optimized part of the code is the SHA1 compression function, and since
crypto/sha1.cis just a wrapper around the compression function inlib/sha1.c, I could eliminate the separate CryptoAPI module and switch the kernel between the C and asm compression functions based on a config option -- so that all SHA1 users get the optimized version, instead of just CryptoAPI. There is an API issue though: the optimized code needs to allocate its workspace on the stack for register-allocation reasons, and the C version expects the caller to allocate a temporary buffer (to minimize the number of times the workspace needs to be cleared when hashing several times in a row). If the optimized code was used with the current API, the caller would be doing a bunch of allocation and clearing for nothing. (Note: the only direct users oflib/sha1.cright now are/dev/randomand syncookies.)
Quoted 1 line
Which (if any) is the preferred approach?
I think the best solution is to replace SHA_WORKSPACE_WORDS with an inline function/macro that returns zero for the optimised version.
Cheers,
Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt