Blame view
crypto/cryptomgr.c
4.37 KB
2b8c19dbd [CRYPTO] api: Add... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/* * Create default crypto algorithm instances. * * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * */ #include <linux/crypto.h> #include <linux/ctype.h> #include <linux/err.h> #include <linux/init.h> |
cf02f5da9 [CRYPTO] cryptomg... |
17 |
#include <linux/kthread.h> |
2b8c19dbd [CRYPTO] api: Add... |
18 19 20 |
#include <linux/module.h> #include <linux/notifier.h> #include <linux/rtnetlink.h> |
6bfd48096 [CRYPTO] api: Add... |
21 |
#include <linux/sched.h> |
2b8c19dbd [CRYPTO] api: Add... |
22 |
#include <linux/string.h> |
2b8c19dbd [CRYPTO] api: Add... |
23 24 25 26 |
#include "internal.h" struct cryptomgr_param { |
39e1ee011 [CRYPTO] api: Add... |
27 |
struct rtattr *tb[CRYPTO_MAX_ATTRS + 2]; |
ebc610e5b [CRYPTO] template... |
28 29 30 31 32 |
struct { struct rtattr attr; struct crypto_attr_type data; } type; |
39e1ee011 [CRYPTO] api: Add... |
33 |
union { |
2b8c19dbd [CRYPTO] api: Add... |
34 |
struct rtattr attr; |
39e1ee011 [CRYPTO] api: Add... |
35 36 37 38 39 40 41 42 43 44 45 |
struct { struct rtattr attr; struct crypto_attr_alg data; } alg; struct { struct rtattr attr; struct crypto_attr_u32 data; } nu32; } attrs[CRYPTO_MAX_ATTRS]; char larval[CRYPTO_MAX_ALG_NAME]; |
2b8c19dbd [CRYPTO] api: Add... |
46 47 |
char template[CRYPTO_MAX_ALG_NAME]; }; |
cf02f5da9 [CRYPTO] cryptomg... |
48 |
static int cryptomgr_probe(void *data) |
2b8c19dbd [CRYPTO] api: Add... |
49 |
{ |
cf02f5da9 [CRYPTO] cryptomg... |
50 |
struct cryptomgr_param *param = data; |
2b8c19dbd [CRYPTO] api: Add... |
51 52 |
struct crypto_template *tmpl; struct crypto_instance *inst; |
6bfd48096 [CRYPTO] api: Add... |
53 |
int err; |
2b8c19dbd [CRYPTO] api: Add... |
54 55 56 57 |
tmpl = crypto_lookup_template(param->template); if (!tmpl) goto err; |
6bfd48096 [CRYPTO] api: Add... |
58 |
do { |
ebc610e5b [CRYPTO] template... |
59 |
inst = tmpl->alloc(param->tb); |
6bfd48096 [CRYPTO] api: Add... |
60 61 62 63 64 |
if (IS_ERR(inst)) err = PTR_ERR(inst); else if ((err = crypto_register_instance(tmpl, inst))) tmpl->free(inst); } while (err == -EAGAIN && !signal_pending(current)); |
2b8c19dbd [CRYPTO] api: Add... |
65 66 |
crypto_tmpl_put(tmpl); |
6bfd48096 [CRYPTO] api: Add... |
67 68 |
if (err) goto err; |
2b8c19dbd [CRYPTO] api: Add... |
69 70 |
out: kfree(param); |
cf02f5da9 [CRYPTO] cryptomg... |
71 |
module_put_and_exit(0); |
2b8c19dbd [CRYPTO] api: Add... |
72 73 |
err: |
39e1ee011 [CRYPTO] api: Add... |
74 |
crypto_larval_error(param->larval, param->type.data.type, |
ebc610e5b [CRYPTO] template... |
75 |
param->type.data.mask); |
2b8c19dbd [CRYPTO] api: Add... |
76 77 78 79 80 |
goto out; } static int cryptomgr_schedule_probe(struct crypto_larval *larval) { |
1605b8471 [CRYPTO] cryptomg... |
81 |
struct task_struct *thread; |
2b8c19dbd [CRYPTO] api: Add... |
82 83 84 85 |
struct cryptomgr_param *param; const char *name = larval->alg.cra_name; const char *p; unsigned int len; |
39e1ee011 [CRYPTO] api: Add... |
86 |
int i; |
2b8c19dbd [CRYPTO] api: Add... |
87 |
|
cf02f5da9 [CRYPTO] cryptomg... |
88 89 |
if (!try_module_get(THIS_MODULE)) goto err; |
ebc610e5b [CRYPTO] template... |
90 |
param = kzalloc(sizeof(*param), GFP_KERNEL); |
2b8c19dbd [CRYPTO] api: Add... |
91 |
if (!param) |
cf02f5da9 [CRYPTO] cryptomg... |
92 |
goto err_put_module; |
2b8c19dbd [CRYPTO] api: Add... |
93 94 95 96 97 98 99 100 101 |
for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++) ; len = p - name; if (!len || *p != '(') goto err_free_param; memcpy(param->template, name, len); |
2b8c19dbd [CRYPTO] api: Add... |
102 |
|
39e1ee011 [CRYPTO] api: Add... |
103 104 105 |
i = 0; for (;;) { int notnum = 0; |
2b8c19dbd [CRYPTO] api: Add... |
106 |
|
39e1ee011 [CRYPTO] api: Add... |
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
name = ++p; len = 0; for (; isalnum(*p) || *p == '-' || *p == '_'; p++) notnum |= !isdigit(*p); if (*p == '(') { int recursion = 0; for (;;) { if (!*++p) goto err_free_param; if (*p == '(') recursion++; else if (*p == ')' && !recursion--) break; } notnum = 1; |
720a650f8 [CRYPTO] cryptomg... |
126 |
p++; |
39e1ee011 [CRYPTO] api: Add... |
127 |
} |
cf02f5da9 [CRYPTO] cryptomg... |
128 129 |
len = p - name; |
39e1ee011 [CRYPTO] api: Add... |
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
if (!len) goto err_free_param; if (notnum) { param->attrs[i].alg.attr.rta_len = sizeof(param->attrs[i].alg); param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG; memcpy(param->attrs[i].alg.data.name, name, len); } else { param->attrs[i].nu32.attr.rta_len = sizeof(param->attrs[i].nu32); param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32; param->attrs[i].nu32.data.num = simple_strtol(name, NULL, 0); } param->tb[i + 1] = ¶m->attrs[i].attr; i++; |
720a650f8 [CRYPTO] cryptomg... |
148 |
if (i >= CRYPTO_MAX_ATTRS) |
39e1ee011 [CRYPTO] api: Add... |
149 150 151 152 153 154 155 |
goto err_free_param; if (*p == ')') break; if (*p != ',') goto err_free_param; |
cf02f5da9 [CRYPTO] cryptomg... |
156 |
} |
39e1ee011 [CRYPTO] api: Add... |
157 |
if (!i) |
2b8c19dbd [CRYPTO] api: Add... |
158 |
goto err_free_param; |
39e1ee011 [CRYPTO] api: Add... |
159 |
param->tb[i + 1] = NULL; |
ebc610e5b [CRYPTO] template... |
160 161 162 163 |
param->type.attr.rta_len = sizeof(param->type); param->type.attr.rta_type = CRYPTOA_TYPE; param->type.data.type = larval->alg.cra_flags; param->type.data.mask = larval->mask; |
39e1ee011 [CRYPTO] api: Add... |
164 |
param->tb[0] = ¶m->type.attr; |
2b8c19dbd [CRYPTO] api: Add... |
165 |
|
39e1ee011 [CRYPTO] api: Add... |
166 |
memcpy(param->larval, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME); |
2b8c19dbd [CRYPTO] api: Add... |
167 |
|
1605b8471 [CRYPTO] cryptomg... |
168 169 |
thread = kthread_run(cryptomgr_probe, param, "cryptomgr"); if (IS_ERR(thread)) |
cf02f5da9 [CRYPTO] cryptomg... |
170 |
goto err_free_param; |
2b8c19dbd [CRYPTO] api: Add... |
171 172 173 174 175 |
return NOTIFY_STOP; err_free_param: kfree(param); |
cf02f5da9 [CRYPTO] cryptomg... |
176 177 |
err_put_module: module_put(THIS_MODULE); |
2b8c19dbd [CRYPTO] api: Add... |
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
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); } return NOTIFY_DONE; } static struct notifier_block cryptomgr_notifier = { .notifier_call = cryptomgr_notify, }; static int __init cryptomgr_init(void) { return crypto_register_notifier(&cryptomgr_notifier); } static void __exit cryptomgr_exit(void) { int err = crypto_unregister_notifier(&cryptomgr_notifier); BUG_ON(err); } module_init(cryptomgr_init); module_exit(cryptomgr_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Crypto Algorithm Manager"); |