[CRYPTO] Add alignmask for low-level cipher implementations
[linux-2.6/x86.git] / crypto / api.c
blobf55856b219927e082b83627e4564c7ef3879bef2
1 /*
2 * Scatterlist Cryptographic API.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
7 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
8 * and Nettle, by Niels Möller.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
16 #include <linux/init.h>
17 #include <linux/crypto.h>
18 #include <linux/errno.h>
19 #include <linux/rwsem.h>
20 #include <linux/slab.h>
21 #include "internal.h"
23 LIST_HEAD(crypto_alg_list);
24 DECLARE_RWSEM(crypto_alg_sem);
26 static inline int crypto_alg_get(struct crypto_alg *alg)
28 return try_module_get(alg->cra_module);
31 static inline void crypto_alg_put(struct crypto_alg *alg)
33 module_put(alg->cra_module);
36 struct crypto_alg *crypto_alg_lookup(const char *name)
38 struct crypto_alg *q, *alg = NULL;
40 if (!name)
41 return NULL;
43 down_read(&crypto_alg_sem);
45 list_for_each_entry(q, &crypto_alg_list, cra_list) {
46 if (!(strcmp(q->cra_name, name))) {
47 if (crypto_alg_get(q))
48 alg = q;
49 break;
53 up_read(&crypto_alg_sem);
54 return alg;
57 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
59 tfm->crt_flags = 0;
61 switch (crypto_tfm_alg_type(tfm)) {
62 case CRYPTO_ALG_TYPE_CIPHER:
63 return crypto_init_cipher_flags(tfm, flags);
65 case CRYPTO_ALG_TYPE_DIGEST:
66 return crypto_init_digest_flags(tfm, flags);
68 case CRYPTO_ALG_TYPE_COMPRESS:
69 return crypto_init_compress_flags(tfm, flags);
71 default:
72 break;
75 BUG();
76 return -EINVAL;
79 static int crypto_init_ops(struct crypto_tfm *tfm)
81 switch (crypto_tfm_alg_type(tfm)) {
82 case CRYPTO_ALG_TYPE_CIPHER:
83 return crypto_init_cipher_ops(tfm);
85 case CRYPTO_ALG_TYPE_DIGEST:
86 return crypto_init_digest_ops(tfm);
88 case CRYPTO_ALG_TYPE_COMPRESS:
89 return crypto_init_compress_ops(tfm);
91 default:
92 break;
95 BUG();
96 return -EINVAL;
99 static void crypto_exit_ops(struct crypto_tfm *tfm)
101 switch (crypto_tfm_alg_type(tfm)) {
102 case CRYPTO_ALG_TYPE_CIPHER:
103 crypto_exit_cipher_ops(tfm);
104 break;
106 case CRYPTO_ALG_TYPE_DIGEST:
107 crypto_exit_digest_ops(tfm);
108 break;
110 case CRYPTO_ALG_TYPE_COMPRESS:
111 crypto_exit_compress_ops(tfm);
112 break;
114 default:
115 BUG();
120 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
122 struct crypto_tfm *tfm = NULL;
123 struct crypto_alg *alg;
125 alg = crypto_alg_mod_lookup(name);
126 if (alg == NULL)
127 goto out;
129 tfm = kmalloc(sizeof(*tfm) + alg->cra_ctxsize, GFP_KERNEL);
130 if (tfm == NULL)
131 goto out_put;
133 memset(tfm, 0, sizeof(*tfm) + alg->cra_ctxsize);
135 tfm->__crt_alg = alg;
137 if (crypto_init_flags(tfm, flags))
138 goto out_free_tfm;
140 if (crypto_init_ops(tfm)) {
141 crypto_exit_ops(tfm);
142 goto out_free_tfm;
145 goto out;
147 out_free_tfm:
148 kfree(tfm);
149 tfm = NULL;
150 out_put:
151 crypto_alg_put(alg);
152 out:
153 return tfm;
156 void crypto_free_tfm(struct crypto_tfm *tfm)
158 struct crypto_alg *alg = tfm->__crt_alg;
159 int size = sizeof(*tfm) + alg->cra_ctxsize;
161 crypto_exit_ops(tfm);
162 crypto_alg_put(alg);
163 memset(tfm, 0, size);
164 kfree(tfm);
167 int crypto_register_alg(struct crypto_alg *alg)
169 int ret = 0;
170 struct crypto_alg *q;
172 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
173 return -EINVAL;
175 if (alg->cra_alignmask > PAGE_SIZE)
176 return -EINVAL;
178 down_write(&crypto_alg_sem);
180 list_for_each_entry(q, &crypto_alg_list, cra_list) {
181 if (!(strcmp(q->cra_name, alg->cra_name))) {
182 ret = -EEXIST;
183 goto out;
187 list_add_tail(&alg->cra_list, &crypto_alg_list);
188 out:
189 up_write(&crypto_alg_sem);
190 return ret;
193 int crypto_unregister_alg(struct crypto_alg *alg)
195 int ret = -ENOENT;
196 struct crypto_alg *q;
198 BUG_ON(!alg->cra_module);
200 down_write(&crypto_alg_sem);
201 list_for_each_entry(q, &crypto_alg_list, cra_list) {
202 if (alg == q) {
203 list_del(&alg->cra_list);
204 ret = 0;
205 goto out;
208 out:
209 up_write(&crypto_alg_sem);
210 return ret;
213 int crypto_alg_available(const char *name, u32 flags)
215 int ret = 0;
216 struct crypto_alg *alg = crypto_alg_mod_lookup(name);
218 if (alg) {
219 crypto_alg_put(alg);
220 ret = 1;
223 return ret;
226 static int __init init_crypto(void)
228 printk(KERN_INFO "Initializing Cryptographic API\n");
229 crypto_init_proc();
230 return 0;
233 __initcall(init_crypto);
235 EXPORT_SYMBOL_GPL(crypto_register_alg);
236 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
237 EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
238 EXPORT_SYMBOL_GPL(crypto_free_tfm);
239 EXPORT_SYMBOL_GPL(crypto_alg_available);