Commit 4cc7720c authored by Herbert Xu's avatar Herbert Xu

[CRYPTO] api: Add template registration

A crypto_template generates a crypto_alg object when given a set of
parameters.  this patch adds the basic data structure fo templates
and code to handle their registration/deregistration.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent cce9e06d
......@@ -13,11 +13,14 @@
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/string.h>
#include "internal.h"
static LIST_HEAD(crypto_template_list);
static inline int crypto_set_driver_name(struct crypto_alg *alg)
{
static const char suffix[] = "-generic";
......@@ -35,11 +38,8 @@ static inline int crypto_set_driver_name(struct crypto_alg *alg)
return 0;
}
int crypto_register_alg(struct crypto_alg *alg)
static int crypto_check_alg(struct crypto_alg *alg)
{
int ret;
struct crypto_alg *q;
if (alg->cra_alignmask & (alg->cra_alignmask + 1))
return -EINVAL;
......@@ -52,41 +52,51 @@ int crypto_register_alg(struct crypto_alg *alg)
if (alg->cra_priority < 0)
return -EINVAL;
ret = crypto_set_driver_name(alg);
if (unlikely(ret))
return ret;
return crypto_set_driver_name(alg);
}
down_write(&crypto_alg_sem);
static int __crypto_register_alg(struct crypto_alg *alg)
{
struct crypto_alg *q;
int ret = -EEXIST;
list_for_each_entry(q, &crypto_alg_list, cra_list) {
if (q == alg) {
ret = -EEXIST;
if (q == alg)
goto out;
}
}
list_add(&alg->cra_list, &crypto_alg_list);
atomic_set(&alg->cra_refcnt, 1);
ret = 0;
out:
up_write(&crypto_alg_sem);
return ret;
}
int crypto_register_alg(struct crypto_alg *alg)
{
int err;
err = crypto_check_alg(alg);
if (err)
return err;
down_write(&crypto_alg_sem);
err = __crypto_register_alg(alg);
up_write(&crypto_alg_sem);
return err;
}
EXPORT_SYMBOL_GPL(crypto_register_alg);
int crypto_unregister_alg(struct crypto_alg *alg)
{
int ret = -ENOENT;
struct crypto_alg *q;
down_write(&crypto_alg_sem);
list_for_each_entry(q, &crypto_alg_list, cra_list) {
if (alg == q) {
list_del(&alg->cra_list);
if (likely(!list_empty(&alg->cra_list))) {
list_del_init(&alg->cra_list);
ret = 0;
goto out;
}
}
out:
up_write(&crypto_alg_sem);
if (ret)
......@@ -100,6 +110,108 @@ out:
}
EXPORT_SYMBOL_GPL(crypto_unregister_alg);
int crypto_register_template(struct crypto_template *tmpl)
{
struct crypto_template *q;
int err = -EEXIST;
down_write(&crypto_alg_sem);
list_for_each_entry(q, &crypto_template_list, list) {
if (q == tmpl)
goto out;
}
list_add(&tmpl->list, &crypto_template_list);
err = 0;
out:
up_write(&crypto_alg_sem);
return err;
}
EXPORT_SYMBOL_GPL(crypto_register_template);
void crypto_unregister_template(struct crypto_template *tmpl)
{
struct crypto_instance *inst;
struct hlist_node *p, *n;
struct hlist_head *list;
down_write(&crypto_alg_sem);
BUG_ON(list_empty(&tmpl->list));
list_del_init(&tmpl->list);
list = &tmpl->instances;
hlist_for_each_entry(inst, p, list, list) {
BUG_ON(list_empty(&inst->alg.cra_list));
list_del_init(&inst->alg.cra_list);
}
up_write(&crypto_alg_sem);
hlist_for_each_entry_safe(inst, p, n, list, list) {
BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
tmpl->free(inst);
}
}
EXPORT_SYMBOL_GPL(crypto_unregister_template);
static struct crypto_template *__crypto_lookup_template(const char *name)
{
struct crypto_template *q, *tmpl = NULL;
down_read(&crypto_alg_sem);
list_for_each_entry(q, &crypto_template_list, list) {
if (strcmp(q->name, name))
continue;
if (unlikely(!crypto_tmpl_get(q)))
continue;
tmpl = q;
break;
}
up_read(&crypto_alg_sem);
return tmpl;
}
struct crypto_template *crypto_lookup_template(const char *name)
{
return try_then_request_module(__crypto_lookup_template(name), name);
}
EXPORT_SYMBOL_GPL(crypto_lookup_template);
int crypto_register_instance(struct crypto_template *tmpl,
struct crypto_instance *inst)
{
int err = -EINVAL;
if (inst->alg.cra_destroy)
goto err;
err = crypto_check_alg(&inst->alg);
if (err)
goto err;
inst->alg.cra_module = tmpl->module;
down_write(&crypto_alg_sem);
err = __crypto_register_alg(&inst->alg);
if (err)
goto unlock;
hlist_add_head(&inst->list, &tmpl->instances);
inst->tmpl = tmpl;
unlock:
up_write(&crypto_alg_sem);
err:
return err;
}
EXPORT_SYMBOL_GPL(crypto_register_instance);
static int __init crypto_algapi_init(void)
{
crypto_init_proc();
......
......@@ -19,11 +19,15 @@
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/rwsem.h>
#include <linux/slab.h>
#include <asm/kmap_types.h>
struct crypto_instance;
struct crypto_template;
extern struct list_head crypto_alg_list;
extern struct rw_semaphore crypto_alg_sem;
......@@ -112,5 +116,18 @@ void crypto_exit_digest_ops(struct crypto_tfm *tfm);
void crypto_exit_cipher_ops(struct crypto_tfm *tfm);
void crypto_exit_compress_ops(struct crypto_tfm *tfm);
int crypto_register_instance(struct crypto_template *tmpl,
struct crypto_instance *inst);
static inline int crypto_tmpl_get(struct crypto_template *tmpl)
{
return try_module_get(tmpl->module);
}
static inline void crypto_tmpl_put(struct crypto_template *tmpl)
{
module_put(tmpl->module);
}
#endif /* _CRYPTO_INTERNAL_H */
......@@ -14,5 +14,36 @@
#include <linux/crypto.h>
struct module;
struct crypto_instance {
struct crypto_alg alg;
struct crypto_template *tmpl;
struct hlist_node list;
void *__ctx[] CRYPTO_MINALIGN_ATTR;
};
struct crypto_template {
struct list_head list;
struct hlist_head instances;
struct module *module;
struct crypto_instance *(*alloc)(void *param, unsigned int len);
void (*free)(struct crypto_instance *inst);
char name[CRYPTO_MAX_ALG_NAME];
};
int crypto_register_template(struct crypto_template *tmpl);
void crypto_unregister_template(struct crypto_template *tmpl);
struct crypto_template *crypto_lookup_template(const char *name);
static inline void *crypto_instance_ctx(struct crypto_instance *inst)
{
return inst->__ctx;
}
#endif /* _CRYPTO_ALGAPI_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment