blob: 113bf16915603d572483e76ce5b6b8d32cbeee03 [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
25extern struct sock *crypto_nlsk;
26
27struct crypto_dump_info {
28 struct sk_buff *in_skb;
29 struct sk_buff *out_skb;
30 u32 nlmsg_seq;
31 u16 nlmsg_flags;
32};
33
34static int crypto_report_aead(struct sk_buff *skb, struct crypto_alg *alg)
35{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +000036 struct crypto_stat_aead raead;
Corentin Labbecac58182018-09-19 10:10:54 +000037
Corentin Labbe9f4debe2018-11-03 14:56:01 -070038 memset(&raead, 0, sizeof(raead));
39
Eric Biggers37db69e2018-11-03 14:56:03 -070040 strscpy(raead.type, "aead", sizeof(raead.type));
Corentin Labbecac58182018-09-19 10:10:54 +000041
Corentin Labbe17c18f92018-11-29 14:42:24 +000042 raead.stat_encrypt_cnt = atomic64_read(&alg->stats.aead.encrypt_cnt);
43 raead.stat_encrypt_tlen = atomic64_read(&alg->stats.aead.encrypt_tlen);
44 raead.stat_decrypt_cnt = atomic64_read(&alg->stats.aead.decrypt_cnt);
45 raead.stat_decrypt_tlen = atomic64_read(&alg->stats.aead.decrypt_tlen);
46 raead.stat_aead_err_cnt = atomic64_read(&alg->stats.aead.aead_err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +000047
Eric Biggers37db69e2018-11-03 14:56:03 -070048 return nla_put(skb, CRYPTOCFGA_STAT_AEAD, sizeof(raead), &raead);
Corentin Labbecac58182018-09-19 10:10:54 +000049}
50
51static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
52{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +000053 struct crypto_stat_cipher rcipher;
Corentin Labbecac58182018-09-19 10:10:54 +000054
Corentin Labbe9f4debe2018-11-03 14:56:01 -070055 memset(&rcipher, 0, sizeof(rcipher));
56
Eric Biggers37db69e2018-11-03 14:56:03 -070057 strscpy(rcipher.type, "cipher", sizeof(rcipher.type));
Corentin Labbecac58182018-09-19 10:10:54 +000058
Corentin Labbe17c18f92018-11-29 14:42:24 +000059 rcipher.stat_encrypt_cnt = atomic64_read(&alg->stats.cipher.encrypt_cnt);
60 rcipher.stat_encrypt_tlen = atomic64_read(&alg->stats.cipher.encrypt_tlen);
61 rcipher.stat_decrypt_cnt = atomic64_read(&alg->stats.cipher.decrypt_cnt);
62 rcipher.stat_decrypt_tlen = atomic64_read(&alg->stats.cipher.decrypt_tlen);
63 rcipher.stat_cipher_err_cnt = atomic64_read(&alg->stats.cipher.cipher_err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +000064
Eric Biggers37db69e2018-11-03 14:56:03 -070065 return nla_put(skb, CRYPTOCFGA_STAT_CIPHER, sizeof(rcipher), &rcipher);
Corentin Labbecac58182018-09-19 10:10:54 +000066}
67
68static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
69{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +000070 struct crypto_stat_compress rcomp;
Corentin Labbecac58182018-09-19 10:10:54 +000071
Corentin Labbe9f4debe2018-11-03 14:56:01 -070072 memset(&rcomp, 0, sizeof(rcomp));
73
Eric Biggers37db69e2018-11-03 14:56:03 -070074 strscpy(rcomp.type, "compression", sizeof(rcomp.type));
Corentin Labbe17c18f92018-11-29 14:42:24 +000075 rcomp.stat_compress_cnt = atomic64_read(&alg->stats.compress.compress_cnt);
76 rcomp.stat_compress_tlen = atomic64_read(&alg->stats.compress.compress_tlen);
77 rcomp.stat_decompress_cnt = atomic64_read(&alg->stats.compress.decompress_cnt);
78 rcomp.stat_decompress_tlen = atomic64_read(&alg->stats.compress.decompress_tlen);
79 rcomp.stat_compress_err_cnt = atomic64_read(&alg->stats.compress.compress_err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +000080
Eric Biggers37db69e2018-11-03 14:56:03 -070081 return nla_put(skb, CRYPTOCFGA_STAT_COMPRESS, sizeof(rcomp), &rcomp);
Corentin Labbecac58182018-09-19 10:10:54 +000082}
83
84static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg)
85{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +000086 struct crypto_stat_compress racomp;
Corentin Labbecac58182018-09-19 10:10:54 +000087
Corentin Labbe9f4debe2018-11-03 14:56:01 -070088 memset(&racomp, 0, sizeof(racomp));
89
Eric Biggers37db69e2018-11-03 14:56:03 -070090 strscpy(racomp.type, "acomp", sizeof(racomp.type));
Corentin Labbe17c18f92018-11-29 14:42:24 +000091 racomp.stat_compress_cnt = atomic64_read(&alg->stats.compress.compress_cnt);
92 racomp.stat_compress_tlen = atomic64_read(&alg->stats.compress.compress_tlen);
93 racomp.stat_decompress_cnt = atomic64_read(&alg->stats.compress.decompress_cnt);
94 racomp.stat_decompress_tlen = atomic64_read(&alg->stats.compress.decompress_tlen);
95 racomp.stat_compress_err_cnt = atomic64_read(&alg->stats.compress.compress_err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +000096
Eric Biggers37db69e2018-11-03 14:56:03 -070097 return nla_put(skb, CRYPTOCFGA_STAT_ACOMP, sizeof(racomp), &racomp);
Corentin Labbecac58182018-09-19 10:10:54 +000098}
99
100static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)
101{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000102 struct crypto_stat_akcipher rakcipher;
Corentin Labbecac58182018-09-19 10:10:54 +0000103
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700104 memset(&rakcipher, 0, sizeof(rakcipher));
105
Eric Biggers37db69e2018-11-03 14:56:03 -0700106 strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
Corentin Labbe17c18f92018-11-29 14:42:24 +0000107 rakcipher.stat_encrypt_cnt = atomic64_read(&alg->stats.akcipher.encrypt_cnt);
108 rakcipher.stat_encrypt_tlen = atomic64_read(&alg->stats.akcipher.encrypt_tlen);
109 rakcipher.stat_decrypt_cnt = atomic64_read(&alg->stats.akcipher.decrypt_cnt);
110 rakcipher.stat_decrypt_tlen = atomic64_read(&alg->stats.akcipher.decrypt_tlen);
111 rakcipher.stat_sign_cnt = atomic64_read(&alg->stats.akcipher.sign_cnt);
112 rakcipher.stat_verify_cnt = atomic64_read(&alg->stats.akcipher.verify_cnt);
113 rakcipher.stat_akcipher_err_cnt = atomic64_read(&alg->stats.akcipher.akcipher_err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +0000114
Eric Biggers37db69e2018-11-03 14:56:03 -0700115 return nla_put(skb, CRYPTOCFGA_STAT_AKCIPHER,
116 sizeof(rakcipher), &rakcipher);
Corentin Labbecac58182018-09-19 10:10:54 +0000117}
118
119static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)
120{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000121 struct crypto_stat_kpp rkpp;
Corentin Labbecac58182018-09-19 10:10:54 +0000122
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700123 memset(&rkpp, 0, sizeof(rkpp));
124
Eric Biggers37db69e2018-11-03 14:56:03 -0700125 strscpy(rkpp.type, "kpp", sizeof(rkpp.type));
Corentin Labbecac58182018-09-19 10:10:54 +0000126
Corentin Labbe17c18f92018-11-29 14:42:24 +0000127 rkpp.stat_setsecret_cnt = atomic64_read(&alg->stats.kpp.setsecret_cnt);
128 rkpp.stat_generate_public_key_cnt = atomic64_read(&alg->stats.kpp.generate_public_key_cnt);
129 rkpp.stat_compute_shared_secret_cnt = atomic64_read(&alg->stats.kpp.compute_shared_secret_cnt);
130 rkpp.stat_kpp_err_cnt = atomic64_read(&alg->stats.kpp.kpp_err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +0000131
Eric Biggers37db69e2018-11-03 14:56:03 -0700132 return nla_put(skb, CRYPTOCFGA_STAT_KPP, sizeof(rkpp), &rkpp);
Corentin Labbecac58182018-09-19 10:10:54 +0000133}
134
135static int crypto_report_ahash(struct sk_buff *skb, struct crypto_alg *alg)
136{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000137 struct crypto_stat_hash rhash;
Corentin Labbecac58182018-09-19 10:10:54 +0000138
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700139 memset(&rhash, 0, sizeof(rhash));
140
Eric Biggers37db69e2018-11-03 14:56:03 -0700141 strscpy(rhash.type, "ahash", sizeof(rhash.type));
Corentin Labbecac58182018-09-19 10:10:54 +0000142
Corentin Labbe17c18f92018-11-29 14:42:24 +0000143 rhash.stat_hash_cnt = atomic64_read(&alg->stats.hash.hash_cnt);
144 rhash.stat_hash_tlen = atomic64_read(&alg->stats.hash.hash_tlen);
145 rhash.stat_hash_err_cnt = atomic64_read(&alg->stats.hash.hash_err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +0000146
Eric Biggers37db69e2018-11-03 14:56:03 -0700147 return nla_put(skb, CRYPTOCFGA_STAT_HASH, sizeof(rhash), &rhash);
Corentin Labbecac58182018-09-19 10:10:54 +0000148}
149
150static int crypto_report_shash(struct sk_buff *skb, struct crypto_alg *alg)
151{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000152 struct crypto_stat_hash rhash;
Corentin Labbecac58182018-09-19 10:10:54 +0000153
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700154 memset(&rhash, 0, sizeof(rhash));
155
Eric Biggers37db69e2018-11-03 14:56:03 -0700156 strscpy(rhash.type, "shash", sizeof(rhash.type));
Corentin Labbecac58182018-09-19 10:10:54 +0000157
Corentin Labbe17c18f92018-11-29 14:42:24 +0000158 rhash.stat_hash_cnt = atomic64_read(&alg->stats.hash.hash_cnt);
159 rhash.stat_hash_tlen = atomic64_read(&alg->stats.hash.hash_tlen);
160 rhash.stat_hash_err_cnt = atomic64_read(&alg->stats.hash.hash_err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +0000161
Eric Biggers37db69e2018-11-03 14:56:03 -0700162 return nla_put(skb, CRYPTOCFGA_STAT_HASH, sizeof(rhash), &rhash);
Corentin Labbecac58182018-09-19 10:10:54 +0000163}
164
165static int crypto_report_rng(struct sk_buff *skb, struct crypto_alg *alg)
166{
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000167 struct crypto_stat_rng rrng;
Corentin Labbecac58182018-09-19 10:10:54 +0000168
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700169 memset(&rrng, 0, sizeof(rrng));
170
Eric Biggers37db69e2018-11-03 14:56:03 -0700171 strscpy(rrng.type, "rng", sizeof(rrng.type));
Corentin Labbecac58182018-09-19 10:10:54 +0000172
Corentin Labbe17c18f92018-11-29 14:42:24 +0000173 rrng.stat_generate_cnt = atomic64_read(&alg->stats.rng.generate_cnt);
174 rrng.stat_generate_tlen = atomic64_read(&alg->stats.rng.generate_tlen);
175 rrng.stat_seed_cnt = atomic64_read(&alg->stats.rng.seed_cnt);
176 rrng.stat_rng_err_cnt = atomic64_read(&alg->stats.rng.rng_err_cnt);
Corentin Labbecac58182018-09-19 10:10:54 +0000177
Eric Biggers37db69e2018-11-03 14:56:03 -0700178 return nla_put(skb, CRYPTOCFGA_STAT_RNG, sizeof(rrng), &rrng);
Corentin Labbecac58182018-09-19 10:10:54 +0000179}
180
181static int crypto_reportstat_one(struct crypto_alg *alg,
182 struct crypto_user_alg *ualg,
183 struct sk_buff *skb)
184{
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700185 memset(ualg, 0, sizeof(*ualg));
186
Eric Biggers37db69e2018-11-03 14:56:03 -0700187 strscpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
188 strscpy(ualg->cru_driver_name, alg->cra_driver_name,
Corentin Labbecac58182018-09-19 10:10:54 +0000189 sizeof(ualg->cru_driver_name));
Eric Biggers37db69e2018-11-03 14:56:03 -0700190 strscpy(ualg->cru_module_name, module_name(alg->cra_module),
Corentin Labbecac58182018-09-19 10:10:54 +0000191 sizeof(ualg->cru_module_name));
192
193 ualg->cru_type = 0;
194 ualg->cru_mask = 0;
195 ualg->cru_flags = alg->cra_flags;
196 ualg->cru_refcnt = refcount_read(&alg->cra_refcnt);
197
198 if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
199 goto nla_put_failure;
200 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
Corentin Labbe7f0a9d52018-11-29 14:42:19 +0000201 struct crypto_stat_larval rl;
Corentin Labbecac58182018-09-19 10:10:54 +0000202
Corentin Labbe9f4debe2018-11-03 14:56:01 -0700203 memset(&rl, 0, sizeof(rl));
Eric Biggers37db69e2018-11-03 14:56:03 -0700204 strscpy(rl.type, "larval", sizeof(rl.type));
205 if (nla_put(skb, CRYPTOCFGA_STAT_LARVAL, sizeof(rl), &rl))
Corentin Labbecac58182018-09-19 10:10:54 +0000206 goto nla_put_failure;
207 goto out;
208 }
209
210 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
211 case CRYPTO_ALG_TYPE_AEAD:
212 if (crypto_report_aead(skb, alg))
213 goto nla_put_failure;
214 break;
215 case CRYPTO_ALG_TYPE_SKCIPHER:
216 if (crypto_report_cipher(skb, alg))
217 goto nla_put_failure;
218 break;
219 case CRYPTO_ALG_TYPE_BLKCIPHER:
220 if (crypto_report_cipher(skb, alg))
221 goto nla_put_failure;
222 break;
223 case CRYPTO_ALG_TYPE_CIPHER:
224 if (crypto_report_cipher(skb, alg))
225 goto nla_put_failure;
226 break;
227 case CRYPTO_ALG_TYPE_COMPRESS:
228 if (crypto_report_comp(skb, alg))
229 goto nla_put_failure;
230 break;
231 case CRYPTO_ALG_TYPE_ACOMPRESS:
232 if (crypto_report_acomp(skb, alg))
233 goto nla_put_failure;
234 break;
235 case CRYPTO_ALG_TYPE_SCOMPRESS:
236 if (crypto_report_acomp(skb, alg))
237 goto nla_put_failure;
238 break;
239 case CRYPTO_ALG_TYPE_AKCIPHER:
240 if (crypto_report_akcipher(skb, alg))
241 goto nla_put_failure;
242 break;
243 case CRYPTO_ALG_TYPE_KPP:
244 if (crypto_report_kpp(skb, alg))
245 goto nla_put_failure;
246 break;
247 case CRYPTO_ALG_TYPE_AHASH:
248 if (crypto_report_ahash(skb, alg))
249 goto nla_put_failure;
250 break;
251 case CRYPTO_ALG_TYPE_HASH:
252 if (crypto_report_shash(skb, alg))
253 goto nla_put_failure;
254 break;
255 case CRYPTO_ALG_TYPE_RNG:
256 if (crypto_report_rng(skb, alg))
257 goto nla_put_failure;
258 break;
259 default:
260 pr_err("ERROR: Unhandled alg %d in %s\n",
261 alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL),
262 __func__);
263 }
264
265out:
266 return 0;
267
268nla_put_failure:
269 return -EMSGSIZE;
270}
271
272static int crypto_reportstat_alg(struct crypto_alg *alg,
273 struct crypto_dump_info *info)
274{
275 struct sk_buff *in_skb = info->in_skb;
276 struct sk_buff *skb = info->out_skb;
277 struct nlmsghdr *nlh;
278 struct crypto_user_alg *ualg;
279 int err = 0;
280
281 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq,
282 CRYPTO_MSG_GETSTAT, sizeof(*ualg), info->nlmsg_flags);
283 if (!nlh) {
284 err = -EMSGSIZE;
285 goto out;
286 }
287
288 ualg = nlmsg_data(nlh);
289
290 err = crypto_reportstat_one(alg, ualg, skb);
291 if (err) {
292 nlmsg_cancel(skb, nlh);
293 goto out;
294 }
295
296 nlmsg_end(skb, nlh);
297
298out:
299 return err;
300}
301
302int crypto_reportstat(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
303 struct nlattr **attrs)
304{
305 struct crypto_user_alg *p = nlmsg_data(in_nlh);
306 struct crypto_alg *alg;
307 struct sk_buff *skb;
308 struct crypto_dump_info info;
309 int err;
310
311 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
312 return -EINVAL;
313
314 alg = crypto_alg_match(p, 0);
315 if (!alg)
316 return -ENOENT;
317
318 err = -ENOMEM;
319 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
320 if (!skb)
321 goto drop_alg;
322
323 info.in_skb = in_skb;
324 info.out_skb = skb;
325 info.nlmsg_seq = in_nlh->nlmsg_seq;
326 info.nlmsg_flags = 0;
327
328 err = crypto_reportstat_alg(alg, &info);
329
330drop_alg:
331 crypto_mod_put(alg);
332
333 if (err)
334 return err;
335
336 return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
337}
338
339int crypto_dump_reportstat(struct sk_buff *skb, struct netlink_callback *cb)
340{
341 struct crypto_alg *alg;
342 struct crypto_dump_info info;
343 int err;
344
345 if (cb->args[0])
346 goto out;
347
348 cb->args[0] = 1;
349
350 info.in_skb = cb->skb;
351 info.out_skb = skb;
352 info.nlmsg_seq = cb->nlh->nlmsg_seq;
353 info.nlmsg_flags = NLM_F_MULTI;
354
355 list_for_each_entry(alg, &crypto_alg_list, cra_list) {
356 err = crypto_reportstat_alg(alg, &info);
357 if (err)
358 goto out_err;
359 }
360
361out:
362 return skb->len;
363out_err:
364 return err;
365}
366
367int crypto_dump_reportstat_done(struct netlink_callback *cb)
368{
369 return 0;
370}
371
372MODULE_LICENSE("GPL");