From patchwork Fri Jan 3 03:58:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316343 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E1DDA1580 for ; Fri, 3 Jan 2020 04:01:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B78302465A for ; Fri, 3 Jan 2020 04:01:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024088; bh=0Jmn6rgHyxICeqesUsUiqMF+viPCeJM7M1LgfU0aapQ=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=ILgfA2dlFmW7MZ59k7RZblbJZnW4hIWA2fsLB+8p0IPxelFR4XDISt7jtVeqzMhAv A0fz0wOnWA87F1NCS3IrFMJjLeuF3R0ynLLNRzl8rwYNLdhpm9W4hITXFz3GJ9j/pe /O73GTy+etRqsBb2JPQt+1ccJLePWoQX/c3/3WJk= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726282AbgACEBZ (ORCPT ); Thu, 2 Jan 2020 23:01:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:33486 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726292AbgACEBY (ORCPT ); Thu, 2 Jan 2020 23:01:24 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 406AB222C3 for ; Fri, 3 Jan 2020 04:01:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024084; bh=0Jmn6rgHyxICeqesUsUiqMF+viPCeJM7M1LgfU0aapQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=wzr0PyT5Sf9Htxgv7mUXnGZCY3iVHPQPqhpzLGyfII5Fpj3gEu3n4lQf2JPwl/h1R i4hRswoXOtZiJhWZjMa9Uey2T62SM2sT4w068CUX6L3/0EfJtLdAca9a3Dw/2s/ILg //ygcO38uiJ203iEiYhjuNl9JUHY/oX6QghFXN0M= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 01/28] crypto: algapi - make crypto_drop_spawn() a no-op on uninitialized spawns Date: Thu, 2 Jan 2020 19:58:41 -0800 Message-Id: <20200103035908.12048-2-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make crypto_drop_spawn() do nothing when the spawn hasn't been initialized with an algorithm yet. This will allow simplifying error handling in all the template ->create() functions, since on error they will be able to just call their usual "free instance" function, rather than having to handle dropping just the spawns that have been initialized so far. This does assume the spawn starts out zero-filled, but that's always the case since instances are allocated with kzalloc(). And some other code already assumes this anyway. Signed-off-by: Eric Biggers --- crypto/algapi.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crypto/algapi.c b/crypto/algapi.c index 363849983941..4c761f48110d 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -734,6 +734,9 @@ EXPORT_SYMBOL_GPL(crypto_grab_spawn); void crypto_drop_spawn(struct crypto_spawn *spawn) { + if (!spawn->alg) /* not yet initialized? */ + return; + down_write(&crypto_alg_sem); if (!spawn->dead) list_del(&spawn->list); From patchwork Fri Jan 3 03:58:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316345 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 227036C1 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EB9E424654 for ; Fri, 3 Jan 2020 04:01:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024089; bh=Xh56S2p/u7dijE7JzTBgalq9BKRLTICE05G86pxpQcw=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=0A+q7yN5QpuWUPtM1ZJ4Rq1tDMT4dZmtspPVt8YlTKjJpPFg0haD4pTSkQ5cAayZM 4nYskYXb+jQJ49QUhIwDiiulV86aSe8CuiV7pCTIcKTd36eKULubfNDhM4I5I1Qkus fHUvA7kZsAGtwNhV9OGSnXd6oGB9rjcqxYtkJo+4= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726654AbgACEBZ (ORCPT ); Thu, 2 Jan 2020 23:01:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:33500 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726481AbgACEBZ (ORCPT ); Thu, 2 Jan 2020 23:01:25 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6F73722314 for ; Fri, 3 Jan 2020 04:01:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024084; bh=Xh56S2p/u7dijE7JzTBgalq9BKRLTICE05G86pxpQcw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=wolVh05oGuNzovkPw6U+gpmB9FbJ2eYb6NKDsTmnVCIV3lr4yhr74kzqJwPz1+0fG xfFLoV7HiErlA7sb8QkIp+rkekhKVepl2sD49RxVDlvNSnGyhXAGnhjydqIgBIovOV +fZhTnk3w/q6b6MPuyauGmdcJAO2HlNQ+5x16Qhs= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 02/28] crypto: algapi - make crypto_grab_spawn() handle an ERR_PTR() name Date: Thu, 2 Jan 2020 19:58:42 -0800 Message-Id: <20200103035908.12048-3-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers To allow further simplifying template ->create() functions, make crypto_grab_spawn() handle an ERR_PTR() name by passing back the error. For most templates, this will allow the result of crypto_attr_alg_name() to be passed directly to crypto_grab_*(), rather than first having to assign it to a variable [where it can then potentially be misused, as it was in the rfc7539 template prior to commit 5e27f38f1f3f ("crypto: chacha20poly1305 - set cra_name correctly")] and check it for error. Signed-off-by: Eric Biggers --- crypto/algapi.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crypto/algapi.c b/crypto/algapi.c index 4c761f48110d..a5223c5f2275 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -720,6 +720,10 @@ int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name, struct crypto_alg *alg; int err; + /* Allow the result of crypto_attr_alg_name() to be passed directly */ + if (IS_ERR(name)) + return PTR_ERR(name); + alg = crypto_find_alg(name, spawn->frontend, type, mask); if (IS_ERR(alg)) return PTR_ERR(alg); From patchwork Fri Jan 3 03:58:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316347 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5738418B6 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2C55924654 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024089; bh=za0h9SO7Ok2opAq2WuG0SE2eh0YyqvISRvGT64IrBR8=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=VA7jbX9X5mY6K5bgLvsD0prv/Smpg7lOMBf1qCtIGR5/zOq54rcmnGdyZrWvzIHcT nLJspbq/OsRCVT06Fs+RPNROoUAnlU+nRiwXfxqinMg3/aA0oJGAytlzkGeHiQx39w o5WXG2v+WM6SmeyH8PFxFD3G7jXcmpmU/0aAMpM4= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726781AbgACEBZ (ORCPT ); Thu, 2 Jan 2020 23:01:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:33506 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726504AbgACEBZ (ORCPT ); Thu, 2 Jan 2020 23:01:25 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A121522525 for ; Fri, 3 Jan 2020 04:01:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024084; bh=za0h9SO7Ok2opAq2WuG0SE2eh0YyqvISRvGT64IrBR8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=orAwhMQYX+PDugze1qYuNjnQrcIqicl3d33suGreC5lqyhT1op5PgA/p+uQy1koAa vN+EUKPeH0mEPMHqRaUDVqiqlVns/8XJRvjNCiB4m2wRzaV2ssFFFQbBa43KfrZ8Qo N8lJ7wSJThJRlevtNlUymHBszsChuKSOhBa6sb8A= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 03/28] crypto: shash - make struct shash_instance be the full size Date: Thu, 2 Jan 2020 19:58:43 -0800 Message-Id: <20200103035908.12048-4-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Define struct shash_instance in a way analogous to struct skcipher_instance, struct aead_instance, and struct akcipher_instance, where the struct is defined to include both the algorithm structure at the beginning and the additional crypto_instance fields at the end. This is needed to allow allocating shash instances directly using kzalloc(sizeof(*inst) + sizeof(*ictx), ...) in the same way as skcipher, aead, and akcipher instances. In turn, that's needed to make spawns be initialized in a consistent way everywhere. Also take advantage of the addition of the base instance to struct shash_instance by simplifying the shash_crypto_instance() and shash_instance() functions. Signed-off-by: Eric Biggers --- include/crypto/internal/hash.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/include/crypto/internal/hash.h b/include/crypto/internal/hash.h index d4b1be519590..7f25eff69d36 100644 --- a/include/crypto/internal/hash.h +++ b/include/crypto/internal/hash.h @@ -34,7 +34,13 @@ struct ahash_instance { }; struct shash_instance { - struct shash_alg alg; + union { + struct { + char head[offsetof(struct shash_alg, base)]; + struct crypto_instance base; + } s; + struct shash_alg alg; + }; }; struct crypto_ahash_spawn { @@ -210,14 +216,13 @@ static inline void *crypto_shash_ctx(struct crypto_shash *tfm) static inline struct crypto_instance *shash_crypto_instance( struct shash_instance *inst) { - return container_of(&inst->alg.base, struct crypto_instance, alg); + return &inst->s.base; } static inline struct shash_instance *shash_instance( struct crypto_instance *inst) { - return container_of(__crypto_shash_alg(&inst->alg), - struct shash_instance, alg); + return container_of(inst, struct shash_instance, s.base); } static inline struct shash_instance *shash_alg_instance( From patchwork Fri Jan 3 03:58:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316349 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8B91017F0 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6100424654 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024089; bh=+9Xnas3uoUxlpPskJipaxSFlWbLDFA4vpAqy8OR6gNc=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=pRMq0Rb9kFzrwSLDA/mqqP1edQ69wE3PKTorzsur8JItPkqEJSUWvJoDXziDlEIjp pjJOycsSCOhWM/3VFWHX36Mxb7Y2vxfCbnNiwh4P1CgFfD9di7gfBv6iDH53LBoiYV Yh7LGg4KRRg1eOJCQPB+caY1bow1R4NGsUoyIr5M= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726820AbgACEBZ (ORCPT ); Thu, 2 Jan 2020 23:01:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:33508 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726292AbgACEBZ (ORCPT ); Thu, 2 Jan 2020 23:01:25 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D09C82253D for ; Fri, 3 Jan 2020 04:01:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024084; bh=+9Xnas3uoUxlpPskJipaxSFlWbLDFA4vpAqy8OR6gNc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=VgkvvxjBWtwK5HxRZ7SOg83Sk/Q01jEq4nCH6wGH8G9JcWabrdJk0yIoLaAJVxrLr tArDme49tAS1I6TOiqPbX/SkxUWKTBkMS3TtqQwOxcnq0Q4Yw/RzO7P8bYiY908XXt 66PObDJBlVXBiYo+jiUxaM1qSSykQAt0zXm5B5RM= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 04/28] crypto: ahash - make struct ahash_instance be the full size Date: Thu, 2 Jan 2020 19:58:44 -0800 Message-Id: <20200103035908.12048-5-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Define struct ahash_instance in a way analogous to struct skcipher_instance, struct aead_instance, and struct akcipher_instance, where the struct is defined to include both the algorithm structure at the beginning and the additional crypto_instance fields at the end. This is needed to allow allocating ahash instances directly using kzalloc(sizeof(*inst) + sizeof(*ictx), ...) in the same way as skcipher, aead, and akcipher instances. In turn, that's needed to make spawns be initialized in a consistent way everywhere. Also take advantage of the addition of the base instance to struct ahash_instance by simplifying the ahash_crypto_instance() and ahash_instance() functions. Signed-off-by: Eric Biggers --- include/crypto/internal/hash.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/crypto/internal/hash.h b/include/crypto/internal/hash.h index 7f25eff69d36..3b426b09bd32 100644 --- a/include/crypto/internal/hash.h +++ b/include/crypto/internal/hash.h @@ -30,7 +30,13 @@ struct crypto_hash_walk { }; struct ahash_instance { - struct ahash_alg alg; + union { + struct { + char head[offsetof(struct ahash_alg, halg.base)]; + struct crypto_instance base; + } s; + struct ahash_alg alg; + }; }; struct shash_instance { @@ -155,13 +161,13 @@ static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm, static inline struct crypto_instance *ahash_crypto_instance( struct ahash_instance *inst) { - return container_of(&inst->alg.halg.base, struct crypto_instance, alg); + return &inst->s.base; } static inline struct ahash_instance *ahash_instance( struct crypto_instance *inst) { - return container_of(&inst->alg, struct ahash_instance, alg.halg.base); + return container_of(inst, struct ahash_instance, s.base); } static inline void *ahash_instance_ctx(struct ahash_instance *inst) From patchwork Fri Jan 3 03:58:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316395 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B2F0E6C1 for ; Fri, 3 Jan 2020 04:01:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 877F422314 for ; Fri, 3 Jan 2020 04:01:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024098; bh=ClyIXS8Xvl75NkhFMi4cReNhOBcgtBL7ysEmsNClS6c=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=xj6LZRyIwygkoMysvzrBxsaiYkj5sotQA2je5tenzfJ4oMp39YuEeADaQMpc3dBoz kYdmDiKo1sPq2UALArQjv5zJuDoMt0Dzyuyi/ZYf5+Hng7NaVfFf6eyry3mlhvN4QB 510Cdu/cvBPycvSRfFHtpxYS5HTzRwUXMz3bes3w= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727175AbgACEBh (ORCPT ); Thu, 2 Jan 2020 23:01:37 -0500 Received: from mail.kernel.org ([198.145.29.99]:33514 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726481AbgACEB1 (ORCPT ); Thu, 2 Jan 2020 23:01:27 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0D23521D7D for ; Fri, 3 Jan 2020 04:01:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024085; bh=ClyIXS8Xvl75NkhFMi4cReNhOBcgtBL7ysEmsNClS6c=; h=From:To:Subject:Date:In-Reply-To:References:From; b=nTF28R0UM6Sd51bXz/RfA3V2pyNhxJy22vriqrcPp0YCCmk9DrEDBsnkmwxmbhT10 7ZLCYQa0bbfbQi+D5n8/G91iL4PGV+35sA4b1Ec6F9h4uN85zdhLhkVA6vdwMnLEKt HXmZ8lC16n2HYdv8SaAc7Y5YTBr8X5PVlVXnnSK0= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 05/28] crypto: skcipher - pass instance to crypto_grab_skcipher() Date: Thu, 2 Jan 2020 19:58:45 -0800 Message-Id: <20200103035908.12048-6-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Initializing a crypto_skcipher_spawn currently requires: 1. Set spawn->base.inst to point to the instance. 2. Call crypto_grab_skcipher(). But there's no reason for these steps to be separate, and in fact this unneeded complication has caused at least one bug, the one fixed by commit 6db43410179b ("crypto: adiantum - initialize crypto_spawn::inst") So just make crypto_grab_skcipher() take the instance as an argument. To keep the function calls from getting too unwieldy due to this extra argument, also introduce a 'mask' variable into the affected places which weren't already using one. Signed-off-by: Eric Biggers --- crypto/adiantum.c | 11 ++++++----- crypto/authenc.c | 12 ++++++------ crypto/authencesn.c | 12 ++++++------ crypto/ccm.c | 9 +++++---- crypto/chacha20poly1305.c | 13 ++++++------- crypto/cryptd.c | 4 ++-- crypto/ctr.c | 4 ++-- crypto/cts.c | 9 +++++---- crypto/essiv.c | 13 +++++-------- crypto/gcm.c | 13 ++++++------- crypto/lrw.c | 15 ++++++++------- crypto/skcipher.c | 4 +++- crypto/xts.c | 9 +++++---- include/crypto/internal/skcipher.h | 11 +++-------- 14 files changed, 68 insertions(+), 71 deletions(-) diff --git a/crypto/adiantum.c b/crypto/adiantum.c index aded26092268..aaf8a66f871c 100644 --- a/crypto/adiantum.c +++ b/crypto/adiantum.c @@ -501,6 +501,7 @@ static bool adiantum_supported_algorithms(struct skcipher_alg *streamcipher_alg, static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) { struct crypto_attr_type *algt; + u32 mask; const char *streamcipher_name; const char *blockcipher_name; const char *nhpoly1305_name; @@ -519,6 +520,8 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + streamcipher_name = crypto_attr_alg_name(tb[1]); if (IS_ERR(streamcipher_name)) return PTR_ERR(streamcipher_name); @@ -539,11 +542,9 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) ictx = skcipher_instance_ctx(inst); /* Stream cipher, e.g. "xchacha12" */ - crypto_set_skcipher_spawn(&ictx->streamcipher_spawn, - skcipher_crypto_instance(inst)); - err = crypto_grab_skcipher(&ictx->streamcipher_spawn, streamcipher_name, - 0, crypto_requires_sync(algt->type, - algt->mask)); + err = crypto_grab_skcipher(&ictx->streamcipher_spawn, + skcipher_crypto_instance(inst), + streamcipher_name, 0, mask); if (err) goto out_free_inst; streamcipher_alg = crypto_spawn_skcipher_alg(&ictx->streamcipher_spawn); diff --git a/crypto/authenc.c b/crypto/authenc.c index 3f0ed9402582..aef04792702a 100644 --- a/crypto/authenc.c +++ b/crypto/authenc.c @@ -383,6 +383,7 @@ static int crypto_authenc_create(struct crypto_template *tmpl, struct rtattr **tb) { struct crypto_attr_type *algt; + u32 mask; struct aead_instance *inst; struct hash_alg_common *auth; struct crypto_alg *auth_base; @@ -398,9 +399,10 @@ static int crypto_authenc_create(struct crypto_template *tmpl, if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH, - CRYPTO_ALG_TYPE_AHASH_MASK | - crypto_requires_sync(algt->type, algt->mask)); + CRYPTO_ALG_TYPE_AHASH_MASK | mask); if (IS_ERR(auth)) return PTR_ERR(auth); @@ -423,10 +425,8 @@ static int crypto_authenc_create(struct crypto_template *tmpl, if (err) goto err_free_inst; - crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst)); - err = crypto_grab_skcipher(&ctx->enc, enc_name, 0, - crypto_requires_sync(algt->type, - algt->mask)); + err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst), + enc_name, 0, mask); if (err) goto err_drop_auth; diff --git a/crypto/authencesn.c b/crypto/authencesn.c index adb7554fca29..48582c3741dc 100644 --- a/crypto/authencesn.c +++ b/crypto/authencesn.c @@ -401,6 +401,7 @@ static int crypto_authenc_esn_create(struct crypto_template *tmpl, struct rtattr **tb) { struct crypto_attr_type *algt; + u32 mask; struct aead_instance *inst; struct hash_alg_common *auth; struct crypto_alg *auth_base; @@ -416,9 +417,10 @@ static int crypto_authenc_esn_create(struct crypto_template *tmpl, if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH, - CRYPTO_ALG_TYPE_AHASH_MASK | - crypto_requires_sync(algt->type, algt->mask)); + CRYPTO_ALG_TYPE_AHASH_MASK | mask); if (IS_ERR(auth)) return PTR_ERR(auth); @@ -441,10 +443,8 @@ static int crypto_authenc_esn_create(struct crypto_template *tmpl, if (err) goto err_free_inst; - crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst)); - err = crypto_grab_skcipher(&ctx->enc, enc_name, 0, - crypto_requires_sync(algt->type, - algt->mask)); + err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst), + enc_name, 0, mask); if (err) goto err_drop_auth; diff --git a/crypto/ccm.c b/crypto/ccm.c index 380eb619f657..d2279dc5b970 100644 --- a/crypto/ccm.c +++ b/crypto/ccm.c @@ -457,6 +457,7 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl, const char *mac_name) { struct crypto_attr_type *algt; + u32 mask; struct aead_instance *inst; struct skcipher_alg *ctr; struct crypto_alg *mac_alg; @@ -471,6 +472,8 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl, if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + mac_alg = crypto_find_alg(mac_name, &crypto_ahash_type, CRYPTO_ALG_TYPE_HASH, CRYPTO_ALG_TYPE_AHASH_MASK | @@ -495,10 +498,8 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl, if (err) goto err_free_inst; - crypto_set_skcipher_spawn(&ictx->ctr, aead_crypto_instance(inst)); - err = crypto_grab_skcipher(&ictx->ctr, ctr_name, 0, - crypto_requires_sync(algt->type, - algt->mask)); + err = crypto_grab_skcipher(&ictx->ctr, aead_crypto_instance(inst), + ctr_name, 0, mask); if (err) goto err_drop_mac; diff --git a/crypto/chacha20poly1305.c b/crypto/chacha20poly1305.c index 74e824e537e6..fcb8ec4ba083 100644 --- a/crypto/chacha20poly1305.c +++ b/crypto/chacha20poly1305.c @@ -563,6 +563,7 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb, const char *name, unsigned int ivsize) { struct crypto_attr_type *algt; + u32 mask; struct aead_instance *inst; struct skcipher_alg *chacha; struct crypto_alg *poly; @@ -581,6 +582,8 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb, if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + chacha_name = crypto_attr_alg_name(tb[1]); if (IS_ERR(chacha_name)) return PTR_ERR(chacha_name); @@ -590,9 +593,7 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb, poly = crypto_find_alg(poly_name, &crypto_ahash_type, CRYPTO_ALG_TYPE_HASH, - CRYPTO_ALG_TYPE_AHASH_MASK | - crypto_requires_sync(algt->type, - algt->mask)); + CRYPTO_ALG_TYPE_AHASH_MASK | mask); if (IS_ERR(poly)) return PTR_ERR(poly); poly_hash = __crypto_hash_alg_common(poly); @@ -613,10 +614,8 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb, if (err) goto err_free_inst; - crypto_set_skcipher_spawn(&ctx->chacha, aead_crypto_instance(inst)); - err = crypto_grab_skcipher(&ctx->chacha, chacha_name, 0, - crypto_requires_sync(algt->type, - algt->mask)); + err = crypto_grab_skcipher(&ctx->chacha, aead_crypto_instance(inst), + chacha_name, 0, mask); if (err) goto err_drop_poly; diff --git a/crypto/cryptd.c b/crypto/cryptd.c index 2c6649b10923..01a1f6aa30ac 100644 --- a/crypto/cryptd.c +++ b/crypto/cryptd.c @@ -421,8 +421,8 @@ static int cryptd_create_skcipher(struct crypto_template *tmpl, ctx = skcipher_instance_ctx(inst); ctx->queue = queue; - crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst)); - err = crypto_grab_skcipher(&ctx->spawn, name, type, mask); + err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst), + name, type, mask); if (err) goto out_free_inst; diff --git a/crypto/ctr.c b/crypto/ctr.c index 1e9d6b86b3c6..c8076d9106a1 100644 --- a/crypto/ctr.c +++ b/crypto/ctr.c @@ -291,8 +291,8 @@ static int crypto_rfc3686_create(struct crypto_template *tmpl, spawn = skcipher_instance_ctx(inst); - crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst)); - err = crypto_grab_skcipher(spawn, cipher_name, 0, mask); + err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst), + cipher_name, 0, mask); if (err) goto err_free_inst; diff --git a/crypto/cts.c b/crypto/cts.c index 6b6087dbb62a..b98c5a563346 100644 --- a/crypto/cts.c +++ b/crypto/cts.c @@ -332,6 +332,7 @@ static int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb) struct crypto_attr_type *algt; struct skcipher_alg *alg; const char *cipher_name; + u32 mask; int err; algt = crypto_get_attr_type(tb); @@ -341,6 +342,8 @@ static int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb) if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + cipher_name = crypto_attr_alg_name(tb[1]); if (IS_ERR(cipher_name)) return PTR_ERR(cipher_name); @@ -351,10 +354,8 @@ static int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb) spawn = skcipher_instance_ctx(inst); - crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst)); - err = crypto_grab_skcipher(spawn, cipher_name, 0, - crypto_requires_sync(algt->type, - algt->mask)); + err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst), + cipher_name, 0, mask); if (err) goto err_free_inst; diff --git a/crypto/essiv.c b/crypto/essiv.c index e4b32c2ea7ec..2019250da08e 100644 --- a/crypto/essiv.c +++ b/crypto/essiv.c @@ -468,6 +468,7 @@ static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb) struct shash_alg *hash_alg; int ivsize; u32 type; + u32 mask; int err; algt = crypto_get_attr_type(tb); @@ -483,6 +484,7 @@ static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb) return PTR_ERR(shash_name); type = algt->type & algt->mask; + mask = crypto_requires_sync(algt->type, algt->mask); switch (type) { case CRYPTO_ALG_TYPE_SKCIPHER: @@ -495,11 +497,8 @@ static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb) ictx = crypto_instance_ctx(inst); /* Symmetric cipher, e.g., "cbc(aes)" */ - crypto_set_skcipher_spawn(&ictx->u.skcipher_spawn, inst); - err = crypto_grab_skcipher(&ictx->u.skcipher_spawn, - inner_cipher_name, 0, - crypto_requires_sync(algt->type, - algt->mask)); + err = crypto_grab_skcipher(&ictx->u.skcipher_spawn, inst, + inner_cipher_name, 0, mask); if (err) goto out_free_inst; skcipher_alg = crypto_spawn_skcipher_alg(&ictx->u.skcipher_spawn); @@ -519,9 +518,7 @@ static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb) /* AEAD cipher, e.g., "authenc(hmac(sha256),cbc(aes))" */ crypto_set_aead_spawn(&ictx->u.aead_spawn, inst); err = crypto_grab_aead(&ictx->u.aead_spawn, - inner_cipher_name, 0, - crypto_requires_sync(algt->type, - algt->mask)); + inner_cipher_name, 0, mask); if (err) goto out_free_inst; aead_alg = crypto_spawn_aead_alg(&ictx->u.aead_spawn); diff --git a/crypto/gcm.c b/crypto/gcm.c index 73884208f075..5a01b2b956f6 100644 --- a/crypto/gcm.c +++ b/crypto/gcm.c @@ -585,6 +585,7 @@ static int crypto_gcm_create_common(struct crypto_template *tmpl, const char *ghash_name) { struct crypto_attr_type *algt; + u32 mask; struct aead_instance *inst; struct skcipher_alg *ctr; struct crypto_alg *ghash_alg; @@ -599,11 +600,11 @@ static int crypto_gcm_create_common(struct crypto_template *tmpl, if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type, CRYPTO_ALG_TYPE_HASH, - CRYPTO_ALG_TYPE_AHASH_MASK | - crypto_requires_sync(algt->type, - algt->mask)); + CRYPTO_ALG_TYPE_AHASH_MASK | mask); if (IS_ERR(ghash_alg)) return PTR_ERR(ghash_alg); @@ -625,10 +626,8 @@ static int crypto_gcm_create_common(struct crypto_template *tmpl, ghash->digestsize != 16) goto err_drop_ghash; - crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst)); - err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0, - crypto_requires_sync(algt->type, - algt->mask)); + err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst), + ctr_name, 0, mask); if (err) goto err_drop_ghash; diff --git a/crypto/lrw.c b/crypto/lrw.c index be829f6afc8e..ae72f8ab1d9f 100644 --- a/crypto/lrw.c +++ b/crypto/lrw.c @@ -303,6 +303,7 @@ static int create(struct crypto_template *tmpl, struct rtattr **tb) struct skcipher_alg *alg; const char *cipher_name; char ecb_name[CRYPTO_MAX_ALG_NAME]; + u32 mask; int err; algt = crypto_get_attr_type(tb); @@ -312,6 +313,8 @@ static int create(struct crypto_template *tmpl, struct rtattr **tb) if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + cipher_name = crypto_attr_alg_name(tb[1]); if (IS_ERR(cipher_name)) return PTR_ERR(cipher_name); @@ -322,19 +325,17 @@ static int create(struct crypto_template *tmpl, struct rtattr **tb) spawn = skcipher_instance_ctx(inst); - crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst)); - err = crypto_grab_skcipher(spawn, cipher_name, 0, - crypto_requires_sync(algt->type, - algt->mask)); + err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst), + cipher_name, 0, mask); if (err == -ENOENT) { err = -ENAMETOOLONG; if (snprintf(ecb_name, CRYPTO_MAX_ALG_NAME, "ecb(%s)", cipher_name) >= CRYPTO_MAX_ALG_NAME) goto err_free_inst; - err = crypto_grab_skcipher(spawn, ecb_name, 0, - crypto_requires_sync(algt->type, - algt->mask)); + err = crypto_grab_skcipher(spawn, + skcipher_crypto_instance(inst), + ecb_name, 0, mask); } if (err) diff --git a/crypto/skcipher.c b/crypto/skcipher.c index 37adb71f7759..e5083dccccdc 100644 --- a/crypto/skcipher.c +++ b/crypto/skcipher.c @@ -758,8 +758,10 @@ static const struct crypto_type crypto_skcipher_type = { }; int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, - const char *name, u32 type, u32 mask) + struct crypto_instance *inst, + const char *name, u32 type, u32 mask) { + spawn->base.inst = inst; spawn->base.frontend = &crypto_skcipher_type; return crypto_grab_spawn(&spawn->base, name, type, mask); } diff --git a/crypto/xts.c b/crypto/xts.c index ab117633d64e..43e9048ba36b 100644 --- a/crypto/xts.c +++ b/crypto/xts.c @@ -361,20 +361,21 @@ static int create(struct crypto_template *tmpl, struct rtattr **tb) ctx = skcipher_instance_ctx(inst); - crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst)); - mask = crypto_requires_off(algt->type, algt->mask, CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC); - err = crypto_grab_skcipher(&ctx->spawn, cipher_name, 0, mask); + err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst), + cipher_name, 0, mask); if (err == -ENOENT) { err = -ENAMETOOLONG; if (snprintf(ctx->name, CRYPTO_MAX_ALG_NAME, "ecb(%s)", cipher_name) >= CRYPTO_MAX_ALG_NAME) goto err_free_inst; - err = crypto_grab_skcipher(&ctx->spawn, ctx->name, 0, mask); + err = crypto_grab_skcipher(&ctx->spawn, + skcipher_crypto_instance(inst), + ctx->name, 0, mask); } if (err) diff --git a/include/crypto/internal/skcipher.h b/include/crypto/internal/skcipher.h index ad4a6330ff53..b81cb4902abc 100644 --- a/include/crypto/internal/skcipher.h +++ b/include/crypto/internal/skcipher.h @@ -88,14 +88,9 @@ static inline void skcipher_request_complete(struct skcipher_request *req, int e req->base.complete(&req->base, err); } -static inline void crypto_set_skcipher_spawn( - struct crypto_skcipher_spawn *spawn, struct crypto_instance *inst) -{ - crypto_set_spawn(&spawn->base, inst); -} - -int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name, - u32 type, u32 mask); +int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, + struct crypto_instance *inst, + const char *name, u32 type, u32 mask); static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn) { From patchwork Fri Jan 3 03:58:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316355 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1689F139A for ; Fri, 3 Jan 2020 04:01:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EA46A24654 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024090; bh=IrycCQJGoY/hKQPN/6Xc7K5Y4Kl9hDwAeLMhBZ6Zb10=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=HSlGYoaFp9wRXN2h4tLhsPvPge+ULDUdPfsgLAQDG6gqy6NThF0uC7xidY2+3TFm9 szVZhom226VGu5hXTKX6vpusgWDMvVSb+O50vho7RYJ+uakmrn2ciT1Jt2S97RgVaI 7qypEdlcgVrwyqk9aSuPgUxAJ5zGDSEy+MPg+ERw= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727166AbgACEB0 (ORCPT ); Thu, 2 Jan 2020 23:01:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:33528 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726504AbgACEB0 (ORCPT ); Thu, 2 Jan 2020 23:01:26 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 47FD7222C3 for ; Fri, 3 Jan 2020 04:01:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024085; bh=IrycCQJGoY/hKQPN/6Xc7K5Y4Kl9hDwAeLMhBZ6Zb10=; h=From:To:Subject:Date:In-Reply-To:References:From; b=QfTPLwcHHHXAz0bEBr1AcD23kYEDbhyPPEbPcojkNEC7MWdSD0RtojC/3RcF9fng/ Wi68wTd0xD/g6H9yvM11+9CsRvLkorfpS+xS1Z1hC9/Tk1Rs3KlkFHWtyW+XyX+ZIl WL8hB0AIqM/7k/txv99fa4QENxYPx9KwXL0OmzCs= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 06/28] crypto: aead - pass instance to crypto_grab_aead() Date: Thu, 2 Jan 2020 19:58:46 -0800 Message-Id: <20200103035908.12048-7-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Initializing a crypto_aead_spawn currently requires: 1. Set spawn->base.inst to point to the instance. 2. Call crypto_grab_aead(). But there's no reason for these steps to be separate, and in fact this unneeded complication has caused at least one bug, the one fixed by commit 6db43410179b ("crypto: adiantum - initialize crypto_spawn::inst") So just make crypto_grab_aead() take the instance as an argument. To keep the function calls from getting too unwieldy due to this extra argument, also introduce a 'mask' variable into the affected places which weren't already using one. Signed-off-by: Eric Biggers --- crypto/aead.c | 6 ++++-- crypto/ccm.c | 8 +++++--- crypto/cryptd.c | 4 ++-- crypto/essiv.c | 3 +-- crypto/gcm.c | 16 ++++++++++------ crypto/geniv.c | 4 ++-- crypto/pcrypt.c | 5 ++--- include/crypto/internal/aead.h | 11 +++-------- 8 files changed, 29 insertions(+), 28 deletions(-) diff --git a/crypto/aead.c b/crypto/aead.c index 47f16d139e8e..c7135e00b8ea 100644 --- a/crypto/aead.c +++ b/crypto/aead.c @@ -207,9 +207,11 @@ static const struct crypto_type crypto_aead_type = { .tfmsize = offsetof(struct crypto_aead, base), }; -int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name, - u32 type, u32 mask) +int crypto_grab_aead(struct crypto_aead_spawn *spawn, + struct crypto_instance *inst, + const char *name, u32 type, u32 mask) { + spawn->base.inst = inst; spawn->base.frontend = &crypto_aead_type; return crypto_grab_spawn(&spawn->base, name, type, mask); } diff --git a/crypto/ccm.c b/crypto/ccm.c index d2279dc5b970..9c377976581d 100644 --- a/crypto/ccm.c +++ b/crypto/ccm.c @@ -746,6 +746,7 @@ static int crypto_rfc4309_create(struct crypto_template *tmpl, struct rtattr **tb) { struct crypto_attr_type *algt; + u32 mask; struct aead_instance *inst; struct crypto_aead_spawn *spawn; struct aead_alg *alg; @@ -759,6 +760,8 @@ static int crypto_rfc4309_create(struct crypto_template *tmpl, if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + ccm_name = crypto_attr_alg_name(tb[1]); if (IS_ERR(ccm_name)) return PTR_ERR(ccm_name); @@ -768,9 +771,8 @@ static int crypto_rfc4309_create(struct crypto_template *tmpl, return -ENOMEM; spawn = aead_instance_ctx(inst); - crypto_set_aead_spawn(spawn, aead_crypto_instance(inst)); - err = crypto_grab_aead(spawn, ccm_name, 0, - crypto_requires_sync(algt->type, algt->mask)); + err = crypto_grab_aead(spawn, aead_crypto_instance(inst), + ccm_name, 0, mask); if (err) goto out_free_inst; diff --git a/crypto/cryptd.c b/crypto/cryptd.c index 01a1f6aa30ac..5aea6d6c49a0 100644 --- a/crypto/cryptd.c +++ b/crypto/cryptd.c @@ -874,8 +874,8 @@ static int cryptd_create_aead(struct crypto_template *tmpl, ctx = aead_instance_ctx(inst); ctx->queue = queue; - crypto_set_aead_spawn(&ctx->aead_spawn, aead_crypto_instance(inst)); - err = crypto_grab_aead(&ctx->aead_spawn, name, type, mask); + err = crypto_grab_aead(&ctx->aead_spawn, aead_crypto_instance(inst), + name, type, mask); if (err) goto out_free_inst; diff --git a/crypto/essiv.c b/crypto/essiv.c index 2019250da08e..388cceb8c1a0 100644 --- a/crypto/essiv.c +++ b/crypto/essiv.c @@ -516,8 +516,7 @@ static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb) ictx = crypto_instance_ctx(inst); /* AEAD cipher, e.g., "authenc(hmac(sha256),cbc(aes))" */ - crypto_set_aead_spawn(&ictx->u.aead_spawn, inst); - err = crypto_grab_aead(&ictx->u.aead_spawn, + err = crypto_grab_aead(&ictx->u.aead_spawn, inst, inner_cipher_name, 0, mask); if (err) goto out_free_inst; diff --git a/crypto/gcm.c b/crypto/gcm.c index 5a01b2b956f6..4241264ff93a 100644 --- a/crypto/gcm.c +++ b/crypto/gcm.c @@ -866,6 +866,7 @@ static int crypto_rfc4106_create(struct crypto_template *tmpl, struct rtattr **tb) { struct crypto_attr_type *algt; + u32 mask; struct aead_instance *inst; struct crypto_aead_spawn *spawn; struct aead_alg *alg; @@ -879,6 +880,8 @@ static int crypto_rfc4106_create(struct crypto_template *tmpl, if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + ccm_name = crypto_attr_alg_name(tb[1]); if (IS_ERR(ccm_name)) return PTR_ERR(ccm_name); @@ -888,9 +891,8 @@ static int crypto_rfc4106_create(struct crypto_template *tmpl, return -ENOMEM; spawn = aead_instance_ctx(inst); - crypto_set_aead_spawn(spawn, aead_crypto_instance(inst)); - err = crypto_grab_aead(spawn, ccm_name, 0, - crypto_requires_sync(algt->type, algt->mask)); + err = crypto_grab_aead(spawn, aead_crypto_instance(inst), + ccm_name, 0, mask); if (err) goto out_free_inst; @@ -1102,6 +1104,7 @@ static int crypto_rfc4543_create(struct crypto_template *tmpl, struct rtattr **tb) { struct crypto_attr_type *algt; + u32 mask; struct aead_instance *inst; struct crypto_aead_spawn *spawn; struct aead_alg *alg; @@ -1116,6 +1119,8 @@ static int crypto_rfc4543_create(struct crypto_template *tmpl, if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + ccm_name = crypto_attr_alg_name(tb[1]); if (IS_ERR(ccm_name)) return PTR_ERR(ccm_name); @@ -1126,9 +1131,8 @@ static int crypto_rfc4543_create(struct crypto_template *tmpl, ctx = aead_instance_ctx(inst); spawn = &ctx->aead; - crypto_set_aead_spawn(spawn, aead_crypto_instance(inst)); - err = crypto_grab_aead(spawn, ccm_name, 0, - crypto_requires_sync(algt->type, algt->mask)); + err = crypto_grab_aead(spawn, aead_crypto_instance(inst), + ccm_name, 0, mask); if (err) goto out_free_inst; diff --git a/crypto/geniv.c b/crypto/geniv.c index b9e45a2a98b5..7afa48414f3a 100644 --- a/crypto/geniv.c +++ b/crypto/geniv.c @@ -64,8 +64,8 @@ struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl, /* Ignore async algorithms if necessary. */ mask |= crypto_requires_sync(algt->type, algt->mask); - crypto_set_aead_spawn(spawn, aead_crypto_instance(inst)); - err = crypto_grab_aead(spawn, name, type, mask); + err = crypto_grab_aead(spawn, aead_crypto_instance(inst), + name, type, mask); if (err) goto err_free_inst; diff --git a/crypto/pcrypt.c b/crypto/pcrypt.c index d6696e217128..1b632139a8c1 100644 --- a/crypto/pcrypt.c +++ b/crypto/pcrypt.c @@ -258,9 +258,8 @@ static int pcrypt_create_aead(struct crypto_template *tmpl, struct rtattr **tb, if (!ctx->psdec) goto out_free_psenc; - crypto_set_aead_spawn(&ctx->spawn, aead_crypto_instance(inst)); - - err = crypto_grab_aead(&ctx->spawn, name, 0, 0); + err = crypto_grab_aead(&ctx->spawn, aead_crypto_instance(inst), + name, 0, 0); if (err) goto out_free_psdec; diff --git a/include/crypto/internal/aead.h b/include/crypto/internal/aead.h index 374185a7567f..27b7b0224ea6 100644 --- a/include/crypto/internal/aead.h +++ b/include/crypto/internal/aead.h @@ -81,14 +81,9 @@ static inline struct aead_request *aead_request_cast( return container_of(req, struct aead_request, base); } -static inline void crypto_set_aead_spawn( - struct crypto_aead_spawn *spawn, struct crypto_instance *inst) -{ - crypto_set_spawn(&spawn->base, inst); -} - -int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name, - u32 type, u32 mask); +int crypto_grab_aead(struct crypto_aead_spawn *spawn, + struct crypto_instance *inst, + const char *name, u32 type, u32 mask); static inline void crypto_drop_aead(struct crypto_aead_spawn *spawn) { From patchwork Fri Jan 3 03:58:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316351 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B64391580 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9575724654 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024089; bh=hZFeqYAzEsmlkEGrVH+RMJl1s1c118MVu3XWCo3kdLY=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=kkkxocBi5KZM+QQv2xMncJfXUiq1ybToJHNZXjflLf+tuY5Lw3HsQkUMlIrurW3Cg djnU8y9fvJD/E4y0KBZ+RvBuodNiBEZK9rasgnYaH5t85YVlE9Oaa1CM6ni9EY65AS zC9P8iiW8FhrshV1DFeD5cz9X+5S4Xk+k5yF2ucs= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727163AbgACEB0 (ORCPT ); Thu, 2 Jan 2020 23:01:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:33508 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726792AbgACEB0 (ORCPT ); Thu, 2 Jan 2020 23:01:26 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 775C822314 for ; Fri, 3 Jan 2020 04:01:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024085; bh=hZFeqYAzEsmlkEGrVH+RMJl1s1c118MVu3XWCo3kdLY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=pHnb8Gt25df9fCc8Np1J48BYikvAS/0ujnFEVW/9c6u+Bg5Xpo1f1IgzvOYCGHF7v hmcOKoPhGJ22dTaCdrv6f7VAXCP8LJxAU6fyFLDLZyMU+G/h/K1Cx6aSG/OOGNEfoV vv3xGW0c6VE6EAKRPKtByvNkUC56NWbEPDfLelOk= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 07/28] crypto: akcipher - pass instance to crypto_grab_akcipher() Date: Thu, 2 Jan 2020 19:58:47 -0800 Message-Id: <20200103035908.12048-8-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Initializing a crypto_akcipher_spawn currently requires: 1. Set spawn->base.inst to point to the instance. 2. Call crypto_grab_akcipher(). But there's no reason for these steps to be separate, and in fact this unneeded complication has caused at least one bug, the one fixed by commit 6db43410179b ("crypto: adiantum - initialize crypto_spawn::inst") So just make crypto_grab_akcipher() take the instance as an argument. To keep the function call from getting too unwieldy due to this extra argument, also introduce a 'mask' variable into pkcs1pad_create(). Signed-off-by: Eric Biggers --- crypto/akcipher.c | 6 ++++-- crypto/rsa-pkcs1pad.c | 8 +++++--- include/crypto/internal/akcipher.h | 12 +++--------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/crypto/akcipher.c b/crypto/akcipher.c index 7d5cf4939423..84ccf9b02bbe 100644 --- a/crypto/akcipher.c +++ b/crypto/akcipher.c @@ -90,9 +90,11 @@ static const struct crypto_type crypto_akcipher_type = { .tfmsize = offsetof(struct crypto_akcipher, base), }; -int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn, const char *name, - u32 type, u32 mask) +int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn, + struct crypto_instance *inst, + const char *name, u32 type, u32 mask) { + spawn->base.inst = inst; spawn->base.frontend = &crypto_akcipher_type; return crypto_grab_spawn(&spawn->base, name, type, mask); } diff --git a/crypto/rsa-pkcs1pad.c b/crypto/rsa-pkcs1pad.c index 0aa489711ec4..176b63afec8d 100644 --- a/crypto/rsa-pkcs1pad.c +++ b/crypto/rsa-pkcs1pad.c @@ -598,6 +598,7 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb) { const struct rsa_asn1_template *digest_info; struct crypto_attr_type *algt; + u32 mask; struct akcipher_instance *inst; struct pkcs1pad_inst_ctx *ctx; struct crypto_akcipher_spawn *spawn; @@ -613,6 +614,8 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb) if ((algt->type ^ CRYPTO_ALG_TYPE_AKCIPHER) & algt->mask) return -EINVAL; + mask = crypto_requires_sync(algt->type, algt->mask); + rsa_alg_name = crypto_attr_alg_name(tb[1]); if (IS_ERR(rsa_alg_name)) return PTR_ERR(rsa_alg_name); @@ -636,9 +639,8 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb) spawn = &ctx->spawn; ctx->digest_info = digest_info; - crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst)); - err = crypto_grab_akcipher(spawn, rsa_alg_name, 0, - crypto_requires_sync(algt->type, algt->mask)); + err = crypto_grab_akcipher(spawn, akcipher_crypto_instance(inst), + rsa_alg_name, 0, mask); if (err) goto out_free_inst; diff --git a/include/crypto/internal/akcipher.h b/include/crypto/internal/akcipher.h index d6c8a42789ad..8d3220c9ab77 100644 --- a/include/crypto/internal/akcipher.h +++ b/include/crypto/internal/akcipher.h @@ -78,15 +78,9 @@ static inline void *akcipher_instance_ctx(struct akcipher_instance *inst) return crypto_instance_ctx(akcipher_crypto_instance(inst)); } -static inline void crypto_set_akcipher_spawn( - struct crypto_akcipher_spawn *spawn, - struct crypto_instance *inst) -{ - crypto_set_spawn(&spawn->base, inst); -} - -int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn, const char *name, - u32 type, u32 mask); +int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn, + struct crypto_instance *inst, + const char *name, u32 type, u32 mask); static inline struct crypto_akcipher *crypto_spawn_akcipher( struct crypto_akcipher_spawn *spawn) From patchwork Fri Jan 3 03:58:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316361 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 964A91580 for ; Fri, 3 Jan 2020 04:01:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 752AD21D7D for ; Fri, 3 Jan 2020 04:01:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024090; bh=T3wXyJUd3JddE1g429EumTNn10IDMcTcuSRCr0rOByY=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=mzTiCBAVqe69eF5hrYbRPuTRMr1D3IFwt69PClCoZkO4E5Xjv4QXO5PUylQcRqPMc SW1OVRSPdjGVw/KF2TswlT0q/nvQ2kS99TgNytDjerY20I0A4h7Rw+TCAEk9rh/IM8 ueCAppe7xLcrhUqkDIzPgCq9yD6j3fGMo3PcEYRs= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727220AbgACEB2 (ORCPT ); Thu, 2 Jan 2020 23:01:28 -0500 Received: from mail.kernel.org ([198.145.29.99]:33554 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726292AbgACEB0 (ORCPT ); Thu, 2 Jan 2020 23:01:26 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A78352253D for ; Fri, 3 Jan 2020 04:01:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024085; bh=T3wXyJUd3JddE1g429EumTNn10IDMcTcuSRCr0rOByY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=GZpeq/DJYh4gMX/n3j4X5P3+/crzElntoeY4269uBsC1wuQ8TQBarblwUzWVi4sPM kE8CZgFO+evP0S7euOSASUhOx2Yp+DnwXsIo0d5F/O7hJyEWlKuijVrv0HRwiu/yDw bQuT+kjDbjviiOP9vQbEk64pUxMhqew8uWHIJvkU= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 08/28] crypto: algapi - pass instance to crypto_grab_spawn() Date: Thu, 2 Jan 2020 19:58:48 -0800 Message-Id: <20200103035908.12048-9-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Currently, crypto_spawn::inst is first used temporarily to pass the instance to crypto_grab_spawn(). Then crypto_init_spawn() overwrites it with crypto_spawn::next, which shares the same union. Finally, crypto_spawn::inst is set again when the instance is registered. Make this less convoluted by just passing the instance as an argument to crypto_grab_spawn() instead. Signed-off-by: Eric Biggers --- crypto/adiantum.c | 6 +++--- crypto/aead.c | 3 +-- crypto/akcipher.c | 3 +-- crypto/algapi.c | 6 +++--- crypto/skcipher.c | 3 +-- include/crypto/algapi.h | 10 ++-------- 6 files changed, 11 insertions(+), 20 deletions(-) diff --git a/crypto/adiantum.c b/crypto/adiantum.c index aaf8a66f871c..9e44180111c8 100644 --- a/crypto/adiantum.c +++ b/crypto/adiantum.c @@ -550,9 +550,9 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) streamcipher_alg = crypto_spawn_skcipher_alg(&ictx->streamcipher_spawn); /* Block cipher, e.g. "aes" */ - crypto_set_spawn(&ictx->blockcipher_spawn, - skcipher_crypto_instance(inst)); - err = crypto_grab_spawn(&ictx->blockcipher_spawn, blockcipher_name, + err = crypto_grab_spawn(&ictx->blockcipher_spawn, + skcipher_crypto_instance(inst), + blockcipher_name, CRYPTO_ALG_TYPE_CIPHER, CRYPTO_ALG_TYPE_MASK); if (err) goto out_drop_streamcipher; diff --git a/crypto/aead.c b/crypto/aead.c index c7135e00b8ea..02a0db076d7e 100644 --- a/crypto/aead.c +++ b/crypto/aead.c @@ -211,9 +211,8 @@ int crypto_grab_aead(struct crypto_aead_spawn *spawn, struct crypto_instance *inst, const char *name, u32 type, u32 mask) { - spawn->base.inst = inst; spawn->base.frontend = &crypto_aead_type; - return crypto_grab_spawn(&spawn->base, name, type, mask); + return crypto_grab_spawn(&spawn->base, inst, name, type, mask); } EXPORT_SYMBOL_GPL(crypto_grab_aead); diff --git a/crypto/akcipher.c b/crypto/akcipher.c index 84ccf9b02bbe..eeed6c151d2f 100644 --- a/crypto/akcipher.c +++ b/crypto/akcipher.c @@ -94,9 +94,8 @@ int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn, struct crypto_instance *inst, const char *name, u32 type, u32 mask) { - spawn->base.inst = inst; spawn->base.frontend = &crypto_akcipher_type; - return crypto_grab_spawn(&spawn->base, name, type, mask); + return crypto_grab_spawn(&spawn->base, inst, name, type, mask); } EXPORT_SYMBOL_GPL(crypto_grab_akcipher); diff --git a/crypto/algapi.c b/crypto/algapi.c index a5223c5f2275..a25ce02918f8 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -714,8 +714,8 @@ int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg, } EXPORT_SYMBOL_GPL(crypto_init_spawn2); -int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name, - u32 type, u32 mask) +int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst, + const char *name, u32 type, u32 mask) { struct crypto_alg *alg; int err; @@ -729,7 +729,7 @@ int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name, return PTR_ERR(alg); spawn->dropref = true; - err = crypto_init_spawn(spawn, alg, spawn->inst, mask); + err = crypto_init_spawn(spawn, alg, inst, mask); if (err) crypto_mod_put(alg); return err; diff --git a/crypto/skcipher.c b/crypto/skcipher.c index e5083dccccdc..a9418a7e80a9 100644 --- a/crypto/skcipher.c +++ b/crypto/skcipher.c @@ -761,9 +761,8 @@ int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, struct crypto_instance *inst, const char *name, u32 type, u32 mask) { - spawn->base.inst = inst; spawn->base.frontend = &crypto_skcipher_type; - return crypto_grab_spawn(&spawn->base, name, type, mask); + return crypto_grab_spawn(&spawn->base, inst, name, type, mask); } EXPORT_SYMBOL_GPL(crypto_grab_skcipher); diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h index 5022cada4fc6..2779c8d34ba9 100644 --- a/include/crypto/algapi.h +++ b/include/crypto/algapi.h @@ -116,20 +116,14 @@ int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg, int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg, struct crypto_instance *inst, const struct crypto_type *frontend); -int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name, - u32 type, u32 mask); +int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst, + const char *name, u32 type, u32 mask); void crypto_drop_spawn(struct crypto_spawn *spawn); struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, u32 mask); void *crypto_spawn_tfm2(struct crypto_spawn *spawn); -static inline void crypto_set_spawn(struct crypto_spawn *spawn, - struct crypto_instance *inst) -{ - spawn->inst = inst; -} - struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb); int crypto_check_attr_type(struct rtattr **tb, u32 type); const char *crypto_attr_alg_name(struct rtattr *rta); From patchwork Fri Jan 3 03:58:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316359 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6B65A17F0 for ; Fri, 3 Jan 2020 04:01:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4AB5321D7D for ; Fri, 3 Jan 2020 04:01:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024090; bh=FN1XiRPNgcl1byUgyyMQgAKON6dwUAIQGzOeQAYdqfw=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=dItu6rTYJD7AaC8eeZrXHkQuYc/28zIZFZjIqYRbGXaqwJY+xLUCOYjEGhJBmedfe wQHATbXGe42vfIb0gO5D0ezhyZb+4xRFfTdUvFJu833ZEVrPWXIXZ38jbmt61BbBnF JMc+5JTKEnlebJAEH4WUs7flxHPZUcDepenNMpAE= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727145AbgACEB1 (ORCPT ); Thu, 2 Jan 2020 23:01:27 -0500 Received: from mail.kernel.org ([198.145.29.99]:33558 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726837AbgACEB0 (ORCPT ); Thu, 2 Jan 2020 23:01:26 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D8A7122525 for ; Fri, 3 Jan 2020 04:01:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024085; bh=FN1XiRPNgcl1byUgyyMQgAKON6dwUAIQGzOeQAYdqfw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=RLya6r+Qig9q2JPT7vysRyqBEeJPSgRYj5oayfDPnfkFXij2uQPDZf7WI0tiOp9mI kZ+tJa+lVBHemSwM14IycgTd0kaHVJxPucdUuC90R3b2uLi+BeULi3R9SHC2nGXr9d 6hW02dDyMBTF/G7DFp5frI8suKDiJgqBQxCeeX3Y= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 09/28] crypto: shash - introduce crypto_grab_shash() Date: Thu, 2 Jan 2020 19:58:49 -0800 Message-Id: <20200103035908.12048-10-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Currently, shash spawns are initialized by using shash_attr_alg() or crypto_alg_mod_lookup() to look up the shash algorithm, then calling crypto_init_shash_spawn(). This is different from how skcipher, aead, and akcipher spawns are initialized (they use crypto_grab_*()), and for no good reason. This difference introduces unnecessary complexity. The crypto_grab_*() functions used to have some problems, like not holding a reference to the algorithm and requiring the caller to initialize spawn->base.inst. But those problems are fixed now. So, let's introduce crypto_grab_shash() so that we can convert all templates to the same way of initializing their spawns. Signed-off-by: Eric Biggers --- crypto/shash.c | 9 +++++++++ include/crypto/internal/hash.h | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/crypto/shash.c b/crypto/shash.c index 7243f60dab87..e0872ac2729a 100644 --- a/crypto/shash.c +++ b/crypto/shash.c @@ -469,6 +469,15 @@ static const struct crypto_type crypto_shash_type = { .tfmsize = offsetof(struct crypto_shash, base), }; +int crypto_grab_shash(struct crypto_shash_spawn *spawn, + struct crypto_instance *inst, + const char *name, u32 type, u32 mask) +{ + spawn->base.frontend = &crypto_shash_type; + return crypto_grab_spawn(&spawn->base, inst, name, type, mask); +} +EXPORT_SYMBOL_GPL(crypto_grab_shash); + struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, u32 mask) { diff --git a/include/crypto/internal/hash.h b/include/crypto/internal/hash.h index 3b426b09bd32..4d1a0d8e4f3a 100644 --- a/include/crypto/internal/hash.h +++ b/include/crypto/internal/hash.h @@ -128,11 +128,21 @@ int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn, struct shash_alg *alg, struct crypto_instance *inst); +int crypto_grab_shash(struct crypto_shash_spawn *spawn, + struct crypto_instance *inst, + const char *name, u32 type, u32 mask); + static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn) { crypto_drop_spawn(&spawn->base); } +static inline struct shash_alg *crypto_spawn_shash_alg( + struct crypto_shash_spawn *spawn) +{ + return __crypto_shash_alg(spawn->base.alg); +} + struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask); int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc); From patchwork Fri Jan 3 03:58:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316353 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E0FD56C1 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BFF9924654 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024089; bh=axmFbQoKU2rhg2ddd/chkcLSQ3CDgMpshQeAmkY/HBg=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=0DQsW0Zaw/UvsIAVOnrprK9QvLW1xs98gsKFK5yk76ff5BpAaryaaId5Gk+jbYArD tMRh2ezlUwSNPqUNsE+oN9hcmTYBwSm024Cnq6kq1Y2PRaykhNmR/kL0YJjIKa8Jmf 3RLQvGAF/WZAsun5CQPnLMowBQSf3IGw9DuVzzUo= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727168AbgACEB0 (ORCPT ); Thu, 2 Jan 2020 23:01:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:33570 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727134AbgACEB0 (ORCPT ); Thu, 2 Jan 2020 23:01:26 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 128BF227BF for ; Fri, 3 Jan 2020 04:01:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024086; bh=axmFbQoKU2rhg2ddd/chkcLSQ3CDgMpshQeAmkY/HBg=; h=From:To:Subject:Date:In-Reply-To:References:From; b=dTXmBsa4WZYDOVhaSjGS7BGg4HJJdIewngN5AMdehOUhtpvMdiSWa106kOfNS49i3 MYODsULkcun9F30jiPr25+VvN6E+6I5KtwpXwwvPajQF/sysykqfkREiuXgN1TOAue 2nte0hBLetUAduwlShWCbqE6fWjmIX+4inb6qiLU= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 10/28] crypto: ahash - introduce crypto_grab_ahash() Date: Thu, 2 Jan 2020 19:58:50 -0800 Message-Id: <20200103035908.12048-11-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Currently, ahash spawns are initialized by using ahash_attr_alg() or crypto_find_alg() to look up the ahash algorithm, then calling crypto_init_ahash_spawn(). This is different from how skcipher, aead, and akcipher spawns are initialized (they use crypto_grab_*()), and for no good reason. This difference introduces unnecessary complexity. The crypto_grab_*() functions used to have some problems, like not holding a reference to the algorithm and requiring the caller to initialize spawn->base.inst. But those problems are fixed now. So, let's introduce crypto_grab_ahash() so that we can convert all templates to the same way of initializing their spawns. Signed-off-by: Eric Biggers --- crypto/ahash.c | 9 +++++++++ include/crypto/internal/hash.h | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/crypto/ahash.c b/crypto/ahash.c index 181bd851b429..e98a1398ed7f 100644 --- a/crypto/ahash.c +++ b/crypto/ahash.c @@ -556,6 +556,15 @@ const struct crypto_type crypto_ahash_type = { }; EXPORT_SYMBOL_GPL(crypto_ahash_type); +int crypto_grab_ahash(struct crypto_ahash_spawn *spawn, + struct crypto_instance *inst, + const char *name, u32 type, u32 mask) +{ + spawn->base.frontend = &crypto_ahash_type; + return crypto_grab_spawn(&spawn->base, inst, name, type, mask); +} +EXPORT_SYMBOL_GPL(crypto_grab_ahash); + struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type, u32 mask) { diff --git a/include/crypto/internal/hash.h b/include/crypto/internal/hash.h index 4d1a0d8e4f3a..e1024fa0032f 100644 --- a/include/crypto/internal/hash.h +++ b/include/crypto/internal/hash.h @@ -109,11 +109,21 @@ int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn, struct hash_alg_common *alg, struct crypto_instance *inst); +int crypto_grab_ahash(struct crypto_ahash_spawn *spawn, + struct crypto_instance *inst, + const char *name, u32 type, u32 mask); + static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn) { crypto_drop_spawn(&spawn->base); } +static inline struct hash_alg_common *crypto_spawn_ahash_alg( + struct crypto_ahash_spawn *spawn) +{ + return __crypto_hash_alg_common(spawn->base.alg); +} + struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask); int crypto_register_shash(struct shash_alg *alg); From patchwork Fri Jan 3 03:58:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316357 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4173218C6 for ; Fri, 3 Jan 2020 04:01:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2046424654 for ; Fri, 3 Jan 2020 04:01:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024090; bh=D6Bu9h7hmOJg0m8gd7GKpA4ERivZRyr1Jj0yFjm1CGk=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=owg4pReroJCBRo6L/q6zix9gjXwJx1luZZ4v/JlXVrUHuuv3/AzxXLgafQo4HiQ2r eDOVwyYuCEORp3MSyu5l0DJ5TmOVLReYcR05jpJZynLAZTMKTV9j31R6qj07aVN95l f9nVBK/Z43FRvgn+HrG8aCJSH0gSRcFKhjqNxj6U= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727134AbgACEB1 (ORCPT ); Thu, 2 Jan 2020 23:01:27 -0500 Received: from mail.kernel.org ([198.145.29.99]:33528 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727145AbgACEB0 (ORCPT ); Thu, 2 Jan 2020 23:01:26 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 41FE622B48 for ; Fri, 3 Jan 2020 04:01:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024086; bh=D6Bu9h7hmOJg0m8gd7GKpA4ERivZRyr1Jj0yFjm1CGk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=1le0w7VA4CFDfOy2vUn8Yb2r6RU8MXipybYBGDVVQRS4io4Fhj5ejVIDACF3JPaTk fTLSEAysJCwhkh8R8H211BmNTHZ0Gb7r9wE0Lhq3jLBZ9vxzAfBoFRmGLbVUummFL4 DV/15oAkgPiOOGXj5whcePuQ3LVAjiR8T4YygIf8= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 11/28] crypto: cipher - introduce crypto_cipher_spawn and crypto_grab_cipher() Date: Thu, 2 Jan 2020 19:58:51 -0800 Message-Id: <20200103035908.12048-12-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Currently, "cipher" (single-block cipher) spawns are usually initialized by using crypto_get_attr_alg() to look up the algorithm, then calling crypto_init_spawn(). In one case, crypto_grab_spawn() is used directly. The former way is different from how skcipher, aead, and akcipher spawns are initialized (they use crypto_grab_*()), and for no good reason. This difference introduces unnecessary complexity. The crypto_grab_*() functions used to have some problems, like not holding a reference to the algorithm and requiring the caller to initialize spawn->base.inst. But those problems are fixed now. Also, the cipher spawns are not strongly typed; e.g., the API requires that the user manually specify the flags CRYPTO_ALG_TYPE_CIPHER and CRYPTO_ALG_TYPE_MASK. Though the "cipher" algorithm type itself isn't yet strongly typed, we can start by making the spawns strongly typed. So, let's introduce a new 'struct crypto_cipher_spawn', and functions crypto_grab_cipher() and crypto_drop_cipher() to grab and drop them. Later patches will convert all cipher spawns to use these, then make crypto_spawn_cipher() take 'struct crypto_cipher_spawn' as well, instead of a bare 'struct crypto_spawn' as it currently does. Signed-off-by: Eric Biggers --- include/crypto/algapi.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h index 2779c8d34ba9..7705387f9459 100644 --- a/include/crypto/algapi.h +++ b/include/crypto/algapi.h @@ -208,6 +208,31 @@ static inline void *crypto_instance_ctx(struct crypto_instance *inst) return inst->__ctx; } +struct crypto_cipher_spawn { + struct crypto_spawn base; +}; + +static inline int crypto_grab_cipher(struct crypto_cipher_spawn *spawn, + struct crypto_instance *inst, + const char *name, u32 type, u32 mask) +{ + type &= ~CRYPTO_ALG_TYPE_MASK; + type |= CRYPTO_ALG_TYPE_CIPHER; + mask |= CRYPTO_ALG_TYPE_MASK; + return crypto_grab_spawn(&spawn->base, inst, name, type, mask); +} + +static inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn) +{ + crypto_drop_spawn(&spawn->base); +} + +static inline struct crypto_alg *crypto_spawn_cipher_alg( + struct crypto_cipher_spawn *spawn) +{ + return spawn->base.alg; +} + static inline struct crypto_cipher *crypto_spawn_cipher( struct crypto_spawn *spawn) { From patchwork Fri Jan 3 03:58:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316393 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7CBAD139A for ; Fri, 3 Jan 2020 04:01:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 52D95222C3 for ; Fri, 3 Jan 2020 04:01:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024098; bh=VAy7i9KLF+tp4M9JhwoVIwor8TQDhSF7HRM9Xv2hSLM=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=UiZRVgaMY3uBkm2bBs20prpdc6MTbnuQGKF0IIHst5dbF83UfhA6oLJ6SmLNoC+AU QoEZQNiARdt+cNgGk2q4jlO3YaZNxMrtdbRidGFlgSv70gu76tTNxCB01tmDHyE0hr AyZ96PU8bBVz3CLtEv0upYXLB4f/0cLy8IznYE2A= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726481AbgACEBh (ORCPT ); Thu, 2 Jan 2020 23:01:37 -0500 Received: from mail.kernel.org ([198.145.29.99]:33508 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726792AbgACEB1 (ORCPT ); Thu, 2 Jan 2020 23:01:27 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 73ED822314 for ; Fri, 3 Jan 2020 04:01:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024086; bh=VAy7i9KLF+tp4M9JhwoVIwor8TQDhSF7HRM9Xv2hSLM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=zlX1uGc0xeCVEKD7Y/Qjbw4bk/Hd2mk9mCIuOKUnHDEKLP1oSgE7JNm/LR7WvmxdO YuEZWdvt9jvcJ5B/j9kU9l9kXR9IDP4/IFj15CVB6+jyqN5B5eNDn1Jrr5P0Oia6gf mXaGl59Etrk6I99+hO6AVKu2LxyPLjKTQvOVCEgI= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 12/28] crypto: adiantum - use crypto_grab_{cipher,shash} and simplify error paths Date: Thu, 2 Jan 2020 19:58:52 -0800 Message-Id: <20200103035908.12048-13-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the adiantum template use the new functions crypto_grab_cipher() and crypto_grab_shash() to initialize its cipher and shash spawns. This is needed to make all spawns be initialized in a consistent way. Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers --- crypto/adiantum.c | 85 ++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 60 deletions(-) diff --git a/crypto/adiantum.c b/crypto/adiantum.c index 9e44180111c8..8abaecde1464 100644 --- a/crypto/adiantum.c +++ b/crypto/adiantum.c @@ -39,8 +39,6 @@ #include #include -#include "internal.h" - /* * Size of right-hand part of input data, in bytes; also the size of the block * cipher's block size and the hash function's output. @@ -64,7 +62,7 @@ struct adiantum_instance_ctx { struct crypto_skcipher_spawn streamcipher_spawn; - struct crypto_spawn blockcipher_spawn; + struct crypto_cipher_spawn blockcipher_spawn; struct crypto_shash_spawn hash_spawn; }; @@ -418,7 +416,7 @@ static int adiantum_init_tfm(struct crypto_skcipher *tfm) if (IS_ERR(streamcipher)) return PTR_ERR(streamcipher); - blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn); + blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn.base); if (IS_ERR(blockcipher)) { err = PTR_ERR(blockcipher); goto err_free_streamcipher; @@ -469,7 +467,7 @@ static void adiantum_free_instance(struct skcipher_instance *inst) struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst); crypto_drop_skcipher(&ictx->streamcipher_spawn); - crypto_drop_spawn(&ictx->blockcipher_spawn); + crypto_drop_cipher(&ictx->blockcipher_spawn); crypto_drop_shash(&ictx->hash_spawn); kfree(inst); } @@ -502,14 +500,11 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) { struct crypto_attr_type *algt; u32 mask; - const char *streamcipher_name; - const char *blockcipher_name; const char *nhpoly1305_name; struct skcipher_instance *inst; struct adiantum_instance_ctx *ictx; struct skcipher_alg *streamcipher_alg; struct crypto_alg *blockcipher_alg; - struct crypto_alg *_hash_alg; struct shash_alg *hash_alg; int err; @@ -522,20 +517,6 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) mask = crypto_requires_sync(algt->type, algt->mask); - streamcipher_name = crypto_attr_alg_name(tb[1]); - if (IS_ERR(streamcipher_name)) - return PTR_ERR(streamcipher_name); - - blockcipher_name = crypto_attr_alg_name(tb[2]); - if (IS_ERR(blockcipher_name)) - return PTR_ERR(blockcipher_name); - - nhpoly1305_name = crypto_attr_alg_name(tb[3]); - if (nhpoly1305_name == ERR_PTR(-ENOENT)) - nhpoly1305_name = "nhpoly1305"; - if (IS_ERR(nhpoly1305_name)) - return PTR_ERR(nhpoly1305_name); - inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL); if (!inst) return -ENOMEM; @@ -544,33 +525,29 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) /* Stream cipher, e.g. "xchacha12" */ err = crypto_grab_skcipher(&ictx->streamcipher_spawn, skcipher_crypto_instance(inst), - streamcipher_name, 0, mask); + crypto_attr_alg_name(tb[1]), 0, mask); if (err) - goto out_free_inst; + goto err_free_inst; streamcipher_alg = crypto_spawn_skcipher_alg(&ictx->streamcipher_spawn); /* Block cipher, e.g. "aes" */ - err = crypto_grab_spawn(&ictx->blockcipher_spawn, - skcipher_crypto_instance(inst), - blockcipher_name, - CRYPTO_ALG_TYPE_CIPHER, CRYPTO_ALG_TYPE_MASK); + err = crypto_grab_cipher(&ictx->blockcipher_spawn, + skcipher_crypto_instance(inst), + crypto_attr_alg_name(tb[2]), 0, mask); if (err) - goto out_drop_streamcipher; - blockcipher_alg = ictx->blockcipher_spawn.alg; + goto err_free_inst; + blockcipher_alg = crypto_spawn_cipher_alg(&ictx->blockcipher_spawn); /* NHPoly1305 ε-∆U hash function */ - _hash_alg = crypto_alg_mod_lookup(nhpoly1305_name, - CRYPTO_ALG_TYPE_SHASH, - CRYPTO_ALG_TYPE_MASK); - if (IS_ERR(_hash_alg)) { - err = PTR_ERR(_hash_alg); - goto out_drop_blockcipher; - } - hash_alg = __crypto_shash_alg(_hash_alg); - err = crypto_init_shash_spawn(&ictx->hash_spawn, hash_alg, - skcipher_crypto_instance(inst)); + nhpoly1305_name = crypto_attr_alg_name(tb[3]); + if (nhpoly1305_name == ERR_PTR(-ENOENT)) + nhpoly1305_name = "nhpoly1305"; + err = crypto_grab_shash(&ictx->hash_spawn, + skcipher_crypto_instance(inst), + nhpoly1305_name, 0, mask); if (err) - goto out_put_hash; + goto err_free_inst; + hash_alg = crypto_spawn_shash_alg(&ictx->hash_spawn); /* Check the set of algorithms */ if (!adiantum_supported_algorithms(streamcipher_alg, blockcipher_alg, @@ -579,7 +556,7 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) streamcipher_alg->base.cra_name, blockcipher_alg->cra_name, hash_alg->base.cra_name); err = -EINVAL; - goto out_drop_hash; + goto err_free_inst; } /* Instance fields */ @@ -588,13 +565,13 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, "adiantum(%s,%s)", streamcipher_alg->base.cra_name, blockcipher_alg->cra_name) >= CRYPTO_MAX_ALG_NAME) - goto out_drop_hash; + goto err_free_inst; if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "adiantum(%s,%s,%s)", streamcipher_alg->base.cra_driver_name, blockcipher_alg->cra_driver_name, hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) - goto out_drop_hash; + goto err_free_inst; inst->alg.base.cra_flags = streamcipher_alg->base.cra_flags & CRYPTO_ALG_ASYNC; @@ -624,22 +601,10 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) inst->free = adiantum_free_instance; err = skcipher_register_instance(tmpl, inst); - if (err) - goto out_drop_hash; - - crypto_mod_put(_hash_alg); - return 0; - -out_drop_hash: - crypto_drop_shash(&ictx->hash_spawn); -out_put_hash: - crypto_mod_put(_hash_alg); -out_drop_blockcipher: - crypto_drop_spawn(&ictx->blockcipher_spawn); -out_drop_streamcipher: - crypto_drop_skcipher(&ictx->streamcipher_spawn); -out_free_inst: - kfree(inst); + if (err) { +err_free_inst: + adiantum_free_instance(inst); + } return err; } From patchwork Fri Jan 3 03:58:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316397 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 69E6D139A for ; Fri, 3 Jan 2020 04:01:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4898A222C3 for ; Fri, 3 Jan 2020 04:01:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024099; bh=BxDmQFQEkRoqmQOoVJ48e4mMgrQdpm4kPf07VDLLxNM=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=h/uutTB2DSq8LSEHqH7cuH89FpU77Sk8vilMWmS8cIukQ7KjMWuY3f9Vh+u5rwfMT FYb9DQLOPL2hPGquh72qAfM2/jL0Otfg2UHl8unW/DONsx51CMPIEZ/b8criz5aVUY nqAez3OrA2jI2l0Bszs6TNatexUcYZSmVqdBWD0M= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726792AbgACEBh (ORCPT ); Thu, 2 Jan 2020 23:01:37 -0500 Received: from mail.kernel.org ([198.145.29.99]:33554 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726504AbgACEB1 (ORCPT ); Thu, 2 Jan 2020 23:01:27 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A38EA2253D for ; Fri, 3 Jan 2020 04:01:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024086; bh=BxDmQFQEkRoqmQOoVJ48e4mMgrQdpm4kPf07VDLLxNM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Z/sOqWcxkq8g272DBcLzs0BN42N7FDSPVz2D04yguQBgtWxs5kV/VM/Qr0mb3Z7g6 fvxztNbwQC+qPYdGWnf0U4XKaj/9e/c/D2TCKBn66qS29yTmj8ux9Hqc0bV2TGwzRE +kypzCRK5n6mJF+L0hE/WohHIV17B71/Q2TPN+Rg= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 13/28] crypto: cryptd - use crypto_grab_shash() and simplify error paths Date: Thu, 2 Jan 2020 19:58:53 -0800 Message-Id: <20200103035908.12048-14-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the cryptd template (in the hash case) use the new function crypto_grab_shash() to initialize its shash spawn. This is needed to make all spawns be initialized in a consistent way. This required making cryptd_create_hash() allocate the instance directly rather than use cryptd_alloc_instance(). Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers --- crypto/cryptd.c | 68 +++++++++++++------------------------------------ 1 file changed, 18 insertions(+), 50 deletions(-) diff --git a/crypto/cryptd.c b/crypto/cryptd.c index 5aea6d6c49a0..3224f142c824 100644 --- a/crypto/cryptd.c +++ b/crypto/cryptd.c @@ -221,32 +221,6 @@ static int cryptd_init_instance(struct crypto_instance *inst, return 0; } -static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head, - unsigned int tail) -{ - char *p; - struct crypto_instance *inst; - int err; - - p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL); - if (!p) - return ERR_PTR(-ENOMEM); - - inst = (void *)(p + head); - - err = cryptd_init_instance(inst, alg); - if (err) - goto out_free_inst; - -out: - return p; - -out_free_inst: - kfree(p); - p = ERR_PTR(err); - goto out; -} - static int cryptd_skcipher_setkey(struct crypto_skcipher *parent, const u8 *key, unsigned int keylen) { @@ -671,39 +645,36 @@ static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb, { struct hashd_instance_ctx *ctx; struct ahash_instance *inst; - struct shash_alg *salg; - struct crypto_alg *alg; + struct shash_alg *alg; u32 type = 0; u32 mask = 0; int err; cryptd_check_internal(tb, &type, &mask); - salg = shash_attr_alg(tb[1], type, mask); - if (IS_ERR(salg)) - return PTR_ERR(salg); - - alg = &salg->base; - inst = cryptd_alloc_instance(alg, ahash_instance_headroom(), - sizeof(*ctx)); - err = PTR_ERR(inst); - if (IS_ERR(inst)) - goto out_put_alg; + inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); + if (!inst) + return -ENOMEM; ctx = ahash_instance_ctx(inst); ctx->queue = queue; - err = crypto_init_shash_spawn(&ctx->spawn, salg, - ahash_crypto_instance(inst)); + err = crypto_grab_shash(&ctx->spawn, ahash_crypto_instance(inst), + crypto_attr_alg_name(tb[1]), type, mask); if (err) - goto out_free_inst; + goto err_free_inst; + alg = crypto_spawn_shash_alg(&ctx->spawn); + + err = cryptd_init_instance(ahash_crypto_instance(inst), &alg->base); + if (err) + goto err_free_inst; inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC | - (alg->cra_flags & (CRYPTO_ALG_INTERNAL | - CRYPTO_ALG_OPTIONAL_KEY)); + (alg->base.cra_flags & (CRYPTO_ALG_INTERNAL | + CRYPTO_ALG_OPTIONAL_KEY)); - inst->alg.halg.digestsize = salg->digestsize; - inst->alg.halg.statesize = salg->statesize; + inst->alg.halg.digestsize = alg->digestsize; + inst->alg.halg.statesize = alg->statesize; inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx); inst->alg.halg.base.cra_init = cryptd_hash_init_tfm; @@ -715,19 +686,16 @@ static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb, inst->alg.finup = cryptd_hash_finup_enqueue; inst->alg.export = cryptd_hash_export; inst->alg.import = cryptd_hash_import; - if (crypto_shash_alg_has_setkey(salg)) + if (crypto_shash_alg_has_setkey(alg)) inst->alg.setkey = cryptd_hash_setkey; inst->alg.digest = cryptd_hash_digest_enqueue; err = ahash_register_instance(tmpl, inst); if (err) { +err_free_inst: crypto_drop_shash(&ctx->spawn); -out_free_inst: kfree(inst); } - -out_put_alg: - crypto_mod_put(alg); return err; } From patchwork Fri Jan 3 03:58:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316389 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 52B1D17F0 for ; Fri, 3 Jan 2020 04:01:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 30C29222C3 for ; Fri, 3 Jan 2020 04:01:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024097; bh=ruT2xBGpJQ0H0TVLybs5CZ6N4Ac42jgoc6/RMOEKKis=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=S1YdR18MdxCJmsioU/2IdqOjT+JjqRo44JY3lVXU5xuVILTAgjek4om2jGt+dZHHp LHaE8/M/gjxoqCiplsekgYJdstH1BXx8wXGvMLwjI33U0AfX348xwNy8bFzP+dprUh VsJ+m8pWtrWftJTB8ASGPT+vaCQAbv6CLAlF+E3w= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727215AbgACEBg (ORCPT ); Thu, 2 Jan 2020 23:01:36 -0500 Received: from mail.kernel.org ([198.145.29.99]:33570 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727175AbgACEB1 (ORCPT ); Thu, 2 Jan 2020 23:01:27 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D24AE222C3 for ; Fri, 3 Jan 2020 04:01:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024086; bh=ruT2xBGpJQ0H0TVLybs5CZ6N4Ac42jgoc6/RMOEKKis=; h=From:To:Subject:Date:In-Reply-To:References:From; b=y+scjNSMd5XGCq20ow0vw7r2BjPEQTwO6d/+9+RhT250sJqTaAnGv5YLkPkNDAfdD XdE5aYrB3WWSx26z4JT0yWfxqZEisLoRvc9KxFf9tvRhSWbFgboraiGgClt9RgX76c uX3usVcNTB1flnv6q2b2BUtblQpeIOx1zOKzfTDQ= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 14/28] crypto: hmac - use crypto_grab_shash() and simplify error paths Date: Thu, 2 Jan 2020 19:58:54 -0800 Message-Id: <20200103035908.12048-15-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the hmac template use the new function crypto_grab_shash() to initialize its shash spawn. This is needed to make all spawns be initialized in a consistent way. This required making hmac_create() allocate the instance directly rather than use shash_alloc_instance(). Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers --- crypto/hmac.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/crypto/hmac.c b/crypto/hmac.c index 685e49953605..0a42b7075763 100644 --- a/crypto/hmac.c +++ b/crypto/hmac.c @@ -165,6 +165,7 @@ static void hmac_exit_tfm(struct crypto_shash *parent) static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb) { struct shash_instance *inst; + struct crypto_shash_spawn *spawn; struct crypto_alg *alg; struct shash_alg *salg; int err; @@ -175,31 +176,32 @@ static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb) if (err) return err; - salg = shash_attr_alg(tb[1], 0, 0); - if (IS_ERR(salg)) - return PTR_ERR(salg); + inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); + if (!inst) + return -ENOMEM; + spawn = shash_instance_ctx(inst); + + err = crypto_grab_shash(spawn, shash_crypto_instance(inst), + crypto_attr_alg_name(tb[1]), 0, 0); + if (err) + goto err_free_inst; + salg = crypto_spawn_shash_alg(spawn); alg = &salg->base; /* The underlying hash algorithm must not require a key */ err = -EINVAL; if (crypto_shash_alg_needs_key(salg)) - goto out_put_alg; + goto err_free_inst; ds = salg->digestsize; ss = salg->statesize; if (ds > alg->cra_blocksize || ss < alg->cra_blocksize) - goto out_put_alg; + goto err_free_inst; - inst = shash_alloc_instance("hmac", alg); - err = PTR_ERR(inst); - if (IS_ERR(inst)) - goto out_put_alg; - - err = crypto_init_shash_spawn(shash_instance_ctx(inst), salg, - shash_crypto_instance(inst)); + err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg); if (err) - goto out_free_inst; + goto err_free_inst; inst->alg.base.cra_priority = alg->cra_priority; inst->alg.base.cra_blocksize = alg->cra_blocksize; @@ -224,12 +226,9 @@ static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb) err = shash_register_instance(tmpl, inst); if (err) { -out_free_inst: +err_free_inst: shash_free_instance(shash_crypto_instance(inst)); } - -out_put_alg: - crypto_mod_put(alg); return err; } From patchwork Fri Jan 3 03:58:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316377 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8F9B417F0 for ; Fri, 3 Jan 2020 04:01:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6EAC6222C3 for ; Fri, 3 Jan 2020 04:01:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024094; bh=Sa6BlByOCaXdGV1JFlq85N8cnQd+GJ++0WOjPxkXZL8=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=mtVttZbsmoEI6ABHVPNYCMwlgcBc2oqXejBLPP60y/+a/5LuHDRr+orSYJVfRKsXJ Vo2w/JaiMJEaLseH40cDgukD3+EibGImMuabvvZ1kh4ccu1cI9tvv5inTB2g6LZXos +DbJERo4ooAqnyI2VlhYavDvxp20jZRKxqJAZPvs= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727186AbgACEBb (ORCPT ); Thu, 2 Jan 2020 23:01:31 -0500 Received: from mail.kernel.org ([198.145.29.99]:33528 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727183AbgACEB2 (ORCPT ); Thu, 2 Jan 2020 23:01:28 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0E02722525 for ; Fri, 3 Jan 2020 04:01:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024087; bh=Sa6BlByOCaXdGV1JFlq85N8cnQd+GJ++0WOjPxkXZL8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=p1f87zdYx+P2/QBUXzfGmYZq4F9fdPOz5XGV5LzpnrjdNOP2bDVd53hbQFVz/UGoJ iEGkhohHem6GjNqpIrvDc+/31PmYuMn2bVOTYS021Bb112nw02pyYfAzPbe9YaJUB8 PH6oc4pNxTATlLGHRSztdJ4vy7j/Gc7C5uhCZL7Y= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 15/28] crypto: authenc - use crypto_grab_ahash() and simplify error paths Date: Thu, 2 Jan 2020 19:58:55 -0800 Message-Id: <20200103035908.12048-16-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the authenc template use the new function crypto_grab_ahash() to initialize its ahash spawn. This is needed to make all spawns be initialized in a consistent way. Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers --- crypto/authenc.c | 52 +++++++++++++----------------------------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/crypto/authenc.c b/crypto/authenc.c index aef04792702a..87133bfd48b9 100644 --- a/crypto/authenc.c +++ b/crypto/authenc.c @@ -385,11 +385,10 @@ static int crypto_authenc_create(struct crypto_template *tmpl, struct crypto_attr_type *algt; u32 mask; struct aead_instance *inst; + struct authenc_instance_ctx *ctx; struct hash_alg_common *auth; struct crypto_alg *auth_base; struct skcipher_alg *enc; - struct authenc_instance_ctx *ctx; - const char *enc_name; int err; algt = crypto_get_attr_type(tb); @@ -401,35 +400,22 @@ static int crypto_authenc_create(struct crypto_template *tmpl, mask = crypto_requires_sync(algt->type, algt->mask); - auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH, - CRYPTO_ALG_TYPE_AHASH_MASK | mask); - if (IS_ERR(auth)) - return PTR_ERR(auth); - - auth_base = &auth->base; - - enc_name = crypto_attr_alg_name(tb[2]); - err = PTR_ERR(enc_name); - if (IS_ERR(enc_name)) - goto out_put_auth; - inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); - err = -ENOMEM; if (!inst) - goto out_put_auth; - + return -ENOMEM; ctx = aead_instance_ctx(inst); - err = crypto_init_ahash_spawn(&ctx->auth, auth, - aead_crypto_instance(inst)); + err = crypto_grab_ahash(&ctx->auth, aead_crypto_instance(inst), + crypto_attr_alg_name(tb[1]), 0, mask); if (err) goto err_free_inst; + auth = crypto_spawn_ahash_alg(&ctx->auth); + auth_base = &auth->base; err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst), - enc_name, 0, mask); + crypto_attr_alg_name(tb[2]), 0, mask); if (err) - goto err_drop_auth; - + goto err_free_inst; enc = crypto_spawn_skcipher_alg(&ctx->enc); ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask, @@ -440,12 +426,12 @@ static int crypto_authenc_create(struct crypto_template *tmpl, "authenc(%s,%s)", auth_base->cra_name, enc->base.cra_name) >= CRYPTO_MAX_ALG_NAME) - goto err_drop_enc; + goto err_free_inst; if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)", auth_base->cra_driver_name, enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) - goto err_drop_enc; + goto err_free_inst; inst->alg.base.cra_flags = (auth_base->cra_flags | enc->base.cra_flags) & CRYPTO_ALG_ASYNC; @@ -470,21 +456,11 @@ static int crypto_authenc_create(struct crypto_template *tmpl, inst->free = crypto_authenc_free; err = aead_register_instance(tmpl, inst); - if (err) - goto err_drop_enc; - -out: - crypto_mod_put(auth_base); - return err; - -err_drop_enc: - crypto_drop_skcipher(&ctx->enc); -err_drop_auth: - crypto_drop_ahash(&ctx->auth); + if (err) { err_free_inst: - kfree(inst); -out_put_auth: - goto out; + crypto_authenc_free(inst); + } + return err; } static struct crypto_template crypto_authenc_tmpl = { From patchwork Fri Jan 3 03:58:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316365 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B639A6C1 for ; Fri, 3 Jan 2020 04:01:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9561A222C3 for ; Fri, 3 Jan 2020 04:01:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024091; bh=VJsUI3nZQBs1+NtInId6C4UnPbvxMWpEAX+GXI/7fVw=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=KcJ+kjupJc4MKkjeK4yRuCzrEJy61+Y/Kv3KWnTyxjynxi7KeBE2f5cnrWl7WTZNM kQkAeK+gYWvpTBmkRxVhAklRnzYQIZjhmSoZ1zUmSiKIUFhOERKoMVLFD+sZeU1vFl Fy/s9UmtB/2/SyxFioNnfr+xbqQODR7emZo/o9n8= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727210AbgACEBa (ORCPT ); Thu, 2 Jan 2020 23:01:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:33558 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727186AbgACEB2 (ORCPT ); Thu, 2 Jan 2020 23:01:28 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3FB9B227BF for ; Fri, 3 Jan 2020 04:01:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024087; bh=VJsUI3nZQBs1+NtInId6C4UnPbvxMWpEAX+GXI/7fVw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=psxp+Wv4eZ8vgXKqcEf3LNGh4fR4GeARw7aF83SnyVDYq0GVzpS27BHJ++p6phqnC oZPIpk8h/0HzlfOliyMUILRExCnMscgWlKlJTN5+WItGNGCqLlr2gc1tkmXR4lGHaV Lv09nwk81wKIaLz2VrjpmhiqYxPd0hsPRa3W/CgA= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 16/28] crypto: authencesn - use crypto_grab_ahash() and simplify error paths Date: Thu, 2 Jan 2020 19:58:56 -0800 Message-Id: <20200103035908.12048-17-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the authencesn template use the new function crypto_grab_ahash() to initialize its ahash spawn. This is needed to make all spawns be initialized in a consistent way. Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers --- crypto/authencesn.c | 52 ++++++++++++--------------------------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/crypto/authencesn.c b/crypto/authencesn.c index 48582c3741dc..030cbcd2fe39 100644 --- a/crypto/authencesn.c +++ b/crypto/authencesn.c @@ -403,11 +403,10 @@ static int crypto_authenc_esn_create(struct crypto_template *tmpl, struct crypto_attr_type *algt; u32 mask; struct aead_instance *inst; + struct authenc_esn_instance_ctx *ctx; struct hash_alg_common *auth; struct crypto_alg *auth_base; struct skcipher_alg *enc; - struct authenc_esn_instance_ctx *ctx; - const char *enc_name; int err; algt = crypto_get_attr_type(tb); @@ -419,47 +418,34 @@ static int crypto_authenc_esn_create(struct crypto_template *tmpl, mask = crypto_requires_sync(algt->type, algt->mask); - auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH, - CRYPTO_ALG_TYPE_AHASH_MASK | mask); - if (IS_ERR(auth)) - return PTR_ERR(auth); - - auth_base = &auth->base; - - enc_name = crypto_attr_alg_name(tb[2]); - err = PTR_ERR(enc_name); - if (IS_ERR(enc_name)) - goto out_put_auth; - inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); - err = -ENOMEM; if (!inst) - goto out_put_auth; - + return -ENOMEM; ctx = aead_instance_ctx(inst); - err = crypto_init_ahash_spawn(&ctx->auth, auth, - aead_crypto_instance(inst)); + err = crypto_grab_ahash(&ctx->auth, aead_crypto_instance(inst), + crypto_attr_alg_name(tb[1]), 0, mask); if (err) goto err_free_inst; + auth = crypto_spawn_ahash_alg(&ctx->auth); + auth_base = &auth->base; err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst), - enc_name, 0, mask); + crypto_attr_alg_name(tb[2]), 0, mask); if (err) - goto err_drop_auth; - + goto err_free_inst; enc = crypto_spawn_skcipher_alg(&ctx->enc); err = -ENAMETOOLONG; if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, "authencesn(%s,%s)", auth_base->cra_name, enc->base.cra_name) >= CRYPTO_MAX_ALG_NAME) - goto err_drop_enc; + goto err_free_inst; if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "authencesn(%s,%s)", auth_base->cra_driver_name, enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) - goto err_drop_enc; + goto err_free_inst; inst->alg.base.cra_flags = (auth_base->cra_flags | enc->base.cra_flags) & CRYPTO_ALG_ASYNC; @@ -485,21 +471,11 @@ static int crypto_authenc_esn_create(struct crypto_template *tmpl, inst->free = crypto_authenc_esn_free, err = aead_register_instance(tmpl, inst); - if (err) - goto err_drop_enc; - -out: - crypto_mod_put(auth_base); - return err; - -err_drop_enc: - crypto_drop_skcipher(&ctx->enc); -err_drop_auth: - crypto_drop_ahash(&ctx->auth); + if (err) { err_free_inst: - kfree(inst); -out_put_auth: - goto out; + crypto_authenc_esn_free(inst); + } + return err; } static struct crypto_template crypto_authenc_esn_tmpl = { From patchwork Fri Jan 3 03:58:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316367 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 073A81580 for ; Fri, 3 Jan 2020 04:01:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DA760222C3 for ; Fri, 3 Jan 2020 04:01:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024091; bh=Ici7lk0J/WuO7xzGPfgbEBFs3WdzkXm7XBazZWCVM00=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=NIdxlKDm7a5QDf02QcTMBxAYYUa5zlcVQTPd/uVC8xommB1qMwqFDaeKs9CoZuJmR j3LAgOrjcnyJhV8DxXXdDxCfvw5oh34Fcn8ATQl0u/LIyDTC51AxzoxPS5ieDWBMMq gP1mhKel9qMgW/83ZBslbbU+bqs02M9pwTicIWaw= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726292AbgACEBb (ORCPT ); Thu, 2 Jan 2020 23:01:31 -0500 Received: from mail.kernel.org ([198.145.29.99]:33616 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726837AbgACEB2 (ORCPT ); Thu, 2 Jan 2020 23:01:28 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6F79722B48 for ; Fri, 3 Jan 2020 04:01:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024087; bh=Ici7lk0J/WuO7xzGPfgbEBFs3WdzkXm7XBazZWCVM00=; h=From:To:Subject:Date:In-Reply-To:References:From; b=t2aizPBwFZk17+1apwC64yARZappMvXHIqW2U86xCYTe4SYWJO0V/4/orSG7negd6 GTz7aHqytB8ePJDKSznFUdfDvqs/4CzgnkzJNgYIjCoCpekYu7FRSbKpBAdLL5pCqe 1H3jsXIMRkt/sddr4Fv0HTt6lCykCQlhbZGv95ww= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 17/28] crypto: gcm - use crypto_grab_ahash() and simplify error paths Date: Thu, 2 Jan 2020 19:58:57 -0800 Message-Id: <20200103035908.12048-18-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the gcm and gcm_base templates use the new function crypto_grab_ahash() to initialize their ahash spawn. This is needed to make all spawns be initialized in a consistent way. Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet. Signed-off-by: Eric Biggers --- crypto/gcm.c | 52 ++++++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/crypto/gcm.c b/crypto/gcm.c index 4241264ff93a..38119d2792d8 100644 --- a/crypto/gcm.c +++ b/crypto/gcm.c @@ -13,7 +13,6 @@ #include #include #include -#include "internal.h" #include #include #include @@ -587,10 +586,9 @@ static int crypto_gcm_create_common(struct crypto_template *tmpl, struct crypto_attr_type *algt; u32 mask; struct aead_instance *inst; + struct gcm_instance_ctx *ctx; struct skcipher_alg *ctr; - struct crypto_alg *ghash_alg; struct hash_alg_common *ghash; - struct gcm_instance_ctx *ctx; int err; algt = crypto_get_attr_type(tb); @@ -602,35 +600,26 @@ static int crypto_gcm_create_common(struct crypto_template *tmpl, mask = crypto_requires_sync(algt->type, algt->mask); - ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type, - CRYPTO_ALG_TYPE_HASH, - CRYPTO_ALG_TYPE_AHASH_MASK | mask); - if (IS_ERR(ghash_alg)) - return PTR_ERR(ghash_alg); - - ghash = __crypto_hash_alg_common(ghash_alg); - - err = -ENOMEM; inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); if (!inst) - goto out_put_ghash; - + return -ENOMEM; ctx = aead_instance_ctx(inst); - err = crypto_init_ahash_spawn(&ctx->ghash, ghash, - aead_crypto_instance(inst)); + + err = crypto_grab_ahash(&ctx->ghash, aead_crypto_instance(inst), + ghash_name, 0, mask); if (err) goto err_free_inst; + ghash = crypto_spawn_ahash_alg(&ctx->ghash); err = -EINVAL; if (strcmp(ghash->base.cra_name, "ghash") != 0 || ghash->digestsize != 16) - goto err_drop_ghash; + goto err_free_inst; err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst), ctr_name, 0, mask); if (err) - goto err_drop_ghash; - + goto err_free_inst; ctr = crypto_spawn_skcipher_alg(&ctx->ctr); /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */ @@ -638,18 +627,18 @@ static int crypto_gcm_create_common(struct crypto_template *tmpl, if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 || crypto_skcipher_alg_ivsize(ctr) != 16 || ctr->base.cra_blocksize != 1) - goto out_put_ctr; + goto err_free_inst; err = -ENAMETOOLONG; if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, "gcm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME) - goto out_put_ctr; + goto err_free_inst; if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)", ctr->base.cra_driver_name, - ghash_alg->cra_driver_name) >= + ghash->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) - goto out_put_ctr; + goto err_free_inst; inst->alg.base.cra_flags = (ghash->base.cra_flags | ctr->base.cra_flags) & CRYPTO_ALG_ASYNC; @@ -672,20 +661,11 @@ static int crypto_gcm_create_common(struct crypto_template *tmpl, inst->free = crypto_gcm_free; err = aead_register_instance(tmpl, inst); - if (err) - goto out_put_ctr; - -out_put_ghash: - crypto_mod_put(ghash_alg); - return err; - -out_put_ctr: - crypto_drop_skcipher(&ctx->ctr); -err_drop_ghash: - crypto_drop_ahash(&ctx->ghash); + if (err) { err_free_inst: - kfree(inst); - goto out_put_ghash; + crypto_gcm_free(inst); + } + return err; } static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb) From patchwork Fri Jan 3 03:58:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316385 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BF1F0139A for ; Fri, 3 Jan 2020 04:01:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9E5A8222C3 for ; Fri, 3 Jan 2020 04:01:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024096; bh=tAi1LDeEgdA1WNWu7/InvNa+PobcDutTXa4U5Xr/HQM=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=yBCc/yMSUY5nWNHiHIzb9gaWzDx5gTK7yalmyv/96Wfg0VktYd3owhC74tlafxLz+ NOuXVugVyLCIFhx7DV9PCvKqIlT2ITLYxKmSZXE3wqmv51gDkwCB4wzXpuVI7zRo+v HR7F8vtKr+Jm7Any8h73fVWn7lpIw0iPPRR0dCZU= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727230AbgACEBf (ORCPT ); Thu, 2 Jan 2020 23:01:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:33626 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727200AbgACEB2 (ORCPT ); Thu, 2 Jan 2020 23:01:28 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9E56824125 for ; Fri, 3 Jan 2020 04:01:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024087; bh=tAi1LDeEgdA1WNWu7/InvNa+PobcDutTXa4U5Xr/HQM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=G4drM+ywT6/R4Nc8+jEZd3FPDsfVt1tdc+usBObRrvu9rN/jiu7sdtS6ZIdr4pNWE XW1N1uGy3Igz5cUscG9E2fHr8ci8QLo7+MwTKdiqIV7LuG0xr8luXtVxH98B9Z1GWi E1kvLZhpULtTfQ0Y9ZDcayytYwOu9lDHGn0E8DI8= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 18/28] crypto: ccm - use crypto_grab_ahash() and simplify error paths Date: Thu, 2 Jan 2020 19:58:58 -0800 Message-Id: <20200103035908.12048-19-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the ccm and ccm_base templates use the new function crypto_grab_ahash() to initialize their ahash spawn. This is needed to make all spawns be initialized in a consistent way. Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet. Signed-off-by: Eric Biggers --- crypto/ccm.c | 61 +++++++++++++++++----------------------------------- 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/crypto/ccm.c b/crypto/ccm.c index 9c377976581d..48c2b2c565a6 100644 --- a/crypto/ccm.c +++ b/crypto/ccm.c @@ -15,8 +15,6 @@ #include #include -#include "internal.h" - struct ccm_instance_ctx { struct crypto_skcipher_spawn ctr; struct crypto_ahash_spawn mac; @@ -459,10 +457,9 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl, struct crypto_attr_type *algt; u32 mask; struct aead_instance *inst; + struct ccm_instance_ctx *ictx; struct skcipher_alg *ctr; - struct crypto_alg *mac_alg; struct hash_alg_common *mac; - struct ccm_instance_ctx *ictx; int err; algt = crypto_get_attr_type(tb); @@ -474,35 +471,26 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl, mask = crypto_requires_sync(algt->type, algt->mask); - mac_alg = crypto_find_alg(mac_name, &crypto_ahash_type, - CRYPTO_ALG_TYPE_HASH, - CRYPTO_ALG_TYPE_AHASH_MASK | - CRYPTO_ALG_ASYNC); - if (IS_ERR(mac_alg)) - return PTR_ERR(mac_alg); - - mac = __crypto_hash_alg_common(mac_alg); - err = -EINVAL; - if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 || - mac->digestsize != 16) - goto out_put_mac; - inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL); - err = -ENOMEM; if (!inst) - goto out_put_mac; - + return -ENOMEM; ictx = aead_instance_ctx(inst); - err = crypto_init_ahash_spawn(&ictx->mac, mac, - aead_crypto_instance(inst)); + + err = crypto_grab_ahash(&ictx->mac, aead_crypto_instance(inst), + mac_name, 0, CRYPTO_ALG_ASYNC); if (err) goto err_free_inst; + mac = crypto_spawn_ahash_alg(&ictx->mac); + + err = -EINVAL; + if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 || + mac->digestsize != 16) + goto err_free_inst; err = crypto_grab_skcipher(&ictx->ctr, aead_crypto_instance(inst), ctr_name, 0, mask); if (err) - goto err_drop_mac; - + goto err_free_inst; ctr = crypto_spawn_skcipher_alg(&ictx->ctr); /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */ @@ -510,21 +498,21 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl, if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 || crypto_skcipher_alg_ivsize(ctr) != 16 || ctr->base.cra_blocksize != 1) - goto err_drop_ctr; + goto err_free_inst; /* ctr and cbcmac must use the same underlying block cipher. */ if (strcmp(ctr->base.cra_name + 4, mac->base.cra_name + 7) != 0) - goto err_drop_ctr; + goto err_free_inst; err = -ENAMETOOLONG; if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, "ccm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME) - goto err_drop_ctr; + goto err_free_inst; if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "ccm_base(%s,%s)", ctr->base.cra_driver_name, mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) - goto err_drop_ctr; + goto err_free_inst; inst->alg.base.cra_flags = ctr->base.cra_flags & CRYPTO_ALG_ASYNC; inst->alg.base.cra_priority = (mac->base.cra_priority + @@ -546,20 +534,11 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl, inst->free = crypto_ccm_free; err = aead_register_instance(tmpl, inst); - if (err) - goto err_drop_ctr; - -out_put_mac: - crypto_mod_put(mac_alg); - return err; - -err_drop_ctr: - crypto_drop_skcipher(&ictx->ctr); -err_drop_mac: - crypto_drop_ahash(&ictx->mac); + if (err) { err_free_inst: - kfree(inst); - goto out_put_mac; + crypto_ccm_free(inst); + } + return err; } static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb) From patchwork Fri Jan 3 03:58:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316381 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0CA406C1 for ; Fri, 3 Jan 2020 04:01:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E098A222C3 for ; Fri, 3 Jan 2020 04:01:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024095; bh=oP03XBrmE4G+p/Xh8RaSzFf/+MvvdLPxYmtN3QYvfM8=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=W+d365cVWv3ywJwxt75JP+6JBhOTUwSIjfD9k6ykRCMuA6liOldURbZP1sKF6kH9G ntm/5PhqSUcJbEc+eOFKggQb7mBTX8EIOw5cl6qWtvSOvkf/4o3lnCDfNIBWbiLoGk 2HeAv74yTW95uLfaTsvb1qUsHuEzjvGNnPaOCDjE= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727213AbgACEBa (ORCPT ); Thu, 2 Jan 2020 23:01:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:33636 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727210AbgACEB2 (ORCPT ); Thu, 2 Jan 2020 23:01:28 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CF48724649 for ; Fri, 3 Jan 2020 04:01:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024087; bh=oP03XBrmE4G+p/Xh8RaSzFf/+MvvdLPxYmtN3QYvfM8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=x/BPPO0rVcV/6kGDGvrclV8Qmgj0XcfEoPlxxh0jpw7uHdI3MWsOxVN7YXIbIXC90 hV/uAQ/QeCk1dLSLimfExkcTN6cnBUgsjOVtY2AAtHJ4STYSh1e7R7pv4gsUp7h76W faJDtALvrtHuVXFUpP7WUDfUoVGaxKP2yBDbE1gE= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 19/28] crypto: chacha20poly1305 - use crypto_grab_ahash() and simplify error paths Date: Thu, 2 Jan 2020 19:58:59 -0800 Message-Id: <20200103035908.12048-20-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the rfc7539 and rfc7539esp templates use the new function crypto_grab_ahash() to initialize their ahash spawn. This is needed to make all spawns be initialized in a consistent way. Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers --- crypto/chacha20poly1305.c | 84 +++++++++++++-------------------------- 1 file changed, 27 insertions(+), 57 deletions(-) diff --git a/crypto/chacha20poly1305.c b/crypto/chacha20poly1305.c index fcb8ec4ba083..72543cfa45ef 100644 --- a/crypto/chacha20poly1305.c +++ b/crypto/chacha20poly1305.c @@ -16,8 +16,6 @@ #include #include -#include "internal.h" - struct chachapoly_instance_ctx { struct crypto_skcipher_spawn chacha; struct crypto_ahash_spawn poly; @@ -565,11 +563,9 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb, struct crypto_attr_type *algt; u32 mask; struct aead_instance *inst; - struct skcipher_alg *chacha; - struct crypto_alg *poly; - struct hash_alg_common *poly_hash; struct chachapoly_instance_ctx *ctx; - const char *chacha_name, *poly_name; + struct skcipher_alg *chacha; + struct hash_alg_common *poly; int err; if (ivsize > CHACHAPOLY_IV_SIZE) @@ -584,68 +580,51 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb, mask = crypto_requires_sync(algt->type, algt->mask); - chacha_name = crypto_attr_alg_name(tb[1]); - if (IS_ERR(chacha_name)) - return PTR_ERR(chacha_name); - poly_name = crypto_attr_alg_name(tb[2]); - if (IS_ERR(poly_name)) - return PTR_ERR(poly_name); - - poly = crypto_find_alg(poly_name, &crypto_ahash_type, - CRYPTO_ALG_TYPE_HASH, - CRYPTO_ALG_TYPE_AHASH_MASK | mask); - if (IS_ERR(poly)) - return PTR_ERR(poly); - poly_hash = __crypto_hash_alg_common(poly); - - err = -EINVAL; - if (poly_hash->digestsize != POLY1305_DIGEST_SIZE) - goto out_put_poly; - - err = -ENOMEM; inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); if (!inst) - goto out_put_poly; - + return -ENOMEM; ctx = aead_instance_ctx(inst); ctx->saltlen = CHACHAPOLY_IV_SIZE - ivsize; - err = crypto_init_ahash_spawn(&ctx->poly, poly_hash, - aead_crypto_instance(inst)); - if (err) - goto err_free_inst; err = crypto_grab_skcipher(&ctx->chacha, aead_crypto_instance(inst), - chacha_name, 0, mask); + crypto_attr_alg_name(tb[1]), 0, mask); if (err) - goto err_drop_poly; - + goto err_free_inst; chacha = crypto_spawn_skcipher_alg(&ctx->chacha); + err = crypto_grab_ahash(&ctx->poly, aead_crypto_instance(inst), + crypto_attr_alg_name(tb[2]), 0, mask); + if (err) + goto err_free_inst; + poly = crypto_spawn_ahash_alg(&ctx->poly); + err = -EINVAL; + if (poly->digestsize != POLY1305_DIGEST_SIZE) + goto err_free_inst; /* Need 16-byte IV size, including Initial Block Counter value */ if (crypto_skcipher_alg_ivsize(chacha) != CHACHA_IV_SIZE) - goto out_drop_chacha; + goto err_free_inst; /* Not a stream cipher? */ if (chacha->base.cra_blocksize != 1) - goto out_drop_chacha; + goto err_free_inst; err = -ENAMETOOLONG; if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s,%s)", name, chacha->base.cra_name, - poly->cra_name) >= CRYPTO_MAX_ALG_NAME) - goto out_drop_chacha; + poly->base.cra_name) >= CRYPTO_MAX_ALG_NAME) + goto err_free_inst; if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s,%s)", name, chacha->base.cra_driver_name, - poly->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) - goto out_drop_chacha; + poly->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) + goto err_free_inst; - inst->alg.base.cra_flags = (chacha->base.cra_flags | poly->cra_flags) & - CRYPTO_ALG_ASYNC; + inst->alg.base.cra_flags = (chacha->base.cra_flags | + poly->base.cra_flags) & CRYPTO_ALG_ASYNC; inst->alg.base.cra_priority = (chacha->base.cra_priority + - poly->cra_priority) / 2; + poly->base.cra_priority) / 2; inst->alg.base.cra_blocksize = 1; inst->alg.base.cra_alignmask = chacha->base.cra_alignmask | - poly->cra_alignmask; + poly->base.cra_alignmask; inst->alg.base.cra_ctxsize = sizeof(struct chachapoly_ctx) + ctx->saltlen; inst->alg.ivsize = ivsize; @@ -661,20 +640,11 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb, inst->free = chachapoly_free; err = aead_register_instance(tmpl, inst); - if (err) - goto out_drop_chacha; - -out_put_poly: - crypto_mod_put(poly); - return err; - -out_drop_chacha: - crypto_drop_skcipher(&ctx->chacha); -err_drop_poly: - crypto_drop_ahash(&ctx->poly); + if (err) { err_free_inst: - kfree(inst); - goto out_put_poly; + chachapoly_free(inst); + } + return err; } static int rfc7539_create(struct crypto_template *tmpl, struct rtattr **tb) From patchwork Fri Jan 3 03:59:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316363 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 29043139A for ; Fri, 3 Jan 2020 04:01:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 084CB222C3 for ; Fri, 3 Jan 2020 04:01:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024091; bh=ZWZKRU2mBzIREo72I47XbqSuNNF67PNQUGEaj77DA3Q=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=t4IRyMltDiqOHYSIgVm+/Ye+6LOXTWp/FD/ztfIm5SDmT3ZgvQN37vxnTi21G2N4M oWPtYhUvgwgpzpIIoQ1DeT+7s0rm/2q1+Vtke8JevYUzwnxZYqSwjutK0KWIXgHSV6 wjiGLL38HCJFVleWZBy44GVOkQrQ2sjrN1N5ty4g= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727253AbgACEBa (ORCPT ); Thu, 2 Jan 2020 23:01:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:33642 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727213AbgACEB3 (ORCPT ); Thu, 2 Jan 2020 23:01:29 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0D6362464E for ; Fri, 3 Jan 2020 04:01:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024088; bh=ZWZKRU2mBzIREo72I47XbqSuNNF67PNQUGEaj77DA3Q=; h=From:To:Subject:Date:In-Reply-To:References:From; b=0auWb3dE2dmepdXhKxCWAJrP56Qv+1rB5JTBeZH2/dO3Sehcj+g+lO+SBN+dd2RUi lxjNGS8kwtAbwNi2dDtRmB6JUlapZ2av6bvpvAWOYaRuIPxP41xhX0AiMODFrTXoI5 gm7f00CffbSNY/upQN+CE829rLqXwqQcJ9m65R5Q= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 20/28] crypto: skcipher - use crypto_grab_cipher() and simplify error paths Date: Thu, 2 Jan 2020 19:59:00 -0800 Message-Id: <20200103035908.12048-21-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make skcipher_alloc_instance_simple() use the new function crypto_grab_cipher() to initialize its cipher spawn. This is needed to make all spawns be initialized in a consistent way. Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers --- crypto/skcipher.c | 39 ++++++++++++------------------ include/crypto/internal/skcipher.h | 4 +-- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/crypto/skcipher.c b/crypto/skcipher.c index a9418a7e80a9..5869ed0ddcad 100644 --- a/crypto/skcipher.c +++ b/crypto/skcipher.c @@ -923,7 +923,7 @@ static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm) static void skcipher_free_instance_simple(struct skcipher_instance *inst) { - crypto_drop_spawn(skcipher_instance_ctx(inst)); + crypto_drop_cipher(skcipher_instance_ctx(inst)); kfree(inst); } @@ -947,10 +947,10 @@ struct skcipher_instance *skcipher_alloc_instance_simple( struct crypto_template *tmpl, struct rtattr **tb) { struct crypto_attr_type *algt; - struct crypto_alg *cipher_alg; - struct skcipher_instance *inst; - struct crypto_spawn *spawn; u32 mask; + struct skcipher_instance *inst; + struct crypto_cipher_spawn *spawn; + struct crypto_alg *cipher_alg; int err; algt = crypto_get_attr_type(tb); @@ -960,32 +960,25 @@ struct skcipher_instance *skcipher_alloc_instance_simple( if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) return ERR_PTR(-EINVAL); - mask = CRYPTO_ALG_TYPE_MASK | - crypto_requires_off(algt->type, algt->mask, - CRYPTO_ALG_NEED_FALLBACK); - - cipher_alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, mask); - if (IS_ERR(cipher_alg)) - return ERR_CAST(cipher_alg); + mask = crypto_requires_off(algt->type, algt->mask, + CRYPTO_ALG_NEED_FALLBACK); inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); - if (!inst) { - err = -ENOMEM; - goto err_put_cipher_alg; - } + if (!inst) + return ERR_PTR(-ENOMEM); spawn = skcipher_instance_ctx(inst); - err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name, - cipher_alg); + err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst), + crypto_attr_alg_name(tb[1]), 0, mask); if (err) goto err_free_inst; + cipher_alg = crypto_spawn_cipher_alg(spawn); - spawn->dropref = true; - err = crypto_init_spawn(spawn, cipher_alg, - skcipher_crypto_instance(inst), - CRYPTO_ALG_TYPE_MASK); + err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name, + cipher_alg); if (err) goto err_free_inst; + inst->free = skcipher_free_instance_simple; /* Default algorithm properties, can be overridden */ @@ -1005,9 +998,7 @@ struct skcipher_instance *skcipher_alloc_instance_simple( return inst; err_free_inst: - kfree(inst); -err_put_cipher_alg: - crypto_mod_put(cipher_alg); + skcipher_free_instance_simple(inst); return ERR_PTR(err); } EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple); diff --git a/include/crypto/internal/skcipher.h b/include/crypto/internal/skcipher.h index b81cb4902abc..d46517275b3c 100644 --- a/include/crypto/internal/skcipher.h +++ b/include/crypto/internal/skcipher.h @@ -216,9 +216,9 @@ struct skcipher_instance *skcipher_alloc_instance_simple( static inline struct crypto_alg *skcipher_ialg_simple( struct skcipher_instance *inst) { - struct crypto_spawn *spawn = skcipher_instance_ctx(inst); + struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst); - return spawn->alg; + return crypto_spawn_cipher_alg(spawn); } #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */ From patchwork Fri Jan 3 03:59:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316391 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7D001139A for ; Fri, 3 Jan 2020 04:01:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5BB64222C3 for ; Fri, 3 Jan 2020 04:01:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024097; bh=1E5RBDoLVBtABBUS2OP6sJOSfTyOrDAVlQHMJSjNI4Y=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=tN8/4cTv1oU2SXtYfHg1YBlfKpecucLWne/9X0d/z5Aq1xAzmwOdhomSCHwOpw6ix BPoWQgQ0ap7083diRPR9gtoqGdv4vKtWetWQbklziH2LQC49dx5x4suqS5K5Dsvuzv BUHzgsO2zed7BaluAmxLSjX4gj2ggFng0jG5WRW4= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727200AbgACEBf (ORCPT ); Thu, 2 Jan 2020 23:01:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:33654 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727215AbgACEB2 (ORCPT ); Thu, 2 Jan 2020 23:01:28 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3CBF424653 for ; Fri, 3 Jan 2020 04:01:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024088; bh=1E5RBDoLVBtABBUS2OP6sJOSfTyOrDAVlQHMJSjNI4Y=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ND8Rss7c5HYp35O/CQSsqis35eqBuhNnZ7NrIwc869LqDZyhLX9rHKkmBCHZkER6a 5ac7sTXGV1b7zMV3VSHat/08YXvA1aqgT+V0AYAtiK0C/M8keRBapyoxC+IT+88oM7 BI8BtTut92DVsJBr/Frtj29NTDEOPttQ6p/LPYFo= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 21/28] crypto: cbcmac - use crypto_grab_cipher() and simplify error paths Date: Thu, 2 Jan 2020 19:59:01 -0800 Message-Id: <20200103035908.12048-22-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the cbcmac template use the new function crypto_grab_cipher() to initialize its cipher spawn. This is needed to make all spawns be initialized in a consistent way. This required making cbcmac_create() allocate the instance directly rather than use shash_alloc_instance(). Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers --- crypto/ccm.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/crypto/ccm.c b/crypto/ccm.c index 48c2b2c565a6..798ae729c77f 100644 --- a/crypto/ccm.c +++ b/crypto/ccm.c @@ -899,6 +899,7 @@ static void cbcmac_exit_tfm(struct crypto_tfm *tfm) static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb) { struct shash_instance *inst; + struct crypto_cipher_spawn *spawn; struct crypto_alg *alg; int err; @@ -906,21 +907,20 @@ static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb) if (err) return err; - alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, - CRYPTO_ALG_TYPE_MASK); - if (IS_ERR(alg)) - return PTR_ERR(alg); + inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); + if (!inst) + return -ENOMEM; + spawn = shash_instance_ctx(inst); - inst = shash_alloc_instance("cbcmac", alg); - err = PTR_ERR(inst); - if (IS_ERR(inst)) - goto out_put_alg; + err = crypto_grab_cipher(spawn, shash_crypto_instance(inst), + crypto_attr_alg_name(tb[1]), 0, 0); + if (err) + goto err_free_inst; + alg = crypto_spawn_cipher_alg(spawn); - err = crypto_init_spawn(shash_instance_ctx(inst), alg, - shash_crypto_instance(inst), - CRYPTO_ALG_TYPE_MASK); + err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg); if (err) - goto out_free_inst; + goto err_free_inst; inst->alg.base.cra_priority = alg->cra_priority; inst->alg.base.cra_blocksize = 1; @@ -940,13 +940,10 @@ static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb) inst->alg.setkey = crypto_cbcmac_digest_setkey; err = shash_register_instance(tmpl, inst); - -out_free_inst: - if (err) + if (err) { +err_free_inst: shash_free_instance(shash_crypto_instance(inst)); - -out_put_alg: - crypto_mod_put(alg); + } return err; } From patchwork Fri Jan 3 03:59:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316369 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2FD59139A for ; Fri, 3 Jan 2020 04:01:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0E9D7222C3 for ; Fri, 3 Jan 2020 04:01:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024093; bh=53NpCC9PIB7NmhV6O++rQmXftGOGfBPNMfBa5ZdhHVI=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=ViOe3DGlpzgPUZVTeX1TMnk5vN0W/cMBP70sJoE1HqeS7MtG9MaIsHKJySVadSeJS eJeijIG7FVlLheBX3qxXEKVKd3srn7iyw9XMz2gDILdhSMARn9q+fFbGHyHiFwWmR1 acb6icb5JRyv+fTJswfmxAA/bFQ+FiiHoxJjthS8= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726837AbgACEBb (ORCPT ); Thu, 2 Jan 2020 23:01:31 -0500 Received: from mail.kernel.org ([198.145.29.99]:33654 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727249AbgACEBa (ORCPT ); Thu, 2 Jan 2020 23:01:30 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6B68E2464B for ; Fri, 3 Jan 2020 04:01:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024088; bh=53NpCC9PIB7NmhV6O++rQmXftGOGfBPNMfBa5ZdhHVI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=JUrVEHdDIKLNbdg/ZuYONKMRXyUBEhuj4deooXnqVuNOEK7XpEdQ5I8btXXkdfcfZ diTri4q6Jt7HWPSRAxX+DYiT/TjV+HCurjOAdW6cXwuErdFr/C1LbqIMXY/Nu3Q5mi 47cQ8SMcIPNR/4vXecNPLyhLg+oq/jGjZEgz/ERk= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 22/28] crypto: cmac - use crypto_grab_cipher() and simplify error paths Date: Thu, 2 Jan 2020 19:59:02 -0800 Message-Id: <20200103035908.12048-23-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the cmac template use the new function crypto_grab_cipher() to initialize its cipher spawn. This is needed to make all spawns be initialized in a consistent way. This required making cmac_create() allocate the instance directly rather than use shash_alloc_instance(). Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers --- crypto/cmac.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/crypto/cmac.c b/crypto/cmac.c index 0928aebc6205..c6bf78b5321a 100644 --- a/crypto/cmac.c +++ b/crypto/cmac.c @@ -222,6 +222,7 @@ static void cmac_exit_tfm(struct crypto_tfm *tfm) static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb) { struct shash_instance *inst; + struct crypto_cipher_spawn *spawn; struct crypto_alg *alg; unsigned long alignmask; int err; @@ -230,10 +231,16 @@ static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb) if (err) return err; - alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, - CRYPTO_ALG_TYPE_MASK); - if (IS_ERR(alg)) - return PTR_ERR(alg); + inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); + if (!inst) + return -ENOMEM; + spawn = shash_instance_ctx(inst); + + err = crypto_grab_cipher(spawn, shash_crypto_instance(inst), + crypto_attr_alg_name(tb[1]), 0, 0); + if (err) + goto err_free_inst; + alg = crypto_spawn_cipher_alg(spawn); switch (alg->cra_blocksize) { case 16: @@ -241,19 +248,12 @@ static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb) break; default: err = -EINVAL; - goto out_put_alg; + goto err_free_inst; } - inst = shash_alloc_instance("cmac", alg); - err = PTR_ERR(inst); - if (IS_ERR(inst)) - goto out_put_alg; - - err = crypto_init_spawn(shash_instance_ctx(inst), alg, - shash_crypto_instance(inst), - CRYPTO_ALG_TYPE_MASK); + err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg); if (err) - goto out_free_inst; + goto err_free_inst; alignmask = alg->cra_alignmask; inst->alg.base.cra_alignmask = alignmask; @@ -282,12 +282,9 @@ static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb) err = shash_register_instance(tmpl, inst); if (err) { -out_free_inst: +err_free_inst: shash_free_instance(shash_crypto_instance(inst)); } - -out_put_alg: - crypto_mod_put(alg); return err; } From patchwork Fri Jan 3 03:59:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316375 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3F8901580 for ; Fri, 3 Jan 2020 04:01:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1EAC422314 for ; Fri, 3 Jan 2020 04:01:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024094; bh=TyFzwlzfBrW4/tW4Pb0/iLEh7VnCJjVScUUPyVQvKg0=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=XV2fGBiTaN0Pulh+WLWKu6QlYG8kHXxCsxrhhefjR3AcQpPaeoU7p83Zk9nSBqQqe yLOait5DjV83n25Xabm8gtynKCGR0L2cLjelriEJIvTThQ+LlmJ2sAXNukVNKFw30M +SL4YbNQYsCs8jF9WDgUWGR8RB70CB1RLHIGVRg0= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727227AbgACEBb (ORCPT ); Thu, 2 Jan 2020 23:01:31 -0500 Received: from mail.kernel.org ([198.145.29.99]:33554 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727222AbgACEBa (ORCPT ); Thu, 2 Jan 2020 23:01:30 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9B65F24655 for ; Fri, 3 Jan 2020 04:01:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024088; bh=TyFzwlzfBrW4/tW4Pb0/iLEh7VnCJjVScUUPyVQvKg0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=EbwV8eHqFLs2uoGyJTpTHvFQvsQJ3PYTOMlz5Uviehnc1RKBAugdtBTihWt2kDXtX SWNQFGlWDha6jiLyHYEd7gD+V21df/iPyHYk/rjocqLhGdN2lk0HpGW+u+RkA85nDU adnOKf7LQVj00PBa1T8R2wNflUn0AozhALPUdDqg= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 23/28] crypto: vmac - use crypto_grab_cipher() and simplify error paths Date: Thu, 2 Jan 2020 19:59:03 -0800 Message-Id: <20200103035908.12048-24-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the vmac64 template use the new function crypto_grab_cipher() to initialize its cipher spawn. This is needed to make all spawns be initialized in a consistent way. This required making vmac_create() allocate the instance directly rather than use shash_alloc_instance(). Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers --- crypto/vmac.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/crypto/vmac.c b/crypto/vmac.c index f50a85060b39..8924f914dc44 100644 --- a/crypto/vmac.c +++ b/crypto/vmac.c @@ -620,6 +620,7 @@ static void vmac_exit_tfm(struct crypto_tfm *tfm) static int vmac_create(struct crypto_template *tmpl, struct rtattr **tb) { struct shash_instance *inst; + struct crypto_cipher_spawn *spawn; struct crypto_alg *alg; int err; @@ -627,25 +628,24 @@ static int vmac_create(struct crypto_template *tmpl, struct rtattr **tb) if (err) return err; - alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, - CRYPTO_ALG_TYPE_MASK); - if (IS_ERR(alg)) - return PTR_ERR(alg); + inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); + if (!inst) + return -ENOMEM; + spawn = shash_instance_ctx(inst); + + err = crypto_grab_cipher(spawn, shash_crypto_instance(inst), + crypto_attr_alg_name(tb[1]), 0, 0); + if (err) + goto err_free_inst; + alg = crypto_spawn_cipher_alg(spawn); err = -EINVAL; if (alg->cra_blocksize != VMAC_NONCEBYTES) - goto out_put_alg; + goto err_free_inst; - inst = shash_alloc_instance(tmpl->name, alg); - err = PTR_ERR(inst); - if (IS_ERR(inst)) - goto out_put_alg; - - err = crypto_init_spawn(shash_instance_ctx(inst), alg, - shash_crypto_instance(inst), - CRYPTO_ALG_TYPE_MASK); + err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg); if (err) - goto out_free_inst; + goto err_free_inst; inst->alg.base.cra_priority = alg->cra_priority; inst->alg.base.cra_blocksize = alg->cra_blocksize; @@ -664,12 +664,9 @@ static int vmac_create(struct crypto_template *tmpl, struct rtattr **tb) err = shash_register_instance(tmpl, inst); if (err) { -out_free_inst: +err_free_inst: shash_free_instance(shash_crypto_instance(inst)); } - -out_put_alg: - crypto_mod_put(alg); return err; } From patchwork Fri Jan 3 03:59:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316373 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 14D3D6C1 for ; Fri, 3 Jan 2020 04:01:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E82E3222C3 for ; Fri, 3 Jan 2020 04:01:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024094; bh=W+vjy+rq1vdVci6oXj7vGift00pdKDFP/LwvQFdusRk=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=HneCw1xTFWCawqBa26pUXiexo3hsYI3p+5FO+Coe/juOGMemc7CALQgG3Ml0zu23T KYXigLww1lPG9hoYrPHEwe9DkBlIEKeIMP6UQaFIVK7yKbNt0k8N9UKJqgT8ddFJIi oWAubIk6RAMWrC3dDvs/3+crmYE10S4k/cxwJauE= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727222AbgACEBd (ORCPT ); Thu, 2 Jan 2020 23:01:33 -0500 Received: from mail.kernel.org ([198.145.29.99]:33508 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727228AbgACEBa (ORCPT ); Thu, 2 Jan 2020 23:01:30 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CC0042465D for ; Fri, 3 Jan 2020 04:01:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024088; bh=W+vjy+rq1vdVci6oXj7vGift00pdKDFP/LwvQFdusRk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=e8t2QJy0ngk+GQ3XURGJ4//TeA5EvDpNc3ll+O/Qzd9JuEfyQC4lLE0NRBCf8VyOn HANCtS9jSE8GKhCs6H4lnGymah0sw5vPT8UKCA3sEi+X2QyWQnfEbsQGs3EziutaeV syqvYN4wcIqkMTqgrau3NVjc2LTGfdNgvAY2zYR8= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 24/28] crypto: xcbc - use crypto_grab_cipher() and simplify error paths Date: Thu, 2 Jan 2020 19:59:04 -0800 Message-Id: <20200103035908.12048-25-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the xcbc template use the new function crypto_grab_cipher() to initialize its cipher spawn. This is needed to make all spawns be initialized in a consistent way. This required making xcbc_create() allocate the instance directly rather than use shash_alloc_instance(). Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers --- crypto/xcbc.c | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/crypto/xcbc.c b/crypto/xcbc.c index 0bb26e8f6f5a..9b97fa511f10 100644 --- a/crypto/xcbc.c +++ b/crypto/xcbc.c @@ -188,6 +188,7 @@ static void xcbc_exit_tfm(struct crypto_tfm *tfm) static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb) { struct shash_instance *inst; + struct crypto_cipher_spawn *spawn; struct crypto_alg *alg; unsigned long alignmask; int err; @@ -196,28 +197,24 @@ static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb) if (err) return err; - alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, - CRYPTO_ALG_TYPE_MASK); - if (IS_ERR(alg)) - return PTR_ERR(alg); + inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); + if (!inst) + return -ENOMEM; + spawn = shash_instance_ctx(inst); - switch(alg->cra_blocksize) { - case XCBC_BLOCKSIZE: - break; - default: - goto out_put_alg; - } + err = crypto_grab_cipher(spawn, shash_crypto_instance(inst), + crypto_attr_alg_name(tb[1]), 0, 0); + if (err) + goto err_free_inst; + alg = crypto_spawn_cipher_alg(spawn); - inst = shash_alloc_instance("xcbc", alg); - err = PTR_ERR(inst); - if (IS_ERR(inst)) - goto out_put_alg; + err = -EINVAL; + if (alg->cra_blocksize != XCBC_BLOCKSIZE) + goto err_free_inst; - err = crypto_init_spawn(shash_instance_ctx(inst), alg, - shash_crypto_instance(inst), - CRYPTO_ALG_TYPE_MASK); + err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg); if (err) - goto out_free_inst; + goto err_free_inst; alignmask = alg->cra_alignmask | 3; inst->alg.base.cra_alignmask = alignmask; @@ -244,12 +241,9 @@ static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb) err = shash_register_instance(tmpl, inst); if (err) { -out_free_inst: +err_free_inst: shash_free_instance(shash_crypto_instance(inst)); } - -out_put_alg: - crypto_mod_put(alg); return err; } From patchwork Fri Jan 3 03:59:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316387 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E92A26C1 for ; Fri, 3 Jan 2020 04:01:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C8ADF22314 for ; Fri, 3 Jan 2020 04:01:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024096; bh=O6Qi4RtnGdOnfUjv5j2KZA5bGZeV08coTLag13kUOWI=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=RbrxdZRvGWD4TlxHLhb1M65zJk/gifMHTsucO8pQ/ZJzM59QlLXmRUAoIOCY89ATa UahD4K9f5a2LEIGORwW9wkGbVSINjrJ9orN7Hp0pdecy+i8GLEMTfACoLZCrm2BckA tGmrLfK7MaxMQt5NK/GET03EDOHheV5ZFYNUB7MI= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727234AbgACEBf (ORCPT ); Thu, 2 Jan 2020 23:01:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:33642 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727230AbgACEBa (ORCPT ); Thu, 2 Jan 2020 23:01:30 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 07AFF2465A for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024089; bh=O6Qi4RtnGdOnfUjv5j2KZA5bGZeV08coTLag13kUOWI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=wTGLLwSCYJHFP1GiQDCVs9dfwdHQoL0PrkNt15sbKuvtBnG9uknF+uFrf24phbcFJ vPvM61SMF2taiXahup1KLmCRhQ3utNsJpITt8N0N70O4kRgwh0loWe6IKgVlmDDmHW hQtub+skyV5tuSWmDJnPHrA8bw/7k5eUWy+en6lo= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 25/28] crypto: cipher - make crypto_spawn_cipher() take a crypto_cipher_spawn Date: Thu, 2 Jan 2020 19:59:05 -0800 Message-Id: <20200103035908.12048-26-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Now that all users of single-block cipher spawns have been converted to use 'struct crypto_cipher_spawn' rather than the less specifically typed 'struct crypto_spawn', make crypto_spawn_cipher() take a pointer to a 'struct crypto_cipher_spawn' rather than a 'struct crypto_spawn'. Signed-off-by: Eric Biggers --- crypto/adiantum.c | 2 +- crypto/ccm.c | 2 +- crypto/cmac.c | 2 +- crypto/skcipher.c | 2 +- crypto/vmac.c | 2 +- crypto/xcbc.c | 2 +- include/crypto/algapi.h | 4 ++-- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crypto/adiantum.c b/crypto/adiantum.c index 8abaecde1464..3b1fef8b6219 100644 --- a/crypto/adiantum.c +++ b/crypto/adiantum.c @@ -416,7 +416,7 @@ static int adiantum_init_tfm(struct crypto_skcipher *tfm) if (IS_ERR(streamcipher)) return PTR_ERR(streamcipher); - blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn.base); + blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn); if (IS_ERR(blockcipher)) { err = PTR_ERR(blockcipher); goto err_free_streamcipher; diff --git a/crypto/ccm.c b/crypto/ccm.c index 798ae729c77f..c7a887565c51 100644 --- a/crypto/ccm.c +++ b/crypto/ccm.c @@ -878,7 +878,7 @@ static int cbcmac_init_tfm(struct crypto_tfm *tfm) { struct crypto_cipher *cipher; struct crypto_instance *inst = (void *)tfm->__crt_alg; - struct crypto_spawn *spawn = crypto_instance_ctx(inst); + struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst); struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm); cipher = crypto_spawn_cipher(spawn); diff --git a/crypto/cmac.c b/crypto/cmac.c index c6bf78b5321a..58dc644416bb 100644 --- a/crypto/cmac.c +++ b/crypto/cmac.c @@ -201,7 +201,7 @@ static int cmac_init_tfm(struct crypto_tfm *tfm) { struct crypto_cipher *cipher; struct crypto_instance *inst = (void *)tfm->__crt_alg; - struct crypto_spawn *spawn = crypto_instance_ctx(inst); + struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst); struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm); cipher = crypto_spawn_cipher(spawn); diff --git a/crypto/skcipher.c b/crypto/skcipher.c index 5869ed0ddcad..8c37243307aa 100644 --- a/crypto/skcipher.c +++ b/crypto/skcipher.c @@ -902,7 +902,7 @@ static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key, static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm) { struct skcipher_instance *inst = skcipher_alg_instance(tfm); - struct crypto_spawn *spawn = skcipher_instance_ctx(inst); + struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst); struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm); struct crypto_cipher *cipher; diff --git a/crypto/vmac.c b/crypto/vmac.c index 8924f914dc44..3841b6e46081 100644 --- a/crypto/vmac.c +++ b/crypto/vmac.c @@ -598,7 +598,7 @@ static int vmac_final(struct shash_desc *desc, u8 *out) static int vmac_init_tfm(struct crypto_tfm *tfm) { struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); - struct crypto_spawn *spawn = crypto_instance_ctx(inst); + struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst); struct vmac_tfm_ctx *tctx = crypto_tfm_ctx(tfm); struct crypto_cipher *cipher; diff --git a/crypto/xcbc.c b/crypto/xcbc.c index 9b97fa511f10..9265e00ea663 100644 --- a/crypto/xcbc.c +++ b/crypto/xcbc.c @@ -167,7 +167,7 @@ static int xcbc_init_tfm(struct crypto_tfm *tfm) { struct crypto_cipher *cipher; struct crypto_instance *inst = (void *)tfm->__crt_alg; - struct crypto_spawn *spawn = crypto_instance_ctx(inst); + struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst); struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm); cipher = crypto_spawn_cipher(spawn); diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h index 7705387f9459..bbf85a854a42 100644 --- a/include/crypto/algapi.h +++ b/include/crypto/algapi.h @@ -234,12 +234,12 @@ static inline struct crypto_alg *crypto_spawn_cipher_alg( } static inline struct crypto_cipher *crypto_spawn_cipher( - struct crypto_spawn *spawn) + struct crypto_cipher_spawn *spawn) { u32 type = CRYPTO_ALG_TYPE_CIPHER; u32 mask = CRYPTO_ALG_TYPE_MASK; - return __crypto_cipher_cast(crypto_spawn_tfm(spawn, type, mask)); + return __crypto_cipher_cast(crypto_spawn_tfm(&spawn->base, type, mask)); } static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm) From patchwork Fri Jan 3 03:59:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316379 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BA602139A for ; Fri, 3 Jan 2020 04:01:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9904E22314 for ; Fri, 3 Jan 2020 04:01:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024094; bh=rQzvp8KxeUk1rVTWBDO8KK+J7RJj/YV1MEDOniL6i5Y=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=MSVBlU1pwTe9YzCRv2uFd5aHLBa15RJ5CFkoQ4mRJxTcvBDlntpS1aP7SFRStwwgx VOB08sKju5p+HuZWDbURMq1kG0UtQtnIyU7n+2uDAixWS/zEKnbXzuU0zP5SNkxE5Y vZNZ5NIejNlRX4OYcmXp+zK5f+8dSxV3ng/BVIqU= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727228AbgACEBd (ORCPT ); Thu, 2 Jan 2020 23:01:33 -0500 Received: from mail.kernel.org ([198.145.29.99]:33616 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727225AbgACEBb (ORCPT ); Thu, 2 Jan 2020 23:01:31 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 37A582465E for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024089; bh=rQzvp8KxeUk1rVTWBDO8KK+J7RJj/YV1MEDOniL6i5Y=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ENQHtXO93qsAuINTTsk5decntUsD4EZ/UxDtRGK/T4Xhsvc3bGs/6oNUOPLaSyi3r ugqqhstmWY+6qMCGBvm7hLv9zfl2Vo2682rS2ugiY749a1qfet7+ouhznruzFFpn9Y DNzOQt6o+eJgjG8P+VjuBz8ftuMr5XdDGgGISAsQ= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 26/28] crypto: algapi - remove obsoleted instance creation helpers Date: Thu, 2 Jan 2020 19:59:06 -0800 Message-Id: <20200103035908.12048-27-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Remove lots of helper functions that were previously used for instantiating crypto templates, but are now unused: - crypto_get_attr_alg() and similar functions looked up an inner algorithm directly from a template parameter. These were replaced with getting the algorithm's name, then calling crypto_grab_*(). - crypto_init_spawn2() and similar functions initialized a spawn, given an algorithm. Similarly, these were replaced with crypto_grab_*(). - crypto_alloc_instance() and similar functions allocated an instance with a single spawn, given the inner algorithm. These aren't useful anymore since crypto_grab_*() need the instance allocated first. Signed-off-by: Eric Biggers --- crypto/ahash.c | 25 --------------- crypto/algapi.c | 57 ---------------------------------- crypto/shash.c | 19 ------------ include/crypto/algapi.h | 22 ------------- include/crypto/internal/hash.h | 31 ------------------ 5 files changed, 154 deletions(-) diff --git a/crypto/ahash.c b/crypto/ahash.c index e98a1398ed7f..2b8449fdb93c 100644 --- a/crypto/ahash.c +++ b/crypto/ahash.c @@ -655,31 +655,6 @@ int ahash_register_instance(struct crypto_template *tmpl, } EXPORT_SYMBOL_GPL(ahash_register_instance); -void ahash_free_instance(struct crypto_instance *inst) -{ - crypto_drop_spawn(crypto_instance_ctx(inst)); - kfree(ahash_instance(inst)); -} -EXPORT_SYMBOL_GPL(ahash_free_instance); - -int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn, - struct hash_alg_common *alg, - struct crypto_instance *inst) -{ - return crypto_init_spawn2(&spawn->base, &alg->base, inst, - &crypto_ahash_type); -} -EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn); - -struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask) -{ - struct crypto_alg *alg; - - alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask); - return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg); -} -EXPORT_SYMBOL_GPL(ahash_attr_alg); - bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg) { struct crypto_alg *alg = &halg->base; diff --git a/crypto/algapi.c b/crypto/algapi.c index a25ce02918f8..f66a4ff57e6e 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -697,23 +697,6 @@ int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg, } EXPORT_SYMBOL_GPL(crypto_init_spawn); -int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg, - struct crypto_instance *inst, - const struct crypto_type *frontend) -{ - int err = -EINVAL; - - if ((alg->cra_flags ^ frontend->type) & frontend->maskset) - goto out; - - spawn->frontend = frontend; - err = crypto_init_spawn(spawn, alg, inst, frontend->maskset); - -out: - return err; -} -EXPORT_SYMBOL_GPL(crypto_init_spawn2); - int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst, const char *name, u32 type, u32 mask) { @@ -876,20 +859,6 @@ const char *crypto_attr_alg_name(struct rtattr *rta) } EXPORT_SYMBOL_GPL(crypto_attr_alg_name); -struct crypto_alg *crypto_attr_alg2(struct rtattr *rta, - const struct crypto_type *frontend, - u32 type, u32 mask) -{ - const char *name; - - name = crypto_attr_alg_name(rta); - if (IS_ERR(name)) - return ERR_CAST(name); - - return crypto_find_alg(name, frontend, type, mask); -} -EXPORT_SYMBOL_GPL(crypto_attr_alg2); - int crypto_attr_u32(struct rtattr *rta, u32 *num) { struct crypto_attr_u32 *nu32; @@ -923,32 +892,6 @@ int crypto_inst_setname(struct crypto_instance *inst, const char *name, } EXPORT_SYMBOL_GPL(crypto_inst_setname); -void *crypto_alloc_instance(const char *name, struct crypto_alg *alg, - unsigned int head) -{ - struct crypto_instance *inst; - char *p; - int err; - - p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn), - GFP_KERNEL); - if (!p) - return ERR_PTR(-ENOMEM); - - inst = (void *)(p + head); - - err = crypto_inst_setname(inst, name, alg); - if (err) - goto err_free_inst; - - return p; - -err_free_inst: - kfree(p); - return ERR_PTR(err); -} -EXPORT_SYMBOL_GPL(crypto_alloc_instance); - void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen) { INIT_LIST_HEAD(&queue->list); diff --git a/crypto/shash.c b/crypto/shash.c index e0872ac2729a..4d6ccb59e126 100644 --- a/crypto/shash.c +++ b/crypto/shash.c @@ -584,24 +584,5 @@ void shash_free_instance(struct crypto_instance *inst) } EXPORT_SYMBOL_GPL(shash_free_instance); -int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn, - struct shash_alg *alg, - struct crypto_instance *inst) -{ - return crypto_init_spawn2(&spawn->base, &alg->base, inst, - &crypto_shash_type); -} -EXPORT_SYMBOL_GPL(crypto_init_shash_spawn); - -struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask) -{ - struct crypto_alg *alg; - - alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask); - return IS_ERR(alg) ? ERR_CAST(alg) : - container_of(alg, struct shash_alg, base); -} -EXPORT_SYMBOL_GPL(shash_attr_alg); - MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Synchronous cryptographic hash type"); diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h index bbf85a854a42..224c72743cce 100644 --- a/include/crypto/algapi.h +++ b/include/crypto/algapi.h @@ -113,12 +113,8 @@ void crypto_unregister_instance(struct crypto_instance *inst); int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg, struct crypto_instance *inst, u32 mask); -int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg, - struct crypto_instance *inst, - const struct crypto_type *frontend); int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst, const char *name, u32 type, u32 mask); - void crypto_drop_spawn(struct crypto_spawn *spawn); struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, u32 mask); @@ -127,21 +123,9 @@ void *crypto_spawn_tfm2(struct crypto_spawn *spawn); struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb); int crypto_check_attr_type(struct rtattr **tb, u32 type); const char *crypto_attr_alg_name(struct rtattr *rta); -struct crypto_alg *crypto_attr_alg2(struct rtattr *rta, - const struct crypto_type *frontend, - u32 type, u32 mask); - -static inline struct crypto_alg *crypto_attr_alg(struct rtattr *rta, - u32 type, u32 mask) -{ - return crypto_attr_alg2(rta, NULL, type, mask); -} - int crypto_attr_u32(struct rtattr *rta, u32 *num); int crypto_inst_setname(struct crypto_instance *inst, const char *name, struct crypto_alg *alg); -void *crypto_alloc_instance(const char *name, struct crypto_alg *alg, - unsigned int head); void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen); int crypto_enqueue_request(struct crypto_queue *queue, @@ -254,12 +238,6 @@ static inline struct crypto_async_request *crypto_get_backlog( container_of(queue->backlog, struct crypto_async_request, list); } -static inline struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, - u32 type, u32 mask) -{ - return crypto_attr_alg(tb[1], type, mask); -} - static inline int crypto_requires_off(u32 type, u32 mask, u32 off) { return (type ^ off) & mask & off; diff --git a/include/crypto/internal/hash.h b/include/crypto/internal/hash.h index e1024fa0032f..79e561abef61 100644 --- a/include/crypto/internal/hash.h +++ b/include/crypto/internal/hash.h @@ -87,7 +87,6 @@ int crypto_register_ahashes(struct ahash_alg *algs, int count); void crypto_unregister_ahashes(struct ahash_alg *algs, int count); int ahash_register_instance(struct crypto_template *tmpl, struct ahash_instance *inst); -void ahash_free_instance(struct crypto_instance *inst); int shash_no_setkey(struct crypto_shash *tfm, const u8 *key, unsigned int keylen); @@ -105,10 +104,6 @@ static inline bool crypto_shash_alg_needs_key(struct shash_alg *alg) bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg); -int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn, - struct hash_alg_common *alg, - struct crypto_instance *inst); - int crypto_grab_ahash(struct crypto_ahash_spawn *spawn, struct crypto_instance *inst, const char *name, u32 type, u32 mask); @@ -124,8 +119,6 @@ static inline struct hash_alg_common *crypto_spawn_ahash_alg( return __crypto_hash_alg_common(spawn->base.alg); } -struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask); - int crypto_register_shash(struct shash_alg *alg); void crypto_unregister_shash(struct shash_alg *alg); int crypto_register_shashes(struct shash_alg *algs, int count); @@ -134,10 +127,6 @@ int shash_register_instance(struct crypto_template *tmpl, struct shash_instance *inst); void shash_free_instance(struct crypto_instance *inst); -int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn, - struct shash_alg *alg, - struct crypto_instance *inst); - int crypto_grab_shash(struct crypto_shash_spawn *spawn, struct crypto_instance *inst, const char *name, u32 type, u32 mask); @@ -153,8 +142,6 @@ static inline struct shash_alg *crypto_spawn_shash_alg( return __crypto_shash_alg(spawn->base.alg); } -struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask); - int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc); int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc); int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc); @@ -195,17 +182,6 @@ static inline void *ahash_instance_ctx(struct ahash_instance *inst) return crypto_instance_ctx(ahash_crypto_instance(inst)); } -static inline unsigned int ahash_instance_headroom(void) -{ - return sizeof(struct ahash_alg) - sizeof(struct crypto_alg); -} - -static inline struct ahash_instance *ahash_alloc_instance( - const char *name, struct crypto_alg *alg) -{ - return crypto_alloc_instance(name, alg, ahash_instance_headroom()); -} - static inline void ahash_request_complete(struct ahash_request *req, int err) { req->base.complete(&req->base, err); @@ -262,13 +238,6 @@ static inline void *shash_instance_ctx(struct shash_instance *inst) return crypto_instance_ctx(shash_crypto_instance(inst)); } -static inline struct shash_instance *shash_alloc_instance( - const char *name, struct crypto_alg *alg) -{ - return crypto_alloc_instance(name, alg, - sizeof(struct shash_alg) - sizeof(*alg)); -} - static inline struct crypto_shash *crypto_spawn_shash( struct crypto_shash_spawn *spawn) { From patchwork Fri Jan 3 03:59:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316371 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5990E1580 for ; Fri, 3 Jan 2020 04:01:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3A3CB21D7D for ; Fri, 3 Jan 2020 04:01:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024093; bh=hy8jBH2alNRHIec3TjwuxiCmcfNcCFSjUlmYkWL4T/E=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=mH5XJ7wh6ItaRhUrvkcBE78gb2dhT6bbPYTjhUVjWR/HBVDDgVSNpEvx4U6HzMUD9 0TrCCogBAwygjQ9x5mzkO+mLHa3NwESYBgQbqYrA1gtVkEMYzVMwHJd6UjsDvT8EQ3 2JiI3HNaeBSgLHuJrIZRj2fiToT6YelxJ4tCmtf4= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727183AbgACEBb (ORCPT ); Thu, 2 Jan 2020 23:01:31 -0500 Received: from mail.kernel.org ([198.145.29.99]:33626 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727227AbgACEBa (ORCPT ); Thu, 2 Jan 2020 23:01:30 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6780324650 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024089; bh=hy8jBH2alNRHIec3TjwuxiCmcfNcCFSjUlmYkWL4T/E=; h=From:To:Subject:Date:In-Reply-To:References:From; b=u4mDnaeYQ6vX8Y4rzTcePvpLmLg0zdu5g/GmH6Hv6vHFDB7jvb2GvViAzHjrm5Hx/ Z6qWIPP+XvKDIgljDA0x/Mhgc09yjYwTOb1HOsXyjBm/Vgaa+soKr/p2pZXyCE3Fw2 GR6dFGwMepiZO0Bags6ywaZZRQqr0o32XQDC9qkg= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 27/28] crypto: ahash - unexport crypto_ahash_type Date: Thu, 2 Jan 2020 19:59:07 -0800 Message-Id: <20200103035908.12048-28-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Now that all the templates that need ahash spawns have been converted to use crypto_grab_ahash() rather than look up the algorithm directly, crypto_ahash_type is no longer used outside of ahash.c. Make it static. Signed-off-by: Eric Biggers --- crypto/ahash.c | 5 +++-- include/crypto/internal/hash.h | 2 -- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/crypto/ahash.c b/crypto/ahash.c index 2b8449fdb93c..c77717fcea8e 100644 --- a/crypto/ahash.c +++ b/crypto/ahash.c @@ -23,6 +23,8 @@ #include "internal.h" +static const struct crypto_type crypto_ahash_type; + struct ahash_request_priv { crypto_completion_t complete; void *data; @@ -542,7 +544,7 @@ static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) __crypto_hash_alg_common(alg)->digestsize); } -const struct crypto_type crypto_ahash_type = { +static const struct crypto_type crypto_ahash_type = { .extsize = crypto_ahash_extsize, .init_tfm = crypto_ahash_init_tfm, #ifdef CONFIG_PROC_FS @@ -554,7 +556,6 @@ const struct crypto_type crypto_ahash_type = { .type = CRYPTO_ALG_TYPE_AHASH, .tfmsize = offsetof(struct crypto_ahash, base), }; -EXPORT_SYMBOL_GPL(crypto_ahash_type); int crypto_grab_ahash(struct crypto_ahash_spawn *spawn, struct crypto_instance *inst, diff --git a/include/crypto/internal/hash.h b/include/crypto/internal/hash.h index 79e561abef61..c84b7cb29887 100644 --- a/include/crypto/internal/hash.h +++ b/include/crypto/internal/hash.h @@ -57,8 +57,6 @@ struct crypto_shash_spawn { struct crypto_spawn base; }; -extern const struct crypto_type crypto_ahash_type; - int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err); int crypto_hash_walk_first(struct ahash_request *req, struct crypto_hash_walk *walk); From patchwork Fri Jan 3 03:59:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11316383 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BB85E139A for ; Fri, 3 Jan 2020 04:01:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9ADE9222C3 for ; Fri, 3 Jan 2020 04:01:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024095; bh=V3lijhL1CcRJBGzyQYzwYDpjzE+0JY70cR572uthStI=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=sh8m9Rp2+67CvWq7oXUrUGNyk8tAgXE6TNuksRffV9Bo6x20pGsdAw49V0BqW6ETI uoiZIiLzzpEZ1SUmCATC1PiwiZYkydx9YAsReOJcbWciSjzV6qt7v7Jt0kXeoqP68m Le+tdN06J1IFoSBwa2CMpSwXZECs73y7snJWCqbM= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727225AbgACEBe (ORCPT ); Thu, 2 Jan 2020 23:01:34 -0500 Received: from mail.kernel.org ([198.145.29.99]:33514 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727234AbgACEBa (ORCPT ); Thu, 2 Jan 2020 23:01:30 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9868E24672 for ; Fri, 3 Jan 2020 04:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024089; bh=V3lijhL1CcRJBGzyQYzwYDpjzE+0JY70cR572uthStI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=AkSdzzljJGMqe396AU8h5zUQH5mWXMkNoSKBpQ9nERkpzsvobex8bBS/zwEHjAbK6 IKRkds3PHoCCdf8nu/MZ320LlbmCdNJONn3dknJnIZ+zNxS34BQQihMTfeUToXfQUA mEqRtW3BsIDaZDxEJAWSvlpUujdUfdtcIVOQw9M4= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 28/28] crypto: algapi - fold crypto_init_spawn() into crypto_grab_spawn() Date: Thu, 2 Jan 2020 19:59:08 -0800 Message-Id: <20200103035908.12048-29-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103035908.12048-1-ebiggers@kernel.org> References: <20200103035908.12048-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Now that crypto_init_spawn() is only called by crypto_grab_spawn(), simplify things by moving its functionality into crypto_grab_spawn(). In the process of doing this, also be more consistent about when the spawn and instance are updated, and remove the crypto_spawn::dropref flag since now it's always set. Signed-off-by: Eric Biggers --- crypto/algapi.c | 43 ++++++++++++++--------------------------- include/crypto/algapi.h | 3 --- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/crypto/algapi.c b/crypto/algapi.c index f66a4ff57e6e..72592795c7e7 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -629,8 +629,7 @@ int crypto_register_instance(struct crypto_template *tmpl, spawn->inst = inst; spawn->registered = true; - if (spawn->dropref) - crypto_mod_put(spawn->alg); + crypto_mod_put(spawn->alg); spawn = next; } @@ -672,47 +671,33 @@ void crypto_unregister_instance(struct crypto_instance *inst) } EXPORT_SYMBOL_GPL(crypto_unregister_instance); -int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg, - struct crypto_instance *inst, u32 mask) +int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst, + const char *name, u32 type, u32 mask) { + struct crypto_alg *alg; int err = -EAGAIN; if (WARN_ON_ONCE(inst == NULL)) return -EINVAL; - spawn->next = inst->spawns; - inst->spawns = spawn; + /* Allow the result of crypto_attr_alg_name() to be passed directly */ + if (IS_ERR(name)) + return PTR_ERR(name); - spawn->mask = mask; + alg = crypto_find_alg(name, spawn->frontend, type, mask); + if (IS_ERR(alg)) + return PTR_ERR(alg); down_write(&crypto_alg_sem); if (!crypto_is_moribund(alg)) { list_add(&spawn->list, &alg->cra_users); spawn->alg = alg; + spawn->mask = mask; + spawn->next = inst->spawns; + inst->spawns = spawn; err = 0; } up_write(&crypto_alg_sem); - - return err; -} -EXPORT_SYMBOL_GPL(crypto_init_spawn); - -int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst, - const char *name, u32 type, u32 mask) -{ - struct crypto_alg *alg; - int err; - - /* Allow the result of crypto_attr_alg_name() to be passed directly */ - if (IS_ERR(name)) - return PTR_ERR(name); - - alg = crypto_find_alg(name, spawn->frontend, type, mask); - if (IS_ERR(alg)) - return PTR_ERR(alg); - - spawn->dropref = true; - err = crypto_init_spawn(spawn, alg, inst, mask); if (err) crypto_mod_put(alg); return err; @@ -729,7 +714,7 @@ void crypto_drop_spawn(struct crypto_spawn *spawn) list_del(&spawn->list); up_write(&crypto_alg_sem); - if (spawn->dropref && !spawn->registered) + if (!spawn->registered) crypto_mod_put(spawn->alg); } EXPORT_SYMBOL_GPL(crypto_drop_spawn); diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h index 224c72743cce..c16c50f8dac1 100644 --- a/include/crypto/algapi.h +++ b/include/crypto/algapi.h @@ -82,7 +82,6 @@ struct crypto_spawn { const struct crypto_type *frontend; u32 mask; bool dead; - bool dropref; bool registered; }; @@ -111,8 +110,6 @@ int crypto_register_instance(struct crypto_template *tmpl, struct crypto_instance *inst); void crypto_unregister_instance(struct crypto_instance *inst); -int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg, - struct crypto_instance *inst, u32 mask); int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst, const char *name, u32 type, u32 mask); void crypto_drop_spawn(struct crypto_spawn *spawn);