^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Synchronous Cryptographic Hash operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/cryptouser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static const struct crypto_type crypto_shash_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Check whether an shash algorithm has a setkey function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * For CFI compatibility, this must not be an inline function. This is because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * when CFI is enabled, modules won't get the same address for shash_no_setkey
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * (if it were exported, which inlining would require) as the core kernel will.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return alg->setkey != shash_no_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) EXPORT_SYMBOL_GPL(crypto_shash_alg_has_setkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct shash_alg *shash = crypto_shash_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long alignmask = crypto_shash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned long absize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u8 *buffer, *alignbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) absize = keylen + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) buffer = kmalloc(absize, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) memcpy(alignbuffer, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) err = shash->setkey(tfm, alignbuffer, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) kfree_sensitive(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static void shash_set_needkey(struct crypto_shash *tfm, struct shash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (crypto_shash_alg_needs_key(alg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) crypto_shash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct shash_alg *shash = crypto_shash_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned long alignmask = crypto_shash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if ((unsigned long)key & alignmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) err = shash_setkey_unaligned(tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) err = shash->setkey(tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) shash_set_needkey(tfm, shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) crypto_shash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) EXPORT_SYMBOL_GPL(crypto_shash_setkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct crypto_shash *tfm = desc->tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct shash_alg *shash = crypto_shash_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) unsigned long alignmask = crypto_shash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) unsigned int unaligned_len = alignmask + 1 -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ((unsigned long)data & alignmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * We cannot count on __aligned() working for large values:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * https://patchwork.kernel.org/patch/9507697/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) u8 ubuf[MAX_ALGAPI_ALIGNMASK * 2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (WARN_ON(buf + unaligned_len > ubuf + sizeof(ubuf)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (unaligned_len > len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unaligned_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) memcpy(buf, data, unaligned_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) err = shash->update(desc, buf, unaligned_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) memset(buf, 0, unaligned_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return err ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) shash->update(desc, data + unaligned_len, len - unaligned_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int crypto_shash_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct crypto_shash *tfm = desc->tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct shash_alg *shash = crypto_shash_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned long alignmask = crypto_shash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if ((unsigned long)data & alignmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return shash_update_unaligned(desc, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return shash->update(desc, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) EXPORT_SYMBOL_GPL(crypto_shash_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct crypto_shash *tfm = desc->tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) unsigned long alignmask = crypto_shash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct shash_alg *shash = crypto_shash_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned int ds = crypto_shash_digestsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * We cannot count on __aligned() working for large values:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * https://patchwork.kernel.org/patch/9507697/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) u8 ubuf[MAX_ALGAPI_ALIGNMASK + HASH_MAX_DIGESTSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (WARN_ON(buf + ds > ubuf + sizeof(ubuf)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) err = shash->final(desc, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) memcpy(out, buf, ds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) memset(buf, 0, ds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int crypto_shash_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct crypto_shash *tfm = desc->tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct shash_alg *shash = crypto_shash_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) unsigned long alignmask = crypto_shash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if ((unsigned long)out & alignmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return shash_final_unaligned(desc, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return shash->final(desc, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) EXPORT_SYMBOL_GPL(crypto_shash_final);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return crypto_shash_update(desc, data, len) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) crypto_shash_final(desc, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct crypto_shash *tfm = desc->tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct shash_alg *shash = crypto_shash_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned long alignmask = crypto_shash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (((unsigned long)data | (unsigned long)out) & alignmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return shash_finup_unaligned(desc, data, len, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return shash->finup(desc, data, len, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) EXPORT_SYMBOL_GPL(crypto_shash_finup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return crypto_shash_init(desc) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) crypto_shash_finup(desc, data, len, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct crypto_shash *tfm = desc->tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct shash_alg *shash = crypto_shash_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) unsigned long alignmask = crypto_shash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (((unsigned long)data | (unsigned long)out) & alignmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return shash_digest_unaligned(desc, data, len, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return shash->digest(desc, data, len, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) EXPORT_SYMBOL_GPL(crypto_shash_digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) SHASH_DESC_ON_STACK(desc, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) desc->tfm = tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) err = crypto_shash_digest(desc, data, len, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) shash_desc_zero(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) EXPORT_SYMBOL_GPL(crypto_shash_tfm_digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static int shash_default_export(struct shash_desc *desc, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static int shash_default_import(struct shash_desc *desc, const void *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return crypto_shash_setkey(*ctx, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static int shash_async_init(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct shash_desc *desc = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) desc->tfm = *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return crypto_shash_init(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct crypto_hash_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) nbytes = crypto_hash_walk_done(&walk, nbytes))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) nbytes = crypto_shash_update(desc, walk.data, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) EXPORT_SYMBOL_GPL(shash_ahash_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static int shash_async_update(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return shash_ahash_update(req, ahash_request_ctx(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static int shash_async_final(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return crypto_shash_final(ahash_request_ctx(req), req->result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct crypto_hash_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) nbytes = crypto_hash_walk_first(req, &walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (!nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return crypto_shash_final(desc, req->result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) nbytes = crypto_hash_walk_last(&walk) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) crypto_shash_finup(desc, walk.data, nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) req->result) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) crypto_shash_update(desc, walk.data, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) nbytes = crypto_hash_walk_done(&walk, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) } while (nbytes > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) EXPORT_SYMBOL_GPL(shash_ahash_finup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static int shash_async_finup(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct shash_desc *desc = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) desc->tfm = *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return shash_ahash_finup(req, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) unsigned int nbytes = req->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) unsigned int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (nbytes &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) (sg = req->src, offset = sg->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) nbytes <= min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) data = kmap_atomic(sg_page(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) err = crypto_shash_digest(desc, data + offset, nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) req->result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) kunmap_atomic(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) err = crypto_shash_init(desc) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) shash_ahash_finup(req, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) EXPORT_SYMBOL_GPL(shash_ahash_digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static int shash_async_digest(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct shash_desc *desc = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) desc->tfm = *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return shash_ahash_digest(req, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static int shash_async_export(struct ahash_request *req, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return crypto_shash_export(ahash_request_ctx(req), out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static int shash_async_import(struct ahash_request *req, const void *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct shash_desc *desc = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) desc->tfm = *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return crypto_shash_import(desc, in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) crypto_free_shash(*ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct crypto_alg *calg = tfm->__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct shash_alg *alg = __crypto_shash_alg(calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct crypto_shash *shash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (!crypto_mod_get(calg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) shash = crypto_create_tfm(calg, &crypto_shash_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (IS_ERR(shash)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) crypto_mod_put(calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return PTR_ERR(shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) *ctx = shash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) tfm->exit = crypto_exit_shash_ops_async;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) crt->init = shash_async_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) crt->update = shash_async_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) crt->final = shash_async_final;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) crt->finup = shash_async_finup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) crt->digest = shash_async_digest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (crypto_shash_alg_has_setkey(alg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) crt->setkey = shash_async_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) crypto_ahash_set_flags(crt, crypto_shash_get_flags(shash) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) CRYPTO_TFM_NEED_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) crt->export = shash_async_export;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) crt->import = shash_async_import;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static void crypto_shash_exit_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) struct crypto_shash *hash = __crypto_shash_cast(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) struct shash_alg *alg = crypto_shash_alg(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) alg->exit_tfm(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) struct crypto_shash *hash = __crypto_shash_cast(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) struct shash_alg *alg = crypto_shash_alg(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) hash->descsize = alg->descsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) shash_set_needkey(hash, alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (alg->exit_tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) tfm->exit = crypto_shash_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (!alg->init_tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) err = alg->init_tfm(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /* ->init_tfm() may have increased the descsize. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (WARN_ON_ONCE(hash->descsize > HASH_MAX_DESCSIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (alg->exit_tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) alg->exit_tfm(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static void crypto_shash_free_instance(struct crypto_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) struct shash_instance *shash = shash_instance(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) shash->free(shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) #ifdef CONFIG_NET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) struct crypto_report_hash rhash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) struct shash_alg *salg = __crypto_shash_alg(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) memset(&rhash, 0, sizeof(rhash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) strscpy(rhash.type, "shash", sizeof(rhash.type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) rhash.blocksize = alg->cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) rhash.digestsize = salg->digestsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) __maybe_unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct shash_alg *salg = __crypto_shash_alg(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) seq_printf(m, "type : shash\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) seq_printf(m, "digestsize : %u\n", salg->digestsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static const struct crypto_type crypto_shash_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) .extsize = crypto_alg_extsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) .init_tfm = crypto_shash_init_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) .free = crypto_shash_free_instance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) .show = crypto_shash_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) .report = crypto_shash_report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) .maskclear = ~CRYPTO_ALG_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) .maskset = CRYPTO_ALG_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) .type = CRYPTO_ALG_TYPE_SHASH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) .tfmsize = offsetof(struct crypto_shash, base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) int crypto_grab_shash(struct crypto_shash_spawn *spawn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) struct crypto_instance *inst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) const char *name, u32 type, u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) spawn->base.frontend = &crypto_shash_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) EXPORT_SYMBOL_GPL(crypto_grab_shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) EXPORT_SYMBOL_GPL(crypto_alloc_shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static int shash_prepare_alg(struct shash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct crypto_alg *base = &alg->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (alg->digestsize > HASH_MAX_DIGESTSIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) alg->descsize > HASH_MAX_DESCSIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) alg->statesize > HASH_MAX_STATESIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if ((alg->export && !alg->import) || (alg->import && !alg->export))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) base->cra_type = &crypto_shash_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (!alg->finup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) alg->finup = shash_finup_unaligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (!alg->digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) alg->digest = shash_digest_unaligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (!alg->export) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) alg->export = shash_default_export;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) alg->import = shash_default_import;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) alg->statesize = alg->descsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (!alg->setkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) alg->setkey = shash_no_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) int crypto_register_shash(struct shash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) struct crypto_alg *base = &alg->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) err = shash_prepare_alg(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) return crypto_register_alg(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) EXPORT_SYMBOL_GPL(crypto_register_shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) void crypto_unregister_shash(struct shash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) crypto_unregister_alg(&alg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) EXPORT_SYMBOL_GPL(crypto_unregister_shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) int crypto_register_shashes(struct shash_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) ret = crypto_register_shash(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) for (--i; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) crypto_unregister_shash(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) EXPORT_SYMBOL_GPL(crypto_register_shashes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) void crypto_unregister_shashes(struct shash_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) for (i = count - 1; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) crypto_unregister_shash(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) EXPORT_SYMBOL_GPL(crypto_unregister_shashes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) int shash_register_instance(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) struct shash_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (WARN_ON(!inst->free))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) err = shash_prepare_alg(&inst->alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) return crypto_register_instance(tmpl, shash_crypto_instance(inst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) EXPORT_SYMBOL_GPL(shash_register_instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) void shash_free_singlespawn_instance(struct shash_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) crypto_drop_spawn(shash_instance_ctx(inst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) EXPORT_SYMBOL_GPL(shash_free_singlespawn_instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) MODULE_DESCRIPTION("Synchronous cryptographic hash type");