blob: d4d1cb2bedd7f9988a7076ad20289beb893f409a [file] [log] [blame]
Corentin Labbecac58182018-09-19 10:10:54 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Crypto user configuration API.
4 *
5 * Copyright (C) 2017-2018 Corentin Labbe <clabbe@baylibre.com>
6 *
7 */
8
9#include <linux/crypto.h>
10#include <linux/cryptouser.h>
11#include <linux/sched.h>
12#include <net/netlink.h>
13#include <crypto/internal/skcipher.h>
14#include <crypto/internal/rng.h>
15#include <crypto/akcipher.h>
16#include <crypto/kpp.h>
17#include <crypto/internal/cryptouser.h>
18
19#include "internal.h"
20
21#define null_terminated(x) (strnlen(x, sizeof(x)) < sizeof(x))
22
23static DEFINE_MUTEX(crypto_cfg_mutex);
24
Corentin Labbecac58182018-09-19 10:10:54 +000025struct crypto_dump_info {
26 struct sk_buff *in_skb;
27 struct sk_buff *out_skb;
28 u32 nlmsg_seq;
29 u16 nlmsg_flags;
30};
31
32static int crypto_report_aead(struct sk_buff *skb, struct crypto_alg *alg)
33{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +000034 struct crypto_stat_aead raead;
Corentin Labbecac58182018-09-19 10:10:54 +000035
Corentin Labbe9f4debe2018-11-03 14:56:01 -070036 memset(&raead, 0, sizeof(raead));
37
Eric Biggers37db69e2018-11-03 14:56:03 -070038 strscpy(raead.type, "aead", sizeof(raead.type));
Corentin Labbecac58182018-09-19 10:10:54 +000039
Corentin Labbe17c18f92018-11-29 14:42:24 +000040 raead.stat_encrypt_cnt = atomic64_read(&alg->stats.aead.encrypt_cnt);
41 raead.stat_encrypt_tlen = atomic64_read(&alg->stats.aead.encrypt_tlen);
42 raead.stat_decrypt_cnt = atomic64_read(&alg->stats.aead.decrypt_cnt);
43 raead.stat_decrypt_tlen = atomic64_read(&alg->stats.aead.decrypt_tlen);
Corentin Labbe44f13132018-11-29 14:42:25 +000044 raead.stat_err_cnt = atomic64_read(&alg->stats.aead.err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +000045
Eric Biggers37db69e2018-11-03 14:56:03 -070046 return nla_put(skb, CRYPTOCFGA_STAT_AEAD, sizeof(raead), &raead);
Corentin Labbecac58182018-09-19 10:10:54 +000047}
48
49static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
50{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +000051 struct crypto_stat_cipher rcipher;
Corentin Labbecac58182018-09-19 10:10:54 +000052
Corentin Labbe9f4debe2018-11-03 14:56:01 -070053 memset(&rcipher, 0, sizeof(rcipher));
54
Eric Biggers37db69e2018-11-03 14:56:03 -070055 strscpy(rcipher.type, "cipher", sizeof(rcipher.type));
Corentin Labbecac58182018-09-19 10:10:54 +000056
Corentin Labbe17c18f92018-11-29 14:42:24 +000057 rcipher.stat_encrypt_cnt = atomic64_read(&alg->stats.cipher.encrypt_cnt);
58 rcipher.stat_encrypt_tlen = atomic64_read(&alg->stats.cipher.encrypt_tlen);
59 rcipher.stat_decrypt_cnt = atomic64_read(&alg->stats.cipher.decrypt_cnt);
60 rcipher.stat_decrypt_tlen = atomic64_read(&alg->stats.cipher.decrypt_tlen);
Corentin Labbe44f13132018-11-29 14:42:25 +000061 rcipher.stat_err_cnt = atomic64_read(&alg->stats.cipher.err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +000062
Eric Biggers37db69e2018-11-03 14:56:03 -070063 return nla_put(skb, CRYPTOCFGA_STAT_CIPHER, sizeof(rcipher), &rcipher);
Corentin Labbecac58182018-09-19 10:10:54 +000064}
65
66static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
67{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +000068 struct crypto_stat_compress rcomp;
Corentin Labbecac58182018-09-19 10:10:54 +000069
Corentin Labbe9f4debe2018-11-03 14:56:01 -070070 memset(&rcomp, 0, sizeof(rcomp));
71
Eric Biggers37db69e2018-11-03 14:56:03 -070072 strscpy(rcomp.type, "compression", sizeof(rcomp.type));
Corentin Labbe17c18f92018-11-29 14:42:24 +000073 rcomp.stat_compress_cnt = atomic64_read(&alg->stats.compress.compress_cnt);
74 rcomp.stat_compress_tlen = atomic64_read(&alg->stats.compress.compress_tlen);
75 rcomp.stat_decompress_cnt = atomic64_read(&alg->stats.compress.decompress_cnt);
76 rcomp.stat_decompress_tlen = atomic64_read(&alg->stats.compress.decompress_tlen);
Corentin Labbe44f13132018-11-29 14:42:25 +000077 rcomp.stat_err_cnt = atomic64_read(&alg->stats.compress.err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +000078
Eric Biggers37db69e2018-11-03 14:56:03 -070079 return nla_put(skb, CRYPTOCFGA_STAT_COMPRESS, sizeof(rcomp), &rcomp);
Corentin Labbecac58182018-09-19 10:10:54 +000080}
81
82static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg)
83{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +000084 struct crypto_stat_compress racomp;
Corentin Labbecac58182018-09-19 10:10:54 +000085
Corentin Labbe9f4debe2018-11-03 14:56:01 -070086 memset(&racomp, 0, sizeof(racomp));
87
Eric Biggers37db69e2018-11-03 14:56:03 -070088 strscpy(racomp.type, "acomp", sizeof(racomp.type));
Corentin Labbe17c18f92018-11-29 14:42:24 +000089 racomp.stat_compress_cnt = atomic64_read(&alg->stats.compress.compress_cnt);
90 racomp.stat_compress_tlen = atomic64_read(&alg->stats.compress.compress_tlen);
91 racomp.stat_decompress_cnt = atomic64_read(&alg->stats.compress.decompress_cnt);
92 racomp.stat_decompress_tlen = atomic64_read(&alg->stats.compress.decompress_tlen);
Corentin Labbe44f13132018-11-29 14:42:25 +000093 racomp.stat_err_cnt = atomic64_read(&alg->stats.compress.err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +000094
Eric Biggers37db69e2018-11-03 14:56:03 -070095 return nla_put(skb, CRYPTOCFGA_STAT_ACOMP, sizeof(racomp), &racomp);
Corentin Labbecac58182018-09-19 10:10:54 +000096}
97
98static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)
99{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000100 struct crypto_stat_akcipher rakcipher;
Corentin Labbecac58182018-09-19 10:10:54 +0000101
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700102 memset(&rakcipher, 0, sizeof(rakcipher));
103
Eric Biggers37db69e2018-11-03 14:56:03 -0700104 strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
Corentin Labbe17c18f92018-11-29 14:42:24 +0000105 rakcipher.stat_encrypt_cnt = atomic64_read(&alg->stats.akcipher.encrypt_cnt);
106 rakcipher.stat_encrypt_tlen = atomic64_read(&alg->stats.akcipher.encrypt_tlen);
107 rakcipher.stat_decrypt_cnt = atomic64_read(&alg->stats.akcipher.decrypt_cnt);
108 rakcipher.stat_decrypt_tlen = atomic64_read(&alg->stats.akcipher.decrypt_tlen);
109 rakcipher.stat_sign_cnt = atomic64_read(&alg->stats.akcipher.sign_cnt);
110 rakcipher.stat_verify_cnt = atomic64_read(&alg->stats.akcipher.verify_cnt);
Corentin Labbe44f13132018-11-29 14:42:25 +0000111 rakcipher.stat_err_cnt = atomic64_read(&alg->stats.akcipher.err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +0000112
Eric Biggers37db69e2018-11-03 14:56:03 -0700113 return nla_put(skb, CRYPTOCFGA_STAT_AKCIPHER,
114 sizeof(rakcipher), &rakcipher);
Corentin Labbecac58182018-09-19 10:10:54 +0000115}
116
117static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)
118{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000119 struct crypto_stat_kpp rkpp;
Corentin Labbecac58182018-09-19 10:10:54 +0000120
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700121 memset(&rkpp, 0, sizeof(rkpp));
122
Eric Biggers37db69e2018-11-03 14:56:03 -0700123 strscpy(rkpp.type, "kpp", sizeof(rkpp.type));
Corentin Labbecac58182018-09-19 10:10:54 +0000124
Corentin Labbe17c18f92018-11-29 14:42:24 +0000125 rkpp.stat_setsecret_cnt = atomic64_read(&alg->stats.kpp.setsecret_cnt);
126 rkpp.stat_generate_public_key_cnt = atomic64_read(&alg->stats.kpp.generate_public_key_cnt);
127 rkpp.stat_compute_shared_secret_cnt = atomic64_read(&alg->stats.kpp.compute_shared_secret_cnt);
Corentin Labbe44f13132018-11-29 14:42:25 +0000128 rkpp.stat_err_cnt = atomic64_read(&alg->stats.kpp.err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +0000129
Eric Biggers37db69e2018-11-03 14:56:03 -0700130 return nla_put(skb, CRYPTOCFGA_STAT_KPP, sizeof(rkpp), &rkpp);
Corentin Labbecac58182018-09-19 10:10:54 +0000131}
132
133static int crypto_report_ahash(struct sk_buff *skb, struct crypto_alg *alg)
134{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000135 struct crypto_stat_hash rhash;
Corentin Labbecac58182018-09-19 10:10:54 +0000136
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700137 memset(&rhash, 0, sizeof(rhash));
138
Eric Biggers37db69e2018-11-03 14:56:03 -0700139 strscpy(rhash.type, "ahash", sizeof(rhash.type));
Corentin Labbecac58182018-09-19 10:10:54 +0000140
Corentin Labbe17c18f92018-11-29 14:42:24 +0000141 rhash.stat_hash_cnt = atomic64_read(&alg->stats.hash.hash_cnt);
142 rhash.stat_hash_tlen = atomic64_read(&alg->stats.hash.hash_tlen);
Corentin Labbe44f13132018-11-29 14:42:25 +0000143 rhash.stat_err_cnt = atomic64_read(&alg->stats.hash.err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +0000144
Eric Biggers37db69e2018-11-03 14:56:03 -0700145 return nla_put(skb, CRYPTOCFGA_STAT_HASH, sizeof(rhash), &rhash);
Corentin Labbecac58182018-09-19 10:10:54 +0000146}
147
148static int crypto_report_shash(struct sk_buff *skb, struct crypto_alg *alg)
149{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000150 struct crypto_stat_hash rhash;
Corentin Labbecac58182018-09-19 10:10:54 +0000151
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700152 memset(&rhash, 0, sizeof(rhash));
153
Eric Biggers37db69e2018-11-03 14:56:03 -0700154 strscpy(rhash.type, "shash", sizeof(rhash.type));
Corentin Labbecac58182018-09-19 10:10:54 +0000155
Corentin Labbe17c18f92018-11-29 14:42:24 +0000156 rhash.stat_hash_cnt = atomic64_read(&alg->stats.hash.hash_cnt);
157 rhash.stat_hash_tlen = atomic64_read(&alg->stats.hash.hash_tlen);
Corentin Labbe44f13132018-11-29 14:42:25 +0000158 rhash.stat_err_cnt = atomic64_read(&alg->stats.hash.err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +0000159
Eric Biggers37db69e2018-11-03 14:56:03 -0700160 return nla_put(skb, CRYPTOCFGA_STAT_HASH, sizeof(rhash), &rhash);
Corentin Labbecac58182018-09-19 10:10:54 +0000161}
162
163static int crypto_report_rng(struct sk_buff *skb, struct crypto_alg *alg)
164{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000165 struct crypto_stat_rng rrng;
Corentin Labbecac58182018-09-19 10:10:54 +0000166
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700167 memset(&rrng, 0, sizeof(rrng));
168
Eric Biggers37db69e2018-11-03 14:56:03 -0700169 strscpy(rrng.type, "rng", sizeof(rrng.type));
Corentin Labbecac58182018-09-19 10:10:54 +0000170
Corentin Labbe17c18f92018-11-29 14:42:24 +0000171 rrng.stat_generate_cnt = atomic64_read(&alg->stats.rng.generate_cnt);
172 rrng.stat_generate_tlen = atomic64_read(&alg->stats.rng.generate_tlen);
173 rrng.stat_seed_cnt = atomic64_read(&alg->stats.rng.seed_cnt);
Corentin Labbe44f13132018-11-29 14:42:25 +0000174 rrng.stat_err_cnt = atomic64_read(&alg->stats.rng.err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +0000175
Eric Biggers37db69e2018-11-03 14:56:03 -0700176 return nla_put(skb, CRYPTOCFGA_STAT_RNG, sizeof(rrng), &rrng);
Corentin Labbecac58182018-09-19 10:10:54 +0000177}
178
179static int crypto_reportstat_one(struct crypto_alg *alg,
180 struct crypto_user_alg *ualg,
181 struct sk_buff *skb)
182{
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700183 memset(ualg, 0, sizeof(*ualg));
184
Eric Biggers37db69e2018-11-03 14:56:03 -0700185 strscpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
186 strscpy(ualg->cru_driver_name, alg->cra_driver_name,
Corentin Labbecac58182018-09-19 10:10:54 +0000187 sizeof(ualg->cru_driver_name));
Eric Biggers37db69e2018-11-03 14:56:03 -0700188 strscpy(ualg->cru_module_name, module_name(alg->cra_module),
Corentin Labbecac58182018-09-19 10:10:54 +0000189 sizeof(ualg->cru_module_name));
190
191 ualg->cru_type = 0;
192 ualg->cru_mask = 0;
193 ualg->cru_flags = alg->cra_flags;
194 ualg->cru_refcnt = refcount_read(&alg->cra_refcnt);
195
196 if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
197 goto nla_put_failure;
198 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000199 struct crypto_stat_larval rl;
Corentin Labbecac58182018-09-19 10:10:54 +0000200
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700201 memset(&rl, 0, sizeof(rl));
Eric Biggers37db69e2018-11-03 14:56:03 -0700202 strscpy(rl.type, "larval", sizeof(rl.type));
203 if (nla_put(skb, CRYPTOCFGA_STAT_LARVAL, sizeof(rl), &rl))
Corentin Labbecac58182018-09-19 10:10:54 +0000204 goto nla_put_failure;
205 goto out;
206 }
207
208 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
209 case CRYPTO_ALG_TYPE_AEAD:
210 if (crypto_report_aead(skb, alg))
211 goto nla_put_failure;
212 break;
213 case CRYPTO_ALG_TYPE_SKCIPHER:
214 if (crypto_report_cipher(skb, alg))
215 goto nla_put_failure;
216 break;
217 case CRYPTO_ALG_TYPE_BLKCIPHER:
218 if (crypto_report_cipher(skb, alg))
219 goto nla_put_failure;
220 break;
221 case CRYPTO_ALG_TYPE_CIPHER:
222 if (crypto_report_cipher(skb, alg))
223 goto nla_put_failure;
224 break;
225 case CRYPTO_ALG_TYPE_COMPRESS:
226 if (crypto_report_comp(skb, alg))
227 goto nla_put_failure;
228 break;
229 case CRYPTO_ALG_TYPE_ACOMPRESS:
230 if (crypto_report_acomp(skb, alg))
231 goto nla_put_failure;
232 break;
233 case CRYPTO_ALG_TYPE_SCOMPRESS:
234 if (crypto_report_acomp(skb, alg))
235 goto nla_put_failure;
236 break;
237 case CRYPTO_ALG_TYPE_AKCIPHER:
238 if (crypto_report_akcipher(skb, alg))
239 goto nla_put_failure;
240 break;
241 case CRYPTO_ALG_TYPE_KPP:
242 if (crypto_report_kpp(skb, alg))
243 goto nla_put_failure;
244 break;
245 case CRYPTO_ALG_TYPE_AHASH:
246 if (crypto_report_ahash(skb, alg))
247 goto nla_put_failure;
248 break;
249 case CRYPTO_ALG_TYPE_HASH:
250 if (crypto_report_shash(skb, alg))
251 goto nla_put_failure;
252 break;
253 case CRYPTO_ALG_TYPE_RNG:
254 if (crypto_report_rng(skb, alg))
255 goto nla_put_failure;
256 break;
257 default:
258 pr_err("ERROR: Unhandled alg %d in %s\n",
259 alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL),
260 __func__);
261 }
262
263out:
264 return 0;
265
266nla_put_failure:
267 return -EMSGSIZE;
268}
269
270static int crypto_reportstat_alg(struct crypto_alg *alg,
271 struct crypto_dump_info *info)
272{
273 struct sk_buff *in_skb = info->in_skb;
274 struct sk_buff *skb = info->out_skb;
275 struct nlmsghdr *nlh;
276 struct crypto_user_alg *ualg;
277 int err = 0;
278
279 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq,
280 CRYPTO_MSG_GETSTAT, sizeof(*ualg), info->nlmsg_flags);
281 if (!nlh) {
282 err = -EMSGSIZE;
283 goto out;
284 }
285
286 ualg = nlmsg_data(nlh);
287
288 err = crypto_reportstat_one(alg, ualg, skb);
289 if (err) {
290 nlmsg_cancel(skb, nlh);
291 goto out;
292 }
293
294 nlmsg_end(skb, nlh);
295
296out:
297 return err;
298}
299
300int crypto_reportstat(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
301 struct nlattr **attrs)
302{
303 struct crypto_user_alg *p = nlmsg_data(in_nlh);
304 struct crypto_alg *alg;
305 struct sk_buff *skb;
306 struct crypto_dump_info info;
307 int err;
308
309 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
310 return -EINVAL;
311
312 alg = crypto_alg_match(p, 0);
313 if (!alg)
314 return -ENOENT;
315
316 err = -ENOMEM;
317 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
318 if (!skb)
319 goto drop_alg;
320
321 info.in_skb = in_skb;
322 info.out_skb = skb;
323 info.nlmsg_seq = in_nlh->nlmsg_seq;
324 info.nlmsg_flags = 0;
325
326 err = crypto_reportstat_alg(alg, &info);
327
328drop_alg:
329 crypto_mod_put(alg);
330
331 if (err)
332 return err;
333
334 return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
335}
336
Corentin Labbecac58182018-09-19 10:10:54 +0000337MODULE_LICENSE("GPL");