Skip to contentSkip to conversation

generic: test HCTR2 filename encryption

RFC PATCH Superseded Nathan Huckleberry 2 participants 4 messages May 20, 2022 – May 30, 2022 fscrypt: file system level encryption support

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover generic: test HCTR2 filename encryption

This patchset is not intended to be accepted yet. It is reliant on HCTR2 support in the kernel which has not yet been accepted. See the HCTR2 patchset
here: https://lore.kernel.org/all/[email protected]/

HCTR2 is a new wide-block encryption mode that can used for filename encryption in fscrypt. This patchset adds a reference implementation of HCTR2 to the fscrypt testing utility and adds tests for filename encryption with HCTR2.

More information on HCTR2 can be found here: "Length-preserving encryption with HCTR2": https://ia.cr/2021/1441

Changes in v3:
* Consolidate tests into one file

Changes in v2:
* Use POLYVAL multiplication directly instead of using GHASH trick
* Split reference implementation and tests into two patches
* Remove v1 policy tests
* Various small style fixes

Nathan Huckleberry (2):
fscrypt-crypt-util: add HCTR2 reference implementation
generic: add tests for fscrypt policies with HCTR2

 common/encrypt           |   2 +
 src/fscrypt-crypt-util.c | 338 ++++++++++++++++++++++++++++++++-------
 tests/generic/900        |  28 ++++
 tests/generic/900.out    |  16 ++
 4 files changed, 327 insertions(+), 57 deletions(-)
 create mode 100755 tests/generic/900
 create mode 100644 tests/generic/900.out
Signature · 1 line
2.36.1.124.g0e6072fb45-goog
#1 lore raw

RFC PATCH v3 1/2 fscrypt-crypt-util: add HCTR2 reference implementation

This patch adds a reference implementation of HCTR2 to the fscrypt testing utility.

More information on HCTR2 can be found here: "Length-preserving encryption with HCTR2": https://ia.cr/2021/1441

---
 common/encrypt           |   2 +
 src/fscrypt-crypt-util.c | 338 ++++++++++++++++++++++++++++++++-------
 2 files changed, 283 insertions(+), 57 deletions(-)
common/encrypt +2 -0
index f90c4ef0..937bb914 100644
@@ -767,6 +767,7 @@ FSCRYPT_MODE_AES_256_CTS=4
 FSCRYPT_MODE_AES_128_CBC=5
 FSCRYPT_MODE_AES_128_CTS=6
 FSCRYPT_MODE_ADIANTUM=9
+FSCRYPT_MODE_AES_256_HCTR2=10
 
 FSCRYPT_POLICY_FLAG_DIRECT_KEY=0x04
 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64=0x08
@@ -785,6 +786,7 @@ _fscrypt_mode_name_to_num()
 	AES-128-CBC-ESSIV)	echo $FSCRYPT_MODE_AES_128_CBC ;;
 	AES-128-CTS-CBC)	echo $FSCRYPT_MODE_AES_128_CTS ;;
 	Adiantum)		echo $FSCRYPT_MODE_ADIANTUM ;;
+	AES-256-HCTR2)		echo $FSCRYPT_MODE_AES_256_HCTR2 ;;
 	*)			_fail "Unknown fscrypt mode: $name" ;;
 	esac
 }
src/fscrypt-crypt-util.c +281 -57

404 lines not on this page · raw message

Signature · 1 line
2.36.1.124.g0e6072fb45-goog
maintainer
#2 lore raw

Re: [RFC PATCH v3 1/2] fscrypt-crypt-util: add HCTR2 reference implementation

Please include the linux-fscrypt mailing list in Cc.

Quoted 2 lines · Nathan Huckleberry

This patch adds a reference implementation of HCTR2 to the fscrypt testing utility.

The reference implementation is at https://github.com/google/hctr2.
This one is just an "implementation", not a "reference implementation".

src/fscrypt-crypt-util.c static inline void gf2_128_mul_x(ble128 *t) · line 360

Quoted 12 lines

/* Multiply a GF(2^128) element by the polynomial 'x' */

src/fscrypt-crypt-util.c +3 -1
-static inline void gf2_128_mul_x(ble128 *t)
+static inline void gf2_128_mul_x_ble(ble128 *t)
 {
 	u64 lo = le64_to_cpu(t->lo);
 	u64 hi = le64_to_cpu(t->hi);
@@ -302,6 +356,38 @@ static inline void gf2_128_mul_x(ble128 *t)
 	t->lo = cpu_to_le64((lo << 1) ^ ((hi & (1ULL << 63)) ? 0x87 : 0));
 }
 
+static inline void gf2_128_mul_x_polyval(ble128 *t)
+{

Please adjust the naming and comments to clearly distinguish between gf2_128_mul_x_ble() and gf2_128_mul_x_polyval(). The intention of the original code was that "ble" gave the field convention used. However, the XTS and POLYVAL field conventions both use "ble" but differ in the reduction polynomial. So "ble" is no longer unambiguous. How about renaming gf2_128_mul_x_ble() to gf2_128_mul_x_xts() to indicate it is using the XTS convention?

src/fscrypt-crypt-util.c static inline void gf2_128_mul_x(ble128 *t) · line 365

Quoted 5 lines
src/fscrypt-crypt-util.c +5 -0
+	u64 lo = le64_to_cpu(t->lo);
+	u64 hi = le64_to_cpu(t->hi);
+
+	u64 lo_reducer = ((hi >> 63) & 1ULL) ? 1 : 0;
+	u64 hi_reducer = ((hi >> 63) & 1ULL) ? 0xc2ULL << 56 : 0;

There shouldn't be a newline between variable declarations.

Also maybe write '(hi & (1ULL << 63))', so the code is more easily comparable to gf2_128_mul_x_xts().

src/fscrypt-crypt-util.c static inline void gf2_128_mul_x(ble128 *t) · line 378

Quoted 8 lines
src/fscrypt-crypt-util.c +8 -0
+void gf2_128_mul_polyval(ble128 *r, const ble128 *b)
+{
+	ble128 p;
+	u64 lo = le64_to_cpu(b->lo);
+	u64 hi = le64_to_cpu(b->hi);
+
+	memset(&p, 0, sizeof(p));
+	for (int i = 0; i < 64; i++) {

'i' should be declared at the beginning.

src/fscrypt-crypt-util.c static inline void gf2_128_mul_x(ble128 *t) · line 389

Quoted 11 lines
src/fscrypt-crypt-util.c +11 -0
+		if (lo & (1ULL << i))
+			xor((u8 *)&p, (u8 *)&p, (u8 *)r, sizeof(p));
+		gf2_128_mul_x_polyval(r);
+	}
+	for (int i = 0; i < 64; i++) {
+		if (hi & (1ULL << i))
+			xor((u8 *)&p, (u8 *)&p, (u8 *)r, sizeof(p));
+		gf2_128_mul_x_polyval(r);
+	}
+	memcpy(r, &p, sizeof(p));
+}

The last line can be '*r = p;'

Quoted 15 lines
src/fscrypt-crypt-util.c +8 -0
+
 /*----------------------------------------------------------------------------*
  *                             Group arithmetic                               *
  *----------------------------------------------------------------------------*/
@@ -901,6 +987,41 @@ static void test_hkdf_sha512(void)
 }
 #endif /* ENABLE_ALG_TESTS */
 
+/*----------------------------------------------------------------------------*
+ *                                 POLYVAL                                     *
+ *----------------------------------------------------------------------------*/
+
+#define POLYVAL_KEY_SIZE	16
+#define POLYVAL_BLOCK_SIZE	16
+

src/fscrypt-crypt-util.c static void test_hkdf_sha512(void) · line 1006

Quoted 10 lines
src/fscrypt-crypt-util.c +10 -0
+static void polyval_update(const u8 key[POLYVAL_KEY_SIZE],
+			   const u8 *msg, size_t msglen,
+			   u8 accumulator[POLYVAL_BLOCK_SIZE])
+{
+	ble128 h;
+	// x^{-128} = x^127 + x^124 + x^121 + x^114 + 1
+	static const ble128 inv128 = {
+	    cpu_to_le64(1),
+	    cpu_to_le64(0x9204ULL << 48)
+	};

Use tabs for indentation, please. This applies elsewhere in the file too.

src/fscrypt-crypt-util.c static void test_hkdf_sha512(void) · line 1007

Quoted 1 line
src/fscrypt-crypt-util.c +1 -0
+	int nblocks = msglen / POLYVAL_BLOCK_SIZE;

size_t

src/fscrypt-crypt-util.c static void test_hkdf_sha512(void) · line 1015

Quoted 8 lines
src/fscrypt-crypt-util.c +8 -0
+	int tail = msglen % POLYVAL_BLOCK_SIZE;
+
+	memcpy(&h, key, sizeof(h));
+	gf2_128_mul_polyval(&h, &inv128);
+
+	while (nblocks > 0) {
+		xor(accumulator, accumulator, msg, POLYVAL_BLOCK_SIZE);
+		gf2_128_mul_polyval((ble128 *)accumulator, &h);

Casting a byte array to ble128, and then dereferencing it, is undefined behavior since it increases the alignment that is assumed.

Quoted 57 lines
src/fscrypt-crypt-util.c +57 -0
+#define HCTR2_IV_SIZE 32
+static void aes_256_hctr2_crypt(const u8 key[AES_256_KEY_SIZE],
+				const u8 iv[HCTR2_IV_SIZE], const u8 *src,
+				u8 *dst, size_t nbytes, bool decrypting)
+{
+	struct aes_key k;
+	u8 hbar[AES_BLOCK_SIZE] = {0};
+	u8 L[AES_BLOCK_SIZE] = {1};
+	size_t bulk_bytes = nbytes - AES_BLOCK_SIZE;
+	size_t remainder = bulk_bytes % AES_BLOCK_SIZE;
+	le128 tweaklen_blk = {
+	    .lo = cpu_to_le64(HCTR2_IV_SIZE * 8 * 2 + 2 + (remainder != 0))
+	};
+	u8 padded_block[POLYVAL_BLOCK_SIZE];
+	u8 digest[POLYVAL_BLOCK_SIZE];
+	const u8 *M = src;
+	const u8 *N = src + AES_BLOCK_SIZE;
+	u8 MM[AES_BLOCK_SIZE];
+	u8 UU[AES_BLOCK_SIZE];
+	u8 S[AES_BLOCK_SIZE];
+	u8 *U = dst;
+	u8 *V = dst + AES_BLOCK_SIZE;
+
+	ASSERT(nbytes >= AES_BLOCK_SIZE);
+	aes_setkey(&k, key, AES_256_KEY_SIZE);
+
+	aes_encrypt(&k, hbar, hbar);
+	aes_encrypt(&k, L, L);
+
+	memset(digest, 0, POLYVAL_BLOCK_SIZE);
+	polyval_update(hbar, (u8 *)&tweaklen_blk, POLYVAL_BLOCK_SIZE, digest);
+	polyval_update(hbar, iv, HCTR2_IV_SIZE, digest);
+
+	polyval_update(hbar, N, bulk_bytes - remainder, digest);
+	if (remainder) {
+		memset(padded_block, 0, POLYVAL_BLOCK_SIZE);
+		memcpy(padded_block, N + bulk_bytes - remainder, remainder);
+		padded_block[remainder] = 0x01;
+		polyval_update(hbar, padded_block, POLYVAL_BLOCK_SIZE, digest);
+	}
+
+	xor(MM, M, digest, AES_BLOCK_SIZE);
+
+	if (decrypting)
+		aes_decrypt(&k, MM, UU);
+	else
+		aes_encrypt(&k, MM, UU);
+
+	xor(S, MM, UU, AES_BLOCK_SIZE);
+	xor(S, L, S, AES_BLOCK_SIZE);
+
+	aes_256_xctr_crypt(key, S, N, V, bulk_bytes);
+
+	memset(digest, 0, POLYVAL_BLOCK_SIZE);
+	polyval_update(hbar, (u8 *)&tweaklen_blk, POLYVAL_BLOCK_SIZE, digest);
+	polyval_update(hbar, iv, HCTR2_IV_SIZE, digest);
+

src/fscrypt-crypt-util.c static void aes_128_cts_cbc_decrypt(const u8 key[AES_128_KEY_SIZE], · line 1384

Quoted 10 lines
src/fscrypt-crypt-util.c +10 -0
+	polyval_update(hbar, V, bulk_bytes - remainder, digest);
+	if (remainder) {
+		memset(padded_block, 0, POLYVAL_BLOCK_SIZE);
+		memcpy(padded_block, V + bulk_bytes - remainder, remainder);
+		padded_block[remainder] = 0x01;
+		polyval_update(hbar, padded_block, POLYVAL_BLOCK_SIZE, digest);
+	}
+
+	xor(U, UU, digest, AES_BLOCK_SIZE);
+}

Long functions with many local variables are hard to read. This would be easier to read if it used some helper functions, each called twice:

static void hctr2_hash_iv(const u8 hbar[POLYVAL_KEY_SIZE],
const u8 iv[HCTR2_IV_SIZE], size_t msglen, u8 digest[POLYVAL_BLOCK_SIZE])

{
	le128 tweaklen_blk = {
		.lo = cpu_to_le64(HCTR2_IV_SIZE * 8 * 2 + 2 +
				  (msglen % AES_BLOCK_SIZE != 0))
	};

	memset(digest, 0, POLYVAL_BLOCK_SIZE);
	polyval_update(hbar, (u8 *)&tweaklen_blk, POLYVAL_BLOCK_SIZE, digest);
	polyval_update(hbar, iv, HCTR2_IV_SIZE, digest);
}

static void hctr2_hash_message(const u8 hbar[POLYVAL_KEY_SIZE],
const u8 *msg, size_t msglen, u8 digest[POLYVAL_BLOCK_SIZE])

{
	size_t remainder = msglen % AES_BLOCK_SIZE;
	u8 padded_block[POLYVAL_BLOCK_SIZE] = {0};

	polyval_update(hbar, msg, msglen - remainder, digest);
	if (remainder) {
		memcpy(padded_block, &msg[msglen - remainder], remainder);
		padded_block[remainder] = 1;
		polyval_update(hbar, padded_block, POLYVAL_BLOCK_SIZE, digest);
	}
}

src/fscrypt-crypt-util.c static void aes_128_cts_cbc_decrypt(const u8 key[AES_128_KEY_SIZE], · line 1431

Quoted 6 lines
src/fscrypt-crypt-util.c +6 -0
+		rand_bytes(key, sizeof(key));
+		rand_bytes(iv, sizeof(iv));
+		rand_bytes(ptext, datalen);
+		memset(key, 0, sizeof(key));
+		memset(iv, 0, sizeof(iv));
+		memset(ptext, 0, datalen);

What's going on here? The memset() calls shouldn't be there.

- Eric

#3 lore raw

RFC PATCH v3 2/2 generic: add tests for fscrypt policies with HCTR2

In reply to Nathan Huckleberry

This patch adds fscrypt policy tests for filename encryption using HCTR2.

More information on HCTR2 can be found here: "Length-preserving encryption with HCTR2" https://ia.cr/2021/1441

---
 tests/generic/900     | 28 ++++++++++++++++++++++++++++
 tests/generic/900.out | 16 ++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100755 tests/generic/900
 create mode 100644 tests/generic/900.out
tests/generic/900 new file +28 -0
new file mode 100755
index 00000000..d1496007
@@ -0,0 +1,28 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright 2022 Google LLC
+#
+# FS QA Test No. generic/900
+#
+# Verify ciphertext for v2 encryption policies that use AES-256-XTS to encrypt
+# file contents and AES-256-HCTR2 to encrypt file names.
+#
+. ./common/preamble
+_begin_fstest auto quick encrypt
+
+# Import common functions.
+. ./common/filter
+. ./common/encrypt
+
+# real QA test starts here
+_supported_fs generic
+
+_verify_ciphertext_for_encryption_policy AES-256-XTS AES-256-HCTR2 v2
+_verify_ciphertext_for_encryption_policy AES-256-XTS AES-256-HCTR2 \
+	v2 iv_ino_lblk_32
+_verify_ciphertext_for_encryption_policy AES-256-XTS AES-256-HCTR2 \
+	v2 iv_ino_lblk_64
+
+# success, all done
+status=0
+exit
tests/generic/900.out new file +16 -0
new file mode 100644
index 00000000..a87c80b3
@@ -0,0 +1,16 @@
+QA output created by 900
+
+Verifying ciphertext with parameters:
+	contents_encryption_mode: AES-256-XTS
+	filenames_encryption_mode: AES-256-HCTR2
+	options: v2
+
+Verifying ciphertext with parameters:
+	contents_encryption_mode: AES-256-XTS
+	filenames_encryption_mode: AES-256-HCTR2
+	options: v2 iv_ino_lblk_32
+
+Verifying ciphertext with parameters:
+	contents_encryption_mode: AES-256-XTS
+	filenames_encryption_mode: AES-256-HCTR2
+	options: v2 iv_ino_lblk_64
Signature · 1 line
2.36.1.124.g0e6072fb45-goog