Skip to content

Commit cac5818

Browse files
montjoieherbertx
authored andcommitted
crypto: user - Implement a generic crypto statistics
This patch implement a generic way to get statistics about all crypto usages. Signed-off-by: Corentin Labbe <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent a9cbfe4 commit cac5818

17 files changed

+970
-35
lines changed

crypto/Kconfig

+11
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,17 @@ config CRYPTO_USER_API_AEAD
17991799
This option enables the user-spaces interface for AEAD
18001800
cipher algorithms.
18011801

1802+
config CRYPTO_STATS
1803+
bool "Crypto usage statistics for User-space"
1804+
help
1805+
This option enables the gathering of crypto stats.
1806+
This will collect:
1807+
- encrypt/decrypt size and numbers of symmeric operations
1808+
- compress/decompress size and numbers of compress operations
1809+
- size and numbers of hash operations
1810+
- encrypt/decrypt/sign/verify numbers for asymmetric operations
1811+
- generate/seed numbers for rng operations
1812+
18021813
config CRYPTO_HASH_INFO
18031814
bool
18041815

crypto/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ cryptomgr-y := algboss.o testmgr.o
5454

5555
obj-$(CONFIG_CRYPTO_MANAGER2) += cryptomgr.o
5656
obj-$(CONFIG_CRYPTO_USER) += crypto_user.o
57+
crypto_user-y := crypto_user_base.o crypto_user_stat.o
5758
obj-$(CONFIG_CRYPTO_CMAC) += cmac.o
5859
obj-$(CONFIG_CRYPTO_HMAC) += hmac.o
5960
obj-$(CONFIG_CRYPTO_VMAC) += vmac.o

crypto/ahash.c

+16-5
Original file line numberDiff line numberDiff line change
@@ -364,24 +364,35 @@ static int crypto_ahash_op(struct ahash_request *req,
364364

365365
int crypto_ahash_final(struct ahash_request *req)
366366
{
367-
return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
367+
int ret;
368+
369+
ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
370+
crypto_stat_ahash_final(req, ret);
371+
return ret;
368372
}
369373
EXPORT_SYMBOL_GPL(crypto_ahash_final);
370374

371375
int crypto_ahash_finup(struct ahash_request *req)
372376
{
373-
return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
377+
int ret;
378+
379+
ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
380+
crypto_stat_ahash_final(req, ret);
381+
return ret;
374382
}
375383
EXPORT_SYMBOL_GPL(crypto_ahash_finup);
376384

377385
int crypto_ahash_digest(struct ahash_request *req)
378386
{
379387
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
388+
int ret;
380389

381390
if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
382-
return -ENOKEY;
383-
384-
return crypto_ahash_op(req, tfm->digest);
391+
ret = -ENOKEY;
392+
else
393+
ret = crypto_ahash_op(req, tfm->digest);
394+
crypto_stat_ahash_final(req, ret);
395+
return ret;
385396
}
386397
EXPORT_SYMBOL_GPL(crypto_ahash_digest);
387398

crypto/algapi.c

+8
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
258258
list_add(&alg->cra_list, &crypto_alg_list);
259259
list_add(&larval->alg.cra_list, &crypto_alg_list);
260260

261+
atomic_set(&alg->encrypt_cnt, 0);
262+
atomic_set(&alg->decrypt_cnt, 0);
263+
atomic64_set(&alg->encrypt_tlen, 0);
264+
atomic64_set(&alg->decrypt_tlen, 0);
265+
atomic_set(&alg->verify_cnt, 0);
266+
atomic_set(&alg->cipher_err_cnt, 0);
267+
atomic_set(&alg->sign_cnt, 0);
268+
261269
out:
262270
return larval;
263271

crypto/crypto_user.c crypto/crypto_user_base.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <crypto/internal/rng.h>
3030
#include <crypto/akcipher.h>
3131
#include <crypto/kpp.h>
32+
#include <crypto/internal/cryptouser.h>
3233

3334
#include "internal.h"
3435

@@ -37,7 +38,7 @@
3738
static DEFINE_MUTEX(crypto_cfg_mutex);
3839

3940
/* The crypto netlink socket */
40-
static struct sock *crypto_nlsk;
41+
struct sock *crypto_nlsk;
4142

4243
struct crypto_dump_info {
4344
struct sk_buff *in_skb;
@@ -46,7 +47,7 @@ struct crypto_dump_info {
4647
u16 nlmsg_flags;
4748
};
4849

49-
static struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
50+
struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
5051
{
5152
struct crypto_alg *q, *alg = NULL;
5253

@@ -461,6 +462,7 @@ static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
461462
[CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
462463
[CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
463464
[CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = 0,
465+
[CRYPTO_MSG_GETSTAT - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
464466
};
465467

466468
static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
@@ -481,6 +483,9 @@ static const struct crypto_link {
481483
.dump = crypto_dump_report,
482484
.done = crypto_dump_report_done},
483485
[CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = { .doit = crypto_del_rng },
486+
[CRYPTO_MSG_GETSTAT - CRYPTO_MSG_BASE] = { .doit = crypto_reportstat,
487+
.dump = crypto_dump_reportstat,
488+
.done = crypto_dump_reportstat_done},
484489
};
485490

486491
static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,

0 commit comments

Comments
 (0)