crypto: aria: add ARIA AES-NI/AVX/x86_64 implementation
Subsystems (2)
cover crypto: aria: add ARIA AES-NI/AVX/x86_64 implementation
The purpose of this patchset is to support the implementation of ARIA-AVX. Many of the ideas in this implementation are from Camellia-avx, especially byte slicing.
Like Camellia, ARIA also uses a 16way strategy.
ARIA cipher algorithm is similar to AES.
There are four s-boxes in the ARIA spec and the first and second s-boxes are the same as AES's s-boxes.
Almost functions are based on aria-generic code except for s-box related function.
The aria-avx doesn't implement the key expanding function. it only support encrypt() and decrypt().
Encryption and Decryption logic is actually the same but it should use separated keys(encryption key and decryption key). En/Decryption steps are like below:
1. Add-Round-Key
2. S-box.
3. Diffusion Layer.
There is no special thing in the Add-Round-Key step.
There are some notable things in s-box step.
Like Camellia, it doesn't use a lookup table, instead, it uses aes-ni.
To calculate the first s-box, it just uses the aesenclast and then inverts shift_row. No more process is needed for this job because the first s-box is the same as the AES encryption s-box.
To calculate a second s-box(invert of s1), it just uses the aesdeclast and then inverts shift_row. No more process is needed for this job because the second s-box is the same as the AES decryption s-box.
To calculate a third and fourth s-boxes, it uses the aesenclast, then inverts shift_row, and affine transformation.
The aria-generic implementation is based on a 32-bit implementation, not an 8-bit implementation.
The aria-avx Diffusion Layer implementation is based on aria-generic implementation because 8-bit implementation is not fit for parallel implementation but 32-bit is fit for this.
The first patch in this series is to export functions for aria-avx. The aria-avx uses existing functions in the aria-generic code. The second patch is to implement aria-avx.
The last patch is to add async test for aria.
Benchmarks:
The tcrypt is used.
cpu: i3-12100
How to test:
modprobe aria-generic
tcrypt mode=610 num_mb=8192
Result:
testing speed of multibuffer ecb(aria) (ecb(aria-generic)) encryption
test 0 (128 bit key, 16 byte blocks): 1 operation in 534 cycles test 2 (128 bit key, 128 byte blocks): 1 operation in 2006 cycles test 3 (128 bit key, 256 byte blocks): 1 operation in 3674 cycles test 6 (128 bit key, 4096 byte blocks): 1 operation in 52374 cycles test 7 (256 bit key, 16 byte blocks): 1 operation in 608 cycles test 9 (256 bit key, 128 byte blocks): 1 operation in 2586 cycles test 10 (256 bit key, 256 byte blocks): 1 operation in 4707 cycles test 13 (256 bit key, 4096 byte blocks): 1 operation in 69794 cycles
testing speed of multibuffer ecb(aria) (ecb(aria-generic)) decryption
test 0 (128 bit key, 16 byte blocks): 1 operation in 545 cycles test 2 (128 bit key, 128 byte blocks): 1 operation in 1995 cycles test 3 (128 bit key, 256 byte blocks): 1 operation in 3673 cycles test 6 (128 bit key, 4096 byte blocks): 1 operation in 52359 cycles test 7 (256 bit key, 16 byte blocks): 1 operation in 615 cycles test 9 (256 bit key, 128 byte blocks): 1 operation in 2588 cycles test 10 (256 bit key, 256 byte blocks): 1 operation in 4712 cycles test 13 (256 bit key, 4096 byte blocks): 1 operation in 69916 cycles
How to test:
modprobe aria
tcrypt mode=610 num_mb=8192
Result:
testing speed of multibuffer ecb(aria) (ecb-aria-avx) encryption
test 0 (128 bit key, 16 byte blocks): 1 operation in 727 cycles test 2 (128 bit key, 128 byte blocks): 1 operation in 2040 cycles test 3 (128 bit key, 256 byte blocks): 1 operation in 1399 cycles test 6 (128 bit key, 4096 byte blocks): 1 operation in 14758 cycles test 7 (256 bit key, 16 byte blocks): 1 operation in 702 cycles test 9 (256 bit key, 128 byte blocks): 1 operation in 2615 cycles test 10 (256 bit key, 256 byte blocks): 1 operation in 1677 cycles test 13 (256 bit key, 4096 byte blocks): 1 operation in 19454 cycles
testing speed of multibuffer ecb(aria) (ecb-aria-avx) decryption
test 0 (128 bit key, 16 byte blocks): 1 operation in 638 cycles test 2 (128 bit key, 128 byte blocks): 1 operation in 2090 cycles test 3 (128 bit key, 256 byte blocks): 1 operation in 1394 cycles test 6 (128 bit key, 4096 byte blocks): 1 operation in 14824 cycles test 7 (256 bit key, 16 byte blocks): 1 operation in 719 cycles test 9 (256 bit key, 128 byte blocks): 1 operation in 2633 cycles test 10 (256 bit key, 256 byte blocks): 1 operation in 1684 cycles test 13 (256 bit key, 4096 byte blocks): 1 operation in 19457 cycles
Taehee Yoo (3):
crypto: aria: prepare generic module for optimized implementations
crypto: aria-avx: add AES-NI/AVX/x86_64 assembler implementation of
aria cipher
crypto: tcrypt: add async speed test for aria cipher
arch/x86/crypto/Makefile | 3 +
arch/x86/crypto/aria-aesni-avx-asm_64.S | 648 ++++++++++++++++++++++++
arch/x86/crypto/aria_aesni_avx_glue.c | 165 ++++++
crypto/Kconfig | 21 +
crypto/Makefile | 2 +-
crypto/{aria.c => aria_generic.c} | 39 +-
crypto/tcrypt.c | 13 +
include/crypto/aria.h | 14 +-
8 files changed, 889 insertions(+), 16 deletions(-)
create mode 100644 arch/x86/crypto/aria-aesni-avx-asm_64.S
create mode 100644 arch/x86/crypto/aria_aesni_avx_glue.c
rename crypto/{aria.c => aria_generic.c} (86%)Signature · 1 line
2.17.1
PATCH 1/3 crypto: aria: prepare generic module for optimized implementations
It renames aria to aria_generic and exports some functions such as aria_set_key(), aria_encrypt(), and aria_decrypt() to be able to be used by aria-avx implementation.
---
crypto/Makefile | 2 +-
crypto/{aria.c => aria_generic.c} | 39 +++++++++++++++++++++++++------
include/crypto/aria.h | 14 +++++------
3 files changed, 39 insertions(+), 16 deletions(-)
rename crypto/{aria.c => aria_generic.c} (86%)crypto/Makefile +1 -1
@@ -149,7 +149,7 @@ obj-$(CONFIG_CRYPTO_TEA) += tea.o obj-$(CONFIG_CRYPTO_KHAZAD) += khazad.o obj-$(CONFIG_CRYPTO_ANUBIS) += anubis.o obj-$(CONFIG_CRYPTO_SEED) += seed.o -obj-$(CONFIG_CRYPTO_ARIA) += aria.o +obj-$(CONFIG_CRYPTO_ARIA) += aria_generic.o obj-$(CONFIG_CRYPTO_CHACHA20) += chacha_generic.o obj-$(CONFIG_CRYPTO_POLY1305) += poly1305_generic.o obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
crypto/aria_generic.c renamed from crypto/aria.c +32 -7
@@ -16,6 +16,14 @@#include <crypto/aria.h> +static const u32 key_rc[20 ] = { +0x517cc1b7 ,0x27220a94 ,0xfe13abe8 ,0xfa9a6ee0 , +0x6db14acc ,0x9e21c820 ,0xff28b1d5 ,0xef5de2b0 , +0xdb92371d ,0x2126e970 ,0x03249775 ,0x04e8c90e , +0x517cc1b7 ,0x27220a94 ,0xfe13abe8 ,0xfa9a6ee0 , +0x6db14acc ,0x9e21c820 ,0xff28b1d5 ,0xef5de2b0 +}; +static void aria_set_encrypt_key (struct aria_ctx *ctx,const u8 *in_key,unsigned int key_len) {
@@ -25,7 +33,7 @@ static void aria_set_encrypt_key(struct aria_ctx *ctx, const u8 *in_key,const u32 *ck;int rkidx =0 ; - ck = &key_rc[(key_len -16 ) /8 ][0 ]; + ck = &key_rc[(key_len -16 ) /2 ]; w0[0 ] =be32_to_cpu (key[0 ]); w0[1 ] =be32_to_cpu (key[1 ]);
@@ -163,8 +171,7 @@ static void aria_set_decrypt_key(struct aria_ctx *ctx) } } -static int aria_set_key (struct crypto_tfm *tfm,const u8 *in_key, -unsigned int key_len) +int aria_set_key (struct crypto_tfm *tfm,const u8 *in_key,unsigned int key_len) {struct aria_ctx *ctx =crypto_tfm_ctx (tfm);
@@ -179,6 +186,7 @@ static int aria_set_key(struct crypto_tfm *tfm, const u8 *in_key,return 0 ; } +EXPORT_SYMBOL_GPL (aria_set_key);static void __aria_crypt (struct aria_ctx *ctx,u8 *out,const u8 *in,u32 key[][ARIA_RD_KEY_WORDS ])
@@ -235,14 +243,30 @@ static void __aria_crypt(struct aria_ctx *ctx, u8 *out, const u8 *in, dst[3 ] =cpu_to_be32 (reg3); } -static void aria_encrypt (struct crypto_tfm *tfm,u8 *out,const u8 *in) +void aria_encrypt (void *_ctx,u8 *out,const u8 *in) +{ +struct aria_ctx *ctx = (struct aria_ctx *)_ctx; + +__aria_crypt (ctx, out, in, ctx->enc_key); +} +EXPORT_SYMBOL_GPL (aria_encrypt); + +void aria_decrypt (void *_ctx,u8 *out,const u8 *in) +{ +struct aria_ctx *ctx = (struct aria_ctx *)_ctx; + +__aria_crypt (ctx, out, in, ctx->dec_key); +} +EXPORT_SYMBOL_GPL (aria_decrypt); + +static void __aria_encrypt (struct crypto_tfm *tfm,u8 *out,const u8 *in) {struct aria_ctx *ctx =crypto_tfm_ctx (tfm);__aria_crypt (ctx, out, in, ctx->enc_key); } -static void aria_decrypt (struct crypto_tfm *tfm,u8 *out,const u8 *in) +static void __aria_decrypt (struct crypto_tfm *tfm,u8 *out,const u8 *in) {struct aria_ctx *ctx =crypto_tfm_ctx (tfm);
@@ -263,8 +287,8 @@ static struct crypto_alg aria_alg = { .cia_min_keysize = ARIA_MIN_KEY_SIZE, .cia_max_keysize = ARIA_MAX_KEY_SIZE, .cia_setkey = aria_set_key, - .cia_encrypt = aria_encrypt, - .cia_decrypt = aria_decrypt + .cia_encrypt = __aria_encrypt, + .cia_decrypt = __aria_decrypt } } };
@@ -286,3 +310,4 @@ MODULE_DESCRIPTION("ARIA Cipher Algorithm");MODULE_LICENSE ("GPL" );MODULE_AUTHOR ("Taehee Yoo <[email protected]>" );MODULE_ALIAS_CRYPTO ("aria" ); +MODULE_ALIAS_CRYPTO ("aria-generic" );
include/crypto/aria.h +6 -8
@@ -28,6 +28,7 @@#define ARIA_MIN_KEY_SIZE 16#define ARIA_MAX_KEY_SIZE 32#define ARIA_BLOCK_SIZE 16 +#define ARIA_AVX_BLOCK_SIZE (ARIA_BLOCK_SIZE * 16)#define ARIA_MAX_RD_KEYS 17#define ARIA_RD_KEY_WORDS (ARIA_BLOCK_SIZE / sizeof(u32))
@@ -38,14 +39,6 @@ struct aria_ctx {u32 dec_key[ARIA_MAX_RD_KEYS ][ARIA_RD_KEY_WORDS ]; }; -static const u32 key_rc[5 ][4 ] = { - {0x517cc1b7 ,0x27220a94 ,0xfe13abe8 ,0xfa9a6ee0 }, - {0x6db14acc ,0x9e21c820 ,0xff28b1d5 ,0xef5de2b0 }, - {0xdb92371d ,0x2126e970 ,0x03249775 ,0x04e8c90e }, - {0x517cc1b7 ,0x27220a94 ,0xfe13abe8 ,0xfa9a6ee0 }, - {0x6db14acc ,0x9e21c820 ,0xff28b1d5 ,0xef5de2b0 } -}; -static const u32 s1[256 ] = {0x00636363 ,0x007c7c7c ,0x00777777 ,0x007b7b7b ,0x00f2f2f2 ,0x006b6b6b ,0x006f6f6f ,0x00c5c5c5 ,
@@ -458,4 +451,9 @@ static inline void aria_gsrk(u32 *rk, u32 *x, u32 *y, u32 n) ((y[(q + 2) % 4]) << (32 - r)); } +void aria_encrypt (void *ctx,u8 *out,const u8 *in); +void aria_decrypt (void *ctx,u8 *out,const u8 *in); +int aria_set_key (struct crypto_tfm *tfm,const u8 *in_key, +unsigned int key_len); +#endif
Signature · 1 line
2.17.1
PATCH 2/3 crypto: aria-avx: add AES-NI/AVX/x86_64 assembler implementation of aria cipher
In reply to Taehee YooThe implementation is based on the 32-bit implementation of the aria. Also, aria-avx process steps are the similar to the camellia-avx.
1. Byteslice(16way)
2. Add-round-key.
3. Sbox
4. Diffusion layer.
Except for s-box, all steps are the same as the aria-generic implementation. s-box step is very similar to camellia and sm4 implementation.
There are 4 s-boxes in the ARIA and the 2 s-boxes are the same as AES's s-boxes. The basic strategy is to use the aes-ni.
To calculate a first sbox, it just uses the aesenclast and then inverts shift_row.
No more process is needed for this job because the first s-box is the same as the AES encryption s-box.
To calculate a second sbox(invert of s1), it just uses the aesdeclast and then inverts shift_row.
No more process is needed for this job because the second s-box is the same as the AES decryption s-box.
To calculate a third and fourth s-boxes, it uses the aesenclast, then inverts shift_row, and affine transformation.
The aria-generic implementation is based on a 32-bit implementation, not an 8-bit implementation. the aria-avx Diffusion Layer implementation is based on aria-generic implementation because 8-bit implementation is not fit for parallel implementation but 32-bit is enough to fit for this.
--- arch/x86/crypto/Makefile | 3 + arch/x86/crypto/aria-aesni-avx-asm_64.S | 648 ++++++++++++++++++++++++ arch/x86/crypto/aria_aesni_avx_glue.c | 165 ++++++ crypto/Kconfig | 21 + 4 files changed, 837 insertions(+) create mode 100644 arch/x86/crypto/aria-aesni-avx-asm_64.S create mode 100644 arch/x86/crypto/aria_aesni_avx_glue.c
arch/x86/crypto/Makefile +3 -0
@@ -100,6 +100,9 @@ sm4-aesni-avx-x86_64-y := sm4-aesni-avx-asm_64.o sm4_aesni_avx_glue.o obj-$(CONFIG_CRYPTO_SM4_AESNI_AVX2_X86_64) += sm4-aesni-avx2-x86_64.o sm4-aesni-avx2-x86_64-y := sm4-aesni-avx2-asm_64.o sm4_aesni_avx2_glue.o +obj-$(CONFIG_CRYPTO_ARIA_AESNI_AVX_X86_64) += aria-aesni-avx-x86_64.o +aria-aesni-avx-x86_64-y := aria-aesni-avx-asm_64.o aria_aesni_avx_glue.o + quiet_cmd_perlasm = PERLASM $@ cmd_perlasm = $(PERL) $< > $@ $(obj)/%.S: $(src)/%.pl FORCE
arch/x86/crypto/aria-aesni-avx-asm_64.S new file +648 -0
648 lines not on this page · raw message
arch/x86/crypto/aria_aesni_avx_glue.c new file +165 -0
165 lines not on this page · raw message
crypto/Kconfig +21 -0
@@ -1659,6 +1659,27 @@ config CRYPTO_ARIA See also: <https://seed.kisa.or.kr/kisa/algorithm/EgovAriaInfo.do> +config CRYPTO_ARIA_AESNI_AVX_X86_64 + tristate "ARIA cipher algorithm (x86_64/AES-NI/AVX)" + depends on X86 && 64BIT + select CRYPTO_SKCIPHER + select CRYPTO_ARIA + select CRYPTO_SIMD + help + ARIA cipher algorithm (RFC5794). + + ARIA is a standard encryption algorithm of the Republic of Korea. + The ARIA specifies three key sizes and rounds. + 128-bit: 12 rounds. + 192-bit: 14 rounds. + 256-bit: 16 rounds. + + This module provides the ARIA cipher algorithm that processes + sixteen blocks parallel using the AVX instruction set. + + See also: + <https://seed.kisa.or.kr/kisa/algorithm/EgovAriaInfo.do> + config CRYPTO_SERPENT tristate "Serpent cipher algorithm" select CRYPTO_ALGAPI
Signature · 1 line
2.17.1
RE: [PATCH 2/3] crypto: aria-avx: add AES-NI/AVX/x86_64 assembler implementation of aria cipher
Quoted 6 lines
-----Original Message-----
From: Taehee Yoo <[email protected]>
Sent: Wednesday, August 24, 2022 10:59 AM
Subject: [PATCH 2/3] crypto: aria-avx: add AES-NI/AVX/x86_64 assembler implementation of aria cipher
...
Quoted 23 lines
arch/x86/crypto/aria_aesni_avx_glue.c new file +8
-0+#include "ecb_cbc_helpers.h" + +asmlinkage void aria_aesni_avx_crypt_16way(const u32 *rk, u8 *dst, +const u8 *src,int rounds); + +static int ecb_do_encrypt (struct skcipher_request *req,const u32 *rkey) +{ + struct aria_ctx *ctx =crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
arch/x86/crypto/aria_aesni_avx_glue.c new file +14
-0+struct skcipher_walk walk; +unsigned int nbytes; +int err; + + err =skcipher_walk_virt (&walk, req, false); + +while ((nbytes = walk.nbytes) >0 ) { +const u8 *src = walk.src.virt.addr; +u8 *dst = walk.dst.virt.addr; + +kernel_fpu_begin (); +while (nbytes >=ARIA_AVX_BLOCK_SIZE ) { +aria_aesni_avx_crypt_16way (rkey, dst, src, ctx->rounds); + dst +=ARIA_AVX_BLOCK_SIZE ;
arch/x86/crypto/aria_aesni_avx_glue.c line 47
Quoted 10 lines
arch/x86/crypto/aria_aesni_avx_glue.c new file +10
-0+ src +=ARIA_AVX_BLOCK_SIZE ; + nbytes -=ARIA_AVX_BLOCK_SIZE ; + } +while (nbytes >=ARIA_BLOCK_SIZE ) { +aria_encrypt (ctx, dst, src); + dst +=ARIA_BLOCK_SIZE ; + src +=ARIA_BLOCK_SIZE ; + nbytes -=ARIA_BLOCK_SIZE ; + } +kernel_fpu_end ();
Is aria_encrypt() an FPU function that belongs between kernel_fpu_begin() and kernel_fpu_end()?
Several drivers (camellia, serpent, twofish, cast5, cast6) use ECB_WALK_START, ECB_BLOCK, and ECB_WALK_END macros for these loops. Those macros do only call kernel_fpu_end() at the very end. That requires the functions to be structured with (ctx, dst, src) arguments, not (rkey, dst, src, rounds).
Re: [PATCH 2/3] crypto: aria-avx: add AES-NI/AVX/x86_64 assembler implementation of aria cipher
Hi Elliott, Robert
Thank you so much for your review!
Quoted 45 lines · Robert (Servers)
Quoted 6 lines
-----Original Message-----
From: Taehee Yoo <[email protected]>
Sent: Wednesday, August 24, 2022 10:59 AM
Subject: [PATCH 2/3] crypto: aria-avx: add AES-NI/AVX/x86_64 assembler implementation of aria cipher...
Quoted 23 lines
arch/x86/crypto/aria_aesni_avx_glue.c new file +8
-0+#include "ecb_cbc_helpers.h" + +asmlinkage void aria_aesni_avx_crypt_16way(const u32 *rk, u8 *dst, +const u8 *src,int rounds); + +static int ecb_do_encrypt (struct skcipher_request *req,const u32 *rkey) +{ + struct aria_ctx *ctx =crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
arch/x86/crypto/aria_aesni_avx_glue.c new file +14
-0+struct skcipher_walk walk; +unsigned int nbytes; +int err; + + err =skcipher_walk_virt (&walk, req, false); + +while ((nbytes = walk.nbytes) >0 ) { +const u8 *src = walk.src.virt.addr; +u8 *dst = walk.dst.virt.addr; + +kernel_fpu_begin (); +while (nbytes >=ARIA_AVX_BLOCK_SIZE ) { +aria_aesni_avx_crypt_16way (rkey, dst, src, ctx->rounds); + dst +=ARIA_AVX_BLOCK_SIZE ;arch/x86/crypto/aria_aesni_avx_glue.c line 47
Quoted 10 lines
arch/x86/crypto/aria_aesni_avx_glue.c new file +10
-0+ src +=ARIA_AVX_BLOCK_SIZE ; + nbytes -=ARIA_AVX_BLOCK_SIZE ; + } +while (nbytes >=ARIA_BLOCK_SIZE ) { +aria_encrypt (ctx, dst, src); + dst +=ARIA_BLOCK_SIZE ; + src +=ARIA_BLOCK_SIZE ; + nbytes -=ARIA_BLOCK_SIZE ; + } +kernel_fpu_end ();Is
aria_encrypt()an FPU function that belongs betweenkernel_fpu_begin()andkernel_fpu_end()?
Quoted 5 lines
Several drivers (camellia, serpent, twofish, cast5, cast6) use
ECB_WALK_START,ECB_BLOCK, andECB_WALK_ENDmacros for these loops. Those macros do only callkernel_fpu_end()at the very end. That requires the functions to be structured with (ctx, dst, src) arguments, not (rkey, dst, src, rounds).
aria_{encrypt | decrypt}() are not FPU functions.
I think if it uses the ECB macro, aria_{encrypt | decrypt}() will be still in the FPU context. So, I will get them out of the kernel FPU context.
Thank you so much!
Taehee Yoo
PATCH 3/3 crypto: tcrypt: add async speed test for aria cipher
In reply to Taehee YooIn order to test for the performance of aria-avx implementation, it needs an async speed test.
So, it adds async speed tests to the tcrypt.
--- crypto/tcrypt.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
crypto/tcrypt.c +13 -0
@@ -2648,6 +2648,13 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb) speed_template_16);break ; +case 519 : +test_acipher_speed ("ecb(aria)" ,ENCRYPT , sec,NULL ,0 , + speed_template_16_24_32); +test_acipher_speed ("ecb(aria)" ,DECRYPT , sec,NULL ,0 , + speed_template_16_24_32); +break ; +case 600 :test_mb_skcipher_speed ("ecb(aes)" ,ENCRYPT , sec,NULL ,0 , speed_template_16_24_32, num_mb);
@@ -2859,6 +2866,12 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb)test_mb_skcipher_speed ("ctr(blowfish)" ,DECRYPT , sec,NULL ,0 , speed_template_8_32, num_mb);break ; +case 610 : +test_mb_skcipher_speed ("ecb(aria)" ,ENCRYPT , sec,NULL ,0 , + speed_template_16_32, num_mb); +test_mb_skcipher_speed ("ecb(aria)" ,DECRYPT , sec,NULL ,0 , + speed_template_16_32, num_mb); +break ;case 1000 :test_available ();
Signature · 1 line
2.17.1