2 * Crypto user configuration API.
4 * Copyright (C) 2011 secunet Security Networks AG
5 * Copyright (C) 2011 Steffen Klassert <steffen.klassert@secunet.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <linux/module.h>
22 #include <linux/crypto.h>
23 #include <linux/cryptouser.h>
24 #include <linux/sched.h>
25 #include <net/netlink.h>
26 #include <linux/security.h>
27 #include <net/net_namespace.h>
28 #include <crypto/internal/skcipher.h>
29 #include <crypto/internal/rng.h>
30 #include <crypto/akcipher.h>
31 #include <crypto/kpp.h>
35 #define null_terminated(x) (strnlen(x, sizeof(x)) < sizeof(x))
37 static DEFINE_MUTEX(crypto_cfg_mutex);
39 /* The crypto netlink socket */
40 static struct sock *crypto_nlsk;
42 struct crypto_dump_info {
43 struct sk_buff *in_skb;
44 struct sk_buff *out_skb;
49 static struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
51 struct crypto_alg *q, *alg = NULL;
53 down_read(&crypto_alg_sem);
55 list_for_each_entry(q, &crypto_alg_list, cra_list) {
58 if (crypto_is_larval(q))
61 if ((q->cra_flags ^ p->cru_type) & p->cru_mask)
64 if (strlen(p->cru_driver_name))
65 match = !strcmp(q->cra_driver_name,
68 match = !strcmp(q->cra_name, p->cru_name);
73 if (unlikely(!crypto_mod_get(q)))
80 up_read(&crypto_alg_sem);
85 static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
87 struct crypto_report_cipher rcipher;
89 strncpy(rcipher.type, "cipher", sizeof(rcipher.type));
91 rcipher.blocksize = alg->cra_blocksize;
92 rcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
93 rcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
95 if (nla_put(skb, CRYPTOCFGA_REPORT_CIPHER,
96 sizeof(struct crypto_report_cipher), &rcipher))
104 static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
106 struct crypto_report_comp rcomp;
108 strncpy(rcomp.type, "compression", sizeof(rcomp.type));
109 if (nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
110 sizeof(struct crypto_report_comp), &rcomp))
111 goto nla_put_failure;
118 static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)
120 struct crypto_report_akcipher rakcipher;
122 strncpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
124 if (nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
125 sizeof(struct crypto_report_akcipher), &rakcipher))
126 goto nla_put_failure;
133 static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)
135 struct crypto_report_kpp rkpp;
137 strncpy(rkpp.type, "kpp", sizeof(rkpp.type));
139 if (nla_put(skb, CRYPTOCFGA_REPORT_KPP,
140 sizeof(struct crypto_report_kpp), &rkpp))
141 goto nla_put_failure;
148 static int crypto_report_one(struct crypto_alg *alg,
149 struct crypto_user_alg *ualg, struct sk_buff *skb)
151 strncpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
152 strncpy(ualg->cru_driver_name, alg->cra_driver_name,
153 sizeof(ualg->cru_driver_name));
154 strncpy(ualg->cru_module_name, module_name(alg->cra_module),
155 sizeof(ualg->cru_module_name));
159 ualg->cru_flags = alg->cra_flags;
160 ualg->cru_refcnt = atomic_read(&alg->cra_refcnt);
162 if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
163 goto nla_put_failure;
164 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
165 struct crypto_report_larval rl;
167 strncpy(rl.type, "larval", sizeof(rl.type));
168 if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL,
169 sizeof(struct crypto_report_larval), &rl))
170 goto nla_put_failure;
174 if (alg->cra_type && alg->cra_type->report) {
175 if (alg->cra_type->report(skb, alg))
176 goto nla_put_failure;
181 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
182 case CRYPTO_ALG_TYPE_CIPHER:
183 if (crypto_report_cipher(skb, alg))
184 goto nla_put_failure;
187 case CRYPTO_ALG_TYPE_COMPRESS:
188 if (crypto_report_comp(skb, alg))
189 goto nla_put_failure;
193 case CRYPTO_ALG_TYPE_AKCIPHER:
194 if (crypto_report_akcipher(skb, alg))
195 goto nla_put_failure;
198 case CRYPTO_ALG_TYPE_KPP:
199 if (crypto_report_kpp(skb, alg))
200 goto nla_put_failure;
211 static int crypto_report_alg(struct crypto_alg *alg,
212 struct crypto_dump_info *info)
214 struct sk_buff *in_skb = info->in_skb;
215 struct sk_buff *skb = info->out_skb;
216 struct nlmsghdr *nlh;
217 struct crypto_user_alg *ualg;
220 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq,
221 CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
227 ualg = nlmsg_data(nlh);
229 err = crypto_report_one(alg, ualg, skb);
231 nlmsg_cancel(skb, nlh);
241 static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
242 struct nlattr **attrs)
244 struct crypto_user_alg *p = nlmsg_data(in_nlh);
245 struct crypto_alg *alg;
247 struct crypto_dump_info info;
250 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
253 alg = crypto_alg_match(p, 0);
258 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
262 info.in_skb = in_skb;
264 info.nlmsg_seq = in_nlh->nlmsg_seq;
265 info.nlmsg_flags = 0;
267 err = crypto_report_alg(alg, &info);
277 return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
280 static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
282 const size_t start_pos = cb->args[0];
284 struct crypto_dump_info info;
285 struct crypto_alg *alg;
288 info.in_skb = cb->skb;
290 info.nlmsg_seq = cb->nlh->nlmsg_seq;
291 info.nlmsg_flags = NLM_F_MULTI;
293 down_read(&crypto_alg_sem);
294 list_for_each_entry(alg, &crypto_alg_list, cra_list) {
295 if (pos >= start_pos) {
296 res = crypto_report_alg(alg, &info);
297 if (res == -EMSGSIZE)
307 up_read(&crypto_alg_sem);
311 static int crypto_dump_report_done(struct netlink_callback *cb)
316 static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
317 struct nlattr **attrs)
319 struct crypto_alg *alg;
320 struct crypto_user_alg *p = nlmsg_data(nlh);
321 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
324 if (!netlink_capable(skb, CAP_NET_ADMIN))
327 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
330 if (priority && !strlen(p->cru_driver_name))
333 alg = crypto_alg_match(p, 1);
337 down_write(&crypto_alg_sem);
339 crypto_remove_spawns(alg, &list, NULL);
342 alg->cra_priority = nla_get_u32(priority);
344 up_write(&crypto_alg_sem);
347 crypto_remove_final(&list);
352 static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
353 struct nlattr **attrs)
355 struct crypto_alg *alg;
356 struct crypto_user_alg *p = nlmsg_data(nlh);
359 if (!netlink_capable(skb, CAP_NET_ADMIN))
362 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
365 alg = crypto_alg_match(p, 1);
369 /* We can not unregister core algorithms such as aes-generic.
370 * We would loose the reference in the crypto_alg_list to this algorithm
371 * if we try to unregister. Unregistering such an algorithm without
372 * removing the module is not possible, so we restrict to crypto
373 * instances that are build from templates. */
375 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
379 if (atomic_read(&alg->cra_refcnt) > 2)
382 err = crypto_unregister_instance((struct crypto_instance *)alg);
389 static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
390 struct nlattr **attrs)
394 struct crypto_alg *alg;
395 struct crypto_user_alg *p = nlmsg_data(nlh);
396 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
398 if (!netlink_capable(skb, CAP_NET_ADMIN))
401 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
404 if (strlen(p->cru_driver_name))
407 if (priority && !exact)
410 alg = crypto_alg_match(p, exact);
416 if (strlen(p->cru_driver_name))
417 name = p->cru_driver_name;
421 alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
425 down_write(&crypto_alg_sem);
428 alg->cra_priority = nla_get_u32(priority);
430 up_write(&crypto_alg_sem);
437 static int crypto_del_rng(struct sk_buff *skb, struct nlmsghdr *nlh,
438 struct nlattr **attrs)
440 if (!netlink_capable(skb, CAP_NET_ADMIN))
442 return crypto_del_default_rng();
445 #define MSGSIZE(type) sizeof(struct type)
447 static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
448 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
449 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
450 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
451 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
452 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = 0,
455 static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
456 [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
461 static const struct crypto_link {
462 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
463 int (*dump)(struct sk_buff *, struct netlink_callback *);
464 int (*done)(struct netlink_callback *);
465 } crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
466 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
467 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
468 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
469 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
470 .dump = crypto_dump_report,
471 .done = crypto_dump_report_done},
472 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = { .doit = crypto_del_rng },
475 static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
477 struct nlattr *attrs[CRYPTOCFGA_MAX+1];
478 const struct crypto_link *link;
481 type = nlh->nlmsg_type;
482 if (type > CRYPTO_MSG_MAX)
485 type -= CRYPTO_MSG_BASE;
486 link = &crypto_dispatch[type];
488 if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
489 (nlh->nlmsg_flags & NLM_F_DUMP))) {
490 struct crypto_alg *alg;
491 unsigned long dump_alloc = 0;
493 if (link->dump == NULL)
496 down_read(&crypto_alg_sem);
497 list_for_each_entry(alg, &crypto_alg_list, cra_list)
498 dump_alloc += CRYPTO_REPORT_MAXSIZE;
499 up_read(&crypto_alg_sem);
502 struct netlink_dump_control c = {
505 .min_dump_alloc = min(dump_alloc, 65535UL),
507 err = netlink_dump_start(crypto_nlsk, skb, nlh, &c);
513 err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
518 if (link->doit == NULL)
521 return link->doit(skb, nlh, attrs);
524 static void crypto_netlink_rcv(struct sk_buff *skb)
526 mutex_lock(&crypto_cfg_mutex);
527 netlink_rcv_skb(skb, &crypto_user_rcv_msg);
528 mutex_unlock(&crypto_cfg_mutex);
531 static int __init crypto_user_init(void)
533 struct netlink_kernel_cfg cfg = {
534 .input = crypto_netlink_rcv,
537 crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO, &cfg);
544 static void __exit crypto_user_exit(void)
546 netlink_kernel_release(crypto_nlsk);
549 module_init(crypto_user_init);
550 module_exit(crypto_user_exit);
551 MODULE_LICENSE("GPL");
552 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
553 MODULE_DESCRIPTION("Crypto userspace configuration API");
554 MODULE_ALIAS("net-pf-16-proto-21");