From patchwork Tue Jul 2 16:48:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 168337 Delivered-To: patch@linaro.org Received: by 2002:ac9:6410:0:0:0:0:0 with SMTP id r16csp4130154ock; Tue, 2 Jul 2019 09:48:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqz5HSwyApcTmw1K7mg19L5V9IuF9qI13icqJ5bEl0ar5aZ8m/t41SKbBEMqx0ypr1aFVyW7 X-Received: by 2002:a63:4f07:: with SMTP id d7mr30798433pgb.77.1562086115774; Tue, 02 Jul 2019 09:48:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562086115; cv=none; d=google.com; s=arc-20160816; b=lj3V7lJYpvkgWTMzoIk5gIRR0gjx2XEVEo8+XYwEoMWV1bRybr78oHj07Xzgu37z15 6oH3OAQCakI52L+byQHaAH4mlQc67W7PYJCSiXK6em5LL1GsnPdY4Tbt3VX47LA4v3lw CodAXu3iVk09Q3Ddvce2tEiOmQF0tswjPiq+b2f0/NLbeRYv0d4C/D6/6Unk/Gl+t9L5 x9AanIXkHGHIo7ERBVJeJ9UJIdSS7oJuGDAQOMsnB8/QdKXjU63FEwNYwXwdeyJtYBv0 dTAWDVTvVFacWKsS5PBT79aZtLGtBAMZnv3GKz1FvOtaVRcAfzI2nFH+OE7zW1RYnQDA Ch7Q== 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=deqb3C583WPwsTT07806RWwyMqGNcKRuVQc+SCr8bbg=; b=BWtIeKqHuhiihA1Ek+TEcTET13p1g7qyuJvHO5f2RhlLMaeG7Ljf5VzrJ4ghfhaU9c uIP0s37aUB2gkxJVD6Gf7QDneeCa3ZLORmpfIS4kDpN4o6bb3nn55vQ+1NXUh6SO4g6n UX/K/f7KrlP047O8fKCjLJzsKCfFSX1RG2pZEQyFKs2osTuTnisXq7g5K3Ht4+JzU95j 5slxn98EhS324o5E22eGMoZY7C/yK0bwIzV9yKPuPcXfDduDgcRL4wf/nHhwKD3b7c4b fiqIZ9/0ePeob0USUFE4HOxw6ZGb4codckpWPhAEl5nZa3+MWLqWwq3P4S2cOpiVVZw2 P2ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zgHsWq06; 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 k21si3770572pls.202.2019.07.02.09.48.35; Tue, 02 Jul 2019 09:48:35 -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=zgHsWq06; 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 S1726591AbfGBQsf (ORCPT + 3 others); Tue, 2 Jul 2019 12:48:35 -0400 Received: from mail-lf1-f68.google.com ([209.85.167.68]:46979 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726664AbfGBQse (ORCPT ); Tue, 2 Jul 2019 12:48:34 -0400 Received: by mail-lf1-f68.google.com with SMTP id z15so11867696lfh.13 for ; Tue, 02 Jul 2019 09:48:32 -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=deqb3C583WPwsTT07806RWwyMqGNcKRuVQc+SCr8bbg=; b=zgHsWq06rJA92GxFdT5mTXPfAUeJYgDmPcttcmQ73pSJkbxefPkGb1Vk0lNSP9hgO0 fP1s6vWpJa3cARqJLH1u7YmTPOaU9s2/N5gNgUfVmF4uU1FGh8/bwOKlIrGAPLHqS/BC nphtqCd9w1bQV8AguKN3pSA2hyOuxnp7CB5Edi0YxVLD6lEHjZ3tqzLLixmr9jiHz1qs s84cWHZj/lcn08b5oICHheMjITocFVVAOUrDwddcA8d7dLpZ4p5Cs38RQZNk0GfcllgH gK78gd57y59KpgQlY/ZAdPSIUQiuOlq2IZJgz9F+h7uCh3JXwxcvamcrrro7znJ6lLBv 57aw== 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=deqb3C583WPwsTT07806RWwyMqGNcKRuVQc+SCr8bbg=; b=LE7pCiBdQcmlBJjO36s9AlmJIdYEFLguiA3zBj7RibkSiqaTHEHcPcYkeAejZX7ZnK gZ2vvEuvWd045HuKXSiOVML63u/Rft8qfcLl2WFzKdipqU2bvxYkFg4N25ep2ikUg+/L RlI9dg+bil9dsbh0B7gVmFCPNkPlatX0h9E1cRQpYCK7Z1Xltt6gESyfNeei/Hn9Mr5E Ot5drG/DNmHp/MTTEwQLVqtKZzlfcMRCYI8GTQoRUwqqwe7w84K9FaSC9VeBTU/tBt51 j6fG1XRtI7ZldEz/uA9TQsNhUGnpChG/lbQulYYU5PP3tsa0J8rt+1fdB6ooVS6SGp4k 7MQQ== X-Gm-Message-State: APjAAAVXAOl/JFlzO+83mF70WWSoOZ0W4gBiL23jToWYQCF8SVDz4py3 2RJ/cTpW27+nBp0D4UOIOeW8oEsK74J9lR3o X-Received: by 2002:a05:6512:21c:: with SMTP id a28mr15599420lfo.14.1562086111826; Tue, 02 Jul 2019 09:48:31 -0700 (PDT) Received: from e111045-lin.arm.com (89-212-78-239.static.t-2.net. [89.212.78.239]) by smtp.gmail.com with ESMTPSA id r17sm3906055ljc.85.2019.07.02.09.48.30 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 02 Jul 2019 09:48:31 -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 v7 2/7] fs: crypto: invoke crypto API for ESSIV handling Date: Tue, 2 Jul 2019 18:48:10 +0200 Message-Id: <20190702164815.6341-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190702164815.6341-1-ard.biesheuvel@linaro.org> References: <20190702164815.6341-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 | 95 +------------------- 4 files changed, 5 insertions(+), 105 deletions(-) -- 2.17.1 diff --git a/fs/crypto/Kconfig b/fs/crypto/Kconfig index 24ed99e2eca0..b0292da8613c 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 CRYPTO_SHA256 diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c index 335a362ee446..c53ce262a06c 100644 --- a/fs/crypto/crypto.c +++ b/fs/crypto/crypto.c @@ -136,9 +136,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); } int fscrypt_do_page_crypto(const struct inode *inode, fscrypt_direction_t rw, @@ -492,8 +489,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 7da276159593..59d0cba9cfb9 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. @@ -166,9 +160,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 dcd91a3fbe49..f39667d4316a 100644 --- a/fs/crypto/keyinfo.c +++ b/fs/crypto/keyinfo.c @@ -13,14 +13,10 @@ #include #include #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); @@ -144,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),aes,sha256)", .keysize = 16, - .ivsize = 16, - .needs_essiv = true, + .ivsize = 8, }, [FS_ENCRYPTION_MODE_AES_128_CTS] = { .friendly_name = "AES-128-CTS-CBC", @@ -377,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 @@ -454,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); @@ -470,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; } @@ -491,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); }