2 * Create default crypto algorithm instances.
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
13 #include <linux/crypto.h>
14 #include <linux/ctype.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/kthread.h>
18 #include <linux/module.h>
19 #include <linux/notifier.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
26 struct cryptomgr_param
{
27 struct rtattr
*tb
[CRYPTOA_MAX
];
31 struct crypto_attr_type data
;
36 struct crypto_attr_alg data
;
40 char name
[CRYPTO_MAX_ALG_NAME
];
43 char template[CRYPTO_MAX_ALG_NAME
];
46 static int cryptomgr_probe(void *data
)
48 struct cryptomgr_param
*param
= data
;
49 struct crypto_template
*tmpl
;
50 struct crypto_instance
*inst
;
53 tmpl
= crypto_lookup_template(param
->template);
58 inst
= tmpl
->alloc(param
->tb
);
61 else if ((err
= crypto_register_instance(tmpl
, inst
)))
63 } while (err
== -EAGAIN
&& !signal_pending(current
));
65 crypto_tmpl_put(tmpl
);
72 module_put_and_exit(0);
75 crypto_larval_error(param
->larval
.name
, param
->type
.data
.type
,
76 param
->type
.data
.mask
);
80 static int cryptomgr_schedule_probe(struct crypto_larval
*larval
)
82 struct task_struct
*thread
;
83 struct cryptomgr_param
*param
;
84 const char *name
= larval
->alg
.cra_name
;
88 if (!try_module_get(THIS_MODULE
))
91 param
= kzalloc(sizeof(*param
), GFP_KERNEL
);
95 for (p
= name
; isalnum(*p
) || *p
== '-' || *p
== '_'; p
++)
99 if (!len
|| *p
!= '(')
102 memcpy(param
->template, name
, len
);
106 for (p
= name
; *p
; p
++) {
107 for (; isalnum(*p
) || *p
== '-' || *p
== '_' || *p
== '('; p
++)
116 if (!len
|| name
[len
+ 1])
119 param
->type
.attr
.rta_len
= sizeof(param
->type
);
120 param
->type
.attr
.rta_type
= CRYPTOA_TYPE
;
121 param
->type
.data
.type
= larval
->alg
.cra_flags
;
122 param
->type
.data
.mask
= larval
->mask
;
123 param
->tb
[CRYPTOA_TYPE
- 1] = ¶m
->type
.attr
;
125 param
->alg
.attr
.rta_len
= sizeof(param
->alg
);
126 param
->alg
.attr
.rta_type
= CRYPTOA_ALG
;
127 memcpy(param
->alg
.data
.name
, name
, len
);
128 param
->tb
[CRYPTOA_ALG
- 1] = ¶m
->alg
.attr
;
130 memcpy(param
->larval
.name
, larval
->alg
.cra_name
, CRYPTO_MAX_ALG_NAME
);
132 thread
= kthread_run(cryptomgr_probe
, param
, "cryptomgr");
141 module_put(THIS_MODULE
);
146 static int cryptomgr_notify(struct notifier_block
*this, unsigned long msg
,
150 case CRYPTO_MSG_ALG_REQUEST
:
151 return cryptomgr_schedule_probe(data
);
157 static struct notifier_block cryptomgr_notifier
= {
158 .notifier_call
= cryptomgr_notify
,
161 static int __init
cryptomgr_init(void)
163 return crypto_register_notifier(&cryptomgr_notifier
);
166 static void __exit
cryptomgr_exit(void)
168 int err
= crypto_unregister_notifier(&cryptomgr_notifier
);
172 module_init(cryptomgr_init
);
173 module_exit(cryptomgr_exit
);
175 MODULE_LICENSE("GPL");
176 MODULE_DESCRIPTION("Crypto Algorithm Manager");