crypto: HCTR2 support
Subsystems (6)
v5 cover crypto: HCTR2 support
HCTR2 is a length-preserving encryption mode that is efficient on processors with instructions to accelerate AES and carryless multiplication, e.g. x86 processors with AES-NI and CLMUL, and ARM processors with the ARMv8 Crypto Extensions.
HCTR2 is specified in https://ia.cr/2021/1441 "Length-preserving encryption with HCTR2" which shows that if AES is secure and HCTR2 is instantiated with AES, then HCTR2 is secure. Reference code and test vectors are at
https://github.com/google/hctr2.
As a length-preserving encryption mode, HCTR2 is suitable for applications such as storage encryption where ciphertext expansion is not possible, and thus authenticated encryption cannot be used. Currently, such applications usually use XTS, or in some cases Adiantum. XTS has the disadvantage that it is a narrow-block mode: a bitflip will only change 16 bytes in the resulting ciphertext or plaintext. This reveals more information to an attacker than necessary.
HCTR2 is a wide-block mode, so it provides a stronger security property: a bitflip will change the entire message. HCTR2 is somewhat similar to Adiantum, which is also a wide-block mode. However, HCTR2 is designed to take advantage of existing crypto instructions, while Adiantum targets devices without such hardware support. Adiantum is also designed with longer messages in mind, while HCTR2 is designed to be efficient even on short messages.
The first intended use of this mode in the kernel is for the encryption of filenames, where for efficiency reasons encryption must be fully deterministic (only one ciphertext for each plaintext) and the existing CBC solution leaks more information than necessary for filenames with common prefixes.
HCTR2 uses two passes of an ε-almost-∆-universal hash function called POLYVAL and one pass of a block cipher mode called XCTR. POLYVAL is a polynomial hash designed for efficiency on modern processors and was originally specified for use in AES-GCM-SIV (RFC 8452). XCTR mode is a variant of CTR mode that is more efficient on little-endian machines.
This patchset adds HCTR2 to Linux's crypto API, including generic implementations of XCTR and POLYVAL, hardware accelerated implementations of XCTR and POLYVAL for both x86-64 and ARM64, a templated implementation of HCTR2, and an fscrypt policy for using HCTR2 for filename encryption.
Changes in v5:
* Refactor HCTR2 tweak hashing
* Remove non-AVX x86-64 XCTR implementation
* Combine arm64 CTR and XCTR modes
* Comment and alias CTR and XCTR modes
* Move generic fallback code for simd POLYVAL into polyval-generic.c
* Various small style fixes
Changes in v4:
* Small style fixes in generic POLYVAL and XCTR
* Move HCTR2 hash exporting/importing to helper functions
* Rewrite montgomery reduction for x86-64 POLYVAL
* Rewrite partial block handling for x86-64 POLYVAL
* Optimize x86-64 POLYVAL loop handling
* Remove ahash wrapper from x86-64 POLYVAL
* Add simd-unavailable handling to x86-64 POLYVAL
* Rewrite montgomery reduction for ARM64 POLYVAL
* Rewrite partial block handling for ARM64 POLYVAL
* Optimize ARM64 POLYVAL loop handling
* Remove ahash wrapper from ARM64 POLYVAL
* Add simd-unavailable handling to ARM64 POLYVAL
Changes in v3:
* Improve testvec coverage for XCTR, POLYVAL and HCTR2
* Fix endianness bug in xctr.c
* Fix alignment issues in polyval-generic.c
* Optimize hctr2.c by exporting/importing hash states
* Fix blockcipher name derivation in hctr2.c
* Move x86-64 XCTR implementation into aes_ctrby8_avx-x86_64.S
* Reuse ARM64 CTR mode tail handling in ARM64 XCTR
* Fix x86-64 POLYVAL comments
* Fix x86-64 POLYVAL key_powers type to match asm
* Fix ARM64 POLYVAL comments
* Fix ARM64 POLYVAL key_powers type to match asm
* Add XTS + HCTR2 policy to fscrypt
Nathan Huckleberry (8):
crypto: xctr - Add XCTR support
crypto: polyval - Add POLYVAL support
crypto: hctr2 - Add HCTR2 support
crypto: x86/aesni-xctr: Add accelerated implementation of XCTR
crypto: arm64/aes-xctr: Add accelerated implementation of XCTR
crypto: x86/polyval: Add PCLMULQDQ accelerated implementation of
POLYVAL
crypto: arm64/polyval: Add PMULL accelerated implementation of POLYVAL
fscrypt: Add HCTR2 support for filename encryption
Documentation/filesystems/fscrypt.rst | 22 +- arch/arm64/crypto/Kconfig | 9 +- arch/arm64/crypto/Makefile | 3 + arch/arm64/crypto/aes-glue.c | 64 +- arch/arm64/crypto/aes-modes.S | 290 +++-- arch/arm64/crypto/polyval-ce-core.S | 369 ++++++ arch/arm64/crypto/polyval-ce-glue.c | 194 +++ arch/x86/crypto/Makefile | 3 + arch/x86/crypto/aes_ctrby8_avx-x86_64.S | 232 ++-- arch/x86/crypto/aesni-intel_glue.c | 109 ++ arch/x86/crypto/polyval-clmulni_asm.S | 330 +++++ arch/x86/crypto/polyval-clmulni_glue.c | 200 +++ crypto/Kconfig | 39 +- crypto/Makefile | 3 + crypto/hctr2.c | 580 +++++++++ crypto/polyval-generic.c | 242 ++++ crypto/tcrypt.c | 10 + crypto/testmgr.c | 20 + crypto/testmgr.h | 1536 +++++++++++++++++++++++ crypto/xctr.c | 191 +++ fs/crypto/fscrypt_private.h | 2 +- fs/crypto/keysetup.c | 7 + fs/crypto/policy.c | 14 +- include/crypto/polyval.h | 26 + include/uapi/linux/fscrypt.h | 3 +- 25 files changed, 4306 insertions(+), 192 deletions(-) create mode 100644 arch/arm64/crypto/polyval-ce-core.S create mode 100644 arch/arm64/crypto/polyval-ce-glue.c create mode 100644 arch/x86/crypto/polyval-clmulni_asm.S create mode 100644 arch/x86/crypto/polyval-clmulni_glue.c create mode 100644 crypto/hctr2.c create mode 100644 crypto/polyval-generic.c create mode 100644 crypto/xctr.c create mode 100644 include/crypto/polyval.h
Signature · 1 line
2.36.0.rc2.479.g8af0fa9b8e-goog
PATCH v5 1/8 crypto: xctr - Add XCTR support
Add a generic implementation of XCTR mode as a template. XCTR is a blockcipher mode similar to CTR mode. XCTR uses XORs and little-endian addition rather than big-endian arithmetic which has two advantages: It is slightly faster on little-endian CPUs and it is less likely to be implemented incorrect since integer overflows are not possible on practical input sizes. XCTR is used as a component to implement HCTR2.
More information on XCTR mode can be found in the HCTR2 paper:
https://eprint.iacr.org/2021/1441.pdf
--- crypto/Kconfig | 9 + crypto/Makefile | 1 + crypto/tcrypt.c | 1 + crypto/testmgr.c | 6 + crypto/testmgr.h | 693 +++++++++++++++++++++++++++++++++++++++++++++++ crypto/xctr.c | 191 +++++++++++++ 6 files changed, 901 insertions(+) create mode 100644 crypto/xctr.c
crypto/Kconfig +9 -0
@@ -460,6 +460,15 @@ config CRYPTO_PCBC PCBC: Propagating Cipher Block Chaining mode This block cipher algorithm is required for RxRPC. +config CRYPTO_XCTR + tristate + select CRYPTO_SKCIPHER + select CRYPTO_MANAGER + help + XCTR: XOR Counter mode. This blockcipher mode is a variant of CTR mode + using XORs and little-endian addition rather than big-endian arithmetic. + XCTR mode is used to implement HCTR2. + config CRYPTO_XTS tristate "XTS support" select CRYPTO_SKCIPHER
crypto/Makefile +1 -0
@@ -93,6 +93,7 @@ obj-$(CONFIG_CRYPTO_CTS) += cts.o obj-$(CONFIG_CRYPTO_LRW) += lrw.o obj-$(CONFIG_CRYPTO_XTS) += xts.o obj-$(CONFIG_CRYPTO_CTR) += ctr.o +obj-$(CONFIG_CRYPTO_XCTR) += xctr.o obj-$(CONFIG_CRYPTO_KEYWRAP) += keywrap.o obj-$(CONFIG_CRYPTO_ADIANTUM) += adiantum.o obj-$(CONFIG_CRYPTO_NHPOLY1305) += nhpoly1305.o
crypto/tcrypt.c +1 -0
@@ -1556,6 +1556,7 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb) ret +=tcrypt_test ("rfc3686(ctr(aes))" ); ret +=tcrypt_test ("ofb(aes)" ); ret +=tcrypt_test ("cfb(aes)" ); + ret +=tcrypt_test ("xctr(aes)" );break ;case 11 :
crypto/testmgr.c +6 -0
@@ -5490,6 +5490,12 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .cipher = __VECS(xchacha20_tv_template) }, + }, { + .alg = "xctr(aes)", + .test = alg_test_skcipher, + .suite = { + .cipher = __VECS(aes_xctr_tv_template) + } }, { .alg = "xts(aes)", .generic_driver = "xts(ecb(aes-generic))",
crypto/testmgr.h +693 -0
697 lines not on this page · raw message
crypto/xctr.c new file +191 -0
191 lines not on this page · raw message
Signature · 1 line
2.36.0.rc2.479.g8af0fa9b8e-goog
PATCH v5 2/8 crypto: polyval - Add POLYVAL support
In reply to Nathan HuckleberryAdd support for POLYVAL, an ε-Δ-universal hash function similar to GHASH. This patch only uses POLYVAL as a component to implement HCTR2 mode. It should be noted that POLYVAL was originally specified for use in AES-GCM-SIV (RFC 8452), but the kernel does not currently support this mode.
POLYVAL is implemented as an shash algorithm. The implementation is modified from ghash-generic.c.
For more information on POLYVAL see:
Length-preserving encryption with HCTR2:
https://eprint.iacr.org/2021/1441.pdf
AES-GCM-SIV: Nonce Misuse-Resistant Authenticated Encryption:
https://datatracker.ietf.org/doc/html/rfc8452
--- crypto/Kconfig | 8 ++ crypto/Makefile | 1 + crypto/polyval-generic.c | 205 +++++++++++++++++++++++++++++++++++++++ crypto/tcrypt.c | 4 + crypto/testmgr.c | 6 ++ crypto/testmgr.h | 171 ++++++++++++++++++++++++++++++++ include/crypto/polyval.h | 17 ++++ 7 files changed, 412 insertions(+) create mode 100644 crypto/polyval-generic.c create mode 100644 include/crypto/polyval.h
crypto/Kconfig +8 -0
@@ -768,6 +768,14 @@ config CRYPTO_GHASH GHASH is the hash function used in GCM (Galois/Counter Mode). It is not a general-purpose cryptographic hash function. +config CRYPTO_POLYVAL + tristate + select CRYPTO_GF128MUL + select CRYPTO_HASH + help + POLYVAL is the hash function used in HCTR2. It is not a general-purpose + cryptographic hash function. + config CRYPTO_POLY1305 tristate "Poly1305 authenticator algorithm" select CRYPTO_HASH
crypto/Makefile +1 -0
@@ -169,6 +169,7 @@ UBSAN_SANITIZE_jitterentropy.o = n jitterentropy_rng-y := jitterentropy.o jitterentropy-kcapi.o obj-$(CONFIG_CRYPTO_TEST) += tcrypt.o obj-$(CONFIG_CRYPTO_GHASH) += ghash-generic.o +obj-$(CONFIG_CRYPTO_POLYVAL) += polyval-generic.o obj-$(CONFIG_CRYPTO_USER_API) += af_alg.o obj-$(CONFIG_CRYPTO_USER_API_HASH) += algif_hash.o obj-$(CONFIG_CRYPTO_USER_API_SKCIPHER) += algif_skcipher.o
crypto/polyval-generic.c new file +205 -0
205 lines not on this page · raw message
crypto/tcrypt.c +4 -0
@@ -1730,6 +1730,10 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb) ret +=tcrypt_test ("ccm(sm4)" );break ; +case 57 : + ret +=tcrypt_test ("polyval" ); +break ; +case 100 : ret +=tcrypt_test ("hmac(md5)" );break ;
crypto/testmgr.c +6 -0
@@ -5284,6 +5284,12 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .hash = __VECS(poly1305_tv_template) } + }, { + .alg = "polyval", + .test = alg_test_hash, + .suite = { + .hash = __VECS(polyval_tv_template) + } }, { .alg = "rfc3686(ctr(aes))", .test = alg_test_skcipher,
crypto/testmgr.h +171 -0
175 lines not on this page · raw message
include/crypto/polyval.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Common values for the Polyval hash algorithm +* +* Copyright 2021 Google LLC +*/ + +#ifndef _CRYPTO_POLYVAL_H +#define _CRYPTO_POLYVAL_H + +#include <linux/types.h> +#include <linux/crypto.h> + +#define POLYVAL_BLOCK_SIZE 16 +#define POLYVAL_DIGEST_SIZE 16 + +#endif
Signature · 1 line
2.36.0.rc2.479.g8af0fa9b8e-goog
PATCH v5 4/8 crypto: x86/aesni-xctr: Add accelerated implementation of XCTR
In reply to Nathan HuckleberryAdd hardware accelerated versions of XCTR for x86-64 CPUs with AESNI support. These implementations are modified versions of the CTR implementations found in aesni-intel_asm.S and aes_ctrby8_avx-x86_64.S.
More information on XCTR can be found in the HCTR2 paper:
"Length-preserving encryption with HCTR2":
https://eprint.iacr.org/2021/1441.pdf
--- arch/x86/crypto/aes_ctrby8_avx-x86_64.S | 232 ++++++++++++++++-------- arch/x86/crypto/aesni-intel_glue.c | 109 +++++++++++ crypto/Kconfig | 2 +- 3 files changed, 262 insertions(+), 81 deletions(-)
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +152 -80
@@ -23,6 +23,10 @@ #define VMOVDQ vmovdqu +/* Note: the "x" prefix in these aliases means "this is an xmm register". The + * alias prefixes have no relation to XCTR where the "X" prefix means "XOR + * counter". + */ #define xdata0 %xmm0 #define xdata1 %xmm1 #define xdata2 %xmm2
@@ -31,8 +35,10 @@ #define xdata5 %xmm5 #define xdata6 %xmm6 #define xdata7 %xmm7 -#define xcounter %xmm8 -#define xbyteswap %xmm9 +#define xcounter %xmm8 // CTR mode only +#define xiv %xmm8 // XCTR mode only +#define xbyteswap %xmm9 // CTR mode only +#define xtmp %xmm9 // XCTR mode only #define xkey0 %xmm10 #define xkey4 %xmm11 #define xkey8 %xmm12
@@ -45,7 +51,7 @@ #define p_keys %rdx #define p_out %rcx #define num_bytes %r8 - +#define counter %r9 // XCTR mode only #define tmp %r10 #define DDQ_DATA 0 #define XDATA 1
@@ -102,7 +108,7 @@ ddq_add_8: * do_aes num_in_par load_keys key_len * This increments p_in, but not p_out */ -.macro do_aes b, k, key_len +.macro do_aes b, k, key_len, xctr .set by, \b .set load_keys, \k .set klen, \key_len
@@ -111,29 +117,48 @@ ddq_add_8: vmovdqa 0*16(p_keys), xkey0 .endif - vpshufb xbyteswap, xcounter, xdata0 - - .set i, 1 - .rept (by - 1) - club XDATA, i - vpaddq (ddq_add_1 + 16 * (i - 1))(%rip), xcounter, var_xdata - vptest ddq_low_msk(%rip), var_xdata - jnz 1f - vpaddq ddq_high_add_1(%rip), var_xdata, var_xdata - vpaddq ddq_high_add_1(%rip), xcounter, xcounter - 1: - vpshufb xbyteswap, var_xdata, var_xdata - .set i, (i +1) - .endr + .if !\xctr + vpshufb xbyteswap, xcounter, xdata0 + .set i, 1 + .rept (by - 1) + club XDATA, i + vpaddq (ddq_add_1 + 16 * (i - 1))(%rip), xcounter, var_xdata + vptest ddq_low_msk(%rip), var_xdata + jnz 1f + vpaddq ddq_high_add_1(%rip), var_xdata, var_xdata + vpaddq ddq_high_add_1(%rip), xcounter, xcounter + 1: + vpshufb xbyteswap, var_xdata, var_xdata + .set i, (i +1) + .endr + .else + movq counter, xtmp + .set i, 0 + .rept (by) + club XDATA, i + vpaddq (ddq_add_1 + 16 * i)(%rip), xtmp, var_xdata + .set i, (i +1) + .endr + .set i, 0 + .rept (by) + club XDATA, i + vpxor xiv, var_xdata, var_xdata + .set i, (i +1) + .endr + .endif vmovdqa 1*16(p_keys), xkeyA vpxor xkey0, xdata0, xdata0 - vpaddq (ddq_add_1 + 16 * (by - 1))(%rip), xcounter, xcounter - vptest ddq_low_msk(%rip), xcounter - jnz 1f - vpaddq ddq_high_add_1(%rip), xcounter, xcounter - 1: + .if !\xctr + vpaddq (ddq_add_1 + 16 * (by - 1))(%rip), xcounter, xcounter + vptest ddq_low_msk(%rip), xcounter + jnz 1f + vpaddq ddq_high_add_1(%rip), xcounter, xcounter + 1: + .else + add $by, counter + .endif .set i, 1 .rept (by - 1)
@@ -371,94 +396,100 @@ ddq_add_8: .endr .endm -.macro do_aes_load val, key_len - do_aes \val, 1, \key_len +.macro do_aes_load val, key_len, xctr + do_aes \val, 1, \key_len, \xctr .endm -.macro do_aes_noload val, key_len - do_aes \val, 0, \key_len +.macro do_aes_noload val, key_len, xctr + do_aes \val, 0, \key_len, \xctr .endm /* main body of aes ctr load */ -.macro do_aes_ctrmain key_len +.macro do_aes_ctrmain key_len, xctr cmp $16, num_bytes - jb .Ldo_return2\key_len + jb .Ldo_return2\xctr\key_len - vmovdqa byteswap_const(%rip), xbyteswap - vmovdqu (p_iv), xcounter - vpshufb xbyteswap, xcounter, xcounter + .if !\xctr + vmovdqa byteswap_const(%rip), xbyteswap + vmovdqu (p_iv), xcounter + vpshufb xbyteswap, xcounter, xcounter + .else + andq $(~0xf), num_bytes + shr $4, counter + vmovdqu (p_iv), xiv + .endif mov num_bytes, tmp and $(7*16), tmp - jz .Lmult_of_8_blks\key_len + jz .Lmult_of_8_blks\xctr\key_len /* 1 <= tmp <= 7 */ cmp $(4*16), tmp - jg .Lgt4\key_len - je .Leq4\key_len + jg .Lgt4\xctr\key_len + je .Leq4\xctr\key_len -.Llt4\key_len: +.Llt4\xctr\key_len: cmp $(2*16), tmp - jg .Leq3\key_len - je .Leq2\key_len + jg .Leq3\xctr\key_len + je .Leq2\xctr\key_len -.Leq1\key_len: - do_aes_load 1, \key_len +.Leq1\xctr\key_len: + do_aes_load 1, \key_len, \xctr add $(1*16), p_out and $(~7*16), num_bytes - jz .Ldo_return2\key_len - jmp .Lmain_loop2\key_len + jz .Ldo_return2\xctr\key_len + jmp .Lmain_loop2\xctr\key_len -.Leq2\key_len: - do_aes_load 2, \key_len +.Leq2\xctr\key_len: + do_aes_load 2, \key_len, \xctr add $(2*16), p_out and $(~7*16), num_bytes - jz .Ldo_return2\key_len - jmp .Lmain_loop2\key_len + jz .Ldo_return2\xctr\key_len + jmp .Lmain_loop2\xctr\key_len -.Leq3\key_len: - do_aes_load 3, \key_len +.Leq3\xctr\key_len: + do_aes_load 3, \key_len, \xctr add $(3*16), p_out and $(~7*16), num_bytes - jz .Ldo_return2\key_len - jmp .Lmain_loop2\key_len + jz .Ldo_return2\xctr\key_len + jmp .Lmain_loop2\xctr\key_len -.Leq4\key_len: - do_aes_load 4, \key_len +.Leq4\xctr\key_len: + do_aes_load 4, \key_len, \xctr add $(4*16), p_out and $(~7*16), num_bytes - jz .Ldo_return2\key_len - jmp .Lmain_loop2\key_len + jz .Ldo_return2\xctr\key_len + jmp .Lmain_loop2\xctr\key_len -.Lgt4\key_len: +.Lgt4\xctr\key_len: cmp $(6*16), tmp - jg .Leq7\key_len - je .Leq6\key_len + jg .Leq7\xctr\key_len + je .Leq6\xctr\key_len -.Leq5\key_len: - do_aes_load 5, \key_len +.Leq5\xctr\key_len: + do_aes_load 5, \key_len, \xctr add $(5*16), p_out and $(~7*16), num_bytes - jz .Ldo_return2\key_len - jmp .Lmain_loop2\key_len + jz .Ldo_return2\xctr\key_len + jmp .Lmain_loop2\xctr\key_len -.Leq6\key_len: - do_aes_load 6, \key_len +.Leq6\xctr\key_len: + do_aes_load 6, \key_len, \xctr add $(6*16), p_out and $(~7*16), num_bytes - jz .Ldo_return2\key_len - jmp .Lmain_loop2\key_len + jz .Ldo_return2\xctr\key_len + jmp .Lmain_loop2\xctr\key_len -.Leq7\key_len: - do_aes_load 7, \key_len +.Leq7\xctr\key_len: + do_aes_load 7, \key_len, \xctr add $(7*16), p_out and $(~7*16), num_bytes - jz .Ldo_return2\key_len - jmp .Lmain_loop2\key_len + jz .Ldo_return2\xctr\key_len + jmp .Lmain_loop2\xctr\key_len -.Lmult_of_8_blks\key_len: +.Lmult_of_8_blks\xctr\key_len: .if (\key_len != KEY_128) vmovdqa 0*16(p_keys), xkey0 vmovdqa 4*16(p_keys), xkey4
@@ -471,17 +502,19 @@ ddq_add_8: vmovdqa 9*16(p_keys), xkey12 .endif .align 16 -.Lmain_loop2\key_len: +.Lmain_loop2\xctr\key_len: /* num_bytes is a multiple of 8 and >0 */ - do_aes_noload 8, \key_len + do_aes_noload 8, \key_len, \xctr add $(8*16), p_out sub $(8*16), num_bytes - jne .Lmain_loop2\key_len + jne .Lmain_loop2\xctr\key_len -.Ldo_return2\key_len: - /* return updated IV */ - vpshufb xbyteswap, xcounter, xcounter - vmovdqu xcounter, (p_iv) +.Ldo_return2\xctr\key_len: + .if !\xctr + /* return updated IV */ + vpshufb xbyteswap, xcounter, xcounter + vmovdqu xcounter, (p_iv) + .endif RET .endm
@@ -494,7 +527,7 @@ ddq_add_8: */ SYM_FUNC_START(aes_ctr_enc_128_avx_by8) /* call the aes main loop */ - do_aes_ctrmain KEY_128 + do_aes_ctrmain KEY_128 0 SYM_FUNC_END(aes_ctr_enc_128_avx_by8)
@@ -507,7 +540,7 @@ SYM_FUNC_END(aes_ctr_enc_128_avx_by8) */ SYM_FUNC_START(aes_ctr_enc_192_avx_by8) /* call the aes main loop */ - do_aes_ctrmain KEY_192 + do_aes_ctrmain KEY_192 0 SYM_FUNC_END(aes_ctr_enc_192_avx_by8)
@@ -520,6 +553,45 @@ SYM_FUNC_END(aes_ctr_enc_192_avx_by8) */ SYM_FUNC_START(aes_ctr_enc_256_avx_by8) /* call the aes main loop */ - do_aes_ctrmain KEY_256 + do_aes_ctrmain KEY_256 0 SYM_FUNC_END(aes_ctr_enc_256_avx_by8) + +/* + * routine to do AES128 XCTR enc/decrypt "by8" + * XMM registers are clobbered. + * Saving/restoring must be done at a higher level + * aes_xctr_enc_128_avx_by8(const u8 *in, const u8 *iv, const void *keys, + * u8* out, unsigned int num_bytes, unsigned int byte_ctr) + */ +SYM_FUNC_START(aes_xctr_enc_128_avx_by8) + /* call the aes main loop */ + do_aes_ctrmain KEY_128 1 + +SYM_FUNC_END(aes_xctr_enc_128_avx_by8) + +/* + * routine to do AES192 XCTR enc/decrypt "by8" + * XMM registers are clobbered. + * Saving/restoring must be done at a higher level + * aes_xctr_enc_192_avx_by8(const u8 *in, const u8 *iv, const void *keys, + * u8* out, unsigned int num_bytes, unsigned int byte_ctr) + */ +SYM_FUNC_START(aes_xctr_enc_192_avx_by8) + /* call the aes main loop */ + do_aes_ctrmain KEY_192 1 + +SYM_FUNC_END(aes_xctr_enc_192_avx_by8) + +/* + * routine to do AES256 XCTR enc/decrypt "by8" + * XMM registers are clobbered. + * Saving/restoring must be done at a higher level + * aes_xctr_enc_256_avx_by8(const u8 *in, const u8 *iv, const void *keys, + * u8* out, unsigned int num_bytes, unsigned int byte_ctr) + */ +SYM_FUNC_START(aes_xctr_enc_256_avx_by8) + /* call the aes main loop */ + do_aes_ctrmain KEY_256 1 + +SYM_FUNC_END(aes_xctr_enc_256_avx_by8)
arch/x86/crypto/aesni-intel_glue.c +109 -0
141 lines not on this page · raw message
crypto/Kconfig +1 -1
@@ -1161,7 +1161,7 @@ config CRYPTO_AES_NI_INTEL In addition to AES cipher algorithm support, the acceleration for some popular block cipher mode is supported too, including ECB, CBC, LRW, XTS. The 64 bit version has additional - acceleration for CTR. + acceleration for CTR and XCTR. config CRYPTO_AES_SPARC64 tristate "AES cipher algorithms (SPARC64)"
Signature · 1 line
2.36.0.rc2.479.g8af0fa9b8e-goog
PATCH v5 3/8 crypto: hctr2 - Add HCTR2 support
In reply to Nathan HuckleberryAdd support for HCTR2 as a template. HCTR2 is a length-preserving encryption mode that is efficient on processors with instructions to accelerate AES and carryless multiplication, e.g. x86 processors with AES-NI and CLMUL, and ARM processors with the ARMv8 Crypto Extensions.
As a length-preserving encryption mode, HCTR2 is suitable for applications such as storage encryption where ciphertext expansion is not possible, and thus authenticated encryption cannot be used. Currently, such applications usually use XTS, or in some cases Adiantum. XTS has the disadvantage that it is a narrow-block mode: a bitflip will only change 16 bytes in the resulting ciphertext or plaintext. This reveals more information to an attacker than necessary.
HCTR2 is a wide-block mode, so it provides a stronger security property:
a bitflip will change the entire message. HCTR2 is somewhat similar to Adiantum, which is also a wide-block mode. However, HCTR2 is designed to take advantage of existing crypto instructions, while Adiantum targets devices without such hardware support. Adiantum is also designed with longer messages in mind, while HCTR2 is designed to be efficient even on short messages.
HCTR2 requires POLYVAL and XCTR as components. More information on HCTR2 can be found here: Length-preserving encryption with HCTR2:
https://eprint.iacr.org/2021/1441.pdf
--- crypto/Kconfig | 11 + crypto/Makefile | 1 + crypto/hctr2.c | 580 ++++++++++++++++++++++++++++++++++++++++ crypto/tcrypt.c | 5 + crypto/testmgr.c | 8 + crypto/testmgr.h | 672 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 1277 insertions(+) create mode 100644 crypto/hctr2.c
crypto/Kconfig +11 -0
@@ -532,6 +532,17 @@ config CRYPTO_ADIANTUM If unsure, say N. +config CRYPTO_HCTR2 + tristate "HCTR2 support" + select CRYPTO_XCTR + select CRYPTO_POLYVAL + select CRYPTO_MANAGER + help + HCTR2 is a length-preserving encryption mode for storage encryption that + is efficient on processors with instructions to accelerate AES and + carryless multiplication, e.g. x86 processors with AES-NI and CLMUL, and + ARM processors with the ARMv8 crypto extensions. + config CRYPTO_ESSIV tristate "ESSIV support for block encryption" select CRYPTO_AUTHENC
crypto/Makefile +1 -0
@@ -94,6 +94,7 @@ obj-$(CONFIG_CRYPTO_LRW) += lrw.o obj-$(CONFIG_CRYPTO_XTS) += xts.o obj-$(CONFIG_CRYPTO_CTR) += ctr.o obj-$(CONFIG_CRYPTO_XCTR) += xctr.o +obj-$(CONFIG_CRYPTO_HCTR2) += hctr2.o obj-$(CONFIG_CRYPTO_KEYWRAP) += keywrap.o obj-$(CONFIG_CRYPTO_ADIANTUM) += adiantum.o obj-$(CONFIG_CRYPTO_NHPOLY1305) += nhpoly1305.o
crypto/hctr2.c new file +580 -0
580 lines not on this page · raw message
crypto/tcrypt.c +5 -0
@@ -2191,6 +2191,11 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb) 16, 16, aead_speed_template_19, num_mb);break ; +case 226 : +test_cipher_speed ("hctr2(aes)" ,ENCRYPT , sec,NULL , +0 , speed_template_32); +break ; +case 300 :if (alg) {test_hash_speed (alg, sec, generic_hash_speed_template);
crypto/testmgr.c +8 -0
@@ -5030,6 +5030,14 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .hash = __VECS(ghash_tv_template) } + }, { + .alg = "hctr2(aes)", + .generic_driver = + "hctr2_base(xctr(aes-generic),polyval-generic)", + .test = alg_test_skcipher, + .suite = { + .cipher = __VECS(aes_hctr2_tv_template) + } }, { .alg = "hmac(md5)", .test = alg_test_hash,
crypto/testmgr.h +672 -0
676 lines not on this page · raw message
Signature · 1 line
2.36.0.rc2.479.g8af0fa9b8e-goog
PATCH v5 5/8 crypto: arm64/aes-xctr: Add accelerated implementation of XCTR
In reply to Nathan HuckleberryAdd hardware accelerated version of XCTR for ARM64 CPUs with ARMv8 Crypto Extension support. This XCTR implementation is based on the CTR implementation in aes-modes.S.
More information on XCTR can be found in
the HCTR2 paper: Length-preserving encryption with HCTR2:
https://eprint.iacr.org/2021/1441.pdf
--- arch/arm64/crypto/Kconfig | 4 +- arch/arm64/crypto/aes-glue.c | 64 +++++++- arch/arm64/crypto/aes-modes.S | 290 ++++++++++++++++++++++------------ 3 files changed, 257 insertions(+), 101 deletions(-)
arch/arm64/crypto/Kconfig +2 -2
@@ -84,13 +84,13 @@ config CRYPTO_AES_ARM64_CE_CCM select CRYPTO_LIB_AES config CRYPTO_AES_ARM64_CE_BLK - tristate "AES in ECB/CBC/CTR/XTS modes using ARMv8 Crypto Extensions" + tristate "AES in ECB/CBC/CTR/XTS/XCTR modes using ARMv8 Crypto Extensions" depends on KERNEL_MODE_NEON select CRYPTO_SKCIPHER select CRYPTO_AES_ARM64_CE config CRYPTO_AES_ARM64_NEON_BLK - tristate "AES in ECB/CBC/CTR/XTS modes using NEON instructions" + tristate "AES in ECB/CBC/CTR/XTS/XCTR modes using NEON instructions" depends on KERNEL_MODE_NEON select CRYPTO_SKCIPHER select CRYPTO_LIB_AES
arch/arm64/crypto/aes-glue.c +62 -2
106 lines not on this page · raw message
arch/arm64/crypto/aes-modes.S +193 -97
351 lines not on this page · raw message
Signature · 1 line
2.36.0.rc2.479.g8af0fa9b8e-goog
PATCH v5 7/8 crypto: arm64/polyval: Add PMULL accelerated implementation of POLYVAL
In reply to Nathan HuckleberryAdd hardware accelerated version of POLYVAL for ARM64 CPUs with Crypto Extensions support.
This implementation is accelerated using PMULL instructions to perform the finite field computations. For added efficiency, 8 blocks of the message are processed simultaneously by precomputing the first 8 powers of the key.
Karatsuba multiplication is used instead of Schoolbook multiplication because it was found to be slightly faster on ARM64 CPUs. Montgomery reduction must be used instead of Barrett reduction due to the difference in modulus between POLYVAL's field and other finite fields.
--- arch/arm64/crypto/Kconfig | 5 + arch/arm64/crypto/Makefile | 3 + arch/arm64/crypto/polyval-ce-core.S | 369 +++++++++++++++++++++++++ arch/arm64/crypto/polyval-ce-glue.c | 194 +++++++++++++ arch/x86/crypto/polyval-clmulni_glue.c | 2 +- 5 files changed, 572 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/crypto/polyval-ce-core.S create mode 100644 arch/arm64/crypto/polyval-ce-glue.c
arch/arm64/crypto/Kconfig +5 -0
@@ -60,6 +60,11 @@ config CRYPTO_GHASH_ARM64_CE select CRYPTO_GF128MUL select CRYPTO_LIB_AES +config CRYPTO_POLYVAL_ARM64_CE + tristate "POLYVAL using ARMv8 Crypto Extensions (for HCTR2)" + depends on KERNEL_MODE_NEON + select CRYPTO_POLYVAL + config CRYPTO_CRCT10DIF_ARM64_CE tristate "CRCT10DIF digest algorithm using PMULL instructions" depends on KERNEL_MODE_NEON && CRC_T10DIF
arch/arm64/crypto/Makefile +3 -0
@@ -26,6 +26,9 @@ sm4-ce-y := sm4-ce-glue.o sm4-ce-core.o obj-$(CONFIG_CRYPTO_GHASH_ARM64_CE) += ghash-ce.o ghash-ce-y := ghash-ce-glue.o ghash-ce-core.o +obj-$(CONFIG_CRYPTO_POLYVAL_ARM64_CE) += polyval-ce.o +polyval-ce-y := polyval-ce-glue.o polyval-ce-core.o + obj-$(CONFIG_CRYPTO_CRCT10DIF_ARM64_CE) += crct10dif-ce.o crct10dif-ce-y := crct10dif-ce-core.o crct10dif-ce-glue.o
arch/arm64/crypto/polyval-ce-core.S new file +369 -0
369 lines not on this page · raw message
arch/arm64/crypto/polyval-ce-glue.c new file +194 -0
194 lines not on this page · raw message
arch/x86/crypto/polyval-clmulni_glue.c +1 -1
@@ -172,7 +172,7 @@ static struct shash_alg polyval_alg = { }, }; -static const struct x86_cpu_id pcmul_cpu_id[] = { +__maybe_unused static const struct x86_cpu_id pcmul_cpu_id[] = {X86_MATCH_FEATURE (X86_FEATURE_PCLMULQDQ ,NULL ),/* Pickle-Mickle-Duck */ {} };
Signature · 1 line
2.36.0.rc2.479.g8af0fa9b8e-goog
PATCH v5 8/8 fscrypt: Add HCTR2 support for filename encryption
In reply to Nathan HuckleberryHCTR2 is a tweakable, length-preserving encryption mode that is intended for use on CPUs with dedicated crypto instructions. HCTR2 has the property that a bitflip in the plaintext changes the entire ciphertext. This property fixes a known weakness with filename encryption: when two filenames in the same directory share a prefix of >= 16 bytes, with AES-CTS-CBC their encrypted filenames share a common substring, leaking information. HCTR2 does not have this problem.
More information on HCTR2 can be found here: "Length-preserving encryption with HCTR2": https://eprint.iacr.org/2021/1441.pdf
--- Documentation/filesystems/fscrypt.rst | 22 +++++++++++++++++----- fs/crypto/fscrypt_private.h | 2 +- fs/crypto/keysetup.c | 7 +++++++ fs/crypto/policy.c | 14 +++++++++++--- include/uapi/linux/fscrypt.h | 3 ++- 5 files changed, 38 insertions(+), 10 deletions(-)
Documentation/filesystems/fscrypt.rst +17 -5
@@ -337,6 +337,7 @@ Currently, the following pairs of encryption modes are supported: - AES-256-XTS for contents and AES-256-CTS-CBC for filenames - AES-128-CBC for contents and AES-128-CTS-CBC for filenames - Adiantum for both contents and filenames +- AES-256-XTS for contents and AES-256-HCTR2 for filenames (v2 policies only) If unsure, you should use the (AES-256-XTS, AES-256-CTS-CBC) pair.
@@ -357,6 +358,17 @@ To use Adiantum, CONFIG_CRYPTO_ADIANTUM must be enabled. Also, fast implementations of ChaCha and NHPoly1305 should be enabled, e.g. CONFIG_CRYPTO_CHACHA20_NEON and CONFIG_CRYPTO_NHPOLY1305_NEON for ARM. +AES-256-HCTR2 is another true wide-block encryption mode that is intended for +use on CPUs with dedicated crypto instructions. AES-256-HCTR2 has the property +that a bitflip in the plaintext changes the entire ciphertext. This property +makes it desirable for filename encryption since initialization vectors are +reused within a directory. For more details on AES-256-HCTR2, see the paper +"Length-preserving encryption with HCTR2" +(https://eprint.iacr.org/2021/1441.pdf). To use AES-256-HCTR2, +CONFIG_CRYPTO_HCTR2 must be enabled. Also, fast implementations of XCTR and +POLYVAL should be enabled, e.g. CRYPTO_POLYVAL_ARM64_CE and +CRYPTO_AES_ARM64_CE_BLK for ARM64. + New encryption modes can be added relatively easily, without changes to individual filesystems. However, authenticated encryption (AE) modes are not currently supported because of the difficulty of dealing
@@ -404,11 +416,11 @@ alternatively has the file's nonce (for `DIRECT_KEY policies`_) or inode number (for `IV_INO_LBLK_64 policies`_) included in the IVs. Thus, IV reuse is limited to within a single directory. -With CTS-CBC, the IV reuse means that when the plaintext filenames -share a common prefix at least as long as the cipher block size (16 -bytes for AES), the corresponding encrypted filenames will also share -a common prefix. This is undesirable. Adiantum does not have this -weakness, as it is a wide-block encryption mode. +With CTS-CBC, the IV reuse means that when the plaintext filenames share a +common prefix at least as long as the cipher block size (16 bytes for AES), the +corresponding encrypted filenames will also share a common prefix. This is +undesirable. Adiantum and HCTR2 do not have this weakness, as they are +wide-block encryption modes. All supported filenames encryption modes accept any plaintext length >= 16 bytes; cipher block alignment is not required. However,
fs/crypto/fscrypt_private.h +1 -1
@@ -31,7 +31,7 @@#define FSCRYPT_CONTEXT_V2 2/* Keep this in sync with include/uapi/linux/fscrypt.h */ -#define FSCRYPT_MODE_MAX FSCRYPT_MODE_ADIANTUM +#define FSCRYPT_MODE_MAX FSCRYPT_MODE_AES_256_HCTR2struct fscrypt_context_v1 {u8 version;/* FSCRYPT_CONTEXT_V1 */
fs/crypto/keysetup.c +7 -0
@@ -53,6 +53,13 @@ struct fscrypt_mode fscrypt_modes[] = { .ivsize = 32, .blk_crypto_mode = BLK_ENCRYPTION_MODE_ADIANTUM, }, + [FSCRYPT_MODE_AES_256_HCTR2] = { + .friendly_name = "AES-256-HCTR2", + .cipher_str = "hctr2(aes)", + .keysize = 32, + .security_strength = 32, + .ivsize = 32, + }, };static DEFINE_MUTEX (fscrypt_mode_key_setup_mutex);
fs/crypto/policy.c +11 -3
@@ -40,7 +40,7 @@ fscrypt_get_dummy_policy(struct super_block *sb)return sb->s_cop->get_dummy_policy (sb); } -static bool fscrypt_valid_enc_modes (u32 contents_mode,u32 filenames_mode) +static bool fscrypt_valid_enc_modes_v1 (u32 contents_mode,u32 filenames_mode) {if (contents_mode ==FSCRYPT_MODE_AES_256_XTS && filenames_mode ==FSCRYPT_MODE_AES_256_CTS )
@@ -57,6 +57,14 @@ static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)return false; } +static bool fscrypt_valid_enc_modes_v2 (u32 contents_mode,u32 filenames_mode) +{ +if (contents_mode ==FSCRYPT_MODE_AES_256_XTS && + filenames_mode ==FSCRYPT_MODE_AES_256_HCTR2 ) +return true; +return fscrypt_valid_enc_modes_v1 (contents_mode, filenames_mode); +} +static bool supported_direct_key_modes (const struct inode *inode,u32 contents_mode,u32 filenames_mode) {
@@ -130,7 +138,7 @@ static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,static bool fscrypt_supported_v1_policy (const struct fscrypt_policy_v1 *policy,const struct inode *inode) { -if (!fscrypt_valid_enc_modes (policy->contents_encryption_mode, +if (!fscrypt_valid_enc_modes_v1 (policy->contents_encryption_mode, policy->filenames_encryption_mode)) { fscrypt_warn(inode, "Unsupported encryption modes (contents %d, filenames %d)",
@@ -166,7 +174,7 @@ static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy, {int count =0 ; -if (!fscrypt_valid_enc_modes (policy->contents_encryption_mode, +if (!fscrypt_valid_enc_modes_v2 (policy->contents_encryption_mode, policy->filenames_encryption_mode)) { fscrypt_warn(inode, "Unsupported encryption modes (contents %d, filenames %d)",
include/uapi/linux/fscrypt.h +2 -1
@@ -27,7 +27,8 @@#define FSCRYPT_MODE_AES_128_CBC 5#define FSCRYPT_MODE_AES_128_CTS 6#define FSCRYPT_MODE_ADIANTUM 9 -/* If adding a mode number > 9, update FSCRYPT_MODE_MAX in fscrypt_private.h */ +#define FSCRYPT_MODE_AES_256_HCTR2 10 +/* If adding a mode number > 10, update FSCRYPT_MODE_MAX in fscrypt_private.h */ /* * Legacy policy version; ad-hoc KDF and no key verification.
Signature · 1 line
2.36.0.rc2.479.g8af0fa9b8e-goog
PATCH v5 6/8 crypto: x86/polyval: Add PCLMULQDQ accelerated implementation of POLYVAL
In reply to Nathan HuckleberryAdd hardware accelerated version of POLYVAL for x86-64 CPUs with PCLMULQDQ support.
This implementation is accelerated using PCLMULQDQ instructions to perform the finite field computations. For added efficiency, 8 blocks of the message are processed simultaneously by precomputing the first 8 powers of the key.
Schoolbook multiplication is used instead of Karatsuba multiplication because it was found to be slightly faster on x86-64 machines. Montgomery reduction must be used instead of Barrett reduction due to the difference in modulus between POLYVAL's field and other finite fields.
More information on POLYVAL can be found in the HCTR2 paper:
Length-preserving encryption with HCTR2:
https://eprint.iacr.org/2021/1441.pdf
--- arch/x86/crypto/Makefile | 3 + arch/x86/crypto/polyval-clmulni_asm.S | 330 +++++++++++++++++++++++++ arch/x86/crypto/polyval-clmulni_glue.c | 200 +++++++++++++++ crypto/Kconfig | 9 + crypto/polyval-generic.c | 43 +++- include/crypto/polyval.h | 9 + 6 files changed, 591 insertions(+), 3 deletions(-) create mode 100644 arch/x86/crypto/polyval-clmulni_asm.S create mode 100644 arch/x86/crypto/polyval-clmulni_glue.c
arch/x86/crypto/Makefile +3 -0
@@ -69,6 +69,9 @@ libblake2s-x86_64-y := blake2s-core.o blake2s-glue.o obj-$(CONFIG_CRYPTO_GHASH_CLMUL_NI_INTEL) += ghash-clmulni-intel.o ghash-clmulni-intel-y := ghash-clmulni-intel_asm.o ghash-clmulni-intel_glue.o +obj-$(CONFIG_CRYPTO_POLYVAL_CLMUL_NI) += polyval-clmulni.o +polyval-clmulni-y := polyval-clmulni_asm.o polyval-clmulni_glue.o + obj-$(CONFIG_CRYPTO_CRC32C_INTEL) += crc32c-intel.o crc32c-intel-y := crc32c-intel_glue.o crc32c-intel-$(CONFIG_64BIT) += crc32c-pcl-intel-asm_64.o
arch/x86/crypto/polyval-clmulni_asm.S new file +330 -0
330 lines not on this page · raw message
arch/x86/crypto/polyval-clmulni_glue.c new file +200 -0
200 lines not on this page · raw message
crypto/Kconfig +9 -0
@@ -787,6 +787,15 @@ config CRYPTO_POLYVAL POLYVAL is the hash function used in HCTR2. It is not a general-purpose cryptographic hash function. +config CRYPTO_POLYVAL_CLMUL_NI + tristate "POLYVAL hash function (CLMUL-NI accelerated)" + depends on X86 && 64BIT + select CRYPTO_POLYVAL + help + This is the x86_64 CLMUL-NI accelerated implementation of POLYVAL. It is + used to efficiently implement HCTR2 on x86-64 processors that support + carry-less multiplication instructions. + config CRYPTO_POLY1305 tristate "Poly1305 authenticator algorithm" select CRYPTO_HASH
crypto/polyval-generic.c +40 -3
61 lines not on this page · raw message
include/crypto/polyval.h +9 -0
@@ -8,10 +8,19 @@#ifndef _CRYPTO_POLYVAL_H#define _CRYPTO_POLYVAL_H +#include <crypto/gf128mul.h> #include <linux/types.h> #include <linux/crypto.h> #define POLYVAL_BLOCK_SIZE 16#define POLYVAL_DIGEST_SIZE 16 +void copy_and_reverse (u8 dst[POLYVAL_BLOCK_SIZE ], +const u8 src[POLYVAL_BLOCK_SIZE ]); + +void polyval_mul_non4k (u8 *op1,const u8 *op2); + +void polyval_update_non4k (const u8 *key,const u8 *in, +size_t nblocks,u8 *accumulator); +#endif
Signature · 1 line
2.36.0.rc2.479.g8af0fa9b8e-goog
kernel test robot
build botRe: [PATCH v5 4/8] crypto: x86/aesni-xctr: Add accelerated implementation of XCTR
In reply to Nathan HuckleberryThank you for the patch! Perhaps something to improve:
Hi Nathan,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on herbert-cryptodev-2.6/master]
[also build test WARNING on herbert-crypto-2.6/master linus/master v5.18-rc4 next-20220426] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/intel-lab-lkp/linux/commits/Nathan-Huckleberry/crypto-HCTR2-support/20220427-084044 base: https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master config: i386-randconfig-a002-20220425 (https://download.01.org/0day-ci/archive/20220427/[email protected]/config) compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project 1cddcfdc3c683b393df1a5c9063252eb60e52818)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/00cd244c8a1bd9623a271407bf10b99c01884ef5
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Nathan-Huckleberry/crypto-HCTR2-support/20220427-084044
git checkout 00cd244c8a1bd9623a271407bf10b99c01884ef5
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash arch/x86/crypto/
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> arch/x86/crypto/aesni-intel_glue.c:1287:1: warning: unused label 'unregister_aeads' [-Wunused-label]
unregister_aeads:
^~~~~~~~~~~~~~~~~
1 warning generated.
vim +/unregister_aeads +1287 arch/x86/crypto/aesni-intel_glue.c
1284
1285 return 0;
1286
> 1287 unregister_aeads:
1288 simd_unregister_aeads(aesni_aeads, ARRAY_SIZE(aesni_aeads),
1289 aesni_simd_aeads);
1290 unregister_skciphers:
1291 simd_unregister_skciphers(aesni_skciphers, ARRAY_SIZE(aesni_skciphers),
1292 aesni_simd_skciphers);
1293 unregister_cipher:
1294 crypto_unregister_alg(&aesni_cipher_alg);
1295 return err;
1296 }
1297
Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
kernel test robot
build botRe: [PATCH v5 7/8] crypto: arm64/polyval: Add PMULL accelerated implementation of POLYVAL
In reply to Nathan HuckleberryThank you for the patch! Yet something to improve:
Hi Nathan,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on herbert-cryptodev-2.6/master]
[also build test ERROR on herbert-crypto-2.6/master linus/master v5.18-rc4 next-20220429] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/intel-lab-lkp/linux/commits/Nathan-Huckleberry/crypto-HCTR2-support/20220427-084044 base: https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master config: arm64-allmodconfig (https://download.01.org/0day-ci/archive/20220430/[email protected]/config) compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project c59473aacce38cd7dd77eebceaf3c98c5707ab3b)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/3d3f8a04f4735ce4fc1a9b59cd6fa091e11a35c3
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Nathan-Huckleberry/crypto-HCTR2-support/20220427-084044
git checkout 3d3f8a04f4735ce4fc1a9b59cd6fa091e11a35c3
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash arch/arm64/
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
>> arch/arm64/crypto/polyval-ce-glue.c:188:1: error: redefinition of '__inittest'
module_init(polyval_ce_mod_init);
^
include/linux/module.h:131:42: note: expanded from macro 'module_init'
static inline initcall_t __maybe_unused __inittest(void) \
^
arch/arm64/crypto/polyval-ce-glue.c:186:1: note: previous definition is here
module_cpu_feature_match(PMULL, polyval_ce_mod_init)
^
include/linux/cpufeature.h:54:10: note: expanded from macro 'module_cpu_feature_match'
} \
^
include/linux/module.h:131:42: note: expanded from macro '\
module_init'
static inline initcall_t __maybe_unused __inittest(void) \
^
>> arch/arm64/crypto/polyval-ce-glue.c:188:1: error: redefinition of 'init_module'
module_init(polyval_ce_mod_init);
^
include/linux/module.h:133:6: note: expanded from macro 'module_init'
int init_module(void) __copy(initfn) \
^
arch/arm64/crypto/polyval-ce-glue.c:186:1: note: previous definition is here
module_cpu_feature_match(PMULL, polyval_ce_mod_init)
^
include/linux/cpufeature.h:54:10: note: expanded from macro 'module_cpu_feature_match'
} \
^
include/linux/module.h:133:6: note: expanded from macro '\
module_init'
int init_module(void) __copy(initfn) \
^2 errors generated.
vim +/__inittest +188 arch/arm64/crypto/polyval-ce-glue.c
187
> 188 module_init(polyval_ce_mod_init);
189 module_exit(polyval_ce_mod_exit);
190
Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
Re: [PATCH v5 3/8] crypto: hctr2 - Add HCTR2 support
In reply to Nathan HuckleberryQuoted 9 lines · Nathan Huckleberry
+/* The input data for each HCTR2 hash step begins with a 16-byte block that
+ * contains the tweak length and a flag that indicates whether the input is evenly
+ * divisible into blocks. Since this implementation only supports one tweak
+ * length, we precompute the two hash states resulting from hashing the two
+ * possible values of this initial block. This reduces by one block the amount of
+ * data that needs to be hashed for each encryption/decryption
+ *
+ * These precomputed hashes are stored inhctr2_tfm_ctx.
+ */
Block comments should look like this:
/*
* text
*/
i.e. there should be a newline after the "/*"
crypto/hctr2.c line 155
Quoted 5 lines
crypto/hctr2.c new file +5
-0+memset (tctx->L,0 ,sizeof (tctx->L)); +memset (hbar,0 ,sizeof (hbar)); + tctx->L[0 ] =0x01 ; +crypto_cipher_encrypt_one (tctx->blockcipher, tctx->L, tctx->L); +crypto_cipher_encrypt_one (tctx->blockcipher, hbar, hbar);
This would be easier to read if the computations of hbar and L were separated:
memset(hbar, 0, sizeof(hbar)); crypto_cipher_encrypt_one(tctx->blockcipher, hbar, hbar); memset(tctx->L, 0, sizeof(tctx->L)); tctx->L[0] = 0x01; crypto_cipher_encrypt_one(tctx->blockcipher, tctx->L, tctx->L);
Quoted 16 lines
crypto/hctr2.c new file +16
-0+static int hctr2_hash_message (struct skcipher_request *req, +struct scatterlist *sgl, +u8 digest[POLYVAL_DIGEST_SIZE ]) +{ +u8 padding[BLOCKCIPHER_BLOCK_SIZE ]; +struct hctr2_request_ctx *rctx =skcipher_request_ctx (req); +struct shash_desc *hash_desc = &rctx->u.hash_desc; +const unsigned int bulk_len = req->cryptlen -BLOCKCIPHER_BLOCK_SIZE ; +struct sg_mapping_iter miter; +unsigned int remainder = bulk_len %BLOCKCIPHER_BLOCK_SIZE ; +int err, i; +int n =0 ; + +sg_miter_start (&miter, sgl,sg_nents (sgl), +SG_MITER_FROM_SG |SG_MITER_ATOMIC ); +for (i =0 ; i < bulk_len; i += n) {
crypto/hctr2.c line 215
Quoted 10 lines
crypto/hctr2.c new file +10
-0+sg_miter_next (&miter); + n = min_t(unsigned int, miter.length, bulk_len - i); + err =crypto_shash_update (hash_desc, miter.addr, n); +if (err) +break ; + } +sg_miter_stop (&miter); + +if (err) +return err;
There's actually an uninitialized variable bug here. If bulk_len==0, then 'err' never gets initialized before being checked. I'm surprised this doesn't cause a compiler warning, but it doesn't! 'err' needs to be initialized to 0.
crypto/hctr2.c line 219
Quoted 4 lines
crypto/hctr2.c new file +4
-0+ +if (remainder) { +memset (padding,0 ,BLOCKCIPHER_BLOCK_SIZE ); + padding[0 ] =0x01 ;
'padding' can be static const:
static const u8 padding[BLOCKCIPHER_BLOCK_SIZE] = { 0x1 };crypto/hctr2.c line 370
Quoted 4 lines
crypto/hctr2.c new file +4
-0+ subreq_size = max(sizeof_field(struct hctr2_request_ctx, u.hash_desc) + + crypto_shash_descsize(polyval), sizeof_field(struct + hctr2_request_ctx, u.xctr_req) + +crypto_skcipher_reqsize (xctr));
This is a little hard to read; it would be better if the sizeof_field()'s were aligned:
subreq_size = max(sizeof_field(struct hctr2_request_ctx, u.hash_desc) +
crypto_shash_descsize(polyval), sizeof_field(struct hctr2_request_ctx, u.xctr_req) + crypto_skcipher_reqsize(xctr));
Other than that, everything looks good. The only thing that really has to be fixed is the uninitialized variable. After that feel free to add:
- Eric
Re: [PATCH v5 8/8] fscrypt: Add HCTR2 support for filename encryption
In reply to Nathan HuckleberryQuoted 8 lines · Nathan Huckleberry
HCTR2 is a tweakable, length-preserving encryption mode that is intended for use on CPUs with dedicated crypto instructions. HCTR2 has the property that a bitflip in the plaintext changes the entire ciphertext. This property fixes a known weakness with filename encryption: when two filenames in the same directory share a prefix of >= 16 bytes, with AES-CTS-CBC their encrypted filenames share a common substring, leaking information. HCTR2 does not have this problem.
Quoted 5 lines
More information on HCTR2 can be found here: "Length-preserving encryption with HCTR2": https://eprint.iacr.org/2021/1441.pdf
Signed-off-by Nathan Huckleberry <[email protected]>Reviewed-by Ard Biesheuvel <[email protected]>
- Eric
Eric Biggers
maintainerRe: [PATCH v5 7/8] crypto: arm64/polyval: Add PMULL accelerated implementation of POLYVAL
In reply to Nathan HuckleberryQuoted 15 lines · Nathan Huckleberry
Add hardware accelerated version of POLYVAL for ARM64 CPUs with Crypto Extensions support.
This implementation is accelerated using PMULL instructions to perform the finite field computations. For added efficiency, 8 blocks of the message are processed simultaneously by precomputing the first 8 powers of the key.
Karatsuba multiplication is used instead of Schoolbook multiplication because it was found to be slightly faster on ARM64 CPUs. Montgomery reduction must be used instead of Barrett reduction due to the difference in modulus between POLYVAL's field and other finite fields.
Signed-off-by Nathan Huckleberry <[email protected]>Reviewed-by Ard Biesheuvel <[email protected]>
Quoted 9 lines
--- arch/arm64/crypto/Kconfig | 5 + arch/arm64/crypto/Makefile | 3 + arch/arm64/crypto/polyval-ce-core.S | 369 +++++++++++++++++++++++++ arch/arm64/crypto/polyval-ce-glue.c | 194 +++++++++++++ arch/x86/crypto/polyval-clmulni_glue.c | 2 +- 5 files changed, 572 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/crypto/polyval-ce-core.S create mode 100644 arch/arm64/crypto/polyval-ce-glue.c
This patch shouldn't be changing arch/x86/. It looks like that hunk was folded into the wrong patch.
arch/arm64/crypto/polyval-ce-core.S line 6
Quoted 12 lines
arch/arm64/crypto/polyval-ce-core.S new file +6
-0@@ -0,0 +1,369 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Implementation of POLYVAL using ARMv8 Crypto Extensions. + * + * Copyright 2021 Google LLC + */
This file is looking good, just a few readability suggestions:
arch/arm64/crypto/polyval-ce-core.S line 33
Quoted 4 lines
arch/arm64/crypto/polyval-ce-core.S new file +4
-0+BLOCKS_LEFT .req x2 +KEY_START .req x10 +EXTRA_BYTES .req x11 +TMP .req x13
It would help to also define and use aliases for the other arguments to pmull_polyval_update(), e.g.:
KEY_POWERS .req x0
MSG .req x1BLOCKS_LEFT .req x2
ACCUMULATOR .req x3
Note that the x86 implementation does this for the first three.
Quoted 14 lines
arch/arm64/crypto/polyval-ce-core.S new file +14
-0+/* + * Computes the 256-bit polynomial represented by LO, HI, MI. Stores + * the result in PL, PH. + * [PH : PL] = + * [HI_1 : HI_0 + HI_1 + MI_1 + LO_1 : LO_1 + LO_0 + MI_0 + HI_0 : LO_0] + */ +.macro karatsuba2 + // v4 = [HI_1 + MI_1 : HI_0 + MI_0] + eor v4.16b, HI.16b, MI.16b + // v4 = [HI_1 + MI_1 + LO_1 : HI_0 + MI_0 + LO_0] + eor v4.16b, v4.16b, LO.16b + // v5 = [HI_0 : LO_1] + ext v5.16b, LO.16b, HI.16b, #8 + // v4 = [HI_0 + HI_1 + MI_1 + LO_1 : LO_1 + LO_0 + MI_0 + HI_0]
arch/arm64/crypto/polyval-ce-core.S line 155
Quoted 10 lines
arch/arm64/crypto/polyval-ce-core.S new file +10
-0+ eor v4.16b, v4.16b, v5.16b + // HI = [HI_0 : HI_1] + ext HI.16b, HI.16b, HI.16b, #8 + // LO = [LO_0 : LO_1] + ext LO.16b, LO.16b, LO.16b, #8 + // PH = [HI_1 : HI_0 + HI_1 + MI_1 + LO_1] + ext PH.16b, v4.16b, HI.16b, #8 + // PL = [LO_1 + LO_0 + MI_0 + HI_0 : LO_0] + ext PL.16b, LO.16b, v4.16b, #8 +.endm
You know that I'm very picky about ordering :-) I got a little confused while reading this because "HI_0 + MI_0 + LO_0" changes to "LO_0 + MI_0 + HI_0" partway through. It would be better to use a consistent order the whole time, probably "HI_0 + MI_0 + LO_0" since that maps naturally onto what the first two lines do. Note, the comment above the macro will need to be updated too.
Quoted 28 lines
arch/arm64/crypto/polyval-ce-core.S new file +28
-0+/* + * Computes the 128-bit reduction of PL, PH. Stores the result in dest. + * + * This macro computes p(x) mod g(x) where p(x) is in montgomery form and g(x) = + * x^128 + x^127 + x^126 + x^121 + 1. + * + * We have a 256-bit polynomial P_H : P_L = P_3 : P_2 : P_1 : P_0 that is the + * product of two 128-bit polynomials in Montgomery form. We need to reduce it + * mod g(x). Also, since polynomials in Montgomery form have an "extra" factor + * of x^128, this product has two extra factors of x^128. To get it back into + * Montgomery form, we need to remove one of these factors by dividing by x^128. + * + * To accomplish both of these goals, we add multiples of g(x) that cancel out + * the low 128 bits P_1 : P_0, leaving just the high 128 bits. Since the low + * bits are zero, the polynomial division by x^128 can be done by right shifting. + * + * Since the only nonzero term in the low 64 bits of g(x) is the constant term, + * the multiple of g(x) needed to cancel out P_0 is P_0 * g(x). The CPU can + * only do 64x64 bit multiplications, so split P_0 * g(x) into x^128 * P_0 + + * x^64 * g*(x) * P_0 + P_0, where g*(x) is bits 64-127 of g(x). Adding this to + * the original polynomial gives P_3 : P_2 + P_0 + T_1 : P_1 + T_0 : 0, where T + * = T_1 : T_0 = g*(x) * P_0. Thus, bits 0-63 got "folded" into bits 64-191. + * + * Repeating this same process on the next 64 bits "folds" bits 64-127 into bits + * 128-255, giving the answer in bits 128-255. This time, we need to cancel P_1 + * + T_0 in bits 64-127. The multiple of g(x) required is (P_1 + T_0) * g(x) * + * x^64. Adding this to our previous computation gives P_3 + P_1 + T_0 + V_1 : + * P_2 + P_0 + T_1 + V_0 : 0 : 0, where V = V_1 : V_0 = g*(x) * (P_1 + T_0).
arch/arm64/crypto/polyval-ce-core.S line 194
Quoted 10 lines
arch/arm64/crypto/polyval-ce-core.S new file +10
-0+ * + * So our final computation is: + * T = T_1 : T_0 = g*(x) * P_0 + * V = V_1 : V_0 = g*(x) * (P_1 + T_0) + * p(x) / x^{128} mod g(x) = P_3 + P_1 + T_0 + V_1 : P_2 + P_0 + T_1 + V_0 + * + * The implementation below saves a XOR instruction by computing P_1 + T_0 : P_0 + * + T_1 and XORing it into dest, rather than separately XORing P_1 : P_0, T_0 : + * T1 into dest. This allows us to reuse P_1 + T_0 when computing V. + */
Can you get this comment in sync with the x86 version? They should be identical (there's nothing x86 or arm64 specific here), but there are some updates you made to the x86 version that didn't make it into here.
arch/arm64/crypto/polyval-ce-core.S line 198
Quoted 4 lines
arch/arm64/crypto/polyval-ce-core.S new file +4
-0+.macro montgomery_reduction dest + DEST .req \dest + // TMP_V = T_1 : T_0 = P_0 * g*(x) + pmull TMP_V.1q, GSTAR.1d, PL.1d
Put GSTAR as the last argument, to match the comment? Likewise for the other 3 multiplications with GSTAR in the file.
arch/arm64/crypto/polyval-ce-core.S line 207
Quoted 1 line
arch/arm64/crypto/polyval-ce-core.S new file +1
-0+ eor DEST.16b, TMP_V.16b, PH.16b
Similarly, swapping TMP_V and PH above (and in partial_reduce) would make the code match the comments naturally.
arch/arm64/crypto/polyval-ce-core.S line 269
Quoted 4 lines
arch/arm64/crypto/polyval-ce-core.S new file +4
-0+/* + * Handle any extra blocks before + * full_stride loop. + */
It's after full_stride loop, not before. Also this comment can fit on one line:
/* Handle any extra blocks after the full_stride loop. */
Quoted 29 lines
arch/arm64/crypto/polyval-ce-core.S new file +29
-0+.macro partial_stride + add x0, KEY_START, #(STRIDE_BLOCKS << 4) + sub x0, x0, BLOCKS_LEFT, lsl #4 + // Clobber key register + ld1 {KEY1.16b}, [x0], #16 + + ld1 {TMP_V.16b}, [x1], #16 + eor SUM.16b, SUM.16b, TMP_V.16b + karatsuba1_store KEY1 SUM + sub BLOCKS_LEFT, BLOCKS_LEFT, #1 + + tst BLOCKS_LEFT, #4 + beq .Lpartial4BlocksDone + ld1 {M0.16b, M1.16b, M2.16b, M3.16b}, [x1], #64 + // Clobber key registers + ld1 {KEY8.16b, KEY7.16b, KEY6.16b, KEY5.16b}, [x0], #64 + karatsuba1 M0 KEY8 + karatsuba1 M1 KEY7 + karatsuba1 M2 KEY6 + karatsuba1 M3 KEY5 +.Lpartial4BlocksDone: + tst BLOCKS_LEFT, #2 + beq .Lpartial2BlocksDone + ld1 {M0.16b, M1.16b}, [x1], #32 + // Clobber key registers + ld1 {KEY8.16b, KEY7.16b}, [x0], #32 + karatsuba1 M0 KEY8 + karatsuba1 M1 KEY7 +.Lpartial2BlocksDone:
arch/arm64/crypto/polyval-ce-core.S line 308
Quoted 10 lines
arch/arm64/crypto/polyval-ce-core.S new file +10
-0+ tst BLOCKS_LEFT, #1 + beq .LpartialDone + ld1 {M0.16b}, [x1], #16 + // Clobber key registers + ld1 {KEY8.16b}, [x0], #16 + karatsuba1 M0 KEY8 +.LpartialDone: + karatsuba2 + montgomery_reduction SUM +.endm
I don't understand the purpose of the "Clobber key registers" comments above. "Clobber" makes it sound like they are being reused for something other than the key powers, but they aren't.
Quoted 13 lines
arch/arm64/crypto/polyval-ce-glue.c new file +7
-0@@ -0,0 +1,194 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Accelerated POLYVAL implementation with ARMv8 Crypto Extensions +* instructions. This file contains glue code. +* +* Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <[email protected]> +* Copyright (c) 2009 Intel Corp.
arch/arm64/crypto/polyval-ce-glue.c line 17
Quoted 10 lines
arch/arm64/crypto/polyval-ce-glue.c new file +10
-0+* Author: Huang Ying <[email protected]> +* Copyright 2021 Google LLC +*/ +/* +* Glue code based on ghash-clmulni-intel_glue.c. +* +* This implementation of POLYVAL uses montgomery multiplication accelerated by +* ARMv8 Crypto Extensions instructions to implement the finite field operations. +* +*/
There's an extra newline in the second block comment. Also, given that there's a standalone block comment that describes the file, the description at the very top should be a one-line summary. So maybe something like:
// SPDX-License-Identifier: GPL-2.0-only
/*
* Glue code for POLYVAL using ARMv8 Crypto Extensions
*
* Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <[email protected]>
* Copyright (c) 2009 Intel Corp.
* Author: Huang Ying <[email protected]>
* Copyright 2021 Google LLC
*/
/*
* Glue code based on ghash-clmulni-intel_glue.c.
*
* This implementation of POLYVAL uses montgomery multiplication accelerated by
* ARMv8 Crypto Extensions instructions to implement the finite field operations. */
Quoted 4 lines
arch/arm64/crypto/polyval-ce-glue.c new file +4
-0+ +#include <crypto/algapi.h> +#include <crypto/gf128mul.h> +#include <crypto/internal/hash.h>
arch/arm64/crypto/polyval-ce-glue.c line 31
Quoted 10 lines
arch/arm64/crypto/polyval-ce-glue.c new file +10
-0+#include <crypto/internal/simd.h> +#include <crypto/polyval.h> +#include <linux/crypto.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/cpufeature.h> +#include <asm/neon.h> +#include <asm/simd.h> +#include <asm/unaligned.h>
<crypto/gf128mul.h> and <asm/unaligned.h> don't appear to be used.
arch/arm64/crypto/polyval-ce-glue.c line 82
Quoted 2 lines
arch/arm64/crypto/polyval-ce-glue.c new file +2
-0+memcpy (ctx->key_powers[NUM_KEY_POWERS -1 ], key, +POLYVAL_BLOCK_SIZE );
This fits on one line.
Quoted 17 lines
arch/arm64/crypto/polyval-ce-glue.c new file +17
-0+static int polyval_arm64_update (struct shash_desc *desc, +const u8 *src,unsigned int srclen) +{ +struct polyval_desc_ctx *dctx =shash_desc_ctx (desc); +struct polyval_tfm_ctx *ctx =crypto_shash_ctx (desc->tfm); +u8 *pos; +unsigned int nblocks; +unsigned int n; + +if (dctx->bytes) { + n =min (srclen, dctx->bytes); + pos = dctx->buffer +POLYVAL_BLOCK_SIZE - dctx->bytes; + + dctx->bytes -= n; + srclen -= n; + +while (n--)
arch/arm64/crypto/polyval-ce-glue.c line 127
Quoted 10 lines
arch/arm64/crypto/polyval-ce-glue.c new file +10
-0+ *pos++ ^= *src++; + +if (!dctx->bytes) +internal_polyval_mul (dctx->buffer, + ctx->key_powers[NUM_KEY_POWERS -1 ]); + } + + nblocks = srclen/POLYVAL_BLOCK_SIZE ; +internal_polyval_update (ctx, src, nblocks, dctx->buffer); + srclen -= nblocks*POLYVAL_BLOCK_SIZE ;
This is executing a kernel_neon_begin()/kernel_neon_end() section of unbounded length. To allow the task to be preempted occasionally, it needs to handle the input in chunks, e.g. 4K at a time, like the existing code for other algorithms does. Something like the following would work:
@@ -122,13 +118,16 @@ static int polyval_arm64_update(struct shash_desc *desc,
ctx->key_powers[NUM_KEY_POWERS-1]);
}
- nblocks = srclen/POLYVAL_BLOCK_SIZE;
- internal_polyval_update(ctx, src, nblocks, dctx->buffer);
- srclen -= nblocks*POLYVAL_BLOCK_SIZE;
+ while (srclen >= POLYVAL_BLOCK_SIZE) {
+ /* Allow rescheduling every 4K bytes. */
+ nblocks = min(srclen, 4096U) / POLYVAL_BLOCK_SIZE;
+ internal_polyval_update(ctx, src, nblocks, dctx->buffer);
+ srclen -= nblocks * POLYVAL_BLOCK_SIZE;
+ src += nblocks * POLYVAL_BLOCK_SIZE;
+ }
if (srclen) {
dctx->bytes = POLYVAL_BLOCK_SIZE - srclen;
- src += nblocks*POLYVAL_BLOCK_SIZE;
pos = dctx->buffer;
while (srclen--)
*pos++ ^= *src++;
arch/arm64/crypto/polyval-ce-glue.c line 61
Quoted 1 line
arch/arm64/crypto/polyval-ce-glue.c new file +1
-0+struct polyval_tfm_ctx *ctx =crypto_shash_ctx (desc->tfm);
struct polyval_tfm_ctx should be const everywhere except in ->setkey.
Quoted 5 lines
arch/arm64/crypto/polyval-ce-glue.c new file +5
-0+static int polyval_arm64_final (struct shash_desc *desc,u8 *dst) +{ +struct polyval_desc_ctx *dctx =shash_desc_ctx (desc); +struct polyval_tfm_ctx *ctx =crypto_shash_ctx (desc->tfm); +
arch/arm64/crypto/polyval-ce-glue.c line 154
Quoted 10 lines
arch/arm64/crypto/polyval-ce-glue.c new file +10
-0+if (dctx->bytes) { +internal_polyval_mul (dctx->buffer, + ctx->key_powers[NUM_KEY_POWERS -1 ]); + } + + dctx->bytes =0 ; +memcpy (dst, dctx->buffer,POLYVAL_BLOCK_SIZE ); + +return 0 ; +}
The line 'dctx->bytes = 0;' is unnecessary.
arch/arm64/crypto/polyval-ce-glue.c line 179
Quoted 6 lines
arch/arm64/crypto/polyval-ce-glue.c new file +6
-0+static int __init polyval_ce_mod_init (void ) +{ +if (!cpu_have_named_feature (PMULL )) +return -ENODEV ; +return crypto_register_shash (&polyval_alg); +}
The cpu_have_named_feature() check above is unnecessary since it is already included in what module_cpu_feature_match() expands into.
- Eric
Eric Biggers
maintainerRe: [PATCH v5 6/8] crypto: x86/polyval: Add PCLMULQDQ accelerated implementation of POLYVAL
In reply to Nathan HuckleberryQuoted 1 line · Nathan Huckleberry
arch/x86/crypto/polyval-clmulni_asm.S +0
-0
[...]
Quoted 3 lines
+ * The implementation below saves a XOR instruction by computing
P_1+T_0:P_0
+ * +T_1and XORing into dest, rather than separately XORingP_1:P_0andT_0:
+ * T1 into dest. This allows us to reuseP_1+T_0when computing V.
T1 => T_1
Quoted 9 lines
+/*
+ * Perform montgomery multiplication in GF(2^128) and store result in op1.
+ *
+ * Computes op1*op2*x^{-128} mod x^128 + x^127 + x^126 + x^121 + 1
+ * If op1, op2 are in montgomery form, this computes the montgomery
+ * form of op1*op2.
+ *
+ * voidclmul_polyval_mul(u8 *op1, const u8 *op2);
+ */
There's a tab in the middle of the text above.
Quoted 14 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +8
-0@@ -0,0 +1,200 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Accelerated POLYVAL implementation with Intel PCLMULQDQ-NI +* instructions. This file contains glue code. +* +* Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <[email protected]> +* Copyright (c) 2009 Intel Corp. +* Author: Huang Ying <[email protected]>
arch/x86/crypto/polyval-clmulni_glue.c line 18
Quoted 10 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +10
-0+* Copyright 2021 Google LLC +*/ +/* +* Glue code based on ghash-clmulni-intel_glue.c. +* +* This implementation of POLYVAL uses montgomery multiplication +* accelerated by PCLMULQDQ-NI to implement the finite field +* operations. +* +*/
Same comments as on the arm64 version regarding the file comments
arch/x86/crypto/polyval-clmulni_glue.c line 19
Quoted 1 line
arch/x86/crypto/polyval-clmulni_glue.c new file +1
-0+#include <crypto/gf128mul.h>
As in the arm64 version, <crypto/gf128mul.h> is unneeded.
arch/x86/crypto/polyval-clmulni_glue.c line 107
Quoted 5 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +5
-0+static int polyval_x86_update (struct shash_desc *desc, +const u8 *src,unsigned int srclen) +{ +struct polyval_desc_ctx *dctx =shash_desc_ctx (desc); +const struct polyval_tfm_ctx *ctx =crypto_shash_ctx (desc->tfm);
Maybe use 'tctx' for the pointer to the polyval_tfm_ctx instead of just ctx, to clearly distinguish it from dctx? Likewise everywhere else.
arch/x86/crypto/polyval-clmulni_glue.c line 133
Quoted 7 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +7
-0+ nblocks = srclen/POLYVAL_BLOCK_SIZE ; +internal_polyval_update (ctx, src, nblocks, dctx->buffer); + srclen -= nblocks*POLYVAL_BLOCK_SIZE ; + +if (srclen) { + dctx->bytes =POLYVAL_BLOCK_SIZE - srclen; + src += nblocks*POLYVAL_BLOCK_SIZE ;
As on the arm64 version, the length of SIMD regions need to be limited, with 4K bytes being a good number.
arch/x86/crypto/polyval-clmulni_glue.c line 152
Quoted 11 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +11
-0+static int polyval_x86_final (struct shash_desc *desc,u8 *dst) +{ +struct polyval_desc_ctx *dctx =shash_desc_ctx (desc); +const struct polyval_tfm_ctx *ctx =crypto_shash_ctx (desc->tfm); + +if (dctx->bytes) { +internal_polyval_mul (dctx->buffer, + ctx->key_powers[NUM_KEY_POWERS -1 ]); + } + + dctx->bytes =0 ;
As on the arm64 version, the line 'dctx->bytes = 0;' is unnecessary.
Quoted 11 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +11
-0+static const struct x86_cpu_id pcmul_cpu_id[] = { +X86_MATCH_FEATURE (X86_FEATURE_PCLMULQDQ ,NULL ),/* Pickle-Mickle-Duck */ + {} +}; +MODULE_DEVICE_TABLE (x86cpu, pcmul_cpu_id); + +static int __init polyval_clmulni_mod_init (void ) +{ +if (!x86_match_cpu (pcmul_cpu_id)) +return -ENODEV ; +
arch/x86/crypto/polyval-clmulni_glue.c line 195
Quoted 10 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +10
-0+return crypto_register_shash (&polyval_alg); +} + +static void __exit polyval_clmulni_mod_exit(void) +{ +crypto_unregister_shash (&polyval_alg); +} + +module_init (polyval_clmulni_mod_init); +module_exit (polyval_clmulni_mod_exit);
Doesn't this need to check for AVX support too?
Quoted 24 lines
crypto/polyval-generic.c +2
-3@@ -46,7 +46,6 @@#include <asm/unaligned.h> #include <crypto/algapi.h> -#include <crypto/gf128mul.h> #include <crypto/polyval.h> #include <crypto/internal/hash.h> #include <linux/crypto.h> @@ -66,8 +65,8 @@ struct polyval_desc_ctx {u32 bytes; }; -static void copy_and_reverse (u8 dst[POLYVAL_BLOCK_SIZE ], -const u8 src[POLYVAL_BLOCK_SIZE ]) +void copy_and_reverse (u8 dst[POLYVAL_BLOCK_SIZE ], +const u8 src[POLYVAL_BLOCK_SIZE ]) {u64 a =get_unaligned ((const u64 *)&src[0 ]);u64 b =get_unaligned ((const u64 *)&src[8 ]);
crypto/polyval-generic.c static void copy_and_reverse(u8 dst[POLYVAL_BLOCK_SIZE], · line 76
Quoted 2 lines
crypto/polyval-generic.c +0
-0@@ -76,6 +75,44 @@ static void copy_and_reverse(u8 dst[POLYVAL_BLOCK_SIZE],put_unaligned (swab64 (b), (u64 *)&dst[0 ]); }
copy_and_reverse() isn't used outside of this file, so it should be left static.
Quoted 27 lines
crypto/polyval-generic.c +27
-0+/* +* Performs multiplication in the POLYVAL field using the GHASH field as a +* subroutine. This function is used as a fallback for hardware accelerated +* implementations when simd registers are unavailable. +* +* Note: This function is not used for polyval-generic, instead we use the 4k +* lookup table implementation for finite field multiplication. +*/ +void polyval_mul_non4k (u8 *op1,const u8 *op2) +{ +be128 a, b; + +// Assume one argument is in Montgomery form and one is not. +copy_and_reverse ((u8 *)&a, op1); +copy_and_reverse ((u8 *)&b, op2); +gf128mul_x_lle (&a, &a); +gf128mul_lle (&a, &b); +copy_and_reverse (op1, (u8 *)&a); +} + +/* +* Perform a POLYVAL update using non4k multiplication. This function is used +* as a fallback for hardware accelerated implementations when simd registers +* are unavailable. +* +* Note: This function is not used for polyval-generic, instead we use the 4k +* lookup table implementation of finite field multiplication.
crypto/polyval-generic.c static void copy_and_reverse(u8 dst[POLYVAL_BLOCK_SIZE], · line 114
Quoted 10 lines
crypto/polyval-generic.c +10
-0+*/ +void polyval_update_non4k (const u8 *key,const u8 *in, +size_t nblocks,u8 *accumulator) +{ +while (nblocks--) { +crypto_xor (accumulator, in,POLYVAL_BLOCK_SIZE ); +polyval_mul_non4k (accumulator, key); + in +=POLYVAL_BLOCK_SIZE ; + } +}
The above two functions need EXPORT_SYMBOL_GPL, as they are potentially being called from a different module.
include/crypto/polyval.h line 11
Quoted 9 lines
include/crypto/polyval.h +1
-0@@ -8,10 +8,19 @@#ifndef _CRYPTO_POLYVAL_H#define _CRYPTO_POLYVAL_H +#include <crypto/gf128mul.h>
<crypto/gf128mul.h> doesn't appear to be needed here.
- Eric
Eric Biggers
maintainerRe: [PATCH v5 4/8] crypto: x86/aesni-xctr: Add accelerated implementation of XCTR
In reply to Nathan HuckleberryQuoted 3 lines · Nathan Huckleberry
Add hardware accelerated versions of XCTR for x86-64 CPUs with AESNI support. These implementations are modified versions of the CTR implementations found in
aesni-intel_asm.Sandaes_ctrby8_avx-x86_64.S.
Just one implementation now, using aes_ctrby8_avx-x86_64.S.
Quoted 4 lines
+/* Note: the "x" prefix in these aliases means "this is an xmm register". The
+ * alias prefixes have no relation to XCTR where the "X" prefix means "XOR
+ * counter".
+ */
Block comments look like:
/*
* text
*/
Quoted 19 lines
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +19
-0+ .if !\xctr + vpshufb xbyteswap, xcounter, xdata0 + .set i, 1 + .rept (by - 1) + club XDATA, i + vpaddq (ddq_add_1 + 16 * (i - 1))(%rip), xcounter, var_xdata + vptest ddq_low_msk(%rip), var_xdata + jnz 1f + vpaddq ddq_high_add_1(%rip), var_xdata, var_xdata + vpaddq ddq_high_add_1(%rip), xcounter, xcounter + 1: + vpshufb xbyteswap, var_xdata, var_xdata + .set i, (i +1) + .endr + .else + movq counter, xtmp + .set i, 0 + .rept (by) + club XDATA, i
arch/x86/crypto/aes_ctrby8_avx-x86_64.S ddq_add_8: · line 148
Quoted 10 lines
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +10
-0+ vpaddq (ddq_add_1 + 16 * i)(%rip), xtmp, var_xdata + .set i, (i +1) + .endr + .set i, 0 + .rept (by) + club XDATA, i + vpxor xiv, var_xdata, var_xdata + .set i, (i +1) + .endr + .endif
I'm not a fan of 'if !condition ... else ...', as the else clause is double-negated. It's more straightforward to do 'if condition ... else ...'.
arch/x86/crypto/aes_ctrby8_avx-x86_64.S ddq_add_8: · line 421
Quoted 9 lines
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +9
-0+ .if !\xctr + vmovdqa byteswap_const(%rip), xbyteswap + vmovdqu (p_iv), xcounter + vpshufb xbyteswap, xcounter, xcounter + .else + andq $(~0xf), num_bytes + shr $4, counter + vmovdqu (p_iv), xiv + .endif
Isn't the 'andq $(~0xf), num_bytes' instruction unnecessary? If it is necessary, I'd expect it to be necessary for CTR too.
Otherwise this file looks good.
Note, the macros in this file all expand to way too much code, especially due to the separate cases for AES-128, AES-192, and AES-256, and for each one every partial stride length 1..7. Of course, this is true for the existing CTR code too, so I don't think you have to fix this... But maybe think about addressing this later. Changing the handling of partial strides might be the easiest way to save a lot of code without hurting any micro-benchmarks too much. Also maybe some or all of the AES key sizes could be combined.
Quoted 16 lines
arch/x86/crypto/aesni-intel_glue.c +16
-0+#ifdef CONFIG_X86_64 +/* +* XCTR does not have a non-AVX implementation, so it must be enabled +* conditionally. +*/ +static struct skcipher_alg aesni_xctr = { + .base = { + .cra_name ="__xctr(aes)" , + .cra_driver_name ="__xctr-aes-aesni" , + .cra_priority =400 , + .cra_flags =CRYPTO_ALG_INTERNAL , + .cra_blocksize =1 , + .cra_ctxsize =CRYPTO_AES_CTX_SIZE , + .cra_module =THIS_MODULE , + }, + .min_keysize = AES_MIN_KEY_SIZE,
arch/x86/crypto/aesni-intel_glue.c static struct skcipher_alg aesni_skciphers[] = { · line 1145
Quoted 10 lines
arch/x86/crypto/aesni-intel_glue.c +10
-0+ .max_keysize = AES_MAX_KEY_SIZE, + .ivsize = AES_BLOCK_SIZE, + .chunksize = AES_BLOCK_SIZE, + .setkey = aesni_skcipher_setkey, + .encrypt = xctr_crypt, + .decrypt = xctr_crypt, +}; + +static struct simd_skcipher_alg *aesni_simd_xctr; +#endif
Comment the #endif above:
#endif /* CONFIG_X86_64 */
Quoted 7 lines
arch/x86/crypto/aesni-intel_glue.c +3
-0@@ -1180,8 +1274,19 @@ static int __init aesni_init(void)if (err) goto unregister_skciphers; +#ifdef CONFIG_X86_64 +if (boot_cpu_has (X86_FEATURE_AVX )) + err = simd_register_skciphers_compat(&aesni_xctr, 1,
arch/x86/crypto/aesni-intel_glue.c static int __init aesni_init(void) · line 1289
Quoted 10 lines
arch/x86/crypto/aesni-intel_glue.c +8
-0+ &aesni_simd_xctr); +if (err) + goto unregister_aeads; +#endif +return 0 ; +unregister_aeads: +simd_unregister_aeads (aesni_aeads,ARRAY_SIZE (aesni_aeads), + aesni_simd_aeads);
This will cause a compiler warning in 32-bit builds because the 'unregister_aeads' label won't be used.
- Eric
Eric Biggers
maintainerRe: [PATCH v5 5/8] crypto: arm64/aes-xctr: Add accelerated implementation of XCTR
In reply to Nathan HuckleberryQuoted 5 lines · Nathan Huckleberry
arch/arm64/crypto/aes-modes.S +0
-0
arch/arm64/crypto/aes-modes.S AES_FUNC_END(aes_cbc_cts_decrypt) · line 323
Quoted 9 lines
arch/arm64/crypto/aes-modes.S +1
-3@@ -318,126 +318,186 @@ AES_FUNC_END(aes_cbc_cts_decrypt) .byte 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff .previous - /* - * aes_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[], int rounds, - * int bytes, u8 ctr[]) + * This macro generates the code for CTR and XCTR mode. */
So I did ask for the register aliases and extra comments, but it's hard to review this with the cleanups to the existing code mixed in with the XCTR-specific additions. Would you mind splitting this up into two patches, one to improve the readability of the existing aes_ctr_encrypt(), and one to add xctr support?
Also, I noticed that the register aliases aren't being used consistently. E.g.
ld1 {vctr.16b}, [x5]... should use IV for x5, and
eor x6, x6, x12 eor x7, x7, x12 eor x8, x8, x12 eor x9, x9, x12 eor x10, x10, x12
Should use IV_PART for x12.
- Eric
Eric Biggers
maintainerRe: [PATCH v5 6/8] crypto: x86/polyval: Add PCLMULQDQ accelerated implementation of POLYVAL
In reply to Eric BiggersQuoted 8 lines · Eric Biggers
Quoted 8 lines
include/crypto/polyval.h +0
-0@@ -8,10 +8,19 @@#ifndef _CRYPTO_POLYVAL_H#define _CRYPTO_POLYVAL_H
Quoted 4 lines
include/crypto/polyval.h line 11
Quoted 1 line
include/crypto/polyval.h +1
-0+#include <crypto/gf128mul.h> <
crypto/gf128mul.h> doesn't appear to be needed here.
It looks like the issue is that this patch removes <crypto/gf128mul.h> from crypto/polyval-generic.c. If you just don't do that, then it won't be needed here.
- Eric
Eric Biggers
maintainerRe: [PATCH v5 7/8] crypto: arm64/polyval: Add PMULL accelerated implementation of POLYVAL
In reply to Eric BiggersQuoted 45 lines · Eric Biggers
Quoted 17 lines
arch/arm64/crypto/polyval-ce-glue.c new file +17
-0+static int polyval_arm64_update (struct shash_desc *desc, +const u8 *src,unsigned int srclen) +{ +struct polyval_desc_ctx *dctx =shash_desc_ctx (desc); +struct polyval_tfm_ctx *ctx =crypto_shash_ctx (desc->tfm); +u8 *pos; +unsigned int nblocks; +unsigned int n; + +if (dctx->bytes) { + n =min (srclen, dctx->bytes); + pos = dctx->buffer +POLYVAL_BLOCK_SIZE - dctx->bytes; + + dctx->bytes -= n; + srclen -= n; + +while (n--)arch/arm64/crypto/polyval-ce-glue.c line 127
Quoted 10 lines
arch/arm64/crypto/polyval-ce-glue.c new file +10
-0+ *pos++ ^= *src++; + +if (!dctx->bytes) +internal_polyval_mul (dctx->buffer, + ctx->key_powers[NUM_KEY_POWERS -1 ]); + } + + nblocks = srclen/POLYVAL_BLOCK_SIZE ; +internal_polyval_update (ctx, src, nblocks, dctx->buffer); + srclen -= nblocks*POLYVAL_BLOCK_SIZE ;This is executing a kernel_neon_begin()/kernel_neon_end() section of unbounded length. To allow the task to be preempted occasionally, it needs to handle the input in chunks, e.g. 4K at a time, like the existing code for other algorithms does. Something like the following would work:
(quoted diff) +4
-3@@ -122,13 +118,16 @@ static int polyval_arm64_update(struct shash_desc *desc, ctx->key_powers[NUM_KEY_POWERS-1]); } - nblocks = srclen/POLYVAL_BLOCK_SIZE; - internal_polyval_update(ctx, src, nblocks, dctx->buffer); - srclen -= nblocks*POLYVAL_BLOCK_SIZE; + while (srclen >= POLYVAL_BLOCK_SIZE) { + /* Allow rescheduling every 4K bytes. */ + nblocks = min(srclen, 4096U) / POLYVAL_BLOCK_SIZE; + internal_polyval_update(ctx, src, nblocks, dctx->buffer);
Review static int polyval_arm64_update(struct shash_desc *desc, · line 133
Quoted 10 lines
(quoted diff) +3
-1+ srclen -= nblocks * POLYVAL_BLOCK_SIZE; + src += nblocks * POLYVAL_BLOCK_SIZE; + } if (srclen) { dctx->bytes = POLYVAL_BLOCK_SIZE - srclen; - src += nblocks*POLYVAL_BLOCK_SIZE; pos = dctx->buffer; while (srclen--) *pos++ ^= *src++;
Also to be clear, this problem is specific to the "shash" API. You don't need to worry about it for "skcipher" algorithms such as xctr(*), as they have to walk a scatterlist to get their data, and that happens a page at a time.
- Eric
Eric Biggers
maintainerRe: [PATCH v5 3/8] crypto: hctr2 - Add HCTR2 support
In reply to Nathan HuckleberryQuoted 27 lines · Nathan Huckleberry
crypto/hctr2.c new file +27
-0+static int hctr2_create_common (struct crypto_template *tmpl, +struct rtattr **tb, +const char *xctr_name, +const char *polyval_name) +{ +u32 mask; +struct skcipher_instance *inst; +struct hctr2_instance_ctx *ictx; +struct skcipher_alg *xctr_alg; +struct crypto_alg *blockcipher_alg; +struct shash_alg *polyval_alg; +char blockcipher_name[CRYPTO_MAX_ALG_NAME ]; +int len; +int err; + + err =crypto_check_attr_type (tb,CRYPTO_ALG_TYPE_SKCIPHER , &mask); +if (err) +return err; + + inst =kzalloc (sizeof (*inst) +sizeof (*ictx),GFP_KERNEL ); +if (!inst) +return -ENOMEM ; + ictx =skcipher_instance_ctx (inst); + +/* Stream cipher, xctr(block_cipher) */ + err = crypto_grab_skcipher(&ictx->xctr_spawn, + skcipher_crypto_instance(inst),
crypto/hctr2.c line 440
Quoted 10 lines
crypto/hctr2.c new file +10
-0+ xctr_name, 0, mask); +if (err) + goto err_free_inst; + xctr_alg =crypto_spawn_skcipher_alg (&ictx->xctr_spawn); + + err = -EINVAL ; +if (strncmp (xctr_alg->base.cra_name,"xctr(" ,5 )) + goto err_free_inst; + len =strscpy (blockcipher_name, xctr_name +5 , +sizeof (blockcipher_name));
Found a bug here; 'xctr_name' in the strscpy() statement needs to be xctr_alg->base.cra_name. Otherwise something like 'hctr2_base(xctr-aes-aesni,polyval-clmulni)' doesn't work, as it will try to extract the block cipher name from the driver name "xctr-aes-aesni" instead of from the algorithm name "xctr(aes)" as intended.
- Eric