xref: /OK3568_Linux_fs/kernel/drivers/crypto/nx/nx-sha256.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /**
3*4882a593Smuzhiyun  * SHA-256 routines supporting the Power 7+ Nest Accelerators driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2011-2012 International Business Machines Inc.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Kent Yoder <yoder1@us.ibm.com>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <crypto/internal/hash.h>
11*4882a593Smuzhiyun #include <crypto/sha.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <asm/vio.h>
14*4882a593Smuzhiyun #include <asm/byteorder.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "nx_csbcpb.h"
17*4882a593Smuzhiyun #include "nx.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun 
nx_crypto_ctx_sha256_init(struct crypto_tfm * tfm)20*4882a593Smuzhiyun static int nx_crypto_ctx_sha256_init(struct crypto_tfm *tfm)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
23*4882a593Smuzhiyun 	int err;
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun 	err = nx_crypto_ctx_sha_init(tfm);
26*4882a593Smuzhiyun 	if (err)
27*4882a593Smuzhiyun 		return err;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	nx_ctx_init(nx_ctx, HCOP_FC_SHA);
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA256];
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA256);
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	return 0;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun 
nx_sha256_init(struct shash_desc * desc)38*4882a593Smuzhiyun static int nx_sha256_init(struct shash_desc *desc) {
39*4882a593Smuzhiyun 	struct sha256_state *sctx = shash_desc_ctx(desc);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	memset(sctx, 0, sizeof *sctx);
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	sctx->state[0] = __cpu_to_be32(SHA256_H0);
44*4882a593Smuzhiyun 	sctx->state[1] = __cpu_to_be32(SHA256_H1);
45*4882a593Smuzhiyun 	sctx->state[2] = __cpu_to_be32(SHA256_H2);
46*4882a593Smuzhiyun 	sctx->state[3] = __cpu_to_be32(SHA256_H3);
47*4882a593Smuzhiyun 	sctx->state[4] = __cpu_to_be32(SHA256_H4);
48*4882a593Smuzhiyun 	sctx->state[5] = __cpu_to_be32(SHA256_H5);
49*4882a593Smuzhiyun 	sctx->state[6] = __cpu_to_be32(SHA256_H6);
50*4882a593Smuzhiyun 	sctx->state[7] = __cpu_to_be32(SHA256_H7);
51*4882a593Smuzhiyun 	sctx->count = 0;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	return 0;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
nx_sha256_update(struct shash_desc * desc,const u8 * data,unsigned int len)56*4882a593Smuzhiyun static int nx_sha256_update(struct shash_desc *desc, const u8 *data,
57*4882a593Smuzhiyun 			    unsigned int len)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	struct sha256_state *sctx = shash_desc_ctx(desc);
60*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
61*4882a593Smuzhiyun 	struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
62*4882a593Smuzhiyun 	struct nx_sg *out_sg;
63*4882a593Smuzhiyun 	u64 to_process = 0, leftover, total;
64*4882a593Smuzhiyun 	unsigned long irq_flags;
65*4882a593Smuzhiyun 	int rc = 0;
66*4882a593Smuzhiyun 	int data_len;
67*4882a593Smuzhiyun 	u32 max_sg_len;
68*4882a593Smuzhiyun 	u64 buf_len = (sctx->count % SHA256_BLOCK_SIZE);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	/* 2 cases for total data len:
73*4882a593Smuzhiyun 	 *  1: < SHA256_BLOCK_SIZE: copy into state, return 0
74*4882a593Smuzhiyun 	 *  2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover
75*4882a593Smuzhiyun 	 */
76*4882a593Smuzhiyun 	total = (sctx->count % SHA256_BLOCK_SIZE) + len;
77*4882a593Smuzhiyun 	if (total < SHA256_BLOCK_SIZE) {
78*4882a593Smuzhiyun 		memcpy(sctx->buf + buf_len, data, len);
79*4882a593Smuzhiyun 		sctx->count += len;
80*4882a593Smuzhiyun 		goto out;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	memcpy(csbcpb->cpb.sha256.message_digest, sctx->state, SHA256_DIGEST_SIZE);
84*4882a593Smuzhiyun 	NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
85*4882a593Smuzhiyun 	NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	max_sg_len = min_t(u64, nx_ctx->ap->sglen,
88*4882a593Smuzhiyun 			nx_driver.of.max_sg_len/sizeof(struct nx_sg));
89*4882a593Smuzhiyun 	max_sg_len = min_t(u64, max_sg_len,
90*4882a593Smuzhiyun 			nx_ctx->ap->databytelen/NX_PAGE_SIZE);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	data_len = SHA256_DIGEST_SIZE;
93*4882a593Smuzhiyun 	out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
94*4882a593Smuzhiyun 				  &data_len, max_sg_len);
95*4882a593Smuzhiyun 	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	if (data_len != SHA256_DIGEST_SIZE) {
98*4882a593Smuzhiyun 		rc = -EINVAL;
99*4882a593Smuzhiyun 		goto out;
100*4882a593Smuzhiyun 	}
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	do {
103*4882a593Smuzhiyun 		int used_sgs = 0;
104*4882a593Smuzhiyun 		struct nx_sg *in_sg = nx_ctx->in_sg;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 		if (buf_len) {
107*4882a593Smuzhiyun 			data_len = buf_len;
108*4882a593Smuzhiyun 			in_sg = nx_build_sg_list(in_sg,
109*4882a593Smuzhiyun 						 (u8 *) sctx->buf,
110*4882a593Smuzhiyun 						 &data_len,
111*4882a593Smuzhiyun 						 max_sg_len);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 			if (data_len != buf_len) {
114*4882a593Smuzhiyun 				rc = -EINVAL;
115*4882a593Smuzhiyun 				goto out;
116*4882a593Smuzhiyun 			}
117*4882a593Smuzhiyun 			used_sgs = in_sg - nx_ctx->in_sg;
118*4882a593Smuzhiyun 		}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 		/* to_process: SHA256_BLOCK_SIZE aligned chunk to be
121*4882a593Smuzhiyun 		 * processed in this iteration. This value is restricted
122*4882a593Smuzhiyun 		 * by sg list limits and number of sgs we already used
123*4882a593Smuzhiyun 		 * for leftover data. (see above)
124*4882a593Smuzhiyun 		 * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len,
125*4882a593Smuzhiyun 		 * but because data may not be aligned, we need to account
126*4882a593Smuzhiyun 		 * for that too. */
127*4882a593Smuzhiyun 		to_process = min_t(u64, total,
128*4882a593Smuzhiyun 			(max_sg_len - 1 - used_sgs) * NX_PAGE_SIZE);
129*4882a593Smuzhiyun 		to_process = to_process & ~(SHA256_BLOCK_SIZE - 1);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 		data_len = to_process - buf_len;
132*4882a593Smuzhiyun 		in_sg = nx_build_sg_list(in_sg, (u8 *) data,
133*4882a593Smuzhiyun 					 &data_len, max_sg_len);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 		nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 		to_process = data_len + buf_len;
138*4882a593Smuzhiyun 		leftover = total - to_process;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 		/*
141*4882a593Smuzhiyun 		 * we've hit the nx chip previously and we're updating
142*4882a593Smuzhiyun 		 * again, so copy over the partial digest.
143*4882a593Smuzhiyun 		 */
144*4882a593Smuzhiyun 		memcpy(csbcpb->cpb.sha256.input_partial_digest,
145*4882a593Smuzhiyun 			       csbcpb->cpb.sha256.message_digest,
146*4882a593Smuzhiyun 			       SHA256_DIGEST_SIZE);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 		if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
149*4882a593Smuzhiyun 			rc = -EINVAL;
150*4882a593Smuzhiyun 			goto out;
151*4882a593Smuzhiyun 		}
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
154*4882a593Smuzhiyun 		if (rc)
155*4882a593Smuzhiyun 			goto out;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 		atomic_inc(&(nx_ctx->stats->sha256_ops));
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 		total -= to_process;
160*4882a593Smuzhiyun 		data += to_process - buf_len;
161*4882a593Smuzhiyun 		buf_len = 0;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	} while (leftover >= SHA256_BLOCK_SIZE);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* copy the leftover back into the state struct */
166*4882a593Smuzhiyun 	if (leftover)
167*4882a593Smuzhiyun 		memcpy(sctx->buf, data, leftover);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	sctx->count += len;
170*4882a593Smuzhiyun 	memcpy(sctx->state, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
171*4882a593Smuzhiyun out:
172*4882a593Smuzhiyun 	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
173*4882a593Smuzhiyun 	return rc;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
nx_sha256_final(struct shash_desc * desc,u8 * out)176*4882a593Smuzhiyun static int nx_sha256_final(struct shash_desc *desc, u8 *out)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	struct sha256_state *sctx = shash_desc_ctx(desc);
179*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
180*4882a593Smuzhiyun 	struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
181*4882a593Smuzhiyun 	struct nx_sg *in_sg, *out_sg;
182*4882a593Smuzhiyun 	unsigned long irq_flags;
183*4882a593Smuzhiyun 	u32 max_sg_len;
184*4882a593Smuzhiyun 	int rc = 0;
185*4882a593Smuzhiyun 	int len;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	max_sg_len = min_t(u64, nx_ctx->ap->sglen,
190*4882a593Smuzhiyun 			nx_driver.of.max_sg_len/sizeof(struct nx_sg));
191*4882a593Smuzhiyun 	max_sg_len = min_t(u64, max_sg_len,
192*4882a593Smuzhiyun 			nx_ctx->ap->databytelen/NX_PAGE_SIZE);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	/* final is represented by continuing the operation and indicating that
195*4882a593Smuzhiyun 	 * this is not an intermediate operation */
196*4882a593Smuzhiyun 	if (sctx->count >= SHA256_BLOCK_SIZE) {
197*4882a593Smuzhiyun 		/* we've hit the nx chip previously, now we're finalizing,
198*4882a593Smuzhiyun 		 * so copy over the partial digest */
199*4882a593Smuzhiyun 		memcpy(csbcpb->cpb.sha256.input_partial_digest, sctx->state, SHA256_DIGEST_SIZE);
200*4882a593Smuzhiyun 		NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
201*4882a593Smuzhiyun 		NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
202*4882a593Smuzhiyun 	} else {
203*4882a593Smuzhiyun 		NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
204*4882a593Smuzhiyun 		NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	csbcpb->cpb.sha256.message_bit_length = (u64) (sctx->count * 8);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	len = sctx->count & (SHA256_BLOCK_SIZE - 1);
210*4882a593Smuzhiyun 	in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) sctx->buf,
211*4882a593Smuzhiyun 				 &len, max_sg_len);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (len != (sctx->count & (SHA256_BLOCK_SIZE - 1))) {
214*4882a593Smuzhiyun 		rc = -EINVAL;
215*4882a593Smuzhiyun 		goto out;
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	len = SHA256_DIGEST_SIZE;
219*4882a593Smuzhiyun 	out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, max_sg_len);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	if (len != SHA256_DIGEST_SIZE) {
222*4882a593Smuzhiyun 		rc = -EINVAL;
223*4882a593Smuzhiyun 		goto out;
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
227*4882a593Smuzhiyun 	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
228*4882a593Smuzhiyun 	if (!nx_ctx->op.outlen) {
229*4882a593Smuzhiyun 		rc = -EINVAL;
230*4882a593Smuzhiyun 		goto out;
231*4882a593Smuzhiyun 	}
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
234*4882a593Smuzhiyun 	if (rc)
235*4882a593Smuzhiyun 		goto out;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	atomic_inc(&(nx_ctx->stats->sha256_ops));
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	atomic64_add(sctx->count, &(nx_ctx->stats->sha256_bytes));
240*4882a593Smuzhiyun 	memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
241*4882a593Smuzhiyun out:
242*4882a593Smuzhiyun 	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
243*4882a593Smuzhiyun 	return rc;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
nx_sha256_export(struct shash_desc * desc,void * out)246*4882a593Smuzhiyun static int nx_sha256_export(struct shash_desc *desc, void *out)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	struct sha256_state *sctx = shash_desc_ctx(desc);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	memcpy(out, sctx, sizeof(*sctx));
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	return 0;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun 
nx_sha256_import(struct shash_desc * desc,const void * in)255*4882a593Smuzhiyun static int nx_sha256_import(struct shash_desc *desc, const void *in)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun 	struct sha256_state *sctx = shash_desc_ctx(desc);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	memcpy(sctx, in, sizeof(*sctx));
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	return 0;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun struct shash_alg nx_shash_sha256_alg = {
265*4882a593Smuzhiyun 	.digestsize = SHA256_DIGEST_SIZE,
266*4882a593Smuzhiyun 	.init       = nx_sha256_init,
267*4882a593Smuzhiyun 	.update     = nx_sha256_update,
268*4882a593Smuzhiyun 	.final      = nx_sha256_final,
269*4882a593Smuzhiyun 	.export     = nx_sha256_export,
270*4882a593Smuzhiyun 	.import     = nx_sha256_import,
271*4882a593Smuzhiyun 	.descsize   = sizeof(struct sha256_state),
272*4882a593Smuzhiyun 	.statesize  = sizeof(struct sha256_state),
273*4882a593Smuzhiyun 	.base       = {
274*4882a593Smuzhiyun 		.cra_name        = "sha256",
275*4882a593Smuzhiyun 		.cra_driver_name = "sha256-nx",
276*4882a593Smuzhiyun 		.cra_priority    = 300,
277*4882a593Smuzhiyun 		.cra_blocksize   = SHA256_BLOCK_SIZE,
278*4882a593Smuzhiyun 		.cra_module      = THIS_MODULE,
279*4882a593Smuzhiyun 		.cra_ctxsize     = sizeof(struct nx_crypto_ctx),
280*4882a593Smuzhiyun 		.cra_init        = nx_crypto_ctx_sha256_init,
281*4882a593Smuzhiyun 		.cra_exit        = nx_crypto_ctx_exit,
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun };
284