| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Synchronous Cryptographic Hash operations. |
| 4 | * |
| 5 | * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> |
| 6 | */ |
| 7 | |
| 8 | #include <crypto/scatterwalk.h> |
| 9 | #include <linux/cryptouser.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/seq_file.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <net/netlink.h> |
| 16 | |
| 17 | #include "hash.h" |
| 18 | |
| 19 | static inline struct crypto_istat_hash *shash_get_stat(struct shash_alg *alg) |
| 20 | { |
| 21 | return hash_get_stat(&alg->halg); |
| 22 | } |
| 23 | |
| 24 | static inline int crypto_shash_errstat(struct shash_alg *alg, int err) |
| 25 | { |
| 26 | if (!IS_ENABLED(CONFIG_CRYPTO_STATS)) |
| 27 | return err; |
| 28 | |
| 29 | if (err && err != -EINPROGRESS && err != -EBUSY) |
| 30 | atomic64_inc(&shash_get_stat(alg)->err_cnt); |
| 31 | |
| 32 | return err; |
| 33 | } |
| 34 | |
| 35 | int shash_no_setkey(struct crypto_shash *tfm, const u8 *key, |
| 36 | unsigned int keylen) |
| 37 | { |
| 38 | return -ENOSYS; |
| 39 | } |
| 40 | EXPORT_SYMBOL_GPL(shash_no_setkey); |
| 41 | |
| 42 | static void shash_set_needkey(struct crypto_shash *tfm, struct shash_alg *alg) |
| 43 | { |
| 44 | if (crypto_shash_alg_needs_key(alg)) |
| 45 | crypto_shash_set_flags(tfm, CRYPTO_TFM_NEED_KEY); |
| 46 | } |
| 47 | |
| 48 | int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, |
| 49 | unsigned int keylen) |
| 50 | { |
| 51 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 52 | int err; |
| 53 | |
| 54 | err = shash->setkey(tfm, key, keylen); |
| 55 | if (unlikely(err)) { |
| 56 | shash_set_needkey(tfm, shash); |
| 57 | return err; |
| 58 | } |
| 59 | |
| 60 | crypto_shash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); |
| 61 | return 0; |
| 62 | } |
| 63 | EXPORT_SYMBOL_GPL(crypto_shash_setkey); |
| 64 | |
| 65 | int crypto_shash_update(struct shash_desc *desc, const u8 *data, |
| 66 | unsigned int len) |
| 67 | { |
| 68 | struct shash_alg *shash = crypto_shash_alg(desc->tfm); |
| 69 | int err; |
| 70 | |
| 71 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) |
| 72 | atomic64_add(len, &shash_get_stat(shash)->hash_tlen); |
| 73 | |
| 74 | err = shash->update(desc, data, len); |
| 75 | |
| 76 | return crypto_shash_errstat(shash, err); |
| 77 | } |
| 78 | EXPORT_SYMBOL_GPL(crypto_shash_update); |
| 79 | |
| 80 | int crypto_shash_final(struct shash_desc *desc, u8 *out) |
| 81 | { |
| 82 | struct shash_alg *shash = crypto_shash_alg(desc->tfm); |
| 83 | int err; |
| 84 | |
| 85 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) |
| 86 | atomic64_inc(&shash_get_stat(shash)->hash_cnt); |
| 87 | |
| 88 | err = shash->final(desc, out); |
| 89 | |
| 90 | return crypto_shash_errstat(shash, err); |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(crypto_shash_final); |
| 93 | |
| 94 | static int shash_default_finup(struct shash_desc *desc, const u8 *data, |
| 95 | unsigned int len, u8 *out) |
| 96 | { |
| 97 | struct shash_alg *shash = crypto_shash_alg(desc->tfm); |
| 98 | |
| 99 | return shash->update(desc, data, len) ?: |
| 100 | shash->final(desc, out); |
| 101 | } |
| 102 | |
| 103 | int crypto_shash_finup(struct shash_desc *desc, const u8 *data, |
| 104 | unsigned int len, u8 *out) |
| 105 | { |
| 106 | struct crypto_shash *tfm = desc->tfm; |
| 107 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 108 | int err; |
| 109 | |
| 110 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) { |
| 111 | struct crypto_istat_hash *istat = shash_get_stat(shash); |
| 112 | |
| 113 | atomic64_inc(&istat->hash_cnt); |
| 114 | atomic64_add(len, &istat->hash_tlen); |
| 115 | } |
| 116 | |
| 117 | err = shash->finup(desc, data, len, out); |
| 118 | |
| 119 | return crypto_shash_errstat(shash, err); |
| 120 | } |
| 121 | EXPORT_SYMBOL_GPL(crypto_shash_finup); |
| 122 | |
| 123 | static int shash_default_digest(struct shash_desc *desc, const u8 *data, |
| 124 | unsigned int len, u8 *out) |
| 125 | { |
| 126 | struct shash_alg *shash = crypto_shash_alg(desc->tfm); |
| 127 | |
| 128 | return shash->init(desc) ?: |
| 129 | shash->finup(desc, data, len, out); |
| 130 | } |
| 131 | |
| 132 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, |
| 133 | unsigned int len, u8 *out) |
| 134 | { |
| 135 | struct crypto_shash *tfm = desc->tfm; |
| 136 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 137 | int err; |
| 138 | |
| 139 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) { |
| 140 | struct crypto_istat_hash *istat = shash_get_stat(shash); |
| 141 | |
| 142 | atomic64_inc(&istat->hash_cnt); |
| 143 | atomic64_add(len, &istat->hash_tlen); |
| 144 | } |
| 145 | |
| 146 | if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
| 147 | err = -ENOKEY; |
| 148 | else |
| 149 | err = shash->digest(desc, data, len, out); |
| 150 | |
| 151 | return crypto_shash_errstat(shash, err); |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(crypto_shash_digest); |
| 154 | |
| 155 | int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data, |
| 156 | unsigned int len, u8 *out) |
| 157 | { |
| 158 | SHASH_DESC_ON_STACK(desc, tfm); |
| 159 | int err; |
| 160 | |
| 161 | desc->tfm = tfm; |
| 162 | |
| 163 | err = crypto_shash_digest(desc, data, len, out); |
| 164 | |
| 165 | shash_desc_zero(desc); |
| 166 | |
| 167 | return err; |
| 168 | } |
| 169 | EXPORT_SYMBOL_GPL(crypto_shash_tfm_digest); |
| 170 | |
| 171 | int crypto_shash_export(struct shash_desc *desc, void *out) |
| 172 | { |
| 173 | struct crypto_shash *tfm = desc->tfm; |
| 174 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 175 | |
| 176 | if (shash->export) |
| 177 | return shash->export(desc, out); |
| 178 | |
| 179 | memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(tfm)); |
| 180 | return 0; |
| 181 | } |
| 182 | EXPORT_SYMBOL_GPL(crypto_shash_export); |
| 183 | |
| 184 | int crypto_shash_import(struct shash_desc *desc, const void *in) |
| 185 | { |
| 186 | struct crypto_shash *tfm = desc->tfm; |
| 187 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 188 | |
| 189 | if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
| 190 | return -ENOKEY; |
| 191 | |
| 192 | if (shash->import) |
| 193 | return shash->import(desc, in); |
| 194 | |
| 195 | memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(tfm)); |
| 196 | return 0; |
| 197 | } |
| 198 | EXPORT_SYMBOL_GPL(crypto_shash_import); |
| 199 | |
| 200 | static void crypto_shash_exit_tfm(struct crypto_tfm *tfm) |
| 201 | { |
| 202 | struct crypto_shash *hash = __crypto_shash_cast(tfm); |
| 203 | struct shash_alg *alg = crypto_shash_alg(hash); |
| 204 | |
| 205 | alg->exit_tfm(hash); |
| 206 | } |
| 207 | |
| 208 | static int crypto_shash_init_tfm(struct crypto_tfm *tfm) |
| 209 | { |
| 210 | struct crypto_shash *hash = __crypto_shash_cast(tfm); |
| 211 | struct shash_alg *alg = crypto_shash_alg(hash); |
| 212 | int err; |
| 213 | |
| 214 | hash->descsize = alg->descsize; |
| 215 | |
| 216 | shash_set_needkey(hash, alg); |
| 217 | |
| 218 | if (alg->exit_tfm) |
| 219 | tfm->exit = crypto_shash_exit_tfm; |
| 220 | |
| 221 | if (!alg->init_tfm) |
| 222 | return 0; |
| 223 | |
| 224 | err = alg->init_tfm(hash); |
| 225 | if (err) |
| 226 | return err; |
| 227 | |
| 228 | /* ->init_tfm() may have increased the descsize. */ |
| 229 | if (WARN_ON_ONCE(hash->descsize > HASH_MAX_DESCSIZE)) { |
| 230 | if (alg->exit_tfm) |
| 231 | alg->exit_tfm(hash); |
| 232 | return -EINVAL; |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static void crypto_shash_free_instance(struct crypto_instance *inst) |
| 239 | { |
| 240 | struct shash_instance *shash = shash_instance(inst); |
| 241 | |
| 242 | shash->free(shash); |
| 243 | } |
| 244 | |
| 245 | static int __maybe_unused crypto_shash_report( |
| 246 | struct sk_buff *skb, struct crypto_alg *alg) |
| 247 | { |
| 248 | struct crypto_report_hash rhash; |
| 249 | struct shash_alg *salg = __crypto_shash_alg(alg); |
| 250 | |
| 251 | memset(&rhash, 0, sizeof(rhash)); |
| 252 | |
| 253 | strscpy(rhash.type, "shash", sizeof(rhash.type)); |
| 254 | |
| 255 | rhash.blocksize = alg->cra_blocksize; |
| 256 | rhash.digestsize = salg->digestsize; |
| 257 | |
| 258 | return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash); |
| 259 | } |
| 260 | |
| 261 | static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) |
| 262 | __maybe_unused; |
| 263 | static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) |
| 264 | { |
| 265 | struct shash_alg *salg = __crypto_shash_alg(alg); |
| 266 | |
| 267 | seq_printf(m, "type : shash\n"); |
| 268 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 269 | seq_printf(m, "digestsize : %u\n", salg->digestsize); |
| 270 | } |
| 271 | |
| 272 | static int __maybe_unused crypto_shash_report_stat( |
| 273 | struct sk_buff *skb, struct crypto_alg *alg) |
| 274 | { |
| 275 | return crypto_hash_report_stat(skb, alg, "shash"); |
| 276 | } |
| 277 | |
| 278 | const struct crypto_type crypto_shash_type = { |
| 279 | .extsize = crypto_alg_extsize, |
| 280 | .init_tfm = crypto_shash_init_tfm, |
| 281 | .free = crypto_shash_free_instance, |
| 282 | #ifdef CONFIG_PROC_FS |
| 283 | .show = crypto_shash_show, |
| 284 | #endif |
| 285 | #if IS_ENABLED(CONFIG_CRYPTO_USER) |
| 286 | .report = crypto_shash_report, |
| 287 | #endif |
| 288 | #ifdef CONFIG_CRYPTO_STATS |
| 289 | .report_stat = crypto_shash_report_stat, |
| 290 | #endif |
| 291 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
| 292 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 293 | .type = CRYPTO_ALG_TYPE_SHASH, |
| 294 | .tfmsize = offsetof(struct crypto_shash, base), |
| 295 | }; |
| 296 | |
| 297 | int crypto_grab_shash(struct crypto_shash_spawn *spawn, |
| 298 | struct crypto_instance *inst, |
| 299 | const char *name, u32 type, u32 mask) |
| 300 | { |
| 301 | spawn->base.frontend = &crypto_shash_type; |
| 302 | return crypto_grab_spawn(&spawn->base, inst, name, type, mask); |
| 303 | } |
| 304 | EXPORT_SYMBOL_GPL(crypto_grab_shash); |
| 305 | |
| 306 | struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, |
| 307 | u32 mask) |
| 308 | { |
| 309 | return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask); |
| 310 | } |
| 311 | EXPORT_SYMBOL_GPL(crypto_alloc_shash); |
| 312 | |
| 313 | int crypto_has_shash(const char *alg_name, u32 type, u32 mask) |
| 314 | { |
| 315 | return crypto_type_has_alg(alg_name, &crypto_shash_type, type, mask); |
| 316 | } |
| 317 | EXPORT_SYMBOL_GPL(crypto_has_shash); |
| 318 | |
| 319 | struct crypto_shash *crypto_clone_shash(struct crypto_shash *hash) |
| 320 | { |
| 321 | struct crypto_tfm *tfm = crypto_shash_tfm(hash); |
| 322 | struct shash_alg *alg = crypto_shash_alg(hash); |
| 323 | struct crypto_shash *nhash; |
| 324 | int err; |
| 325 | |
| 326 | if (!crypto_shash_alg_has_setkey(alg)) { |
| 327 | tfm = crypto_tfm_get(tfm); |
| 328 | if (IS_ERR(tfm)) |
| 329 | return ERR_CAST(tfm); |
| 330 | |
| 331 | return hash; |
| 332 | } |
| 333 | |
| 334 | if (!alg->clone_tfm && (alg->init_tfm || alg->base.cra_init)) |
| 335 | return ERR_PTR(-ENOSYS); |
| 336 | |
| 337 | nhash = crypto_clone_tfm(&crypto_shash_type, tfm); |
| 338 | if (IS_ERR(nhash)) |
| 339 | return nhash; |
| 340 | |
| 341 | nhash->descsize = hash->descsize; |
| 342 | |
| 343 | if (alg->clone_tfm) { |
| 344 | err = alg->clone_tfm(nhash, hash); |
| 345 | if (err) { |
| 346 | crypto_free_shash(nhash); |
| 347 | return ERR_PTR(err); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | return nhash; |
| 352 | } |
| 353 | EXPORT_SYMBOL_GPL(crypto_clone_shash); |
| 354 | |
| 355 | int hash_prepare_alg(struct hash_alg_common *alg) |
| 356 | { |
| 357 | struct crypto_istat_hash *istat = hash_get_stat(alg); |
| 358 | struct crypto_alg *base = &alg->base; |
| 359 | |
| 360 | if (alg->digestsize > HASH_MAX_DIGESTSIZE) |
| 361 | return -EINVAL; |
| 362 | |
| 363 | /* alignmask is not useful for hashes, so it is not supported. */ |
| 364 | if (base->cra_alignmask) |
| 365 | return -EINVAL; |
| 366 | |
| 367 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
| 368 | |
| 369 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) |
| 370 | memset(istat, 0, sizeof(*istat)); |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | static int shash_prepare_alg(struct shash_alg *alg) |
| 376 | { |
| 377 | struct crypto_alg *base = &alg->halg.base; |
| 378 | int err; |
| 379 | |
| 380 | if (alg->descsize > HASH_MAX_DESCSIZE) |
| 381 | return -EINVAL; |
| 382 | |
| 383 | if ((alg->export && !alg->import) || (alg->import && !alg->export)) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | err = hash_prepare_alg(&alg->halg); |
| 387 | if (err) |
| 388 | return err; |
| 389 | |
| 390 | base->cra_type = &crypto_shash_type; |
| 391 | base->cra_flags |= CRYPTO_ALG_TYPE_SHASH; |
| 392 | |
| 393 | /* |
| 394 | * Handle missing optional functions. For each one we can either |
| 395 | * install a default here, or we can leave the pointer as NULL and check |
| 396 | * the pointer for NULL in crypto_shash_*(), avoiding an indirect call |
| 397 | * when the default behavior is desired. For ->finup and ->digest we |
| 398 | * install defaults, since for optimal performance algorithms should |
| 399 | * implement these anyway. On the other hand, for ->import and |
| 400 | * ->export the common case and best performance comes from the simple |
| 401 | * memcpy of the shash_desc_ctx, so when those pointers are NULL we |
| 402 | * leave them NULL and provide the memcpy with no indirect call. |
| 403 | */ |
| 404 | if (!alg->finup) |
| 405 | alg->finup = shash_default_finup; |
| 406 | if (!alg->digest) |
| 407 | alg->digest = shash_default_digest; |
| 408 | if (!alg->export) |
| 409 | alg->halg.statesize = alg->descsize; |
| 410 | if (!alg->setkey) |
| 411 | alg->setkey = shash_no_setkey; |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | int crypto_register_shash(struct shash_alg *alg) |
| 417 | { |
| 418 | struct crypto_alg *base = &alg->base; |
| 419 | int err; |
| 420 | |
| 421 | err = shash_prepare_alg(alg); |
| 422 | if (err) |
| 423 | return err; |
| 424 | |
| 425 | return crypto_register_alg(base); |
| 426 | } |
| 427 | EXPORT_SYMBOL_GPL(crypto_register_shash); |
| 428 | |
| 429 | void crypto_unregister_shash(struct shash_alg *alg) |
| 430 | { |
| 431 | crypto_unregister_alg(&alg->base); |
| 432 | } |
| 433 | EXPORT_SYMBOL_GPL(crypto_unregister_shash); |
| 434 | |
| 435 | int crypto_register_shashes(struct shash_alg *algs, int count) |
| 436 | { |
| 437 | int i, ret; |
| 438 | |
| 439 | for (i = 0; i < count; i++) { |
| 440 | ret = crypto_register_shash(&algs[i]); |
| 441 | if (ret) |
| 442 | goto err; |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | |
| 447 | err: |
| 448 | for (--i; i >= 0; --i) |
| 449 | crypto_unregister_shash(&algs[i]); |
| 450 | |
| 451 | return ret; |
| 452 | } |
| 453 | EXPORT_SYMBOL_GPL(crypto_register_shashes); |
| 454 | |
| 455 | void crypto_unregister_shashes(struct shash_alg *algs, int count) |
| 456 | { |
| 457 | int i; |
| 458 | |
| 459 | for (i = count - 1; i >= 0; --i) |
| 460 | crypto_unregister_shash(&algs[i]); |
| 461 | } |
| 462 | EXPORT_SYMBOL_GPL(crypto_unregister_shashes); |
| 463 | |
| 464 | int shash_register_instance(struct crypto_template *tmpl, |
| 465 | struct shash_instance *inst) |
| 466 | { |
| 467 | int err; |
| 468 | |
| 469 | if (WARN_ON(!inst->free)) |
| 470 | return -EINVAL; |
| 471 | |
| 472 | err = shash_prepare_alg(&inst->alg); |
| 473 | if (err) |
| 474 | return err; |
| 475 | |
| 476 | return crypto_register_instance(tmpl, shash_crypto_instance(inst)); |
| 477 | } |
| 478 | EXPORT_SYMBOL_GPL(shash_register_instance); |
| 479 | |
| 480 | void shash_free_singlespawn_instance(struct shash_instance *inst) |
| 481 | { |
| 482 | crypto_drop_spawn(shash_instance_ctx(inst)); |
| 483 | kfree(inst); |
| 484 | } |
| 485 | EXPORT_SYMBOL_GPL(shash_free_singlespawn_instance); |
| 486 | |
| 487 | MODULE_LICENSE("GPL"); |
| 488 | MODULE_DESCRIPTION("Synchronous cryptographic hash type"); |