blob: 1814d2c5188a31ef1db46f6ea5e1cbfc1f303f27 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xu2b8c19d2006-09-21 11:31:44 +10002/*
3 * Create default crypto algorithm instances.
4 *
5 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xu2b8c19d2006-09-21 11:31:44 +10006 */
7
Herbert Xu6fe4a282009-02-18 21:41:29 +08008#include <crypto/internal/aead.h>
Herbert Xu39871032012-06-22 20:08:29 +08009#include <linux/completion.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100010#include <linux/ctype.h>
11#include <linux/err.h>
12#include <linux/init.h>
Herbert Xucf02f5d2007-03-29 17:32:59 +100013#include <linux/kthread.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100014#include <linux/module.h>
15#include <linux/notifier.h>
16#include <linux/rtnetlink.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010017#include <linux/sched/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100019#include <linux/string.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100020
21#include "internal.h"
22
23struct cryptomgr_param {
Herbert Xu39e1ee012007-08-29 19:27:26 +080024 struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
Herbert Xuebc610e2007-01-01 18:37:02 +110025
26 struct {
27 struct rtattr attr;
28 struct crypto_attr_type data;
29 } type;
30
Liu Shixin10ff9972021-06-11 10:01:00 +080031 struct {
Herbert Xu2b8c19d2006-09-21 11:31:44 +100032 struct rtattr attr;
Liu Shixin10ff9972021-06-11 10:01:00 +080033 struct crypto_attr_alg data;
Herbert Xu39e1ee012007-08-29 19:27:26 +080034 } attrs[CRYPTO_MAX_ATTRS];
Herbert Xu2b8c19d2006-09-21 11:31:44 +100035
Herbert Xu2b8c19d2006-09-21 11:31:44 +100036 char template[CRYPTO_MAX_ALG_NAME];
Herbert Xu73d38642008-08-03 21:15:23 +080037
Herbert Xu939e1772013-06-25 19:15:17 +080038 struct crypto_larval *larval;
Herbert Xu39871032012-06-22 20:08:29 +080039
Herbert Xu73d38642008-08-03 21:15:23 +080040 u32 otype;
41 u32 omask;
42};
43
44struct crypto_test_param {
45 char driver[CRYPTO_MAX_ALG_NAME];
46 char alg[CRYPTO_MAX_ALG_NAME];
47 u32 type;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100048};
49
Herbert Xucf02f5d2007-03-29 17:32:59 +100050static int cryptomgr_probe(void *data)
Herbert Xu2b8c19d2006-09-21 11:31:44 +100051{
Herbert Xucf02f5d2007-03-29 17:32:59 +100052 struct cryptomgr_param *param = data;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100053 struct crypto_template *tmpl;
Herbert Xu6bfd4802006-09-21 11:39:29 +100054 int err;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100055
56 tmpl = crypto_lookup_template(param->template);
57 if (!tmpl)
Herbert Xu39871032012-06-22 20:08:29 +080058 goto out;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100059
Herbert Xu6bfd4802006-09-21 11:39:29 +100060 do {
Eric Biggersa24a1fd72020-01-02 20:04:39 -080061 err = tmpl->create(tmpl, param->tb);
Herbert Xu6bfd4802006-09-21 11:39:29 +100062 } while (err == -EAGAIN && !signal_pending(current));
Herbert Xu2b8c19d2006-09-21 11:31:44 +100063
64 crypto_tmpl_put(tmpl);
65
66out:
Herbert Xu939e1772013-06-25 19:15:17 +080067 complete_all(&param->larval->completion);
68 crypto_alg_put(&param->larval->alg);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100069 kfree(param);
Herbert Xucf02f5d2007-03-29 17:32:59 +100070 module_put_and_exit(0);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100071}
72
73static int cryptomgr_schedule_probe(struct crypto_larval *larval)
74{
Herbert Xu1605b842007-05-09 13:04:39 +100075 struct task_struct *thread;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100076 struct cryptomgr_param *param;
77 const char *name = larval->alg.cra_name;
78 const char *p;
79 unsigned int len;
Herbert Xu39e1ee012007-08-29 19:27:26 +080080 int i;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100081
Herbert Xucf02f5d2007-03-29 17:32:59 +100082 if (!try_module_get(THIS_MODULE))
83 goto err;
84
Herbert Xuebc610e2007-01-01 18:37:02 +110085 param = kzalloc(sizeof(*param), GFP_KERNEL);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100086 if (!param)
Herbert Xucf02f5d2007-03-29 17:32:59 +100087 goto err_put_module;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100088
89 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
90 ;
91
92 len = p - name;
93 if (!len || *p != '(')
94 goto err_free_param;
95
96 memcpy(param->template, name, len);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100097
Herbert Xu39e1ee012007-08-29 19:27:26 +080098 i = 0;
99 for (;;) {
Herbert Xu39e1ee012007-08-29 19:27:26 +0800100 name = ++p;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800101
102 for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
Liu Shixin10ff9972021-06-11 10:01:00 +0800103 ;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800104
105 if (*p == '(') {
106 int recursion = 0;
107
108 for (;;) {
109 if (!*++p)
110 goto err_free_param;
111 if (*p == '(')
112 recursion++;
113 else if (*p == ')' && !recursion--)
114 break;
115 }
116
Herbert Xu720a6502007-09-28 09:06:11 +0800117 p++;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800118 }
Herbert Xucf02f5d2007-03-29 17:32:59 +1000119
120 len = p - name;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800121 if (!len)
122 goto err_free_param;
123
Liu Shixin10ff9972021-06-11 10:01:00 +0800124 param->attrs[i].attr.rta_len = sizeof(param->attrs[i]);
125 param->attrs[i].attr.rta_type = CRYPTOA_ALG;
126 memcpy(param->attrs[i].data.name, name, len);
Herbert Xu39e1ee012007-08-29 19:27:26 +0800127
128 param->tb[i + 1] = &param->attrs[i].attr;
129 i++;
130
Herbert Xu720a6502007-09-28 09:06:11 +0800131 if (i >= CRYPTO_MAX_ATTRS)
Herbert Xu39e1ee012007-08-29 19:27:26 +0800132 goto err_free_param;
133
134 if (*p == ')')
135 break;
136
137 if (*p != ',')
138 goto err_free_param;
Herbert Xucf02f5d2007-03-29 17:32:59 +1000139 }
140
Herbert Xu39e1ee012007-08-29 19:27:26 +0800141 if (!i)
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000142 goto err_free_param;
143
Herbert Xu39e1ee012007-08-29 19:27:26 +0800144 param->tb[i + 1] = NULL;
145
Herbert Xuebc610e2007-01-01 18:37:02 +1100146 param->type.attr.rta_len = sizeof(param->type);
147 param->type.attr.rta_type = CRYPTOA_TYPE;
Herbert Xu73d38642008-08-03 21:15:23 +0800148 param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
149 param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800150 param->tb[0] = &param->type.attr;
Herbert Xuebc610e2007-01-01 18:37:02 +1100151
Herbert Xu73d38642008-08-03 21:15:23 +0800152 param->otype = larval->alg.cra_flags;
153 param->omask = larval->mask;
154
Herbert Xu939e1772013-06-25 19:15:17 +0800155 crypto_alg_get(&larval->alg);
156 param->larval = larval;
Herbert Xu39871032012-06-22 20:08:29 +0800157
Herbert Xu73d38642008-08-03 21:15:23 +0800158 thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
159 if (IS_ERR(thread))
Herbert Xu939e1772013-06-25 19:15:17 +0800160 goto err_put_larval;
Herbert Xu73d38642008-08-03 21:15:23 +0800161
162 return NOTIFY_STOP;
163
Herbert Xu939e1772013-06-25 19:15:17 +0800164err_put_larval:
165 crypto_alg_put(&larval->alg);
Herbert Xu73d38642008-08-03 21:15:23 +0800166err_free_param:
167 kfree(param);
168err_put_module:
169 module_put(THIS_MODULE);
170err:
171 return NOTIFY_OK;
172}
173
174static int cryptomgr_test(void *data)
175{
176 struct crypto_test_param *param = data;
177 u32 type = param->type;
178 int err = 0;
179
Herbert Xu326a6342010-08-06 09:40:28 +0800180#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
181 goto skiptest;
182#endif
183
Herbert Xu6fe4a282009-02-18 21:41:29 +0800184 if (type & CRYPTO_ALG_TESTED)
Herbert Xu73d38642008-08-03 21:15:23 +0800185 goto skiptest;
186
Herbert Xu1aa4ecd2008-08-17 17:01:56 +1000187 err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
Herbert Xu73d38642008-08-03 21:15:23 +0800188
189skiptest:
190 crypto_alg_tested(param->driver, err);
191
192 kfree(param);
193 module_put_and_exit(0);
194}
195
196static int cryptomgr_schedule_test(struct crypto_alg *alg)
197{
198 struct task_struct *thread;
199 struct crypto_test_param *param;
Herbert Xu6fe4a282009-02-18 21:41:29 +0800200 u32 type;
Herbert Xu73d38642008-08-03 21:15:23 +0800201
202 if (!try_module_get(THIS_MODULE))
203 goto err;
204
205 param = kzalloc(sizeof(*param), GFP_KERNEL);
206 if (!param)
207 goto err_put_module;
208
209 memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
210 memcpy(param->alg, alg->cra_name, sizeof(param->alg));
Herbert Xu6fe4a282009-02-18 21:41:29 +0800211 type = alg->cra_flags;
212
Herbert Xueed93e02016-11-22 20:08:31 +0800213 /* Do not test internal algorithms. */
214 if (type & CRYPTO_ALG_INTERNAL)
Herbert Xu6fe4a282009-02-18 21:41:29 +0800215 type |= CRYPTO_ALG_TESTED;
216
217 param->type = type;
Herbert Xu73d38642008-08-03 21:15:23 +0800218
219 thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
Herbert Xu1605b842007-05-09 13:04:39 +1000220 if (IS_ERR(thread))
Herbert Xucf02f5d2007-03-29 17:32:59 +1000221 goto err_free_param;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000222
223 return NOTIFY_STOP;
224
225err_free_param:
226 kfree(param);
Herbert Xucf02f5d2007-03-29 17:32:59 +1000227err_put_module:
228 module_put(THIS_MODULE);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000229err:
230 return NOTIFY_OK;
231}
232
233static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
234 void *data)
235{
236 switch (msg) {
237 case CRYPTO_MSG_ALG_REQUEST:
238 return cryptomgr_schedule_probe(data);
Herbert Xu73d38642008-08-03 21:15:23 +0800239 case CRYPTO_MSG_ALG_REGISTER:
240 return cryptomgr_schedule_test(data);
Martin K. Petersendd8b0832018-08-30 11:00:14 -0400241 case CRYPTO_MSG_ALG_LOADED:
242 break;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000243 }
244
245 return NOTIFY_DONE;
246}
247
248static struct notifier_block cryptomgr_notifier = {
249 .notifier_call = cryptomgr_notify,
250};
251
252static int __init cryptomgr_init(void)
253{
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800254 return crypto_register_notifier(&cryptomgr_notifier);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000255}
256
257static void __exit cryptomgr_exit(void)
258{
259 int err = crypto_unregister_notifier(&cryptomgr_notifier);
260 BUG_ON(err);
261}
262
Eric Biggersc4741b22019-04-11 21:57:42 -0700263/*
264 * This is arch_initcall() so that the crypto self-tests are run on algorithms
265 * registered early by subsys_initcall(). subsys_initcall() is needed for
266 * generic implementations so that they're available for comparison tests when
267 * other implementations are registered later by module_init().
268 */
269arch_initcall(cryptomgr_init);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000270module_exit(cryptomgr_exit);
271
272MODULE_LICENSE("GPL");
273MODULE_DESCRIPTION("Crypto Algorithm Manager");