From patchwork Sat Aug 10 09:40:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 171012 Delivered-To: patch@linaro.org Received: by 2002:a92:d204:0:0:0:0:0 with SMTP id y4csp328420ily; Sat, 10 Aug 2019 02:41:22 -0700 (PDT) X-Google-Smtp-Source: APXvYqzRz7F/E+KGQGh2+Ba7rDeKFcstAKBBkduEB7kQd3OwXkLAQ4j+C7GeYTyqAg9GLj1Tk5wO X-Received: by 2002:a62:ae02:: with SMTP id q2mr25614946pff.1.1565430082163; Sat, 10 Aug 2019 02:41:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565430082; cv=none; d=google.com; s=arc-20160816; b=v39VX+luwzGlXMSfhKytoZWYgd8VuUS3CxcLW0SU3AqVHbLubJn1+RfAgoSaZIhdUS i5oirpzsjuuEN5g0r/a1SWZLhs6QYNU09hrh+vkssHbaHRC9oaM8niYfG6PoqJPlEqBE myIiEb0iCCJGQgpNZX4oPxTwzMkHxFJsV4odC4NfB8ZITUqMkXaLEiKSjddBKSv7vW2Y +D0+icM8s6/SGh14k3X41w1cl6vREb5PHouNnbHpPRnVp2yosJ1jw1Ah1mQ/Dz41TyAi HfVyvBDmGToIUfz8cqCZOZ0XMtmr2Xlx3/f3w24CIRPpTSCw7I6Q44dghKfpAln1jOHR 51wg== 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; bh=WCWl+tpGIKbnP/rNTSW1y6zpS02IMHAFrYZrL23PudA=; b=IAgvjiImwGLP1nZjC4AvewW/79jE0DM1VtlrWw1DYFaj9ofM0EGUJz1sU1kpca4VpE A/wIFpA/OtjYqfhVfvYAJtj9pWi181q5qKkOrFisp2hK9jxMBsjpMqIMmXAxKrPRMTHI jxubJ+ju7XQCXf0yR3HxXuDRKL+hnLXjYgJ/9GpW64uJI7JhfcA0/VNsG3jg2FqONoyJ YSgtinQLACkJ2sSqV1qCtsVCqr4aiNTgVy8ljHe9six3jI1/X6A1Bns8CH/UmUYNsfA7 FtLbz+aF7GvmR0qRTAhdvd7W3X/LHBzMlwUBYl+EjlMvUzeDevyfsSkPFq24sKpX5AAU MtOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=lIG1bQYT; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c2si16412879plz.407.2019.08.10.02.41.21; Sat, 10 Aug 2019 02:41:22 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-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=@linaro.org header.s=google header.b=lIG1bQYT; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725888AbfHJJlV (ORCPT + 3 others); Sat, 10 Aug 2019 05:41:21 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:39197 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726145AbfHJJlV (ORCPT ); Sat, 10 Aug 2019 05:41:21 -0400 Received: by mail-wr1-f68.google.com with SMTP id t16so10230556wra.6 for ; Sat, 10 Aug 2019 02:41:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=WCWl+tpGIKbnP/rNTSW1y6zpS02IMHAFrYZrL23PudA=; b=lIG1bQYTyTk9Ltdgc1P+te+E9IG/mJdvbes1vypeL5iM+EEResFKLnzqdJWEIWzBr+ GyJ4a886QCM4HVRKL0ip+dKLgyldwuX4YJH+LLfQnP201B4pGmbafTfSf6gu8DIwB3Hq 3HNf1cc0TfRBJgFku+7hPuTtSnRqz31sC7yw9pWfnqQc57zchfIqZyA0PiVA2XZ6jJlc pQtI8t/QDGEuIsutl5usskHaTLI2OPMrgoqeyMlDZcLVIjtyjX11CSIlSGeHTExB/9Tj lp0WFJrKrm0btpfbJCloH3M81jLMs7wyKMhcJ1qDSu50VNAzedD5jhxqL0yfqWSjJGLz UINA== 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=WCWl+tpGIKbnP/rNTSW1y6zpS02IMHAFrYZrL23PudA=; b=Z3M8O32fvXV4E3PQqEefs+Y0yBf/d79dNzI6RjwywWyC//62pzeZbHfOcZvmTmSws/ a3N5zS9E4QHYepZ+SLn/HS2M7zzZmZaSW2tkTQ2G7v1deb3cK9YTHRwRFD7J4v0VG37k pDNsviL6cPGYavzjgygDHrmGa4fnyjzcY9YglJANr3G6MtUEdLopX4IuOTTuSHEXHkF5 WFbqQPhzP9DIWXQAkO3AlgwWpRLjf42m0hUhrRUGwdS+p7Vh8lLsOcc8uhLumYcMhkMv ZcXPDVR7+5dzKA53hr3sNclwa8B/9scABmRBodoariNkEHXxvg8fWxOKQTi3/3zK1Z7O tdPw== X-Gm-Message-State: APjAAAWm7tskbqKzMbF+Yyy/gqI/GOYILhgGi6UvOss2j05+LMzpDbVv uS1iab/37UxHBIEQpXKLk+/urwXPDoYQCw== X-Received: by 2002:adf:e602:: with SMTP id p2mr29379166wrm.306.1565430077936; Sat, 10 Aug 2019 02:41:17 -0700 (PDT) Received: from localhost.localdomain ([2a02:587:a407:da00:582f:8334:9cd9:7241]) by smtp.gmail.com with ESMTPSA id n16sm519883wmk.12.2019.08.10.02.41.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 10 Aug 2019 02:41:17 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: Ard Biesheuvel , Herbert Xu , Eric Biggers , dm-devel@redhat.com, linux-fscrypt@vger.kernel.org, Gilad Ben-Yossef , Milan Broz Subject: [PATCH v9 2/7] fs: crypto: invoke crypto API for ESSIV handling Date: Sat, 10 Aug 2019 12:40:48 +0300 Message-Id: <20190810094053.7423-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190810094053.7423-1-ard.biesheuvel@linaro.org> References: <20190810094053.7423-1-ard.biesheuvel@linaro.org> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Instead of open coding the calculations for ESSIV handling, use a ESSIV skcipher which does all of this under the hood. Signed-off-by: Ard Biesheuvel --- fs/crypto/Kconfig | 1 + fs/crypto/crypto.c | 5 -- fs/crypto/fscrypt_private.h | 9 -- fs/crypto/keyinfo.c | 92 +------------------- 4 files changed, 4 insertions(+), 103 deletions(-) -- 2.17.1 diff --git a/fs/crypto/Kconfig b/fs/crypto/Kconfig index 5fdf24877c17..6f3d59b880b7 100644 --- a/fs/crypto/Kconfig +++ b/fs/crypto/Kconfig @@ -5,6 +5,7 @@ config FS_ENCRYPTION select CRYPTO_AES select CRYPTO_CBC select CRYPTO_ECB + select CRYPTO_ESSIV select CRYPTO_XTS select CRYPTO_CTS select KEYS diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c index 45c3d0427fb2..fd13231c5ff6 100644 --- a/fs/crypto/crypto.c +++ b/fs/crypto/crypto.c @@ -143,9 +143,6 @@ void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num, if (ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY) memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE); - - if (ci->ci_essiv_tfm != NULL) - crypto_cipher_encrypt_one(ci->ci_essiv_tfm, iv->raw, iv->raw); } /* Encrypt or decrypt a single filesystem block of file contents */ @@ -523,8 +520,6 @@ static void __exit fscrypt_exit(void) destroy_workqueue(fscrypt_read_workqueue); kmem_cache_destroy(fscrypt_ctx_cachep); kmem_cache_destroy(fscrypt_info_cachep); - - fscrypt_essiv_cleanup(); } module_exit(fscrypt_exit); diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h index 8978eec9d766..2fc6f0bd2d13 100644 --- a/fs/crypto/fscrypt_private.h +++ b/fs/crypto/fscrypt_private.h @@ -61,12 +61,6 @@ struct fscrypt_info { /* The actual crypto transform used for encryption and decryption */ struct crypto_skcipher *ci_ctfm; - /* - * Cipher for ESSIV IV generation. Only set for CBC contents - * encryption, otherwise is NULL. - */ - struct crypto_cipher *ci_essiv_tfm; - /* * Encryption mode used for this inode. It corresponds to either * ci_data_mode or ci_filename_mode, depending on the inode type. @@ -163,9 +157,6 @@ struct fscrypt_mode { int keysize; int ivsize; bool logged_impl_name; - bool needs_essiv; }; -extern void __exit fscrypt_essiv_cleanup(void); - #endif /* _FSCRYPT_PRIVATE_H */ diff --git a/fs/crypto/keyinfo.c b/fs/crypto/keyinfo.c index 207ebed918c1..80924a0f72ca 100644 --- a/fs/crypto/keyinfo.c +++ b/fs/crypto/keyinfo.c @@ -14,12 +14,9 @@ #include #include #include -#include #include #include "fscrypt_private.h" -static struct crypto_shash *essiv_hash_tfm; - /* Table of keys referenced by FS_POLICY_FLAG_DIRECT_KEY policies */ static DEFINE_HASHTABLE(fscrypt_master_keys, 6); /* 6 bits = 64 buckets */ static DEFINE_SPINLOCK(fscrypt_master_keys_lock); @@ -143,10 +140,9 @@ static struct fscrypt_mode available_modes[] = { }, [FS_ENCRYPTION_MODE_AES_128_CBC] = { .friendly_name = "AES-128-CBC", - .cipher_str = "cbc(aes)", + .cipher_str = "essiv(cbc(aes),sha256)", .keysize = 16, .ivsize = 16, - .needs_essiv = true, }, [FS_ENCRYPTION_MODE_AES_128_CTS] = { .friendly_name = "AES-128-CTS-CBC", @@ -376,72 +372,6 @@ fscrypt_get_master_key(const struct fscrypt_info *ci, struct fscrypt_mode *mode, return ERR_PTR(err); } -static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt) -{ - struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm); - - /* init hash transform on demand */ - if (unlikely(!tfm)) { - struct crypto_shash *prev_tfm; - - tfm = crypto_alloc_shash("sha256", 0, 0); - if (IS_ERR(tfm)) { - fscrypt_warn(NULL, - "error allocating SHA-256 transform: %ld", - PTR_ERR(tfm)); - return PTR_ERR(tfm); - } - prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm); - if (prev_tfm) { - crypto_free_shash(tfm); - tfm = prev_tfm; - } - } - - { - SHASH_DESC_ON_STACK(desc, tfm); - desc->tfm = tfm; - - return crypto_shash_digest(desc, key, keysize, salt); - } -} - -static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key, - int keysize) -{ - int err; - struct crypto_cipher *essiv_tfm; - u8 salt[SHA256_DIGEST_SIZE]; - - essiv_tfm = crypto_alloc_cipher("aes", 0, 0); - if (IS_ERR(essiv_tfm)) - return PTR_ERR(essiv_tfm); - - ci->ci_essiv_tfm = essiv_tfm; - - err = derive_essiv_salt(raw_key, keysize, salt); - if (err) - goto out; - - /* - * Using SHA256 to derive the salt/key will result in AES-256 being - * used for IV generation. File contents encryption will still use the - * configured keysize (AES-128) nevertheless. - */ - err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt)); - if (err) - goto out; - -out: - memzero_explicit(salt, sizeof(salt)); - return err; -} - -void __exit fscrypt_essiv_cleanup(void) -{ - crypto_free_shash(essiv_hash_tfm); -} - /* * Given the encryption mode and key (normally the derived key, but for * FS_POLICY_FLAG_DIRECT_KEY mode it's the master key), set up the inode's @@ -453,7 +383,6 @@ static int setup_crypto_transform(struct fscrypt_info *ci, { struct fscrypt_master_key *mk; struct crypto_skcipher *ctfm; - int err; if (ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY) { mk = fscrypt_get_master_key(ci, mode, raw_key, inode); @@ -469,19 +398,6 @@ static int setup_crypto_transform(struct fscrypt_info *ci, ci->ci_master_key = mk; ci->ci_ctfm = ctfm; - if (mode->needs_essiv) { - /* ESSIV implies 16-byte IVs which implies !DIRECT_KEY */ - WARN_ON(mode->ivsize != AES_BLOCK_SIZE); - WARN_ON(ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY); - - err = init_essiv_generator(ci, raw_key, mode->keysize); - if (err) { - fscrypt_warn(inode->i_sb, - "error initializing ESSIV generator for inode %lu: %d", - inode->i_ino, err); - return err; - } - } return 0; } @@ -490,12 +406,10 @@ static void put_crypt_info(struct fscrypt_info *ci) if (!ci) return; - if (ci->ci_master_key) { + if (ci->ci_master_key) put_master_key(ci->ci_master_key); - } else { + else crypto_free_skcipher(ci->ci_ctfm); - crypto_free_cipher(ci->ci_essiv_tfm); - } kmem_cache_free(fscrypt_info_cachep, ci); }