Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "sha.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /* crypto hw padding constant for first operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define SHA_PADDING		64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define SHA_PADDING_MASK	(SHA_PADDING - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static LIST_HEAD(ahash_algs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static const u32 std_iv_sha1[SHA256_DIGEST_SIZE / sizeof(u32)] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4, 0, 0, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static const u32 std_iv_sha256[SHA256_DIGEST_SIZE / sizeof(u32)] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	SHA256_H0, SHA256_H1, SHA256_H2, SHA256_H3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static void qce_ahash_done(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct crypto_async_request *async_req = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct ahash_request *req = ahash_request_cast(async_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct qce_device *qce = tmpl->qce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct qce_result_dump *result = qce->dma.result_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	unsigned int digestsize = crypto_ahash_digestsize(ahash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	u32 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	error = qce_dma_terminate_all(&qce->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		dev_dbg(qce->dev, "ahash dma termination error (%d)\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	dma_unmap_sg(qce->dev, req->src, rctx->src_nents, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	dma_unmap_sg(qce->dev, &rctx->result_sg, 1, DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	memcpy(rctx->digest, result->auth_iv, digestsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (req->result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		memcpy(req->result, result->auth_iv, digestsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	rctx->byte_count[0] = cpu_to_be32(result->auth_byte_count[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	rctx->byte_count[1] = cpu_to_be32(result->auth_byte_count[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	error = qce_check_status(qce, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		dev_dbg(qce->dev, "ahash operation error (%x)\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	req->src = rctx->src_orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	req->nbytes = rctx->nbytes_orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	rctx->last_blk = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	rctx->first_blk = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	qce->async_req_done(tmpl->qce, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static int qce_ahash_async_req_handle(struct crypto_async_request *async_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct ahash_request *req = ahash_request_cast(async_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct qce_sha_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct qce_device *qce = tmpl->qce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	unsigned long flags = rctx->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (IS_SHA_HMAC(flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		rctx->authkey = ctx->authkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		rctx->authklen = QCE_SHA_HMAC_KEY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	} else if (IS_CMAC(flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		rctx->authkey = ctx->authkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		rctx->authklen = AES_KEYSIZE_128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	rctx->src_nents = sg_nents_for_len(req->src, req->nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (rctx->src_nents < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		dev_err(qce->dev, "Invalid numbers of src SG.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return rctx->src_nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	ret = dma_map_sg(qce->dev, req->src, rctx->src_nents, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	ret = dma_map_sg(qce->dev, &rctx->result_sg, 1, DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		goto error_unmap_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	ret = qce_dma_prep_sgs(&qce->dma, req->src, rctx->src_nents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			       &rctx->result_sg, 1, qce_ahash_done, async_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		goto error_unmap_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	qce_dma_issue_pending(&qce->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	ret = qce_start(async_req, tmpl->crypto_alg_type, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		goto error_terminate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) error_terminate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	qce_dma_terminate_all(&qce->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) error_unmap_dst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	dma_unmap_sg(qce->dev, &rctx->result_sg, 1, DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) error_unmap_src:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	dma_unmap_sg(qce->dev, req->src, rctx->src_nents, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int qce_ahash_init(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	const u32 *std_iv = tmpl->std_iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	memset(rctx, 0, sizeof(*rctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	rctx->first_blk = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	rctx->last_blk = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	rctx->flags = tmpl->alg_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	memcpy(rctx->digest, std_iv, sizeof(rctx->digest));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int qce_ahash_export(struct ahash_request *req, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	unsigned long flags = rctx->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	unsigned int digestsize = crypto_ahash_digestsize(ahash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	unsigned int blocksize =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (IS_SHA1(flags) || IS_SHA1_HMAC(flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		struct sha1_state *out_state = out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		out_state->count = rctx->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		qce_cpu_to_be32p_array((__be32 *)out_state->state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				       rctx->digest, digestsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		memcpy(out_state->buffer, rctx->buf, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	} else if (IS_SHA256(flags) || IS_SHA256_HMAC(flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		struct sha256_state *out_state = out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		out_state->count = rctx->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		qce_cpu_to_be32p_array((__be32 *)out_state->state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				       rctx->digest, digestsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		memcpy(out_state->buf, rctx->buf, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int qce_import_common(struct ahash_request *req, u64 in_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			     const u32 *state, const u8 *buffer, bool hmac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	unsigned int digestsize = crypto_ahash_digestsize(ahash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	unsigned int blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	u64 count = in_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	rctx->count = in_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	memcpy(rctx->buf, buffer, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (in_count <= blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		rctx->first_blk = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		rctx->first_blk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		 * For HMAC, there is a hardware padding done when first block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		 * is set. Therefore the byte_count must be incremened by 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		 * after the first block operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		if (hmac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			count += SHA_PADDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	rctx->byte_count[0] = (__force __be32)(count & ~SHA_PADDING_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	rctx->byte_count[1] = (__force __be32)(count >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	qce_cpu_to_be32p_array((__be32 *)rctx->digest, (const u8 *)state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			       digestsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	rctx->buflen = (unsigned int)(in_count & (blocksize - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int qce_ahash_import(struct ahash_request *req, const void *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	struct qce_sha_reqctx *rctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	bool hmac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	ret = qce_ahash_init(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	flags = rctx->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	hmac = IS_SHA_HMAC(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (IS_SHA1(flags) || IS_SHA1_HMAC(flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		const struct sha1_state *state = in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		ret = qce_import_common(req, state->count, state->state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 					state->buffer, hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	} else if (IS_SHA256(flags) || IS_SHA256_HMAC(flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		const struct sha256_state *state = in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		ret = qce_import_common(req, state->count, state->state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 					state->buf, hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static int qce_ahash_update(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct qce_device *qce = tmpl->qce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct scatterlist *sg_last, *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	unsigned int total, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	unsigned int hash_later;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	unsigned int blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	rctx->count += req->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	/* check for buffer from previous updates and append it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	total = req->nbytes + rctx->buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (total <= blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		scatterwalk_map_and_copy(rctx->buf + rctx->buflen, req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 					 0, req->nbytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		rctx->buflen += req->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/* save the original req structure fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	rctx->src_orig = req->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	rctx->nbytes_orig = req->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	 * if we have data from previous update copy them on buffer. The old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	 * data will be combined with current request bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (rctx->buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		memcpy(rctx->tmpbuf, rctx->buf, rctx->buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	/* calculate how many bytes will be hashed later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	hash_later = total % blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (hash_later) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		unsigned int src_offset = req->nbytes - hash_later;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		scatterwalk_map_and_copy(rctx->buf, req->src, src_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 					 hash_later, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	/* here nbytes is multiple of blocksize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	nbytes = total - hash_later;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	len = rctx->buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	sg = sg_last = req->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	while (len < nbytes && sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		if (len + sg_dma_len(sg) > nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		len += sg_dma_len(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		sg_last = sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		sg = sg_next(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (!sg_last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (rctx->buflen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		sg_init_table(rctx->sg, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		sg_set_buf(rctx->sg, rctx->tmpbuf, rctx->buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		sg_chain(rctx->sg, 2, req->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		req->src = rctx->sg;
^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) 	req->nbytes = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	rctx->buflen = hash_later;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	return qce->async_req_enqueue(tmpl->qce, &req->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static int qce_ahash_final(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct qce_device *qce = tmpl->qce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (!rctx->buflen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		if (tmpl->hash_zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			memcpy(req->result, tmpl->hash_zero,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 					tmpl->alg.ahash.halg.digestsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	rctx->last_blk = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	rctx->src_orig = req->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	rctx->nbytes_orig = req->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	memcpy(rctx->tmpbuf, rctx->buf, rctx->buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	sg_init_one(rctx->sg, rctx->tmpbuf, rctx->buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	req->src = rctx->sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	req->nbytes = rctx->buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	return qce->async_req_enqueue(tmpl->qce, &req->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static int qce_ahash_digest(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	struct qce_device *qce = tmpl->qce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	ret = qce_ahash_init(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	rctx->src_orig = req->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	rctx->nbytes_orig = req->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	rctx->first_blk = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	rctx->last_blk = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (!rctx->nbytes_orig) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		if (tmpl->hash_zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			memcpy(req->result, tmpl->hash_zero,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 					tmpl->alg.ahash.halg.digestsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	return qce->async_req_enqueue(tmpl->qce, &req->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int qce_ahash_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 				 unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	unsigned int digestsize = crypto_ahash_digestsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	struct qce_sha_ctx *ctx = crypto_tfm_ctx(&tfm->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	struct crypto_wait wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	struct ahash_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct scatterlist sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	unsigned int blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	struct crypto_ahash *ahash_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	const char *alg_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	memset(ctx->authkey, 0, sizeof(ctx->authkey));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if (keylen <= blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		memcpy(ctx->authkey, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (digestsize == SHA1_DIGEST_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		alg_name = "sha1-qce";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	else if (digestsize == SHA256_DIGEST_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		alg_name = "sha256-qce";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	ahash_tfm = crypto_alloc_ahash(alg_name, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (IS_ERR(ahash_tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return PTR_ERR(ahash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	req = ahash_request_alloc(ahash_tfm, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (!req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		goto err_free_ahash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	crypto_init_wait(&wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 				   crypto_req_done, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	crypto_ahash_clear_flags(ahash_tfm, ~0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	buf = kzalloc(keylen + QCE_MAX_ALIGN_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		goto err_free_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	memcpy(buf, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	sg_init_one(&sg, buf, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	ahash_request_set_crypt(req, &sg, ctx->authkey, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) err_free_req:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	ahash_request_free(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) err_free_ahash:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	crypto_free_ahash(ahash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static int qce_ahash_cra_init(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	struct qce_sha_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	crypto_ahash_set_reqsize(ahash, sizeof(struct qce_sha_reqctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	memset(ctx, 0, sizeof(*ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct qce_ahash_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	const char *drv_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	unsigned int digestsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	unsigned int blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	unsigned int statesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	const u32 *std_iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static const struct qce_ahash_def ahash_def[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		.flags		= QCE_HASH_SHA1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		.name		= "sha1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		.drv_name	= "sha1-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		.digestsize	= SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		.blocksize	= SHA1_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		.statesize	= sizeof(struct sha1_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		.std_iv		= std_iv_sha1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		.flags		= QCE_HASH_SHA256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		.name		= "sha256",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		.drv_name	= "sha256-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		.digestsize	= SHA256_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		.blocksize	= SHA256_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		.statesize	= sizeof(struct sha256_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		.std_iv		= std_iv_sha256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		.flags		= QCE_HASH_SHA1_HMAC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		.name		= "hmac(sha1)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		.drv_name	= "hmac-sha1-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		.digestsize	= SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		.blocksize	= SHA1_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		.statesize	= sizeof(struct sha1_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		.std_iv		= std_iv_sha1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		.flags		= QCE_HASH_SHA256_HMAC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		.name		= "hmac(sha256)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		.drv_name	= "hmac-sha256-qce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		.digestsize	= SHA256_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		.blocksize	= SHA256_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		.statesize	= sizeof(struct sha256_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		.std_iv		= std_iv_sha256,
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) static int qce_ahash_register_one(const struct qce_ahash_def *def,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 				  struct qce_device *qce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	struct qce_alg_template *tmpl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	struct ahash_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	struct crypto_alg *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (!tmpl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	tmpl->std_iv = def->std_iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	alg = &tmpl->alg.ahash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	alg->init = qce_ahash_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	alg->update = qce_ahash_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	alg->final = qce_ahash_final;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	alg->digest = qce_ahash_digest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	alg->export = qce_ahash_export;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	alg->import = qce_ahash_import;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	if (IS_SHA_HMAC(def->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		alg->setkey = qce_ahash_hmac_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	alg->halg.digestsize = def->digestsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	alg->halg.statesize = def->statesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	if (IS_SHA1(def->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		tmpl->hash_zero = sha1_zero_message_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	else if (IS_SHA256(def->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		tmpl->hash_zero = sha256_zero_message_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	base = &alg->halg.base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	base->cra_blocksize = def->blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	base->cra_priority = 300;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	base->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	base->cra_ctxsize = sizeof(struct qce_sha_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	base->cra_alignmask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	base->cra_module = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	base->cra_init = qce_ahash_cra_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		 def->drv_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	INIT_LIST_HEAD(&tmpl->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_AHASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	tmpl->alg_flags = def->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	tmpl->qce = qce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	ret = crypto_register_ahash(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		dev_err(qce->dev, "%s registration failed\n", base->cra_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		kfree(tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	list_add_tail(&tmpl->entry, &ahash_algs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	dev_dbg(qce->dev, "%s is registered\n", base->cra_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	return 0;
^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) static void qce_ahash_unregister(struct qce_device *qce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	struct qce_alg_template *tmpl, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	list_for_each_entry_safe(tmpl, n, &ahash_algs, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		crypto_unregister_ahash(&tmpl->alg.ahash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		list_del(&tmpl->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		kfree(tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) static int qce_ahash_register(struct qce_device *qce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	for (i = 0; i < ARRAY_SIZE(ahash_def); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		ret = qce_ahash_register_one(&ahash_def[i], qce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	qce_ahash_unregister(qce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) const struct qce_algo_ops ahash_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	.type = CRYPTO_ALG_TYPE_AHASH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	.register_algs = qce_ahash_register,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	.unregister_algs = qce_ahash_unregister,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	.async_req_handle = qce_ahash_async_req_handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) };