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