Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 1 | // 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 | |
| 23 | static DEFINE_MUTEX(crypto_cfg_mutex); |
| 24 | |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 25 | struct 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 | |
| 32 | static int crypto_report_aead(struct sk_buff *skb, struct crypto_alg *alg) |
| 33 | { |
Corentin Labbe | 7f0a9d5 | 2018-11-29 14:42:19 +0000 | [diff] [blame] | 34 | struct crypto_stat_aead raead; |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 35 | |
Corentin Labbe | 9f4debe | 2018-11-03 14:56:01 -0700 | [diff] [blame] | 36 | memset(&raead, 0, sizeof(raead)); |
| 37 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 38 | strscpy(raead.type, "aead", sizeof(raead.type)); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 39 | |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 40 | 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 Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 44 | raead.stat_err_cnt = atomic64_read(&alg->stats.aead.err_cnt); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 45 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 46 | return nla_put(skb, CRYPTOCFGA_STAT_AEAD, sizeof(raead), &raead); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg) |
| 50 | { |
Corentin Labbe | 7f0a9d5 | 2018-11-29 14:42:19 +0000 | [diff] [blame] | 51 | struct crypto_stat_cipher rcipher; |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 52 | |
Corentin Labbe | 9f4debe | 2018-11-03 14:56:01 -0700 | [diff] [blame] | 53 | memset(&rcipher, 0, sizeof(rcipher)); |
| 54 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 55 | strscpy(rcipher.type, "cipher", sizeof(rcipher.type)); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 56 | |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 57 | 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 Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 61 | rcipher.stat_err_cnt = atomic64_read(&alg->stats.cipher.err_cnt); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 62 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 63 | return nla_put(skb, CRYPTOCFGA_STAT_CIPHER, sizeof(rcipher), &rcipher); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg) |
| 67 | { |
Corentin Labbe | 7f0a9d5 | 2018-11-29 14:42:19 +0000 | [diff] [blame] | 68 | struct crypto_stat_compress rcomp; |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 69 | |
Corentin Labbe | 9f4debe | 2018-11-03 14:56:01 -0700 | [diff] [blame] | 70 | memset(&rcomp, 0, sizeof(rcomp)); |
| 71 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 72 | strscpy(rcomp.type, "compression", sizeof(rcomp.type)); |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 73 | 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 Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 77 | rcomp.stat_err_cnt = atomic64_read(&alg->stats.compress.err_cnt); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 78 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 79 | return nla_put(skb, CRYPTOCFGA_STAT_COMPRESS, sizeof(rcomp), &rcomp); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg) |
| 83 | { |
Corentin Labbe | 7f0a9d5 | 2018-11-29 14:42:19 +0000 | [diff] [blame] | 84 | struct crypto_stat_compress racomp; |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 85 | |
Corentin Labbe | 9f4debe | 2018-11-03 14:56:01 -0700 | [diff] [blame] | 86 | memset(&racomp, 0, sizeof(racomp)); |
| 87 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 88 | strscpy(racomp.type, "acomp", sizeof(racomp.type)); |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 89 | 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 Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 93 | racomp.stat_err_cnt = atomic64_read(&alg->stats.compress.err_cnt); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 94 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 95 | return nla_put(skb, CRYPTOCFGA_STAT_ACOMP, sizeof(racomp), &racomp); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg) |
| 99 | { |
Corentin Labbe | 7f0a9d5 | 2018-11-29 14:42:19 +0000 | [diff] [blame] | 100 | struct crypto_stat_akcipher rakcipher; |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 101 | |
Corentin Labbe | 9f4debe | 2018-11-03 14:56:01 -0700 | [diff] [blame] | 102 | memset(&rakcipher, 0, sizeof(rakcipher)); |
| 103 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 104 | strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type)); |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 105 | 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 Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 111 | rakcipher.stat_err_cnt = atomic64_read(&alg->stats.akcipher.err_cnt); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 112 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 113 | return nla_put(skb, CRYPTOCFGA_STAT_AKCIPHER, |
| 114 | sizeof(rakcipher), &rakcipher); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg) |
| 118 | { |
Corentin Labbe | 7f0a9d5 | 2018-11-29 14:42:19 +0000 | [diff] [blame] | 119 | struct crypto_stat_kpp rkpp; |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 120 | |
Corentin Labbe | 9f4debe | 2018-11-03 14:56:01 -0700 | [diff] [blame] | 121 | memset(&rkpp, 0, sizeof(rkpp)); |
| 122 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 123 | strscpy(rkpp.type, "kpp", sizeof(rkpp.type)); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 124 | |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 125 | 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 Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 128 | rkpp.stat_err_cnt = atomic64_read(&alg->stats.kpp.err_cnt); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 129 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 130 | return nla_put(skb, CRYPTOCFGA_STAT_KPP, sizeof(rkpp), &rkpp); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static int crypto_report_ahash(struct sk_buff *skb, struct crypto_alg *alg) |
| 134 | { |
Corentin Labbe | 7f0a9d5 | 2018-11-29 14:42:19 +0000 | [diff] [blame] | 135 | struct crypto_stat_hash rhash; |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 136 | |
Corentin Labbe | 9f4debe | 2018-11-03 14:56:01 -0700 | [diff] [blame] | 137 | memset(&rhash, 0, sizeof(rhash)); |
| 138 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 139 | strscpy(rhash.type, "ahash", sizeof(rhash.type)); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 140 | |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 141 | rhash.stat_hash_cnt = atomic64_read(&alg->stats.hash.hash_cnt); |
| 142 | rhash.stat_hash_tlen = atomic64_read(&alg->stats.hash.hash_tlen); |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 143 | rhash.stat_err_cnt = atomic64_read(&alg->stats.hash.err_cnt); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 144 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 145 | return nla_put(skb, CRYPTOCFGA_STAT_HASH, sizeof(rhash), &rhash); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static int crypto_report_shash(struct sk_buff *skb, struct crypto_alg *alg) |
| 149 | { |
Corentin Labbe | 7f0a9d5 | 2018-11-29 14:42:19 +0000 | [diff] [blame] | 150 | struct crypto_stat_hash rhash; |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 151 | |
Corentin Labbe | 9f4debe | 2018-11-03 14:56:01 -0700 | [diff] [blame] | 152 | memset(&rhash, 0, sizeof(rhash)); |
| 153 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 154 | strscpy(rhash.type, "shash", sizeof(rhash.type)); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 155 | |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 156 | rhash.stat_hash_cnt = atomic64_read(&alg->stats.hash.hash_cnt); |
| 157 | rhash.stat_hash_tlen = atomic64_read(&alg->stats.hash.hash_tlen); |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 158 | rhash.stat_err_cnt = atomic64_read(&alg->stats.hash.err_cnt); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 159 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 160 | return nla_put(skb, CRYPTOCFGA_STAT_HASH, sizeof(rhash), &rhash); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static int crypto_report_rng(struct sk_buff *skb, struct crypto_alg *alg) |
| 164 | { |
Corentin Labbe | 7f0a9d5 | 2018-11-29 14:42:19 +0000 | [diff] [blame] | 165 | struct crypto_stat_rng rrng; |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 166 | |
Corentin Labbe | 9f4debe | 2018-11-03 14:56:01 -0700 | [diff] [blame] | 167 | memset(&rrng, 0, sizeof(rrng)); |
| 168 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 169 | strscpy(rrng.type, "rng", sizeof(rrng.type)); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 170 | |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 171 | 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 Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 174 | rrng.stat_err_cnt = atomic64_read(&alg->stats.rng.err_cnt); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 175 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 176 | return nla_put(skb, CRYPTOCFGA_STAT_RNG, sizeof(rrng), &rrng); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static int crypto_reportstat_one(struct crypto_alg *alg, |
| 180 | struct crypto_user_alg *ualg, |
| 181 | struct sk_buff *skb) |
| 182 | { |
Corentin Labbe | 9f4debe | 2018-11-03 14:56:01 -0700 | [diff] [blame] | 183 | memset(ualg, 0, sizeof(*ualg)); |
| 184 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 185 | strscpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name)); |
| 186 | strscpy(ualg->cru_driver_name, alg->cra_driver_name, |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 187 | sizeof(ualg->cru_driver_name)); |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 188 | strscpy(ualg->cru_module_name, module_name(alg->cra_module), |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 189 | 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 Labbe | 7f0a9d5 | 2018-11-29 14:42:19 +0000 | [diff] [blame] | 199 | struct crypto_stat_larval rl; |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 200 | |
Corentin Labbe | 9f4debe | 2018-11-03 14:56:01 -0700 | [diff] [blame] | 201 | memset(&rl, 0, sizeof(rl)); |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 202 | strscpy(rl.type, "larval", sizeof(rl.type)); |
| 203 | if (nla_put(skb, CRYPTOCFGA_STAT_LARVAL, sizeof(rl), &rl)) |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 204 | 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 | |
| 263 | out: |
| 264 | return 0; |
| 265 | |
| 266 | nla_put_failure: |
| 267 | return -EMSGSIZE; |
| 268 | } |
| 269 | |
| 270 | static 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 | |
| 296 | out: |
| 297 | return err; |
| 298 | } |
| 299 | |
| 300 | int 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 | |
| 328 | drop_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 Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 337 | MODULE_LICENSE("GPL"); |