xref: /OK3568_Linux_fs/kernel/crypto/ccm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * CCM: Counter with CBC-MAC
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <crypto/internal/aead.h>
9*4882a593Smuzhiyun #include <crypto/internal/cipher.h>
10*4882a593Smuzhiyun #include <crypto/internal/hash.h>
11*4882a593Smuzhiyun #include <crypto/internal/skcipher.h>
12*4882a593Smuzhiyun #include <crypto/scatterwalk.h>
13*4882a593Smuzhiyun #include <linux/err.h>
14*4882a593Smuzhiyun #include <linux/init.h>
15*4882a593Smuzhiyun #include <linux/kernel.h>
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun struct ccm_instance_ctx {
20*4882a593Smuzhiyun 	struct crypto_skcipher_spawn ctr;
21*4882a593Smuzhiyun 	struct crypto_ahash_spawn mac;
22*4882a593Smuzhiyun };
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun struct crypto_ccm_ctx {
25*4882a593Smuzhiyun 	struct crypto_ahash *mac;
26*4882a593Smuzhiyun 	struct crypto_skcipher *ctr;
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun struct crypto_rfc4309_ctx {
30*4882a593Smuzhiyun 	struct crypto_aead *child;
31*4882a593Smuzhiyun 	u8 nonce[3];
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun struct crypto_rfc4309_req_ctx {
35*4882a593Smuzhiyun 	struct scatterlist src[3];
36*4882a593Smuzhiyun 	struct scatterlist dst[3];
37*4882a593Smuzhiyun 	struct aead_request subreq;
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun struct crypto_ccm_req_priv_ctx {
41*4882a593Smuzhiyun 	u8 odata[16];
42*4882a593Smuzhiyun 	u8 idata[16];
43*4882a593Smuzhiyun 	u8 auth_tag[16];
44*4882a593Smuzhiyun 	u32 flags;
45*4882a593Smuzhiyun 	struct scatterlist src[3];
46*4882a593Smuzhiyun 	struct scatterlist dst[3];
47*4882a593Smuzhiyun 	union {
48*4882a593Smuzhiyun 		struct ahash_request ahreq;
49*4882a593Smuzhiyun 		struct skcipher_request skreq;
50*4882a593Smuzhiyun 	};
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun struct cbcmac_tfm_ctx {
54*4882a593Smuzhiyun 	struct crypto_cipher *child;
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun struct cbcmac_desc_ctx {
58*4882a593Smuzhiyun 	unsigned int len;
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun 
crypto_ccm_reqctx(struct aead_request * req)61*4882a593Smuzhiyun static inline struct crypto_ccm_req_priv_ctx *crypto_ccm_reqctx(
62*4882a593Smuzhiyun 	struct aead_request *req)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
set_msg_len(u8 * block,unsigned int msglen,int csize)69*4882a593Smuzhiyun static int set_msg_len(u8 *block, unsigned int msglen, int csize)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	__be32 data;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	memset(block, 0, csize);
74*4882a593Smuzhiyun 	block += csize;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	if (csize >= 4)
77*4882a593Smuzhiyun 		csize = 4;
78*4882a593Smuzhiyun 	else if (msglen > (1 << (8 * csize)))
79*4882a593Smuzhiyun 		return -EOVERFLOW;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	data = cpu_to_be32(msglen);
82*4882a593Smuzhiyun 	memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	return 0;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
crypto_ccm_setkey(struct crypto_aead * aead,const u8 * key,unsigned int keylen)87*4882a593Smuzhiyun static int crypto_ccm_setkey(struct crypto_aead *aead, const u8 *key,
88*4882a593Smuzhiyun 			     unsigned int keylen)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
91*4882a593Smuzhiyun 	struct crypto_skcipher *ctr = ctx->ctr;
92*4882a593Smuzhiyun 	struct crypto_ahash *mac = ctx->mac;
93*4882a593Smuzhiyun 	int err;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
96*4882a593Smuzhiyun 	crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
97*4882a593Smuzhiyun 				       CRYPTO_TFM_REQ_MASK);
98*4882a593Smuzhiyun 	err = crypto_skcipher_setkey(ctr, key, keylen);
99*4882a593Smuzhiyun 	if (err)
100*4882a593Smuzhiyun 		return err;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	crypto_ahash_clear_flags(mac, CRYPTO_TFM_REQ_MASK);
103*4882a593Smuzhiyun 	crypto_ahash_set_flags(mac, crypto_aead_get_flags(aead) &
104*4882a593Smuzhiyun 				    CRYPTO_TFM_REQ_MASK);
105*4882a593Smuzhiyun 	return crypto_ahash_setkey(mac, key, keylen);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
crypto_ccm_setauthsize(struct crypto_aead * tfm,unsigned int authsize)108*4882a593Smuzhiyun static int crypto_ccm_setauthsize(struct crypto_aead *tfm,
109*4882a593Smuzhiyun 				  unsigned int authsize)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	switch (authsize) {
112*4882a593Smuzhiyun 	case 4:
113*4882a593Smuzhiyun 	case 6:
114*4882a593Smuzhiyun 	case 8:
115*4882a593Smuzhiyun 	case 10:
116*4882a593Smuzhiyun 	case 12:
117*4882a593Smuzhiyun 	case 14:
118*4882a593Smuzhiyun 	case 16:
119*4882a593Smuzhiyun 		break;
120*4882a593Smuzhiyun 	default:
121*4882a593Smuzhiyun 		return -EINVAL;
122*4882a593Smuzhiyun 	}
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	return 0;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
format_input(u8 * info,struct aead_request * req,unsigned int cryptlen)127*4882a593Smuzhiyun static int format_input(u8 *info, struct aead_request *req,
128*4882a593Smuzhiyun 			unsigned int cryptlen)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
131*4882a593Smuzhiyun 	unsigned int lp = req->iv[0];
132*4882a593Smuzhiyun 	unsigned int l = lp + 1;
133*4882a593Smuzhiyun 	unsigned int m;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	m = crypto_aead_authsize(aead);
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	memcpy(info, req->iv, 16);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/* format control info per RFC 3610 and
140*4882a593Smuzhiyun 	 * NIST Special Publication 800-38C
141*4882a593Smuzhiyun 	 */
142*4882a593Smuzhiyun 	*info |= (8 * ((m - 2) / 2));
143*4882a593Smuzhiyun 	if (req->assoclen)
144*4882a593Smuzhiyun 		*info |= 64;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	return set_msg_len(info + 16 - l, cryptlen, l);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
format_adata(u8 * adata,unsigned int a)149*4882a593Smuzhiyun static int format_adata(u8 *adata, unsigned int a)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	int len = 0;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	/* add control info for associated data
154*4882a593Smuzhiyun 	 * RFC 3610 and NIST Special Publication 800-38C
155*4882a593Smuzhiyun 	 */
156*4882a593Smuzhiyun 	if (a < 65280) {
157*4882a593Smuzhiyun 		*(__be16 *)adata = cpu_to_be16(a);
158*4882a593Smuzhiyun 		len = 2;
159*4882a593Smuzhiyun 	} else  {
160*4882a593Smuzhiyun 		*(__be16 *)adata = cpu_to_be16(0xfffe);
161*4882a593Smuzhiyun 		*(__be32 *)&adata[2] = cpu_to_be32(a);
162*4882a593Smuzhiyun 		len = 6;
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	return len;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun 
crypto_ccm_auth(struct aead_request * req,struct scatterlist * plain,unsigned int cryptlen)168*4882a593Smuzhiyun static int crypto_ccm_auth(struct aead_request *req, struct scatterlist *plain,
169*4882a593Smuzhiyun 			   unsigned int cryptlen)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
172*4882a593Smuzhiyun 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
173*4882a593Smuzhiyun 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
174*4882a593Smuzhiyun 	struct ahash_request *ahreq = &pctx->ahreq;
175*4882a593Smuzhiyun 	unsigned int assoclen = req->assoclen;
176*4882a593Smuzhiyun 	struct scatterlist sg[3];
177*4882a593Smuzhiyun 	u8 *odata = pctx->odata;
178*4882a593Smuzhiyun 	u8 *idata = pctx->idata;
179*4882a593Smuzhiyun 	int ilen, err;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	/* format control data for input */
182*4882a593Smuzhiyun 	err = format_input(odata, req, cryptlen);
183*4882a593Smuzhiyun 	if (err)
184*4882a593Smuzhiyun 		goto out;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	sg_init_table(sg, 3);
187*4882a593Smuzhiyun 	sg_set_buf(&sg[0], odata, 16);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	/* format associated data and compute into mac */
190*4882a593Smuzhiyun 	if (assoclen) {
191*4882a593Smuzhiyun 		ilen = format_adata(idata, assoclen);
192*4882a593Smuzhiyun 		sg_set_buf(&sg[1], idata, ilen);
193*4882a593Smuzhiyun 		sg_chain(sg, 3, req->src);
194*4882a593Smuzhiyun 	} else {
195*4882a593Smuzhiyun 		ilen = 0;
196*4882a593Smuzhiyun 		sg_chain(sg, 2, req->src);
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	ahash_request_set_tfm(ahreq, ctx->mac);
200*4882a593Smuzhiyun 	ahash_request_set_callback(ahreq, pctx->flags, NULL, NULL);
201*4882a593Smuzhiyun 	ahash_request_set_crypt(ahreq, sg, NULL, assoclen + ilen + 16);
202*4882a593Smuzhiyun 	err = crypto_ahash_init(ahreq);
203*4882a593Smuzhiyun 	if (err)
204*4882a593Smuzhiyun 		goto out;
205*4882a593Smuzhiyun 	err = crypto_ahash_update(ahreq);
206*4882a593Smuzhiyun 	if (err)
207*4882a593Smuzhiyun 		goto out;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	/* we need to pad the MAC input to a round multiple of the block size */
210*4882a593Smuzhiyun 	ilen = 16 - (assoclen + ilen) % 16;
211*4882a593Smuzhiyun 	if (ilen < 16) {
212*4882a593Smuzhiyun 		memset(idata, 0, ilen);
213*4882a593Smuzhiyun 		sg_init_table(sg, 2);
214*4882a593Smuzhiyun 		sg_set_buf(&sg[0], idata, ilen);
215*4882a593Smuzhiyun 		if (plain)
216*4882a593Smuzhiyun 			sg_chain(sg, 2, plain);
217*4882a593Smuzhiyun 		plain = sg;
218*4882a593Smuzhiyun 		cryptlen += ilen;
219*4882a593Smuzhiyun 	}
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	ahash_request_set_crypt(ahreq, plain, pctx->odata, cryptlen);
222*4882a593Smuzhiyun 	err = crypto_ahash_finup(ahreq);
223*4882a593Smuzhiyun out:
224*4882a593Smuzhiyun 	return err;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun 
crypto_ccm_encrypt_done(struct crypto_async_request * areq,int err)227*4882a593Smuzhiyun static void crypto_ccm_encrypt_done(struct crypto_async_request *areq, int err)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	struct aead_request *req = areq->data;
230*4882a593Smuzhiyun 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
231*4882a593Smuzhiyun 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
232*4882a593Smuzhiyun 	u8 *odata = pctx->odata;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	if (!err)
235*4882a593Smuzhiyun 		scatterwalk_map_and_copy(odata, req->dst,
236*4882a593Smuzhiyun 					 req->assoclen + req->cryptlen,
237*4882a593Smuzhiyun 					 crypto_aead_authsize(aead), 1);
238*4882a593Smuzhiyun 	aead_request_complete(req, err);
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun 
crypto_ccm_check_iv(const u8 * iv)241*4882a593Smuzhiyun static inline int crypto_ccm_check_iv(const u8 *iv)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	/* 2 <= L <= 8, so 1 <= L' <= 7. */
244*4882a593Smuzhiyun 	if (1 > iv[0] || iv[0] > 7)
245*4882a593Smuzhiyun 		return -EINVAL;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	return 0;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun 
crypto_ccm_init_crypt(struct aead_request * req,u8 * tag)250*4882a593Smuzhiyun static int crypto_ccm_init_crypt(struct aead_request *req, u8 *tag)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
253*4882a593Smuzhiyun 	struct scatterlist *sg;
254*4882a593Smuzhiyun 	u8 *iv = req->iv;
255*4882a593Smuzhiyun 	int err;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	err = crypto_ccm_check_iv(iv);
258*4882a593Smuzhiyun 	if (err)
259*4882a593Smuzhiyun 		return err;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	pctx->flags = aead_request_flags(req);
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	 /* Note: rfc 3610 and NIST 800-38C require counter of
264*4882a593Smuzhiyun 	 * zero to encrypt auth tag.
265*4882a593Smuzhiyun 	 */
266*4882a593Smuzhiyun 	memset(iv + 15 - iv[0], 0, iv[0] + 1);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	sg_init_table(pctx->src, 3);
269*4882a593Smuzhiyun 	sg_set_buf(pctx->src, tag, 16);
270*4882a593Smuzhiyun 	sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
271*4882a593Smuzhiyun 	if (sg != pctx->src + 1)
272*4882a593Smuzhiyun 		sg_chain(pctx->src, 2, sg);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	if (req->src != req->dst) {
275*4882a593Smuzhiyun 		sg_init_table(pctx->dst, 3);
276*4882a593Smuzhiyun 		sg_set_buf(pctx->dst, tag, 16);
277*4882a593Smuzhiyun 		sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
278*4882a593Smuzhiyun 		if (sg != pctx->dst + 1)
279*4882a593Smuzhiyun 			sg_chain(pctx->dst, 2, sg);
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	return 0;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun 
crypto_ccm_encrypt(struct aead_request * req)285*4882a593Smuzhiyun static int crypto_ccm_encrypt(struct aead_request *req)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
288*4882a593Smuzhiyun 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
289*4882a593Smuzhiyun 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
290*4882a593Smuzhiyun 	struct skcipher_request *skreq = &pctx->skreq;
291*4882a593Smuzhiyun 	struct scatterlist *dst;
292*4882a593Smuzhiyun 	unsigned int cryptlen = req->cryptlen;
293*4882a593Smuzhiyun 	u8 *odata = pctx->odata;
294*4882a593Smuzhiyun 	u8 *iv = req->iv;
295*4882a593Smuzhiyun 	int err;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	err = crypto_ccm_init_crypt(req, odata);
298*4882a593Smuzhiyun 	if (err)
299*4882a593Smuzhiyun 		return err;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	err = crypto_ccm_auth(req, sg_next(pctx->src), cryptlen);
302*4882a593Smuzhiyun 	if (err)
303*4882a593Smuzhiyun 		return err;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	dst = pctx->src;
306*4882a593Smuzhiyun 	if (req->src != req->dst)
307*4882a593Smuzhiyun 		dst = pctx->dst;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	skcipher_request_set_tfm(skreq, ctx->ctr);
310*4882a593Smuzhiyun 	skcipher_request_set_callback(skreq, pctx->flags,
311*4882a593Smuzhiyun 				      crypto_ccm_encrypt_done, req);
312*4882a593Smuzhiyun 	skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
313*4882a593Smuzhiyun 	err = crypto_skcipher_encrypt(skreq);
314*4882a593Smuzhiyun 	if (err)
315*4882a593Smuzhiyun 		return err;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	/* copy authtag to end of dst */
318*4882a593Smuzhiyun 	scatterwalk_map_and_copy(odata, sg_next(dst), cryptlen,
319*4882a593Smuzhiyun 				 crypto_aead_authsize(aead), 1);
320*4882a593Smuzhiyun 	return err;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
crypto_ccm_decrypt_done(struct crypto_async_request * areq,int err)323*4882a593Smuzhiyun static void crypto_ccm_decrypt_done(struct crypto_async_request *areq,
324*4882a593Smuzhiyun 				   int err)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	struct aead_request *req = areq->data;
327*4882a593Smuzhiyun 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
328*4882a593Smuzhiyun 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
329*4882a593Smuzhiyun 	unsigned int authsize = crypto_aead_authsize(aead);
330*4882a593Smuzhiyun 	unsigned int cryptlen = req->cryptlen - authsize;
331*4882a593Smuzhiyun 	struct scatterlist *dst;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	pctx->flags = 0;
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	dst = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	if (!err) {
338*4882a593Smuzhiyun 		err = crypto_ccm_auth(req, dst, cryptlen);
339*4882a593Smuzhiyun 		if (!err && crypto_memneq(pctx->auth_tag, pctx->odata, authsize))
340*4882a593Smuzhiyun 			err = -EBADMSG;
341*4882a593Smuzhiyun 	}
342*4882a593Smuzhiyun 	aead_request_complete(req, err);
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun 
crypto_ccm_decrypt(struct aead_request * req)345*4882a593Smuzhiyun static int crypto_ccm_decrypt(struct aead_request *req)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
348*4882a593Smuzhiyun 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
349*4882a593Smuzhiyun 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
350*4882a593Smuzhiyun 	struct skcipher_request *skreq = &pctx->skreq;
351*4882a593Smuzhiyun 	struct scatterlist *dst;
352*4882a593Smuzhiyun 	unsigned int authsize = crypto_aead_authsize(aead);
353*4882a593Smuzhiyun 	unsigned int cryptlen = req->cryptlen;
354*4882a593Smuzhiyun 	u8 *authtag = pctx->auth_tag;
355*4882a593Smuzhiyun 	u8 *odata = pctx->odata;
356*4882a593Smuzhiyun 	u8 *iv = pctx->idata;
357*4882a593Smuzhiyun 	int err;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	cryptlen -= authsize;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	err = crypto_ccm_init_crypt(req, authtag);
362*4882a593Smuzhiyun 	if (err)
363*4882a593Smuzhiyun 		return err;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	scatterwalk_map_and_copy(authtag, sg_next(pctx->src), cryptlen,
366*4882a593Smuzhiyun 				 authsize, 0);
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	dst = pctx->src;
369*4882a593Smuzhiyun 	if (req->src != req->dst)
370*4882a593Smuzhiyun 		dst = pctx->dst;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	memcpy(iv, req->iv, 16);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	skcipher_request_set_tfm(skreq, ctx->ctr);
375*4882a593Smuzhiyun 	skcipher_request_set_callback(skreq, pctx->flags,
376*4882a593Smuzhiyun 				      crypto_ccm_decrypt_done, req);
377*4882a593Smuzhiyun 	skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
378*4882a593Smuzhiyun 	err = crypto_skcipher_decrypt(skreq);
379*4882a593Smuzhiyun 	if (err)
380*4882a593Smuzhiyun 		return err;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	err = crypto_ccm_auth(req, sg_next(dst), cryptlen);
383*4882a593Smuzhiyun 	if (err)
384*4882a593Smuzhiyun 		return err;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	/* verify */
387*4882a593Smuzhiyun 	if (crypto_memneq(authtag, odata, authsize))
388*4882a593Smuzhiyun 		return -EBADMSG;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	return err;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun 
crypto_ccm_init_tfm(struct crypto_aead * tfm)393*4882a593Smuzhiyun static int crypto_ccm_init_tfm(struct crypto_aead *tfm)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun 	struct aead_instance *inst = aead_alg_instance(tfm);
396*4882a593Smuzhiyun 	struct ccm_instance_ctx *ictx = aead_instance_ctx(inst);
397*4882a593Smuzhiyun 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
398*4882a593Smuzhiyun 	struct crypto_ahash *mac;
399*4882a593Smuzhiyun 	struct crypto_skcipher *ctr;
400*4882a593Smuzhiyun 	unsigned long align;
401*4882a593Smuzhiyun 	int err;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	mac = crypto_spawn_ahash(&ictx->mac);
404*4882a593Smuzhiyun 	if (IS_ERR(mac))
405*4882a593Smuzhiyun 		return PTR_ERR(mac);
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	ctr = crypto_spawn_skcipher(&ictx->ctr);
408*4882a593Smuzhiyun 	err = PTR_ERR(ctr);
409*4882a593Smuzhiyun 	if (IS_ERR(ctr))
410*4882a593Smuzhiyun 		goto err_free_mac;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	ctx->mac = mac;
413*4882a593Smuzhiyun 	ctx->ctr = ctr;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	align = crypto_aead_alignmask(tfm);
416*4882a593Smuzhiyun 	align &= ~(crypto_tfm_ctx_alignment() - 1);
417*4882a593Smuzhiyun 	crypto_aead_set_reqsize(
418*4882a593Smuzhiyun 		tfm,
419*4882a593Smuzhiyun 		align + sizeof(struct crypto_ccm_req_priv_ctx) +
420*4882a593Smuzhiyun 		max(crypto_ahash_reqsize(mac), crypto_skcipher_reqsize(ctr)));
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	return 0;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun err_free_mac:
425*4882a593Smuzhiyun 	crypto_free_ahash(mac);
426*4882a593Smuzhiyun 	return err;
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun 
crypto_ccm_exit_tfm(struct crypto_aead * tfm)429*4882a593Smuzhiyun static void crypto_ccm_exit_tfm(struct crypto_aead *tfm)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	crypto_free_ahash(ctx->mac);
434*4882a593Smuzhiyun 	crypto_free_skcipher(ctx->ctr);
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun 
crypto_ccm_free(struct aead_instance * inst)437*4882a593Smuzhiyun static void crypto_ccm_free(struct aead_instance *inst)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun 	struct ccm_instance_ctx *ctx = aead_instance_ctx(inst);
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	crypto_drop_ahash(&ctx->mac);
442*4882a593Smuzhiyun 	crypto_drop_skcipher(&ctx->ctr);
443*4882a593Smuzhiyun 	kfree(inst);
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun 
crypto_ccm_create_common(struct crypto_template * tmpl,struct rtattr ** tb,const char * ctr_name,const char * mac_name)446*4882a593Smuzhiyun static int crypto_ccm_create_common(struct crypto_template *tmpl,
447*4882a593Smuzhiyun 				    struct rtattr **tb,
448*4882a593Smuzhiyun 				    const char *ctr_name,
449*4882a593Smuzhiyun 				    const char *mac_name)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun 	u32 mask;
452*4882a593Smuzhiyun 	struct aead_instance *inst;
453*4882a593Smuzhiyun 	struct ccm_instance_ctx *ictx;
454*4882a593Smuzhiyun 	struct skcipher_alg *ctr;
455*4882a593Smuzhiyun 	struct hash_alg_common *mac;
456*4882a593Smuzhiyun 	int err;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
459*4882a593Smuzhiyun 	if (err)
460*4882a593Smuzhiyun 		return err;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
463*4882a593Smuzhiyun 	if (!inst)
464*4882a593Smuzhiyun 		return -ENOMEM;
465*4882a593Smuzhiyun 	ictx = aead_instance_ctx(inst);
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	err = crypto_grab_ahash(&ictx->mac, aead_crypto_instance(inst),
468*4882a593Smuzhiyun 				mac_name, 0, mask | CRYPTO_ALG_ASYNC);
469*4882a593Smuzhiyun 	if (err)
470*4882a593Smuzhiyun 		goto err_free_inst;
471*4882a593Smuzhiyun 	mac = crypto_spawn_ahash_alg(&ictx->mac);
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	err = -EINVAL;
474*4882a593Smuzhiyun 	if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 ||
475*4882a593Smuzhiyun 	    mac->digestsize != 16)
476*4882a593Smuzhiyun 		goto err_free_inst;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	err = crypto_grab_skcipher(&ictx->ctr, aead_crypto_instance(inst),
479*4882a593Smuzhiyun 				   ctr_name, 0, mask);
480*4882a593Smuzhiyun 	if (err)
481*4882a593Smuzhiyun 		goto err_free_inst;
482*4882a593Smuzhiyun 	ctr = crypto_spawn_skcipher_alg(&ictx->ctr);
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	/* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
485*4882a593Smuzhiyun 	err = -EINVAL;
486*4882a593Smuzhiyun 	if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
487*4882a593Smuzhiyun 	    crypto_skcipher_alg_ivsize(ctr) != 16 ||
488*4882a593Smuzhiyun 	    ctr->base.cra_blocksize != 1)
489*4882a593Smuzhiyun 		goto err_free_inst;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	/* ctr and cbcmac must use the same underlying block cipher. */
492*4882a593Smuzhiyun 	if (strcmp(ctr->base.cra_name + 4, mac->base.cra_name + 7) != 0)
493*4882a593Smuzhiyun 		goto err_free_inst;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	err = -ENAMETOOLONG;
496*4882a593Smuzhiyun 	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
497*4882a593Smuzhiyun 		     "ccm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
498*4882a593Smuzhiyun 		goto err_free_inst;
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
501*4882a593Smuzhiyun 		     "ccm_base(%s,%s)", ctr->base.cra_driver_name,
502*4882a593Smuzhiyun 		     mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
503*4882a593Smuzhiyun 		goto err_free_inst;
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	inst->alg.base.cra_priority = (mac->base.cra_priority +
506*4882a593Smuzhiyun 				       ctr->base.cra_priority) / 2;
507*4882a593Smuzhiyun 	inst->alg.base.cra_blocksize = 1;
508*4882a593Smuzhiyun 	inst->alg.base.cra_alignmask = mac->base.cra_alignmask |
509*4882a593Smuzhiyun 				       ctr->base.cra_alignmask;
510*4882a593Smuzhiyun 	inst->alg.ivsize = 16;
511*4882a593Smuzhiyun 	inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
512*4882a593Smuzhiyun 	inst->alg.maxauthsize = 16;
513*4882a593Smuzhiyun 	inst->alg.base.cra_ctxsize = sizeof(struct crypto_ccm_ctx);
514*4882a593Smuzhiyun 	inst->alg.init = crypto_ccm_init_tfm;
515*4882a593Smuzhiyun 	inst->alg.exit = crypto_ccm_exit_tfm;
516*4882a593Smuzhiyun 	inst->alg.setkey = crypto_ccm_setkey;
517*4882a593Smuzhiyun 	inst->alg.setauthsize = crypto_ccm_setauthsize;
518*4882a593Smuzhiyun 	inst->alg.encrypt = crypto_ccm_encrypt;
519*4882a593Smuzhiyun 	inst->alg.decrypt = crypto_ccm_decrypt;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	inst->free = crypto_ccm_free;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	err = aead_register_instance(tmpl, inst);
524*4882a593Smuzhiyun 	if (err) {
525*4882a593Smuzhiyun err_free_inst:
526*4882a593Smuzhiyun 		crypto_ccm_free(inst);
527*4882a593Smuzhiyun 	}
528*4882a593Smuzhiyun 	return err;
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun 
crypto_ccm_create(struct crypto_template * tmpl,struct rtattr ** tb)531*4882a593Smuzhiyun static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun 	const char *cipher_name;
534*4882a593Smuzhiyun 	char ctr_name[CRYPTO_MAX_ALG_NAME];
535*4882a593Smuzhiyun 	char mac_name[CRYPTO_MAX_ALG_NAME];
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	cipher_name = crypto_attr_alg_name(tb[1]);
538*4882a593Smuzhiyun 	if (IS_ERR(cipher_name))
539*4882a593Smuzhiyun 		return PTR_ERR(cipher_name);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
542*4882a593Smuzhiyun 		     cipher_name) >= CRYPTO_MAX_ALG_NAME)
543*4882a593Smuzhiyun 		return -ENAMETOOLONG;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	if (snprintf(mac_name, CRYPTO_MAX_ALG_NAME, "cbcmac(%s)",
546*4882a593Smuzhiyun 		     cipher_name) >= CRYPTO_MAX_ALG_NAME)
547*4882a593Smuzhiyun 		return -ENAMETOOLONG;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name);
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun 
crypto_ccm_base_create(struct crypto_template * tmpl,struct rtattr ** tb)552*4882a593Smuzhiyun static int crypto_ccm_base_create(struct crypto_template *tmpl,
553*4882a593Smuzhiyun 				  struct rtattr **tb)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun 	const char *ctr_name;
556*4882a593Smuzhiyun 	const char *mac_name;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	ctr_name = crypto_attr_alg_name(tb[1]);
559*4882a593Smuzhiyun 	if (IS_ERR(ctr_name))
560*4882a593Smuzhiyun 		return PTR_ERR(ctr_name);
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	mac_name = crypto_attr_alg_name(tb[2]);
563*4882a593Smuzhiyun 	if (IS_ERR(mac_name))
564*4882a593Smuzhiyun 		return PTR_ERR(mac_name);
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name);
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun 
crypto_rfc4309_setkey(struct crypto_aead * parent,const u8 * key,unsigned int keylen)569*4882a593Smuzhiyun static int crypto_rfc4309_setkey(struct crypto_aead *parent, const u8 *key,
570*4882a593Smuzhiyun 				 unsigned int keylen)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun 	struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
573*4882a593Smuzhiyun 	struct crypto_aead *child = ctx->child;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	if (keylen < 3)
576*4882a593Smuzhiyun 		return -EINVAL;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	keylen -= 3;
579*4882a593Smuzhiyun 	memcpy(ctx->nonce, key + keylen, 3);
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
582*4882a593Smuzhiyun 	crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
583*4882a593Smuzhiyun 				     CRYPTO_TFM_REQ_MASK);
584*4882a593Smuzhiyun 	return crypto_aead_setkey(child, key, keylen);
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun 
crypto_rfc4309_setauthsize(struct crypto_aead * parent,unsigned int authsize)587*4882a593Smuzhiyun static int crypto_rfc4309_setauthsize(struct crypto_aead *parent,
588*4882a593Smuzhiyun 				      unsigned int authsize)
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun 	struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	switch (authsize) {
593*4882a593Smuzhiyun 	case 8:
594*4882a593Smuzhiyun 	case 12:
595*4882a593Smuzhiyun 	case 16:
596*4882a593Smuzhiyun 		break;
597*4882a593Smuzhiyun 	default:
598*4882a593Smuzhiyun 		return -EINVAL;
599*4882a593Smuzhiyun 	}
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	return crypto_aead_setauthsize(ctx->child, authsize);
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun 
crypto_rfc4309_crypt(struct aead_request * req)604*4882a593Smuzhiyun static struct aead_request *crypto_rfc4309_crypt(struct aead_request *req)
605*4882a593Smuzhiyun {
606*4882a593Smuzhiyun 	struct crypto_rfc4309_req_ctx *rctx = aead_request_ctx(req);
607*4882a593Smuzhiyun 	struct aead_request *subreq = &rctx->subreq;
608*4882a593Smuzhiyun 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
609*4882a593Smuzhiyun 	struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(aead);
610*4882a593Smuzhiyun 	struct crypto_aead *child = ctx->child;
611*4882a593Smuzhiyun 	struct scatterlist *sg;
612*4882a593Smuzhiyun 	u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
613*4882a593Smuzhiyun 			   crypto_aead_alignmask(child) + 1);
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	/* L' */
616*4882a593Smuzhiyun 	iv[0] = 3;
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	memcpy(iv + 1, ctx->nonce, 3);
619*4882a593Smuzhiyun 	memcpy(iv + 4, req->iv, 8);
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	scatterwalk_map_and_copy(iv + 16, req->src, 0, req->assoclen - 8, 0);
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	sg_init_table(rctx->src, 3);
624*4882a593Smuzhiyun 	sg_set_buf(rctx->src, iv + 16, req->assoclen - 8);
625*4882a593Smuzhiyun 	sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
626*4882a593Smuzhiyun 	if (sg != rctx->src + 1)
627*4882a593Smuzhiyun 		sg_chain(rctx->src, 2, sg);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	if (req->src != req->dst) {
630*4882a593Smuzhiyun 		sg_init_table(rctx->dst, 3);
631*4882a593Smuzhiyun 		sg_set_buf(rctx->dst, iv + 16, req->assoclen - 8);
632*4882a593Smuzhiyun 		sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
633*4882a593Smuzhiyun 		if (sg != rctx->dst + 1)
634*4882a593Smuzhiyun 			sg_chain(rctx->dst, 2, sg);
635*4882a593Smuzhiyun 	}
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	aead_request_set_tfm(subreq, child);
638*4882a593Smuzhiyun 	aead_request_set_callback(subreq, req->base.flags, req->base.complete,
639*4882a593Smuzhiyun 				  req->base.data);
640*4882a593Smuzhiyun 	aead_request_set_crypt(subreq, rctx->src,
641*4882a593Smuzhiyun 			       req->src == req->dst ? rctx->src : rctx->dst,
642*4882a593Smuzhiyun 			       req->cryptlen, iv);
643*4882a593Smuzhiyun 	aead_request_set_ad(subreq, req->assoclen - 8);
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	return subreq;
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun 
crypto_rfc4309_encrypt(struct aead_request * req)648*4882a593Smuzhiyun static int crypto_rfc4309_encrypt(struct aead_request *req)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun 	if (req->assoclen != 16 && req->assoclen != 20)
651*4882a593Smuzhiyun 		return -EINVAL;
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	req = crypto_rfc4309_crypt(req);
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	return crypto_aead_encrypt(req);
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun 
crypto_rfc4309_decrypt(struct aead_request * req)658*4882a593Smuzhiyun static int crypto_rfc4309_decrypt(struct aead_request *req)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun 	if (req->assoclen != 16 && req->assoclen != 20)
661*4882a593Smuzhiyun 		return -EINVAL;
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	req = crypto_rfc4309_crypt(req);
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	return crypto_aead_decrypt(req);
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun 
crypto_rfc4309_init_tfm(struct crypto_aead * tfm)668*4882a593Smuzhiyun static int crypto_rfc4309_init_tfm(struct crypto_aead *tfm)
669*4882a593Smuzhiyun {
670*4882a593Smuzhiyun 	struct aead_instance *inst = aead_alg_instance(tfm);
671*4882a593Smuzhiyun 	struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
672*4882a593Smuzhiyun 	struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
673*4882a593Smuzhiyun 	struct crypto_aead *aead;
674*4882a593Smuzhiyun 	unsigned long align;
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	aead = crypto_spawn_aead(spawn);
677*4882a593Smuzhiyun 	if (IS_ERR(aead))
678*4882a593Smuzhiyun 		return PTR_ERR(aead);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	ctx->child = aead;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	align = crypto_aead_alignmask(aead);
683*4882a593Smuzhiyun 	align &= ~(crypto_tfm_ctx_alignment() - 1);
684*4882a593Smuzhiyun 	crypto_aead_set_reqsize(
685*4882a593Smuzhiyun 		tfm,
686*4882a593Smuzhiyun 		sizeof(struct crypto_rfc4309_req_ctx) +
687*4882a593Smuzhiyun 		ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
688*4882a593Smuzhiyun 		align + 32);
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	return 0;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun 
crypto_rfc4309_exit_tfm(struct crypto_aead * tfm)693*4882a593Smuzhiyun static void crypto_rfc4309_exit_tfm(struct crypto_aead *tfm)
694*4882a593Smuzhiyun {
695*4882a593Smuzhiyun 	struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	crypto_free_aead(ctx->child);
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun 
crypto_rfc4309_free(struct aead_instance * inst)700*4882a593Smuzhiyun static void crypto_rfc4309_free(struct aead_instance *inst)
701*4882a593Smuzhiyun {
702*4882a593Smuzhiyun 	crypto_drop_aead(aead_instance_ctx(inst));
703*4882a593Smuzhiyun 	kfree(inst);
704*4882a593Smuzhiyun }
705*4882a593Smuzhiyun 
crypto_rfc4309_create(struct crypto_template * tmpl,struct rtattr ** tb)706*4882a593Smuzhiyun static int crypto_rfc4309_create(struct crypto_template *tmpl,
707*4882a593Smuzhiyun 				 struct rtattr **tb)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun 	u32 mask;
710*4882a593Smuzhiyun 	struct aead_instance *inst;
711*4882a593Smuzhiyun 	struct crypto_aead_spawn *spawn;
712*4882a593Smuzhiyun 	struct aead_alg *alg;
713*4882a593Smuzhiyun 	int err;
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
716*4882a593Smuzhiyun 	if (err)
717*4882a593Smuzhiyun 		return err;
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
720*4882a593Smuzhiyun 	if (!inst)
721*4882a593Smuzhiyun 		return -ENOMEM;
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	spawn = aead_instance_ctx(inst);
724*4882a593Smuzhiyun 	err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
725*4882a593Smuzhiyun 			       crypto_attr_alg_name(tb[1]), 0, mask);
726*4882a593Smuzhiyun 	if (err)
727*4882a593Smuzhiyun 		goto err_free_inst;
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	alg = crypto_spawn_aead_alg(spawn);
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	err = -EINVAL;
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	/* We only support 16-byte blocks. */
734*4882a593Smuzhiyun 	if (crypto_aead_alg_ivsize(alg) != 16)
735*4882a593Smuzhiyun 		goto err_free_inst;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	/* Not a stream cipher? */
738*4882a593Smuzhiyun 	if (alg->base.cra_blocksize != 1)
739*4882a593Smuzhiyun 		goto err_free_inst;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	err = -ENAMETOOLONG;
742*4882a593Smuzhiyun 	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
743*4882a593Smuzhiyun 		     "rfc4309(%s)", alg->base.cra_name) >=
744*4882a593Smuzhiyun 	    CRYPTO_MAX_ALG_NAME ||
745*4882a593Smuzhiyun 	    snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
746*4882a593Smuzhiyun 		     "rfc4309(%s)", alg->base.cra_driver_name) >=
747*4882a593Smuzhiyun 	    CRYPTO_MAX_ALG_NAME)
748*4882a593Smuzhiyun 		goto err_free_inst;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	inst->alg.base.cra_priority = alg->base.cra_priority;
751*4882a593Smuzhiyun 	inst->alg.base.cra_blocksize = 1;
752*4882a593Smuzhiyun 	inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	inst->alg.ivsize = 8;
755*4882a593Smuzhiyun 	inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
756*4882a593Smuzhiyun 	inst->alg.maxauthsize = 16;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4309_ctx);
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	inst->alg.init = crypto_rfc4309_init_tfm;
761*4882a593Smuzhiyun 	inst->alg.exit = crypto_rfc4309_exit_tfm;
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	inst->alg.setkey = crypto_rfc4309_setkey;
764*4882a593Smuzhiyun 	inst->alg.setauthsize = crypto_rfc4309_setauthsize;
765*4882a593Smuzhiyun 	inst->alg.encrypt = crypto_rfc4309_encrypt;
766*4882a593Smuzhiyun 	inst->alg.decrypt = crypto_rfc4309_decrypt;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	inst->free = crypto_rfc4309_free;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	err = aead_register_instance(tmpl, inst);
771*4882a593Smuzhiyun 	if (err) {
772*4882a593Smuzhiyun err_free_inst:
773*4882a593Smuzhiyun 		crypto_rfc4309_free(inst);
774*4882a593Smuzhiyun 	}
775*4882a593Smuzhiyun 	return err;
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun 
crypto_cbcmac_digest_setkey(struct crypto_shash * parent,const u8 * inkey,unsigned int keylen)778*4882a593Smuzhiyun static int crypto_cbcmac_digest_setkey(struct crypto_shash *parent,
779*4882a593Smuzhiyun 				     const u8 *inkey, unsigned int keylen)
780*4882a593Smuzhiyun {
781*4882a593Smuzhiyun 	struct cbcmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	return crypto_cipher_setkey(ctx->child, inkey, keylen);
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun 
crypto_cbcmac_digest_init(struct shash_desc * pdesc)786*4882a593Smuzhiyun static int crypto_cbcmac_digest_init(struct shash_desc *pdesc)
787*4882a593Smuzhiyun {
788*4882a593Smuzhiyun 	struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
789*4882a593Smuzhiyun 	int bs = crypto_shash_digestsize(pdesc->tfm);
790*4882a593Smuzhiyun 	u8 *dg = (u8 *)ctx + crypto_shash_descsize(pdesc->tfm) - bs;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	ctx->len = 0;
793*4882a593Smuzhiyun 	memset(dg, 0, bs);
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	return 0;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
crypto_cbcmac_digest_update(struct shash_desc * pdesc,const u8 * p,unsigned int len)798*4882a593Smuzhiyun static int crypto_cbcmac_digest_update(struct shash_desc *pdesc, const u8 *p,
799*4882a593Smuzhiyun 				       unsigned int len)
800*4882a593Smuzhiyun {
801*4882a593Smuzhiyun 	struct crypto_shash *parent = pdesc->tfm;
802*4882a593Smuzhiyun 	struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
803*4882a593Smuzhiyun 	struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
804*4882a593Smuzhiyun 	struct crypto_cipher *tfm = tctx->child;
805*4882a593Smuzhiyun 	int bs = crypto_shash_digestsize(parent);
806*4882a593Smuzhiyun 	u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	while (len > 0) {
809*4882a593Smuzhiyun 		unsigned int l = min(len, bs - ctx->len);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 		crypto_xor(dg + ctx->len, p, l);
812*4882a593Smuzhiyun 		ctx->len +=l;
813*4882a593Smuzhiyun 		len -= l;
814*4882a593Smuzhiyun 		p += l;
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 		if (ctx->len == bs) {
817*4882a593Smuzhiyun 			crypto_cipher_encrypt_one(tfm, dg, dg);
818*4882a593Smuzhiyun 			ctx->len = 0;
819*4882a593Smuzhiyun 		}
820*4882a593Smuzhiyun 	}
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	return 0;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun 
crypto_cbcmac_digest_final(struct shash_desc * pdesc,u8 * out)825*4882a593Smuzhiyun static int crypto_cbcmac_digest_final(struct shash_desc *pdesc, u8 *out)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun 	struct crypto_shash *parent = pdesc->tfm;
828*4882a593Smuzhiyun 	struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
829*4882a593Smuzhiyun 	struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
830*4882a593Smuzhiyun 	struct crypto_cipher *tfm = tctx->child;
831*4882a593Smuzhiyun 	int bs = crypto_shash_digestsize(parent);
832*4882a593Smuzhiyun 	u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	if (ctx->len)
835*4882a593Smuzhiyun 		crypto_cipher_encrypt_one(tfm, dg, dg);
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	memcpy(out, dg, bs);
838*4882a593Smuzhiyun 	return 0;
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun 
cbcmac_init_tfm(struct crypto_tfm * tfm)841*4882a593Smuzhiyun static int cbcmac_init_tfm(struct crypto_tfm *tfm)
842*4882a593Smuzhiyun {
843*4882a593Smuzhiyun 	struct crypto_cipher *cipher;
844*4882a593Smuzhiyun 	struct crypto_instance *inst = (void *)tfm->__crt_alg;
845*4882a593Smuzhiyun 	struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
846*4882a593Smuzhiyun 	struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	cipher = crypto_spawn_cipher(spawn);
849*4882a593Smuzhiyun 	if (IS_ERR(cipher))
850*4882a593Smuzhiyun 		return PTR_ERR(cipher);
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 	ctx->child = cipher;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	return 0;
855*4882a593Smuzhiyun };
856*4882a593Smuzhiyun 
cbcmac_exit_tfm(struct crypto_tfm * tfm)857*4882a593Smuzhiyun static void cbcmac_exit_tfm(struct crypto_tfm *tfm)
858*4882a593Smuzhiyun {
859*4882a593Smuzhiyun 	struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
860*4882a593Smuzhiyun 	crypto_free_cipher(ctx->child);
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun 
cbcmac_create(struct crypto_template * tmpl,struct rtattr ** tb)863*4882a593Smuzhiyun static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb)
864*4882a593Smuzhiyun {
865*4882a593Smuzhiyun 	struct shash_instance *inst;
866*4882a593Smuzhiyun 	struct crypto_cipher_spawn *spawn;
867*4882a593Smuzhiyun 	struct crypto_alg *alg;
868*4882a593Smuzhiyun 	u32 mask;
869*4882a593Smuzhiyun 	int err;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
872*4882a593Smuzhiyun 	if (err)
873*4882a593Smuzhiyun 		return err;
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
876*4882a593Smuzhiyun 	if (!inst)
877*4882a593Smuzhiyun 		return -ENOMEM;
878*4882a593Smuzhiyun 	spawn = shash_instance_ctx(inst);
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
881*4882a593Smuzhiyun 				 crypto_attr_alg_name(tb[1]), 0, mask);
882*4882a593Smuzhiyun 	if (err)
883*4882a593Smuzhiyun 		goto err_free_inst;
884*4882a593Smuzhiyun 	alg = crypto_spawn_cipher_alg(spawn);
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
887*4882a593Smuzhiyun 	if (err)
888*4882a593Smuzhiyun 		goto err_free_inst;
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	inst->alg.base.cra_priority = alg->cra_priority;
891*4882a593Smuzhiyun 	inst->alg.base.cra_blocksize = 1;
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	inst->alg.digestsize = alg->cra_blocksize;
894*4882a593Smuzhiyun 	inst->alg.descsize = ALIGN(sizeof(struct cbcmac_desc_ctx),
895*4882a593Smuzhiyun 				   alg->cra_alignmask + 1) +
896*4882a593Smuzhiyun 			     alg->cra_blocksize;
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	inst->alg.base.cra_ctxsize = sizeof(struct cbcmac_tfm_ctx);
899*4882a593Smuzhiyun 	inst->alg.base.cra_init = cbcmac_init_tfm;
900*4882a593Smuzhiyun 	inst->alg.base.cra_exit = cbcmac_exit_tfm;
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	inst->alg.init = crypto_cbcmac_digest_init;
903*4882a593Smuzhiyun 	inst->alg.update = crypto_cbcmac_digest_update;
904*4882a593Smuzhiyun 	inst->alg.final = crypto_cbcmac_digest_final;
905*4882a593Smuzhiyun 	inst->alg.setkey = crypto_cbcmac_digest_setkey;
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	inst->free = shash_free_singlespawn_instance;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	err = shash_register_instance(tmpl, inst);
910*4882a593Smuzhiyun 	if (err) {
911*4882a593Smuzhiyun err_free_inst:
912*4882a593Smuzhiyun 		shash_free_singlespawn_instance(inst);
913*4882a593Smuzhiyun 	}
914*4882a593Smuzhiyun 	return err;
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun static struct crypto_template crypto_ccm_tmpls[] = {
918*4882a593Smuzhiyun 	{
919*4882a593Smuzhiyun 		.name = "cbcmac",
920*4882a593Smuzhiyun 		.create = cbcmac_create,
921*4882a593Smuzhiyun 		.module = THIS_MODULE,
922*4882a593Smuzhiyun 	}, {
923*4882a593Smuzhiyun 		.name = "ccm_base",
924*4882a593Smuzhiyun 		.create = crypto_ccm_base_create,
925*4882a593Smuzhiyun 		.module = THIS_MODULE,
926*4882a593Smuzhiyun 	}, {
927*4882a593Smuzhiyun 		.name = "ccm",
928*4882a593Smuzhiyun 		.create = crypto_ccm_create,
929*4882a593Smuzhiyun 		.module = THIS_MODULE,
930*4882a593Smuzhiyun 	}, {
931*4882a593Smuzhiyun 		.name = "rfc4309",
932*4882a593Smuzhiyun 		.create = crypto_rfc4309_create,
933*4882a593Smuzhiyun 		.module = THIS_MODULE,
934*4882a593Smuzhiyun 	},
935*4882a593Smuzhiyun };
936*4882a593Smuzhiyun 
crypto_ccm_module_init(void)937*4882a593Smuzhiyun static int __init crypto_ccm_module_init(void)
938*4882a593Smuzhiyun {
939*4882a593Smuzhiyun 	return crypto_register_templates(crypto_ccm_tmpls,
940*4882a593Smuzhiyun 					 ARRAY_SIZE(crypto_ccm_tmpls));
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun 
crypto_ccm_module_exit(void)943*4882a593Smuzhiyun static void __exit crypto_ccm_module_exit(void)
944*4882a593Smuzhiyun {
945*4882a593Smuzhiyun 	crypto_unregister_templates(crypto_ccm_tmpls,
946*4882a593Smuzhiyun 				    ARRAY_SIZE(crypto_ccm_tmpls));
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun subsys_initcall(crypto_ccm_module_init);
950*4882a593Smuzhiyun module_exit(crypto_ccm_module_exit);
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun MODULE_LICENSE("GPL");
953*4882a593Smuzhiyun MODULE_DESCRIPTION("Counter with CBC MAC");
954*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("ccm_base");
955*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("rfc4309");
956*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("ccm");
957*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("cbcmac");
958*4882a593Smuzhiyun MODULE_IMPORT_NS(CRYPTO_INTERNAL);
959