1 // SPDX-License-Identifier: GPL-2.0
3 * Crypto user configuration API.
5 * Copyright (C) 2017-2018 Corentin Labbe <clabbe@baylibre.com>
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>
21 #define null_terminated(x) (strnlen(x, sizeof(x)) < sizeof(x))
23 static DEFINE_MUTEX(crypto_cfg_mutex
);
25 extern struct sock
*crypto_nlsk
;
27 struct crypto_dump_info
{
28 struct sk_buff
*in_skb
;
29 struct sk_buff
*out_skb
;
34 static int crypto_report_aead(struct sk_buff
*skb
, struct crypto_alg
*alg
)
36 struct crypto_stat raead
;
40 memset(&raead
, 0, sizeof(raead
));
42 strncpy(raead
.type
, "aead", sizeof(raead
.type
));
44 v32
= atomic_read(&alg
->encrypt_cnt
);
45 raead
.stat_encrypt_cnt
= v32
;
46 v64
= atomic64_read(&alg
->encrypt_tlen
);
47 raead
.stat_encrypt_tlen
= v64
;
48 v32
= atomic_read(&alg
->decrypt_cnt
);
49 raead
.stat_decrypt_cnt
= v32
;
50 v64
= atomic64_read(&alg
->decrypt_tlen
);
51 raead
.stat_decrypt_tlen
= v64
;
52 v32
= atomic_read(&alg
->aead_err_cnt
);
53 raead
.stat_aead_err_cnt
= v32
;
55 if (nla_put(skb
, CRYPTOCFGA_STAT_AEAD
,
56 sizeof(struct crypto_stat
), &raead
))
64 static int crypto_report_cipher(struct sk_buff
*skb
, struct crypto_alg
*alg
)
66 struct crypto_stat rcipher
;
70 memset(&rcipher
, 0, sizeof(rcipher
));
72 strlcpy(rcipher
.type
, "cipher", sizeof(rcipher
.type
));
74 v32
= atomic_read(&alg
->encrypt_cnt
);
75 rcipher
.stat_encrypt_cnt
= v32
;
76 v64
= atomic64_read(&alg
->encrypt_tlen
);
77 rcipher
.stat_encrypt_tlen
= v64
;
78 v32
= atomic_read(&alg
->decrypt_cnt
);
79 rcipher
.stat_decrypt_cnt
= v32
;
80 v64
= atomic64_read(&alg
->decrypt_tlen
);
81 rcipher
.stat_decrypt_tlen
= v64
;
82 v32
= atomic_read(&alg
->cipher_err_cnt
);
83 rcipher
.stat_cipher_err_cnt
= v32
;
85 if (nla_put(skb
, CRYPTOCFGA_STAT_CIPHER
,
86 sizeof(struct crypto_stat
), &rcipher
))
94 static int crypto_report_comp(struct sk_buff
*skb
, struct crypto_alg
*alg
)
96 struct crypto_stat rcomp
;
100 memset(&rcomp
, 0, sizeof(rcomp
));
102 strlcpy(rcomp
.type
, "compression", sizeof(rcomp
.type
));
103 v32
= atomic_read(&alg
->compress_cnt
);
104 rcomp
.stat_compress_cnt
= v32
;
105 v64
= atomic64_read(&alg
->compress_tlen
);
106 rcomp
.stat_compress_tlen
= v64
;
107 v32
= atomic_read(&alg
->decompress_cnt
);
108 rcomp
.stat_decompress_cnt
= v32
;
109 v64
= atomic64_read(&alg
->decompress_tlen
);
110 rcomp
.stat_decompress_tlen
= v64
;
111 v32
= atomic_read(&alg
->cipher_err_cnt
);
112 rcomp
.stat_compress_err_cnt
= v32
;
114 if (nla_put(skb
, CRYPTOCFGA_STAT_COMPRESS
,
115 sizeof(struct crypto_stat
), &rcomp
))
116 goto nla_put_failure
;
123 static int crypto_report_acomp(struct sk_buff
*skb
, struct crypto_alg
*alg
)
125 struct crypto_stat racomp
;
129 memset(&racomp
, 0, sizeof(racomp
));
131 strlcpy(racomp
.type
, "acomp", sizeof(racomp
.type
));
132 v32
= atomic_read(&alg
->compress_cnt
);
133 racomp
.stat_compress_cnt
= v32
;
134 v64
= atomic64_read(&alg
->compress_tlen
);
135 racomp
.stat_compress_tlen
= v64
;
136 v32
= atomic_read(&alg
->decompress_cnt
);
137 racomp
.stat_decompress_cnt
= v32
;
138 v64
= atomic64_read(&alg
->decompress_tlen
);
139 racomp
.stat_decompress_tlen
= v64
;
140 v32
= atomic_read(&alg
->cipher_err_cnt
);
141 racomp
.stat_compress_err_cnt
= v32
;
143 if (nla_put(skb
, CRYPTOCFGA_STAT_ACOMP
,
144 sizeof(struct crypto_stat
), &racomp
))
145 goto nla_put_failure
;
152 static int crypto_report_akcipher(struct sk_buff
*skb
, struct crypto_alg
*alg
)
154 struct crypto_stat rakcipher
;
158 memset(&rakcipher
, 0, sizeof(rakcipher
));
160 strncpy(rakcipher
.type
, "akcipher", sizeof(rakcipher
.type
));
161 v32
= atomic_read(&alg
->encrypt_cnt
);
162 rakcipher
.stat_encrypt_cnt
= v32
;
163 v64
= atomic64_read(&alg
->encrypt_tlen
);
164 rakcipher
.stat_encrypt_tlen
= v64
;
165 v32
= atomic_read(&alg
->decrypt_cnt
);
166 rakcipher
.stat_decrypt_cnt
= v32
;
167 v64
= atomic64_read(&alg
->decrypt_tlen
);
168 rakcipher
.stat_decrypt_tlen
= v64
;
169 v32
= atomic_read(&alg
->sign_cnt
);
170 rakcipher
.stat_sign_cnt
= v32
;
171 v32
= atomic_read(&alg
->verify_cnt
);
172 rakcipher
.stat_verify_cnt
= v32
;
173 v32
= atomic_read(&alg
->akcipher_err_cnt
);
174 rakcipher
.stat_akcipher_err_cnt
= v32
;
176 if (nla_put(skb
, CRYPTOCFGA_STAT_AKCIPHER
,
177 sizeof(struct crypto_stat
), &rakcipher
))
178 goto nla_put_failure
;
185 static int crypto_report_kpp(struct sk_buff
*skb
, struct crypto_alg
*alg
)
187 struct crypto_stat rkpp
;
190 memset(&rkpp
, 0, sizeof(rkpp
));
192 strlcpy(rkpp
.type
, "kpp", sizeof(rkpp
.type
));
194 v
= atomic_read(&alg
->setsecret_cnt
);
195 rkpp
.stat_setsecret_cnt
= v
;
196 v
= atomic_read(&alg
->generate_public_key_cnt
);
197 rkpp
.stat_generate_public_key_cnt
= v
;
198 v
= atomic_read(&alg
->compute_shared_secret_cnt
);
199 rkpp
.stat_compute_shared_secret_cnt
= v
;
200 v
= atomic_read(&alg
->kpp_err_cnt
);
201 rkpp
.stat_kpp_err_cnt
= v
;
203 if (nla_put(skb
, CRYPTOCFGA_STAT_KPP
,
204 sizeof(struct crypto_stat
), &rkpp
))
205 goto nla_put_failure
;
212 static int crypto_report_ahash(struct sk_buff
*skb
, struct crypto_alg
*alg
)
214 struct crypto_stat rhash
;
218 memset(&rhash
, 0, sizeof(rhash
));
220 strncpy(rhash
.type
, "ahash", sizeof(rhash
.type
));
222 v32
= atomic_read(&alg
->hash_cnt
);
223 rhash
.stat_hash_cnt
= v32
;
224 v64
= atomic64_read(&alg
->hash_tlen
);
225 rhash
.stat_hash_tlen
= v64
;
226 v32
= atomic_read(&alg
->hash_err_cnt
);
227 rhash
.stat_hash_err_cnt
= v32
;
229 if (nla_put(skb
, CRYPTOCFGA_STAT_HASH
,
230 sizeof(struct crypto_stat
), &rhash
))
231 goto nla_put_failure
;
238 static int crypto_report_shash(struct sk_buff
*skb
, struct crypto_alg
*alg
)
240 struct crypto_stat rhash
;
244 memset(&rhash
, 0, sizeof(rhash
));
246 strncpy(rhash
.type
, "shash", sizeof(rhash
.type
));
248 v32
= atomic_read(&alg
->hash_cnt
);
249 rhash
.stat_hash_cnt
= v32
;
250 v64
= atomic64_read(&alg
->hash_tlen
);
251 rhash
.stat_hash_tlen
= v64
;
252 v32
= atomic_read(&alg
->hash_err_cnt
);
253 rhash
.stat_hash_err_cnt
= v32
;
255 if (nla_put(skb
, CRYPTOCFGA_STAT_HASH
,
256 sizeof(struct crypto_stat
), &rhash
))
257 goto nla_put_failure
;
264 static int crypto_report_rng(struct sk_buff
*skb
, struct crypto_alg
*alg
)
266 struct crypto_stat rrng
;
270 memset(&rrng
, 0, sizeof(rrng
));
272 strncpy(rrng
.type
, "rng", sizeof(rrng
.type
));
274 v32
= atomic_read(&alg
->generate_cnt
);
275 rrng
.stat_generate_cnt
= v32
;
276 v64
= atomic64_read(&alg
->generate_tlen
);
277 rrng
.stat_generate_tlen
= v64
;
278 v32
= atomic_read(&alg
->seed_cnt
);
279 rrng
.stat_seed_cnt
= v32
;
280 v32
= atomic_read(&alg
->hash_err_cnt
);
281 rrng
.stat_rng_err_cnt
= v32
;
283 if (nla_put(skb
, CRYPTOCFGA_STAT_RNG
,
284 sizeof(struct crypto_stat
), &rrng
))
285 goto nla_put_failure
;
292 static int crypto_reportstat_one(struct crypto_alg
*alg
,
293 struct crypto_user_alg
*ualg
,
296 memset(ualg
, 0, sizeof(*ualg
));
298 strlcpy(ualg
->cru_name
, alg
->cra_name
, sizeof(ualg
->cru_name
));
299 strlcpy(ualg
->cru_driver_name
, alg
->cra_driver_name
,
300 sizeof(ualg
->cru_driver_name
));
301 strlcpy(ualg
->cru_module_name
, module_name(alg
->cra_module
),
302 sizeof(ualg
->cru_module_name
));
306 ualg
->cru_flags
= alg
->cra_flags
;
307 ualg
->cru_refcnt
= refcount_read(&alg
->cra_refcnt
);
309 if (nla_put_u32(skb
, CRYPTOCFGA_PRIORITY_VAL
, alg
->cra_priority
))
310 goto nla_put_failure
;
311 if (alg
->cra_flags
& CRYPTO_ALG_LARVAL
) {
312 struct crypto_stat rl
;
314 memset(&rl
, 0, sizeof(rl
));
315 strlcpy(rl
.type
, "larval", sizeof(rl
.type
));
316 if (nla_put(skb
, CRYPTOCFGA_STAT_LARVAL
,
317 sizeof(struct crypto_stat
), &rl
))
318 goto nla_put_failure
;
322 switch (alg
->cra_flags
& (CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_LARVAL
)) {
323 case CRYPTO_ALG_TYPE_AEAD
:
324 if (crypto_report_aead(skb
, alg
))
325 goto nla_put_failure
;
327 case CRYPTO_ALG_TYPE_SKCIPHER
:
328 if (crypto_report_cipher(skb
, alg
))
329 goto nla_put_failure
;
331 case CRYPTO_ALG_TYPE_BLKCIPHER
:
332 if (crypto_report_cipher(skb
, alg
))
333 goto nla_put_failure
;
335 case CRYPTO_ALG_TYPE_CIPHER
:
336 if (crypto_report_cipher(skb
, alg
))
337 goto nla_put_failure
;
339 case CRYPTO_ALG_TYPE_COMPRESS
:
340 if (crypto_report_comp(skb
, alg
))
341 goto nla_put_failure
;
343 case CRYPTO_ALG_TYPE_ACOMPRESS
:
344 if (crypto_report_acomp(skb
, alg
))
345 goto nla_put_failure
;
347 case CRYPTO_ALG_TYPE_SCOMPRESS
:
348 if (crypto_report_acomp(skb
, alg
))
349 goto nla_put_failure
;
351 case CRYPTO_ALG_TYPE_AKCIPHER
:
352 if (crypto_report_akcipher(skb
, alg
))
353 goto nla_put_failure
;
355 case CRYPTO_ALG_TYPE_KPP
:
356 if (crypto_report_kpp(skb
, alg
))
357 goto nla_put_failure
;
359 case CRYPTO_ALG_TYPE_AHASH
:
360 if (crypto_report_ahash(skb
, alg
))
361 goto nla_put_failure
;
363 case CRYPTO_ALG_TYPE_HASH
:
364 if (crypto_report_shash(skb
, alg
))
365 goto nla_put_failure
;
367 case CRYPTO_ALG_TYPE_RNG
:
368 if (crypto_report_rng(skb
, alg
))
369 goto nla_put_failure
;
372 pr_err("ERROR: Unhandled alg %d in %s\n",
373 alg
->cra_flags
& (CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_LARVAL
),
384 static int crypto_reportstat_alg(struct crypto_alg
*alg
,
385 struct crypto_dump_info
*info
)
387 struct sk_buff
*in_skb
= info
->in_skb
;
388 struct sk_buff
*skb
= info
->out_skb
;
389 struct nlmsghdr
*nlh
;
390 struct crypto_user_alg
*ualg
;
393 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, info
->nlmsg_seq
,
394 CRYPTO_MSG_GETSTAT
, sizeof(*ualg
), info
->nlmsg_flags
);
400 ualg
= nlmsg_data(nlh
);
402 err
= crypto_reportstat_one(alg
, ualg
, skb
);
404 nlmsg_cancel(skb
, nlh
);
414 int crypto_reportstat(struct sk_buff
*in_skb
, struct nlmsghdr
*in_nlh
,
415 struct nlattr
**attrs
)
417 struct crypto_user_alg
*p
= nlmsg_data(in_nlh
);
418 struct crypto_alg
*alg
;
420 struct crypto_dump_info info
;
423 if (!null_terminated(p
->cru_name
) || !null_terminated(p
->cru_driver_name
))
426 alg
= crypto_alg_match(p
, 0);
431 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
435 info
.in_skb
= in_skb
;
437 info
.nlmsg_seq
= in_nlh
->nlmsg_seq
;
438 info
.nlmsg_flags
= 0;
440 err
= crypto_reportstat_alg(alg
, &info
);
448 return nlmsg_unicast(crypto_nlsk
, skb
, NETLINK_CB(in_skb
).portid
);
451 int crypto_dump_reportstat(struct sk_buff
*skb
, struct netlink_callback
*cb
)
453 struct crypto_alg
*alg
;
454 struct crypto_dump_info info
;
462 info
.in_skb
= cb
->skb
;
464 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
465 info
.nlmsg_flags
= NLM_F_MULTI
;
467 list_for_each_entry(alg
, &crypto_alg_list
, cra_list
) {
468 err
= crypto_reportstat_alg(alg
, &info
);
479 int crypto_dump_reportstat_done(struct netlink_callback
*cb
)
484 MODULE_LICENSE("GPL");