xref: /OK3568_Linux_fs/kernel/drivers/crypto/nx/nx-aes-ccm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /**
3*4882a593Smuzhiyun  * AES CCM routines supporting the Power 7+ Nest Accelerators driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 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/aead.h>
11*4882a593Smuzhiyun #include <crypto/aes.h>
12*4882a593Smuzhiyun #include <crypto/algapi.h>
13*4882a593Smuzhiyun #include <crypto/scatterwalk.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/types.h>
16*4882a593Smuzhiyun #include <linux/crypto.h>
17*4882a593Smuzhiyun #include <asm/vio.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include "nx_csbcpb.h"
20*4882a593Smuzhiyun #include "nx.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun 
ccm_aes_nx_set_key(struct crypto_aead * tfm,const u8 * in_key,unsigned int key_len)23*4882a593Smuzhiyun static int ccm_aes_nx_set_key(struct crypto_aead *tfm,
24*4882a593Smuzhiyun 			      const u8           *in_key,
25*4882a593Smuzhiyun 			      unsigned int        key_len)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&tfm->base);
28*4882a593Smuzhiyun 	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
29*4882a593Smuzhiyun 	struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	nx_ctx_init(nx_ctx, HCOP_FC_AES);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	switch (key_len) {
34*4882a593Smuzhiyun 	case AES_KEYSIZE_128:
35*4882a593Smuzhiyun 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
36*4882a593Smuzhiyun 		NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_128);
37*4882a593Smuzhiyun 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
38*4882a593Smuzhiyun 		break;
39*4882a593Smuzhiyun 	default:
40*4882a593Smuzhiyun 		return -EINVAL;
41*4882a593Smuzhiyun 	}
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	csbcpb->cpb.hdr.mode = NX_MODE_AES_CCM;
44*4882a593Smuzhiyun 	memcpy(csbcpb->cpb.aes_ccm.key, in_key, key_len);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	csbcpb_aead->cpb.hdr.mode = NX_MODE_AES_CCA;
47*4882a593Smuzhiyun 	memcpy(csbcpb_aead->cpb.aes_cca.key, in_key, key_len);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	return 0;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun 
ccm4309_aes_nx_set_key(struct crypto_aead * tfm,const u8 * in_key,unsigned int key_len)53*4882a593Smuzhiyun static int ccm4309_aes_nx_set_key(struct crypto_aead *tfm,
54*4882a593Smuzhiyun 				  const u8           *in_key,
55*4882a593Smuzhiyun 				  unsigned int        key_len)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&tfm->base);
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	if (key_len < 3)
60*4882a593Smuzhiyun 		return -EINVAL;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	key_len -= 3;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	memcpy(nx_ctx->priv.ccm.nonce, in_key + key_len, 3);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	return ccm_aes_nx_set_key(tfm, in_key, key_len);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
ccm_aes_nx_setauthsize(struct crypto_aead * tfm,unsigned int authsize)69*4882a593Smuzhiyun static int ccm_aes_nx_setauthsize(struct crypto_aead *tfm,
70*4882a593Smuzhiyun 				  unsigned int authsize)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	switch (authsize) {
73*4882a593Smuzhiyun 	case 4:
74*4882a593Smuzhiyun 	case 6:
75*4882a593Smuzhiyun 	case 8:
76*4882a593Smuzhiyun 	case 10:
77*4882a593Smuzhiyun 	case 12:
78*4882a593Smuzhiyun 	case 14:
79*4882a593Smuzhiyun 	case 16:
80*4882a593Smuzhiyun 		break;
81*4882a593Smuzhiyun 	default:
82*4882a593Smuzhiyun 		return -EINVAL;
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	return 0;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
ccm4309_aes_nx_setauthsize(struct crypto_aead * tfm,unsigned int authsize)88*4882a593Smuzhiyun static int ccm4309_aes_nx_setauthsize(struct crypto_aead *tfm,
89*4882a593Smuzhiyun 				      unsigned int authsize)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	switch (authsize) {
92*4882a593Smuzhiyun 	case 8:
93*4882a593Smuzhiyun 	case 12:
94*4882a593Smuzhiyun 	case 16:
95*4882a593Smuzhiyun 		break;
96*4882a593Smuzhiyun 	default:
97*4882a593Smuzhiyun 		return -EINVAL;
98*4882a593Smuzhiyun 	}
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	return 0;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /* taken from crypto/ccm.c */
set_msg_len(u8 * block,unsigned int msglen,int csize)104*4882a593Smuzhiyun static int set_msg_len(u8 *block, unsigned int msglen, int csize)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	__be32 data;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	memset(block, 0, csize);
109*4882a593Smuzhiyun 	block += csize;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	if (csize >= 4)
112*4882a593Smuzhiyun 		csize = 4;
113*4882a593Smuzhiyun 	else if (msglen > (unsigned int)(1 << (8 * csize)))
114*4882a593Smuzhiyun 		return -EOVERFLOW;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	data = cpu_to_be32(msglen);
117*4882a593Smuzhiyun 	memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	return 0;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun /* taken from crypto/ccm.c */
crypto_ccm_check_iv(const u8 * iv)123*4882a593Smuzhiyun static inline int crypto_ccm_check_iv(const u8 *iv)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	/* 2 <= L <= 8, so 1 <= L' <= 7. */
126*4882a593Smuzhiyun 	if (1 > iv[0] || iv[0] > 7)
127*4882a593Smuzhiyun 		return -EINVAL;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	return 0;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /* based on code from crypto/ccm.c */
generate_b0(u8 * iv,unsigned int assoclen,unsigned int authsize,unsigned int cryptlen,u8 * b0)133*4882a593Smuzhiyun static int generate_b0(u8 *iv, unsigned int assoclen, unsigned int authsize,
134*4882a593Smuzhiyun 		       unsigned int cryptlen, u8 *b0)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	unsigned int l, lp, m = authsize;
137*4882a593Smuzhiyun 	int rc;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	memcpy(b0, iv, 16);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	lp = b0[0];
142*4882a593Smuzhiyun 	l = lp + 1;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	/* set m, bits 3-5 */
145*4882a593Smuzhiyun 	*b0 |= (8 * ((m - 2) / 2));
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/* set adata, bit 6, if associated data is used */
148*4882a593Smuzhiyun 	if (assoclen)
149*4882a593Smuzhiyun 		*b0 |= 64;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	rc = set_msg_len(b0 + 16 - l, cryptlen, l);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	return rc;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
generate_pat(u8 * iv,struct aead_request * req,struct nx_crypto_ctx * nx_ctx,unsigned int authsize,unsigned int nbytes,unsigned int assoclen,u8 * out)156*4882a593Smuzhiyun static int generate_pat(u8                   *iv,
157*4882a593Smuzhiyun 			struct aead_request  *req,
158*4882a593Smuzhiyun 			struct nx_crypto_ctx *nx_ctx,
159*4882a593Smuzhiyun 			unsigned int          authsize,
160*4882a593Smuzhiyun 			unsigned int          nbytes,
161*4882a593Smuzhiyun 			unsigned int	      assoclen,
162*4882a593Smuzhiyun 			u8                   *out)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	struct nx_sg *nx_insg = nx_ctx->in_sg;
165*4882a593Smuzhiyun 	struct nx_sg *nx_outsg = nx_ctx->out_sg;
166*4882a593Smuzhiyun 	unsigned int iauth_len = 0;
167*4882a593Smuzhiyun 	u8 tmp[16], *b1 = NULL, *b0 = NULL, *result = NULL;
168*4882a593Smuzhiyun 	int rc;
169*4882a593Smuzhiyun 	unsigned int max_sg_len;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	/* zero the ctr value */
172*4882a593Smuzhiyun 	memset(iv + 15 - iv[0], 0, iv[0] + 1);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	/* page 78 of nx_wb.pdf has,
175*4882a593Smuzhiyun 	 * Note: RFC3610 allows the AAD data to be up to 2^64 -1 bytes
176*4882a593Smuzhiyun 	 * in length. If a full message is used, the AES CCA implementation
177*4882a593Smuzhiyun 	 * restricts the maximum AAD length to 2^32 -1 bytes.
178*4882a593Smuzhiyun 	 * If partial messages are used, the implementation supports
179*4882a593Smuzhiyun 	 * 2^64 -1 bytes maximum AAD length.
180*4882a593Smuzhiyun 	 *
181*4882a593Smuzhiyun 	 * However, in the cryptoapi's aead_request structure,
182*4882a593Smuzhiyun 	 * assoclen is an unsigned int, thus it cannot hold a length
183*4882a593Smuzhiyun 	 * value greater than 2^32 - 1.
184*4882a593Smuzhiyun 	 * Thus the AAD is further constrained by this and is never
185*4882a593Smuzhiyun 	 * greater than 2^32.
186*4882a593Smuzhiyun 	 */
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	if (!assoclen) {
189*4882a593Smuzhiyun 		b0 = nx_ctx->csbcpb->cpb.aes_ccm.in_pat_or_b0;
190*4882a593Smuzhiyun 	} else if (assoclen <= 14) {
191*4882a593Smuzhiyun 		/* if associated data is 14 bytes or less, we do 1 GCM
192*4882a593Smuzhiyun 		 * operation on 2 AES blocks, B0 (stored in the csbcpb) and B1,
193*4882a593Smuzhiyun 		 * which is fed in through the source buffers here */
194*4882a593Smuzhiyun 		b0 = nx_ctx->csbcpb->cpb.aes_ccm.in_pat_or_b0;
195*4882a593Smuzhiyun 		b1 = nx_ctx->priv.ccm.iauth_tag;
196*4882a593Smuzhiyun 		iauth_len = assoclen;
197*4882a593Smuzhiyun 	} else if (assoclen <= 65280) {
198*4882a593Smuzhiyun 		/* if associated data is less than (2^16 - 2^8), we construct
199*4882a593Smuzhiyun 		 * B1 differently and feed in the associated data to a CCA
200*4882a593Smuzhiyun 		 * operation */
201*4882a593Smuzhiyun 		b0 = nx_ctx->csbcpb_aead->cpb.aes_cca.b0;
202*4882a593Smuzhiyun 		b1 = nx_ctx->csbcpb_aead->cpb.aes_cca.b1;
203*4882a593Smuzhiyun 		iauth_len = 14;
204*4882a593Smuzhiyun 	} else {
205*4882a593Smuzhiyun 		b0 = nx_ctx->csbcpb_aead->cpb.aes_cca.b0;
206*4882a593Smuzhiyun 		b1 = nx_ctx->csbcpb_aead->cpb.aes_cca.b1;
207*4882a593Smuzhiyun 		iauth_len = 10;
208*4882a593Smuzhiyun 	}
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/* generate B0 */
211*4882a593Smuzhiyun 	rc = generate_b0(iv, assoclen, authsize, nbytes, b0);
212*4882a593Smuzhiyun 	if (rc)
213*4882a593Smuzhiyun 		return rc;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	/* generate B1:
216*4882a593Smuzhiyun 	 * add control info for associated data
217*4882a593Smuzhiyun 	 * RFC 3610 and NIST Special Publication 800-38C
218*4882a593Smuzhiyun 	 */
219*4882a593Smuzhiyun 	if (b1) {
220*4882a593Smuzhiyun 		memset(b1, 0, 16);
221*4882a593Smuzhiyun 		if (assoclen <= 65280) {
222*4882a593Smuzhiyun 			*(u16 *)b1 = assoclen;
223*4882a593Smuzhiyun 			scatterwalk_map_and_copy(b1 + 2, req->src, 0,
224*4882a593Smuzhiyun 					 iauth_len, SCATTERWALK_FROM_SG);
225*4882a593Smuzhiyun 		} else {
226*4882a593Smuzhiyun 			*(u16 *)b1 = (u16)(0xfffe);
227*4882a593Smuzhiyun 			*(u32 *)&b1[2] = assoclen;
228*4882a593Smuzhiyun 			scatterwalk_map_and_copy(b1 + 6, req->src, 0,
229*4882a593Smuzhiyun 					 iauth_len, SCATTERWALK_FROM_SG);
230*4882a593Smuzhiyun 		}
231*4882a593Smuzhiyun 	}
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	/* now copy any remaining AAD to scatterlist and call nx... */
234*4882a593Smuzhiyun 	if (!assoclen) {
235*4882a593Smuzhiyun 		return rc;
236*4882a593Smuzhiyun 	} else if (assoclen <= 14) {
237*4882a593Smuzhiyun 		unsigned int len = 16;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 		nx_insg = nx_build_sg_list(nx_insg, b1, &len, nx_ctx->ap->sglen);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 		if (len != 16)
242*4882a593Smuzhiyun 			return -EINVAL;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 		nx_outsg = nx_build_sg_list(nx_outsg, tmp, &len,
245*4882a593Smuzhiyun 					    nx_ctx->ap->sglen);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 		if (len != 16)
248*4882a593Smuzhiyun 			return -EINVAL;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 		/* inlen should be negative, indicating to phyp that its a
251*4882a593Smuzhiyun 		 * pointer to an sg list */
252*4882a593Smuzhiyun 		nx_ctx->op.inlen = (nx_ctx->in_sg - nx_insg) *
253*4882a593Smuzhiyun 					sizeof(struct nx_sg);
254*4882a593Smuzhiyun 		nx_ctx->op.outlen = (nx_ctx->out_sg - nx_outsg) *
255*4882a593Smuzhiyun 					sizeof(struct nx_sg);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 		NX_CPB_FDM(nx_ctx->csbcpb) |= NX_FDM_ENDE_ENCRYPT;
258*4882a593Smuzhiyun 		NX_CPB_FDM(nx_ctx->csbcpb) |= NX_FDM_INTERMEDIATE;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 		result = nx_ctx->csbcpb->cpb.aes_ccm.out_pat_or_mac;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
263*4882a593Smuzhiyun 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
264*4882a593Smuzhiyun 		if (rc)
265*4882a593Smuzhiyun 			return rc;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 		atomic_inc(&(nx_ctx->stats->aes_ops));
268*4882a593Smuzhiyun 		atomic64_add(assoclen, &nx_ctx->stats->aes_bytes);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	} else {
271*4882a593Smuzhiyun 		unsigned int processed = 0, to_process;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 		processed += iauth_len;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 		/* page_limit: number of sg entries that fit on one page */
276*4882a593Smuzhiyun 		max_sg_len = min_t(u64, nx_ctx->ap->sglen,
277*4882a593Smuzhiyun 				nx_driver.of.max_sg_len/sizeof(struct nx_sg));
278*4882a593Smuzhiyun 		max_sg_len = min_t(u64, max_sg_len,
279*4882a593Smuzhiyun 				nx_ctx->ap->databytelen/NX_PAGE_SIZE);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 		do {
282*4882a593Smuzhiyun 			to_process = min_t(u32, assoclen - processed,
283*4882a593Smuzhiyun 					   nx_ctx->ap->databytelen);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 			nx_insg = nx_walk_and_build(nx_ctx->in_sg,
286*4882a593Smuzhiyun 						    nx_ctx->ap->sglen,
287*4882a593Smuzhiyun 						    req->src, processed,
288*4882a593Smuzhiyun 						    &to_process);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 			if ((to_process + processed) < assoclen) {
291*4882a593Smuzhiyun 				NX_CPB_FDM(nx_ctx->csbcpb_aead) |=
292*4882a593Smuzhiyun 					NX_FDM_INTERMEDIATE;
293*4882a593Smuzhiyun 			} else {
294*4882a593Smuzhiyun 				NX_CPB_FDM(nx_ctx->csbcpb_aead) &=
295*4882a593Smuzhiyun 					~NX_FDM_INTERMEDIATE;
296*4882a593Smuzhiyun 			}
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 			nx_ctx->op_aead.inlen = (nx_ctx->in_sg - nx_insg) *
300*4882a593Smuzhiyun 						sizeof(struct nx_sg);
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 			result = nx_ctx->csbcpb_aead->cpb.aes_cca.out_pat_or_b0;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 			rc = nx_hcall_sync(nx_ctx, &nx_ctx->op_aead,
305*4882a593Smuzhiyun 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
306*4882a593Smuzhiyun 			if (rc)
307*4882a593Smuzhiyun 				return rc;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 			memcpy(nx_ctx->csbcpb_aead->cpb.aes_cca.b0,
310*4882a593Smuzhiyun 				nx_ctx->csbcpb_aead->cpb.aes_cca.out_pat_or_b0,
311*4882a593Smuzhiyun 				AES_BLOCK_SIZE);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 			NX_CPB_FDM(nx_ctx->csbcpb_aead) |= NX_FDM_CONTINUATION;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 			atomic_inc(&(nx_ctx->stats->aes_ops));
316*4882a593Smuzhiyun 			atomic64_add(assoclen, &nx_ctx->stats->aes_bytes);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 			processed += to_process;
319*4882a593Smuzhiyun 		} while (processed < assoclen);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 		result = nx_ctx->csbcpb_aead->cpb.aes_cca.out_pat_or_b0;
322*4882a593Smuzhiyun 	}
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	memcpy(out, result, AES_BLOCK_SIZE);
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	return rc;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
ccm_nx_decrypt(struct aead_request * req,u8 * iv,unsigned int assoclen)329*4882a593Smuzhiyun static int ccm_nx_decrypt(struct aead_request   *req,
330*4882a593Smuzhiyun 			  u8                    *iv,
331*4882a593Smuzhiyun 			  unsigned int assoclen)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
334*4882a593Smuzhiyun 	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
335*4882a593Smuzhiyun 	unsigned int nbytes = req->cryptlen;
336*4882a593Smuzhiyun 	unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
337*4882a593Smuzhiyun 	struct nx_ccm_priv *priv = &nx_ctx->priv.ccm;
338*4882a593Smuzhiyun 	unsigned long irq_flags;
339*4882a593Smuzhiyun 	unsigned int processed = 0, to_process;
340*4882a593Smuzhiyun 	int rc = -1;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	nbytes -= authsize;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	/* copy out the auth tag to compare with later */
347*4882a593Smuzhiyun 	scatterwalk_map_and_copy(priv->oauth_tag,
348*4882a593Smuzhiyun 				 req->src, nbytes + req->assoclen, authsize,
349*4882a593Smuzhiyun 				 SCATTERWALK_FROM_SG);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	rc = generate_pat(iv, req, nx_ctx, authsize, nbytes, assoclen,
352*4882a593Smuzhiyun 			  csbcpb->cpb.aes_ccm.in_pat_or_b0);
353*4882a593Smuzhiyun 	if (rc)
354*4882a593Smuzhiyun 		goto out;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	do {
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 		/* to_process: the AES_BLOCK_SIZE data chunk to process in this
359*4882a593Smuzhiyun 		 * update. This value is bound by sg list limits.
360*4882a593Smuzhiyun 		 */
361*4882a593Smuzhiyun 		to_process = nbytes - processed;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 		if ((to_process + processed) < nbytes)
364*4882a593Smuzhiyun 			NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
365*4882a593Smuzhiyun 		else
366*4882a593Smuzhiyun 			NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 		NX_CPB_FDM(nx_ctx->csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 		rc = nx_build_sg_lists(nx_ctx, iv, req->dst, req->src,
371*4882a593Smuzhiyun 				       &to_process, processed + req->assoclen,
372*4882a593Smuzhiyun 				       csbcpb->cpb.aes_ccm.iv_or_ctr);
373*4882a593Smuzhiyun 		if (rc)
374*4882a593Smuzhiyun 			goto out;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
377*4882a593Smuzhiyun 			   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
378*4882a593Smuzhiyun 		if (rc)
379*4882a593Smuzhiyun 			goto out;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 		/* for partial completion, copy following for next
382*4882a593Smuzhiyun 		 * entry into loop...
383*4882a593Smuzhiyun 		 */
384*4882a593Smuzhiyun 		memcpy(iv, csbcpb->cpb.aes_ccm.out_ctr, AES_BLOCK_SIZE);
385*4882a593Smuzhiyun 		memcpy(csbcpb->cpb.aes_ccm.in_pat_or_b0,
386*4882a593Smuzhiyun 			csbcpb->cpb.aes_ccm.out_pat_or_mac, AES_BLOCK_SIZE);
387*4882a593Smuzhiyun 		memcpy(csbcpb->cpb.aes_ccm.in_s0,
388*4882a593Smuzhiyun 			csbcpb->cpb.aes_ccm.out_s0, AES_BLOCK_SIZE);
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 		NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 		/* update stats */
393*4882a593Smuzhiyun 		atomic_inc(&(nx_ctx->stats->aes_ops));
394*4882a593Smuzhiyun 		atomic64_add(csbcpb->csb.processed_byte_count,
395*4882a593Smuzhiyun 			     &(nx_ctx->stats->aes_bytes));
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 		processed += to_process;
398*4882a593Smuzhiyun 	} while (processed < nbytes);
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	rc = crypto_memneq(csbcpb->cpb.aes_ccm.out_pat_or_mac, priv->oauth_tag,
401*4882a593Smuzhiyun 		    authsize) ? -EBADMSG : 0;
402*4882a593Smuzhiyun out:
403*4882a593Smuzhiyun 	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
404*4882a593Smuzhiyun 	return rc;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
ccm_nx_encrypt(struct aead_request * req,u8 * iv,unsigned int assoclen)407*4882a593Smuzhiyun static int ccm_nx_encrypt(struct aead_request   *req,
408*4882a593Smuzhiyun 			  u8                    *iv,
409*4882a593Smuzhiyun 			  unsigned int assoclen)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
412*4882a593Smuzhiyun 	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
413*4882a593Smuzhiyun 	unsigned int nbytes = req->cryptlen;
414*4882a593Smuzhiyun 	unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
415*4882a593Smuzhiyun 	unsigned long irq_flags;
416*4882a593Smuzhiyun 	unsigned int processed = 0, to_process;
417*4882a593Smuzhiyun 	int rc = -1;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	rc = generate_pat(iv, req, nx_ctx, authsize, nbytes, assoclen,
422*4882a593Smuzhiyun 			  csbcpb->cpb.aes_ccm.in_pat_or_b0);
423*4882a593Smuzhiyun 	if (rc)
424*4882a593Smuzhiyun 		goto out;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	do {
427*4882a593Smuzhiyun 		/* to process: the AES_BLOCK_SIZE data chunk to process in this
428*4882a593Smuzhiyun 		 * update. This value is bound by sg list limits.
429*4882a593Smuzhiyun 		 */
430*4882a593Smuzhiyun 		to_process = nbytes - processed;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 		if ((to_process + processed) < nbytes)
433*4882a593Smuzhiyun 			NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
434*4882a593Smuzhiyun 		else
435*4882a593Smuzhiyun 			NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 		NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 		rc = nx_build_sg_lists(nx_ctx, iv, req->dst, req->src,
440*4882a593Smuzhiyun 				       &to_process, processed + req->assoclen,
441*4882a593Smuzhiyun 				       csbcpb->cpb.aes_ccm.iv_or_ctr);
442*4882a593Smuzhiyun 		if (rc)
443*4882a593Smuzhiyun 			goto out;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
446*4882a593Smuzhiyun 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
447*4882a593Smuzhiyun 		if (rc)
448*4882a593Smuzhiyun 			goto out;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 		/* for partial completion, copy following for next
451*4882a593Smuzhiyun 		 * entry into loop...
452*4882a593Smuzhiyun 		 */
453*4882a593Smuzhiyun 		memcpy(iv, csbcpb->cpb.aes_ccm.out_ctr, AES_BLOCK_SIZE);
454*4882a593Smuzhiyun 		memcpy(csbcpb->cpb.aes_ccm.in_pat_or_b0,
455*4882a593Smuzhiyun 			csbcpb->cpb.aes_ccm.out_pat_or_mac, AES_BLOCK_SIZE);
456*4882a593Smuzhiyun 		memcpy(csbcpb->cpb.aes_ccm.in_s0,
457*4882a593Smuzhiyun 			csbcpb->cpb.aes_ccm.out_s0, AES_BLOCK_SIZE);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 		NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 		/* update stats */
462*4882a593Smuzhiyun 		atomic_inc(&(nx_ctx->stats->aes_ops));
463*4882a593Smuzhiyun 		atomic64_add(csbcpb->csb.processed_byte_count,
464*4882a593Smuzhiyun 			     &(nx_ctx->stats->aes_bytes));
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 		processed += to_process;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	} while (processed < nbytes);
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	/* copy out the auth tag */
471*4882a593Smuzhiyun 	scatterwalk_map_and_copy(csbcpb->cpb.aes_ccm.out_pat_or_mac,
472*4882a593Smuzhiyun 				 req->dst, nbytes + req->assoclen, authsize,
473*4882a593Smuzhiyun 				 SCATTERWALK_TO_SG);
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun out:
476*4882a593Smuzhiyun 	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
477*4882a593Smuzhiyun 	return rc;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun 
ccm4309_aes_nx_encrypt(struct aead_request * req)480*4882a593Smuzhiyun static int ccm4309_aes_nx_encrypt(struct aead_request *req)
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
483*4882a593Smuzhiyun 	struct nx_gcm_rctx *rctx = aead_request_ctx(req);
484*4882a593Smuzhiyun 	u8 *iv = rctx->iv;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	iv[0] = 3;
487*4882a593Smuzhiyun 	memcpy(iv + 1, nx_ctx->priv.ccm.nonce, 3);
488*4882a593Smuzhiyun 	memcpy(iv + 4, req->iv, 8);
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	return ccm_nx_encrypt(req, iv, req->assoclen - 8);
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun 
ccm_aes_nx_encrypt(struct aead_request * req)493*4882a593Smuzhiyun static int ccm_aes_nx_encrypt(struct aead_request *req)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun 	int rc;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	rc = crypto_ccm_check_iv(req->iv);
498*4882a593Smuzhiyun 	if (rc)
499*4882a593Smuzhiyun 		return rc;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	return ccm_nx_encrypt(req, req->iv, req->assoclen);
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun 
ccm4309_aes_nx_decrypt(struct aead_request * req)504*4882a593Smuzhiyun static int ccm4309_aes_nx_decrypt(struct aead_request *req)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
507*4882a593Smuzhiyun 	struct nx_gcm_rctx *rctx = aead_request_ctx(req);
508*4882a593Smuzhiyun 	u8 *iv = rctx->iv;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	iv[0] = 3;
511*4882a593Smuzhiyun 	memcpy(iv + 1, nx_ctx->priv.ccm.nonce, 3);
512*4882a593Smuzhiyun 	memcpy(iv + 4, req->iv, 8);
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	return ccm_nx_decrypt(req, iv, req->assoclen - 8);
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun 
ccm_aes_nx_decrypt(struct aead_request * req)517*4882a593Smuzhiyun static int ccm_aes_nx_decrypt(struct aead_request *req)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun 	int rc;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	rc = crypto_ccm_check_iv(req->iv);
522*4882a593Smuzhiyun 	if (rc)
523*4882a593Smuzhiyun 		return rc;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	return ccm_nx_decrypt(req, req->iv, req->assoclen);
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun struct aead_alg nx_ccm_aes_alg = {
529*4882a593Smuzhiyun 	.base = {
530*4882a593Smuzhiyun 		.cra_name        = "ccm(aes)",
531*4882a593Smuzhiyun 		.cra_driver_name = "ccm-aes-nx",
532*4882a593Smuzhiyun 		.cra_priority    = 300,
533*4882a593Smuzhiyun 		.cra_flags       = CRYPTO_ALG_NEED_FALLBACK,
534*4882a593Smuzhiyun 		.cra_blocksize   = 1,
535*4882a593Smuzhiyun 		.cra_ctxsize     = sizeof(struct nx_crypto_ctx),
536*4882a593Smuzhiyun 		.cra_module      = THIS_MODULE,
537*4882a593Smuzhiyun 	},
538*4882a593Smuzhiyun 	.init        = nx_crypto_ctx_aes_ccm_init,
539*4882a593Smuzhiyun 	.exit        = nx_crypto_ctx_aead_exit,
540*4882a593Smuzhiyun 	.ivsize      = AES_BLOCK_SIZE,
541*4882a593Smuzhiyun 	.maxauthsize = AES_BLOCK_SIZE,
542*4882a593Smuzhiyun 	.setkey      = ccm_aes_nx_set_key,
543*4882a593Smuzhiyun 	.setauthsize = ccm_aes_nx_setauthsize,
544*4882a593Smuzhiyun 	.encrypt     = ccm_aes_nx_encrypt,
545*4882a593Smuzhiyun 	.decrypt     = ccm_aes_nx_decrypt,
546*4882a593Smuzhiyun };
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun struct aead_alg nx_ccm4309_aes_alg = {
549*4882a593Smuzhiyun 	.base = {
550*4882a593Smuzhiyun 		.cra_name        = "rfc4309(ccm(aes))",
551*4882a593Smuzhiyun 		.cra_driver_name = "rfc4309-ccm-aes-nx",
552*4882a593Smuzhiyun 		.cra_priority    = 300,
553*4882a593Smuzhiyun 		.cra_flags       = CRYPTO_ALG_NEED_FALLBACK,
554*4882a593Smuzhiyun 		.cra_blocksize   = 1,
555*4882a593Smuzhiyun 		.cra_ctxsize     = sizeof(struct nx_crypto_ctx),
556*4882a593Smuzhiyun 		.cra_module      = THIS_MODULE,
557*4882a593Smuzhiyun 	},
558*4882a593Smuzhiyun 	.init        = nx_crypto_ctx_aes_ccm_init,
559*4882a593Smuzhiyun 	.exit        = nx_crypto_ctx_aead_exit,
560*4882a593Smuzhiyun 	.ivsize      = 8,
561*4882a593Smuzhiyun 	.maxauthsize = AES_BLOCK_SIZE,
562*4882a593Smuzhiyun 	.setkey      = ccm4309_aes_nx_set_key,
563*4882a593Smuzhiyun 	.setauthsize = ccm4309_aes_nx_setauthsize,
564*4882a593Smuzhiyun 	.encrypt     = ccm4309_aes_nx_encrypt,
565*4882a593Smuzhiyun 	.decrypt     = ccm4309_aes_nx_decrypt,
566*4882a593Smuzhiyun };
567