]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - crypto/api.c
[CRYPTO] api: Added event notification
[mirror_ubuntu-zesty-kernel.git] / crypto / api.c
1 /*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
7 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/kmod.h>
21 #include <linux/param.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include "internal.h"
25
26 LIST_HEAD(crypto_alg_list);
27 EXPORT_SYMBOL_GPL(crypto_alg_list);
28 DECLARE_RWSEM(crypto_alg_sem);
29 EXPORT_SYMBOL_GPL(crypto_alg_sem);
30
31 BLOCKING_NOTIFIER_HEAD(crypto_chain);
32 EXPORT_SYMBOL_GPL(crypto_chain);
33
34 static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
35 {
36 atomic_inc(&alg->cra_refcnt);
37 return alg;
38 }
39
40 static inline void crypto_alg_put(struct crypto_alg *alg)
41 {
42 if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
43 alg->cra_destroy(alg);
44 }
45
46 struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
47 {
48 return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
49 }
50 EXPORT_SYMBOL_GPL(crypto_mod_get);
51
52 void crypto_mod_put(struct crypto_alg *alg)
53 {
54 crypto_alg_put(alg);
55 module_put(alg->cra_module);
56 }
57 EXPORT_SYMBOL_GPL(crypto_mod_put);
58
59 struct crypto_alg *__crypto_alg_lookup(const char *name)
60 {
61 struct crypto_alg *q, *alg = NULL;
62 int best = -2;
63
64 list_for_each_entry(q, &crypto_alg_list, cra_list) {
65 int exact, fuzzy;
66
67 exact = !strcmp(q->cra_driver_name, name);
68 fuzzy = !strcmp(q->cra_name, name);
69 if (!exact && !(fuzzy && q->cra_priority > best))
70 continue;
71
72 if (unlikely(!crypto_mod_get(q)))
73 continue;
74
75 best = q->cra_priority;
76 if (alg)
77 crypto_mod_put(alg);
78 alg = q;
79
80 if (exact)
81 break;
82 }
83
84 return alg;
85 }
86 EXPORT_SYMBOL_GPL(__crypto_alg_lookup);
87
88 static void crypto_larval_destroy(struct crypto_alg *alg)
89 {
90 struct crypto_larval *larval = (void *)alg;
91
92 BUG_ON(!crypto_is_larval(alg));
93 if (larval->adult)
94 crypto_mod_put(larval->adult);
95 kfree(larval);
96 }
97
98 static struct crypto_alg *crypto_larval_alloc(const char *name)
99 {
100 struct crypto_alg *alg;
101 struct crypto_larval *larval;
102
103 larval = kzalloc(sizeof(*larval), GFP_KERNEL);
104 if (!larval)
105 return NULL;
106
107 larval->alg.cra_flags = CRYPTO_ALG_LARVAL;
108 larval->alg.cra_priority = -1;
109 larval->alg.cra_destroy = crypto_larval_destroy;
110
111 atomic_set(&larval->alg.cra_refcnt, 2);
112 strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
113 init_completion(&larval->completion);
114
115 down_write(&crypto_alg_sem);
116 alg = __crypto_alg_lookup(name);
117 if (!alg) {
118 alg = &larval->alg;
119 list_add(&alg->cra_list, &crypto_alg_list);
120 }
121 up_write(&crypto_alg_sem);
122
123 if (alg != &larval->alg)
124 kfree(larval);
125
126 return alg;
127 }
128
129 static void crypto_larval_kill(struct crypto_alg *alg)
130 {
131 struct crypto_larval *larval = (void *)alg;
132
133 down_write(&crypto_alg_sem);
134 list_del(&alg->cra_list);
135 up_write(&crypto_alg_sem);
136 complete(&larval->completion);
137 crypto_alg_put(alg);
138 }
139
140 static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
141 {
142 struct crypto_larval *larval = (void *)alg;
143
144 wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ);
145 alg = larval->adult;
146 if (alg && !crypto_mod_get(alg))
147 alg = NULL;
148 crypto_mod_put(&larval->alg);
149
150 return alg;
151 }
152
153 static struct crypto_alg *crypto_alg_lookup(const char *name)
154 {
155 struct crypto_alg *alg;
156
157 if (!name)
158 return NULL;
159
160 down_read(&crypto_alg_sem);
161 alg = __crypto_alg_lookup(name);
162 up_read(&crypto_alg_sem);
163
164 return alg;
165 }
166
167 /* A far more intelligent version of this is planned. For now, just
168 * try an exact match on the name of the algorithm. */
169 static struct crypto_alg *crypto_alg_mod_lookup(const char *name)
170 {
171 struct crypto_alg *alg;
172 struct crypto_alg *larval;
173
174 alg = try_then_request_module(crypto_alg_lookup(name), name);
175 if (alg)
176 return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
177
178 larval = crypto_larval_alloc(name);
179 if (!larval || !crypto_is_larval(larval))
180 return larval;
181
182 if (crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval) == NOTIFY_STOP)
183 alg = crypto_larval_wait(larval);
184 else {
185 crypto_mod_put(larval);
186 alg = NULL;
187 }
188 crypto_larval_kill(larval);
189 return alg;
190 }
191
192 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
193 {
194 tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
195 flags &= ~CRYPTO_TFM_REQ_MASK;
196
197 switch (crypto_tfm_alg_type(tfm)) {
198 case CRYPTO_ALG_TYPE_CIPHER:
199 return crypto_init_cipher_flags(tfm, flags);
200
201 case CRYPTO_ALG_TYPE_DIGEST:
202 return crypto_init_digest_flags(tfm, flags);
203
204 case CRYPTO_ALG_TYPE_COMPRESS:
205 return crypto_init_compress_flags(tfm, flags);
206
207 default:
208 break;
209 }
210
211 BUG();
212 return -EINVAL;
213 }
214
215 static int crypto_init_ops(struct crypto_tfm *tfm)
216 {
217 switch (crypto_tfm_alg_type(tfm)) {
218 case CRYPTO_ALG_TYPE_CIPHER:
219 return crypto_init_cipher_ops(tfm);
220
221 case CRYPTO_ALG_TYPE_DIGEST:
222 return crypto_init_digest_ops(tfm);
223
224 case CRYPTO_ALG_TYPE_COMPRESS:
225 return crypto_init_compress_ops(tfm);
226
227 default:
228 break;
229 }
230
231 BUG();
232 return -EINVAL;
233 }
234
235 static void crypto_exit_ops(struct crypto_tfm *tfm)
236 {
237 switch (crypto_tfm_alg_type(tfm)) {
238 case CRYPTO_ALG_TYPE_CIPHER:
239 crypto_exit_cipher_ops(tfm);
240 break;
241
242 case CRYPTO_ALG_TYPE_DIGEST:
243 crypto_exit_digest_ops(tfm);
244 break;
245
246 case CRYPTO_ALG_TYPE_COMPRESS:
247 crypto_exit_compress_ops(tfm);
248 break;
249
250 default:
251 BUG();
252
253 }
254 }
255
256 static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
257 {
258 unsigned int len;
259
260 switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
261 default:
262 BUG();
263
264 case CRYPTO_ALG_TYPE_CIPHER:
265 len = crypto_cipher_ctxsize(alg, flags);
266 break;
267
268 case CRYPTO_ALG_TYPE_DIGEST:
269 len = crypto_digest_ctxsize(alg, flags);
270 break;
271
272 case CRYPTO_ALG_TYPE_COMPRESS:
273 len = crypto_compress_ctxsize(alg, flags);
274 break;
275 }
276
277 return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
278 }
279
280 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
281 {
282 struct crypto_tfm *tfm = NULL;
283 struct crypto_alg *alg;
284 unsigned int tfm_size;
285
286 alg = crypto_alg_mod_lookup(name);
287 if (alg == NULL)
288 goto out;
289
290 tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
291 tfm = kzalloc(tfm_size, GFP_KERNEL);
292 if (tfm == NULL)
293 goto out_put;
294
295 tfm->__crt_alg = alg;
296
297 if (crypto_init_flags(tfm, flags))
298 goto out_free_tfm;
299
300 if (crypto_init_ops(tfm))
301 goto out_free_tfm;
302
303 if (alg->cra_init && alg->cra_init(tfm))
304 goto cra_init_failed;
305
306 goto out;
307
308 cra_init_failed:
309 crypto_exit_ops(tfm);
310 out_free_tfm:
311 kfree(tfm);
312 tfm = NULL;
313 out_put:
314 crypto_mod_put(alg);
315 out:
316 return tfm;
317 }
318
319 void crypto_free_tfm(struct crypto_tfm *tfm)
320 {
321 struct crypto_alg *alg;
322 int size;
323
324 if (unlikely(!tfm))
325 return;
326
327 alg = tfm->__crt_alg;
328 size = sizeof(*tfm) + alg->cra_ctxsize;
329
330 if (alg->cra_exit)
331 alg->cra_exit(tfm);
332 crypto_exit_ops(tfm);
333 crypto_mod_put(alg);
334 memset(tfm, 0, size);
335 kfree(tfm);
336 }
337
338 int crypto_alg_available(const char *name, u32 flags)
339 {
340 int ret = 0;
341 struct crypto_alg *alg = crypto_alg_mod_lookup(name);
342
343 if (alg) {
344 crypto_mod_put(alg);
345 ret = 1;
346 }
347
348 return ret;
349 }
350
351 EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
352 EXPORT_SYMBOL_GPL(crypto_free_tfm);
353 EXPORT_SYMBOL_GPL(crypto_alg_available);