aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2025-04-23 17:22:28 +0800
committerHerbert Xu <herbert@gondor.apana.org.au>2025-04-28 19:40:54 +0800
commit19da081a28c95fe9b03ce952a2bf4a6f6bf5112c (patch)
tree224f4fc987087caa04437c1c10b26256cb7b245d /crypto
parentaf9ce62783dd6acd595491badec08f1235c84739 (diff)
downloadnet-19da081a28c95fe9b03ce952a2bf4a6f6bf5112c.tar.gz
crypto: api - Add crypto_request_clone and fb
Add a helper to clone crypto requests and eliminate code duplication. Use kmemdup in the helper. Also add an fb field to crypto_tfm. This also happens to fix the existing implementations which were buggy. Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202504230118.1CxUaUoX-lkp@intel.com/ Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202504230004.c7mrY0C6-lkp@intel.com/ Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/acompress.c29
-rw-r--r--crypto/ahash.c32
-rw-r--r--crypto/api.c18
3 files changed, 26 insertions, 53 deletions
diff --git a/crypto/acompress.c b/crypto/acompress.c
index 4c665c6fb5d631..9dea76ed451358 100644
--- a/crypto/acompress.c
+++ b/crypto/acompress.c
@@ -11,15 +11,13 @@
#include <crypto/scatterwalk.h>
#include <linux/cryptouser.h>
#include <linux/cpumask.h>
-#include <linux/errno.h>
+#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/module.h>
-#include <linux/page-flags.h>
#include <linux/percpu.h>
#include <linux/scatterlist.h>
#include <linux/sched.h>
#include <linux/seq_file.h>
-#include <linux/slab.h>
#include <linux/smp.h>
#include <linux/spinlock.h>
#include <linux/string.h>
@@ -79,7 +77,7 @@ static void crypto_acomp_exit_tfm(struct crypto_tfm *tfm)
alg->exit(acomp);
if (acomp_is_async(acomp))
- crypto_free_acomp(acomp->fb);
+ crypto_free_acomp(crypto_acomp_fb(acomp));
}
static int crypto_acomp_init_tfm(struct crypto_tfm *tfm)
@@ -89,8 +87,6 @@ static int crypto_acomp_init_tfm(struct crypto_tfm *tfm)
struct crypto_acomp *fb = NULL;
int err;
- acomp->fb = acomp;
-
if (tfm->__crt_alg->cra_type != &crypto_acomp_type)
return crypto_init_scomp_ops_async(tfm);
@@ -104,7 +100,7 @@ static int crypto_acomp_init_tfm(struct crypto_tfm *tfm)
if (crypto_acomp_reqsize(fb) > MAX_SYNC_COMP_REQSIZE)
goto out_free_fb;
- acomp->fb = fb;
+ tfm->fb = crypto_acomp_tfm(fb);
}
acomp->compress = alg->compress;
@@ -570,24 +566,5 @@ int acomp_walk_virt(struct acomp_walk *__restrict walk,
}
EXPORT_SYMBOL_GPL(acomp_walk_virt);
-struct acomp_req *acomp_request_clone(struct acomp_req *req,
- size_t total, gfp_t gfp)
-{
- struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
- struct acomp_req *nreq;
-
- nreq = kmalloc(total, gfp);
- if (!nreq) {
- acomp_request_set_tfm(req, tfm->fb);
- req->base.flags = CRYPTO_TFM_REQ_ON_STACK;
- return req;
- }
-
- memcpy(nreq, req, total);
- acomp_request_set_tfm(req, tfm);
- return req;
-}
-EXPORT_SYMBOL_GPL(acomp_request_clone);
-
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Asynchronous compression type");
diff --git a/crypto/ahash.c b/crypto/ahash.c
index 7a74092323b901..9b813f7b9177db 100644
--- a/crypto/ahash.c
+++ b/crypto/ahash.c
@@ -12,13 +12,11 @@
* Copyright (c) 2008 Loc Ho <lho@amcc.com>
*/
-#include <crypto/scatterwalk.h>
#include <linux/cryptouser.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
-#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/seq_file.h>
#include <linux/string.h>
@@ -301,7 +299,8 @@ int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
err = alg->setkey(tfm, key, keylen);
if (!err && ahash_is_async(tfm))
- err = crypto_ahash_setkey(tfm->fb, key, keylen);
+ err = crypto_ahash_setkey(crypto_ahash_fb(tfm),
+ key, keylen);
if (unlikely(err)) {
ahash_set_needkey(tfm, alg);
return err;
@@ -732,7 +731,7 @@ static void crypto_ahash_exit_tfm(struct crypto_tfm *tfm)
tfm->__crt_alg->cra_exit(tfm);
if (ahash_is_async(hash))
- crypto_free_ahash(hash->fb);
+ crypto_free_ahash(crypto_ahash_fb(hash));
}
static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
@@ -745,8 +744,6 @@ static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
crypto_ahash_set_statesize(hash, alg->halg.statesize);
crypto_ahash_set_reqsize(hash, crypto_tfm_alg_reqsize(tfm));
- hash->fb = hash;
-
if (tfm->__crt_alg->cra_type == &crypto_shash_type)
return crypto_init_ahash_using_shash(tfm);
@@ -756,7 +753,7 @@ static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
if (IS_ERR(fb))
return PTR_ERR(fb);
- hash->fb = fb;
+ tfm->fb = crypto_ahash_tfm(fb);
}
ahash_set_needkey(hash, alg);
@@ -1036,7 +1033,7 @@ EXPORT_SYMBOL_GPL(ahash_request_free);
int crypto_hash_digest(struct crypto_ahash *tfm, const u8 *data,
unsigned int len, u8 *out)
{
- HASH_REQUEST_ON_STACK(req, tfm->fb);
+ HASH_REQUEST_ON_STACK(req, crypto_ahash_fb(tfm));
int err;
ahash_request_set_callback(req, 0, NULL, NULL);
@@ -1049,24 +1046,5 @@ int crypto_hash_digest(struct crypto_ahash *tfm, const u8 *data,
}
EXPORT_SYMBOL_GPL(crypto_hash_digest);
-struct ahash_request *ahash_request_clone(struct ahash_request *req,
- size_t total, gfp_t gfp)
-{
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- struct ahash_request *nreq;
-
- nreq = kmalloc(total, gfp);
- if (!nreq) {
- ahash_request_set_tfm(req, tfm->fb);
- req->base.flags = CRYPTO_TFM_REQ_ON_STACK;
- return req;
- }
-
- memcpy(nreq, req, total);
- ahash_request_set_tfm(req, tfm);
- return req;
-}
-EXPORT_SYMBOL_GPL(ahash_request_clone);
-
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Asynchronous cryptographic hash type");
diff --git a/crypto/api.c b/crypto/api.c
index e427cc5662b5e5..172e82f79c69a7 100644
--- a/crypto/api.c
+++ b/crypto/api.c
@@ -528,6 +528,7 @@ void *crypto_create_tfm_node(struct crypto_alg *alg,
goto out;
tfm = (struct crypto_tfm *)(mem + frontend->tfmsize);
+ tfm->fb = tfm;
err = frontend->init_tfm(tfm);
if (err)
@@ -712,5 +713,22 @@ void crypto_destroy_alg(struct crypto_alg *alg)
}
EXPORT_SYMBOL_GPL(crypto_destroy_alg);
+struct crypto_async_request *crypto_request_clone(
+ struct crypto_async_request *req, size_t total, gfp_t gfp)
+{
+ struct crypto_tfm *tfm = req->tfm;
+ struct crypto_async_request *nreq;
+
+ nreq = kmemdup(req, total, gfp);
+ if (!nreq) {
+ req->tfm = tfm->fb;
+ return req;
+ }
+
+ nreq->flags &= ~CRYPTO_TFM_REQ_ON_STACK;
+ return nreq;
+}
+EXPORT_SYMBOL_GPL(crypto_request_clone);
+
MODULE_DESCRIPTION("Cryptographic core API");
MODULE_LICENSE("GPL");