crypto: arm64/ARM: NEON accelerated ChaCha20 *skcipher*
Subsystems (4)
v2 cover crypto: arm64/ARM: NEON accelerated ChaCha20 *skcipher*
Another port of existing x86 SSE code to NEON, again both for arm64 and ARM.
ChaCha20 is a stream cipher described in RFC 7539, and is intended to be an efficient software implementable 'standby cipher', in case AES cannot be used.
This NEON implementation is almost 2x as fast as the generic C code (measured on Cortex-A57 using the arm64 version)
Changes in v2:
- add patch to convert the generic and x86 to skciphers first
- tweaked the arm64 version for some additional performance
- use chunksize == 4x blocksize for optimal speed
Ard Biesheuvel (3):
crypto: chacha20 - convert generic and x86 versions to skcipher
crypto: arm64/chacha20 - implement NEON version based on SSE3 code
crypto: arm/chacha20 - implement NEON version based on SSE3 code
arch/arm/crypto/Kconfig | 6 + arch/arm/crypto/Makefile | 2 + arch/arm/crypto/chacha20-neon-core.S | 524 ++++++++++++++++++++ arch/arm/crypto/chacha20-neon-glue.c | 127 +++++ arch/arm64/crypto/Kconfig | 6 + arch/arm64/crypto/Makefile | 3 + arch/arm64/crypto/chacha20-neon-core.S | 450 +++++++++++++++++ arch/arm64/crypto/chacha20-neon-glue.c | 122 +++++ arch/x86/crypto/chacha20_glue.c | 69 ++- crypto/chacha20_generic.c | 73 ++- include/crypto/chacha20.h | 6 +- 11 files changed, 1304 insertions(+), 84 deletions(-) create mode 100644 arch/arm/crypto/chacha20-neon-core.S create mode 100644 arch/arm/crypto/chacha20-neon-glue.c create mode 100644 arch/arm64/crypto/chacha20-neon-core.S create mode 100644 arch/arm64/crypto/chacha20-neon-glue.c
Signature · 1 line
2.7.4
PATCH v2 1/3 crypto: chacha20 - convert generic and x86 versions to skcipher
This converts the ChaCha20 code from a blkcipher to a skcipher, which is now the preferred way to implement symmetric block and stream ciphers.
This ports the generic and x86 versions at the same time because the latter reuses routines of the former.
Note that the skcipher_walk() API guarantees that all presented blocks except the final one are a multiple of the chunk size, so we can simplify the encrypt() routine somewhat.
--- arch/x86/crypto/chacha20_glue.c | 69 +++++++++--------- crypto/chacha20_generic.c | 73 ++++++++------------ include/crypto/chacha20.h | 6 +- 3 files changed, 64 insertions(+), 84 deletions(-)
arch/x86/crypto/chacha20_glue.c +31 -38
@@ -11,7 +11,7 @@#include <crypto/algapi.h> #include <crypto/chacha20.h> -#include <linux/crypto.h> +#include <crypto/internal/skcipher.h> #include <linux/kernel.h> #include <linux/module.h> #include <asm/fpu/api.h>
@@ -63,36 +63,34 @@ static void chacha20_dosimd(u32 *state, u8 *dst, const u8 *src, } } -static int chacha20_simd (struct blkcipher_desc *desc,struct scatterlist *dst, -struct scatterlist *src,unsigned int nbytes) +static int chacha20_simd (struct skcipher_request *req) { -u32 *state, state_buf[16 + (CHACHA20_STATE_ALIGN /sizeof (u32)) -1 ]; -struct blkcipher_walk walk; +struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); +struct chacha20_ctx *ctx =crypto_skcipher_ctx (tfm); +u32 state[16 ]__aligned(CHACHA20_STATE_ALIGN) ; +struct skcipher_walk walk;int err; -if (nbytes <=CHACHA20_BLOCK_SIZE || !may_use_simd ()) -return crypto_chacha20_crypt (desc, dst, src, nbytes); +if (req->cryptlen <=CHACHA20_BLOCK_SIZE || !may_use_simd ()) +return crypto_chacha20_crypt (req); - state = (u32 *)roundup ((uintptr_t )state_buf,CHACHA20_STATE_ALIGN ); + err =skcipher_walk_virt (&walk, req, true); -blkcipher_walk_init (&walk, dst, src, nbytes); - err =blkcipher_walk_virt_block (desc, &walk,CHACHA20_BLOCK_SIZE ); - -crypto_chacha20_init (state,crypto_blkcipher_ctx (desc->tfm), walk.iv); +crypto_chacha20_init (state, ctx, walk.iv);kernel_fpu_begin ();while (walk.nbytes >=CHACHA20_BLOCK_SIZE ) {chacha20_dosimd (state, walk.dst.virt.addr, walk.src.virt.addr,rounddown (walk.nbytes,CHACHA20_BLOCK_SIZE )); - err =blkcipher_walk_done (desc, &walk, - walk.nbytes %CHACHA20_BLOCK_SIZE ); + err =skcipher_walk_done (&walk, + walk.nbytes %CHACHA20_BLOCK_SIZE ); }if (walk.nbytes) {chacha20_dosimd (state, walk.dst.virt.addr, walk.src.virt.addr, walk.nbytes); - err =blkcipher_walk_done (desc, &walk,0 ); + err =skcipher_walk_done (&walk,0 ); }kernel_fpu_end ();
@@ -100,27 +98,22 @@ static int chacha20_simd(struct blkcipher_desc *desc, struct scatterlist *dst,return err; } -static struct crypto_alg alg = { - .cra_name ="chacha20" , - .cra_driver_name ="chacha20-simd" , - .cra_priority =300 , - .cra_flags =CRYPTO_ALG_TYPE_BLKCIPHER , - .cra_blocksize =1 , - .cra_type = &crypto_blkcipher_type, - .cra_ctxsize =sizeof (struct chacha20_ctx ), - .cra_alignmask =sizeof (u32) -1 , - .cra_module =THIS_MODULE , - .cra_u = { - .blkcipher = { - .min_keysize =CHACHA20_KEY_SIZE , - .max_keysize =CHACHA20_KEY_SIZE , - .ivsize =CHACHA20_IV_SIZE , - .geniv ="seqiv" , - .setkey = crypto_chacha20_setkey, - .encrypt = chacha20_simd, - .decrypt = chacha20_simd, - }, - }, +static struct skcipher_alg alg = { + .base.cra_name ="chacha20" , + .base.cra_driver_name ="chacha20-simd" , + .base.cra_priority =300 , + .base.cra_blocksize =1 , + .base.cra_ctxsize =sizeof (struct chacha20_ctx ), + .base.cra_alignmask =sizeof (u32) -1 , + .base.cra_module =THIS_MODULE , + + .min_keysize =CHACHA20_KEY_SIZE , + .max_keysize =CHACHA20_KEY_SIZE , + .ivsize =CHACHA20_IV_SIZE , + .chunksize =CHACHA20_BLOCK_SIZE , + .setkey = crypto_chacha20_setkey, + .encrypt = chacha20_simd, + .decrypt = chacha20_simd, };static int __init chacha20_simd_mod_init (void )
@@ -133,12 +126,12 @@ static int __init chacha20_simd_mod_init(void)boot_cpu_has (X86_FEATURE_AVX2 ) &&cpu_has_xfeatures (XFEATURE_MASK_SSE |XFEATURE_MASK_YMM ,NULL );#endif -return crypto_register_alg (&alg); +return crypto_register_skcipher (&alg); } static void __exit chacha20_simd_mod_fini(void) { -crypto_unregister_alg (&alg); +crypto_unregister_skcipher (&alg); }module_init (chacha20_simd_mod_init);
crypto/chacha20_generic.c +30 -43
116 lines not on this page · raw message
include/crypto/chacha20.h +3 -3
@@ -5,6 +5,7 @@#ifndef _CRYPTO_CHACHA20_H#define _CRYPTO_CHACHA20_H +#include <crypto/skcipher.h> #include <linux/types.h> #include <linux/crypto.h>
@@ -18,9 +19,8 @@ struct chacha20_ctx {void chacha20_block (u32 *state,void *stream);void crypto_chacha20_init (u32 *state,struct chacha20_ctx *ctx,u8 *iv); -int crypto_chacha20_setkey (struct crypto_tfm *tfm,const u8 *key, +int crypto_chacha20_setkey (struct crypto_skcipher *tfm,const u8 *key,unsigned int keysize); -int crypto_chacha20_crypt (struct blkcipher_desc *desc,struct scatterlist *dst, -struct scatterlist *src,unsigned int nbytes); +int crypto_chacha20_crypt (struct skcipher_request *req);#endif
Signature · 1 line
2.7.4
Herbert Xu
maintainerPATCH v2 1/3 crypto: chacha20 - convert generic and x86 versions to skcipher
Quoted 11 lines · Ard Biesheuvel
This converts the ChaCha20 code from a blkcipher to a skcipher, which is now the preferred way to implement symmetric block and stream ciphers.
This ports the generic and x86 versions at the same time because the latter reuses routines of the former.
Note that the
skcipher_walk()API guarantees that all presented blocks except the final one are a multiple of the chunk size, so we can simplify theencrypt()routine somewhat.Signed-off-by Ard Biesheuvel <[email protected]>
Patch applied. 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
Herbert Xu
maintainerPATCH v2 1/3 crypto: chacha20 - convert generic and x86 versions to skcipher
Quoted 7 lines · Herbert Xu
Quoted 6 lines · Ard Biesheuvel
This converts the ChaCha20 code from a blkcipher to a skcipher, which is now the preferred way to implement symmetric block and stream ciphers.
This ports the generic and x86 versions at the same time because the latter reuses routines of the former.
Quoted 7 lines
Quoted 5 lines
Note that the
skcipher_walk()API guarantees that all presented blocks except the final one are a multiple of the chunk size, so we can simplify theencrypt()routine somewhat.Signed-off-by Ard Biesheuvel <[email protected]>Patch applied. Thanks.
I'm reverting this patch because it breaks the build on ARM without the two subsequent patches.
When resubmitting please make sure that the kernel can build and work after each patch. Otherwise git bisections will fail.
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
Herbert Xu
maintainerPATCH v2 1/3 crypto: chacha20 - convert generic and x86 versions to skcipher
Quoted 14 lines · Herbert Xu
Quoted 13 lines · Herbert Xu
Quoted 11 lines · Ard Biesheuvel
This converts the ChaCha20 code from a blkcipher to a skcipher, which is now the preferred way to implement symmetric block and stream ciphers.
This ports the generic and x86 versions at the same time because the latter reuses routines of the former.
Note that the
skcipher_walk()API guarantees that all presented blocks except the final one are a multiple of the chunk size, so we can simplify theencrypt()routine somewhat.Signed-off-by Ard Biesheuvel <[email protected]>
Quoted 7 lines
Quoted 1 line
Patch applied. Thanks.
I'm reverting this patch because it breaks the build on ARM without the two subsequent patches.
When resubmitting please make sure that the kernel can build and work after each patch. Otherwise git bisections will fail.
Sorry, it only broke the build because I applied your other two patches first.
So I'll just revert all three and it should work.
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
PATCH v2 1/3 crypto: chacha20 - convert generic and x86 versions to skcipher
In reply to Herbert XuQuoted 18 lines · Herbert Xu
Quoted 15 lines · Herbert Xu
Quoted 12 lines · Ard Biesheuvel
This converts the ChaCha20 code from a blkcipher to a skcipher, which is now the preferred way to implement symmetric block and stream ciphers.
This ports the generic and x86 versions at the same time because the latter reuses routines of the former.
Note that the
skcipher_walk()API guarantees that all presented blocks except the final one are a multiple of the chunk size, so we can simplify theencrypt()routine somewhat.Signed-off-by Ard Biesheuvel <[email protected]>
Quoted 8 lines
Quoted 1 line
Patch applied. Thanks.
I'm reverting this patch because it breaks the build on ARM without the two subsequent patches.
When resubmitting please make sure that the kernel can build and work after each patch. Otherwise git bisections will fail.
Ehm, could you keep this one and revert the other two instead please? This patch will not change in the respin, and the other two are blkciphers rather than skciphers (and in the arm64 version, I optimized the asm code as well) so they will look rather different ( as they do in the nacked v2)
Quoted 4 lines
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
Herbert Xu
maintainerPATCH v2 1/3 crypto: chacha20 - convert generic and x86 versions to skcipher
Quoted 2 lines · Ard Biesheuvel
Ehm, could you keep this one and revert the other two instead please? This patch will not change in the respin, and the other two are blkciphers rather than skciphers (and in the arm64 version, I optimized the asm code as well) so they will look rather different ( as they do in the nacked v2)
OK I will keep this patch and revert the other two.
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
PATCH v2 2/3 crypto: arm64/chacha20 - implement NEON version based on SSE3 code
In reply to Ard BiesheuvelThis is a straight port to arm64/NEON of the x86 SSE3 implementation of the ChaCha20 stream cipher.
--- arch/arm64/crypto/Kconfig | 6 + arch/arm64/crypto/Makefile | 3 + arch/arm64/crypto/chacha20-neon-core.S | 450 ++++++++++++++++++++ arch/arm64/crypto/chacha20-neon-glue.c | 122 ++++++ 4 files changed, 581 insertions(+)
arch/arm64/crypto/Kconfig +6 -0
@@ -72,4 +72,10 @@ config CRYPTO_CRC32_ARM64 depends on ARM64 select CRYPTO_HASH +config CRYPTO_CHACHA20_NEON + tristate "NEON accelerated ChaCha20 symmetric cipher" + depends on KERNEL_MODE_NEON + select CRYPTO_BLKCIPHER + select CRYPTO_CHACHA20 + endif
arch/arm64/crypto/Makefile +3 -0
@@ -41,6 +41,9 @@ sha256-arm64-y := sha256-glue.o sha256-core.o obj-$(CONFIG_CRYPTO_SHA512_ARM64) += sha512-arm64.o sha512-arm64-y := sha512-glue.o sha512-core.o +obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha20-neon.o +chacha20-neon-y := chacha20-neon-core.o chacha20-neon-glue.o + AFLAGS_aes-ce.o := -DINTERLEAVE=4 AFLAGS_aes-neon.o := -DINTERLEAVE=4
arch/arm64/crypto/chacha20-neon-core.S new file +450 -0
450 lines not on this page · raw message
arch/arm64/crypto/chacha20-neon-glue.c new file +122 -0
122 lines not on this page · raw message
Signature · 1 line
2.7.4
Herbert Xu
maintainerPATCH v2 2/3 crypto: arm64/chacha20 - implement NEON version based on SSE3 code
arch/arm64/crypto/chacha20-neon-glue.c line 101
Quoted 2 lines · Ard Biesheuvel
arch/arm64/crypto/chacha20-neon-glue.c new file +1
-0+ .chunksize = 4 * CHACHA20_BLOCK_SIZE,
This should use a new attribute specific to the walk interface.
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
PATCH v2 3/3 crypto: arm/chacha20 - implement NEON version based on SSE3 code
In reply to Ard BiesheuvelThis is a straight port to ARM/NEON of the x86 SSE3 implementation of the ChaCha20 stream cipher.
--- arch/arm/crypto/Kconfig | 6 + arch/arm/crypto/Makefile | 2 + arch/arm/crypto/chacha20-neon-core.S | 524 ++++++++++++++++++++ arch/arm/crypto/chacha20-neon-glue.c | 127 +++++ 4 files changed, 659 insertions(+)
arch/arm/crypto/Kconfig +6 -0
@@ -130,4 +130,10 @@ config CRYPTO_CRC32_ARM_CE depends on KERNEL_MODE_NEON && CRC32 select CRYPTO_HASH +config CRYPTO_CHACHA20_NEON + tristate "NEON accelerated ChaCha20 symmetric cipher" + depends on KERNEL_MODE_NEON + select CRYPTO_BLKCIPHER + select CRYPTO_CHACHA20 + endif
arch/arm/crypto/Makefile +2 -0
@@ -8,6 +8,7 @@ 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) += sha512-arm.o +obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha20-neon.o ce-obj-$(CONFIG_CRYPTO_AES_ARM_CE) += aes-arm-ce.o ce-obj-$(CONFIG_CRYPTO_SHA1_ARM_CE) += sha1-arm-ce.o
@@ -40,6 +41,7 @@ aes-arm-ce-y := aes-ce-core.o aes-ce-glue.o ghash-arm-ce-y := ghash-ce-core.o ghash-ce-glue.o crct10dif-arm-ce-y := crct10dif-ce-core.o crct10dif-ce-glue.o crc32-arm-ce-y:= crc32-ce-core.o crc32-ce-glue.o +chacha20-neon-y := chacha20-neon-core.o chacha20-neon-glue.o quiet_cmd_perl = PERL $@ cmd_perl = $(PERL) $(<) > $(@)
arch/arm/crypto/chacha20-neon-core.S new file +524 -0
524 lines not on this page · raw message
arch/arm/crypto/chacha20-neon-glue.c new file +127 -0
127 lines not on this page · raw message
Signature · 1 line
2.7.4