crypto: HCTR2 support
Subsystems (6)
v3 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 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 | 11 +- 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 | 372 ++++++ arch/arm64/crypto/polyval-ce-glue.c | 363 ++++++ 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 | 376 ++++++ arch/x86/crypto/polyval-clmulni_glue.c | 361 ++++++ crypto/Kconfig | 40 +- crypto/Makefile | 3 + crypto/hctr2.c | 580 +++++++++ crypto/polyval-generic.c | 205 +++ crypto/tcrypt.c | 10 + crypto/testmgr.c | 20 + crypto/testmgr.h | 1536 +++++++++++++++++++++++ crypto/xctr.c | 193 +++ 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, 4633 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.723.g4982287a31-goog
PATCH v3 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 | 193 +++++++++++++ 6 files changed, 903 insertions(+) create mode 100644 crypto/xctr.c
crypto/Kconfig +9 -0
@@ -460,6 +460,15 @@ config CRYPTO_PCBC PCBC: Propagating Cipher Block Chaining mode This block cipher algorithm is required for RxRPC. +config CRYPTO_XCTR + tristate + select CRYPTO_SKCIPHER + select CRYPTO_MANAGER + help + XCTR: XOR Counter mode. This blockcipher mode is a variant of CTR mode + using XORs and little-endian addition rather than big-endian arithmetic. + XCTR mode is used to implement HCTR2. + config CRYPTO_XTS tristate "XTS support" select CRYPTO_SKCIPHER
crypto/Makefile +1 -0
@@ -93,6 +93,7 @@ obj-$(CONFIG_CRYPTO_CTS) += cts.o obj-$(CONFIG_CRYPTO_LRW) += lrw.o obj-$(CONFIG_CRYPTO_XTS) += xts.o obj-$(CONFIG_CRYPTO_CTR) += ctr.o +obj-$(CONFIG_CRYPTO_XCTR) += xctr.o obj-$(CONFIG_CRYPTO_KEYWRAP) += keywrap.o obj-$(CONFIG_CRYPTO_ADIANTUM) += adiantum.o obj-$(CONFIG_CRYPTO_NHPOLY1305) += nhpoly1305.o
crypto/tcrypt.c +1 -0
@@ -1556,6 +1556,7 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb) ret +=tcrypt_test ("rfc3686(ctr(aes))" ); ret +=tcrypt_test ("ofb(aes)" ); ret +=tcrypt_test ("cfb(aes)" ); + ret +=tcrypt_test ("xctr(aes)" );break ;case 11 :
crypto/testmgr.c +6 -0
@@ -5490,6 +5490,12 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .cipher = __VECS(xchacha20_tv_template) }, + }, { + .alg = "xctr(aes)", + .test = alg_test_skcipher, + .suite = { + .cipher = __VECS(aes_xctr_tv_template) + } }, { .alg = "xts(aes)", .generic_driver = "xts(ecb(aes-generic))",
crypto/testmgr.h +693 -0
697 lines not on this page · raw message
crypto/xctr.c new file +193 -0
193 lines not on this page · raw message
Signature · 1 line
2.35.1.723.g4982287a31-goog
Re: [PATCH v3 1/8] crypto: xctr - Add XCTR support
Quoted 11 lines · Nathan Huckleberry
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.pdfSigned-off-by Nathan Huckleberry <[email protected]>
Looks good, feel free to add:
A few minor nits below:
crypto/xctr.c line 38
Quoted 8 lines
crypto/xctr.c new file +8
-0+// Limited to 16-byte blocks for simplicity +#define XCTR_BLOCKSIZE 16 + +static void crypto_xctr_crypt_final (struct skcipher_walk *walk, +struct crypto_cipher *tfm,u32 byte_ctr) +{ +u8 keystream[XCTR_BLOCKSIZE ]; +u8 *src = walk->src.virt.addr;
Use 'const u8 *src'
crypto/xctr.c line 54
Quoted 6 lines
crypto/xctr.c new file +6
-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;
Likewise, 'const u8 *src'
crypto/xctr.c line 64
Quoted 10 lines
crypto/xctr.c new file +10
-0+u8 *dst = walk->dst.virt.addr; +unsigned int nbytes = walk->nbytes; +__le32 ctr32 =cpu_to_le32 (byte_ctr /XCTR_BLOCKSIZE +1 ); + + do { + /* create keystream */ + crypto_xor(walk->iv, (u8 *)&ctr32, sizeof(ctr32)); + fn(crypto_cipher_tfm(tfm), dst, walk->iv); + crypto_xor(dst, src, XCTR_BLOCKSIZE); + crypto_xor(walk->iv, (u8 *)&ctr32, sizeof(ctr32));
The comment "/* create keystream /*" is a bit misleading, since the part of the code that it describes isn't just creating the keystream, but also XOR'ing it with the data. It would be better to just remove that comment.
Review
Quoted 2 lines
(quoted diff) +2
-0+ + ctr32 = cpu_to_le32(le32_to_cpu(ctr32) + 1);
This could use le32_add_cpu().
Quoted 6 lines
crypto/xctr.c new file +6
-0+ + src +=XCTR_BLOCKSIZE ; + dst +=XCTR_BLOCKSIZE ; + }while ((nbytes -=XCTR_BLOCKSIZE ) >=XCTR_BLOCKSIZE ); + +return nbytes;
crypto/xctr.c line 82
Quoted 10 lines
crypto/xctr.c new file +10
-0+} + +static int crypto_xctr_crypt_inplace (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; +unsigned long alignmask =crypto_cipher_alignmask (tfm); +unsigned int nbytes = walk->nbytes; +u8 *src = walk->src.virt.addr;
Perhaps call this 'data' instead of 'src', since here it's both the source and destination?
crypto/xctr.c line 88
Quoted 6 lines
crypto/xctr.c new file +6
-0+u8 tmp[XCTR_BLOCKSIZE +MAX_CIPHER_ALIGNMASK ]; +u8 *keystream =PTR_ALIGN (tmp +0 , alignmask +1 ); +__le32 ctr32 =cpu_to_le32 (byte_ctr /XCTR_BLOCKSIZE +1 ); + + do { + /* create keystream */
Likewise, remove or clarify the '/* create keystream */' comment.
crypto/xctr.c line 94
Quoted 6 lines
crypto/xctr.c new file +6
-0+crypto_xor (walk->iv, (u8 *)&ctr32,sizeof (ctr32)); +fn (crypto_cipher_tfm (tfm), keystream, walk->iv); +crypto_xor (src, keystream,XCTR_BLOCKSIZE ); +crypto_xor (walk->iv, (u8 *)&ctr32,sizeof (ctr32)); + + ctr32 =cpu_to_le32 (le32_to_cpu (ctr32) +1 );
Likewise, le32_add_cpu().
- Eric
PATCH v3 2/8 crypto: polyval - Add POLYVAL support
In reply to Nathan HuckleberryAdd support for POLYVAL, an ε-Δ-universal hash function similar to GHASH. POLYVAL is used as a component to implement HCTR2 mode.
POLYVAL is implemented as an shash algorithm. The implementation is modified from ghash-generic.c.
More information on POLYVAL can be found in the HCTR2 paper:
https://eprint.iacr.org/2021/1441.pdf
--- crypto/Kconfig | 8 ++ crypto/Makefile | 1 + crypto/polyval-generic.c | 205 +++++++++++++++++++++++++++++++++++++++ crypto/tcrypt.c | 4 + crypto/testmgr.c | 6 ++ crypto/testmgr.h | 171 ++++++++++++++++++++++++++++++++ include/crypto/polyval.h | 17 ++++ 7 files changed, 412 insertions(+) create mode 100644 crypto/polyval-generic.c create mode 100644 include/crypto/polyval.h
crypto/Kconfig +8 -0
@@ -768,6 +768,14 @@ config CRYPTO_GHASH GHASH is the hash function used in GCM (Galois/Counter Mode). It is not a general-purpose cryptographic hash function. +config CRYPTO_POLYVAL + tristate + select CRYPTO_GF128MUL + select CRYPTO_HASH + help + POLYVAL is the hash function used in HCTR2. It is not a general-purpose + cryptographic hash function. + config CRYPTO_POLY1305 tristate "Poly1305 authenticator algorithm" select CRYPTO_HASH
crypto/Makefile +1 -0
@@ -169,6 +169,7 @@ UBSAN_SANITIZE_jitterentropy.o = n jitterentropy_rng-y := jitterentropy.o jitterentropy-kcapi.o obj-$(CONFIG_CRYPTO_TEST) += tcrypt.o obj-$(CONFIG_CRYPTO_GHASH) += ghash-generic.o +obj-$(CONFIG_CRYPTO_POLYVAL) += polyval-generic.o obj-$(CONFIG_CRYPTO_USER_API) += af_alg.o obj-$(CONFIG_CRYPTO_USER_API_HASH) += algif_hash.o obj-$(CONFIG_CRYPTO_USER_API_SKCIPHER) += algif_skcipher.o
crypto/polyval-generic.c new file +205 -0
205 lines not on this page · raw message
crypto/tcrypt.c +4 -0
@@ -1730,6 +1730,10 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb) ret +=tcrypt_test ("ccm(sm4)" );break ; +case 57 : + ret +=tcrypt_test ("polyval" ); +break ; +case 100 : ret +=tcrypt_test ("hmac(md5)" );break ;
crypto/testmgr.c +6 -0
@@ -5284,6 +5284,12 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .hash = __VECS(poly1305_tv_template) } + }, { + .alg = "polyval", + .test = alg_test_hash, + .suite = { + .hash = __VECS(polyval_tv_template) + } }, { .alg = "rfc3686(ctr(aes))", .test = alg_test_skcipher,
crypto/testmgr.h +171 -0
175 lines not on this page · raw message
include/crypto/polyval.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Common values for the Polyval hash algorithm +* +* Copyright 2021 Google LLC +*/ + +#ifndef _CRYPTO_POLYVAL_H +#define _CRYPTO_POLYVAL_H + +#include <linux/types.h> +#include <linux/crypto.h> + +#define POLYVAL_BLOCK_SIZE 16 +#define POLYVAL_DIGEST_SIZE 16 + +#endif
Signature · 1 line
2.35.1.723.g4982287a31-goog
Re: [PATCH v3 2/8] crypto: polyval - Add POLYVAL support
Quoted 10 lines · Nathan Huckleberry
Add support for POLYVAL, an ε-Δ-universal hash function similar to GHASH. POLYVAL is used as a component to implement HCTR2 mode.
POLYVAL is implemented as an shash algorithm. The implementation is modified from
ghash-generic.c.More information on POLYVAL can be found in the HCTR2 paper:
https://eprint.iacr.org/2021/1441.pdfSigned-off-by Nathan Huckleberry <[email protected]>
Generally looks good, feel free to add:
But, I think you should mention that POLYVAL is originally from AES-GCM-SIV (RFC 8452). It's true that the kernel doesn't implement AES-GCM-SIV currently, but it's still important to mention. Both the commit message and comment in crypto/polyval-generic.c should mention this, IMO. As-is, the only hint of this in this patch is the comment above the test vectors.
Your explanation about how POLYVAL can be implemented on top of GHASH is also a bit incomplete. Linking to
https://datatracker.ietf.org/doc/html/rfc8452#appendix-A would be helpful.
- Eric
PATCH v3 3/8 crypto: hctr2 - Add HCTR2 support
In reply to Nathan HuckleberryAdd support for HCTR2 as a template. HCTR2 is a length-preserving encryption mode that is efficient on processors with instructions to accelerate AES and carryless multiplication, e.g. x86 processors with AES-NI and CLMUL, and ARM processors with the ARMv8 Crypto Extensions.
As a length-preserving encryption mode, HCTR2 is suitable for applications such as storage encryption where ciphertext expansion is not possible, and thus authenticated encryption cannot be used. Currently, such applications usually use XTS, or in some cases Adiantum. XTS has the disadvantage that it is a narrow-block mode: a bitflip will only change 16 bytes in the resulting ciphertext or plaintext. This reveals more information to an attacker than necessary.
HCTR2 is a wide-block mode, so it provides a stronger security property:
a bitflip will change the entire message. HCTR2 is somewhat similar to Adiantum, which is also a wide-block mode. However, HCTR2 is designed to take advantage of existing crypto instructions, while Adiantum targets devices without such hardware support. Adiantum is also designed with longer messages in mind, while HCTR2 is designed to be efficient even on short messages.
HCTR2 requires POLYVAL and XCTR as components. More information on HCTR2 can be found here: Length-preserving encryption with HCTR2:
https://eprint.iacr.org/2021/1441.pdf
--- crypto/Kconfig | 11 + crypto/Makefile | 1 + crypto/hctr2.c | 580 ++++++++++++++++++++++++++++++++++++++++ crypto/tcrypt.c | 5 + crypto/testmgr.c | 8 + crypto/testmgr.h | 672 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 1277 insertions(+) create mode 100644 crypto/hctr2.c
crypto/Kconfig +11 -0
@@ -532,6 +532,17 @@ config CRYPTO_ADIANTUM If unsure, say N. +config CRYPTO_HCTR2 + tristate "HCTR2 support" + select CRYPTO_XCTR + select CRYPTO_POLYVAL + select CRYPTO_MANAGER + help + HCTR2 is a length-preserving encryption mode for storage encryption that + is efficient on processors with instructions to accelerate AES and + carryless multiplication, e.g. x86 processors with AES-NI and CLMUL, and + ARM processors with the ARMv8 crypto extensions. + config CRYPTO_ESSIV tristate "ESSIV support for block encryption" select CRYPTO_AUTHENC
crypto/Makefile +1 -0
@@ -94,6 +94,7 @@ obj-$(CONFIG_CRYPTO_LRW) += lrw.o obj-$(CONFIG_CRYPTO_XTS) += xts.o obj-$(CONFIG_CRYPTO_CTR) += ctr.o obj-$(CONFIG_CRYPTO_XCTR) += xctr.o +obj-$(CONFIG_CRYPTO_HCTR2) += hctr2.o obj-$(CONFIG_CRYPTO_KEYWRAP) += keywrap.o obj-$(CONFIG_CRYPTO_ADIANTUM) += adiantum.o obj-$(CONFIG_CRYPTO_NHPOLY1305) += nhpoly1305.o
crypto/hctr2.c new file +580 -0
580 lines not on this page · raw message
crypto/tcrypt.c +5 -0
@@ -2191,6 +2191,11 @@ static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb) 16, 16, aead_speed_template_19, num_mb);break ; +case 226 : +test_cipher_speed ("hctr2(aes)" ,ENCRYPT , sec,NULL , +0 , speed_template_32); +break ; +case 300 :if (alg) {test_hash_speed (alg, sec, generic_hash_speed_template);
crypto/testmgr.c +8 -0
@@ -5030,6 +5030,14 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .hash = __VECS(ghash_tv_template) } + }, { + .alg = "hctr2(aes)", + .generic_driver = + "hctr2_base(xctr(aes-generic),polyval-generic)", + .test = alg_test_skcipher, + .suite = { + .cipher = __VECS(aes_hctr2_tv_template) + } }, { .alg = "hmac(md5)", .test = alg_test_hash,
crypto/testmgr.h +672 -0
676 lines not on this page · raw message
Signature · 1 line
2.35.1.723.g4982287a31-goog
Eric Biggers
maintainerRe: [PATCH v3 3/8] crypto: hctr2 - Add HCTR2 support
crypto/hctr2.c line 48
Quoted 6 lines · Nathan Huckleberry
crypto/hctr2.c new file +6
-0+struct hctr2_tfm_ctx { +struct crypto_cipher *blockcipher; +struct crypto_skcipher *xctr; +struct crypto_shash *polyval; +u8 L[BLOCKCIPHER_BLOCK_SIZE ]; +};
How about adding a comment at the end of the struct above that says that the struct is followed by the two exported_length_digests? (Or hashed_tweaklen, which is the name suggested in my suggested helper functions below?)
Quoted 4 lines
crypto/hctr2.c new file +4
-0+ +struct hctr2_request_ctx { +u8 first_block[BLOCKCIPHER_BLOCK_SIZE ]; +u8 xctr_iv[BLOCKCIPHER_BLOCK_SIZE ];
crypto/hctr2.c line 62
Quoted 10 lines
crypto/hctr2.c new file +10
-0+struct scatterlist *bulk_part_dst; +struct scatterlist *bulk_part_src; +struct scatterlist sg_src[2 ]; +struct scatterlist sg_dst[2 ]; +/* Sub-requests, must be last */ +union { +struct shash_desc hash_desc; +struct skcipher_request xctr_req; + } u; +};
Likewise above for the hashed tweak.
Also how about adding inline functions or macros that return these new fields, so that the arithmetic to find them doesn't have to be duplicated in the code? The 'exported_length_digests' array local variables are a bit weird. Maybe just use some helper functions directly to get at the fields?
How about:
static inline u8 *hctr2_hashed_tweaklen(const struct hctr2_tfm_ctx *tctx,
bool odd)
{
u8 *p = (u8 *)tctx + sizeof(*tctx);
if (odd) /* For messages not a multiple of block length */
p += crypto_shash_statesize(tctx->polyval);
return p;
}static inline u8 *hctr2_hashed_tweak(const struct hctr2_tfm_ctx *tctx,
struct hctr2_request_ctx *rctx)
{
return (u8 *)rctx + tctx->hashed_tweak_offset;
}crypto/hctr2.c line 76
Quoted 13 lines
crypto/hctr2.c new file +13
-0+static int hctr2_setkey (struct crypto_skcipher *tfm,const u8 *key, +unsigned int keylen) +{ +struct hctr2_tfm_ctx *tctx =crypto_skcipher_ctx (tfm); +u8 hbar[BLOCKCIPHER_BLOCK_SIZE ]; +__le64 tweak_length_block[2 ]; +void *exported_length_digests[2 ]; +SHASH_DESC_ON_STACK (shash, tfm->polyval); +int err; + + exported_length_digests[0 ] = (u8 *)tctx +sizeof (*tctx); + exported_length_digests[1 ] = (u8 *)tctx +sizeof (*tctx) + +crypto_shash_descsize (tctx->polyval);
The size needed by crypto_shash_export() is crypto_shash_statesize(), not crypto_shash_descsize(). They happen to be the same with all polyval implementations you've proposed, but it's not guaranteed for shash algorithms in general.
Quoted 45 lines
crypto/hctr2.c new file +45
-0+crypto_cipher_clear_flags (tctx->blockcipher,CRYPTO_TFM_REQ_MASK ); +crypto_cipher_set_flags (tctx->blockcipher, +crypto_skcipher_get_flags (tfm) & +CRYPTO_TFM_REQ_MASK ); + err =crypto_cipher_setkey (tctx->blockcipher, key, keylen); +if (err) +return err; + +crypto_skcipher_clear_flags (tctx->xctr,CRYPTO_TFM_REQ_MASK ); +crypto_skcipher_set_flags (tctx->xctr, +crypto_skcipher_get_flags (tfm) & +CRYPTO_TFM_REQ_MASK ); + err =crypto_skcipher_setkey (tctx->xctr, key, keylen); +if (err) +return err; + +memset (tctx->L,0 ,sizeof (tctx->L)); +memset (hbar,0 ,sizeof (hbar)); + tctx->L[0 ] =0x01 ; +crypto_cipher_encrypt_one (tctx->blockcipher, tctx->L, tctx->L); +crypto_cipher_encrypt_one (tctx->blockcipher, hbar, hbar); + +crypto_shash_clear_flags (tctx->polyval,CRYPTO_TFM_REQ_MASK ); +crypto_shash_set_flags (tctx->polyval,crypto_skcipher_get_flags (tfm) & +CRYPTO_TFM_REQ_MASK ); + err =crypto_shash_setkey (tctx->polyval, hbar,BLOCKCIPHER_BLOCK_SIZE ); +if (err) +return err; +memzero_explicit (hbar,sizeof (hbar)); + + 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, exported_length_digests[0 ]); +if (err) +return err; +
crypto/hctr2.c line 131
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, exported_length_digests[1 ]); +}
hctr2_setkey() is getting pretty long. How about splitting the tweak length pre-hashing into a helper function?
Also, a comment that explains why the tweak length is being pre-hashed, and why it *can* be pre-hashed, would be helpful. Note that it is only possible because this implementation only supports one tweak length.
- Eric
PATCH v3 4/8 crypto: x86/aesni-xctr: Add accelerated implementation of XCTR
In reply to Nathan HuckleberryAdd hardware accelerated versions of XCTR for x86-64 CPUs with AESNI support. These implementations are modified versions of the CTR implementations found in aesni-intel_asm.S and aes_ctrby8_avx-x86_64.S.
More information on XCTR can be found in the HCTR2 paper:
Length-preserving encryption with HCTR2:
https://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
@@ -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
@@ -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.723.g4982287a31-goog
PATCH v3 5/8 crypto: arm64/aes-xctr: Add accelerated implementation of XCTR
In reply to Nathan HuckleberryAdd hardware accelerated version of XCTR for ARM64 CPUs with ARMv8 Crypto Extension support. This XCTR implementation is based on the CTR implementation in aes-modes.S.
More information on XCTR can be found in
the HCTR2 paper: Length-preserving encryption with HCTR2:
https://eprint.iacr.org/2021/1441.pdf
--- arch/arm64/crypto/Kconfig | 4 +- arch/arm64/crypto/aes-glue.c | 65 ++++++++++++++++- arch/arm64/crypto/aes-modes.S | 134 ++++++++++++++++++++++++++++++++++ 3 files changed, 199 insertions(+), 4 deletions(-)
arch/arm64/crypto/Kconfig +2 -2
@@ -84,13 +84,13 @@ config CRYPTO_AES_ARM64_CE_CCM select CRYPTO_LIB_AES config CRYPTO_AES_ARM64_CE_BLK - tristate "AES in ECB/CBC/CTR/XTS modes using ARMv8 Crypto Extensions" + tristate "AES in ECB/CBC/CTR/XTS/XCTR modes using ARMv8 Crypto Extensions" depends on KERNEL_MODE_NEON select CRYPTO_SKCIPHER select CRYPTO_AES_ARM64_CE config CRYPTO_AES_ARM64_NEON_BLK - tristate "AES in ECB/CBC/CTR/XTS modes using NEON instructions" + tristate "AES in ECB/CBC/CTR/XTS/XCTR modes using NEON instructions" depends on KERNEL_MODE_NEON select CRYPTO_SKCIPHER select CRYPTO_LIB_AES
arch/arm64/crypto/aes-glue.c +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.723.g4982287a31-goog
PATCH v3 6/8 crypto: x86/polyval: Add PCLMULQDQ accelerated implementation of POLYVAL
In reply to Nathan HuckleberryAdd hardware accelerated version of POLYVAL for x86-64 CPUs with PCLMULQDQ support.
This implementation is accelerated using PCLMULQDQ instructions to perform the finite field computations. For added efficiency, 8 blocks of the message are processed simultaneously by precomputing the first 8 powers of the key.
Schoolbook multiplication is used instead of Karatsuba multiplication because it was found to be slightly faster on x86-64 machines. Montgomery reduction must be used instead of Barrett reduction due to the difference in modulus between POLYVAL's field and other finite fields.
More information on POLYVAL can be found in the HCTR2 paper:
Length-preserving encryption with HCTR2:
https://eprint.iacr.org/2021/1441.pdf
--- arch/x86/crypto/Makefile | 3 + arch/x86/crypto/polyval-clmulni_asm.S | 376 +++++++++++++++++++++++++ arch/x86/crypto/polyval-clmulni_glue.c | 361 ++++++++++++++++++++++++ crypto/Kconfig | 10 + 4 files changed, 750 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
@@ -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 +376 -0
376 lines not on this page · raw message
arch/x86/crypto/polyval-clmulni_glue.c new file +361 -0
361 lines not on this page · raw message
crypto/Kconfig +10 -0
@@ -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.723.g4982287a31-goog
Eric Biggers
maintainerRe: [PATCH v3 6/8] crypto: x86/polyval: Add PCLMULQDQ accelerated implementation of POLYVAL
Quoted 24 lines · Nathan Huckleberry
arch/x86/crypto/polyval-clmulni_asm.S new file +18
-0@@ -0,0 +1,376 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright 2021 Google LLC + */ +/* + * This is an efficient implementation of POLYVAL using intel PCLMULQDQ-NI + * instructions. It works on 8 blocks at a time, by precomputing the first 8 + * keys powers h^8, ..., h^1 in the POLYVAL finite field. This precomputation + * allows us to split finite field multiplication into two steps. + * + * In the first step, we consider h^i, m_i as normal polynomials of degree less + * than 128. We then compute p(x) = h^8m_0 + ... + h^1m_7 where multiplication + * is simply polynomial multiplication. + * + * In the second step, we compute the reduction of p(x) modulo the finite field + * modulus g(x) = x^128 + x^127 + x^126 + x^121 + 1. + * + * This two step process is equivalent to computing h^8m_0 + ... + h^1m_7 where
arch/x86/crypto/polyval-clmulni_asm.S line 28
Quoted 10 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +10
-0+ * multiplication is finite field multiplication. The advantage is that the + * two-step process only requires 1 finite field reduction for every 8 + * polynomial multiplications. Further parallelism is gained by interleaving the + * multiplications and polynomial reductions. + */ + +#include <linux/linkage.h> +#include <asm/frame.h> + +#define NUM_PRECOMPUTE_POWERS 8
STRIDE_BLOCKS might be a better name than NUM_PRECOMPUTE_POWERS. Shorter but more descriptive, IMO.
arch/x86/crypto/polyval-clmulni_asm.S line 64
Quoted 11 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +11
-0+/* + * Performs schoolbook1_iteration on two lists of 128-bit polynomials of length + * b pointed to by MSG and KEY_POWERS. + */ +.macro schoolbook1 count + .set i, 0 + .rept (\count) + schoolbook1_iteration i 0 + .set i, (i +1) + .endr +.endm
'count', not 'b'.
Quoted 18 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +18
-0+/* + * 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
arch/x86/crypto/polyval-clmulni_asm.S line 93
Quoted 10 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +10
-0+ * HI ^= HIGH + * + * Later, the 256-bit result can be extracted as: + * [HI_H : HI_L ^ MI_H : LO_H ^ MI_L : LO_L] + * This step is done when computing the polynomial reduction for efficiency + * reasons. + * + * If xor_sum == 1, then also XOR the value of SUM into m_0. This avoids an + * extra multiplication of SUM and h^N. + */
h^8 instead of h^N? The above is one of only two places where "N" is mentioned, and the other uses to mean something different from here.
Quoted 4 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +4
-0+/* + * Performs the same computation as schoolbook1_iteration, except we expect the + * arguments to already be loaded into xmm0 and xmm1. + */
arch/x86/crypto/polyval-clmulni_asm.S line 122
Quoted 10 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +10
-0+.macro schoolbook1_noload + vpclmulqdq $0x01, %xmm0, %xmm1, %xmm2 + vpclmulqdq $0x00, %xmm0, %xmm1, %xmm3 + vpclmulqdq $0x11, %xmm0, %xmm1, %xmm4 + vpclmulqdq $0x10, %xmm0, %xmm1, %xmm5 + vpxor %xmm2, MI, MI + vpxor %xmm3, LO, LO + vpxor %xmm5, MI, MI + vpxor %xmm4, HI, HI +.endm
How about making this macro set LO, MI, HI and directly instead of XOR'ing into them? That's actually what the two users of it want. I.e.:
/*
* Performs the same computation as schoolbook1_iteration, except we expect the
* arguments to already be loaded into xmm0 and xmm1, and we set the result
* registers LO, MI, and HI directly rather than XOR'ing into them. */
.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
That would save some instructions.
arch/x86/crypto/polyval-clmulni_asm.S line 83
Quoted 2 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +2
-0+/* + * Computes the 128-bit reduction of PL, PH. Stores the result in PH.
"PL, PH" => "PH : PL".
Also mention which register this clobbers.
arch/x86/crypto/polyval-clmulni_asm.S line 142
Quoted 5 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +5
-0+ * + * This macro computes p(x) mod g(x) where p(x) is in montgomery form and g(x) = + * x^128 + x^127 + x^126 + x^121 + 1. + * + * We have a 256-bit polynomial P_3 : P_2 : P_1 : P_0 that is the product of
"P_3 : P_2 : P_1 : P_0" => "PH : PL = P_3 : P_2 : P_1 : P_0", so that it's clear how P_3 through P_0 relate to PH and PL.
arch/x86/crypto/polyval-clmulni_asm.S line 155
Quoted 13 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +13
-0+ * two 128-bit polynomials in Montgomery form. We need to reduce it mod g(x). + * Also, since polynomials in Montgomery form have an "extra" factor of x^128, + * this product has two extra factors of x^128. To get it back into Montgomery + * form, we need to remove one of these factors by dividing by x^128. + * + * To accomplish both of these goals, we add multiples of g(x) that cancel out + * the low 128 bits P_1 : P_0, leaving just the high 128 bits. Since the low + * bits are zero, the polynomial division by x^128 can be done by right shifting. + * + * Since the only nonzero term in the low 64 bits of g(x) is the constant term, + * the multiple of g(x) needed to cancel out P_0 is P_0 * g(x). The CPU can + * only do 64x64 bit multiplications, so split P_0 * g(x) into x^128 * P_0 + + * x^64 g*(x) * P_0 + P_0, where g*(x) is bits 64-127 of g(x). Adding this to
"x^64 g*(x)" => "x^64 * g*(x)"
arch/x86/crypto/polyval-clmulni_asm.S line 157
Quoted 2 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +2
-0+ * the original polynomial gives P_3 : P_2 + P_0 + T_1 : P_1 + T_0 : 0, where T + * = T_1 : T_0 = g*(x) * P0. Thus, bits 0-63 got "folded" into bits 64-191.
"P0" => "P_0"
arch/x86/crypto/polyval-clmulni_asm.S line 168
Quoted 11 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +11
-0+ * + * Repeating this same process on the next 64 bits "folds" bits 64-127 into bits + * 128-255, giving the answer in bits 128-255. This time, we need to cancel P_1 + * + T_0 in bits 64-127. The multiple of g(x) required is (P_1 + T_0) * g(x) * + * x^64. Adding this to our previous computation gives P_3 + P_1 + T_0 + V_1 : + * P_2 + P_0 + T_1 + V_0 : 0 : 0, where V = V_1 : V_0 = g*(x) * (P_1 + T_0). + * + * 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) + * p(x) / x^{128} mod g(x) = P_3 ^ P_1 ^ V_1 ^ T_0 : P_2 ^ P_0 ^ V_0 ^ T_1
The notation suddenly changes from + to ^. How about consistently using +? Or ^, either one as long as it's consistent...
Also, for the final line, the order "P_3 + P_1 + T_0 + V_1 : P_2 + P_0 + T_1 + V_0" would make more sense, as it would match the logic of the code.
Quoted 4 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +4
-0+ * + * The implementation below saves a XOR instruction by computing P_1 ^ T_0 : P_0 + * ^ T_1 and XORing into PH, rather than directly XORing P_1 : P_0, T_0 : T1 + * into PH. This allows us to reuse P_1 ^ T_0 when computing V.
arch/x86/crypto/polyval-clmulni_asm.S line 182
Quoted 10 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +10
-0+ */ +.macro montgomery_reduction + movdqa PL, T + pclmulqdq $0x00, GSTAR, T # T = [P_0 * g*(x)] + pshufd $0b01001110, T, V # V = [T_0 : T_1] + pxor V, PL # PL = [P_1 ^ T_0 : P_0 ^ T_1] + pxor PL, PH # PH = [P_1 ^ T_0 ^ P_3 : P_0 ^ T_1 ^ P_2] + pclmulqdq $0x11, GSTAR, PL # PL = [(P_1 ^ T_0) * g*(x)] + pxor PL, PH +.endm
Several comments here:
- Aligning the comments would make them much easier to read.
- Only one temporary register is needed, since T isn't used after it's used to compute V.
- The thing called V isn't actually the same as the V described in the long comment above. Maybe just call the temporary variable 'TMP_XMM' or something? Or even just hard-code %xmm6, similar to %xmm0-%xmm5.
- It's not necessary to modify PL.
- Since this file is relying on AVX anyway, the three-operand instructions are available, and can be used to avoid the 'movdqa' at the beginning.
- None of the users of this macro really want the result in register PH. How about passing the destination register as an argument and using vpxor to put it in the appropriate place?
So in summary, this is what I'd suggest:
.macro montgomery_reduction dest
vpclmulqdq $0x00, GSTAR, PL, TMP_XMM # TMP_XMM = T_1 : T_0 = P_0 * g*(x) pshufd $0b01001110, TMP_XMM, TMP_XMM # TMP_XMM = T_0 : T_1 pxor PL, TMP_XMM # TMP_XMM = P_1 + T_0 : P_0 + T_1 pxor TMP_XMM, PH # PH = P_3 + P_1 + T_0 : P_2 + P_0 + T_1 pclmulqdq $0x11, GSTAR, TMP_XMM # TMP_XMM = V_1 : V_0 = V = [(P_1 + T_0) * g*(x)] vpxor TMP_XMM, PH, \dest
.endm
Quoted 6 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +6
-0+ +/* + * Compute schoolbook multiplication for 8 blocks + * m_0h^8 + ... + m_7h^1 + * + * If reduce is set, also computes the montgomery reduction of the
arch/x86/crypto/polyval-clmulni_asm.S line 198
Quoted 10 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +10
-0+ * 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 + * + */ +.macro full_stride reduce + mov %rsi, KEY_POWERS
I don't see why KEY_POWERS and %rsi are different registers. Why not just define KEY_POWERS to %rsi? It stays the same during any full_strides, and then will be incremented by partial_stride. That's fine.
[...]
arch/x86/crypto/polyval-clmulni_asm.S line 242
Quoted 1 line
arch/x86/crypto/polyval-clmulni_asm.S new file +1
-0+ addq $(8*16), KEY_POWERS
As per the above, there's no need to increment KEY_POWERS here.
arch/x86/crypto/polyval-clmulni_asm.S line 249
Quoted 7 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +7
-0+ schoolbook2 +.endm + +/* + * Compute poly on window size of %rdx blocks + * 0 < %rdx < NUM_PRECOMPUTE_POWERS + */
The code doesn't actually use %rdx directly. It should be BLOCKS_LEFT.
Quoted 52 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +52
-0+.macro partial_stride + pxor LO, LO + pxor HI, HI + pxor MI, MI + mov BLOCKS_LEFT, TMP + shlq $4, TMP + mov %rsi, KEY_POWERS + addq $(16*NUM_PRECOMPUTE_POWERS), KEY_POWERS + subq TMP, KEY_POWERS + # Multiply sum by h^N + movups (KEY_POWERS), %xmm0 + movdqa SUM, %xmm1 + schoolbook1_noload + schoolbook2 + montgomery_reduction + movdqa PH, SUM + pxor LO, LO + pxor HI, HI + pxor MI, MI + xor IDX, IDX +.LloopPartial: + cmpq BLOCKS_LEFT, IDX # IDX < rdx + jae .LloopExitPartial + + movq BLOCKS_LEFT, TMP + subq IDX, TMP # TMP = rdx - IDX + + cmp $4, TMP # TMP < 4 ? + jl .Llt4Partial + schoolbook1 4 + addq $4, IDX + addq $(4*16), MSG + addq $(4*16), KEY_POWERS + jmp .LoutPartial +.Llt4Partial: + cmp $3, TMP # TMP < 3 ? + jl .Llt3Partial + schoolbook1 3 + addq $3, IDX + addq $(3*16), MSG + addq $(3*16), KEY_POWERS + jmp .LoutPartial +.Llt3Partial: + cmp $2, TMP # TMP < 2 ? + jl .Llt2Partial + schoolbook1 2 + addq $2, IDX + addq $(2*16), MSG + addq $(2*16), KEY_POWERS + jmp .LoutPartial +.Llt2Partial: + schoolbook1 1 # TMP < 1 ?
arch/x86/crypto/polyval-clmulni_asm.S line 311
Quoted 10 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +10
-0+ addq $1, IDX + addq $(1*16), MSG + addq $(1*16), KEY_POWERS +.LoutPartial: + jmp .LloopPartial +.LloopExitPartial: + schoolbook2 + montgomery_reduction + pxor PH, SUM +.endm
This can be simplified and optimized quite a bit:
- The first schoolbook2 and montgomery_reduction are unnecessary.
- The IDX variable is unnecessary.
- There's no need for a loop if there are going to be separate cases for 4, 2, and 1 blocks anyway. We can just always jump forward.
- There's no need to increment MSG and KEY_POWERS after the last block.
Can you consider the following?
/*
* Process BLOCKS_LEFT blocks, where 0 < BLOCKS_LEFT < STRIDE_BLOCKS */
.macro partial_stride
mov BLOCKS_LEFT, TMP
shlq $4, TMP
addq $(16*STRIDE_BLOCKS), KEY_POWERS
subq TMP, KEY_POWERS
movups (MSG), %xmm0
pxor SUM, %xmm0
movaps (KEY_POWERS), %xmm1
schoolbook1_noload
dec BLOCKS_LEFT
addq $16, MSG
addq $16, KEY_POWERS
test $4, BLOCKS_LEFT
jz .Lpartial4BlocksDone
schoolbook1 4
addq $(4*16), MSG
addq $(4*16), KEY_POWERS
.Lpartial4BlocksDone:
test $2, BLOCKS_LEFT jz .Lpartial2BlocksDone schoolbook1 2 addq $(2*16), MSG addq $(2*16), KEY_POWERS
.Lpartial2BlocksDone:
test $1, BLOCKS_LEFT
jz .LpartialDone
schoolbook1 1
.LpartialDone:
schoolbook2
montgomery_reduction SUM
.endm
arch/x86/crypto/polyval-clmulni_asm.S line 336
Quoted 3 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +3
-0+ FRAME_END + ret +SYM_FUNC_END(clmul_polyval_mul)
It needs to be RET, not ret. See https://git.kernel.org/linus/f94909ceb1ed4bfd
Quoted 30 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +30
-0+ +/* + * Perform polynomial evaluation as specified by POLYVAL. This computes: + * h^n * accumulator + h^n * m_0 + ... + h^1 * m_{n-1} + * where n=nblocks, h is the hash key, and m_i are the message blocks. + * + * rdi - pointer to message blocks + * rsi - pointer to precomputed key powers h^8 ... h^1 + * rdx - number of blocks to hash + * rcx - pointer to the accumulator + * + * void clmul_polyval_update(const u8 *in, const struct polyval_ctx *ctx, + * size_t nblocks, u8 *accumulator); + */ +SYM_FUNC_START(clmul_polyval_update) + FRAME_BEGIN + vmovdqa .Lgstar(%rip), GSTAR + movups (%rcx), SUM + cmpq $NUM_PRECOMPUTE_POWERS, BLOCKS_LEFT + jb .LstrideLoopExit + full_stride 0 + subq $NUM_PRECOMPUTE_POWERS, BLOCKS_LEFT +.LstrideLoop: + cmpq $NUM_PRECOMPUTE_POWERS, BLOCKS_LEFT + jb .LstrideLoopExitReduce + full_stride 1 + subq $NUM_PRECOMPUTE_POWERS, BLOCKS_LEFT + jmp .LstrideLoop +.LstrideLoopExitReduce: + montgomery_reduction
arch/x86/crypto/polyval-clmulni_asm.S line 376
Quoted 10 lines
arch/x86/crypto/polyval-clmulni_asm.S new file +10
-0+ movdqa PH, SUM +.LstrideLoopExit: + test BLOCKS_LEFT, BLOCKS_LEFT + je .LskipPartial + partial_stride +.LskipPartial: + movups SUM, (%rcx) + FRAME_END + ret +SYM_FUNC_END(clmul_polyval_update)
There are several unneeded instructions above. Unconditional jumps can be avoided, as can comparisons if they are already paired with subtractions using the same amounts (since on x86, subtractions set the flags too).
Consider the following:
SYM_FUNC_START(clmul_polyval_update)
FRAME_BEGIN
vmovdqa .Lgstar(%rip), GSTAR
movups (%rcx), SUM
subq $STRIDE_BLOCKS, BLOCKS_LEFT
js .LstrideLoopExit
full_stride 0
subq $STRIDE_BLOCKS, BLOCKS_LEFT
js .LstrideLoopExitReduce
.LstrideLoop:
full_stride 1
subq $STRIDE_BLOCKS, BLOCKS_LEFT
jns .LstrideLoop
.LstrideLoopExitReduce:
montgomery_reduction SUM
.LstrideLoopExit:
add $STRIDE_BLOCKS, BLOCKS_LEFT
jz .LskipPartial
partial_stride
.LskipPartial:
movups SUM, (%rcx)
FRAME_END
RETSYM_FUNC_END(clmul_polyval_update)
- Eric
PATCH v3 7/8 crypto: arm64/polyval: Add PMULL accelerated implementation of POLYVAL
In reply to Nathan HuckleberryAdd hardware accelerated version of POLYVAL for ARM64 CPUs with Crypto 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 | 7 + arch/arm64/crypto/Makefile | 3 + arch/arm64/crypto/polyval-ce-core.S | 372 ++++++++++++++++++++++++++++ arch/arm64/crypto/polyval-ce-glue.c | 363 +++++++++++++++++++++++++++ 4 files changed, 745 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 +7 -0
@@ -60,6 +60,13 @@ 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_HASH + select CRYPTO_POLYVAL + config CRYPTO_CRCT10DIF_ARM64_CE tristate "CRCT10DIF digest algorithm using PMULL instructions" depends on KERNEL_MODE_NEON && CRC_T10DIF
arch/arm64/crypto/Makefile +3 -0
@@ -26,6 +26,9 @@ sm4-ce-y := sm4-ce-glue.o sm4-ce-core.o obj-$(CONFIG_CRYPTO_GHASH_ARM64_CE) += ghash-ce.o ghash-ce-y := ghash-ce-glue.o ghash-ce-core.o +obj-$(CONFIG_CRYPTO_POLYVAL_ARM64_CE) += polyval-ce.o +polyval-ce-y := polyval-ce-glue.o polyval-ce-core.o + obj-$(CONFIG_CRYPTO_CRCT10DIF_ARM64_CE) += crct10dif-ce.o crct10dif-ce-y := crct10dif-ce-core.o crct10dif-ce-glue.o
arch/arm64/crypto/polyval-ce-core.S new file +372 -0
372 lines not on this page · raw message
arch/arm64/crypto/polyval-ce-glue.c new file +363 -0
363 lines not on this page · raw message
Signature · 1 line
2.35.1.723.g4982287a31-goog
Eric Biggers
maintainerRe: [PATCH v3 7/8] crypto: arm64/polyval: Add PMULL accelerated implementation of POLYVAL
Quoted 2 lines · Nathan Huckleberry
Add hardware accelerated version of POLYVAL for ARM64 CPUs with Crypto Extension support.
Nit: It's "Crypto Extensions", not "Crypto Extension".
arch/arm64/crypto/Kconfig config CRYPTO_GHASH_ARM64_CE · line 68
Quoted 6 lines
arch/arm64/crypto/Kconfig +6
-0+config CRYPTO_POLYVAL_ARM64_CE + tristate "POLYVAL using ARMv8 Crypto Extensions (for HCTR2)" + depends on KERNEL_MODE_NEON + select CRYPTO_CRYPTD + select CRYPTO_HASH + select CRYPTO_POLYVAL
CRYPTO_POLYVAL selects CRYPTO_HASH already, so there's no need to select it here.
Quoted 29 lines
arch/arm64/crypto/polyval-ce-core.S new file +29
-0+/* + * Perform polynomial evaluation as specified by POLYVAL. This computes: + * h^n * accumulator + h^n * m_0 + ... + h^1 * m_{n-1} + * where n=nblocks, h is the hash key, and m_i are the message blocks. + * + * x0 - pointer to message blocks + * x1 - pointer to precomputed key powers h^8 ... h^1 + * x2 - number of blocks to hash + * x3 - pointer to accumulator + * + * void pmull_polyval_update(const u8 *in, const struct polyval_ctx *ctx, + * size_t nblocks, u8 *accumulator); + */ +SYM_FUNC_START(pmull_polyval_update) + adr TMP, .Lgstar + ld1 {GSTAR.2d}, [TMP] + ld1 {SUM.16b}, [x3] + ands PARTIAL_LEFT, BLOCKS_LEFT, #7 + beq .LskipPartial + partial_stride +.LskipPartial: + subs BLOCKS_LEFT, BLOCKS_LEFT, #NUM_PRECOMPUTE_POWERS + blt .LstrideLoopExit + ld1 {KEY8.16b, KEY7.16b, KEY6.16b, KEY5.16b}, [x1], #64 + ld1 {KEY4.16b, KEY3.16b, KEY2.16b, KEY1.16b}, [x1], #64 + full_stride 0 + subs BLOCKS_LEFT, BLOCKS_LEFT, #NUM_PRECOMPUTE_POWERS + blt .LstrideLoopExitReduce +.LstrideLoop:
arch/arm64/crypto/polyval-ce-core.S line 372
Quoted 10 lines
arch/arm64/crypto/polyval-ce-core.S new file +10
-0+ full_stride 1 + subs BLOCKS_LEFT, BLOCKS_LEFT, #NUM_PRECOMPUTE_POWERS + bge .LstrideLoop +.LstrideLoopExitReduce: + montgomery_reduction + mov SUM.16b, PH.16b +.LstrideLoopExit: + st1 {SUM.16b}, [x3] + ret +SYM_FUNC_END(pmull_polyval_update)
Is there a reason why partial_stride is done first in the arm64 implementation, but last in the x86 implementation? It would be nice if the implementations worked the same way. Probably last would be better? What is the advantage of doing it first?
Besides that, many of the comments I made on the x86 implementation apply to the arm64 implementation too.
- Eric
Re: [PATCH v3 7/8] crypto: arm64/polyval: Add PMULL accelerated implementation of POLYVAL
Quoted 53 lines · Eric Biggers
Quoted 2 lines · Nathan Huckleberry
Add hardware accelerated version of POLYVAL for ARM64 CPUs with Crypto Extension support.
Nit: It's "Crypto Extensions", not "Crypto Extension".
arch/arm64/crypto/Kconfig config CRYPTO_GHASH_ARM64_CE · line 68
Quoted 6 lines
arch/arm64/crypto/Kconfig +6
-0+config CRYPTO_POLYVAL_ARM64_CE + tristate "POLYVAL using ARMv8 Crypto Extensions (for HCTR2)" + depends on KERNEL_MODE_NEON + select CRYPTO_CRYPTD + select CRYPTO_HASH + select CRYPTO_POLYVAL
CRYPTO_POLYVALselectsCRYPTO_HASHalready, so there's no need to select it here.Quoted 36 lines
arch/arm64/crypto/polyval-ce-core.S new file +36
-0+/* + * Perform polynomial evaluation as specified by POLYVAL. This computes: + * h^n * accumulator + h^n * m_0 + ... + h^1 * m_{n-1} + * where n=nblocks, h is the hash key, and m_i are the message blocks. + * + * x0 - pointer to message blocks + * x1 - pointer to precomputed key powers h^8 ... h^1 + * x2 - number of blocks to hash + * x3 - pointer to accumulator + * + * void pmull_polyval_update(const u8 *in, const struct polyval_ctx *ctx, + * size_t nblocks, u8 *accumulator); + */ +SYM_FUNC_START(pmull_polyval_update) + adr TMP, .Lgstar + ld1 {GSTAR.2d}, [TMP] + ld1 {SUM.16b}, [x3] + ands PARTIAL_LEFT, BLOCKS_LEFT, #7 + beq .LskipPartial + partial_stride +.LskipPartial: + subs BLOCKS_LEFT, BLOCKS_LEFT, #NUM_PRECOMPUTE_POWERS + blt .LstrideLoopExit + ld1 {KEY8.16b, KEY7.16b, KEY6.16b, KEY5.16b}, [x1], #64 + ld1 {KEY4.16b, KEY3.16b, KEY2.16b, KEY1.16b}, [x1], #64 + full_stride 0 + subs BLOCKS_LEFT, BLOCKS_LEFT, #NUM_PRECOMPUTE_POWERS + blt .LstrideLoopExitReduce +.LstrideLoop: + full_stride 1 + subs BLOCKS_LEFT, BLOCKS_LEFT, #NUM_PRECOMPUTE_POWERS + bge .LstrideLoop +.LstrideLoopExitReduce: + montgomery_reduction + mov SUM.16b, PH.16b +.LstrideLoopExit:
Quoted 8 lines
arch/arm64/crypto/polyval-ce-core.S line 372
Quoted 3 lines
arch/arm64/crypto/polyval-ce-core.S new file +3
-0+ st1 {SUM.16b}, [x3] + ret +SYM_FUNC_END(pmull_polyval_update)Is there a reason why
partial_strideis done first in the arm64 implementation, but last in the x86 implementation? It would be nice if the implementations worked the same way. Probably last would be better? What is the advantage of doing it first?
It was so I could return early without loading keys into registers, since I only need them if there's
a full stride. I was able to rewrite it in the same way that the x86 implementation works.
Quoted 5 lines
Besides that, many of the comments I made on the x86 implementation apply to the arm64 implementation too.
- Eric
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v3 8/8 fscrypt: Add HCTR2 support for filename encryption
In reply to Nathan HuckleberryHCTR2 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
@@ -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
@@ -31,7 +31,7 @@#define FSCRYPT_CONTEXT_V2 2/* Keep this in sync with include/uapi/linux/fscrypt.h */ -#define FSCRYPT_MODE_MAX FSCRYPT_MODE_ADIANTUM +#define FSCRYPT_MODE_MAX FSCRYPT_MODE_AES_256_HCTR2struct fscrypt_context_v1 {u8 version;/* FSCRYPT_CONTEXT_V1 */
fs/crypto/keysetup.c +7 -0
@@ -53,6 +53,13 @@ struct fscrypt_mode fscrypt_modes[] = { .ivsize = 32, .blk_crypto_mode = BLK_ENCRYPTION_MODE_ADIANTUM, }, + [FSCRYPT_MODE_AES_256_HCTR2] = { + .friendly_name = "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
@@ -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
@@ -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
@@ -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.723.g4982287a31-goog