Skip to content

Commit 4cc7720

Browse files
committed
[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: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent cce9e06 commit 4cc7720

3 files changed

Lines changed: 182 additions & 22 deletions

File tree

‎crypto/algapi.c‎

Lines changed: 134 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
#include <linux/errno.h>
1414
#include <linux/init.h>
1515
#include <linux/kernel.h>
16+
#include <linux/list.h>
1617
#include <linux/module.h>
1718
#include <linux/string.h>
1819

1920
#include "internal.h"
2021

22+
static LIST_HEAD(crypto_template_list);
23+
2124
static inline int crypto_set_driver_name(struct crypto_alg *alg)
2225
{
2326
static const char suffix[] = "-generic";
@@ -35,11 +38,8 @@ static inline int crypto_set_driver_name(struct crypto_alg *alg)
3538
return 0;
3639
}
3740

38-
int crypto_register_alg(struct crypto_alg *alg)
41+
static int crypto_check_alg(struct crypto_alg *alg)
3942
{
40-
int ret;
41-
struct crypto_alg *q;
42-
4343
if (alg->cra_alignmask & (alg->cra_alignmask + 1))
4444
return -EINVAL;
4545

@@ -51,42 +51,52 @@ int crypto_register_alg(struct crypto_alg *alg)
5151

5252
if (alg->cra_priority < 0)
5353
return -EINVAL;
54-
55-
ret = crypto_set_driver_name(alg);
56-
if (unlikely(ret))
57-
return ret;
5854

59-
down_write(&crypto_alg_sem);
60-
55+
return crypto_set_driver_name(alg);
56+
}
57+
58+
static int __crypto_register_alg(struct crypto_alg *alg)
59+
{
60+
struct crypto_alg *q;
61+
int ret = -EEXIST;
62+
6163
list_for_each_entry(q, &crypto_alg_list, cra_list) {
62-
if (q == alg) {
63-
ret = -EEXIST;
64+
if (q == alg)
6465
goto out;
65-
}
6666
}
6767

6868
list_add(&alg->cra_list, &crypto_alg_list);
6969
atomic_set(&alg->cra_refcnt, 1);
70+
ret = 0;
7071
out:
71-
up_write(&crypto_alg_sem);
7272
return ret;
7373
}
74+
75+
int crypto_register_alg(struct crypto_alg *alg)
76+
{
77+
int err;
78+
79+
err = crypto_check_alg(alg);
80+
if (err)
81+
return err;
82+
83+
down_write(&crypto_alg_sem);
84+
err = __crypto_register_alg(alg);
85+
up_write(&crypto_alg_sem);
86+
87+
return err;
88+
}
7489
EXPORT_SYMBOL_GPL(crypto_register_alg);
7590

7691
int crypto_unregister_alg(struct crypto_alg *alg)
7792
{
7893
int ret = -ENOENT;
79-
struct crypto_alg *q;
8094

8195
down_write(&crypto_alg_sem);
82-
list_for_each_entry(q, &crypto_alg_list, cra_list) {
83-
if (alg == q) {
84-
list_del(&alg->cra_list);
85-
ret = 0;
86-
goto out;
87-
}
96+
if (likely(!list_empty(&alg->cra_list))) {
97+
list_del_init(&alg->cra_list);
98+
ret = 0;
8899
}
89-
out:
90100
up_write(&crypto_alg_sem);
91101

92102
if (ret)
@@ -100,6 +110,108 @@ int crypto_unregister_alg(struct crypto_alg *alg)
100110
}
101111
EXPORT_SYMBOL_GPL(crypto_unregister_alg);
102112

113+
int crypto_register_template(struct crypto_template *tmpl)
114+
{
115+
struct crypto_template *q;
116+
int err = -EEXIST;
117+
118+
down_write(&crypto_alg_sem);
119+
120+
list_for_each_entry(q, &crypto_template_list, list) {
121+
if (q == tmpl)
122+
goto out;
123+
}
124+
125+
list_add(&tmpl->list, &crypto_template_list);
126+
err = 0;
127+
out:
128+
up_write(&crypto_alg_sem);
129+
return err;
130+
}
131+
EXPORT_SYMBOL_GPL(crypto_register_template);
132+
133+
void crypto_unregister_template(struct crypto_template *tmpl)
134+
{
135+
struct crypto_instance *inst;
136+
struct hlist_node *p, *n;
137+
struct hlist_head *list;
138+
139+
down_write(&crypto_alg_sem);
140+
141+
BUG_ON(list_empty(&tmpl->list));
142+
list_del_init(&tmpl->list);
143+
144+
list = &tmpl->instances;
145+
hlist_for_each_entry(inst, p, list, list) {
146+
BUG_ON(list_empty(&inst->alg.cra_list));
147+
list_del_init(&inst->alg.cra_list);
148+
}
149+
150+
up_write(&crypto_alg_sem);
151+
152+
hlist_for_each_entry_safe(inst, p, n, list, list) {
153+
BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
154+
tmpl->free(inst);
155+
}
156+
}
157+
EXPORT_SYMBOL_GPL(crypto_unregister_template);
158+
159+
static struct crypto_template *__crypto_lookup_template(const char *name)
160+
{
161+
struct crypto_template *q, *tmpl = NULL;
162+
163+
down_read(&crypto_alg_sem);
164+
list_for_each_entry(q, &crypto_template_list, list) {
165+
if (strcmp(q->name, name))
166+
continue;
167+
if (unlikely(!crypto_tmpl_get(q)))
168+
continue;
169+
170+
tmpl = q;
171+
break;
172+
}
173+
up_read(&crypto_alg_sem);
174+
175+
return tmpl;
176+
}
177+
178+
struct crypto_template *crypto_lookup_template(const char *name)
179+
{
180+
return try_then_request_module(__crypto_lookup_template(name), name);
181+
}
182+
EXPORT_SYMBOL_GPL(crypto_lookup_template);
183+
184+
int crypto_register_instance(struct crypto_template *tmpl,
185+
struct crypto_instance *inst)
186+
{
187+
int err = -EINVAL;
188+
189+
if (inst->alg.cra_destroy)
190+
goto err;
191+
192+
err = crypto_check_alg(&inst->alg);
193+
if (err)
194+
goto err;
195+
196+
inst->alg.cra_module = tmpl->module;
197+
198+
down_write(&crypto_alg_sem);
199+
200+
err = __crypto_register_alg(&inst->alg);
201+
if (err)
202+
goto unlock;
203+
204+
hlist_add_head(&inst->list, &tmpl->instances);
205+
inst->tmpl = tmpl;
206+
207+
unlock:
208+
up_write(&crypto_alg_sem);
209+
210+
err:
211+
return err;
212+
}
213+
EXPORT_SYMBOL_GPL(crypto_register_instance);
214+
103215
static int __init crypto_algapi_init(void)
104216
{
105217
crypto_init_proc();

‎crypto/internal.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919
#include <linux/interrupt.h>
2020
#include <linux/init.h>
2121
#include <linux/list.h>
22+
#include <linux/module.h>
2223
#include <linux/kernel.h>
2324
#include <linux/rwsem.h>
2425
#include <linux/slab.h>
2526
#include <asm/kmap_types.h>
2627

28+
struct crypto_instance;
29+
struct crypto_template;
30+
2731
extern struct list_head crypto_alg_list;
2832
extern struct rw_semaphore crypto_alg_sem;
2933

@@ -112,5 +116,18 @@ void crypto_exit_digest_ops(struct crypto_tfm *tfm);
112116
void crypto_exit_cipher_ops(struct crypto_tfm *tfm);
113117
void crypto_exit_compress_ops(struct crypto_tfm *tfm);
114118

119+
int crypto_register_instance(struct crypto_template *tmpl,
120+
struct crypto_instance *inst);
121+
122+
static inline int crypto_tmpl_get(struct crypto_template *tmpl)
123+
{
124+
return try_module_get(tmpl->module);
125+
}
126+
127+
static inline void crypto_tmpl_put(struct crypto_template *tmpl)
128+
{
129+
module_put(tmpl->module);
130+
}
131+
115132
#endif /* _CRYPTO_INTERNAL_H */
116133

‎include/crypto/algapi.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,36 @@
1414

1515
#include <linux/crypto.h>
1616

17+
struct module;
18+
19+
struct crypto_instance {
20+
struct crypto_alg alg;
21+
22+
struct crypto_template *tmpl;
23+
struct hlist_node list;
24+
25+
void *__ctx[] CRYPTO_MINALIGN_ATTR;
26+
};
27+
28+
struct crypto_template {
29+
struct list_head list;
30+
struct hlist_head instances;
31+
struct module *module;
32+
33+
struct crypto_instance *(*alloc)(void *param, unsigned int len);
34+
void (*free)(struct crypto_instance *inst);
35+
36+
char name[CRYPTO_MAX_ALG_NAME];
37+
};
38+
39+
int crypto_register_template(struct crypto_template *tmpl);
40+
void crypto_unregister_template(struct crypto_template *tmpl);
41+
struct crypto_template *crypto_lookup_template(const char *name);
42+
43+
static inline void *crypto_instance_ctx(struct crypto_instance *inst)
44+
{
45+
return inst->__ctx;
46+
}
47+
1748
#endif /* _CRYPTO_ALGAPI_H */
1849

0 commit comments

Comments
 (0)