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 <net/netlink.h>
25 #include <linux/security.h>
26 #include <net/net_namespace.h>
29 DEFINE_MUTEX(crypto_cfg_mutex);
31 /* The crypto netlink socket */
32 static struct sock *crypto_nlsk;
34 struct crypto_dump_info {
35 struct sk_buff *in_skb;
36 struct sk_buff *out_skb;
41 static struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
43 struct crypto_alg *q, *alg = NULL;
45 down_read(&crypto_alg_sem);
47 list_for_each_entry(q, &crypto_alg_list, cra_list) {
50 if ((q->cra_flags ^ p->cru_type) & p->cru_mask)
53 if (strlen(p->cru_driver_name))
54 match = !strcmp(q->cra_driver_name,
57 match = !strcmp(q->cra_name, p->cru_name);
65 up_read(&crypto_alg_sem);
70 static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
72 struct crypto_report_cipher rcipher;
74 snprintf(rcipher.type, CRYPTO_MAX_ALG_NAME, "%s", "cipher");
76 rcipher.blocksize = alg->cra_blocksize;
77 rcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
78 rcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
80 NLA_PUT(skb, CRYPTOCFGA_REPORT_CIPHER,
81 sizeof(struct crypto_report_cipher), &rcipher);
89 static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
91 struct crypto_report_comp rcomp;
93 snprintf(rcomp.type, CRYPTO_MAX_ALG_NAME, "%s", "compression");
95 NLA_PUT(skb, CRYPTOCFGA_REPORT_COMPRESS,
96 sizeof(struct crypto_report_comp), &rcomp);
104 static int crypto_report_one(struct crypto_alg *alg,
105 struct crypto_user_alg *ualg, struct sk_buff *skb)
107 memcpy(&ualg->cru_name, &alg->cra_name, sizeof(ualg->cru_name));
108 memcpy(&ualg->cru_driver_name, &alg->cra_driver_name,
109 sizeof(ualg->cru_driver_name));
110 memcpy(&ualg->cru_module_name, module_name(alg->cra_module),
111 CRYPTO_MAX_ALG_NAME);
113 ualg->cru_flags = alg->cra_flags;
114 ualg->cru_refcnt = atomic_read(&alg->cra_refcnt);
116 NLA_PUT_U32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority);
118 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
119 struct crypto_report_larval rl;
121 snprintf(rl.type, CRYPTO_MAX_ALG_NAME, "%s", "larval");
123 NLA_PUT(skb, CRYPTOCFGA_REPORT_LARVAL,
124 sizeof(struct crypto_report_larval), &rl);
129 if (alg->cra_type && alg->cra_type->report) {
130 if (alg->cra_type->report(skb, alg))
131 goto nla_put_failure;
136 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
137 case CRYPTO_ALG_TYPE_CIPHER:
138 if (crypto_report_cipher(skb, alg))
139 goto nla_put_failure;
142 case CRYPTO_ALG_TYPE_COMPRESS:
143 if (crypto_report_comp(skb, alg))
144 goto nla_put_failure;
156 static int crypto_report_alg(struct crypto_alg *alg,
157 struct crypto_dump_info *info)
159 struct sk_buff *in_skb = info->in_skb;
160 struct sk_buff *skb = info->out_skb;
161 struct nlmsghdr *nlh;
162 struct crypto_user_alg *ualg;
165 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, info->nlmsg_seq,
166 CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
172 ualg = nlmsg_data(nlh);
174 err = crypto_report_one(alg, ualg, skb);
176 nlmsg_cancel(skb, nlh);
186 static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
187 struct nlattr **attrs)
189 struct crypto_user_alg *p = nlmsg_data(in_nlh);
190 struct crypto_alg *alg;
192 struct crypto_dump_info info;
195 if (!p->cru_driver_name)
198 alg = crypto_alg_match(p, 1);
202 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
206 info.in_skb = in_skb;
208 info.nlmsg_seq = in_nlh->nlmsg_seq;
209 info.nlmsg_flags = 0;
211 err = crypto_report_alg(alg, &info);
215 return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).pid);
218 static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
220 struct crypto_alg *alg;
221 struct crypto_dump_info info;
229 info.in_skb = cb->skb;
231 info.nlmsg_seq = cb->nlh->nlmsg_seq;
232 info.nlmsg_flags = NLM_F_MULTI;
234 list_for_each_entry(alg, &crypto_alg_list, cra_list) {
235 err = crypto_report_alg(alg, &info);
246 static int crypto_dump_report_done(struct netlink_callback *cb)
251 static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
252 struct nlattr **attrs)
254 struct crypto_alg *alg;
255 struct crypto_user_alg *p = nlmsg_data(nlh);
256 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
259 if (priority && !strlen(p->cru_driver_name))
262 alg = crypto_alg_match(p, 1);
266 down_write(&crypto_alg_sem);
268 crypto_remove_spawns(alg, &list, NULL);
271 alg->cra_priority = nla_get_u32(priority);
273 up_write(&crypto_alg_sem);
275 crypto_remove_final(&list);
280 static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
281 struct nlattr **attrs)
283 struct crypto_alg *alg;
284 struct crypto_user_alg *p = nlmsg_data(nlh);
286 alg = crypto_alg_match(p, 1);
290 /* We can not unregister core algorithms such as aes-generic.
291 * We would loose the reference in the crypto_alg_list to this algorithm
292 * if we try to unregister. Unregistering such an algorithm without
293 * removing the module is not possible, so we restrict to crypto
294 * instances that are build from templates. */
295 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
298 if (atomic_read(&alg->cra_refcnt) != 1)
301 return crypto_unregister_alg(alg);
304 static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
305 struct nlattr **attrs)
309 struct crypto_alg *alg;
310 struct crypto_user_alg *p = nlmsg_data(nlh);
311 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
313 if (strlen(p->cru_driver_name))
316 if (priority && !exact)
319 alg = crypto_alg_match(p, exact);
323 if (strlen(p->cru_driver_name))
324 name = p->cru_driver_name;
328 alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
332 down_write(&crypto_alg_sem);
335 alg->cra_priority = nla_get_u32(priority);
337 up_write(&crypto_alg_sem);
344 #define MSGSIZE(type) sizeof(struct type)
346 static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
347 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
348 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
349 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
350 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
353 static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
354 [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
359 static struct crypto_link {
360 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
361 int (*dump)(struct sk_buff *, struct netlink_callback *);
362 int (*done)(struct netlink_callback *);
363 } crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
364 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
365 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
366 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
367 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
368 .dump = crypto_dump_report,
369 .done = crypto_dump_report_done},
372 static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
374 struct nlattr *attrs[CRYPTOCFGA_MAX+1];
375 struct crypto_link *link;
378 type = nlh->nlmsg_type;
379 if (type > CRYPTO_MSG_MAX)
382 type -= CRYPTO_MSG_BASE;
383 link = &crypto_dispatch[type];
385 if (security_netlink_recv(skb, CAP_NET_ADMIN))
388 if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
389 (nlh->nlmsg_flags & NLM_F_DUMP))) {
390 if (link->dump == NULL)
393 return netlink_dump_start(crypto_nlsk, skb, nlh,
394 link->dump, link->done, 0);
397 err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
402 if (link->doit == NULL)
405 return link->doit(skb, nlh, attrs);
408 static void crypto_netlink_rcv(struct sk_buff *skb)
410 mutex_lock(&crypto_cfg_mutex);
411 netlink_rcv_skb(skb, &crypto_user_rcv_msg);
412 mutex_unlock(&crypto_cfg_mutex);
415 static int __init crypto_user_init(void)
417 crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO,
418 0, crypto_netlink_rcv,
426 static void __exit crypto_user_exit(void)
428 netlink_kernel_release(crypto_nlsk);
431 module_init(crypto_user_init);
432 module_exit(crypto_user_exit);
433 MODULE_LICENSE("GPL");
434 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
435 MODULE_DESCRIPTION("Crypto userspace configuration API");