xref: /openbmc/linux/crypto/ahash.c (revision 84a9c938e5063709f799fd6bab17a8ea723c2eb5)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2004a403cSLoc Ho /*
3004a403cSLoc Ho  * Asynchronous Cryptographic Hash operations.
4004a403cSLoc Ho  *
5004a403cSLoc Ho  * This is the asynchronous version of hash.c with notification of
6004a403cSLoc Ho  * completion via a callback.
7004a403cSLoc Ho  *
8004a403cSLoc Ho  * Copyright (c) 2008 Loc Ho <lho@amcc.com>
9004a403cSLoc Ho  */
10004a403cSLoc Ho 
1120036252SHerbert Xu #include <crypto/internal/hash.h>
1220036252SHerbert Xu #include <crypto/scatterwalk.h>
1375ecb231SHerbert Xu #include <linux/bug.h>
14004a403cSLoc Ho #include <linux/err.h>
15004a403cSLoc Ho #include <linux/kernel.h>
16004a403cSLoc Ho #include <linux/module.h>
17004a403cSLoc Ho #include <linux/sched.h>
18004a403cSLoc Ho #include <linux/slab.h>
19004a403cSLoc Ho #include <linux/seq_file.h>
206238cbaeSSteffen Klassert #include <linux/cryptouser.h>
21d8c34b94SGideon Israel Dsouza #include <linux/compiler.h>
226238cbaeSSteffen Klassert #include <net/netlink.h>
23004a403cSLoc Ho 
24004a403cSLoc Ho #include "internal.h"
25004a403cSLoc Ho 
2666f6ce5eSHerbert Xu struct ahash_request_priv {
2766f6ce5eSHerbert Xu 	crypto_completion_t complete;
2866f6ce5eSHerbert Xu 	void *data;
2966f6ce5eSHerbert Xu 	u8 *result;
30ef0579b6SHerbert Xu 	u32 flags;
3166f6ce5eSHerbert Xu 	void *ubuf[] CRYPTO_MINALIGN_ATTR;
3266f6ce5eSHerbert Xu };
3366f6ce5eSHerbert Xu 
3488056ec3SHerbert Xu static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
3588056ec3SHerbert Xu {
3688056ec3SHerbert Xu 	return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
3788056ec3SHerbert Xu 			    halg);
3888056ec3SHerbert Xu }
3988056ec3SHerbert Xu 
4020036252SHerbert Xu static int hash_walk_next(struct crypto_hash_walk *walk)
4120036252SHerbert Xu {
4220036252SHerbert Xu 	unsigned int alignmask = walk->alignmask;
4320036252SHerbert Xu 	unsigned int offset = walk->offset;
4420036252SHerbert Xu 	unsigned int nbytes = min(walk->entrylen,
4520036252SHerbert Xu 				  ((unsigned int)(PAGE_SIZE)) - offset);
4620036252SHerbert Xu 
4775ecb231SHerbert Xu 	if (walk->flags & CRYPTO_ALG_ASYNC)
4875ecb231SHerbert Xu 		walk->data = kmap(walk->pg);
4975ecb231SHerbert Xu 	else
50f0dfc0b0SCong Wang 		walk->data = kmap_atomic(walk->pg);
5120036252SHerbert Xu 	walk->data += offset;
5220036252SHerbert Xu 
5323a75eeeSSzilveszter Ördög 	if (offset & alignmask) {
5423a75eeeSSzilveszter Ördög 		unsigned int unaligned = alignmask + 1 - (offset & alignmask);
55b516d514SJoshua I. James 
5623a75eeeSSzilveszter Ördög 		if (nbytes > unaligned)
5723a75eeeSSzilveszter Ördög 			nbytes = unaligned;
5823a75eeeSSzilveszter Ördög 	}
5920036252SHerbert Xu 
6020036252SHerbert Xu 	walk->entrylen -= nbytes;
6120036252SHerbert Xu 	return nbytes;
6220036252SHerbert Xu }
6320036252SHerbert Xu 
6420036252SHerbert Xu static int hash_walk_new_entry(struct crypto_hash_walk *walk)
6520036252SHerbert Xu {
6620036252SHerbert Xu 	struct scatterlist *sg;
6720036252SHerbert Xu 
6820036252SHerbert Xu 	sg = walk->sg;
6920036252SHerbert Xu 	walk->offset = sg->offset;
7013f4bb78SHerbert Xu 	walk->pg = sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT);
7113f4bb78SHerbert Xu 	walk->offset = offset_in_page(walk->offset);
7220036252SHerbert Xu 	walk->entrylen = sg->length;
7320036252SHerbert Xu 
7420036252SHerbert Xu 	if (walk->entrylen > walk->total)
7520036252SHerbert Xu 		walk->entrylen = walk->total;
7620036252SHerbert Xu 	walk->total -= walk->entrylen;
7720036252SHerbert Xu 
7820036252SHerbert Xu 	return hash_walk_next(walk);
7920036252SHerbert Xu }
8020036252SHerbert Xu 
8120036252SHerbert Xu int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
8220036252SHerbert Xu {
8320036252SHerbert Xu 	unsigned int alignmask = walk->alignmask;
8420036252SHerbert Xu 
8520036252SHerbert Xu 	walk->data -= walk->offset;
8620036252SHerbert Xu 
8777568e53SEric Biggers 	if (walk->entrylen && (walk->offset & alignmask) && !err) {
8877568e53SEric Biggers 		unsigned int nbytes;
8920036252SHerbert Xu 
9077568e53SEric Biggers 		walk->offset = ALIGN(walk->offset, alignmask + 1);
9177568e53SEric Biggers 		nbytes = min(walk->entrylen,
9277568e53SEric Biggers 			     (unsigned int)(PAGE_SIZE - walk->offset));
93900a081fSHerbert Xu 		if (nbytes) {
9477568e53SEric Biggers 			walk->entrylen -= nbytes;
95900a081fSHerbert Xu 			walk->data += walk->offset;
9620036252SHerbert Xu 			return nbytes;
9720036252SHerbert Xu 		}
98900a081fSHerbert Xu 	}
9920036252SHerbert Xu 
10075ecb231SHerbert Xu 	if (walk->flags & CRYPTO_ALG_ASYNC)
10175ecb231SHerbert Xu 		kunmap(walk->pg);
10275ecb231SHerbert Xu 	else {
103f0dfc0b0SCong Wang 		kunmap_atomic(walk->data);
10475ecb231SHerbert Xu 		/*
10575ecb231SHerbert Xu 		 * The may sleep test only makes sense for sync users.
10675ecb231SHerbert Xu 		 * Async users don't need to sleep here anyway.
10775ecb231SHerbert Xu 		 */
10820036252SHerbert Xu 		crypto_yield(walk->flags);
10975ecb231SHerbert Xu 	}
11020036252SHerbert Xu 
11120036252SHerbert Xu 	if (err)
11220036252SHerbert Xu 		return err;
11320036252SHerbert Xu 
11477568e53SEric Biggers 	if (walk->entrylen) {
11520036252SHerbert Xu 		walk->offset = 0;
116d315a0e0SHerbert Xu 		walk->pg++;
11720036252SHerbert Xu 		return hash_walk_next(walk);
118d315a0e0SHerbert Xu 	}
11920036252SHerbert Xu 
12020036252SHerbert Xu 	if (!walk->total)
12120036252SHerbert Xu 		return 0;
12220036252SHerbert Xu 
1235be4d4c9SCristian Stoica 	walk->sg = sg_next(walk->sg);
12420036252SHerbert Xu 
12520036252SHerbert Xu 	return hash_walk_new_entry(walk);
12620036252SHerbert Xu }
12720036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
12820036252SHerbert Xu 
12920036252SHerbert Xu int crypto_hash_walk_first(struct ahash_request *req,
13020036252SHerbert Xu 			   struct crypto_hash_walk *walk)
13120036252SHerbert Xu {
13220036252SHerbert Xu 	walk->total = req->nbytes;
13320036252SHerbert Xu 
1346d9529c5STim Chen 	if (!walk->total) {
1356d9529c5STim Chen 		walk->entrylen = 0;
13620036252SHerbert Xu 		return 0;
1376d9529c5STim Chen 	}
13820036252SHerbert Xu 
13920036252SHerbert Xu 	walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
14020036252SHerbert Xu 	walk->sg = req->src;
14175ecb231SHerbert Xu 	walk->flags = req->base.flags & CRYPTO_TFM_REQ_MASK;
14220036252SHerbert Xu 
14320036252SHerbert Xu 	return hash_walk_new_entry(walk);
14420036252SHerbert Xu }
14520036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
14620036252SHerbert Xu 
14775ecb231SHerbert Xu int crypto_ahash_walk_first(struct ahash_request *req,
14875ecb231SHerbert Xu 			    struct crypto_hash_walk *walk)
14975ecb231SHerbert Xu {
15075ecb231SHerbert Xu 	walk->total = req->nbytes;
15175ecb231SHerbert Xu 
1526d9529c5STim Chen 	if (!walk->total) {
1536d9529c5STim Chen 		walk->entrylen = 0;
15475ecb231SHerbert Xu 		return 0;
1556d9529c5STim Chen 	}
15675ecb231SHerbert Xu 
15775ecb231SHerbert Xu 	walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
15875ecb231SHerbert Xu 	walk->sg = req->src;
15975ecb231SHerbert Xu 	walk->flags = req->base.flags & CRYPTO_TFM_REQ_MASK;
16075ecb231SHerbert Xu 	walk->flags |= CRYPTO_ALG_ASYNC;
16175ecb231SHerbert Xu 
16275ecb231SHerbert Xu 	BUILD_BUG_ON(CRYPTO_TFM_REQ_MASK & CRYPTO_ALG_ASYNC);
16375ecb231SHerbert Xu 
16475ecb231SHerbert Xu 	return hash_walk_new_entry(walk);
16575ecb231SHerbert Xu }
16675ecb231SHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_walk_first);
16775ecb231SHerbert Xu 
168004a403cSLoc Ho static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
169004a403cSLoc Ho 				unsigned int keylen)
170004a403cSLoc Ho {
171004a403cSLoc Ho 	unsigned long alignmask = crypto_ahash_alignmask(tfm);
172004a403cSLoc Ho 	int ret;
173004a403cSLoc Ho 	u8 *buffer, *alignbuffer;
174004a403cSLoc Ho 	unsigned long absize;
175004a403cSLoc Ho 
176004a403cSLoc Ho 	absize = keylen + alignmask;
177093900c2SHerbert Xu 	buffer = kmalloc(absize, GFP_KERNEL);
178004a403cSLoc Ho 	if (!buffer)
179004a403cSLoc Ho 		return -ENOMEM;
180004a403cSLoc Ho 
181004a403cSLoc Ho 	alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
182004a403cSLoc Ho 	memcpy(alignbuffer, key, keylen);
183a70c5225SHerbert Xu 	ret = tfm->setkey(tfm, alignbuffer, keylen);
1848c32c516SHerbert Xu 	kzfree(buffer);
185004a403cSLoc Ho 	return ret;
186004a403cSLoc Ho }
187004a403cSLoc Ho 
188ba7d7433SEric Biggers static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
189ba7d7433SEric Biggers 			  unsigned int keylen)
190ba7d7433SEric Biggers {
191ba7d7433SEric Biggers 	return -ENOSYS;
192ba7d7433SEric Biggers }
193ba7d7433SEric Biggers 
194ba7d7433SEric Biggers static void ahash_set_needkey(struct crypto_ahash *tfm)
195ba7d7433SEric Biggers {
196ba7d7433SEric Biggers 	const struct hash_alg_common *alg = crypto_hash_alg_common(tfm);
197ba7d7433SEric Biggers 
198ba7d7433SEric Biggers 	if (tfm->setkey != ahash_nosetkey &&
199ba7d7433SEric Biggers 	    !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
200ba7d7433SEric Biggers 		crypto_ahash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
201ba7d7433SEric Biggers }
202ba7d7433SEric Biggers 
20366f6ce5eSHerbert Xu int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
204004a403cSLoc Ho 			unsigned int keylen)
205004a403cSLoc Ho {
206004a403cSLoc Ho 	unsigned long alignmask = crypto_ahash_alignmask(tfm);
2079fa68f62SEric Biggers 	int err;
208004a403cSLoc Ho 
209004a403cSLoc Ho 	if ((unsigned long)key & alignmask)
2109fa68f62SEric Biggers 		err = ahash_setkey_unaligned(tfm, key, keylen);
2119fa68f62SEric Biggers 	else
2129fa68f62SEric Biggers 		err = tfm->setkey(tfm, key, keylen);
213004a403cSLoc Ho 
214ba7d7433SEric Biggers 	if (unlikely(err)) {
215ba7d7433SEric Biggers 		ahash_set_needkey(tfm);
2169fa68f62SEric Biggers 		return err;
217ba7d7433SEric Biggers 	}
2189fa68f62SEric Biggers 
2199fa68f62SEric Biggers 	crypto_ahash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
2209fa68f62SEric Biggers 	return 0;
221004a403cSLoc Ho }
22266f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
223004a403cSLoc Ho 
22466f6ce5eSHerbert Xu static inline unsigned int ahash_align_buffer_size(unsigned len,
22566f6ce5eSHerbert Xu 						   unsigned long mask)
22666f6ce5eSHerbert Xu {
22766f6ce5eSHerbert Xu 	return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
22866f6ce5eSHerbert Xu }
22966f6ce5eSHerbert Xu 
2301ffc9fbdSMarek Vasut static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
23166f6ce5eSHerbert Xu {
23266f6ce5eSHerbert Xu 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
23366f6ce5eSHerbert Xu 	unsigned long alignmask = crypto_ahash_alignmask(tfm);
23466f6ce5eSHerbert Xu 	unsigned int ds = crypto_ahash_digestsize(tfm);
23566f6ce5eSHerbert Xu 	struct ahash_request_priv *priv;
23666f6ce5eSHerbert Xu 
23766f6ce5eSHerbert Xu 	priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
23866f6ce5eSHerbert Xu 		       (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
2395befbd5aSSteffen Klassert 		       GFP_KERNEL : GFP_ATOMIC);
24066f6ce5eSHerbert Xu 	if (!priv)
24166f6ce5eSHerbert Xu 		return -ENOMEM;
24266f6ce5eSHerbert Xu 
243ab6bf4e5SMarek Vasut 	/*
244ab6bf4e5SMarek Vasut 	 * WARNING: Voodoo programming below!
245ab6bf4e5SMarek Vasut 	 *
246ab6bf4e5SMarek Vasut 	 * The code below is obscure and hard to understand, thus explanation
247ab6bf4e5SMarek Vasut 	 * is necessary. See include/crypto/hash.h and include/linux/crypto.h
248ab6bf4e5SMarek Vasut 	 * to understand the layout of structures used here!
249ab6bf4e5SMarek Vasut 	 *
250ab6bf4e5SMarek Vasut 	 * The code here will replace portions of the ORIGINAL request with
251ab6bf4e5SMarek Vasut 	 * pointers to new code and buffers so the hashing operation can store
252ab6bf4e5SMarek Vasut 	 * the result in aligned buffer. We will call the modified request
253ab6bf4e5SMarek Vasut 	 * an ADJUSTED request.
254ab6bf4e5SMarek Vasut 	 *
255ab6bf4e5SMarek Vasut 	 * The newly mangled request will look as such:
256ab6bf4e5SMarek Vasut 	 *
257ab6bf4e5SMarek Vasut 	 * req {
258ab6bf4e5SMarek Vasut 	 *   .result        = ADJUSTED[new aligned buffer]
259ab6bf4e5SMarek Vasut 	 *   .base.complete = ADJUSTED[pointer to completion function]
260ab6bf4e5SMarek Vasut 	 *   .base.data     = ADJUSTED[*req (pointer to self)]
261ab6bf4e5SMarek Vasut 	 *   .priv          = ADJUSTED[new priv] {
262ab6bf4e5SMarek Vasut 	 *           .result   = ORIGINAL(result)
263ab6bf4e5SMarek Vasut 	 *           .complete = ORIGINAL(base.complete)
264ab6bf4e5SMarek Vasut 	 *           .data     = ORIGINAL(base.data)
265ab6bf4e5SMarek Vasut 	 *   }
266ab6bf4e5SMarek Vasut 	 */
267ab6bf4e5SMarek Vasut 
26866f6ce5eSHerbert Xu 	priv->result = req->result;
26966f6ce5eSHerbert Xu 	priv->complete = req->base.complete;
27066f6ce5eSHerbert Xu 	priv->data = req->base.data;
271ef0579b6SHerbert Xu 	priv->flags = req->base.flags;
272ef0579b6SHerbert Xu 
273ab6bf4e5SMarek Vasut 	/*
274ab6bf4e5SMarek Vasut 	 * WARNING: We do not backup req->priv here! The req->priv
275ab6bf4e5SMarek Vasut 	 *          is for internal use of the Crypto API and the
276ab6bf4e5SMarek Vasut 	 *          user must _NOT_ _EVER_ depend on it's content!
277ab6bf4e5SMarek Vasut 	 */
27866f6ce5eSHerbert Xu 
27966f6ce5eSHerbert Xu 	req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
2801ffc9fbdSMarek Vasut 	req->base.complete = cplt;
28166f6ce5eSHerbert Xu 	req->base.data = req;
28266f6ce5eSHerbert Xu 	req->priv = priv;
28366f6ce5eSHerbert Xu 
2841ffc9fbdSMarek Vasut 	return 0;
2851ffc9fbdSMarek Vasut }
2861ffc9fbdSMarek Vasut 
287ef0579b6SHerbert Xu static void ahash_restore_req(struct ahash_request *req, int err)
2881ffc9fbdSMarek Vasut {
2891ffc9fbdSMarek Vasut 	struct ahash_request_priv *priv = req->priv;
2901ffc9fbdSMarek Vasut 
291ef0579b6SHerbert Xu 	if (!err)
292ef0579b6SHerbert Xu 		memcpy(priv->result, req->result,
293ef0579b6SHerbert Xu 		       crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
294ef0579b6SHerbert Xu 
2951ffc9fbdSMarek Vasut 	/* Restore the original crypto request. */
2961ffc9fbdSMarek Vasut 	req->result = priv->result;
297ef0579b6SHerbert Xu 
298ef0579b6SHerbert Xu 	ahash_request_set_callback(req, priv->flags,
299ef0579b6SHerbert Xu 				   priv->complete, priv->data);
3001ffc9fbdSMarek Vasut 	req->priv = NULL;
3011ffc9fbdSMarek Vasut 
3021ffc9fbdSMarek Vasut 	/* Free the req->priv.priv from the ADJUSTED request. */
3031ffc9fbdSMarek Vasut 	kzfree(priv);
3041ffc9fbdSMarek Vasut }
3051ffc9fbdSMarek Vasut 
306ef0579b6SHerbert Xu static void ahash_notify_einprogress(struct ahash_request *req)
3071ffc9fbdSMarek Vasut {
3081ffc9fbdSMarek Vasut 	struct ahash_request_priv *priv = req->priv;
309ef0579b6SHerbert Xu 	struct crypto_async_request oreq;
3101ffc9fbdSMarek Vasut 
311ef0579b6SHerbert Xu 	oreq.data = priv->data;
3121ffc9fbdSMarek Vasut 
313ef0579b6SHerbert Xu 	priv->complete(&oreq, -EINPROGRESS);
3141ffc9fbdSMarek Vasut }
3151ffc9fbdSMarek Vasut 
3161ffc9fbdSMarek Vasut static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
3171ffc9fbdSMarek Vasut {
3181ffc9fbdSMarek Vasut 	struct ahash_request *areq = req->data;
3191ffc9fbdSMarek Vasut 
320ef0579b6SHerbert Xu 	if (err == -EINPROGRESS) {
321ef0579b6SHerbert Xu 		ahash_notify_einprogress(areq);
322ef0579b6SHerbert Xu 		return;
323ef0579b6SHerbert Xu 	}
324ef0579b6SHerbert Xu 
3251ffc9fbdSMarek Vasut 	/*
3261ffc9fbdSMarek Vasut 	 * Restore the original request, see ahash_op_unaligned() for what
3271ffc9fbdSMarek Vasut 	 * goes where.
3281ffc9fbdSMarek Vasut 	 *
3291ffc9fbdSMarek Vasut 	 * The "struct ahash_request *req" here is in fact the "req.base"
3301ffc9fbdSMarek Vasut 	 * from the ADJUSTED request from ahash_op_unaligned(), thus as it
3311ffc9fbdSMarek Vasut 	 * is a pointer to self, it is also the ADJUSTED "req" .
3321ffc9fbdSMarek Vasut 	 */
3331ffc9fbdSMarek Vasut 
3341ffc9fbdSMarek Vasut 	/* First copy req->result into req->priv.result */
335ef0579b6SHerbert Xu 	ahash_restore_req(areq, err);
3361ffc9fbdSMarek Vasut 
3371ffc9fbdSMarek Vasut 	/* Complete the ORIGINAL request. */
3381ffc9fbdSMarek Vasut 	areq->base.complete(&areq->base, err);
3391ffc9fbdSMarek Vasut }
3401ffc9fbdSMarek Vasut 
3411ffc9fbdSMarek Vasut static int ahash_op_unaligned(struct ahash_request *req,
3421ffc9fbdSMarek Vasut 			      int (*op)(struct ahash_request *))
3431ffc9fbdSMarek Vasut {
3441ffc9fbdSMarek Vasut 	int err;
3451ffc9fbdSMarek Vasut 
3461ffc9fbdSMarek Vasut 	err = ahash_save_req(req, ahash_op_unaligned_done);
3471ffc9fbdSMarek Vasut 	if (err)
3481ffc9fbdSMarek Vasut 		return err;
3491ffc9fbdSMarek Vasut 
35066f6ce5eSHerbert Xu 	err = op(req);
3514e5b0ad5SGilad Ben-Yossef 	if (err == -EINPROGRESS || err == -EBUSY)
352ef0579b6SHerbert Xu 		return err;
353ef0579b6SHerbert Xu 
354ef0579b6SHerbert Xu 	ahash_restore_req(req, err);
35566f6ce5eSHerbert Xu 
35666f6ce5eSHerbert Xu 	return err;
35766f6ce5eSHerbert Xu }
35866f6ce5eSHerbert Xu 
35966f6ce5eSHerbert Xu static int crypto_ahash_op(struct ahash_request *req,
36066f6ce5eSHerbert Xu 			   int (*op)(struct ahash_request *))
36166f6ce5eSHerbert Xu {
36266f6ce5eSHerbert Xu 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
36366f6ce5eSHerbert Xu 	unsigned long alignmask = crypto_ahash_alignmask(tfm);
36466f6ce5eSHerbert Xu 
36566f6ce5eSHerbert Xu 	if ((unsigned long)req->result & alignmask)
36666f6ce5eSHerbert Xu 		return ahash_op_unaligned(req, op);
36766f6ce5eSHerbert Xu 
36866f6ce5eSHerbert Xu 	return op(req);
36966f6ce5eSHerbert Xu }
37066f6ce5eSHerbert Xu 
37166f6ce5eSHerbert Xu int crypto_ahash_final(struct ahash_request *req)
37266f6ce5eSHerbert Xu {
373f7d76e05SCorentin Labbe 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
374f7d76e05SCorentin Labbe 	struct crypto_alg *alg = tfm->base.__crt_alg;
375f7d76e05SCorentin Labbe 	unsigned int nbytes = req->nbytes;
376cac5818cSCorentin Labbe 	int ret;
377cac5818cSCorentin Labbe 
378f7d76e05SCorentin Labbe 	crypto_stats_get(alg);
379cac5818cSCorentin Labbe 	ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
380f7d76e05SCorentin Labbe 	crypto_stats_ahash_final(nbytes, ret, alg);
381cac5818cSCorentin Labbe 	return ret;
38266f6ce5eSHerbert Xu }
38366f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_final);
38466f6ce5eSHerbert Xu 
38566f6ce5eSHerbert Xu int crypto_ahash_finup(struct ahash_request *req)
38666f6ce5eSHerbert Xu {
387f7d76e05SCorentin Labbe 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
388f7d76e05SCorentin Labbe 	struct crypto_alg *alg = tfm->base.__crt_alg;
389f7d76e05SCorentin Labbe 	unsigned int nbytes = req->nbytes;
390cac5818cSCorentin Labbe 	int ret;
391cac5818cSCorentin Labbe 
392f7d76e05SCorentin Labbe 	crypto_stats_get(alg);
393cac5818cSCorentin Labbe 	ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
394f7d76e05SCorentin Labbe 	crypto_stats_ahash_final(nbytes, ret, alg);
395cac5818cSCorentin Labbe 	return ret;
39666f6ce5eSHerbert Xu }
39766f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_finup);
39866f6ce5eSHerbert Xu 
39966f6ce5eSHerbert Xu int crypto_ahash_digest(struct ahash_request *req)
40066f6ce5eSHerbert Xu {
4019fa68f62SEric Biggers 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
402f7d76e05SCorentin Labbe 	struct crypto_alg *alg = tfm->base.__crt_alg;
403f7d76e05SCorentin Labbe 	unsigned int nbytes = req->nbytes;
404cac5818cSCorentin Labbe 	int ret;
4059fa68f62SEric Biggers 
406f7d76e05SCorentin Labbe 	crypto_stats_get(alg);
4079fa68f62SEric Biggers 	if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
408cac5818cSCorentin Labbe 		ret = -ENOKEY;
409cac5818cSCorentin Labbe 	else
410cac5818cSCorentin Labbe 		ret = crypto_ahash_op(req, tfm->digest);
411f7d76e05SCorentin Labbe 	crypto_stats_ahash_final(nbytes, ret, alg);
412cac5818cSCorentin Labbe 	return ret;
41366f6ce5eSHerbert Xu }
41466f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_digest);
41566f6ce5eSHerbert Xu 
41666f6ce5eSHerbert Xu static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
41766f6ce5eSHerbert Xu {
41866f6ce5eSHerbert Xu 	struct ahash_request *areq = req->data;
41966f6ce5eSHerbert Xu 
420ef0579b6SHerbert Xu 	if (err == -EINPROGRESS)
421ef0579b6SHerbert Xu 		return;
422ef0579b6SHerbert Xu 
423ef0579b6SHerbert Xu 	ahash_restore_req(areq, err);
42466f6ce5eSHerbert Xu 
425d4a7a0fbSMarek Vasut 	areq->base.complete(&areq->base, err);
42666f6ce5eSHerbert Xu }
42766f6ce5eSHerbert Xu 
42866f6ce5eSHerbert Xu static int ahash_def_finup_finish1(struct ahash_request *req, int err)
42966f6ce5eSHerbert Xu {
43066f6ce5eSHerbert Xu 	if (err)
43166f6ce5eSHerbert Xu 		goto out;
43266f6ce5eSHerbert Xu 
43366f6ce5eSHerbert Xu 	req->base.complete = ahash_def_finup_done2;
434ef0579b6SHerbert Xu 
43566f6ce5eSHerbert Xu 	err = crypto_ahash_reqtfm(req)->final(req);
4364e5b0ad5SGilad Ben-Yossef 	if (err == -EINPROGRESS || err == -EBUSY)
437ef0579b6SHerbert Xu 		return err;
43866f6ce5eSHerbert Xu 
43966f6ce5eSHerbert Xu out:
440ef0579b6SHerbert Xu 	ahash_restore_req(req, err);
44166f6ce5eSHerbert Xu 	return err;
44266f6ce5eSHerbert Xu }
44366f6ce5eSHerbert Xu 
44466f6ce5eSHerbert Xu static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
44566f6ce5eSHerbert Xu {
44666f6ce5eSHerbert Xu 	struct ahash_request *areq = req->data;
44766f6ce5eSHerbert Xu 
448ef0579b6SHerbert Xu 	if (err == -EINPROGRESS) {
449ef0579b6SHerbert Xu 		ahash_notify_einprogress(areq);
450ef0579b6SHerbert Xu 		return;
451ef0579b6SHerbert Xu 	}
452ef0579b6SHerbert Xu 
453ef0579b6SHerbert Xu 	areq->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
454ef0579b6SHerbert Xu 
45566f6ce5eSHerbert Xu 	err = ahash_def_finup_finish1(areq, err);
456ef0579b6SHerbert Xu 	if (areq->priv)
457ef0579b6SHerbert Xu 		return;
45866f6ce5eSHerbert Xu 
459d4a7a0fbSMarek Vasut 	areq->base.complete(&areq->base, err);
46066f6ce5eSHerbert Xu }
46166f6ce5eSHerbert Xu 
46266f6ce5eSHerbert Xu static int ahash_def_finup(struct ahash_request *req)
46366f6ce5eSHerbert Xu {
46466f6ce5eSHerbert Xu 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
465d4a7a0fbSMarek Vasut 	int err;
46666f6ce5eSHerbert Xu 
467d4a7a0fbSMarek Vasut 	err = ahash_save_req(req, ahash_def_finup_done1);
468d4a7a0fbSMarek Vasut 	if (err)
469d4a7a0fbSMarek Vasut 		return err;
47066f6ce5eSHerbert Xu 
471d4a7a0fbSMarek Vasut 	err = tfm->update(req);
4724e5b0ad5SGilad Ben-Yossef 	if (err == -EINPROGRESS || err == -EBUSY)
473ef0579b6SHerbert Xu 		return err;
474ef0579b6SHerbert Xu 
475d4a7a0fbSMarek Vasut 	return ahash_def_finup_finish1(req, err);
47666f6ce5eSHerbert Xu }
47766f6ce5eSHerbert Xu 
47888056ec3SHerbert Xu static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
47988056ec3SHerbert Xu {
48088056ec3SHerbert Xu 	struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
48188056ec3SHerbert Xu 	struct ahash_alg *alg = crypto_ahash_alg(hash);
48288056ec3SHerbert Xu 
48366f6ce5eSHerbert Xu 	hash->setkey = ahash_nosetkey;
48466f6ce5eSHerbert Xu 
48588056ec3SHerbert Xu 	if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
48688056ec3SHerbert Xu 		return crypto_init_shash_ops_async(tfm);
48788056ec3SHerbert Xu 
48888056ec3SHerbert Xu 	hash->init = alg->init;
48988056ec3SHerbert Xu 	hash->update = alg->update;
49088056ec3SHerbert Xu 	hash->final = alg->final;
49166f6ce5eSHerbert Xu 	hash->finup = alg->finup ?: ahash_def_finup;
49288056ec3SHerbert Xu 	hash->digest = alg->digest;
4936f221f7eSKamil Konieczny 	hash->export = alg->export;
4946f221f7eSKamil Konieczny 	hash->import = alg->import;
49566f6ce5eSHerbert Xu 
496a5596d63SHerbert Xu 	if (alg->setkey) {
49766f6ce5eSHerbert Xu 		hash->setkey = alg->setkey;
498ba7d7433SEric Biggers 		ahash_set_needkey(hash);
499a5596d63SHerbert Xu 	}
50088056ec3SHerbert Xu 
50188056ec3SHerbert Xu 	return 0;
50288056ec3SHerbert Xu }
50388056ec3SHerbert Xu 
50488056ec3SHerbert Xu static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
50588056ec3SHerbert Xu {
5062495cf25SHerbert Xu 	if (alg->cra_type != &crypto_ahash_type)
50788056ec3SHerbert Xu 		return sizeof(struct crypto_shash *);
5082495cf25SHerbert Xu 
5092495cf25SHerbert Xu 	return crypto_alg_extsize(alg);
51088056ec3SHerbert Xu }
51188056ec3SHerbert Xu 
5123acc8473SHerbert Xu #ifdef CONFIG_NET
5136238cbaeSSteffen Klassert static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
5146238cbaeSSteffen Klassert {
5156238cbaeSSteffen Klassert 	struct crypto_report_hash rhash;
5166238cbaeSSteffen Klassert 
51737db69e0SEric Biggers 	memset(&rhash, 0, sizeof(rhash));
51837db69e0SEric Biggers 
51937db69e0SEric Biggers 	strscpy(rhash.type, "ahash", sizeof(rhash.type));
5206238cbaeSSteffen Klassert 
5216238cbaeSSteffen Klassert 	rhash.blocksize = alg->cra_blocksize;
5226238cbaeSSteffen Klassert 	rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
5236238cbaeSSteffen Klassert 
52437db69e0SEric Biggers 	return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash);
5256238cbaeSSteffen Klassert }
5263acc8473SHerbert Xu #else
5273acc8473SHerbert Xu static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
5283acc8473SHerbert Xu {
5293acc8473SHerbert Xu 	return -ENOSYS;
5303acc8473SHerbert Xu }
5313acc8473SHerbert Xu #endif
5326238cbaeSSteffen Klassert 
533004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
534d8c34b94SGideon Israel Dsouza 	__maybe_unused;
535004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
536004a403cSLoc Ho {
537004a403cSLoc Ho 	seq_printf(m, "type         : ahash\n");
538004a403cSLoc Ho 	seq_printf(m, "async        : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
539004a403cSLoc Ho 					     "yes" : "no");
540004a403cSLoc Ho 	seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
54188056ec3SHerbert Xu 	seq_printf(m, "digestsize   : %u\n",
54288056ec3SHerbert Xu 		   __crypto_hash_alg_common(alg)->digestsize);
543004a403cSLoc Ho }
544004a403cSLoc Ho 
545004a403cSLoc Ho const struct crypto_type crypto_ahash_type = {
54688056ec3SHerbert Xu 	.extsize = crypto_ahash_extsize,
54788056ec3SHerbert Xu 	.init_tfm = crypto_ahash_init_tfm,
548004a403cSLoc Ho #ifdef CONFIG_PROC_FS
549004a403cSLoc Ho 	.show = crypto_ahash_show,
550004a403cSLoc Ho #endif
5516238cbaeSSteffen Klassert 	.report = crypto_ahash_report,
55288056ec3SHerbert Xu 	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
55388056ec3SHerbert Xu 	.maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
55488056ec3SHerbert Xu 	.type = CRYPTO_ALG_TYPE_AHASH,
55588056ec3SHerbert Xu 	.tfmsize = offsetof(struct crypto_ahash, base),
556004a403cSLoc Ho };
557004a403cSLoc Ho EXPORT_SYMBOL_GPL(crypto_ahash_type);
558004a403cSLoc Ho 
559*84a9c938SEric Biggers int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
560*84a9c938SEric Biggers 		      struct crypto_instance *inst,
561*84a9c938SEric Biggers 		      const char *name, u32 type, u32 mask)
562*84a9c938SEric Biggers {
563*84a9c938SEric Biggers 	spawn->base.frontend = &crypto_ahash_type;
564*84a9c938SEric Biggers 	return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
565*84a9c938SEric Biggers }
566*84a9c938SEric Biggers EXPORT_SYMBOL_GPL(crypto_grab_ahash);
567*84a9c938SEric Biggers 
56888056ec3SHerbert Xu struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
56988056ec3SHerbert Xu 					u32 mask)
57088056ec3SHerbert Xu {
57188056ec3SHerbert Xu 	return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
57288056ec3SHerbert Xu }
57388056ec3SHerbert Xu EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
57488056ec3SHerbert Xu 
5758d18e34cSHerbert Xu int crypto_has_ahash(const char *alg_name, u32 type, u32 mask)
5768d18e34cSHerbert Xu {
5778d18e34cSHerbert Xu 	return crypto_type_has_alg(alg_name, &crypto_ahash_type, type, mask);
5788d18e34cSHerbert Xu }
5798d18e34cSHerbert Xu EXPORT_SYMBOL_GPL(crypto_has_ahash);
5808d18e34cSHerbert Xu 
58101c2deceSHerbert Xu static int ahash_prepare_alg(struct ahash_alg *alg)
58201c2deceSHerbert Xu {
58301c2deceSHerbert Xu 	struct crypto_alg *base = &alg->halg.base;
58401c2deceSHerbert Xu 
585b68a7ec1SKees Cook 	if (alg->halg.digestsize > HASH_MAX_DIGESTSIZE ||
586b68a7ec1SKees Cook 	    alg->halg.statesize > HASH_MAX_STATESIZE ||
5878996eafdSRussell King 	    alg->halg.statesize == 0)
58801c2deceSHerbert Xu 		return -EINVAL;
58901c2deceSHerbert Xu 
59001c2deceSHerbert Xu 	base->cra_type = &crypto_ahash_type;
59101c2deceSHerbert Xu 	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
59201c2deceSHerbert Xu 	base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
59301c2deceSHerbert Xu 
59401c2deceSHerbert Xu 	return 0;
59501c2deceSHerbert Xu }
59601c2deceSHerbert Xu 
59701c2deceSHerbert Xu int crypto_register_ahash(struct ahash_alg *alg)
59801c2deceSHerbert Xu {
59901c2deceSHerbert Xu 	struct crypto_alg *base = &alg->halg.base;
60001c2deceSHerbert Xu 	int err;
60101c2deceSHerbert Xu 
60201c2deceSHerbert Xu 	err = ahash_prepare_alg(alg);
60301c2deceSHerbert Xu 	if (err)
60401c2deceSHerbert Xu 		return err;
60501c2deceSHerbert Xu 
60601c2deceSHerbert Xu 	return crypto_register_alg(base);
60701c2deceSHerbert Xu }
60801c2deceSHerbert Xu EXPORT_SYMBOL_GPL(crypto_register_ahash);
60901c2deceSHerbert Xu 
610c6d633a9SEric Biggers void crypto_unregister_ahash(struct ahash_alg *alg)
61101c2deceSHerbert Xu {
612c6d633a9SEric Biggers 	crypto_unregister_alg(&alg->halg.base);
61301c2deceSHerbert Xu }
61401c2deceSHerbert Xu EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
61501c2deceSHerbert Xu 
6166f7473c5SRabin Vincent int crypto_register_ahashes(struct ahash_alg *algs, int count)
6176f7473c5SRabin Vincent {
6186f7473c5SRabin Vincent 	int i, ret;
6196f7473c5SRabin Vincent 
6206f7473c5SRabin Vincent 	for (i = 0; i < count; i++) {
6216f7473c5SRabin Vincent 		ret = crypto_register_ahash(&algs[i]);
6226f7473c5SRabin Vincent 		if (ret)
6236f7473c5SRabin Vincent 			goto err;
6246f7473c5SRabin Vincent 	}
6256f7473c5SRabin Vincent 
6266f7473c5SRabin Vincent 	return 0;
6276f7473c5SRabin Vincent 
6286f7473c5SRabin Vincent err:
6296f7473c5SRabin Vincent 	for (--i; i >= 0; --i)
6306f7473c5SRabin Vincent 		crypto_unregister_ahash(&algs[i]);
6316f7473c5SRabin Vincent 
6326f7473c5SRabin Vincent 	return ret;
6336f7473c5SRabin Vincent }
6346f7473c5SRabin Vincent EXPORT_SYMBOL_GPL(crypto_register_ahashes);
6356f7473c5SRabin Vincent 
6366f7473c5SRabin Vincent void crypto_unregister_ahashes(struct ahash_alg *algs, int count)
6376f7473c5SRabin Vincent {
6386f7473c5SRabin Vincent 	int i;
6396f7473c5SRabin Vincent 
6406f7473c5SRabin Vincent 	for (i = count - 1; i >= 0; --i)
6416f7473c5SRabin Vincent 		crypto_unregister_ahash(&algs[i]);
6426f7473c5SRabin Vincent }
6436f7473c5SRabin Vincent EXPORT_SYMBOL_GPL(crypto_unregister_ahashes);
6446f7473c5SRabin Vincent 
64501c2deceSHerbert Xu int ahash_register_instance(struct crypto_template *tmpl,
64601c2deceSHerbert Xu 			    struct ahash_instance *inst)
64701c2deceSHerbert Xu {
64801c2deceSHerbert Xu 	int err;
64901c2deceSHerbert Xu 
65001c2deceSHerbert Xu 	err = ahash_prepare_alg(&inst->alg);
65101c2deceSHerbert Xu 	if (err)
65201c2deceSHerbert Xu 		return err;
65301c2deceSHerbert Xu 
65401c2deceSHerbert Xu 	return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
65501c2deceSHerbert Xu }
65601c2deceSHerbert Xu EXPORT_SYMBOL_GPL(ahash_register_instance);
65701c2deceSHerbert Xu 
65801c2deceSHerbert Xu void ahash_free_instance(struct crypto_instance *inst)
65901c2deceSHerbert Xu {
66001c2deceSHerbert Xu 	crypto_drop_spawn(crypto_instance_ctx(inst));
66101c2deceSHerbert Xu 	kfree(ahash_instance(inst));
66201c2deceSHerbert Xu }
66301c2deceSHerbert Xu EXPORT_SYMBOL_GPL(ahash_free_instance);
66401c2deceSHerbert Xu 
66501c2deceSHerbert Xu int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
66601c2deceSHerbert Xu 			    struct hash_alg_common *alg,
66701c2deceSHerbert Xu 			    struct crypto_instance *inst)
66801c2deceSHerbert Xu {
66901c2deceSHerbert Xu 	return crypto_init_spawn2(&spawn->base, &alg->base, inst,
67001c2deceSHerbert Xu 				  &crypto_ahash_type);
67101c2deceSHerbert Xu }
67201c2deceSHerbert Xu EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
67301c2deceSHerbert Xu 
67401c2deceSHerbert Xu struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
67501c2deceSHerbert Xu {
67601c2deceSHerbert Xu 	struct crypto_alg *alg;
67701c2deceSHerbert Xu 
67801c2deceSHerbert Xu 	alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
67901c2deceSHerbert Xu 	return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
68001c2deceSHerbert Xu }
68101c2deceSHerbert Xu EXPORT_SYMBOL_GPL(ahash_attr_alg);
68201c2deceSHerbert Xu 
683cd6ed77aSEric Biggers bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg)
684cd6ed77aSEric Biggers {
685cd6ed77aSEric Biggers 	struct crypto_alg *alg = &halg->base;
686cd6ed77aSEric Biggers 
687cd6ed77aSEric Biggers 	if (alg->cra_type != &crypto_ahash_type)
688cd6ed77aSEric Biggers 		return crypto_shash_alg_has_setkey(__crypto_shash_alg(alg));
689cd6ed77aSEric Biggers 
690cd6ed77aSEric Biggers 	return __crypto_ahash_alg(alg)->setkey != NULL;
691cd6ed77aSEric Biggers }
692cd6ed77aSEric Biggers EXPORT_SYMBOL_GPL(crypto_hash_alg_has_setkey);
693cd6ed77aSEric Biggers 
694004a403cSLoc Ho MODULE_LICENSE("GPL");
695004a403cSLoc Ho MODULE_DESCRIPTION("Asynchronous cryptographic hash type");
696