11b44c5a6SAntoine Ténart /*
21b44c5a6SAntoine Ténart  * Copyright (C) 2017 Marvell
31b44c5a6SAntoine Ténart  *
41b44c5a6SAntoine Ténart  * Antoine Tenart <antoine.tenart@free-electrons.com>
51b44c5a6SAntoine Ténart  *
61b44c5a6SAntoine Ténart  * This file is licensed under the terms of the GNU General Public
71b44c5a6SAntoine Ténart  * License version 2. This program is licensed "as is" without any
81b44c5a6SAntoine Ténart  * warranty of any kind, whether express or implied.
91b44c5a6SAntoine Ténart  */
101b44c5a6SAntoine Ténart 
11aed3731eSAntoine Ténart #include <crypto/hmac.h>
121b44c5a6SAntoine Ténart #include <crypto/sha.h>
131b44c5a6SAntoine Ténart #include <linux/device.h>
141b44c5a6SAntoine Ténart #include <linux/dma-mapping.h>
151b44c5a6SAntoine Ténart #include <linux/dmapool.h>
161b44c5a6SAntoine Ténart 
171b44c5a6SAntoine Ténart 
181b44c5a6SAntoine Ténart #include "safexcel.h"
191b44c5a6SAntoine Ténart 
201b44c5a6SAntoine Ténart struct safexcel_ahash_ctx {
211b44c5a6SAntoine Ténart 	struct safexcel_context base;
221b44c5a6SAntoine Ténart 	struct safexcel_crypto_priv *priv;
231b44c5a6SAntoine Ténart 
241b44c5a6SAntoine Ténart 	u32 alg;
251b44c5a6SAntoine Ténart 	u32 digest;
261b44c5a6SAntoine Ténart 
271b44c5a6SAntoine Ténart 	u32 ipad[SHA1_DIGEST_SIZE / sizeof(u32)];
281b44c5a6SAntoine Ténart 	u32 opad[SHA1_DIGEST_SIZE / sizeof(u32)];
291b44c5a6SAntoine Ténart };
301b44c5a6SAntoine Ténart 
311b44c5a6SAntoine Ténart struct safexcel_ahash_req {
321b44c5a6SAntoine Ténart 	bool last_req;
331b44c5a6SAntoine Ténart 	bool finish;
341b44c5a6SAntoine Ténart 	bool hmac;
351eb7b403SOfer Heifetz 	bool needs_inv;
361b44c5a6SAntoine Ténart 
371b44c5a6SAntoine Ténart 	u8 state_sz;    /* expected sate size, only set once */
382973633eSAntoine Ténart 	u32 state[SHA256_DIGEST_SIZE / sizeof(u32)] __aligned(sizeof(u32));
391b44c5a6SAntoine Ténart 
401b44c5a6SAntoine Ténart 	u64 len;
411b44c5a6SAntoine Ténart 	u64 processed;
421b44c5a6SAntoine Ténart 
431b44c5a6SAntoine Ténart 	u8 cache[SHA256_BLOCK_SIZE] __aligned(sizeof(u32));
441b44c5a6SAntoine Ténart 	u8 cache_next[SHA256_BLOCK_SIZE] __aligned(sizeof(u32));
451b44c5a6SAntoine Ténart };
461b44c5a6SAntoine Ténart 
471b44c5a6SAntoine Ténart struct safexcel_ahash_export_state {
481b44c5a6SAntoine Ténart 	u64 len;
491b44c5a6SAntoine Ténart 	u64 processed;
501b44c5a6SAntoine Ténart 
511b44c5a6SAntoine Ténart 	u32 state[SHA256_DIGEST_SIZE / sizeof(u32)];
521b44c5a6SAntoine Ténart 	u8 cache[SHA256_BLOCK_SIZE];
531b44c5a6SAntoine Ténart };
541b44c5a6SAntoine Ténart 
551b44c5a6SAntoine Ténart static void safexcel_hash_token(struct safexcel_command_desc *cdesc,
561b44c5a6SAntoine Ténart 				u32 input_length, u32 result_length)
571b44c5a6SAntoine Ténart {
581b44c5a6SAntoine Ténart 	struct safexcel_token *token =
591b44c5a6SAntoine Ténart 		(struct safexcel_token *)cdesc->control_data.token;
601b44c5a6SAntoine Ténart 
611b44c5a6SAntoine Ténart 	token[0].opcode = EIP197_TOKEN_OPCODE_DIRECTION;
621b44c5a6SAntoine Ténart 	token[0].packet_length = input_length;
631b44c5a6SAntoine Ténart 	token[0].stat = EIP197_TOKEN_STAT_LAST_HASH;
641b44c5a6SAntoine Ténart 	token[0].instructions = EIP197_TOKEN_INS_TYPE_HASH;
651b44c5a6SAntoine Ténart 
661b44c5a6SAntoine Ténart 	token[1].opcode = EIP197_TOKEN_OPCODE_INSERT;
671b44c5a6SAntoine Ténart 	token[1].packet_length = result_length;
681b44c5a6SAntoine Ténart 	token[1].stat = EIP197_TOKEN_STAT_LAST_HASH |
691b44c5a6SAntoine Ténart 			EIP197_TOKEN_STAT_LAST_PACKET;
701b44c5a6SAntoine Ténart 	token[1].instructions = EIP197_TOKEN_INS_TYPE_OUTPUT |
711b44c5a6SAntoine Ténart 				EIP197_TOKEN_INS_INSERT_HASH_DIGEST;
721b44c5a6SAntoine Ténart }
731b44c5a6SAntoine Ténart 
741b44c5a6SAntoine Ténart static void safexcel_context_control(struct safexcel_ahash_ctx *ctx,
751b44c5a6SAntoine Ténart 				     struct safexcel_ahash_req *req,
761b44c5a6SAntoine Ténart 				     struct safexcel_command_desc *cdesc,
771b44c5a6SAntoine Ténart 				     unsigned int digestsize,
781b44c5a6SAntoine Ténart 				     unsigned int blocksize)
791b44c5a6SAntoine Ténart {
801b44c5a6SAntoine Ténart 	int i;
811b44c5a6SAntoine Ténart 
821b44c5a6SAntoine Ténart 	cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_HASH_OUT;
831b44c5a6SAntoine Ténart 	cdesc->control_data.control0 |= ctx->alg;
841b44c5a6SAntoine Ténart 	cdesc->control_data.control0 |= ctx->digest;
851b44c5a6SAntoine Ténart 
861b44c5a6SAntoine Ténart 	if (ctx->digest == CONTEXT_CONTROL_DIGEST_PRECOMPUTED) {
871b44c5a6SAntoine Ténart 		if (req->processed) {
881b44c5a6SAntoine Ténart 			if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA1)
891b44c5a6SAntoine Ténart 				cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(6);
901b44c5a6SAntoine Ténart 			else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA224 ||
911b44c5a6SAntoine Ténart 				 ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA256)
921b44c5a6SAntoine Ténart 				cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(9);
931b44c5a6SAntoine Ténart 
941b44c5a6SAntoine Ténart 			cdesc->control_data.control1 |= CONTEXT_CONTROL_DIGEST_CNT;
951b44c5a6SAntoine Ténart 		} else {
961b44c5a6SAntoine Ténart 			cdesc->control_data.control0 |= CONTEXT_CONTROL_RESTART_HASH;
971b44c5a6SAntoine Ténart 		}
981b44c5a6SAntoine Ténart 
991b44c5a6SAntoine Ténart 		if (!req->finish)
1001b44c5a6SAntoine Ténart 			cdesc->control_data.control0 |= CONTEXT_CONTROL_NO_FINISH_HASH;
1011b44c5a6SAntoine Ténart 
1021b44c5a6SAntoine Ténart 		/*
1031b44c5a6SAntoine Ténart 		 * Copy the input digest if needed, and setup the context
1041b44c5a6SAntoine Ténart 		 * fields. Do this now as we need it to setup the first command
1051b44c5a6SAntoine Ténart 		 * descriptor.
1061b44c5a6SAntoine Ténart 		 */
1071b44c5a6SAntoine Ténart 		if (req->processed) {
1081b44c5a6SAntoine Ténart 			for (i = 0; i < digestsize / sizeof(u32); i++)
1091b44c5a6SAntoine Ténart 				ctx->base.ctxr->data[i] = cpu_to_le32(req->state[i]);
1101b44c5a6SAntoine Ténart 
1111b44c5a6SAntoine Ténart 			if (req->finish)
1121b44c5a6SAntoine Ténart 				ctx->base.ctxr->data[i] = cpu_to_le32(req->processed / blocksize);
1131b44c5a6SAntoine Ténart 		}
1141b44c5a6SAntoine Ténart 	} else if (ctx->digest == CONTEXT_CONTROL_DIGEST_HMAC) {
1151b44c5a6SAntoine Ténart 		cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(10);
1161b44c5a6SAntoine Ténart 
1171b44c5a6SAntoine Ténart 		memcpy(ctx->base.ctxr->data, ctx->ipad, digestsize);
1181b44c5a6SAntoine Ténart 		memcpy(ctx->base.ctxr->data + digestsize / sizeof(u32),
1191b44c5a6SAntoine Ténart 		       ctx->opad, digestsize);
1201b44c5a6SAntoine Ténart 	}
1211b44c5a6SAntoine Ténart }
1221b44c5a6SAntoine Ténart 
1231eb7b403SOfer Heifetz static int safexcel_handle_req_result(struct safexcel_crypto_priv *priv, int ring,
1241b44c5a6SAntoine Ténart 				      struct crypto_async_request *async,
1251b44c5a6SAntoine Ténart 				      bool *should_complete, int *ret)
1261b44c5a6SAntoine Ténart {
1271b44c5a6SAntoine Ténart 	struct safexcel_result_desc *rdesc;
1281b44c5a6SAntoine Ténart 	struct ahash_request *areq = ahash_request_cast(async);
1291b44c5a6SAntoine Ténart 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
1301b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *sreq = ahash_request_ctx(areq);
1312973633eSAntoine Ténart 	int cache_len;
1321b44c5a6SAntoine Ténart 
1331b44c5a6SAntoine Ténart 	*ret = 0;
1341b44c5a6SAntoine Ténart 
1351b44c5a6SAntoine Ténart 	spin_lock_bh(&priv->ring[ring].egress_lock);
1361b44c5a6SAntoine Ténart 	rdesc = safexcel_ring_next_rptr(priv, &priv->ring[ring].rdr);
1371b44c5a6SAntoine Ténart 	if (IS_ERR(rdesc)) {
1381b44c5a6SAntoine Ténart 		dev_err(priv->dev,
1391b44c5a6SAntoine Ténart 			"hash: result: could not retrieve the result descriptor\n");
1401b44c5a6SAntoine Ténart 		*ret = PTR_ERR(rdesc);
1411b44c5a6SAntoine Ténart 	} else if (rdesc->result_data.error_code) {
1421b44c5a6SAntoine Ténart 		dev_err(priv->dev,
1431b44c5a6SAntoine Ténart 			"hash: result: result descriptor error (%d)\n",
1441b44c5a6SAntoine Ténart 			rdesc->result_data.error_code);
1451b44c5a6SAntoine Ténart 		*ret = -EINVAL;
1461b44c5a6SAntoine Ténart 	}
1471b44c5a6SAntoine Ténart 
1481b44c5a6SAntoine Ténart 	safexcel_complete(priv, ring);
1491b44c5a6SAntoine Ténart 	spin_unlock_bh(&priv->ring[ring].egress_lock);
1501b44c5a6SAntoine Ténart 
1511b44c5a6SAntoine Ténart 	if (sreq->finish)
1522973633eSAntoine Ténart 		memcpy(areq->result, sreq->state,
1532973633eSAntoine Ténart 		       crypto_ahash_digestsize(ahash));
1541b44c5a6SAntoine Ténart 
1551b44c5a6SAntoine Ténart 	dma_unmap_sg(priv->dev, areq->src,
1561b44c5a6SAntoine Ténart 		     sg_nents_for_len(areq->src, areq->nbytes), DMA_TO_DEVICE);
1571b44c5a6SAntoine Ténart 
1581b44c5a6SAntoine Ténart 	safexcel_free_context(priv, async, sreq->state_sz);
1591b44c5a6SAntoine Ténart 
1601b44c5a6SAntoine Ténart 	cache_len = sreq->len - sreq->processed;
1611b44c5a6SAntoine Ténart 	if (cache_len)
1621b44c5a6SAntoine Ténart 		memcpy(sreq->cache, sreq->cache_next, cache_len);
1631b44c5a6SAntoine Ténart 
1641b44c5a6SAntoine Ténart 	*should_complete = true;
1651b44c5a6SAntoine Ténart 
1661b44c5a6SAntoine Ténart 	return 1;
1671b44c5a6SAntoine Ténart }
1681b44c5a6SAntoine Ténart 
1691eb7b403SOfer Heifetz static int safexcel_ahash_send_req(struct crypto_async_request *async, int ring,
1701eb7b403SOfer Heifetz 				   struct safexcel_request *request,
1711eb7b403SOfer Heifetz 				   int *commands, int *results)
1721b44c5a6SAntoine Ténart {
1731b44c5a6SAntoine Ténart 	struct ahash_request *areq = ahash_request_cast(async);
1741b44c5a6SAntoine Ténart 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
1751b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
1761b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
1771b44c5a6SAntoine Ténart 	struct safexcel_crypto_priv *priv = ctx->priv;
1781b44c5a6SAntoine Ténart 	struct safexcel_command_desc *cdesc, *first_cdesc = NULL;
1791b44c5a6SAntoine Ténart 	struct safexcel_result_desc *rdesc;
1801b44c5a6SAntoine Ténart 	struct scatterlist *sg;
1811b44c5a6SAntoine Ténart 	int i, nents, queued, len, cache_len, extra, n_cdesc = 0, ret = 0;
1821b44c5a6SAntoine Ténart 
1831b44c5a6SAntoine Ténart 	queued = len = req->len - req->processed;
1841b44c5a6SAntoine Ténart 	if (queued < crypto_ahash_blocksize(ahash))
1851b44c5a6SAntoine Ténart 		cache_len = queued;
1861b44c5a6SAntoine Ténart 	else
1871b44c5a6SAntoine Ténart 		cache_len = queued - areq->nbytes;
1881b44c5a6SAntoine Ténart 
1891b44c5a6SAntoine Ténart 	/*
1901b44c5a6SAntoine Ténart 	 * If this is not the last request and the queued data does not fit
1911b44c5a6SAntoine Ténart 	 * into full blocks, cache it for the next send() call.
1921b44c5a6SAntoine Ténart 	 */
1931b44c5a6SAntoine Ténart 	extra = queued & (crypto_ahash_blocksize(ahash) - 1);
1941b44c5a6SAntoine Ténart 	if (!req->last_req && extra) {
1951b44c5a6SAntoine Ténart 		sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
1961b44c5a6SAntoine Ténart 				   req->cache_next, extra, areq->nbytes - extra);
1971b44c5a6SAntoine Ténart 
1981b44c5a6SAntoine Ténart 		queued -= extra;
1991b44c5a6SAntoine Ténart 		len -= extra;
2001b44c5a6SAntoine Ténart 	}
2011b44c5a6SAntoine Ténart 
2021b44c5a6SAntoine Ténart 	spin_lock_bh(&priv->ring[ring].egress_lock);
2031b44c5a6SAntoine Ténart 
2041b44c5a6SAntoine Ténart 	/* Add a command descriptor for the cached data, if any */
2051b44c5a6SAntoine Ténart 	if (cache_len) {
2061b44c5a6SAntoine Ténart 		ctx->base.cache = kzalloc(cache_len, EIP197_GFP_FLAGS(*async));
2071b44c5a6SAntoine Ténart 		if (!ctx->base.cache) {
2081b44c5a6SAntoine Ténart 			ret = -ENOMEM;
2091b44c5a6SAntoine Ténart 			goto unlock;
2101b44c5a6SAntoine Ténart 		}
2111b44c5a6SAntoine Ténart 		memcpy(ctx->base.cache, req->cache, cache_len);
2121b44c5a6SAntoine Ténart 		ctx->base.cache_dma = dma_map_single(priv->dev, ctx->base.cache,
2131b44c5a6SAntoine Ténart 						     cache_len, DMA_TO_DEVICE);
2141b44c5a6SAntoine Ténart 		if (dma_mapping_error(priv->dev, ctx->base.cache_dma)) {
2151b44c5a6SAntoine Ténart 			ret = -EINVAL;
2161b44c5a6SAntoine Ténart 			goto free_cache;
2171b44c5a6SAntoine Ténart 		}
2181b44c5a6SAntoine Ténart 
2191b44c5a6SAntoine Ténart 		ctx->base.cache_sz = cache_len;
2201b44c5a6SAntoine Ténart 		first_cdesc = safexcel_add_cdesc(priv, ring, 1,
2211b44c5a6SAntoine Ténart 						 (cache_len == len),
2221b44c5a6SAntoine Ténart 						 ctx->base.cache_dma,
2231b44c5a6SAntoine Ténart 						 cache_len, len,
2241b44c5a6SAntoine Ténart 						 ctx->base.ctxr_dma);
2251b44c5a6SAntoine Ténart 		if (IS_ERR(first_cdesc)) {
2261b44c5a6SAntoine Ténart 			ret = PTR_ERR(first_cdesc);
2271b44c5a6SAntoine Ténart 			goto unmap_cache;
2281b44c5a6SAntoine Ténart 		}
2291b44c5a6SAntoine Ténart 		n_cdesc++;
2301b44c5a6SAntoine Ténart 
2311b44c5a6SAntoine Ténart 		queued -= cache_len;
2321b44c5a6SAntoine Ténart 		if (!queued)
2331b44c5a6SAntoine Ténart 			goto send_command;
2341b44c5a6SAntoine Ténart 	}
2351b44c5a6SAntoine Ténart 
2361b44c5a6SAntoine Ténart 	/* Now handle the current ahash request buffer(s) */
2371b44c5a6SAntoine Ténart 	nents = dma_map_sg(priv->dev, areq->src,
2381b44c5a6SAntoine Ténart 		       sg_nents_for_len(areq->src, areq->nbytes),
2391b44c5a6SAntoine Ténart 		       DMA_TO_DEVICE);
2401b44c5a6SAntoine Ténart 	if (!nents) {
2411b44c5a6SAntoine Ténart 		ret = -ENOMEM;
2421b44c5a6SAntoine Ténart 		goto cdesc_rollback;
2431b44c5a6SAntoine Ténart 	}
2441b44c5a6SAntoine Ténart 
2451b44c5a6SAntoine Ténart 	for_each_sg(areq->src, sg, nents, i) {
2461b44c5a6SAntoine Ténart 		int sglen = sg_dma_len(sg);
2471b44c5a6SAntoine Ténart 
2481b44c5a6SAntoine Ténart 		/* Do not overflow the request */
2491b44c5a6SAntoine Ténart 		if (queued - sglen < 0)
2501b44c5a6SAntoine Ténart 			sglen = queued;
2511b44c5a6SAntoine Ténart 
2521b44c5a6SAntoine Ténart 		cdesc = safexcel_add_cdesc(priv, ring, !n_cdesc,
2531b44c5a6SAntoine Ténart 					   !(queued - sglen), sg_dma_address(sg),
2541b44c5a6SAntoine Ténart 					   sglen, len, ctx->base.ctxr_dma);
2551b44c5a6SAntoine Ténart 		if (IS_ERR(cdesc)) {
2561b44c5a6SAntoine Ténart 			ret = PTR_ERR(cdesc);
2571b44c5a6SAntoine Ténart 			goto cdesc_rollback;
2581b44c5a6SAntoine Ténart 		}
2591b44c5a6SAntoine Ténart 		n_cdesc++;
2601b44c5a6SAntoine Ténart 
2611b44c5a6SAntoine Ténart 		if (n_cdesc == 1)
2621b44c5a6SAntoine Ténart 			first_cdesc = cdesc;
2631b44c5a6SAntoine Ténart 
2641b44c5a6SAntoine Ténart 		queued -= sglen;
2651b44c5a6SAntoine Ténart 		if (!queued)
2661b44c5a6SAntoine Ténart 			break;
2671b44c5a6SAntoine Ténart 	}
2681b44c5a6SAntoine Ténart 
2691b44c5a6SAntoine Ténart send_command:
2701b44c5a6SAntoine Ténart 	/* Setup the context options */
2711b44c5a6SAntoine Ténart 	safexcel_context_control(ctx, req, first_cdesc, req->state_sz,
2721b44c5a6SAntoine Ténart 				 crypto_ahash_blocksize(ahash));
2731b44c5a6SAntoine Ténart 
2741b44c5a6SAntoine Ténart 	/* Add the token */
2751b44c5a6SAntoine Ténart 	safexcel_hash_token(first_cdesc, len, req->state_sz);
2761b44c5a6SAntoine Ténart 
2772973633eSAntoine Ténart 	ctx->base.result_dma = dma_map_single(priv->dev, req->state,
2781b44c5a6SAntoine Ténart 					      req->state_sz, DMA_FROM_DEVICE);
2791b44c5a6SAntoine Ténart 	if (dma_mapping_error(priv->dev, ctx->base.result_dma)) {
2801b44c5a6SAntoine Ténart 		ret = -EINVAL;
2811b44c5a6SAntoine Ténart 		goto cdesc_rollback;
2821b44c5a6SAntoine Ténart 	}
2831b44c5a6SAntoine Ténart 
2841b44c5a6SAntoine Ténart 	/* Add a result descriptor */
2851b44c5a6SAntoine Ténart 	rdesc = safexcel_add_rdesc(priv, ring, 1, 1, ctx->base.result_dma,
2861b44c5a6SAntoine Ténart 				   req->state_sz);
2871b44c5a6SAntoine Ténart 	if (IS_ERR(rdesc)) {
2881b44c5a6SAntoine Ténart 		ret = PTR_ERR(rdesc);
2891b44c5a6SAntoine Ténart 		goto cdesc_rollback;
2901b44c5a6SAntoine Ténart 	}
2911b44c5a6SAntoine Ténart 
2921b44c5a6SAntoine Ténart 	spin_unlock_bh(&priv->ring[ring].egress_lock);
2931b44c5a6SAntoine Ténart 
29497858434SAntoine Ténart 	req->processed += len;
29597858434SAntoine Ténart 	request->req = &areq->base;
29697858434SAntoine Ténart 
2971b44c5a6SAntoine Ténart 	*commands = n_cdesc;
2981b44c5a6SAntoine Ténart 	*results = 1;
2991b44c5a6SAntoine Ténart 	return 0;
3001b44c5a6SAntoine Ténart 
3011b44c5a6SAntoine Ténart cdesc_rollback:
3021b44c5a6SAntoine Ténart 	for (i = 0; i < n_cdesc; i++)
3031b44c5a6SAntoine Ténart 		safexcel_ring_rollback_wptr(priv, &priv->ring[ring].cdr);
3041b44c5a6SAntoine Ténart unmap_cache:
3051b44c5a6SAntoine Ténart 	if (ctx->base.cache_dma) {
3061b44c5a6SAntoine Ténart 		dma_unmap_single(priv->dev, ctx->base.cache_dma,
3071b44c5a6SAntoine Ténart 				 ctx->base.cache_sz, DMA_TO_DEVICE);
3081b44c5a6SAntoine Ténart 		ctx->base.cache_sz = 0;
3091b44c5a6SAntoine Ténart 	}
3101b44c5a6SAntoine Ténart free_cache:
3111b44c5a6SAntoine Ténart 	kfree(ctx->base.cache);
3121b44c5a6SAntoine Ténart 	ctx->base.cache = NULL;
3131b44c5a6SAntoine Ténart 
3141b44c5a6SAntoine Ténart unlock:
3151b44c5a6SAntoine Ténart 	spin_unlock_bh(&priv->ring[ring].egress_lock);
3161b44c5a6SAntoine Ténart 	return ret;
3171b44c5a6SAntoine Ténart }
3181b44c5a6SAntoine Ténart 
3191b44c5a6SAntoine Ténart static inline bool safexcel_ahash_needs_inv_get(struct ahash_request *areq)
3201b44c5a6SAntoine Ténart {
3211b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
3221b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
3231b44c5a6SAntoine Ténart 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
3241b44c5a6SAntoine Ténart 	unsigned int state_w_sz = req->state_sz / sizeof(u32);
3251b44c5a6SAntoine Ténart 	int i;
3261b44c5a6SAntoine Ténart 
3271b44c5a6SAntoine Ténart 	for (i = 0; i < state_w_sz; i++)
3281b44c5a6SAntoine Ténart 		if (ctx->base.ctxr->data[i] != cpu_to_le32(req->state[i]))
3291b44c5a6SAntoine Ténart 			return true;
3301b44c5a6SAntoine Ténart 
3311b44c5a6SAntoine Ténart 	if (ctx->base.ctxr->data[state_w_sz] !=
3321b44c5a6SAntoine Ténart 	    cpu_to_le32(req->processed / crypto_ahash_blocksize(ahash)))
3331b44c5a6SAntoine Ténart 		return true;
3341b44c5a6SAntoine Ténart 
3351b44c5a6SAntoine Ténart 	return false;
3361b44c5a6SAntoine Ténart }
3371b44c5a6SAntoine Ténart 
3381b44c5a6SAntoine Ténart static int safexcel_handle_inv_result(struct safexcel_crypto_priv *priv,
3391b44c5a6SAntoine Ténart 				      int ring,
3401b44c5a6SAntoine Ténart 				      struct crypto_async_request *async,
3411b44c5a6SAntoine Ténart 				      bool *should_complete, int *ret)
3421b44c5a6SAntoine Ténart {
3431b44c5a6SAntoine Ténart 	struct safexcel_result_desc *rdesc;
3441b44c5a6SAntoine Ténart 	struct ahash_request *areq = ahash_request_cast(async);
3451b44c5a6SAntoine Ténart 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
3461b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(ahash);
3471b44c5a6SAntoine Ténart 	int enq_ret;
3481b44c5a6SAntoine Ténart 
3491b44c5a6SAntoine Ténart 	*ret = 0;
3501b44c5a6SAntoine Ténart 
3511b44c5a6SAntoine Ténart 	spin_lock_bh(&priv->ring[ring].egress_lock);
3521b44c5a6SAntoine Ténart 	rdesc = safexcel_ring_next_rptr(priv, &priv->ring[ring].rdr);
3531b44c5a6SAntoine Ténart 	if (IS_ERR(rdesc)) {
3541b44c5a6SAntoine Ténart 		dev_err(priv->dev,
3551b44c5a6SAntoine Ténart 			"hash: invalidate: could not retrieve the result descriptor\n");
3561b44c5a6SAntoine Ténart 		*ret = PTR_ERR(rdesc);
3571b44c5a6SAntoine Ténart 	} else if (rdesc->result_data.error_code) {
3581b44c5a6SAntoine Ténart 		dev_err(priv->dev,
3591b44c5a6SAntoine Ténart 			"hash: invalidate: result descriptor error (%d)\n",
3601b44c5a6SAntoine Ténart 			rdesc->result_data.error_code);
3611b44c5a6SAntoine Ténart 		*ret = -EINVAL;
3621b44c5a6SAntoine Ténart 	}
3631b44c5a6SAntoine Ténart 
3641b44c5a6SAntoine Ténart 	safexcel_complete(priv, ring);
3651b44c5a6SAntoine Ténart 	spin_unlock_bh(&priv->ring[ring].egress_lock);
3661b44c5a6SAntoine Ténart 
3671b44c5a6SAntoine Ténart 	if (ctx->base.exit_inv) {
3681b44c5a6SAntoine Ténart 		dma_pool_free(priv->context_pool, ctx->base.ctxr,
3691b44c5a6SAntoine Ténart 			      ctx->base.ctxr_dma);
3701b44c5a6SAntoine Ténart 
3711b44c5a6SAntoine Ténart 		*should_complete = true;
3721b44c5a6SAntoine Ténart 		return 1;
3731b44c5a6SAntoine Ténart 	}
3741b44c5a6SAntoine Ténart 
37586671abbSAntoine Ténart 	ring = safexcel_select_ring(priv);
37686671abbSAntoine Ténart 	ctx->base.ring = ring;
3771b44c5a6SAntoine Ténart 
37886671abbSAntoine Ténart 	spin_lock_bh(&priv->ring[ring].queue_lock);
37986671abbSAntoine Ténart 	enq_ret = crypto_enqueue_request(&priv->ring[ring].queue, async);
38086671abbSAntoine Ténart 	spin_unlock_bh(&priv->ring[ring].queue_lock);
3811b44c5a6SAntoine Ténart 
3821b44c5a6SAntoine Ténart 	if (enq_ret != -EINPROGRESS)
3831b44c5a6SAntoine Ténart 		*ret = enq_ret;
3841b44c5a6SAntoine Ténart 
38586671abbSAntoine Ténart 	if (!priv->ring[ring].need_dequeue)
38686671abbSAntoine Ténart 		safexcel_dequeue(priv, ring);
38786671abbSAntoine Ténart 
3881b44c5a6SAntoine Ténart 	*should_complete = false;
3891b44c5a6SAntoine Ténart 
3901b44c5a6SAntoine Ténart 	return 1;
3911b44c5a6SAntoine Ténart }
3921b44c5a6SAntoine Ténart 
3931eb7b403SOfer Heifetz static int safexcel_handle_result(struct safexcel_crypto_priv *priv, int ring,
3941eb7b403SOfer Heifetz 				  struct crypto_async_request *async,
3951eb7b403SOfer Heifetz 				  bool *should_complete, int *ret)
3961eb7b403SOfer Heifetz {
3971eb7b403SOfer Heifetz 	struct ahash_request *areq = ahash_request_cast(async);
3981eb7b403SOfer Heifetz 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
3991eb7b403SOfer Heifetz 	int err;
4001eb7b403SOfer Heifetz 
4011eb7b403SOfer Heifetz 	if (req->needs_inv) {
4021eb7b403SOfer Heifetz 		req->needs_inv = false;
4031eb7b403SOfer Heifetz 		err = safexcel_handle_inv_result(priv, ring, async,
4041eb7b403SOfer Heifetz 						 should_complete, ret);
4051eb7b403SOfer Heifetz 	} else {
4061eb7b403SOfer Heifetz 		err = safexcel_handle_req_result(priv, ring, async,
4071eb7b403SOfer Heifetz 						 should_complete, ret);
4081eb7b403SOfer Heifetz 	}
4091eb7b403SOfer Heifetz 
4101eb7b403SOfer Heifetz 	return err;
4111eb7b403SOfer Heifetz }
4121eb7b403SOfer Heifetz 
4131b44c5a6SAntoine Ténart static int safexcel_ahash_send_inv(struct crypto_async_request *async,
4141b44c5a6SAntoine Ténart 				   int ring, struct safexcel_request *request,
4151b44c5a6SAntoine Ténart 				   int *commands, int *results)
4161b44c5a6SAntoine Ténart {
4171b44c5a6SAntoine Ténart 	struct ahash_request *areq = ahash_request_cast(async);
4181b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
4191b44c5a6SAntoine Ténart 	int ret;
4201b44c5a6SAntoine Ténart 
4211b44c5a6SAntoine Ténart 	ret = safexcel_invalidate_cache(async, &ctx->base, ctx->priv,
4221b44c5a6SAntoine Ténart 					ctx->base.ctxr_dma, ring, request);
4231b44c5a6SAntoine Ténart 	if (unlikely(ret))
4241b44c5a6SAntoine Ténart 		return ret;
4251b44c5a6SAntoine Ténart 
4261b44c5a6SAntoine Ténart 	*commands = 1;
4271b44c5a6SAntoine Ténart 	*results = 1;
4281b44c5a6SAntoine Ténart 
4291b44c5a6SAntoine Ténart 	return 0;
4301b44c5a6SAntoine Ténart }
4311b44c5a6SAntoine Ténart 
4321eb7b403SOfer Heifetz static int safexcel_ahash_send(struct crypto_async_request *async,
4331eb7b403SOfer Heifetz 			       int ring, struct safexcel_request *request,
4341eb7b403SOfer Heifetz 			       int *commands, int *results)
4351eb7b403SOfer Heifetz {
4361eb7b403SOfer Heifetz 	struct ahash_request *areq = ahash_request_cast(async);
4371eb7b403SOfer Heifetz 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
4381eb7b403SOfer Heifetz 	int ret;
4391eb7b403SOfer Heifetz 
4401eb7b403SOfer Heifetz 	if (req->needs_inv)
4411eb7b403SOfer Heifetz 		ret = safexcel_ahash_send_inv(async, ring, request,
4421eb7b403SOfer Heifetz 					      commands, results);
4431eb7b403SOfer Heifetz 	else
4441eb7b403SOfer Heifetz 		ret = safexcel_ahash_send_req(async, ring, request,
4451eb7b403SOfer Heifetz 					      commands, results);
4461eb7b403SOfer Heifetz 	return ret;
4471eb7b403SOfer Heifetz }
4481eb7b403SOfer Heifetz 
4491b44c5a6SAntoine Ténart static int safexcel_ahash_exit_inv(struct crypto_tfm *tfm)
4501b44c5a6SAntoine Ténart {
4511b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm);
4521b44c5a6SAntoine Ténart 	struct safexcel_crypto_priv *priv = ctx->priv;
4537cad2fabSAntoine Ténart 	AHASH_REQUEST_ON_STACK(req, __crypto_ahash_cast(tfm));
4547cad2fabSAntoine Ténart 	struct safexcel_ahash_req *rctx = ahash_request_ctx(req);
4553e1166b9SArnd Bergmann 	struct safexcel_inv_result result = {};
45686671abbSAntoine Ténart 	int ring = ctx->base.ring;
4571b44c5a6SAntoine Ténart 
4587cad2fabSAntoine Ténart 	memset(req, 0, sizeof(struct ahash_request));
4591b44c5a6SAntoine Ténart 
4601b44c5a6SAntoine Ténart 	/* create invalidation request */
4611b44c5a6SAntoine Ténart 	init_completion(&result.completion);
4627cad2fabSAntoine Ténart 	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4631b44c5a6SAntoine Ténart 				   safexcel_inv_complete, &result);
4641b44c5a6SAntoine Ténart 
4657cad2fabSAntoine Ténart 	ahash_request_set_tfm(req, __crypto_ahash_cast(tfm));
4667cad2fabSAntoine Ténart 	ctx = crypto_tfm_ctx(req->base.tfm);
4671b44c5a6SAntoine Ténart 	ctx->base.exit_inv = true;
4681eb7b403SOfer Heifetz 	rctx->needs_inv = true;
4691b44c5a6SAntoine Ténart 
47086671abbSAntoine Ténart 	spin_lock_bh(&priv->ring[ring].queue_lock);
4717cad2fabSAntoine Ténart 	crypto_enqueue_request(&priv->ring[ring].queue, &req->base);
47286671abbSAntoine Ténart 	spin_unlock_bh(&priv->ring[ring].queue_lock);
4731b44c5a6SAntoine Ténart 
47486671abbSAntoine Ténart 	if (!priv->ring[ring].need_dequeue)
47586671abbSAntoine Ténart 		safexcel_dequeue(priv, ring);
4761b44c5a6SAntoine Ténart 
4771b44c5a6SAntoine Ténart 	wait_for_completion_interruptible(&result.completion);
4781b44c5a6SAntoine Ténart 
4791b44c5a6SAntoine Ténart 	if (result.error) {
4801b44c5a6SAntoine Ténart 		dev_warn(priv->dev, "hash: completion error (%d)\n",
4811b44c5a6SAntoine Ténart 			 result.error);
4821b44c5a6SAntoine Ténart 		return result.error;
4831b44c5a6SAntoine Ténart 	}
4841b44c5a6SAntoine Ténart 
4851b44c5a6SAntoine Ténart 	return 0;
4861b44c5a6SAntoine Ténart }
4871b44c5a6SAntoine Ténart 
4881b44c5a6SAntoine Ténart static int safexcel_ahash_cache(struct ahash_request *areq)
4891b44c5a6SAntoine Ténart {
4901b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
4911b44c5a6SAntoine Ténart 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
4921b44c5a6SAntoine Ténart 	int queued, cache_len;
4931b44c5a6SAntoine Ténart 
4941b44c5a6SAntoine Ténart 	cache_len = req->len - areq->nbytes - req->processed;
4951b44c5a6SAntoine Ténart 	queued = req->len - req->processed;
4961b44c5a6SAntoine Ténart 
4971b44c5a6SAntoine Ténart 	/*
4981b44c5a6SAntoine Ténart 	 * In case there isn't enough bytes to proceed (less than a
4991b44c5a6SAntoine Ténart 	 * block size), cache the data until we have enough.
5001b44c5a6SAntoine Ténart 	 */
5011b44c5a6SAntoine Ténart 	if (cache_len + areq->nbytes <= crypto_ahash_blocksize(ahash)) {
5021b44c5a6SAntoine Ténart 		sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
5031b44c5a6SAntoine Ténart 				   req->cache + cache_len,
5041b44c5a6SAntoine Ténart 				   areq->nbytes, 0);
5051b44c5a6SAntoine Ténart 		return areq->nbytes;
5061b44c5a6SAntoine Ténart 	}
5071b44c5a6SAntoine Ténart 
5081b44c5a6SAntoine Ténart 	/* We could'nt cache all the data */
5091b44c5a6SAntoine Ténart 	return -E2BIG;
5101b44c5a6SAntoine Ténart }
5111b44c5a6SAntoine Ténart 
5121b44c5a6SAntoine Ténart static int safexcel_ahash_enqueue(struct ahash_request *areq)
5131b44c5a6SAntoine Ténart {
5141b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
5151b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
5161b44c5a6SAntoine Ténart 	struct safexcel_crypto_priv *priv = ctx->priv;
51786671abbSAntoine Ténart 	int ret, ring;
5181b44c5a6SAntoine Ténart 
5191eb7b403SOfer Heifetz 	req->needs_inv = false;
5201b44c5a6SAntoine Ténart 
5211b44c5a6SAntoine Ténart 	if (req->processed && ctx->digest == CONTEXT_CONTROL_DIGEST_PRECOMPUTED)
5221b44c5a6SAntoine Ténart 		ctx->base.needs_inv = safexcel_ahash_needs_inv_get(areq);
5231b44c5a6SAntoine Ténart 
5241b44c5a6SAntoine Ténart 	if (ctx->base.ctxr) {
5251eb7b403SOfer Heifetz 		if (ctx->base.needs_inv) {
5261eb7b403SOfer Heifetz 			ctx->base.needs_inv = false;
5271eb7b403SOfer Heifetz 			req->needs_inv = true;
5281eb7b403SOfer Heifetz 		}
5291b44c5a6SAntoine Ténart 	} else {
5301b44c5a6SAntoine Ténart 		ctx->base.ring = safexcel_select_ring(priv);
5311b44c5a6SAntoine Ténart 		ctx->base.ctxr = dma_pool_zalloc(priv->context_pool,
5321b44c5a6SAntoine Ténart 						 EIP197_GFP_FLAGS(areq->base),
5331b44c5a6SAntoine Ténart 						 &ctx->base.ctxr_dma);
5341b44c5a6SAntoine Ténart 		if (!ctx->base.ctxr)
5351b44c5a6SAntoine Ténart 			return -ENOMEM;
5361b44c5a6SAntoine Ténart 	}
5371b44c5a6SAntoine Ténart 
53886671abbSAntoine Ténart 	ring = ctx->base.ring;
5391b44c5a6SAntoine Ténart 
54086671abbSAntoine Ténart 	spin_lock_bh(&priv->ring[ring].queue_lock);
54186671abbSAntoine Ténart 	ret = crypto_enqueue_request(&priv->ring[ring].queue, &areq->base);
54286671abbSAntoine Ténart 	spin_unlock_bh(&priv->ring[ring].queue_lock);
54386671abbSAntoine Ténart 
54486671abbSAntoine Ténart 	if (!priv->ring[ring].need_dequeue)
54586671abbSAntoine Ténart 		safexcel_dequeue(priv, ring);
5461b44c5a6SAntoine Ténart 
5471b44c5a6SAntoine Ténart 	return ret;
5481b44c5a6SAntoine Ténart }
5491b44c5a6SAntoine Ténart 
5501b44c5a6SAntoine Ténart static int safexcel_ahash_update(struct ahash_request *areq)
5511b44c5a6SAntoine Ténart {
5521b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
5531b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
5541b44c5a6SAntoine Ténart 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
5551b44c5a6SAntoine Ténart 
5561b44c5a6SAntoine Ténart 	/* If the request is 0 length, do nothing */
5571b44c5a6SAntoine Ténart 	if (!areq->nbytes)
5581b44c5a6SAntoine Ténart 		return 0;
5591b44c5a6SAntoine Ténart 
5601b44c5a6SAntoine Ténart 	req->len += areq->nbytes;
5611b44c5a6SAntoine Ténart 
5621b44c5a6SAntoine Ténart 	safexcel_ahash_cache(areq);
5631b44c5a6SAntoine Ténart 
5641b44c5a6SAntoine Ténart 	/*
5651b44c5a6SAntoine Ténart 	 * We're not doing partial updates when performing an hmac request.
5661b44c5a6SAntoine Ténart 	 * Everything will be handled by the final() call.
5671b44c5a6SAntoine Ténart 	 */
5681b44c5a6SAntoine Ténart 	if (ctx->digest == CONTEXT_CONTROL_DIGEST_HMAC)
5691b44c5a6SAntoine Ténart 		return 0;
5701b44c5a6SAntoine Ténart 
5711b44c5a6SAntoine Ténart 	if (req->hmac)
5721b44c5a6SAntoine Ténart 		return safexcel_ahash_enqueue(areq);
5731b44c5a6SAntoine Ténart 
5741b44c5a6SAntoine Ténart 	if (!req->last_req &&
5751b44c5a6SAntoine Ténart 	    req->len - req->processed > crypto_ahash_blocksize(ahash))
5761b44c5a6SAntoine Ténart 		return safexcel_ahash_enqueue(areq);
5771b44c5a6SAntoine Ténart 
5781b44c5a6SAntoine Ténart 	return 0;
5791b44c5a6SAntoine Ténart }
5801b44c5a6SAntoine Ténart 
5811b44c5a6SAntoine Ténart static int safexcel_ahash_final(struct ahash_request *areq)
5821b44c5a6SAntoine Ténart {
5831b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
5841b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
5851b44c5a6SAntoine Ténart 
5861b44c5a6SAntoine Ténart 	req->last_req = true;
5871b44c5a6SAntoine Ténart 	req->finish = true;
5881b44c5a6SAntoine Ténart 
5891b44c5a6SAntoine Ténart 	/* If we have an overall 0 length request */
5901b44c5a6SAntoine Ténart 	if (!(req->len + areq->nbytes)) {
5911b44c5a6SAntoine Ténart 		if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA1)
5921b44c5a6SAntoine Ténart 			memcpy(areq->result, sha1_zero_message_hash,
5931b44c5a6SAntoine Ténart 			       SHA1_DIGEST_SIZE);
5941b44c5a6SAntoine Ténart 		else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA224)
5951b44c5a6SAntoine Ténart 			memcpy(areq->result, sha224_zero_message_hash,
5961b44c5a6SAntoine Ténart 			       SHA224_DIGEST_SIZE);
5971b44c5a6SAntoine Ténart 		else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA256)
5981b44c5a6SAntoine Ténart 			memcpy(areq->result, sha256_zero_message_hash,
5991b44c5a6SAntoine Ténart 			       SHA256_DIGEST_SIZE);
6001b44c5a6SAntoine Ténart 
6011b44c5a6SAntoine Ténart 		return 0;
6021b44c5a6SAntoine Ténart 	}
6031b44c5a6SAntoine Ténart 
6041b44c5a6SAntoine Ténart 	return safexcel_ahash_enqueue(areq);
6051b44c5a6SAntoine Ténart }
6061b44c5a6SAntoine Ténart 
6071b44c5a6SAntoine Ténart static int safexcel_ahash_finup(struct ahash_request *areq)
6081b44c5a6SAntoine Ténart {
6091b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
6101b44c5a6SAntoine Ténart 
6111b44c5a6SAntoine Ténart 	req->last_req = true;
6121b44c5a6SAntoine Ténart 	req->finish = true;
6131b44c5a6SAntoine Ténart 
6141b44c5a6SAntoine Ténart 	safexcel_ahash_update(areq);
6151b44c5a6SAntoine Ténart 	return safexcel_ahash_final(areq);
6161b44c5a6SAntoine Ténart }
6171b44c5a6SAntoine Ténart 
6181b44c5a6SAntoine Ténart static int safexcel_ahash_export(struct ahash_request *areq, void *out)
6191b44c5a6SAntoine Ténart {
6201b44c5a6SAntoine Ténart 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
6211b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
6221b44c5a6SAntoine Ténart 	struct safexcel_ahash_export_state *export = out;
6231b44c5a6SAntoine Ténart 
6241b44c5a6SAntoine Ténart 	export->len = req->len;
6251b44c5a6SAntoine Ténart 	export->processed = req->processed;
6261b44c5a6SAntoine Ténart 
6271b44c5a6SAntoine Ténart 	memcpy(export->state, req->state, req->state_sz);
6281b44c5a6SAntoine Ténart 	memset(export->cache, 0, crypto_ahash_blocksize(ahash));
6291b44c5a6SAntoine Ténart 	memcpy(export->cache, req->cache, crypto_ahash_blocksize(ahash));
6301b44c5a6SAntoine Ténart 
6311b44c5a6SAntoine Ténart 	return 0;
6321b44c5a6SAntoine Ténart }
6331b44c5a6SAntoine Ténart 
6341b44c5a6SAntoine Ténart static int safexcel_ahash_import(struct ahash_request *areq, const void *in)
6351b44c5a6SAntoine Ténart {
6361b44c5a6SAntoine Ténart 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
6371b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
6381b44c5a6SAntoine Ténart 	const struct safexcel_ahash_export_state *export = in;
6391b44c5a6SAntoine Ténart 	int ret;
6401b44c5a6SAntoine Ténart 
6411b44c5a6SAntoine Ténart 	ret = crypto_ahash_init(areq);
6421b44c5a6SAntoine Ténart 	if (ret)
6431b44c5a6SAntoine Ténart 		return ret;
6441b44c5a6SAntoine Ténart 
6451b44c5a6SAntoine Ténart 	req->len = export->len;
6461b44c5a6SAntoine Ténart 	req->processed = export->processed;
6471b44c5a6SAntoine Ténart 
6481b44c5a6SAntoine Ténart 	memcpy(req->cache, export->cache, crypto_ahash_blocksize(ahash));
6491b44c5a6SAntoine Ténart 	memcpy(req->state, export->state, req->state_sz);
6501b44c5a6SAntoine Ténart 
6511b44c5a6SAntoine Ténart 	return 0;
6521b44c5a6SAntoine Ténart }
6531b44c5a6SAntoine Ténart 
6541b44c5a6SAntoine Ténart static int safexcel_ahash_cra_init(struct crypto_tfm *tfm)
6551b44c5a6SAntoine Ténart {
6561b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm);
6571b44c5a6SAntoine Ténart 	struct safexcel_alg_template *tmpl =
6581b44c5a6SAntoine Ténart 		container_of(__crypto_ahash_alg(tfm->__crt_alg),
6591b44c5a6SAntoine Ténart 			     struct safexcel_alg_template, alg.ahash);
6601b44c5a6SAntoine Ténart 
6611b44c5a6SAntoine Ténart 	ctx->priv = tmpl->priv;
6621eb7b403SOfer Heifetz 	ctx->base.send = safexcel_ahash_send;
6631eb7b403SOfer Heifetz 	ctx->base.handle_result = safexcel_handle_result;
6641b44c5a6SAntoine Ténart 
6651b44c5a6SAntoine Ténart 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
6661b44c5a6SAntoine Ténart 				 sizeof(struct safexcel_ahash_req));
6671b44c5a6SAntoine Ténart 	return 0;
6681b44c5a6SAntoine Ténart }
6691b44c5a6SAntoine Ténart 
6701b44c5a6SAntoine Ténart static int safexcel_sha1_init(struct ahash_request *areq)
6711b44c5a6SAntoine Ténart {
6721b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
6731b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
6741b44c5a6SAntoine Ténart 
6751b44c5a6SAntoine Ténart 	memset(req, 0, sizeof(*req));
6761b44c5a6SAntoine Ténart 
6771b44c5a6SAntoine Ténart 	req->state[0] = SHA1_H0;
6781b44c5a6SAntoine Ténart 	req->state[1] = SHA1_H1;
6791b44c5a6SAntoine Ténart 	req->state[2] = SHA1_H2;
6801b44c5a6SAntoine Ténart 	req->state[3] = SHA1_H3;
6811b44c5a6SAntoine Ténart 	req->state[4] = SHA1_H4;
6821b44c5a6SAntoine Ténart 
6831b44c5a6SAntoine Ténart 	ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA1;
6841b44c5a6SAntoine Ténart 	ctx->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED;
6851b44c5a6SAntoine Ténart 	req->state_sz = SHA1_DIGEST_SIZE;
6861b44c5a6SAntoine Ténart 
6871b44c5a6SAntoine Ténart 	return 0;
6881b44c5a6SAntoine Ténart }
6891b44c5a6SAntoine Ténart 
6901b44c5a6SAntoine Ténart static int safexcel_sha1_digest(struct ahash_request *areq)
6911b44c5a6SAntoine Ténart {
6921b44c5a6SAntoine Ténart 	int ret = safexcel_sha1_init(areq);
6931b44c5a6SAntoine Ténart 
6941b44c5a6SAntoine Ténart 	if (ret)
6951b44c5a6SAntoine Ténart 		return ret;
6961b44c5a6SAntoine Ténart 
6971b44c5a6SAntoine Ténart 	return safexcel_ahash_finup(areq);
6981b44c5a6SAntoine Ténart }
6991b44c5a6SAntoine Ténart 
7001b44c5a6SAntoine Ténart static void safexcel_ahash_cra_exit(struct crypto_tfm *tfm)
7011b44c5a6SAntoine Ténart {
7021b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm);
7031b44c5a6SAntoine Ténart 	struct safexcel_crypto_priv *priv = ctx->priv;
7041b44c5a6SAntoine Ténart 	int ret;
7051b44c5a6SAntoine Ténart 
7061b44c5a6SAntoine Ténart 	/* context not allocated, skip invalidation */
7071b44c5a6SAntoine Ténart 	if (!ctx->base.ctxr)
7081b44c5a6SAntoine Ténart 		return;
7091b44c5a6SAntoine Ténart 
7101b44c5a6SAntoine Ténart 	ret = safexcel_ahash_exit_inv(tfm);
7111b44c5a6SAntoine Ténart 	if (ret)
7121b44c5a6SAntoine Ténart 		dev_warn(priv->dev, "hash: invalidation error %d\n", ret);
7131b44c5a6SAntoine Ténart }
7141b44c5a6SAntoine Ténart 
7151b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_sha1 = {
7161b44c5a6SAntoine Ténart 	.type = SAFEXCEL_ALG_TYPE_AHASH,
7171b44c5a6SAntoine Ténart 	.alg.ahash = {
7181b44c5a6SAntoine Ténart 		.init = safexcel_sha1_init,
7191b44c5a6SAntoine Ténart 		.update = safexcel_ahash_update,
7201b44c5a6SAntoine Ténart 		.final = safexcel_ahash_final,
7211b44c5a6SAntoine Ténart 		.finup = safexcel_ahash_finup,
7221b44c5a6SAntoine Ténart 		.digest = safexcel_sha1_digest,
7231b44c5a6SAntoine Ténart 		.export = safexcel_ahash_export,
7241b44c5a6SAntoine Ténart 		.import = safexcel_ahash_import,
7251b44c5a6SAntoine Ténart 		.halg = {
7261b44c5a6SAntoine Ténart 			.digestsize = SHA1_DIGEST_SIZE,
7271b44c5a6SAntoine Ténart 			.statesize = sizeof(struct safexcel_ahash_export_state),
7281b44c5a6SAntoine Ténart 			.base = {
7291b44c5a6SAntoine Ténart 				.cra_name = "sha1",
7301b44c5a6SAntoine Ténart 				.cra_driver_name = "safexcel-sha1",
7311b44c5a6SAntoine Ténart 				.cra_priority = 300,
7321b44c5a6SAntoine Ténart 				.cra_flags = CRYPTO_ALG_ASYNC |
7331b44c5a6SAntoine Ténart 					     CRYPTO_ALG_KERN_DRIVER_ONLY,
7341b44c5a6SAntoine Ténart 				.cra_blocksize = SHA1_BLOCK_SIZE,
7351b44c5a6SAntoine Ténart 				.cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
7361b44c5a6SAntoine Ténart 				.cra_init = safexcel_ahash_cra_init,
7371b44c5a6SAntoine Ténart 				.cra_exit = safexcel_ahash_cra_exit,
7381b44c5a6SAntoine Ténart 				.cra_module = THIS_MODULE,
7391b44c5a6SAntoine Ténart 			},
7401b44c5a6SAntoine Ténart 		},
7411b44c5a6SAntoine Ténart 	},
7421b44c5a6SAntoine Ténart };
7431b44c5a6SAntoine Ténart 
7441b44c5a6SAntoine Ténart static int safexcel_hmac_sha1_init(struct ahash_request *areq)
7451b44c5a6SAntoine Ténart {
7461b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
7471b44c5a6SAntoine Ténart 
7481b44c5a6SAntoine Ténart 	safexcel_sha1_init(areq);
7491b44c5a6SAntoine Ténart 	ctx->digest = CONTEXT_CONTROL_DIGEST_HMAC;
7501b44c5a6SAntoine Ténart 	return 0;
7511b44c5a6SAntoine Ténart }
7521b44c5a6SAntoine Ténart 
7531b44c5a6SAntoine Ténart static int safexcel_hmac_sha1_digest(struct ahash_request *areq)
7541b44c5a6SAntoine Ténart {
7551b44c5a6SAntoine Ténart 	int ret = safexcel_hmac_sha1_init(areq);
7561b44c5a6SAntoine Ténart 
7571b44c5a6SAntoine Ténart 	if (ret)
7581b44c5a6SAntoine Ténart 		return ret;
7591b44c5a6SAntoine Ténart 
7601b44c5a6SAntoine Ténart 	return safexcel_ahash_finup(areq);
7611b44c5a6SAntoine Ténart }
7621b44c5a6SAntoine Ténart 
7631b44c5a6SAntoine Ténart struct safexcel_ahash_result {
7641b44c5a6SAntoine Ténart 	struct completion completion;
7651b44c5a6SAntoine Ténart 	int error;
7661b44c5a6SAntoine Ténart };
7671b44c5a6SAntoine Ténart 
7681b44c5a6SAntoine Ténart static void safexcel_ahash_complete(struct crypto_async_request *req, int error)
7691b44c5a6SAntoine Ténart {
7701b44c5a6SAntoine Ténart 	struct safexcel_ahash_result *result = req->data;
7711b44c5a6SAntoine Ténart 
7721b44c5a6SAntoine Ténart 	if (error == -EINPROGRESS)
7731b44c5a6SAntoine Ténart 		return;
7741b44c5a6SAntoine Ténart 
7751b44c5a6SAntoine Ténart 	result->error = error;
7761b44c5a6SAntoine Ténart 	complete(&result->completion);
7771b44c5a6SAntoine Ténart }
7781b44c5a6SAntoine Ténart 
7791b44c5a6SAntoine Ténart static int safexcel_hmac_init_pad(struct ahash_request *areq,
7801b44c5a6SAntoine Ténart 				  unsigned int blocksize, const u8 *key,
7811b44c5a6SAntoine Ténart 				  unsigned int keylen, u8 *ipad, u8 *opad)
7821b44c5a6SAntoine Ténart {
7831b44c5a6SAntoine Ténart 	struct safexcel_ahash_result result;
7841b44c5a6SAntoine Ténart 	struct scatterlist sg;
7851b44c5a6SAntoine Ténart 	int ret, i;
7861b44c5a6SAntoine Ténart 	u8 *keydup;
7871b44c5a6SAntoine Ténart 
7881b44c5a6SAntoine Ténart 	if (keylen <= blocksize) {
7891b44c5a6SAntoine Ténart 		memcpy(ipad, key, keylen);
7901b44c5a6SAntoine Ténart 	} else {
7911b44c5a6SAntoine Ténart 		keydup = kmemdup(key, keylen, GFP_KERNEL);
7921b44c5a6SAntoine Ténart 		if (!keydup)
7931b44c5a6SAntoine Ténart 			return -ENOMEM;
7941b44c5a6SAntoine Ténart 
7951b44c5a6SAntoine Ténart 		ahash_request_set_callback(areq, CRYPTO_TFM_REQ_MAY_BACKLOG,
7961b44c5a6SAntoine Ténart 					   safexcel_ahash_complete, &result);
7971b44c5a6SAntoine Ténart 		sg_init_one(&sg, keydup, keylen);
7981b44c5a6SAntoine Ténart 		ahash_request_set_crypt(areq, &sg, ipad, keylen);
7991b44c5a6SAntoine Ténart 		init_completion(&result.completion);
8001b44c5a6SAntoine Ténart 
8011b44c5a6SAntoine Ténart 		ret = crypto_ahash_digest(areq);
8021b44c5a6SAntoine Ténart 		if (ret == -EINPROGRESS) {
8031b44c5a6SAntoine Ténart 			wait_for_completion_interruptible(&result.completion);
8041b44c5a6SAntoine Ténart 			ret = result.error;
8051b44c5a6SAntoine Ténart 		}
8061b44c5a6SAntoine Ténart 
8071b44c5a6SAntoine Ténart 		/* Avoid leaking */
8081b44c5a6SAntoine Ténart 		memzero_explicit(keydup, keylen);
8091b44c5a6SAntoine Ténart 		kfree(keydup);
8101b44c5a6SAntoine Ténart 
8111b44c5a6SAntoine Ténart 		if (ret)
8121b44c5a6SAntoine Ténart 			return ret;
8131b44c5a6SAntoine Ténart 
8141b44c5a6SAntoine Ténart 		keylen = crypto_ahash_digestsize(crypto_ahash_reqtfm(areq));
8151b44c5a6SAntoine Ténart 	}
8161b44c5a6SAntoine Ténart 
8171b44c5a6SAntoine Ténart 	memset(ipad + keylen, 0, blocksize - keylen);
8181b44c5a6SAntoine Ténart 	memcpy(opad, ipad, blocksize);
8191b44c5a6SAntoine Ténart 
8201b44c5a6SAntoine Ténart 	for (i = 0; i < blocksize; i++) {
821aed3731eSAntoine Ténart 		ipad[i] ^= HMAC_IPAD_VALUE;
822aed3731eSAntoine Ténart 		opad[i] ^= HMAC_OPAD_VALUE;
8231b44c5a6SAntoine Ténart 	}
8241b44c5a6SAntoine Ténart 
8251b44c5a6SAntoine Ténart 	return 0;
8261b44c5a6SAntoine Ténart }
8271b44c5a6SAntoine Ténart 
8281b44c5a6SAntoine Ténart static int safexcel_hmac_init_iv(struct ahash_request *areq,
8291b44c5a6SAntoine Ténart 				 unsigned int blocksize, u8 *pad, void *state)
8301b44c5a6SAntoine Ténart {
8311b44c5a6SAntoine Ténart 	struct safexcel_ahash_result result;
8321b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req;
8331b44c5a6SAntoine Ténart 	struct scatterlist sg;
8341b44c5a6SAntoine Ténart 	int ret;
8351b44c5a6SAntoine Ténart 
8361b44c5a6SAntoine Ténart 	ahash_request_set_callback(areq, CRYPTO_TFM_REQ_MAY_BACKLOG,
8371b44c5a6SAntoine Ténart 				   safexcel_ahash_complete, &result);
8381b44c5a6SAntoine Ténart 	sg_init_one(&sg, pad, blocksize);
8391b44c5a6SAntoine Ténart 	ahash_request_set_crypt(areq, &sg, pad, blocksize);
8401b44c5a6SAntoine Ténart 	init_completion(&result.completion);
8411b44c5a6SAntoine Ténart 
8421b44c5a6SAntoine Ténart 	ret = crypto_ahash_init(areq);
8431b44c5a6SAntoine Ténart 	if (ret)
8441b44c5a6SAntoine Ténart 		return ret;
8451b44c5a6SAntoine Ténart 
8461b44c5a6SAntoine Ténart 	req = ahash_request_ctx(areq);
8471b44c5a6SAntoine Ténart 	req->hmac = true;
8481b44c5a6SAntoine Ténart 	req->last_req = true;
8491b44c5a6SAntoine Ténart 
8501b44c5a6SAntoine Ténart 	ret = crypto_ahash_update(areq);
8511b44c5a6SAntoine Ténart 	if (ret && ret != -EINPROGRESS)
8521b44c5a6SAntoine Ténart 		return ret;
8531b44c5a6SAntoine Ténart 
8541b44c5a6SAntoine Ténart 	wait_for_completion_interruptible(&result.completion);
8551b44c5a6SAntoine Ténart 	if (result.error)
8561b44c5a6SAntoine Ténart 		return result.error;
8571b44c5a6SAntoine Ténart 
8581b44c5a6SAntoine Ténart 	return crypto_ahash_export(areq, state);
8591b44c5a6SAntoine Ténart }
8601b44c5a6SAntoine Ténart 
8611b44c5a6SAntoine Ténart static int safexcel_hmac_setkey(const char *alg, const u8 *key,
8621b44c5a6SAntoine Ténart 				unsigned int keylen, void *istate, void *ostate)
8631b44c5a6SAntoine Ténart {
8641b44c5a6SAntoine Ténart 	struct ahash_request *areq;
8651b44c5a6SAntoine Ténart 	struct crypto_ahash *tfm;
8661b44c5a6SAntoine Ténart 	unsigned int blocksize;
8671b44c5a6SAntoine Ténart 	u8 *ipad, *opad;
8681b44c5a6SAntoine Ténart 	int ret;
8691b44c5a6SAntoine Ténart 
8701b44c5a6SAntoine Ténart 	tfm = crypto_alloc_ahash(alg, CRYPTO_ALG_TYPE_AHASH,
8711b44c5a6SAntoine Ténart 				 CRYPTO_ALG_TYPE_AHASH_MASK);
8721b44c5a6SAntoine Ténart 	if (IS_ERR(tfm))
8731b44c5a6SAntoine Ténart 		return PTR_ERR(tfm);
8741b44c5a6SAntoine Ténart 
8751b44c5a6SAntoine Ténart 	areq = ahash_request_alloc(tfm, GFP_KERNEL);
8761b44c5a6SAntoine Ténart 	if (!areq) {
8771b44c5a6SAntoine Ténart 		ret = -ENOMEM;
8781b44c5a6SAntoine Ténart 		goto free_ahash;
8791b44c5a6SAntoine Ténart 	}
8801b44c5a6SAntoine Ténart 
8811b44c5a6SAntoine Ténart 	crypto_ahash_clear_flags(tfm, ~0);
8821b44c5a6SAntoine Ténart 	blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
8831b44c5a6SAntoine Ténart 
8841b44c5a6SAntoine Ténart 	ipad = kzalloc(2 * blocksize, GFP_KERNEL);
8851b44c5a6SAntoine Ténart 	if (!ipad) {
8861b44c5a6SAntoine Ténart 		ret = -ENOMEM;
8871b44c5a6SAntoine Ténart 		goto free_request;
8881b44c5a6SAntoine Ténart 	}
8891b44c5a6SAntoine Ténart 
8901b44c5a6SAntoine Ténart 	opad = ipad + blocksize;
8911b44c5a6SAntoine Ténart 
8921b44c5a6SAntoine Ténart 	ret = safexcel_hmac_init_pad(areq, blocksize, key, keylen, ipad, opad);
8931b44c5a6SAntoine Ténart 	if (ret)
8941b44c5a6SAntoine Ténart 		goto free_ipad;
8951b44c5a6SAntoine Ténart 
8961b44c5a6SAntoine Ténart 	ret = safexcel_hmac_init_iv(areq, blocksize, ipad, istate);
8971b44c5a6SAntoine Ténart 	if (ret)
8981b44c5a6SAntoine Ténart 		goto free_ipad;
8991b44c5a6SAntoine Ténart 
9001b44c5a6SAntoine Ténart 	ret = safexcel_hmac_init_iv(areq, blocksize, opad, ostate);
9011b44c5a6SAntoine Ténart 
9021b44c5a6SAntoine Ténart free_ipad:
9031b44c5a6SAntoine Ténart 	kfree(ipad);
9041b44c5a6SAntoine Ténart free_request:
9051b44c5a6SAntoine Ténart 	ahash_request_free(areq);
9061b44c5a6SAntoine Ténart free_ahash:
9071b44c5a6SAntoine Ténart 	crypto_free_ahash(tfm);
9081b44c5a6SAntoine Ténart 
9091b44c5a6SAntoine Ténart 	return ret;
9101b44c5a6SAntoine Ténart }
9111b44c5a6SAntoine Ténart 
9121b44c5a6SAntoine Ténart static int safexcel_hmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key,
9131b44c5a6SAntoine Ténart 				     unsigned int keylen)
9141b44c5a6SAntoine Ténart {
9151b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
9161b44c5a6SAntoine Ténart 	struct safexcel_ahash_export_state istate, ostate;
9171b44c5a6SAntoine Ténart 	int ret, i;
9181b44c5a6SAntoine Ténart 
9191b44c5a6SAntoine Ténart 	ret = safexcel_hmac_setkey("safexcel-sha1", key, keylen, &istate, &ostate);
9201b44c5a6SAntoine Ténart 	if (ret)
9211b44c5a6SAntoine Ténart 		return ret;
9221b44c5a6SAntoine Ténart 
92360c4081eSAntoine Ténart 	for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) {
9241b44c5a6SAntoine Ténart 		if (ctx->ipad[i] != le32_to_cpu(istate.state[i]) ||
9251b44c5a6SAntoine Ténart 		    ctx->opad[i] != le32_to_cpu(ostate.state[i])) {
9261b44c5a6SAntoine Ténart 			ctx->base.needs_inv = true;
9271b44c5a6SAntoine Ténart 			break;
9281b44c5a6SAntoine Ténart 		}
9291b44c5a6SAntoine Ténart 	}
9301b44c5a6SAntoine Ténart 
93142ef3bedSAntoine Ténart 	memcpy(ctx->ipad, &istate.state, SHA1_DIGEST_SIZE);
93242ef3bedSAntoine Ténart 	memcpy(ctx->opad, &ostate.state, SHA1_DIGEST_SIZE);
93342ef3bedSAntoine Ténart 
9341b44c5a6SAntoine Ténart 	return 0;
9351b44c5a6SAntoine Ténart }
9361b44c5a6SAntoine Ténart 
9371b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_hmac_sha1 = {
9381b44c5a6SAntoine Ténart 	.type = SAFEXCEL_ALG_TYPE_AHASH,
9391b44c5a6SAntoine Ténart 	.alg.ahash = {
9401b44c5a6SAntoine Ténart 		.init = safexcel_hmac_sha1_init,
9411b44c5a6SAntoine Ténart 		.update = safexcel_ahash_update,
9421b44c5a6SAntoine Ténart 		.final = safexcel_ahash_final,
9431b44c5a6SAntoine Ténart 		.finup = safexcel_ahash_finup,
9441b44c5a6SAntoine Ténart 		.digest = safexcel_hmac_sha1_digest,
9451b44c5a6SAntoine Ténart 		.setkey = safexcel_hmac_sha1_setkey,
9461b44c5a6SAntoine Ténart 		.export = safexcel_ahash_export,
9471b44c5a6SAntoine Ténart 		.import = safexcel_ahash_import,
9481b44c5a6SAntoine Ténart 		.halg = {
9491b44c5a6SAntoine Ténart 			.digestsize = SHA1_DIGEST_SIZE,
9501b44c5a6SAntoine Ténart 			.statesize = sizeof(struct safexcel_ahash_export_state),
9511b44c5a6SAntoine Ténart 			.base = {
9521b44c5a6SAntoine Ténart 				.cra_name = "hmac(sha1)",
9531b44c5a6SAntoine Ténart 				.cra_driver_name = "safexcel-hmac-sha1",
9541b44c5a6SAntoine Ténart 				.cra_priority = 300,
9551b44c5a6SAntoine Ténart 				.cra_flags = CRYPTO_ALG_ASYNC |
9561b44c5a6SAntoine Ténart 					     CRYPTO_ALG_KERN_DRIVER_ONLY,
9571b44c5a6SAntoine Ténart 				.cra_blocksize = SHA1_BLOCK_SIZE,
9581b44c5a6SAntoine Ténart 				.cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
9591b44c5a6SAntoine Ténart 				.cra_init = safexcel_ahash_cra_init,
9601b44c5a6SAntoine Ténart 				.cra_exit = safexcel_ahash_cra_exit,
9611b44c5a6SAntoine Ténart 				.cra_module = THIS_MODULE,
9621b44c5a6SAntoine Ténart 			},
9631b44c5a6SAntoine Ténart 		},
9641b44c5a6SAntoine Ténart 	},
9651b44c5a6SAntoine Ténart };
9661b44c5a6SAntoine Ténart 
9671b44c5a6SAntoine Ténart static int safexcel_sha256_init(struct ahash_request *areq)
9681b44c5a6SAntoine Ténart {
9691b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
9701b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
9711b44c5a6SAntoine Ténart 
9721b44c5a6SAntoine Ténart 	memset(req, 0, sizeof(*req));
9731b44c5a6SAntoine Ténart 
9741b44c5a6SAntoine Ténart 	req->state[0] = SHA256_H0;
9751b44c5a6SAntoine Ténart 	req->state[1] = SHA256_H1;
9761b44c5a6SAntoine Ténart 	req->state[2] = SHA256_H2;
9771b44c5a6SAntoine Ténart 	req->state[3] = SHA256_H3;
9781b44c5a6SAntoine Ténart 	req->state[4] = SHA256_H4;
9791b44c5a6SAntoine Ténart 	req->state[5] = SHA256_H5;
9801b44c5a6SAntoine Ténart 	req->state[6] = SHA256_H6;
9811b44c5a6SAntoine Ténart 	req->state[7] = SHA256_H7;
9821b44c5a6SAntoine Ténart 
9831b44c5a6SAntoine Ténart 	ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA256;
9841b44c5a6SAntoine Ténart 	ctx->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED;
9851b44c5a6SAntoine Ténart 	req->state_sz = SHA256_DIGEST_SIZE;
9861b44c5a6SAntoine Ténart 
9871b44c5a6SAntoine Ténart 	return 0;
9881b44c5a6SAntoine Ténart }
9891b44c5a6SAntoine Ténart 
9901b44c5a6SAntoine Ténart static int safexcel_sha256_digest(struct ahash_request *areq)
9911b44c5a6SAntoine Ténart {
9921b44c5a6SAntoine Ténart 	int ret = safexcel_sha256_init(areq);
9931b44c5a6SAntoine Ténart 
9941b44c5a6SAntoine Ténart 	if (ret)
9951b44c5a6SAntoine Ténart 		return ret;
9961b44c5a6SAntoine Ténart 
9971b44c5a6SAntoine Ténart 	return safexcel_ahash_finup(areq);
9981b44c5a6SAntoine Ténart }
9991b44c5a6SAntoine Ténart 
10001b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_sha256 = {
10011b44c5a6SAntoine Ténart 	.type = SAFEXCEL_ALG_TYPE_AHASH,
10021b44c5a6SAntoine Ténart 	.alg.ahash = {
10031b44c5a6SAntoine Ténart 		.init = safexcel_sha256_init,
10041b44c5a6SAntoine Ténart 		.update = safexcel_ahash_update,
10051b44c5a6SAntoine Ténart 		.final = safexcel_ahash_final,
10061b44c5a6SAntoine Ténart 		.finup = safexcel_ahash_finup,
10071b44c5a6SAntoine Ténart 		.digest = safexcel_sha256_digest,
10081b44c5a6SAntoine Ténart 		.export = safexcel_ahash_export,
10091b44c5a6SAntoine Ténart 		.import = safexcel_ahash_import,
10101b44c5a6SAntoine Ténart 		.halg = {
10111b44c5a6SAntoine Ténart 			.digestsize = SHA256_DIGEST_SIZE,
10121b44c5a6SAntoine Ténart 			.statesize = sizeof(struct safexcel_ahash_export_state),
10131b44c5a6SAntoine Ténart 			.base = {
10141b44c5a6SAntoine Ténart 				.cra_name = "sha256",
10151b44c5a6SAntoine Ténart 				.cra_driver_name = "safexcel-sha256",
10161b44c5a6SAntoine Ténart 				.cra_priority = 300,
10171b44c5a6SAntoine Ténart 				.cra_flags = CRYPTO_ALG_ASYNC |
10181b44c5a6SAntoine Ténart 					     CRYPTO_ALG_KERN_DRIVER_ONLY,
10191b44c5a6SAntoine Ténart 				.cra_blocksize = SHA256_BLOCK_SIZE,
10201b44c5a6SAntoine Ténart 				.cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
10211b44c5a6SAntoine Ténart 				.cra_init = safexcel_ahash_cra_init,
10221b44c5a6SAntoine Ténart 				.cra_exit = safexcel_ahash_cra_exit,
10231b44c5a6SAntoine Ténart 				.cra_module = THIS_MODULE,
10241b44c5a6SAntoine Ténart 			},
10251b44c5a6SAntoine Ténart 		},
10261b44c5a6SAntoine Ténart 	},
10271b44c5a6SAntoine Ténart };
10281b44c5a6SAntoine Ténart 
10291b44c5a6SAntoine Ténart static int safexcel_sha224_init(struct ahash_request *areq)
10301b44c5a6SAntoine Ténart {
10311b44c5a6SAntoine Ténart 	struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
10321b44c5a6SAntoine Ténart 	struct safexcel_ahash_req *req = ahash_request_ctx(areq);
10331b44c5a6SAntoine Ténart 
10341b44c5a6SAntoine Ténart 	memset(req, 0, sizeof(*req));
10351b44c5a6SAntoine Ténart 
10361b44c5a6SAntoine Ténart 	req->state[0] = SHA224_H0;
10371b44c5a6SAntoine Ténart 	req->state[1] = SHA224_H1;
10381b44c5a6SAntoine Ténart 	req->state[2] = SHA224_H2;
10391b44c5a6SAntoine Ténart 	req->state[3] = SHA224_H3;
10401b44c5a6SAntoine Ténart 	req->state[4] = SHA224_H4;
10411b44c5a6SAntoine Ténart 	req->state[5] = SHA224_H5;
10421b44c5a6SAntoine Ténart 	req->state[6] = SHA224_H6;
10431b44c5a6SAntoine Ténart 	req->state[7] = SHA224_H7;
10441b44c5a6SAntoine Ténart 
10451b44c5a6SAntoine Ténart 	ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA224;
10461b44c5a6SAntoine Ténart 	ctx->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED;
10471b44c5a6SAntoine Ténart 	req->state_sz = SHA256_DIGEST_SIZE;
10481b44c5a6SAntoine Ténart 
10491b44c5a6SAntoine Ténart 	return 0;
10501b44c5a6SAntoine Ténart }
10511b44c5a6SAntoine Ténart 
10521b44c5a6SAntoine Ténart static int safexcel_sha224_digest(struct ahash_request *areq)
10531b44c5a6SAntoine Ténart {
10541b44c5a6SAntoine Ténart 	int ret = safexcel_sha224_init(areq);
10551b44c5a6SAntoine Ténart 
10561b44c5a6SAntoine Ténart 	if (ret)
10571b44c5a6SAntoine Ténart 		return ret;
10581b44c5a6SAntoine Ténart 
10591b44c5a6SAntoine Ténart 	return safexcel_ahash_finup(areq);
10601b44c5a6SAntoine Ténart }
10611b44c5a6SAntoine Ténart 
10621b44c5a6SAntoine Ténart struct safexcel_alg_template safexcel_alg_sha224 = {
10631b44c5a6SAntoine Ténart 	.type = SAFEXCEL_ALG_TYPE_AHASH,
10641b44c5a6SAntoine Ténart 	.alg.ahash = {
10651b44c5a6SAntoine Ténart 		.init = safexcel_sha224_init,
10661b44c5a6SAntoine Ténart 		.update = safexcel_ahash_update,
10671b44c5a6SAntoine Ténart 		.final = safexcel_ahash_final,
10681b44c5a6SAntoine Ténart 		.finup = safexcel_ahash_finup,
10691b44c5a6SAntoine Ténart 		.digest = safexcel_sha224_digest,
10701b44c5a6SAntoine Ténart 		.export = safexcel_ahash_export,
10711b44c5a6SAntoine Ténart 		.import = safexcel_ahash_import,
10721b44c5a6SAntoine Ténart 		.halg = {
10731b44c5a6SAntoine Ténart 			.digestsize = SHA224_DIGEST_SIZE,
10741b44c5a6SAntoine Ténart 			.statesize = sizeof(struct safexcel_ahash_export_state),
10751b44c5a6SAntoine Ténart 			.base = {
10761b44c5a6SAntoine Ténart 				.cra_name = "sha224",
10771b44c5a6SAntoine Ténart 				.cra_driver_name = "safexcel-sha224",
10781b44c5a6SAntoine Ténart 				.cra_priority = 300,
10791b44c5a6SAntoine Ténart 				.cra_flags = CRYPTO_ALG_ASYNC |
10801b44c5a6SAntoine Ténart 					     CRYPTO_ALG_KERN_DRIVER_ONLY,
10811b44c5a6SAntoine Ténart 				.cra_blocksize = SHA224_BLOCK_SIZE,
10821b44c5a6SAntoine Ténart 				.cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
10831b44c5a6SAntoine Ténart 				.cra_init = safexcel_ahash_cra_init,
10841b44c5a6SAntoine Ténart 				.cra_exit = safexcel_ahash_cra_exit,
10851b44c5a6SAntoine Ténart 				.cra_module = THIS_MODULE,
10861b44c5a6SAntoine Ténart 			},
10871b44c5a6SAntoine Ténart 		},
10881b44c5a6SAntoine Ténart 	},
10891b44c5a6SAntoine Ténart };
1090