2 * Cryptographic API for algorithms (i.e., low-level API).
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/string.h>
22 static LIST_HEAD(crypto_template_list);
24 static inline int crypto_set_driver_name(struct crypto_alg *alg)
26 static const char suffix[] = "-generic";
27 char *driver_name = alg->cra_driver_name;
33 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
34 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
37 memcpy(driver_name + len, suffix, sizeof(suffix));
41 static int crypto_check_alg(struct crypto_alg *alg)
43 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
46 if (alg->cra_alignmask & alg->cra_blocksize)
49 if (alg->cra_blocksize > PAGE_SIZE / 8)
52 if (alg->cra_priority < 0)
55 return crypto_set_driver_name(alg);
58 static int __crypto_register_alg(struct crypto_alg *alg)
63 list_for_each_entry(q, &crypto_alg_list, cra_list) {
68 list_add(&alg->cra_list, &crypto_alg_list);
69 atomic_set(&alg->cra_refcnt, 1);
75 int crypto_register_alg(struct crypto_alg *alg)
79 err = crypto_check_alg(alg);
83 down_write(&crypto_alg_sem);
84 err = __crypto_register_alg(alg);
85 up_write(&crypto_alg_sem);
89 EXPORT_SYMBOL_GPL(crypto_register_alg);
91 int crypto_unregister_alg(struct crypto_alg *alg)
95 down_write(&crypto_alg_sem);
96 if (likely(!list_empty(&alg->cra_list))) {
97 list_del_init(&alg->cra_list);
100 up_write(&crypto_alg_sem);
105 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
106 if (alg->cra_destroy)
107 alg->cra_destroy(alg);
111 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
113 int crypto_register_template(struct crypto_template *tmpl)
115 struct crypto_template *q;
118 down_write(&crypto_alg_sem);
120 list_for_each_entry(q, &crypto_template_list, list) {
125 list_add(&tmpl->list, &crypto_template_list);
128 up_write(&crypto_alg_sem);
131 EXPORT_SYMBOL_GPL(crypto_register_template);
133 void crypto_unregister_template(struct crypto_template *tmpl)
135 struct crypto_instance *inst;
136 struct hlist_node *p, *n;
137 struct hlist_head *list;
139 down_write(&crypto_alg_sem);
141 BUG_ON(list_empty(&tmpl->list));
142 list_del_init(&tmpl->list);
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);
150 up_write(&crypto_alg_sem);
152 hlist_for_each_entry_safe(inst, p, n, list, list) {
153 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
157 EXPORT_SYMBOL_GPL(crypto_unregister_template);
159 static struct crypto_template *__crypto_lookup_template(const char *name)
161 struct crypto_template *q, *tmpl = NULL;
163 down_read(&crypto_alg_sem);
164 list_for_each_entry(q, &crypto_template_list, list) {
165 if (strcmp(q->name, name))
167 if (unlikely(!crypto_tmpl_get(q)))
173 up_read(&crypto_alg_sem);
178 struct crypto_template *crypto_lookup_template(const char *name)
180 return try_then_request_module(__crypto_lookup_template(name), name);
182 EXPORT_SYMBOL_GPL(crypto_lookup_template);
184 int crypto_register_instance(struct crypto_template *tmpl,
185 struct crypto_instance *inst)
189 if (inst->alg.cra_destroy)
192 err = crypto_check_alg(&inst->alg);
196 inst->alg.cra_module = tmpl->module;
198 down_write(&crypto_alg_sem);
200 err = __crypto_register_alg(&inst->alg);
204 hlist_add_head(&inst->list, &tmpl->instances);
208 up_write(&crypto_alg_sem);
213 EXPORT_SYMBOL_GPL(crypto_register_instance);
215 static int __init crypto_algapi_init(void)
221 static void __exit crypto_algapi_exit(void)
226 module_init(crypto_algapi_init);
227 module_exit(crypto_algapi_exit);
229 MODULE_LICENSE("GPL");
230 MODULE_DESCRIPTION("Cryptographic algorithms API");