Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Create default crypto algorithm instances. |
| 4 | * |
| 5 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 6 | */ |
| 7 | |
Herbert Xu | 6fe4a28 | 2009-02-18 21:41:29 +0800 | [diff] [blame] | 8 | #include <crypto/internal/aead.h> |
Herbert Xu | 3987103 | 2012-06-22 20:08:29 +0800 | [diff] [blame] | 9 | #include <linux/completion.h> |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 10 | #include <linux/ctype.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/init.h> |
Herbert Xu | cf02f5d | 2007-03-29 17:32:59 +1000 | [diff] [blame] | 13 | #include <linux/kthread.h> |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/notifier.h> |
| 16 | #include <linux/rtnetlink.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 17 | #include <linux/sched/signal.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 19 | #include <linux/string.h> |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 20 | |
| 21 | #include "internal.h" |
| 22 | |
| 23 | struct cryptomgr_param { |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 24 | struct rtattr *tb[CRYPTO_MAX_ATTRS + 2]; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 25 | |
| 26 | struct { |
| 27 | struct rtattr attr; |
| 28 | struct crypto_attr_type data; |
| 29 | } type; |
| 30 | |
Liu Shixin | 10ff997 | 2021-06-11 10:01:00 +0800 | [diff] [blame] | 31 | struct { |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 32 | struct rtattr attr; |
Liu Shixin | 10ff997 | 2021-06-11 10:01:00 +0800 | [diff] [blame] | 33 | struct crypto_attr_alg data; |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 34 | } attrs[CRYPTO_MAX_ATTRS]; |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 35 | |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 36 | char template[CRYPTO_MAX_ALG_NAME]; |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 37 | |
Herbert Xu | 939e177 | 2013-06-25 19:15:17 +0800 | [diff] [blame] | 38 | struct crypto_larval *larval; |
Herbert Xu | 3987103 | 2012-06-22 20:08:29 +0800 | [diff] [blame] | 39 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 40 | u32 otype; |
| 41 | u32 omask; |
| 42 | }; |
| 43 | |
| 44 | struct crypto_test_param { |
| 45 | char driver[CRYPTO_MAX_ALG_NAME]; |
| 46 | char alg[CRYPTO_MAX_ALG_NAME]; |
| 47 | u32 type; |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 48 | }; |
| 49 | |
Herbert Xu | cf02f5d | 2007-03-29 17:32:59 +1000 | [diff] [blame] | 50 | static int cryptomgr_probe(void *data) |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 51 | { |
Herbert Xu | cf02f5d | 2007-03-29 17:32:59 +1000 | [diff] [blame] | 52 | struct cryptomgr_param *param = data; |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 53 | struct crypto_template *tmpl; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 54 | int err; |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 55 | |
| 56 | tmpl = crypto_lookup_template(param->template); |
| 57 | if (!tmpl) |
Herbert Xu | 3987103 | 2012-06-22 20:08:29 +0800 | [diff] [blame] | 58 | goto out; |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 59 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 60 | do { |
Eric Biggers | a24a1fd7 | 2020-01-02 20:04:39 -0800 | [diff] [blame] | 61 | err = tmpl->create(tmpl, param->tb); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 62 | } while (err == -EAGAIN && !signal_pending(current)); |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 63 | |
| 64 | crypto_tmpl_put(tmpl); |
| 65 | |
| 66 | out: |
Herbert Xu | 939e177 | 2013-06-25 19:15:17 +0800 | [diff] [blame] | 67 | complete_all(¶m->larval->completion); |
| 68 | crypto_alg_put(¶m->larval->alg); |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 69 | kfree(param); |
Herbert Xu | cf02f5d | 2007-03-29 17:32:59 +1000 | [diff] [blame] | 70 | module_put_and_exit(0); |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static int cryptomgr_schedule_probe(struct crypto_larval *larval) |
| 74 | { |
Herbert Xu | 1605b84 | 2007-05-09 13:04:39 +1000 | [diff] [blame] | 75 | struct task_struct *thread; |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 76 | struct cryptomgr_param *param; |
| 77 | const char *name = larval->alg.cra_name; |
| 78 | const char *p; |
| 79 | unsigned int len; |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 80 | int i; |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 81 | |
Herbert Xu | cf02f5d | 2007-03-29 17:32:59 +1000 | [diff] [blame] | 82 | if (!try_module_get(THIS_MODULE)) |
| 83 | goto err; |
| 84 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 85 | param = kzalloc(sizeof(*param), GFP_KERNEL); |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 86 | if (!param) |
Herbert Xu | cf02f5d | 2007-03-29 17:32:59 +1000 | [diff] [blame] | 87 | goto err_put_module; |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 88 | |
| 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 Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 97 | |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 98 | i = 0; |
| 99 | for (;;) { |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 100 | name = ++p; |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 101 | |
| 102 | for (; isalnum(*p) || *p == '-' || *p == '_'; p++) |
Liu Shixin | 10ff997 | 2021-06-11 10:01:00 +0800 | [diff] [blame] | 103 | ; |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 104 | |
| 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 Xu | 720a650 | 2007-09-28 09:06:11 +0800 | [diff] [blame] | 117 | p++; |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 118 | } |
Herbert Xu | cf02f5d | 2007-03-29 17:32:59 +1000 | [diff] [blame] | 119 | |
| 120 | len = p - name; |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 121 | if (!len) |
| 122 | goto err_free_param; |
| 123 | |
Liu Shixin | 10ff997 | 2021-06-11 10:01:00 +0800 | [diff] [blame] | 124 | 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 Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 127 | |
| 128 | param->tb[i + 1] = ¶m->attrs[i].attr; |
| 129 | i++; |
| 130 | |
Herbert Xu | 720a650 | 2007-09-28 09:06:11 +0800 | [diff] [blame] | 131 | if (i >= CRYPTO_MAX_ATTRS) |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 132 | goto err_free_param; |
| 133 | |
| 134 | if (*p == ')') |
| 135 | break; |
| 136 | |
| 137 | if (*p != ',') |
| 138 | goto err_free_param; |
Herbert Xu | cf02f5d | 2007-03-29 17:32:59 +1000 | [diff] [blame] | 139 | } |
| 140 | |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 141 | if (!i) |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 142 | goto err_free_param; |
| 143 | |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 144 | param->tb[i + 1] = NULL; |
| 145 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 146 | param->type.attr.rta_len = sizeof(param->type); |
| 147 | param->type.attr.rta_type = CRYPTOA_TYPE; |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 148 | param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED; |
| 149 | param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED; |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 150 | param->tb[0] = ¶m->type.attr; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 151 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 152 | param->otype = larval->alg.cra_flags; |
| 153 | param->omask = larval->mask; |
| 154 | |
Herbert Xu | 939e177 | 2013-06-25 19:15:17 +0800 | [diff] [blame] | 155 | crypto_alg_get(&larval->alg); |
| 156 | param->larval = larval; |
Herbert Xu | 3987103 | 2012-06-22 20:08:29 +0800 | [diff] [blame] | 157 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 158 | thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe"); |
| 159 | if (IS_ERR(thread)) |
Herbert Xu | 939e177 | 2013-06-25 19:15:17 +0800 | [diff] [blame] | 160 | goto err_put_larval; |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 161 | |
| 162 | return NOTIFY_STOP; |
| 163 | |
Herbert Xu | 939e177 | 2013-06-25 19:15:17 +0800 | [diff] [blame] | 164 | err_put_larval: |
| 165 | crypto_alg_put(&larval->alg); |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 166 | err_free_param: |
| 167 | kfree(param); |
| 168 | err_put_module: |
| 169 | module_put(THIS_MODULE); |
| 170 | err: |
| 171 | return NOTIFY_OK; |
| 172 | } |
| 173 | |
| 174 | static int cryptomgr_test(void *data) |
| 175 | { |
| 176 | struct crypto_test_param *param = data; |
| 177 | u32 type = param->type; |
| 178 | int err = 0; |
| 179 | |
Herbert Xu | 326a634 | 2010-08-06 09:40:28 +0800 | [diff] [blame] | 180 | #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS |
| 181 | goto skiptest; |
| 182 | #endif |
| 183 | |
Herbert Xu | 6fe4a28 | 2009-02-18 21:41:29 +0800 | [diff] [blame] | 184 | if (type & CRYPTO_ALG_TESTED) |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 185 | goto skiptest; |
| 186 | |
Herbert Xu | 1aa4ecd | 2008-08-17 17:01:56 +1000 | [diff] [blame] | 187 | err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED); |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 188 | |
| 189 | skiptest: |
| 190 | crypto_alg_tested(param->driver, err); |
| 191 | |
| 192 | kfree(param); |
| 193 | module_put_and_exit(0); |
| 194 | } |
| 195 | |
| 196 | static int cryptomgr_schedule_test(struct crypto_alg *alg) |
| 197 | { |
| 198 | struct task_struct *thread; |
| 199 | struct crypto_test_param *param; |
Herbert Xu | 6fe4a28 | 2009-02-18 21:41:29 +0800 | [diff] [blame] | 200 | u32 type; |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 201 | |
| 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 Xu | 6fe4a28 | 2009-02-18 21:41:29 +0800 | [diff] [blame] | 211 | type = alg->cra_flags; |
| 212 | |
Herbert Xu | eed93e0 | 2016-11-22 20:08:31 +0800 | [diff] [blame] | 213 | /* Do not test internal algorithms. */ |
| 214 | if (type & CRYPTO_ALG_INTERNAL) |
Herbert Xu | 6fe4a28 | 2009-02-18 21:41:29 +0800 | [diff] [blame] | 215 | type |= CRYPTO_ALG_TESTED; |
| 216 | |
| 217 | param->type = type; |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 218 | |
| 219 | thread = kthread_run(cryptomgr_test, param, "cryptomgr_test"); |
Herbert Xu | 1605b84 | 2007-05-09 13:04:39 +1000 | [diff] [blame] | 220 | if (IS_ERR(thread)) |
Herbert Xu | cf02f5d | 2007-03-29 17:32:59 +1000 | [diff] [blame] | 221 | goto err_free_param; |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 222 | |
| 223 | return NOTIFY_STOP; |
| 224 | |
| 225 | err_free_param: |
| 226 | kfree(param); |
Herbert Xu | cf02f5d | 2007-03-29 17:32:59 +1000 | [diff] [blame] | 227 | err_put_module: |
| 228 | module_put(THIS_MODULE); |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 229 | err: |
| 230 | return NOTIFY_OK; |
| 231 | } |
| 232 | |
| 233 | static 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 Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 239 | case CRYPTO_MSG_ALG_REGISTER: |
| 240 | return cryptomgr_schedule_test(data); |
Martin K. Petersen | dd8b083 | 2018-08-30 11:00:14 -0400 | [diff] [blame] | 241 | case CRYPTO_MSG_ALG_LOADED: |
| 242 | break; |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return NOTIFY_DONE; |
| 246 | } |
| 247 | |
| 248 | static struct notifier_block cryptomgr_notifier = { |
| 249 | .notifier_call = cryptomgr_notify, |
| 250 | }; |
| 251 | |
| 252 | static int __init cryptomgr_init(void) |
| 253 | { |
Herbert Xu | f8b0d4d | 2009-05-06 14:15:47 +0800 | [diff] [blame] | 254 | return crypto_register_notifier(&cryptomgr_notifier); |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void __exit cryptomgr_exit(void) |
| 258 | { |
| 259 | int err = crypto_unregister_notifier(&cryptomgr_notifier); |
| 260 | BUG_ON(err); |
| 261 | } |
| 262 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 263 | /* |
| 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 | */ |
| 269 | arch_initcall(cryptomgr_init); |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 270 | module_exit(cryptomgr_exit); |
| 271 | |
| 272 | MODULE_LICENSE("GPL"); |
| 273 | MODULE_DESCRIPTION("Crypto Algorithm Manager"); |