blob: 535f1f87e6c1d5760fa987ef6f273dc5cd0c3729 [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
Herbert Xu39e1ee012007-08-29 19:27:26 +080031 union {
Herbert Xu2b8c19d2006-09-21 11:31:44 +100032 struct rtattr attr;
Herbert Xu39e1ee012007-08-29 19:27:26 +080033 struct {
34 struct rtattr attr;
35 struct crypto_attr_alg data;
36 } alg;
37 struct {
38 struct rtattr attr;
39 struct crypto_attr_u32 data;
40 } nu32;
41 } attrs[CRYPTO_MAX_ATTRS];
Herbert Xu2b8c19d2006-09-21 11:31:44 +100042
Herbert Xu2b8c19d2006-09-21 11:31:44 +100043 char template[CRYPTO_MAX_ALG_NAME];
Herbert Xu73d38642008-08-03 21:15:23 +080044
Herbert Xu939e1772013-06-25 19:15:17 +080045 struct crypto_larval *larval;
Herbert Xu39871032012-06-22 20:08:29 +080046
Herbert Xu73d38642008-08-03 21:15:23 +080047 u32 otype;
48 u32 omask;
49};
50
51struct crypto_test_param {
52 char driver[CRYPTO_MAX_ALG_NAME];
53 char alg[CRYPTO_MAX_ALG_NAME];
54 u32 type;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100055};
56
Herbert Xucf02f5d2007-03-29 17:32:59 +100057static int cryptomgr_probe(void *data)
Herbert Xu2b8c19d2006-09-21 11:31:44 +100058{
Herbert Xucf02f5d2007-03-29 17:32:59 +100059 struct cryptomgr_param *param = data;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100060 struct crypto_template *tmpl;
Herbert Xu6bfd4802006-09-21 11:39:29 +100061 int err;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100062
63 tmpl = crypto_lookup_template(param->template);
64 if (!tmpl)
Herbert Xu39871032012-06-22 20:08:29 +080065 goto out;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100066
Herbert Xu6bfd4802006-09-21 11:39:29 +100067 do {
Eric Biggersa24a1fd2020-01-02 20:04:39 -080068 err = tmpl->create(tmpl, param->tb);
Herbert Xu6bfd4802006-09-21 11:39:29 +100069 } while (err == -EAGAIN && !signal_pending(current));
Herbert Xu2b8c19d2006-09-21 11:31:44 +100070
71 crypto_tmpl_put(tmpl);
72
73out:
Herbert Xu939e1772013-06-25 19:15:17 +080074 complete_all(&param->larval->completion);
75 crypto_alg_put(&param->larval->alg);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100076 kfree(param);
Herbert Xucf02f5d2007-03-29 17:32:59 +100077 module_put_and_exit(0);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100078}
79
80static int cryptomgr_schedule_probe(struct crypto_larval *larval)
81{
Herbert Xu1605b842007-05-09 13:04:39 +100082 struct task_struct *thread;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100083 struct cryptomgr_param *param;
84 const char *name = larval->alg.cra_name;
85 const char *p;
86 unsigned int len;
Herbert Xu39e1ee012007-08-29 19:27:26 +080087 int i;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100088
Herbert Xucf02f5d2007-03-29 17:32:59 +100089 if (!try_module_get(THIS_MODULE))
90 goto err;
91
Herbert Xuebc610e2007-01-01 18:37:02 +110092 param = kzalloc(sizeof(*param), GFP_KERNEL);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100093 if (!param)
Herbert Xucf02f5d2007-03-29 17:32:59 +100094 goto err_put_module;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100095
96 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
97 ;
98
99 len = p - name;
100 if (!len || *p != '(')
101 goto err_free_param;
102
103 memcpy(param->template, name, len);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000104
Herbert Xu39e1ee012007-08-29 19:27:26 +0800105 i = 0;
106 for (;;) {
107 int notnum = 0;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000108
Herbert Xu39e1ee012007-08-29 19:27:26 +0800109 name = ++p;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800110
111 for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
112 notnum |= !isdigit(*p);
113
114 if (*p == '(') {
115 int recursion = 0;
116
117 for (;;) {
118 if (!*++p)
119 goto err_free_param;
120 if (*p == '(')
121 recursion++;
122 else if (*p == ')' && !recursion--)
123 break;
124 }
125
126 notnum = 1;
Herbert Xu720a6502007-09-28 09:06:11 +0800127 p++;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800128 }
Herbert Xucf02f5d2007-03-29 17:32:59 +1000129
130 len = p - name;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800131 if (!len)
132 goto err_free_param;
133
134 if (notnum) {
135 param->attrs[i].alg.attr.rta_len =
136 sizeof(param->attrs[i].alg);
137 param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
138 memcpy(param->attrs[i].alg.data.name, name, len);
139 } else {
140 param->attrs[i].nu32.attr.rta_len =
141 sizeof(param->attrs[i].nu32);
142 param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
143 param->attrs[i].nu32.data.num =
144 simple_strtol(name, NULL, 0);
145 }
146
147 param->tb[i + 1] = &param->attrs[i].attr;
148 i++;
149
Herbert Xu720a6502007-09-28 09:06:11 +0800150 if (i >= CRYPTO_MAX_ATTRS)
Herbert Xu39e1ee012007-08-29 19:27:26 +0800151 goto err_free_param;
152
153 if (*p == ')')
154 break;
155
156 if (*p != ',')
157 goto err_free_param;
Herbert Xucf02f5d2007-03-29 17:32:59 +1000158 }
159
Herbert Xu39e1ee012007-08-29 19:27:26 +0800160 if (!i)
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000161 goto err_free_param;
162
Herbert Xu39e1ee012007-08-29 19:27:26 +0800163 param->tb[i + 1] = NULL;
164
Herbert Xuebc610e2007-01-01 18:37:02 +1100165 param->type.attr.rta_len = sizeof(param->type);
166 param->type.attr.rta_type = CRYPTOA_TYPE;
Herbert Xu73d38642008-08-03 21:15:23 +0800167 param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
168 param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800169 param->tb[0] = &param->type.attr;
Herbert Xuebc610e2007-01-01 18:37:02 +1100170
Herbert Xu73d38642008-08-03 21:15:23 +0800171 param->otype = larval->alg.cra_flags;
172 param->omask = larval->mask;
173
Herbert Xu939e1772013-06-25 19:15:17 +0800174 crypto_alg_get(&larval->alg);
175 param->larval = larval;
Herbert Xu39871032012-06-22 20:08:29 +0800176
Herbert Xu73d38642008-08-03 21:15:23 +0800177 thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
178 if (IS_ERR(thread))
Herbert Xu939e1772013-06-25 19:15:17 +0800179 goto err_put_larval;
Herbert Xu73d38642008-08-03 21:15:23 +0800180
Herbert Xu39871032012-06-22 20:08:29 +0800181 wait_for_completion_interruptible(&larval->completion);
182
Herbert Xu73d38642008-08-03 21:15:23 +0800183 return NOTIFY_STOP;
184
Herbert Xu939e1772013-06-25 19:15:17 +0800185err_put_larval:
186 crypto_alg_put(&larval->alg);
Herbert Xu73d38642008-08-03 21:15:23 +0800187err_free_param:
188 kfree(param);
189err_put_module:
190 module_put(THIS_MODULE);
191err:
192 return NOTIFY_OK;
193}
194
195static int cryptomgr_test(void *data)
196{
197 struct crypto_test_param *param = data;
198 u32 type = param->type;
199 int err = 0;
200
Herbert Xu326a6342010-08-06 09:40:28 +0800201#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
202 goto skiptest;
203#endif
204
Herbert Xu6fe4a282009-02-18 21:41:29 +0800205 if (type & CRYPTO_ALG_TESTED)
Herbert Xu73d38642008-08-03 21:15:23 +0800206 goto skiptest;
207
Herbert Xu1aa4ecd2008-08-17 17:01:56 +1000208 err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
Herbert Xu73d38642008-08-03 21:15:23 +0800209
210skiptest:
211 crypto_alg_tested(param->driver, err);
212
213 kfree(param);
214 module_put_and_exit(0);
215}
216
217static int cryptomgr_schedule_test(struct crypto_alg *alg)
218{
219 struct task_struct *thread;
220 struct crypto_test_param *param;
Herbert Xu6fe4a282009-02-18 21:41:29 +0800221 u32 type;
Herbert Xu73d38642008-08-03 21:15:23 +0800222
223 if (!try_module_get(THIS_MODULE))
224 goto err;
225
226 param = kzalloc(sizeof(*param), GFP_KERNEL);
227 if (!param)
228 goto err_put_module;
229
230 memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
231 memcpy(param->alg, alg->cra_name, sizeof(param->alg));
Herbert Xu6fe4a282009-02-18 21:41:29 +0800232 type = alg->cra_flags;
233
Herbert Xueed93e02016-11-22 20:08:31 +0800234 /* Do not test internal algorithms. */
235 if (type & CRYPTO_ALG_INTERNAL)
Herbert Xu6fe4a282009-02-18 21:41:29 +0800236 type |= CRYPTO_ALG_TESTED;
237
238 param->type = type;
Herbert Xu73d38642008-08-03 21:15:23 +0800239
240 thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
Herbert Xu1605b842007-05-09 13:04:39 +1000241 if (IS_ERR(thread))
Herbert Xucf02f5d2007-03-29 17:32:59 +1000242 goto err_free_param;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000243
244 return NOTIFY_STOP;
245
246err_free_param:
247 kfree(param);
Herbert Xucf02f5d2007-03-29 17:32:59 +1000248err_put_module:
249 module_put(THIS_MODULE);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000250err:
251 return NOTIFY_OK;
252}
253
254static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
255 void *data)
256{
257 switch (msg) {
258 case CRYPTO_MSG_ALG_REQUEST:
259 return cryptomgr_schedule_probe(data);
Herbert Xu73d38642008-08-03 21:15:23 +0800260 case CRYPTO_MSG_ALG_REGISTER:
261 return cryptomgr_schedule_test(data);
Martin K. Petersendd8b0832018-08-30 11:00:14 -0400262 case CRYPTO_MSG_ALG_LOADED:
263 break;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000264 }
265
266 return NOTIFY_DONE;
267}
268
269static struct notifier_block cryptomgr_notifier = {
270 .notifier_call = cryptomgr_notify,
271};
272
273static int __init cryptomgr_init(void)
274{
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800275 return crypto_register_notifier(&cryptomgr_notifier);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000276}
277
278static void __exit cryptomgr_exit(void)
279{
280 int err = crypto_unregister_notifier(&cryptomgr_notifier);
281 BUG_ON(err);
282}
283
Eric Biggersc4741b22019-04-11 21:57:42 -0700284/*
285 * This is arch_initcall() so that the crypto self-tests are run on algorithms
286 * registered early by subsys_initcall(). subsys_initcall() is needed for
287 * generic implementations so that they're available for comparison tests when
288 * other implementations are registered later by module_init().
289 */
290arch_initcall(cryptomgr_init);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000291module_exit(cryptomgr_exit);
292
293MODULE_LICENSE("GPL");
294MODULE_DESCRIPTION("Crypto Algorithm Manager");