All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4]  crypto: switch to crypto API for ESSIV generation
@ 2019-06-18 21:27 ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-18 21:27 UTC (permalink / raw)
  To: linux-crypto
  Cc: Ard Biesheuvel, Herbert Xu, Eric Biggers, dm-devel,
	linux-fscrypt, Gilad Ben-Yossef, Milan Broz

This series creates an ESSIV template that produces a skcipher or AEAD
transform based on a tuple of the form '<skcipher>,<cipher>,<shash>'
(or '<aead>,<cipher>,<shash>' for the AEAD case). It exposes the
encapsulated sync or async skcipher/aead by passing through all operations,
while using the cipher/shash pair to transform the input IV into an ESSIV
output IV.

This matches what both users of ESSIV in the kernel do, and so it is proposed
as a replacement for those, in patches #2 and #4.

This code has been tested using the fscrypt test suggested by Eric
(generic/549), as well as the mode-test script suggested by Milan for
the dm-crypt case. I also tested the aead case in a virtual machine,
but it definitely needs some wider testing from the dm-crypt experts.

Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Eric Biggers <ebiggers@google.com>
Cc: dm-devel@redhat.com
Cc: linux-fscrypt@vger.kernel.org
Cc: Gilad Ben-Yossef <gilad@benyossef.com>
Cc: Milan Broz <gmazyland@gmail.com>

Ard Biesheuvel (4):
  crypto: essiv - create wrapper template for ESSIV generation
  fs: crypto: invoke crypto API for ESSIV handling
  md: dm-crypt: infer ESSIV block cipher from cipher string directly
  md: dm-crypt: switch to ESSIV crypto API template

 crypto/Kconfig              |   4 +
 crypto/Makefile             |   1 +
 crypto/essiv.c              | 624 ++++++++++++++++++++
 drivers/md/Kconfig          |   1 +
 drivers/md/dm-crypt.c       | 237 ++------
 fs/crypto/Kconfig           |   1 +
 fs/crypto/crypto.c          |   5 -
 fs/crypto/fscrypt_private.h |   9 -
 fs/crypto/keyinfo.c         |  88 +--
 9 files changed, 675 insertions(+), 295 deletions(-)
 create mode 100644 crypto/essiv.c

-- 
2.17.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-18 21:27 ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-18 21:27 UTC (permalink / raw)
  To: linux-crypto
  Cc: Herbert Xu, Eric Biggers, Ard Biesheuvel, linux-fscrypt,
	Gilad Ben-Yossef, dm-devel, Milan Broz

This series creates an ESSIV template that produces a skcipher or AEAD
transform based on a tuple of the form '<skcipher>,<cipher>,<shash>'
(or '<aead>,<cipher>,<shash>' for the AEAD case). It exposes the
encapsulated sync or async skcipher/aead by passing through all operations,
while using the cipher/shash pair to transform the input IV into an ESSIV
output IV.

This matches what both users of ESSIV in the kernel do, and so it is proposed
as a replacement for those, in patches #2 and #4.

This code has been tested using the fscrypt test suggested by Eric
(generic/549), as well as the mode-test script suggested by Milan for
the dm-crypt case. I also tested the aead case in a virtual machine,
but it definitely needs some wider testing from the dm-crypt experts.

Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Eric Biggers <ebiggers@google.com>
Cc: dm-devel@redhat.com
Cc: linux-fscrypt@vger.kernel.org
Cc: Gilad Ben-Yossef <gilad@benyossef.com>
Cc: Milan Broz <gmazyland@gmail.com>

Ard Biesheuvel (4):
  crypto: essiv - create wrapper template for ESSIV generation
  fs: crypto: invoke crypto API for ESSIV handling
  md: dm-crypt: infer ESSIV block cipher from cipher string directly
  md: dm-crypt: switch to ESSIV crypto API template

 crypto/Kconfig              |   4 +
 crypto/Makefile             |   1 +
 crypto/essiv.c              | 624 ++++++++++++++++++++
 drivers/md/Kconfig          |   1 +
 drivers/md/dm-crypt.c       | 237 ++------
 fs/crypto/Kconfig           |   1 +
 fs/crypto/crypto.c          |   5 -
 fs/crypto/fscrypt_private.h |   9 -
 fs/crypto/keyinfo.c         |  88 +--
 9 files changed, 675 insertions(+), 295 deletions(-)
 create mode 100644 crypto/essiv.c

-- 
2.17.1

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v2 1/4] crypto: essiv - create wrapper template for ESSIV generation
  2019-06-18 21:27 ` Ard Biesheuvel
@ 2019-06-18 21:27   ` Ard Biesheuvel
  -1 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-18 21:27 UTC (permalink / raw)
  To: linux-crypto
  Cc: Ard Biesheuvel, Herbert Xu, Eric Biggers, dm-devel,
	linux-fscrypt, Gilad Ben-Yossef, Milan Broz

Implement a template that wraps a (skcipher,cipher,shash) or
(aead,cipher,shash) tuple so that we can consolidate the ESSIV handling
in fscrypt and dm-crypt and move it into the crypto API. This will result
in better test coverage, and will allow future changes to make the bare
cipher interface internal to the crypto subsystem, in order to increase
robustness of the API against misuse.

Note that especially the AEAD handling is a bit complex, and is tightly
coupled to the way dm-crypt combines AEAD based on the authenc() template
with the ESSIV handling.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 crypto/Kconfig  |   4 +
 crypto/Makefile |   1 +
 crypto/essiv.c  | 624 ++++++++++++++++++++
 3 files changed, 629 insertions(+)

diff --git a/crypto/Kconfig b/crypto/Kconfig
index 3d056e7da65f..1aa47087c1a2 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -1917,6 +1917,10 @@ config CRYPTO_STATS
 config CRYPTO_HASH_INFO
 	bool
 
+config CRYPTO_ESSIV
+	tristate
+	select CRYPTO_AUTHENC
+
 source "drivers/crypto/Kconfig"
 source "crypto/asymmetric_keys/Kconfig"
 source "certs/Kconfig"
diff --git a/crypto/Makefile b/crypto/Makefile
index 266a4cdbb9e2..ad1d99ba6d56 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -148,6 +148,7 @@ obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o
 obj-$(CONFIG_CRYPTO_ZSTD) += zstd.o
 obj-$(CONFIG_CRYPTO_OFB) += ofb.o
 obj-$(CONFIG_CRYPTO_ECC) += ecc.o
+obj-$(CONFIG_CRYPTO_ESSIV) += essiv.o
 
 ecdh_generic-y += ecdh.o
 ecdh_generic-y += ecdh_helper.o
diff --git a/crypto/essiv.c b/crypto/essiv.c
new file mode 100644
index 000000000000..029a65afb4d7
--- /dev/null
+++ b/crypto/essiv.c
@@ -0,0 +1,624 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ESSIV skcipher template for block encryption
+ *
+ * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
+ *
+ * Heavily based on:
+ * adiantum length-preserving encryption mode
+ *
+ * Copyright 2018 Google LLC
+ */
+
+#include <crypto/authenc.h>
+#include <crypto/internal/aead.h>
+#include <crypto/internal/hash.h>
+#include <crypto/internal/skcipher.h>
+#include <crypto/scatterwalk.h>
+#include <linux/module.h>
+
+#include "internal.h"
+
+#define ESSIV_IV_SIZE		sizeof(u64)	// IV size of the outer algo
+#define MAX_INNER_IV_SIZE	16		// max IV size of inner algo
+
+struct essiv_instance_ctx {
+	union {
+		struct crypto_skcipher_spawn	blockcipher_spawn;
+		struct crypto_aead_spawn	aead_spawn;
+	} u;
+	struct crypto_spawn			essiv_cipher_spawn;
+	struct crypto_shash_spawn		hash_spawn;
+};
+
+struct essiv_tfm_ctx {
+	union {
+		struct crypto_skcipher	*blockcipher;
+		struct crypto_aead	*aead;
+	} u;
+	struct crypto_cipher		*essiv_cipher;
+	struct crypto_shash		*hash;
+};
+
+struct essiv_skcipher_request_ctx {
+	u8				iv[MAX_INNER_IV_SIZE];
+	struct skcipher_request		blockcipher_req;
+};
+
+struct essiv_aead_request_ctx {
+	u8				iv[MAX_INNER_IV_SIZE];
+	struct scatterlist		src[4], dst[4];
+	struct aead_request		aead_req;
+};
+
+static int essiv_skcipher_setkey(struct crypto_skcipher *tfm,
+				 const u8 *key, unsigned int keylen)
+{
+	u32 flags = crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_REQ_MASK;
+	struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
+	SHASH_DESC_ON_STACK(desc, tctx->hash);
+	unsigned int saltsize;
+	u8 *salt;
+	int err;
+
+	crypto_skcipher_clear_flags(tctx->u.blockcipher, CRYPTO_TFM_REQ_MASK);
+	crypto_skcipher_set_flags(tctx->u.blockcipher, flags);
+	err = crypto_skcipher_setkey(tctx->u.blockcipher, key, keylen);
+	crypto_skcipher_set_flags(tfm,
+				  crypto_skcipher_get_flags(tctx->u.blockcipher) &
+				  CRYPTO_TFM_RES_MASK);
+	if (err)
+		return err;
+
+	saltsize = crypto_shash_digestsize(tctx->hash);
+	salt = kmalloc(saltsize, GFP_KERNEL);
+	if (!salt)
+		return -ENOMEM;
+
+	desc->tfm = tctx->hash;
+	crypto_shash_digest(desc, key, keylen, salt);
+
+	crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
+	crypto_cipher_set_flags(tctx->essiv_cipher, flags & CRYPTO_TFM_REQ_MASK);
+	err = crypto_cipher_setkey(tctx->essiv_cipher, salt, saltsize);
+	flags = crypto_cipher_get_flags(tctx->essiv_cipher) & CRYPTO_TFM_RES_MASK;
+	crypto_skcipher_set_flags(tfm, flags);
+
+	kzfree(salt);
+	return err;
+}
+
+static int essiv_aead_setkey(struct crypto_aead *tfm, const u8 *key,
+			     unsigned int keylen)
+{
+	struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
+	SHASH_DESC_ON_STACK(desc, tctx->hash);
+	struct crypto_authenc_keys keys;
+	unsigned int saltsize;
+	u8 *salt;
+	int err;
+
+	crypto_aead_clear_flags(tctx->u.aead, CRYPTO_TFM_REQ_MASK);
+	crypto_aead_set_flags(tctx->u.aead, crypto_aead_get_flags(tfm) &
+					    CRYPTO_TFM_REQ_MASK);
+	err = crypto_aead_setkey(tctx->u.aead, key, keylen);
+	crypto_aead_set_flags(tfm, crypto_aead_get_flags(tctx->u.aead) &
+				   CRYPTO_TFM_RES_MASK);
+	if (err)
+		return err;
+
+	if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
+		crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
+		return -EINVAL;
+	}
+
+	saltsize = crypto_shash_digestsize(tctx->hash);
+	salt = kmalloc(saltsize, GFP_KERNEL);
+	if (!salt)
+		return -ENOMEM;
+
+	desc->tfm = tctx->hash;
+	crypto_shash_init(desc);
+	crypto_shash_update(desc, keys.enckey, keys.enckeylen);
+	crypto_shash_finup(desc, keys.authkey, keys.authkeylen, salt);
+
+	crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
+	crypto_cipher_set_flags(tctx->essiv_cipher, crypto_aead_get_flags(tfm) &
+						    CRYPTO_TFM_REQ_MASK);
+	err = crypto_cipher_setkey(tctx->essiv_cipher, salt, saltsize);
+	crypto_aead_set_flags(tfm, crypto_cipher_get_flags(tctx->essiv_cipher) &
+				   CRYPTO_TFM_RES_MASK);
+
+	kzfree(salt);
+	return err;
+}
+
+static int essiv_aead_setauthsize(struct crypto_aead *tfm,
+				  unsigned int authsize)
+{
+	struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
+
+	return crypto_aead_setauthsize(tctx->u.aead, authsize);
+}
+
+static void essiv_skcipher_done(struct crypto_async_request *areq, int err)
+{
+	struct skcipher_request *req = areq->data;
+
+	skcipher_request_complete(req, err);
+}
+
+static void essiv_aead_done(struct crypto_async_request *areq, int err)
+{
+	struct aead_request *req = areq->data;
+
+	aead_request_complete(req, err);
+}
+
+static void essiv_skcipher_prepare_subreq(struct skcipher_request *req)
+{
+	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
+	const struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
+	struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
+	struct skcipher_request *subreq = &rctx->blockcipher_req;
+
+	memset(rctx->iv, 0, crypto_cipher_blocksize(tctx->essiv_cipher));
+	memcpy(rctx->iv, req->iv, crypto_skcipher_ivsize(tfm));
+
+	crypto_cipher_encrypt_one(tctx->essiv_cipher, rctx->iv, rctx->iv);
+
+	skcipher_request_set_tfm(subreq, tctx->u.blockcipher);
+	skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
+				   rctx->iv);
+	skcipher_request_set_callback(subreq, req->base.flags,
+				      essiv_skcipher_done, req);
+}
+
+static int essiv_aead_prepare_subreq(struct aead_request *req)
+{
+	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
+	const struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
+	struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
+	int ivsize = crypto_cipher_blocksize(tctx->essiv_cipher);
+	int ssize = req->assoclen - crypto_aead_ivsize(tfm);
+	struct aead_request *subreq = &rctx->aead_req;
+	struct scatterlist *sg;
+
+	/*
+	 * dm-crypt embeds the sector number and the IV in the AAD region so we
+	 * have to splice the converted IV into the subrequest that we pass on
+	 * to the AEAD transform. This means we are tightly coupled to dm-crypt,
+	 * but that should be the only user of this code in AEAD mode.
+	 */
+	if (ssize < 0 || sg_nents_for_len(req->src, ssize) != 1)
+		return -EINVAL;
+
+	memset(rctx->iv, 0, ivsize);
+	memcpy(rctx->iv, req->iv, crypto_aead_ivsize(tfm));
+
+	crypto_cipher_encrypt_one(tctx->essiv_cipher, rctx->iv, rctx->iv);
+
+	sg_init_table(rctx->src, 4);
+	sg_set_page(rctx->src, sg_page(req->src), ssize, req->src->offset);
+	sg_set_buf(rctx->src + 1, rctx->iv, ivsize);
+	sg = scatterwalk_ffwd(rctx->src + 2, req->src, req->assoclen);
+	if (sg != rctx->src + 2)
+		sg_chain(rctx->src, 3, sg);
+
+	sg_init_table(rctx->dst, 4);
+	sg_set_page(rctx->dst, sg_page(req->dst), ssize, req->dst->offset);
+	sg_set_buf(rctx->dst + 1, rctx->iv, ivsize);
+	sg = scatterwalk_ffwd(rctx->dst + 2, req->dst, req->assoclen);
+	if (sg != rctx->dst + 2)
+		sg_chain(rctx->dst, 3, sg);
+
+	aead_request_set_tfm(subreq, tctx->u.aead);
+	aead_request_set_crypt(subreq, rctx->src, rctx->dst, req->cryptlen,
+			       rctx->iv);
+	aead_request_set_ad(subreq, ssize + ivsize);
+	aead_request_set_callback(subreq, req->base.flags, essiv_aead_done, req);
+
+	return 0;
+}
+
+static int essiv_skcipher_encrypt(struct skcipher_request *req)
+{
+	struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
+
+	essiv_skcipher_prepare_subreq(req);
+	return crypto_skcipher_encrypt(&rctx->blockcipher_req);
+}
+
+static int essiv_aead_encrypt(struct aead_request *req)
+{
+	struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
+	int err;
+
+	err = essiv_aead_prepare_subreq(req);
+	if (err)
+		return err;
+	return crypto_aead_encrypt(&rctx->aead_req);
+}
+
+static int essiv_skcipher_decrypt(struct skcipher_request *req)
+{
+	struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
+	return crypto_skcipher_decrypt(&rctx->blockcipher_req);
+}
+
+static int essiv_aead_decrypt(struct aead_request *req)
+{
+	struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
+	int err;
+
+	err = essiv_aead_prepare_subreq(req);
+	if (err)
+		return err;
+
+	essiv_aead_prepare_subreq(req);
+	return crypto_aead_decrypt(&rctx->aead_req);
+}
+
+static int essiv_init_tfm(struct essiv_instance_ctx *ictx,
+			  struct essiv_tfm_ctx *tctx)
+{
+	struct crypto_cipher *essiv_cipher;
+	struct crypto_shash *hash;
+	int err;
+
+	essiv_cipher = crypto_spawn_cipher(&ictx->essiv_cipher_spawn);
+	if (IS_ERR(essiv_cipher))
+		return PTR_ERR(essiv_cipher);
+
+	hash = crypto_spawn_shash(&ictx->hash_spawn);
+	if (IS_ERR(hash)) {
+		err = PTR_ERR(hash);
+		goto err_free_essiv_cipher;
+	}
+
+	tctx->essiv_cipher = essiv_cipher;
+	tctx->hash = hash;
+
+	return 0;
+
+err_free_essiv_cipher:
+	crypto_free_cipher(essiv_cipher);
+	return err;
+}
+
+static int essiv_skcipher_init_tfm(struct crypto_skcipher *tfm)
+{
+	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
+	struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
+	struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
+	struct crypto_skcipher *blockcipher;
+	unsigned int subreq_size;
+	int err;
+
+	BUILD_BUG_ON(offsetofend(struct essiv_skcipher_request_ctx,
+				 blockcipher_req) !=
+		     sizeof(struct essiv_skcipher_request_ctx));
+
+	blockcipher = crypto_spawn_skcipher(&ictx->u.blockcipher_spawn);
+	if (IS_ERR(blockcipher))
+		return PTR_ERR(blockcipher);
+
+	subreq_size = FIELD_SIZEOF(struct essiv_skcipher_request_ctx,
+				   blockcipher_req) +
+		      crypto_skcipher_reqsize(blockcipher);
+
+	crypto_skcipher_set_reqsize(tfm, offsetof(struct essiv_skcipher_request_ctx,
+						  blockcipher_req) + subreq_size);
+
+	err = essiv_init_tfm(ictx, tctx);
+	if (err)
+		crypto_free_skcipher(blockcipher);
+
+	tctx->u.blockcipher = blockcipher;
+	return err;
+}
+
+static int essiv_aead_init_tfm(struct crypto_aead *tfm)
+{
+	struct aead_instance *inst = aead_alg_instance(tfm);
+	struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
+	struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
+	struct crypto_aead *aead;
+	unsigned int subreq_size;
+	int err;
+
+	BUILD_BUG_ON(offsetofend(struct essiv_aead_request_ctx, aead_req) !=
+		     sizeof(struct essiv_aead_request_ctx));
+
+	aead = crypto_spawn_aead(&ictx->u.aead_spawn);
+	if (IS_ERR(aead))
+		return PTR_ERR(aead);
+
+	subreq_size = FIELD_SIZEOF(struct essiv_aead_request_ctx, aead_req) +
+		      crypto_aead_reqsize(aead);
+
+	crypto_aead_set_reqsize(tfm, offsetof(struct essiv_aead_request_ctx,
+					      aead_req) + subreq_size);
+
+	err = essiv_init_tfm(ictx, tctx);
+	if (err)
+		crypto_free_aead(aead);
+
+	tctx->u.aead = aead;
+	return err;
+}
+
+static void essiv_skcipher_exit_tfm(struct crypto_skcipher *tfm)
+{
+	struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
+
+	crypto_free_skcipher(tctx->u.blockcipher);
+	crypto_free_cipher(tctx->essiv_cipher);
+	crypto_free_shash(tctx->hash);
+}
+
+static void essiv_aead_exit_tfm(struct crypto_aead *tfm)
+{
+	struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
+
+	crypto_free_aead(tctx->u.aead);
+	crypto_free_cipher(tctx->essiv_cipher);
+	crypto_free_shash(tctx->hash);
+}
+
+static void essiv_skcipher_free_instance(struct skcipher_instance *inst)
+{
+	struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
+
+	crypto_drop_skcipher(&ictx->u.blockcipher_spawn);
+	crypto_drop_spawn(&ictx->essiv_cipher_spawn);
+	crypto_drop_shash(&ictx->hash_spawn);
+	kfree(inst);
+}
+
+static void essiv_aead_free_instance(struct aead_instance *inst)
+{
+	struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
+
+	crypto_drop_aead(&ictx->u.aead_spawn);
+	crypto_drop_spawn(&ictx->essiv_cipher_spawn);
+	crypto_drop_shash(&ictx->hash_spawn);
+	kfree(inst);
+}
+
+static bool essiv_supported_algorithms(struct crypto_alg *essiv_cipher_alg,
+				       struct shash_alg *hash_alg,
+				       int ivsize)
+{
+	if (hash_alg->digestsize < essiv_cipher_alg->cra_cipher.cia_min_keysize ||
+	    hash_alg->digestsize > essiv_cipher_alg->cra_cipher.cia_max_keysize)
+		return false;
+
+	if (ivsize != essiv_cipher_alg->cra_blocksize)
+		return false;
+
+	if (ivsize > MAX_INNER_IV_SIZE)
+		return false;
+
+	return true;
+}
+
+static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb)
+{
+	struct crypto_attr_type *algt;
+	const char *blockcipher_name;
+	const char *essiv_cipher_name;
+	const char *shash_name;
+	struct skcipher_instance *skcipher_inst = NULL;
+	struct aead_instance *aead_inst = NULL;
+	struct crypto_instance *inst;
+	struct crypto_alg *base, *block_base;
+	struct essiv_instance_ctx *ictx;
+	struct skcipher_alg *blockcipher_alg = NULL;
+	struct aead_alg *aead_alg = NULL;
+	struct crypto_alg *essiv_cipher_alg;
+	struct crypto_alg *_hash_alg;
+	struct shash_alg *hash_alg;
+	int ivsize;
+	u32 type;
+	int err;
+
+	algt = crypto_get_attr_type(tb);
+	if (IS_ERR(algt))
+		return PTR_ERR(algt);
+
+	blockcipher_name = crypto_attr_alg_name(tb[1]);
+	if (IS_ERR(blockcipher_name))
+		return PTR_ERR(blockcipher_name);
+
+	essiv_cipher_name = crypto_attr_alg_name(tb[2]);
+	if (IS_ERR(essiv_cipher_name))
+		return PTR_ERR(essiv_cipher_name);
+
+	shash_name = crypto_attr_alg_name(tb[3]);
+	if (IS_ERR(shash_name))
+		return PTR_ERR(shash_name);
+
+	type = algt->type & algt->mask;
+
+	switch (type) {
+	case CRYPTO_ALG_TYPE_BLKCIPHER:
+		skcipher_inst = kzalloc(sizeof(*skcipher_inst) +
+					sizeof(*ictx), GFP_KERNEL);
+		if (!skcipher_inst)
+			return -ENOMEM;
+		inst = skcipher_crypto_instance(skcipher_inst);
+		base = &skcipher_inst->alg.base;
+		ictx = crypto_instance_ctx(inst);
+
+		/* Block cipher, e.g. "cbc(aes)" */
+		crypto_set_skcipher_spawn(&ictx->u.blockcipher_spawn, inst);
+		err = crypto_grab_skcipher(&ictx->u.blockcipher_spawn,
+					   blockcipher_name, 0,
+					   crypto_requires_sync(algt->type,
+								algt->mask));
+		if (err)
+			goto out_free_inst;
+		blockcipher_alg = crypto_spawn_skcipher_alg(&ictx->u.blockcipher_spawn);
+		block_base = &blockcipher_alg->base;
+		ivsize = blockcipher_alg->ivsize;
+		break;
+
+	case CRYPTO_ALG_TYPE_AEAD:
+		aead_inst = kzalloc(sizeof(*aead_inst) +
+				    sizeof(*ictx), GFP_KERNEL);
+		if (!aead_inst)
+			return -ENOMEM;
+		inst = aead_crypto_instance(aead_inst);
+		base = &aead_inst->alg.base;
+		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,
+				       blockcipher_name, 0,
+				       crypto_requires_sync(algt->type,
+							    algt->mask));
+		if (err)
+			goto out_free_inst;
+		aead_alg = crypto_spawn_aead_alg(&ictx->u.aead_spawn);
+		block_base = &aead_alg->base;
+		ivsize = aead_alg->ivsize;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	/* Block cipher, e.g. "aes" */
+	crypto_set_spawn(&ictx->essiv_cipher_spawn, inst);
+	err = crypto_grab_spawn(&ictx->essiv_cipher_spawn, essiv_cipher_name,
+				CRYPTO_ALG_TYPE_CIPHER, CRYPTO_ALG_TYPE_MASK);
+	if (err)
+		goto out_drop_blockcipher;
+	essiv_cipher_alg = ictx->essiv_cipher_spawn.alg;
+
+	/* Synchronous hash, e.g., "sha256" */
+	_hash_alg = crypto_alg_mod_lookup(shash_name,
+					  CRYPTO_ALG_TYPE_SHASH,
+					  CRYPTO_ALG_TYPE_MASK);
+	if (IS_ERR(_hash_alg)) {
+		err = PTR_ERR(_hash_alg);
+		goto out_drop_essiv_cipher;
+	}
+	hash_alg = __crypto_shash_alg(_hash_alg);
+	err = crypto_init_shash_spawn(&ictx->hash_spawn, hash_alg, inst);
+	if (err)
+		goto out_put_hash;
+
+	/* Check the set of algorithms */
+	if (!essiv_supported_algorithms(essiv_cipher_alg, hash_alg, ivsize)) {
+		pr_warn("Unsupported essiv instantiation: (%s,%s,%s)\n",
+			block_base->cra_name,
+			essiv_cipher_alg->cra_name,
+			hash_alg->base.cra_name);
+		err = -EINVAL;
+		goto out_drop_hash;
+	}
+
+	/* Instance fields */
+
+	err = -ENAMETOOLONG;
+	if (snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME,
+		     "essiv(%s,%s,%s)", block_base->cra_name,
+		     essiv_cipher_alg->cra_name,
+		     hash_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
+		goto out_drop_hash;
+	if (snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME,
+		     "essiv(%s,%s,%s)",
+		     block_base->cra_driver_name,
+		     essiv_cipher_alg->cra_driver_name,
+		     hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
+		goto out_drop_hash;
+
+	base->cra_flags		= block_base->cra_flags & CRYPTO_ALG_ASYNC;
+	base->cra_blocksize	= block_base->cra_blocksize;
+	base->cra_ctxsize	= sizeof(struct essiv_tfm_ctx);
+	base->cra_alignmask	= block_base->cra_alignmask;
+	base->cra_priority	= block_base->cra_priority;
+
+	if (type == CRYPTO_ALG_TYPE_BLKCIPHER) {
+		skcipher_inst->alg.setkey	= essiv_skcipher_setkey;
+		skcipher_inst->alg.encrypt	= essiv_skcipher_encrypt;
+		skcipher_inst->alg.decrypt	= essiv_skcipher_decrypt;
+		skcipher_inst->alg.init		= essiv_skcipher_init_tfm;
+		skcipher_inst->alg.exit		= essiv_skcipher_exit_tfm;
+
+		skcipher_inst->alg.min_keysize	= crypto_skcipher_alg_min_keysize(blockcipher_alg);
+		skcipher_inst->alg.max_keysize	= crypto_skcipher_alg_max_keysize(blockcipher_alg);
+		skcipher_inst->alg.ivsize	= ESSIV_IV_SIZE;
+		skcipher_inst->alg.chunksize	= blockcipher_alg->chunksize;
+		skcipher_inst->alg.walksize	= blockcipher_alg->walksize;
+
+		skcipher_inst->free		= essiv_skcipher_free_instance;
+
+		err = skcipher_register_instance(tmpl, skcipher_inst);
+	} else {
+		aead_inst->alg.setkey		= essiv_aead_setkey;
+		aead_inst->alg.setauthsize	= essiv_aead_setauthsize;
+		aead_inst->alg.encrypt		= essiv_aead_encrypt;
+		aead_inst->alg.decrypt		= essiv_aead_decrypt;
+		aead_inst->alg.init		= essiv_aead_init_tfm;
+		aead_inst->alg.exit		= essiv_aead_exit_tfm;
+
+		aead_inst->alg.ivsize		= ESSIV_IV_SIZE;
+		aead_inst->alg.maxauthsize	= aead_alg->maxauthsize;
+		aead_inst->alg.chunksize	= aead_alg->chunksize;
+
+		aead_inst->free			= essiv_aead_free_instance;
+
+		err = aead_register_instance(tmpl, aead_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_essiv_cipher:
+	crypto_drop_spawn(&ictx->essiv_cipher_spawn);
+out_drop_blockcipher:
+	if (type == CRYPTO_ALG_TYPE_BLKCIPHER) {
+		crypto_drop_skcipher(&ictx->u.blockcipher_spawn);
+	} else {
+		crypto_drop_aead(&ictx->u.aead_spawn);
+	}
+out_free_inst:
+	kfree(skcipher_inst);
+	kfree(aead_inst);
+	return err;
+}
+
+/* essiv(blockcipher_name, essiv_cipher_name, shash_name) */
+static struct crypto_template essiv_tmpl = {
+	.name	= "essiv",
+	.create	= essiv_create,
+	.module	= THIS_MODULE,
+};
+
+static int __init essiv_module_init(void)
+{
+	return crypto_register_template(&essiv_tmpl);
+}
+
+static void __exit essiv_module_exit(void)
+{
+	crypto_unregister_template(&essiv_tmpl);
+}
+
+subsys_initcall(essiv_module_init);
+module_exit(essiv_module_exit);
+
+MODULE_DESCRIPTION("ESSIV skcipher/aead wrapper for block encryption");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS_CRYPTO("essiv");
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v2 1/4] crypto: essiv - create wrapper template for ESSIV generation
@ 2019-06-18 21:27   ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-18 21:27 UTC (permalink / raw)
  To: linux-crypto
  Cc: Herbert Xu, Eric Biggers, Ard Biesheuvel, linux-fscrypt,
	Gilad Ben-Yossef, dm-devel, Milan Broz

Implement a template that wraps a (skcipher,cipher,shash) or
(aead,cipher,shash) tuple so that we can consolidate the ESSIV handling
in fscrypt and dm-crypt and move it into the crypto API. This will result
in better test coverage, and will allow future changes to make the bare
cipher interface internal to the crypto subsystem, in order to increase
robustness of the API against misuse.

Note that especially the AEAD handling is a bit complex, and is tightly
coupled to the way dm-crypt combines AEAD based on the authenc() template
with the ESSIV handling.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 crypto/Kconfig  |   4 +
 crypto/Makefile |   1 +
 crypto/essiv.c  | 624 ++++++++++++++++++++
 3 files changed, 629 insertions(+)

diff --git a/crypto/Kconfig b/crypto/Kconfig
index 3d056e7da65f..1aa47087c1a2 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -1917,6 +1917,10 @@ config CRYPTO_STATS
 config CRYPTO_HASH_INFO
 	bool
 
+config CRYPTO_ESSIV
+	tristate
+	select CRYPTO_AUTHENC
+
 source "drivers/crypto/Kconfig"
 source "crypto/asymmetric_keys/Kconfig"
 source "certs/Kconfig"
diff --git a/crypto/Makefile b/crypto/Makefile
index 266a4cdbb9e2..ad1d99ba6d56 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -148,6 +148,7 @@ obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o
 obj-$(CONFIG_CRYPTO_ZSTD) += zstd.o
 obj-$(CONFIG_CRYPTO_OFB) += ofb.o
 obj-$(CONFIG_CRYPTO_ECC) += ecc.o
+obj-$(CONFIG_CRYPTO_ESSIV) += essiv.o
 
 ecdh_generic-y += ecdh.o
 ecdh_generic-y += ecdh_helper.o
diff --git a/crypto/essiv.c b/crypto/essiv.c
new file mode 100644
index 000000000000..029a65afb4d7
--- /dev/null
+++ b/crypto/essiv.c
@@ -0,0 +1,624 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ESSIV skcipher template for block encryption
+ *
+ * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
+ *
+ * Heavily based on:
+ * adiantum length-preserving encryption mode
+ *
+ * Copyright 2018 Google LLC
+ */
+
+#include <crypto/authenc.h>
+#include <crypto/internal/aead.h>
+#include <crypto/internal/hash.h>
+#include <crypto/internal/skcipher.h>
+#include <crypto/scatterwalk.h>
+#include <linux/module.h>
+
+#include "internal.h"
+
+#define ESSIV_IV_SIZE		sizeof(u64)	// IV size of the outer algo
+#define MAX_INNER_IV_SIZE	16		// max IV size of inner algo
+
+struct essiv_instance_ctx {
+	union {
+		struct crypto_skcipher_spawn	blockcipher_spawn;
+		struct crypto_aead_spawn	aead_spawn;
+	} u;
+	struct crypto_spawn			essiv_cipher_spawn;
+	struct crypto_shash_spawn		hash_spawn;
+};
+
+struct essiv_tfm_ctx {
+	union {
+		struct crypto_skcipher	*blockcipher;
+		struct crypto_aead	*aead;
+	} u;
+	struct crypto_cipher		*essiv_cipher;
+	struct crypto_shash		*hash;
+};
+
+struct essiv_skcipher_request_ctx {
+	u8				iv[MAX_INNER_IV_SIZE];
+	struct skcipher_request		blockcipher_req;
+};
+
+struct essiv_aead_request_ctx {
+	u8				iv[MAX_INNER_IV_SIZE];
+	struct scatterlist		src[4], dst[4];
+	struct aead_request		aead_req;
+};
+
+static int essiv_skcipher_setkey(struct crypto_skcipher *tfm,
+				 const u8 *key, unsigned int keylen)
+{
+	u32 flags = crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_REQ_MASK;
+	struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
+	SHASH_DESC_ON_STACK(desc, tctx->hash);
+	unsigned int saltsize;
+	u8 *salt;
+	int err;
+
+	crypto_skcipher_clear_flags(tctx->u.blockcipher, CRYPTO_TFM_REQ_MASK);
+	crypto_skcipher_set_flags(tctx->u.blockcipher, flags);
+	err = crypto_skcipher_setkey(tctx->u.blockcipher, key, keylen);
+	crypto_skcipher_set_flags(tfm,
+				  crypto_skcipher_get_flags(tctx->u.blockcipher) &
+				  CRYPTO_TFM_RES_MASK);
+	if (err)
+		return err;
+
+	saltsize = crypto_shash_digestsize(tctx->hash);
+	salt = kmalloc(saltsize, GFP_KERNEL);
+	if (!salt)
+		return -ENOMEM;
+
+	desc->tfm = tctx->hash;
+	crypto_shash_digest(desc, key, keylen, salt);
+
+	crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
+	crypto_cipher_set_flags(tctx->essiv_cipher, flags & CRYPTO_TFM_REQ_MASK);
+	err = crypto_cipher_setkey(tctx->essiv_cipher, salt, saltsize);
+	flags = crypto_cipher_get_flags(tctx->essiv_cipher) & CRYPTO_TFM_RES_MASK;
+	crypto_skcipher_set_flags(tfm, flags);
+
+	kzfree(salt);
+	return err;
+}
+
+static int essiv_aead_setkey(struct crypto_aead *tfm, const u8 *key,
+			     unsigned int keylen)
+{
+	struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
+	SHASH_DESC_ON_STACK(desc, tctx->hash);
+	struct crypto_authenc_keys keys;
+	unsigned int saltsize;
+	u8 *salt;
+	int err;
+
+	crypto_aead_clear_flags(tctx->u.aead, CRYPTO_TFM_REQ_MASK);
+	crypto_aead_set_flags(tctx->u.aead, crypto_aead_get_flags(tfm) &
+					    CRYPTO_TFM_REQ_MASK);
+	err = crypto_aead_setkey(tctx->u.aead, key, keylen);
+	crypto_aead_set_flags(tfm, crypto_aead_get_flags(tctx->u.aead) &
+				   CRYPTO_TFM_RES_MASK);
+	if (err)
+		return err;
+
+	if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
+		crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
+		return -EINVAL;
+	}
+
+	saltsize = crypto_shash_digestsize(tctx->hash);
+	salt = kmalloc(saltsize, GFP_KERNEL);
+	if (!salt)
+		return -ENOMEM;
+
+	desc->tfm = tctx->hash;
+	crypto_shash_init(desc);
+	crypto_shash_update(desc, keys.enckey, keys.enckeylen);
+	crypto_shash_finup(desc, keys.authkey, keys.authkeylen, salt);
+
+	crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
+	crypto_cipher_set_flags(tctx->essiv_cipher, crypto_aead_get_flags(tfm) &
+						    CRYPTO_TFM_REQ_MASK);
+	err = crypto_cipher_setkey(tctx->essiv_cipher, salt, saltsize);
+	crypto_aead_set_flags(tfm, crypto_cipher_get_flags(tctx->essiv_cipher) &
+				   CRYPTO_TFM_RES_MASK);
+
+	kzfree(salt);
+	return err;
+}
+
+static int essiv_aead_setauthsize(struct crypto_aead *tfm,
+				  unsigned int authsize)
+{
+	struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
+
+	return crypto_aead_setauthsize(tctx->u.aead, authsize);
+}
+
+static void essiv_skcipher_done(struct crypto_async_request *areq, int err)
+{
+	struct skcipher_request *req = areq->data;
+
+	skcipher_request_complete(req, err);
+}
+
+static void essiv_aead_done(struct crypto_async_request *areq, int err)
+{
+	struct aead_request *req = areq->data;
+
+	aead_request_complete(req, err);
+}
+
+static void essiv_skcipher_prepare_subreq(struct skcipher_request *req)
+{
+	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
+	const struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
+	struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
+	struct skcipher_request *subreq = &rctx->blockcipher_req;
+
+	memset(rctx->iv, 0, crypto_cipher_blocksize(tctx->essiv_cipher));
+	memcpy(rctx->iv, req->iv, crypto_skcipher_ivsize(tfm));
+
+	crypto_cipher_encrypt_one(tctx->essiv_cipher, rctx->iv, rctx->iv);
+
+	skcipher_request_set_tfm(subreq, tctx->u.blockcipher);
+	skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
+				   rctx->iv);
+	skcipher_request_set_callback(subreq, req->base.flags,
+				      essiv_skcipher_done, req);
+}
+
+static int essiv_aead_prepare_subreq(struct aead_request *req)
+{
+	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
+	const struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
+	struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
+	int ivsize = crypto_cipher_blocksize(tctx->essiv_cipher);
+	int ssize = req->assoclen - crypto_aead_ivsize(tfm);
+	struct aead_request *subreq = &rctx->aead_req;
+	struct scatterlist *sg;
+
+	/*
+	 * dm-crypt embeds the sector number and the IV in the AAD region so we
+	 * have to splice the converted IV into the subrequest that we pass on
+	 * to the AEAD transform. This means we are tightly coupled to dm-crypt,
+	 * but that should be the only user of this code in AEAD mode.
+	 */
+	if (ssize < 0 || sg_nents_for_len(req->src, ssize) != 1)
+		return -EINVAL;
+
+	memset(rctx->iv, 0, ivsize);
+	memcpy(rctx->iv, req->iv, crypto_aead_ivsize(tfm));
+
+	crypto_cipher_encrypt_one(tctx->essiv_cipher, rctx->iv, rctx->iv);
+
+	sg_init_table(rctx->src, 4);
+	sg_set_page(rctx->src, sg_page(req->src), ssize, req->src->offset);
+	sg_set_buf(rctx->src + 1, rctx->iv, ivsize);
+	sg = scatterwalk_ffwd(rctx->src + 2, req->src, req->assoclen);
+	if (sg != rctx->src + 2)
+		sg_chain(rctx->src, 3, sg);
+
+	sg_init_table(rctx->dst, 4);
+	sg_set_page(rctx->dst, sg_page(req->dst), ssize, req->dst->offset);
+	sg_set_buf(rctx->dst + 1, rctx->iv, ivsize);
+	sg = scatterwalk_ffwd(rctx->dst + 2, req->dst, req->assoclen);
+	if (sg != rctx->dst + 2)
+		sg_chain(rctx->dst, 3, sg);
+
+	aead_request_set_tfm(subreq, tctx->u.aead);
+	aead_request_set_crypt(subreq, rctx->src, rctx->dst, req->cryptlen,
+			       rctx->iv);
+	aead_request_set_ad(subreq, ssize + ivsize);
+	aead_request_set_callback(subreq, req->base.flags, essiv_aead_done, req);
+
+	return 0;
+}
+
+static int essiv_skcipher_encrypt(struct skcipher_request *req)
+{
+	struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
+
+	essiv_skcipher_prepare_subreq(req);
+	return crypto_skcipher_encrypt(&rctx->blockcipher_req);
+}
+
+static int essiv_aead_encrypt(struct aead_request *req)
+{
+	struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
+	int err;
+
+	err = essiv_aead_prepare_subreq(req);
+	if (err)
+		return err;
+	return crypto_aead_encrypt(&rctx->aead_req);
+}
+
+static int essiv_skcipher_decrypt(struct skcipher_request *req)
+{
+	struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
+	return crypto_skcipher_decrypt(&rctx->blockcipher_req);
+}
+
+static int essiv_aead_decrypt(struct aead_request *req)
+{
+	struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
+	int err;
+
+	err = essiv_aead_prepare_subreq(req);
+	if (err)
+		return err;
+
+	essiv_aead_prepare_subreq(req);
+	return crypto_aead_decrypt(&rctx->aead_req);
+}
+
+static int essiv_init_tfm(struct essiv_instance_ctx *ictx,
+			  struct essiv_tfm_ctx *tctx)
+{
+	struct crypto_cipher *essiv_cipher;
+	struct crypto_shash *hash;
+	int err;
+
+	essiv_cipher = crypto_spawn_cipher(&ictx->essiv_cipher_spawn);
+	if (IS_ERR(essiv_cipher))
+		return PTR_ERR(essiv_cipher);
+
+	hash = crypto_spawn_shash(&ictx->hash_spawn);
+	if (IS_ERR(hash)) {
+		err = PTR_ERR(hash);
+		goto err_free_essiv_cipher;
+	}
+
+	tctx->essiv_cipher = essiv_cipher;
+	tctx->hash = hash;
+
+	return 0;
+
+err_free_essiv_cipher:
+	crypto_free_cipher(essiv_cipher);
+	return err;
+}
+
+static int essiv_skcipher_init_tfm(struct crypto_skcipher *tfm)
+{
+	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
+	struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
+	struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
+	struct crypto_skcipher *blockcipher;
+	unsigned int subreq_size;
+	int err;
+
+	BUILD_BUG_ON(offsetofend(struct essiv_skcipher_request_ctx,
+				 blockcipher_req) !=
+		     sizeof(struct essiv_skcipher_request_ctx));
+
+	blockcipher = crypto_spawn_skcipher(&ictx->u.blockcipher_spawn);
+	if (IS_ERR(blockcipher))
+		return PTR_ERR(blockcipher);
+
+	subreq_size = FIELD_SIZEOF(struct essiv_skcipher_request_ctx,
+				   blockcipher_req) +
+		      crypto_skcipher_reqsize(blockcipher);
+
+	crypto_skcipher_set_reqsize(tfm, offsetof(struct essiv_skcipher_request_ctx,
+						  blockcipher_req) + subreq_size);
+
+	err = essiv_init_tfm(ictx, tctx);
+	if (err)
+		crypto_free_skcipher(blockcipher);
+
+	tctx->u.blockcipher = blockcipher;
+	return err;
+}
+
+static int essiv_aead_init_tfm(struct crypto_aead *tfm)
+{
+	struct aead_instance *inst = aead_alg_instance(tfm);
+	struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
+	struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
+	struct crypto_aead *aead;
+	unsigned int subreq_size;
+	int err;
+
+	BUILD_BUG_ON(offsetofend(struct essiv_aead_request_ctx, aead_req) !=
+		     sizeof(struct essiv_aead_request_ctx));
+
+	aead = crypto_spawn_aead(&ictx->u.aead_spawn);
+	if (IS_ERR(aead))
+		return PTR_ERR(aead);
+
+	subreq_size = FIELD_SIZEOF(struct essiv_aead_request_ctx, aead_req) +
+		      crypto_aead_reqsize(aead);
+
+	crypto_aead_set_reqsize(tfm, offsetof(struct essiv_aead_request_ctx,
+					      aead_req) + subreq_size);
+
+	err = essiv_init_tfm(ictx, tctx);
+	if (err)
+		crypto_free_aead(aead);
+
+	tctx->u.aead = aead;
+	return err;
+}
+
+static void essiv_skcipher_exit_tfm(struct crypto_skcipher *tfm)
+{
+	struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
+
+	crypto_free_skcipher(tctx->u.blockcipher);
+	crypto_free_cipher(tctx->essiv_cipher);
+	crypto_free_shash(tctx->hash);
+}
+
+static void essiv_aead_exit_tfm(struct crypto_aead *tfm)
+{
+	struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
+
+	crypto_free_aead(tctx->u.aead);
+	crypto_free_cipher(tctx->essiv_cipher);
+	crypto_free_shash(tctx->hash);
+}
+
+static void essiv_skcipher_free_instance(struct skcipher_instance *inst)
+{
+	struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
+
+	crypto_drop_skcipher(&ictx->u.blockcipher_spawn);
+	crypto_drop_spawn(&ictx->essiv_cipher_spawn);
+	crypto_drop_shash(&ictx->hash_spawn);
+	kfree(inst);
+}
+
+static void essiv_aead_free_instance(struct aead_instance *inst)
+{
+	struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
+
+	crypto_drop_aead(&ictx->u.aead_spawn);
+	crypto_drop_spawn(&ictx->essiv_cipher_spawn);
+	crypto_drop_shash(&ictx->hash_spawn);
+	kfree(inst);
+}
+
+static bool essiv_supported_algorithms(struct crypto_alg *essiv_cipher_alg,
+				       struct shash_alg *hash_alg,
+				       int ivsize)
+{
+	if (hash_alg->digestsize < essiv_cipher_alg->cra_cipher.cia_min_keysize ||
+	    hash_alg->digestsize > essiv_cipher_alg->cra_cipher.cia_max_keysize)
+		return false;
+
+	if (ivsize != essiv_cipher_alg->cra_blocksize)
+		return false;
+
+	if (ivsize > MAX_INNER_IV_SIZE)
+		return false;
+
+	return true;
+}
+
+static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb)
+{
+	struct crypto_attr_type *algt;
+	const char *blockcipher_name;
+	const char *essiv_cipher_name;
+	const char *shash_name;
+	struct skcipher_instance *skcipher_inst = NULL;
+	struct aead_instance *aead_inst = NULL;
+	struct crypto_instance *inst;
+	struct crypto_alg *base, *block_base;
+	struct essiv_instance_ctx *ictx;
+	struct skcipher_alg *blockcipher_alg = NULL;
+	struct aead_alg *aead_alg = NULL;
+	struct crypto_alg *essiv_cipher_alg;
+	struct crypto_alg *_hash_alg;
+	struct shash_alg *hash_alg;
+	int ivsize;
+	u32 type;
+	int err;
+
+	algt = crypto_get_attr_type(tb);
+	if (IS_ERR(algt))
+		return PTR_ERR(algt);
+
+	blockcipher_name = crypto_attr_alg_name(tb[1]);
+	if (IS_ERR(blockcipher_name))
+		return PTR_ERR(blockcipher_name);
+
+	essiv_cipher_name = crypto_attr_alg_name(tb[2]);
+	if (IS_ERR(essiv_cipher_name))
+		return PTR_ERR(essiv_cipher_name);
+
+	shash_name = crypto_attr_alg_name(tb[3]);
+	if (IS_ERR(shash_name))
+		return PTR_ERR(shash_name);
+
+	type = algt->type & algt->mask;
+
+	switch (type) {
+	case CRYPTO_ALG_TYPE_BLKCIPHER:
+		skcipher_inst = kzalloc(sizeof(*skcipher_inst) +
+					sizeof(*ictx), GFP_KERNEL);
+		if (!skcipher_inst)
+			return -ENOMEM;
+		inst = skcipher_crypto_instance(skcipher_inst);
+		base = &skcipher_inst->alg.base;
+		ictx = crypto_instance_ctx(inst);
+
+		/* Block cipher, e.g. "cbc(aes)" */
+		crypto_set_skcipher_spawn(&ictx->u.blockcipher_spawn, inst);
+		err = crypto_grab_skcipher(&ictx->u.blockcipher_spawn,
+					   blockcipher_name, 0,
+					   crypto_requires_sync(algt->type,
+								algt->mask));
+		if (err)
+			goto out_free_inst;
+		blockcipher_alg = crypto_spawn_skcipher_alg(&ictx->u.blockcipher_spawn);
+		block_base = &blockcipher_alg->base;
+		ivsize = blockcipher_alg->ivsize;
+		break;
+
+	case CRYPTO_ALG_TYPE_AEAD:
+		aead_inst = kzalloc(sizeof(*aead_inst) +
+				    sizeof(*ictx), GFP_KERNEL);
+		if (!aead_inst)
+			return -ENOMEM;
+		inst = aead_crypto_instance(aead_inst);
+		base = &aead_inst->alg.base;
+		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,
+				       blockcipher_name, 0,
+				       crypto_requires_sync(algt->type,
+							    algt->mask));
+		if (err)
+			goto out_free_inst;
+		aead_alg = crypto_spawn_aead_alg(&ictx->u.aead_spawn);
+		block_base = &aead_alg->base;
+		ivsize = aead_alg->ivsize;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	/* Block cipher, e.g. "aes" */
+	crypto_set_spawn(&ictx->essiv_cipher_spawn, inst);
+	err = crypto_grab_spawn(&ictx->essiv_cipher_spawn, essiv_cipher_name,
+				CRYPTO_ALG_TYPE_CIPHER, CRYPTO_ALG_TYPE_MASK);
+	if (err)
+		goto out_drop_blockcipher;
+	essiv_cipher_alg = ictx->essiv_cipher_spawn.alg;
+
+	/* Synchronous hash, e.g., "sha256" */
+	_hash_alg = crypto_alg_mod_lookup(shash_name,
+					  CRYPTO_ALG_TYPE_SHASH,
+					  CRYPTO_ALG_TYPE_MASK);
+	if (IS_ERR(_hash_alg)) {
+		err = PTR_ERR(_hash_alg);
+		goto out_drop_essiv_cipher;
+	}
+	hash_alg = __crypto_shash_alg(_hash_alg);
+	err = crypto_init_shash_spawn(&ictx->hash_spawn, hash_alg, inst);
+	if (err)
+		goto out_put_hash;
+
+	/* Check the set of algorithms */
+	if (!essiv_supported_algorithms(essiv_cipher_alg, hash_alg, ivsize)) {
+		pr_warn("Unsupported essiv instantiation: (%s,%s,%s)\n",
+			block_base->cra_name,
+			essiv_cipher_alg->cra_name,
+			hash_alg->base.cra_name);
+		err = -EINVAL;
+		goto out_drop_hash;
+	}
+
+	/* Instance fields */
+
+	err = -ENAMETOOLONG;
+	if (snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME,
+		     "essiv(%s,%s,%s)", block_base->cra_name,
+		     essiv_cipher_alg->cra_name,
+		     hash_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
+		goto out_drop_hash;
+	if (snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME,
+		     "essiv(%s,%s,%s)",
+		     block_base->cra_driver_name,
+		     essiv_cipher_alg->cra_driver_name,
+		     hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
+		goto out_drop_hash;
+
+	base->cra_flags		= block_base->cra_flags & CRYPTO_ALG_ASYNC;
+	base->cra_blocksize	= block_base->cra_blocksize;
+	base->cra_ctxsize	= sizeof(struct essiv_tfm_ctx);
+	base->cra_alignmask	= block_base->cra_alignmask;
+	base->cra_priority	= block_base->cra_priority;
+
+	if (type == CRYPTO_ALG_TYPE_BLKCIPHER) {
+		skcipher_inst->alg.setkey	= essiv_skcipher_setkey;
+		skcipher_inst->alg.encrypt	= essiv_skcipher_encrypt;
+		skcipher_inst->alg.decrypt	= essiv_skcipher_decrypt;
+		skcipher_inst->alg.init		= essiv_skcipher_init_tfm;
+		skcipher_inst->alg.exit		= essiv_skcipher_exit_tfm;
+
+		skcipher_inst->alg.min_keysize	= crypto_skcipher_alg_min_keysize(blockcipher_alg);
+		skcipher_inst->alg.max_keysize	= crypto_skcipher_alg_max_keysize(blockcipher_alg);
+		skcipher_inst->alg.ivsize	= ESSIV_IV_SIZE;
+		skcipher_inst->alg.chunksize	= blockcipher_alg->chunksize;
+		skcipher_inst->alg.walksize	= blockcipher_alg->walksize;
+
+		skcipher_inst->free		= essiv_skcipher_free_instance;
+
+		err = skcipher_register_instance(tmpl, skcipher_inst);
+	} else {
+		aead_inst->alg.setkey		= essiv_aead_setkey;
+		aead_inst->alg.setauthsize	= essiv_aead_setauthsize;
+		aead_inst->alg.encrypt		= essiv_aead_encrypt;
+		aead_inst->alg.decrypt		= essiv_aead_decrypt;
+		aead_inst->alg.init		= essiv_aead_init_tfm;
+		aead_inst->alg.exit		= essiv_aead_exit_tfm;
+
+		aead_inst->alg.ivsize		= ESSIV_IV_SIZE;
+		aead_inst->alg.maxauthsize	= aead_alg->maxauthsize;
+		aead_inst->alg.chunksize	= aead_alg->chunksize;
+
+		aead_inst->free			= essiv_aead_free_instance;
+
+		err = aead_register_instance(tmpl, aead_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_essiv_cipher:
+	crypto_drop_spawn(&ictx->essiv_cipher_spawn);
+out_drop_blockcipher:
+	if (type == CRYPTO_ALG_TYPE_BLKCIPHER) {
+		crypto_drop_skcipher(&ictx->u.blockcipher_spawn);
+	} else {
+		crypto_drop_aead(&ictx->u.aead_spawn);
+	}
+out_free_inst:
+	kfree(skcipher_inst);
+	kfree(aead_inst);
+	return err;
+}
+
+/* essiv(blockcipher_name, essiv_cipher_name, shash_name) */
+static struct crypto_template essiv_tmpl = {
+	.name	= "essiv",
+	.create	= essiv_create,
+	.module	= THIS_MODULE,
+};
+
+static int __init essiv_module_init(void)
+{
+	return crypto_register_template(&essiv_tmpl);
+}
+
+static void __exit essiv_module_exit(void)
+{
+	crypto_unregister_template(&essiv_tmpl);
+}
+
+subsys_initcall(essiv_module_init);
+module_exit(essiv_module_exit);
+
+MODULE_DESCRIPTION("ESSIV skcipher/aead wrapper for block encryption");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS_CRYPTO("essiv");
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v2 2/4] fs: crypto: invoke crypto API for ESSIV handling
  2019-06-18 21:27 ` Ard Biesheuvel
@ 2019-06-18 21:27   ` Ard Biesheuvel
  -1 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-18 21:27 UTC (permalink / raw)
  To: linux-crypto
  Cc: Ard Biesheuvel, Herbert Xu, Eric Biggers, dm-devel,
	linux-fscrypt, Gilad Ben-Yossef, Milan Broz

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 <ard.biesheuvel@linaro.org>
---
 fs/crypto/Kconfig           |  1 +
 fs/crypto/crypto.c          |  5 --
 fs/crypto/fscrypt_private.h |  9 --
 fs/crypto/keyinfo.c         | 88 +-------------------
 4 files changed, 3 insertions(+), 100 deletions(-)

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..82c7eb86ca00 100644
--- a/fs/crypto/keyinfo.c
+++ b/fs/crypto/keyinfo.c
@@ -19,8 +19,6 @@
 #include <crypto/skcipher.h>
 #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 +142,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 +374,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 +385,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 +400,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;
 }
 
@@ -495,7 +412,6 @@ static void put_crypt_info(struct fscrypt_info *ci)
 		put_master_key(ci->ci_master_key);
 	} else {
 		crypto_free_skcipher(ci->ci_ctfm);
-		crypto_free_cipher(ci->ci_essiv_tfm);
 	}
 	kmem_cache_free(fscrypt_info_cachep, ci);
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v2 2/4] fs: crypto: invoke crypto API for ESSIV handling
@ 2019-06-18 21:27   ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-18 21:27 UTC (permalink / raw)
  To: linux-crypto
  Cc: Herbert Xu, Eric Biggers, Ard Biesheuvel, linux-fscrypt,
	Gilad Ben-Yossef, dm-devel, Milan Broz

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 <ard.biesheuvel@linaro.org>
---
 fs/crypto/Kconfig           |  1 +
 fs/crypto/crypto.c          |  5 --
 fs/crypto/fscrypt_private.h |  9 --
 fs/crypto/keyinfo.c         | 88 +-------------------
 4 files changed, 3 insertions(+), 100 deletions(-)

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..82c7eb86ca00 100644
--- a/fs/crypto/keyinfo.c
+++ b/fs/crypto/keyinfo.c
@@ -19,8 +19,6 @@
 #include <crypto/skcipher.h>
 #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 +142,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 +374,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 +385,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 +400,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;
 }
 
@@ -495,7 +412,6 @@ static void put_crypt_info(struct fscrypt_info *ci)
 		put_master_key(ci->ci_master_key);
 	} else {
 		crypto_free_skcipher(ci->ci_ctfm);
-		crypto_free_cipher(ci->ci_essiv_tfm);
 	}
 	kmem_cache_free(fscrypt_info_cachep, ci);
 }
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v2 3/4] md: dm-crypt: infer ESSIV block cipher from cipher string directly
  2019-06-18 21:27 ` Ard Biesheuvel
@ 2019-06-18 21:27   ` Ard Biesheuvel
  -1 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-18 21:27 UTC (permalink / raw)
  To: linux-crypto
  Cc: Ard Biesheuvel, Herbert Xu, Eric Biggers, dm-devel,
	linux-fscrypt, Gilad Ben-Yossef, Milan Broz

Instead of allocating a crypto skcipher tfm 'foo' and attempting to
infer the encapsulated block cipher from the driver's 'name' field,
directly parse the string that we used to allocated the tfm. These
are always identical (unless the allocation failed, in which case
we bail anyway), but using the string allows us to use it in the
allocation, which is something we will need when switching to the
'essiv' crypto API template.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 drivers/md/dm-crypt.c | 35 +++++++++-----------
 1 file changed, 15 insertions(+), 20 deletions(-)

diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index 1b16d34bb785..f001f1104cb5 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -2321,25 +2321,17 @@ static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
  * The cc->cipher is currently used only in ESSIV.
  * This should be probably done by crypto-api calls (once available...)
  */
-static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
+static int crypt_ctr_blkdev_cipher(struct crypt_config *cc, char *alg_name)
 {
-	const char *alg_name = NULL;
 	char *start, *end;
 
 	if (crypt_integrity_aead(cc)) {
-		alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
-		if (!alg_name)
-			return -EINVAL;
 		if (crypt_integrity_hmac(cc)) {
 			alg_name = strchr(alg_name, ',');
 			if (!alg_name)
 				return -EINVAL;
 		}
 		alg_name++;
-	} else {
-		alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
-		if (!alg_name)
-			return -EINVAL;
 	}
 
 	start = strchr(alg_name, '(');
@@ -2434,6 +2426,20 @@ static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key
 	if (*ivmode && !strcmp(*ivmode, "lmk"))
 		cc->tfms_count = 64;
 
+	if (crypt_integrity_aead(cc)) {
+		ret = crypt_ctr_auth_cipher(cc, cipher_api);
+		if (ret < 0) {
+			ti->error = "Invalid AEAD cipher spec";
+			return -ENOMEM;
+	       }
+	}
+
+	ret = crypt_ctr_blkdev_cipher(cc, cipher_api);
+	if (ret < 0) {
+		ti->error = "Cannot allocate cipher string";
+		return -ENOMEM;
+	}
+
 	cc->key_parts = cc->tfms_count;
 
 	/* Allocate cipher */
@@ -2445,21 +2451,10 @@ static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key
 
 	/* Alloc AEAD, can be used only in new format. */
 	if (crypt_integrity_aead(cc)) {
-		ret = crypt_ctr_auth_cipher(cc, cipher_api);
-		if (ret < 0) {
-			ti->error = "Invalid AEAD cipher spec";
-			return -ENOMEM;
-		}
 		cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
 	} else
 		cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
 
-	ret = crypt_ctr_blkdev_cipher(cc);
-	if (ret < 0) {
-		ti->error = "Cannot allocate cipher string";
-		return -ENOMEM;
-	}
-
 	return 0;
 }
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v2 3/4] md: dm-crypt: infer ESSIV block cipher from cipher string directly
@ 2019-06-18 21:27   ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-18 21:27 UTC (permalink / raw)
  To: linux-crypto
  Cc: Herbert Xu, Eric Biggers, Ard Biesheuvel, linux-fscrypt,
	Gilad Ben-Yossef, dm-devel, Milan Broz

Instead of allocating a crypto skcipher tfm 'foo' and attempting to
infer the encapsulated block cipher from the driver's 'name' field,
directly parse the string that we used to allocated the tfm. These
are always identical (unless the allocation failed, in which case
we bail anyway), but using the string allows us to use it in the
allocation, which is something we will need when switching to the
'essiv' crypto API template.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 drivers/md/dm-crypt.c | 35 +++++++++-----------
 1 file changed, 15 insertions(+), 20 deletions(-)

diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index 1b16d34bb785..f001f1104cb5 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -2321,25 +2321,17 @@ static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
  * The cc->cipher is currently used only in ESSIV.
  * This should be probably done by crypto-api calls (once available...)
  */
-static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
+static int crypt_ctr_blkdev_cipher(struct crypt_config *cc, char *alg_name)
 {
-	const char *alg_name = NULL;
 	char *start, *end;
 
 	if (crypt_integrity_aead(cc)) {
-		alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
-		if (!alg_name)
-			return -EINVAL;
 		if (crypt_integrity_hmac(cc)) {
 			alg_name = strchr(alg_name, ',');
 			if (!alg_name)
 				return -EINVAL;
 		}
 		alg_name++;
-	} else {
-		alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
-		if (!alg_name)
-			return -EINVAL;
 	}
 
 	start = strchr(alg_name, '(');
@@ -2434,6 +2426,20 @@ static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key
 	if (*ivmode && !strcmp(*ivmode, "lmk"))
 		cc->tfms_count = 64;
 
+	if (crypt_integrity_aead(cc)) {
+		ret = crypt_ctr_auth_cipher(cc, cipher_api);
+		if (ret < 0) {
+			ti->error = "Invalid AEAD cipher spec";
+			return -ENOMEM;
+	       }
+	}
+
+	ret = crypt_ctr_blkdev_cipher(cc, cipher_api);
+	if (ret < 0) {
+		ti->error = "Cannot allocate cipher string";
+		return -ENOMEM;
+	}
+
 	cc->key_parts = cc->tfms_count;
 
 	/* Allocate cipher */
@@ -2445,21 +2451,10 @@ static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key
 
 	/* Alloc AEAD, can be used only in new format. */
 	if (crypt_integrity_aead(cc)) {
-		ret = crypt_ctr_auth_cipher(cc, cipher_api);
-		if (ret < 0) {
-			ti->error = "Invalid AEAD cipher spec";
-			return -ENOMEM;
-		}
 		cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
 	} else
 		cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
 
-	ret = crypt_ctr_blkdev_cipher(cc);
-	if (ret < 0) {
-		ti->error = "Cannot allocate cipher string";
-		return -ENOMEM;
-	}
-
 	return 0;
 }
 
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v2 4/4] md: dm-crypt: switch to ESSIV crypto API template
  2019-06-18 21:27 ` Ard Biesheuvel
@ 2019-06-18 21:27   ` Ard Biesheuvel
  -1 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-18 21:27 UTC (permalink / raw)
  To: linux-crypto
  Cc: Ard Biesheuvel, Herbert Xu, Eric Biggers, dm-devel,
	linux-fscrypt, Gilad Ben-Yossef, Milan Broz

Replace the explicit ESSIV handling in the dm-crypt driver with calls
into the crypto API, which now possesses the capability to perform
this processing within the crypto subsystem.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 drivers/md/Kconfig    |   1 +
 drivers/md/dm-crypt.c | 208 +++-----------------
 2 files changed, 31 insertions(+), 178 deletions(-)

diff --git a/drivers/md/Kconfig b/drivers/md/Kconfig
index 45254b3ef715..30ca87cf25db 100644
--- a/drivers/md/Kconfig
+++ b/drivers/md/Kconfig
@@ -271,6 +271,7 @@ config DM_CRYPT
 	depends on BLK_DEV_DM
 	select CRYPTO
 	select CRYPTO_CBC
+	select CRYPTO_ESSIV
 	---help---
 	  This device-mapper target allows you to create a device that
 	  transparently encrypts the data on it. You'll need to activate
diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index f001f1104cb5..89efd7d249fd 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -98,11 +98,6 @@ struct crypt_iv_operations {
 		    struct dm_crypt_request *dmreq);
 };
 
-struct iv_essiv_private {
-	struct crypto_shash *hash_tfm;
-	u8 *salt;
-};
-
 struct iv_benbi_private {
 	int shift;
 };
@@ -155,7 +150,6 @@ struct crypt_config {
 
 	const struct crypt_iv_operations *iv_gen_ops;
 	union {
-		struct iv_essiv_private essiv;
 		struct iv_benbi_private benbi;
 		struct iv_lmk_private lmk;
 		struct iv_tcw_private tcw;
@@ -165,8 +159,6 @@ struct crypt_config {
 	unsigned short int sector_size;
 	unsigned char sector_shift;
 
-	/* ESSIV: struct crypto_cipher *essiv_tfm */
-	void *iv_private;
 	union {
 		struct crypto_skcipher **tfms;
 		struct crypto_aead **tfms_aead;
@@ -323,161 +315,6 @@ static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv,
 	return 0;
 }
 
-/* Initialise ESSIV - compute salt but no local memory allocations */
-static int crypt_iv_essiv_init(struct crypt_config *cc)
-{
-	struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
-	SHASH_DESC_ON_STACK(desc, essiv->hash_tfm);
-	struct crypto_cipher *essiv_tfm;
-	int err;
-
-	desc->tfm = essiv->hash_tfm;
-
-	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_shash_digestsize(essiv->hash_tfm));
-	if (err)
-		return err;
-
-	return 0;
-}
-
-/* Wipe salt and reset key derived from volume key */
-static int crypt_iv_essiv_wipe(struct crypt_config *cc)
-{
-	struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
-	unsigned salt_size = crypto_shash_digestsize(essiv->hash_tfm);
-	struct crypto_cipher *essiv_tfm;
-	int r, err = 0;
-
-	memset(essiv->salt, 0, salt_size);
-
-	essiv_tfm = cc->iv_private;
-	r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
-	if (r)
-		err = r;
-
-	return err;
-}
-
-/* Allocate the cipher for ESSIV */
-static struct crypto_cipher *alloc_essiv_cipher(struct crypt_config *cc,
-						struct dm_target *ti,
-						const u8 *salt,
-						unsigned int saltsize)
-{
-	struct crypto_cipher *essiv_tfm;
-	int err;
-
-	/* Setup the essiv_tfm with the given salt */
-	essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, 0);
-	if (IS_ERR(essiv_tfm)) {
-		ti->error = "Error allocating crypto tfm for ESSIV";
-		return essiv_tfm;
-	}
-
-	if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
-		ti->error = "Block size of ESSIV cipher does "
-			    "not match IV size of block cipher";
-		crypto_free_cipher(essiv_tfm);
-		return ERR_PTR(-EINVAL);
-	}
-
-	err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
-	if (err) {
-		ti->error = "Failed to set key for ESSIV cipher";
-		crypto_free_cipher(essiv_tfm);
-		return ERR_PTR(err);
-	}
-
-	return essiv_tfm;
-}
-
-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_shash(essiv->hash_tfm);
-	essiv->hash_tfm = NULL;
-
-	kzfree(essiv->salt);
-	essiv->salt = NULL;
-
-	essiv_tfm = cc->iv_private;
-
-	if (essiv_tfm)
-		crypto_free_cipher(essiv_tfm);
-
-	cc->iv_private = NULL;
-}
-
-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_shash *hash_tfm = NULL;
-	u8 *salt = NULL;
-	int err;
-
-	if (!opts) {
-		ti->error = "Digest algorithm missing for ESSIV mode";
-		return -EINVAL;
-	}
-
-	/* Allocate hash algorithm */
-	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_shash_digestsize(hash_tfm), GFP_KERNEL);
-	if (!salt) {
-		ti->error = "Error kmallocing salt storage in ESSIV";
-		err = -ENOMEM;
-		goto bad;
-	}
-
-	cc->iv_gen_private.essiv.salt = salt;
-	cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
-
-	essiv_tfm = alloc_essiv_cipher(cc, ti, salt,
-				       crypto_shash_digestsize(hash_tfm));
-	if (IS_ERR(essiv_tfm)) {
-		crypt_iv_essiv_dtr(cc);
-		return PTR_ERR(essiv_tfm);
-	}
-	cc->iv_private = essiv_tfm;
-
-	return 0;
-
-bad:
-	if (hash_tfm && !IS_ERR(hash_tfm))
-		crypto_free_shash(hash_tfm);
-	kfree(salt);
-	return err;
-}
-
-static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
-			      struct dm_crypt_request *dmreq)
-{
-	struct crypto_cipher *essiv_tfm = cc->iv_private;
-
-	memset(iv, 0, cc->iv_size);
-	*(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
-	crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
-
-	return 0;
-}
-
 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
 			      const char *opts)
 {
@@ -853,14 +690,6 @@ static const struct crypt_iv_operations crypt_iv_plain64be_ops = {
 	.generator = crypt_iv_plain64be_gen
 };
 
-static const struct crypt_iv_operations crypt_iv_essiv_ops = {
-	.ctr       = crypt_iv_essiv_ctr,
-	.dtr       = crypt_iv_essiv_dtr,
-	.init      = crypt_iv_essiv_init,
-	.wipe      = crypt_iv_essiv_wipe,
-	.generator = crypt_iv_essiv_gen
-};
-
 static const struct crypt_iv_operations crypt_iv_benbi_ops = {
 	.ctr	   = crypt_iv_benbi_ctr,
 	.dtr	   = crypt_iv_benbi_dtr,
@@ -2283,7 +2112,7 @@ static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
 	else if (strcmp(ivmode, "plain64be") == 0)
 		cc->iv_gen_ops = &crypt_iv_plain64be_ops;
 	else if (strcmp(ivmode, "essiv") == 0)
-		cc->iv_gen_ops = &crypt_iv_essiv_ops;
+		cc->iv_gen_ops = &crypt_iv_plain64_ops;
 	else if (strcmp(ivmode, "benbi") == 0)
 		cc->iv_gen_ops = &crypt_iv_benbi_ops;
 	else if (strcmp(ivmode, "null") == 0)
@@ -2397,7 +2226,7 @@ static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key
 				char **ivmode, char **ivopts)
 {
 	struct crypt_config *cc = ti->private;
-	char *tmp, *cipher_api;
+	char *tmp, *cipher_api, buf[CRYPTO_MAX_ALG_NAME];
 	int ret = -EINVAL;
 
 	cc->tfms_count = 1;
@@ -2435,9 +2264,19 @@ static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key
 	}
 
 	ret = crypt_ctr_blkdev_cipher(cc, cipher_api);
-	if (ret < 0) {
-		ti->error = "Cannot allocate cipher string";
-		return -ENOMEM;
+	if (ret < 0)
+		goto bad_mem;
+
+	if (*ivmode && !strcmp(*ivmode, "essiv")) {
+		if (!*ivopts) {
+			ti->error = "Digest algorithm missing for ESSIV mode";
+			return -EINVAL;
+		}
+		ret = snprintf(buf, CRYPTO_MAX_ALG_NAME, "essiv(%s,%s,%s)",
+			       cipher_api, cc->cipher, *ivopts);
+		if (ret < 0)
+			goto bad_mem;
+		cipher_api = buf;
 	}
 
 	cc->key_parts = cc->tfms_count;
@@ -2456,6 +2295,9 @@ static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key
 		cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
 
 	return 0;
+bad_mem:
+	ti->error = "Cannot allocate cipher string";
+	return -ENOMEM;
 }
 
 static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
@@ -2515,8 +2357,18 @@ static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key
 	if (!cipher_api)
 		goto bad_mem;
 
-	ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
-		       "%s(%s)", chainmode, cipher);
+	if (!strcmp(*ivmode, "essiv")) {
+		if (!*ivopts) {
+			ti->error = "Digest algorithm missing for ESSIV mode";
+			return -EINVAL;
+		}
+		ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
+			       "essiv(%s(%s),%s,%s)", chainmode, cipher,
+			       cipher, *ivopts);
+	} else {
+		ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
+			       "%s(%s)", chainmode, cipher);
+	}
 	if (ret < 0) {
 		kfree(cipher_api);
 		goto bad_mem;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v2 4/4] md: dm-crypt: switch to ESSIV crypto API template
@ 2019-06-18 21:27   ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-18 21:27 UTC (permalink / raw)
  To: linux-crypto
  Cc: Herbert Xu, Eric Biggers, Ard Biesheuvel, linux-fscrypt,
	Gilad Ben-Yossef, dm-devel, Milan Broz

Replace the explicit ESSIV handling in the dm-crypt driver with calls
into the crypto API, which now possesses the capability to perform
this processing within the crypto subsystem.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 drivers/md/Kconfig    |   1 +
 drivers/md/dm-crypt.c | 208 +++-----------------
 2 files changed, 31 insertions(+), 178 deletions(-)

diff --git a/drivers/md/Kconfig b/drivers/md/Kconfig
index 45254b3ef715..30ca87cf25db 100644
--- a/drivers/md/Kconfig
+++ b/drivers/md/Kconfig
@@ -271,6 +271,7 @@ config DM_CRYPT
 	depends on BLK_DEV_DM
 	select CRYPTO
 	select CRYPTO_CBC
+	select CRYPTO_ESSIV
 	---help---
 	  This device-mapper target allows you to create a device that
 	  transparently encrypts the data on it. You'll need to activate
diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index f001f1104cb5..89efd7d249fd 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -98,11 +98,6 @@ struct crypt_iv_operations {
 		    struct dm_crypt_request *dmreq);
 };
 
-struct iv_essiv_private {
-	struct crypto_shash *hash_tfm;
-	u8 *salt;
-};
-
 struct iv_benbi_private {
 	int shift;
 };
@@ -155,7 +150,6 @@ struct crypt_config {
 
 	const struct crypt_iv_operations *iv_gen_ops;
 	union {
-		struct iv_essiv_private essiv;
 		struct iv_benbi_private benbi;
 		struct iv_lmk_private lmk;
 		struct iv_tcw_private tcw;
@@ -165,8 +159,6 @@ struct crypt_config {
 	unsigned short int sector_size;
 	unsigned char sector_shift;
 
-	/* ESSIV: struct crypto_cipher *essiv_tfm */
-	void *iv_private;
 	union {
 		struct crypto_skcipher **tfms;
 		struct crypto_aead **tfms_aead;
@@ -323,161 +315,6 @@ static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv,
 	return 0;
 }
 
-/* Initialise ESSIV - compute salt but no local memory allocations */
-static int crypt_iv_essiv_init(struct crypt_config *cc)
-{
-	struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
-	SHASH_DESC_ON_STACK(desc, essiv->hash_tfm);
-	struct crypto_cipher *essiv_tfm;
-	int err;
-
-	desc->tfm = essiv->hash_tfm;
-
-	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_shash_digestsize(essiv->hash_tfm));
-	if (err)
-		return err;
-
-	return 0;
-}
-
-/* Wipe salt and reset key derived from volume key */
-static int crypt_iv_essiv_wipe(struct crypt_config *cc)
-{
-	struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
-	unsigned salt_size = crypto_shash_digestsize(essiv->hash_tfm);
-	struct crypto_cipher *essiv_tfm;
-	int r, err = 0;
-
-	memset(essiv->salt, 0, salt_size);
-
-	essiv_tfm = cc->iv_private;
-	r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
-	if (r)
-		err = r;
-
-	return err;
-}
-
-/* Allocate the cipher for ESSIV */
-static struct crypto_cipher *alloc_essiv_cipher(struct crypt_config *cc,
-						struct dm_target *ti,
-						const u8 *salt,
-						unsigned int saltsize)
-{
-	struct crypto_cipher *essiv_tfm;
-	int err;
-
-	/* Setup the essiv_tfm with the given salt */
-	essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, 0);
-	if (IS_ERR(essiv_tfm)) {
-		ti->error = "Error allocating crypto tfm for ESSIV";
-		return essiv_tfm;
-	}
-
-	if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
-		ti->error = "Block size of ESSIV cipher does "
-			    "not match IV size of block cipher";
-		crypto_free_cipher(essiv_tfm);
-		return ERR_PTR(-EINVAL);
-	}
-
-	err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
-	if (err) {
-		ti->error = "Failed to set key for ESSIV cipher";
-		crypto_free_cipher(essiv_tfm);
-		return ERR_PTR(err);
-	}
-
-	return essiv_tfm;
-}
-
-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_shash(essiv->hash_tfm);
-	essiv->hash_tfm = NULL;
-
-	kzfree(essiv->salt);
-	essiv->salt = NULL;
-
-	essiv_tfm = cc->iv_private;
-
-	if (essiv_tfm)
-		crypto_free_cipher(essiv_tfm);
-
-	cc->iv_private = NULL;
-}
-
-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_shash *hash_tfm = NULL;
-	u8 *salt = NULL;
-	int err;
-
-	if (!opts) {
-		ti->error = "Digest algorithm missing for ESSIV mode";
-		return -EINVAL;
-	}
-
-	/* Allocate hash algorithm */
-	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_shash_digestsize(hash_tfm), GFP_KERNEL);
-	if (!salt) {
-		ti->error = "Error kmallocing salt storage in ESSIV";
-		err = -ENOMEM;
-		goto bad;
-	}
-
-	cc->iv_gen_private.essiv.salt = salt;
-	cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
-
-	essiv_tfm = alloc_essiv_cipher(cc, ti, salt,
-				       crypto_shash_digestsize(hash_tfm));
-	if (IS_ERR(essiv_tfm)) {
-		crypt_iv_essiv_dtr(cc);
-		return PTR_ERR(essiv_tfm);
-	}
-	cc->iv_private = essiv_tfm;
-
-	return 0;
-
-bad:
-	if (hash_tfm && !IS_ERR(hash_tfm))
-		crypto_free_shash(hash_tfm);
-	kfree(salt);
-	return err;
-}
-
-static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
-			      struct dm_crypt_request *dmreq)
-{
-	struct crypto_cipher *essiv_tfm = cc->iv_private;
-
-	memset(iv, 0, cc->iv_size);
-	*(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
-	crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
-
-	return 0;
-}
-
 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
 			      const char *opts)
 {
@@ -853,14 +690,6 @@ static const struct crypt_iv_operations crypt_iv_plain64be_ops = {
 	.generator = crypt_iv_plain64be_gen
 };
 
-static const struct crypt_iv_operations crypt_iv_essiv_ops = {
-	.ctr       = crypt_iv_essiv_ctr,
-	.dtr       = crypt_iv_essiv_dtr,
-	.init      = crypt_iv_essiv_init,
-	.wipe      = crypt_iv_essiv_wipe,
-	.generator = crypt_iv_essiv_gen
-};
-
 static const struct crypt_iv_operations crypt_iv_benbi_ops = {
 	.ctr	   = crypt_iv_benbi_ctr,
 	.dtr	   = crypt_iv_benbi_dtr,
@@ -2283,7 +2112,7 @@ static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
 	else if (strcmp(ivmode, "plain64be") == 0)
 		cc->iv_gen_ops = &crypt_iv_plain64be_ops;
 	else if (strcmp(ivmode, "essiv") == 0)
-		cc->iv_gen_ops = &crypt_iv_essiv_ops;
+		cc->iv_gen_ops = &crypt_iv_plain64_ops;
 	else if (strcmp(ivmode, "benbi") == 0)
 		cc->iv_gen_ops = &crypt_iv_benbi_ops;
 	else if (strcmp(ivmode, "null") == 0)
@@ -2397,7 +2226,7 @@ static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key
 				char **ivmode, char **ivopts)
 {
 	struct crypt_config *cc = ti->private;
-	char *tmp, *cipher_api;
+	char *tmp, *cipher_api, buf[CRYPTO_MAX_ALG_NAME];
 	int ret = -EINVAL;
 
 	cc->tfms_count = 1;
@@ -2435,9 +2264,19 @@ static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key
 	}
 
 	ret = crypt_ctr_blkdev_cipher(cc, cipher_api);
-	if (ret < 0) {
-		ti->error = "Cannot allocate cipher string";
-		return -ENOMEM;
+	if (ret < 0)
+		goto bad_mem;
+
+	if (*ivmode && !strcmp(*ivmode, "essiv")) {
+		if (!*ivopts) {
+			ti->error = "Digest algorithm missing for ESSIV mode";
+			return -EINVAL;
+		}
+		ret = snprintf(buf, CRYPTO_MAX_ALG_NAME, "essiv(%s,%s,%s)",
+			       cipher_api, cc->cipher, *ivopts);
+		if (ret < 0)
+			goto bad_mem;
+		cipher_api = buf;
 	}
 
 	cc->key_parts = cc->tfms_count;
@@ -2456,6 +2295,9 @@ static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key
 		cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
 
 	return 0;
+bad_mem:
+	ti->error = "Cannot allocate cipher string";
+	return -ENOMEM;
 }
 
 static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
@@ -2515,8 +2357,18 @@ static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key
 	if (!cipher_api)
 		goto bad_mem;
 
-	ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
-		       "%s(%s)", chainmode, cipher);
+	if (!strcmp(*ivmode, "essiv")) {
+		if (!*ivopts) {
+			ti->error = "Digest algorithm missing for ESSIV mode";
+			return -EINVAL;
+		}
+		ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
+			       "essiv(%s(%s),%s,%s)", chainmode, cipher,
+			       cipher, *ivopts);
+	} else {
+		ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
+			       "%s(%s)", chainmode, cipher);
+	}
 	if (ret < 0) {
 		kfree(cipher_api);
 		goto bad_mem;
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
  2019-06-18 21:27 ` Ard Biesheuvel
@ 2019-06-19  6:56   ` Milan Broz
  -1 siblings, 0 replies; 45+ messages in thread
From: Milan Broz @ 2019-06-19  6:56 UTC (permalink / raw)
  To: Ard Biesheuvel, linux-crypto
  Cc: Herbert Xu, Eric Biggers, dm-devel, linux-fscrypt, Gilad Ben-Yossef

On 18/06/2019 23:27, Ard Biesheuvel wrote:
> This series creates an ESSIV template that produces a skcipher or AEAD
> transform based on a tuple of the form '<skcipher>,<cipher>,<shash>'
> (or '<aead>,<cipher>,<shash>' for the AEAD case). It exposes the
> encapsulated sync or async skcipher/aead by passing through all operations,
> while using the cipher/shash pair to transform the input IV into an ESSIV
> output IV.
> 
> This matches what both users of ESSIV in the kernel do, and so it is proposed
> as a replacement for those, in patches #2 and #4.
> 
> This code has been tested using the fscrypt test suggested by Eric
> (generic/549), as well as the mode-test script suggested by Milan for
> the dm-crypt case. I also tested the aead case in a virtual machine,
> but it definitely needs some wider testing from the dm-crypt experts.

Well, I just run "make check" on cyptsetup upstream (32bit VM, Linus' tree
with this patcheset applied), and get this on the first api test...

Just try
cryptsetup open --type plain -c aes-cbc-essiv:sha256 /dev/sdd test

kernel: alg: No test for essiv(cbc(aes),aes,sha256) (essiv(cbc-aes-aesni,aes-aesni,sha256-generic))
kernel: BUG: unable to handle page fault for address: 00c14578
kernel: #PF: supervisor read access in kernel mode
kernel: #PF: error_code(0x0000) - not-present page
kernel: *pde = 00000000 
kernel: Oops: 0000 [#1] PREEMPT SMP
kernel: CPU: 2 PID: 15611 Comm: kworker/u17:2 Not tainted 5.2.0-rc5+ #519
kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
kernel: Workqueue: kcryptd/253:2 kcryptd_crypt [dm_crypt]
kernel: EIP: essiv_skcipher_decrypt+0x3/0x20
kernel: Code: 5f 5d c3 90 90 90 90 55 8b 48 0c 89 e5 8d 41 10 ff 51 18 5d c3 66 90 55 8b 40 0c 89 e5 ff 50 08 5d c3 8d 74 26 00 90 8b 50 58 <f6> 02 01 75 10 55 83 c0 38 89 e5 ff 52 f0 5d c3 8d 74 26 00 90 b8
kernel: EAX: ee87fc08 EBX: ee87fd40 ECX: ee87fdc4 EDX: 00c14578
kernel: ESI: ee87fb78 EDI: f0a70800 EBP: ef7a9ed8 ESP: ef7a9e3c
kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010246
kernel: CR0: 80050033 CR2: 00c14578 CR3: 01b87000 CR4: 00140690
kernel: Call Trace:
kernel:  ? crypt_convert+0x864/0xe50 [dm_crypt]
kernel:  ? static_obj+0x32/0x50
kernel:  ? lockdep_init_map+0x34/0x1b0
kernel:  ? __init_waitqueue_head+0x29/0x40
kernel:  kcryptd_crypt+0xca/0x3b0 [dm_crypt]
kernel:  ? process_one_work+0x1a6/0x5a0
kernel:  process_one_work+0x214/0x5a0
kernel:  worker_thread+0x134/0x3e0
kernel:  ? process_one_work+0x5a0/0x5a0
kernel:  kthread+0xd4/0x100
kernel:  ? process_one_work+0x5a0/0x5a0
kernel:  ? kthread_park+0x90/0x90
kernel:  ret_from_fork+0x19/0x24
kernel: Modules linked in: dm_zero dm_integrity async_xor xor async_tx dm_verity reed_solomon dm_bufio dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
kernel: CR2: 0000000000c14578
kernel: ---[ end trace 8a651b067b7b6a10 ]---
kernel: EIP: essiv_skcipher_decrypt+0x3/0x20
kernel: Code: 5f 5d c3 90 90 90 90 55 8b 48 0c 89 e5 8d 41 10 ff 51 18 5d c3 66 90 55 8b 40 0c 89 e5 ff 50 08 5d c3 8d 74 26 00 90 8b 50 58 <f6> 02 01 75 10 55 83 c0 38 89 e5 ff 52 f0 5d c3 8d 74 26 00 90 b8
kernel: EAX: ee87fc08 EBX: ee87fd40 ECX: ee87fdc4 EDX: 00c14578
kernel: ESI: ee87fb78 EDI: f0a70800 EBP: ef7a9ed8 ESP: c1b8b45c
kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010246
kernel: CR0: 80050033 CR2: 00c14578 CR3: 01b87000 CR4: 00140690

Milan

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19  6:56   ` Milan Broz
  0 siblings, 0 replies; 45+ messages in thread
From: Milan Broz @ 2019-06-19  6:56 UTC (permalink / raw)
  To: Ard Biesheuvel, linux-crypto
  Cc: Gilad Ben-Yossef, dm-devel, linux-fscrypt, Herbert Xu, Eric Biggers

On 18/06/2019 23:27, Ard Biesheuvel wrote:
> This series creates an ESSIV template that produces a skcipher or AEAD
> transform based on a tuple of the form '<skcipher>,<cipher>,<shash>'
> (or '<aead>,<cipher>,<shash>' for the AEAD case). It exposes the
> encapsulated sync or async skcipher/aead by passing through all operations,
> while using the cipher/shash pair to transform the input IV into an ESSIV
> output IV.
> 
> This matches what both users of ESSIV in the kernel do, and so it is proposed
> as a replacement for those, in patches #2 and #4.
> 
> This code has been tested using the fscrypt test suggested by Eric
> (generic/549), as well as the mode-test script suggested by Milan for
> the dm-crypt case. I also tested the aead case in a virtual machine,
> but it definitely needs some wider testing from the dm-crypt experts.

Well, I just run "make check" on cyptsetup upstream (32bit VM, Linus' tree
with this patcheset applied), and get this on the first api test...

Just try
cryptsetup open --type plain -c aes-cbc-essiv:sha256 /dev/sdd test

kernel: alg: No test for essiv(cbc(aes),aes,sha256) (essiv(cbc-aes-aesni,aes-aesni,sha256-generic))
kernel: BUG: unable to handle page fault for address: 00c14578
kernel: #PF: supervisor read access in kernel mode
kernel: #PF: error_code(0x0000) - not-present page
kernel: *pde = 00000000 
kernel: Oops: 0000 [#1] PREEMPT SMP
kernel: CPU: 2 PID: 15611 Comm: kworker/u17:2 Not tainted 5.2.0-rc5+ #519
kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
kernel: Workqueue: kcryptd/253:2 kcryptd_crypt [dm_crypt]
kernel: EIP: essiv_skcipher_decrypt+0x3/0x20
kernel: Code: 5f 5d c3 90 90 90 90 55 8b 48 0c 89 e5 8d 41 10 ff 51 18 5d c3 66 90 55 8b 40 0c 89 e5 ff 50 08 5d c3 8d 74 26 00 90 8b 50 58 <f6> 02 01 75 10 55 83 c0 38 89 e5 ff 52 f0 5d c3 8d 74 26 00 90 b8
kernel: EAX: ee87fc08 EBX: ee87fd40 ECX: ee87fdc4 EDX: 00c14578
kernel: ESI: ee87fb78 EDI: f0a70800 EBP: ef7a9ed8 ESP: ef7a9e3c
kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010246
kernel: CR0: 80050033 CR2: 00c14578 CR3: 01b87000 CR4: 00140690
kernel: Call Trace:
kernel:  ? crypt_convert+0x864/0xe50 [dm_crypt]
kernel:  ? static_obj+0x32/0x50
kernel:  ? lockdep_init_map+0x34/0x1b0
kernel:  ? __init_waitqueue_head+0x29/0x40
kernel:  kcryptd_crypt+0xca/0x3b0 [dm_crypt]
kernel:  ? process_one_work+0x1a6/0x5a0
kernel:  process_one_work+0x214/0x5a0
kernel:  worker_thread+0x134/0x3e0
kernel:  ? process_one_work+0x5a0/0x5a0
kernel:  kthread+0xd4/0x100
kernel:  ? process_one_work+0x5a0/0x5a0
kernel:  ? kthread_park+0x90/0x90
kernel:  ret_from_fork+0x19/0x24
kernel: Modules linked in: dm_zero dm_integrity async_xor xor async_tx dm_verity reed_solomon dm_bufio dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
kernel: CR2: 0000000000c14578
kernel: ---[ end trace 8a651b067b7b6a10 ]---
kernel: EIP: essiv_skcipher_decrypt+0x3/0x20
kernel: Code: 5f 5d c3 90 90 90 90 55 8b 48 0c 89 e5 8d 41 10 ff 51 18 5d c3 66 90 55 8b 40 0c 89 e5 ff 50 08 5d c3 8d 74 26 00 90 8b 50 58 <f6> 02 01 75 10 55 83 c0 38 89 e5 ff 52 f0 5d c3 8d 74 26 00 90 b8
kernel: EAX: ee87fc08 EBX: ee87fd40 ECX: ee87fdc4 EDX: 00c14578
kernel: ESI: ee87fb78 EDI: f0a70800 EBP: ef7a9ed8 ESP: c1b8b45c
kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010246
kernel: CR0: 80050033 CR2: 00c14578 CR3: 01b87000 CR4: 00140690

Milan

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
  2019-06-19  6:56   ` Milan Broz
  (?)
@ 2019-06-19  7:11     ` Ard Biesheuvel
  -1 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19  7:11 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 08:56, Milan Broz <gmazyland@gmail.com> wrote:
>
> On 18/06/2019 23:27, Ard Biesheuvel wrote:
> > This series creates an ESSIV template that produces a skcipher or AEAD
> > transform based on a tuple of the form '<skcipher>,<cipher>,<shash>'
> > (or '<aead>,<cipher>,<shash>' for the AEAD case). It exposes the
> > encapsulated sync or async skcipher/aead by passing through all operations,
> > while using the cipher/shash pair to transform the input IV into an ESSIV
> > output IV.
> >
> > This matches what both users of ESSIV in the kernel do, and so it is proposed
> > as a replacement for those, in patches #2 and #4.
> >
> > This code has been tested using the fscrypt test suggested by Eric
> > (generic/549), as well as the mode-test script suggested by Milan for
> > the dm-crypt case. I also tested the aead case in a virtual machine,
> > but it definitely needs some wider testing from the dm-crypt experts.
>
> Well, I just run "make check" on cyptsetup upstream (32bit VM, Linus' tree
> with this patcheset applied), and get this on the first api test...
>

Ugh. Thanks for trying. I will have a look today.


> Just try
> cryptsetup open --type plain -c aes-cbc-essiv:sha256 /dev/sdd test
>
> kernel: alg: No test for essiv(cbc(aes),aes,sha256) (essiv(cbc-aes-aesni,aes-aesni,sha256-generic))
> kernel: BUG: unable to handle page fault for address: 00c14578
> kernel: #PF: supervisor read access in kernel mode
> kernel: #PF: error_code(0x0000) - not-present page
> kernel: *pde = 00000000
> kernel: Oops: 0000 [#1] PREEMPT SMP
> kernel: CPU: 2 PID: 15611 Comm: kworker/u17:2 Not tainted 5.2.0-rc5+ #519
> kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
> kernel: Workqueue: kcryptd/253:2 kcryptd_crypt [dm_crypt]
> kernel: EIP: essiv_skcipher_decrypt+0x3/0x20
> kernel: Code: 5f 5d c3 90 90 90 90 55 8b 48 0c 89 e5 8d 41 10 ff 51 18 5d c3 66 90 55 8b 40 0c 89 e5 ff 50 08 5d c3 8d 74 26 00 90 8b 50 58 <f6> 02 01 75 10 55 83 c0 38 89 e5 ff 52 f0 5d c3 8d 74 26 00 90 b8
> kernel: EAX: ee87fc08 EBX: ee87fd40 ECX: ee87fdc4 EDX: 00c14578
> kernel: ESI: ee87fb78 EDI: f0a70800 EBP: ef7a9ed8 ESP: ef7a9e3c
> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010246
> kernel: CR0: 80050033 CR2: 00c14578 CR3: 01b87000 CR4: 00140690
> kernel: Call Trace:
> kernel:  ? crypt_convert+0x864/0xe50 [dm_crypt]
> kernel:  ? static_obj+0x32/0x50
> kernel:  ? lockdep_init_map+0x34/0x1b0
> kernel:  ? __init_waitqueue_head+0x29/0x40
> kernel:  kcryptd_crypt+0xca/0x3b0 [dm_crypt]
> kernel:  ? process_one_work+0x1a6/0x5a0
> kernel:  process_one_work+0x214/0x5a0
> kernel:  worker_thread+0x134/0x3e0
> kernel:  ? process_one_work+0x5a0/0x5a0
> kernel:  kthread+0xd4/0x100
> kernel:  ? process_one_work+0x5a0/0x5a0
> kernel:  ? kthread_park+0x90/0x90
> kernel:  ret_from_fork+0x19/0x24
> kernel: Modules linked in: dm_zero dm_integrity async_xor xor async_tx dm_verity reed_solomon dm_bufio dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
> kernel: CR2: 0000000000c14578
> kernel: ---[ end trace 8a651b067b7b6a10 ]---
> kernel: EIP: essiv_skcipher_decrypt+0x3/0x20
> kernel: Code: 5f 5d c3 90 90 90 90 55 8b 48 0c 89 e5 8d 41 10 ff 51 18 5d c3 66 90 55 8b 40 0c 89 e5 ff 50 08 5d c3 8d 74 26 00 90 8b 50 58 <f6> 02 01 75 10 55 83 c0 38 89 e5 ff 52 f0 5d c3 8d 74 26 00 90 b8
> kernel: EAX: ee87fc08 EBX: ee87fd40 ECX: ee87fdc4 EDX: 00c14578
> kernel: ESI: ee87fb78 EDI: f0a70800 EBP: ef7a9ed8 ESP: c1b8b45c
> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010246
> kernel: CR0: 80050033 CR2: 00c14578 CR3: 01b87000 CR4: 00140690
>
> Milan

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19  7:11     ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19  7:11 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 08:56, Milan Broz <gmazyland@gmail.com> wrote:
>
> On 18/06/2019 23:27, Ard Biesheuvel wrote:
> > This series creates an ESSIV template that produces a skcipher or AEAD
> > transform based on a tuple of the form '<skcipher>,<cipher>,<shash>'
> > (or '<aead>,<cipher>,<shash>' for the AEAD case). It exposes the
> > encapsulated sync or async skcipher/aead by passing through all operations,
> > while using the cipher/shash pair to transform the input IV into an ESSIV
> > output IV.
> >
> > This matches what both users of ESSIV in the kernel do, and so it is proposed
> > as a replacement for those, in patches #2 and #4.
> >
> > This code has been tested using the fscrypt test suggested by Eric
> > (generic/549), as well as the mode-test script suggested by Milan for
> > the dm-crypt case. I also tested the aead case in a virtual machine,
> > but it definitely needs some wider testing from the dm-crypt experts.
>
> Well, I just run "make check" on cyptsetup upstream (32bit VM, Linus' tree
> with this patcheset applied), and get this on the first api test...
>

Ugh. Thanks for trying. I will have a look today.


> Just try
> cryptsetup open --type plain -c aes-cbc-essiv:sha256 /dev/sdd test
>
> kernel: alg: No test for essiv(cbc(aes),aes,sha256) (essiv(cbc-aes-aesni,aes-aesni,sha256-generic))
> kernel: BUG: unable to handle page fault for address: 00c14578
> kernel: #PF: supervisor read access in kernel mode
> kernel: #PF: error_code(0x0000) - not-present page
> kernel: *pde = 00000000
> kernel: Oops: 0000 [#1] PREEMPT SMP
> kernel: CPU: 2 PID: 15611 Comm: kworker/u17:2 Not tainted 5.2.0-rc5+ #519
> kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
> kernel: Workqueue: kcryptd/253:2 kcryptd_crypt [dm_crypt]
> kernel: EIP: essiv_skcipher_decrypt+0x3/0x20
> kernel: Code: 5f 5d c3 90 90 90 90 55 8b 48 0c 89 e5 8d 41 10 ff 51 18 5d c3 66 90 55 8b 40 0c 89 e5 ff 50 08 5d c3 8d 74 26 00 90 8b 50 58 <f6> 02 01 75 10 55 83 c0 38 89 e5 ff 52 f0 5d c3 8d 74 26 00 90 b8
> kernel: EAX: ee87fc08 EBX: ee87fd40 ECX: ee87fdc4 EDX: 00c14578
> kernel: ESI: ee87fb78 EDI: f0a70800 EBP: ef7a9ed8 ESP: ef7a9e3c
> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010246
> kernel: CR0: 80050033 CR2: 00c14578 CR3: 01b87000 CR4: 00140690
> kernel: Call Trace:
> kernel:  ? crypt_convert+0x864/0xe50 [dm_crypt]
> kernel:  ? static_obj+0x32/0x50
> kernel:  ? lockdep_init_map+0x34/0x1b0
> kernel:  ? __init_waitqueue_head+0x29/0x40
> kernel:  kcryptd_crypt+0xca/0x3b0 [dm_crypt]
> kernel:  ? process_one_work+0x1a6/0x5a0
> kernel:  process_one_work+0x214/0x5a0
> kernel:  worker_thread+0x134/0x3e0
> kernel:  ? process_one_work+0x5a0/0x5a0
> kernel:  kthread+0xd4/0x100
> kernel:  ? process_one_work+0x5a0/0x5a0
> kernel:  ? kthread_park+0x90/0x90
> kernel:  ret_from_fork+0x19/0x24
> kernel: Modules linked in: dm_zero dm_integrity async_xor xor async_tx dm_verity reed_solomon dm_bufio dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
> kernel: CR2: 0000000000c14578
> kernel: ---[ end trace 8a651b067b7b6a10 ]---
> kernel: EIP: essiv_skcipher_decrypt+0x3/0x20
> kernel: Code: 5f 5d c3 90 90 90 90 55 8b 48 0c 89 e5 8d 41 10 ff 51 18 5d c3 66 90 55 8b 40 0c 89 e5 ff 50 08 5d c3 8d 74 26 00 90 8b 50 58 <f6> 02 01 75 10 55 83 c0 38 89 e5 ff 52 f0 5d c3 8d 74 26 00 90 b8
> kernel: EAX: ee87fc08 EBX: ee87fd40 ECX: ee87fdc4 EDX: 00c14578
> kernel: ESI: ee87fb78 EDI: f0a70800 EBP: ef7a9ed8 ESP: c1b8b45c
> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010246
> kernel: CR0: 80050033 CR2: 00c14578 CR3: 01b87000 CR4: 00140690
>
> Milan

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19  7:11     ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19  7:11 UTC (permalink / raw)
  To: Milan Broz
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	device-mapper development,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE

On Wed, 19 Jun 2019 at 08:56, Milan Broz <gmazyland@gmail.com> wrote:
>
> On 18/06/2019 23:27, Ard Biesheuvel wrote:
> > This series creates an ESSIV template that produces a skcipher or AEAD
> > transform based on a tuple of the form '<skcipher>,<cipher>,<shash>'
> > (or '<aead>,<cipher>,<shash>' for the AEAD case). It exposes the
> > encapsulated sync or async skcipher/aead by passing through all operations,
> > while using the cipher/shash pair to transform the input IV into an ESSIV
> > output IV.
> >
> > This matches what both users of ESSIV in the kernel do, and so it is proposed
> > as a replacement for those, in patches #2 and #4.
> >
> > This code has been tested using the fscrypt test suggested by Eric
> > (generic/549), as well as the mode-test script suggested by Milan for
> > the dm-crypt case. I also tested the aead case in a virtual machine,
> > but it definitely needs some wider testing from the dm-crypt experts.
>
> Well, I just run "make check" on cyptsetup upstream (32bit VM, Linus' tree
> with this patcheset applied), and get this on the first api test...
>

Ugh. Thanks for trying. I will have a look today.


> Just try
> cryptsetup open --type plain -c aes-cbc-essiv:sha256 /dev/sdd test
>
> kernel: alg: No test for essiv(cbc(aes),aes,sha256) (essiv(cbc-aes-aesni,aes-aesni,sha256-generic))
> kernel: BUG: unable to handle page fault for address: 00c14578
> kernel: #PF: supervisor read access in kernel mode
> kernel: #PF: error_code(0x0000) - not-present page
> kernel: *pde = 00000000
> kernel: Oops: 0000 [#1] PREEMPT SMP
> kernel: CPU: 2 PID: 15611 Comm: kworker/u17:2 Not tainted 5.2.0-rc5+ #519
> kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
> kernel: Workqueue: kcryptd/253:2 kcryptd_crypt [dm_crypt]
> kernel: EIP: essiv_skcipher_decrypt+0x3/0x20
> kernel: Code: 5f 5d c3 90 90 90 90 55 8b 48 0c 89 e5 8d 41 10 ff 51 18 5d c3 66 90 55 8b 40 0c 89 e5 ff 50 08 5d c3 8d 74 26 00 90 8b 50 58 <f6> 02 01 75 10 55 83 c0 38 89 e5 ff 52 f0 5d c3 8d 74 26 00 90 b8
> kernel: EAX: ee87fc08 EBX: ee87fd40 ECX: ee87fdc4 EDX: 00c14578
> kernel: ESI: ee87fb78 EDI: f0a70800 EBP: ef7a9ed8 ESP: ef7a9e3c
> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010246
> kernel: CR0: 80050033 CR2: 00c14578 CR3: 01b87000 CR4: 00140690
> kernel: Call Trace:
> kernel:  ? crypt_convert+0x864/0xe50 [dm_crypt]
> kernel:  ? static_obj+0x32/0x50
> kernel:  ? lockdep_init_map+0x34/0x1b0
> kernel:  ? __init_waitqueue_head+0x29/0x40
> kernel:  kcryptd_crypt+0xca/0x3b0 [dm_crypt]
> kernel:  ? process_one_work+0x1a6/0x5a0
> kernel:  process_one_work+0x214/0x5a0
> kernel:  worker_thread+0x134/0x3e0
> kernel:  ? process_one_work+0x5a0/0x5a0
> kernel:  kthread+0xd4/0x100
> kernel:  ? process_one_work+0x5a0/0x5a0
> kernel:  ? kthread_park+0x90/0x90
> kernel:  ret_from_fork+0x19/0x24
> kernel: Modules linked in: dm_zero dm_integrity async_xor xor async_tx dm_verity reed_solomon dm_bufio dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
> kernel: CR2: 0000000000c14578
> kernel: ---[ end trace 8a651b067b7b6a10 ]---
> kernel: EIP: essiv_skcipher_decrypt+0x3/0x20
> kernel: Code: 5f 5d c3 90 90 90 90 55 8b 48 0c 89 e5 8d 41 10 ff 51 18 5d c3 66 90 55 8b 40 0c 89 e5 ff 50 08 5d c3 8d 74 26 00 90 8b 50 58 <f6> 02 01 75 10 55 83 c0 38 89 e5 ff 52 f0 5d c3 8d 74 26 00 90 b8
> kernel: EAX: ee87fc08 EBX: ee87fd40 ECX: ee87fdc4 EDX: 00c14578
> kernel: ESI: ee87fb78 EDI: f0a70800 EBP: ef7a9ed8 ESP: c1b8b45c
> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00010246
> kernel: CR0: 80050033 CR2: 00c14578 CR3: 01b87000 CR4: 00140690
>
> Milan

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
  2019-06-19  7:11     ` Ard Biesheuvel
  (?)
@ 2019-06-19  9:14       ` Ard Biesheuvel
  -1 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19  9:14 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 09:11, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>
> On Wed, 19 Jun 2019 at 08:56, Milan Broz <gmazyland@gmail.com> wrote:
> >
> > On 18/06/2019 23:27, Ard Biesheuvel wrote:
> > > This series creates an ESSIV template that produces a skcipher or AEAD
> > > transform based on a tuple of the form '<skcipher>,<cipher>,<shash>'
> > > (or '<aead>,<cipher>,<shash>' for the AEAD case). It exposes the
> > > encapsulated sync or async skcipher/aead by passing through all operations,
> > > while using the cipher/shash pair to transform the input IV into an ESSIV
> > > output IV.
> > >
> > > This matches what both users of ESSIV in the kernel do, and so it is proposed
> > > as a replacement for those, in patches #2 and #4.
> > >
> > > This code has been tested using the fscrypt test suggested by Eric
> > > (generic/549), as well as the mode-test script suggested by Milan for
> > > the dm-crypt case. I also tested the aead case in a virtual machine,
> > > but it definitely needs some wider testing from the dm-crypt experts.
> >
> > Well, I just run "make check" on cyptsetup upstream (32bit VM, Linus' tree
> > with this patcheset applied), and get this on the first api test...
> >
>
> Ugh. Thanks for trying. I will have a look today.
>
>
> > Just try
> > cryptsetup open --type plain -c aes-cbc-essiv:sha256 /dev/sdd test
> >

Apologies, this was a rebase error on my part.

Could you please apply the hunk below and try again?

diff --git a/crypto/essiv.c b/crypto/essiv.c
index 029a65afb4d7..5dc2e592077e 100644
--- a/crypto/essiv.c
+++ b/crypto/essiv.c
@@ -243,6 +243,8 @@ static int essiv_aead_encrypt(struct aead_request *req)
 static int essiv_skcipher_decrypt(struct skcipher_request *req)
 {
        struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
+
+       essiv_skcipher_prepare_subreq(req);
        return crypto_skcipher_decrypt(&rctx->blockcipher_req);
 }

^ permalink raw reply related	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19  9:14       ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19  9:14 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 09:11, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>
> On Wed, 19 Jun 2019 at 08:56, Milan Broz <gmazyland@gmail.com> wrote:
> >
> > On 18/06/2019 23:27, Ard Biesheuvel wrote:
> > > This series creates an ESSIV template that produces a skcipher or AEAD
> > > transform based on a tuple of the form '<skcipher>,<cipher>,<shash>'
> > > (or '<aead>,<cipher>,<shash>' for the AEAD case). It exposes the
> > > encapsulated sync or async skcipher/aead by passing through all operations,
> > > while using the cipher/shash pair to transform the input IV into an ESSIV
> > > output IV.
> > >
> > > This matches what both users of ESSIV in the kernel do, and so it is proposed
> > > as a replacement for those, in patches #2 and #4.
> > >
> > > This code has been tested using the fscrypt test suggested by Eric
> > > (generic/549), as well as the mode-test script suggested by Milan for
> > > the dm-crypt case. I also tested the aead case in a virtual machine,
> > > but it definitely needs some wider testing from the dm-crypt experts.
> >
> > Well, I just run "make check" on cyptsetup upstream (32bit VM, Linus' tree
> > with this patcheset applied), and get this on the first api test...
> >
>
> Ugh. Thanks for trying. I will have a look today.
>
>
> > Just try
> > cryptsetup open --type plain -c aes-cbc-essiv:sha256 /dev/sdd test
> >

Apologies, this was a rebase error on my part.

Could you please apply the hunk below and try again?

diff --git a/crypto/essiv.c b/crypto/essiv.c
index 029a65afb4d7..5dc2e592077e 100644
--- a/crypto/essiv.c
+++ b/crypto/essiv.c
@@ -243,6 +243,8 @@ static int essiv_aead_encrypt(struct aead_request *req)
 static int essiv_skcipher_decrypt(struct skcipher_request *req)
 {
        struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
+
+       essiv_skcipher_prepare_subreq(req);
        return crypto_skcipher_decrypt(&rctx->blockcipher_req);
 }

^ permalink raw reply related	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19  9:14       ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19  9:14 UTC (permalink / raw)
  To: Milan Broz
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	device-mapper development,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE

On Wed, 19 Jun 2019 at 09:11, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>
> On Wed, 19 Jun 2019 at 08:56, Milan Broz <gmazyland@gmail.com> wrote:
> >
> > On 18/06/2019 23:27, Ard Biesheuvel wrote:
> > > This series creates an ESSIV template that produces a skcipher or AEAD
> > > transform based on a tuple of the form '<skcipher>,<cipher>,<shash>'
> > > (or '<aead>,<cipher>,<shash>' for the AEAD case). It exposes the
> > > encapsulated sync or async skcipher/aead by passing through all operations,
> > > while using the cipher/shash pair to transform the input IV into an ESSIV
> > > output IV.
> > >
> > > This matches what both users of ESSIV in the kernel do, and so it is proposed
> > > as a replacement for those, in patches #2 and #4.
> > >
> > > This code has been tested using the fscrypt test suggested by Eric
> > > (generic/549), as well as the mode-test script suggested by Milan for
> > > the dm-crypt case. I also tested the aead case in a virtual machine,
> > > but it definitely needs some wider testing from the dm-crypt experts.
> >
> > Well, I just run "make check" on cyptsetup upstream (32bit VM, Linus' tree
> > with this patcheset applied), and get this on the first api test...
> >
>
> Ugh. Thanks for trying. I will have a look today.
>
>
> > Just try
> > cryptsetup open --type plain -c aes-cbc-essiv:sha256 /dev/sdd test
> >

Apologies, this was a rebase error on my part.

Could you please apply the hunk below and try again?

diff --git a/crypto/essiv.c b/crypto/essiv.c
index 029a65afb4d7..5dc2e592077e 100644
--- a/crypto/essiv.c
+++ b/crypto/essiv.c
@@ -243,6 +243,8 @@ static int essiv_aead_encrypt(struct aead_request *req)
 static int essiv_skcipher_decrypt(struct skcipher_request *req)
 {
        struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
+
+       essiv_skcipher_prepare_subreq(req);
        return crypto_skcipher_decrypt(&rctx->blockcipher_req);
 }

^ permalink raw reply related	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
  2019-06-19  9:14       ` Ard Biesheuvel
  (?)
@ 2019-06-19 11:01         ` Milan Broz
  -1 siblings, 0 replies; 45+ messages in thread
From: Milan Broz @ 2019-06-19 11:01 UTC (permalink / raw)
  To: Ard Biesheuvel, Milan Broz
  Cc: linux-crypto, Herbert Xu, Eric Biggers,
	device-mapper development, linux-fscrypt, Gilad Ben-Yossef

On 19/06/2019 11:14, Ard Biesheuvel wrote:
> Apologies, this was a rebase error on my part.
> 
> Could you please apply the hunk below and try again?
> 
> diff --git a/crypto/essiv.c b/crypto/essiv.c
> index 029a65afb4d7..5dc2e592077e 100644
> --- a/crypto/essiv.c
> +++ b/crypto/essiv.c
> @@ -243,6 +243,8 @@ static int essiv_aead_encrypt(struct aead_request *req)
>  static int essiv_skcipher_decrypt(struct skcipher_request *req)
>  {
>         struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +
> +       essiv_skcipher_prepare_subreq(req);
>         return crypto_skcipher_decrypt(&rctx->blockcipher_req);
>  }

That helps, but now the null cipher is broken...
(We use it for debugging and during reencryption from non-encrypted device)

Try
  cryptsetup open --type plain -c null /dev/sdd test -q
or
  dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"

(or just run full cryptsetup testsuite)

kernel: BUG: kernel NULL pointer dereference, address: 00000000
kernel: #PF: supervisor read access in kernel mode
kernel: #PF: error_code(0x0000) - not-present page
kernel: *pde = 00000000 
kernel: Oops: 0000 [#1] PREEMPT SMP
kernel: CPU: 2 PID: 2261 Comm: cryptsetup Not tainted 5.2.0-rc5+ #521
kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
kernel: EIP: strcmp+0x9/0x20
kernel: Code: 00 55 89 c1 89 e5 57 89 c7 56 89 d6 ac aa 84 c0 75 fa 5e 89 c8 5f 5d c3 8d b4 26 00 00 00 00 66 90 55 89 e5 57 89 d7 56 89 c6 <ac> ae 75 08 84 c0 75 f8 31 c0 eb 04 19 c0 0c 01 5e 5f 5d c3 8d 76
kernel: EAX: 00000000 EBX: ef51016c ECX: 0000000c EDX: f78e585e
kernel: ESI: 00000000 EDI: f78e585e EBP: f238dcb0 ESP: f238dca8
kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00210282
kernel: CR0: 80050033 CR2: 00000000 CR3: 30a28000 CR4: 00140690
kernel: Call Trace:
kernel:  crypt_ctr+0x473/0xf4e [dm_crypt]
kernel:  dm_table_add_target+0x15f/0x340 [dm_mod]
kernel:  table_load+0xe9/0x280 [dm_mod]
kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
kernel:  ctl_ioctl+0x1c8/0x400 [dm_mod]
kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
kernel:  ? ctl_ioctl+0x400/0x400 [dm_mod]
kernel:  dm_ctl_ioctl+0x8/0x10 [dm_mod]
kernel:  do_vfs_ioctl+0x3dd/0x790
kernel:  ? trace_hardirqs_on+0x4a/0xf0
kernel:  ? ksys_old_semctl+0x27/0x30
kernel:  ksys_ioctl+0x2e/0x60
kernel:  ? mpihelp_add_n+0x39/0x50
kernel:  sys_ioctl+0x11/0x20
kernel:  do_int80_syscall_32+0x4b/0x1a0
kernel:  ? mpihelp_add_n+0x39/0x50
kernel:  entry_INT80_32+0xcf/0xcf
kernel: EIP: 0xb7f5bbf2
kernel: Code: de 01 00 05 ed 73 02 00 83 ec 14 8d 80 0c ac ff ff 50 6a 02 e8 5f 12 01 00 c7 04 24 7f 00 00 00 e8 ce cd 01 00 66 90 90 cd 80 <c3> 8d b6 00 00 00 00 8d bc 27 00 00 00 00 8b 1c 24 c3 8d b6 00 00
kernel: EAX: ffffffda EBX: 00000005 ECX: c138fd09 EDX: 00511080
kernel: ESI: b7b83d40 EDI: b7b785af EBP: 0050dda0 ESP: bf9e1c34
kernel: DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00200286
kernel:  ? mpihelp_add_n+0x39/0x50
kernel: Modules linked in: dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
kernel: CR2: 0000000000000000
kernel: ---[ end trace 0d32231f952fd372 ]---

m.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [dm-devel] [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 11:01         ` Milan Broz
  0 siblings, 0 replies; 45+ messages in thread
From: Milan Broz @ 2019-06-19 11:01 UTC (permalink / raw)
  To: Ard Biesheuvel, Milan Broz
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	device-mapper development, linux-crypto

On 19/06/2019 11:14, Ard Biesheuvel wrote:
> Apologies, this was a rebase error on my part.
> 
> Could you please apply the hunk below and try again?
> 
> diff --git a/crypto/essiv.c b/crypto/essiv.c
> index 029a65afb4d7..5dc2e592077e 100644
> --- a/crypto/essiv.c
> +++ b/crypto/essiv.c
> @@ -243,6 +243,8 @@ static int essiv_aead_encrypt(struct aead_request *req)
>  static int essiv_skcipher_decrypt(struct skcipher_request *req)
>  {
>         struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +
> +       essiv_skcipher_prepare_subreq(req);
>         return crypto_skcipher_decrypt(&rctx->blockcipher_req);
>  }

That helps, but now the null cipher is broken...
(We use it for debugging and during reencryption from non-encrypted device)

Try
  cryptsetup open --type plain -c null /dev/sdd test -q
or
  dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"

(or just run full cryptsetup testsuite)

kernel: BUG: kernel NULL pointer dereference, address: 00000000
kernel: #PF: supervisor read access in kernel mode
kernel: #PF: error_code(0x0000) - not-present page
kernel: *pde = 00000000 
kernel: Oops: 0000 [#1] PREEMPT SMP
kernel: CPU: 2 PID: 2261 Comm: cryptsetup Not tainted 5.2.0-rc5+ #521
kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
kernel: EIP: strcmp+0x9/0x20
kernel: Code: 00 55 89 c1 89 e5 57 89 c7 56 89 d6 ac aa 84 c0 75 fa 5e 89 c8 5f 5d c3 8d b4 26 00 00 00 00 66 90 55 89 e5 57 89 d7 56 89 c6 <ac> ae 75 08 84 c0 75 f8 31 c0 eb 04 19 c0 0c 01 5e 5f 5d c3 8d 76
kernel: EAX: 00000000 EBX: ef51016c ECX: 0000000c EDX: f78e585e
kernel: ESI: 00000000 EDI: f78e585e EBP: f238dcb0 ESP: f238dca8
kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00210282
kernel: CR0: 80050033 CR2: 00000000 CR3: 30a28000 CR4: 00140690
kernel: Call Trace:
kernel:  crypt_ctr+0x473/0xf4e [dm_crypt]
kernel:  dm_table_add_target+0x15f/0x340 [dm_mod]
kernel:  table_load+0xe9/0x280 [dm_mod]
kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
kernel:  ctl_ioctl+0x1c8/0x400 [dm_mod]
kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
kernel:  ? ctl_ioctl+0x400/0x400 [dm_mod]
kernel:  dm_ctl_ioctl+0x8/0x10 [dm_mod]
kernel:  do_vfs_ioctl+0x3dd/0x790
kernel:  ? trace_hardirqs_on+0x4a/0xf0
kernel:  ? ksys_old_semctl+0x27/0x30
kernel:  ksys_ioctl+0x2e/0x60
kernel:  ? mpihelp_add_n+0x39/0x50
kernel:  sys_ioctl+0x11/0x20
kernel:  do_int80_syscall_32+0x4b/0x1a0
kernel:  ? mpihelp_add_n+0x39/0x50
kernel:  entry_INT80_32+0xcf/0xcf
kernel: EIP: 0xb7f5bbf2
kernel: Code: de 01 00 05 ed 73 02 00 83 ec 14 8d 80 0c ac ff ff 50 6a 02 e8 5f 12 01 00 c7 04 24 7f 00 00 00 e8 ce cd 01 00 66 90 90 cd 80 <c3> 8d b6 00 00 00 00 8d bc 27 00 00 00 00 8b 1c 24 c3 8d b6 00 00
kernel: EAX: ffffffda EBX: 00000005 ECX: c138fd09 EDX: 00511080
kernel: ESI: b7b83d40 EDI: b7b785af EBP: 0050dda0 ESP: bf9e1c34
kernel: DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00200286
kernel:  ? mpihelp_add_n+0x39/0x50
kernel: Modules linked in: dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
kernel: CR2: 0000000000000000
kernel: ---[ end trace 0d32231f952fd372 ]---

m.

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 11:01         ` Milan Broz
  0 siblings, 0 replies; 45+ messages in thread
From: Milan Broz @ 2019-06-19 11:01 UTC (permalink / raw)
  To: Ard Biesheuvel, Milan Broz
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	device-mapper development, linux-crypto

On 19/06/2019 11:14, Ard Biesheuvel wrote:
> Apologies, this was a rebase error on my part.
> 
> Could you please apply the hunk below and try again?
> 
> diff --git a/crypto/essiv.c b/crypto/essiv.c
> index 029a65afb4d7..5dc2e592077e 100644
> --- a/crypto/essiv.c
> +++ b/crypto/essiv.c
> @@ -243,6 +243,8 @@ static int essiv_aead_encrypt(struct aead_request *req)
>  static int essiv_skcipher_decrypt(struct skcipher_request *req)
>  {
>         struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +
> +       essiv_skcipher_prepare_subreq(req);
>         return crypto_skcipher_decrypt(&rctx->blockcipher_req);
>  }

That helps, but now the null cipher is broken...
(We use it for debugging and during reencryption from non-encrypted device)

Try
  cryptsetup open --type plain -c null /dev/sdd test -q
or
  dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"

(or just run full cryptsetup testsuite)

kernel: BUG: kernel NULL pointer dereference, address: 00000000
kernel: #PF: supervisor read access in kernel mode
kernel: #PF: error_code(0x0000) - not-present page
kernel: *pde = 00000000 
kernel: Oops: 0000 [#1] PREEMPT SMP
kernel: CPU: 2 PID: 2261 Comm: cryptsetup Not tainted 5.2.0-rc5+ #521
kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
kernel: EIP: strcmp+0x9/0x20
kernel: Code: 00 55 89 c1 89 e5 57 89 c7 56 89 d6 ac aa 84 c0 75 fa 5e 89 c8 5f 5d c3 8d b4 26 00 00 00 00 66 90 55 89 e5 57 89 d7 56 89 c6 <ac> ae 75 08 84 c0 75 f8 31 c0 eb 04 19 c0 0c 01 5e 5f 5d c3 8d 76
kernel: EAX: 00000000 EBX: ef51016c ECX: 0000000c EDX: f78e585e
kernel: ESI: 00000000 EDI: f78e585e EBP: f238dcb0 ESP: f238dca8
kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00210282
kernel: CR0: 80050033 CR2: 00000000 CR3: 30a28000 CR4: 00140690
kernel: Call Trace:
kernel:  crypt_ctr+0x473/0xf4e [dm_crypt]
kernel:  dm_table_add_target+0x15f/0x340 [dm_mod]
kernel:  table_load+0xe9/0x280 [dm_mod]
kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
kernel:  ctl_ioctl+0x1c8/0x400 [dm_mod]
kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
kernel:  ? ctl_ioctl+0x400/0x400 [dm_mod]
kernel:  dm_ctl_ioctl+0x8/0x10 [dm_mod]
kernel:  do_vfs_ioctl+0x3dd/0x790
kernel:  ? trace_hardirqs_on+0x4a/0xf0
kernel:  ? ksys_old_semctl+0x27/0x30
kernel:  ksys_ioctl+0x2e/0x60
kernel:  ? mpihelp_add_n+0x39/0x50
kernel:  sys_ioctl+0x11/0x20
kernel:  do_int80_syscall_32+0x4b/0x1a0
kernel:  ? mpihelp_add_n+0x39/0x50
kernel:  entry_INT80_32+0xcf/0xcf
kernel: EIP: 0xb7f5bbf2
kernel: Code: de 01 00 05 ed 73 02 00 83 ec 14 8d 80 0c ac ff ff 50 6a 02 e8 5f 12 01 00 c7 04 24 7f 00 00 00 e8 ce cd 01 00 66 90 90 cd 80 <c3> 8d b6 00 00 00 00 8d bc 27 00 00 00 00 8b 1c 24 c3 8d b6 00 00
kernel: EAX: ffffffda EBX: 00000005 ECX: c138fd09 EDX: 00511080
kernel: ESI: b7b83d40 EDI: b7b785af EBP: 0050dda0 ESP: bf9e1c34
kernel: DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00200286
kernel:  ? mpihelp_add_n+0x39/0x50
kernel: Modules linked in: dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
kernel: CR2: 0000000000000000
kernel: ---[ end trace 0d32231f952fd372 ]---

m.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
  2019-06-19 11:01         ` [dm-devel] " Milan Broz
  (?)
@ 2019-06-19 11:16           ` Ard Biesheuvel
  -1 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 11:16 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 13:01, Milan Broz <gmazyland@gmail.com> wrote:
>
> On 19/06/2019 11:14, Ard Biesheuvel wrote:
> > Apologies, this was a rebase error on my part.
> >
> > Could you please apply the hunk below and try again?
> >
> > diff --git a/crypto/essiv.c b/crypto/essiv.c
> > index 029a65afb4d7..5dc2e592077e 100644
> > --- a/crypto/essiv.c
> > +++ b/crypto/essiv.c
> > @@ -243,6 +243,8 @@ static int essiv_aead_encrypt(struct aead_request *req)
> >  static int essiv_skcipher_decrypt(struct skcipher_request *req)
> >  {
> >         struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> > +
> > +       essiv_skcipher_prepare_subreq(req);
> >         return crypto_skcipher_decrypt(&rctx->blockcipher_req);
> >  }
>
> That helps, but now the null cipher is broken...
> (We use it for debugging and during reencryption from non-encrypted device)
>

I guess that would be the cipher parsing change in the first patch.
I'll have a look.

> Try
>   cryptsetup open --type plain -c null /dev/sdd test -q
> or
>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
>
> (or just run full cryptsetup testsuite)
>

Is that your mode-test script?

I saw some errors about the null cipher, but tbh, it looked completely
unrelated to me, so i skipped those for the moment. But now, it looks
like it is related after all.


> kernel: BUG: kernel NULL pointer dereference, address: 00000000
> kernel: #PF: supervisor read access in kernel mode
> kernel: #PF: error_code(0x0000) - not-present page
> kernel: *pde = 00000000
> kernel: Oops: 0000 [#1] PREEMPT SMP
> kernel: CPU: 2 PID: 2261 Comm: cryptsetup Not tainted 5.2.0-rc5+ #521
> kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
> kernel: EIP: strcmp+0x9/0x20
> kernel: Code: 00 55 89 c1 89 e5 57 89 c7 56 89 d6 ac aa 84 c0 75 fa 5e 89 c8 5f 5d c3 8d b4 26 00 00 00 00 66 90 55 89 e5 57 89 d7 56 89 c6 <ac> ae 75 08 84 c0 75 f8 31 c0 eb 04 19 c0 0c 01 5e 5f 5d c3 8d 76
> kernel: EAX: 00000000 EBX: ef51016c ECX: 0000000c EDX: f78e585e
> kernel: ESI: 00000000 EDI: f78e585e EBP: f238dcb0 ESP: f238dca8
> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00210282
> kernel: CR0: 80050033 CR2: 00000000 CR3: 30a28000 CR4: 00140690
> kernel: Call Trace:
> kernel:  crypt_ctr+0x473/0xf4e [dm_crypt]
> kernel:  dm_table_add_target+0x15f/0x340 [dm_mod]
> kernel:  table_load+0xe9/0x280 [dm_mod]
> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
> kernel:  ctl_ioctl+0x1c8/0x400 [dm_mod]
> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
> kernel:  ? ctl_ioctl+0x400/0x400 [dm_mod]
> kernel:  dm_ctl_ioctl+0x8/0x10 [dm_mod]
> kernel:  do_vfs_ioctl+0x3dd/0x790
> kernel:  ? trace_hardirqs_on+0x4a/0xf0
> kernel:  ? ksys_old_semctl+0x27/0x30
> kernel:  ksys_ioctl+0x2e/0x60
> kernel:  ? mpihelp_add_n+0x39/0x50
> kernel:  sys_ioctl+0x11/0x20
> kernel:  do_int80_syscall_32+0x4b/0x1a0
> kernel:  ? mpihelp_add_n+0x39/0x50
> kernel:  entry_INT80_32+0xcf/0xcf
> kernel: EIP: 0xb7f5bbf2
> kernel: Code: de 01 00 05 ed 73 02 00 83 ec 14 8d 80 0c ac ff ff 50 6a 02 e8 5f 12 01 00 c7 04 24 7f 00 00 00 e8 ce cd 01 00 66 90 90 cd 80 <c3> 8d b6 00 00 00 00 8d bc 27 00 00 00 00 8b 1c 24 c3 8d b6 00 00
> kernel: EAX: ffffffda EBX: 00000005 ECX: c138fd09 EDX: 00511080
> kernel: ESI: b7b83d40 EDI: b7b785af EBP: 0050dda0 ESP: bf9e1c34
> kernel: DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00200286
> kernel:  ? mpihelp_add_n+0x39/0x50
> kernel: Modules linked in: dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
> kernel: CR2: 0000000000000000
> kernel: ---[ end trace 0d32231f952fd372 ]---
>
> m.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 11:16           ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 11:16 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 13:01, Milan Broz <gmazyland@gmail.com> wrote:
>
> On 19/06/2019 11:14, Ard Biesheuvel wrote:
> > Apologies, this was a rebase error on my part.
> >
> > Could you please apply the hunk below and try again?
> >
> > diff --git a/crypto/essiv.c b/crypto/essiv.c
> > index 029a65afb4d7..5dc2e592077e 100644
> > --- a/crypto/essiv.c
> > +++ b/crypto/essiv.c
> > @@ -243,6 +243,8 @@ static int essiv_aead_encrypt(struct aead_request *req)
> >  static int essiv_skcipher_decrypt(struct skcipher_request *req)
> >  {
> >         struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> > +
> > +       essiv_skcipher_prepare_subreq(req);
> >         return crypto_skcipher_decrypt(&rctx->blockcipher_req);
> >  }
>
> That helps, but now the null cipher is broken...
> (We use it for debugging and during reencryption from non-encrypted device)
>

I guess that would be the cipher parsing change in the first patch.
I'll have a look.

> Try
>   cryptsetup open --type plain -c null /dev/sdd test -q
> or
>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
>
> (or just run full cryptsetup testsuite)
>

Is that your mode-test script?

I saw some errors about the null cipher, but tbh, it looked completely
unrelated to me, so i skipped those for the moment. But now, it looks
like it is related after all.


> kernel: BUG: kernel NULL pointer dereference, address: 00000000
> kernel: #PF: supervisor read access in kernel mode
> kernel: #PF: error_code(0x0000) - not-present page
> kernel: *pde = 00000000
> kernel: Oops: 0000 [#1] PREEMPT SMP
> kernel: CPU: 2 PID: 2261 Comm: cryptsetup Not tainted 5.2.0-rc5+ #521
> kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
> kernel: EIP: strcmp+0x9/0x20
> kernel: Code: 00 55 89 c1 89 e5 57 89 c7 56 89 d6 ac aa 84 c0 75 fa 5e 89 c8 5f 5d c3 8d b4 26 00 00 00 00 66 90 55 89 e5 57 89 d7 56 89 c6 <ac> ae 75 08 84 c0 75 f8 31 c0 eb 04 19 c0 0c 01 5e 5f 5d c3 8d 76
> kernel: EAX: 00000000 EBX: ef51016c ECX: 0000000c EDX: f78e585e
> kernel: ESI: 00000000 EDI: f78e585e EBP: f238dcb0 ESP: f238dca8
> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00210282
> kernel: CR0: 80050033 CR2: 00000000 CR3: 30a28000 CR4: 00140690
> kernel: Call Trace:
> kernel:  crypt_ctr+0x473/0xf4e [dm_crypt]
> kernel:  dm_table_add_target+0x15f/0x340 [dm_mod]
> kernel:  table_load+0xe9/0x280 [dm_mod]
> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
> kernel:  ctl_ioctl+0x1c8/0x400 [dm_mod]
> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
> kernel:  ? ctl_ioctl+0x400/0x400 [dm_mod]
> kernel:  dm_ctl_ioctl+0x8/0x10 [dm_mod]
> kernel:  do_vfs_ioctl+0x3dd/0x790
> kernel:  ? trace_hardirqs_on+0x4a/0xf0
> kernel:  ? ksys_old_semctl+0x27/0x30
> kernel:  ksys_ioctl+0x2e/0x60
> kernel:  ? mpihelp_add_n+0x39/0x50
> kernel:  sys_ioctl+0x11/0x20
> kernel:  do_int80_syscall_32+0x4b/0x1a0
> kernel:  ? mpihelp_add_n+0x39/0x50
> kernel:  entry_INT80_32+0xcf/0xcf
> kernel: EIP: 0xb7f5bbf2
> kernel: Code: de 01 00 05 ed 73 02 00 83 ec 14 8d 80 0c ac ff ff 50 6a 02 e8 5f 12 01 00 c7 04 24 7f 00 00 00 e8 ce cd 01 00 66 90 90 cd 80 <c3> 8d b6 00 00 00 00 8d bc 27 00 00 00 00 8b 1c 24 c3 8d b6 00 00
> kernel: EAX: ffffffda EBX: 00000005 ECX: c138fd09 EDX: 00511080
> kernel: ESI: b7b83d40 EDI: b7b785af EBP: 0050dda0 ESP: bf9e1c34
> kernel: DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00200286
> kernel:  ? mpihelp_add_n+0x39/0x50
> kernel: Modules linked in: dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
> kernel: CR2: 0000000000000000
> kernel: ---[ end trace 0d32231f952fd372 ]---
>
> m.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 11:16           ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 11:16 UTC (permalink / raw)
  To: Milan Broz
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	device-mapper development,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE

On Wed, 19 Jun 2019 at 13:01, Milan Broz <gmazyland@gmail.com> wrote:
>
> On 19/06/2019 11:14, Ard Biesheuvel wrote:
> > Apologies, this was a rebase error on my part.
> >
> > Could you please apply the hunk below and try again?
> >
> > diff --git a/crypto/essiv.c b/crypto/essiv.c
> > index 029a65afb4d7..5dc2e592077e 100644
> > --- a/crypto/essiv.c
> > +++ b/crypto/essiv.c
> > @@ -243,6 +243,8 @@ static int essiv_aead_encrypt(struct aead_request *req)
> >  static int essiv_skcipher_decrypt(struct skcipher_request *req)
> >  {
> >         struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> > +
> > +       essiv_skcipher_prepare_subreq(req);
> >         return crypto_skcipher_decrypt(&rctx->blockcipher_req);
> >  }
>
> That helps, but now the null cipher is broken...
> (We use it for debugging and during reencryption from non-encrypted device)
>

I guess that would be the cipher parsing change in the first patch.
I'll have a look.

> Try
>   cryptsetup open --type plain -c null /dev/sdd test -q
> or
>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
>
> (or just run full cryptsetup testsuite)
>

Is that your mode-test script?

I saw some errors about the null cipher, but tbh, it looked completely
unrelated to me, so i skipped those for the moment. But now, it looks
like it is related after all.


> kernel: BUG: kernel NULL pointer dereference, address: 00000000
> kernel: #PF: supervisor read access in kernel mode
> kernel: #PF: error_code(0x0000) - not-present page
> kernel: *pde = 00000000
> kernel: Oops: 0000 [#1] PREEMPT SMP
> kernel: CPU: 2 PID: 2261 Comm: cryptsetup Not tainted 5.2.0-rc5+ #521
> kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
> kernel: EIP: strcmp+0x9/0x20
> kernel: Code: 00 55 89 c1 89 e5 57 89 c7 56 89 d6 ac aa 84 c0 75 fa 5e 89 c8 5f 5d c3 8d b4 26 00 00 00 00 66 90 55 89 e5 57 89 d7 56 89 c6 <ac> ae 75 08 84 c0 75 f8 31 c0 eb 04 19 c0 0c 01 5e 5f 5d c3 8d 76
> kernel: EAX: 00000000 EBX: ef51016c ECX: 0000000c EDX: f78e585e
> kernel: ESI: 00000000 EDI: f78e585e EBP: f238dcb0 ESP: f238dca8
> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00210282
> kernel: CR0: 80050033 CR2: 00000000 CR3: 30a28000 CR4: 00140690
> kernel: Call Trace:
> kernel:  crypt_ctr+0x473/0xf4e [dm_crypt]
> kernel:  dm_table_add_target+0x15f/0x340 [dm_mod]
> kernel:  table_load+0xe9/0x280 [dm_mod]
> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
> kernel:  ctl_ioctl+0x1c8/0x400 [dm_mod]
> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
> kernel:  ? ctl_ioctl+0x400/0x400 [dm_mod]
> kernel:  dm_ctl_ioctl+0x8/0x10 [dm_mod]
> kernel:  do_vfs_ioctl+0x3dd/0x790
> kernel:  ? trace_hardirqs_on+0x4a/0xf0
> kernel:  ? ksys_old_semctl+0x27/0x30
> kernel:  ksys_ioctl+0x2e/0x60
> kernel:  ? mpihelp_add_n+0x39/0x50
> kernel:  sys_ioctl+0x11/0x20
> kernel:  do_int80_syscall_32+0x4b/0x1a0
> kernel:  ? mpihelp_add_n+0x39/0x50
> kernel:  entry_INT80_32+0xcf/0xcf
> kernel: EIP: 0xb7f5bbf2
> kernel: Code: de 01 00 05 ed 73 02 00 83 ec 14 8d 80 0c ac ff ff 50 6a 02 e8 5f 12 01 00 c7 04 24 7f 00 00 00 e8 ce cd 01 00 66 90 90 cd 80 <c3> 8d b6 00 00 00 00 8d bc 27 00 00 00 00 8b 1c 24 c3 8d b6 00 00
> kernel: EAX: ffffffda EBX: 00000005 ECX: c138fd09 EDX: 00511080
> kernel: ESI: b7b83d40 EDI: b7b785af EBP: 0050dda0 ESP: bf9e1c34
> kernel: DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00200286
> kernel:  ? mpihelp_add_n+0x39/0x50
> kernel: Modules linked in: dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
> kernel: CR2: 0000000000000000
> kernel: ---[ end trace 0d32231f952fd372 ]---
>
> m.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
  2019-06-19 11:16           ` Ard Biesheuvel
  (?)
@ 2019-06-19 11:33             ` Milan Broz
  -1 siblings, 0 replies; 45+ messages in thread
From: Milan Broz @ 2019-06-19 11:33 UTC (permalink / raw)
  To: Ard Biesheuvel, Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On 19/06/2019 13:16, Ard Biesheuvel wrote:
>> Try
>>   cryptsetup open --type plain -c null /dev/sdd test -q
>> or
>>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
>>
>> (or just run full cryptsetup testsuite)
>>
> 
> Is that your mode-test script?
> 
> I saw some errors about the null cipher, but tbh, it looked completely
> unrelated to me, so i skipped those for the moment. But now, it looks
> like it is related after all.

This was triggered by align-test, mode-test fails the same though.

It is definitely related, I think you just changed the mode parsing in dm-crypt.
(cipher null contains only one dash I guess).

m.


> 
> 
>> kernel: BUG: kernel NULL pointer dereference, address: 00000000
>> kernel: #PF: supervisor read access in kernel mode
>> kernel: #PF: error_code(0x0000) - not-present page
>> kernel: *pde = 00000000
>> kernel: Oops: 0000 [#1] PREEMPT SMP
>> kernel: CPU: 2 PID: 2261 Comm: cryptsetup Not tainted 5.2.0-rc5+ #521
>> kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
>> kernel: EIP: strcmp+0x9/0x20
>> kernel: Code: 00 55 89 c1 89 e5 57 89 c7 56 89 d6 ac aa 84 c0 75 fa 5e 89 c8 5f 5d c3 8d b4 26 00 00 00 00 66 90 55 89 e5 57 89 d7 56 89 c6 <ac> ae 75 08 84 c0 75 f8 31 c0 eb 04 19 c0 0c 01 5e 5f 5d c3 8d 76
>> kernel: EAX: 00000000 EBX: ef51016c ECX: 0000000c EDX: f78e585e
>> kernel: ESI: 00000000 EDI: f78e585e EBP: f238dcb0 ESP: f238dca8
>> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00210282
>> kernel: CR0: 80050033 CR2: 00000000 CR3: 30a28000 CR4: 00140690
>> kernel: Call Trace:
>> kernel:  crypt_ctr+0x473/0xf4e [dm_crypt]
>> kernel:  dm_table_add_target+0x15f/0x340 [dm_mod]
>> kernel:  table_load+0xe9/0x280 [dm_mod]
>> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
>> kernel:  ctl_ioctl+0x1c8/0x400 [dm_mod]
>> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
>> kernel:  ? ctl_ioctl+0x400/0x400 [dm_mod]
>> kernel:  dm_ctl_ioctl+0x8/0x10 [dm_mod]
>> kernel:  do_vfs_ioctl+0x3dd/0x790
>> kernel:  ? trace_hardirqs_on+0x4a/0xf0
>> kernel:  ? ksys_old_semctl+0x27/0x30
>> kernel:  ksys_ioctl+0x2e/0x60
>> kernel:  ? mpihelp_add_n+0x39/0x50
>> kernel:  sys_ioctl+0x11/0x20
>> kernel:  do_int80_syscall_32+0x4b/0x1a0
>> kernel:  ? mpihelp_add_n+0x39/0x50
>> kernel:  entry_INT80_32+0xcf/0xcf
>> kernel: EIP: 0xb7f5bbf2
>> kernel: Code: de 01 00 05 ed 73 02 00 83 ec 14 8d 80 0c ac ff ff 50 6a 02 e8 5f 12 01 00 c7 04 24 7f 00 00 00 e8 ce cd 01 00 66 90 90 cd 80 <c3> 8d b6 00 00 00 00 8d bc 27 00 00 00 00 8b 1c 24 c3 8d b6 00 00
>> kernel: EAX: ffffffda EBX: 00000005 ECX: c138fd09 EDX: 00511080
>> kernel: ESI: b7b83d40 EDI: b7b785af EBP: 0050dda0 ESP: bf9e1c34
>> kernel: DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00200286
>> kernel:  ? mpihelp_add_n+0x39/0x50
>> kernel: Modules linked in: dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
>> kernel: CR2: 0000000000000000
>> kernel: ---[ end trace 0d32231f952fd372 ]---
>>
>> m.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 11:33             ` Milan Broz
  0 siblings, 0 replies; 45+ messages in thread
From: Milan Broz @ 2019-06-19 11:33 UTC (permalink / raw)
  To: Ard Biesheuvel, Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On 19/06/2019 13:16, Ard Biesheuvel wrote:
>> Try
>>   cryptsetup open --type plain -c null /dev/sdd test -q
>> or
>>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
>>
>> (or just run full cryptsetup testsuite)
>>
> 
> Is that your mode-test script?
> 
> I saw some errors about the null cipher, but tbh, it looked completely
> unrelated to me, so i skipped those for the moment. But now, it looks
> like it is related after all.

This was triggered by align-test, mode-test fails the same though.

It is definitely related, I think you just changed the mode parsing in dm-crypt.
(cipher null contains only one dash I guess).

m.


> 
> 
>> kernel: BUG: kernel NULL pointer dereference, address: 00000000
>> kernel: #PF: supervisor read access in kernel mode
>> kernel: #PF: error_code(0x0000) - not-present page
>> kernel: *pde = 00000000
>> kernel: Oops: 0000 [#1] PREEMPT SMP
>> kernel: CPU: 2 PID: 2261 Comm: cryptsetup Not tainted 5.2.0-rc5+ #521
>> kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
>> kernel: EIP: strcmp+0x9/0x20
>> kernel: Code: 00 55 89 c1 89 e5 57 89 c7 56 89 d6 ac aa 84 c0 75 fa 5e 89 c8 5f 5d c3 8d b4 26 00 00 00 00 66 90 55 89 e5 57 89 d7 56 89 c6 <ac> ae 75 08 84 c0 75 f8 31 c0 eb 04 19 c0 0c 01 5e 5f 5d c3 8d 76
>> kernel: EAX: 00000000 EBX: ef51016c ECX: 0000000c EDX: f78e585e
>> kernel: ESI: 00000000 EDI: f78e585e EBP: f238dcb0 ESP: f238dca8
>> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00210282
>> kernel: CR0: 80050033 CR2: 00000000 CR3: 30a28000 CR4: 00140690
>> kernel: Call Trace:
>> kernel:  crypt_ctr+0x473/0xf4e [dm_crypt]
>> kernel:  dm_table_add_target+0x15f/0x340 [dm_mod]
>> kernel:  table_load+0xe9/0x280 [dm_mod]
>> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
>> kernel:  ctl_ioctl+0x1c8/0x400 [dm_mod]
>> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
>> kernel:  ? ctl_ioctl+0x400/0x400 [dm_mod]
>> kernel:  dm_ctl_ioctl+0x8/0x10 [dm_mod]
>> kernel:  do_vfs_ioctl+0x3dd/0x790
>> kernel:  ? trace_hardirqs_on+0x4a/0xf0
>> kernel:  ? ksys_old_semctl+0x27/0x30
>> kernel:  ksys_ioctl+0x2e/0x60
>> kernel:  ? mpihelp_add_n+0x39/0x50
>> kernel:  sys_ioctl+0x11/0x20
>> kernel:  do_int80_syscall_32+0x4b/0x1a0
>> kernel:  ? mpihelp_add_n+0x39/0x50
>> kernel:  entry_INT80_32+0xcf/0xcf
>> kernel: EIP: 0xb7f5bbf2
>> kernel: Code: de 01 00 05 ed 73 02 00 83 ec 14 8d 80 0c ac ff ff 50 6a 02 e8 5f 12 01 00 c7 04 24 7f 00 00 00 e8 ce cd 01 00 66 90 90 cd 80 <c3> 8d b6 00 00 00 00 8d bc 27 00 00 00 00 8b 1c 24 c3 8d b6 00 00
>> kernel: EAX: ffffffda EBX: 00000005 ECX: c138fd09 EDX: 00511080
>> kernel: ESI: b7b83d40 EDI: b7b785af EBP: 0050dda0 ESP: bf9e1c34
>> kernel: DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00200286
>> kernel:  ? mpihelp_add_n+0x39/0x50
>> kernel: Modules linked in: dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
>> kernel: CR2: 0000000000000000
>> kernel: ---[ end trace 0d32231f952fd372 ]---
>>
>> m.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 11:33             ` Milan Broz
  0 siblings, 0 replies; 45+ messages in thread
From: Milan Broz @ 2019-06-19 11:33 UTC (permalink / raw)
  To: Ard Biesheuvel, Milan Broz
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	device-mapper development,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE

On 19/06/2019 13:16, Ard Biesheuvel wrote:
>> Try
>>   cryptsetup open --type plain -c null /dev/sdd test -q
>> or
>>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
>>
>> (or just run full cryptsetup testsuite)
>>
> 
> Is that your mode-test script?
> 
> I saw some errors about the null cipher, but tbh, it looked completely
> unrelated to me, so i skipped those for the moment. But now, it looks
> like it is related after all.

This was triggered by align-test, mode-test fails the same though.

It is definitely related, I think you just changed the mode parsing in dm-crypt.
(cipher null contains only one dash I guess).

m.


> 
> 
>> kernel: BUG: kernel NULL pointer dereference, address: 00000000
>> kernel: #PF: supervisor read access in kernel mode
>> kernel: #PF: error_code(0x0000) - not-present page
>> kernel: *pde = 00000000
>> kernel: Oops: 0000 [#1] PREEMPT SMP
>> kernel: CPU: 2 PID: 2261 Comm: cryptsetup Not tainted 5.2.0-rc5+ #521
>> kernel: Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/13/2018
>> kernel: EIP: strcmp+0x9/0x20
>> kernel: Code: 00 55 89 c1 89 e5 57 89 c7 56 89 d6 ac aa 84 c0 75 fa 5e 89 c8 5f 5d c3 8d b4 26 00 00 00 00 66 90 55 89 e5 57 89 d7 56 89 c6 <ac> ae 75 08 84 c0 75 f8 31 c0 eb 04 19 c0 0c 01 5e 5f 5d c3 8d 76
>> kernel: EAX: 00000000 EBX: ef51016c ECX: 0000000c EDX: f78e585e
>> kernel: ESI: 00000000 EDI: f78e585e EBP: f238dcb0 ESP: f238dca8
>> kernel: DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068 EFLAGS: 00210282
>> kernel: CR0: 80050033 CR2: 00000000 CR3: 30a28000 CR4: 00140690
>> kernel: Call Trace:
>> kernel:  crypt_ctr+0x473/0xf4e [dm_crypt]
>> kernel:  dm_table_add_target+0x15f/0x340 [dm_mod]
>> kernel:  table_load+0xe9/0x280 [dm_mod]
>> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
>> kernel:  ctl_ioctl+0x1c8/0x400 [dm_mod]
>> kernel:  ? retrieve_status+0x200/0x200 [dm_mod]
>> kernel:  ? ctl_ioctl+0x400/0x400 [dm_mod]
>> kernel:  dm_ctl_ioctl+0x8/0x10 [dm_mod]
>> kernel:  do_vfs_ioctl+0x3dd/0x790
>> kernel:  ? trace_hardirqs_on+0x4a/0xf0
>> kernel:  ? ksys_old_semctl+0x27/0x30
>> kernel:  ksys_ioctl+0x2e/0x60
>> kernel:  ? mpihelp_add_n+0x39/0x50
>> kernel:  sys_ioctl+0x11/0x20
>> kernel:  do_int80_syscall_32+0x4b/0x1a0
>> kernel:  ? mpihelp_add_n+0x39/0x50
>> kernel:  entry_INT80_32+0xcf/0xcf
>> kernel: EIP: 0xb7f5bbf2
>> kernel: Code: de 01 00 05 ed 73 02 00 83 ec 14 8d 80 0c ac ff ff 50 6a 02 e8 5f 12 01 00 c7 04 24 7f 00 00 00 e8 ce cd 01 00 66 90 90 cd 80 <c3> 8d b6 00 00 00 00 8d bc 27 00 00 00 00 8b 1c 24 c3 8d b6 00 00
>> kernel: EAX: ffffffda EBX: 00000005 ECX: c138fd09 EDX: 00511080
>> kernel: ESI: b7b83d40 EDI: b7b785af EBP: 0050dda0 ESP: bf9e1c34
>> kernel: DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00200286
>> kernel:  ? mpihelp_add_n+0x39/0x50
>> kernel: Modules linked in: dm_crypt loop dm_mod pktcdvd crc32_pclmul crc32c_intel aesni_intel aes_i586 crypto_simd cryptd ata_piix
>> kernel: CR2: 0000000000000000
>> kernel: ---[ end trace 0d32231f952fd372 ]---
>>
>> m.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
  2019-06-19 11:33             ` Milan Broz
  (?)
@ 2019-06-19 12:36               ` Ard Biesheuvel
  -1 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 12:36 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
>
> On 19/06/2019 13:16, Ard Biesheuvel wrote:
> >> Try
> >>   cryptsetup open --type plain -c null /dev/sdd test -q
> >> or
> >>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
> >>
> >> (or just run full cryptsetup testsuite)
> >>
> >
> > Is that your mode-test script?
> >
> > I saw some errors about the null cipher, but tbh, it looked completely
> > unrelated to me, so i skipped those for the moment. But now, it looks
> > like it is related after all.
>
> This was triggered by align-test, mode-test fails the same though.
>
> It is definitely related, I think you just changed the mode parsing in dm-crypt.
> (cipher null contains only one dash I guess).
>

On my unpatched 4.19 kernel, mode-test gives me

$ sudo ./mode-test
aes                            PLAIN:[table OK][status OK]
LUKS1:[table OK][status OK] CHECKSUM:[OK]
aes-plain                      PLAIN:[table OK][status OK]
LUKS1:[table OK][status OK] CHECKSUM:[OK]
null                           PLAIN:[table OK][status OK]
LUKS1:[table OK][status OK] CHECKSUM:[OK]
cipher_null                    PLAIN:[table FAIL]
 Expecting cipher_null-ecb got cipher_null-cbc-plain.
FAILED at line 64 ./mode-test

which is why I commented out those tests in the first place.

I can reproduce the crash after I re-enable them again, so I will need
to look into that. But something seems to be broken already.
Note that this is running on arm64 using a kconfig based on the Debian kernel.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 12:36               ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 12:36 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
>
> On 19/06/2019 13:16, Ard Biesheuvel wrote:
> >> Try
> >>   cryptsetup open --type plain -c null /dev/sdd test -q
> >> or
> >>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
> >>
> >> (or just run full cryptsetup testsuite)
> >>
> >
> > Is that your mode-test script?
> >
> > I saw some errors about the null cipher, but tbh, it looked completely
> > unrelated to me, so i skipped those for the moment. But now, it looks
> > like it is related after all.
>
> This was triggered by align-test, mode-test fails the same though.
>
> It is definitely related, I think you just changed the mode parsing in dm-crypt.
> (cipher null contains only one dash I guess).
>

On my unpatched 4.19 kernel, mode-test gives me

$ sudo ./mode-test
aes                            PLAIN:[table OK][status OK]
LUKS1:[table OK][status OK] CHECKSUM:[OK]
aes-plain                      PLAIN:[table OK][status OK]
LUKS1:[table OK][status OK] CHECKSUM:[OK]
null                           PLAIN:[table OK][status OK]
LUKS1:[table OK][status OK] CHECKSUM:[OK]
cipher_null                    PLAIN:[table FAIL]
 Expecting cipher_null-ecb got cipher_null-cbc-plain.
FAILED at line 64 ./mode-test

which is why I commented out those tests in the first place.

I can reproduce the crash after I re-enable them again, so I will need
to look into that. But something seems to be broken already.
Note that this is running on arm64 using a kconfig based on the Debian kernel.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 12:36               ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 12:36 UTC (permalink / raw)
  To: Milan Broz
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	device-mapper development,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE

On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
>
> On 19/06/2019 13:16, Ard Biesheuvel wrote:
> >> Try
> >>   cryptsetup open --type plain -c null /dev/sdd test -q
> >> or
> >>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
> >>
> >> (or just run full cryptsetup testsuite)
> >>
> >
> > Is that your mode-test script?
> >
> > I saw some errors about the null cipher, but tbh, it looked completely
> > unrelated to me, so i skipped those for the moment. But now, it looks
> > like it is related after all.
>
> This was triggered by align-test, mode-test fails the same though.
>
> It is definitely related, I think you just changed the mode parsing in dm-crypt.
> (cipher null contains only one dash I guess).
>

On my unpatched 4.19 kernel, mode-test gives me

$ sudo ./mode-test
aes                            PLAIN:[table OK][status OK]
LUKS1:[table OK][status OK] CHECKSUM:[OK]
aes-plain                      PLAIN:[table OK][status OK]
LUKS1:[table OK][status OK] CHECKSUM:[OK]
null                           PLAIN:[table OK][status OK]
LUKS1:[table OK][status OK] CHECKSUM:[OK]
cipher_null                    PLAIN:[table FAIL]
 Expecting cipher_null-ecb got cipher_null-cbc-plain.
FAILED at line 64 ./mode-test

which is why I commented out those tests in the first place.

I can reproduce the crash after I re-enable them again, so I will need
to look into that. But something seems to be broken already.
Note that this is running on arm64 using a kconfig based on the Debian kernel.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
  2019-06-19 12:36               ` Ard Biesheuvel
  (?)
@ 2019-06-19 12:49                 ` Ard Biesheuvel
  -1 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 12:49 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 14:36, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>
> On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
> >
> > On 19/06/2019 13:16, Ard Biesheuvel wrote:
> > >> Try
> > >>   cryptsetup open --type plain -c null /dev/sdd test -q
> > >> or
> > >>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
> > >>
> > >> (or just run full cryptsetup testsuite)
> > >>
> > >
> > > Is that your mode-test script?
> > >
> > > I saw some errors about the null cipher, but tbh, it looked completely
> > > unrelated to me, so i skipped those for the moment. But now, it looks
> > > like it is related after all.
> >
> > This was triggered by align-test, mode-test fails the same though.
> >
> > It is definitely related, I think you just changed the mode parsing in dm-crypt.
> > (cipher null contains only one dash I guess).
> >
>
> On my unpatched 4.19 kernel, mode-test gives me
>
> $ sudo ./mode-test
> aes                            PLAIN:[table OK][status OK]
> LUKS1:[table OK][status OK] CHECKSUM:[OK]
> aes-plain                      PLAIN:[table OK][status OK]
> LUKS1:[table OK][status OK] CHECKSUM:[OK]
> null                           PLAIN:[table OK][status OK]
> LUKS1:[table OK][status OK] CHECKSUM:[OK]
> cipher_null                    PLAIN:[table FAIL]
>  Expecting cipher_null-ecb got cipher_null-cbc-plain.
> FAILED at line 64 ./mode-test
>
> which is why I commented out those tests in the first place.
>
> I can reproduce the crash after I re-enable them again, so I will need
> to look into that. But something seems to be broken already.
> Note that this is running on arm64 using a kconfig based on the Debian kernel.

Actually, could this be an issue with cryptsetup being out of date? On
another arm64 system with a more recent distro, it works fine

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 12:49                 ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 12:49 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 14:36, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>
> On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
> >
> > On 19/06/2019 13:16, Ard Biesheuvel wrote:
> > >> Try
> > >>   cryptsetup open --type plain -c null /dev/sdd test -q
> > >> or
> > >>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
> > >>
> > >> (or just run full cryptsetup testsuite)
> > >>
> > >
> > > Is that your mode-test script?
> > >
> > > I saw some errors about the null cipher, but tbh, it looked completely
> > > unrelated to me, so i skipped those for the moment. But now, it looks
> > > like it is related after all.
> >
> > This was triggered by align-test, mode-test fails the same though.
> >
> > It is definitely related, I think you just changed the mode parsing in dm-crypt.
> > (cipher null contains only one dash I guess).
> >
>
> On my unpatched 4.19 kernel, mode-test gives me
>
> $ sudo ./mode-test
> aes                            PLAIN:[table OK][status OK]
> LUKS1:[table OK][status OK] CHECKSUM:[OK]
> aes-plain                      PLAIN:[table OK][status OK]
> LUKS1:[table OK][status OK] CHECKSUM:[OK]
> null                           PLAIN:[table OK][status OK]
> LUKS1:[table OK][status OK] CHECKSUM:[OK]
> cipher_null                    PLAIN:[table FAIL]
>  Expecting cipher_null-ecb got cipher_null-cbc-plain.
> FAILED at line 64 ./mode-test
>
> which is why I commented out those tests in the first place.
>
> I can reproduce the crash after I re-enable them again, so I will need
> to look into that. But something seems to be broken already.
> Note that this is running on arm64 using a kconfig based on the Debian kernel.

Actually, could this be an issue with cryptsetup being out of date? On
another arm64 system with a more recent distro, it works fine

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 12:49                 ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 12:49 UTC (permalink / raw)
  To: Milan Broz
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	device-mapper development,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE

On Wed, 19 Jun 2019 at 14:36, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>
> On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
> >
> > On 19/06/2019 13:16, Ard Biesheuvel wrote:
> > >> Try
> > >>   cryptsetup open --type plain -c null /dev/sdd test -q
> > >> or
> > >>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
> > >>
> > >> (or just run full cryptsetup testsuite)
> > >>
> > >
> > > Is that your mode-test script?
> > >
> > > I saw some errors about the null cipher, but tbh, it looked completely
> > > unrelated to me, so i skipped those for the moment. But now, it looks
> > > like it is related after all.
> >
> > This was triggered by align-test, mode-test fails the same though.
> >
> > It is definitely related, I think you just changed the mode parsing in dm-crypt.
> > (cipher null contains only one dash I guess).
> >
>
> On my unpatched 4.19 kernel, mode-test gives me
>
> $ sudo ./mode-test
> aes                            PLAIN:[table OK][status OK]
> LUKS1:[table OK][status OK] CHECKSUM:[OK]
> aes-plain                      PLAIN:[table OK][status OK]
> LUKS1:[table OK][status OK] CHECKSUM:[OK]
> null                           PLAIN:[table OK][status OK]
> LUKS1:[table OK][status OK] CHECKSUM:[OK]
> cipher_null                    PLAIN:[table FAIL]
>  Expecting cipher_null-ecb got cipher_null-cbc-plain.
> FAILED at line 64 ./mode-test
>
> which is why I commented out those tests in the first place.
>
> I can reproduce the crash after I re-enable them again, so I will need
> to look into that. But something seems to be broken already.
> Note that this is running on arm64 using a kconfig based on the Debian kernel.

Actually, could this be an issue with cryptsetup being out of date? On
another arm64 system with a more recent distro, it works fine

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
  2019-06-19 12:49                 ` Ard Biesheuvel
  (?)
@ 2019-06-19 13:08                   ` Milan Broz
  -1 siblings, 0 replies; 45+ messages in thread
From: Milan Broz @ 2019-06-19 13:08 UTC (permalink / raw)
  To: Ard Biesheuvel, Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On 19/06/2019 14:49, Ard Biesheuvel wrote:
> On Wed, 19 Jun 2019 at 14:36, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>>
>> On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
>>>
>>> On 19/06/2019 13:16, Ard Biesheuvel wrote:
>>>>> Try
>>>>>   cryptsetup open --type plain -c null /dev/sdd test -q
>>>>> or
>>>>>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
>>>>>
>>>>> (or just run full cryptsetup testsuite)
>>>>>
>>>>
>>>> Is that your mode-test script?
>>>>
>>>> I saw some errors about the null cipher, but tbh, it looked completely
>>>> unrelated to me, so i skipped those for the moment. But now, it looks
>>>> like it is related after all.
>>>
>>> This was triggered by align-test, mode-test fails the same though.
>>>
>>> It is definitely related, I think you just changed the mode parsing in dm-crypt.
>>> (cipher null contains only one dash I guess).
>>>
>>
>> On my unpatched 4.19 kernel, mode-test gives me
>>
>> $ sudo ./mode-test
>> aes                            PLAIN:[table OK][status OK]
>> LUKS1:[table OK][status OK] CHECKSUM:[OK]
>> aes-plain                      PLAIN:[table OK][status OK]
>> LUKS1:[table OK][status OK] CHECKSUM:[OK]
>> null                           PLAIN:[table OK][status OK]
>> LUKS1:[table OK][status OK] CHECKSUM:[OK]
>> cipher_null                    PLAIN:[table FAIL]
>>  Expecting cipher_null-ecb got cipher_null-cbc-plain.
>> FAILED at line 64 ./mode-test
>>
>> which is why I commented out those tests in the first place.
>>
>> I can reproduce the crash after I re-enable them again, so I will need
>> to look into that. But something seems to be broken already.
>> Note that this is running on arm64 using a kconfig based on the Debian kernel.
> 
> Actually, could this be an issue with cryptsetup being out of date? On
> another arm64 system with a more recent distro, it works fine

Ah yes, it was changed because we hardened dm-crypt mode validation in kernel
https://gitlab.com/cryptsetup/cryptsetup/commit/aeea93fa9553ad70ed57f273aecb233113b204d6#f40cab3037a50bf28ce20d8aae52bfa6a0c0e2c4_137_137

So either use test form the released version of cryptsetup (all version are here)
https://mirrors.edge.kernel.org/pub/linux/utils/cryptsetup/

Or better use upstream git, we added a lot of tests anyway.

Milan



^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 13:08                   ` Milan Broz
  0 siblings, 0 replies; 45+ messages in thread
From: Milan Broz @ 2019-06-19 13:08 UTC (permalink / raw)
  To: Ard Biesheuvel, Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On 19/06/2019 14:49, Ard Biesheuvel wrote:
> On Wed, 19 Jun 2019 at 14:36, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>>
>> On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
>>>
>>> On 19/06/2019 13:16, Ard Biesheuvel wrote:
>>>>> Try
>>>>>   cryptsetup open --type plain -c null /dev/sdd test -q
>>>>> or
>>>>>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
>>>>>
>>>>> (or just run full cryptsetup testsuite)
>>>>>
>>>>
>>>> Is that your mode-test script?
>>>>
>>>> I saw some errors about the null cipher, but tbh, it looked completely
>>>> unrelated to me, so i skipped those for the moment. But now, it looks
>>>> like it is related after all.
>>>
>>> This was triggered by align-test, mode-test fails the same though.
>>>
>>> It is definitely related, I think you just changed the mode parsing in dm-crypt.
>>> (cipher null contains only one dash I guess).
>>>
>>
>> On my unpatched 4.19 kernel, mode-test gives me
>>
>> $ sudo ./mode-test
>> aes                            PLAIN:[table OK][status OK]
>> LUKS1:[table OK][status OK] CHECKSUM:[OK]
>> aes-plain                      PLAIN:[table OK][status OK]
>> LUKS1:[table OK][status OK] CHECKSUM:[OK]
>> null                           PLAIN:[table OK][status OK]
>> LUKS1:[table OK][status OK] CHECKSUM:[OK]
>> cipher_null                    PLAIN:[table FAIL]
>>  Expecting cipher_null-ecb got cipher_null-cbc-plain.
>> FAILED at line 64 ./mode-test
>>
>> which is why I commented out those tests in the first place.
>>
>> I can reproduce the crash after I re-enable them again, so I will need
>> to look into that. But something seems to be broken already.
>> Note that this is running on arm64 using a kconfig based on the Debian kernel.
> 
> Actually, could this be an issue with cryptsetup being out of date? On
> another arm64 system with a more recent distro, it works fine

Ah yes, it was changed because we hardened dm-crypt mode validation in kernel
https://gitlab.com/cryptsetup/cryptsetup/commit/aeea93fa9553ad70ed57f273aecb233113b204d6#f40cab3037a50bf28ce20d8aae52bfa6a0c0e2c4_137_137

So either use test form the released version of cryptsetup (all version are here)
https://mirrors.edge.kernel.org/pub/linux/utils/cryptsetup/

Or better use upstream git, we added a lot of tests anyway.

Milan

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 13:08                   ` Milan Broz
  0 siblings, 0 replies; 45+ messages in thread
From: Milan Broz @ 2019-06-19 13:08 UTC (permalink / raw)
  To: Ard Biesheuvel, Milan Broz
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	device-mapper development,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE

On 19/06/2019 14:49, Ard Biesheuvel wrote:
> On Wed, 19 Jun 2019 at 14:36, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>>
>> On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
>>>
>>> On 19/06/2019 13:16, Ard Biesheuvel wrote:
>>>>> Try
>>>>>   cryptsetup open --type plain -c null /dev/sdd test -q
>>>>> or
>>>>>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
>>>>>
>>>>> (or just run full cryptsetup testsuite)
>>>>>
>>>>
>>>> Is that your mode-test script?
>>>>
>>>> I saw some errors about the null cipher, but tbh, it looked completely
>>>> unrelated to me, so i skipped those for the moment. But now, it looks
>>>> like it is related after all.
>>>
>>> This was triggered by align-test, mode-test fails the same though.
>>>
>>> It is definitely related, I think you just changed the mode parsing in dm-crypt.
>>> (cipher null contains only one dash I guess).
>>>
>>
>> On my unpatched 4.19 kernel, mode-test gives me
>>
>> $ sudo ./mode-test
>> aes                            PLAIN:[table OK][status OK]
>> LUKS1:[table OK][status OK] CHECKSUM:[OK]
>> aes-plain                      PLAIN:[table OK][status OK]
>> LUKS1:[table OK][status OK] CHECKSUM:[OK]
>> null                           PLAIN:[table OK][status OK]
>> LUKS1:[table OK][status OK] CHECKSUM:[OK]
>> cipher_null                    PLAIN:[table FAIL]
>>  Expecting cipher_null-ecb got cipher_null-cbc-plain.
>> FAILED at line 64 ./mode-test
>>
>> which is why I commented out those tests in the first place.
>>
>> I can reproduce the crash after I re-enable them again, so I will need
>> to look into that. But something seems to be broken already.
>> Note that this is running on arm64 using a kconfig based on the Debian kernel.
> 
> Actually, could this be an issue with cryptsetup being out of date? On
> another arm64 system with a more recent distro, it works fine

Ah yes, it was changed because we hardened dm-crypt mode validation in kernel
https://gitlab.com/cryptsetup/cryptsetup/commit/aeea93fa9553ad70ed57f273aecb233113b204d6#f40cab3037a50bf28ce20d8aae52bfa6a0c0e2c4_137_137

So either use test form the released version of cryptsetup (all version are here)
https://mirrors.edge.kernel.org/pub/linux/utils/cryptsetup/

Or better use upstream git, we added a lot of tests anyway.

Milan

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
  2019-06-19 12:49                 ` Ard Biesheuvel
  (?)
@ 2019-06-19 13:13                   ` Ard Biesheuvel
  -1 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 13:13 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 14:49, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>
> On Wed, 19 Jun 2019 at 14:36, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
> >
> > On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
> > >
> > > On 19/06/2019 13:16, Ard Biesheuvel wrote:
> > > >> Try
> > > >>   cryptsetup open --type plain -c null /dev/sdd test -q
> > > >> or
> > > >>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
> > > >>
> > > >> (or just run full cryptsetup testsuite)
> > > >>
> > > >
> > > > Is that your mode-test script?
> > > >
> > > > I saw some errors about the null cipher, but tbh, it looked completely
> > > > unrelated to me, so i skipped those for the moment. But now, it looks
> > > > like it is related after all.
> > >
> > > This was triggered by align-test, mode-test fails the same though.
> > >
> > > It is definitely related, I think you just changed the mode parsing in dm-crypt.
> > > (cipher null contains only one dash I guess).
> > >
> >
> > On my unpatched 4.19 kernel, mode-test gives me
> >
> > $ sudo ./mode-test
> > aes                            PLAIN:[table OK][status OK]
> > LUKS1:[table OK][status OK] CHECKSUM:[OK]
> > aes-plain                      PLAIN:[table OK][status OK]
> > LUKS1:[table OK][status OK] CHECKSUM:[OK]
> > null                           PLAIN:[table OK][status OK]
> > LUKS1:[table OK][status OK] CHECKSUM:[OK]
> > cipher_null                    PLAIN:[table FAIL]
> >  Expecting cipher_null-ecb got cipher_null-cbc-plain.
> > FAILED at line 64 ./mode-test
> >
> > which is why I commented out those tests in the first place.
> >
> > I can reproduce the crash after I re-enable them again, so I will need
> > to look into that. But something seems to be broken already.
> > Note that this is running on arm64 using a kconfig based on the Debian kernel.
>
> Actually, could this be an issue with cryptsetup being out of date? On
> another arm64 system with a more recent distro, it works fine

This should fix the crash you are seeing

diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index 89efd7d249fd..12d28880ec34 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -2357,7 +2357,7 @@ static int crypt_ctr_cipher_old(struct dm_target
*ti, char *cipher_in, char *key
        if (!cipher_api)
                goto bad_mem;

-       if (!strcmp(*ivmode, "essiv")) {
+       if (*ivmode && !strcmp(*ivmode, "essiv")) {
                if (!*ivopts) {
                        ti->error = "Digest algorithm missing for ESSIV mode";
                        return -EINVAL;

Apologies for the sloppiness - this is a check that I had added and
then removed again, given that *ivmode was assigned unconditionally,
but i didn't realize tmp could be NULL.

With these two changes applied, mode-test successfully runs to completion.

Can you recommend another test suite I could run?

^ permalink raw reply related	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 13:13                   ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 13:13 UTC (permalink / raw)
  To: Milan Broz
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef

On Wed, 19 Jun 2019 at 14:49, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>
> On Wed, 19 Jun 2019 at 14:36, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
> >
> > On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
> > >
> > > On 19/06/2019 13:16, Ard Biesheuvel wrote:
> > > >> Try
> > > >>   cryptsetup open --type plain -c null /dev/sdd test -q
> > > >> or
> > > >>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
> > > >>
> > > >> (or just run full cryptsetup testsuite)
> > > >>
> > > >
> > > > Is that your mode-test script?
> > > >
> > > > I saw some errors about the null cipher, but tbh, it looked completely
> > > > unrelated to me, so i skipped those for the moment. But now, it looks
> > > > like it is related after all.
> > >
> > > This was triggered by align-test, mode-test fails the same though.
> > >
> > > It is definitely related, I think you just changed the mode parsing in dm-crypt.
> > > (cipher null contains only one dash I guess).
> > >
> >
> > On my unpatched 4.19 kernel, mode-test gives me
> >
> > $ sudo ./mode-test
> > aes                            PLAIN:[table OK][status OK]
> > LUKS1:[table OK][status OK] CHECKSUM:[OK]
> > aes-plain                      PLAIN:[table OK][status OK]
> > LUKS1:[table OK][status OK] CHECKSUM:[OK]
> > null                           PLAIN:[table OK][status OK]
> > LUKS1:[table OK][status OK] CHECKSUM:[OK]
> > cipher_null                    PLAIN:[table FAIL]
> >  Expecting cipher_null-ecb got cipher_null-cbc-plain.
> > FAILED at line 64 ./mode-test
> >
> > which is why I commented out those tests in the first place.
> >
> > I can reproduce the crash after I re-enable them again, so I will need
> > to look into that. But something seems to be broken already.
> > Note that this is running on arm64 using a kconfig based on the Debian kernel.
>
> Actually, could this be an issue with cryptsetup being out of date? On
> another arm64 system with a more recent distro, it works fine

This should fix the crash you are seeing

diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index 89efd7d249fd..12d28880ec34 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -2357,7 +2357,7 @@ static int crypt_ctr_cipher_old(struct dm_target
*ti, char *cipher_in, char *key
        if (!cipher_api)
                goto bad_mem;

-       if (!strcmp(*ivmode, "essiv")) {
+       if (*ivmode && !strcmp(*ivmode, "essiv")) {
                if (!*ivopts) {
                        ti->error = "Digest algorithm missing for ESSIV mode";
                        return -EINVAL;

Apologies for the sloppiness - this is a check that I had added and
then removed again, given that *ivmode was assigned unconditionally,
but i didn't realize tmp could be NULL.

With these two changes applied, mode-test successfully runs to completion.

Can you recommend another test suite I could run?

^ permalink raw reply related	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation
@ 2019-06-19 13:13                   ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 13:13 UTC (permalink / raw)
  To: Milan Broz
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	device-mapper development,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE

On Wed, 19 Jun 2019 at 14:49, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>
> On Wed, 19 Jun 2019 at 14:36, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
> >
> > On Wed, 19 Jun 2019 at 13:33, Milan Broz <gmazyland@gmail.com> wrote:
> > >
> > > On 19/06/2019 13:16, Ard Biesheuvel wrote:
> > > >> Try
> > > >>   cryptsetup open --type plain -c null /dev/sdd test -q
> > > >> or
> > > >>   dmsetup create test --table " 0 417792 crypt cipher_null-ecb - 0 /dev/sdd 0"
> > > >>
> > > >> (or just run full cryptsetup testsuite)
> > > >>
> > > >
> > > > Is that your mode-test script?
> > > >
> > > > I saw some errors about the null cipher, but tbh, it looked completely
> > > > unrelated to me, so i skipped those for the moment. But now, it looks
> > > > like it is related after all.
> > >
> > > This was triggered by align-test, mode-test fails the same though.
> > >
> > > It is definitely related, I think you just changed the mode parsing in dm-crypt.
> > > (cipher null contains only one dash I guess).
> > >
> >
> > On my unpatched 4.19 kernel, mode-test gives me
> >
> > $ sudo ./mode-test
> > aes                            PLAIN:[table OK][status OK]
> > LUKS1:[table OK][status OK] CHECKSUM:[OK]
> > aes-plain                      PLAIN:[table OK][status OK]
> > LUKS1:[table OK][status OK] CHECKSUM:[OK]
> > null                           PLAIN:[table OK][status OK]
> > LUKS1:[table OK][status OK] CHECKSUM:[OK]
> > cipher_null                    PLAIN:[table FAIL]
> >  Expecting cipher_null-ecb got cipher_null-cbc-plain.
> > FAILED at line 64 ./mode-test
> >
> > which is why I commented out those tests in the first place.
> >
> > I can reproduce the crash after I re-enable them again, so I will need
> > to look into that. But something seems to be broken already.
> > Note that this is running on arm64 using a kconfig based on the Debian kernel.
>
> Actually, could this be an issue with cryptsetup being out of date? On
> another arm64 system with a more recent distro, it works fine

This should fix the crash you are seeing

diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index 89efd7d249fd..12d28880ec34 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -2357,7 +2357,7 @@ static int crypt_ctr_cipher_old(struct dm_target
*ti, char *cipher_in, char *key
        if (!cipher_api)
                goto bad_mem;

-       if (!strcmp(*ivmode, "essiv")) {
+       if (*ivmode && !strcmp(*ivmode, "essiv")) {
                if (!*ivopts) {
                        ti->error = "Digest algorithm missing for ESSIV mode";
                        return -EINVAL;

Apologies for the sloppiness - this is a check that I had added and
then removed again, given that *ivmode was assigned unconditionally,
but i didn't realize tmp could be NULL.

With these two changes applied, mode-test successfully runs to completion.

Can you recommend another test suite I could run?

^ permalink raw reply related	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 1/4] crypto: essiv - create wrapper template for ESSIV generation
  2019-06-18 21:27   ` Ard Biesheuvel
  (?)
@ 2019-06-19 15:18     ` Ondrej Mosnáček
  -1 siblings, 0 replies; 45+ messages in thread
From: Ondrej Mosnáček @ 2019-06-19 15:18 UTC (permalink / raw)
  To: Ard Biesheuvel
  Cc: linux-crypto, Herbert Xu, Eric Biggers, dm-devel, linux-fscrypt,
	Gilad Ben-Yossef, Milan Broz

Hi Ard,

ut 18. 6. 2019 o 23:28 Ard Biesheuvel <ard.biesheuvel@linaro.org> napísal(a):
> Implement a template that wraps a (skcipher,cipher,shash) or
> (aead,cipher,shash) tuple so that we can consolidate the ESSIV handling
> in fscrypt and dm-crypt and move it into the crypto API. This will result
> in better test coverage, and will allow future changes to make the bare
> cipher interface internal to the crypto subsystem, in order to increase
> robustness of the API against misuse.
>
> Note that especially the AEAD handling is a bit complex, and is tightly
> coupled to the way dm-crypt combines AEAD based on the authenc() template
> with the ESSIV handling.

Wouldn't it work better to have a template only for skcipher and in
dm-crypt just inject the essiv() template into the cipher string? For
example: "authenc(hmac(sha256),cbc(aes))-essiv:sha256" ->
"authenc(hmac(sha256),essiv(cbc(aes),aes,sha256))". That seems to me a
much simpler hack. (But maybe I'm missing some issue in that
approach...)


>
> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> ---
>  crypto/Kconfig  |   4 +
>  crypto/Makefile |   1 +
>  crypto/essiv.c  | 624 ++++++++++++++++++++
>  3 files changed, 629 insertions(+)
>
> diff --git a/crypto/Kconfig b/crypto/Kconfig
> index 3d056e7da65f..1aa47087c1a2 100644
> --- a/crypto/Kconfig
> +++ b/crypto/Kconfig
> @@ -1917,6 +1917,10 @@ config CRYPTO_STATS
>  config CRYPTO_HASH_INFO
>         bool
>
> +config CRYPTO_ESSIV
> +       tristate
> +       select CRYPTO_AUTHENC
> +
>  source "drivers/crypto/Kconfig"
>  source "crypto/asymmetric_keys/Kconfig"
>  source "certs/Kconfig"
> diff --git a/crypto/Makefile b/crypto/Makefile
> index 266a4cdbb9e2..ad1d99ba6d56 100644
> --- a/crypto/Makefile
> +++ b/crypto/Makefile
> @@ -148,6 +148,7 @@ obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o
>  obj-$(CONFIG_CRYPTO_ZSTD) += zstd.o
>  obj-$(CONFIG_CRYPTO_OFB) += ofb.o
>  obj-$(CONFIG_CRYPTO_ECC) += ecc.o
> +obj-$(CONFIG_CRYPTO_ESSIV) += essiv.o
>
>  ecdh_generic-y += ecdh.o
>  ecdh_generic-y += ecdh_helper.o
> diff --git a/crypto/essiv.c b/crypto/essiv.c
> new file mode 100644
> index 000000000000..029a65afb4d7
> --- /dev/null
> +++ b/crypto/essiv.c
> @@ -0,0 +1,624 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * ESSIV skcipher template for block encryption
> + *
> + * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
> + *
> + * Heavily based on:
> + * adiantum length-preserving encryption mode
> + *
> + * Copyright 2018 Google LLC
> + */
> +
> +#include <crypto/authenc.h>
> +#include <crypto/internal/aead.h>
> +#include <crypto/internal/hash.h>
> +#include <crypto/internal/skcipher.h>
> +#include <crypto/scatterwalk.h>
> +#include <linux/module.h>
> +
> +#include "internal.h"
> +
> +#define ESSIV_IV_SIZE          sizeof(u64)     // IV size of the outer algo
> +#define MAX_INNER_IV_SIZE      16              // max IV size of inner algo
> +
> +struct essiv_instance_ctx {
> +       union {
> +               struct crypto_skcipher_spawn    blockcipher_spawn;
> +               struct crypto_aead_spawn        aead_spawn;
> +       } u;
> +       struct crypto_spawn                     essiv_cipher_spawn;
> +       struct crypto_shash_spawn               hash_spawn;
> +};
> +
> +struct essiv_tfm_ctx {
> +       union {
> +               struct crypto_skcipher  *blockcipher;
> +               struct crypto_aead      *aead;
> +       } u;
> +       struct crypto_cipher            *essiv_cipher;
> +       struct crypto_shash             *hash;
> +};
> +
> +struct essiv_skcipher_request_ctx {
> +       u8                              iv[MAX_INNER_IV_SIZE];
> +       struct skcipher_request         blockcipher_req;
> +};
> +
> +struct essiv_aead_request_ctx {
> +       u8                              iv[MAX_INNER_IV_SIZE];
> +       struct scatterlist              src[4], dst[4];
> +       struct aead_request             aead_req;
> +};
> +
> +static int essiv_skcipher_setkey(struct crypto_skcipher *tfm,
> +                                const u8 *key, unsigned int keylen)
> +{
> +       u32 flags = crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_REQ_MASK;
> +       struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +       SHASH_DESC_ON_STACK(desc, tctx->hash);
> +       unsigned int saltsize;
> +       u8 *salt;
> +       int err;
> +
> +       crypto_skcipher_clear_flags(tctx->u.blockcipher, CRYPTO_TFM_REQ_MASK);
> +       crypto_skcipher_set_flags(tctx->u.blockcipher, flags);
> +       err = crypto_skcipher_setkey(tctx->u.blockcipher, key, keylen);
> +       crypto_skcipher_set_flags(tfm,
> +                                 crypto_skcipher_get_flags(tctx->u.blockcipher) &
> +                                 CRYPTO_TFM_RES_MASK);
> +       if (err)
> +               return err;
> +
> +       saltsize = crypto_shash_digestsize(tctx->hash);
> +       salt = kmalloc(saltsize, GFP_KERNEL);
> +       if (!salt)
> +               return -ENOMEM;
> +
> +       desc->tfm = tctx->hash;
> +       crypto_shash_digest(desc, key, keylen, salt);
> +
> +       crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
> +       crypto_cipher_set_flags(tctx->essiv_cipher, flags & CRYPTO_TFM_REQ_MASK);
> +       err = crypto_cipher_setkey(tctx->essiv_cipher, salt, saltsize);
> +       flags = crypto_cipher_get_flags(tctx->essiv_cipher) & CRYPTO_TFM_RES_MASK;
> +       crypto_skcipher_set_flags(tfm, flags);
> +
> +       kzfree(salt);
> +       return err;
> +}
> +
> +static int essiv_aead_setkey(struct crypto_aead *tfm, const u8 *key,
> +                            unsigned int keylen)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +       SHASH_DESC_ON_STACK(desc, tctx->hash);
> +       struct crypto_authenc_keys keys;
> +       unsigned int saltsize;
> +       u8 *salt;
> +       int err;
> +
> +       crypto_aead_clear_flags(tctx->u.aead, CRYPTO_TFM_REQ_MASK);
> +       crypto_aead_set_flags(tctx->u.aead, crypto_aead_get_flags(tfm) &
> +                                           CRYPTO_TFM_REQ_MASK);
> +       err = crypto_aead_setkey(tctx->u.aead, key, keylen);
> +       crypto_aead_set_flags(tfm, crypto_aead_get_flags(tctx->u.aead) &
> +                                  CRYPTO_TFM_RES_MASK);
> +       if (err)
> +               return err;
> +
> +       if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
> +               crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
> +               return -EINVAL;
> +       }
> +
> +       saltsize = crypto_shash_digestsize(tctx->hash);
> +       salt = kmalloc(saltsize, GFP_KERNEL);
> +       if (!salt)
> +               return -ENOMEM;
> +
> +       desc->tfm = tctx->hash;
> +       crypto_shash_init(desc);
> +       crypto_shash_update(desc, keys.enckey, keys.enckeylen);
> +       crypto_shash_finup(desc, keys.authkey, keys.authkeylen, salt);
> +
> +       crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
> +       crypto_cipher_set_flags(tctx->essiv_cipher, crypto_aead_get_flags(tfm) &
> +                                                   CRYPTO_TFM_REQ_MASK);
> +       err = crypto_cipher_setkey(tctx->essiv_cipher, salt, saltsize);
> +       crypto_aead_set_flags(tfm, crypto_cipher_get_flags(tctx->essiv_cipher) &
> +                                  CRYPTO_TFM_RES_MASK);
> +
> +       kzfree(salt);
> +       return err;
> +}
> +
> +static int essiv_aead_setauthsize(struct crypto_aead *tfm,
> +                                 unsigned int authsize)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +
> +       return crypto_aead_setauthsize(tctx->u.aead, authsize);
> +}
> +
> +static void essiv_skcipher_done(struct crypto_async_request *areq, int err)
> +{
> +       struct skcipher_request *req = areq->data;
> +
> +       skcipher_request_complete(req, err);
> +}
> +
> +static void essiv_aead_done(struct crypto_async_request *areq, int err)
> +{
> +       struct aead_request *req = areq->data;
> +
> +       aead_request_complete(req, err);
> +}
> +
> +static void essiv_skcipher_prepare_subreq(struct skcipher_request *req)
> +{
> +       struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
> +       const struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +       struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +       struct skcipher_request *subreq = &rctx->blockcipher_req;
> +
> +       memset(rctx->iv, 0, crypto_cipher_blocksize(tctx->essiv_cipher));
> +       memcpy(rctx->iv, req->iv, crypto_skcipher_ivsize(tfm));
> +
> +       crypto_cipher_encrypt_one(tctx->essiv_cipher, rctx->iv, rctx->iv);
> +
> +       skcipher_request_set_tfm(subreq, tctx->u.blockcipher);
> +       skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
> +                                  rctx->iv);
> +       skcipher_request_set_callback(subreq, req->base.flags,
> +                                     essiv_skcipher_done, req);
> +}
> +
> +static int essiv_aead_prepare_subreq(struct aead_request *req)
> +{
> +       struct crypto_aead *tfm = crypto_aead_reqtfm(req);
> +       const struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +       struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
> +       int ivsize = crypto_cipher_blocksize(tctx->essiv_cipher);
> +       int ssize = req->assoclen - crypto_aead_ivsize(tfm);
> +       struct aead_request *subreq = &rctx->aead_req;
> +       struct scatterlist *sg;
> +
> +       /*
> +        * dm-crypt embeds the sector number and the IV in the AAD region so we
> +        * have to splice the converted IV into the subrequest that we pass on
> +        * to the AEAD transform. This means we are tightly coupled to dm-crypt,
> +        * but that should be the only user of this code in AEAD mode.
> +        */
> +       if (ssize < 0 || sg_nents_for_len(req->src, ssize) != 1)
> +               return -EINVAL;
> +
> +       memset(rctx->iv, 0, ivsize);
> +       memcpy(rctx->iv, req->iv, crypto_aead_ivsize(tfm));
> +
> +       crypto_cipher_encrypt_one(tctx->essiv_cipher, rctx->iv, rctx->iv);
> +
> +       sg_init_table(rctx->src, 4);
> +       sg_set_page(rctx->src, sg_page(req->src), ssize, req->src->offset);
> +       sg_set_buf(rctx->src + 1, rctx->iv, ivsize);
> +       sg = scatterwalk_ffwd(rctx->src + 2, req->src, req->assoclen);
> +       if (sg != rctx->src + 2)
> +               sg_chain(rctx->src, 3, sg);
> +
> +       sg_init_table(rctx->dst, 4);
> +       sg_set_page(rctx->dst, sg_page(req->dst), ssize, req->dst->offset);
> +       sg_set_buf(rctx->dst + 1, rctx->iv, ivsize);
> +       sg = scatterwalk_ffwd(rctx->dst + 2, req->dst, req->assoclen);
> +       if (sg != rctx->dst + 2)
> +               sg_chain(rctx->dst, 3, sg);
> +
> +       aead_request_set_tfm(subreq, tctx->u.aead);
> +       aead_request_set_crypt(subreq, rctx->src, rctx->dst, req->cryptlen,
> +                              rctx->iv);
> +       aead_request_set_ad(subreq, ssize + ivsize);
> +       aead_request_set_callback(subreq, req->base.flags, essiv_aead_done, req);
> +
> +       return 0;
> +}
> +
> +static int essiv_skcipher_encrypt(struct skcipher_request *req)
> +{
> +       struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +
> +       essiv_skcipher_prepare_subreq(req);
> +       return crypto_skcipher_encrypt(&rctx->blockcipher_req);
> +}
> +
> +static int essiv_aead_encrypt(struct aead_request *req)
> +{
> +       struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
> +       int err;
> +
> +       err = essiv_aead_prepare_subreq(req);
> +       if (err)
> +               return err;
> +       return crypto_aead_encrypt(&rctx->aead_req);
> +}
> +
> +static int essiv_skcipher_decrypt(struct skcipher_request *req)
> +{
> +       struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +       return crypto_skcipher_decrypt(&rctx->blockcipher_req);
> +}
> +
> +static int essiv_aead_decrypt(struct aead_request *req)
> +{
> +       struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
> +       int err;
> +
> +       err = essiv_aead_prepare_subreq(req);
> +       if (err)
> +               return err;
> +
> +       essiv_aead_prepare_subreq(req);
> +       return crypto_aead_decrypt(&rctx->aead_req);
> +}
> +
> +static int essiv_init_tfm(struct essiv_instance_ctx *ictx,
> +                         struct essiv_tfm_ctx *tctx)
> +{
> +       struct crypto_cipher *essiv_cipher;
> +       struct crypto_shash *hash;
> +       int err;
> +
> +       essiv_cipher = crypto_spawn_cipher(&ictx->essiv_cipher_spawn);
> +       if (IS_ERR(essiv_cipher))
> +               return PTR_ERR(essiv_cipher);
> +
> +       hash = crypto_spawn_shash(&ictx->hash_spawn);
> +       if (IS_ERR(hash)) {
> +               err = PTR_ERR(hash);
> +               goto err_free_essiv_cipher;
> +       }
> +
> +       tctx->essiv_cipher = essiv_cipher;
> +       tctx->hash = hash;
> +
> +       return 0;
> +
> +err_free_essiv_cipher:
> +       crypto_free_cipher(essiv_cipher);
> +       return err;
> +}
> +
> +static int essiv_skcipher_init_tfm(struct crypto_skcipher *tfm)
> +{
> +       struct skcipher_instance *inst = skcipher_alg_instance(tfm);
> +       struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
> +       struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +       struct crypto_skcipher *blockcipher;
> +       unsigned int subreq_size;
> +       int err;
> +
> +       BUILD_BUG_ON(offsetofend(struct essiv_skcipher_request_ctx,
> +                                blockcipher_req) !=
> +                    sizeof(struct essiv_skcipher_request_ctx));
> +
> +       blockcipher = crypto_spawn_skcipher(&ictx->u.blockcipher_spawn);
> +       if (IS_ERR(blockcipher))
> +               return PTR_ERR(blockcipher);
> +
> +       subreq_size = FIELD_SIZEOF(struct essiv_skcipher_request_ctx,
> +                                  blockcipher_req) +
> +                     crypto_skcipher_reqsize(blockcipher);
> +
> +       crypto_skcipher_set_reqsize(tfm, offsetof(struct essiv_skcipher_request_ctx,
> +                                                 blockcipher_req) + subreq_size);
> +
> +       err = essiv_init_tfm(ictx, tctx);
> +       if (err)
> +               crypto_free_skcipher(blockcipher);
> +
> +       tctx->u.blockcipher = blockcipher;
> +       return err;
> +}
> +
> +static int essiv_aead_init_tfm(struct crypto_aead *tfm)
> +{
> +       struct aead_instance *inst = aead_alg_instance(tfm);
> +       struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +       struct crypto_aead *aead;
> +       unsigned int subreq_size;
> +       int err;
> +
> +       BUILD_BUG_ON(offsetofend(struct essiv_aead_request_ctx, aead_req) !=
> +                    sizeof(struct essiv_aead_request_ctx));
> +
> +       aead = crypto_spawn_aead(&ictx->u.aead_spawn);
> +       if (IS_ERR(aead))
> +               return PTR_ERR(aead);
> +
> +       subreq_size = FIELD_SIZEOF(struct essiv_aead_request_ctx, aead_req) +
> +                     crypto_aead_reqsize(aead);
> +
> +       crypto_aead_set_reqsize(tfm, offsetof(struct essiv_aead_request_ctx,
> +                                             aead_req) + subreq_size);
> +
> +       err = essiv_init_tfm(ictx, tctx);
> +       if (err)
> +               crypto_free_aead(aead);
> +
> +       tctx->u.aead = aead;
> +       return err;
> +}
> +
> +static void essiv_skcipher_exit_tfm(struct crypto_skcipher *tfm)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +
> +       crypto_free_skcipher(tctx->u.blockcipher);
> +       crypto_free_cipher(tctx->essiv_cipher);
> +       crypto_free_shash(tctx->hash);
> +}
> +
> +static void essiv_aead_exit_tfm(struct crypto_aead *tfm)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +
> +       crypto_free_aead(tctx->u.aead);
> +       crypto_free_cipher(tctx->essiv_cipher);
> +       crypto_free_shash(tctx->hash);
> +}
> +
> +static void essiv_skcipher_free_instance(struct skcipher_instance *inst)
> +{
> +       struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
> +
> +       crypto_drop_skcipher(&ictx->u.blockcipher_spawn);
> +       crypto_drop_spawn(&ictx->essiv_cipher_spawn);
> +       crypto_drop_shash(&ictx->hash_spawn);
> +       kfree(inst);
> +}
> +
> +static void essiv_aead_free_instance(struct aead_instance *inst)
> +{
> +       struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
> +
> +       crypto_drop_aead(&ictx->u.aead_spawn);
> +       crypto_drop_spawn(&ictx->essiv_cipher_spawn);
> +       crypto_drop_shash(&ictx->hash_spawn);
> +       kfree(inst);
> +}
> +
> +static bool essiv_supported_algorithms(struct crypto_alg *essiv_cipher_alg,
> +                                      struct shash_alg *hash_alg,
> +                                      int ivsize)
> +{
> +       if (hash_alg->digestsize < essiv_cipher_alg->cra_cipher.cia_min_keysize ||
> +           hash_alg->digestsize > essiv_cipher_alg->cra_cipher.cia_max_keysize)
> +               return false;
> +
> +       if (ivsize != essiv_cipher_alg->cra_blocksize)
> +               return false;
> +
> +       if (ivsize > MAX_INNER_IV_SIZE)
> +               return false;
> +
> +       return true;
> +}
> +
> +static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb)
> +{
> +       struct crypto_attr_type *algt;
> +       const char *blockcipher_name;
> +       const char *essiv_cipher_name;
> +       const char *shash_name;
> +       struct skcipher_instance *skcipher_inst = NULL;
> +       struct aead_instance *aead_inst = NULL;
> +       struct crypto_instance *inst;
> +       struct crypto_alg *base, *block_base;
> +       struct essiv_instance_ctx *ictx;
> +       struct skcipher_alg *blockcipher_alg = NULL;
> +       struct aead_alg *aead_alg = NULL;
> +       struct crypto_alg *essiv_cipher_alg;
> +       struct crypto_alg *_hash_alg;
> +       struct shash_alg *hash_alg;
> +       int ivsize;
> +       u32 type;
> +       int err;
> +
> +       algt = crypto_get_attr_type(tb);
> +       if (IS_ERR(algt))
> +               return PTR_ERR(algt);
> +
> +       blockcipher_name = crypto_attr_alg_name(tb[1]);
> +       if (IS_ERR(blockcipher_name))
> +               return PTR_ERR(blockcipher_name);
> +
> +       essiv_cipher_name = crypto_attr_alg_name(tb[2]);
> +       if (IS_ERR(essiv_cipher_name))
> +               return PTR_ERR(essiv_cipher_name);
> +
> +       shash_name = crypto_attr_alg_name(tb[3]);
> +       if (IS_ERR(shash_name))
> +               return PTR_ERR(shash_name);
> +
> +       type = algt->type & algt->mask;
> +
> +       switch (type) {
> +       case CRYPTO_ALG_TYPE_BLKCIPHER:
> +               skcipher_inst = kzalloc(sizeof(*skcipher_inst) +
> +                                       sizeof(*ictx), GFP_KERNEL);
> +               if (!skcipher_inst)
> +                       return -ENOMEM;
> +               inst = skcipher_crypto_instance(skcipher_inst);
> +               base = &skcipher_inst->alg.base;
> +               ictx = crypto_instance_ctx(inst);
> +
> +               /* Block cipher, e.g. "cbc(aes)" */
> +               crypto_set_skcipher_spawn(&ictx->u.blockcipher_spawn, inst);
> +               err = crypto_grab_skcipher(&ictx->u.blockcipher_spawn,
> +                                          blockcipher_name, 0,
> +                                          crypto_requires_sync(algt->type,
> +                                                               algt->mask));
> +               if (err)
> +                       goto out_free_inst;
> +               blockcipher_alg = crypto_spawn_skcipher_alg(&ictx->u.blockcipher_spawn);
> +               block_base = &blockcipher_alg->base;
> +               ivsize = blockcipher_alg->ivsize;
> +               break;
> +
> +       case CRYPTO_ALG_TYPE_AEAD:
> +               aead_inst = kzalloc(sizeof(*aead_inst) +
> +                                   sizeof(*ictx), GFP_KERNEL);
> +               if (!aead_inst)
> +                       return -ENOMEM;
> +               inst = aead_crypto_instance(aead_inst);
> +               base = &aead_inst->alg.base;
> +               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,
> +                                      blockcipher_name, 0,
> +                                      crypto_requires_sync(algt->type,
> +                                                           algt->mask));
> +               if (err)
> +                       goto out_free_inst;
> +               aead_alg = crypto_spawn_aead_alg(&ictx->u.aead_spawn);
> +               block_base = &aead_alg->base;
> +               ivsize = aead_alg->ivsize;
> +               break;
> +
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       /* Block cipher, e.g. "aes" */
> +       crypto_set_spawn(&ictx->essiv_cipher_spawn, inst);
> +       err = crypto_grab_spawn(&ictx->essiv_cipher_spawn, essiv_cipher_name,
> +                               CRYPTO_ALG_TYPE_CIPHER, CRYPTO_ALG_TYPE_MASK);
> +       if (err)
> +               goto out_drop_blockcipher;
> +       essiv_cipher_alg = ictx->essiv_cipher_spawn.alg;
> +
> +       /* Synchronous hash, e.g., "sha256" */
> +       _hash_alg = crypto_alg_mod_lookup(shash_name,
> +                                         CRYPTO_ALG_TYPE_SHASH,
> +                                         CRYPTO_ALG_TYPE_MASK);
> +       if (IS_ERR(_hash_alg)) {
> +               err = PTR_ERR(_hash_alg);
> +               goto out_drop_essiv_cipher;
> +       }
> +       hash_alg = __crypto_shash_alg(_hash_alg);
> +       err = crypto_init_shash_spawn(&ictx->hash_spawn, hash_alg, inst);
> +       if (err)
> +               goto out_put_hash;
> +
> +       /* Check the set of algorithms */
> +       if (!essiv_supported_algorithms(essiv_cipher_alg, hash_alg, ivsize)) {
> +               pr_warn("Unsupported essiv instantiation: (%s,%s,%s)\n",
> +                       block_base->cra_name,
> +                       essiv_cipher_alg->cra_name,
> +                       hash_alg->base.cra_name);
> +               err = -EINVAL;
> +               goto out_drop_hash;
> +       }
> +
> +       /* Instance fields */
> +
> +       err = -ENAMETOOLONG;
> +       if (snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME,
> +                    "essiv(%s,%s,%s)", block_base->cra_name,
> +                    essiv_cipher_alg->cra_name,
> +                    hash_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
> +               goto out_drop_hash;
> +       if (snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME,
> +                    "essiv(%s,%s,%s)",
> +                    block_base->cra_driver_name,
> +                    essiv_cipher_alg->cra_driver_name,
> +                    hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
> +               goto out_drop_hash;
> +
> +       base->cra_flags         = block_base->cra_flags & CRYPTO_ALG_ASYNC;
> +       base->cra_blocksize     = block_base->cra_blocksize;
> +       base->cra_ctxsize       = sizeof(struct essiv_tfm_ctx);
> +       base->cra_alignmask     = block_base->cra_alignmask;
> +       base->cra_priority      = block_base->cra_priority;
> +
> +       if (type == CRYPTO_ALG_TYPE_BLKCIPHER) {
> +               skcipher_inst->alg.setkey       = essiv_skcipher_setkey;
> +               skcipher_inst->alg.encrypt      = essiv_skcipher_encrypt;
> +               skcipher_inst->alg.decrypt      = essiv_skcipher_decrypt;
> +               skcipher_inst->alg.init         = essiv_skcipher_init_tfm;
> +               skcipher_inst->alg.exit         = essiv_skcipher_exit_tfm;
> +
> +               skcipher_inst->alg.min_keysize  = crypto_skcipher_alg_min_keysize(blockcipher_alg);
> +               skcipher_inst->alg.max_keysize  = crypto_skcipher_alg_max_keysize(blockcipher_alg);
> +               skcipher_inst->alg.ivsize       = ESSIV_IV_SIZE;
> +               skcipher_inst->alg.chunksize    = blockcipher_alg->chunksize;
> +               skcipher_inst->alg.walksize     = blockcipher_alg->walksize;
> +
> +               skcipher_inst->free             = essiv_skcipher_free_instance;
> +
> +               err = skcipher_register_instance(tmpl, skcipher_inst);
> +       } else {
> +               aead_inst->alg.setkey           = essiv_aead_setkey;
> +               aead_inst->alg.setauthsize      = essiv_aead_setauthsize;
> +               aead_inst->alg.encrypt          = essiv_aead_encrypt;
> +               aead_inst->alg.decrypt          = essiv_aead_decrypt;
> +               aead_inst->alg.init             = essiv_aead_init_tfm;
> +               aead_inst->alg.exit             = essiv_aead_exit_tfm;
> +
> +               aead_inst->alg.ivsize           = ESSIV_IV_SIZE;
> +               aead_inst->alg.maxauthsize      = aead_alg->maxauthsize;
> +               aead_inst->alg.chunksize        = aead_alg->chunksize;
> +
> +               aead_inst->free                 = essiv_aead_free_instance;
> +
> +               err = aead_register_instance(tmpl, aead_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_essiv_cipher:
> +       crypto_drop_spawn(&ictx->essiv_cipher_spawn);
> +out_drop_blockcipher:
> +       if (type == CRYPTO_ALG_TYPE_BLKCIPHER) {
> +               crypto_drop_skcipher(&ictx->u.blockcipher_spawn);
> +       } else {
> +               crypto_drop_aead(&ictx->u.aead_spawn);
> +       }
> +out_free_inst:
> +       kfree(skcipher_inst);
> +       kfree(aead_inst);
> +       return err;
> +}
> +
> +/* essiv(blockcipher_name, essiv_cipher_name, shash_name) */
> +static struct crypto_template essiv_tmpl = {
> +       .name   = "essiv",
> +       .create = essiv_create,
> +       .module = THIS_MODULE,
> +};
> +
> +static int __init essiv_module_init(void)
> +{
> +       return crypto_register_template(&essiv_tmpl);
> +}
> +
> +static void __exit essiv_module_exit(void)
> +{
> +       crypto_unregister_template(&essiv_tmpl);
> +}
> +
> +subsys_initcall(essiv_module_init);
> +module_exit(essiv_module_exit);
> +
> +MODULE_DESCRIPTION("ESSIV skcipher/aead wrapper for block encryption");
> +MODULE_LICENSE("GPL v2");
> +MODULE_ALIAS_CRYPTO("essiv");
> --
> 2.17.1
>

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [dm-devel] [PATCH v2 1/4] crypto: essiv - create wrapper template for ESSIV generation
@ 2019-06-19 15:18     ` Ondrej Mosnáček
  0 siblings, 0 replies; 45+ messages in thread
From: Ondrej Mosnáček @ 2019-06-19 15:18 UTC (permalink / raw)
  To: Ard Biesheuvel
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	dm-devel, linux-crypto, Milan Broz

Hi Ard,

ut 18. 6. 2019 o 23:28 Ard Biesheuvel <ard.biesheuvel@linaro.org> napísal(a):
> Implement a template that wraps a (skcipher,cipher,shash) or
> (aead,cipher,shash) tuple so that we can consolidate the ESSIV handling
> in fscrypt and dm-crypt and move it into the crypto API. This will result
> in better test coverage, and will allow future changes to make the bare
> cipher interface internal to the crypto subsystem, in order to increase
> robustness of the API against misuse.
>
> Note that especially the AEAD handling is a bit complex, and is tightly
> coupled to the way dm-crypt combines AEAD based on the authenc() template
> with the ESSIV handling.

Wouldn't it work better to have a template only for skcipher and in
dm-crypt just inject the essiv() template into the cipher string? For
example: "authenc(hmac(sha256),cbc(aes))-essiv:sha256" ->
"authenc(hmac(sha256),essiv(cbc(aes),aes,sha256))". That seems to me a
much simpler hack. (But maybe I'm missing some issue in that
approach...)


>
> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> ---
>  crypto/Kconfig  |   4 +
>  crypto/Makefile |   1 +
>  crypto/essiv.c  | 624 ++++++++++++++++++++
>  3 files changed, 629 insertions(+)
>
> diff --git a/crypto/Kconfig b/crypto/Kconfig
> index 3d056e7da65f..1aa47087c1a2 100644
> --- a/crypto/Kconfig
> +++ b/crypto/Kconfig
> @@ -1917,6 +1917,10 @@ config CRYPTO_STATS
>  config CRYPTO_HASH_INFO
>         bool
>
> +config CRYPTO_ESSIV
> +       tristate
> +       select CRYPTO_AUTHENC
> +
>  source "drivers/crypto/Kconfig"
>  source "crypto/asymmetric_keys/Kconfig"
>  source "certs/Kconfig"
> diff --git a/crypto/Makefile b/crypto/Makefile
> index 266a4cdbb9e2..ad1d99ba6d56 100644
> --- a/crypto/Makefile
> +++ b/crypto/Makefile
> @@ -148,6 +148,7 @@ obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o
>  obj-$(CONFIG_CRYPTO_ZSTD) += zstd.o
>  obj-$(CONFIG_CRYPTO_OFB) += ofb.o
>  obj-$(CONFIG_CRYPTO_ECC) += ecc.o
> +obj-$(CONFIG_CRYPTO_ESSIV) += essiv.o
>
>  ecdh_generic-y += ecdh.o
>  ecdh_generic-y += ecdh_helper.o
> diff --git a/crypto/essiv.c b/crypto/essiv.c
> new file mode 100644
> index 000000000000..029a65afb4d7
> --- /dev/null
> +++ b/crypto/essiv.c
> @@ -0,0 +1,624 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * ESSIV skcipher template for block encryption
> + *
> + * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
> + *
> + * Heavily based on:
> + * adiantum length-preserving encryption mode
> + *
> + * Copyright 2018 Google LLC
> + */
> +
> +#include <crypto/authenc.h>
> +#include <crypto/internal/aead.h>
> +#include <crypto/internal/hash.h>
> +#include <crypto/internal/skcipher.h>
> +#include <crypto/scatterwalk.h>
> +#include <linux/module.h>
> +
> +#include "internal.h"
> +
> +#define ESSIV_IV_SIZE          sizeof(u64)     // IV size of the outer algo
> +#define MAX_INNER_IV_SIZE      16              // max IV size of inner algo
> +
> +struct essiv_instance_ctx {
> +       union {
> +               struct crypto_skcipher_spawn    blockcipher_spawn;
> +               struct crypto_aead_spawn        aead_spawn;
> +       } u;
> +       struct crypto_spawn                     essiv_cipher_spawn;
> +       struct crypto_shash_spawn               hash_spawn;
> +};
> +
> +struct essiv_tfm_ctx {
> +       union {
> +               struct crypto_skcipher  *blockcipher;
> +               struct crypto_aead      *aead;
> +       } u;
> +       struct crypto_cipher            *essiv_cipher;
> +       struct crypto_shash             *hash;
> +};
> +
> +struct essiv_skcipher_request_ctx {
> +       u8                              iv[MAX_INNER_IV_SIZE];
> +       struct skcipher_request         blockcipher_req;
> +};
> +
> +struct essiv_aead_request_ctx {
> +       u8                              iv[MAX_INNER_IV_SIZE];
> +       struct scatterlist              src[4], dst[4];
> +       struct aead_request             aead_req;
> +};
> +
> +static int essiv_skcipher_setkey(struct crypto_skcipher *tfm,
> +                                const u8 *key, unsigned int keylen)
> +{
> +       u32 flags = crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_REQ_MASK;
> +       struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +       SHASH_DESC_ON_STACK(desc, tctx->hash);
> +       unsigned int saltsize;
> +       u8 *salt;
> +       int err;
> +
> +       crypto_skcipher_clear_flags(tctx->u.blockcipher, CRYPTO_TFM_REQ_MASK);
> +       crypto_skcipher_set_flags(tctx->u.blockcipher, flags);
> +       err = crypto_skcipher_setkey(tctx->u.blockcipher, key, keylen);
> +       crypto_skcipher_set_flags(tfm,
> +                                 crypto_skcipher_get_flags(tctx->u.blockcipher) &
> +                                 CRYPTO_TFM_RES_MASK);
> +       if (err)
> +               return err;
> +
> +       saltsize = crypto_shash_digestsize(tctx->hash);
> +       salt = kmalloc(saltsize, GFP_KERNEL);
> +       if (!salt)
> +               return -ENOMEM;
> +
> +       desc->tfm = tctx->hash;
> +       crypto_shash_digest(desc, key, keylen, salt);
> +
> +       crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
> +       crypto_cipher_set_flags(tctx->essiv_cipher, flags & CRYPTO_TFM_REQ_MASK);
> +       err = crypto_cipher_setkey(tctx->essiv_cipher, salt, saltsize);
> +       flags = crypto_cipher_get_flags(tctx->essiv_cipher) & CRYPTO_TFM_RES_MASK;
> +       crypto_skcipher_set_flags(tfm, flags);
> +
> +       kzfree(salt);
> +       return err;
> +}
> +
> +static int essiv_aead_setkey(struct crypto_aead *tfm, const u8 *key,
> +                            unsigned int keylen)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +       SHASH_DESC_ON_STACK(desc, tctx->hash);
> +       struct crypto_authenc_keys keys;
> +       unsigned int saltsize;
> +       u8 *salt;
> +       int err;
> +
> +       crypto_aead_clear_flags(tctx->u.aead, CRYPTO_TFM_REQ_MASK);
> +       crypto_aead_set_flags(tctx->u.aead, crypto_aead_get_flags(tfm) &
> +                                           CRYPTO_TFM_REQ_MASK);
> +       err = crypto_aead_setkey(tctx->u.aead, key, keylen);
> +       crypto_aead_set_flags(tfm, crypto_aead_get_flags(tctx->u.aead) &
> +                                  CRYPTO_TFM_RES_MASK);
> +       if (err)
> +               return err;
> +
> +       if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
> +               crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
> +               return -EINVAL;
> +       }
> +
> +       saltsize = crypto_shash_digestsize(tctx->hash);
> +       salt = kmalloc(saltsize, GFP_KERNEL);
> +       if (!salt)
> +               return -ENOMEM;
> +
> +       desc->tfm = tctx->hash;
> +       crypto_shash_init(desc);
> +       crypto_shash_update(desc, keys.enckey, keys.enckeylen);
> +       crypto_shash_finup(desc, keys.authkey, keys.authkeylen, salt);
> +
> +       crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
> +       crypto_cipher_set_flags(tctx->essiv_cipher, crypto_aead_get_flags(tfm) &
> +                                                   CRYPTO_TFM_REQ_MASK);
> +       err = crypto_cipher_setkey(tctx->essiv_cipher, salt, saltsize);
> +       crypto_aead_set_flags(tfm, crypto_cipher_get_flags(tctx->essiv_cipher) &
> +                                  CRYPTO_TFM_RES_MASK);
> +
> +       kzfree(salt);
> +       return err;
> +}
> +
> +static int essiv_aead_setauthsize(struct crypto_aead *tfm,
> +                                 unsigned int authsize)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +
> +       return crypto_aead_setauthsize(tctx->u.aead, authsize);
> +}
> +
> +static void essiv_skcipher_done(struct crypto_async_request *areq, int err)
> +{
> +       struct skcipher_request *req = areq->data;
> +
> +       skcipher_request_complete(req, err);
> +}
> +
> +static void essiv_aead_done(struct crypto_async_request *areq, int err)
> +{
> +       struct aead_request *req = areq->data;
> +
> +       aead_request_complete(req, err);
> +}
> +
> +static void essiv_skcipher_prepare_subreq(struct skcipher_request *req)
> +{
> +       struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
> +       const struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +       struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +       struct skcipher_request *subreq = &rctx->blockcipher_req;
> +
> +       memset(rctx->iv, 0, crypto_cipher_blocksize(tctx->essiv_cipher));
> +       memcpy(rctx->iv, req->iv, crypto_skcipher_ivsize(tfm));
> +
> +       crypto_cipher_encrypt_one(tctx->essiv_cipher, rctx->iv, rctx->iv);
> +
> +       skcipher_request_set_tfm(subreq, tctx->u.blockcipher);
> +       skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
> +                                  rctx->iv);
> +       skcipher_request_set_callback(subreq, req->base.flags,
> +                                     essiv_skcipher_done, req);
> +}
> +
> +static int essiv_aead_prepare_subreq(struct aead_request *req)
> +{
> +       struct crypto_aead *tfm = crypto_aead_reqtfm(req);
> +       const struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +       struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
> +       int ivsize = crypto_cipher_blocksize(tctx->essiv_cipher);
> +       int ssize = req->assoclen - crypto_aead_ivsize(tfm);
> +       struct aead_request *subreq = &rctx->aead_req;
> +       struct scatterlist *sg;
> +
> +       /*
> +        * dm-crypt embeds the sector number and the IV in the AAD region so we
> +        * have to splice the converted IV into the subrequest that we pass on
> +        * to the AEAD transform. This means we are tightly coupled to dm-crypt,
> +        * but that should be the only user of this code in AEAD mode.
> +        */
> +       if (ssize < 0 || sg_nents_for_len(req->src, ssize) != 1)
> +               return -EINVAL;
> +
> +       memset(rctx->iv, 0, ivsize);
> +       memcpy(rctx->iv, req->iv, crypto_aead_ivsize(tfm));
> +
> +       crypto_cipher_encrypt_one(tctx->essiv_cipher, rctx->iv, rctx->iv);
> +
> +       sg_init_table(rctx->src, 4);
> +       sg_set_page(rctx->src, sg_page(req->src), ssize, req->src->offset);
> +       sg_set_buf(rctx->src + 1, rctx->iv, ivsize);
> +       sg = scatterwalk_ffwd(rctx->src + 2, req->src, req->assoclen);
> +       if (sg != rctx->src + 2)
> +               sg_chain(rctx->src, 3, sg);
> +
> +       sg_init_table(rctx->dst, 4);
> +       sg_set_page(rctx->dst, sg_page(req->dst), ssize, req->dst->offset);
> +       sg_set_buf(rctx->dst + 1, rctx->iv, ivsize);
> +       sg = scatterwalk_ffwd(rctx->dst + 2, req->dst, req->assoclen);
> +       if (sg != rctx->dst + 2)
> +               sg_chain(rctx->dst, 3, sg);
> +
> +       aead_request_set_tfm(subreq, tctx->u.aead);
> +       aead_request_set_crypt(subreq, rctx->src, rctx->dst, req->cryptlen,
> +                              rctx->iv);
> +       aead_request_set_ad(subreq, ssize + ivsize);
> +       aead_request_set_callback(subreq, req->base.flags, essiv_aead_done, req);
> +
> +       return 0;
> +}
> +
> +static int essiv_skcipher_encrypt(struct skcipher_request *req)
> +{
> +       struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +
> +       essiv_skcipher_prepare_subreq(req);
> +       return crypto_skcipher_encrypt(&rctx->blockcipher_req);
> +}
> +
> +static int essiv_aead_encrypt(struct aead_request *req)
> +{
> +       struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
> +       int err;
> +
> +       err = essiv_aead_prepare_subreq(req);
> +       if (err)
> +               return err;
> +       return crypto_aead_encrypt(&rctx->aead_req);
> +}
> +
> +static int essiv_skcipher_decrypt(struct skcipher_request *req)
> +{
> +       struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +       return crypto_skcipher_decrypt(&rctx->blockcipher_req);
> +}
> +
> +static int essiv_aead_decrypt(struct aead_request *req)
> +{
> +       struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
> +       int err;
> +
> +       err = essiv_aead_prepare_subreq(req);
> +       if (err)
> +               return err;
> +
> +       essiv_aead_prepare_subreq(req);
> +       return crypto_aead_decrypt(&rctx->aead_req);
> +}
> +
> +static int essiv_init_tfm(struct essiv_instance_ctx *ictx,
> +                         struct essiv_tfm_ctx *tctx)
> +{
> +       struct crypto_cipher *essiv_cipher;
> +       struct crypto_shash *hash;
> +       int err;
> +
> +       essiv_cipher = crypto_spawn_cipher(&ictx->essiv_cipher_spawn);
> +       if (IS_ERR(essiv_cipher))
> +               return PTR_ERR(essiv_cipher);
> +
> +       hash = crypto_spawn_shash(&ictx->hash_spawn);
> +       if (IS_ERR(hash)) {
> +               err = PTR_ERR(hash);
> +               goto err_free_essiv_cipher;
> +       }
> +
> +       tctx->essiv_cipher = essiv_cipher;
> +       tctx->hash = hash;
> +
> +       return 0;
> +
> +err_free_essiv_cipher:
> +       crypto_free_cipher(essiv_cipher);
> +       return err;
> +}
> +
> +static int essiv_skcipher_init_tfm(struct crypto_skcipher *tfm)
> +{
> +       struct skcipher_instance *inst = skcipher_alg_instance(tfm);
> +       struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
> +       struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +       struct crypto_skcipher *blockcipher;
> +       unsigned int subreq_size;
> +       int err;
> +
> +       BUILD_BUG_ON(offsetofend(struct essiv_skcipher_request_ctx,
> +                                blockcipher_req) !=
> +                    sizeof(struct essiv_skcipher_request_ctx));
> +
> +       blockcipher = crypto_spawn_skcipher(&ictx->u.blockcipher_spawn);
> +       if (IS_ERR(blockcipher))
> +               return PTR_ERR(blockcipher);
> +
> +       subreq_size = FIELD_SIZEOF(struct essiv_skcipher_request_ctx,
> +                                  blockcipher_req) +
> +                     crypto_skcipher_reqsize(blockcipher);
> +
> +       crypto_skcipher_set_reqsize(tfm, offsetof(struct essiv_skcipher_request_ctx,
> +                                                 blockcipher_req) + subreq_size);
> +
> +       err = essiv_init_tfm(ictx, tctx);
> +       if (err)
> +               crypto_free_skcipher(blockcipher);
> +
> +       tctx->u.blockcipher = blockcipher;
> +       return err;
> +}
> +
> +static int essiv_aead_init_tfm(struct crypto_aead *tfm)
> +{
> +       struct aead_instance *inst = aead_alg_instance(tfm);
> +       struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +       struct crypto_aead *aead;
> +       unsigned int subreq_size;
> +       int err;
> +
> +       BUILD_BUG_ON(offsetofend(struct essiv_aead_request_ctx, aead_req) !=
> +                    sizeof(struct essiv_aead_request_ctx));
> +
> +       aead = crypto_spawn_aead(&ictx->u.aead_spawn);
> +       if (IS_ERR(aead))
> +               return PTR_ERR(aead);
> +
> +       subreq_size = FIELD_SIZEOF(struct essiv_aead_request_ctx, aead_req) +
> +                     crypto_aead_reqsize(aead);
> +
> +       crypto_aead_set_reqsize(tfm, offsetof(struct essiv_aead_request_ctx,
> +                                             aead_req) + subreq_size);
> +
> +       err = essiv_init_tfm(ictx, tctx);
> +       if (err)
> +               crypto_free_aead(aead);
> +
> +       tctx->u.aead = aead;
> +       return err;
> +}
> +
> +static void essiv_skcipher_exit_tfm(struct crypto_skcipher *tfm)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +
> +       crypto_free_skcipher(tctx->u.blockcipher);
> +       crypto_free_cipher(tctx->essiv_cipher);
> +       crypto_free_shash(tctx->hash);
> +}
> +
> +static void essiv_aead_exit_tfm(struct crypto_aead *tfm)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +
> +       crypto_free_aead(tctx->u.aead);
> +       crypto_free_cipher(tctx->essiv_cipher);
> +       crypto_free_shash(tctx->hash);
> +}
> +
> +static void essiv_skcipher_free_instance(struct skcipher_instance *inst)
> +{
> +       struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
> +
> +       crypto_drop_skcipher(&ictx->u.blockcipher_spawn);
> +       crypto_drop_spawn(&ictx->essiv_cipher_spawn);
> +       crypto_drop_shash(&ictx->hash_spawn);
> +       kfree(inst);
> +}
> +
> +static void essiv_aead_free_instance(struct aead_instance *inst)
> +{
> +       struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
> +
> +       crypto_drop_aead(&ictx->u.aead_spawn);
> +       crypto_drop_spawn(&ictx->essiv_cipher_spawn);
> +       crypto_drop_shash(&ictx->hash_spawn);
> +       kfree(inst);
> +}
> +
> +static bool essiv_supported_algorithms(struct crypto_alg *essiv_cipher_alg,
> +                                      struct shash_alg *hash_alg,
> +                                      int ivsize)
> +{
> +       if (hash_alg->digestsize < essiv_cipher_alg->cra_cipher.cia_min_keysize ||
> +           hash_alg->digestsize > essiv_cipher_alg->cra_cipher.cia_max_keysize)
> +               return false;
> +
> +       if (ivsize != essiv_cipher_alg->cra_blocksize)
> +               return false;
> +
> +       if (ivsize > MAX_INNER_IV_SIZE)
> +               return false;
> +
> +       return true;
> +}
> +
> +static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb)
> +{
> +       struct crypto_attr_type *algt;
> +       const char *blockcipher_name;
> +       const char *essiv_cipher_name;
> +       const char *shash_name;
> +       struct skcipher_instance *skcipher_inst = NULL;
> +       struct aead_instance *aead_inst = NULL;
> +       struct crypto_instance *inst;
> +       struct crypto_alg *base, *block_base;
> +       struct essiv_instance_ctx *ictx;
> +       struct skcipher_alg *blockcipher_alg = NULL;
> +       struct aead_alg *aead_alg = NULL;
> +       struct crypto_alg *essiv_cipher_alg;
> +       struct crypto_alg *_hash_alg;
> +       struct shash_alg *hash_alg;
> +       int ivsize;
> +       u32 type;
> +       int err;
> +
> +       algt = crypto_get_attr_type(tb);
> +       if (IS_ERR(algt))
> +               return PTR_ERR(algt);
> +
> +       blockcipher_name = crypto_attr_alg_name(tb[1]);
> +       if (IS_ERR(blockcipher_name))
> +               return PTR_ERR(blockcipher_name);
> +
> +       essiv_cipher_name = crypto_attr_alg_name(tb[2]);
> +       if (IS_ERR(essiv_cipher_name))
> +               return PTR_ERR(essiv_cipher_name);
> +
> +       shash_name = crypto_attr_alg_name(tb[3]);
> +       if (IS_ERR(shash_name))
> +               return PTR_ERR(shash_name);
> +
> +       type = algt->type & algt->mask;
> +
> +       switch (type) {
> +       case CRYPTO_ALG_TYPE_BLKCIPHER:
> +               skcipher_inst = kzalloc(sizeof(*skcipher_inst) +
> +                                       sizeof(*ictx), GFP_KERNEL);
> +               if (!skcipher_inst)
> +                       return -ENOMEM;
> +               inst = skcipher_crypto_instance(skcipher_inst);
> +               base = &skcipher_inst->alg.base;
> +               ictx = crypto_instance_ctx(inst);
> +
> +               /* Block cipher, e.g. "cbc(aes)" */
> +               crypto_set_skcipher_spawn(&ictx->u.blockcipher_spawn, inst);
> +               err = crypto_grab_skcipher(&ictx->u.blockcipher_spawn,
> +                                          blockcipher_name, 0,
> +                                          crypto_requires_sync(algt->type,
> +                                                               algt->mask));
> +               if (err)
> +                       goto out_free_inst;
> +               blockcipher_alg = crypto_spawn_skcipher_alg(&ictx->u.blockcipher_spawn);
> +               block_base = &blockcipher_alg->base;
> +               ivsize = blockcipher_alg->ivsize;
> +               break;
> +
> +       case CRYPTO_ALG_TYPE_AEAD:
> +               aead_inst = kzalloc(sizeof(*aead_inst) +
> +                                   sizeof(*ictx), GFP_KERNEL);
> +               if (!aead_inst)
> +                       return -ENOMEM;
> +               inst = aead_crypto_instance(aead_inst);
> +               base = &aead_inst->alg.base;
> +               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,
> +                                      blockcipher_name, 0,
> +                                      crypto_requires_sync(algt->type,
> +                                                           algt->mask));
> +               if (err)
> +                       goto out_free_inst;
> +               aead_alg = crypto_spawn_aead_alg(&ictx->u.aead_spawn);
> +               block_base = &aead_alg->base;
> +               ivsize = aead_alg->ivsize;
> +               break;
> +
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       /* Block cipher, e.g. "aes" */
> +       crypto_set_spawn(&ictx->essiv_cipher_spawn, inst);
> +       err = crypto_grab_spawn(&ictx->essiv_cipher_spawn, essiv_cipher_name,
> +                               CRYPTO_ALG_TYPE_CIPHER, CRYPTO_ALG_TYPE_MASK);
> +       if (err)
> +               goto out_drop_blockcipher;
> +       essiv_cipher_alg = ictx->essiv_cipher_spawn.alg;
> +
> +       /* Synchronous hash, e.g., "sha256" */
> +       _hash_alg = crypto_alg_mod_lookup(shash_name,
> +                                         CRYPTO_ALG_TYPE_SHASH,
> +                                         CRYPTO_ALG_TYPE_MASK);
> +       if (IS_ERR(_hash_alg)) {
> +               err = PTR_ERR(_hash_alg);
> +               goto out_drop_essiv_cipher;
> +       }
> +       hash_alg = __crypto_shash_alg(_hash_alg);
> +       err = crypto_init_shash_spawn(&ictx->hash_spawn, hash_alg, inst);
> +       if (err)
> +               goto out_put_hash;
> +
> +       /* Check the set of algorithms */
> +       if (!essiv_supported_algorithms(essiv_cipher_alg, hash_alg, ivsize)) {
> +               pr_warn("Unsupported essiv instantiation: (%s,%s,%s)\n",
> +                       block_base->cra_name,
> +                       essiv_cipher_alg->cra_name,
> +                       hash_alg->base.cra_name);
> +               err = -EINVAL;
> +               goto out_drop_hash;
> +       }
> +
> +       /* Instance fields */
> +
> +       err = -ENAMETOOLONG;
> +       if (snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME,
> +                    "essiv(%s,%s,%s)", block_base->cra_name,
> +                    essiv_cipher_alg->cra_name,
> +                    hash_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
> +               goto out_drop_hash;
> +       if (snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME,
> +                    "essiv(%s,%s,%s)",
> +                    block_base->cra_driver_name,
> +                    essiv_cipher_alg->cra_driver_name,
> +                    hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
> +               goto out_drop_hash;
> +
> +       base->cra_flags         = block_base->cra_flags & CRYPTO_ALG_ASYNC;
> +       base->cra_blocksize     = block_base->cra_blocksize;
> +       base->cra_ctxsize       = sizeof(struct essiv_tfm_ctx);
> +       base->cra_alignmask     = block_base->cra_alignmask;
> +       base->cra_priority      = block_base->cra_priority;
> +
> +       if (type == CRYPTO_ALG_TYPE_BLKCIPHER) {
> +               skcipher_inst->alg.setkey       = essiv_skcipher_setkey;
> +               skcipher_inst->alg.encrypt      = essiv_skcipher_encrypt;
> +               skcipher_inst->alg.decrypt      = essiv_skcipher_decrypt;
> +               skcipher_inst->alg.init         = essiv_skcipher_init_tfm;
> +               skcipher_inst->alg.exit         = essiv_skcipher_exit_tfm;
> +
> +               skcipher_inst->alg.min_keysize  = crypto_skcipher_alg_min_keysize(blockcipher_alg);
> +               skcipher_inst->alg.max_keysize  = crypto_skcipher_alg_max_keysize(blockcipher_alg);
> +               skcipher_inst->alg.ivsize       = ESSIV_IV_SIZE;
> +               skcipher_inst->alg.chunksize    = blockcipher_alg->chunksize;
> +               skcipher_inst->alg.walksize     = blockcipher_alg->walksize;
> +
> +               skcipher_inst->free             = essiv_skcipher_free_instance;
> +
> +               err = skcipher_register_instance(tmpl, skcipher_inst);
> +       } else {
> +               aead_inst->alg.setkey           = essiv_aead_setkey;
> +               aead_inst->alg.setauthsize      = essiv_aead_setauthsize;
> +               aead_inst->alg.encrypt          = essiv_aead_encrypt;
> +               aead_inst->alg.decrypt          = essiv_aead_decrypt;
> +               aead_inst->alg.init             = essiv_aead_init_tfm;
> +               aead_inst->alg.exit             = essiv_aead_exit_tfm;
> +
> +               aead_inst->alg.ivsize           = ESSIV_IV_SIZE;
> +               aead_inst->alg.maxauthsize      = aead_alg->maxauthsize;
> +               aead_inst->alg.chunksize        = aead_alg->chunksize;
> +
> +               aead_inst->free                 = essiv_aead_free_instance;
> +
> +               err = aead_register_instance(tmpl, aead_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_essiv_cipher:
> +       crypto_drop_spawn(&ictx->essiv_cipher_spawn);
> +out_drop_blockcipher:
> +       if (type == CRYPTO_ALG_TYPE_BLKCIPHER) {
> +               crypto_drop_skcipher(&ictx->u.blockcipher_spawn);
> +       } else {
> +               crypto_drop_aead(&ictx->u.aead_spawn);
> +       }
> +out_free_inst:
> +       kfree(skcipher_inst);
> +       kfree(aead_inst);
> +       return err;
> +}
> +
> +/* essiv(blockcipher_name, essiv_cipher_name, shash_name) */
> +static struct crypto_template essiv_tmpl = {
> +       .name   = "essiv",
> +       .create = essiv_create,
> +       .module = THIS_MODULE,
> +};
> +
> +static int __init essiv_module_init(void)
> +{
> +       return crypto_register_template(&essiv_tmpl);
> +}
> +
> +static void __exit essiv_module_exit(void)
> +{
> +       crypto_unregister_template(&essiv_tmpl);
> +}
> +
> +subsys_initcall(essiv_module_init);
> +module_exit(essiv_module_exit);
> +
> +MODULE_DESCRIPTION("ESSIV skcipher/aead wrapper for block encryption");
> +MODULE_LICENSE("GPL v2");
> +MODULE_ALIAS_CRYPTO("essiv");
> --
> 2.17.1
>

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 1/4] crypto: essiv - create wrapper template for ESSIV generation
@ 2019-06-19 15:18     ` Ondrej Mosnáček
  0 siblings, 0 replies; 45+ messages in thread
From: Ondrej Mosnáček @ 2019-06-19 15:18 UTC (permalink / raw)
  To: Ard Biesheuvel
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	dm-devel, linux-crypto, Milan Broz

Hi Ard,

ut 18. 6. 2019 o 23:28 Ard Biesheuvel <ard.biesheuvel@linaro.org> napísal(a):
> Implement a template that wraps a (skcipher,cipher,shash) or
> (aead,cipher,shash) tuple so that we can consolidate the ESSIV handling
> in fscrypt and dm-crypt and move it into the crypto API. This will result
> in better test coverage, and will allow future changes to make the bare
> cipher interface internal to the crypto subsystem, in order to increase
> robustness of the API against misuse.
>
> Note that especially the AEAD handling is a bit complex, and is tightly
> coupled to the way dm-crypt combines AEAD based on the authenc() template
> with the ESSIV handling.

Wouldn't it work better to have a template only for skcipher and in
dm-crypt just inject the essiv() template into the cipher string? For
example: "authenc(hmac(sha256),cbc(aes))-essiv:sha256" ->
"authenc(hmac(sha256),essiv(cbc(aes),aes,sha256))". That seems to me a
much simpler hack. (But maybe I'm missing some issue in that
approach...)


>
> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> ---
>  crypto/Kconfig  |   4 +
>  crypto/Makefile |   1 +
>  crypto/essiv.c  | 624 ++++++++++++++++++++
>  3 files changed, 629 insertions(+)
>
> diff --git a/crypto/Kconfig b/crypto/Kconfig
> index 3d056e7da65f..1aa47087c1a2 100644
> --- a/crypto/Kconfig
> +++ b/crypto/Kconfig
> @@ -1917,6 +1917,10 @@ config CRYPTO_STATS
>  config CRYPTO_HASH_INFO
>         bool
>
> +config CRYPTO_ESSIV
> +       tristate
> +       select CRYPTO_AUTHENC
> +
>  source "drivers/crypto/Kconfig"
>  source "crypto/asymmetric_keys/Kconfig"
>  source "certs/Kconfig"
> diff --git a/crypto/Makefile b/crypto/Makefile
> index 266a4cdbb9e2..ad1d99ba6d56 100644
> --- a/crypto/Makefile
> +++ b/crypto/Makefile
> @@ -148,6 +148,7 @@ obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o
>  obj-$(CONFIG_CRYPTO_ZSTD) += zstd.o
>  obj-$(CONFIG_CRYPTO_OFB) += ofb.o
>  obj-$(CONFIG_CRYPTO_ECC) += ecc.o
> +obj-$(CONFIG_CRYPTO_ESSIV) += essiv.o
>
>  ecdh_generic-y += ecdh.o
>  ecdh_generic-y += ecdh_helper.o
> diff --git a/crypto/essiv.c b/crypto/essiv.c
> new file mode 100644
> index 000000000000..029a65afb4d7
> --- /dev/null
> +++ b/crypto/essiv.c
> @@ -0,0 +1,624 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * ESSIV skcipher template for block encryption
> + *
> + * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
> + *
> + * Heavily based on:
> + * adiantum length-preserving encryption mode
> + *
> + * Copyright 2018 Google LLC
> + */
> +
> +#include <crypto/authenc.h>
> +#include <crypto/internal/aead.h>
> +#include <crypto/internal/hash.h>
> +#include <crypto/internal/skcipher.h>
> +#include <crypto/scatterwalk.h>
> +#include <linux/module.h>
> +
> +#include "internal.h"
> +
> +#define ESSIV_IV_SIZE          sizeof(u64)     // IV size of the outer algo
> +#define MAX_INNER_IV_SIZE      16              // max IV size of inner algo
> +
> +struct essiv_instance_ctx {
> +       union {
> +               struct crypto_skcipher_spawn    blockcipher_spawn;
> +               struct crypto_aead_spawn        aead_spawn;
> +       } u;
> +       struct crypto_spawn                     essiv_cipher_spawn;
> +       struct crypto_shash_spawn               hash_spawn;
> +};
> +
> +struct essiv_tfm_ctx {
> +       union {
> +               struct crypto_skcipher  *blockcipher;
> +               struct crypto_aead      *aead;
> +       } u;
> +       struct crypto_cipher            *essiv_cipher;
> +       struct crypto_shash             *hash;
> +};
> +
> +struct essiv_skcipher_request_ctx {
> +       u8                              iv[MAX_INNER_IV_SIZE];
> +       struct skcipher_request         blockcipher_req;
> +};
> +
> +struct essiv_aead_request_ctx {
> +       u8                              iv[MAX_INNER_IV_SIZE];
> +       struct scatterlist              src[4], dst[4];
> +       struct aead_request             aead_req;
> +};
> +
> +static int essiv_skcipher_setkey(struct crypto_skcipher *tfm,
> +                                const u8 *key, unsigned int keylen)
> +{
> +       u32 flags = crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_REQ_MASK;
> +       struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +       SHASH_DESC_ON_STACK(desc, tctx->hash);
> +       unsigned int saltsize;
> +       u8 *salt;
> +       int err;
> +
> +       crypto_skcipher_clear_flags(tctx->u.blockcipher, CRYPTO_TFM_REQ_MASK);
> +       crypto_skcipher_set_flags(tctx->u.blockcipher, flags);
> +       err = crypto_skcipher_setkey(tctx->u.blockcipher, key, keylen);
> +       crypto_skcipher_set_flags(tfm,
> +                                 crypto_skcipher_get_flags(tctx->u.blockcipher) &
> +                                 CRYPTO_TFM_RES_MASK);
> +       if (err)
> +               return err;
> +
> +       saltsize = crypto_shash_digestsize(tctx->hash);
> +       salt = kmalloc(saltsize, GFP_KERNEL);
> +       if (!salt)
> +               return -ENOMEM;
> +
> +       desc->tfm = tctx->hash;
> +       crypto_shash_digest(desc, key, keylen, salt);
> +
> +       crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
> +       crypto_cipher_set_flags(tctx->essiv_cipher, flags & CRYPTO_TFM_REQ_MASK);
> +       err = crypto_cipher_setkey(tctx->essiv_cipher, salt, saltsize);
> +       flags = crypto_cipher_get_flags(tctx->essiv_cipher) & CRYPTO_TFM_RES_MASK;
> +       crypto_skcipher_set_flags(tfm, flags);
> +
> +       kzfree(salt);
> +       return err;
> +}
> +
> +static int essiv_aead_setkey(struct crypto_aead *tfm, const u8 *key,
> +                            unsigned int keylen)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +       SHASH_DESC_ON_STACK(desc, tctx->hash);
> +       struct crypto_authenc_keys keys;
> +       unsigned int saltsize;
> +       u8 *salt;
> +       int err;
> +
> +       crypto_aead_clear_flags(tctx->u.aead, CRYPTO_TFM_REQ_MASK);
> +       crypto_aead_set_flags(tctx->u.aead, crypto_aead_get_flags(tfm) &
> +                                           CRYPTO_TFM_REQ_MASK);
> +       err = crypto_aead_setkey(tctx->u.aead, key, keylen);
> +       crypto_aead_set_flags(tfm, crypto_aead_get_flags(tctx->u.aead) &
> +                                  CRYPTO_TFM_RES_MASK);
> +       if (err)
> +               return err;
> +
> +       if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
> +               crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
> +               return -EINVAL;
> +       }
> +
> +       saltsize = crypto_shash_digestsize(tctx->hash);
> +       salt = kmalloc(saltsize, GFP_KERNEL);
> +       if (!salt)
> +               return -ENOMEM;
> +
> +       desc->tfm = tctx->hash;
> +       crypto_shash_init(desc);
> +       crypto_shash_update(desc, keys.enckey, keys.enckeylen);
> +       crypto_shash_finup(desc, keys.authkey, keys.authkeylen, salt);
> +
> +       crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
> +       crypto_cipher_set_flags(tctx->essiv_cipher, crypto_aead_get_flags(tfm) &
> +                                                   CRYPTO_TFM_REQ_MASK);
> +       err = crypto_cipher_setkey(tctx->essiv_cipher, salt, saltsize);
> +       crypto_aead_set_flags(tfm, crypto_cipher_get_flags(tctx->essiv_cipher) &
> +                                  CRYPTO_TFM_RES_MASK);
> +
> +       kzfree(salt);
> +       return err;
> +}
> +
> +static int essiv_aead_setauthsize(struct crypto_aead *tfm,
> +                                 unsigned int authsize)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +
> +       return crypto_aead_setauthsize(tctx->u.aead, authsize);
> +}
> +
> +static void essiv_skcipher_done(struct crypto_async_request *areq, int err)
> +{
> +       struct skcipher_request *req = areq->data;
> +
> +       skcipher_request_complete(req, err);
> +}
> +
> +static void essiv_aead_done(struct crypto_async_request *areq, int err)
> +{
> +       struct aead_request *req = areq->data;
> +
> +       aead_request_complete(req, err);
> +}
> +
> +static void essiv_skcipher_prepare_subreq(struct skcipher_request *req)
> +{
> +       struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
> +       const struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +       struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +       struct skcipher_request *subreq = &rctx->blockcipher_req;
> +
> +       memset(rctx->iv, 0, crypto_cipher_blocksize(tctx->essiv_cipher));
> +       memcpy(rctx->iv, req->iv, crypto_skcipher_ivsize(tfm));
> +
> +       crypto_cipher_encrypt_one(tctx->essiv_cipher, rctx->iv, rctx->iv);
> +
> +       skcipher_request_set_tfm(subreq, tctx->u.blockcipher);
> +       skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
> +                                  rctx->iv);
> +       skcipher_request_set_callback(subreq, req->base.flags,
> +                                     essiv_skcipher_done, req);
> +}
> +
> +static int essiv_aead_prepare_subreq(struct aead_request *req)
> +{
> +       struct crypto_aead *tfm = crypto_aead_reqtfm(req);
> +       const struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +       struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
> +       int ivsize = crypto_cipher_blocksize(tctx->essiv_cipher);
> +       int ssize = req->assoclen - crypto_aead_ivsize(tfm);
> +       struct aead_request *subreq = &rctx->aead_req;
> +       struct scatterlist *sg;
> +
> +       /*
> +        * dm-crypt embeds the sector number and the IV in the AAD region so we
> +        * have to splice the converted IV into the subrequest that we pass on
> +        * to the AEAD transform. This means we are tightly coupled to dm-crypt,
> +        * but that should be the only user of this code in AEAD mode.
> +        */
> +       if (ssize < 0 || sg_nents_for_len(req->src, ssize) != 1)
> +               return -EINVAL;
> +
> +       memset(rctx->iv, 0, ivsize);
> +       memcpy(rctx->iv, req->iv, crypto_aead_ivsize(tfm));
> +
> +       crypto_cipher_encrypt_one(tctx->essiv_cipher, rctx->iv, rctx->iv);
> +
> +       sg_init_table(rctx->src, 4);
> +       sg_set_page(rctx->src, sg_page(req->src), ssize, req->src->offset);
> +       sg_set_buf(rctx->src + 1, rctx->iv, ivsize);
> +       sg = scatterwalk_ffwd(rctx->src + 2, req->src, req->assoclen);
> +       if (sg != rctx->src + 2)
> +               sg_chain(rctx->src, 3, sg);
> +
> +       sg_init_table(rctx->dst, 4);
> +       sg_set_page(rctx->dst, sg_page(req->dst), ssize, req->dst->offset);
> +       sg_set_buf(rctx->dst + 1, rctx->iv, ivsize);
> +       sg = scatterwalk_ffwd(rctx->dst + 2, req->dst, req->assoclen);
> +       if (sg != rctx->dst + 2)
> +               sg_chain(rctx->dst, 3, sg);
> +
> +       aead_request_set_tfm(subreq, tctx->u.aead);
> +       aead_request_set_crypt(subreq, rctx->src, rctx->dst, req->cryptlen,
> +                              rctx->iv);
> +       aead_request_set_ad(subreq, ssize + ivsize);
> +       aead_request_set_callback(subreq, req->base.flags, essiv_aead_done, req);
> +
> +       return 0;
> +}
> +
> +static int essiv_skcipher_encrypt(struct skcipher_request *req)
> +{
> +       struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +
> +       essiv_skcipher_prepare_subreq(req);
> +       return crypto_skcipher_encrypt(&rctx->blockcipher_req);
> +}
> +
> +static int essiv_aead_encrypt(struct aead_request *req)
> +{
> +       struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
> +       int err;
> +
> +       err = essiv_aead_prepare_subreq(req);
> +       if (err)
> +               return err;
> +       return crypto_aead_encrypt(&rctx->aead_req);
> +}
> +
> +static int essiv_skcipher_decrypt(struct skcipher_request *req)
> +{
> +       struct essiv_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
> +       return crypto_skcipher_decrypt(&rctx->blockcipher_req);
> +}
> +
> +static int essiv_aead_decrypt(struct aead_request *req)
> +{
> +       struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
> +       int err;
> +
> +       err = essiv_aead_prepare_subreq(req);
> +       if (err)
> +               return err;
> +
> +       essiv_aead_prepare_subreq(req);
> +       return crypto_aead_decrypt(&rctx->aead_req);
> +}
> +
> +static int essiv_init_tfm(struct essiv_instance_ctx *ictx,
> +                         struct essiv_tfm_ctx *tctx)
> +{
> +       struct crypto_cipher *essiv_cipher;
> +       struct crypto_shash *hash;
> +       int err;
> +
> +       essiv_cipher = crypto_spawn_cipher(&ictx->essiv_cipher_spawn);
> +       if (IS_ERR(essiv_cipher))
> +               return PTR_ERR(essiv_cipher);
> +
> +       hash = crypto_spawn_shash(&ictx->hash_spawn);
> +       if (IS_ERR(hash)) {
> +               err = PTR_ERR(hash);
> +               goto err_free_essiv_cipher;
> +       }
> +
> +       tctx->essiv_cipher = essiv_cipher;
> +       tctx->hash = hash;
> +
> +       return 0;
> +
> +err_free_essiv_cipher:
> +       crypto_free_cipher(essiv_cipher);
> +       return err;
> +}
> +
> +static int essiv_skcipher_init_tfm(struct crypto_skcipher *tfm)
> +{
> +       struct skcipher_instance *inst = skcipher_alg_instance(tfm);
> +       struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
> +       struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +       struct crypto_skcipher *blockcipher;
> +       unsigned int subreq_size;
> +       int err;
> +
> +       BUILD_BUG_ON(offsetofend(struct essiv_skcipher_request_ctx,
> +                                blockcipher_req) !=
> +                    sizeof(struct essiv_skcipher_request_ctx));
> +
> +       blockcipher = crypto_spawn_skcipher(&ictx->u.blockcipher_spawn);
> +       if (IS_ERR(blockcipher))
> +               return PTR_ERR(blockcipher);
> +
> +       subreq_size = FIELD_SIZEOF(struct essiv_skcipher_request_ctx,
> +                                  blockcipher_req) +
> +                     crypto_skcipher_reqsize(blockcipher);
> +
> +       crypto_skcipher_set_reqsize(tfm, offsetof(struct essiv_skcipher_request_ctx,
> +                                                 blockcipher_req) + subreq_size);
> +
> +       err = essiv_init_tfm(ictx, tctx);
> +       if (err)
> +               crypto_free_skcipher(blockcipher);
> +
> +       tctx->u.blockcipher = blockcipher;
> +       return err;
> +}
> +
> +static int essiv_aead_init_tfm(struct crypto_aead *tfm)
> +{
> +       struct aead_instance *inst = aead_alg_instance(tfm);
> +       struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +       struct crypto_aead *aead;
> +       unsigned int subreq_size;
> +       int err;
> +
> +       BUILD_BUG_ON(offsetofend(struct essiv_aead_request_ctx, aead_req) !=
> +                    sizeof(struct essiv_aead_request_ctx));
> +
> +       aead = crypto_spawn_aead(&ictx->u.aead_spawn);
> +       if (IS_ERR(aead))
> +               return PTR_ERR(aead);
> +
> +       subreq_size = FIELD_SIZEOF(struct essiv_aead_request_ctx, aead_req) +
> +                     crypto_aead_reqsize(aead);
> +
> +       crypto_aead_set_reqsize(tfm, offsetof(struct essiv_aead_request_ctx,
> +                                             aead_req) + subreq_size);
> +
> +       err = essiv_init_tfm(ictx, tctx);
> +       if (err)
> +               crypto_free_aead(aead);
> +
> +       tctx->u.aead = aead;
> +       return err;
> +}
> +
> +static void essiv_skcipher_exit_tfm(struct crypto_skcipher *tfm)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
> +
> +       crypto_free_skcipher(tctx->u.blockcipher);
> +       crypto_free_cipher(tctx->essiv_cipher);
> +       crypto_free_shash(tctx->hash);
> +}
> +
> +static void essiv_aead_exit_tfm(struct crypto_aead *tfm)
> +{
> +       struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
> +
> +       crypto_free_aead(tctx->u.aead);
> +       crypto_free_cipher(tctx->essiv_cipher);
> +       crypto_free_shash(tctx->hash);
> +}
> +
> +static void essiv_skcipher_free_instance(struct skcipher_instance *inst)
> +{
> +       struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
> +
> +       crypto_drop_skcipher(&ictx->u.blockcipher_spawn);
> +       crypto_drop_spawn(&ictx->essiv_cipher_spawn);
> +       crypto_drop_shash(&ictx->hash_spawn);
> +       kfree(inst);
> +}
> +
> +static void essiv_aead_free_instance(struct aead_instance *inst)
> +{
> +       struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
> +
> +       crypto_drop_aead(&ictx->u.aead_spawn);
> +       crypto_drop_spawn(&ictx->essiv_cipher_spawn);
> +       crypto_drop_shash(&ictx->hash_spawn);
> +       kfree(inst);
> +}
> +
> +static bool essiv_supported_algorithms(struct crypto_alg *essiv_cipher_alg,
> +                                      struct shash_alg *hash_alg,
> +                                      int ivsize)
> +{
> +       if (hash_alg->digestsize < essiv_cipher_alg->cra_cipher.cia_min_keysize ||
> +           hash_alg->digestsize > essiv_cipher_alg->cra_cipher.cia_max_keysize)
> +               return false;
> +
> +       if (ivsize != essiv_cipher_alg->cra_blocksize)
> +               return false;
> +
> +       if (ivsize > MAX_INNER_IV_SIZE)
> +               return false;
> +
> +       return true;
> +}
> +
> +static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb)
> +{
> +       struct crypto_attr_type *algt;
> +       const char *blockcipher_name;
> +       const char *essiv_cipher_name;
> +       const char *shash_name;
> +       struct skcipher_instance *skcipher_inst = NULL;
> +       struct aead_instance *aead_inst = NULL;
> +       struct crypto_instance *inst;
> +       struct crypto_alg *base, *block_base;
> +       struct essiv_instance_ctx *ictx;
> +       struct skcipher_alg *blockcipher_alg = NULL;
> +       struct aead_alg *aead_alg = NULL;
> +       struct crypto_alg *essiv_cipher_alg;
> +       struct crypto_alg *_hash_alg;
> +       struct shash_alg *hash_alg;
> +       int ivsize;
> +       u32 type;
> +       int err;
> +
> +       algt = crypto_get_attr_type(tb);
> +       if (IS_ERR(algt))
> +               return PTR_ERR(algt);
> +
> +       blockcipher_name = crypto_attr_alg_name(tb[1]);
> +       if (IS_ERR(blockcipher_name))
> +               return PTR_ERR(blockcipher_name);
> +
> +       essiv_cipher_name = crypto_attr_alg_name(tb[2]);
> +       if (IS_ERR(essiv_cipher_name))
> +               return PTR_ERR(essiv_cipher_name);
> +
> +       shash_name = crypto_attr_alg_name(tb[3]);
> +       if (IS_ERR(shash_name))
> +               return PTR_ERR(shash_name);
> +
> +       type = algt->type & algt->mask;
> +
> +       switch (type) {
> +       case CRYPTO_ALG_TYPE_BLKCIPHER:
> +               skcipher_inst = kzalloc(sizeof(*skcipher_inst) +
> +                                       sizeof(*ictx), GFP_KERNEL);
> +               if (!skcipher_inst)
> +                       return -ENOMEM;
> +               inst = skcipher_crypto_instance(skcipher_inst);
> +               base = &skcipher_inst->alg.base;
> +               ictx = crypto_instance_ctx(inst);
> +
> +               /* Block cipher, e.g. "cbc(aes)" */
> +               crypto_set_skcipher_spawn(&ictx->u.blockcipher_spawn, inst);
> +               err = crypto_grab_skcipher(&ictx->u.blockcipher_spawn,
> +                                          blockcipher_name, 0,
> +                                          crypto_requires_sync(algt->type,
> +                                                               algt->mask));
> +               if (err)
> +                       goto out_free_inst;
> +               blockcipher_alg = crypto_spawn_skcipher_alg(&ictx->u.blockcipher_spawn);
> +               block_base = &blockcipher_alg->base;
> +               ivsize = blockcipher_alg->ivsize;
> +               break;
> +
> +       case CRYPTO_ALG_TYPE_AEAD:
> +               aead_inst = kzalloc(sizeof(*aead_inst) +
> +                                   sizeof(*ictx), GFP_KERNEL);
> +               if (!aead_inst)
> +                       return -ENOMEM;
> +               inst = aead_crypto_instance(aead_inst);
> +               base = &aead_inst->alg.base;
> +               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,
> +                                      blockcipher_name, 0,
> +                                      crypto_requires_sync(algt->type,
> +                                                           algt->mask));
> +               if (err)
> +                       goto out_free_inst;
> +               aead_alg = crypto_spawn_aead_alg(&ictx->u.aead_spawn);
> +               block_base = &aead_alg->base;
> +               ivsize = aead_alg->ivsize;
> +               break;
> +
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       /* Block cipher, e.g. "aes" */
> +       crypto_set_spawn(&ictx->essiv_cipher_spawn, inst);
> +       err = crypto_grab_spawn(&ictx->essiv_cipher_spawn, essiv_cipher_name,
> +                               CRYPTO_ALG_TYPE_CIPHER, CRYPTO_ALG_TYPE_MASK);
> +       if (err)
> +               goto out_drop_blockcipher;
> +       essiv_cipher_alg = ictx->essiv_cipher_spawn.alg;
> +
> +       /* Synchronous hash, e.g., "sha256" */
> +       _hash_alg = crypto_alg_mod_lookup(shash_name,
> +                                         CRYPTO_ALG_TYPE_SHASH,
> +                                         CRYPTO_ALG_TYPE_MASK);
> +       if (IS_ERR(_hash_alg)) {
> +               err = PTR_ERR(_hash_alg);
> +               goto out_drop_essiv_cipher;
> +       }
> +       hash_alg = __crypto_shash_alg(_hash_alg);
> +       err = crypto_init_shash_spawn(&ictx->hash_spawn, hash_alg, inst);
> +       if (err)
> +               goto out_put_hash;
> +
> +       /* Check the set of algorithms */
> +       if (!essiv_supported_algorithms(essiv_cipher_alg, hash_alg, ivsize)) {
> +               pr_warn("Unsupported essiv instantiation: (%s,%s,%s)\n",
> +                       block_base->cra_name,
> +                       essiv_cipher_alg->cra_name,
> +                       hash_alg->base.cra_name);
> +               err = -EINVAL;
> +               goto out_drop_hash;
> +       }
> +
> +       /* Instance fields */
> +
> +       err = -ENAMETOOLONG;
> +       if (snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME,
> +                    "essiv(%s,%s,%s)", block_base->cra_name,
> +                    essiv_cipher_alg->cra_name,
> +                    hash_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
> +               goto out_drop_hash;
> +       if (snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME,
> +                    "essiv(%s,%s,%s)",
> +                    block_base->cra_driver_name,
> +                    essiv_cipher_alg->cra_driver_name,
> +                    hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
> +               goto out_drop_hash;
> +
> +       base->cra_flags         = block_base->cra_flags & CRYPTO_ALG_ASYNC;
> +       base->cra_blocksize     = block_base->cra_blocksize;
> +       base->cra_ctxsize       = sizeof(struct essiv_tfm_ctx);
> +       base->cra_alignmask     = block_base->cra_alignmask;
> +       base->cra_priority      = block_base->cra_priority;
> +
> +       if (type == CRYPTO_ALG_TYPE_BLKCIPHER) {
> +               skcipher_inst->alg.setkey       = essiv_skcipher_setkey;
> +               skcipher_inst->alg.encrypt      = essiv_skcipher_encrypt;
> +               skcipher_inst->alg.decrypt      = essiv_skcipher_decrypt;
> +               skcipher_inst->alg.init         = essiv_skcipher_init_tfm;
> +               skcipher_inst->alg.exit         = essiv_skcipher_exit_tfm;
> +
> +               skcipher_inst->alg.min_keysize  = crypto_skcipher_alg_min_keysize(blockcipher_alg);
> +               skcipher_inst->alg.max_keysize  = crypto_skcipher_alg_max_keysize(blockcipher_alg);
> +               skcipher_inst->alg.ivsize       = ESSIV_IV_SIZE;
> +               skcipher_inst->alg.chunksize    = blockcipher_alg->chunksize;
> +               skcipher_inst->alg.walksize     = blockcipher_alg->walksize;
> +
> +               skcipher_inst->free             = essiv_skcipher_free_instance;
> +
> +               err = skcipher_register_instance(tmpl, skcipher_inst);
> +       } else {
> +               aead_inst->alg.setkey           = essiv_aead_setkey;
> +               aead_inst->alg.setauthsize      = essiv_aead_setauthsize;
> +               aead_inst->alg.encrypt          = essiv_aead_encrypt;
> +               aead_inst->alg.decrypt          = essiv_aead_decrypt;
> +               aead_inst->alg.init             = essiv_aead_init_tfm;
> +               aead_inst->alg.exit             = essiv_aead_exit_tfm;
> +
> +               aead_inst->alg.ivsize           = ESSIV_IV_SIZE;
> +               aead_inst->alg.maxauthsize      = aead_alg->maxauthsize;
> +               aead_inst->alg.chunksize        = aead_alg->chunksize;
> +
> +               aead_inst->free                 = essiv_aead_free_instance;
> +
> +               err = aead_register_instance(tmpl, aead_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_essiv_cipher:
> +       crypto_drop_spawn(&ictx->essiv_cipher_spawn);
> +out_drop_blockcipher:
> +       if (type == CRYPTO_ALG_TYPE_BLKCIPHER) {
> +               crypto_drop_skcipher(&ictx->u.blockcipher_spawn);
> +       } else {
> +               crypto_drop_aead(&ictx->u.aead_spawn);
> +       }
> +out_free_inst:
> +       kfree(skcipher_inst);
> +       kfree(aead_inst);
> +       return err;
> +}
> +
> +/* essiv(blockcipher_name, essiv_cipher_name, shash_name) */
> +static struct crypto_template essiv_tmpl = {
> +       .name   = "essiv",
> +       .create = essiv_create,
> +       .module = THIS_MODULE,
> +};
> +
> +static int __init essiv_module_init(void)
> +{
> +       return crypto_register_template(&essiv_tmpl);
> +}
> +
> +static void __exit essiv_module_exit(void)
> +{
> +       crypto_unregister_template(&essiv_tmpl);
> +}
> +
> +subsys_initcall(essiv_module_init);
> +module_exit(essiv_module_exit);
> +
> +MODULE_DESCRIPTION("ESSIV skcipher/aead wrapper for block encryption");
> +MODULE_LICENSE("GPL v2");
> +MODULE_ALIAS_CRYPTO("essiv");
> --
> 2.17.1
>

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 1/4] crypto: essiv - create wrapper template for ESSIV generation
  2019-06-19 15:18     ` [dm-devel] " Ondrej Mosnáček
  (?)
@ 2019-06-19 15:45       ` Ard Biesheuvel
  -1 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 15:45 UTC (permalink / raw)
  To: Ondrej Mosnáček
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef, Milan Broz

On Wed, 19 Jun 2019 at 17:18, Ondrej Mosnáček <omosnacek@gmail.com> wrote:
>
> Hi Ard,
>
> ut 18. 6. 2019 o 23:28 Ard Biesheuvel <ard.biesheuvel@linaro.org> napísal(a):
> > Implement a template that wraps a (skcipher,cipher,shash) or
> > (aead,cipher,shash) tuple so that we can consolidate the ESSIV handling
> > in fscrypt and dm-crypt and move it into the crypto API. This will result
> > in better test coverage, and will allow future changes to make the bare
> > cipher interface internal to the crypto subsystem, in order to increase
> > robustness of the API against misuse.
> >
> > Note that especially the AEAD handling is a bit complex, and is tightly
> > coupled to the way dm-crypt combines AEAD based on the authenc() template
> > with the ESSIV handling.
>
> Wouldn't it work better to have a template only for skcipher and in
> dm-crypt just inject the essiv() template into the cipher string? For
> example: "authenc(hmac(sha256),cbc(aes))-essiv:sha256" ->
> "authenc(hmac(sha256),essiv(cbc(aes),aes,sha256))". That seems to me a
> much simpler hack. (But maybe I'm missing some issue in that
> approach...)
>

Unfortunately, that doesn't work. When using AEADs, dm-crypt also puts
the IV in the AAD area.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 1/4] crypto: essiv - create wrapper template for ESSIV generation
@ 2019-06-19 15:45       ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 15:45 UTC (permalink / raw)
  To: Ondrej Mosnáček
  Cc: open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Herbert Xu,
	Eric Biggers, device-mapper development, linux-fscrypt,
	Gilad Ben-Yossef, Milan Broz

On Wed, 19 Jun 2019 at 17:18, Ondrej Mosnáček <omosnacek@gmail.com> wrote:
>
> Hi Ard,
>
> ut 18. 6. 2019 o 23:28 Ard Biesheuvel <ard.biesheuvel@linaro.org> napísal(a):
> > Implement a template that wraps a (skcipher,cipher,shash) or
> > (aead,cipher,shash) tuple so that we can consolidate the ESSIV handling
> > in fscrypt and dm-crypt and move it into the crypto API. This will result
> > in better test coverage, and will allow future changes to make the bare
> > cipher interface internal to the crypto subsystem, in order to increase
> > robustness of the API against misuse.
> >
> > Note that especially the AEAD handling is a bit complex, and is tightly
> > coupled to the way dm-crypt combines AEAD based on the authenc() template
> > with the ESSIV handling.
>
> Wouldn't it work better to have a template only for skcipher and in
> dm-crypt just inject the essiv() template into the cipher string? For
> example: "authenc(hmac(sha256),cbc(aes))-essiv:sha256" ->
> "authenc(hmac(sha256),essiv(cbc(aes),aes,sha256))". That seems to me a
> much simpler hack. (But maybe I'm missing some issue in that
> approach...)
>

Unfortunately, that doesn't work. When using AEADs, dm-crypt also puts
the IV in the AAD area.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v2 1/4] crypto: essiv - create wrapper template for ESSIV generation
@ 2019-06-19 15:45       ` Ard Biesheuvel
  0 siblings, 0 replies; 45+ messages in thread
From: Ard Biesheuvel @ 2019-06-19 15:45 UTC (permalink / raw)
  To: Ondrej Mosnáček
  Cc: Herbert Xu, Eric Biggers, linux-fscrypt, Gilad Ben-Yossef,
	device-mapper development,
	open list:HARDWARE RANDOM NUMBER GENERATOR CORE, Milan Broz

On Wed, 19 Jun 2019 at 17:18, Ondrej Mosnáček <omosnacek@gmail.com> wrote:
>
> Hi Ard,
>
> ut 18. 6. 2019 o 23:28 Ard Biesheuvel <ard.biesheuvel@linaro.org> napísal(a):
> > Implement a template that wraps a (skcipher,cipher,shash) or
> > (aead,cipher,shash) tuple so that we can consolidate the ESSIV handling
> > in fscrypt and dm-crypt and move it into the crypto API. This will result
> > in better test coverage, and will allow future changes to make the bare
> > cipher interface internal to the crypto subsystem, in order to increase
> > robustness of the API against misuse.
> >
> > Note that especially the AEAD handling is a bit complex, and is tightly
> > coupled to the way dm-crypt combines AEAD based on the authenc() template
> > with the ESSIV handling.
>
> Wouldn't it work better to have a template only for skcipher and in
> dm-crypt just inject the essiv() template into the cipher string? For
> example: "authenc(hmac(sha256),cbc(aes))-essiv:sha256" ->
> "authenc(hmac(sha256),essiv(cbc(aes),aes,sha256))". That seems to me a
> much simpler hack. (But maybe I'm missing some issue in that
> approach...)
>

Unfortunately, that doesn't work. When using AEADs, dm-crypt also puts
the IV in the AAD area.

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2019-06-19 15:45 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-18 21:27 [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation Ard Biesheuvel
2019-06-18 21:27 ` Ard Biesheuvel
2019-06-18 21:27 ` [PATCH v2 1/4] crypto: essiv - create wrapper template " Ard Biesheuvel
2019-06-18 21:27   ` Ard Biesheuvel
2019-06-19 15:18   ` Ondrej Mosnáček
2019-06-19 15:18     ` Ondrej Mosnáček
2019-06-19 15:18     ` [dm-devel] " Ondrej Mosnáček
2019-06-19 15:45     ` Ard Biesheuvel
2019-06-19 15:45       ` Ard Biesheuvel
2019-06-19 15:45       ` Ard Biesheuvel
2019-06-18 21:27 ` [PATCH v2 2/4] fs: crypto: invoke crypto API for ESSIV handling Ard Biesheuvel
2019-06-18 21:27   ` Ard Biesheuvel
2019-06-18 21:27 ` [PATCH v2 3/4] md: dm-crypt: infer ESSIV block cipher from cipher string directly Ard Biesheuvel
2019-06-18 21:27   ` Ard Biesheuvel
2019-06-18 21:27 ` [PATCH v2 4/4] md: dm-crypt: switch to ESSIV crypto API template Ard Biesheuvel
2019-06-18 21:27   ` Ard Biesheuvel
2019-06-19  6:56 ` [PATCH v2 0/4] crypto: switch to crypto API for ESSIV generation Milan Broz
2019-06-19  6:56   ` Milan Broz
2019-06-19  7:11   ` Ard Biesheuvel
2019-06-19  7:11     ` Ard Biesheuvel
2019-06-19  7:11     ` Ard Biesheuvel
2019-06-19  9:14     ` Ard Biesheuvel
2019-06-19  9:14       ` Ard Biesheuvel
2019-06-19  9:14       ` Ard Biesheuvel
2019-06-19 11:01       ` Milan Broz
2019-06-19 11:01         ` Milan Broz
2019-06-19 11:01         ` [dm-devel] " Milan Broz
2019-06-19 11:16         ` Ard Biesheuvel
2019-06-19 11:16           ` Ard Biesheuvel
2019-06-19 11:16           ` Ard Biesheuvel
2019-06-19 11:33           ` Milan Broz
2019-06-19 11:33             ` Milan Broz
2019-06-19 11:33             ` Milan Broz
2019-06-19 12:36             ` Ard Biesheuvel
2019-06-19 12:36               ` Ard Biesheuvel
2019-06-19 12:36               ` Ard Biesheuvel
2019-06-19 12:49               ` Ard Biesheuvel
2019-06-19 12:49                 ` Ard Biesheuvel
2019-06-19 12:49                 ` Ard Biesheuvel
2019-06-19 13:08                 ` Milan Broz
2019-06-19 13:08                   ` Milan Broz
2019-06-19 13:08                   ` Milan Broz
2019-06-19 13:13                 ` Ard Biesheuvel
2019-06-19 13:13                   ` Ard Biesheuvel
2019-06-19 13:13                   ` Ard Biesheuvel

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.