From patchwork Tue Oct 18 07:10:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianjia Zhang X-Patchwork-Id: 3980 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1816943wrs; Tue, 18 Oct 2022 00:16:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7mB1yoUw3jPE+xRIGWDNpFJdnl7+jAiJKRqYmLPOQOXD9h5fHSrHeGcZqAC9cm74lVURyK X-Received: by 2002:a17:907:2c59:b0:78d:8e03:134 with SMTP id hf25-20020a1709072c5900b0078d8e030134mr1236744ejc.310.1666077374284; Tue, 18 Oct 2022 00:16:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666077374; cv=none; d=google.com; s=arc-20160816; b=gw43mDBmDLWhX3iQgDF0UU4Nam55+uSsIuQlww4O4c8KUH7yLUxe62e101YQDYKMF+ vr4ywXh0KV66fyP79ahNndsDkIqjAhkKwxV1TRMGj+5hlA45+18UXEPLbHmWh61Ozm2e u2A1He17FTom4TtTEQTPC5OJsJOO6NHm+pObGatmjjnpHvRSV736gFmMvPdXDAK15Qk+ aVq30MlIwi8nwR1Ug3RKEehCm8TuprduiF5oVD0HT73+h0J7xXGVetIuZ2N/bxThw7ih 23q11Se8pF1bwWG62mJFGWJNi3nMFjXNdoCA75/7nOL+d406iUpVdYykc1bFQi9qWoxv 72XA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=HmTRQKeALm1a8/qz2YOit7Xt/vG5nsMLO0Pfj9jCVlU=; b=UUXg1sBCawOQBCDtfpL9Rh9HP9ZyrHWcgO2Q9YkJOQXs7LTRwj9gVBhj7nmyc3d93h xBGHcbzcIfYgXaqI6Jw4gLdbM5AHDqN0Lr+Zh3daDk0B9wncXoyRXF5u77gnjRKFShNu IJsYxi/aug2yPS47b59oX/d27nvJF6bae2QQXYdpkBjTq7L1h88oOp9DWh4dVnywfnPj 1jFYfVrHIMeZk3TWF8FVA29GW4QUGS952NijVpCe1ZMKyF4JSYuosTL8Xi9GA6m4Ob6e aUGgp8Xfuzn9RUi6spY4TGAQ6rTjoPvNFVuaL/6cBFdgnl+IGyC9+K4CbEXSV4oj2VeC MpAg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w9-20020a50d789000000b004542e6bd242si10348035edi.621.2022.10.18.00.15.49; Tue, 18 Oct 2022 00:16:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230395AbiJRHL4 (ORCPT + 99 others); Tue, 18 Oct 2022 03:11:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230120AbiJRHLG (ORCPT ); Tue, 18 Oct 2022 03:11:06 -0400 Received: from out30-45.freemail.mail.aliyun.com (out30-45.freemail.mail.aliyun.com [115.124.30.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98548A98F9; Tue, 18 Oct 2022 00:10:42 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046050;MF=tianjia.zhang@linux.alibaba.com;NM=1;PH=DS;RN=15;SR=0;TI=SMTPD_---0VSTUHcm_1666077032; Received: from localhost(mailfrom:tianjia.zhang@linux.alibaba.com fp:SMTPD_---0VSTUHcm_1666077032) by smtp.aliyun-inc.com; Tue, 18 Oct 2022 15:10:34 +0800 From: Tianjia Zhang To: Herbert Xu , "David S. Miller" , Jussi Kivilinna , Ard Biesheuvel , Mark Brown , Catalin Marinas , Will Deacon , Maxime Coquelin , Alexandre Torgue , Eric Biggers , linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com Cc: Tianjia Zhang Subject: [PATCH v2 11/15] crypto: essiv - allow digestsize to be greater than keysize Date: Tue, 18 Oct 2022 15:10:02 +0800 Message-Id: <20221018071006.5717-12-tianjia.zhang@linux.alibaba.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20221018071006.5717-1-tianjia.zhang@linux.alibaba.com> References: <20221018071006.5717-1-tianjia.zhang@linux.alibaba.com> MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747008748579560453?= X-GMAIL-MSGID: =?utf-8?q?1747008748579560453?= In essiv mode, the digest of the hash algorithm is used as the key to encrypt the IV. The current implementation requires that the digest size of the hash algorithm is equal to the key size, which will exclude algorithms that do not meet this situation, such as essiv(cbc(sm4),sm3), the hash result of sm3 is fixed 256 bits, and the key size of sm4 symmetric algorithm is fixed 128 bits, which makes it impossible to use essiv mode. This patch allows algorithms whose digest size is greater than key size to use esssiv mode by truncating the digest. Signed-off-by: Tianjia Zhang --- crypto/essiv.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crypto/essiv.c b/crypto/essiv.c index e33369df9034..6ee5a61bcae4 100644 --- a/crypto/essiv.c +++ b/crypto/essiv.c @@ -68,6 +68,7 @@ static int essiv_skcipher_setkey(struct crypto_skcipher *tfm, { struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm); u8 salt[HASH_MAX_DIGESTSIZE]; + unsigned int saltlen; int err; crypto_skcipher_clear_flags(tctx->u.skcipher, CRYPTO_TFM_REQ_MASK); @@ -86,8 +87,11 @@ static int essiv_skcipher_setkey(struct crypto_skcipher *tfm, crypto_cipher_set_flags(tctx->essiv_cipher, crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_REQ_MASK); - return crypto_cipher_setkey(tctx->essiv_cipher, salt, - crypto_shash_digestsize(tctx->hash)); + + saltlen = min(crypto_shash_digestsize(tctx->hash), + crypto_skcipher_max_keysize(tctx->u.skcipher)); + + return crypto_cipher_setkey(tctx->essiv_cipher, salt, saltlen); } static int essiv_aead_setkey(struct crypto_aead *tfm, const u8 *key, @@ -418,8 +422,7 @@ static bool essiv_supported_algorithms(const char *essiv_cipher_name, if (IS_ERR(alg)) return false; - if (hash_alg->digestsize < alg->cra_cipher.cia_min_keysize || - hash_alg->digestsize > alg->cra_cipher.cia_max_keysize) + if (hash_alg->digestsize < alg->cra_cipher.cia_min_keysize) goto out; if (ivsize != alg->cra_blocksize)