From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796263677; b=H9FKY4sLZgm+c12PbNRhTGX2noLGW2OzZkvkFWdO7JyDlqvhjbhzqm1xEzsEMIJ1C9/h /LUF6RnwrU1CbayuFPbHc2PFJEx3dLypz5sbwxJ9tS/ReZ5ePUj1LW2VYocLLyIPcGZPJ 9Ss+T3Y2zEvYxSRTuUdb9upNG/lEd7Pb/6CFNah70InBlJ2fZFjSo893HHOEqOVsDrGu9 ZlE7ABQSa/rxEhw694OZHXs6kGMXyFAHf5aS8NKmptCHo+oIh3r8KGHYYUqJ8azwP1VeH KsB8ceI7vpP0iNbadIXiemb+S9EIKUQAn7YKgbcmRbNCQwWUL8JV/R+Ciov2o0twHQw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796263677; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=8oj67iPLPRECRKeiR2L3HdTYyoURJYpRfsbWMGt6zDo=; b=Jk13nWixZWXsjXsMAOGs7wwwPpiu+Z5FyL2nr4hSD4bS8pMSq8furP0iGT5jFcDdseko L6HQpNSsfaTnfzhF7jTEpDD8y6H8iVRWxrQEOkx2cDy161YOovyxIBsuDaNnShlQVEDxF 8apeuE5bDZC1TFiMfi2EVKir/RjpZrMTAna1O2XiFp1+1F5zWjkjcF+uegJuS3YZACgDq SqXUY2fFGho7PXZikfCgHjj0E92DdK/rEsucCOR+dXRaSfpqTxVOfIy2QUEhVU/3HZ86c JzfLREZVBJPiOjHnwt7cKp9eAaaI+tzK1yzhPkOAsES4Mh5oqbUJmbws6O4OMfRa3pQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=FjHxyHLP; arc=none smtp.remote-ip=209.85.214.181 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC399606A33D for ; Thu, 3 Dec 2026 02:07:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DC399606A33D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=FjHxyHLP Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2f052ae3c86so75915505ad.1 for ; Wed, 02 Dec 2026 18:07:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796263676; x=1796868476; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=8oj67iPLPRECRKeiR2L3HdTYyoURJYpRfsbWMGt6zDo=; b=FjHxyHLPiiJZiILgXYfebi93wPH/JzLBSAP+ANQbxi9Tn7STsuukdVYQsy/LtIcE5e ObWs2ZLVc3DpN2qndwiT3nJPaComxbEWo0zHzslOM6XNbP63w3lv0MaY0fgQI8RQ8OyB Qmwb9i6+FuWcmhdJlTIqJp75k2lnNFGHTSVFCmFdx1mn+PO70sYJx4WvYJHQleqV+3zO DiqljbxiZ1sq5tUk0gac59vMK8uSTRaY8KuTc+Iguv1IEHoeUwLgJ1uwm79Cs7PYx/6H 22EQVENcLpv0p/hXjO/qqJe5BTQ1XKcmaXy1WpOFxRYJoQ9fc5pvfYU+Hewr4wHO2Sl8 okBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796263676; x=1796868476; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=8oj67iPLPRECRKeiR2L3HdTYyoURJYpRfsbWMGt6zDo=; b=OPb46dVoGIl9/t6f6UUHN4X5b7eYo1hVg06UU1B1gFHMTHLkFHNHqb02OJUEs4evx8 +xQayxgxwYlc1dmcaBsgAeq7lzQd4+yVtFv03GFc9MUdWeVv5wemH/eIWgSqeP/icb99 FijI4rkfWS6gAwL+dINdZjSHwGck5u8GWyKYLQ14IFW2JjQnnsjHNkStRi/kRMhW60xe RKdwZKePRu/AqoOLHY3+Ovcad7OVAmqaG7JigLF6kwodsn5HA+/Hai4DrLmXWiiub4Vb 38Lb53pbE6VaTgq7KGFMg2d98Bj18J1QBrcqAe7frSry3CBsOTDP4DGr9PxqjprQ+GKk 0X3Q== X-Gm-Message-State: APuU4Z96r4kuc6M7AJK0Kt/oluU0e89QoWejh8MnDfEM6Tdusq1ETspm QiZbN/bstCKWJDWBoVpzLbNCGvnhOPqoP2XV/kreQJjcxJNAAMm5Jlsj X-Gm-Gg: AZFdTZdqqnwJOj7Idg+uhX0dU/bu8rRT56h0t50TKaITVEvE3sMXntomJisCTbp47bu pgsErNubmCPwrfyOUBSDfrao9d+x7Bn/yU6TXy+ivjhrxkf5y8yRImAfwD+hL3bIQqYcQiD7E+l pjWgAEps0+SYORlv9iJ5hsQK42V0cQtPC4WaGXFgkQauYDwrJxXhcAM44UoNqCrSFxWCRldkceh 8FACM6zbAduZeWfA7t+yMg16TI8oerWMXhP+/E3mOMRkYN8jsMU3hb0i+jGvb1F8K7RfhzmN6t2 EL9QqUAhDa671f4E2R6J0/vRB2hIqLBuXhwkZc4MdIZsqPoEkCOFtCTGMDXDLXkhRFpvBTstWzy XFGv9lFsMLlA54wf47S08tUlNvCQmXeXdF2B7bFLHUnqWg4Qjy0IkBehFD7BkRhvO7ZJqpTfg1U HKRB61NS/CZqvfYtGZeui5Bi0HPz6stYJHW9/jDXoZ0ZF1CSfxhtt67nkfL95wIXRA+kpF+a8t/ z1jbvxwz9YIE8eTkW6wK/xPHUr52jCbM00lvn6fBc8W5nXT4zCT8hvjwwmXen7a4MkeyXNeaYYf Fi80dPdtjMKgjPdyV2ab/UQmM4zpGmTUSuKmeTf8fd5xLrI5HzVXrvWYzJg5o/WWmPGSO3HYJER 2sMPdazlt2UX3PYCEzJtaJ4wHGl07qNisTG9S43kjjmrIxbpaegT78rbhLEDtlzoRi65diHpQq2 IPag== X-Received: by 2002:a17:902:eccf:b0:2f0:25ea:8fa8 with SMTP id d9443c01a7336-2f03f176abamr458840485ad.19.1796263675992; Wed, 02 Dec 2026 18:07:55 -0800 (PST) Received: from ryzen.lan ([2601:644:8000:37f::e34]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f0b58fce71sm157752485ad.45.2026.12.02.18.07.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 18:07:55 -0800 (PST) From: Rosen Penev To: Srujana Challa , Bharat Bhushan , Herbert Xu , "David S. Miller" , Rosen Penev , Boris Brezillon , Arnaud Ebalard , Russell King , Romain Perier , linux-crypto@vger.kernel.org Cc: stable@vger.kernel.org Subject: [PATCH v2 4/8] crypto: cesa - finish the HMAC outer hash when padding by hand Date: Wed, 2 Dec 2026 18:07:29 -0800 Message-ID: <20261203020733.260995-5-rosenp@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203020733.260995-1-rosenp@gmail.com> References: <20261203020733.260995-1-rosenp@gmail.com> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The engine applies the HMAC outer hash only when it finalises the message itself, i.e. on a last or unfragmented operation that has data and whose total length fits the descriptor field (0xffff). Otherwise the driver pads the message by hand and runs the padding as a middle fragment, which leaves the engine with a plain inner hash state. For HMAC that is wrong twice over: - the padding encodes the message length only, while the inner hash also covers the ipad block, and - the outer hash is never computed, so the result read back from the IVDIG registers is H(ipad || m) instead of HMAC(k, m). This hits every HMAC whose last request has no data left to hash, i.e. any update()/final() sequence over a block-aligned length and any finup() or digest() of an empty message, and every HMAC of 64 KiB or more. Count the ipad block in the manual padding of HMAC requests, and when the result comes from the IVDIG registers on the last request of an HMAC, finish the outer hash in software: import the opad state computed at setkey time into a software hash, with one block already counted, and hash the inner digest. Each HMAC transform allocates that software hash at cra_init(). Select the software hashes so they are always available. Verified on an Armada 385 (WRT3200ACM) against the software HMACs, single threaded with random lengths and update splits: about 0.5% of HMAC-SHA1 and HMAC-SHA256 digests were wrong before, all of them with a block-aligned total length, and none of about 195000 after, nor of 30000 more with lengths up to 140000 bytes. HMAC-MD5 was not tested: the test kernel has no software MD5. Fixes: f63601fd616a ("crypto: marvell/cesa - add a new driver for Marvell's CESA") Cc: stable@vger.kernel.org Signed-off-by: Rosen Penev --- drivers/crypto/marvell/Kconfig | 3 + drivers/crypto/marvell/cesa/cesa.h | 9 +++ drivers/crypto/marvell/cesa/hash.c | 113 +++++++++++++++++++++++++++-- 3 files changed, 119 insertions(+), 6 deletions(-) diff --git a/drivers/crypto/marvell/Kconfig b/drivers/crypto/marvell/Kconfig index aa269abb0499..965061abec3c 100644 --- a/drivers/crypto/marvell/Kconfig +++ b/drivers/crypto/marvell/Kconfig @@ -12,6 +12,9 @@ config CRYPTO_DEV_MARVELL_CESA select CRYPTO_LIB_DES select CRYPTO_SKCIPHER select CRYPTO_HASH + select CRYPTO_MD5 + select CRYPTO_SHA1 + select CRYPTO_SHA256 select SRAM select CRYPTO_DEV_MARVELL help diff --git a/drivers/crypto/marvell/cesa/cesa.h b/drivers/crypto/marvell/cesa/cesa.h index 44351b252861..906d55fb6a3a 100644 --- a/drivers/crypto/marvell/cesa/cesa.h +++ b/drivers/crypto/marvell/cesa/cesa.h @@ -514,12 +514,15 @@ struct mv_cesa_hash_ctx { * struct mv_cesa_hash_ctx - CESA hmac operation context * @base: base context structure * @iv: initialization vectors + * @outer: software hash used to finish the outer hash when the engine + * could not finalise the message itself * * HMAC context structure. */ struct mv_cesa_hmac_ctx { struct mv_cesa_ctx base; __be32 iv[16]; + struct crypto_shash *outer; }; /** @@ -609,6 +612,10 @@ struct mv_cesa_ahash_dma_req { * @src_nents: number of entries in the scatterlist * @last_req: define whether the current operation is the last one * or not + * @hmac: HMAC request: manual padding must account for the ipad + * block and the outer hash is finished in software + * @sw_outer: the driver padded the last request by hand, so the + * engine did not apply the HMAC outer hash * @state: hash state */ struct mv_cesa_ahash_req { @@ -624,6 +631,8 @@ struct mv_cesa_ahash_req { int src_nents; bool last_req; bool algo_le; + bool hmac; + bool sw_outer; u32 state[8]; }; diff --git a/drivers/crypto/marvell/cesa/hash.c b/drivers/crypto/marvell/cesa/hash.c index 9fd3d5c32312..f6894ed7c1cc 100644 --- a/drivers/crypto/marvell/cesa/hash.c +++ b/drivers/crypto/marvell/cesa/hash.c @@ -140,6 +140,14 @@ static int mv_cesa_ahash_pad_len(struct mv_cesa_ahash_req *creq) static int mv_cesa_ahash_pad_req(struct mv_cesa_ahash_req *creq, u8 *buf) { unsigned int padlen; + u64 len = creq->len; + + /* The HMAC inner hash also covers the ipad block. */ + if (creq->hmac) + len += CESA_MAX_HASH_BLOCK_SIZE; + + /* The engine will not apply the HMAC outer hash to this. */ + creq->sw_outer = true; buf[0] = 0x80; /* Pad out to 56 mod 64 */ @@ -147,11 +155,11 @@ static int mv_cesa_ahash_pad_req(struct mv_cesa_ahash_req *creq, u8 *buf) memset(buf + 1, 0, padlen - 1); if (creq->algo_le) { - __le64 bits = cpu_to_le64(creq->len << 3); + __le64 bits = cpu_to_le64(len << 3); memcpy(buf + padlen, &bits, sizeof(bits)); } else { - __be64 bits = cpu_to_be64(creq->len << 3); + __be64 bits = cpu_to_be64(len << 3); memcpy(buf + padlen, &bits, sizeof(bits)); } @@ -353,6 +361,59 @@ static int mv_cesa_ahash_process(struct crypto_async_request *req, u32 status) return mv_cesa_ahash_std_process(ahashreq, status); } +/* + * The engine only applies the HMAC outer hash when it finalises the message + * itself. When the driver had to pad by hand instead (no data left for the + * last fragment, or a message longer than the engine's total length field), + * the IVDIG registers hold the inner digest. Finish the outer hash in + * software, starting from the opad state computed at setkey time. + */ +static void mv_cesa_ahmac_outer(struct ahash_request *req) +{ + struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm); + unsigned int digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req)); + SHASH_DESC_ON_STACK(desc, ctx->outer); + u8 inner[SHA256_DIGEST_SIZE]; + union { + struct md5_state md5; + struct sha1_state sha1; + struct sha256_state sha256; + } st = {}; + u32 *state; + u64 *count; + int i, ret; + + switch (digsize) { + case MD5_DIGEST_SIZE: + state = st.md5.hash; + count = &st.md5.byte_count; + break; + case SHA1_DIGEST_SIZE: + state = st.sha1.state; + count = &st.sha1.count; + break; + default: + state = st.sha256.state; + count = &st.sha256.count; + break; + } + + for (i = 0; i < digsize / 4; i++) + state[i] = be32_to_cpu(ctx->iv[8 + i]); + *count = CESA_MAX_HASH_BLOCK_SIZE; + + memcpy(inner, req->result, digsize); + desc->tfm = ctx->outer; + ret = crypto_shash_import_core(desc, &st); + if (!ret) + ret = crypto_shash_finup(desc, inner, digsize, req->result); + WARN_ON_ONCE(ret); + + shash_desc_zero(desc); + memzero_explicit(inner, sizeof(inner)); + memzero_explicit(&st, sizeof(st)); +} + static void mv_cesa_ahash_complete(struct crypto_async_request *req) { struct ahash_request *ahashreq = ahash_request_cast(req); @@ -394,6 +455,9 @@ static void mv_cesa_ahash_complete(struct crypto_async_request *req) for (i = 0; i < digsize / 4; i++) result[i] = cpu_to_be32(creq->state[i]); } + + if (creq->hmac && creq->sw_outer) + mv_cesa_ahmac_outer(ahashreq); } } } @@ -452,6 +516,14 @@ static void mv_cesa_ahash_init(struct ahash_request *req, creq->op_tmpl = *tmpl; creq->len = 0; creq->algo_le = algo_le; + + switch (mv_cesa_get_op_cfg(tmpl) & CESA_SA_DESC_CFG_MACM_MSK) { + case CESA_SA_DESC_CFG_MACM_HMAC_MD5: + case CESA_SA_DESC_CFG_MACM_HMAC_SHA1: + case CESA_SA_DESC_CFG_MACM_HMAC_SHA256: + creq->hmac = true; + break; + } } static inline int mv_cesa_ahash_cra_init(struct crypto_tfm *tfm) @@ -1242,10 +1314,14 @@ static int mv_cesa_ahmac_setkey(const char *hash_alg_name, return ret; } -static int mv_cesa_ahmac_cra_init(struct crypto_tfm *tfm) +static int mv_cesa_ahmac_cra_init(struct crypto_tfm *tfm, const char *hash) { struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(tfm); + ctx->outer = crypto_alloc_shash(hash, 0, 0); + if (IS_ERR(ctx->outer)) + return PTR_ERR(ctx->outer); + ctx->base.ops = &mv_cesa_ahash_req_ops; crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), @@ -1253,6 +1329,28 @@ static int mv_cesa_ahmac_cra_init(struct crypto_tfm *tfm) return 0; } +static int mv_cesa_ahmac_md5_cra_init(struct crypto_tfm *tfm) +{ + return mv_cesa_ahmac_cra_init(tfm, "md5"); +} + +static int mv_cesa_ahmac_sha1_cra_init(struct crypto_tfm *tfm) +{ + return mv_cesa_ahmac_cra_init(tfm, "sha1"); +} + +static int mv_cesa_ahmac_sha256_cra_init(struct crypto_tfm *tfm) +{ + return mv_cesa_ahmac_cra_init(tfm, "sha256"); +} + +static void mv_cesa_ahmac_cra_exit(struct crypto_tfm *tfm) +{ + struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(tfm); + + crypto_free_shash(ctx->outer); +} + static int mv_cesa_ahmac_md5_init(struct ahash_request *req) { struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm); @@ -1318,7 +1416,8 @@ struct ahash_alg mv_ahmac_md5_alg = { CRYPTO_ALG_KERN_DRIVER_ONLY, .cra_blocksize = MD5_HMAC_BLOCK_SIZE, .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx), - .cra_init = mv_cesa_ahmac_cra_init, + .cra_init = mv_cesa_ahmac_md5_cra_init, + .cra_exit = mv_cesa_ahmac_cra_exit, .cra_module = THIS_MODULE, } } @@ -1389,7 +1488,8 @@ struct ahash_alg mv_ahmac_sha1_alg = { CRYPTO_ALG_KERN_DRIVER_ONLY, .cra_blocksize = SHA1_BLOCK_SIZE, .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx), - .cra_init = mv_cesa_ahmac_cra_init, + .cra_init = mv_cesa_ahmac_sha1_cra_init, + .cra_exit = mv_cesa_ahmac_cra_exit, .cra_module = THIS_MODULE, } } @@ -1460,7 +1560,8 @@ struct ahash_alg mv_ahmac_sha256_alg = { CRYPTO_ALG_KERN_DRIVER_ONLY, .cra_blocksize = SHA256_BLOCK_SIZE, .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx), - .cra_init = mv_cesa_ahmac_cra_init, + .cra_init = mv_cesa_ahmac_sha256_cra_init, + .cra_exit = mv_cesa_ahmac_cra_exit, .cra_module = THIS_MODULE, } } -- 2.56.1