From patchwork Tue Jul 24 16:49:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10542539 X-Patchwork-Delegate: snitzer@redhat.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 24663112E for ; Tue, 24 Jul 2018 16:50:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 104232909F for ; Tue, 24 Jul 2018 16:50:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0DDEF2905A; Tue, 24 Jul 2018 16:50:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7655D290E9 for ; Tue, 24 Jul 2018 16:50:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7A517308A953; Tue, 24 Jul 2018 16:50:16 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4C118308BDB7; Tue, 24 Jul 2018 16:50:16 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 1566918037F5; Tue, 24 Jul 2018 16:50:16 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6OGnv3C010917 for ; Tue, 24 Jul 2018 12:49:57 -0400 Received: by smtp.corp.redhat.com (Postfix) id C9FB8308BDBB; Tue, 24 Jul 2018 16:49:56 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx01.extmail.prod.ext.phx2.redhat.com [10.5.110.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A9896308BDB8 for ; Tue, 24 Jul 2018 16:49:56 +0000 (UTC) Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 605B0796FC for ; Tue, 24 Jul 2018 16:49:55 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id r1-v6so3293516pgp.11 for ; Tue, 24 Jul 2018 09:49:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=CPbMm0CRMjn6WWe1IlnQfdE8WKGufaSORW1Jwv5jp9g=; b=Sqy/U6lEAtaHCvGGjuA15TO8EgzKx6zxS1R9gJQDDAWcgcCf3FdmexBo+PzFomA1eZ 6Kgtq57CYV+rNLJRqvrZiZmKTnI9d9GT0L9Ya0rv5DgzRx00TaZZ3kiK3boa/VeQIjWu zR0dm4KEiWAal8wcI7KYU8TkXwp7zqUORY8AA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=CPbMm0CRMjn6WWe1IlnQfdE8WKGufaSORW1Jwv5jp9g=; b=HneA0yLyIbT61Z2ezhEtxn8N9/DJBbXyuN6fYiN4of6ZvO1zCC4h9yXwITlY2o2SbZ YhdTh+I/jgHpMKK/CD5x5njkWWNOMJMUhylzW9R3ez9wZFis8IVJEG4fCALzHGA6XosF CZt04GkJepcRVMrMPtEG10yYvwdzdo2Qzdg+msGNwl4bUw1U1hhcBhcbgCzPhmgVDu2b ch/IHn+eNISZ9IVwq+PeKrfeTK/gZC9BwNrdrCo0yzKAqhrPJWFH4TKVdhZUIxHnY/+w d23V9Cs4MtKcU4VYtnTWeW2tmpeLlRr5UOR4/aLqNob8wCCYzXogWaXJSw+TxHOzaPnn MthQ== X-Gm-Message-State: AOUpUlHHH1Ct14tqx+qbnzgQojJlXsQUGhxzKpR2LVOPkwpwd1bkVlim iAIm4f4tBdgftCQfk0BxI984iw== X-Google-Smtp-Source: AAOMgpdIDiJGQzsbr+YTILmO9p528DomYEZ+nGflZUHk5FySzT4GPqu95oyrX932WDCrQnA/7RF0pA== X-Received: by 2002:a63:2644:: with SMTP id m65-v6mr16889618pgm.371.1532450994828; Tue, 24 Jul 2018 09:49:54 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id e7-v6sm23751655pgc.55.2018.07.24.09.49.47 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 24 Jul 2018 09:49:47 -0700 (PDT) From: Kees Cook To: Herbert Xu Date: Tue, 24 Jul 2018 09:49:29 -0700 Message-Id: <20180724164936.37477-12-keescook@chromium.org> In-Reply-To: <20180724164936.37477-1-keescook@chromium.org> References: <20180724164936.37477-1-keescook@chromium.org> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 24 Jul 2018 16:49:55 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 24 Jul 2018 16:49:55 +0000 (UTC) for IP:'209.85.215.196' DOMAIN:'mail-pg1-f196.google.com' HELO:'mail-pg1-f196.google.com' FROM:'keescook@chromium.org' RCPT:'' X-RedHat-Spam-Score: -0.131 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_PASS) 209.85.215.196 mail-pg1-f196.google.com 209.85.215.196 mail-pg1-f196.google.com X-Scanned-By: MIMEDefang 2.83 on 10.5.110.25 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-loop: dm-devel@redhat.com Cc: Giovanni Cabiddu , Mike Snitzer , "Gustavo A. R. Silva" , linux-wireless@vger.kernel.org, Will Deacon , linux-kernel@vger.kernel.org, David Howells , dm-devel@redhat.com, Jia-Ju Bai , Paul Mackerras , Pavel Machek , "H. Peter Anvin" , linux-afs@lists.infradead.org, Alasdair Kergon , Kees Cook , devel@driverdev.osuosl.org, x86@kernel.org, qat-linux@intel.com, Ingo Molnar , Eric Biggers , Geert Uytterhoeven , drbd-dev@lists.linbit.com, Rabin Vincent , Arnd Bergmann , Tudor-Dan Ambarus , linux-block@vger.kernel.org, Josh Poimboeuf , Thomas Gleixner , Lars Ellenberg , Jens Axboe , Andrew Morton , Greg Kroah-Hartman , linux-pm@vger.kernel.org, "Rafael J. Wysocki" , Philipp Reisner , linux-crypto@vger.kernel.org, linux-ppp@vger.kernel.org, Johannes Berg , Tim Chen , netdev@vger.kernel.org, David Woodhouse Subject: [dm-devel] [PATCH v6 11/18] dm crypt: Convert essiv from ahash to shash X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Tue, 24 Jul 2018 16:50:17 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP In preparing to remove all stack VLA usage from the kernel[1], this removes the discouraged use of AHASH_REQUEST_ON_STACK in favor of the smaller SHASH_DESC_ON_STACK by converting from ahash-wrapped-shash to direct shash. By removing a layer of indirection this both improves performance and reduces stack usage. The stack allocation will be made a fixed size in a later patch to the crypto subsystem. [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Signed-off-by: Kees Cook Reviewed-by: Eric Biggers --- drivers/md/dm-crypt.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c index b61b069c33af..c4c922990090 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -99,7 +99,7 @@ struct crypt_iv_operations { }; struct iv_essiv_private { - struct crypto_ahash *hash_tfm; + struct crypto_shash *hash_tfm; u8 *salt; }; @@ -327,25 +327,22 @@ static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv, static int crypt_iv_essiv_init(struct crypt_config *cc) { struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; - AHASH_REQUEST_ON_STACK(req, essiv->hash_tfm); - struct scatterlist sg; + SHASH_DESC_ON_STACK(desc, essiv->hash_tfm); struct crypto_cipher *essiv_tfm; int err; - sg_init_one(&sg, cc->key, cc->key_size); - ahash_request_set_tfm(req, essiv->hash_tfm); - ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); - ahash_request_set_crypt(req, &sg, essiv->salt, cc->key_size); + desc->tfm = essiv->hash_tfm; + desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; - err = crypto_ahash_digest(req); - ahash_request_zero(req); + err = crypto_shash_digest(desc, cc->key, cc->key_size, essiv->salt); + shash_desc_zero(desc); if (err) return err; essiv_tfm = cc->iv_private; err = crypto_cipher_setkey(essiv_tfm, essiv->salt, - crypto_ahash_digestsize(essiv->hash_tfm)); + crypto_shash_digestsize(essiv->hash_tfm)); if (err) return err; @@ -356,7 +353,7 @@ static int crypt_iv_essiv_init(struct crypt_config *cc) static int crypt_iv_essiv_wipe(struct crypt_config *cc) { struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; - unsigned salt_size = crypto_ahash_digestsize(essiv->hash_tfm); + unsigned salt_size = crypto_shash_digestsize(essiv->hash_tfm); struct crypto_cipher *essiv_tfm; int r, err = 0; @@ -408,7 +405,7 @@ static void crypt_iv_essiv_dtr(struct crypt_config *cc) struct crypto_cipher *essiv_tfm; struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; - crypto_free_ahash(essiv->hash_tfm); + crypto_free_shash(essiv->hash_tfm); essiv->hash_tfm = NULL; kzfree(essiv->salt); @@ -426,7 +423,7 @@ static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti, const char *opts) { struct crypto_cipher *essiv_tfm = NULL; - struct crypto_ahash *hash_tfm = NULL; + struct crypto_shash *hash_tfm = NULL; u8 *salt = NULL; int err; @@ -436,14 +433,14 @@ static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti, } /* Allocate hash algorithm */ - hash_tfm = crypto_alloc_ahash(opts, 0, CRYPTO_ALG_ASYNC); + hash_tfm = crypto_alloc_shash(opts, 0, 0); if (IS_ERR(hash_tfm)) { ti->error = "Error initializing ESSIV hash"; err = PTR_ERR(hash_tfm); goto bad; } - salt = kzalloc(crypto_ahash_digestsize(hash_tfm), GFP_KERNEL); + salt = kzalloc(crypto_shash_digestsize(hash_tfm), GFP_KERNEL); if (!salt) { ti->error = "Error kmallocing salt storage in ESSIV"; err = -ENOMEM; @@ -454,7 +451,7 @@ static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti, cc->iv_gen_private.essiv.hash_tfm = hash_tfm; essiv_tfm = alloc_essiv_cipher(cc, ti, salt, - crypto_ahash_digestsize(hash_tfm)); + crypto_shash_digestsize(hash_tfm)); if (IS_ERR(essiv_tfm)) { crypt_iv_essiv_dtr(cc); return PTR_ERR(essiv_tfm); @@ -465,7 +462,7 @@ static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti, bad: if (hash_tfm && !IS_ERR(hash_tfm)) - crypto_free_ahash(hash_tfm); + crypto_free_shash(hash_tfm); kfree(salt); return err; }