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