Skip to contentSkip to conversation

crypto: qce - Fix crypto self-test failures

PATCH Superseded Bartosz Golaszewski 4 participants 15 messages Jul 17 – Jul 30

Subsystems (4)

v6 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v6 cover crypto: qce - Fix crypto self-test failures

This extends the initial submission from Kuldeep.

The QCE hardware crypto engine has several limitations that cause it to produce incorrect results or stall on certain inputs. This series fixes several bugs and adds workaround allowing the deiver to pass crypto self-tests.

The failures addressed are:

- HMAC self-test failures for empty messages
- AES-XTS returning success on zero-length input (should be -EINVAL)
- AES-CTR: partial final block causes the engine to stall, output IV derivation was incorrect
- AES-XTS with key1 == key2 is not supported by the CE
- AES-CCM: partial final block and fragmented payload both stall the engine

All fixes were tested on an SM8650 QRD board with CONFIG_CRYPTO_SELFTESTS=y and CONFIG_CRYPTO_SELFTESTS_FULL=y.

---
Changes in v6:
- Handle all zero-length HMAC finalizations (like imported state with
data already hashed), not only the genuinely empty message case
- Add an additional patch addressing the fragmented skcipher payload
issue
- Link to v5: https://patch.msgid.link/[email protected]

Changes in v5:
- Dropped patch 1/8 that's already queued
- Use the pre-allocated fallback ahash for HMAC transforms (Herbert)
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- Remove remaining ECB and DES3 bits
- Pick up tags
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Remove even more algorithms and dead code in patch 1/8
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Add fixes for the full suite of crypto self-tests
- Add Fixes and Cc tags
- Link to v1: https://patch.msgid.link/[email protected]/

---
Bartosz Golaszewski (6):
crypto: qce - Fix HMAC self-test failures for empty messages
crypto: qce - Reject empty messages for AES-XTS
crypto: qce - Use a fallback for AES-CTR with a partial final block
crypto: qce - Use fallback for fragmented skcipher payloads
crypto: qce - Use a fallback for CCM with a partial final block
crypto: qce - Use fallback for CCM with a fragmented payload

Kuldeep Singh (2):
crypto: qce - Fix CTR-AES for partial block requests
crypto: qce - Fix xts-aes-qce for weak keys

 drivers/crypto/qce/aead.c     | 32 +++++++++++++++++++++++-
 drivers/crypto/qce/cipher.h   |  1 +
 drivers/crypto/qce/common.h   |  1 -
 drivers/crypto/qce/sha.c      | 58 ++++++++++++++++++++++++++++++++-----------
 drivers/crypto/qce/skcipher.c | 50 ++++++++++++++++++++++++++++++-------
 5 files changed, 116 insertions(+), 26 deletions(-)

---
base-commit: f6fa674bd75f9ed0e74f4ca85f88a6e461b5f230
change-id: 20260610-qce-fix-self-tests-492ffd2ef955

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
#1 lore raw

PATCH v6 1/8 crypto: qce - Fix HMAC self-test failures for empty messages

BAM DMA cannot process zero-length transfers, so the driver always holds back at least one byte to submit to the engine and only ever finalizes with an empty buffer when nothing is left to submit. For plain hashes this was handled by returning the precomputed hash of the empty message (tmpl->hash_zero), but HMAC's result depends on the key and cannot be constant, so hmac(sha256) produced an incorrect digest for an empty message and the crypto self-tests failed.

A zero pending buffer at finalization time does not necessarily mean the message itself is empty, though: the caller can also reach it by importing a state that already reflects some hashed data with nothing currently buffered (crypto_ahash_import() followed directly by finalization). Special-casing only a genuinely empty message would silently compute the wrong result for an imported state in that scenario.

Handle every zero-length finalization consistently through the software fallback ahash instead. When nothing has been processed yet, let the fallback compute the result from scratch using the key already propagated to it via setkey(). Otherwise, reconstruct the fallback's running hash state from the state kept by the driver and finalize from there, accounting for the HMAC ipad block that the engine absorbs internally and that never goes through update().

Fixes ec8f5d8f6f76 ("crypto: qce - Qualcomm crypto engine driver")
---
 drivers/crypto/qce/common.h |  1 -
 drivers/crypto/qce/sha.c    | 58 +++++++++++++++++++++++++++++++++------------
 2 files changed, 43 insertions(+), 16 deletions(-)
drivers/crypto/qce/common.h +0 -1
index 9cd2e6ed8bbb0f76e24be187d8ae7e2fe2f7b932..587d349da91d1faa2bed7cd488306d4358467b2d 100644
@@ -82,7 +82,6 @@ struct qce_alg_template {
 		struct aead_alg aead;
 	} alg;
 	struct qce_device *qce;
-	const u8 *hash_zero;
 	const u32 digest_size;
 };
 
drivers/crypto/qce/sha.c +43 -15

82 lines not on this page · raw message

Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH v6 1/8] crypto: qce - Fix HMAC self-test failures for empty messages

drivers/crypto/qce/sha.c static int qce_ahash_update(struct ahash_request *req) · line 264

Quoted 13 lines · Bartosz Golaszewski
drivers/crypto/qce/sha.c +13 -0
+/*
+ * BAM DMA cannot handle zero-length transfers, so the driver always holds
+ * back at least one byte to submit to the engine. A zero rctx->buflen at
+ * finalization time does not necessarily mean the message is empty: the
+ * caller may have imported a state that already reflects some hashed data
+ * with nothing currently buffered. Handle both cases through the software
+ * fallback: reconstruct the running state when there is one instead of
+ * assuming the message is empty.
+ */
+static int qce_ahash_finalize_zero(struct ahash_request *req)
+{
+	struct qce_sha_reqctx *rctx = ahash_request_ctx_dma(req);
+	HASH_FBREQ_ON_STACK(fbreq, req);

I'm trying to understand how this is intended to work. This uses crypto_tfm::fb. But this driver doesn't set CRYPTO_ALG_NEED_FALLBACK, so crypto_tfm::fb is actually just a pointer to the same crypto_tfm. (See crypto_create_tfm_node() and crypto_ahash_init_tfm().) Doesn't this just result in an infinite recursive loop and a crash?

- Eric

maintainer
#3 lore raw

Re: [PATCH v6 1/8] crypto: qce - Fix HMAC self-test failures for empty messages

Quoted 6 lines · Eric Biggers

I'm trying to understand how this is intended to work. This uses crypto_tfm::fb. But this driver doesn't set CRYPTO_ALG_NEED_FALLBACK, so crypto_tfm::fb is actually just a pointer to the same crypto_tfm. (See crypto_create_tfm_node() and crypto_ahash_init_tfm().) Doesn't this just result in an infinite recursive loop and a crash?

Every async driver gets a fallback by default, unless you set the bit CRYPTO_ALG_NO_FALLBACK or you declare that you support CRYPTO_ALG_REQ_VIRT (which no driver does).

Cheers,
--
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

#4 lore raw

Re: [PATCH v6 1/8] crypto: qce - Fix HMAC self-test failures for empty messages

Quoted 8 lines · Herbert Xu
Quoted 6 lines · Eric Biggers

I'm trying to understand how this is intended to work. This uses crypto_tfm::fb. But this driver doesn't set CRYPTO_ALG_NEED_FALLBACK, so crypto_tfm::fb is actually just a pointer to the same crypto_tfm. (See crypto_create_tfm_node() and crypto_ahash_init_tfm().) Doesn't this just result in an infinite recursive loop and a crash?

Quoted 4 lines

Every async driver gets a fallback by default, unless you set the bit CRYPTO_ALG_NO_FALLBACK or you declare that you support CRYPTO_ALG_REQ_VIRT (which no driver does).

Herbert,

Is there anything else I need to do for this series to get picked up?

Thanks,
Bartosz

#5 lore raw

PATCH v6 2/8 crypto: qce - Reject empty messages for AES-XTS

In reply to Bartosz Golaszewski

XTS is not defined for an empty plaintext: it requires at least one full block of data. The driver treated a zero-length request as a successful no-op, so the crypto self-tests "unexpectedly succeeded" when -EINVAL was expected.

Return -EINVAL for empty XTS requests while keeping the no-op behavior for the other ciphers, which the crypto engine simply cannot process due to its DMA not supporting zero-length transfers.

Fixes f08789462255 ("crypto: qce - Return error for zero length messages")
---
 drivers/crypto/qce/skcipher.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/crypto/qce/skcipher.c +5 -1
index b27008ace93a8a40c291d564c3fb9d73df5447ec..e1f69057607fac36e8b4bdb5dd9e62a2aabe5f50 100644
@@ -223,8 +223,12 @@ static int qce_skcipher_crypt(struct skcipher_request *req, int encrypt)
 	keylen = IS_XTS(rctx->flags) ? ctx->enc_keylen >> 1 : ctx->enc_keylen;
 
 	/* CE does not handle 0 length messages */
-	if (!req->cryptlen)
+	if (!req->cryptlen) {
+		/* XTS requires at least one full block of data */
+		if (IS_XTS(rctx->flags))
+			return -EINVAL;
 		return 0;
+	}
 
 	/*
 	 * ECB and CBC algorithms require message lengths to be
Signature · 1 line
2.47.3
#6 lore raw

PATCH v6 3/8 crypto: qce - Fix CTR-AES for partial block requests

In reply to Bartosz Golaszewski

From: Kuldeep Singh <[email protected]>

In CTR mode, the IV acts as the initial counter block.
APer NIST SP 800-38A, after a CTR mode operation the next unused counter value is:

IV_next = IV_in + ceil(cryptlen / AES_BLOCK_SIZE)

The skcipher requires req->iv to hold this updated counter on completion, ensuring chained requests produce correct results.

Referring to Crypto6.0 documentation, Section 2.2.5 says:
"The count value increments automatically once per block of data (in AES, a block is 16 bytes) based on the value in the CRYPTO_ENCR_CNTR_MASK registers."

QCE increments internal counter register once per full 16-byte block(for ctr-aes) is processed. In case of partial request length, the hardware uses the current counter to generate keystreams but does not increment the counter register afterwards. So the counter value written in CRYPTO_ENCR_CNTRn_IVn later once read by software is one less than the expected value.

Crypto selftest framework capture this scenario with test vector 4 comprising of a 499-byte payload (31 full blocks + 3 partial bytes). Error:

[    5.606169] alg: skcipher: ctr-aes-qce encryption test failed (wrong output IV) on test vector 4, cfg="in-place (one sglist)"
[    5.606176] 00000000: e7 82 1d b8 53 11 ac 47 e2 7d 18 d6 71 0c a7 61
[    5.606192] alg: self-tests for ctr(aes) using ctr-aes-qce failed (rc=-22)

Expected iv_out: 0x62 (iv_in + 32)
Obtained iv_out: 0x61 (iv_in + 31, partial block not counted)

To fix this, just increase the counter value for partial block requests by 1 and for the full block size requests, don't take any action as expected value is already returned by the hardware.

Fixes 3e806a12d10a ("crypto: qce - update the skcipher IV")
---
 drivers/crypto/qce/skcipher.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/crypto/qce/skcipher.c +16 -1
index e1f69057607fac36e8b4bdb5dd9e62a2aabe5f50..35ddbe03cfcd75db7599a5754e4ff978f3528105 100644
@@ -33,6 +33,7 @@ static void qce_skcipher_done(void *data)
 	struct qce_device *qce = tmpl->qce;
 	struct qce_result_dump *result_buf = qce->dma.result_buf;
 	enum dma_data_direction dir_src, dir_dst;
+	unsigned int blocks;
 	u32 status;
 	int error;
 	bool diff_dst;
@@ -56,7 +57,21 @@ static void qce_skcipher_done(void *data)
 	if (error < 0)
 		dev_dbg(qce->dev, "skcipher operation error (%x)\n", status);
 
-	memcpy(rctx->iv, result_buf->encr_cntr_iv, rctx->ivsize);
+	if (IS_CTR(rctx->flags)) {
+		/*
+		 * QCE hardware does not increment the counter for a partial
+		 * final block. Increment it in software so that iv_out
+		 * reflects the correct next counter value expected by the CTR
+		 * mode.
+		 */
+		blocks = DIV_ROUND_UP(rctx->cryptlen, AES_BLOCK_SIZE);
+
+		while (blocks--)
+			crypto_inc(rctx->iv, rctx->ivsize);
+	} else {
+		memcpy(rctx->iv, result_buf->encr_cntr_iv, rctx->ivsize);
+	}
+
 	qce->async_req_done(tmpl->qce, error);
 }
 
Signature · 1 line
2.47.3
#7 lore raw

PATCH v6 4/8 crypto: qce - Use a fallback for AES-CTR with a partial final block

In reply to Bartosz Golaszewski

ctr(aes) is registered with a block size of 1, so the crypto API hands the driver requests whose length is not a multiple of the AES block size. The crypto engine, however, stalls waiting for a full block of input in that case, leaving the operation incomplete and failing the request (and the crypto self-tests) with a hardware operation error.

Route AES-CTR requests with a partial final block to the software fallback, which already handles the other cases the engine cannot.

Fixes bb5c863b3d3c ("crypto: qce - fix ctr-aes-qce block, chunk sizes")
---
 drivers/crypto/qce/skcipher.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/crypto/qce/skcipher.c +3 -0
index 35ddbe03cfcd75db7599a5754e4ff978f3528105..54ff013e24317cd4d7a0dcde88cef8268db784c9 100644
@@ -260,9 +260,12 @@ static int qce_skcipher_crypt(struct skcipher_request *req, int encrypt)
 	 * AES-XTS request with len > QCE_SECTOR_SIZE and
 	 * is not a multiple of it.(Revisit this condition to check if it is
 	 * needed in all versions of CE)
+	 * AES-CTR with a partial final block (the CE stalls waiting for a full
+	 * block of input).
 	 */
 	if (IS_AES(rctx->flags) &&
 	    ((keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_256) ||
+	    (IS_CTR(rctx->flags) && !IS_ALIGNED(req->cryptlen, AES_BLOCK_SIZE)) ||
 	    (IS_XTS(rctx->flags) && ((req->cryptlen <= aes_sw_max_len) ||
 	    (req->cryptlen > QCE_SECTOR_SIZE &&
 	    req->cryptlen % QCE_SECTOR_SIZE))))) {
Signature · 1 line
2.47.3
#8 lore raw

PATCH v6 5/8 crypto: qce - Use fallback for fragmented skcipher payloads

In reply to Bartosz Golaszewski

The crypto engine reliably processes AES requests only when the payload is a single contiguous buffer. A payload split across multiple scatterlist entries makes the engine stall waiting for input, failing the request with a hardware operation error. This was uncovered by the crypto self-tests, which feed the algorithms randomly fragmented buffers.

Detect a payload that spans more than one scatterlist entry, in either the source or the destination, and route the request to the software fallback.

Fixes 25b71d61d631 ("crypto: qce - Improve the conditions for requesting AES fallback cipher")
---
 drivers/crypto/qce/skcipher.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/crypto/qce/skcipher.c +5 -1
index 54ff013e24317cd4d7a0dcde88cef8268db784c9..4779218d4f33a07331c510abf0d6d23e7e70d102 100644
@@ -262,13 +262,17 @@ static int qce_skcipher_crypt(struct skcipher_request *req, int encrypt)
 	 * needed in all versions of CE)
 	 * AES-CTR with a partial final block (the CE stalls waiting for a full
 	 * block of input).
+	 * A payload fragmented across more than one scatterlist entry (the CE
+	 * stalls waiting for input in that case too).
 	 */
 	if (IS_AES(rctx->flags) &&
 	    ((keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_256) ||
 	    (IS_CTR(rctx->flags) && !IS_ALIGNED(req->cryptlen, AES_BLOCK_SIZE)) ||
 	    (IS_XTS(rctx->flags) && ((req->cryptlen <= aes_sw_max_len) ||
 	    (req->cryptlen > QCE_SECTOR_SIZE &&
-	    req->cryptlen % QCE_SECTOR_SIZE))))) {
+	    req->cryptlen % QCE_SECTOR_SIZE))) ||
+	    sg_nents_for_len(req->src, req->cryptlen) > 1 ||
+	    sg_nents_for_len(req->dst, req->cryptlen) > 1)) {
 		skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback);
 		skcipher_request_set_callback(&rctx->fallback_req,
 					      req->base.flags,
Signature · 1 line
2.47.3
#9 lore raw

PATCH v6 6/8 crypto: qce - Fix xts-aes-qce for weak keys

In reply to Bartosz Golaszewski

From: Kuldeep Singh <[email protected]>

The QCE hardware does not support AES XTS mode when key1 and key2 are equal. The driver was handling this by unconditionally rejecting the keys with -ENOKEY(-126), regardless of whether FIPS mode is active or the FORBID_WEAK_KEYS flag is set.

[    5.599170] alg: skcipher: xts-aes-qce setkey failed on test vector 0; expected_error=0, actual_error=-126, flags=0x1
[    5.599184] alg: self-tests for xts(aes) using xts-aes-qce failed (rc=-126)

In general for weak keys,
- If FIPS mode is active or FORBID_WEAK_KEYS is set: return -EINVAL.
- In non-FIPS mode, Accept the key and encrypt successfully.

Since QCE was returning -ENOKEY for non-FIPS mode whereas the expectation is to encrypt content and return success, the selftest saw a mismatch and failed.

There are two problems in QCE behavior:
* -ENOKEY is returned instead of -EINVAL for the FIPS/weak-key rejection case.
* key1 == key2 is rejected even in non-FIPS mode

Fix xts-aes-qce behavior by using generic helper xts_verify_key() to reject keys early with -EINVAL for FIPS mode active(or FORBID_WEAK_KEYS set). For non-FIPS mode, since QCE hardware cannot accept the keys, use software fallback mechanism to encrypt the data.

Fixes f0d078dd6c49 ("crypto: qce - Return unsupported if key1 and key 2 are same for AES XTS algorithm")
---
 drivers/crypto/qce/cipher.h   |  1 +
 drivers/crypto/qce/skcipher.c | 18 ++++++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)
drivers/crypto/qce/cipher.h +1 -0
index 850f257d00f3aca0397adc1f703aea690c754d60..daea07551118d444d2f749588bdfe2ae2c6c553f 100644
@@ -14,6 +14,7 @@
 struct qce_cipher_ctx {
 	u8 enc_key[QCE_MAX_KEY_SIZE];
 	unsigned int enc_keylen;
+	bool use_fallback;
 	struct crypto_skcipher *fallback;
 };
 
drivers/crypto/qce/skcipher.c +12 -6
index 4779218d4f33a07331c510abf0d6d23e7e70d102..3df583b1a56ec371e556fd00e7930d9fc4447ce0 100644
@@ -12,6 +12,7 @@
 #include <linux/errno.h>
 #include <crypto/aes.h>
 #include <crypto/internal/skcipher.h>
+#include <crypto/xts.h>
 
 #include "cipher.h"
 
@@ -194,14 +195,17 @@ static int qce_skcipher_setkey(struct crypto_skcipher *ablk, const u8 *key,
 	if (!key || !keylen)
 		return -EINVAL;
 
-	/*
-	 * AES XTS key1 = key2 not supported by crypto engine.
-	 * Revisit to request a fallback cipher in this case.
-	 */
 	if (IS_XTS(flags)) {
+		ret = xts_verify_key(ablk, key, keylen);
+		if (ret)
+			return ret;
 		__keylen = keylen >> 1;
-		if (!memcmp(key, key + __keylen, __keylen))
-			return -ENOKEY;
+		/*
+		 * QCE does not support key1 == key2 for XTS.
+		 * Use fallback cipher in this case.
+		 */
+		ctx->use_fallback = !crypto_memneq(key, key + __keylen,
+						       __keylen);
 	} else {
 		__keylen = keylen;
 	}
@@ -262,6 +266,7 @@ static int qce_skcipher_crypt(struct skcipher_request *req, int encrypt)
 	 * needed in all versions of CE)
 	 * AES-CTR with a partial final block (the CE stalls waiting for a full
 	 * block of input).
+	 * AES-XTS with key1 == key2 (not supported by the CE).
 	 * A payload fragmented across more than one scatterlist entry (the CE
 	 * stalls waiting for input in that case too).
 	 */
@@ -271,6 +276,7 @@ static int qce_skcipher_crypt(struct skcipher_request *req, int encrypt)
 	    (IS_XTS(rctx->flags) && ((req->cryptlen <= aes_sw_max_len) ||
 	    (req->cryptlen > QCE_SECTOR_SIZE &&
 	    req->cryptlen % QCE_SECTOR_SIZE))) ||
+	    (IS_XTS(rctx->flags) && ctx->use_fallback) ||
 	    sg_nents_for_len(req->src, req->cryptlen) > 1 ||
 	    sg_nents_for_len(req->dst, req->cryptlen) > 1)) {
 		skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback);
Signature · 1 line
2.47.3
#10 lore raw

PATCH v6 7/8 crypto: qce - Use a fallback for CCM with a partial final block

In reply to Bartosz Golaszewski

CCM builds on AES-CTR for encryption, and the crypto engine stalls on a partial final block just as it does for plain ctr(aes): a payload whose length is not a multiple of the AES block size leaves the operation incomplete and fails with a hardware operation error. This was caught by the ccm(aes) crypto self-tests.

Force the software fallback for CCM requests whose message length is not block aligned, reusing the driver's existing need_fallback mechanism.

Fixes 9363efb4181c ("crypto: qce - Add support for AEAD algorithms")
---
 drivers/crypto/qce/aead.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/crypto/qce/aead.c +8 -0
index 336614a11377e0be246817da584296124f4de5d8..4fa018204cb628c112f64c45ff6c7407df73b945 100644
@@ -514,6 +514,14 @@ static int qce_aead_crypt(struct aead_request *req, int encrypt)
 			ctx->need_fallback = true;
 	}
 
+	/*
+	 * CCM uses AES-CTR internally and the CE stalls on a partial final
+	 * block, so a payload that is not a multiple of the block size has to
+	 * be handled by the fallback.
+	 */
+	if (IS_CCM(rctx->flags) && !IS_ALIGNED(rctx->cryptlen, AES_BLOCK_SIZE))
+		ctx->need_fallback = true;
+
 	/* If fallback is needed, schedule and exit */
 	if (ctx->need_fallback) {
 		/* Reset need_fallback in case the same ctx is used for another transaction */
Signature · 1 line
2.47.3
#11 lore raw

Re: [PATCH v6 7/8] crypto: qce - Use a fallback for CCM with a partial final block

Quoted 25 lines · Bartosz Golaszewski

CCM builds on AES-CTR for encryption, and the crypto engine stalls on a partial final block just as it does for plain ctr(aes): a payload whose length is not a multiple of the AES block size leaves the operation incomplete and fails with a hardware operation error. This was caught by the ccm(aes) crypto self-tests.

Force the software fallback for CCM requests whose message length is not block aligned, reusing the driver's existing need_fallback mechanism.

Fixes 9363efb4181c ("crypto: qce - Add support for AEAD algorithms")
---
 drivers/crypto/qce/aead.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/crypto/qce/aead.c +0 -0
index 336614a11377e0be246817da584296124f4de5d8..4fa018204cb628c112f64c45ff6c7407df73b945 100644
@@ -514,6 +514,14 @@ static int qce_aead_crypt(struct aead_request *req, int encrypt)
 			ctx->need_fallback = true;
 	}
 

drivers/crypto/qce/aead.c static int qce_aead_crypt(struct aead_request *req, int encrypt) · line 526

Quoted 10 lines
drivers/crypto/qce/aead.c +8 -0
+	/*
+	 * CCM uses AES-CTR internally and the CE stalls on a partial final
+	 * block, so a payload that is not a multiple of the block size has to
+	 * be handled by the fallback.
+	 */
+	if (IS_CCM(rctx->flags) && !IS_ALIGNED(rctx->cryptlen, AES_BLOCK_SIZE))
+		ctx->need_fallback = true;
+
 	/* If fallback is needed, schedule and exit */
 	if (ctx->need_fallback) {

Here, the driver is storing per-request state ('need_fallback') in the transformation context, which is a shared structure. This can cause the driver to produce incorrect results if used for multiple concurrent requests with the same algorithm.

On the topic of CCM, there's also a memory leak and buffer overread in qce_aead_ccm_prepare_buf_assoclen(). It allocates a buffer of length ALIGN(assoclen,16)+6, then treats it as a buffer of length ALIGN(assoclen+6,16). That can be greater than the allocated length, for example if assoclen = 15. Then it never frees the buffer.

The allocation is also being done using GFP_ATOMIC, which is failure-prone. Users need crypto operations to be reliable.

This driver's CCM implementation also fails to validate that the message length is allowed for the specified nonce length.

The error handling in qce_aead_async_req_handle() is also incorrect: it calls dma_unmap_sg() on req->src instead of the actual mapped scatterlist rctx->src_sg, which differ when assoclen > 0. It also uses < 0 to check for failures from dma_map_sg(), when it actually returns an unsigned int and returns 0 on failure.

Note that the ARMv8 CE implementation of AES-CCM is much faster and does not have these issues.

- Eric

#12 lore raw

PATCH v6 8/8 crypto: qce - Use fallback for CCM with a fragmented payload

In reply to Bartosz Golaszewski

The crypto engine reliably processes CCM only when the message payload is a single contiguous buffer. The associated data is already linearized into a bounce buffer before being submitted, but when the payload itself is split across multiple scatterlist entries the engine stalls waiting for input and the request fails with a hardware operation error. This was uncovered by the crypto self-tests, which feed the algorithms randomly fragmented buffers.

Detect a payload that spans more than one scatterlist entry (in either the source or the destination, skipping past the associated data) and route the request to the software fallback.

Fixes 9363efb4181c ("crypto: qce - Add support for AEAD algorithms")
---
 drivers/crypto/qce/aead.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
drivers/crypto/qce/aead.c +23 -1
index 4fa018204cb628c112f64c45ff6c7407df73b945..9ff8fe2a7efcd2734e4ff029744961a7b1101013 100644
@@ -498,7 +498,8 @@ static int qce_aead_crypt(struct aead_request *req, int encrypt)
 	struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
 	struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
 	struct qce_alg_template *tmpl = to_aead_tmpl(tfm);
-	unsigned int blocksize = crypto_aead_blocksize(tfm);
+	unsigned int blocksize = crypto_aead_blocksize(tfm), authsize;
+	struct scatterlist __sg[2], *msg_sg;
 
 	rctx->flags  = tmpl->alg_flags;
 	rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
@@ -522,6 +523,27 @@ static int qce_aead_crypt(struct aead_request *req, int encrypt)
 	if (IS_CCM(rctx->flags) && !IS_ALIGNED(rctx->cryptlen, AES_BLOCK_SIZE))
 		ctx->need_fallback = true;
 
+	/*
+	 * The CE reliably processes CCM only when the message payload is a
+	 * single contiguous buffer. The associated data is linearized into a
+	 * bounce buffer before being handed to the engine, but a fragmented
+	 * payload makes the engine stall waiting for input, so route those
+	 * requests to the fallback.
+	 */
+	if (IS_CCM(rctx->flags) && rctx->cryptlen) {
+		authsize = ctx->authsize;
+
+		msg_sg = scatterwalk_ffwd(__sg, req->src, req->assoclen);
+		if (sg_nents_for_len(msg_sg, rctx->cryptlen +
+				     (encrypt ? 0 : authsize)) > 1)
+			ctx->need_fallback = true;
+
+		msg_sg = scatterwalk_ffwd(__sg, req->dst, req->assoclen);
+		if (sg_nents_for_len(msg_sg, rctx->cryptlen +
+				     (encrypt ? authsize : 0)) > 1)
+			ctx->need_fallback = true;
+	}
+
 	/* If fallback is needed, schedule and exit */
 	if (ctx->need_fallback) {
 		/* Reset need_fallback in case the same ctx is used for another transaction */
Signature · 1 line
2.47.3
#13 lore raw
In reply to Bartosz Golaszewski
Quoted 8 lines · Bartosz Golaszewski

This extends the initial submission from Kuldeep.

The QCE hardware crypto engine has several limitations that cause it to produce incorrect results or stall on certain inputs. This series fixes several bugs and adds workaround allowing the deiver to pass crypto self-tests.

The failures addressed are:

I was also looking at how the request queueing works in this driver. qce_handle_queue() gets invoked synchronously from almost all the ahash/skcipher/aead API entry points.

However, it takes a mutex, which can sleep.

Not a great choice when these APIs can be called in softirq context.

I strongly suspect this would crash if anyone actually tried to use this with IPsec, for example.

This is interesting when we consider that the driver implements the "rfc4309(ccm(aes))" algorithm, whose exclusive purpose is IPsec.

I'm not sure if there are some out of tree patches going on, or if this was just never actually tested with what it was theoretically supposed to have been useful for. But I'd guess the latter.

- Eric

#14 lore raw
Quoted 9 lines · Eric Biggers
Quoted 8 lines · Bartosz Golaszewski

This extends the initial submission from Kuldeep.

The QCE hardware crypto engine has several limitations that cause it to produce incorrect results or stall on certain inputs. This series fixes several bugs and adds workaround allowing the deiver to pass crypto self-tests.

The failures addressed are:

I also noticed that qce_aead_done() compares MACs using memcmp() instead of crypto_memneq(). That makes it vulnerable to timing side-channel attacks (https://en.wikipedia.org/wiki/Timing_attack).

This is yet another thing that is inconsistent with claims that this driver improves security.

Of course, the software implementation does not have this problem.

- Eric