netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/6] crypto: api - Add support for cloning tfms
@ 2023-04-13  6:23 Herbert Xu
  2023-04-13  6:24 ` [PATCH 1/6] crypto: api - Add crypto_tfm_get Herbert Xu
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Herbert Xu @ 2023-04-13  6:23 UTC (permalink / raw)
  To: Linux Crypto Mailing List
  Cc: David Ahern, Eric Dumazet, Paolo Abeni, Jakub Kicinski,
	David S. Miller, Dmitry Safonov, Andy Lutomirski, Ard Biesheuvel,
	Bob Gilligan, Dan Carpenter, David Laight, Dmitry Safonov,
	Eric Biggers, Eric W. Biederman, Francesco Ruggeri,
	Hideaki YOSHIFUJI, Ivan Delalande, Leonard Crestez,
	Salam Noureddine, netdev

Over the years, various networking-related users have needed
per-packet keys for hashing (and potentially for ciphers because
hashing can be derived from ciphers, e.g., CMAC).

Currently this is impossible to do with the Crypto API for two
reasons.  Each key is tied to a tfm object, which cannot be
allocated on the network data path (as it requires sleeping).
Secondly, various drivers rely on the fact that setkey is usually
done right after allocating a tfm object and therefore sleep in
their setkey functions.

This series tries to resolve this by making it possible to clone
an existing tfm object, thus making it possible to allocate new
tfms on the data path.

To do so you simply call crypto_clone_ahash/shash on an existing
tfm object.  Afterwards you may call setkey on it as usual.

Cheers,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

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

* [PATCH 1/6] crypto: api - Add crypto_tfm_get
  2023-04-13  6:23 [PATCH 0/6] crypto: api - Add support for cloning tfms Herbert Xu
@ 2023-04-13  6:24 ` Herbert Xu
  2023-04-18 12:27   ` Simon Horman
  2023-04-13  6:24 ` [PATCH 2/6] crypto: api - Add crypto_clone_tfm Herbert Xu
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: Herbert Xu @ 2023-04-13  6:24 UTC (permalink / raw)
  To: Linux Crypto Mailing List, David Ahern, Eric Dumazet, Paolo Abeni,
	Jakub Kicinski, David S. Miller, Dmitry Safonov, Andy Lutomirski,
	Ard Biesheuvel, Bob Gilligan, Dan Carpenter, David Laight,
	Dmitry Safonov, Eric Biggers, Eric W. Biederman,
	Francesco Ruggeri, Hideaki YOSHIFUJI, Ivan Delalande,
	Leonard Crestez, Salam Noureddine, netdev

Add a crypto_tfm_get interface to allow tfm objects to be shared.
They can still be freed in the usual way.

This should only be done with tfm objects with no keys.  You must
also not modify the tfm flags in any way once it becomes shared.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---

 crypto/api.c           |    4 ++++
 crypto/internal.h      |    6 ++++++
 include/linux/crypto.h |    1 +
 3 files changed, 11 insertions(+)

diff --git a/crypto/api.c b/crypto/api.c
index e67cc63368ed..f509d73fa682 100644
--- a/crypto/api.c
+++ b/crypto/api.c
@@ -408,6 +408,7 @@ struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
 		goto out_err;
 
 	tfm->__crt_alg = alg;
+	refcount_set(&tfm->refcnt, 1);
 
 	err = crypto_init_ops(tfm, type, mask);
 	if (err)
@@ -507,6 +508,7 @@ void *crypto_create_tfm_node(struct crypto_alg *alg,
 	tfm = (struct crypto_tfm *)(mem + tfmsize);
 	tfm->__crt_alg = alg;
 	tfm->node = node;
+	refcount_set(&tfm->refcnt, 1);
 
 	err = frontend->init_tfm(tfm);
 	if (err)
@@ -619,6 +621,8 @@ void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
 	if (IS_ERR_OR_NULL(mem))
 		return;
 
+	if (!refcount_dec_and_test(&tfm->refcnt))
+		return;
 	alg = tfm->__crt_alg;
 
 	if (!tfm->exit && alg->cra_exit)
diff --git a/crypto/internal.h b/crypto/internal.h
index f84dfe6491e5..5eee009ee494 100644
--- a/crypto/internal.h
+++ b/crypto/internal.h
@@ -10,6 +10,7 @@
 
 #include <crypto/algapi.h>
 #include <linux/completion.h>
+#include <linux/err.h>
 #include <linux/jump_label.h>
 #include <linux/list.h>
 #include <linux/module.h>
@@ -186,5 +187,10 @@ static inline int crypto_is_test_larval(struct crypto_larval *larval)
 	return larval->alg.cra_driver_name[0];
 }
 
+static inline struct crypto_tfm *crypto_tfm_get(struct crypto_tfm *tfm)
+{
+	return refcount_inc_not_zero(&tfm->refcnt) ? tfm : ERR_PTR(-EOVERFLOW);
+}
+
 #endif	/* _CRYPTO_INTERNAL_H */
 
diff --git a/include/linux/crypto.h b/include/linux/crypto.h
index fdfa3e8eda43..fa310ac1db59 100644
--- a/include/linux/crypto.h
+++ b/include/linux/crypto.h
@@ -419,6 +419,7 @@ int crypto_has_alg(const char *name, u32 type, u32 mask);
  */
 
 struct crypto_tfm {
+	refcount_t refcnt;
 
 	u32 crt_flags;
 

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

* [PATCH 2/6] crypto: api - Add crypto_clone_tfm
  2023-04-13  6:23 [PATCH 0/6] crypto: api - Add support for cloning tfms Herbert Xu
  2023-04-13  6:24 ` [PATCH 1/6] crypto: api - Add crypto_tfm_get Herbert Xu
@ 2023-04-13  6:24 ` Herbert Xu
  2023-04-18 12:28   ` Simon Horman
  2023-04-13  6:24 ` [PATCH 3/6] crypto: hash - Add crypto_clone_ahash/shash Herbert Xu
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: Herbert Xu @ 2023-04-13  6:24 UTC (permalink / raw)
  To: Linux Crypto Mailing List, David Ahern, Eric Dumazet, Paolo Abeni,
	Jakub Kicinski, David S. Miller, Dmitry Safonov, Andy Lutomirski,
	Ard Biesheuvel, Bob Gilligan, Dan Carpenter, David Laight,
	Dmitry Safonov, Eric Biggers, Eric W. Biederman,
	Francesco Ruggeri, Hideaki YOSHIFUJI, Ivan Delalande,
	Leonard Crestez, Salam Noureddine, netdev

This patch adds the helper crypto_clone_tfm.  The purpose is to
allocate a tfm object with GFP_ATOMIC.  As we cannot sleep, the
object has to be cloned from an existing tfm object.

This allows code paths that cannot otherwise allocate a crypto_tfm
object to do so.  Once a new tfm has been obtained its key could
then be changed without impacting other users.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---

 crypto/api.c      |   59 +++++++++++++++++++++++++++++++++++++++++++++---------
 crypto/internal.h |    2 +
 2 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/crypto/api.c b/crypto/api.c
index f509d73fa682..d375e8cd770d 100644
--- a/crypto/api.c
+++ b/crypto/api.c
@@ -488,28 +488,44 @@ struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
 }
 EXPORT_SYMBOL_GPL(crypto_alloc_base);
 
-void *crypto_create_tfm_node(struct crypto_alg *alg,
-			const struct crypto_type *frontend,
-			int node)
+static void *crypto_alloc_tfmmem(struct crypto_alg *alg,
+				 const struct crypto_type *frontend, int node,
+				 gfp_t gfp)
 {
-	char *mem;
-	struct crypto_tfm *tfm = NULL;
+	struct crypto_tfm *tfm;
 	unsigned int tfmsize;
 	unsigned int total;
-	int err = -ENOMEM;
+	char *mem;
 
 	tfmsize = frontend->tfmsize;
 	total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
 
-	mem = kzalloc_node(total, GFP_KERNEL, node);
+	mem = kzalloc_node(total, gfp, node);
 	if (mem == NULL)
-		goto out_err;
+		return ERR_PTR(-ENOMEM);
 
 	tfm = (struct crypto_tfm *)(mem + tfmsize);
 	tfm->__crt_alg = alg;
 	tfm->node = node;
 	refcount_set(&tfm->refcnt, 1);
 
+	return mem;
+}
+
+void *crypto_create_tfm_node(struct crypto_alg *alg,
+			     const struct crypto_type *frontend,
+			     int node)
+{
+	struct crypto_tfm *tfm;
+	char *mem;
+	int err;
+
+	mem = crypto_alloc_tfmmem(alg, frontend, node, GFP_KERNEL);
+	if (IS_ERR(mem))
+		goto out;
+
+	tfm = (struct crypto_tfm *)(mem + frontend->tfmsize);
+
 	err = frontend->init_tfm(tfm);
 	if (err)
 		goto out_free_tfm;
@@ -525,13 +541,38 @@ void *crypto_create_tfm_node(struct crypto_alg *alg,
 	if (err == -EAGAIN)
 		crypto_shoot_alg(alg);
 	kfree(mem);
-out_err:
 	mem = ERR_PTR(err);
 out:
 	return mem;
 }
 EXPORT_SYMBOL_GPL(crypto_create_tfm_node);
 
+void *crypto_clone_tfm(const struct crypto_type *frontend,
+		       struct crypto_tfm *otfm)
+{
+	struct crypto_alg *alg = otfm->__crt_alg;
+	struct crypto_tfm *tfm;
+	char *mem;
+
+	mem = ERR_PTR(-ESTALE);
+	if (unlikely(!crypto_mod_get(alg)))
+		goto out;
+
+	mem = crypto_alloc_tfmmem(alg, frontend, otfm->node, GFP_ATOMIC);
+	if (IS_ERR(mem)) {
+		crypto_mod_put(alg);
+		goto out;
+	}
+
+	tfm = (struct crypto_tfm *)(mem + frontend->tfmsize);
+	tfm->crt_flags = otfm->crt_flags;
+	tfm->exit = otfm->exit;
+
+out:
+	return mem;
+}
+EXPORT_SYMBOL_GPL(crypto_clone_tfm);
+
 struct crypto_alg *crypto_find_alg(const char *alg_name,
 				   const struct crypto_type *frontend,
 				   u32 type, u32 mask)
diff --git a/crypto/internal.h b/crypto/internal.h
index 5eee009ee494..8dd746b1130b 100644
--- a/crypto/internal.h
+++ b/crypto/internal.h
@@ -106,6 +106,8 @@ struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
 				      u32 mask);
 void *crypto_create_tfm_node(struct crypto_alg *alg,
 			const struct crypto_type *frontend, int node);
+void *crypto_clone_tfm(const struct crypto_type *frontend,
+		       struct crypto_tfm *otfm);
 
 static inline void *crypto_create_tfm(struct crypto_alg *alg,
 			const struct crypto_type *frontend)

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

* [PATCH 3/6] crypto: hash - Add crypto_clone_ahash/shash
  2023-04-13  6:23 [PATCH 0/6] crypto: api - Add support for cloning tfms Herbert Xu
  2023-04-13  6:24 ` [PATCH 1/6] crypto: api - Add crypto_tfm_get Herbert Xu
  2023-04-13  6:24 ` [PATCH 2/6] crypto: api - Add crypto_clone_tfm Herbert Xu
@ 2023-04-13  6:24 ` Herbert Xu
  2023-04-18 12:29   ` Simon Horman
  2023-04-13  6:24 ` [PATCH 4/6] crypto: hmac - Add support for cloning Herbert Xu
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: Herbert Xu @ 2023-04-13  6:24 UTC (permalink / raw)
  To: Linux Crypto Mailing List, David Ahern, Eric Dumazet, Paolo Abeni,
	Jakub Kicinski, David S. Miller, Dmitry Safonov, Andy Lutomirski,
	Ard Biesheuvel, Bob Gilligan, Dan Carpenter, David Laight,
	Dmitry Safonov, Eric Biggers, Eric W. Biederman,
	Francesco Ruggeri, Hideaki YOSHIFUJI, Ivan Delalande,
	Leonard Crestez, Salam Noureddine, netdev

This patch adds the helpers crypto_clone_ahash and crypto_clone_shash.
They are the hash-specific counterparts of crypto_clone_tfm.

This allows code paths that cannot otherwise allocate a hash tfm
object to do so.  Once a new tfm has been obtained its key could
then be changed without impacting other users.

Note that only algorithms that implement clone_tfm can be cloned.
However, all keyless hashes can be cloned by simply reusing the
tfm object.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---

 crypto/ahash.c                 |   51 ++++++++++++++++++++++++++++++++++++++++
 crypto/hash.h                  |    4 +++
 crypto/shash.c                 |   52 +++++++++++++++++++++++++++++++++++++++++
 include/crypto/hash.h          |    8 ++++++
 include/crypto/internal/hash.h |    2 -
 5 files changed, 115 insertions(+), 2 deletions(-)

diff --git a/crypto/ahash.c b/crypto/ahash.c
index 2d858d7fd1bb..b8a607928e72 100644
--- a/crypto/ahash.c
+++ b/crypto/ahash.c
@@ -543,6 +543,57 @@ int crypto_has_ahash(const char *alg_name, u32 type, u32 mask)
 }
 EXPORT_SYMBOL_GPL(crypto_has_ahash);
 
+struct crypto_ahash *crypto_clone_ahash(struct crypto_ahash *hash)
+{
+	struct hash_alg_common *halg = crypto_hash_alg_common(hash);
+	struct crypto_tfm *tfm = crypto_ahash_tfm(hash);
+	struct crypto_ahash *nhash;
+	struct ahash_alg *alg;
+	int err;
+
+	if (!crypto_hash_alg_has_setkey(halg)) {
+		tfm = crypto_tfm_get(tfm);
+		if (IS_ERR(tfm))
+			return ERR_CAST(tfm);
+
+		return hash;
+	}
+
+	nhash = crypto_clone_tfm(&crypto_ahash_type, tfm);
+
+	if (IS_ERR(nhash))
+		return nhash;
+
+	nhash->init = hash->init;
+	nhash->update = hash->update;
+	nhash->final = hash->final;
+	nhash->finup = hash->finup;
+	nhash->digest = hash->digest;
+	nhash->export = hash->export;
+	nhash->import = hash->import;
+	nhash->setkey = hash->setkey;
+	nhash->reqsize = hash->reqsize;
+
+	if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
+		return crypto_clone_shash_ops_async(nhash, hash);
+
+	err = -ENOSYS;
+	alg = crypto_ahash_alg(hash);
+	if (!alg->clone_tfm)
+		goto out_free_nhash;
+
+	err = alg->clone_tfm(nhash, hash);
+	if (err)
+		goto out_free_nhash;
+
+	return nhash;
+
+out_free_nhash:
+	crypto_free_ahash(nhash);
+	return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(crypto_clone_ahash);
+
 static int ahash_prepare_alg(struct ahash_alg *alg)
 {
 	struct crypto_alg *base = &alg->halg.base;
diff --git a/crypto/hash.h b/crypto/hash.h
index 57b28a986d69..7e6c1a948692 100644
--- a/crypto/hash.h
+++ b/crypto/hash.h
@@ -31,6 +31,10 @@ static inline int crypto_hash_report_stat(struct sk_buff *skb,
 	return nla_put(skb, CRYPTOCFGA_STAT_HASH, sizeof(rhash), &rhash);
 }
 
+int crypto_init_shash_ops_async(struct crypto_tfm *tfm);
+struct crypto_ahash *crypto_clone_shash_ops_async(struct crypto_ahash *nhash,
+						  struct crypto_ahash *hash);
+
 int hash_prepare_alg(struct hash_alg_common *alg);
 
 #endif	/* _LOCAL_CRYPTO_HASH_H */
diff --git a/crypto/shash.c b/crypto/shash.c
index 4cefa614dbbd..5845b7d59b2f 100644
--- a/crypto/shash.c
+++ b/crypto/shash.c
@@ -445,6 +445,24 @@ int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
 	return 0;
 }
 
+struct crypto_ahash *crypto_clone_shash_ops_async(struct crypto_ahash *nhash,
+						  struct crypto_ahash *hash)
+{
+	struct crypto_shash **nctx = crypto_ahash_ctx(nhash);
+	struct crypto_shash **ctx = crypto_ahash_ctx(hash);
+	struct crypto_shash *shash;
+
+	shash = crypto_clone_shash(*ctx);
+	if (IS_ERR(shash)) {
+		crypto_free_ahash(nhash);
+		return ERR_CAST(shash);
+	}
+
+	*nctx = shash;
+
+	return nhash;
+}
+
 static void crypto_shash_exit_tfm(struct crypto_tfm *tfm)
 {
 	struct crypto_shash *hash = __crypto_shash_cast(tfm);
@@ -564,6 +582,40 @@ int crypto_has_shash(const char *alg_name, u32 type, u32 mask)
 }
 EXPORT_SYMBOL_GPL(crypto_has_shash);
 
+struct crypto_shash *crypto_clone_shash(struct crypto_shash *hash)
+{
+	struct crypto_tfm *tfm = crypto_shash_tfm(hash);
+	struct shash_alg *alg = crypto_shash_alg(hash);
+	struct crypto_shash *nhash;
+	int err;
+
+	if (!crypto_shash_alg_has_setkey(alg)) {
+		tfm = crypto_tfm_get(tfm);
+		if (IS_ERR(tfm))
+			return ERR_CAST(tfm);
+
+		return hash;
+	}
+
+	if (!alg->clone_tfm)
+		return ERR_PTR(-ENOSYS);
+
+	nhash = crypto_clone_tfm(&crypto_shash_type, tfm);
+	if (IS_ERR(nhash))
+		return nhash;
+
+	nhash->descsize = hash->descsize;
+
+	err = alg->clone_tfm(nhash, hash);
+	if (err) {
+		crypto_free_shash(nhash);
+		return ERR_PTR(err);
+	}
+
+	return nhash;
+}
+EXPORT_SYMBOL_GPL(crypto_clone_shash);
+
 int hash_prepare_alg(struct hash_alg_common *alg)
 {
 	struct crypto_istat_hash *istat = hash_get_stat(alg);
diff --git a/include/crypto/hash.h b/include/crypto/hash.h
index 3a04e601ad6a..e69542d86a2b 100644
--- a/include/crypto/hash.h
+++ b/include/crypto/hash.h
@@ -152,6 +152,7 @@ struct ahash_request {
  * @exit_tfm: Deinitialize the cryptographic transformation object.
  *	      This is a counterpart to @init_tfm, used to remove
  *	      various changes set in @init_tfm.
+ * @clone_tfm: Copy transform into new object, may allocate memory.
  * @halg: see struct hash_alg_common
  */
 struct ahash_alg {
@@ -166,6 +167,7 @@ struct ahash_alg {
 		      unsigned int keylen);
 	int (*init_tfm)(struct crypto_ahash *tfm);
 	void (*exit_tfm)(struct crypto_ahash *tfm);
+	int (*clone_tfm)(struct crypto_ahash *dst, struct crypto_ahash *src);
 
 	struct hash_alg_common halg;
 };
@@ -209,6 +211,7 @@ struct shash_desc {
  * @exit_tfm: Deinitialize the cryptographic transformation object.
  *	      This is a counterpart to @init_tfm, used to remove
  *	      various changes set in @init_tfm.
+ * @clone_tfm: Copy transform into new object, may allocate memory.
  * @digestsize: see struct ahash_alg
  * @statesize: see struct ahash_alg
  * @descsize: Size of the operational state for the message digest. This state
@@ -234,6 +237,7 @@ struct shash_alg {
 		      unsigned int keylen);
 	int (*init_tfm)(struct crypto_shash *tfm);
 	void (*exit_tfm)(struct crypto_shash *tfm);
+	int (*clone_tfm)(struct crypto_shash *dst, struct crypto_shash *src);
 
 	unsigned int descsize;
 
@@ -297,6 +301,8 @@ static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
 					u32 mask);
 
+struct crypto_ahash *crypto_clone_ahash(struct crypto_ahash *tfm);
+
 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
 {
 	return &tfm->base;
@@ -761,6 +767,8 @@ static inline void ahash_request_set_crypt(struct ahash_request *req,
 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
 					u32 mask);
 
+struct crypto_shash *crypto_clone_shash(struct crypto_shash *tfm);
+
 int crypto_has_shash(const char *alg_name, u32 type, u32 mask);
 
 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
diff --git a/include/crypto/internal/hash.h b/include/crypto/internal/hash.h
index 0b259dbb97af..37edf3f4e8af 100644
--- a/include/crypto/internal/hash.h
+++ b/include/crypto/internal/hash.h
@@ -133,8 +133,6 @@ int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
 int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc);
 int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);
 
-int crypto_init_shash_ops_async(struct crypto_tfm *tfm);
-
 static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
 {
 	return crypto_tfm_ctx(crypto_ahash_tfm(tfm));

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

* [PATCH 4/6] crypto: hmac - Add support for cloning
  2023-04-13  6:23 [PATCH 0/6] crypto: api - Add support for cloning tfms Herbert Xu
                   ` (2 preceding siblings ...)
  2023-04-13  6:24 ` [PATCH 3/6] crypto: hash - Add crypto_clone_ahash/shash Herbert Xu
@ 2023-04-13  6:24 ` Herbert Xu
  2023-04-13  6:24 ` [PATCH 5/6] crypto: cryptd - Convert hash to use modern init_tfm/exit_tfm Herbert Xu
  2023-04-13  6:24 ` [PATCH 6/6] crypto: cryptd - Add support for cloning hashes Herbert Xu
  5 siblings, 0 replies; 11+ messages in thread
From: Herbert Xu @ 2023-04-13  6:24 UTC (permalink / raw)
  To: Linux Crypto Mailing List, David Ahern, Eric Dumazet, Paolo Abeni,
	Jakub Kicinski, David S. Miller, Dmitry Safonov, Andy Lutomirski,
	Ard Biesheuvel, Bob Gilligan, Dan Carpenter, David Laight,
	Dmitry Safonov, Eric Biggers, Eric W. Biederman,
	Francesco Ruggeri, Hideaki YOSHIFUJI, Ivan Delalande,
	Leonard Crestez, Salam Noureddine, netdev

Allow hmac to be cloned.  The underlying hash can be used directly
with a reference count.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---

 crypto/hmac.c |   15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/crypto/hmac.c b/crypto/hmac.c
index 3610ff0b6739..09a7872b4060 100644
--- a/crypto/hmac.c
+++ b/crypto/hmac.c
@@ -160,6 +160,20 @@ static int hmac_init_tfm(struct crypto_shash *parent)
 	return 0;
 }
 
+static int hmac_clone_tfm(struct crypto_shash *dst, struct crypto_shash *src)
+{
+	struct hmac_ctx *sctx = hmac_ctx(src);
+	struct hmac_ctx *dctx = hmac_ctx(dst);
+	struct crypto_shash *hash;
+
+	hash = crypto_clone_shash(sctx->hash);
+	if (IS_ERR(hash))
+		return PTR_ERR(hash);
+
+	dctx->hash = hash;
+	return 0;
+}
+
 static void hmac_exit_tfm(struct crypto_shash *parent)
 {
 	struct hmac_ctx *ctx = hmac_ctx(parent);
@@ -227,6 +241,7 @@ static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
 	inst->alg.import = hmac_import;
 	inst->alg.setkey = hmac_setkey;
 	inst->alg.init_tfm = hmac_init_tfm;
+	inst->alg.clone_tfm = hmac_clone_tfm;
 	inst->alg.exit_tfm = hmac_exit_tfm;
 
 	inst->free = shash_free_singlespawn_instance;

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

* [PATCH 5/6] crypto: cryptd - Convert hash to use modern init_tfm/exit_tfm
  2023-04-13  6:23 [PATCH 0/6] crypto: api - Add support for cloning tfms Herbert Xu
                   ` (3 preceding siblings ...)
  2023-04-13  6:24 ` [PATCH 4/6] crypto: hmac - Add support for cloning Herbert Xu
@ 2023-04-13  6:24 ` Herbert Xu
  2023-04-18 12:30   ` Simon Horman
  2023-04-13  6:24 ` [PATCH 6/6] crypto: cryptd - Add support for cloning hashes Herbert Xu
  5 siblings, 1 reply; 11+ messages in thread
From: Herbert Xu @ 2023-04-13  6:24 UTC (permalink / raw)
  To: Linux Crypto Mailing List, David Ahern, Eric Dumazet, Paolo Abeni,
	Jakub Kicinski, David S. Miller, Dmitry Safonov, Andy Lutomirski,
	Ard Biesheuvel, Bob Gilligan, Dan Carpenter, David Laight,
	Dmitry Safonov, Eric Biggers, Eric W. Biederman,
	Francesco Ruggeri, Hideaki YOSHIFUJI, Ivan Delalande,
	Leonard Crestez, Salam Noureddine, netdev

The cryptd hash template was still using the obsolete cra_init/cra_exit
interface.  Make it use the modern ahash init_tfm/exit_tfm instead.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---

 crypto/cryptd.c |   18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/crypto/cryptd.c b/crypto/cryptd.c
index 37365ed30b38..43ce347ccba0 100644
--- a/crypto/cryptd.c
+++ b/crypto/cryptd.c
@@ -427,12 +427,12 @@ static int cryptd_create_skcipher(struct crypto_template *tmpl,
 	return err;
 }
 
-static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
+static int cryptd_hash_init_tfm(struct crypto_ahash *tfm)
 {
-	struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
-	struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
+	struct ahash_instance *inst = ahash_alg_instance(tfm);
+	struct hashd_instance_ctx *ictx = ahash_instance_ctx(inst);
 	struct crypto_shash_spawn *spawn = &ictx->spawn;
-	struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
+	struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
 	struct crypto_shash *hash;
 
 	hash = crypto_spawn_shash(spawn);
@@ -440,15 +440,15 @@ static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
 		return PTR_ERR(hash);
 
 	ctx->child = hash;
-	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
+	crypto_ahash_set_reqsize(tfm,
 				 sizeof(struct cryptd_hash_request_ctx) +
 				 crypto_shash_descsize(hash));
 	return 0;
 }
 
-static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
+static void cryptd_hash_exit_tfm(struct crypto_ahash *tfm)
 {
-	struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
+	struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
 
 	crypto_free_shash(ctx->child);
 }
@@ -677,8 +677,8 @@ static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
 	inst->alg.halg.statesize = alg->statesize;
 	inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
 
-	inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
-	inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
+	inst->alg.init_tfm = cryptd_hash_init_tfm;
+	inst->alg.exit_tfm = cryptd_hash_exit_tfm;
 
 	inst->alg.init   = cryptd_hash_init_enqueue;
 	inst->alg.update = cryptd_hash_update_enqueue;

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

* [PATCH 6/6] crypto: cryptd - Add support for cloning hashes
  2023-04-13  6:23 [PATCH 0/6] crypto: api - Add support for cloning tfms Herbert Xu
                   ` (4 preceding siblings ...)
  2023-04-13  6:24 ` [PATCH 5/6] crypto: cryptd - Convert hash to use modern init_tfm/exit_tfm Herbert Xu
@ 2023-04-13  6:24 ` Herbert Xu
  5 siblings, 0 replies; 11+ messages in thread
From: Herbert Xu @ 2023-04-13  6:24 UTC (permalink / raw)
  To: Linux Crypto Mailing List, David Ahern, Eric Dumazet, Paolo Abeni,
	Jakub Kicinski, David S. Miller, Dmitry Safonov, Andy Lutomirski,
	Ard Biesheuvel, Bob Gilligan, Dan Carpenter, David Laight,
	Dmitry Safonov, Eric Biggers, Eric W. Biederman,
	Francesco Ruggeri, Hideaki YOSHIFUJI, Ivan Delalande,
	Leonard Crestez, Salam Noureddine, netdev

Allow cryptd hashes to be cloned.  The underlying hash will be cloned.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---

 crypto/cryptd.c |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/crypto/cryptd.c b/crypto/cryptd.c
index 43ce347ccba0..bbcc368b6a55 100644
--- a/crypto/cryptd.c
+++ b/crypto/cryptd.c
@@ -446,6 +446,21 @@ static int cryptd_hash_init_tfm(struct crypto_ahash *tfm)
 	return 0;
 }
 
+static int cryptd_hash_clone_tfm(struct crypto_ahash *ntfm,
+				 struct crypto_ahash *tfm)
+{
+	struct cryptd_hash_ctx *nctx = crypto_ahash_ctx(ntfm);
+	struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
+	struct crypto_shash *hash;
+
+	hash = crypto_clone_shash(ctx->child);
+	if (IS_ERR(hash))
+		return PTR_ERR(hash);
+
+	nctx->child = hash;
+	return 0;
+}
+
 static void cryptd_hash_exit_tfm(struct crypto_ahash *tfm)
 {
 	struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
@@ -678,6 +693,7 @@ static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
 	inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
 
 	inst->alg.init_tfm = cryptd_hash_init_tfm;
+	inst->alg.clone_tfm = cryptd_hash_clone_tfm;
 	inst->alg.exit_tfm = cryptd_hash_exit_tfm;
 
 	inst->alg.init   = cryptd_hash_init_enqueue;

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

* Re: [PATCH 1/6] crypto: api - Add crypto_tfm_get
  2023-04-13  6:24 ` [PATCH 1/6] crypto: api - Add crypto_tfm_get Herbert Xu
@ 2023-04-18 12:27   ` Simon Horman
  0 siblings, 0 replies; 11+ messages in thread
From: Simon Horman @ 2023-04-18 12:27 UTC (permalink / raw)
  To: Herbert Xu
  Cc: Linux Crypto Mailing List, David Ahern, Eric Dumazet, Paolo Abeni,
	Jakub Kicinski, David S. Miller, Dmitry Safonov, Andy Lutomirski,
	Ard Biesheuvel, Bob Gilligan, Dan Carpenter, David Laight,
	Dmitry Safonov, Eric Biggers, Eric W. Biederman,
	Francesco Ruggeri, Hideaki YOSHIFUJI, Ivan Delalande,
	Leonard Crestez, Salam Noureddine, netdev

On Thu, Apr 13, 2023 at 02:24:15PM +0800, Herbert Xu wrote:
> Add a crypto_tfm_get interface to allow tfm objects to be shared.
> They can still be freed in the usual way.
> 
> This should only be done with tfm objects with no keys.  You must
> also not modify the tfm flags in any way once it becomes shared.
> 
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Reviewed-by: Simon Horman <simon.horman@corigine.com>


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

* Re: [PATCH 2/6] crypto: api - Add crypto_clone_tfm
  2023-04-13  6:24 ` [PATCH 2/6] crypto: api - Add crypto_clone_tfm Herbert Xu
@ 2023-04-18 12:28   ` Simon Horman
  0 siblings, 0 replies; 11+ messages in thread
From: Simon Horman @ 2023-04-18 12:28 UTC (permalink / raw)
  To: Herbert Xu
  Cc: Linux Crypto Mailing List, David Ahern, Eric Dumazet, Paolo Abeni,
	Jakub Kicinski, David S. Miller, Dmitry Safonov, Andy Lutomirski,
	Ard Biesheuvel, Bob Gilligan, Dan Carpenter, David Laight,
	Dmitry Safonov, Eric Biggers, Eric W. Biederman,
	Francesco Ruggeri, Hideaki YOSHIFUJI, Ivan Delalande,
	Leonard Crestez, Salam Noureddine, netdev

On Thu, Apr 13, 2023 at 02:24:17PM +0800, Herbert Xu wrote:
> This patch adds the helper crypto_clone_tfm.  The purpose is to
> allocate a tfm object with GFP_ATOMIC.  As we cannot sleep, the
> object has to be cloned from an existing tfm object.
> 
> This allows code paths that cannot otherwise allocate a crypto_tfm
> object to do so.  Once a new tfm has been obtained its key could
> then be changed without impacting other users.
> 
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Reviewed-by: Simon Horman <simon.horman@corigine.com>


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

* Re: [PATCH 3/6] crypto: hash - Add crypto_clone_ahash/shash
  2023-04-13  6:24 ` [PATCH 3/6] crypto: hash - Add crypto_clone_ahash/shash Herbert Xu
@ 2023-04-18 12:29   ` Simon Horman
  0 siblings, 0 replies; 11+ messages in thread
From: Simon Horman @ 2023-04-18 12:29 UTC (permalink / raw)
  To: Herbert Xu
  Cc: Linux Crypto Mailing List, David Ahern, Eric Dumazet, Paolo Abeni,
	Jakub Kicinski, David S. Miller, Dmitry Safonov, Andy Lutomirski,
	Ard Biesheuvel, Bob Gilligan, Dan Carpenter, David Laight,
	Dmitry Safonov, Eric Biggers, Eric W. Biederman,
	Francesco Ruggeri, Hideaki YOSHIFUJI, Ivan Delalande,
	Leonard Crestez, Salam Noureddine, netdev

On Thu, Apr 13, 2023 at 02:24:19PM +0800, Herbert Xu wrote:
> This patch adds the helpers crypto_clone_ahash and crypto_clone_shash.
> They are the hash-specific counterparts of crypto_clone_tfm.
> 
> This allows code paths that cannot otherwise allocate a hash tfm
> object to do so.  Once a new tfm has been obtained its key could
> then be changed without impacting other users.
> 
> Note that only algorithms that implement clone_tfm can be cloned.
> However, all keyless hashes can be cloned by simply reusing the
> tfm object.
> 
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Reviewed-by: Simon Horman <simon.horman@corigine.com>


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

* Re: [PATCH 5/6] crypto: cryptd - Convert hash to use modern init_tfm/exit_tfm
  2023-04-13  6:24 ` [PATCH 5/6] crypto: cryptd - Convert hash to use modern init_tfm/exit_tfm Herbert Xu
@ 2023-04-18 12:30   ` Simon Horman
  0 siblings, 0 replies; 11+ messages in thread
From: Simon Horman @ 2023-04-18 12:30 UTC (permalink / raw)
  To: Herbert Xu
  Cc: Linux Crypto Mailing List, David Ahern, Eric Dumazet, Paolo Abeni,
	Jakub Kicinski, David S. Miller, Dmitry Safonov, Andy Lutomirski,
	Ard Biesheuvel, Bob Gilligan, Dan Carpenter, David Laight,
	Dmitry Safonov, Eric Biggers, Eric W. Biederman,
	Francesco Ruggeri, Hideaki YOSHIFUJI, Ivan Delalande,
	Leonard Crestez, Salam Noureddine, netdev

On Thu, Apr 13, 2023 at 02:24:23PM +0800, Herbert Xu wrote:
> The cryptd hash template was still using the obsolete cra_init/cra_exit
> interface.  Make it use the modern ahash init_tfm/exit_tfm instead.
> 
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Reviewed-by: Simon Horman <simon.horman@corigine.com>


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

end of thread, other threads:[~2023-04-18 12:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-13  6:23 [PATCH 0/6] crypto: api - Add support for cloning tfms Herbert Xu
2023-04-13  6:24 ` [PATCH 1/6] crypto: api - Add crypto_tfm_get Herbert Xu
2023-04-18 12:27   ` Simon Horman
2023-04-13  6:24 ` [PATCH 2/6] crypto: api - Add crypto_clone_tfm Herbert Xu
2023-04-18 12:28   ` Simon Horman
2023-04-13  6:24 ` [PATCH 3/6] crypto: hash - Add crypto_clone_ahash/shash Herbert Xu
2023-04-18 12:29   ` Simon Horman
2023-04-13  6:24 ` [PATCH 4/6] crypto: hmac - Add support for cloning Herbert Xu
2023-04-13  6:24 ` [PATCH 5/6] crypto: cryptd - Convert hash to use modern init_tfm/exit_tfm Herbert Xu
2023-04-18 12:30   ` Simon Horman
2023-04-13  6:24 ` [PATCH 6/6] crypto: cryptd - Add support for cloning hashes Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).