Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp7191985imm; Tue, 24 Jul 2018 09:53:32 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfCnk6XaDayG43CQcY/joBsCKHkcPGbTjPLC8A/yqrMk1/I1vpEXB3Csz+Yd1iznySRujUu X-Received: by 2002:a62:3d41:: with SMTP id k62-v6mr18497935pfa.35.1532451212916; Tue, 24 Jul 2018 09:53:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532451212; cv=none; d=google.com; s=arc-20160816; b=W2K7ADqHzpBzXkFyvifTrteq3Az1ZpCXmTHZArjyliZz0rLApjRJWprkdxEHe5BNPf 6AXyg4OhIpEPnRhhLnPA0zXi7CkQaoHky6iP36CSsC6/muOIzPzisJ5n2GBFrAumh42y 673x0T0KHgj5ke7Sxowb2rsar1kcKKqGOvUDiBiiVRnRLatMLASFwzURpda4rxAz+pJi aV8NicmENYKGoBYDoZzYgmjU/872O4AwwmWnjHyMFPrFyD5hdjNY+UsexJ0icktDfNpa w3TDxd7q6prNdk6MVkZDAzlE6/7dTeZ/9M5ReROEIH4e5suNYXh4uByGcBQnLLHMumrP AfFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=CPbMm0CRMjn6WWe1IlnQfdE8WKGufaSORW1Jwv5jp9g=; b=zq1f2CYtfV+KEYgaLOfOBGIqXSN7LW0JvqGlPaLg+61EIc82MjGnS17YV2e62i7s67 nRrGbj3Zt6zhAtICer+/6HFkP4kUd7Y+2xlXKGVbNH3wrjEP8AobPtoaOWWn1COI4ulm BkkOIjSusI2z/vf8+PNIQ48+4TOSePU3NWOXSdFnM9bFfBH069jbNkbfxU/jPHEMrlVm xW6oLdCFgFy3v3rLgKbsWWCHm/D9zhK73WBYJI51giaoCmpzyCmdMPwFuLEFcO/1kJXF ZE4HMiUGXjeEOeXnwj6ZV2DBsW3NmU9hkgrbj+L/4l6Y0XmHJWMeNZ1Dpr4amJfHNeZf IoLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="Sqy/U6lE"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1-v6si11515503pls.385.2018.07.24.09.53.18; Tue, 24 Jul 2018 09:53:32 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="Sqy/U6lE"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388694AbeGXR5R (ORCPT + 99 others); Tue, 24 Jul 2018 13:57:17 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:32947 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388628AbeGXR5P (ORCPT ); Tue, 24 Jul 2018 13:57:15 -0400 Received: by mail-pg1-f196.google.com with SMTP id r5-v6so3305619pgv.0 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=r9uxd/h8XtXUQs20spN+tdPgwzyUo70NUrV4LkiXMqmzkBYaKgruOw/0U/COdCxiL6 qkEq522fTTZrNmV7M+M+Y6sa5jPS6O5wa7V5WSO+TQ3zL0CCmPYQodb8sBqHYbYghDXI bIaiKuym/Oz9SK14qoutzNS1q3hTwhBup7nooD6Ah/zpalnw0WohZW6oYjdnYxHLiqyJ Z8fshRdZtW5uN+O26qgW5S/IYxvvfRJznCLyfON8V/pbhJJ5kRT1cjNAUyDNIDazfHSc rBEaOy5254q2ZfYaIkVK79q/0+pxRsd2h6P317iryvb4kWpfcj8CawSTCLI0KXMliOC2 adGg== X-Gm-Message-State: AOUpUlH1GoY6uVcEL9omR9BkzmMRLo+Xbxp0WZsNyMBBASxf3lxv6cyv QZ71JfhXZt64obt8CEMV11M+Ow== 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 Cc: Kees Cook , Arnd Bergmann , Eric Biggers , "Gustavo A. R. Silva" , Alasdair Kergon , Rabin Vincent , Tim Chen , "Rafael J. Wysocki" , Pavel Machek , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Philipp Reisner , Lars Ellenberg , Jens Axboe , Giovanni Cabiddu , Mike Snitzer , Paul Mackerras , Greg Kroah-Hartman , David Howells , Johannes Berg , Tudor-Dan Ambarus , Jia-Ju Bai , Andrew Morton , Geert Uytterhoeven , Josh Poimboeuf , David Woodhouse , Will Deacon , dm-devel@redhat.com, linux-pm@vger.kernel.org, linux-crypto@vger.kernel.org, drbd-dev@lists.linbit.com, linux-block@vger.kernel.org, qat-linux@intel.com, linux-ppp@vger.kernel.org, netdev@vger.kernel.org, devel@driverdev.osuosl.org, linux-afs@lists.infradead.org, linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 11/18] dm crypt: Convert essiv from ahash to shash Date: Tue, 24 Jul 2018 09:49:29 -0700 Message-Id: <20180724164936.37477-12-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180724164936.37477-1-keescook@chromium.org> References: <20180724164936.37477-1-keescook@chromium.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; } -- 2.17.1