blob: e858946adeed7bf15bab4ddcc111e5e0b79e7d41 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xucce9e062006-08-21 21:08:13 +10002/*
3 * Cryptographic API for algorithms (i.e., low-level API).
4 *
5 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xucce9e062006-08-21 21:08:13 +10006 */
7
Salvatore Mesoraca13c935b2018-04-09 15:54:46 +02008#include <crypto/algapi.h>
Herbert Xu6bfd4802006-09-21 11:39:29 +10009#include <linux/err.h>
Herbert Xucce9e062006-08-21 21:08:13 +100010#include <linux/errno.h>
Herbert Xu3133d762015-04-22 13:25:53 +080011#include <linux/fips.h>
Herbert Xucce9e062006-08-21 21:08:13 +100012#include <linux/init.h>
13#include <linux/kernel.h>
Herbert Xu4cc77202006-08-06 21:16:34 +100014#include <linux/list.h>
Herbert Xucce9e062006-08-21 21:08:13 +100015#include <linux/module.h>
Herbert Xu7fed0bf2006-08-06 23:10:45 +100016#include <linux/rtnetlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Herbert Xucce9e062006-08-21 21:08:13 +100018#include <linux/string.h>
19
20#include "internal.h"
21
Herbert Xu4cc77202006-08-06 21:16:34 +100022static LIST_HEAD(crypto_template_list);
23
Jarod Wilson002c77a2014-07-02 15:37:30 -040024static inline void crypto_check_module_sig(struct module *mod)
25{
Herbert Xu59afdc72015-04-22 11:28:46 +080026 if (fips_enabled && mod && !module_sig_ok(mod))
Jarod Wilson002c77a2014-07-02 15:37:30 -040027 panic("Module %s signature verification failed in FIPS mode\n",
Herbert Xubd4a7c62015-04-23 14:48:05 +080028 module_name(mod));
Jarod Wilson002c77a2014-07-02 15:37:30 -040029}
30
Herbert Xu4cc77202006-08-06 21:16:34 +100031static int crypto_check_alg(struct crypto_alg *alg)
Herbert Xucce9e062006-08-21 21:08:13 +100032{
Jarod Wilson002c77a2014-07-02 15:37:30 -040033 crypto_check_module_sig(alg->cra_module);
34
Eric Biggers177f87d2019-06-02 22:40:58 -070035 if (!alg->cra_name[0] || !alg->cra_driver_name[0])
36 return -EINVAL;
37
Herbert Xucce9e062006-08-21 21:08:13 +100038 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
39 return -EINVAL;
40
Kees Cooka9f7f882018-08-07 14:18:40 -070041 /* General maximums for all algs. */
42 if (alg->cra_alignmask > MAX_ALGAPI_ALIGNMASK)
Herbert Xucce9e062006-08-21 21:08:13 +100043 return -EINVAL;
44
Kees Cooka9f7f882018-08-07 14:18:40 -070045 if (alg->cra_blocksize > MAX_ALGAPI_BLOCKSIZE)
46 return -EINVAL;
47
48 /* Lower maximums for specific alg types. */
Salvatore Mesoraca13c935b2018-04-09 15:54:46 +020049 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
Herbert Xucce9e062006-08-21 21:08:13 +100058 if (alg->cra_priority < 0)
59 return -EINVAL;
Herbert Xucce9e062006-08-21 21:08:13 +100060
Eric Biggersce8614a2017-12-29 10:00:46 -060061 refcount_set(&alg->cra_refcnt, 1);
Herbert Xue9b8e5b2015-04-09 17:40:35 +080062
Eric Biggers177f87d2019-06-02 22:40:58 -070063 return 0;
Herbert Xu4cc77202006-08-06 21:16:34 +100064}
65
Herbert Xu319382a2015-07-09 07:17:15 +080066static void crypto_free_instance(struct crypto_instance *inst)
67{
68 if (!inst->alg.cra_type->free) {
69 inst->tmpl->free(inst);
70 return;
71 }
72
73 inst->alg.cra_type->free(inst);
74}
75
Herbert Xu6bfd4802006-09-21 11:39:29 +100076static void crypto_destroy_instance(struct crypto_alg *alg)
77{
78 struct crypto_instance *inst = (void *)alg;
79 struct crypto_template *tmpl = inst->tmpl;
80
Herbert Xu319382a2015-07-09 07:17:15 +080081 crypto_free_instance(inst);
Herbert Xu6bfd4802006-09-21 11:39:29 +100082 crypto_tmpl_put(tmpl);
83}
84
Herbert Xu2bf29012009-08-31 15:56:54 +100085static struct list_head *crypto_more_spawns(struct crypto_alg *alg,
86 struct list_head *stack,
87 struct list_head *top,
88 struct list_head *secondary_spawns)
89{
90 struct crypto_spawn *spawn, *n;
91
Geliang Tang304e4812015-11-16 22:37:14 +080092 spawn = list_first_entry_or_null(stack, struct crypto_spawn, list);
93 if (!spawn)
Herbert Xu2bf29012009-08-31 15:56:54 +100094 return NULL;
95
Herbert Xu4f87ee12019-12-07 22:15:17 +080096 n = list_prev_entry(spawn, list);
Herbert Xu2bf29012009-08-31 15:56:54 +100097 list_move(&spawn->list, secondary_spawns);
98
Herbert Xu4f87ee12019-12-07 22:15:17 +080099 if (list_is_last(&n->list, stack))
100 return top;
101
102 n = list_next_entry(n, list);
103 if (!spawn->dead)
104 n->dead = false;
105
106 return &n->inst->alg.cra_users;
Herbert Xu2bf29012009-08-31 15:56:54 +1000107}
108
Herbert Xu1f723712015-04-02 22:31:22 +0800109static void crypto_remove_instance(struct crypto_instance *inst,
110 struct list_head *list)
Herbert Xua73e6992007-04-08 21:31:36 +1000111{
Herbert Xua73e6992007-04-08 21:31:36 +1000112 struct crypto_template *tmpl = inst->tmpl;
113
Herbert Xua73e6992007-04-08 21:31:36 +1000114 if (crypto_is_dead(&inst->alg))
115 return;
116
117 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
Herbert Xu38cb2412007-11-17 22:09:38 +0800118 if (hlist_unhashed(&inst->list))
119 return;
120
Herbert Xua73e6992007-04-08 21:31:36 +1000121 if (!tmpl || !crypto_tmpl_get(tmpl))
122 return;
123
Herbert Xua73e6992007-04-08 21:31:36 +1000124 list_move(&inst->alg.cra_list, list);
125 hlist_del(&inst->list);
126 inst->alg.cra_destroy = crypto_destroy_instance;
127
Herbert Xu2bf29012009-08-31 15:56:54 +1000128 BUG_ON(!list_empty(&inst->alg.cra_users));
Herbert Xua73e6992007-04-08 21:31:36 +1000129}
130
Steffen Klassert89b596b2011-09-27 07:22:08 +0200131void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
132 struct crypto_alg *nalg)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000133{
Herbert Xu2bf29012009-08-31 15:56:54 +1000134 u32 new_type = (nalg ?: alg)->cra_flags;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000135 struct crypto_spawn *spawn, *n;
Herbert Xua73e6992007-04-08 21:31:36 +1000136 LIST_HEAD(secondary_spawns);
Herbert Xu2bf29012009-08-31 15:56:54 +1000137 struct list_head *spawns;
138 LIST_HEAD(stack);
139 LIST_HEAD(top);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000140
Herbert Xu2bf29012009-08-31 15:56:54 +1000141 spawns = &alg->cra_users;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000142 list_for_each_entry_safe(spawn, n, spawns, list) {
Herbert Xua73e6992007-04-08 21:31:36 +1000143 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000144 continue;
145
Herbert Xu2bf29012009-08-31 15:56:54 +1000146 list_move(&spawn->list, &top);
Herbert Xua73e6992007-04-08 21:31:36 +1000147 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000148
Herbert Xu2bf29012009-08-31 15:56:54 +1000149 spawns = &top;
150 do {
151 while (!list_empty(spawns)) {
152 struct crypto_instance *inst;
153
154 spawn = list_first_entry(spawns, struct crypto_spawn,
155 list);
156 inst = spawn->inst;
157
158 BUG_ON(&inst->alg == alg);
159
160 list_move(&spawn->list, &stack);
161
162 if (&inst->alg == nalg)
163 break;
164
Herbert Xu4f87ee12019-12-07 22:15:17 +0800165 spawn->dead = true;
Herbert Xu2bf29012009-08-31 15:56:54 +1000166 spawns = &inst->alg.cra_users;
Eric Biggers9a006742017-12-29 14:30:19 -0600167
168 /*
169 * We may encounter an unregistered instance here, since
170 * an instance's spawns are set up prior to the instance
171 * being registered. An unregistered instance will have
172 * NULL ->cra_users.next, since ->cra_users isn't
173 * properly initialized until registration. But an
174 * unregistered instance cannot have any users, so treat
175 * it the same as ->cra_users being empty.
176 */
177 if (spawns->next == NULL)
178 break;
Herbert Xu2bf29012009-08-31 15:56:54 +1000179 }
180 } while ((spawns = crypto_more_spawns(alg, &stack, &top,
181 &secondary_spawns)));
182
183 list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
Herbert Xu4f87ee12019-12-07 22:15:17 +0800184 if (!spawn->dead)
Herbert Xu2bf29012009-08-31 15:56:54 +1000185 list_move(&spawn->list, &spawn->alg->cra_users);
186 else
Herbert Xu1f723712015-04-02 22:31:22 +0800187 crypto_remove_instance(spawn->inst, list);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000188 }
189}
Steffen Klassert89b596b2011-09-27 07:22:08 +0200190EXPORT_SYMBOL_GPL(crypto_remove_spawns);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000191
Herbert Xu73d38642008-08-03 21:15:23 +0800192static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
Herbert Xu4cc77202006-08-06 21:16:34 +1000193{
194 struct crypto_alg *q;
Herbert Xu73d38642008-08-03 21:15:23 +0800195 struct crypto_larval *larval;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000196 int ret = -EAGAIN;
197
198 if (crypto_is_dead(alg))
Herbert Xu73d38642008-08-03 21:15:23 +0800199 goto err;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000200
201 INIT_LIST_HEAD(&alg->cra_users);
202
Herbert Xu73d38642008-08-03 21:15:23 +0800203 /* No cheating! */
204 alg->cra_flags &= ~CRYPTO_ALG_TESTED;
205
Herbert Xu6bfd4802006-09-21 11:39:29 +1000206 ret = -EEXIST;
Herbert Xu4cc77202006-08-06 21:16:34 +1000207
Herbert Xucce9e062006-08-21 21:08:13 +1000208 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu4cc77202006-08-06 21:16:34 +1000209 if (q == alg)
Herbert Xu73d38642008-08-03 21:15:23 +0800210 goto err;
211
Herbert Xub8e15992009-01-28 14:09:59 +1100212 if (crypto_is_moribund(q))
213 continue;
214
Herbert Xu73d38642008-08-03 21:15:23 +0800215 if (crypto_is_larval(q)) {
216 if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
217 goto err;
218 continue;
219 }
220
221 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
222 !strcmp(q->cra_name, alg->cra_driver_name))
223 goto err;
224 }
225
226 larval = crypto_larval_alloc(alg->cra_name,
227 alg->cra_flags | CRYPTO_ALG_TESTED, 0);
228 if (IS_ERR(larval))
229 goto out;
230
231 ret = -ENOENT;
232 larval->adult = crypto_mod_get(alg);
233 if (!larval->adult)
234 goto free_larval;
235
Eric Biggersce8614a2017-12-29 10:00:46 -0600236 refcount_set(&larval->alg.cra_refcnt, 1);
Herbert Xu73d38642008-08-03 21:15:23 +0800237 memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
238 CRYPTO_MAX_ALG_NAME);
239 larval->alg.cra_priority = alg->cra_priority;
240
241 list_add(&alg->cra_list, &crypto_alg_list);
242 list_add(&larval->alg.cra_list, &crypto_alg_list);
243
Corentin Labbe1f6669b2018-11-29 14:42:26 +0000244 crypto_stats_init(alg);
Corentin Labbecac58182018-09-19 10:10:54 +0000245
Richard Hartmann5357c6c2010-02-16 20:25:21 +0800246out:
Herbert Xu73d38642008-08-03 21:15:23 +0800247 return larval;
248
249free_larval:
250 kfree(larval);
251err:
252 larval = ERR_PTR(ret);
253 goto out;
254}
255
256void crypto_alg_tested(const char *name, int err)
257{
258 struct crypto_larval *test;
259 struct crypto_alg *alg;
260 struct crypto_alg *q;
261 LIST_HEAD(list);
262
263 down_write(&crypto_alg_sem);
264 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xub8e15992009-01-28 14:09:59 +1100265 if (crypto_is_moribund(q) || !crypto_is_larval(q))
Herbert Xu73d38642008-08-03 21:15:23 +0800266 continue;
267
268 test = (struct crypto_larval *)q;
269
270 if (!strcmp(q->cra_driver_name, name))
271 goto found;
272 }
273
Karim Eshapac7235852017-05-13 21:05:19 +0200274 pr_err("alg: Unexpected test result for %s: %d\n", name, err);
Herbert Xu73d38642008-08-03 21:15:23 +0800275 goto unlock;
276
277found:
Herbert Xub8e15992009-01-28 14:09:59 +1100278 q->cra_flags |= CRYPTO_ALG_DEAD;
Herbert Xu73d38642008-08-03 21:15:23 +0800279 alg = test->adult;
280 if (err || list_empty(&alg->cra_list))
281 goto complete;
282
283 alg->cra_flags |= CRYPTO_ALG_TESTED;
284
285 list_for_each_entry(q, &crypto_alg_list, cra_list) {
286 if (q == alg)
287 continue;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000288
289 if (crypto_is_moribund(q))
290 continue;
291
292 if (crypto_is_larval(q)) {
Herbert Xu28259822006-08-06 21:23:26 +1000293 struct crypto_larval *larval = (void *)q;
294
Herbert Xud8058482007-10-02 22:27:29 +0800295 /*
296 * Check to see if either our generic name or
297 * specific name can satisfy the name requested
298 * by the larval entry q.
299 */
Herbert Xu6bfd4802006-09-21 11:39:29 +1000300 if (strcmp(alg->cra_name, q->cra_name) &&
301 strcmp(alg->cra_driver_name, q->cra_name))
302 continue;
303
304 if (larval->adult)
305 continue;
Herbert Xu492e2b62006-09-21 11:35:17 +1000306 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
307 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000308 if (!crypto_mod_get(alg))
309 continue;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000310
Herbert Xu28259822006-08-06 21:23:26 +1000311 larval->adult = alg;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000312 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000313 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000314
315 if (strcmp(alg->cra_name, q->cra_name))
316 continue;
317
318 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
319 q->cra_priority > alg->cra_priority)
320 continue;
321
Herbert Xu2bf29012009-08-31 15:56:54 +1000322 crypto_remove_spawns(q, &list, alg);
Herbert Xucce9e062006-08-21 21:08:13 +1000323 }
Herbert Xu28259822006-08-06 21:23:26 +1000324
Herbert Xu73d38642008-08-03 21:15:23 +0800325complete:
326 complete_all(&test->completion);
Herbert Xu28259822006-08-06 21:23:26 +1000327
Herbert Xu73d38642008-08-03 21:15:23 +0800328unlock:
329 up_write(&crypto_alg_sem);
330
331 crypto_remove_final(&list);
Herbert Xucce9e062006-08-21 21:08:13 +1000332}
Herbert Xu73d38642008-08-03 21:15:23 +0800333EXPORT_SYMBOL_GPL(crypto_alg_tested);
Herbert Xu4cc77202006-08-06 21:16:34 +1000334
Steffen Klassert22e5b202011-09-27 07:23:07 +0200335void crypto_remove_final(struct list_head *list)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000336{
337 struct crypto_alg *alg;
338 struct crypto_alg *n;
339
340 list_for_each_entry_safe(alg, n, list, cra_list) {
341 list_del_init(&alg->cra_list);
342 crypto_alg_put(alg);
343 }
344}
Steffen Klassert22e5b202011-09-27 07:23:07 +0200345EXPORT_SYMBOL_GPL(crypto_remove_final);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000346
Herbert Xu73d38642008-08-03 21:15:23 +0800347static void crypto_wait_for_test(struct crypto_larval *larval)
348{
349 int err;
350
351 err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
352 if (err != NOTIFY_STOP) {
353 if (WARN_ON(err != NOTIFY_DONE))
354 goto out;
355 crypto_alg_tested(larval->alg.cra_driver_name, 0);
356 }
357
Herbert Xu3fc89ad2015-10-19 18:23:57 +0800358 err = wait_for_completion_killable(&larval->completion);
Herbert Xu73d38642008-08-03 21:15:23 +0800359 WARN_ON(err);
Martin K. Petersendd8b0832018-08-30 11:00:14 -0400360 if (!err)
361 crypto_probing_notify(CRYPTO_MSG_ALG_LOADED, larval);
Herbert Xu73d38642008-08-03 21:15:23 +0800362
363out:
364 crypto_larval_kill(&larval->alg);
365}
366
Herbert Xu4cc77202006-08-06 21:16:34 +1000367int crypto_register_alg(struct crypto_alg *alg)
368{
Herbert Xu73d38642008-08-03 21:15:23 +0800369 struct crypto_larval *larval;
Herbert Xu4cc77202006-08-06 21:16:34 +1000370 int err;
371
Salvatore Benedettod6040762017-01-13 11:54:08 +0000372 alg->cra_flags &= ~CRYPTO_ALG_DEAD;
Herbert Xu4cc77202006-08-06 21:16:34 +1000373 err = crypto_check_alg(alg);
374 if (err)
375 return err;
376
377 down_write(&crypto_alg_sem);
Herbert Xu73d38642008-08-03 21:15:23 +0800378 larval = __crypto_register_alg(alg);
Herbert Xu4cc77202006-08-06 21:16:34 +1000379 up_write(&crypto_alg_sem);
380
Herbert Xu73d38642008-08-03 21:15:23 +0800381 if (IS_ERR(larval))
382 return PTR_ERR(larval);
383
384 crypto_wait_for_test(larval);
385 return 0;
Herbert Xu4cc77202006-08-06 21:16:34 +1000386}
Herbert Xucce9e062006-08-21 21:08:13 +1000387EXPORT_SYMBOL_GPL(crypto_register_alg);
388
Herbert Xu6bfd4802006-09-21 11:39:29 +1000389static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
390{
391 if (unlikely(list_empty(&alg->cra_list)))
392 return -ENOENT;
393
394 alg->cra_flags |= CRYPTO_ALG_DEAD;
395
Herbert Xu6bfd4802006-09-21 11:39:29 +1000396 list_del_init(&alg->cra_list);
Herbert Xu2bf29012009-08-31 15:56:54 +1000397 crypto_remove_spawns(alg, list, NULL);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000398
399 return 0;
400}
401
Herbert Xucce9e062006-08-21 21:08:13 +1000402int crypto_unregister_alg(struct crypto_alg *alg)
403{
Herbert Xu6bfd4802006-09-21 11:39:29 +1000404 int ret;
405 LIST_HEAD(list);
Richard Hartmann5357c6c2010-02-16 20:25:21 +0800406
Herbert Xucce9e062006-08-21 21:08:13 +1000407 down_write(&crypto_alg_sem);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000408 ret = crypto_remove_alg(alg, &list);
Herbert Xucce9e062006-08-21 21:08:13 +1000409 up_write(&crypto_alg_sem);
410
411 if (ret)
412 return ret;
413
Eric Biggersce8614a2017-12-29 10:00:46 -0600414 BUG_ON(refcount_read(&alg->cra_refcnt) != 1);
Herbert Xucce9e062006-08-21 21:08:13 +1000415 if (alg->cra_destroy)
416 alg->cra_destroy(alg);
417
Herbert Xu6bfd4802006-09-21 11:39:29 +1000418 crypto_remove_final(&list);
Herbert Xucce9e062006-08-21 21:08:13 +1000419 return 0;
420}
421EXPORT_SYMBOL_GPL(crypto_unregister_alg);
422
Mark Brown4b004342012-01-17 23:34:26 +0000423int crypto_register_algs(struct crypto_alg *algs, int count)
424{
425 int i, ret;
426
427 for (i = 0; i < count; i++) {
428 ret = crypto_register_alg(&algs[i]);
429 if (ret)
430 goto err;
431 }
432
433 return 0;
434
435err:
436 for (--i; i >= 0; --i)
437 crypto_unregister_alg(&algs[i]);
438
439 return ret;
440}
441EXPORT_SYMBOL_GPL(crypto_register_algs);
442
443int crypto_unregister_algs(struct crypto_alg *algs, int count)
444{
445 int i, ret;
446
447 for (i = 0; i < count; i++) {
448 ret = crypto_unregister_alg(&algs[i]);
449 if (ret)
450 pr_err("Failed to unregister %s %s: %d\n",
451 algs[i].cra_driver_name, algs[i].cra_name, ret);
452 }
453
454 return 0;
455}
456EXPORT_SYMBOL_GPL(crypto_unregister_algs);
457
Herbert Xu4cc77202006-08-06 21:16:34 +1000458int crypto_register_template(struct crypto_template *tmpl)
459{
460 struct crypto_template *q;
461 int err = -EEXIST;
462
463 down_write(&crypto_alg_sem);
464
Jarod Wilson002c77a2014-07-02 15:37:30 -0400465 crypto_check_module_sig(tmpl->module);
466
Herbert Xu4cc77202006-08-06 21:16:34 +1000467 list_for_each_entry(q, &crypto_template_list, list) {
468 if (q == tmpl)
469 goto out;
470 }
471
472 list_add(&tmpl->list, &crypto_template_list);
473 err = 0;
474out:
475 up_write(&crypto_alg_sem);
476 return err;
477}
478EXPORT_SYMBOL_GPL(crypto_register_template);
479
Xiongfeng Wang95724422019-01-18 13:58:11 +0800480int crypto_register_templates(struct crypto_template *tmpls, int count)
481{
482 int i, err;
483
484 for (i = 0; i < count; i++) {
485 err = crypto_register_template(&tmpls[i]);
486 if (err)
487 goto out;
488 }
489 return 0;
490
491out:
492 for (--i; i >= 0; --i)
493 crypto_unregister_template(&tmpls[i]);
494 return err;
495}
496EXPORT_SYMBOL_GPL(crypto_register_templates);
497
Herbert Xu4cc77202006-08-06 21:16:34 +1000498void crypto_unregister_template(struct crypto_template *tmpl)
499{
500 struct crypto_instance *inst;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800501 struct hlist_node *n;
Herbert Xu4cc77202006-08-06 21:16:34 +1000502 struct hlist_head *list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000503 LIST_HEAD(users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000504
505 down_write(&crypto_alg_sem);
506
507 BUG_ON(list_empty(&tmpl->list));
508 list_del_init(&tmpl->list);
509
510 list = &tmpl->instances;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800511 hlist_for_each_entry(inst, list, list) {
Herbert Xu6bfd4802006-09-21 11:39:29 +1000512 int err = crypto_remove_alg(&inst->alg, &users);
Joshua I. James0efcb8d2014-12-05 15:00:10 +0900513
Herbert Xu6bfd4802006-09-21 11:39:29 +1000514 BUG_ON(err);
Herbert Xu4cc77202006-08-06 21:16:34 +1000515 }
516
517 up_write(&crypto_alg_sem);
518
Sasha Levinb67bfe02013-02-27 17:06:00 -0800519 hlist_for_each_entry_safe(inst, n, list, list) {
Eric Biggersce8614a2017-12-29 10:00:46 -0600520 BUG_ON(refcount_read(&inst->alg.cra_refcnt) != 1);
Herbert Xu319382a2015-07-09 07:17:15 +0800521 crypto_free_instance(inst);
Herbert Xu4cc77202006-08-06 21:16:34 +1000522 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000523 crypto_remove_final(&users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000524}
525EXPORT_SYMBOL_GPL(crypto_unregister_template);
526
Xiongfeng Wang95724422019-01-18 13:58:11 +0800527void crypto_unregister_templates(struct crypto_template *tmpls, int count)
528{
529 int i;
530
531 for (i = count - 1; i >= 0; --i)
532 crypto_unregister_template(&tmpls[i]);
533}
534EXPORT_SYMBOL_GPL(crypto_unregister_templates);
535
Herbert Xu4cc77202006-08-06 21:16:34 +1000536static struct crypto_template *__crypto_lookup_template(const char *name)
537{
538 struct crypto_template *q, *tmpl = NULL;
539
540 down_read(&crypto_alg_sem);
541 list_for_each_entry(q, &crypto_template_list, list) {
542 if (strcmp(q->name, name))
543 continue;
544 if (unlikely(!crypto_tmpl_get(q)))
545 continue;
546
547 tmpl = q;
548 break;
549 }
550 up_read(&crypto_alg_sem);
551
552 return tmpl;
553}
554
555struct crypto_template *crypto_lookup_template(const char *name)
556{
Kees Cook4943ba12014-11-24 16:32:38 -0800557 return try_then_request_module(__crypto_lookup_template(name),
558 "crypto-%s", name);
Herbert Xu4cc77202006-08-06 21:16:34 +1000559}
560EXPORT_SYMBOL_GPL(crypto_lookup_template);
561
562int crypto_register_instance(struct crypto_template *tmpl,
563 struct crypto_instance *inst)
564{
Herbert Xu73d38642008-08-03 21:15:23 +0800565 struct crypto_larval *larval;
566 int err;
Herbert Xu4cc77202006-08-06 21:16:34 +1000567
Herbert Xu4cc77202006-08-06 21:16:34 +1000568 err = crypto_check_alg(&inst->alg);
569 if (err)
Stephan Mueller9c521a22015-04-09 12:09:55 +0200570 return err;
571
Herbert Xu4cc77202006-08-06 21:16:34 +1000572 inst->alg.cra_module = tmpl->module;
Steffen Klassert64a947b2011-09-27 07:21:26 +0200573 inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
Herbert Xu4cc77202006-08-06 21:16:34 +1000574
575 down_write(&crypto_alg_sem);
576
Herbert Xu73d38642008-08-03 21:15:23 +0800577 larval = __crypto_register_alg(&inst->alg);
578 if (IS_ERR(larval))
Herbert Xu4cc77202006-08-06 21:16:34 +1000579 goto unlock;
580
581 hlist_add_head(&inst->list, &tmpl->instances);
582 inst->tmpl = tmpl;
583
584unlock:
585 up_write(&crypto_alg_sem);
586
Herbert Xu73d38642008-08-03 21:15:23 +0800587 err = PTR_ERR(larval);
588 if (IS_ERR(larval))
589 goto err;
590
591 crypto_wait_for_test(larval);
592 err = 0;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000593
Herbert Xu4cc77202006-08-06 21:16:34 +1000594err:
595 return err;
596}
597EXPORT_SYMBOL_GPL(crypto_register_instance);
598
Herbert Xu87b16752015-04-02 22:39:40 +0800599int crypto_unregister_instance(struct crypto_instance *inst)
Steffen Klassertce3fd842011-11-08 10:09:17 +0100600{
Herbert Xu1f723712015-04-02 22:31:22 +0800601 LIST_HEAD(list);
Steffen Klassertce3fd842011-11-08 10:09:17 +0100602
Steffen Klassertce3fd842011-11-08 10:09:17 +0100603 down_write(&crypto_alg_sem);
604
Herbert Xu87b16752015-04-02 22:39:40 +0800605 crypto_remove_spawns(&inst->alg, &list, NULL);
Herbert Xu1f723712015-04-02 22:31:22 +0800606 crypto_remove_instance(inst, &list);
Steffen Klassertce3fd842011-11-08 10:09:17 +0100607
608 up_write(&crypto_alg_sem);
609
Herbert Xu1f723712015-04-02 22:31:22 +0800610 crypto_remove_final(&list);
Steffen Klassertce3fd842011-11-08 10:09:17 +0100611
612 return 0;
613}
614EXPORT_SYMBOL_GPL(crypto_unregister_instance);
615
Herbert Xu6bfd4802006-09-21 11:39:29 +1000616int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
Herbert Xua73e6992007-04-08 21:31:36 +1000617 struct crypto_instance *inst, u32 mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000618{
619 int err = -EAGAIN;
620
Eric Biggers6b476662019-01-06 12:46:06 -0800621 if (WARN_ON_ONCE(inst == NULL))
622 return -EINVAL;
623
Herbert Xu6bfd4802006-09-21 11:39:29 +1000624 spawn->inst = inst;
Herbert Xua73e6992007-04-08 21:31:36 +1000625 spawn->mask = mask;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000626
627 down_write(&crypto_alg_sem);
628 if (!crypto_is_moribund(alg)) {
629 list_add(&spawn->list, &alg->cra_users);
630 spawn->alg = alg;
631 err = 0;
632 }
633 up_write(&crypto_alg_sem);
634
635 return err;
636}
637EXPORT_SYMBOL_GPL(crypto_init_spawn);
638
Herbert Xu97eedce2009-07-08 15:55:52 +0800639int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
640 struct crypto_instance *inst,
641 const struct crypto_type *frontend)
642{
643 int err = -EINVAL;
644
Dan Carpenterc614e102010-05-03 11:08:15 +0800645 if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
Herbert Xu97eedce2009-07-08 15:55:52 +0800646 goto out;
647
648 spawn->frontend = frontend;
649 err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
650
651out:
652 return err;
653}
654EXPORT_SYMBOL_GPL(crypto_init_spawn2);
655
Herbert Xud6ef2f12015-05-11 17:47:39 +0800656int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name,
657 u32 type, u32 mask)
658{
659 struct crypto_alg *alg;
660 int err;
661
662 alg = crypto_find_alg(name, spawn->frontend, type, mask);
663 if (IS_ERR(alg))
664 return PTR_ERR(alg);
665
666 err = crypto_init_spawn(spawn, alg, spawn->inst, mask);
667 crypto_mod_put(alg);
668 return err;
669}
670EXPORT_SYMBOL_GPL(crypto_grab_spawn);
671
Herbert Xu6bfd4802006-09-21 11:39:29 +1000672void crypto_drop_spawn(struct crypto_spawn *spawn)
673{
674 down_write(&crypto_alg_sem);
Herbert Xu4f87ee12019-12-07 22:15:17 +0800675 if (!spawn->dead)
Herbert Xu7db3b612019-12-06 13:55:17 +0800676 list_del(&spawn->list);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000677 up_write(&crypto_alg_sem);
678}
679EXPORT_SYMBOL_GPL(crypto_drop_spawn);
680
Herbert Xu97eedce2009-07-08 15:55:52 +0800681static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000682{
683 struct crypto_alg *alg;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000684
685 down_read(&crypto_alg_sem);
686 alg = spawn->alg;
Herbert Xu4f87ee12019-12-07 22:15:17 +0800687 if (!spawn->dead && !crypto_mod_get(alg)) {
Herbert Xu73669cc2019-12-07 22:15:15 +0800688 alg->cra_flags |= CRYPTO_ALG_DYING;
689 alg = NULL;
690 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000691 up_read(&crypto_alg_sem);
692
Herbert Xu73669cc2019-12-07 22:15:15 +0800693 return alg ?: ERR_PTR(-EAGAIN);
Herbert Xu97eedce2009-07-08 15:55:52 +0800694}
695
696struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
697 u32 mask)
698{
699 struct crypto_alg *alg;
700 struct crypto_tfm *tfm;
701
702 alg = crypto_spawn_alg(spawn);
703 if (IS_ERR(alg))
704 return ERR_CAST(alg);
705
Herbert Xu2e306ee2006-12-17 10:05:58 +1100706 tfm = ERR_PTR(-EINVAL);
707 if (unlikely((alg->cra_flags ^ type) & mask))
708 goto out_put_alg;
709
Herbert Xu27d2a332007-01-24 20:50:26 +1100710 tfm = __crypto_alloc_tfm(alg, type, mask);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000711 if (IS_ERR(tfm))
Herbert Xu2e306ee2006-12-17 10:05:58 +1100712 goto out_put_alg;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000713
714 return tfm;
Herbert Xu2e306ee2006-12-17 10:05:58 +1100715
716out_put_alg:
717 crypto_mod_put(alg);
718 return tfm;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000719}
720EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
721
Herbert Xu97eedce2009-07-08 15:55:52 +0800722void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
723{
724 struct crypto_alg *alg;
725 struct crypto_tfm *tfm;
726
727 alg = crypto_spawn_alg(spawn);
728 if (IS_ERR(alg))
729 return ERR_CAST(alg);
730
731 tfm = crypto_create_tfm(alg, spawn->frontend);
732 if (IS_ERR(tfm))
733 goto out_put_alg;
734
735 return tfm;
736
737out_put_alg:
738 crypto_mod_put(alg);
739 return tfm;
740}
741EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
742
Herbert Xu28259822006-08-06 21:23:26 +1000743int crypto_register_notifier(struct notifier_block *nb)
744{
745 return blocking_notifier_chain_register(&crypto_chain, nb);
746}
747EXPORT_SYMBOL_GPL(crypto_register_notifier);
748
749int crypto_unregister_notifier(struct notifier_block *nb)
750{
751 return blocking_notifier_chain_unregister(&crypto_chain, nb);
752}
753EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
754
Herbert Xuebc610e2007-01-01 18:37:02 +1100755struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000756{
Herbert Xu39e1ee012007-08-29 19:27:26 +0800757 struct rtattr *rta = tb[0];
Herbert Xuebc610e2007-01-01 18:37:02 +1100758 struct crypto_attr_type *algt;
759
760 if (!rta)
761 return ERR_PTR(-ENOENT);
762 if (RTA_PAYLOAD(rta) < sizeof(*algt))
763 return ERR_PTR(-EINVAL);
Herbert Xu39e1ee012007-08-29 19:27:26 +0800764 if (rta->rta_type != CRYPTOA_TYPE)
765 return ERR_PTR(-EINVAL);
Herbert Xuebc610e2007-01-01 18:37:02 +1100766
767 algt = RTA_DATA(rta);
768
769 return algt;
770}
771EXPORT_SYMBOL_GPL(crypto_get_attr_type);
772
773int crypto_check_attr_type(struct rtattr **tb, u32 type)
774{
775 struct crypto_attr_type *algt;
776
777 algt = crypto_get_attr_type(tb);
778 if (IS_ERR(algt))
779 return PTR_ERR(algt);
780
781 if ((algt->type ^ type) & algt->mask)
782 return -EINVAL;
783
784 return 0;
785}
786EXPORT_SYMBOL_GPL(crypto_check_attr_type);
787
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800788const char *crypto_attr_alg_name(struct rtattr *rta)
Herbert Xuebc610e2007-01-01 18:37:02 +1100789{
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000790 struct crypto_attr_alg *alga;
791
Herbert Xuebc610e2007-01-01 18:37:02 +1100792 if (!rta)
793 return ERR_PTR(-ENOENT);
794 if (RTA_PAYLOAD(rta) < sizeof(*alga))
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000795 return ERR_PTR(-EINVAL);
Herbert Xu39e1ee012007-08-29 19:27:26 +0800796 if (rta->rta_type != CRYPTOA_ALG)
797 return ERR_PTR(-EINVAL);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000798
799 alga = RTA_DATA(rta);
800 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
801
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800802 return alga->name;
803}
804EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
805
Herbert Xud06854f2009-07-08 17:53:16 +0800806struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
807 const struct crypto_type *frontend,
808 u32 type, u32 mask)
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800809{
810 const char *name;
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800811
812 name = crypto_attr_alg_name(rta);
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800813 if (IS_ERR(name))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100814 return ERR_CAST(name);
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800815
Herbert Xud06854f2009-07-08 17:53:16 +0800816 return crypto_find_alg(name, frontend, type, mask);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000817}
Herbert Xud06854f2009-07-08 17:53:16 +0800818EXPORT_SYMBOL_GPL(crypto_attr_alg2);
Herbert Xu3c09f172007-08-30 16:24:15 +0800819
820int crypto_attr_u32(struct rtattr *rta, u32 *num)
821{
822 struct crypto_attr_u32 *nu32;
823
824 if (!rta)
825 return -ENOENT;
826 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
827 return -EINVAL;
828 if (rta->rta_type != CRYPTOA_U32)
829 return -EINVAL;
830
831 nu32 = RTA_DATA(rta);
832 *num = nu32->num;
833
834 return 0;
835}
836EXPORT_SYMBOL_GPL(crypto_attr_u32);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000837
Herbert Xu32f27c72016-06-29 18:04:13 +0800838int crypto_inst_setname(struct crypto_instance *inst, const char *name,
839 struct crypto_alg *alg)
840{
841 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
842 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
843 return -ENAMETOOLONG;
844
845 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
846 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
847 return -ENAMETOOLONG;
848
849 return 0;
850}
851EXPORT_SYMBOL_GPL(crypto_inst_setname);
852
Eric Biggers14aa1a82019-01-03 20:16:25 -0800853void *crypto_alloc_instance(const char *name, struct crypto_alg *alg,
854 unsigned int head)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000855{
856 struct crypto_instance *inst;
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800857 char *p;
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000858 int err;
859
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800860 p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
861 GFP_KERNEL);
862 if (!p)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000863 return ERR_PTR(-ENOMEM);
864
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800865 inst = (void *)(p + head);
866
Herbert Xu32f27c72016-06-29 18:04:13 +0800867 err = crypto_inst_setname(inst, name, alg);
868 if (err)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000869 goto err_free_inst;
870
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800871 return p;
872
873err_free_inst:
874 kfree(p);
875 return ERR_PTR(err);
876}
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000877EXPORT_SYMBOL_GPL(crypto_alloc_instance);
878
Herbert Xub5b7f082007-04-16 20:48:54 +1000879void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
880{
881 INIT_LIST_HEAD(&queue->list);
882 queue->backlog = &queue->list;
883 queue->qlen = 0;
884 queue->max_qlen = max_qlen;
885}
886EXPORT_SYMBOL_GPL(crypto_init_queue);
887
888int crypto_enqueue_request(struct crypto_queue *queue,
889 struct crypto_async_request *request)
890{
891 int err = -EINPROGRESS;
892
893 if (unlikely(queue->qlen >= queue->max_qlen)) {
Gilad Ben-Yossef6b80ea32017-10-18 08:00:33 +0100894 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
895 err = -ENOSPC;
Herbert Xub5b7f082007-04-16 20:48:54 +1000896 goto out;
Gilad Ben-Yossef6b80ea32017-10-18 08:00:33 +0100897 }
898 err = -EBUSY;
Herbert Xub5b7f082007-04-16 20:48:54 +1000899 if (queue->backlog == &queue->list)
900 queue->backlog = &request->list;
901 }
902
903 queue->qlen++;
904 list_add_tail(&request->list, &queue->list);
905
906out:
907 return err;
908}
909EXPORT_SYMBOL_GPL(crypto_enqueue_request);
910
Herbert Xu31d228c2015-07-08 11:55:30 +0800911struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
Herbert Xub5b7f082007-04-16 20:48:54 +1000912{
913 struct list_head *request;
914
915 if (unlikely(!queue->qlen))
916 return NULL;
917
918 queue->qlen--;
919
920 if (queue->backlog != &queue->list)
921 queue->backlog = queue->backlog->next;
922
923 request = queue->list.next;
924 list_del(request);
925
Herbert Xu31d228c2015-07-08 11:55:30 +0800926 return list_entry(request, struct crypto_async_request, list);
Herbert Xub5b7f082007-04-16 20:48:54 +1000927}
928EXPORT_SYMBOL_GPL(crypto_dequeue_request);
929
Herbert Xu76136362007-11-20 17:26:06 +0800930static inline void crypto_inc_byte(u8 *a, unsigned int size)
931{
932 u8 *b = (a + size);
933 u8 c;
934
935 for (; size; size--) {
936 c = *--b + 1;
937 *b = c;
938 if (c)
939 break;
940 }
941}
942
943void crypto_inc(u8 *a, unsigned int size)
944{
945 __be32 *b = (__be32 *)(a + size);
946 u32 c;
947
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000948 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
Ard Biesheuvel27c539a2017-02-14 21:51:02 +0000949 IS_ALIGNED((unsigned long)b, __alignof__(*b)))
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000950 for (; size >= 4; size -= 4) {
951 c = be32_to_cpu(*--b) + 1;
952 *b = cpu_to_be32(c);
Ard Biesheuvel27c539a2017-02-14 21:51:02 +0000953 if (likely(c))
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000954 return;
955 }
Herbert Xu76136362007-11-20 17:26:06 +0800956
957 crypto_inc_byte(a, size);
958}
959EXPORT_SYMBOL_GPL(crypto_inc);
960
Ard Biesheuvela7c391f2017-07-24 11:28:03 +0100961void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int len)
Herbert Xu76136362007-11-20 17:26:06 +0800962{
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000963 int relalign = 0;
964
965 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
966 int size = sizeof(unsigned long);
Ard Biesheuvela7c391f2017-07-24 11:28:03 +0100967 int d = (((unsigned long)dst ^ (unsigned long)src1) |
968 ((unsigned long)dst ^ (unsigned long)src2)) &
969 (size - 1);
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000970
971 relalign = d ? 1 << __ffs(d) : size;
972
973 /*
974 * If we care about alignment, process as many bytes as
975 * needed to advance dst and src to values whose alignments
976 * equal their relative alignment. This will allow us to
977 * process the remainder of the input using optimal strides.
978 */
979 while (((unsigned long)dst & (relalign - 1)) && len > 0) {
Ard Biesheuvela7c391f2017-07-24 11:28:03 +0100980 *dst++ = *src1++ ^ *src2++;
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000981 len--;
982 }
983 }
984
985 while (IS_ENABLED(CONFIG_64BIT) && len >= 8 && !(relalign & 7)) {
Ard Biesheuvela7c391f2017-07-24 11:28:03 +0100986 *(u64 *)dst = *(u64 *)src1 ^ *(u64 *)src2;
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000987 dst += 8;
Ard Biesheuvela7c391f2017-07-24 11:28:03 +0100988 src1 += 8;
989 src2 += 8;
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000990 len -= 8;
991 }
992
993 while (len >= 4 && !(relalign & 3)) {
Ard Biesheuvela7c391f2017-07-24 11:28:03 +0100994 *(u32 *)dst = *(u32 *)src1 ^ *(u32 *)src2;
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000995 dst += 4;
Ard Biesheuvela7c391f2017-07-24 11:28:03 +0100996 src1 += 4;
997 src2 += 4;
Ard Biesheuveldb91af02017-02-05 10:06:12 +0000998 len -= 4;
999 }
1000
1001 while (len >= 2 && !(relalign & 1)) {
Ard Biesheuvela7c391f2017-07-24 11:28:03 +01001002 *(u16 *)dst = *(u16 *)src1 ^ *(u16 *)src2;
Ard Biesheuveldb91af02017-02-05 10:06:12 +00001003 dst += 2;
Ard Biesheuvela7c391f2017-07-24 11:28:03 +01001004 src1 += 2;
1005 src2 += 2;
Ard Biesheuveldb91af02017-02-05 10:06:12 +00001006 len -= 2;
1007 }
1008
1009 while (len--)
Ard Biesheuvela7c391f2017-07-24 11:28:03 +01001010 *dst++ = *src1++ ^ *src2++;
Herbert Xu76136362007-11-20 17:26:06 +08001011}
Ard Biesheuveldb91af02017-02-05 10:06:12 +00001012EXPORT_SYMBOL_GPL(__crypto_xor);
Herbert Xu76136362007-11-20 17:26:06 +08001013
Herbert Xu38d21432015-04-20 13:39:00 +08001014unsigned int crypto_alg_extsize(struct crypto_alg *alg)
1015{
Herbert Xuc2110f22015-05-28 22:07:56 +08001016 return alg->cra_ctxsize +
1017 (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
Herbert Xu38d21432015-04-20 13:39:00 +08001018}
1019EXPORT_SYMBOL_GPL(crypto_alg_extsize);
1020
Herbert Xuf2aefda2016-01-23 13:51:01 +08001021int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
1022 u32 type, u32 mask)
1023{
1024 int ret = 0;
1025 struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask);
1026
1027 if (!IS_ERR(alg)) {
1028 crypto_mod_put(alg);
1029 ret = 1;
1030 }
1031
1032 return ret;
1033}
1034EXPORT_SYMBOL_GPL(crypto_type_has_alg);
1035
Corentin Labbef7d76e02018-11-29 14:42:21 +00001036#ifdef CONFIG_CRYPTO_STATS
Corentin Labbe1f6669b2018-11-29 14:42:26 +00001037void crypto_stats_init(struct crypto_alg *alg)
1038{
1039 memset(&alg->stats, 0, sizeof(alg->stats));
1040}
1041EXPORT_SYMBOL_GPL(crypto_stats_init);
1042
Corentin Labbef7d76e02018-11-29 14:42:21 +00001043void crypto_stats_get(struct crypto_alg *alg)
1044{
1045 crypto_alg_get(alg);
1046}
1047EXPORT_SYMBOL_GPL(crypto_stats_get);
1048
Corentin Labbef7d76e02018-11-29 14:42:21 +00001049void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg,
1050 int ret)
1051{
1052 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
Corentin Labbe44f13132018-11-29 14:42:25 +00001053 atomic64_inc(&alg->stats.aead.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001054 } else {
Corentin Labbe17c18f92018-11-29 14:42:24 +00001055 atomic64_inc(&alg->stats.aead.encrypt_cnt);
1056 atomic64_add(cryptlen, &alg->stats.aead.encrypt_tlen);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001057 }
1058 crypto_alg_put(alg);
1059}
1060EXPORT_SYMBOL_GPL(crypto_stats_aead_encrypt);
1061
1062void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg,
1063 int ret)
1064{
1065 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
Corentin Labbe44f13132018-11-29 14:42:25 +00001066 atomic64_inc(&alg->stats.aead.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001067 } else {
Corentin Labbe17c18f92018-11-29 14:42:24 +00001068 atomic64_inc(&alg->stats.aead.decrypt_cnt);
1069 atomic64_add(cryptlen, &alg->stats.aead.decrypt_tlen);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001070 }
1071 crypto_alg_put(alg);
1072}
1073EXPORT_SYMBOL_GPL(crypto_stats_aead_decrypt);
1074
1075void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret,
1076 struct crypto_alg *alg)
1077{
1078 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
Corentin Labbe44f13132018-11-29 14:42:25 +00001079 atomic64_inc(&alg->stats.akcipher.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001080 } else {
Corentin Labbe17c18f92018-11-29 14:42:24 +00001081 atomic64_inc(&alg->stats.akcipher.encrypt_cnt);
1082 atomic64_add(src_len, &alg->stats.akcipher.encrypt_tlen);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001083 }
1084 crypto_alg_put(alg);
1085}
1086EXPORT_SYMBOL_GPL(crypto_stats_akcipher_encrypt);
1087
1088void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret,
1089 struct crypto_alg *alg)
1090{
1091 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
Corentin Labbe44f13132018-11-29 14:42:25 +00001092 atomic64_inc(&alg->stats.akcipher.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001093 } else {
Corentin Labbe17c18f92018-11-29 14:42:24 +00001094 atomic64_inc(&alg->stats.akcipher.decrypt_cnt);
1095 atomic64_add(src_len, &alg->stats.akcipher.decrypt_tlen);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001096 }
1097 crypto_alg_put(alg);
1098}
1099EXPORT_SYMBOL_GPL(crypto_stats_akcipher_decrypt);
1100
1101void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
1102{
1103 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
Corentin Labbe44f13132018-11-29 14:42:25 +00001104 atomic64_inc(&alg->stats.akcipher.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001105 else
Corentin Labbe17c18f92018-11-29 14:42:24 +00001106 atomic64_inc(&alg->stats.akcipher.sign_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001107 crypto_alg_put(alg);
1108}
1109EXPORT_SYMBOL_GPL(crypto_stats_akcipher_sign);
1110
1111void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
1112{
1113 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
Corentin Labbe44f13132018-11-29 14:42:25 +00001114 atomic64_inc(&alg->stats.akcipher.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001115 else
Corentin Labbe17c18f92018-11-29 14:42:24 +00001116 atomic64_inc(&alg->stats.akcipher.verify_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001117 crypto_alg_put(alg);
1118}
1119EXPORT_SYMBOL_GPL(crypto_stats_akcipher_verify);
1120
1121void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
1122{
1123 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
Corentin Labbe44f13132018-11-29 14:42:25 +00001124 atomic64_inc(&alg->stats.compress.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001125 } else {
Corentin Labbe17c18f92018-11-29 14:42:24 +00001126 atomic64_inc(&alg->stats.compress.compress_cnt);
1127 atomic64_add(slen, &alg->stats.compress.compress_tlen);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001128 }
1129 crypto_alg_put(alg);
1130}
1131EXPORT_SYMBOL_GPL(crypto_stats_compress);
1132
1133void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
1134{
1135 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
Corentin Labbe44f13132018-11-29 14:42:25 +00001136 atomic64_inc(&alg->stats.compress.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001137 } else {
Corentin Labbe17c18f92018-11-29 14:42:24 +00001138 atomic64_inc(&alg->stats.compress.decompress_cnt);
1139 atomic64_add(slen, &alg->stats.compress.decompress_tlen);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001140 }
1141 crypto_alg_put(alg);
1142}
1143EXPORT_SYMBOL_GPL(crypto_stats_decompress);
1144
1145void crypto_stats_ahash_update(unsigned int nbytes, int ret,
1146 struct crypto_alg *alg)
1147{
1148 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
Corentin Labbe44f13132018-11-29 14:42:25 +00001149 atomic64_inc(&alg->stats.hash.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001150 else
Corentin Labbe17c18f92018-11-29 14:42:24 +00001151 atomic64_add(nbytes, &alg->stats.hash.hash_tlen);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001152 crypto_alg_put(alg);
1153}
1154EXPORT_SYMBOL_GPL(crypto_stats_ahash_update);
1155
1156void crypto_stats_ahash_final(unsigned int nbytes, int ret,
1157 struct crypto_alg *alg)
1158{
1159 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
Corentin Labbe44f13132018-11-29 14:42:25 +00001160 atomic64_inc(&alg->stats.hash.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001161 } else {
Corentin Labbe17c18f92018-11-29 14:42:24 +00001162 atomic64_inc(&alg->stats.hash.hash_cnt);
1163 atomic64_add(nbytes, &alg->stats.hash.hash_tlen);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001164 }
1165 crypto_alg_put(alg);
1166}
1167EXPORT_SYMBOL_GPL(crypto_stats_ahash_final);
1168
1169void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
1170{
1171 if (ret)
Corentin Labbe44f13132018-11-29 14:42:25 +00001172 atomic64_inc(&alg->stats.kpp.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001173 else
Corentin Labbe17c18f92018-11-29 14:42:24 +00001174 atomic64_inc(&alg->stats.kpp.setsecret_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001175 crypto_alg_put(alg);
1176}
1177EXPORT_SYMBOL_GPL(crypto_stats_kpp_set_secret);
1178
1179void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
1180{
1181 if (ret)
Corentin Labbe44f13132018-11-29 14:42:25 +00001182 atomic64_inc(&alg->stats.kpp.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001183 else
Corentin Labbe17c18f92018-11-29 14:42:24 +00001184 atomic64_inc(&alg->stats.kpp.generate_public_key_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001185 crypto_alg_put(alg);
1186}
1187EXPORT_SYMBOL_GPL(crypto_stats_kpp_generate_public_key);
1188
1189void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
1190{
1191 if (ret)
Corentin Labbe44f13132018-11-29 14:42:25 +00001192 atomic64_inc(&alg->stats.kpp.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001193 else
Corentin Labbe17c18f92018-11-29 14:42:24 +00001194 atomic64_inc(&alg->stats.kpp.compute_shared_secret_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001195 crypto_alg_put(alg);
1196}
1197EXPORT_SYMBOL_GPL(crypto_stats_kpp_compute_shared_secret);
1198
1199void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
1200{
1201 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
Corentin Labbe44f13132018-11-29 14:42:25 +00001202 atomic64_inc(&alg->stats.rng.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001203 else
Corentin Labbe17c18f92018-11-29 14:42:24 +00001204 atomic64_inc(&alg->stats.rng.seed_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001205 crypto_alg_put(alg);
1206}
1207EXPORT_SYMBOL_GPL(crypto_stats_rng_seed);
1208
1209void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen,
1210 int ret)
1211{
1212 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
Corentin Labbe44f13132018-11-29 14:42:25 +00001213 atomic64_inc(&alg->stats.rng.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001214 } else {
Corentin Labbe17c18f92018-11-29 14:42:24 +00001215 atomic64_inc(&alg->stats.rng.generate_cnt);
1216 atomic64_add(dlen, &alg->stats.rng.generate_tlen);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001217 }
1218 crypto_alg_put(alg);
1219}
1220EXPORT_SYMBOL_GPL(crypto_stats_rng_generate);
1221
1222void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret,
1223 struct crypto_alg *alg)
1224{
1225 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
Corentin Labbe44f13132018-11-29 14:42:25 +00001226 atomic64_inc(&alg->stats.cipher.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001227 } else {
Corentin Labbe17c18f92018-11-29 14:42:24 +00001228 atomic64_inc(&alg->stats.cipher.encrypt_cnt);
1229 atomic64_add(cryptlen, &alg->stats.cipher.encrypt_tlen);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001230 }
1231 crypto_alg_put(alg);
1232}
1233EXPORT_SYMBOL_GPL(crypto_stats_skcipher_encrypt);
1234
1235void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret,
1236 struct crypto_alg *alg)
1237{
1238 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
Corentin Labbe44f13132018-11-29 14:42:25 +00001239 atomic64_inc(&alg->stats.cipher.err_cnt);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001240 } else {
Corentin Labbe17c18f92018-11-29 14:42:24 +00001241 atomic64_inc(&alg->stats.cipher.decrypt_cnt);
1242 atomic64_add(cryptlen, &alg->stats.cipher.decrypt_tlen);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001243 }
1244 crypto_alg_put(alg);
1245}
1246EXPORT_SYMBOL_GPL(crypto_stats_skcipher_decrypt);
1247#endif
1248
Herbert Xucce9e062006-08-21 21:08:13 +10001249static int __init crypto_algapi_init(void)
1250{
1251 crypto_init_proc();
1252 return 0;
1253}
1254
1255static void __exit crypto_algapi_exit(void)
1256{
1257 crypto_exit_proc();
1258}
1259
1260module_init(crypto_algapi_init);
1261module_exit(crypto_algapi_exit);
1262
1263MODULE_LICENSE("GPL");
1264MODULE_DESCRIPTION("Cryptographic algorithms API");