^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) * Asynchronous Cryptographic Hash operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This is the asynchronous version of hash.c with notification of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * completion via a callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (c) 2008 Loc Ho <lho@amcc.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/cryptouser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static const struct crypto_type crypto_ahash_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct ahash_request_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) crypto_completion_t complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u8 *result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) void *ubuf[] CRYPTO_MINALIGN_ATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) halg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static int hash_walk_next(struct crypto_hash_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned int alignmask = walk->alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int offset = walk->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned int nbytes = min(walk->entrylen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ((unsigned int)(PAGE_SIZE)) - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) walk->data = kmap_atomic(walk->pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) walk->data += offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (offset & alignmask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int unaligned = alignmask + 1 - (offset & alignmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (nbytes > unaligned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) nbytes = unaligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) walk->entrylen -= nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static int hash_walk_new_entry(struct crypto_hash_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) sg = walk->sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) walk->offset = sg->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) walk->pg = sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) walk->offset = offset_in_page(walk->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) walk->entrylen = sg->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (walk->entrylen > walk->total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) walk->entrylen = walk->total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) walk->total -= walk->entrylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return hash_walk_next(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) unsigned int alignmask = walk->alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) walk->data -= walk->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (walk->entrylen && (walk->offset & alignmask) && !err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) walk->offset = ALIGN(walk->offset, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) nbytes = min(walk->entrylen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) (unsigned int)(PAGE_SIZE - walk->offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) walk->entrylen -= nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) walk->data += walk->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) kunmap_atomic(walk->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) crypto_yield(walk->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (walk->entrylen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) walk->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) walk->pg++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return hash_walk_next(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!walk->total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) walk->sg = sg_next(walk->sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return hash_walk_new_entry(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int crypto_hash_walk_first(struct ahash_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct crypto_hash_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) walk->total = req->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (!walk->total) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) walk->entrylen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) walk->sg = req->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) walk->flags = req->base.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return hash_walk_new_entry(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned long alignmask = crypto_ahash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) u8 *buffer, *alignbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) unsigned long absize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) absize = keylen + alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) buffer = kmalloc(absize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) memcpy(alignbuffer, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) ret = tfm->setkey(tfm, alignbuffer, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) kfree_sensitive(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -ENOSYS;
^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) static void ahash_set_needkey(struct crypto_ahash *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) const struct hash_alg_common *alg = crypto_hash_alg_common(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (tfm->setkey != ahash_nosetkey &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) crypto_ahash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) unsigned long alignmask = crypto_ahash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if ((unsigned long)key & alignmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) err = ahash_setkey_unaligned(tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) err = tfm->setkey(tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ahash_set_needkey(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) crypto_ahash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static inline unsigned int ahash_align_buffer_size(unsigned len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) unsigned long alignmask = crypto_ahash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) unsigned int ds = crypto_ahash_digestsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct ahash_request_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) GFP_KERNEL : GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * WARNING: Voodoo programming below!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * The code below is obscure and hard to understand, thus explanation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * is necessary. See include/crypto/hash.h and include/linux/crypto.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * to understand the layout of structures used here!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * The code here will replace portions of the ORIGINAL request with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * pointers to new code and buffers so the hashing operation can store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * the result in aligned buffer. We will call the modified request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * an ADJUSTED request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * The newly mangled request will look as such:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * req {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * .result = ADJUSTED[new aligned buffer]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * .base.complete = ADJUSTED[pointer to completion function]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * .base.data = ADJUSTED[*req (pointer to self)]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * .priv = ADJUSTED[new priv] {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * .result = ORIGINAL(result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * .complete = ORIGINAL(base.complete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * .data = ORIGINAL(base.data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) priv->result = req->result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) priv->complete = req->base.complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) priv->data = req->base.data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) priv->flags = req->base.flags;
^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) * WARNING: We do not backup req->priv here! The req->priv
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * is for internal use of the Crypto API and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * user must _NOT_ _EVER_ depend on it's content!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) req->base.complete = cplt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) req->base.data = req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) req->priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static void ahash_restore_req(struct ahash_request *req, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct ahash_request_priv *priv = req->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) memcpy(priv->result, req->result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /* Restore the original crypto request. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) req->result = priv->result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) ahash_request_set_callback(req, priv->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) priv->complete, priv->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) req->priv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /* Free the req->priv.priv from the ADJUSTED request. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) kfree_sensitive(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static void ahash_notify_einprogress(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct ahash_request_priv *priv = req->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) struct crypto_async_request oreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) oreq.data = priv->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) priv->complete(&oreq, -EINPROGRESS);
^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 void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct ahash_request *areq = req->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (err == -EINPROGRESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) ahash_notify_einprogress(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * Restore the original request, see ahash_op_unaligned() for what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * goes where.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * The "struct ahash_request *req" here is in fact the "req.base"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * from the ADJUSTED request from ahash_op_unaligned(), thus as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * is a pointer to self, it is also the ADJUSTED "req" .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /* First copy req->result into req->priv.result */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ahash_restore_req(areq, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /* Complete the ORIGINAL request. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) areq->base.complete(&areq->base, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int ahash_op_unaligned(struct ahash_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int (*op)(struct ahash_request *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) err = ahash_save_req(req, ahash_op_unaligned_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) err = op(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (err == -EINPROGRESS || err == -EBUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) ahash_restore_req(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static int crypto_ahash_op(struct ahash_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) int (*op)(struct ahash_request *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) unsigned long alignmask = crypto_ahash_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if ((unsigned long)req->result & alignmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return ahash_op_unaligned(req, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return op(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) int crypto_ahash_final(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct crypto_alg *alg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) unsigned int nbytes = req->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) crypto_stats_get(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) crypto_stats_ahash_final(nbytes, ret, alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) EXPORT_SYMBOL_GPL(crypto_ahash_final);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) int crypto_ahash_finup(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) struct crypto_alg *alg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) unsigned int nbytes = req->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) crypto_stats_get(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) crypto_stats_ahash_final(nbytes, ret, alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) EXPORT_SYMBOL_GPL(crypto_ahash_finup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) int crypto_ahash_digest(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct crypto_alg *alg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) unsigned int nbytes = req->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) crypto_stats_get(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ret = -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) ret = crypto_ahash_op(req, tfm->digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) crypto_stats_ahash_final(nbytes, ret, alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) EXPORT_SYMBOL_GPL(crypto_ahash_digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) struct ahash_request *areq = req->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) ahash_restore_req(areq, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) areq->base.complete(&areq->base, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static int ahash_def_finup_finish1(struct ahash_request *req, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) req->base.complete = ahash_def_finup_done2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) err = crypto_ahash_reqtfm(req)->final(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (err == -EINPROGRESS || err == -EBUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) ahash_restore_req(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct ahash_request *areq = req->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (err == -EINPROGRESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) ahash_notify_einprogress(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) areq->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) err = ahash_def_finup_finish1(areq, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (areq->priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) areq->base.complete(&areq->base, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) static int ahash_def_finup(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) err = ahash_save_req(req, ahash_def_finup_done1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) err = tfm->update(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (err == -EINPROGRESS || err == -EBUSY)
^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) return ahash_def_finup_finish1(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static void crypto_ahash_exit_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct ahash_alg *alg = crypto_ahash_alg(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) alg->exit_tfm(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) struct ahash_alg *alg = crypto_ahash_alg(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) hash->setkey = ahash_nosetkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) return crypto_init_shash_ops_async(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) hash->init = alg->init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) hash->update = alg->update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) hash->final = alg->final;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) hash->finup = alg->finup ?: ahash_def_finup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) hash->digest = alg->digest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) hash->export = alg->export;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) hash->import = alg->import;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (alg->setkey) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) hash->setkey = alg->setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ahash_set_needkey(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (alg->exit_tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) tfm->exit = crypto_ahash_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return alg->init_tfm ? alg->init_tfm(hash) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (alg->cra_type != &crypto_ahash_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return sizeof(struct crypto_shash *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return crypto_alg_extsize(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static void crypto_ahash_free_instance(struct crypto_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) struct ahash_instance *ahash = ahash_instance(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) ahash->free(ahash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) #ifdef CONFIG_NET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct crypto_report_hash rhash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) memset(&rhash, 0, sizeof(rhash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) strscpy(rhash.type, "ahash", sizeof(rhash.type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) rhash.blocksize = alg->cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) __maybe_unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) seq_printf(m, "type : ahash\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) "yes" : "no");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) seq_printf(m, "digestsize : %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) __crypto_hash_alg_common(alg)->digestsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static const struct crypto_type crypto_ahash_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) .extsize = crypto_ahash_extsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) .init_tfm = crypto_ahash_init_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) .free = crypto_ahash_free_instance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) .show = crypto_ahash_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) .report = crypto_ahash_report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) .maskclear = ~CRYPTO_ALG_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) .type = CRYPTO_ALG_TYPE_AHASH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) .tfmsize = offsetof(struct crypto_ahash, base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) struct crypto_instance *inst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) const char *name, u32 type, u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) spawn->base.frontend = &crypto_ahash_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) EXPORT_SYMBOL_GPL(crypto_grab_ahash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) int crypto_has_ahash(const char *alg_name, u32 type, u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) return crypto_type_has_alg(alg_name, &crypto_ahash_type, type, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) EXPORT_SYMBOL_GPL(crypto_has_ahash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) static int ahash_prepare_alg(struct ahash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) struct crypto_alg *base = &alg->halg.base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (alg->halg.digestsize > HASH_MAX_DIGESTSIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) alg->halg.statesize > HASH_MAX_STATESIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) alg->halg.statesize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) base->cra_type = &crypto_ahash_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) return 0;
^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) int crypto_register_ahash(struct ahash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) struct crypto_alg *base = &alg->halg.base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) err = ahash_prepare_alg(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) return crypto_register_alg(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) EXPORT_SYMBOL_GPL(crypto_register_ahash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) void crypto_unregister_ahash(struct ahash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) crypto_unregister_alg(&alg->halg.base);
^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_ahash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) int crypto_register_ahashes(struct ahash_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) ret = crypto_register_ahash(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) for (--i; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) crypto_unregister_ahash(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) EXPORT_SYMBOL_GPL(crypto_register_ahashes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) void crypto_unregister_ahashes(struct ahash_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) for (i = count - 1; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) crypto_unregister_ahash(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) EXPORT_SYMBOL_GPL(crypto_unregister_ahashes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) int ahash_register_instance(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) struct ahash_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (WARN_ON(!inst->free))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) err = ahash_prepare_alg(&inst->alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) EXPORT_SYMBOL_GPL(ahash_register_instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) struct crypto_alg *alg = &halg->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) if (alg->cra_type != &crypto_ahash_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) return crypto_shash_alg_has_setkey(__crypto_shash_alg(alg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) return __crypto_ahash_alg(alg)->setkey != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) EXPORT_SYMBOL_GPL(crypto_hash_alg_has_setkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) MODULE_DESCRIPTION("Asynchronous cryptographic hash type");