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/byteorder.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/printk.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
26 static inline int crypto_set_driver_name(struct crypto_alg *alg)
28 static const char suffix[] = "-generic";
29 char *driver_name = alg->cra_driver_name;
35 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
36 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
39 memcpy(driver_name + len, suffix, sizeof(suffix));
43 static int crypto_check_alg(struct crypto_alg *alg)
45 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
48 if (alg->cra_blocksize > PAGE_SIZE / 8)
51 if (alg->cra_priority < 0)
54 atomic_set(&alg->cra_refcnt, 1);
56 return crypto_set_driver_name(alg);
59 static int __crypto_register_alg(struct crypto_alg *alg)
64 INIT_LIST_HEAD(&alg->cra_users);
68 list_for_each_entry(q, &crypto_alg_list, cra_list) {
72 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
73 !strcmp(q->cra_name, alg->cra_driver_name))
77 list_add(&alg->cra_list, &crypto_alg_list);
83 void crypto_remove_final(struct list_head *list)
85 struct crypto_alg *alg;
88 list_for_each_entry_safe(alg, n, list, cra_list) {
89 list_del_init(&alg->cra_list);
94 int crypto_register_alg(struct crypto_alg *alg)
98 err = crypto_check_alg(alg);
102 down_write(&crypto_alg_sem);
103 err = __crypto_register_alg(alg);
104 up_write(&crypto_alg_sem);
109 static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
111 if (unlikely(list_empty(&alg->cra_list)))
114 list_del_init(&alg->cra_list);
118 int crypto_unregister_alg(struct crypto_alg *alg)
123 down_write(&crypto_alg_sem);
124 ret = crypto_remove_alg(alg, &list);
125 up_write(&crypto_alg_sem);
130 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
131 if (alg->cra_destroy)
132 alg->cra_destroy(alg);
134 crypto_remove_final(&list);
138 int crypto_register_algs(struct crypto_alg *algs, int count)
142 for (i = 0; i < count; i++) {
143 ret = crypto_register_alg(&algs[i]);
151 for (--i; i >= 0; --i)
152 crypto_unregister_alg(&algs[i]);
157 int crypto_unregister_algs(struct crypto_alg *algs, int count)
161 for (i = 0; i < count; i++) {
162 ret = crypto_unregister_alg(&algs[i]);
164 pr_err("Failed to unregister %s %s: %d\n",
165 algs[i].cra_driver_name, algs[i].cra_name, ret);
171 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
173 struct rtattr *rta = tb[0];
174 struct crypto_attr_type *algt;
177 return ERR_PTR(-ENOENT);
178 if (RTA_PAYLOAD(rta) < sizeof(*algt))
179 return ERR_PTR(-EINVAL);
180 if (rta->rta_type != CRYPTOA_TYPE)
181 return ERR_PTR(-EINVAL);
183 algt = RTA_DATA(rta);
188 int crypto_check_attr_type(struct rtattr **tb, u32 type)
190 struct crypto_attr_type *algt;
192 algt = crypto_get_attr_type(tb);
194 return PTR_ERR(algt);
196 if ((algt->type ^ type) & algt->mask)
202 const char *crypto_attr_alg_name(struct rtattr *rta)
204 struct crypto_attr_alg *alga;
207 return ERR_PTR(-ENOENT);
208 if (RTA_PAYLOAD(rta) < sizeof(*alga))
209 return ERR_PTR(-EINVAL);
210 if (rta->rta_type != CRYPTOA_ALG)
211 return ERR_PTR(-EINVAL);
213 alga = RTA_DATA(rta);
214 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
219 struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
220 const struct crypto_type *frontend,
225 name = crypto_attr_alg_name(rta);
227 return ERR_CAST(name);
229 return crypto_find_alg(name, frontend, type, mask);
232 int crypto_attr_u32(struct rtattr *rta, u32 *num)
234 struct crypto_attr_u32 *nu32;
238 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
240 if (rta->rta_type != CRYPTOA_U32)
243 nu32 = RTA_DATA(rta);
249 static inline void crypto_inc_byte(u8 *a, unsigned int size)
254 for (; size; size--) {
262 void crypto_inc(u8 *a, unsigned int size)
264 __be32 *b = (__be32 *)(a + size);
267 for (; size >= 4; size -= 4) {
268 c = be32_to_cpu(*--b) + 1;
274 crypto_inc_byte(a, size);
277 static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
283 void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
288 for (; size >= 4; size -= 4)
291 crypto_xor_byte((u8 *)a, (u8 *)b, size);
294 unsigned int crypto_alg_extsize(struct crypto_alg *alg)
296 return alg->cra_ctxsize +
297 (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
300 int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
304 struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask);
314 MODULE_LICENSE("GPL");
315 MODULE_DESCRIPTION("Cryptographic algorithms API");