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)  * AMD Cryptographic Coprocessor (CCP) SHA crypto API support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2013,2018 Advanced Micro Devices, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Tom Lendacky <thomas.lendacky@amd.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Author: Gary R Hook <gary.hook@amd.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <crypto/hmac.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <crypto/sha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "ccp-crypto.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static int ccp_sha_complete(struct crypto_async_request *async_req, int ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct ahash_request *req = ahash_request_cast(async_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	unsigned int digest_size = crypto_ahash_digestsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		goto e_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (rctx->hash_rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		/* Save remaining data to buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		unsigned int offset = rctx->nbytes - rctx->hash_rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		scatterwalk_map_and_copy(rctx->buf, rctx->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 					 offset, rctx->hash_rem, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		rctx->buf_count = rctx->hash_rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		rctx->buf_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	/* Update result area if supplied */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (req->result && rctx->final)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		memcpy(req->result, rctx->ctx, digest_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) e_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	sg_free_table(&rctx->data_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static int ccp_do_sha_update(struct ahash_request *req, unsigned int nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			     unsigned int final)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	unsigned int block_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	unsigned int sg_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	gfp_t gfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	len = (u64)rctx->buf_count + (u64)nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if (!final && (len <= block_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 					 0, nbytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		rctx->buf_count += nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	rctx->src = req->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	rctx->nbytes = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	rctx->final = final;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	rctx->hash_rem = final ? 0 : len & (block_size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	rctx->hash_cnt = len - rctx->hash_rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (!final && !rctx->hash_rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		/* CCP can't do zero length final, so keep some data around */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		rctx->hash_cnt -= block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		rctx->hash_rem = block_size;
^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) 	/* Initialize the context scatterlist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	sg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (rctx->buf_count && nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		/* Build the data scatterlist table - allocate enough entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		 * for both data pieces (buffer and input data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			GFP_KERNEL : GFP_ATOMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		sg_count = sg_nents(req->src) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		if (!sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			goto e_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		if (!sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			goto e_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		sg_mark_end(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		sg = rctx->data_sg.sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	} else if (rctx->buf_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		sg = &rctx->buf_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	} else if (nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		sg = req->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	rctx->msg_bits += (rctx->hash_cnt << 3);	/* Total in bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	INIT_LIST_HEAD(&rctx->cmd.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	rctx->cmd.engine = CCP_ENGINE_SHA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	rctx->cmd.u.sha.type = rctx->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	rctx->cmd.u.sha.ctx = &rctx->ctx_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	switch (rctx->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	case CCP_SHA_TYPE_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		rctx->cmd.u.sha.ctx_len = SHA1_DIGEST_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	case CCP_SHA_TYPE_224:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		rctx->cmd.u.sha.ctx_len = SHA224_DIGEST_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	case CCP_SHA_TYPE_256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		rctx->cmd.u.sha.ctx_len = SHA256_DIGEST_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	case CCP_SHA_TYPE_384:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		rctx->cmd.u.sha.ctx_len = SHA384_DIGEST_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	case CCP_SHA_TYPE_512:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		rctx->cmd.u.sha.ctx_len = SHA512_DIGEST_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		/* Should never get here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	rctx->cmd.u.sha.src = sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	rctx->cmd.u.sha.src_len = rctx->hash_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	rctx->cmd.u.sha.opad = ctx->u.sha.key_len ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		&ctx->u.sha.opad_sg : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		ctx->u.sha.opad_count : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	rctx->cmd.u.sha.first = rctx->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	rctx->cmd.u.sha.final = rctx->final;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	rctx->first = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) e_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	sg_free_table(&rctx->data_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static int ccp_sha_init(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct ccp_crypto_ahash_alg *alg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	unsigned int block_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	memset(rctx, 0, sizeof(*rctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	rctx->type = alg->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	rctx->first = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (ctx->u.sha.key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		/* Buffer the HMAC key for first update */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		rctx->buf_count = block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^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 ccp_sha_update(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return ccp_do_sha_update(req, req->nbytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static int ccp_sha_final(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	return ccp_do_sha_update(req, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static int ccp_sha_finup(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	return ccp_do_sha_update(req, req->nbytes, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static int ccp_sha_digest(struct ahash_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	ret = ccp_sha_init(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	return ccp_sha_finup(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static int ccp_sha_export(struct ahash_request *req, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct ccp_sha_exp_ctx state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	/* Don't let anything leak to 'out' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	memset(&state, 0, sizeof(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	state.type = rctx->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	state.msg_bits = rctx->msg_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	state.first = rctx->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	memcpy(state.ctx, rctx->ctx, sizeof(state.ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	state.buf_count = rctx->buf_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	memcpy(state.buf, rctx->buf, sizeof(state.buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	/* 'out' may not be aligned so memcpy from local variable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	memcpy(out, &state, sizeof(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static int ccp_sha_import(struct ahash_request *req, const void *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct ccp_sha_exp_ctx state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	/* 'in' may not be aligned so memcpy to local variable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	memcpy(&state, in, sizeof(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	memset(rctx, 0, sizeof(*rctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	rctx->type = state.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	rctx->msg_bits = state.msg_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	rctx->first = state.first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	memcpy(rctx->ctx, state.ctx, sizeof(rctx->ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	rctx->buf_count = state.buf_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	memcpy(rctx->buf, state.buf, sizeof(rctx->buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			  unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	struct crypto_shash *shash = ctx->u.sha.hmac_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	unsigned int block_size = crypto_shash_blocksize(shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	unsigned int digest_size = crypto_shash_digestsize(shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	/* Set to zero until complete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	ctx->u.sha.key_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	/* Clear key area to provide zero padding for keys smaller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * than the block size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (key_len > block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		/* Must hash the input key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		ret = crypto_shash_tfm_digest(shash, key, key_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 					      ctx->u.sha.key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		key_len = digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		memcpy(ctx->u.sha.key, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	for (i = 0; i < block_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ HMAC_IPAD_VALUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ HMAC_OPAD_VALUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	ctx->u.sha.opad_count = block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	ctx->u.sha.key_len = key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static int ccp_sha_cra_init(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	ctx->complete = ccp_sha_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	ctx->u.sha.key_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	struct crypto_shash *hmac_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	if (IS_ERR(hmac_tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		pr_warn("could not load driver %s need for HMAC support\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			alg->child_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		return PTR_ERR(hmac_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	ctx->u.sha.hmac_tfm = hmac_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	return ccp_sha_cra_init(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (ctx->u.sha.hmac_tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		crypto_free_shash(ctx->u.sha.hmac_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	ccp_sha_cra_exit(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct ccp_sha_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	unsigned int version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	const char *drv_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	enum ccp_sha_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	u32 digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	u32 block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static struct ccp_sha_def sha_algs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		.version	= CCP_VERSION(3, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		.name		= "sha1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		.drv_name	= "sha1-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		.type		= CCP_SHA_TYPE_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		.digest_size	= SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		.block_size	= SHA1_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		.version	= CCP_VERSION(3, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		.name		= "sha224",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		.drv_name	= "sha224-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		.type		= CCP_SHA_TYPE_224,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		.digest_size	= SHA224_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		.block_size	= SHA224_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		.version	= CCP_VERSION(3, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		.name		= "sha256",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		.drv_name	= "sha256-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		.type		= CCP_SHA_TYPE_256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		.digest_size	= SHA256_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		.block_size	= SHA256_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		.version	= CCP_VERSION(5, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		.name		= "sha384",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		.drv_name	= "sha384-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		.type		= CCP_SHA_TYPE_384,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		.digest_size	= SHA384_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		.block_size	= SHA384_BLOCK_SIZE,
^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) 		.version	= CCP_VERSION(5, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		.name		= "sha512",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		.drv_name	= "sha512-ccp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		.type		= CCP_SHA_TYPE_512,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		.digest_size	= SHA512_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		.block_size	= SHA512_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static int ccp_register_hmac_alg(struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 				 const struct ccp_sha_def *def,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 				 const struct ccp_crypto_ahash_alg *base_alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	struct ccp_crypto_ahash_alg *ccp_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	struct ahash_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	struct hash_alg_common *halg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	struct crypto_alg *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	if (!ccp_alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	/* Copy the base algorithm and only change what's necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	*ccp_alg = *base_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	INIT_LIST_HEAD(&ccp_alg->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	strscpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	alg = &ccp_alg->alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	alg->setkey = ccp_sha_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	halg = &alg->halg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	base = &halg->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		 def->drv_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	base->cra_init = ccp_hmac_sha_cra_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	base->cra_exit = ccp_hmac_sha_cra_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	ret = crypto_register_ahash(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		pr_err("%s ahash algorithm registration error (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		       base->cra_name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		kfree(ccp_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	list_add(&ccp_alg->entry, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static int ccp_register_sha_alg(struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 				const struct ccp_sha_def *def)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	struct ccp_crypto_ahash_alg *ccp_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct ahash_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	struct hash_alg_common *halg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	struct crypto_alg *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	if (!ccp_alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	INIT_LIST_HEAD(&ccp_alg->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	ccp_alg->type = def->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	alg = &ccp_alg->alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	alg->init = ccp_sha_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	alg->update = ccp_sha_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	alg->final = ccp_sha_final;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	alg->finup = ccp_sha_finup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	alg->digest = ccp_sha_digest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	alg->export = ccp_sha_export;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	alg->import = ccp_sha_import;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	halg = &alg->halg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	halg->digestsize = def->digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	halg->statesize = sizeof(struct ccp_sha_exp_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	base = &halg->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		 def->drv_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	base->cra_flags = CRYPTO_ALG_ASYNC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			  CRYPTO_ALG_ALLOCATES_MEMORY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 			  CRYPTO_ALG_KERN_DRIVER_ONLY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			  CRYPTO_ALG_NEED_FALLBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	base->cra_blocksize = def->block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	base->cra_ctxsize = sizeof(struct ccp_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	base->cra_priority = CCP_CRA_PRIORITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	base->cra_init = ccp_sha_cra_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	base->cra_exit = ccp_sha_cra_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	base->cra_module = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	ret = crypto_register_ahash(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		pr_err("%s ahash algorithm registration error (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		       base->cra_name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		kfree(ccp_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	list_add(&ccp_alg->entry, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	ret = ccp_register_hmac_alg(head, def, ccp_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) int ccp_register_sha_algs(struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	unsigned int ccpversion = ccp_version();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		if (sha_algs[i].version > ccpversion)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		ret = ccp_register_sha_alg(head, &sha_algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }