blob: 5ebccbd6b74ed6167ba2cd45ebb2917c0a99836e [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 Biggersa24a1fd72020-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
181 return NOTIFY_STOP;
182
Herbert Xu939e1772013-06-25 19:15:17 +0800183err_put_larval:
184 crypto_alg_put(&larval->alg);
Herbert Xu73d38642008-08-03 21:15:23 +0800185err_free_param:
186 kfree(param);
187err_put_module:
188 module_put(THIS_MODULE);
189err:
190 return NOTIFY_OK;
191}
192
193static int cryptomgr_test(void *data)
194{
195 struct crypto_test_param *param = data;
196 u32 type = param->type;
197 int err = 0;
198
Herbert Xu326a6342010-08-06 09:40:28 +0800199#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
200 goto skiptest;
201#endif
202
Herbert Xu6fe4a282009-02-18 21:41:29 +0800203 if (type & CRYPTO_ALG_TESTED)
Herbert Xu73d38642008-08-03 21:15:23 +0800204 goto skiptest;
205
Herbert Xu1aa4ecd2008-08-17 17:01:56 +1000206 err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
Herbert Xu73d38642008-08-03 21:15:23 +0800207
208skiptest:
209 crypto_alg_tested(param->driver, err);
210
211 kfree(param);
212 module_put_and_exit(0);
213}
214
215static int cryptomgr_schedule_test(struct crypto_alg *alg)
216{
217 struct task_struct *thread;
218 struct crypto_test_param *param;
Herbert Xu6fe4a282009-02-18 21:41:29 +0800219 u32 type;
Herbert Xu73d38642008-08-03 21:15:23 +0800220
221 if (!try_module_get(THIS_MODULE))
222 goto err;
223
224 param = kzalloc(sizeof(*param), GFP_KERNEL);
225 if (!param)
226 goto err_put_module;
227
228 memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
229 memcpy(param->alg, alg->cra_name, sizeof(param->alg));
Herbert Xu6fe4a282009-02-18 21:41:29 +0800230 type = alg->cra_flags;
231
Herbert Xueed93e02016-11-22 20:08:31 +0800232 /* Do not test internal algorithms. */
233 if (type & CRYPTO_ALG_INTERNAL)
Herbert Xu6fe4a282009-02-18 21:41:29 +0800234 type |= CRYPTO_ALG_TESTED;
235
236 param->type = type;
Herbert Xu73d38642008-08-03 21:15:23 +0800237
238 thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
Herbert Xu1605b842007-05-09 13:04:39 +1000239 if (IS_ERR(thread))
Herbert Xucf02f5d2007-03-29 17:32:59 +1000240 goto err_free_param;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000241
242 return NOTIFY_STOP;
243
244err_free_param:
245 kfree(param);
Herbert Xucf02f5d2007-03-29 17:32:59 +1000246err_put_module:
247 module_put(THIS_MODULE);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000248err:
249 return NOTIFY_OK;
250}
251
252static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
253 void *data)
254{
255 switch (msg) {
256 case CRYPTO_MSG_ALG_REQUEST:
257 return cryptomgr_schedule_probe(data);
Herbert Xu73d38642008-08-03 21:15:23 +0800258 case CRYPTO_MSG_ALG_REGISTER:
259 return cryptomgr_schedule_test(data);
Martin K. Petersendd8b0832018-08-30 11:00:14 -0400260 case CRYPTO_MSG_ALG_LOADED:
261 break;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000262 }
263
264 return NOTIFY_DONE;
265}
266
267static struct notifier_block cryptomgr_notifier = {
268 .notifier_call = cryptomgr_notify,
269};
270
271static int __init cryptomgr_init(void)
272{
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800273 return crypto_register_notifier(&cryptomgr_notifier);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000274}
275
276static void __exit cryptomgr_exit(void)
277{
278 int err = crypto_unregister_notifier(&cryptomgr_notifier);
279 BUG_ON(err);
280}
281
Eric Biggersc4741b22019-04-11 21:57:42 -0700282/*
283 * This is arch_initcall() so that the crypto self-tests are run on algorithms
284 * registered early by subsys_initcall(). subsys_initcall() is needed for
285 * generic implementations so that they're available for comparison tests when
286 * other implementations are registered later by module_init().
287 */
288arch_initcall(cryptomgr_init);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000289module_exit(cryptomgr_exit);
290
291MODULE_LICENSE("GPL");
292MODULE_DESCRIPTION("Crypto Algorithm Manager");