Blame view
crypto/algboss.c
5.44 KB
2874c5fd2
|
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
2b8c19dbd
|
2 3 4 5 |
/* * Create default crypto algorithm instances. * * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
2b8c19dbd
|
6 |
*/ |
6fe4a28d8
|
7 |
#include <crypto/internal/aead.h> |
398710379
|
8 |
#include <linux/completion.h> |
2b8c19dbd
|
9 10 11 |
#include <linux/ctype.h> #include <linux/err.h> #include <linux/init.h> |
cf02f5da9
|
12 |
#include <linux/kthread.h> |
2b8c19dbd
|
13 14 15 |
#include <linux/module.h> #include <linux/notifier.h> #include <linux/rtnetlink.h> |
174cd4b1e
|
16 |
#include <linux/sched/signal.h> |
5a0e3ad6a
|
17 |
#include <linux/slab.h> |
2b8c19dbd
|
18 |
#include <linux/string.h> |
2b8c19dbd
|
19 20 21 22 |
#include "internal.h" struct cryptomgr_param { |
39e1ee011
|
23 |
struct rtattr *tb[CRYPTO_MAX_ATTRS + 2]; |
ebc610e5b
|
24 25 26 27 28 |
struct { struct rtattr attr; struct crypto_attr_type data; } type; |
10ff9976d
|
29 |
struct { |
2b8c19dbd
|
30 |
struct rtattr attr; |
10ff9976d
|
31 |
struct crypto_attr_alg data; |
39e1ee011
|
32 |
} attrs[CRYPTO_MAX_ATTRS]; |
2b8c19dbd
|
33 |
char template[CRYPTO_MAX_ALG_NAME]; |
73d3864a4
|
34 |
|
939e17799
|
35 |
struct crypto_larval *larval; |
398710379
|
36 |
|
73d3864a4
|
37 38 39 40 41 42 43 44 |
u32 otype; u32 omask; }; struct crypto_test_param { char driver[CRYPTO_MAX_ALG_NAME]; char alg[CRYPTO_MAX_ALG_NAME]; u32 type; |
2b8c19dbd
|
45 |
}; |
cf02f5da9
|
46 |
static int cryptomgr_probe(void *data) |
2b8c19dbd
|
47 |
{ |
cf02f5da9
|
48 |
struct cryptomgr_param *param = data; |
2b8c19dbd
|
49 |
struct crypto_template *tmpl; |
6bfd48096
|
50 |
int err; |
2b8c19dbd
|
51 52 53 |
tmpl = crypto_lookup_template(param->template); if (!tmpl) |
398710379
|
54 |
goto out; |
2b8c19dbd
|
55 |
|
6bfd48096
|
56 |
do { |
a24a1fd73
|
57 |
err = tmpl->create(tmpl, param->tb); |
6bfd48096
|
58 |
} while (err == -EAGAIN && !signal_pending(current)); |
2b8c19dbd
|
59 60 61 62 |
crypto_tmpl_put(tmpl); out: |
939e17799
|
63 64 |
complete_all(¶m->larval->completion); crypto_alg_put(¶m->larval->alg); |
2b8c19dbd
|
65 |
kfree(param); |
cf02f5da9
|
66 |
module_put_and_exit(0); |
2b8c19dbd
|
67 68 69 70 |
} static int cryptomgr_schedule_probe(struct crypto_larval *larval) { |
1605b8471
|
71 |
struct task_struct *thread; |
2b8c19dbd
|
72 73 74 75 |
struct cryptomgr_param *param; const char *name = larval->alg.cra_name; const char *p; unsigned int len; |
39e1ee011
|
76 |
int i; |
2b8c19dbd
|
77 |
|
cf02f5da9
|
78 79 |
if (!try_module_get(THIS_MODULE)) goto err; |
ebc610e5b
|
80 |
param = kzalloc(sizeof(*param), GFP_KERNEL); |
2b8c19dbd
|
81 |
if (!param) |
cf02f5da9
|
82 |
goto err_put_module; |
2b8c19dbd
|
83 84 85 86 87 88 89 90 91 |
for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++) ; len = p - name; if (!len || *p != '(') goto err_free_param; memcpy(param->template, name, len); |
2b8c19dbd
|
92 |
|
39e1ee011
|
93 94 |
i = 0; for (;;) { |
39e1ee011
|
95 |
name = ++p; |
39e1ee011
|
96 97 |
for (; isalnum(*p) || *p == '-' || *p == '_'; p++) |
10ff9976d
|
98 |
; |
39e1ee011
|
99 100 101 102 103 104 105 106 107 108 109 110 |
if (*p == '(') { int recursion = 0; for (;;) { if (!*++p) goto err_free_param; if (*p == '(') recursion++; else if (*p == ')' && !recursion--) break; } |
720a650f8
|
111 |
p++; |
39e1ee011
|
112 |
} |
cf02f5da9
|
113 114 |
len = p - name; |
39e1ee011
|
115 116 |
if (!len) goto err_free_param; |
10ff9976d
|
117 118 119 |
param->attrs[i].attr.rta_len = sizeof(param->attrs[i]); param->attrs[i].attr.rta_type = CRYPTOA_ALG; memcpy(param->attrs[i].data.name, name, len); |
39e1ee011
|
120 121 122 |
param->tb[i + 1] = ¶m->attrs[i].attr; i++; |
720a650f8
|
123 |
if (i >= CRYPTO_MAX_ATTRS) |
39e1ee011
|
124 125 126 127 128 129 130 |
goto err_free_param; if (*p == ')') break; if (*p != ',') goto err_free_param; |
cf02f5da9
|
131 |
} |
39e1ee011
|
132 |
if (!i) |
2b8c19dbd
|
133 |
goto err_free_param; |
39e1ee011
|
134 |
param->tb[i + 1] = NULL; |
ebc610e5b
|
135 136 |
param->type.attr.rta_len = sizeof(param->type); param->type.attr.rta_type = CRYPTOA_TYPE; |
73d3864a4
|
137 138 |
param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED; param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED; |
39e1ee011
|
139 |
param->tb[0] = ¶m->type.attr; |
2b8c19dbd
|
140 |
|
73d3864a4
|
141 142 |
param->otype = larval->alg.cra_flags; param->omask = larval->mask; |
939e17799
|
143 144 |
crypto_alg_get(&larval->alg); param->larval = larval; |
398710379
|
145 |
|
73d3864a4
|
146 147 |
thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe"); if (IS_ERR(thread)) |
939e17799
|
148 |
goto err_put_larval; |
73d3864a4
|
149 150 |
return NOTIFY_STOP; |
939e17799
|
151 152 |
err_put_larval: crypto_alg_put(&larval->alg); |
73d3864a4
|
153 154 155 156 157 158 159 160 161 162 163 164 165 |
err_free_param: kfree(param); err_put_module: module_put(THIS_MODULE); err: return NOTIFY_OK; } static int cryptomgr_test(void *data) { struct crypto_test_param *param = data; u32 type = param->type; int err = 0; |
326a6346f
|
166 167 168 |
#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS goto skiptest; #endif |
6fe4a28d8
|
169 |
if (type & CRYPTO_ALG_TESTED) |
73d3864a4
|
170 |
goto skiptest; |
1aa4ecd95
|
171 |
err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED); |
73d3864a4
|
172 173 174 175 176 177 178 179 180 181 182 183 |
skiptest: crypto_alg_tested(param->driver, err); kfree(param); module_put_and_exit(0); } static int cryptomgr_schedule_test(struct crypto_alg *alg) { struct task_struct *thread; struct crypto_test_param *param; |
6fe4a28d8
|
184 |
u32 type; |
73d3864a4
|
185 186 187 188 189 190 191 192 193 194 |
if (!try_module_get(THIS_MODULE)) goto err; param = kzalloc(sizeof(*param), GFP_KERNEL); if (!param) goto err_put_module; memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver)); memcpy(param->alg, alg->cra_name, sizeof(param->alg)); |
6fe4a28d8
|
195 |
type = alg->cra_flags; |
eed93e0ce
|
196 197 |
/* Do not test internal algorithms. */ if (type & CRYPTO_ALG_INTERNAL) |
6fe4a28d8
|
198 199 200 |
type |= CRYPTO_ALG_TESTED; param->type = type; |
73d3864a4
|
201 202 |
thread = kthread_run(cryptomgr_test, param, "cryptomgr_test"); |
1605b8471
|
203 |
if (IS_ERR(thread)) |
cf02f5da9
|
204 |
goto err_free_param; |
2b8c19dbd
|
205 206 207 208 209 |
return NOTIFY_STOP; err_free_param: kfree(param); |
cf02f5da9
|
210 211 |
err_put_module: module_put(THIS_MODULE); |
2b8c19dbd
|
212 213 214 215 216 217 218 219 220 221 |
err: return NOTIFY_OK; } static int cryptomgr_notify(struct notifier_block *this, unsigned long msg, void *data) { switch (msg) { case CRYPTO_MSG_ALG_REQUEST: return cryptomgr_schedule_probe(data); |
73d3864a4
|
222 223 |
case CRYPTO_MSG_ALG_REGISTER: return cryptomgr_schedule_test(data); |
dd8b083f9
|
224 225 |
case CRYPTO_MSG_ALG_LOADED: break; |
2b8c19dbd
|
226 227 228 229 230 231 232 233 234 235 236 |
} return NOTIFY_DONE; } static struct notifier_block cryptomgr_notifier = { .notifier_call = cryptomgr_notify, }; static int __init cryptomgr_init(void) { |
f8b0d4d09
|
237 |
return crypto_register_notifier(&cryptomgr_notifier); |
2b8c19dbd
|
238 239 240 241 242 243 244 |
} static void __exit cryptomgr_exit(void) { int err = crypto_unregister_notifier(&cryptomgr_notifier); BUG_ON(err); } |
c4741b230
|
245 246 247 248 249 250 251 |
/* * This is arch_initcall() so that the crypto self-tests are run on algorithms * registered early by subsys_initcall(). subsys_initcall() is needed for * generic implementations so that they're available for comparison tests when * other implementations are registered later by module_init(). */ arch_initcall(cryptomgr_init); |
2b8c19dbd
|
252 253 254 255 |
module_exit(cryptomgr_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Crypto Algorithm Manager"); |