Skip to content

Commit 2825982

Browse files
committed
[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>
1 parent 4cc7720 commit 2825982

4 files changed

Lines changed: 199 additions & 14 deletions

File tree

‎crypto/algapi.c‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121

2222
static LIST_HEAD(crypto_template_list);
2323

24+
void crypto_larval_error(const char *name)
25+
{
26+
struct crypto_alg *alg;
27+
28+
down_read(&crypto_alg_sem);
29+
alg = __crypto_alg_lookup(name);
30+
up_read(&crypto_alg_sem);
31+
32+
if (alg) {
33+
if (crypto_is_larval(alg)) {
34+
struct crypto_larval *larval = (void *)alg;
35+
complete(&larval->completion);
36+
}
37+
crypto_mod_put(alg);
38+
}
39+
}
40+
EXPORT_SYMBOL_GPL(crypto_larval_error);
41+
2442
static inline int crypto_set_driver_name(struct crypto_alg *alg)
2543
{
2644
static const char suffix[] = "-generic";
@@ -60,14 +78,27 @@ static int __crypto_register_alg(struct crypto_alg *alg)
6078
struct crypto_alg *q;
6179
int ret = -EEXIST;
6280

81+
atomic_set(&alg->cra_refcnt, 1);
6382
list_for_each_entry(q, &crypto_alg_list, cra_list) {
6483
if (q == alg)
6584
goto out;
85+
if (crypto_is_larval(q) &&
86+
(!strcmp(alg->cra_name, q->cra_name) ||
87+
!strcmp(alg->cra_driver_name, q->cra_name))) {
88+
struct crypto_larval *larval = (void *)q;
89+
90+
if (!crypto_mod_get(alg))
91+
continue;
92+
larval->adult = alg;
93+
complete(&larval->completion);
94+
}
6695
}
6796

6897
list_add(&alg->cra_list, &crypto_alg_list);
69-
atomic_set(&alg->cra_refcnt, 1);
98+
99+
crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
70100
ret = 0;
101+
71102
out:
72103
return ret;
73104
}
@@ -97,6 +128,7 @@ int crypto_unregister_alg(struct crypto_alg *alg)
97128
list_del_init(&alg->cra_list);
98129
ret = 0;
99130
}
131+
crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
100132
up_write(&crypto_alg_sem);
101133

102134
if (ret)
@@ -123,6 +155,7 @@ int crypto_register_template(struct crypto_template *tmpl)
123155
}
124156

125157
list_add(&tmpl->list, &crypto_template_list);
158+
crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
126159
err = 0;
127160
out:
128161
up_write(&crypto_alg_sem);
@@ -145,8 +178,11 @@ void crypto_unregister_template(struct crypto_template *tmpl)
145178
hlist_for_each_entry(inst, p, list, list) {
146179
BUG_ON(list_empty(&inst->alg.cra_list));
147180
list_del_init(&inst->alg.cra_list);
181+
crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
148182
}
149183

184+
crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
185+
150186
up_write(&crypto_alg_sem);
151187

152188
hlist_for_each_entry_safe(inst, p, n, list, list) {
@@ -212,6 +248,18 @@ int crypto_register_instance(struct crypto_template *tmpl,
212248
}
213249
EXPORT_SYMBOL_GPL(crypto_register_instance);
214250

251+
int crypto_register_notifier(struct notifier_block *nb)
252+
{
253+
return blocking_notifier_chain_register(&crypto_chain, nb);
254+
}
255+
EXPORT_SYMBOL_GPL(crypto_register_notifier);
256+
257+
int crypto_unregister_notifier(struct notifier_block *nb)
258+
{
259+
return blocking_notifier_chain_unregister(&crypto_chain, nb);
260+
}
261+
EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
262+
215263
static int __init crypto_algapi_init(void)
216264
{
217265
crypto_init_proc();

‎crypto/api.c‎

Lines changed: 110 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/errno.h>
1919
#include <linux/kernel.h>
2020
#include <linux/kmod.h>
21+
#include <linux/param.h>
2122
#include <linux/slab.h>
2223
#include <linux/string.h>
2324
#include "internal.h"
@@ -27,6 +28,9 @@ EXPORT_SYMBOL_GPL(crypto_alg_list);
2728
DECLARE_RWSEM(crypto_alg_sem);
2829
EXPORT_SYMBOL_GPL(crypto_alg_sem);
2930

31+
BLOCKING_NOTIFIER_HEAD(crypto_chain);
32+
EXPORT_SYMBOL_GPL(crypto_chain);
33+
3034
static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
3135
{
3236
atomic_inc(&alg->cra_refcnt);
@@ -39,27 +43,24 @@ static inline void crypto_alg_put(struct crypto_alg *alg)
3943
alg->cra_destroy(alg);
4044
}
4145

42-
static struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
46+
struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
4347
{
4448
return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
4549
}
50+
EXPORT_SYMBOL_GPL(crypto_mod_get);
4651

47-
static void crypto_mod_put(struct crypto_alg *alg)
52+
void crypto_mod_put(struct crypto_alg *alg)
4853
{
4954
crypto_alg_put(alg);
5055
module_put(alg->cra_module);
5156
}
57+
EXPORT_SYMBOL_GPL(crypto_mod_put);
5258

53-
static struct crypto_alg *crypto_alg_lookup(const char *name)
59+
struct crypto_alg *__crypto_alg_lookup(const char *name)
5460
{
5561
struct crypto_alg *q, *alg = NULL;
56-
int best = -1;
62+
int best = -2;
5763

58-
if (!name)
59-
return NULL;
60-
61-
down_read(&crypto_alg_sem);
62-
6364
list_for_each_entry(q, &crypto_alg_list, cra_list) {
6465
int exact, fuzzy;
6566

@@ -79,16 +80,113 @@ static struct crypto_alg *crypto_alg_lookup(const char *name)
7980
if (exact)
8081
break;
8182
}
82-
83+
84+
return alg;
85+
}
86+
EXPORT_SYMBOL_GPL(__crypto_alg_lookup);
87+
88+
static void crypto_larval_destroy(struct crypto_alg *alg)
89+
{
90+
struct crypto_larval *larval = (void *)alg;
91+
92+
BUG_ON(!crypto_is_larval(alg));
93+
if (larval->adult)
94+
crypto_mod_put(larval->adult);
95+
kfree(larval);
96+
}
97+
98+
static struct crypto_alg *crypto_larval_alloc(const char *name)
99+
{
100+
struct crypto_alg *alg;
101+
struct crypto_larval *larval;
102+
103+
larval = kzalloc(sizeof(*larval), GFP_KERNEL);
104+
if (!larval)
105+
return NULL;
106+
107+
larval->alg.cra_flags = CRYPTO_ALG_LARVAL;
108+
larval->alg.cra_priority = -1;
109+
larval->alg.cra_destroy = crypto_larval_destroy;
110+
111+
atomic_set(&larval->alg.cra_refcnt, 2);
112+
strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
113+
init_completion(&larval->completion);
114+
115+
down_write(&crypto_alg_sem);
116+
alg = __crypto_alg_lookup(name);
117+
if (!alg) {
118+
alg = &larval->alg;
119+
list_add(&alg->cra_list, &crypto_alg_list);
120+
}
121+
up_write(&crypto_alg_sem);
122+
123+
if (alg != &larval->alg)
124+
kfree(larval);
125+
126+
return alg;
127+
}
128+
129+
static void crypto_larval_kill(struct crypto_alg *alg)
130+
{
131+
struct crypto_larval *larval = (void *)alg;
132+
133+
down_write(&crypto_alg_sem);
134+
list_del(&alg->cra_list);
135+
up_write(&crypto_alg_sem);
136+
complete(&larval->completion);
137+
crypto_alg_put(alg);
138+
}
139+
140+
static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
141+
{
142+
struct crypto_larval *larval = (void *)alg;
143+
144+
wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ);
145+
alg = larval->adult;
146+
if (alg && !crypto_mod_get(alg))
147+
alg = NULL;
148+
crypto_mod_put(&larval->alg);
149+
150+
return alg;
151+
}
152+
153+
static struct crypto_alg *crypto_alg_lookup(const char *name)
154+
{
155+
struct crypto_alg *alg;
156+
157+
if (!name)
158+
return NULL;
159+
160+
down_read(&crypto_alg_sem);
161+
alg = __crypto_alg_lookup(name);
83162
up_read(&crypto_alg_sem);
163+
84164
return alg;
85165
}
86166

87167
/* A far more intelligent version of this is planned. For now, just
88168
* try an exact match on the name of the algorithm. */
89-
static inline struct crypto_alg *crypto_alg_mod_lookup(const char *name)
169+
static struct crypto_alg *crypto_alg_mod_lookup(const char *name)
90170
{
91-
return try_then_request_module(crypto_alg_lookup(name), name);
171+
struct crypto_alg *alg;
172+
struct crypto_alg *larval;
173+
174+
alg = try_then_request_module(crypto_alg_lookup(name), name);
175+
if (alg)
176+
return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
177+
178+
larval = crypto_larval_alloc(name);
179+
if (!larval || !crypto_is_larval(larval))
180+
return larval;
181+
182+
if (crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval) == NOTIFY_STOP)
183+
alg = crypto_larval_wait(larval);
184+
else {
185+
crypto_mod_put(larval);
186+
alg = NULL;
187+
}
188+
crypto_larval_kill(larval);
189+
return alg;
92190
}
93191

94192
static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)

‎crypto/internal.h‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,40 @@
1414
#define _CRYPTO_INTERNAL_H
1515

1616
#include <crypto/algapi.h>
17+
#include <linux/completion.h>
1718
#include <linux/mm.h>
1819
#include <linux/highmem.h>
1920
#include <linux/interrupt.h>
2021
#include <linux/init.h>
2122
#include <linux/list.h>
2223
#include <linux/module.h>
2324
#include <linux/kernel.h>
25+
#include <linux/notifier.h>
2426
#include <linux/rwsem.h>
2527
#include <linux/slab.h>
2628
#include <asm/kmap_types.h>
2729

30+
/* Crypto notification events. */
31+
enum {
32+
CRYPTO_MSG_ALG_REQUEST,
33+
CRYPTO_MSG_ALG_REGISTER,
34+
CRYPTO_MSG_ALG_UNREGISTER,
35+
CRYPTO_MSG_TMPL_REGISTER,
36+
CRYPTO_MSG_TMPL_UNREGISTER,
37+
};
38+
2839
struct crypto_instance;
2940
struct crypto_template;
3041

42+
struct crypto_larval {
43+
struct crypto_alg alg;
44+
struct crypto_alg *adult;
45+
struct completion completion;
46+
};
47+
3148
extern struct list_head crypto_alg_list;
3249
extern struct rw_semaphore crypto_alg_sem;
50+
extern struct blocking_notifier_head crypto_chain;
3351

3452
extern enum km_type crypto_km_types[];
3553

@@ -104,6 +122,10 @@ static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg,
104122
return alg->cra_ctxsize;
105123
}
106124

125+
struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
126+
void crypto_mod_put(struct crypto_alg *alg);
127+
struct crypto_alg *__crypto_alg_lookup(const char *name);
128+
107129
int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags);
108130
int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags);
109131
int crypto_init_compress_flags(struct crypto_tfm *tfm, u32 flags);
@@ -116,9 +138,14 @@ void crypto_exit_digest_ops(struct crypto_tfm *tfm);
116138
void crypto_exit_cipher_ops(struct crypto_tfm *tfm);
117139
void crypto_exit_compress_ops(struct crypto_tfm *tfm);
118140

141+
void crypto_larval_error(const char *name);
142+
119143
int crypto_register_instance(struct crypto_template *tmpl,
120144
struct crypto_instance *inst);
121145

146+
int crypto_register_notifier(struct notifier_block *nb);
147+
int crypto_unregister_notifier(struct notifier_block *nb);
148+
122149
static inline int crypto_tmpl_get(struct crypto_template *tmpl)
123150
{
124151
return try_module_get(tmpl->module);
@@ -129,5 +156,15 @@ static inline void crypto_tmpl_put(struct crypto_template *tmpl)
129156
module_put(tmpl->module);
130157
}
131158

159+
static inline int crypto_is_larval(struct crypto_alg *alg)
160+
{
161+
return alg->cra_flags & CRYPTO_ALG_LARVAL;
162+
}
163+
164+
static inline int crypto_notify(unsigned long val, void *v)
165+
{
166+
return blocking_notifier_call_chain(&crypto_chain, val, v);
167+
}
168+
132169
#endif /* _CRYPTO_INTERNAL_H */
133170

‎include/linux/crypto.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
/*
3030
* Algorithm masks and types.
3131
*/
32-
#define CRYPTO_ALG_TYPE_MASK 0x000000ff
32+
#define CRYPTO_ALG_TYPE_MASK 0x0000000f
3333
#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
3434
#define CRYPTO_ALG_TYPE_DIGEST 0x00000002
3535
#define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
3636

37+
#define CRYPTO_ALG_LARVAL 0x00000010
38+
3739
/*
3840
* Transform masks and values (for crt_flags).
3941
*/

0 commit comments

Comments
 (0)