0001
0002
0003
0004
0005
0006
0007
0008 #include <crypto/scatterwalk.h>
0009 #include <crypto/internal/hash.h>
0010 #include <linux/err.h>
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/seq_file.h>
0015 #include <linux/cryptouser.h>
0016 #include <net/netlink.h>
0017 #include <linux/compiler.h>
0018
0019 #include "internal.h"
0020
0021 static const struct crypto_type crypto_shash_type;
0022
0023 static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
0024 unsigned int keylen)
0025 {
0026 return -ENOSYS;
0027 }
0028
0029
0030
0031
0032
0033
0034
0035
0036 bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
0037 {
0038 return alg->setkey != shash_no_setkey;
0039 }
0040 EXPORT_SYMBOL_GPL(crypto_shash_alg_has_setkey);
0041
0042 static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
0043 unsigned int keylen)
0044 {
0045 struct shash_alg *shash = crypto_shash_alg(tfm);
0046 unsigned long alignmask = crypto_shash_alignmask(tfm);
0047 unsigned long absize;
0048 u8 *buffer, *alignbuffer;
0049 int err;
0050
0051 absize = keylen + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
0052 buffer = kmalloc(absize, GFP_ATOMIC);
0053 if (!buffer)
0054 return -ENOMEM;
0055
0056 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
0057 memcpy(alignbuffer, key, keylen);
0058 err = shash->setkey(tfm, alignbuffer, keylen);
0059 kfree_sensitive(buffer);
0060 return err;
0061 }
0062
0063 static void shash_set_needkey(struct crypto_shash *tfm, struct shash_alg *alg)
0064 {
0065 if (crypto_shash_alg_needs_key(alg))
0066 crypto_shash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
0067 }
0068
0069 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
0070 unsigned int keylen)
0071 {
0072 struct shash_alg *shash = crypto_shash_alg(tfm);
0073 unsigned long alignmask = crypto_shash_alignmask(tfm);
0074 int err;
0075
0076 if ((unsigned long)key & alignmask)
0077 err = shash_setkey_unaligned(tfm, key, keylen);
0078 else
0079 err = shash->setkey(tfm, key, keylen);
0080
0081 if (unlikely(err)) {
0082 shash_set_needkey(tfm, shash);
0083 return err;
0084 }
0085
0086 crypto_shash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
0087 return 0;
0088 }
0089 EXPORT_SYMBOL_GPL(crypto_shash_setkey);
0090
0091 static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
0092 unsigned int len)
0093 {
0094 struct crypto_shash *tfm = desc->tfm;
0095 struct shash_alg *shash = crypto_shash_alg(tfm);
0096 unsigned long alignmask = crypto_shash_alignmask(tfm);
0097 unsigned int unaligned_len = alignmask + 1 -
0098 ((unsigned long)data & alignmask);
0099
0100
0101
0102
0103 u8 ubuf[MAX_ALGAPI_ALIGNMASK * 2];
0104 u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
0105 int err;
0106
0107 if (WARN_ON(buf + unaligned_len > ubuf + sizeof(ubuf)))
0108 return -EINVAL;
0109
0110 if (unaligned_len > len)
0111 unaligned_len = len;
0112
0113 memcpy(buf, data, unaligned_len);
0114 err = shash->update(desc, buf, unaligned_len);
0115 memset(buf, 0, unaligned_len);
0116
0117 return err ?:
0118 shash->update(desc, data + unaligned_len, len - unaligned_len);
0119 }
0120
0121 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
0122 unsigned int len)
0123 {
0124 struct crypto_shash *tfm = desc->tfm;
0125 struct shash_alg *shash = crypto_shash_alg(tfm);
0126 unsigned long alignmask = crypto_shash_alignmask(tfm);
0127
0128 if ((unsigned long)data & alignmask)
0129 return shash_update_unaligned(desc, data, len);
0130
0131 return shash->update(desc, data, len);
0132 }
0133 EXPORT_SYMBOL_GPL(crypto_shash_update);
0134
0135 static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
0136 {
0137 struct crypto_shash *tfm = desc->tfm;
0138 unsigned long alignmask = crypto_shash_alignmask(tfm);
0139 struct shash_alg *shash = crypto_shash_alg(tfm);
0140 unsigned int ds = crypto_shash_digestsize(tfm);
0141
0142
0143
0144
0145 u8 ubuf[MAX_ALGAPI_ALIGNMASK + HASH_MAX_DIGESTSIZE];
0146 u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
0147 int err;
0148
0149 if (WARN_ON(buf + ds > ubuf + sizeof(ubuf)))
0150 return -EINVAL;
0151
0152 err = shash->final(desc, buf);
0153 if (err)
0154 goto out;
0155
0156 memcpy(out, buf, ds);
0157
0158 out:
0159 memset(buf, 0, ds);
0160 return err;
0161 }
0162
0163 int crypto_shash_final(struct shash_desc *desc, u8 *out)
0164 {
0165 struct crypto_shash *tfm = desc->tfm;
0166 struct shash_alg *shash = crypto_shash_alg(tfm);
0167 unsigned long alignmask = crypto_shash_alignmask(tfm);
0168
0169 if ((unsigned long)out & alignmask)
0170 return shash_final_unaligned(desc, out);
0171
0172 return shash->final(desc, out);
0173 }
0174 EXPORT_SYMBOL_GPL(crypto_shash_final);
0175
0176 static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
0177 unsigned int len, u8 *out)
0178 {
0179 return crypto_shash_update(desc, data, len) ?:
0180 crypto_shash_final(desc, out);
0181 }
0182
0183 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
0184 unsigned int len, u8 *out)
0185 {
0186 struct crypto_shash *tfm = desc->tfm;
0187 struct shash_alg *shash = crypto_shash_alg(tfm);
0188 unsigned long alignmask = crypto_shash_alignmask(tfm);
0189
0190 if (((unsigned long)data | (unsigned long)out) & alignmask)
0191 return shash_finup_unaligned(desc, data, len, out);
0192
0193 return shash->finup(desc, data, len, out);
0194 }
0195 EXPORT_SYMBOL_GPL(crypto_shash_finup);
0196
0197 static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
0198 unsigned int len, u8 *out)
0199 {
0200 return crypto_shash_init(desc) ?:
0201 crypto_shash_finup(desc, data, len, out);
0202 }
0203
0204 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
0205 unsigned int len, u8 *out)
0206 {
0207 struct crypto_shash *tfm = desc->tfm;
0208 struct shash_alg *shash = crypto_shash_alg(tfm);
0209 unsigned long alignmask = crypto_shash_alignmask(tfm);
0210
0211 if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
0212 return -ENOKEY;
0213
0214 if (((unsigned long)data | (unsigned long)out) & alignmask)
0215 return shash_digest_unaligned(desc, data, len, out);
0216
0217 return shash->digest(desc, data, len, out);
0218 }
0219 EXPORT_SYMBOL_GPL(crypto_shash_digest);
0220
0221 int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,
0222 unsigned int len, u8 *out)
0223 {
0224 SHASH_DESC_ON_STACK(desc, tfm);
0225 int err;
0226
0227 desc->tfm = tfm;
0228
0229 err = crypto_shash_digest(desc, data, len, out);
0230
0231 shash_desc_zero(desc);
0232
0233 return err;
0234 }
0235 EXPORT_SYMBOL_GPL(crypto_shash_tfm_digest);
0236
0237 static int shash_default_export(struct shash_desc *desc, void *out)
0238 {
0239 memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
0240 return 0;
0241 }
0242
0243 static int shash_default_import(struct shash_desc *desc, const void *in)
0244 {
0245 memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->tfm));
0246 return 0;
0247 }
0248
0249 static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
0250 unsigned int keylen)
0251 {
0252 struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
0253
0254 return crypto_shash_setkey(*ctx, key, keylen);
0255 }
0256
0257 static int shash_async_init(struct ahash_request *req)
0258 {
0259 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
0260 struct shash_desc *desc = ahash_request_ctx(req);
0261
0262 desc->tfm = *ctx;
0263
0264 return crypto_shash_init(desc);
0265 }
0266
0267 int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
0268 {
0269 struct crypto_hash_walk walk;
0270 int nbytes;
0271
0272 for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
0273 nbytes = crypto_hash_walk_done(&walk, nbytes))
0274 nbytes = crypto_shash_update(desc, walk.data, nbytes);
0275
0276 return nbytes;
0277 }
0278 EXPORT_SYMBOL_GPL(shash_ahash_update);
0279
0280 static int shash_async_update(struct ahash_request *req)
0281 {
0282 return shash_ahash_update(req, ahash_request_ctx(req));
0283 }
0284
0285 static int shash_async_final(struct ahash_request *req)
0286 {
0287 return crypto_shash_final(ahash_request_ctx(req), req->result);
0288 }
0289
0290 int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc)
0291 {
0292 struct crypto_hash_walk walk;
0293 int nbytes;
0294
0295 nbytes = crypto_hash_walk_first(req, &walk);
0296 if (!nbytes)
0297 return crypto_shash_final(desc, req->result);
0298
0299 do {
0300 nbytes = crypto_hash_walk_last(&walk) ?
0301 crypto_shash_finup(desc, walk.data, nbytes,
0302 req->result) :
0303 crypto_shash_update(desc, walk.data, nbytes);
0304 nbytes = crypto_hash_walk_done(&walk, nbytes);
0305 } while (nbytes > 0);
0306
0307 return nbytes;
0308 }
0309 EXPORT_SYMBOL_GPL(shash_ahash_finup);
0310
0311 static int shash_async_finup(struct ahash_request *req)
0312 {
0313 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
0314 struct shash_desc *desc = ahash_request_ctx(req);
0315
0316 desc->tfm = *ctx;
0317
0318 return shash_ahash_finup(req, desc);
0319 }
0320
0321 int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
0322 {
0323 unsigned int nbytes = req->nbytes;
0324 struct scatterlist *sg;
0325 unsigned int offset;
0326 int err;
0327
0328 if (nbytes &&
0329 (sg = req->src, offset = sg->offset,
0330 nbytes <= min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) {
0331 void *data;
0332
0333 data = kmap_atomic(sg_page(sg));
0334 err = crypto_shash_digest(desc, data + offset, nbytes,
0335 req->result);
0336 kunmap_atomic(data);
0337 } else
0338 err = crypto_shash_init(desc) ?:
0339 shash_ahash_finup(req, desc);
0340
0341 return err;
0342 }
0343 EXPORT_SYMBOL_GPL(shash_ahash_digest);
0344
0345 static int shash_async_digest(struct ahash_request *req)
0346 {
0347 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
0348 struct shash_desc *desc = ahash_request_ctx(req);
0349
0350 desc->tfm = *ctx;
0351
0352 return shash_ahash_digest(req, desc);
0353 }
0354
0355 static int shash_async_export(struct ahash_request *req, void *out)
0356 {
0357 return crypto_shash_export(ahash_request_ctx(req), out);
0358 }
0359
0360 static int shash_async_import(struct ahash_request *req, const void *in)
0361 {
0362 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
0363 struct shash_desc *desc = ahash_request_ctx(req);
0364
0365 desc->tfm = *ctx;
0366
0367 return crypto_shash_import(desc, in);
0368 }
0369
0370 static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
0371 {
0372 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
0373
0374 crypto_free_shash(*ctx);
0375 }
0376
0377 int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
0378 {
0379 struct crypto_alg *calg = tfm->__crt_alg;
0380 struct shash_alg *alg = __crypto_shash_alg(calg);
0381 struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
0382 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
0383 struct crypto_shash *shash;
0384
0385 if (!crypto_mod_get(calg))
0386 return -EAGAIN;
0387
0388 shash = crypto_create_tfm(calg, &crypto_shash_type);
0389 if (IS_ERR(shash)) {
0390 crypto_mod_put(calg);
0391 return PTR_ERR(shash);
0392 }
0393
0394 *ctx = shash;
0395 tfm->exit = crypto_exit_shash_ops_async;
0396
0397 crt->init = shash_async_init;
0398 crt->update = shash_async_update;
0399 crt->final = shash_async_final;
0400 crt->finup = shash_async_finup;
0401 crt->digest = shash_async_digest;
0402 if (crypto_shash_alg_has_setkey(alg))
0403 crt->setkey = shash_async_setkey;
0404
0405 crypto_ahash_set_flags(crt, crypto_shash_get_flags(shash) &
0406 CRYPTO_TFM_NEED_KEY);
0407
0408 crt->export = shash_async_export;
0409 crt->import = shash_async_import;
0410
0411 crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
0412
0413 return 0;
0414 }
0415
0416 static void crypto_shash_exit_tfm(struct crypto_tfm *tfm)
0417 {
0418 struct crypto_shash *hash = __crypto_shash_cast(tfm);
0419 struct shash_alg *alg = crypto_shash_alg(hash);
0420
0421 alg->exit_tfm(hash);
0422 }
0423
0424 static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
0425 {
0426 struct crypto_shash *hash = __crypto_shash_cast(tfm);
0427 struct shash_alg *alg = crypto_shash_alg(hash);
0428 int err;
0429
0430 hash->descsize = alg->descsize;
0431
0432 shash_set_needkey(hash, alg);
0433
0434 if (alg->exit_tfm)
0435 tfm->exit = crypto_shash_exit_tfm;
0436
0437 if (!alg->init_tfm)
0438 return 0;
0439
0440 err = alg->init_tfm(hash);
0441 if (err)
0442 return err;
0443
0444
0445 if (WARN_ON_ONCE(hash->descsize > HASH_MAX_DESCSIZE)) {
0446 if (alg->exit_tfm)
0447 alg->exit_tfm(hash);
0448 return -EINVAL;
0449 }
0450
0451 return 0;
0452 }
0453
0454 static void crypto_shash_free_instance(struct crypto_instance *inst)
0455 {
0456 struct shash_instance *shash = shash_instance(inst);
0457
0458 shash->free(shash);
0459 }
0460
0461 #ifdef CONFIG_NET
0462 static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
0463 {
0464 struct crypto_report_hash rhash;
0465 struct shash_alg *salg = __crypto_shash_alg(alg);
0466
0467 memset(&rhash, 0, sizeof(rhash));
0468
0469 strscpy(rhash.type, "shash", sizeof(rhash.type));
0470
0471 rhash.blocksize = alg->cra_blocksize;
0472 rhash.digestsize = salg->digestsize;
0473
0474 return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash);
0475 }
0476 #else
0477 static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
0478 {
0479 return -ENOSYS;
0480 }
0481 #endif
0482
0483 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
0484 __maybe_unused;
0485 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
0486 {
0487 struct shash_alg *salg = __crypto_shash_alg(alg);
0488
0489 seq_printf(m, "type : shash\n");
0490 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
0491 seq_printf(m, "digestsize : %u\n", salg->digestsize);
0492 }
0493
0494 static const struct crypto_type crypto_shash_type = {
0495 .extsize = crypto_alg_extsize,
0496 .init_tfm = crypto_shash_init_tfm,
0497 .free = crypto_shash_free_instance,
0498 #ifdef CONFIG_PROC_FS
0499 .show = crypto_shash_show,
0500 #endif
0501 .report = crypto_shash_report,
0502 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
0503 .maskset = CRYPTO_ALG_TYPE_MASK,
0504 .type = CRYPTO_ALG_TYPE_SHASH,
0505 .tfmsize = offsetof(struct crypto_shash, base),
0506 };
0507
0508 int crypto_grab_shash(struct crypto_shash_spawn *spawn,
0509 struct crypto_instance *inst,
0510 const char *name, u32 type, u32 mask)
0511 {
0512 spawn->base.frontend = &crypto_shash_type;
0513 return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
0514 }
0515 EXPORT_SYMBOL_GPL(crypto_grab_shash);
0516
0517 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
0518 u32 mask)
0519 {
0520 return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
0521 }
0522 EXPORT_SYMBOL_GPL(crypto_alloc_shash);
0523
0524 int crypto_has_shash(const char *alg_name, u32 type, u32 mask)
0525 {
0526 return crypto_type_has_alg(alg_name, &crypto_shash_type, type, mask);
0527 }
0528 EXPORT_SYMBOL_GPL(crypto_has_shash);
0529
0530 static int shash_prepare_alg(struct shash_alg *alg)
0531 {
0532 struct crypto_alg *base = &alg->base;
0533
0534 if (alg->digestsize > HASH_MAX_DIGESTSIZE ||
0535 alg->descsize > HASH_MAX_DESCSIZE ||
0536 alg->statesize > HASH_MAX_STATESIZE)
0537 return -EINVAL;
0538
0539 if ((alg->export && !alg->import) || (alg->import && !alg->export))
0540 return -EINVAL;
0541
0542 base->cra_type = &crypto_shash_type;
0543 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
0544 base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
0545
0546 if (!alg->finup)
0547 alg->finup = shash_finup_unaligned;
0548 if (!alg->digest)
0549 alg->digest = shash_digest_unaligned;
0550 if (!alg->export) {
0551 alg->export = shash_default_export;
0552 alg->import = shash_default_import;
0553 alg->statesize = alg->descsize;
0554 }
0555 if (!alg->setkey)
0556 alg->setkey = shash_no_setkey;
0557
0558 return 0;
0559 }
0560
0561 int crypto_register_shash(struct shash_alg *alg)
0562 {
0563 struct crypto_alg *base = &alg->base;
0564 int err;
0565
0566 err = shash_prepare_alg(alg);
0567 if (err)
0568 return err;
0569
0570 return crypto_register_alg(base);
0571 }
0572 EXPORT_SYMBOL_GPL(crypto_register_shash);
0573
0574 void crypto_unregister_shash(struct shash_alg *alg)
0575 {
0576 crypto_unregister_alg(&alg->base);
0577 }
0578 EXPORT_SYMBOL_GPL(crypto_unregister_shash);
0579
0580 int crypto_register_shashes(struct shash_alg *algs, int count)
0581 {
0582 int i, ret;
0583
0584 for (i = 0; i < count; i++) {
0585 ret = crypto_register_shash(&algs[i]);
0586 if (ret)
0587 goto err;
0588 }
0589
0590 return 0;
0591
0592 err:
0593 for (--i; i >= 0; --i)
0594 crypto_unregister_shash(&algs[i]);
0595
0596 return ret;
0597 }
0598 EXPORT_SYMBOL_GPL(crypto_register_shashes);
0599
0600 void crypto_unregister_shashes(struct shash_alg *algs, int count)
0601 {
0602 int i;
0603
0604 for (i = count - 1; i >= 0; --i)
0605 crypto_unregister_shash(&algs[i]);
0606 }
0607 EXPORT_SYMBOL_GPL(crypto_unregister_shashes);
0608
0609 int shash_register_instance(struct crypto_template *tmpl,
0610 struct shash_instance *inst)
0611 {
0612 int err;
0613
0614 if (WARN_ON(!inst->free))
0615 return -EINVAL;
0616
0617 err = shash_prepare_alg(&inst->alg);
0618 if (err)
0619 return err;
0620
0621 return crypto_register_instance(tmpl, shash_crypto_instance(inst));
0622 }
0623 EXPORT_SYMBOL_GPL(shash_register_instance);
0624
0625 void shash_free_singlespawn_instance(struct shash_instance *inst)
0626 {
0627 crypto_drop_spawn(shash_instance_ctx(inst));
0628 kfree(inst);
0629 }
0630 EXPORT_SYMBOL_GPL(shash_free_singlespawn_instance);
0631
0632 MODULE_LICENSE("GPL");
0633 MODULE_DESCRIPTION("Synchronous cryptographic hash type");