Skip to contentSkip to conversation

crypto: HCTR2 support

PATCH Superseded Nathan Huckleberry 4 participants 29 messages Apr 12, 2022 – Apr 21, 2022

Subsystems (6)
Documentation x86 Crypto API arm64 +2 more subsystems

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 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 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   |   19 +-
 arch/arm64/crypto/Kconfig               |   10 +-
 arch/arm64/crypto/Makefile              |    3 +
 arch/arm64/crypto/aes-glue.c            |   65 +-
 arch/arm64/crypto/aes-modes.S           |  134 ++
 arch/arm64/crypto/polyval-ce-core.S     |  366 ++++++
 arch/arm64/crypto/polyval-ce-glue.c     |  240 ++++
 arch/x86/crypto/Makefile                |    3 +
 arch/x86/crypto/aes_ctrby8_avx-x86_64.S |  233 ++--
 arch/x86/crypto/aesni-intel_asm.S       |   70 ++
 arch/x86/crypto/aesni-intel_glue.c      |   89 ++
 arch/x86/crypto/polyval-clmulni_asm.S   |  333 +++++
 arch/x86/crypto/polyval-clmulni_glue.c  |  234 ++++
 crypto/Kconfig                          |   40 +-
 crypto/Makefile                         |    3 +
 crypto/hctr2.c                          |  616 +++++++++
 crypto/polyval-generic.c                |  214 ++++
 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                      |    4 +
 include/crypto/polyval.h                |   17 +
 include/uapi/linux/fscrypt.h            |    3 +-
 tools/include/uapi/linux/fscrypt.h      |    3 +-
 27 files changed, 4376 insertions(+), 89 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.35.1.1178.g4f1659d476-goog
#1 lore raw

PATCH v4 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
index d6d7e84bb7f8..47752aaa16ff 100644
@@ -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
index d76bff8d0ffd..6b3fe3df1489 100644
@@ -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
index 2bacf8384f59..fd671d0e2012 100644
@@ -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
index 2d632a285869..fbb12d7d78af 100644
@@ -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.35.1.1178.g4f1659d476-goog
#3 lore raw

PATCH v4 2/8 crypto: polyval - Add POLYVAL support

In reply to Nathan Huckleberry

Add support for POLYVAL, an ε-Δ-universal hash function similar to GHASH. This patch adds POLYVAL to be used 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 | 214 +++++++++++++++++++++++++++++++++++++++
 crypto/tcrypt.c          |   4 +
 crypto/testmgr.c         |   6 ++
 crypto/testmgr.h         | 171 +++++++++++++++++++++++++++++++
 include/crypto/polyval.h |  17 ++++
 7 files changed, 421 insertions(+)
 create mode 100644 crypto/polyval-generic.c
 create mode 100644 include/crypto/polyval.h
crypto/Kconfig +8 -0
index 47752aaa16ff..00139845d76d 100644
@@ -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
index 6b3fe3df1489..561f901a91d4 100644
@@ -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 +214 -0

214 lines not on this page · raw message

crypto/tcrypt.c +4 -0
index fd671d0e2012..dd9cf216029b 100644
@@ -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
index fbb12d7d78af..d807b200edf6 100644
@@ -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
new file mode 100644
index 000000000000..b14c38aa9166
@@ -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.35.1.1178.g4f1659d476-goog
#8 lore raw

PATCH v4 4/8 crypto: x86/aesni-xctr: Add accelerated implementation of XCTR

In reply to 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.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://enterprint.iacr.org/2021/1441.pdf

---
 arch/x86/crypto/aes_ctrby8_avx-x86_64.S | 233 ++++++++++++++++--------
 arch/x86/crypto/aesni-intel_asm.S       |  70 +++++++
 arch/x86/crypto/aesni-intel_glue.c      |  89 +++++++++
 crypto/Kconfig                          |   2 +-
 4 files changed, 317 insertions(+), 77 deletions(-)
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +157 -76
index 43852ba6e19c..9e20d7d3d6da 100644
@@ -53,6 +53,10 @@
 #define KEY_192		2
 #define KEY_256		3
 
+// XCTR mode only
+#define counter		%r9
+#define xiv		%xmm8
+
 .section .rodata
 .align 16
 
@@ -102,38 +106,67 @@ 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
 
+	.if (\xctr == 1)
+		.set i, 0
+		.rept (by)
+			club XDATA, i
+			movq counter, var_xdata
+			.set i, (i +1)
+		.endr
+	.endif
+
 	.if (load_keys)
 		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 == 0)
+		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
+	.endif
+	.if (\xctr == 1)
+		.set i, 0
+		.rept (by)
+			club XDATA, i
+			vpaddq	(ddq_add_1 + 16 * i)(%rip), var_xdata, 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 == 0)
+		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:
+	.endif
+	.if (\xctr == 1)
+		add $by, counter
+	.endif
 
 	.set i, 1
 	.rept (by - 1)
@@ -371,94 +404,101 @@ 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 == 0)
+		vmovdqu	(p_iv), xcounter
+		vpshufb	xbyteswap, xcounter, xcounter
+	.endif
+	.if (\xctr == 1)
+		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 +511,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 == 0)
+		/* return updated IV */
+		vpshufb	xbyteswap, xcounter, xcounter
+		vmovdqu	xcounter, (p_iv)
+	.endif
 	RET
 .endm
 
@@ -494,7 +536,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 +549,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 +562,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 aes_ctx *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 aes_ctx *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 aes_ctx *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_asm.S +70 -0

76 lines not on this page · raw message

arch/x86/crypto/aesni-intel_glue.c +89 -0

119 lines not on this page · raw message

crypto/Kconfig +1 -1
index 0dedba74db4a..aa06af0e0ebe 100644
@@ -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.35.1.1178.g4f1659d476-goog
#5 lore raw

PATCH v4 3/8 crypto: hctr2 - Add HCTR2 support

In reply to Nathan Huckleberry

Add 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   | 616 +++++++++++++++++++++++++++++++++++++++++++
 crypto/tcrypt.c  |   5 +
 crypto/testmgr.c |   8 +
 crypto/testmgr.h | 672 +++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 1313 insertions(+)
 create mode 100644 crypto/hctr2.c
crypto/Kconfig +11 -0
index 00139845d76d..0dedba74db4a 100644
@@ -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
index 561f901a91d4..2dca9dbdede6 100644
@@ -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 +616 -0

616 lines not on this page · raw message

crypto/tcrypt.c +5 -0
index dd9cf216029b..336598da8eac 100644
@@ -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
index d807b200edf6..3244b7e5aa7e 100644
@@ -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.35.1.1178.g4f1659d476-goog
#14 lore raw

PATCH v4 5/8 crypto: arm64/aes-xctr: Add accelerated implementation of XCTR

In reply to Nathan Huckleberry

Add 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  |  65 ++++++++++++++++-
 arch/arm64/crypto/aes-modes.S | 134 ++++++++++++++++++++++++++++++++++
 3 files changed, 199 insertions(+), 4 deletions(-)
arch/arm64/crypto/Kconfig +2 -2
index 2a965aa0188d..897f9a4b5b67 100644
@@ -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 +63 -2

107 lines not on this page · raw message

arch/arm64/crypto/aes-modes.S +134 -0

140 lines not on this page · raw message

Signature · 1 line
2.35.1.1178.g4f1659d476-goog
#16 lore raw

PATCH v4 6/8 crypto: x86/polyval: Add PCLMULQDQ accelerated implementation of POLYVAL

In reply to Nathan Huckleberry

Add 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  | 333 +++++++++++++++++++++++++
 arch/x86/crypto/polyval-clmulni_glue.c | 234 +++++++++++++++++
 crypto/Kconfig                         |  10 +
 4 files changed, 580 insertions(+)
 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
index 2831685adf6f..b9847152acd8 100644
@@ -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 +333 -0

333 lines not on this page · raw message

arch/x86/crypto/polyval-clmulni_glue.c new file +234 -0

234 lines not on this page · raw message

crypto/Kconfig +10 -0
index aa06af0e0ebe..c6aec88213b1 100644
@@ -787,6 +787,16 @@ 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_CRYPTD
+	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
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
#19 lore raw

PATCH v4 7/8 crypto: arm64/polyval: Add PMULL accelerated implementation of POLYVAL

In reply to Nathan Huckleberry

Add hardware accelerated version of POLYVAL for ARM64 CPUs with Crypto Extension 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           |   6 +
 arch/arm64/crypto/Makefile          |   3 +
 arch/arm64/crypto/polyval-ce-core.S | 366 ++++++++++++++++++++++++++++
 arch/arm64/crypto/polyval-ce-glue.c | 240 ++++++++++++++++++
 4 files changed, 615 insertions(+)
 create mode 100644 arch/arm64/crypto/polyval-ce-core.S
 create mode 100644 arch/arm64/crypto/polyval-ce-glue.c
arch/arm64/crypto/Kconfig +6 -0
index 897f9a4b5b67..6457fd7ec7af 100644
@@ -60,6 +60,12 @@ 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_CRYPTD
+	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
index 09a805cc32d7..53f9af962b86 100644
@@ -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 +366 -0

366 lines not on this page · raw message

arch/arm64/crypto/polyval-ce-glue.c new file +240 -0

240 lines not on this page · raw message

Signature · 1 line
2.35.1.1178.g4f1659d476-goog
#22 lore raw

PATCH v4 8/8 fscrypt: Add HCTR2 support for filename encryption

In reply to Nathan Huckleberry

HCTR2 is a tweakable, length-preserving encryption mode. It has the same security guarantees as Adiantum, but is intended for use on CPUs with dedicated crypto instructions. It fixes a known weakness with filename encryption: when two filenames in the same directory share a prefix of >= 16 bytes, with 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 | 19 ++++++++++++++-----
 fs/crypto/fscrypt_private.h           |  2 +-
 fs/crypto/keysetup.c                  |  7 +++++++
 fs/crypto/policy.c                    |  4 ++++
 include/uapi/linux/fscrypt.h          |  3 ++-
 tools/include/uapi/linux/fscrypt.h    |  3 ++-
 6 files changed, 30 insertions(+), 8 deletions(-)
Documentation/filesystems/fscrypt.rst +14 -5
index 4d5d50dca65c..09915086abd8 100644
@@ -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
 
 If unsure, you should use the (AES-256-XTS, AES-256-CTS-CBC) pair.
 
@@ -357,6 +358,14 @@ 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.  It has the same
+security guarantees as Adiantum, but is intended for use on CPUs with dedicated
+crypto instructions. See the paper "Length-preserving encryption with HCTR2"
+(https://eprint.iacr.org/2021/1441.pdf) for more details. To use 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 +413,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
index 5b0a9e6478b5..d8617d01f7bd 100644
@@ -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_HCTR2
 
 struct fscrypt_context_v1 {
 	u8 version; /* FSCRYPT_CONTEXT_V1 */
fs/crypto/keysetup.c +7 -0
index eede186b04ce..ae24b581d3d7 100644
@@ -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 = "HCTR2",
+		.cipher_str = "hctr2(aes)",
+		.keysize = 32,
+		.security_strength = 32,
+		.ivsize = 32,
+	},
 };
 
 static DEFINE_MUTEX(fscrypt_mode_key_setup_mutex);
fs/crypto/policy.c +4 -0
index ed3d623724cd..fa8bdb8c76b7 100644
@@ -54,6 +54,10 @@ static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)
 	    filenames_mode == FSCRYPT_MODE_ADIANTUM)
 		return true;
 
+	if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
+	    filenames_mode == FSCRYPT_MODE_AES_256_HCTR2)
+		return true;
+
 	return false;
 }
 
include/uapi/linux/fscrypt.h +2 -1
index 9f4428be3e36..a756b29afcc2 100644
@@ -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.
tools/include/uapi/linux/fscrypt.h +2 -1
index 9f4428be3e36..a756b29afcc2 100644
@@ -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.35.1.1178.g4f1659d476-goog
maintainer
#6 lore raw

Re: [PATCH v4 3/8] crypto: hctr2 - Add HCTR2 support

In reply to Nathan Huckleberry

crypto/hctr2.c line 11

Quoted 3 lines · Nathan Huckleberry

+ /*

crypto/hctr2.c new file +2 -0
+  * HCTR2 is a length-preserving encryption mode that is efficient on             
+  * processors with instructions to accelerate aes and carryless                  

"aes" should be capitalized ("AES")

Quoted 1 line

* For more details, see the paper: Length-preserving encryption with HCTR2

Quote the title to distinguish it from the surrounding text:

For more details, see the paper "Length-preserving encryption with HCTR2"

Quoted 5 lines
crypto/hctr2.c new file +5 -0
+struct hctr2_tfm_ctx {
+	struct crypto_cipher *blockcipher;
+	struct crypto_skcipher *xctr;
+	struct crypto_shash *polyval;
+	u8 L[BLOCKCIPHER_BLOCK_SIZE];

crypto/hctr2.c line 57

Quoted 10 lines
crypto/hctr2.c new file +10 -0
+	int hashed_tweak_offset;
+	/*
+	 * This struct is allocated with extra space for two exported hash
+	 * digests.  Since the digest length is not known at compile-time, we
+	 * can't add them to the struct directly.
+	 *
+	 * hashed_tweaklen_even;
+	 * hashed_tweaklen_odd;
+	 */
+};

The digest length *is* known at compile time; it's POLYVAL_DIGEST_SIZE. This should say "hash state" instead of "hash digest", and "hash state size" instead of "digest length".

crypto/hctr2.c line 70

Quoted 5 lines
crypto/hctr2.c new file +5 -0
+      /* Sub-requests, must be last */
+      union {
+              struct shash_desc hash_desc;
+              struct skcipher_request xctr_req;
+      } u;

Clarify what "last" means above, since it is no longer really last.

crypto/hctr2.c line 77

Quoted 8 lines
crypto/hctr2.c new file +8 -0
+
+	/*
+	 * This struct is allocated with extra space for one exported hash
+	 * digest.  Since the digest length is not known at compile-time, we
+	 * can't add it to the struct directly.
+	 *
+	 * hashed_tweak;
+	 */

Similarly, this should say "hash state" and "hash state size", not "hash digest" and "digest length".

Quoted 4 lines

+/*
+ * HCTR2 requires hashing values based off the tweak length. Since the kernel
+ * implementation only supports 32-byte tweaks, we can precompute these when
+ * setting the key.

It's not clear what "hashing values based off the tweak length" means. Please write something clearer like:

"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."

Quoted 4 lines

+ * If the message length is a multiple of the blocksize, we use H(tweak_len * 2
+ * + 2). If the message length is not a multiple of the blocksize, we use
+ * H(tweak_len * 2 + 3).
+ */

This would basically be covered by what I suggested above. Repeating the exact formula isn't really needed, especially if it's going to be misleading. (As written, it omits the conversion from bytes to bits.)

Quoted 21 lines
crypto/hctr2.c new file +21 -0
+static int hctr2_hash_tweaklens(struct hctr2_tfm_ctx *tctx)
+{
+	SHASH_DESC_ON_STACK(shash, tfm->polyval);
+	__le64 tweak_length_block[2];
+	int err;
+
+	shash->tfm = tctx->polyval;
+	memset(tweak_length_block, 0, sizeof(tweak_length_block));
+
+	tweak_length_block[0] = cpu_to_le64(TWEAK_SIZE * 8 * 2 + 2);
+	err = crypto_shash_init(shash);
+	if (err)
+		return err;
+	err = crypto_shash_update(shash, (u8 *)tweak_length_block,
+				  POLYVAL_BLOCK_SIZE);
+	if (err)
+		return err;
+	err = crypto_shash_export(shash, hctr2_hashed_tweaklen(tctx, true));
+	if (err)
+		return err;
+

crypto/hctr2.c line 138

Quoted 10 lines
crypto/hctr2.c new file +10 -0
+	tweak_length_block[0] = cpu_to_le64(TWEAK_SIZE * 8 * 2 + 3);
+	err = crypto_shash_init(shash);
+	if (err)
+		return err;
+	err = crypto_shash_update(shash, (u8 *)tweak_length_block,
+				  POLYVAL_BLOCK_SIZE);
+	if (err)
+		return err;
+	return crypto_shash_export(shash, hctr2_hashed_tweaklen(tctx, false));
+}

The two hashed_tweaklens are backwards; odd means even, and even means odd :-( Can you write it the logical way? Even should mean evenly divisible into blocks, while odd should mean *not* evenly divisible into blocks. If you want to call it something else, like aligned and unaligned, that could also work, but the way you've written it is definitely backwards if we're going with even/odd.

Also, when possible please keep things concise and avoid duplicating code. This could be replaced with just the following:

static int hctr2_hash_tweaklen(struct hctr2_tfm_ctx *tctx, bool odd)

{
	SHASH_DESC_ON_STACK(shash, unused);
	__le64 block[2] = { cpu_to_le64(TWEAK_SIZE * 8 * 2 + 2 + odd), 0 };

	shash->tfm = tctx->polyval;

	return crypto_shash_init(shash) ?:
	       crypto_shash_update(shash, (u8 *)block, sizeof(block)) ?:
	       crypto_shash_export(shash, hctr2_hashed_tweaklen(tctx, odd));
}

/* (comment here) */
static int hctr2_hash_tweaklens(struct hctr2_tfm_ctx *tctx)

{
	return hctr2_hash_tweaklen(tctx, false) ?:
	       hctr2_hash_tweaklen(tctx, true);
}

crypto/hctr2.c line 382

Quoted 4 lines
crypto/hctr2.c new file +4 -0
+       subreq_size = max(sizeof_field(struct hctr2_request_ctx, u.hash_desc) +
+                         crypto_shash_statesize(polyval), sizeof_field(struct
+                         hctr2_request_ctx, u.xctr_req) +
+                         crypto_skcipher_reqsize(xctr));

This one needs to be crypto_shash_descsize(), not crypto_shash_statesize(). The descsize is the size of the context that follows the struct shash_desc, whereas the statesize is the size of the opaque byte array used by crypto_shash_export() and crypto_shash_import(). They don't necessarily have the same value.

Also, this would be easier to read if the two parts were indented the same:

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));

crypto/hctr2.c line 480

Quoted 13 lines
crypto/hctr2.c new file +13 -0
+        if (!strncmp(xctr_alg->base.cra_name, "xctr(", 5)) {                     
+                len = strscpy(blockcipher_name, xctr_name + 5,                   
+                            sizeof(blockcipher_name));                           
+                                                                                 
+                if (len < 1)                                                     
+                        return -EINVAL;                                          
+                                                                                 
+                if (blockcipher_name[len - 1] != ')')                            
+                        return -EINVAL;                                          
+                                                                                 
+                blockcipher_name[len - 1] = 0;                                   
+        } else                                                                   
+                return -EINVAL;                                                  

The "return -EINVAL" cases are leaking memory. How about writing this as:

	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));
	if (len < 1)
		goto err_free_inst;
	if (blockcipher_name[len - 1] != ')')
		goto err_free_inst;
	blockcipher_name[len - 1] = 0;

crypto/hctr2.c line 597

Quoted 13 lines

+ /* hctr2(blockcipher_name) */
+ /* hctr2_base(xctr_name, polyval_name) */
+ static struct crypto_template hctr2_tmpls[] = {

crypto/hctr2.c new file +10 -0
+         {                                                                        
+                 .name = "hctr2_base",                                            
+                 .create = hctr2_create_base,                                     
+                 .module = THIS_MODULE,                                           
+         }, {                                                                     
+                 .name = "hctr2",                                                 
+                 .create = hctr2_create,                                          
+                 .module = THIS_MODULE,                                           
+         }                                                                        
+ };                                                                               

Perhaps put the comments by the corresponding entries?

static struct crypto_template hctr2_tmpls[] = {
	{
		/* hctr2_base(xctr_name, polyval_name) */
		.name = "hctr2_base",
		.create = hctr2_create_base,
		.module = THIS_MODULE,
	}, {
		/* hctr2(blockcipher_name) */
		.name = "hctr2",
		.create = hctr2_create,
		.module = THIS_MODULE,
	}
};

- Eric

maintainer
#17 lore raw

Re: [PATCH v4 6/8] crypto: x86/polyval: Add PCLMULQDQ accelerated implementation of POLYVAL

In reply to Nathan Huckleberry
Quoted 1 line · Nathan Huckleberry
arch/x86/crypto/polyval-clmulni_asm.S +0 -0

[...]

Quoted 19 lines

+/*
+ * Computes the product of two 128-bit polynomials at the memory locations
+ * specified by (MSG + 16*i) and (KEY_POWERS + 16*i) and XORs the components of the
+ * 256-bit product into LO, MI, HI.
+ *
+ * The multiplication produces four parts:

+ *   LOW: The polynomial given by performing carryless multiplication of the
+ *   bottom 64-bits of each polynomial
+ *   MID1: The polynomial given by performing carryless multiplication of the
+ *   bottom 64-bits of the first polynomial and the top 64-bits of the second
+ *   MID2: The polynomial given by performing carryless multiplication of the
+ *   bottom 64-bits of the second polynomial and the top 64-bits of the first
+ *   HIGH: The polynomial given by performing carryless multiplication of the
+ *   top 64-bits of each polynomial
+ *
+ * We compute:
+ *  LO += LOW
+ *  MI += MID1 + MID2
+ *  HI += HIGH
Quoted 4 lines

+ *
+ * LO = [L0_1 : LO_0]
+ * MI = [MI_1 : MI_0]
+ * HI = [HI_1 : HI_0]

One of the "LO" has a 0 instead of an O.

Quoted 4 lines

+.macro schoolbook1_iteration i xor_sum

arch/x86/crypto/polyval-clmulni_asm.S +3 -0
+	movups (16*\i)(MSG), %xmm0
+	.if (\i == 0 && \xor_sum == 1)
+		pxor SUM, %xmm0

arch/x86/crypto/polyval-clmulni_asm.S line 109

Quoted 10 lines
arch/x86/crypto/polyval-clmulni_asm.S +10 -0
+	.endif
+	vpclmulqdq $0x00, (16*\i)(KEY_POWERS), %xmm0, %xmm2
+	vpclmulqdq $0x01, (16*\i)(KEY_POWERS), %xmm0, %xmm1
+	vpclmulqdq $0x11, (16*\i)(KEY_POWERS), %xmm0, %xmm3
+	vpclmulqdq $0x10, (16*\i)(KEY_POWERS), %xmm0, %xmm4
+	vpxor %xmm2, LO, LO
+	vpxor %xmm1, MI, MI
+	vpxor %xmm4, MI, MI
+	vpxor %xmm3, HI, HI
+.endm

Can you allocate the xmm1-xmm4 registers in a logical order? Either in order as they are first used, or in the same order as the vpclmulqdq constants. It doesn't actually matter, of course, but I don't understand the logic here. Did you also consider my suggestion
https://lore.kernel.org/r/YhA%2FtAaDi%[email protected] which would avoid adjacent instructions that depend on each other, like the XORs into MI that you have? In that version I had allocated the registers in the same order as the vpclmulqdq constants, so there was some logic behind it; it wasn't just random. (Whereas what you have here seems "random" to me!)

arch/x86/crypto/polyval-clmulni_asm.S line 121

Quoted 12 lines
arch/x86/crypto/polyval-clmulni_asm.S +12 -0
+
+/*
+ * Performs the same computation as schoolbook1_iteration, except we expect the
+ * arguments to already be loaded into xmm0 and xmm1.
+ */
+.macro schoolbook1_noload
+	vpclmulqdq $0x01, %xmm0, %xmm1, MI
+	vpclmulqdq $0x10, %xmm0, %xmm1, %xmm2
+	vpclmulqdq $0x00, %xmm0, %xmm1, LO
+	vpclmulqdq $0x11, %xmm0, %xmm1, HI
+	vpxor %xmm2, MI, MI
+.endm

The comment should be updated to to mention the second difference from schoolbook1_iteration:

"... and we set the result registers LO, MI, and HI directly rather than XOR'ing into them."

Quoted 3 lines

+ * So our final computation is:
+ * T = T_1 : T_0 = g*(x) * P_0
+ * V = V_1 : V_0 = g*(x) * (T_0 + P_1)

Above should use "P_1 + T_0" to keep using a consistent order.

Quoted 1 line

rather than directly XORing P_1 : P_0, T_0 : T1 into dest.

"rather than separately XORing P_1 : P_0 and T_0 : T_1 into dest"

Quoted 5 lines

+/*
+ * Compute schoolbook multiplication for 8 blocks
+ * m_0h^8 + ... + m_7h^1
+ *
+ * If reduce is set, also computes the montgomery reduction of the

Quoted 8 lines

+ * previous full_stride call and XORs with the first message block.
+ * (m_0 + REDUCE(PL, PH))h^8 + ... + m_7h^1.
+ * I.e., the first multiplication uses m_0 + REDUCE(PL, PH) instead of m_0.
+ *
+ * Sets PL, PH
+ * Clobbers LO, HI, MI
+ *
+ */

The "Sets PL, PH ... Clobbers LO, HI, MI" part is incomplete. PL and PH are sometimes used as inputs, not just set, and other registers are clobbered too.

This part of the comment should either be complete, or it should be removed.

arch/x86/crypto/polyval-clmulni_asm.S line 201

Quoted 7 lines

+.macro full_stride reduce

arch/x86/crypto/polyval-clmulni_asm.S +6 -0
+	pxor LO, LO
+	pxor HI, HI
+	pxor MI, MI
+
+	schoolbook1_iteration 7 0
+	.if (\reduce)

.if expressions don't need parentheses around them; this isn't C.

Quoted 5 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +0 -0
new file mode 100644
index 000000000000..4f62284f980c

[...]

arch/x86/crypto/polyval-clmulni_glue.c line 31

Quoted 12 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +12 -0
+#include <crypto/algapi.h>
+#include <crypto/cryptd.h>
+#include <crypto/gf128mul.h>
+#include <crypto/internal/hash.h>
+#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 <asm/cpu_device_id.h>
+#include <asm/simd.h>

Includes that are no longer required should be removed. At least <crypto/cryptd.h> is no longer required.

arch/x86/crypto/polyval-clmulni_glue.c line 58

Quoted 8 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +8 -0
+static void reverse_be128(be128 *x)
+{
+	__be64 a = x->a;
+	__be64 b = x->b;
+
+	x->a = swab64(b);
+	x->b = swab64(a);
+}

This is generating warnings from 'sparse':

arch/x86/crypto/polyval-clmulni_glue.c:56:16: warning: cast from restricted __be64 arch/x86/crypto/polyval-clmulni_glue.c:56:14: warning: incorrect type in assignment (different base types)
arch/x86/crypto/polyval-clmulni_glue.c:56:14: expected restricted __be64 [usertype] a
arch/x86/crypto/polyval-clmulni_glue.c:56:14: got unsigned long long [usertype]
arch/x86/crypto/polyval-clmulni_glue.c:57:16: warning: cast from restricted __be64 arch/x86/crypto/polyval-clmulni_glue.c:57:14: warning: incorrect type in assignment (different base types)
arch/x86/crypto/polyval-clmulni_glue.c:57:14: expected restricted __be64 [usertype] b
arch/x86/crypto/polyval-clmulni_glue.c:57:14: got unsigned long long [usertype]

Make sure to run 'make C=2' on changed files.

Quoted 15 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +15 -0
+
+static void generic_polyval_mul(u8 *op1, const u8 *op2)
+{
+	be128 a, b;
+
+	// Assume one argument is in Montgomery form and one is not.
+	memcpy(&a, op1, sizeof(a));
+	memcpy(&b, op2, sizeof(b));
+	reverse_be128(&a);
+	reverse_be128(&b);
+	gf128mul_x_lle(&a, &a);
+	gf128mul_lle(&a, &b);
+	reverse_be128(&a);
+	memcpy(op1, &a, sizeof(a));
+}

arch/x86/crypto/polyval-clmulni_glue.c line 83

Quoted 10 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +10 -0
+
+static void generic_polyval_update(const u8 *in, struct polyval_ctx *keys,
+			  size_t nblocks, u8 *accumulator)
+{
+	while (nblocks--) {
+		crypto_xor(accumulator, in, POLYVAL_BLOCK_SIZE);
+		generic_polyval_mul(accumulator, keys->key_powers[7]);
+		in += POLYVAL_BLOCK_SIZE;
+	}
+}

The above is hardcoding 7, whereas other places are using NUM_PRECOMPUTE_POWERS-1 for the same thing.

The naming of NUM_PRECOMPUTE_POWERS is also not great. It's missing a "D" at the end of "PRECOMPUTE", right? I think NUM_KEY_POWERS would make more sense.

generic_polyval_update() is also duplicated in both arch/x86/crypto/polyval-clmulni_glue.c and arch/arm64/crypto/polyval-ce-glue.c. How about putting it in crypto/polyval.c to share the code? It would need to be passed the key directly, rather than the implementation-specific polyval_ctx, but otherwise it would work, right?

arch/x86/crypto/polyval-clmulni_glue.c line 86

Quoted 3 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +3 -0
+
+static void internal_polyval_update(const u8 *in, struct polyval_ctx *keys,
+			  size_t nblocks, u8 *accumulator)

struct polyval_ctx should be 'const', everywhere except in ->setkey().

Quoted 18 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +18 -0
+static int polyval_init(struct shash_desc *desc)
+{
+	struct polyval_desc_ctx *dctx = shash_desc_ctx(desc);
+
+	memset(dctx, 0, sizeof(*dctx));
+
+	return 0;
+}
+
+static int polyval_setkey(struct crypto_shash *tfm,
+			const u8 *key, unsigned int keylen)
+{
+	struct polyval_ctx *ctx = crypto_shash_ctx(tfm);
+	int i;
+
+	if (keylen != POLYVAL_BLOCK_SIZE)
+		return -EINVAL;
+

arch/x86/crypto/polyval-clmulni_glue.c line 135

Quoted 10 lines
arch/x86/crypto/polyval-clmulni_glue.c new file +10 -0
+	memcpy(ctx->key_powers[NUM_PRECOMPUTE_POWERS-1], key,
+	       POLYVAL_BLOCK_SIZE);
+
+	for (i = NUM_PRECOMPUTE_POWERS-2; i >= 0; i--) {
+		memcpy(ctx->key_powers[i], key, POLYVAL_BLOCK_SIZE);
+		internal_polyval_mul(ctx->key_powers[i], ctx->key_powers[i+1]);
+	}
+
+	return 0;
+}

polyval_setkey() is the first step, so it would make sense to put its definition before polyval_init() so that everything more or less goes in order.

Also: the names of these functions, and polyval_update and polyval_final below, collide with the same-named functions in crypto/polyval-generic.c. This is sort of okay since these are all static, but it is bad practice as it can create confusing stack traces and require that the functions be renamed if/when things get refactored in the future. How about calling these polyval_x86_{setkey,init,update,final}()?

Similarly, polyval_arm64_{setkey,init,update,final} in the arm64 version.

Quoted 7 lines
crypto/Kconfig +0 -0
index aa06af0e0ebe..c6aec88213b1 100644
@@ -787,6 +787,16 @@ config CRYPTO_POLYVAL
 	  POLYVAL is the hash function used in HCTR2.  It is not a general-purpose
 	  cryptographic hash function.

crypto/Kconfig config CRYPTO_POLYVAL · line 798

Quoted 10 lines
crypto/Kconfig +9 -0
 
+config CRYPTO_POLYVAL_CLMUL_NI
+	tristate "POLYVAL hash function (CLMUL-NI accelerated)"
+	depends on X86 && 64BIT
+	select CRYPTO_CRYPTD
+	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.

Selecting CRYPTO_CRYPTD is no longer required.

- Eric

maintainer
#20 lore raw

Re: [PATCH v4 7/8] crypto: arm64/polyval: Add PMULL accelerated implementation of POLYVAL

In reply to Nathan Huckleberry

Many of the comments I made on the x86 version apply to this too, but a few unique comments below:

Quoted 2 lines · Nathan Huckleberry

Add hardware accelerated version of POLYVAL for ARM64 CPUs with Crypto Extension support.

As I've mentioned, it seems to be "Crypto Extensions", not "Crypto Extension". That's what everything else in the kernel says, at least. It's just a nit, but it's unclear whether you intentionally decided to not change this, or missed it.

Quoted 1 line
arch/arm64/crypto/polyval-ce-core.S +0 -0

[..]

arch/arm64/crypto/polyval-ce-core.S line 64

Quoted 5 lines
arch/arm64/crypto/polyval-ce-core.S +5 -0
+	.text
+	.align	4
+
+	.arch	armv8-a+crypto
+	.align	4

Having '.align' twice is redundant, right?

Quoted 16 lines
arch/arm64/crypto/polyval-ce-core.S +16 -0
+
+.Lgstar:
+	.quad	0xc200000000000000, 0xc200000000000000
+
+/*
+ * Computes the product of two 128-bit polynomials in X and Y and XORs the
+ * components of the 256-bit product into LO, MI, HI.
+ *
+ * X = [X_1 : X_0]
+ * Y = [Y_1 : Y_0]
+ *
+ * The multiplication produces four parts:
+ *   LOW: The polynomial given by performing carryless multiplication of X_0 and
+ *   Y_0
+ *   MID: The polynomial given by performing carryless multiplication of (X_0 +
+ *   X_1) and (Y_0 + Y_1)

arch/arm64/crypto/polyval-ce-core.S line 90

Quoted 10 lines
arch/arm64/crypto/polyval-ce-core.S +10 -0
+ *   HIGH: The polynomial given by performing carryless multiplication of X_1
+ *   and Y_1
+ *
+ * We compute:
+ *  LO += LOW
+ *  MI += MID
+ *  HI += HIGH
+ *
+ * Later, the 256-bit result can be extracted as:
+ *   [HI_1 : HI_0 + HI_1 + MI_1 + LO_1 :: LO_1 + HI_0 + MI_0 + LO_0 : LO_0]

What does the double colon mean?

Quoted 4 lines

+ * This step is done when computing the polynomial reduction for efficiency
+ * reasons.
+ */
+.macro karatsuba1 X Y

A super brief explanation of why Karatsuba multiplication is used instead of schoolbook multiplication would be helpful.

Quoted 5 lines
arch/arm64/crypto/polyval-ce-core.S +5 -0
+	X .req \X
+	Y .req \Y
+	ext	v25.16b, X.16b, X.16b, #8
+	ext	v26.16b, Y.16b, Y.16b, #8
+	eor	v25.16b, v25.16b, X.16b

arch/arm64/crypto/polyval-ce-core.S line 109

Quoted 10 lines
arch/arm64/crypto/polyval-ce-core.S +10 -0
+	eor	v26.16b, v26.16b, Y.16b
+	pmull	v27.1q, v25.1d, v26.1d
+	pmull2	v28.1q, X.2d, Y.2d
+	pmull	v29.1q, X.1d, Y.1d
+	eor	MI.16b, MI.16b, v27.16b
+	eor	HI.16b, HI.16b, v28.16b
+	eor	LO.16b, LO.16b, v29.16b
+	.unreq X
+	.unreq Y
+.endm

Maybe move the pmull into v27 and the eor into MI down a bit, since they have more dependencies? I.e.

	ext	v25.16b, X.16b, X.16b, #8
	ext	v26.16b, Y.16b, Y.16b, #8
	eor	v25.16b, v25.16b, X.16b
	eor	v26.16b, v26.16b, Y.16b
	pmull2	v28.1q, X.2d, Y.2d
	pmull	v29.1q, X.1d, Y.1d
	pmull	v27.1q, v25.1d, v26.1d
	eor	HI.16b, HI.16b, v28.16b
	eor	LO.16b, LO.16b, v29.16b
	eor	MI.16b, MI.16b, v27.16b

I don't know whether it will actually matter on arm64, but on lower-powered arm32 CPUs this sort of thing definitely can matter.

arch/arm64/crypto/polyval-ce-core.S line 127

Quoted 13 lines

+.macro karatsuba1_store X Y

arch/arm64/crypto/polyval-ce-core.S +12 -0
+	X .req \X
+	Y .req \Y
+	ext	v25.16b, X.16b, X.16b, #8
+	ext	v26.16b, Y.16b, Y.16b, #8
+	eor	v25.16b, v25.16b, X.16b
+	eor	v26.16b, v26.16b, Y.16b
+	pmull	MI.1q, v25.1d, v26.1d
+	pmull2	HI.1q, X.2d, Y.2d
+	pmull	LO.1q, X.1d, Y.1d
+	.unreq X
+	.unreq Y
+.endm

Likewise above.

Quoted 1 line
arch/arm64/crypto/polyval-ce-glue.c +0 -0

[...]

arch/arm64/crypto/polyval-ce-glue.c line 38

Quoted 3 lines
arch/arm64/crypto/polyval-ce-glue.c +3 -0
+struct polyval_async_ctx {
+	struct cryptd_ahash *cryptd_tfm;
+};

struct polyval_async_ctx is no longer used.

arch/arm64/crypto/polyval-ce-glue.c line 128

Quoted 10 lines
arch/arm64/crypto/polyval-ce-glue.c +10 -0
+static int polyval_setkey(struct crypto_shash *tfm,
+			const u8 *key, unsigned int keylen)
+{
+	struct polyval_ctx *ctx = crypto_shash_ctx(tfm);
+	int i;
+
+	if (keylen != POLYVAL_BLOCK_SIZE)
+		return -EINVAL;
+
+	BUILD_BUG_ON(sizeof(u128) != POLYVAL_BLOCK_SIZE);

Where does 'sizeof(u128)' come from? This file doesn't use u128 at all.

- Eric

maintainer
#23 lore raw

Re: [PATCH v4 8/8] fscrypt: Add HCTR2 support for filename encryption

In reply to Nathan Huckleberry
Quoted 10 lines · Nathan Huckleberry

HCTR2 is a tweakable, length-preserving encryption mode. It has the same security guarantees as Adiantum, but is intended for use on CPUs with dedicated crypto instructions. It fixes a known weakness with filename encryption: when two filenames in the same directory share a prefix of >= 16 bytes, with 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

Please quote titles to distinguish them from the surrounding text. E.g.

More information on HCTR2 can be found in the paper "Length-preserving encryption with HCTR2" (https://eprint.iacr.org/2021/1441.pdf)

Quoted 10 lines
---
 Documentation/filesystems/fscrypt.rst | 19 ++++++++++++++-----
 fs/crypto/fscrypt_private.h           |  2 +-
 fs/crypto/keysetup.c                  |  7 +++++++
 fs/crypto/policy.c                    |  4 ++++
 include/uapi/linux/fscrypt.h          |  3 ++-
 tools/include/uapi/linux/fscrypt.h    |  3 ++-
 6 files changed, 30 insertions(+), 8 deletions(-)

Can you make sure that all fscrypt patches are Cc'ed to the linux-fscrypt mailing list? In this case, just Cc the whole series to there.

Quoted 14 lines
Documentation/filesystems/fscrypt.rst +1 -0
index 4d5d50dca65c..09915086abd8 100644
@@ -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
 
 If unsure, you should use the (AES-256-XTS, AES-256-CTS-CBC) pair.
 

Documentation/filesystems/fscrypt.rst To use Adiantum, CONFIG_CRYPTO_ADIANTUM must be enabled. Also, fast · line 367

Quoted 10 lines
Documentation/filesystems/fscrypt.rst +7 -0
@@ -357,6 +358,14 @@ 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.  It has the same
+security guarantees as Adiantum, but is intended for use on CPUs with dedicated
+crypto instructions. See the paper "Length-preserving encryption with HCTR2"
+(https://eprint.iacr.org/2021/1441.pdf) for more details. To use 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.

"same security guarantees as Adiantum" is not really correct. Both Adiantum and HCTR2 are secure super-pseudorandom permutations if their underlying primitives are secure. So their security guarantees are pretty similar, but not literally the same. Can you reword this? This potentially-misleading claim also showed up in the commit message.

Quoted 5 lines
fs/crypto/policy.c +0 -0
index ed3d623724cd..fa8bdb8c76b7 100644

fs/crypto/policy.c static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode) · line 62

Quoted 9 lines
fs/crypto/policy.c +4 -0
@@ -54,6 +54,10 @@ static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)
 	    filenames_mode == FSCRYPT_MODE_ADIANTUM)
 		return true;
 
+	if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
+	    filenames_mode == FSCRYPT_MODE_AES_256_HCTR2)
+		return true;
+
 	return false;
 }

This is allowing HCTR2 for both v1 and v2 encryption policies. I don't think we should add any new features to v1 encryption policies, as they are deprecated. How about allowing HCTR2 for v2 encryption policies only? This is the first new encryption mode where this issue has come up, but this could be handled easily by splitting fscrypt_valid_enc_modes() into fscrypt_valid_enc_modes_v1() and fscrypt_valid_enc_modes_v2(). The v2 one can call the v1 one to share code.

tools/include/uapi/linux/fscrypt.h line 32

Quoted 12 lines
tools/include/uapi/linux/fscrypt.h +2 -1
index 9f4428be3e36..a756b29afcc2 100644
@@ -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 */
 

As far as I know, you don't actually need to update the copy of UAPI headers in tools/. The people who maintain those files handle that. It doesn't make sense to have copies of files in the source tree anyway.

- Eric

#24 lore raw

Re: [PATCH v4 8/8] fscrypt: Add HCTR2 support for filename encryption

Quoted 86 lines · Eric Biggers
Quoted 10 lines · Nathan Huckleberry

HCTR2 is a tweakable, length-preserving encryption mode. It has the same security guarantees as Adiantum, but is intended for use on CPUs with dedicated crypto instructions. It fixes a known weakness with filename encryption: when two filenames in the same directory share a prefix of >= 16 bytes, with 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

Please quote titles to distinguish them from the surrounding text. E.g.

More information on HCTR2 can be found in the paper "Length-preserving encryption with HCTR2" (https://eprint.iacr.org/2021/1441.pdf)

Quoted 10 lines
---
 Documentation/filesystems/fscrypt.rst | 19 ++++++++++++++-----
 fs/crypto/fscrypt_private.h           |  2 +-
 fs/crypto/keysetup.c                  |  7 +++++++
 fs/crypto/policy.c                    |  4 ++++
 include/uapi/linux/fscrypt.h          |  3 ++-
 tools/include/uapi/linux/fscrypt.h    |  3 ++-
 6 files changed, 30 insertions(+), 8 deletions(-)

Can you make sure that all fscrypt patches are Cc'ed to the linux-fscrypt mailing list? In this case, just Cc the whole series to there.

Quoted 14 lines
Documentation/filesystems/fscrypt.rst +1 -0
index 4d5d50dca65c..09915086abd8 100644
@@ -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
 
 If unsure, you should use the (AES-256-XTS, AES-256-CTS-CBC) pair.
 

Documentation/filesystems/fscrypt.rst To use Adiantum, CONFIG_CRYPTO_ADIANTUM must be enabled. Also, fast · line 367

Quoted 10 lines
Documentation/filesystems/fscrypt.rst +7 -0
@@ -357,6 +358,14 @@ 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.  It has the same
+security guarantees as Adiantum, but is intended for use on CPUs with dedicated
+crypto instructions. See the paper "Length-preserving encryption with HCTR2"
+(https://eprint.iacr.org/2021/1441.pdf) for more details. To use 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.

"same security guarantees as Adiantum" is not really correct. Both Adiantum and HCTR2 are secure super-pseudorandom permutations if their underlying primitives are secure. So their security guarantees are pretty similar, but not literally the same. Can you reword this? This potentially-misleading claim also showed up in the commit message.

Quoted 5 lines
fs/crypto/policy.c +0 -0
index ed3d623724cd..fa8bdb8c76b7 100644

fs/crypto/policy.c static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode) · line 62

Quoted 9 lines
fs/crypto/policy.c +4 -0
@@ -54,6 +54,10 @@ static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)
          filenames_mode == FSCRYPT_MODE_ADIANTUM)
              return true;
 
+     if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
+         filenames_mode == FSCRYPT_MODE_AES_256_HCTR2)
+             return true;
+
      return false;
 }

This is allowing HCTR2 for both v1 and v2 encryption policies. I don't think we should add any new features to v1 encryption policies, as they are deprecated. How about allowing HCTR2 for v2 encryption policies only? This is the first new encryption mode where this issue has come up, but this could be handled easily by splitting fscrypt_valid_enc_modes() into fscrypt_valid_enc_modes_v1() and fscrypt_valid_enc_modes_v2(). The v2 one can call the v1 one to share code.

Quoted 17 lines

tools/include/uapi/linux/fscrypt.h line 32

Quoted 12 lines
tools/include/uapi/linux/fscrypt.h +2 -1
index 9f4428be3e36..a756b29afcc2 100644
@@ -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 */
 

As far as I know, you don't actually need to update the copy of UAPI headers in tools/. The people who maintain those files handle that. It doesn't make sense to have copies of files in the source tree anyway.

Doesn't the x86 build emit a warning if these go out of sync?

maintainer
#9 lore raw

Re: [PATCH v4 4/8] crypto: x86/aesni-xctr: Add accelerated implementation of XCTR

In reply to Nathan Huckleberry

A few initial comments, I'll take a closer look at the .S file soon...

Quoted 7 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.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://enterprint.iacr.org/2021/1441.pdf

The above link doesn't work.

arch/x86/crypto/aesni-intel_asm.S SYM_FUNC_END(aesni_ctr_enc) · line 2828

Quoted 5 lines
arch/x86/crypto/aesni-intel_asm.S +5 -0
+#ifdef __x86_64__
+/*
+ * void aesni_xctr_enc(struct crypto_aes_ctx *ctx, const u8 *dst, u8 *src,
+ *		      size_t len, u8 *iv, int byte_ctr)
+ */

This prototype doesn't match the one declared in the .c file.

arch/x86/crypto/aesni-intel_glue.c asmlinkage void aes_ctr_enc_192_avx_by8(const u8 *in, u8 *iv, · line 151

Quoted 9 lines
arch/x86/crypto/aesni-intel_glue.c +9 -0
+
+asmlinkage void aes_xctr_enc_128_avx_by8(const u8 *in, u8 *iv, void *keys, u8
+	*out, unsigned int num_bytes, unsigned int byte_ctr);
+
+asmlinkage void aes_xctr_enc_192_avx_by8(const u8 *in, u8 *iv, void *keys, u8
+	*out, unsigned int num_bytes, unsigned int byte_ctr);
+
+asmlinkage void aes_xctr_enc_256_avx_by8(const u8 *in, u8 *iv, void *keys, u8
+	*out, unsigned int num_bytes, unsigned int byte_ctr);

Please don't have line breaks between parameter types and their names. These should look like:

asmlinkage void aes_xctr_enc_128_avx_by8(const u8 *in, u8 *iv, void *keys,

	u8 *out, unsigned int num_bytes, unsigned int byte_ctr);

Also, why aren't the keys const?

Quoted 4 lines
arch/x86/crypto/aesni-intel_glue.c +4 -0
+static void aesni_xctr_enc_avx_tfm(struct crypto_aes_ctx *ctx, u8 *out, const u8
+				   *in, unsigned int len, u8 *iv, unsigned int
+				   byte_ctr)
+{

arch/x86/crypto/aesni-intel_glue.c static int ctr_crypt(struct skcipher_request *req) · line 558

Quoted 10 lines
arch/x86/crypto/aesni-intel_glue.c +10 -0
+	if (ctx->key_length == AES_KEYSIZE_128)
+		aes_xctr_enc_128_avx_by8(in, iv, (void *)ctx, out, len,
+					 byte_ctr);
+	else if (ctx->key_length == AES_KEYSIZE_192)
+		aes_xctr_enc_192_avx_by8(in, iv, (void *)ctx, out, len,
+					 byte_ctr);
+	else
+		aes_xctr_enc_256_avx_by8(in, iv, (void *)ctx, out, len,
+					 byte_ctr);
+}

Same comments above.

Quoted 24 lines
arch/x86/crypto/aesni-intel_glue.c +24 -0
+static int xctr_crypt(struct skcipher_request *req)
+{
+	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
+	struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
+	u8 keystream[AES_BLOCK_SIZE];
+	u8 ctr[AES_BLOCK_SIZE];
+	struct skcipher_walk walk;
+	unsigned int nbytes;
+	unsigned int byte_ctr = 0;
+	int err;
+	__le32 ctr32;
+
+	err = skcipher_walk_virt(&walk, req, false);
+
+	while ((nbytes = walk.nbytes) > 0) {
+		kernel_fpu_begin();
+		if (nbytes & AES_BLOCK_MASK)
+			static_call(aesni_xctr_enc_tfm)(ctx, walk.dst.virt.addr,
+				walk.src.virt.addr, nbytes & AES_BLOCK_MASK,
+				walk.iv, byte_ctr);
+		nbytes &= ~AES_BLOCK_MASK;
+		byte_ctr += walk.nbytes - nbytes;
+
+		if (walk.nbytes == walk.total && nbytes > 0) {

arch/x86/crypto/aesni-intel_glue.c static int ctr_crypt(struct skcipher_request *req) · line 593

Quoted 10 lines
arch/x86/crypto/aesni-intel_glue.c +10 -0
+			ctr32 = cpu_to_le32(byte_ctr / AES_BLOCK_SIZE + 1);
+			memcpy(ctr, walk.iv, AES_BLOCK_SIZE);
+			crypto_xor(ctr, (u8 *)&ctr32, sizeof(ctr32));
+			aesni_enc(ctx, keystream, ctr);
+			crypto_xor_cpy(walk.dst.virt.addr + walk.nbytes -
+				       nbytes, walk.src.virt.addr + walk.nbytes
+				       - nbytes, keystream, nbytes);
+			byte_ctr += nbytes;
+			nbytes = 0;
+		}

For the final block case, it would be a bit simpler to do something like this:

	__le32 block[AES_BLOCK_SIZE / sizeof(__le32)]

	
	...
	memcpy(block, walk.iv, AES_BLOCK_SIZE);
	block[0] ^= cpu_to_le32(1 + byte_ctr / AES_BLOCK_SIZE);
	aesni_enc(ctx, (u8 *)block, (u8 *)block);

I.e., have one buffer, use a regular XOR instead of crypto_xor(), and encrypt it in-place.

arch/x86/crypto/aesni-intel_glue.c static int __init aesni_init(void) · line 1254

Quoted 7 lines
arch/x86/crypto/aesni-intel_glue.c +2 -0
@@ -1162,6 +1249,8 @@ static int __init aesni_init(void)
 		/* optimize performance of ctr mode encryption transform */
 		static_call_update(aesni_ctr_enc_tfm, aesni_ctr_enc_avx_tfm);
 		pr_info("AES CTR mode by8 optimization enabled\n");
+		static_call_update(aesni_xctr_enc_tfm, aesni_xctr_enc_avx_tfm);
+		pr_info("AES XCTR mode by8 optimization enabled\n");
 	}

Please don't add the log message above, as it would get printed at every boot-up on most x86 systems, and it's not important enough for that. The existing message "AES CTR mode ..." shouldn't really exist in the first place.

- Eric

maintainer
#25 lore raw

Re: [PATCH v4 8/8] fscrypt: Add HCTR2 support for filename encryption

In reply to Ard Biesheuvel
Quoted 18 lines · Ard Biesheuvel
Quoted 8 lines
Quoted 8 lines
tools/include/uapi/linux/fscrypt.h +0 -0
index 9f4428be3e36..a756b29afcc2 100644
@@ -27,7 +27,8 @@
 #define FSCRYPT_MODE_AES_128_CBC             5
 #define FSCRYPT_MODE_AES_128_CTS             6
 #define FSCRYPT_MODE_ADIANTUM                        9
Quoted 9 lines

tools/include/uapi/linux/fscrypt.h line 32

Quoted 4 lines
tools/include/uapi/linux/fscrypt.h +2 -1
-/* 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 */
 

As far as I know, you don't actually need to update the copy of UAPI headers in tools/. The people who maintain those files handle that. It doesn't make sense to have copies of files in the source tree anyway.

Quoted 1 line

Doesn't the x86 build emit a warning if these go out of sync?

The warning is emitted when building tools/perf/, not the kernel itself.

According to https://lore.kernel.org/r/[email protected], the perf maintainers actually prefer that their files are *not* updated for them. And I'd like to push back against having duplicate source files in the tree anyway, for obvious reasons. So I think we shouldn't update this file.

- Eric

#26 lore raw

Re: [PATCH v4 8/8] fscrypt: Add HCTR2 support for filename encryption

Quoted 20 lines · Eric Biggers
Quoted 18 lines · Ard Biesheuvel
Quoted 17 lines

tools/include/uapi/linux/fscrypt.h line 32

Quoted 12 lines
tools/include/uapi/linux/fscrypt.h +2 -1
index 9f4428be3e36..a756b29afcc2 100644
@@ -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 */
 

As far as I know, you don't actually need to update the copy of UAPI headers in tools/. The people who maintain those files handle that. It doesn't make sense to have copies of files in the source tree anyway.

Quoted 9 lines
Quoted 1 line

Doesn't the x86 build emit a warning if these go out of sync?

The warning is emitted when building tools/perf/, not the kernel itself.

According to https://lore.kernel.org/r/[email protected], the perf maintainers actually prefer that their files are *not* updated for them. And I'd like to push back against having duplicate source files in the tree anyway, for obvious reasons. So I think we shouldn't update this file.

Fair enough.

Reviewed-by on the v4 series
#28 lore raw
In reply to Nathan Huckleberry
Quoted 72 lines · Nathan Huckleberry

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 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

Quoted 11 lines

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

This is looking very good, thanks for your continued efforts on this.

Once Eric's feeback has been addressed, feel free to add

to the series.

Quoted 37 lines
 Documentation/filesystems/fscrypt.rst   |   19 +-
 arch/arm64/crypto/Kconfig               |   10 +-
 arch/arm64/crypto/Makefile              |    3 +
 arch/arm64/crypto/aes-glue.c            |   65 +-
 arch/arm64/crypto/aes-modes.S           |  134 ++
 arch/arm64/crypto/polyval-ce-core.S     |  366 ++++++
 arch/arm64/crypto/polyval-ce-glue.c     |  240 ++++
 arch/x86/crypto/Makefile                |    3 +
 arch/x86/crypto/aes_ctrby8_avx-x86_64.S |  233 ++--
 arch/x86/crypto/aesni-intel_asm.S       |   70 ++
 arch/x86/crypto/aesni-intel_glue.c      |   89 ++
 arch/x86/crypto/polyval-clmulni_asm.S   |  333 +++++
 arch/x86/crypto/polyval-clmulni_glue.c  |  234 ++++
 crypto/Kconfig                          |   40 +-
 crypto/Makefile                         |    3 +
 crypto/hctr2.c                          |  616 +++++++++
 crypto/polyval-generic.c                |  214 ++++
 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                      |    4 +
 include/crypto/polyval.h                |   17 +
 include/uapi/linux/fscrypt.h            |    3 +-
 tools/include/uapi/linux/fscrypt.h      |    3 +-
 27 files changed, 4376 insertions(+), 89 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
Quoted 3 lines

--
2.35.1.1178.g4f1659d476-goog

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#27 lore raw

Re: [PATCH v4 8/8] fscrypt: Add HCTR2 support for filename encryption

In reply to Nathan Huckleberry

fs/crypto/keysetup.c struct fscrypt_mode fscrypt_modes[] = { · line 57

Quoted 10 lines · Nathan Huckleberry
fs/crypto/keysetup.c +2 -0
index eede186b04ce..ae24b581d3d7 100644
@@ -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 = "HCTR2",

Can you use "AES-256-HCTR2" here instead of just "HCTR2"? This would be similar to how FSCRYPT_MODE_AES_256_XTS uses .friendly_name = "AES-256-XTS".

- Eric

maintainer
#2 lore raw

Re: [PATCH v4 1/8] crypto: xctr - Add XCTR support

In reply to Nathan Huckleberry

crypto/xctr.c line 32

Quoted 2 lines · Nathan Huckleberry
crypto/xctr.c new file +2 -0
+// Limited to 16-byte blocks for simplicity
+#define XCTR_BLOCKSIZE 16

Make it clear that this is talking about the current implementation, not XCTR itself. E.g.:

/* For now this implementation is limited to 16-byte blocks for simplicity. */ #define XCTR_BLOCKSIZE 16

crypto/xctr.c line 55

Quoted 7 lines
crypto/xctr.c new file +7 -0
+static int crypto_xctr_crypt_segment(struct skcipher_walk *walk,
+				    struct crypto_cipher *tfm, u32 byte_ctr)
+{
+	void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
+		   crypto_cipher_alg(tfm)->cia_encrypt;
+	u8 *src = walk->src.virt.addr;
+	u8 *dst = walk->dst.virt.addr;

'src' can be const.

- Eric

maintainer
#4 lore raw

Re: [PATCH v4 2/8] crypto: polyval - Add POLYVAL support

In reply to Nathan Huckleberry

A couple more nits:

Quoted 1 line · Nathan Huckleberry

+static int polyval_init(struct shash_desc *desc)

[...]

Quoted 1 line

+static int polyval_setkey(struct crypto_shash *tfm,

As I mentioned on the x86 patch, setkey() is the first step, before init(). People sometimes mix this up, e.g. see https://git.kernel.org/linus/f3aefb6a7066e24b.
Putting the definitions in their natural order might be helpful:

1. polyval_setkey()
2. polyval_init()
3. polyval_update()
4. polyval_final()

Review

Quoted 10 lines
(quoted diff) +10 -0
+static void reverse_block(u8 block[POLYVAL_BLOCK_SIZE])
+{
+	u64 *p1 = (u64 *)block;
+	u64 *p2 = (u64 *)&block[8];
+	u64 a = get_unaligned(p1);
+	u64 b = get_unaligned(p2);
+
+	put_unaligned(swab64(a), p2);
+	put_unaligned(swab64(b), p1);
+}

This is always paired with a memcpy() of the block, so consider making this helper function handle the copy too. E.g.

crypto/polyval-generic.c +9 -18
index 1399af125b937..b50db5dd51fd1 100644
@@ -75,15 +75,14 @@ static int polyval_init(struct shash_desc *desc)
 	return 0;
 }
 
-static void reverse_block(u8 block[POLYVAL_BLOCK_SIZE])
+static void copy_and_reverse(u8 dst[POLYVAL_BLOCK_SIZE],
+			     const u8 src[POLYVAL_BLOCK_SIZE])
 {
-	u64 *p1 = (u64 *)block;
-	u64 *p2 = (u64 *)&block[8];
-	u64 a = get_unaligned(p1);
-	u64 b = get_unaligned(p2);
+	u64 a = get_unaligned((const u64 *)&src[0]);
+	u64 b = get_unaligned((const u64 *)&src[8]);
 
-	put_unaligned(swab64(a), p2);
-	put_unaligned(swab64(b), p1);
+	put_unaligned(swab64(a), (u64 *)&dst[8]);
+	put_unaligned(swab64(b), (u64 *)&dst[0]);
 }
 
 static int polyval_setkey(struct crypto_shash *tfm,
@@ -98,10 +97,7 @@ static int polyval_setkey(struct crypto_shash *tfm,
 	gf128mul_free_4k(ctx->gf128);
 
 	BUILD_BUG_ON(sizeof(k) != POLYVAL_BLOCK_SIZE);
-	// avoid violating alignment rules
-	memcpy(&k, key, POLYVAL_BLOCK_SIZE);
-
-	reverse_block((u8 *)&k);
+	copy_and_reverse((u8 *)&k, key);
 	gf128mul_x_lle(&k, &k);
 
 	ctx->gf128 = gf128mul_init_4k_lle(&k);
@@ -137,8 +133,7 @@ static int polyval_update(struct shash_desc *desc,
 	}
 
 	while (srclen >= POLYVAL_BLOCK_SIZE) {
-		memcpy(tmp, src, POLYVAL_BLOCK_SIZE);
-		reverse_block(tmp);
+		copy_and_reverse(tmp, src);
 		crypto_xor(dctx->buffer, tmp, POLYVAL_BLOCK_SIZE);
 		gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
 		src += POLYVAL_BLOCK_SIZE;
@@ -162,11 +157,7 @@ static int polyval_final(struct shash_desc *desc, u8 *dst)
 
 	if (dctx->bytes)
 		gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
-	dctx->bytes = 0;
-
-	reverse_block(dctx->buffer);
-	memcpy(dst, dctx->buffer, POLYVAL_BLOCK_SIZE);
-
+	copy_and_reverse(dst, dctx->buffer);
 	return 0;
 }
maintainer
#7 lore raw

Re: [PATCH v4 3/8] crypto: hctr2 - Add HCTR2 support

In reply to Nathan Huckleberry

One more comment:

Quoted 9 lines · Nathan Huckleberry
crypto/hctr2.c new file +9 -0
+/*
+ * Check for a supported set of inner algorithms.
+ * See the comment at the beginning of this file.
+ */
+static bool hctr2_supported_algorithms(struct skcipher_alg *xctr_alg,
+				       struct crypto_alg *blockcipher_alg,
+				       struct shash_alg *polyval_alg)
+{
+	if (strncmp(xctr_alg->base.cra_name, "xctr(", 4) != 0)

crypto/hctr2.c line 434

Quoted 10 lines
crypto/hctr2.c new file +10 -0
+		return false;
+
+	if (blockcipher_alg->cra_blocksize != BLOCKCIPHER_BLOCK_SIZE)
+		return false;
+
+	if (strcmp(polyval_alg->base.cra_name, "polyval") != 0)
+		return false;
+
+	return true;
+}

There are a couple issues here:

- "See the comment at the beginning of this file" doesn't make sense. I guess this was copied from adiantum.c where there is indeed a comment at the beginning of the file that explains which "inner" algorithms are allowed. However, in hctr2.c there is no such comment (and that's fine; there aren't as many special considerations in this area for hctr2 as for adiantum).

- The strncmp() expression uses a string of 5 characters but only compares 4. Also this check is redundant anyway, since hctr2_create_common() already does this check (correctly, with 5 characters).

How about deleting the hctr2_supported_algorithms() function and putting the 2 needed checks directly in hctr2_create_common()? I.e., check blockcipher_alg->cra_blocksize right after the line:

	blockcipher_alg = crypto_spawn_cipher_alg(&ictx->blockcipher_spawn);

... and check polyval_alg->base.cra_name right after the line:

	polyval_alg = crypto_spawn_shash_alg(&ictx->polyval_spawn);

Note, the pr_warn() message "Unsupported HCTR2 instantiation" isn't very important, and it arguably shouldn't be there since it is user-triggerable. So you can just delete it.

- Eric

#21 lore raw

Re: [PATCH v4 7/8] crypto: arm64/polyval: Add PMULL accelerated implementation of POLYVAL

In reply to Nathan Huckleberry
Thank 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-rc3 next-20220414] [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/20220413-013255
base:   https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master
config: arm64-allyesconfig (https://download.01.org/0day-ci/archive/20220419/[email protected]/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project 429cbac0390654f90bba18a41799464adf31a5ec)

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/014203529f7aef5a93602c31f9fee3ba9843619c
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Nathan-Huckleberry/crypto-HCTR2-support/20220413-013255
git checkout 014203529f7aef5a93602c31f9fee3ba9843619c
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash arch/arm64/crypto/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> arch/arm64/crypto/polyval-ce-glue.c:229:33: warning: unused variable 'polyval_cpu_feature' [-Wunused-const-variable]
   static const struct cpu_feature polyval_cpu_feature[] = {
                                   ^

1 warning generated.

vim +/polyval_cpu_feature +229 arch/arm64/crypto/polyval-ce-glue.c

228

 > 229	static const struct cpu_feature polyval_cpu_feature[] = {

230 { cpu_feature(PMULL) }, { }
231 };
232 MODULE_DEVICE_TABLE(cpu, polyval_cpu_feature);
233

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
maintainer
#18 lore raw

Re: [PATCH v4 6/8] crypto: x86/polyval: Add PCLMULQDQ accelerated implementation of POLYVAL

In reply to Nathan Huckleberry

A few more comments:

Quoted 5 lines · Nathan Huckleberry

+/*
+ * Computes the 256-bit polynomial represented by LO, HI, MI. Stores
+ * the result in PL, PH.
+ * [PH :: PL] = [HI_1 : HI_0 + MI_1 :: LO_1 + MI_0 : LO_0]
+ */

It is unclear what the double colon means. Maybe you meant for it to indicate 128-bit boundaries, as opposed to 64-bit boundaries? It is not used consistently, though.

Quoted 2 lines

+/*
+ * Computes the 128-bit reduction of PL : PH. Stores the result in dest.

This should use the order "PH : PL", to be consistent with the notation elsewhere.

Quoted 1 line
arch/x86/crypto/polyval-clmulni_glue.c +0 -0

[...]

arch/x86/crypto/polyval-clmulni_glue.c line 45

Quoted 11 lines
arch/x86/crypto/polyval-clmulni_glue.c +11 -0
+struct polyval_ctx {
+	/*
+	 * These powers must be in the order h^8, ..., h^1.
+	 */
+	u8 key_powers[NUM_PRECOMPUTE_POWERS][POLYVAL_BLOCK_SIZE];
+};
+
+struct polyval_desc_ctx {
+	u8 buffer[POLYVAL_BLOCK_SIZE];
+	u32 bytes;
+};

As I've mentioned elsewhere, it is confusing to have both ctx and desc_ctx. The former should be called polyval_tfm_ctx, like it is in polyval-generic.c.

arch/x86/crypto/polyval-clmulni_glue.c line 48

Quoted 2 lines
arch/x86/crypto/polyval-clmulni_glue.c +2 -0
+asmlinkage void clmul_polyval_update(const u8 *in, struct polyval_ctx *keys,
+				     size_t nblocks, u8 *accumulator);

The argument order here is a bit weird. It would be more logical to have it be (keys, in, nblocks, accumulator), similar to crypto_shash_digest().

Also, 'keys' should be const.

- Eric

maintainer
#10 lore raw

Re: [PATCH v4 4/8] crypto: x86/aesni-xctr: Add accelerated implementation of XCTR

In reply to Nathan Huckleberry

arch/x86/crypto/aes_ctrby8_avx-x86_64.S line 59

Quoted 12 lines · Nathan Huckleberry
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +4 -0
index 43852ba6e19c..9e20d7d3d6da 100644
@@ -53,6 +53,10 @@
 #define KEY_192		2
 #define KEY_256		3
 
+// XCTR mode only
+#define counter		%r9
+#define xiv		%xmm8
+

It would be helpful if the registers were listed in order, and if the CTR-specific ones were marked as being specific to CTR. This would make it easy to verify that there are no collisions in register allocation. I.e.:

[...]
#define xdata7 %xmm7
#define xcounter %xmm8 // CTR mode only
#define xiv %xmm8 // XCTR mode only
#define xbyteswap %xmm9 // CTR mode only
#define xkey0 %xmm10
[...]
#define num_bytes %r8
#define counter %r9 // XCTR mode only
#define tmp %r10
[...]

I'm also not a fan of the naming, with "xcounter" being used by CTR only and "counter" being used by XCTR only... I see why you did it, though, as the existing code uses the "x" prefix to mean "this is an xmm register". It could at least use a comment that makes this super clear, though:

// Note: the "x" prefix in these aliases means "this is an xmm register". // No relation to XCTR where the "X" prefix means "XOR counter".
#define xdata0 %xmm0

arch/x86/crypto/aes_ctrby8_avx-x86_64.S line 60

Quoted 1 line
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +1 -0
+	.if (\xctr == 1)

As \xctr is either 0 or 1, this can be written as simply '.if \xctr'

arch/x86/crypto/aes_ctrby8_avx-x86_64.S ddq_add_8: · line 122

Quoted 8 lines
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +8 -0
+		.set i, 0
+		.rept (by)
+			club XDATA, i
+			movq counter, var_xdata
+			.set i, (i +1)
+		.endr
+	.endif
+

Since the 3-operand add instruction (vpaddq) is available here, and in fact is being used already, it isn't necessary to move 'counter' into all (up to 8) of the var_xdata registers. Just move it into the last var_xdata register, or into a temporary register, and use it as a source operand for all the additions.

Quoted 33 lines
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +19 -14
-	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 == 0)
+		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
+	.endif
+	.if (\xctr == 1)
+		.set i, 0
+		.rept (by)
+			club XDATA, i

arch/x86/crypto/aes_ctrby8_avx-x86_64.S ddq_add_8: · line 155

Quoted 10 lines
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +10 -0
+			vpaddq	(ddq_add_1 + 16 * i)(%rip), var_xdata, 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

This can be written as:

.if \xctr
[second part above]
.else
[first part above]
.endif

Quoted 5 lines
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +0 -5
-	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:

arch/x86/crypto/aes_ctrby8_avx-x86_64.S ddq_add_8: · line 169

Quoted 10 lines
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +10 -0
+	.if (\xctr == 0)
+		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:
+	.endif
+	.if (\xctr == 1)
+		add $by, counter
+	.endif

Likewise here.

Quoted 7 lines

+.macro do_aes_ctrmain key_len, xctr

arch/x86/crypto/aes_ctrby8_avx-x86_64.S +1 -2
 	cmp	$16, num_bytes
-	jb	.Ldo_return2\key_len
+	jb	.Ldo_return2\xctr\key_len
 
 	vmovdqa	byteswap_const(%rip), xbyteswap
-	vmovdqu	(p_iv), xcounter

arch/x86/crypto/aes_ctrby8_avx-x86_64.S ddq_add_8: · line 430

Quoted 10 lines
arch/x86/crypto/aes_ctrby8_avx-x86_64.S +9 -1
-	vpshufb	xbyteswap, xcounter, xcounter
+	.if (\xctr == 0)
+		vmovdqu	(p_iv), xcounter
+		vpshufb	xbyteswap, xcounter, xcounter
+	.endif
+	.if (\xctr == 1)
+		andq	$(~0xf), num_bytes
+		shr	$4, counter
+		vmovdqu	(p_iv), xiv
+	.endif

And likewise here. Also, the load of byteswap_const can be moved into the !\xctr block.

- Eric

maintainer
#11 lore raw

Re: [PATCH v4 4/8] crypto: x86/aesni-xctr: Add accelerated implementation of XCTR

In reply to Nathan Huckleberry
Quoted 67 lines · Nathan Huckleberry
arch/x86/crypto/aesni-intel_asm.S +59 -0
index 363699dd7220..ce17fe630150 100644
@@ -2821,6 +2821,76 @@ SYM_FUNC_END(aesni_ctr_enc)
 
 #endif
 
+#ifdef __x86_64__
+/*
+ * void aesni_xctr_enc(struct crypto_aes_ctx *ctx, const u8 *dst, u8 *src,
+ *		      size_t len, u8 *iv, int byte_ctr)
+ */
+SYM_FUNC_START(aesni_xctr_enc)
+	FRAME_BEGIN
+	cmp $16, LEN
+	jb .Lxctr_ret
+	shr	$4, %arg6
+	movq %arg6, CTR
+	mov 480(KEYP), KLEN
+	movups (IVP), IV
+	cmp $64, LEN
+	jb .Lxctr_enc_loop1
+.align 4
+.Lxctr_enc_loop4:
+	movaps IV, STATE1
+	vpaddq ONE(%rip), CTR, CTR
+	vpxor CTR, STATE1, STATE1
+	movups (INP), IN1
+	movaps IV, STATE2
+	vpaddq ONE(%rip), CTR, CTR
+	vpxor CTR, STATE2, STATE2
+	movups 0x10(INP), IN2
+	movaps IV, STATE3
+	vpaddq ONE(%rip), CTR, CTR
+	vpxor CTR, STATE3, STATE3
+	movups 0x20(INP), IN3
+	movaps IV, STATE4
+	vpaddq ONE(%rip), CTR, CTR
+	vpxor CTR, STATE4, STATE4
+	movups 0x30(INP), IN4
+	call _aesni_enc4
+	pxor IN1, STATE1
+	movups STATE1, (OUTP)
+	pxor IN2, STATE2
+	movups STATE2, 0x10(OUTP)
+	pxor IN3, STATE3
+	movups STATE3, 0x20(OUTP)
+	pxor IN4, STATE4
+	movups STATE4, 0x30(OUTP)
+	sub $64, LEN
+	add $64, INP
+	add $64, OUTP
+	cmp $64, LEN
+	jge .Lxctr_enc_loop4
+	cmp $16, LEN
+	jb .Lxctr_ret
+.align 4
+.Lxctr_enc_loop1:
+	movaps IV, STATE
+	vpaddq ONE(%rip), CTR, CTR
+	vpxor CTR, STATE1, STATE1
+	movups (INP), IN
+	call _aesni_enc1
+	pxor IN, STATE
+	movups STATE, (OUTP)
+	sub $16, LEN

arch/x86/crypto/aesni-intel_asm.S SYM_FUNC_END(aesni_ctr_enc) · line 2892

Quoted 10 lines
arch/x86/crypto/aesni-intel_asm.S +10 -0
+	add $16, INP
+	add $16, OUTP
+	cmp $16, LEN
+	jge .Lxctr_enc_loop1
+.Lxctr_ret:
+	FRAME_END
+	RET
+SYM_FUNC_END(aesni_xctr_enc)
+
+#endif

Sorry, I missed this file. This is the non-AVX version, right? That means that AVX instructions, i.e. basically anything instruction starting with "v", can't be used here. So the above isn't going to work. (There might be a way to test this with QEMU; maybe --cpu-type=Nehalem without --enable-kvm?)

You could rewrite this without using AVX instructions. However, polyval-clmulni is broken in the same way; it uses AVX instructions without checking whether they are available. But your patchset doesn't aim to provide a non-AVX polyval implementation at all. So even if you got the non-AVX XCTR working, it wouldn't be paired with an accelerated polyval.

So I think you should just not provide non-AVX versions for now. That would mean:

1.) Drop the change to aesni-intel_asm.S
2.) Don't register the AES XCTR algorithm unless AVX is available
(in addition to AES-NI)
3.) Don't register polyval-clmulni unless AVX is available
(in addition to CLMUL-NI)

- Eric

maintainer
#15 lore raw

Re: [PATCH v4 5/8] crypto: arm64/aes-xctr: Add accelerated implementation of XCTR

In reply to Nathan Huckleberry
Quoted 5 lines · Nathan Huckleberry
arch/arm64/crypto/aes-modes.S +0 -0
index dc35eb0245c5..ac37e2f7ca84 100644

arch/arm64/crypto/aes-modes.S ST5( mov v3.16b, v4.16b ) · line 487

Quoted 9 lines
arch/arm64/crypto/aes-modes.S +6 -0
@@ -479,6 +479,140 @@ ST5(	mov		v3.16b, v4.16b			)
 	b		.Lctrout
 AES_FUNC_END(aes_ctr_encrypt)
 
+	/*
+	 * aes_xctr_encrypt(u8 out[], u8 const in[], u8 const rk[], int rounds,
+	 *		   int bytes, u8 const ctr[], u8 finalbuf[], int
+	 *		   byte_ctr)
+	 */
+

What is the 'finalbuf' parameter for? It is never used.

Why is byte_ctr an 'int' here but an 'unsigned int' in the .c file?

It looks like 'ctr' is actually the IV; perhaps it should be called 'iv' to distinguish it from the byte_ctr?

As mentioned elsewhere, please don't have a line break between a parameter's type and name.

Generally, comments and register aliases would be super helpful throughout the code. As-is, this is much harder to read than the x86 version...

Also, this function is heavily duplicated with aes_ctr_encrypt. Did you consider generating both from a single macro, like you did with the x86 version?

arch/arm64/crypto/aes-modes.S ST5( mov v3.16b, v4.16b ) · line 495

Quoted 1 line
arch/arm64/crypto/aes-modes.S +1 -0
+	umov		x12, vctr.d[0]		/* keep ctr in reg */

/* keep first 8 bytes of IV in reg */

arch/arm64/crypto/aes-modes.S ST5( mov v3.16b, v4.16b ) · line 496

Quoted 1 line
arch/arm64/crypto/aes-modes.S +1 -0
+	lsr		x7, x7, #4

x7 needs to be w7, since it corresponds to a 32-bit parameter ('int byte_ctr'). The upper 32 bits of the register are not guaranteed to be zero.

Quoted 5 lines
arch/arm64/crypto/aes-modes.S +5 -0
+	sub		x7, x11, #MAX_STRIDE
+	eor		x7, x12, x7
+	ins		v0.d[0], x7
+	sub		x7, x11, #MAX_STRIDE - 1
+	sub		x8, x11, #MAX_STRIDE - 2

arch/arm64/crypto/aes-modes.S ST5( mov v3.16b, v4.16b ) · line 528

Quoted 10 lines
arch/arm64/crypto/aes-modes.S +10 -0
+	eor		x7, x7, x12
+	sub		x9, x11, #MAX_STRIDE - 3
+	mov		v1.d[0], x7
+	eor		x8, x8, x12
+	eor		x9, x9, x12
+ST5(	sub		x10, x11, #MAX_STRIDE - 4)
+	mov		v2.d[0], x8
+	eor		x10, x10, x12
+	mov		v3.d[0], x9
+ST5(	mov		v4.d[0], x10			)

There seem to be some unnecessarily tight instruction dependencies here. E.g., the first 3 instructions are all sequential. Are there not enough free registers to write it otherwise? I.e. do all the sub's first, then the eor's, then the mov's.

The trailing parenthesis after #MAX_STRIDE - 4 should be indented another level. As-is it looks like a typo.

Why does one place use 'ins' and the others use 'mov'?

- Eric

#12 lore raw

Re: [PATCH v4 4/8] crypto: x86/aesni-xctr: Add accelerated implementation of XCTR

In reply to Eric Biggers
Quoted 91 lines · Eric Biggers
Quoted 67 lines · Nathan Huckleberry
arch/x86/crypto/aesni-intel_asm.S +59 -0
index 363699dd7220..ce17fe630150 100644
@@ -2821,6 +2821,76 @@ SYM_FUNC_END(aesni_ctr_enc)
 
 #endif
 
+#ifdef __x86_64__
+/*
+ * void aesni_xctr_enc(struct crypto_aes_ctx *ctx, const u8 *dst, u8 *src,
+ *                 size_t len, u8 *iv, int byte_ctr)
+ */
+SYM_FUNC_START(aesni_xctr_enc)
+     FRAME_BEGIN
+     cmp $16, LEN
+     jb .Lxctr_ret
+     shr     $4, %arg6
+     movq %arg6, CTR
+     mov 480(KEYP), KLEN
+     movups (IVP), IV
+     cmp $64, LEN
+     jb .Lxctr_enc_loop1
+.align 4
+.Lxctr_enc_loop4:
+     movaps IV, STATE1
+     vpaddq ONE(%rip), CTR, CTR
+     vpxor CTR, STATE1, STATE1
+     movups (INP), IN1
+     movaps IV, STATE2
+     vpaddq ONE(%rip), CTR, CTR
+     vpxor CTR, STATE2, STATE2
+     movups 0x10(INP), IN2
+     movaps IV, STATE3
+     vpaddq ONE(%rip), CTR, CTR
+     vpxor CTR, STATE3, STATE3
+     movups 0x20(INP), IN3
+     movaps IV, STATE4
+     vpaddq ONE(%rip), CTR, CTR
+     vpxor CTR, STATE4, STATE4
+     movups 0x30(INP), IN4
+     call _aesni_enc4
+     pxor IN1, STATE1
+     movups STATE1, (OUTP)
+     pxor IN2, STATE2
+     movups STATE2, 0x10(OUTP)
+     pxor IN3, STATE3
+     movups STATE3, 0x20(OUTP)
+     pxor IN4, STATE4
+     movups STATE4, 0x30(OUTP)
+     sub $64, LEN
+     add $64, INP
+     add $64, OUTP
+     cmp $64, LEN
+     jge .Lxctr_enc_loop4
+     cmp $16, LEN
+     jb .Lxctr_ret
+.align 4
+.Lxctr_enc_loop1:
+     movaps IV, STATE
+     vpaddq ONE(%rip), CTR, CTR
+     vpxor CTR, STATE1, STATE1
+     movups (INP), IN
+     call _aesni_enc1
+     pxor IN, STATE
+     movups STATE, (OUTP)
+     sub $16, LEN

arch/x86/crypto/aesni-intel_asm.S SYM_FUNC_END(aesni_ctr_enc) · line 2892

Quoted 10 lines
arch/x86/crypto/aesni-intel_asm.S +10 -0
+     add $16, INP
+     add $16, OUTP
+     cmp $16, LEN
+     jge .Lxctr_enc_loop1
+.Lxctr_ret:
+     FRAME_END
+     RET
+SYM_FUNC_END(aesni_xctr_enc)
+
+#endif

Sorry, I missed this file. This is the non-AVX version, right? That means that AVX instructions, i.e. basically anything instruction starting with "v", can't be used here. So the above isn't going to work. (There might be a way to test this with QEMU; maybe --cpu-type=Nehalem without --enable-kvm?)

You could rewrite this without using AVX instructions. However, polyval-clmulni is broken in the same way; it uses AVX instructions without checking whether they are available. But your patchset doesn't aim to provide a non-AVX polyval implementation at all. So even if you got the non-AVX XCTR working, it wouldn't be paired with an accelerated polyval.

Quoted 6 lines

So I think you should just not provide non-AVX versions for now. That would mean:

1.) Drop the change to aesni-intel_asm.S
2.) Don't register the AES XCTR algorithm unless AVX is available
(in addition to AES-NI)

Is there a preferred way to conditionally register xctr? It looks like aesni-intel_glue.c registers a default implementation for all the algorithms in the array, then better versions are enabled depending on cpu features. Should I remove xctr from the list of other algorithms and register it separately?

Quoted 4 lines

3.) Don't register polyval-clmulni unless AVX is available
(in addition to CLMUL-NI)

- Eric

maintainer
#13 lore raw

Re: [PATCH v4 4/8] crypto: x86/aesni-xctr: Add accelerated implementation of XCTR

Quoted 99 lines · Nathan Huckleberry
Quoted 91 lines · Eric Biggers
Quoted 67 lines · Nathan Huckleberry
arch/x86/crypto/aesni-intel_asm.S +59 -0
index 363699dd7220..ce17fe630150 100644
@@ -2821,6 +2821,76 @@ SYM_FUNC_END(aesni_ctr_enc)
 
 #endif
 
+#ifdef __x86_64__
+/*
+ * void aesni_xctr_enc(struct crypto_aes_ctx *ctx, const u8 *dst, u8 *src,
+ *                 size_t len, u8 *iv, int byte_ctr)
+ */
+SYM_FUNC_START(aesni_xctr_enc)
+     FRAME_BEGIN
+     cmp $16, LEN
+     jb .Lxctr_ret
+     shr     $4, %arg6
+     movq %arg6, CTR
+     mov 480(KEYP), KLEN
+     movups (IVP), IV
+     cmp $64, LEN
+     jb .Lxctr_enc_loop1
+.align 4
+.Lxctr_enc_loop4:
+     movaps IV, STATE1
+     vpaddq ONE(%rip), CTR, CTR
+     vpxor CTR, STATE1, STATE1
+     movups (INP), IN1
+     movaps IV, STATE2
+     vpaddq ONE(%rip), CTR, CTR
+     vpxor CTR, STATE2, STATE2
+     movups 0x10(INP), IN2
+     movaps IV, STATE3
+     vpaddq ONE(%rip), CTR, CTR
+     vpxor CTR, STATE3, STATE3
+     movups 0x20(INP), IN3
+     movaps IV, STATE4
+     vpaddq ONE(%rip), CTR, CTR
+     vpxor CTR, STATE4, STATE4
+     movups 0x30(INP), IN4
+     call _aesni_enc4
+     pxor IN1, STATE1
+     movups STATE1, (OUTP)
+     pxor IN2, STATE2
+     movups STATE2, 0x10(OUTP)
+     pxor IN3, STATE3
+     movups STATE3, 0x20(OUTP)
+     pxor IN4, STATE4
+     movups STATE4, 0x30(OUTP)
+     sub $64, LEN
+     add $64, INP
+     add $64, OUTP
+     cmp $64, LEN
+     jge .Lxctr_enc_loop4
+     cmp $16, LEN
+     jb .Lxctr_ret
+.align 4
+.Lxctr_enc_loop1:
+     movaps IV, STATE
+     vpaddq ONE(%rip), CTR, CTR
+     vpxor CTR, STATE1, STATE1
+     movups (INP), IN
+     call _aesni_enc1
+     pxor IN, STATE
+     movups STATE, (OUTP)
+     sub $16, LEN

arch/x86/crypto/aesni-intel_asm.S SYM_FUNC_END(aesni_ctr_enc) · line 2892

Quoted 10 lines
arch/x86/crypto/aesni-intel_asm.S +10 -0
+     add $16, INP
+     add $16, OUTP
+     cmp $16, LEN
+     jge .Lxctr_enc_loop1
+.Lxctr_ret:
+     FRAME_END
+     RET
+SYM_FUNC_END(aesni_xctr_enc)
+
+#endif

Sorry, I missed this file. This is the non-AVX version, right? That means that AVX instructions, i.e. basically anything instruction starting with "v", can't be used here. So the above isn't going to work. (There might be a way to test this with QEMU; maybe --cpu-type=Nehalem without --enable-kvm?)

You could rewrite this without using AVX instructions. However, polyval-clmulni is broken in the same way; it uses AVX instructions without checking whether they are available. But your patchset doesn't aim to provide a non-AVX polyval implementation at all. So even if you got the non-AVX XCTR working, it wouldn't be paired with an accelerated polyval.

Quoted 6 lines

So I think you should just not provide non-AVX versions for now. That would mean:

1.) Drop the change to aesni-intel_asm.S
2.) Don't register the AES XCTR algorithm unless AVX is available
(in addition to AES-NI)

Quoted 6 lines

Is there a preferred way to conditionally register xctr? It looks like aesni-intel_glue.c registers a default implementation for all the algorithms in the array, then better versions are enabled depending on cpu features. Should I remove xctr from the list of other algorithms and register it separately?

Yes, it will need to be removed from the aesni_skciphers array. I don't see any other algorithms in that file that are conditional on AES-NI && AVX, so it will have to go by itself.

- Eric