Skip to contentSkip to conversation

xfstests: make fscrypt-crypt-util self-tests work with OpenSSL 3.0

PATCH In review Eric Biggers 2 participants 6 messages Mar 19, 2023 – Mar 20, 2023 fscrypt: file system level encryption support

lore
Share & tools
lore
author maintainer
#3 lore raw

PATCH 3/3 fscrypt-crypt-util: fix XTS self-test with latest OpenSSL

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

In OpenSSL 3.0, XTS encryption fails if the message is zero-length. Therefore, update test_aes_256_xts() to not test this case.

This only affects the algorithm self-tests within fscrypt-crypt-util, which are not compiled by default.

---
 src/fscrypt-crypt-util.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
src/fscrypt-crypt-util.c +7 -3
index 040b80c0..6edf0047 100644
@@ -1107,12 +1107,16 @@ static void test_aes_256_xts(void)
 	while (num_tests--) {
 		u8 key[2 * AES_256_KEY_SIZE];
 		u8 iv[AES_BLOCK_SIZE];
-		u8 ptext[512];
+		u8 ptext[32 * AES_BLOCK_SIZE];
 		u8 ctext[sizeof(ptext)];
 		u8 ref_ctext[sizeof(ptext)];
 		u8 decrypted[sizeof(ptext)];
-		const size_t datalen = ROUND_DOWN(rand() % (1 + sizeof(ptext)),
-						  AES_BLOCK_SIZE);
+		// Don't test message lengths that aren't a multiple of the AES
+		// block size, since support for that is not implemented here.
+		// Also don't test zero-length messages, since OpenSSL 3.0 and
+		// later returns an error for those.
+		const size_t datalen = AES_BLOCK_SIZE *
+			(1 + rand() % (sizeof(ptext) / AES_BLOCK_SIZE));
 		int outl, res;
 
 		rand_bytes(key, sizeof(key));
Signature · 1 line
2.40.0
author maintainer
#2 lore raw

PATCH 2/3 fscrypt-crypt-util: use OpenSSL EVP API for AES self-tests

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

OpenSSL 3.0 has deprecated the easy-to-use AES block cipher API in favor of EVP. EVP is also available in earlier OpenSSL versions. Therefore, update test_aes_keysize() to use the non-deprecated API to avoid deprecation warnings when building the algorithm self-tests.

---
 src/fscrypt-crypt-util.c | 32 +++++++++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 5 deletions(-)
src/fscrypt-crypt-util.c +27 -5
index 4bb4f4e5..040b80c0 100644
@@ -662,19 +662,37 @@ static void aes_decrypt(const struct aes_key *k, const u8 src[AES_BLOCK_SIZE],
 }
 
 #ifdef ENABLE_ALG_TESTS
-#include <openssl/aes.h>
+#include <openssl/evp.h>
 static void test_aes_keysize(int keysize)
 {
 	unsigned long num_tests = NUM_ALG_TEST_ITERATIONS;
+	const EVP_CIPHER *evp_cipher;
+	EVP_CIPHER_CTX *ctx;
+
+	switch (keysize) {
+	case 16:
+		evp_cipher = EVP_aes_128_ecb();
+		break;
+	case 24:
+		evp_cipher = EVP_aes_192_ecb();
+		break;
+	case 32:
+		evp_cipher = EVP_aes_256_ecb();
+		break;
+	default:
+		ASSERT(0);
+	}
 
+	ctx = EVP_CIPHER_CTX_new();
+	ASSERT(ctx != NULL);
 	while (num_tests--) {
 		struct aes_key k;
-		AES_KEY ref_k;
 		u8 key[AES_256_KEY_SIZE];
 		u8 ptext[AES_BLOCK_SIZE];
 		u8 ctext[AES_BLOCK_SIZE];
 		u8 ref_ctext[AES_BLOCK_SIZE];
 		u8 decrypted[AES_BLOCK_SIZE];
+		int outl, res;
 
 		rand_bytes(key, keysize);
 		rand_bytes(ptext, AES_BLOCK_SIZE);
@@ -682,14 +700,18 @@ static void test_aes_keysize(int keysize)
 		aes_setkey(&k, key, keysize);
 		aes_encrypt(&k, ptext, ctext);
 
-		ASSERT(AES_set_encrypt_key(key, keysize*8, &ref_k) == 0);
-		AES_encrypt(ptext, ref_ctext, &ref_k);
-
+		res = EVP_EncryptInit_ex(ctx, evp_cipher, NULL, key, NULL);
+		ASSERT(res > 0);
+		res = EVP_EncryptUpdate(ctx, ref_ctext, &outl, ptext,
+					AES_BLOCK_SIZE);
+		ASSERT(res > 0);
+		ASSERT(outl == AES_BLOCK_SIZE);
 		ASSERT(memcmp(ctext, ref_ctext, AES_BLOCK_SIZE) == 0);
 
 		aes_decrypt(&k, ctext, decrypted);
 		ASSERT(memcmp(ptext, decrypted, AES_BLOCK_SIZE) == 0);
 	}
+	EVP_CIPHER_CTX_free(ctx);
 }
 
 static void test_aes(void)
Signature · 1 line
2.40.0
author maintainer
#1 lore raw

PATCH 1/3 fscrypt-crypt-util: fix HKDF self-test with latest OpenSSL

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

In OpenSSL 3.0, EVP_PKEY_derive() fails if the output is zero-length. Therefore, update test_hkdf_sha512() to not test this case.

This only affects the algorithm self-tests within fscrypt-crypt-util, which are not compiled by default.

---
 src/fscrypt-crypt-util.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/fscrypt-crypt-util.c +3 -1
index 087ae09a..4bb4f4e5 100644
@@ -975,7 +975,9 @@ static void test_hkdf_sha512(void)
 		size_t ikmlen = 1 + (rand() % sizeof(ikm));
 		size_t saltlen = rand() % (1 + sizeof(salt));
 		size_t infolen = rand() % (1 + sizeof(info));
-		size_t outlen = rand() % (1 + sizeof(actual_output));
+		// Don't test zero-length outputs, since OpenSSL 3.0 and later
+		// returns an error for those.
+		size_t outlen = 1 + (rand() % sizeof(actual_output));
 
 		rand_bytes(ikm, ikmlen);
 		rand_bytes(salt, saltlen);
Signature · 1 line
2.40.0

Eric Biggers

author maintainer
#0 lore raw

cover xfstests: make fscrypt-crypt-util self-tests work with OpenSSL 3.0

This series makes the algorithm self-tests in fscrypt-crypt-util (which are not compiled by default) work with OpenSSL 3.0. Previously they only worked with OpenSSL 1.1.

Eric Biggers (3):
fscrypt-crypt-util: fix HKDF self-test with latest OpenSSL
fscrypt-crypt-util: use OpenSSL EVP API for AES self-tests
fscrypt-crypt-util: fix XTS self-test with latest OpenSSL

 src/fscrypt-crypt-util.c | 46 ++++++++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 9 deletions(-)
Signature · 1 line
2.40.0
#4 lore raw
Quoted 4 lines · Eric Biggers

This series makes the algorithm self-tests in fscrypt-crypt-util (which are not compiled by default) work with OpenSSL 3.0. Previously they only worked with OpenSSL 1.1.

Quoted 9 lines

Eric Biggers (3):
fscrypt-crypt-util: fix HKDF self-test with latest OpenSSL
fscrypt-crypt-util: use OpenSSL EVP API for AES self-tests
fscrypt-crypt-util: fix XTS self-test with latest OpenSSL

 src/fscrypt-crypt-util.c | 46 ++++++++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 9 deletions(-)

I'm not familiar with fscrypt changes, but from the commit log of 3 patches, this patchset looks good to me. If there's not more review points from linux-fscrypt@, I'll merge this patchset.

Just one tiny review point, I'd like to keep using same comment format, especially in same source file. As src/fscrypt-crypt-util.c generally use "/* ... */", so I'll change your "//..." to "/* ... */" when I merge it, if you don't mind.

Thanks,
Zorro

Quoted 2 lines

2.40.0

author maintainer
#5 lore raw
Quoted 3 lines · Zorro Lang

Just one tiny review point, I'd like to keep using same comment format, especially in same source file. As src/fscrypt-crypt-util.c generally use "/* ... */", so I'll change your "//..." to "/* ... */" when I merge it, if you don't mind.

Yes that's fine.

- Eric