crypto: api - Add support for duplicating algorithms before registration
authorHerbert Xu <herbert@gondor.apana.org.au>
Sat, 12 Apr 2025 05:16:43 +0000 (13:16 +0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Wed, 16 Apr 2025 07:36:24 +0000 (15:36 +0800)
If the bit CRYPTO_ALG_DUP_FIRST is set, an algorithm will be
duplicated by kmemdup before registration.  This is inteded for
hardware-based algorithms that may be unplugged at will.

Do not use this if the algorithm data structure is embedded in a
bigger data structure.  Perform the duplication in the driver
instead.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
15 files changed:
crypto/acompress.c
crypto/aead.c
crypto/ahash.c
crypto/akcipher.c
crypto/algapi.c
crypto/api.c
crypto/internal.h
crypto/kpp.c
crypto/lskcipher.c
crypto/rng.c
crypto/scompress.c
crypto/shash.c
crypto/sig.c
crypto/skcipher.c
include/linux/crypto.h

index d5605b0ad2664d8375cd4745317683203b399f72..606d09a7fbfd246be9042bb172ec85e52621a0b7 100644 (file)
@@ -150,6 +150,7 @@ static const struct crypto_type crypto_acomp_type = {
        .maskset = CRYPTO_ALG_TYPE_ACOMPRESS_MASK,
        .type = CRYPTO_ALG_TYPE_ACOMPRESS,
        .tfmsize = offsetof(struct crypto_acomp, base),
+       .algsize = offsetof(struct acomp_alg, base),
 };
 
 struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
index 12f5b42171af35e23664e386de46727b8c184cb5..5d14b775036eeb7eb7c950fa0720fce5f561508f 100644 (file)
@@ -186,6 +186,7 @@ static const struct crypto_type crypto_aead_type = {
        .maskset = CRYPTO_ALG_TYPE_MASK,
        .type = CRYPTO_ALG_TYPE_AEAD,
        .tfmsize = offsetof(struct crypto_aead, base),
+       .algsize = offsetof(struct aead_alg, base),
 };
 
 int crypto_grab_aead(struct crypto_aead_spawn *spawn,
index ba0a639144af75d834fe35b58bff9e0bb3c0628e..7c9c0931197f74b211a971358ae01da619a1c442 100644 (file)
@@ -792,6 +792,7 @@ static const struct crypto_type crypto_ahash_type = {
        .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
        .type = CRYPTO_ALG_TYPE_AHASH,
        .tfmsize = offsetof(struct crypto_ahash, base),
+       .algsize = offsetof(struct ahash_alg, halg.base),
 };
 
 int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
index 72c82d9aa077894ede781f58b8aeb2a7e115c880..a36f50c8382787f126051de52f4771a15ff297a5 100644 (file)
@@ -97,6 +97,7 @@ static const struct crypto_type crypto_akcipher_type = {
        .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
        .type = CRYPTO_ALG_TYPE_AKCIPHER,
        .tfmsize = offsetof(struct crypto_akcipher, base),
+       .algsize = offsetof(struct akcipher_alg, base),
 };
 
 int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
index f368c0dc0d6d22a1b8b1626d236f33dd725d4772..532d3efc3c7d00c87dd7fbedf535bfe9da8d6743 100644 (file)
@@ -66,13 +66,7 @@ static int crypto_check_alg(struct crypto_alg *alg)
 
 static void crypto_free_instance(struct crypto_instance *inst)
 {
-       struct crypto_alg *alg = &inst->alg;
-       const struct crypto_type *type;
-
-       type = alg->cra_type;
-       if (type->destroy)
-               type->destroy(alg);
-       type->free(inst);
+       inst->alg.cra_type->free(inst);
 }
 
 static void crypto_destroy_instance_workfn(struct work_struct *w)
@@ -424,6 +418,15 @@ void crypto_remove_final(struct list_head *list)
 }
 EXPORT_SYMBOL_GPL(crypto_remove_final);
 
+static void crypto_free_alg(struct crypto_alg *alg)
+{
+       unsigned int algsize = alg->cra_type->algsize;
+       u8 *p = (u8 *)alg - algsize;
+
+       crypto_destroy_alg(alg);
+       kfree(p);
+}
+
 int crypto_register_alg(struct crypto_alg *alg)
 {
        struct crypto_larval *larval;
@@ -436,6 +439,19 @@ int crypto_register_alg(struct crypto_alg *alg)
        if (err)
                return err;
 
+       if (alg->cra_flags & CRYPTO_ALG_DUP_FIRST &&
+           !WARN_ON_ONCE(alg->cra_destroy)) {
+               unsigned int algsize = alg->cra_type->algsize;
+               u8 *p = (u8 *)alg - algsize;
+
+               p = kmemdup(p, algsize + sizeof(*alg), GFP_KERNEL);
+               if (!p)
+                       return -ENOMEM;
+
+               alg = (void *)(p + algsize);
+               alg->cra_destroy = crypto_free_alg;
+       }
+
        down_write(&crypto_alg_sem);
        larval = __crypto_register_alg(alg, &algs_to_put);
        if (!IS_ERR_OR_NULL(larval)) {
@@ -444,8 +460,10 @@ int crypto_register_alg(struct crypto_alg *alg)
        }
        up_write(&crypto_alg_sem);
 
-       if (IS_ERR(larval))
+       if (IS_ERR(larval)) {
+               crypto_alg_put(alg);
                return PTR_ERR(larval);
+       }
 
        if (test_started)
                crypto_schedule_test(larval);
@@ -481,12 +499,9 @@ void crypto_unregister_alg(struct crypto_alg *alg)
        if (WARN(ret, "Algorithm %s is not registered", alg->cra_driver_name))
                return;
 
-       if (alg->cra_destroy)
-               crypto_alg_put(alg);
-       else if (!WARN_ON(refcount_read(&alg->cra_refcnt) != 1) &&
-                alg->cra_type && alg->cra_type->destroy)
-               alg->cra_type->destroy(alg);
+       WARN_ON(!alg->cra_destroy && refcount_read(&alg->cra_refcnt) != 1);
 
+       list_add(&alg->cra_list, &list);
        crypto_remove_final(&list);
 }
 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
index 2880aa04bb99309d00240657b4ed81af887733d3..e427cc5662b5e5552267f2b2941c796e17aa5bc4 100644 (file)
@@ -703,5 +703,14 @@ void crypto_req_done(void *data, int err)
 }
 EXPORT_SYMBOL_GPL(crypto_req_done);
 
+void crypto_destroy_alg(struct crypto_alg *alg)
+{
+       if (alg->cra_type && alg->cra_type->destroy)
+               alg->cra_type->destroy(alg);
+       if (alg->cra_destroy)
+               alg->cra_destroy(alg);
+}
+EXPORT_SYMBOL_GPL(crypto_destroy_alg);
+
 MODULE_DESCRIPTION("Cryptographic core API");
 MODULE_LICENSE("GPL");
index 2edefb546ad405f07dbf11730712736356204bbf..2ed79bf208ca289ffc82b08b91f31e80d90213c6 100644 (file)
@@ -46,6 +46,7 @@ struct crypto_type {
        unsigned int maskclear;
        unsigned int maskset;
        unsigned int tfmsize;
+       unsigned int algsize;
 };
 
 enum {
@@ -162,10 +163,12 @@ static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
        return alg;
 }
 
+void crypto_destroy_alg(struct crypto_alg *alg);
+
 static inline void crypto_alg_put(struct crypto_alg *alg)
 {
        if (refcount_dec_and_test(&alg->cra_refcnt))
-               alg->cra_destroy(alg);
+               crypto_destroy_alg(alg);
 }
 
 static inline int crypto_tmpl_get(struct crypto_template *tmpl)
index ecc63a1a948dfebb1ba751aa129b47012e7e01c9..2e0cefe7a25f5430f0916e215e2a6fb22b335ecd 100644 (file)
@@ -80,6 +80,7 @@ static const struct crypto_type crypto_kpp_type = {
        .maskset = CRYPTO_ALG_TYPE_MASK,
        .type = CRYPTO_ALG_TYPE_KPP,
        .tfmsize = offsetof(struct crypto_kpp, base),
+       .algsize = offsetof(struct kpp_alg, base),
 };
 
 struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask)
index cdb4897c63e6f9102cdbd0fd146d4a067ec018c9..c2e2c38b5aa8d8b8c23a88e418154424e2e22f5d 100644 (file)
@@ -294,6 +294,7 @@ static const struct crypto_type crypto_lskcipher_type = {
        .maskset = CRYPTO_ALG_TYPE_MASK,
        .type = CRYPTO_ALG_TYPE_LSKCIPHER,
        .tfmsize = offsetof(struct crypto_lskcipher, base),
+       .algsize = offsetof(struct lskcipher_alg, co.base),
 };
 
 static void crypto_lskcipher_exit_tfm_sg(struct crypto_tfm *tfm)
index 9d8804e464226d1c7ec4e1d694bfef222ca9dde1..b8ae6ebc091dd5a1ef392a7ab54577c42797a044 100644 (file)
@@ -98,6 +98,7 @@ static const struct crypto_type crypto_rng_type = {
        .maskset = CRYPTO_ALG_TYPE_MASK,
        .type = CRYPTO_ALG_TYPE_RNG,
        .tfmsize = offsetof(struct crypto_rng, base),
+       .algsize = offsetof(struct rng_alg, base),
 };
 
 struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask)
index c330b81bc5a6bf25700724fcdfeea2e5a0e760b5..f7ce29b4cdb8d59a855fc4dece0089964d393f7c 100644 (file)
@@ -347,6 +347,7 @@ static const struct crypto_type crypto_scomp_type = {
        .maskset = CRYPTO_ALG_TYPE_MASK,
        .type = CRYPTO_ALG_TYPE_SCOMPRESS,
        .tfmsize = offsetof(struct crypto_scomp, base),
+       .algsize = offsetof(struct scomp_alg, base),
 };
 
 static void scomp_prepare_alg(struct scomp_alg *alg)
index 301ab42bf84991c233a22154152d1a9e6c2feaea..a2a7d6609172341a1816bb11665492fd6b2f4cea 100644 (file)
@@ -227,6 +227,7 @@ const struct crypto_type crypto_shash_type = {
        .maskset = CRYPTO_ALG_TYPE_MASK,
        .type = CRYPTO_ALG_TYPE_SHASH,
        .tfmsize = offsetof(struct crypto_shash, base),
+       .algsize = offsetof(struct shash_alg, base),
 };
 
 int crypto_grab_shash(struct crypto_shash_spawn *spawn,
index 53a3dd6fbe3fe656b3fbd7622d672d6a6983fb2e..beba745b640575a12c7c7a5e9f9a764f9c4ce4ec 100644 (file)
@@ -74,6 +74,7 @@ static const struct crypto_type crypto_sig_type = {
        .maskset = CRYPTO_ALG_TYPE_MASK,
        .type = CRYPTO_ALG_TYPE_SIG,
        .tfmsize = offsetof(struct crypto_sig, base),
+       .algsize = offsetof(struct sig_alg, base),
 };
 
 struct crypto_sig *crypto_alloc_sig(const char *alg_name, u32 type, u32 mask)
index 132075a905d99a7f6111f1b8b0241447044a8edd..319215cfded56ca433b56c81fba2fbacef62bf6b 100644 (file)
@@ -620,6 +620,7 @@ static const struct crypto_type crypto_skcipher_type = {
        .maskset = CRYPTO_ALG_TYPE_SKCIPHER_MASK,
        .type = CRYPTO_ALG_TYPE_SKCIPHER,
        .tfmsize = offsetof(struct crypto_skcipher, base),
+       .algsize = offsetof(struct skcipher_alg, base),
 };
 
 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
index 15476b085ce3eea3171c7a3a9ef18f1607a9aacd..b89b1b348095ff64c955a86272c639b41c07a1c9 100644 (file)
  */
 #define CRYPTO_ALG_NEED_FALLBACK       0x00000100
 
+/*
+ * Set if the algorithm data structure should be duplicated into
+ * kmalloc memory before registration.  This is useful for hardware
+ * that can be disconnected at will.  Do not use this if the data
+ * structure is embedded into a bigger one.  Duplicate the overall
+ * data structure in the driver in that case.
+ */
+#define CRYPTO_ALG_DUP_FIRST           0x00000200
+
 /*
  * Set if the algorithm has passed automated run-time testing.  Note that
  * if there is no run-time testing for a given algorithm it is considered