1 // SPDX-License-Identifier: GPL-2.0
3 * Crypto user configuration API.
5 * Copyright (C) 2017-2018 Corentin Labbe <clabbe@baylibre.com>
9 #include <crypto/algapi.h>
10 #include <crypto/internal/cryptouser.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <net/netlink.h>
18 #define null_terminated(x) (strnlen(x, sizeof(x)) < sizeof(x))
20 struct crypto_dump_info {
21 struct sk_buff *in_skb;
22 struct sk_buff *out_skb;
27 static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
29 struct crypto_stat_cipher rcipher;
31 memset(&rcipher, 0, sizeof(rcipher));
33 strscpy(rcipher.type, "cipher", sizeof(rcipher.type));
35 return nla_put(skb, CRYPTOCFGA_STAT_CIPHER, sizeof(rcipher), &rcipher);
38 static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
40 struct crypto_stat_compress rcomp;
42 memset(&rcomp, 0, sizeof(rcomp));
44 strscpy(rcomp.type, "compression", sizeof(rcomp.type));
46 return nla_put(skb, CRYPTOCFGA_STAT_COMPRESS, sizeof(rcomp), &rcomp);
49 static int crypto_reportstat_one(struct crypto_alg *alg,
50 struct crypto_user_alg *ualg,
53 memset(ualg, 0, sizeof(*ualg));
55 strscpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
56 strscpy(ualg->cru_driver_name, alg->cra_driver_name,
57 sizeof(ualg->cru_driver_name));
58 strscpy(ualg->cru_module_name, module_name(alg->cra_module),
59 sizeof(ualg->cru_module_name));
63 ualg->cru_flags = alg->cra_flags;
64 ualg->cru_refcnt = refcount_read(&alg->cra_refcnt);
66 if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
68 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
69 struct crypto_stat_larval rl;
71 memset(&rl, 0, sizeof(rl));
72 strscpy(rl.type, "larval", sizeof(rl.type));
73 if (nla_put(skb, CRYPTOCFGA_STAT_LARVAL, sizeof(rl), &rl))
78 if (alg->cra_type && alg->cra_type->report_stat) {
79 if (alg->cra_type->report_stat(skb, alg))
84 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
85 case CRYPTO_ALG_TYPE_CIPHER:
86 if (crypto_report_cipher(skb, alg))
89 case CRYPTO_ALG_TYPE_COMPRESS:
90 if (crypto_report_comp(skb, alg))
94 pr_err("ERROR: Unhandled alg %d in %s\n",
95 alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL),
106 static int crypto_reportstat_alg(struct crypto_alg *alg,
107 struct crypto_dump_info *info)
109 struct sk_buff *in_skb = info->in_skb;
110 struct sk_buff *skb = info->out_skb;
111 struct nlmsghdr *nlh;
112 struct crypto_user_alg *ualg;
115 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq,
116 CRYPTO_MSG_GETSTAT, sizeof(*ualg), info->nlmsg_flags);
122 ualg = nlmsg_data(nlh);
124 err = crypto_reportstat_one(alg, ualg, skb);
126 nlmsg_cancel(skb, nlh);
136 int crypto_reportstat(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
137 struct nlattr **attrs)
139 struct net *net = sock_net(in_skb->sk);
140 struct crypto_user_alg *p = nlmsg_data(in_nlh);
141 struct crypto_alg *alg;
143 struct crypto_dump_info info;
146 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
149 alg = crypto_alg_match(p, 0);
154 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
158 info.in_skb = in_skb;
160 info.nlmsg_seq = in_nlh->nlmsg_seq;
161 info.nlmsg_flags = 0;
163 err = crypto_reportstat_alg(alg, &info);
173 return nlmsg_unicast(net->crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
176 MODULE_LICENSE("GPL");