xref: /OK3568_Linux_fs/kernel/drivers/crypto/nx/nx-sha512.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /**
3*4882a593Smuzhiyun  * SHA-512 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 
15*4882a593Smuzhiyun #include "nx_csbcpb.h"
16*4882a593Smuzhiyun #include "nx.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun 
nx_crypto_ctx_sha512_init(struct crypto_tfm * tfm)19*4882a593Smuzhiyun static int nx_crypto_ctx_sha512_init(struct crypto_tfm *tfm)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
22*4882a593Smuzhiyun 	int err;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	err = nx_crypto_ctx_sha_init(tfm);
25*4882a593Smuzhiyun 	if (err)
26*4882a593Smuzhiyun 		return err;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	nx_ctx_init(nx_ctx, HCOP_FC_SHA);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA512];
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA512);
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	return 0;
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun 
nx_sha512_init(struct shash_desc * desc)37*4882a593Smuzhiyun static int nx_sha512_init(struct shash_desc *desc)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	struct sha512_state *sctx = shash_desc_ctx(desc);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	memset(sctx, 0, sizeof *sctx);
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	sctx->state[0] = __cpu_to_be64(SHA512_H0);
44*4882a593Smuzhiyun 	sctx->state[1] = __cpu_to_be64(SHA512_H1);
45*4882a593Smuzhiyun 	sctx->state[2] = __cpu_to_be64(SHA512_H2);
46*4882a593Smuzhiyun 	sctx->state[3] = __cpu_to_be64(SHA512_H3);
47*4882a593Smuzhiyun 	sctx->state[4] = __cpu_to_be64(SHA512_H4);
48*4882a593Smuzhiyun 	sctx->state[5] = __cpu_to_be64(SHA512_H5);
49*4882a593Smuzhiyun 	sctx->state[6] = __cpu_to_be64(SHA512_H6);
50*4882a593Smuzhiyun 	sctx->state[7] = __cpu_to_be64(SHA512_H7);
51*4882a593Smuzhiyun 	sctx->count[0] = 0;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	return 0;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
nx_sha512_update(struct shash_desc * desc,const u8 * data,unsigned int len)56*4882a593Smuzhiyun static int nx_sha512_update(struct shash_desc *desc, const u8 *data,
57*4882a593Smuzhiyun 			    unsigned int len)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	struct sha512_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, leftover = 0, 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[0] % SHA512_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: < SHA512_BLOCK_SIZE: copy into state, return 0
74*4882a593Smuzhiyun 	 *  2: >= SHA512_BLOCK_SIZE: process X blocks, copy in leftover
75*4882a593Smuzhiyun 	 */
76*4882a593Smuzhiyun 	total = (sctx->count[0] % SHA512_BLOCK_SIZE) + len;
77*4882a593Smuzhiyun 	if (total < SHA512_BLOCK_SIZE) {
78*4882a593Smuzhiyun 		memcpy(sctx->buf + buf_len, data, len);
79*4882a593Smuzhiyun 		sctx->count[0] += len;
80*4882a593Smuzhiyun 		goto out;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	memcpy(csbcpb->cpb.sha512.message_digest, sctx->state, SHA512_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 = SHA512_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 != SHA512_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, max_sg_len);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 			if (data_len != buf_len) {
113*4882a593Smuzhiyun 				rc = -EINVAL;
114*4882a593Smuzhiyun 				goto out;
115*4882a593Smuzhiyun 			}
116*4882a593Smuzhiyun 			used_sgs = in_sg - nx_ctx->in_sg;
117*4882a593Smuzhiyun 		}
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 		/* to_process: SHA512_BLOCK_SIZE aligned chunk to be
120*4882a593Smuzhiyun 		 * processed in this iteration. This value is restricted
121*4882a593Smuzhiyun 		 * by sg list limits and number of sgs we already used
122*4882a593Smuzhiyun 		 * for leftover data. (see above)
123*4882a593Smuzhiyun 		 * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len,
124*4882a593Smuzhiyun 		 * but because data may not be aligned, we need to account
125*4882a593Smuzhiyun 		 * for that too. */
126*4882a593Smuzhiyun 		to_process = min_t(u64, total,
127*4882a593Smuzhiyun 			(max_sg_len - 1 - used_sgs) * NX_PAGE_SIZE);
128*4882a593Smuzhiyun 		to_process = to_process & ~(SHA512_BLOCK_SIZE - 1);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 		data_len = to_process - buf_len;
131*4882a593Smuzhiyun 		in_sg = nx_build_sg_list(in_sg, (u8 *) data,
132*4882a593Smuzhiyun 					 &data_len, max_sg_len);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 		nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 		if (data_len != (to_process - buf_len)) {
137*4882a593Smuzhiyun 			rc = -EINVAL;
138*4882a593Smuzhiyun 			goto out;
139*4882a593Smuzhiyun 		}
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 		to_process = data_len + buf_len;
142*4882a593Smuzhiyun 		leftover = total - to_process;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 		/*
145*4882a593Smuzhiyun 		 * we've hit the nx chip previously and we're updating
146*4882a593Smuzhiyun 		 * again, so copy over the partial digest.
147*4882a593Smuzhiyun 		 */
148*4882a593Smuzhiyun 		memcpy(csbcpb->cpb.sha512.input_partial_digest,
149*4882a593Smuzhiyun 			       csbcpb->cpb.sha512.message_digest,
150*4882a593Smuzhiyun 			       SHA512_DIGEST_SIZE);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 		if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
153*4882a593Smuzhiyun 			rc = -EINVAL;
154*4882a593Smuzhiyun 			goto out;
155*4882a593Smuzhiyun 		}
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
158*4882a593Smuzhiyun 		if (rc)
159*4882a593Smuzhiyun 			goto out;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 		atomic_inc(&(nx_ctx->stats->sha512_ops));
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 		total -= to_process;
164*4882a593Smuzhiyun 		data += to_process - buf_len;
165*4882a593Smuzhiyun 		buf_len = 0;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	} while (leftover >= SHA512_BLOCK_SIZE);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	/* copy the leftover back into the state struct */
170*4882a593Smuzhiyun 	if (leftover)
171*4882a593Smuzhiyun 		memcpy(sctx->buf, data, leftover);
172*4882a593Smuzhiyun 	sctx->count[0] += len;
173*4882a593Smuzhiyun 	memcpy(sctx->state, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
174*4882a593Smuzhiyun out:
175*4882a593Smuzhiyun 	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
176*4882a593Smuzhiyun 	return rc;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
nx_sha512_final(struct shash_desc * desc,u8 * out)179*4882a593Smuzhiyun static int nx_sha512_final(struct shash_desc *desc, u8 *out)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	struct sha512_state *sctx = shash_desc_ctx(desc);
182*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
183*4882a593Smuzhiyun 	struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
184*4882a593Smuzhiyun 	struct nx_sg *in_sg, *out_sg;
185*4882a593Smuzhiyun 	u32 max_sg_len;
186*4882a593Smuzhiyun 	u64 count0;
187*4882a593Smuzhiyun 	unsigned long irq_flags;
188*4882a593Smuzhiyun 	int rc = 0;
189*4882a593Smuzhiyun 	int len;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	max_sg_len = min_t(u64, nx_ctx->ap->sglen,
194*4882a593Smuzhiyun 			nx_driver.of.max_sg_len/sizeof(struct nx_sg));
195*4882a593Smuzhiyun 	max_sg_len = min_t(u64, max_sg_len,
196*4882a593Smuzhiyun 			nx_ctx->ap->databytelen/NX_PAGE_SIZE);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	/* final is represented by continuing the operation and indicating that
199*4882a593Smuzhiyun 	 * this is not an intermediate operation */
200*4882a593Smuzhiyun 	if (sctx->count[0] >= SHA512_BLOCK_SIZE) {
201*4882a593Smuzhiyun 		/* we've hit the nx chip previously, now we're finalizing,
202*4882a593Smuzhiyun 		 * so copy over the partial digest */
203*4882a593Smuzhiyun 		memcpy(csbcpb->cpb.sha512.input_partial_digest, sctx->state,
204*4882a593Smuzhiyun 							SHA512_DIGEST_SIZE);
205*4882a593Smuzhiyun 		NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
206*4882a593Smuzhiyun 		NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
207*4882a593Smuzhiyun 	} else {
208*4882a593Smuzhiyun 		NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
209*4882a593Smuzhiyun 		NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	count0 = sctx->count[0] * 8;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	csbcpb->cpb.sha512.message_bit_length_lo = count0;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	len = sctx->count[0] & (SHA512_BLOCK_SIZE - 1);
219*4882a593Smuzhiyun 	in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buf, &len,
220*4882a593Smuzhiyun 				 max_sg_len);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	if (len != (sctx->count[0] & (SHA512_BLOCK_SIZE - 1))) {
223*4882a593Smuzhiyun 		rc = -EINVAL;
224*4882a593Smuzhiyun 		goto out;
225*4882a593Smuzhiyun 	}
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	len = SHA512_DIGEST_SIZE;
228*4882a593Smuzhiyun 	out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len,
229*4882a593Smuzhiyun 				 max_sg_len);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
232*4882a593Smuzhiyun 	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	if (!nx_ctx->op.outlen) {
235*4882a593Smuzhiyun 		rc = -EINVAL;
236*4882a593Smuzhiyun 		goto out;
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
240*4882a593Smuzhiyun 	if (rc)
241*4882a593Smuzhiyun 		goto out;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	atomic_inc(&(nx_ctx->stats->sha512_ops));
244*4882a593Smuzhiyun 	atomic64_add(sctx->count[0], &(nx_ctx->stats->sha512_bytes));
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
247*4882a593Smuzhiyun out:
248*4882a593Smuzhiyun 	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
249*4882a593Smuzhiyun 	return rc;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun 
nx_sha512_export(struct shash_desc * desc,void * out)252*4882a593Smuzhiyun static int nx_sha512_export(struct shash_desc *desc, void *out)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	struct sha512_state *sctx = shash_desc_ctx(desc);
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	memcpy(out, sctx, sizeof(*sctx));
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	return 0;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
nx_sha512_import(struct shash_desc * desc,const void * in)261*4882a593Smuzhiyun static int nx_sha512_import(struct shash_desc *desc, const void *in)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	struct sha512_state *sctx = shash_desc_ctx(desc);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	memcpy(sctx, in, sizeof(*sctx));
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	return 0;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun struct shash_alg nx_shash_sha512_alg = {
271*4882a593Smuzhiyun 	.digestsize = SHA512_DIGEST_SIZE,
272*4882a593Smuzhiyun 	.init       = nx_sha512_init,
273*4882a593Smuzhiyun 	.update     = nx_sha512_update,
274*4882a593Smuzhiyun 	.final      = nx_sha512_final,
275*4882a593Smuzhiyun 	.export     = nx_sha512_export,
276*4882a593Smuzhiyun 	.import     = nx_sha512_import,
277*4882a593Smuzhiyun 	.descsize   = sizeof(struct sha512_state),
278*4882a593Smuzhiyun 	.statesize  = sizeof(struct sha512_state),
279*4882a593Smuzhiyun 	.base       = {
280*4882a593Smuzhiyun 		.cra_name        = "sha512",
281*4882a593Smuzhiyun 		.cra_driver_name = "sha512-nx",
282*4882a593Smuzhiyun 		.cra_priority    = 300,
283*4882a593Smuzhiyun 		.cra_blocksize   = SHA512_BLOCK_SIZE,
284*4882a593Smuzhiyun 		.cra_module      = THIS_MODULE,
285*4882a593Smuzhiyun 		.cra_ctxsize     = sizeof(struct nx_crypto_ctx),
286*4882a593Smuzhiyun 		.cra_init        = nx_crypto_ctx_sha512_init,
287*4882a593Smuzhiyun 		.cra_exit        = nx_crypto_ctx_exit,
288*4882a593Smuzhiyun 	}
289*4882a593Smuzhiyun };
290