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_acomp(struct sk_buff
*skb
, struct crypto_alg
*alg
)
120 struct crypto_report_acomp racomp
;
122 strncpy(racomp
.type
, "acomp", sizeof(racomp
.type
));
124 if (nla_put(skb
, CRYPTOCFGA_REPORT_ACOMP
,
125 sizeof(struct crypto_report_acomp
), &racomp
))
126 goto nla_put_failure
;
133 static int crypto_report_akcipher(struct sk_buff
*skb
, struct crypto_alg
*alg
)
135 struct crypto_report_akcipher rakcipher
;
137 strncpy(rakcipher
.type
, "akcipher", sizeof(rakcipher
.type
));
139 if (nla_put(skb
, CRYPTOCFGA_REPORT_AKCIPHER
,
140 sizeof(struct crypto_report_akcipher
), &rakcipher
))
141 goto nla_put_failure
;
148 static int crypto_report_kpp(struct sk_buff
*skb
, struct crypto_alg
*alg
)
150 struct crypto_report_kpp rkpp
;
152 strncpy(rkpp
.type
, "kpp", sizeof(rkpp
.type
));
154 if (nla_put(skb
, CRYPTOCFGA_REPORT_KPP
,
155 sizeof(struct crypto_report_kpp
), &rkpp
))
156 goto nla_put_failure
;
163 static int crypto_report_one(struct crypto_alg
*alg
,
164 struct crypto_user_alg
*ualg
, struct sk_buff
*skb
)
166 strncpy(ualg
->cru_name
, alg
->cra_name
, sizeof(ualg
->cru_name
));
167 strncpy(ualg
->cru_driver_name
, alg
->cra_driver_name
,
168 sizeof(ualg
->cru_driver_name
));
169 strncpy(ualg
->cru_module_name
, module_name(alg
->cra_module
),
170 sizeof(ualg
->cru_module_name
));
174 ualg
->cru_flags
= alg
->cra_flags
;
175 ualg
->cru_refcnt
= refcount_read(&alg
->cra_refcnt
);
177 if (nla_put_u32(skb
, CRYPTOCFGA_PRIORITY_VAL
, alg
->cra_priority
))
178 goto nla_put_failure
;
179 if (alg
->cra_flags
& CRYPTO_ALG_LARVAL
) {
180 struct crypto_report_larval rl
;
182 strncpy(rl
.type
, "larval", sizeof(rl
.type
));
183 if (nla_put(skb
, CRYPTOCFGA_REPORT_LARVAL
,
184 sizeof(struct crypto_report_larval
), &rl
))
185 goto nla_put_failure
;
189 if (alg
->cra_type
&& alg
->cra_type
->report
) {
190 if (alg
->cra_type
->report(skb
, alg
))
191 goto nla_put_failure
;
196 switch (alg
->cra_flags
& (CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_LARVAL
)) {
197 case CRYPTO_ALG_TYPE_CIPHER
:
198 if (crypto_report_cipher(skb
, alg
))
199 goto nla_put_failure
;
202 case CRYPTO_ALG_TYPE_COMPRESS
:
203 if (crypto_report_comp(skb
, alg
))
204 goto nla_put_failure
;
207 case CRYPTO_ALG_TYPE_ACOMPRESS
:
208 if (crypto_report_acomp(skb
, alg
))
209 goto nla_put_failure
;
212 case CRYPTO_ALG_TYPE_AKCIPHER
:
213 if (crypto_report_akcipher(skb
, alg
))
214 goto nla_put_failure
;
217 case CRYPTO_ALG_TYPE_KPP
:
218 if (crypto_report_kpp(skb
, alg
))
219 goto nla_put_failure
;
230 static int crypto_report_alg(struct crypto_alg
*alg
,
231 struct crypto_dump_info
*info
)
233 struct sk_buff
*in_skb
= info
->in_skb
;
234 struct sk_buff
*skb
= info
->out_skb
;
235 struct nlmsghdr
*nlh
;
236 struct crypto_user_alg
*ualg
;
239 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, info
->nlmsg_seq
,
240 CRYPTO_MSG_GETALG
, sizeof(*ualg
), info
->nlmsg_flags
);
246 ualg
= nlmsg_data(nlh
);
248 err
= crypto_report_one(alg
, ualg
, skb
);
250 nlmsg_cancel(skb
, nlh
);
260 static int crypto_report(struct sk_buff
*in_skb
, struct nlmsghdr
*in_nlh
,
261 struct nlattr
**attrs
)
263 struct crypto_user_alg
*p
= nlmsg_data(in_nlh
);
264 struct crypto_alg
*alg
;
266 struct crypto_dump_info info
;
269 if (!null_terminated(p
->cru_name
) || !null_terminated(p
->cru_driver_name
))
272 alg
= crypto_alg_match(p
, 0);
277 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
281 info
.in_skb
= in_skb
;
283 info
.nlmsg_seq
= in_nlh
->nlmsg_seq
;
284 info
.nlmsg_flags
= 0;
286 err
= crypto_report_alg(alg
, &info
);
296 return nlmsg_unicast(crypto_nlsk
, skb
, NETLINK_CB(in_skb
).portid
);
299 static int crypto_dump_report(struct sk_buff
*skb
, struct netlink_callback
*cb
)
301 const size_t start_pos
= cb
->args
[0];
303 struct crypto_dump_info info
;
304 struct crypto_alg
*alg
;
307 info
.in_skb
= cb
->skb
;
309 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
310 info
.nlmsg_flags
= NLM_F_MULTI
;
312 down_read(&crypto_alg_sem
);
313 list_for_each_entry(alg
, &crypto_alg_list
, cra_list
) {
314 if (pos
>= start_pos
) {
315 res
= crypto_report_alg(alg
, &info
);
316 if (res
== -EMSGSIZE
)
326 up_read(&crypto_alg_sem
);
330 static int crypto_dump_report_done(struct netlink_callback
*cb
)
335 static int crypto_update_alg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
336 struct nlattr
**attrs
)
338 struct crypto_alg
*alg
;
339 struct crypto_user_alg
*p
= nlmsg_data(nlh
);
340 struct nlattr
*priority
= attrs
[CRYPTOCFGA_PRIORITY_VAL
];
343 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
346 if (!null_terminated(p
->cru_name
) || !null_terminated(p
->cru_driver_name
))
349 if (priority
&& !strlen(p
->cru_driver_name
))
352 alg
= crypto_alg_match(p
, 1);
356 down_write(&crypto_alg_sem
);
358 crypto_remove_spawns(alg
, &list
, NULL
);
361 alg
->cra_priority
= nla_get_u32(priority
);
363 up_write(&crypto_alg_sem
);
366 crypto_remove_final(&list
);
371 static int crypto_del_alg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
372 struct nlattr
**attrs
)
374 struct crypto_alg
*alg
;
375 struct crypto_user_alg
*p
= nlmsg_data(nlh
);
378 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
381 if (!null_terminated(p
->cru_name
) || !null_terminated(p
->cru_driver_name
))
384 alg
= crypto_alg_match(p
, 1);
388 /* We can not unregister core algorithms such as aes-generic.
389 * We would loose the reference in the crypto_alg_list to this algorithm
390 * if we try to unregister. Unregistering such an algorithm without
391 * removing the module is not possible, so we restrict to crypto
392 * instances that are build from templates. */
394 if (!(alg
->cra_flags
& CRYPTO_ALG_INSTANCE
))
398 if (refcount_read(&alg
->cra_refcnt
) > 2)
401 err
= crypto_unregister_instance((struct crypto_instance
*)alg
);
408 static int crypto_add_alg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
409 struct nlattr
**attrs
)
413 struct crypto_alg
*alg
;
414 struct crypto_user_alg
*p
= nlmsg_data(nlh
);
415 struct nlattr
*priority
= attrs
[CRYPTOCFGA_PRIORITY_VAL
];
417 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
420 if (!null_terminated(p
->cru_name
) || !null_terminated(p
->cru_driver_name
))
423 if (strlen(p
->cru_driver_name
))
426 if (priority
&& !exact
)
429 alg
= crypto_alg_match(p
, exact
);
435 if (strlen(p
->cru_driver_name
))
436 name
= p
->cru_driver_name
;
440 alg
= crypto_alg_mod_lookup(name
, p
->cru_type
, p
->cru_mask
);
444 down_write(&crypto_alg_sem
);
447 alg
->cra_priority
= nla_get_u32(priority
);
449 up_write(&crypto_alg_sem
);
456 static int crypto_del_rng(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
457 struct nlattr
**attrs
)
459 if (!netlink_capable(skb
, CAP_NET_ADMIN
))
461 return crypto_del_default_rng();
464 #define MSGSIZE(type) sizeof(struct type)
466 static const int crypto_msg_min
[CRYPTO_NR_MSGTYPES
] = {
467 [CRYPTO_MSG_NEWALG
- CRYPTO_MSG_BASE
] = MSGSIZE(crypto_user_alg
),
468 [CRYPTO_MSG_DELALG
- CRYPTO_MSG_BASE
] = MSGSIZE(crypto_user_alg
),
469 [CRYPTO_MSG_UPDATEALG
- CRYPTO_MSG_BASE
] = MSGSIZE(crypto_user_alg
),
470 [CRYPTO_MSG_GETALG
- CRYPTO_MSG_BASE
] = MSGSIZE(crypto_user_alg
),
471 [CRYPTO_MSG_DELRNG
- CRYPTO_MSG_BASE
] = 0,
474 static const struct nla_policy crypto_policy
[CRYPTOCFGA_MAX
+1] = {
475 [CRYPTOCFGA_PRIORITY_VAL
] = { .type
= NLA_U32
},
480 static const struct crypto_link
{
481 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
482 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
483 int (*done
)(struct netlink_callback
*);
484 } crypto_dispatch
[CRYPTO_NR_MSGTYPES
] = {
485 [CRYPTO_MSG_NEWALG
- CRYPTO_MSG_BASE
] = { .doit
= crypto_add_alg
},
486 [CRYPTO_MSG_DELALG
- CRYPTO_MSG_BASE
] = { .doit
= crypto_del_alg
},
487 [CRYPTO_MSG_UPDATEALG
- CRYPTO_MSG_BASE
] = { .doit
= crypto_update_alg
},
488 [CRYPTO_MSG_GETALG
- CRYPTO_MSG_BASE
] = { .doit
= crypto_report
,
489 .dump
= crypto_dump_report
,
490 .done
= crypto_dump_report_done
},
491 [CRYPTO_MSG_DELRNG
- CRYPTO_MSG_BASE
] = { .doit
= crypto_del_rng
},
494 static int crypto_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
495 struct netlink_ext_ack
*extack
)
497 struct nlattr
*attrs
[CRYPTOCFGA_MAX
+1];
498 const struct crypto_link
*link
;
501 type
= nlh
->nlmsg_type
;
502 if (type
> CRYPTO_MSG_MAX
)
505 type
-= CRYPTO_MSG_BASE
;
506 link
= &crypto_dispatch
[type
];
508 if ((type
== (CRYPTO_MSG_GETALG
- CRYPTO_MSG_BASE
) &&
509 (nlh
->nlmsg_flags
& NLM_F_DUMP
))) {
510 struct crypto_alg
*alg
;
511 unsigned long dump_alloc
= 0;
513 if (link
->dump
== NULL
)
516 down_read(&crypto_alg_sem
);
517 list_for_each_entry(alg
, &crypto_alg_list
, cra_list
)
518 dump_alloc
+= CRYPTO_REPORT_MAXSIZE
;
519 up_read(&crypto_alg_sem
);
522 struct netlink_dump_control c
= {
525 .min_dump_alloc
= min(dump_alloc
, 65535UL),
527 err
= netlink_dump_start(crypto_nlsk
, skb
, nlh
, &c
);
533 err
= nlmsg_parse(nlh
, crypto_msg_min
[type
], attrs
, CRYPTOCFGA_MAX
,
534 crypto_policy
, extack
);
538 if (link
->doit
== NULL
)
541 return link
->doit(skb
, nlh
, attrs
);
544 static void crypto_netlink_rcv(struct sk_buff
*skb
)
546 mutex_lock(&crypto_cfg_mutex
);
547 netlink_rcv_skb(skb
, &crypto_user_rcv_msg
);
548 mutex_unlock(&crypto_cfg_mutex
);
551 static int __init
crypto_user_init(void)
553 struct netlink_kernel_cfg cfg
= {
554 .input
= crypto_netlink_rcv
,
557 crypto_nlsk
= netlink_kernel_create(&init_net
, NETLINK_CRYPTO
, &cfg
);
564 static void __exit
crypto_user_exit(void)
566 netlink_kernel_release(crypto_nlsk
);
569 module_init(crypto_user_init
);
570 module_exit(crypto_user_exit
);
571 MODULE_LICENSE("GPL");
572 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
573 MODULE_DESCRIPTION("Crypto userspace configuration API");
574 MODULE_ALIAS("net-pf-16-proto-21");