]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
cce9e06d HX |
2 | /* |
3 | * Cryptographic API for algorithms (i.e., low-level API). | |
4 | * | |
5 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> | |
cce9e06d HX |
6 | */ |
7 | ||
13c935bb | 8 | #include <crypto/algapi.h> |
6bfd4809 | 9 | #include <linux/err.h> |
cce9e06d | 10 | #include <linux/errno.h> |
3133d76f | 11 | #include <linux/fips.h> |
cce9e06d HX |
12 | #include <linux/init.h> |
13 | #include <linux/kernel.h> | |
4cc7720c | 14 | #include <linux/list.h> |
cce9e06d | 15 | #include <linux/module.h> |
7fed0bf2 | 16 | #include <linux/rtnetlink.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
cce9e06d HX |
18 | #include <linux/string.h> |
19 | ||
20 | #include "internal.h" | |
21 | ||
4cc7720c HX |
22 | static LIST_HEAD(crypto_template_list); |
23 | ||
002c77a4 JW |
24 | static inline void crypto_check_module_sig(struct module *mod) |
25 | { | |
59afdc7b | 26 | if (fips_enabled && mod && !module_sig_ok(mod)) |
002c77a4 | 27 | panic("Module %s signature verification failed in FIPS mode\n", |
bd4a7c69 | 28 | module_name(mod)); |
002c77a4 JW |
29 | } |
30 | ||
4cc7720c | 31 | static int crypto_check_alg(struct crypto_alg *alg) |
cce9e06d | 32 | { |
002c77a4 JW |
33 | crypto_check_module_sig(alg->cra_module); |
34 | ||
177f87d0 EB |
35 | if (!alg->cra_name[0] || !alg->cra_driver_name[0]) |
36 | return -EINVAL; | |
37 | ||
cce9e06d HX |
38 | if (alg->cra_alignmask & (alg->cra_alignmask + 1)) |
39 | return -EINVAL; | |
40 | ||
a9f7f88a KC |
41 | /* General maximums for all algs. */ |
42 | if (alg->cra_alignmask > MAX_ALGAPI_ALIGNMASK) | |
cce9e06d HX |
43 | return -EINVAL; |
44 | ||
a9f7f88a KC |
45 | if (alg->cra_blocksize > MAX_ALGAPI_BLOCKSIZE) |
46 | return -EINVAL; | |
47 | ||
48 | /* Lower maximums for specific alg types. */ | |
13c935bb SM |
49 | if (!alg->cra_type && (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == |
50 | CRYPTO_ALG_TYPE_CIPHER) { | |
51 | if (alg->cra_alignmask > MAX_CIPHER_ALIGNMASK) | |
52 | return -EINVAL; | |
53 | ||
54 | if (alg->cra_blocksize > MAX_CIPHER_BLOCKSIZE) | |
55 | return -EINVAL; | |
56 | } | |
57 | ||
cce9e06d HX |
58 | if (alg->cra_priority < 0) |
59 | return -EINVAL; | |
cce9e06d | 60 | |
ce8614a3 | 61 | refcount_set(&alg->cra_refcnt, 1); |
e9b8e5be | 62 | |
177f87d0 | 63 | return 0; |
4cc7720c HX |
64 | } |
65 | ||
319382a6 HX |
66 | static void crypto_free_instance(struct crypto_instance *inst) |
67 | { | |
319382a6 HX |
68 | inst->alg.cra_type->free(inst); |
69 | } | |
70 | ||
6bfd4809 HX |
71 | static void crypto_destroy_instance(struct crypto_alg *alg) |
72 | { | |
73 | struct crypto_instance *inst = (void *)alg; | |
74 | struct crypto_template *tmpl = inst->tmpl; | |
75 | ||
319382a6 | 76 | crypto_free_instance(inst); |
6bfd4809 HX |
77 | crypto_tmpl_put(tmpl); |
78 | } | |
79 | ||
02244ba4 HX |
80 | /* |
81 | * This function adds a spawn to the list secondary_spawns which | |
82 | * will be used at the end of crypto_remove_spawns to unregister | |
83 | * instances, unless the spawn happens to be one that is depended | |
84 | * on by the new algorithm (nalg in crypto_remove_spawns). | |
85 | * | |
86 | * This function is also responsible for resurrecting any algorithms | |
87 | * in the dependency chain of nalg by unsetting n->dead. | |
88 | */ | |
2bf29016 HX |
89 | static struct list_head *crypto_more_spawns(struct crypto_alg *alg, |
90 | struct list_head *stack, | |
91 | struct list_head *top, | |
92 | struct list_head *secondary_spawns) | |
93 | { | |
94 | struct crypto_spawn *spawn, *n; | |
95 | ||
304e4818 GT |
96 | spawn = list_first_entry_or_null(stack, struct crypto_spawn, list); |
97 | if (!spawn) | |
2bf29016 HX |
98 | return NULL; |
99 | ||
4f87ee11 HX |
100 | n = list_prev_entry(spawn, list); |
101 | list_move(&spawn->list, secondary_spawns); | |
2bf29016 | 102 | |
4f87ee11 HX |
103 | if (list_is_last(&n->list, stack)) |
104 | return top; | |
2bf29016 | 105 | |
4f87ee11 HX |
106 | n = list_next_entry(n, list); |
107 | if (!spawn->dead) | |
108 | n->dead = false; | |
2bf29016 | 109 | |
4f87ee11 | 110 | return &n->inst->alg.cra_users; |
2bf29016 HX |
111 | } |
112 | ||
1f723710 HX |
113 | static void crypto_remove_instance(struct crypto_instance *inst, |
114 | struct list_head *list) | |
6bfd4809 | 115 | { |
a73e6996 | 116 | struct crypto_template *tmpl = inst->tmpl; |
6bfd4809 | 117 | |
a73e6996 HX |
118 | if (crypto_is_dead(&inst->alg)) |
119 | return; | |
6bfd4809 | 120 | |
a73e6996 | 121 | inst->alg.cra_flags |= CRYPTO_ALG_DEAD; |
38cb2419 | 122 | |
a73e6996 HX |
123 | if (!tmpl || !crypto_tmpl_get(tmpl)) |
124 | return; | |
125 | ||
a73e6996 HX |
126 | list_move(&inst->alg.cra_list, list); |
127 | hlist_del(&inst->list); | |
128 | inst->alg.cra_destroy = crypto_destroy_instance; | |
129 | ||
2bf29016 | 130 | BUG_ON(!list_empty(&inst->alg.cra_users)); |
a73e6996 HX |
131 | } |
132 | ||
02244ba4 HX |
133 | /* |
134 | * Given an algorithm alg, remove all algorithms that depend on it | |
135 | * through spawns. If nalg is not null, then exempt any algorithms | |
136 | * that is depended on by nalg. This is useful when nalg itself | |
137 | * depends on alg. | |
138 | */ | |
89b596ba SK |
139 | void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list, |
140 | struct crypto_alg *nalg) | |
a73e6996 | 141 | { |
2bf29016 | 142 | u32 new_type = (nalg ?: alg)->cra_flags; |
a73e6996 HX |
143 | struct crypto_spawn *spawn, *n; |
144 | LIST_HEAD(secondary_spawns); | |
2bf29016 HX |
145 | struct list_head *spawns; |
146 | LIST_HEAD(stack); | |
147 | LIST_HEAD(top); | |
6bfd4809 | 148 | |
2bf29016 | 149 | spawns = &alg->cra_users; |
a73e6996 HX |
150 | list_for_each_entry_safe(spawn, n, spawns, list) { |
151 | if ((spawn->alg->cra_flags ^ new_type) & spawn->mask) | |
6bfd4809 HX |
152 | continue; |
153 | ||
2bf29016 | 154 | list_move(&spawn->list, &top); |
a73e6996 | 155 | } |
6bfd4809 | 156 | |
02244ba4 HX |
157 | /* |
158 | * Perform a depth-first walk starting from alg through | |
159 | * the cra_users tree. The list stack records the path | |
160 | * from alg to the current spawn. | |
161 | */ | |
2bf29016 HX |
162 | spawns = ⊤ |
163 | do { | |
164 | while (!list_empty(spawns)) { | |
165 | struct crypto_instance *inst; | |
166 | ||
167 | spawn = list_first_entry(spawns, struct crypto_spawn, | |
168 | list); | |
169 | inst = spawn->inst; | |
170 | ||
2bf29016 | 171 | list_move(&spawn->list, &stack); |
5f567fff HX |
172 | spawn->dead = !spawn->registered || &inst->alg != nalg; |
173 | ||
174 | if (!spawn->registered) | |
175 | break; | |
176 | ||
177 | BUG_ON(&inst->alg == alg); | |
2bf29016 HX |
178 | |
179 | if (&inst->alg == nalg) | |
180 | break; | |
181 | ||
2bf29016 | 182 | spawns = &inst->alg.cra_users; |
9a006742 EB |
183 | |
184 | /* | |
5f567fff HX |
185 | * Even if spawn->registered is true, the |
186 | * instance itself may still be unregistered. | |
187 | * This is because it may have failed during | |
188 | * registration. Therefore we still need to | |
189 | * make the following test. | |
190 | * | |
9a006742 EB |
191 | * We may encounter an unregistered instance here, since |
192 | * an instance's spawns are set up prior to the instance | |
193 | * being registered. An unregistered instance will have | |
194 | * NULL ->cra_users.next, since ->cra_users isn't | |
195 | * properly initialized until registration. But an | |
196 | * unregistered instance cannot have any users, so treat | |
197 | * it the same as ->cra_users being empty. | |
198 | */ | |
199 | if (spawns->next == NULL) | |
200 | break; | |
2bf29016 HX |
201 | } |
202 | } while ((spawns = crypto_more_spawns(alg, &stack, &top, | |
203 | &secondary_spawns))); | |
204 | ||
02244ba4 HX |
205 | /* |
206 | * Remove all instances that are marked as dead. Also | |
207 | * complete the resurrection of the others by moving them | |
208 | * back to the cra_users list. | |
209 | */ | |
2bf29016 | 210 | list_for_each_entry_safe(spawn, n, &secondary_spawns, list) { |
4f87ee11 | 211 | if (!spawn->dead) |
2bf29016 | 212 | list_move(&spawn->list, &spawn->alg->cra_users); |
5f567fff | 213 | else if (spawn->registered) |
1f723710 | 214 | crypto_remove_instance(spawn->inst, list); |
6bfd4809 HX |
215 | } |
216 | } | |
89b596ba | 217 | EXPORT_SYMBOL_GPL(crypto_remove_spawns); |
6bfd4809 | 218 | |
73d3864a | 219 | static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg) |
4cc7720c HX |
220 | { |
221 | struct crypto_alg *q; | |
73d3864a | 222 | struct crypto_larval *larval; |
6bfd4809 HX |
223 | int ret = -EAGAIN; |
224 | ||
225 | if (crypto_is_dead(alg)) | |
73d3864a | 226 | goto err; |
6bfd4809 HX |
227 | |
228 | INIT_LIST_HEAD(&alg->cra_users); | |
229 | ||
73d3864a HX |
230 | /* No cheating! */ |
231 | alg->cra_flags &= ~CRYPTO_ALG_TESTED; | |
232 | ||
6bfd4809 | 233 | ret = -EEXIST; |
4cc7720c | 234 | |
cce9e06d | 235 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
4cc7720c | 236 | if (q == alg) |
73d3864a HX |
237 | goto err; |
238 | ||
b8e15992 HX |
239 | if (crypto_is_moribund(q)) |
240 | continue; | |
241 | ||
73d3864a HX |
242 | if (crypto_is_larval(q)) { |
243 | if (!strcmp(alg->cra_driver_name, q->cra_driver_name)) | |
244 | goto err; | |
245 | continue; | |
246 | } | |
247 | ||
248 | if (!strcmp(q->cra_driver_name, alg->cra_name) || | |
249 | !strcmp(q->cra_name, alg->cra_driver_name)) | |
250 | goto err; | |
251 | } | |
252 | ||
253 | larval = crypto_larval_alloc(alg->cra_name, | |
254 | alg->cra_flags | CRYPTO_ALG_TESTED, 0); | |
255 | if (IS_ERR(larval)) | |
256 | goto out; | |
257 | ||
258 | ret = -ENOENT; | |
259 | larval->adult = crypto_mod_get(alg); | |
260 | if (!larval->adult) | |
261 | goto free_larval; | |
262 | ||
ce8614a3 | 263 | refcount_set(&larval->alg.cra_refcnt, 1); |
73d3864a HX |
264 | memcpy(larval->alg.cra_driver_name, alg->cra_driver_name, |
265 | CRYPTO_MAX_ALG_NAME); | |
266 | larval->alg.cra_priority = alg->cra_priority; | |
267 | ||
268 | list_add(&alg->cra_list, &crypto_alg_list); | |
269 | list_add(&larval->alg.cra_list, &crypto_alg_list); | |
270 | ||
1f6669b9 | 271 | crypto_stats_init(alg); |
cac5818c | 272 | |
5357c6c4 | 273 | out: |
73d3864a HX |
274 | return larval; |
275 | ||
276 | free_larval: | |
277 | kfree(larval); | |
278 | err: | |
279 | larval = ERR_PTR(ret); | |
280 | goto out; | |
281 | } | |
282 | ||
283 | void crypto_alg_tested(const char *name, int err) | |
284 | { | |
285 | struct crypto_larval *test; | |
286 | struct crypto_alg *alg; | |
287 | struct crypto_alg *q; | |
288 | LIST_HEAD(list); | |
2bbb3375 | 289 | bool best; |
73d3864a HX |
290 | |
291 | down_write(&crypto_alg_sem); | |
292 | list_for_each_entry(q, &crypto_alg_list, cra_list) { | |
b8e15992 | 293 | if (crypto_is_moribund(q) || !crypto_is_larval(q)) |
73d3864a HX |
294 | continue; |
295 | ||
296 | test = (struct crypto_larval *)q; | |
297 | ||
298 | if (!strcmp(q->cra_driver_name, name)) | |
299 | goto found; | |
300 | } | |
301 | ||
c7235857 | 302 | pr_err("alg: Unexpected test result for %s: %d\n", name, err); |
73d3864a HX |
303 | goto unlock; |
304 | ||
305 | found: | |
b8e15992 | 306 | q->cra_flags |= CRYPTO_ALG_DEAD; |
73d3864a HX |
307 | alg = test->adult; |
308 | if (err || list_empty(&alg->cra_list)) | |
309 | goto complete; | |
310 | ||
311 | alg->cra_flags |= CRYPTO_ALG_TESTED; | |
312 | ||
2bbb3375 HX |
313 | /* Only satisfy larval waiters if we are the best. */ |
314 | best = true; | |
315 | list_for_each_entry(q, &crypto_alg_list, cra_list) { | |
316 | if (crypto_is_moribund(q) || !crypto_is_larval(q)) | |
317 | continue; | |
318 | ||
319 | if (strcmp(alg->cra_name, q->cra_name)) | |
320 | continue; | |
321 | ||
322 | if (q->cra_priority > alg->cra_priority) { | |
323 | best = false; | |
324 | break; | |
325 | } | |
326 | } | |
327 | ||
73d3864a HX |
328 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
329 | if (q == alg) | |
330 | continue; | |
6bfd4809 HX |
331 | |
332 | if (crypto_is_moribund(q)) | |
333 | continue; | |
334 | ||
335 | if (crypto_is_larval(q)) { | |
2825982d HX |
336 | struct crypto_larval *larval = (void *)q; |
337 | ||
d8058480 HX |
338 | /* |
339 | * Check to see if either our generic name or | |
340 | * specific name can satisfy the name requested | |
341 | * by the larval entry q. | |
342 | */ | |
6bfd4809 HX |
343 | if (strcmp(alg->cra_name, q->cra_name) && |
344 | strcmp(alg->cra_driver_name, q->cra_name)) | |
345 | continue; | |
346 | ||
347 | if (larval->adult) | |
348 | continue; | |
492e2b63 HX |
349 | if ((q->cra_flags ^ alg->cra_flags) & larval->mask) |
350 | continue; | |
6bfd4809 | 351 | |
2bbb3375 HX |
352 | if (best && crypto_mod_get(alg)) |
353 | larval->adult = alg; | |
354 | else | |
355 | larval->adult = ERR_PTR(-EAGAIN); | |
356 | ||
6bfd4809 | 357 | continue; |
2825982d | 358 | } |
6bfd4809 HX |
359 | |
360 | if (strcmp(alg->cra_name, q->cra_name)) | |
361 | continue; | |
362 | ||
363 | if (strcmp(alg->cra_driver_name, q->cra_driver_name) && | |
364 | q->cra_priority > alg->cra_priority) | |
365 | continue; | |
366 | ||
2bf29016 | 367 | crypto_remove_spawns(q, &list, alg); |
cce9e06d | 368 | } |
2825982d | 369 | |
73d3864a HX |
370 | complete: |
371 | complete_all(&test->completion); | |
2825982d | 372 | |
73d3864a HX |
373 | unlock: |
374 | up_write(&crypto_alg_sem); | |
375 | ||
376 | crypto_remove_final(&list); | |
cce9e06d | 377 | } |
73d3864a | 378 | EXPORT_SYMBOL_GPL(crypto_alg_tested); |
4cc7720c | 379 | |
22e5b20b | 380 | void crypto_remove_final(struct list_head *list) |
6bfd4809 HX |
381 | { |
382 | struct crypto_alg *alg; | |
383 | struct crypto_alg *n; | |
384 | ||
385 | list_for_each_entry_safe(alg, n, list, cra_list) { | |
386 | list_del_init(&alg->cra_list); | |
387 | crypto_alg_put(alg); | |
388 | } | |
389 | } | |
22e5b20b | 390 | EXPORT_SYMBOL_GPL(crypto_remove_final); |
6bfd4809 | 391 | |
4cc7720c HX |
392 | int crypto_register_alg(struct crypto_alg *alg) |
393 | { | |
73d3864a | 394 | struct crypto_larval *larval; |
adad556e | 395 | bool test_started; |
4cc7720c HX |
396 | int err; |
397 | ||
d6040764 | 398 | alg->cra_flags &= ~CRYPTO_ALG_DEAD; |
4cc7720c HX |
399 | err = crypto_check_alg(alg); |
400 | if (err) | |
401 | return err; | |
402 | ||
403 | down_write(&crypto_alg_sem); | |
73d3864a | 404 | larval = __crypto_register_alg(alg); |
adad556e HX |
405 | test_started = static_key_enabled(&crypto_boot_test_finished); |
406 | larval->test_started = test_started; | |
4cc7720c HX |
407 | up_write(&crypto_alg_sem); |
408 | ||
73d3864a HX |
409 | if (IS_ERR(larval)) |
410 | return PTR_ERR(larval); | |
411 | ||
adad556e HX |
412 | if (test_started) |
413 | crypto_wait_for_test(larval); | |
73d3864a | 414 | return 0; |
4cc7720c | 415 | } |
cce9e06d HX |
416 | EXPORT_SYMBOL_GPL(crypto_register_alg); |
417 | ||
6bfd4809 HX |
418 | static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list) |
419 | { | |
420 | if (unlikely(list_empty(&alg->cra_list))) | |
421 | return -ENOENT; | |
422 | ||
423 | alg->cra_flags |= CRYPTO_ALG_DEAD; | |
424 | ||
6bfd4809 | 425 | list_del_init(&alg->cra_list); |
2bf29016 | 426 | crypto_remove_spawns(alg, list, NULL); |
6bfd4809 HX |
427 | |
428 | return 0; | |
429 | } | |
430 | ||
c6d633a9 | 431 | void crypto_unregister_alg(struct crypto_alg *alg) |
cce9e06d | 432 | { |
6bfd4809 HX |
433 | int ret; |
434 | LIST_HEAD(list); | |
5357c6c4 | 435 | |
cce9e06d | 436 | down_write(&crypto_alg_sem); |
6bfd4809 | 437 | ret = crypto_remove_alg(alg, &list); |
cce9e06d HX |
438 | up_write(&crypto_alg_sem); |
439 | ||
c6d633a9 EB |
440 | if (WARN(ret, "Algorithm %s is not registered", alg->cra_driver_name)) |
441 | return; | |
cce9e06d | 442 | |
ce8614a3 | 443 | BUG_ON(refcount_read(&alg->cra_refcnt) != 1); |
cce9e06d HX |
444 | if (alg->cra_destroy) |
445 | alg->cra_destroy(alg); | |
446 | ||
6bfd4809 | 447 | crypto_remove_final(&list); |
cce9e06d HX |
448 | } |
449 | EXPORT_SYMBOL_GPL(crypto_unregister_alg); | |
450 | ||
4b004346 MB |
451 | int crypto_register_algs(struct crypto_alg *algs, int count) |
452 | { | |
453 | int i, ret; | |
454 | ||
455 | for (i = 0; i < count; i++) { | |
456 | ret = crypto_register_alg(&algs[i]); | |
457 | if (ret) | |
458 | goto err; | |
459 | } | |
460 | ||
461 | return 0; | |
462 | ||
463 | err: | |
464 | for (--i; i >= 0; --i) | |
465 | crypto_unregister_alg(&algs[i]); | |
466 | ||
467 | return ret; | |
468 | } | |
469 | EXPORT_SYMBOL_GPL(crypto_register_algs); | |
470 | ||
c6d633a9 | 471 | void crypto_unregister_algs(struct crypto_alg *algs, int count) |
4b004346 | 472 | { |
c6d633a9 | 473 | int i; |
4b004346 | 474 | |
c6d633a9 EB |
475 | for (i = 0; i < count; i++) |
476 | crypto_unregister_alg(&algs[i]); | |
4b004346 MB |
477 | } |
478 | EXPORT_SYMBOL_GPL(crypto_unregister_algs); | |
479 | ||
4cc7720c HX |
480 | int crypto_register_template(struct crypto_template *tmpl) |
481 | { | |
482 | struct crypto_template *q; | |
483 | int err = -EEXIST; | |
484 | ||
485 | down_write(&crypto_alg_sem); | |
486 | ||
002c77a4 JW |
487 | crypto_check_module_sig(tmpl->module); |
488 | ||
4cc7720c HX |
489 | list_for_each_entry(q, &crypto_template_list, list) { |
490 | if (q == tmpl) | |
491 | goto out; | |
492 | } | |
493 | ||
494 | list_add(&tmpl->list, &crypto_template_list); | |
495 | err = 0; | |
496 | out: | |
497 | up_write(&crypto_alg_sem); | |
498 | return err; | |
499 | } | |
500 | EXPORT_SYMBOL_GPL(crypto_register_template); | |
501 | ||
9572442d XW |
502 | int crypto_register_templates(struct crypto_template *tmpls, int count) |
503 | { | |
504 | int i, err; | |
505 | ||
506 | for (i = 0; i < count; i++) { | |
507 | err = crypto_register_template(&tmpls[i]); | |
508 | if (err) | |
509 | goto out; | |
510 | } | |
511 | return 0; | |
512 | ||
513 | out: | |
514 | for (--i; i >= 0; --i) | |
515 | crypto_unregister_template(&tmpls[i]); | |
516 | return err; | |
517 | } | |
518 | EXPORT_SYMBOL_GPL(crypto_register_templates); | |
519 | ||
4cc7720c HX |
520 | void crypto_unregister_template(struct crypto_template *tmpl) |
521 | { | |
522 | struct crypto_instance *inst; | |
b67bfe0d | 523 | struct hlist_node *n; |
4cc7720c | 524 | struct hlist_head *list; |
6bfd4809 | 525 | LIST_HEAD(users); |
4cc7720c HX |
526 | |
527 | down_write(&crypto_alg_sem); | |
528 | ||
529 | BUG_ON(list_empty(&tmpl->list)); | |
530 | list_del_init(&tmpl->list); | |
531 | ||
532 | list = &tmpl->instances; | |
b67bfe0d | 533 | hlist_for_each_entry(inst, list, list) { |
6bfd4809 | 534 | int err = crypto_remove_alg(&inst->alg, &users); |
0efcb8d5 | 535 | |
6bfd4809 | 536 | BUG_ON(err); |
4cc7720c HX |
537 | } |
538 | ||
539 | up_write(&crypto_alg_sem); | |
540 | ||
b67bfe0d | 541 | hlist_for_each_entry_safe(inst, n, list, list) { |
ce8614a3 | 542 | BUG_ON(refcount_read(&inst->alg.cra_refcnt) != 1); |
319382a6 | 543 | crypto_free_instance(inst); |
4cc7720c | 544 | } |
6bfd4809 | 545 | crypto_remove_final(&users); |
4cc7720c HX |
546 | } |
547 | EXPORT_SYMBOL_GPL(crypto_unregister_template); | |
548 | ||
9572442d XW |
549 | void crypto_unregister_templates(struct crypto_template *tmpls, int count) |
550 | { | |
551 | int i; | |
552 | ||
553 | for (i = count - 1; i >= 0; --i) | |
554 | crypto_unregister_template(&tmpls[i]); | |
555 | } | |
556 | EXPORT_SYMBOL_GPL(crypto_unregister_templates); | |
557 | ||
4cc7720c HX |
558 | static struct crypto_template *__crypto_lookup_template(const char *name) |
559 | { | |
560 | struct crypto_template *q, *tmpl = NULL; | |
561 | ||
562 | down_read(&crypto_alg_sem); | |
563 | list_for_each_entry(q, &crypto_template_list, list) { | |
564 | if (strcmp(q->name, name)) | |
565 | continue; | |
566 | if (unlikely(!crypto_tmpl_get(q))) | |
567 | continue; | |
568 | ||
569 | tmpl = q; | |
570 | break; | |
571 | } | |
572 | up_read(&crypto_alg_sem); | |
573 | ||
574 | return tmpl; | |
575 | } | |
576 | ||
577 | struct crypto_template *crypto_lookup_template(const char *name) | |
578 | { | |
4943ba16 KC |
579 | return try_then_request_module(__crypto_lookup_template(name), |
580 | "crypto-%s", name); | |
4cc7720c HX |
581 | } |
582 | EXPORT_SYMBOL_GPL(crypto_lookup_template); | |
583 | ||
584 | int crypto_register_instance(struct crypto_template *tmpl, | |
585 | struct crypto_instance *inst) | |
586 | { | |
73d3864a | 587 | struct crypto_larval *larval; |
5f567fff | 588 | struct crypto_spawn *spawn; |
73d3864a | 589 | int err; |
4cc7720c | 590 | |
4cc7720c HX |
591 | err = crypto_check_alg(&inst->alg); |
592 | if (err) | |
9c521a20 SM |
593 | return err; |
594 | ||
4cc7720c | 595 | inst->alg.cra_module = tmpl->module; |
64a947b1 | 596 | inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE; |
4cc7720c HX |
597 | |
598 | down_write(&crypto_alg_sem); | |
599 | ||
5f567fff HX |
600 | larval = ERR_PTR(-EAGAIN); |
601 | for (spawn = inst->spawns; spawn;) { | |
602 | struct crypto_spawn *next; | |
603 | ||
604 | if (spawn->dead) | |
605 | goto unlock; | |
606 | ||
607 | next = spawn->next; | |
608 | spawn->inst = inst; | |
609 | spawn->registered = true; | |
610 | ||
aed11cf5 | 611 | crypto_mod_put(spawn->alg); |
5f567fff HX |
612 | |
613 | spawn = next; | |
614 | } | |
615 | ||
73d3864a HX |
616 | larval = __crypto_register_alg(&inst->alg); |
617 | if (IS_ERR(larval)) | |
4cc7720c HX |
618 | goto unlock; |
619 | ||
adad556e HX |
620 | larval->test_started = true; |
621 | ||
4cc7720c HX |
622 | hlist_add_head(&inst->list, &tmpl->instances); |
623 | inst->tmpl = tmpl; | |
624 | ||
625 | unlock: | |
626 | up_write(&crypto_alg_sem); | |
627 | ||
73d3864a HX |
628 | err = PTR_ERR(larval); |
629 | if (IS_ERR(larval)) | |
630 | goto err; | |
631 | ||
632 | crypto_wait_for_test(larval); | |
633 | err = 0; | |
6bfd4809 | 634 | |
4cc7720c HX |
635 | err: |
636 | return err; | |
637 | } | |
638 | EXPORT_SYMBOL_GPL(crypto_register_instance); | |
ce3fd840 | 639 | |
c6d633a9 | 640 | void crypto_unregister_instance(struct crypto_instance *inst) |
ce3fd840 | 641 | { |
1f723710 | 642 | LIST_HEAD(list); |
ce3fd840 | 643 | |
ce3fd840 SK |
644 | down_write(&crypto_alg_sem); |
645 | ||
87b16756 | 646 | crypto_remove_spawns(&inst->alg, &list, NULL); |
1f723710 | 647 | crypto_remove_instance(inst, &list); |
ce3fd840 SK |
648 | |
649 | up_write(&crypto_alg_sem); | |
650 | ||
1f723710 | 651 | crypto_remove_final(&list); |
ce3fd840 SK |
652 | } |
653 | EXPORT_SYMBOL_GPL(crypto_unregister_instance); | |
4cc7720c | 654 | |
aed11cf5 EB |
655 | int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst, |
656 | const char *name, u32 type, u32 mask) | |
6bfd4809 | 657 | { |
aed11cf5 | 658 | struct crypto_alg *alg; |
6bfd4809 HX |
659 | int err = -EAGAIN; |
660 | ||
6b476662 EB |
661 | if (WARN_ON_ONCE(inst == NULL)) |
662 | return -EINVAL; | |
663 | ||
aed11cf5 EB |
664 | /* Allow the result of crypto_attr_alg_name() to be passed directly */ |
665 | if (IS_ERR(name)) | |
666 | return PTR_ERR(name); | |
5f567fff | 667 | |
aed11cf5 EB |
668 | alg = crypto_find_alg(name, spawn->frontend, type, mask); |
669 | if (IS_ERR(alg)) | |
670 | return PTR_ERR(alg); | |
6bfd4809 HX |
671 | |
672 | down_write(&crypto_alg_sem); | |
673 | if (!crypto_is_moribund(alg)) { | |
674 | list_add(&spawn->list, &alg->cra_users); | |
675 | spawn->alg = alg; | |
aed11cf5 EB |
676 | spawn->mask = mask; |
677 | spawn->next = inst->spawns; | |
678 | inst->spawns = spawn; | |
7bcb2c99 EB |
679 | inst->alg.cra_flags |= |
680 | (alg->cra_flags & CRYPTO_ALG_INHERITED_FLAGS); | |
6bfd4809 HX |
681 | err = 0; |
682 | } | |
683 | up_write(&crypto_alg_sem); | |
5f567fff HX |
684 | if (err) |
685 | crypto_mod_put(alg); | |
d6ef2f19 HX |
686 | return err; |
687 | } | |
688 | EXPORT_SYMBOL_GPL(crypto_grab_spawn); | |
689 | ||
6bfd4809 HX |
690 | void crypto_drop_spawn(struct crypto_spawn *spawn) |
691 | { | |
ff670627 EB |
692 | if (!spawn->alg) /* not yet initialized? */ |
693 | return; | |
694 | ||
6bfd4809 | 695 | down_write(&crypto_alg_sem); |
4f87ee11 | 696 | if (!spawn->dead) |
7db3b61b | 697 | list_del(&spawn->list); |
6bfd4809 | 698 | up_write(&crypto_alg_sem); |
5f567fff | 699 | |
aed11cf5 | 700 | if (!spawn->registered) |
5f567fff | 701 | crypto_mod_put(spawn->alg); |
6bfd4809 HX |
702 | } |
703 | EXPORT_SYMBOL_GPL(crypto_drop_spawn); | |
704 | ||
97eedce1 | 705 | static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn) |
6bfd4809 | 706 | { |
6603523b HX |
707 | struct crypto_alg *alg = ERR_PTR(-EAGAIN); |
708 | struct crypto_alg *target; | |
709 | bool shoot = false; | |
6bfd4809 HX |
710 | |
711 | down_read(&crypto_alg_sem); | |
6603523b HX |
712 | if (!spawn->dead) { |
713 | alg = spawn->alg; | |
714 | if (!crypto_mod_get(alg)) { | |
715 | target = crypto_alg_get(alg); | |
716 | shoot = true; | |
717 | alg = ERR_PTR(-EAGAIN); | |
718 | } | |
6bfd4809 | 719 | } |
73669cc5 | 720 | up_read(&crypto_alg_sem); |
6bfd4809 | 721 | |
6603523b HX |
722 | if (shoot) { |
723 | crypto_shoot_alg(target); | |
724 | crypto_alg_put(target); | |
725 | } | |
726 | ||
727 | return alg; | |
97eedce1 HX |
728 | } |
729 | ||
730 | struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, | |
731 | u32 mask) | |
732 | { | |
733 | struct crypto_alg *alg; | |
734 | struct crypto_tfm *tfm; | |
735 | ||
736 | alg = crypto_spawn_alg(spawn); | |
737 | if (IS_ERR(alg)) | |
738 | return ERR_CAST(alg); | |
739 | ||
2e306ee0 HX |
740 | tfm = ERR_PTR(-EINVAL); |
741 | if (unlikely((alg->cra_flags ^ type) & mask)) | |
742 | goto out_put_alg; | |
743 | ||
27d2a330 | 744 | tfm = __crypto_alloc_tfm(alg, type, mask); |
6bfd4809 | 745 | if (IS_ERR(tfm)) |
2e306ee0 HX |
746 | goto out_put_alg; |
747 | ||
748 | return tfm; | |
6bfd4809 | 749 | |
2e306ee0 HX |
750 | out_put_alg: |
751 | crypto_mod_put(alg); | |
6bfd4809 HX |
752 | return tfm; |
753 | } | |
754 | EXPORT_SYMBOL_GPL(crypto_spawn_tfm); | |
755 | ||
97eedce1 HX |
756 | void *crypto_spawn_tfm2(struct crypto_spawn *spawn) |
757 | { | |
758 | struct crypto_alg *alg; | |
759 | struct crypto_tfm *tfm; | |
760 | ||
761 | alg = crypto_spawn_alg(spawn); | |
762 | if (IS_ERR(alg)) | |
763 | return ERR_CAST(alg); | |
764 | ||
765 | tfm = crypto_create_tfm(alg, spawn->frontend); | |
766 | if (IS_ERR(tfm)) | |
767 | goto out_put_alg; | |
768 | ||
769 | return tfm; | |
770 | ||
771 | out_put_alg: | |
772 | crypto_mod_put(alg); | |
773 | return tfm; | |
774 | } | |
775 | EXPORT_SYMBOL_GPL(crypto_spawn_tfm2); | |
776 | ||
2825982d HX |
777 | int crypto_register_notifier(struct notifier_block *nb) |
778 | { | |
779 | return blocking_notifier_chain_register(&crypto_chain, nb); | |
780 | } | |
781 | EXPORT_SYMBOL_GPL(crypto_register_notifier); | |
782 | ||
783 | int crypto_unregister_notifier(struct notifier_block *nb) | |
784 | { | |
785 | return blocking_notifier_chain_unregister(&crypto_chain, nb); | |
786 | } | |
787 | EXPORT_SYMBOL_GPL(crypto_unregister_notifier); | |
788 | ||
ebc610e5 | 789 | struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb) |
7fed0bf2 | 790 | { |
39e1ee01 | 791 | struct rtattr *rta = tb[0]; |
ebc610e5 HX |
792 | struct crypto_attr_type *algt; |
793 | ||
794 | if (!rta) | |
795 | return ERR_PTR(-ENOENT); | |
796 | if (RTA_PAYLOAD(rta) < sizeof(*algt)) | |
797 | return ERR_PTR(-EINVAL); | |
39e1ee01 HX |
798 | if (rta->rta_type != CRYPTOA_TYPE) |
799 | return ERR_PTR(-EINVAL); | |
ebc610e5 HX |
800 | |
801 | algt = RTA_DATA(rta); | |
802 | ||
803 | return algt; | |
804 | } | |
805 | EXPORT_SYMBOL_GPL(crypto_get_attr_type); | |
806 | ||
7bcb2c99 EB |
807 | /** |
808 | * crypto_check_attr_type() - check algorithm type and compute inherited mask | |
809 | * @tb: the template parameters | |
810 | * @type: the algorithm type the template would be instantiated as | |
811 | * @mask_ret: (output) the mask that should be passed to crypto_grab_*() | |
812 | * to restrict the flags of any inner algorithms | |
813 | * | |
814 | * Validate that the algorithm type the user requested is compatible with the | |
815 | * one the template would actually be instantiated as. E.g., if the user is | |
816 | * doing crypto_alloc_shash("cbc(aes)", ...), this would return an error because | |
817 | * the "cbc" template creates an "skcipher" algorithm, not an "shash" algorithm. | |
818 | * | |
819 | * Also compute the mask to use to restrict the flags of any inner algorithms. | |
820 | * | |
821 | * Return: 0 on success; -errno on failure | |
822 | */ | |
823 | int crypto_check_attr_type(struct rtattr **tb, u32 type, u32 *mask_ret) | |
ebc610e5 HX |
824 | { |
825 | struct crypto_attr_type *algt; | |
826 | ||
827 | algt = crypto_get_attr_type(tb); | |
828 | if (IS_ERR(algt)) | |
829 | return PTR_ERR(algt); | |
830 | ||
831 | if ((algt->type ^ type) & algt->mask) | |
832 | return -EINVAL; | |
833 | ||
7bcb2c99 | 834 | *mask_ret = crypto_algt_inherited_mask(algt); |
ebc610e5 HX |
835 | return 0; |
836 | } | |
837 | EXPORT_SYMBOL_GPL(crypto_check_attr_type); | |
838 | ||
68b6c7d6 | 839 | const char *crypto_attr_alg_name(struct rtattr *rta) |
ebc610e5 | 840 | { |
7fed0bf2 HX |
841 | struct crypto_attr_alg *alga; |
842 | ||
ebc610e5 HX |
843 | if (!rta) |
844 | return ERR_PTR(-ENOENT); | |
845 | if (RTA_PAYLOAD(rta) < sizeof(*alga)) | |
7fed0bf2 | 846 | return ERR_PTR(-EINVAL); |
39e1ee01 HX |
847 | if (rta->rta_type != CRYPTOA_ALG) |
848 | return ERR_PTR(-EINVAL); | |
7fed0bf2 HX |
849 | |
850 | alga = RTA_DATA(rta); | |
851 | alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0; | |
852 | ||
68b6c7d6 HX |
853 | return alga->name; |
854 | } | |
855 | EXPORT_SYMBOL_GPL(crypto_attr_alg_name); | |
856 | ||
32f27c74 HX |
857 | int crypto_inst_setname(struct crypto_instance *inst, const char *name, |
858 | struct crypto_alg *alg) | |
859 | { | |
860 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name, | |
861 | alg->cra_name) >= CRYPTO_MAX_ALG_NAME) | |
862 | return -ENAMETOOLONG; | |
863 | ||
864 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", | |
865 | name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) | |
866 | return -ENAMETOOLONG; | |
867 | ||
868 | return 0; | |
869 | } | |
870 | EXPORT_SYMBOL_GPL(crypto_inst_setname); | |
871 | ||
b5b7f088 HX |
872 | void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen) |
873 | { | |
874 | INIT_LIST_HEAD(&queue->list); | |
875 | queue->backlog = &queue->list; | |
876 | queue->qlen = 0; | |
877 | queue->max_qlen = max_qlen; | |
878 | } | |
879 | EXPORT_SYMBOL_GPL(crypto_init_queue); | |
880 | ||
881 | int crypto_enqueue_request(struct crypto_queue *queue, | |
882 | struct crypto_async_request *request) | |
883 | { | |
884 | int err = -EINPROGRESS; | |
885 | ||
886 | if (unlikely(queue->qlen >= queue->max_qlen)) { | |
6b80ea38 GBY |
887 | if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) { |
888 | err = -ENOSPC; | |
b5b7f088 | 889 | goto out; |
6b80ea38 GBY |
890 | } |
891 | err = -EBUSY; | |
b5b7f088 HX |
892 | if (queue->backlog == &queue->list) |
893 | queue->backlog = &request->list; | |
894 | } | |
895 | ||
896 | queue->qlen++; | |
897 | list_add_tail(&request->list, &queue->list); | |
898 | ||
899 | out: | |
900 | return err; | |
901 | } | |
902 | EXPORT_SYMBOL_GPL(crypto_enqueue_request); | |
903 | ||
ec6e2bf3 IP |
904 | void crypto_enqueue_request_head(struct crypto_queue *queue, |
905 | struct crypto_async_request *request) | |
906 | { | |
907 | queue->qlen++; | |
908 | list_add(&request->list, &queue->list); | |
909 | } | |
910 | EXPORT_SYMBOL_GPL(crypto_enqueue_request_head); | |
911 | ||
31d228cc | 912 | struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue) |
b5b7f088 HX |
913 | { |
914 | struct list_head *request; | |
915 | ||
916 | if (unlikely(!queue->qlen)) | |
917 | return NULL; | |
918 | ||
919 | queue->qlen--; | |
920 | ||
921 | if (queue->backlog != &queue->list) | |
922 | queue->backlog = queue->backlog->next; | |
923 | ||
924 | request = queue->list.next; | |
925 | list_del(request); | |
926 | ||
31d228cc | 927 | return list_entry(request, struct crypto_async_request, list); |
b5b7f088 HX |
928 | } |
929 | EXPORT_SYMBOL_GPL(crypto_dequeue_request); | |
930 | ||
7613636d HX |
931 | static inline void crypto_inc_byte(u8 *a, unsigned int size) |
932 | { | |
933 | u8 *b = (a + size); | |
934 | u8 c; | |
935 | ||
936 | for (; size; size--) { | |
937 | c = *--b + 1; | |
938 | *b = c; | |
939 | if (c) | |
940 | break; | |
941 | } | |
942 | } | |
943 | ||
944 | void crypto_inc(u8 *a, unsigned int size) | |
945 | { | |
946 | __be32 *b = (__be32 *)(a + size); | |
947 | u32 c; | |
948 | ||
db91af0f | 949 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || |
27c539ae | 950 | IS_ALIGNED((unsigned long)b, __alignof__(*b))) |
db91af0f AB |
951 | for (; size >= 4; size -= 4) { |
952 | c = be32_to_cpu(*--b) + 1; | |
953 | *b = cpu_to_be32(c); | |
27c539ae | 954 | if (likely(c)) |
db91af0f AB |
955 | return; |
956 | } | |
7613636d HX |
957 | |
958 | crypto_inc_byte(a, size); | |
959 | } | |
960 | EXPORT_SYMBOL_GPL(crypto_inc); | |
961 | ||
a7c391f0 | 962 | void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int len) |
7613636d | 963 | { |
db91af0f AB |
964 | int relalign = 0; |
965 | ||
966 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { | |
967 | int size = sizeof(unsigned long); | |
a7c391f0 AB |
968 | int d = (((unsigned long)dst ^ (unsigned long)src1) | |
969 | ((unsigned long)dst ^ (unsigned long)src2)) & | |
970 | (size - 1); | |
db91af0f AB |
971 | |
972 | relalign = d ? 1 << __ffs(d) : size; | |
973 | ||
974 | /* | |
975 | * If we care about alignment, process as many bytes as | |
976 | * needed to advance dst and src to values whose alignments | |
977 | * equal their relative alignment. This will allow us to | |
978 | * process the remainder of the input using optimal strides. | |
979 | */ | |
980 | while (((unsigned long)dst & (relalign - 1)) && len > 0) { | |
a7c391f0 | 981 | *dst++ = *src1++ ^ *src2++; |
db91af0f AB |
982 | len--; |
983 | } | |
984 | } | |
7613636d | 985 | |
db91af0f | 986 | while (IS_ENABLED(CONFIG_64BIT) && len >= 8 && !(relalign & 7)) { |
a7c391f0 | 987 | *(u64 *)dst = *(u64 *)src1 ^ *(u64 *)src2; |
db91af0f | 988 | dst += 8; |
a7c391f0 AB |
989 | src1 += 8; |
990 | src2 += 8; | |
db91af0f AB |
991 | len -= 8; |
992 | } | |
7613636d | 993 | |
db91af0f | 994 | while (len >= 4 && !(relalign & 3)) { |
a7c391f0 | 995 | *(u32 *)dst = *(u32 *)src1 ^ *(u32 *)src2; |
db91af0f | 996 | dst += 4; |
a7c391f0 AB |
997 | src1 += 4; |
998 | src2 += 4; | |
db91af0f AB |
999 | len -= 4; |
1000 | } | |
1001 | ||
1002 | while (len >= 2 && !(relalign & 1)) { | |
a7c391f0 | 1003 | *(u16 *)dst = *(u16 *)src1 ^ *(u16 *)src2; |
db91af0f | 1004 | dst += 2; |
a7c391f0 AB |
1005 | src1 += 2; |
1006 | src2 += 2; | |
db91af0f AB |
1007 | len -= 2; |
1008 | } | |
7613636d | 1009 | |
db91af0f | 1010 | while (len--) |
a7c391f0 | 1011 | *dst++ = *src1++ ^ *src2++; |
7613636d | 1012 | } |
db91af0f | 1013 | EXPORT_SYMBOL_GPL(__crypto_xor); |
7613636d | 1014 | |
38d21433 HX |
1015 | unsigned int crypto_alg_extsize(struct crypto_alg *alg) |
1016 | { | |
c2110f28 HX |
1017 | return alg->cra_ctxsize + |
1018 | (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1)); | |
38d21433 HX |
1019 | } |
1020 | EXPORT_SYMBOL_GPL(crypto_alg_extsize); | |
1021 | ||
f2aefdab HX |
1022 | int crypto_type_has_alg(const char *name, const struct crypto_type *frontend, |
1023 | u32 type, u32 mask) | |
1024 | { | |
1025 | int ret = 0; | |
1026 | struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask); | |
1027 | ||
1028 | if (!IS_ERR(alg)) { | |
1029 | crypto_mod_put(alg); | |
1030 | ret = 1; | |
1031 | } | |
1032 | ||
1033 | return ret; | |
1034 | } | |
1035 | EXPORT_SYMBOL_GPL(crypto_type_has_alg); | |
1036 | ||
f7d76e05 | 1037 | #ifdef CONFIG_CRYPTO_STATS |
1f6669b9 CL |
1038 | void crypto_stats_init(struct crypto_alg *alg) |
1039 | { | |
1040 | memset(&alg->stats, 0, sizeof(alg->stats)); | |
1041 | } | |
1042 | EXPORT_SYMBOL_GPL(crypto_stats_init); | |
1043 | ||
f7d76e05 CL |
1044 | void crypto_stats_get(struct crypto_alg *alg) |
1045 | { | |
1046 | crypto_alg_get(alg); | |
1047 | } | |
1048 | EXPORT_SYMBOL_GPL(crypto_stats_get); | |
1049 | ||
f7d76e05 CL |
1050 | void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, |
1051 | int ret) | |
1052 | { | |
1053 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { | |
44f13133 | 1054 | atomic64_inc(&alg->stats.aead.err_cnt); |
f7d76e05 | 1055 | } else { |
17c18f9e CL |
1056 | atomic64_inc(&alg->stats.aead.encrypt_cnt); |
1057 | atomic64_add(cryptlen, &alg->stats.aead.encrypt_tlen); | |
f7d76e05 CL |
1058 | } |
1059 | crypto_alg_put(alg); | |
1060 | } | |
1061 | EXPORT_SYMBOL_GPL(crypto_stats_aead_encrypt); | |
1062 | ||
1063 | void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, | |
1064 | int ret) | |
1065 | { | |
1066 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { | |
44f13133 | 1067 | atomic64_inc(&alg->stats.aead.err_cnt); |
f7d76e05 | 1068 | } else { |
17c18f9e CL |
1069 | atomic64_inc(&alg->stats.aead.decrypt_cnt); |
1070 | atomic64_add(cryptlen, &alg->stats.aead.decrypt_tlen); | |
f7d76e05 CL |
1071 | } |
1072 | crypto_alg_put(alg); | |
1073 | } | |
1074 | EXPORT_SYMBOL_GPL(crypto_stats_aead_decrypt); | |
1075 | ||
1076 | void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, | |
1077 | struct crypto_alg *alg) | |
1078 | { | |
1079 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { | |
44f13133 | 1080 | atomic64_inc(&alg->stats.akcipher.err_cnt); |
f7d76e05 | 1081 | } else { |
17c18f9e CL |
1082 | atomic64_inc(&alg->stats.akcipher.encrypt_cnt); |
1083 | atomic64_add(src_len, &alg->stats.akcipher.encrypt_tlen); | |
f7d76e05 CL |
1084 | } |
1085 | crypto_alg_put(alg); | |
1086 | } | |
1087 | EXPORT_SYMBOL_GPL(crypto_stats_akcipher_encrypt); | |
1088 | ||
1089 | void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, | |
1090 | struct crypto_alg *alg) | |
1091 | { | |
1092 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { | |
44f13133 | 1093 | atomic64_inc(&alg->stats.akcipher.err_cnt); |
f7d76e05 | 1094 | } else { |
17c18f9e CL |
1095 | atomic64_inc(&alg->stats.akcipher.decrypt_cnt); |
1096 | atomic64_add(src_len, &alg->stats.akcipher.decrypt_tlen); | |
f7d76e05 CL |
1097 | } |
1098 | crypto_alg_put(alg); | |
1099 | } | |
1100 | EXPORT_SYMBOL_GPL(crypto_stats_akcipher_decrypt); | |
1101 | ||
1102 | void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg) | |
1103 | { | |
1104 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) | |
44f13133 | 1105 | atomic64_inc(&alg->stats.akcipher.err_cnt); |
f7d76e05 | 1106 | else |
17c18f9e | 1107 | atomic64_inc(&alg->stats.akcipher.sign_cnt); |
f7d76e05 CL |
1108 | crypto_alg_put(alg); |
1109 | } | |
1110 | EXPORT_SYMBOL_GPL(crypto_stats_akcipher_sign); | |
1111 | ||
1112 | void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg) | |
1113 | { | |
1114 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) | |
44f13133 | 1115 | atomic64_inc(&alg->stats.akcipher.err_cnt); |
f7d76e05 | 1116 | else |
17c18f9e | 1117 | atomic64_inc(&alg->stats.akcipher.verify_cnt); |
f7d76e05 CL |
1118 | crypto_alg_put(alg); |
1119 | } | |
1120 | EXPORT_SYMBOL_GPL(crypto_stats_akcipher_verify); | |
1121 | ||
1122 | void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg) | |
1123 | { | |
1124 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { | |
44f13133 | 1125 | atomic64_inc(&alg->stats.compress.err_cnt); |
f7d76e05 | 1126 | } else { |
17c18f9e CL |
1127 | atomic64_inc(&alg->stats.compress.compress_cnt); |
1128 | atomic64_add(slen, &alg->stats.compress.compress_tlen); | |
f7d76e05 CL |
1129 | } |
1130 | crypto_alg_put(alg); | |
1131 | } | |
1132 | EXPORT_SYMBOL_GPL(crypto_stats_compress); | |
1133 | ||
1134 | void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg) | |
1135 | { | |
1136 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { | |
44f13133 | 1137 | atomic64_inc(&alg->stats.compress.err_cnt); |
f7d76e05 | 1138 | } else { |
17c18f9e CL |
1139 | atomic64_inc(&alg->stats.compress.decompress_cnt); |
1140 | atomic64_add(slen, &alg->stats.compress.decompress_tlen); | |
f7d76e05 CL |
1141 | } |
1142 | crypto_alg_put(alg); | |
1143 | } | |
1144 | EXPORT_SYMBOL_GPL(crypto_stats_decompress); | |
1145 | ||
1146 | void crypto_stats_ahash_update(unsigned int nbytes, int ret, | |
1147 | struct crypto_alg *alg) | |
1148 | { | |
1149 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) | |
44f13133 | 1150 | atomic64_inc(&alg->stats.hash.err_cnt); |
f7d76e05 | 1151 | else |
17c18f9e | 1152 | atomic64_add(nbytes, &alg->stats.hash.hash_tlen); |
f7d76e05 CL |
1153 | crypto_alg_put(alg); |
1154 | } | |
1155 | EXPORT_SYMBOL_GPL(crypto_stats_ahash_update); | |
1156 | ||
1157 | void crypto_stats_ahash_final(unsigned int nbytes, int ret, | |
1158 | struct crypto_alg *alg) | |
1159 | { | |
1160 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { | |
44f13133 | 1161 | atomic64_inc(&alg->stats.hash.err_cnt); |
f7d76e05 | 1162 | } else { |
17c18f9e CL |
1163 | atomic64_inc(&alg->stats.hash.hash_cnt); |
1164 | atomic64_add(nbytes, &alg->stats.hash.hash_tlen); | |
f7d76e05 CL |
1165 | } |
1166 | crypto_alg_put(alg); | |
1167 | } | |
1168 | EXPORT_SYMBOL_GPL(crypto_stats_ahash_final); | |
1169 | ||
1170 | void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret) | |
1171 | { | |
1172 | if (ret) | |
44f13133 | 1173 | atomic64_inc(&alg->stats.kpp.err_cnt); |
f7d76e05 | 1174 | else |
17c18f9e | 1175 | atomic64_inc(&alg->stats.kpp.setsecret_cnt); |
f7d76e05 CL |
1176 | crypto_alg_put(alg); |
1177 | } | |
1178 | EXPORT_SYMBOL_GPL(crypto_stats_kpp_set_secret); | |
1179 | ||
1180 | void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret) | |
1181 | { | |
1182 | if (ret) | |
44f13133 | 1183 | atomic64_inc(&alg->stats.kpp.err_cnt); |
f7d76e05 | 1184 | else |
17c18f9e | 1185 | atomic64_inc(&alg->stats.kpp.generate_public_key_cnt); |
f7d76e05 CL |
1186 | crypto_alg_put(alg); |
1187 | } | |
1188 | EXPORT_SYMBOL_GPL(crypto_stats_kpp_generate_public_key); | |
1189 | ||
1190 | void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret) | |
1191 | { | |
1192 | if (ret) | |
44f13133 | 1193 | atomic64_inc(&alg->stats.kpp.err_cnt); |
f7d76e05 | 1194 | else |
17c18f9e | 1195 | atomic64_inc(&alg->stats.kpp.compute_shared_secret_cnt); |
f7d76e05 CL |
1196 | crypto_alg_put(alg); |
1197 | } | |
1198 | EXPORT_SYMBOL_GPL(crypto_stats_kpp_compute_shared_secret); | |
1199 | ||
1200 | void crypto_stats_rng_seed(struct crypto_alg *alg, int ret) | |
1201 | { | |
1202 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) | |
44f13133 | 1203 | atomic64_inc(&alg->stats.rng.err_cnt); |
f7d76e05 | 1204 | else |
17c18f9e | 1205 | atomic64_inc(&alg->stats.rng.seed_cnt); |
f7d76e05 CL |
1206 | crypto_alg_put(alg); |
1207 | } | |
1208 | EXPORT_SYMBOL_GPL(crypto_stats_rng_seed); | |
1209 | ||
1210 | void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, | |
1211 | int ret) | |
1212 | { | |
1213 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { | |
44f13133 | 1214 | atomic64_inc(&alg->stats.rng.err_cnt); |
f7d76e05 | 1215 | } else { |
17c18f9e CL |
1216 | atomic64_inc(&alg->stats.rng.generate_cnt); |
1217 | atomic64_add(dlen, &alg->stats.rng.generate_tlen); | |
f7d76e05 CL |
1218 | } |
1219 | crypto_alg_put(alg); | |
1220 | } | |
1221 | EXPORT_SYMBOL_GPL(crypto_stats_rng_generate); | |
1222 | ||
1223 | void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, | |
1224 | struct crypto_alg *alg) | |
1225 | { | |
1226 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { | |
44f13133 | 1227 | atomic64_inc(&alg->stats.cipher.err_cnt); |
f7d76e05 | 1228 | } else { |
17c18f9e CL |
1229 | atomic64_inc(&alg->stats.cipher.encrypt_cnt); |
1230 | atomic64_add(cryptlen, &alg->stats.cipher.encrypt_tlen); | |
f7d76e05 CL |
1231 | } |
1232 | crypto_alg_put(alg); | |
1233 | } | |
1234 | EXPORT_SYMBOL_GPL(crypto_stats_skcipher_encrypt); | |
1235 | ||
1236 | void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, | |
1237 | struct crypto_alg *alg) | |
1238 | { | |
1239 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { | |
44f13133 | 1240 | atomic64_inc(&alg->stats.cipher.err_cnt); |
f7d76e05 | 1241 | } else { |
17c18f9e CL |
1242 | atomic64_inc(&alg->stats.cipher.decrypt_cnt); |
1243 | atomic64_add(cryptlen, &alg->stats.cipher.decrypt_tlen); | |
f7d76e05 CL |
1244 | } |
1245 | crypto_alg_put(alg); | |
1246 | } | |
1247 | EXPORT_SYMBOL_GPL(crypto_stats_skcipher_decrypt); | |
1248 | #endif | |
1249 | ||
adad556e HX |
1250 | static void __init crypto_start_tests(void) |
1251 | { | |
1252 | for (;;) { | |
1253 | struct crypto_larval *larval = NULL; | |
1254 | struct crypto_alg *q; | |
1255 | ||
1256 | down_write(&crypto_alg_sem); | |
1257 | ||
1258 | list_for_each_entry(q, &crypto_alg_list, cra_list) { | |
1259 | struct crypto_larval *l; | |
1260 | ||
1261 | if (!crypto_is_larval(q)) | |
1262 | continue; | |
1263 | ||
1264 | l = (void *)q; | |
1265 | ||
1266 | if (!crypto_is_test_larval(l)) | |
1267 | continue; | |
1268 | ||
1269 | if (l->test_started) | |
1270 | continue; | |
1271 | ||
1272 | l->test_started = true; | |
1273 | larval = l; | |
1274 | break; | |
1275 | } | |
1276 | ||
1277 | up_write(&crypto_alg_sem); | |
1278 | ||
1279 | if (!larval) | |
1280 | break; | |
1281 | ||
1282 | crypto_wait_for_test(larval); | |
1283 | } | |
1284 | ||
1285 | static_branch_enable(&crypto_boot_test_finished); | |
1286 | } | |
1287 | ||
cce9e06d HX |
1288 | static int __init crypto_algapi_init(void) |
1289 | { | |
1290 | crypto_init_proc(); | |
adad556e | 1291 | crypto_start_tests(); |
cce9e06d HX |
1292 | return 0; |
1293 | } | |
1294 | ||
1295 | static void __exit crypto_algapi_exit(void) | |
1296 | { | |
1297 | crypto_exit_proc(); | |
1298 | } | |
1299 | ||
adad556e HX |
1300 | /* |
1301 | * We run this at late_initcall so that all the built-in algorithms | |
1302 | * have had a chance to register themselves first. | |
1303 | */ | |
1304 | late_initcall(crypto_algapi_init); | |
cce9e06d HX |
1305 | module_exit(crypto_algapi_exit); |
1306 | ||
1307 | MODULE_LICENSE("GPL"); | |
1308 | MODULE_DESCRIPTION("Cryptographic algorithms API"); |