[CRYPTO] api: Added event notification

This patch adds a notifier chain for algorithm/template registration events.
This will be used to register compound algorithms such as cbc(aes).  In
future this will also be passed onto user-space through netlink.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Herbert Xu 2006-08-06 21:23:26 +10:00
parent 4cc7720cd1
commit 2825982d9d
4 changed files with 199 additions and 14 deletions

View File

@ -21,6 +21,24 @@
static LIST_HEAD(crypto_template_list);
void crypto_larval_error(const char *name)
{
struct crypto_alg *alg;
down_read(&crypto_alg_sem);
alg = __crypto_alg_lookup(name);
up_read(&crypto_alg_sem);
if (alg) {
if (crypto_is_larval(alg)) {
struct crypto_larval *larval = (void *)alg;
complete(&larval->completion);
}
crypto_mod_put(alg);
}
}
EXPORT_SYMBOL_GPL(crypto_larval_error);
static inline int crypto_set_driver_name(struct crypto_alg *alg)
{
static const char suffix[] = "-generic";
@ -60,14 +78,27 @@ static int __crypto_register_alg(struct crypto_alg *alg)
struct crypto_alg *q;
int ret = -EEXIST;
atomic_set(&alg->cra_refcnt, 1);
list_for_each_entry(q, &crypto_alg_list, cra_list) {
if (q == alg)
goto out;
if (crypto_is_larval(q) &&
(!strcmp(alg->cra_name, q->cra_name) ||
!strcmp(alg->cra_driver_name, q->cra_name))) {
struct crypto_larval *larval = (void *)q;
if (!crypto_mod_get(alg))
continue;
larval->adult = alg;
complete(&larval->completion);
}
}
list_add(&alg->cra_list, &crypto_alg_list);
atomic_set(&alg->cra_refcnt, 1);
crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
ret = 0;
out:
return ret;
}
@ -97,6 +128,7 @@ int crypto_unregister_alg(struct crypto_alg *alg)
list_del_init(&alg->cra_list);
ret = 0;
}
crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
up_write(&crypto_alg_sem);
if (ret)
@ -123,6 +155,7 @@ int crypto_register_template(struct crypto_template *tmpl)
}
list_add(&tmpl->list, &crypto_template_list);
crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
err = 0;
out:
up_write(&crypto_alg_sem);
@ -145,8 +178,11 @@ void crypto_unregister_template(struct crypto_template *tmpl)
hlist_for_each_entry(inst, p, list, list) {
BUG_ON(list_empty(&inst->alg.cra_list));
list_del_init(&inst->alg.cra_list);
crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
}
crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
up_write(&crypto_alg_sem);
hlist_for_each_entry_safe(inst, p, n, list, list) {
@ -212,6 +248,18 @@ int crypto_register_instance(struct crypto_template *tmpl,
}
EXPORT_SYMBOL_GPL(crypto_register_instance);
int crypto_register_notifier(struct notifier_block *nb)
{
return blocking_notifier_chain_register(&crypto_chain, nb);
}
EXPORT_SYMBOL_GPL(crypto_register_notifier);
int crypto_unregister_notifier(struct notifier_block *nb)
{
return blocking_notifier_chain_unregister(&crypto_chain, nb);
}
EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
static int __init crypto_algapi_init(void)
{
crypto_init_proc();

View File

@ -18,6 +18,7 @@
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/kmod.h>
#include <linux/param.h>
#include <linux/slab.h>
#include <linux/string.h>
#include "internal.h"
@ -27,6 +28,9 @@ EXPORT_SYMBOL_GPL(crypto_alg_list);
DECLARE_RWSEM(crypto_alg_sem);
EXPORT_SYMBOL_GPL(crypto_alg_sem);
BLOCKING_NOTIFIER_HEAD(crypto_chain);
EXPORT_SYMBOL_GPL(crypto_chain);
static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
{
atomic_inc(&alg->cra_refcnt);
@ -39,27 +43,24 @@ static inline void crypto_alg_put(struct crypto_alg *alg)
alg->cra_destroy(alg);
}
static struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
{
return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
}
EXPORT_SYMBOL_GPL(crypto_mod_get);
static void crypto_mod_put(struct crypto_alg *alg)
void crypto_mod_put(struct crypto_alg *alg)
{
crypto_alg_put(alg);
module_put(alg->cra_module);
}
EXPORT_SYMBOL_GPL(crypto_mod_put);
static struct crypto_alg *crypto_alg_lookup(const char *name)
struct crypto_alg *__crypto_alg_lookup(const char *name)
{
struct crypto_alg *q, *alg = NULL;
int best = -1;
int best = -2;
if (!name)
return NULL;
down_read(&crypto_alg_sem);
list_for_each_entry(q, &crypto_alg_list, cra_list) {
int exact, fuzzy;
@ -79,16 +80,113 @@ static struct crypto_alg *crypto_alg_lookup(const char *name)
if (exact)
break;
}
return alg;
}
EXPORT_SYMBOL_GPL(__crypto_alg_lookup);
static void crypto_larval_destroy(struct crypto_alg *alg)
{
struct crypto_larval *larval = (void *)alg;
BUG_ON(!crypto_is_larval(alg));
if (larval->adult)
crypto_mod_put(larval->adult);
kfree(larval);
}
static struct crypto_alg *crypto_larval_alloc(const char *name)
{
struct crypto_alg *alg;
struct crypto_larval *larval;
larval = kzalloc(sizeof(*larval), GFP_KERNEL);
if (!larval)
return NULL;
larval->alg.cra_flags = CRYPTO_ALG_LARVAL;
larval->alg.cra_priority = -1;
larval->alg.cra_destroy = crypto_larval_destroy;
atomic_set(&larval->alg.cra_refcnt, 2);
strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
init_completion(&larval->completion);
down_write(&crypto_alg_sem);
alg = __crypto_alg_lookup(name);
if (!alg) {
alg = &larval->alg;
list_add(&alg->cra_list, &crypto_alg_list);
}
up_write(&crypto_alg_sem);
if (alg != &larval->alg)
kfree(larval);
return alg;
}
static void crypto_larval_kill(struct crypto_alg *alg)
{
struct crypto_larval *larval = (void *)alg;
down_write(&crypto_alg_sem);
list_del(&alg->cra_list);
up_write(&crypto_alg_sem);
complete(&larval->completion);
crypto_alg_put(alg);
}
static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
{
struct crypto_larval *larval = (void *)alg;
wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ);
alg = larval->adult;
if (alg && !crypto_mod_get(alg))
alg = NULL;
crypto_mod_put(&larval->alg);
return alg;
}
static struct crypto_alg *crypto_alg_lookup(const char *name)
{
struct crypto_alg *alg;
if (!name)
return NULL;
down_read(&crypto_alg_sem);
alg = __crypto_alg_lookup(name);
up_read(&crypto_alg_sem);
return alg;
}
/* A far more intelligent version of this is planned. For now, just
* try an exact match on the name of the algorithm. */
static inline struct crypto_alg *crypto_alg_mod_lookup(const char *name)
static struct crypto_alg *crypto_alg_mod_lookup(const char *name)
{
return try_then_request_module(crypto_alg_lookup(name), name);
struct crypto_alg *alg;
struct crypto_alg *larval;
alg = try_then_request_module(crypto_alg_lookup(name), name);
if (alg)
return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
larval = crypto_larval_alloc(name);
if (!larval || !crypto_is_larval(larval))
return larval;
if (crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval) == NOTIFY_STOP)
alg = crypto_larval_wait(larval);
else {
crypto_mod_put(larval);
alg = NULL;
}
crypto_larval_kill(larval);
return alg;
}
static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)

View File

@ -14,6 +14,7 @@
#define _CRYPTO_INTERNAL_H
#include <crypto/algapi.h>
#include <linux/completion.h>
#include <linux/mm.h>
#include <linux/highmem.h>
#include <linux/interrupt.h>
@ -21,15 +22,32 @@
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/notifier.h>
#include <linux/rwsem.h>
#include <linux/slab.h>
#include <asm/kmap_types.h>
/* Crypto notification events. */
enum {
CRYPTO_MSG_ALG_REQUEST,
CRYPTO_MSG_ALG_REGISTER,
CRYPTO_MSG_ALG_UNREGISTER,
CRYPTO_MSG_TMPL_REGISTER,
CRYPTO_MSG_TMPL_UNREGISTER,
};
struct crypto_instance;
struct crypto_template;
struct crypto_larval {
struct crypto_alg alg;
struct crypto_alg *adult;
struct completion completion;
};
extern struct list_head crypto_alg_list;
extern struct rw_semaphore crypto_alg_sem;
extern struct blocking_notifier_head crypto_chain;
extern enum km_type crypto_km_types[];
@ -104,6 +122,10 @@ static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg,
return alg->cra_ctxsize;
}
struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
void crypto_mod_put(struct crypto_alg *alg);
struct crypto_alg *__crypto_alg_lookup(const char *name);
int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags);
int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags);
int crypto_init_compress_flags(struct crypto_tfm *tfm, u32 flags);
@ -116,9 +138,14 @@ 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);
void crypto_larval_error(const char *name);
int crypto_register_instance(struct crypto_template *tmpl,
struct crypto_instance *inst);
int crypto_register_notifier(struct notifier_block *nb);
int crypto_unregister_notifier(struct notifier_block *nb);
static inline int crypto_tmpl_get(struct crypto_template *tmpl)
{
return try_module_get(tmpl->module);
@ -129,5 +156,15 @@ static inline void crypto_tmpl_put(struct crypto_template *tmpl)
module_put(tmpl->module);
}
static inline int crypto_is_larval(struct crypto_alg *alg)
{
return alg->cra_flags & CRYPTO_ALG_LARVAL;
}
static inline int crypto_notify(unsigned long val, void *v)
{
return blocking_notifier_call_chain(&crypto_chain, val, v);
}
#endif /* _CRYPTO_INTERNAL_H */

View File

@ -29,11 +29,13 @@
/*
* Algorithm masks and types.
*/
#define CRYPTO_ALG_TYPE_MASK 0x000000ff
#define CRYPTO_ALG_TYPE_MASK 0x0000000f
#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
#define CRYPTO_ALG_TYPE_DIGEST 0x00000002
#define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
#define CRYPTO_ALG_LARVAL 0x00000010
/*
* Transform masks and values (for crt_flags).
*/