From patchwork Thu May 4 09:32:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yangfl X-Patchwork-Id: 90025 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp176965vqo; Thu, 4 May 2023 02:40:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6oR8DCTljAn0rKB6fRNwwNOYI9OekxRzEBH8vjsXI2bk1gVxJW4+SDTQpWF59bc7LZK+V7 X-Received: by 2002:a05:6a00:150e:b0:63b:6933:a328 with SMTP id q14-20020a056a00150e00b0063b6933a328mr1840304pfu.28.1683193208161; Thu, 04 May 2023 02:40:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683193208; cv=none; d=google.com; s=arc-20160816; b=KelphM0EygxVGLxM2IjDIswLTqzttVDv1Ta282H1sagSLaPCjwGykn+jqPqpr5MRfL w9QBbn3UbRoYi9gI7cM7jeqTqyvfrJjKeYQujeTWL+pCBsMN+OtxDju3iJSJQclz31yC rXqf7rSoaKB5d+zUI6QmiuVfSiK6DEOGogdTEApw2cZjaNDuQg5C3A9GIlD6wVoT2rl/ ErbZ1h1eUKz9fdubj9kJdZaAqEtGtL/cwA5nWT8YKfiphOvR1KCdV/gmuyxeuuX/+8zz E2F2OnhhnWoej1gIyHE7I/gLnFxijMYpNWIvPFuHPQ4PH74CF/gszHqVoJhMemYukmlI zJKA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=lBOT6ucg6ng9qBkKlorD62fyrVLYVgD1ftPso5pMrEM=; b=M0XWvz5fdNKEP62FsDSvaSo2Ik0jbbPif6jErIwkSNjI1ui/x5sk55NaDniiIKQw5S TKj/+8nW4wEWZO11yGFlmj37J8mfu5XzmkR1gtgClwtoNhql0XoYaO4eTZTUfIQwI0Fn vuySNITIzKpbIM490JxRd1yFzKJKV94gi9UVR1VegjoZRr92egiV9PrI7FLFeOaCj1mK CB+pjTUPrf1OR1XEVJ2ZwjNpNuywPiZKHc6gow5GY5PL9W1hZjZiTj6yccIkCBoZRm2X pMdgYKdLFHtfzpWs4gYJgXibGkdaJI6pNoJx6ky1KRciaEQTduj/MTaXXwjMReY8HA1y Sh4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=D8eFhT00; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c6-20020a056a00008600b0063b82c0941fsi35007418pfj.36.2023.05.04.02.39.55; Thu, 04 May 2023 02:40:08 -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; dkim=pass header.i=@gmail.com header.s=20221208 header.b=D8eFhT00; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229886AbjEDJdk (ORCPT + 99 others); Thu, 4 May 2023 05:33:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230023AbjEDJdJ (ORCPT ); Thu, 4 May 2023 05:33:09 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93A1E49C0; Thu, 4 May 2023 02:33:07 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6438d95f447so165567b3a.3; Thu, 04 May 2023 02:33:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683192787; x=1685784787; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=lBOT6ucg6ng9qBkKlorD62fyrVLYVgD1ftPso5pMrEM=; b=D8eFhT00Xes9DNAgtmKZUycfUY1lAYrerwL3VrA7JsqResg/EsYF6SZLQZfnhyH1Sz FRCZK2pVrKdZlpolhCOuwWUtNg5lNEb2+D32SQfupFBKOwKYqADlYTzfpCsSNaeiHlP2 JiJ9nqLsRj4oSSmUvtd0ZTx1zkWm06FkfMeF3LCCUaOxUjTLxplv/CMLkXPU3u5+qY5D FnUj+ZqHBY32dlGnWzk/rYxY97prPDXSuK2iWJOE8/UJ6qErny5+dYVChzgKL40yxyCp aUq1IjyCTg1l5GxtZuN3fL0AWhnKctJhsJanvYgxBYGJoUrq7ZwNsI0wh/l9UFrsKrKZ HUdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683192787; x=1685784787; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lBOT6ucg6ng9qBkKlorD62fyrVLYVgD1ftPso5pMrEM=; b=OfySO9Us7HO/MQXc2yo5qb19L2uwb8lJtNAli+QxO7P00keeTW01BDUPypu/8UrdzS rBvJwiY+9VP3xFv/rIpkviBjN5cxMOwvMxCFwnfaoW9i88jcH1POsWaeKB3heVYUMztm N0hFuOL8jeuYdN99DsGIBNJwitL/Ji7sD+HQYFr0WHA/ke2fDE9Sr4OOiy6x2vMYi3VF cgJ9FAwHpkcU9AjtXZh83uGXAGgMCb9Cq6ysUOZRgpqlnHOzmVoKv0QqJLgyfP5K9fmh NO/sb4xdfk8XC3A0vpU/Cr/nJPWTSGoNFmG2ILVmLTRM1/Qp1dmqj8dnLvP0B2wDqo2Q gYuw== X-Gm-Message-State: AC+VfDzfw4cQqvFYIQS2jFyPh2UbxW5XeoB6Zb941vKm12RMdZu7gvJK Gux3CxFxNo/YaLPE0cfV85wGkal7euWc8TWX X-Received: by 2002:a05:6a00:ad3:b0:643:aa2:4dd0 with SMTP id c19-20020a056a000ad300b006430aa24dd0mr1706733pfl.9.1683192786747; Thu, 04 May 2023 02:33:06 -0700 (PDT) Received: from d.home.yangfl.dn42 ([104.28.245.199]) by smtp.gmail.com with ESMTPSA id q14-20020aa7842e000000b00640dbf177b8sm18765163pfn.37.2023.05.04.02.33.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 May 2023 02:33:06 -0700 (PDT) From: David Yang To: linux-crypto@vger.kernel.org Cc: David Yang , Herbert Xu , "David S. Miller" , linux-kernel@vger.kernel.org Subject: [PATCH] crypto: hash - Add default handlers for digest, export, import Date: Thu, 4 May 2023 17:32:55 +0800 Message-Id: <20230504093255.350365-1-mmyangfl@gmail.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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?1764956001424592044?= X-GMAIL-MSGID: =?utf-8?q?1764956001424592044?= The document of struct ahash_alg explicitly states that @finup, combination of @update and @final, is optional, while said nothing about @digest, which is combination of @init and @finup (@update and @final). This gives a illusion that @digest might be optional. However crypto_ahash_init_tfm() strangely does expect a non-NULL @digest, otherwise it'll result in NULL pointer dereference. Add default handlers for digest, export, import according to the behavior in the document, and mark them optional. Signed-off-by: David Yang --- crypto/ahash.c | 28 +++++++++++++++++++++++++--- include/crypto/hash.h | 12 ++++++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/crypto/ahash.c b/crypto/ahash.c index b8a607928e72..53abbfe58a5f 100644 --- a/crypto/ahash.c +++ b/crypto/ahash.c @@ -417,6 +417,28 @@ static int ahash_def_finup(struct ahash_request *req) return ahash_def_finup_finish1(req, err); } +static int ahash_def_digest(struct ahash_request *req) +{ + struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); + int err; + + err = tfm->init(req); + if (err) + return err; + + return ahash_def_finup(req); +} + +static int ahash_def_noexport(struct ahash_request *req, void *out) +{ + return -EINVAL; +} + +static int ahash_def_noimport(struct ahash_request *req, const void *in) +{ + return -EINVAL; +} + static void crypto_ahash_exit_tfm(struct crypto_tfm *tfm) { struct crypto_ahash *hash = __crypto_ahash_cast(tfm); @@ -439,9 +461,9 @@ static int crypto_ahash_init_tfm(struct crypto_tfm *tfm) hash->update = alg->update; hash->final = alg->final; hash->finup = alg->finup ?: ahash_def_finup; - hash->digest = alg->digest; - hash->export = alg->export; - hash->import = alg->import; + hash->digest = alg->digest ?: ahash_def_digest; + hash->export = alg->export ?: ahash_def_noexport; + hash->import = alg->import ?: ahash_def_noimport; if (alg->setkey) { hash->setkey = alg->setkey; diff --git a/include/crypto/hash.h b/include/crypto/hash.h index e69542d86a2b..17dee80ac76a 100644 --- a/include/crypto/hash.h +++ b/include/crypto/hash.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Hash: Hash algorithms under the crypto API - * + * * Copyright (c) 2008 Herbert Xu */ @@ -110,7 +110,7 @@ struct ahash_request { * added to allow such hardware to be used at least by IPsec. Data * processing can happen synchronously [SHASH] or asynchronously [AHASH] * at this point. - * @digest: Combination of @init and @update and @final. This function + * @digest: **[optional]** Combination of @init and @update and @final. This function * effectively behaves as the entire chain of operations, @init, * @update and @final issued in sequence. Just like @finup, this was * added for hardware which cannot do even the @finup, but can only do @@ -129,15 +129,15 @@ struct ahash_request { * this function. This function must be called before any other of the * @init, @update, @final, @finup, @digest is called. No data * processing happens at this point. - * @export: Export partial state of the transformation. This function dumps the - * entire state of the ongoing transformation into a provided block of + * @export: **[optional]** Export partial state of the transformation. This function + * dumps the entire state of the ongoing transformation into a provided block of * data so it can be @import 'ed back later on. This is useful in case * you want to save partial result of the transformation after * processing certain amount of data and reload this partial result * multiple times later on for multiple re-use. No data processing * happens at this point. Driver must not use req->result. - * @import: Import partial state of the transformation. This function loads the - * entire state of the ongoing transformation from a provided block of + * @import: **[optional]** Import partial state of the transformation. This function + * loads the entire state of the ongoing transformation from a provided block of * data so the transformation can continue from this point onward. No * data processing happens at this point. Driver must not use * req->result.