[CRYPTO] api: Add type-safe spawns
[linux-2.6.git] / crypto / algapi.c
index 36c4f1b..0f1abca 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/module.h>
+#include <linux/rtnetlink.h>
 #include <linux/string.h>
 
 #include "internal.h"
@@ -376,7 +377,8 @@ void crypto_drop_spawn(struct crypto_spawn *spawn)
 }
 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
 
-struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn)
+struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
+                                   u32 mask)
 {
        struct crypto_alg *alg;
        struct crypto_alg *alg2;
@@ -395,10 +397,18 @@ struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn)
                return ERR_PTR(-EAGAIN);
        }
 
-       tfm = __crypto_alloc_tfm(alg, 0);
+       tfm = ERR_PTR(-EINVAL);
+       if (unlikely((alg->cra_flags ^ type) & mask))
+               goto out_put_alg;
+
+       tfm = __crypto_alloc_tfm(alg);
        if (IS_ERR(tfm))
-               crypto_mod_put(alg);
+               goto out_put_alg;
+
+       return tfm;
 
+out_put_alg:
+       crypto_mod_put(alg);
        return tfm;
 }
 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
@@ -415,6 +425,58 @@ int crypto_unregister_notifier(struct notifier_block *nb)
 }
 EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
 
+struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len,
+                                      u32 type, u32 mask)
+{
+       struct rtattr *rta = param;
+       struct crypto_attr_alg *alga;
+
+       if (!RTA_OK(rta, len))
+               return ERR_PTR(-EBADR);
+       if (rta->rta_type != CRYPTOA_ALG || RTA_PAYLOAD(rta) < sizeof(*alga))
+               return ERR_PTR(-EINVAL);
+
+       alga = RTA_DATA(rta);
+       alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
+
+       return crypto_alg_mod_lookup(alga->name, type, mask);
+}
+EXPORT_SYMBOL_GPL(crypto_get_attr_alg);
+
+struct crypto_instance *crypto_alloc_instance(const char *name,
+                                             struct crypto_alg *alg)
+{
+       struct crypto_instance *inst;
+       struct crypto_spawn *spawn;
+       int err;
+
+       inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
+       if (!inst)
+               return ERR_PTR(-ENOMEM);
+
+       err = -ENAMETOOLONG;
+       if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
+                    alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
+               goto err_free_inst;
+
+       if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
+                    name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
+               goto err_free_inst;
+
+       spawn = crypto_instance_ctx(inst);
+       err = crypto_init_spawn(spawn, alg, inst);
+
+       if (err)
+               goto err_free_inst;
+
+       return inst;
+
+err_free_inst:
+       kfree(inst);
+       return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(crypto_alloc_instance);
+
 static int __init crypto_algapi_init(void)
 {
        crypto_init_proc();