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>
34 #define null_terminated(x) (strnlen(x, sizeof(x)) < sizeof(x))
36 static DEFINE_MUTEX(crypto_cfg_mutex);
38 /* The crypto netlink socket */
39 static struct sock *crypto_nlsk;
41 struct crypto_dump_info {
42 struct sk_buff *in_skb;
43 struct sk_buff *out_skb;
48 static struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
50 struct crypto_alg *q, *alg = NULL;
52 down_read(&crypto_alg_sem);
54 list_for_each_entry(q, &crypto_alg_list, cra_list) {
57 if (crypto_is_larval(q))
60 if ((q->cra_flags ^ p->cru_type) & p->cru_mask)
63 if (strlen(p->cru_driver_name))
64 match = !strcmp(q->cra_driver_name,
67 match = !strcmp(q->cra_name, p->cru_name);
72 if (unlikely(!crypto_mod_get(q)))
79 up_read(&crypto_alg_sem);
84 static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
86 struct crypto_report_cipher rcipher;
88 strncpy(rcipher.type, "cipher", sizeof(rcipher.type));
90 rcipher.blocksize = alg->cra_blocksize;
91 rcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
92 rcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
94 if (nla_put(skb, CRYPTOCFGA_REPORT_CIPHER,
95 sizeof(struct crypto_report_cipher), &rcipher))
103 static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
105 struct crypto_report_comp rcomp;
107 strncpy(rcomp.type, "compression", sizeof(rcomp.type));
108 if (nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
109 sizeof(struct crypto_report_comp), &rcomp))
110 goto nla_put_failure;
117 static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)
119 struct crypto_report_akcipher rakcipher;
121 strncpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
123 if (nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
124 sizeof(struct crypto_report_akcipher), &rakcipher))
125 goto nla_put_failure;
132 static int crypto_report_one(struct crypto_alg *alg,
133 struct crypto_user_alg *ualg, struct sk_buff *skb)
135 strncpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
136 strncpy(ualg->cru_driver_name, alg->cra_driver_name,
137 sizeof(ualg->cru_driver_name));
138 strncpy(ualg->cru_module_name, module_name(alg->cra_module),
139 sizeof(ualg->cru_module_name));
143 ualg->cru_flags = alg->cra_flags;
144 ualg->cru_refcnt = atomic_read(&alg->cra_refcnt);
146 if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
147 goto nla_put_failure;
148 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
149 struct crypto_report_larval rl;
151 strncpy(rl.type, "larval", sizeof(rl.type));
152 if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL,
153 sizeof(struct crypto_report_larval), &rl))
154 goto nla_put_failure;
158 if (alg->cra_type && alg->cra_type->report) {
159 if (alg->cra_type->report(skb, alg))
160 goto nla_put_failure;
165 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
166 case CRYPTO_ALG_TYPE_CIPHER:
167 if (crypto_report_cipher(skb, alg))
168 goto nla_put_failure;
171 case CRYPTO_ALG_TYPE_COMPRESS:
172 if (crypto_report_comp(skb, alg))
173 goto nla_put_failure;
177 case CRYPTO_ALG_TYPE_AKCIPHER:
178 if (crypto_report_akcipher(skb, alg))
179 goto nla_put_failure;
191 static int crypto_report_alg(struct crypto_alg *alg,
192 struct crypto_dump_info *info)
194 struct sk_buff *in_skb = info->in_skb;
195 struct sk_buff *skb = info->out_skb;
196 struct nlmsghdr *nlh;
197 struct crypto_user_alg *ualg;
200 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq,
201 CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
207 ualg = nlmsg_data(nlh);
209 err = crypto_report_one(alg, ualg, skb);
211 nlmsg_cancel(skb, nlh);
221 static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
222 struct nlattr **attrs)
224 struct crypto_user_alg *p = nlmsg_data(in_nlh);
225 struct crypto_alg *alg;
227 struct crypto_dump_info info;
230 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
233 alg = crypto_alg_match(p, 0);
238 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
242 info.in_skb = in_skb;
244 info.nlmsg_seq = in_nlh->nlmsg_seq;
245 info.nlmsg_flags = 0;
247 err = crypto_report_alg(alg, &info);
257 return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
260 static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
262 const size_t start_pos = cb->args[0];
264 struct crypto_dump_info info;
265 struct crypto_alg *alg;
268 info.in_skb = cb->skb;
270 info.nlmsg_seq = cb->nlh->nlmsg_seq;
271 info.nlmsg_flags = NLM_F_MULTI;
273 down_read(&crypto_alg_sem);
274 list_for_each_entry(alg, &crypto_alg_list, cra_list) {
275 if (pos >= start_pos) {
276 res = crypto_report_alg(alg, &info);
277 if (res == -EMSGSIZE)
287 up_read(&crypto_alg_sem);
291 static int crypto_dump_report_done(struct netlink_callback *cb)
296 static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
297 struct nlattr **attrs)
299 struct crypto_alg *alg;
300 struct crypto_user_alg *p = nlmsg_data(nlh);
301 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
304 if (!netlink_capable(skb, CAP_NET_ADMIN))
307 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
310 if (priority && !strlen(p->cru_driver_name))
313 alg = crypto_alg_match(p, 1);
317 down_write(&crypto_alg_sem);
319 crypto_remove_spawns(alg, &list, NULL);
322 alg->cra_priority = nla_get_u32(priority);
324 up_write(&crypto_alg_sem);
327 crypto_remove_final(&list);
332 static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
333 struct nlattr **attrs)
335 struct crypto_alg *alg;
336 struct crypto_user_alg *p = nlmsg_data(nlh);
339 if (!netlink_capable(skb, CAP_NET_ADMIN))
342 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
345 alg = crypto_alg_match(p, 1);
349 /* We can not unregister core algorithms such as aes-generic.
350 * We would loose the reference in the crypto_alg_list to this algorithm
351 * if we try to unregister. Unregistering such an algorithm without
352 * removing the module is not possible, so we restrict to crypto
353 * instances that are build from templates. */
355 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
359 if (atomic_read(&alg->cra_refcnt) > 2)
362 err = crypto_unregister_instance((struct crypto_instance *)alg);
369 static struct crypto_alg *crypto_user_skcipher_alg(const char *name, u32 type,
373 struct crypto_alg *alg;
375 type = crypto_skcipher_type(type);
376 mask = crypto_skcipher_mask(mask);
379 alg = crypto_lookup_skcipher(name, type, mask);
386 if (fatal_signal_pending(current)) {
395 static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
396 struct nlattr **attrs)
400 struct crypto_alg *alg;
401 struct crypto_user_alg *p = nlmsg_data(nlh);
402 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
404 if (!netlink_capable(skb, CAP_NET_ADMIN))
407 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
410 if (strlen(p->cru_driver_name))
413 if (priority && !exact)
416 alg = crypto_alg_match(p, exact);
422 if (strlen(p->cru_driver_name))
423 name = p->cru_driver_name;
427 switch (p->cru_type & p->cru_mask & CRYPTO_ALG_TYPE_MASK) {
428 case CRYPTO_ALG_TYPE_GIVCIPHER:
429 case CRYPTO_ALG_TYPE_BLKCIPHER:
430 case CRYPTO_ALG_TYPE_ABLKCIPHER:
431 alg = crypto_user_skcipher_alg(name, p->cru_type, p->cru_mask);
434 alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
440 down_write(&crypto_alg_sem);
443 alg->cra_priority = nla_get_u32(priority);
445 up_write(&crypto_alg_sem);
452 static int crypto_del_rng(struct sk_buff *skb, struct nlmsghdr *nlh,
453 struct nlattr **attrs)
455 if (!netlink_capable(skb, CAP_NET_ADMIN))
457 return crypto_del_default_rng();
460 #define MSGSIZE(type) sizeof(struct type)
462 static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
463 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
464 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
465 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
466 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
467 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = 0,
470 static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
471 [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
476 static const struct crypto_link {
477 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
478 int (*dump)(struct sk_buff *, struct netlink_callback *);
479 int (*done)(struct netlink_callback *);
480 } crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
481 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
482 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
483 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
484 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
485 .dump = crypto_dump_report,
486 .done = crypto_dump_report_done},
487 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = { .doit = crypto_del_rng },
490 static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
492 struct nlattr *attrs[CRYPTOCFGA_MAX+1];
493 const struct crypto_link *link;
496 type = nlh->nlmsg_type;
497 if (type > CRYPTO_MSG_MAX)
500 type -= CRYPTO_MSG_BASE;
501 link = &crypto_dispatch[type];
503 if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
504 (nlh->nlmsg_flags & NLM_F_DUMP))) {
505 struct crypto_alg *alg;
506 unsigned long dump_alloc = 0;
508 if (link->dump == NULL)
511 down_read(&crypto_alg_sem);
512 list_for_each_entry(alg, &crypto_alg_list, cra_list)
513 dump_alloc += CRYPTO_REPORT_MAXSIZE;
514 up_read(&crypto_alg_sem);
517 struct netlink_dump_control c = {
520 .min_dump_alloc = min(dump_alloc, 65535UL),
522 err = netlink_dump_start(crypto_nlsk, skb, nlh, &c);
528 err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
533 if (link->doit == NULL)
536 return link->doit(skb, nlh, attrs);
539 static void crypto_netlink_rcv(struct sk_buff *skb)
541 mutex_lock(&crypto_cfg_mutex);
542 netlink_rcv_skb(skb, &crypto_user_rcv_msg);
543 mutex_unlock(&crypto_cfg_mutex);
546 static int __init crypto_user_init(void)
548 struct netlink_kernel_cfg cfg = {
549 .input = crypto_netlink_rcv,
552 crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO, &cfg);
559 static void __exit crypto_user_exit(void)
561 netlink_kernel_release(crypto_nlsk);
564 module_init(crypto_user_init);
565 module_exit(crypto_user_exit);
566 MODULE_LICENSE("GPL");
567 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
568 MODULE_DESCRIPTION("Crypto userspace configuration API");
569 MODULE_ALIAS("net-pf-16-proto-21");