More meth updates.
[linux-2.6/linux-mips.git] / crypto / api.c
blob4eec20b963d9c0b113c2a5e4cf4ad87f310b79e5
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 down_read(&crypto_alg_sem);
42 list_for_each_entry(q, &crypto_alg_list, cra_list) {
43 if (!(strcmp(q->cra_name, name))) {
44 if (crypto_alg_get(q))
45 alg = q;
46 break;
50 up_read(&crypto_alg_sem);
51 return alg;
54 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
56 tfm->crt_flags = 0;
58 switch (crypto_tfm_alg_type(tfm)) {
59 case CRYPTO_ALG_TYPE_CIPHER:
60 return crypto_init_cipher_flags(tfm, flags);
62 case CRYPTO_ALG_TYPE_DIGEST:
63 return crypto_init_digest_flags(tfm, flags);
65 case CRYPTO_ALG_TYPE_COMPRESS:
66 return crypto_init_compress_flags(tfm, flags);
68 default:
69 break;
72 BUG();
73 return -EINVAL;
76 static int crypto_init_ops(struct crypto_tfm *tfm)
78 switch (crypto_tfm_alg_type(tfm)) {
79 case CRYPTO_ALG_TYPE_CIPHER:
80 return crypto_init_cipher_ops(tfm);
82 case CRYPTO_ALG_TYPE_DIGEST:
83 return crypto_init_digest_ops(tfm);
85 case CRYPTO_ALG_TYPE_COMPRESS:
86 return crypto_init_compress_ops(tfm);
88 default:
89 break;
92 BUG();
93 return -EINVAL;
96 static void crypto_exit_ops(struct crypto_tfm *tfm)
98 switch (crypto_tfm_alg_type(tfm)) {
99 case CRYPTO_ALG_TYPE_CIPHER:
100 crypto_exit_cipher_ops(tfm);
101 break;
103 case CRYPTO_ALG_TYPE_DIGEST:
104 crypto_exit_digest_ops(tfm);
105 break;
107 case CRYPTO_ALG_TYPE_COMPRESS:
108 crypto_exit_compress_ops(tfm);
109 break;
111 default:
112 BUG();
117 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
119 struct crypto_tfm *tfm = NULL;
120 struct crypto_alg *alg;
122 alg = crypto_alg_mod_lookup(name);
123 if (alg == NULL)
124 goto out;
126 tfm = kmalloc(sizeof(*tfm) + alg->cra_ctxsize, GFP_KERNEL);
127 if (tfm == NULL)
128 goto out_put;
130 memset(tfm, 0, sizeof(*tfm) + alg->cra_ctxsize);
132 tfm->__crt_alg = alg;
134 if (crypto_init_flags(tfm, flags))
135 goto out_free_tfm;
137 if (crypto_init_ops(tfm)) {
138 crypto_exit_ops(tfm);
139 goto out_free_tfm;
142 goto out;
144 out_free_tfm:
145 kfree(tfm);
146 tfm = NULL;
147 out_put:
148 crypto_alg_put(alg);
149 out:
150 return tfm;
153 void crypto_free_tfm(struct crypto_tfm *tfm)
155 crypto_exit_ops(tfm);
156 crypto_alg_put(tfm->__crt_alg);
157 kfree(tfm);
160 int crypto_register_alg(struct crypto_alg *alg)
162 int ret = 0;
163 struct crypto_alg *q;
165 down_write(&crypto_alg_sem);
167 list_for_each_entry(q, &crypto_alg_list, cra_list) {
168 if (!(strcmp(q->cra_name, alg->cra_name))) {
169 ret = -EEXIST;
170 goto out;
174 list_add_tail(&alg->cra_list, &crypto_alg_list);
175 out:
176 up_write(&crypto_alg_sem);
177 return ret;
180 int crypto_unregister_alg(struct crypto_alg *alg)
182 int ret = -ENOENT;
183 struct crypto_alg *q;
185 BUG_ON(!alg->cra_module);
187 down_write(&crypto_alg_sem);
188 list_for_each_entry(q, &crypto_alg_list, cra_list) {
189 if (alg == q) {
190 list_del(&alg->cra_list);
191 ret = 0;
192 goto out;
195 out:
196 up_write(&crypto_alg_sem);
197 return ret;
200 int crypto_alg_available(const char *name, u32 flags)
202 int ret = 0;
203 struct crypto_alg *alg = crypto_alg_mod_lookup(name);
205 if (alg) {
206 crypto_alg_put(alg);
207 ret = 1;
210 return ret;
213 static int __init init_crypto(void)
215 printk(KERN_INFO "Initializing Cryptographic API\n");
216 crypto_init_proc();
217 return 0;
220 __initcall(init_crypto);
222 EXPORT_SYMBOL_GPL(crypto_register_alg);
223 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
224 EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
225 EXPORT_SYMBOL_GPL(crypto_free_tfm);
226 EXPORT_SYMBOL_GPL(crypto_alg_available);