xref: /OK3568_Linux_fs/kernel/crypto/cts.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * CTS: Cipher Text Stealing mode
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * COPYRIGHT (c) 2008
5*4882a593Smuzhiyun  * The Regents of the University of Michigan
6*4882a593Smuzhiyun  * ALL RIGHTS RESERVED
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Permission is granted to use, copy, create derivative works
9*4882a593Smuzhiyun  * and redistribute this software and such derivative works
10*4882a593Smuzhiyun  * for any purpose, so long as the name of The University of
11*4882a593Smuzhiyun  * Michigan is not used in any advertising or publicity
12*4882a593Smuzhiyun  * pertaining to the use of distribution of this software
13*4882a593Smuzhiyun  * without specific, written prior authorization.  If the
14*4882a593Smuzhiyun  * above copyright notice or any other identification of the
15*4882a593Smuzhiyun  * University of Michigan is included in any copy of any
16*4882a593Smuzhiyun  * portion of this software, then the disclaimer below must
17*4882a593Smuzhiyun  * also be included.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
20*4882a593Smuzhiyun  * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
21*4882a593Smuzhiyun  * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
22*4882a593Smuzhiyun  * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
23*4882a593Smuzhiyun  * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
24*4882a593Smuzhiyun  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
25*4882a593Smuzhiyun  * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
26*4882a593Smuzhiyun  * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
27*4882a593Smuzhiyun  * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
28*4882a593Smuzhiyun  * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
29*4882a593Smuzhiyun  * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
30*4882a593Smuzhiyun  * SUCH DAMAGES.
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /* Derived from various:
34*4882a593Smuzhiyun  *	Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun  * This is the Cipher Text Stealing mode as described by
39*4882a593Smuzhiyun  * Section 8 of rfc2040 and referenced by rfc3962.
40*4882a593Smuzhiyun  * rfc3962 includes errata information in its Appendix A.
41*4882a593Smuzhiyun  */
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #include <crypto/algapi.h>
44*4882a593Smuzhiyun #include <crypto/internal/skcipher.h>
45*4882a593Smuzhiyun #include <linux/err.h>
46*4882a593Smuzhiyun #include <linux/init.h>
47*4882a593Smuzhiyun #include <linux/kernel.h>
48*4882a593Smuzhiyun #include <linux/log2.h>
49*4882a593Smuzhiyun #include <linux/module.h>
50*4882a593Smuzhiyun #include <linux/scatterlist.h>
51*4882a593Smuzhiyun #include <crypto/scatterwalk.h>
52*4882a593Smuzhiyun #include <linux/slab.h>
53*4882a593Smuzhiyun #include <linux/compiler.h>
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun struct crypto_cts_ctx {
56*4882a593Smuzhiyun 	struct crypto_skcipher *child;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun struct crypto_cts_reqctx {
60*4882a593Smuzhiyun 	struct scatterlist sg[2];
61*4882a593Smuzhiyun 	unsigned offset;
62*4882a593Smuzhiyun 	struct skcipher_request subreq;
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun 
crypto_cts_reqctx_space(struct skcipher_request * req)65*4882a593Smuzhiyun static inline u8 *crypto_cts_reqctx_space(struct skcipher_request *req)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
68*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
69*4882a593Smuzhiyun 	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
70*4882a593Smuzhiyun 	struct crypto_skcipher *child = ctx->child;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	return PTR_ALIGN((u8 *)(rctx + 1) + crypto_skcipher_reqsize(child),
73*4882a593Smuzhiyun 			 crypto_skcipher_alignmask(tfm) + 1);
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
crypto_cts_setkey(struct crypto_skcipher * parent,const u8 * key,unsigned int keylen)76*4882a593Smuzhiyun static int crypto_cts_setkey(struct crypto_skcipher *parent, const u8 *key,
77*4882a593Smuzhiyun 			     unsigned int keylen)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(parent);
80*4882a593Smuzhiyun 	struct crypto_skcipher *child = ctx->child;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
83*4882a593Smuzhiyun 	crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
84*4882a593Smuzhiyun 					 CRYPTO_TFM_REQ_MASK);
85*4882a593Smuzhiyun 	return crypto_skcipher_setkey(child, key, keylen);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
cts_cbc_crypt_done(struct crypto_async_request * areq,int err)88*4882a593Smuzhiyun static void cts_cbc_crypt_done(struct crypto_async_request *areq, int err)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	struct skcipher_request *req = areq->data;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	if (err == -EINPROGRESS)
93*4882a593Smuzhiyun 		return;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	skcipher_request_complete(req, err);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
cts_cbc_encrypt(struct skcipher_request * req)98*4882a593Smuzhiyun static int cts_cbc_encrypt(struct skcipher_request *req)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
101*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
102*4882a593Smuzhiyun 	struct skcipher_request *subreq = &rctx->subreq;
103*4882a593Smuzhiyun 	int bsize = crypto_skcipher_blocksize(tfm);
104*4882a593Smuzhiyun 	u8 d[MAX_CIPHER_BLOCKSIZE * 2] __aligned(__alignof__(u32));
105*4882a593Smuzhiyun 	struct scatterlist *sg;
106*4882a593Smuzhiyun 	unsigned int offset;
107*4882a593Smuzhiyun 	int lastn;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	offset = rctx->offset;
110*4882a593Smuzhiyun 	lastn = req->cryptlen - offset;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
113*4882a593Smuzhiyun 	scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	memset(d, 0, bsize);
116*4882a593Smuzhiyun 	scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
119*4882a593Smuzhiyun 	memzero_explicit(d, sizeof(d));
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	skcipher_request_set_callback(subreq, req->base.flags &
122*4882a593Smuzhiyun 					      CRYPTO_TFM_REQ_MAY_BACKLOG,
123*4882a593Smuzhiyun 				      cts_cbc_crypt_done, req);
124*4882a593Smuzhiyun 	skcipher_request_set_crypt(subreq, sg, sg, bsize, req->iv);
125*4882a593Smuzhiyun 	return crypto_skcipher_encrypt(subreq);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
crypto_cts_encrypt_done(struct crypto_async_request * areq,int err)128*4882a593Smuzhiyun static void crypto_cts_encrypt_done(struct crypto_async_request *areq, int err)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	struct skcipher_request *req = areq->data;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	if (err)
133*4882a593Smuzhiyun 		goto out;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	err = cts_cbc_encrypt(req);
136*4882a593Smuzhiyun 	if (err == -EINPROGRESS || err == -EBUSY)
137*4882a593Smuzhiyun 		return;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun out:
140*4882a593Smuzhiyun 	skcipher_request_complete(req, err);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
crypto_cts_encrypt(struct skcipher_request * req)143*4882a593Smuzhiyun static int crypto_cts_encrypt(struct skcipher_request *req)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
146*4882a593Smuzhiyun 	struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
147*4882a593Smuzhiyun 	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
148*4882a593Smuzhiyun 	struct skcipher_request *subreq = &rctx->subreq;
149*4882a593Smuzhiyun 	int bsize = crypto_skcipher_blocksize(tfm);
150*4882a593Smuzhiyun 	unsigned int nbytes = req->cryptlen;
151*4882a593Smuzhiyun 	unsigned int offset;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	skcipher_request_set_tfm(subreq, ctx->child);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	if (nbytes < bsize)
156*4882a593Smuzhiyun 		return -EINVAL;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	if (nbytes == bsize) {
159*4882a593Smuzhiyun 		skcipher_request_set_callback(subreq, req->base.flags,
160*4882a593Smuzhiyun 					      req->base.complete,
161*4882a593Smuzhiyun 					      req->base.data);
162*4882a593Smuzhiyun 		skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
163*4882a593Smuzhiyun 					   req->iv);
164*4882a593Smuzhiyun 		return crypto_skcipher_encrypt(subreq);
165*4882a593Smuzhiyun 	}
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	offset = rounddown(nbytes - 1, bsize);
168*4882a593Smuzhiyun 	rctx->offset = offset;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	skcipher_request_set_callback(subreq, req->base.flags,
171*4882a593Smuzhiyun 				      crypto_cts_encrypt_done, req);
172*4882a593Smuzhiyun 	skcipher_request_set_crypt(subreq, req->src, req->dst,
173*4882a593Smuzhiyun 				   offset, req->iv);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	return crypto_skcipher_encrypt(subreq) ?:
176*4882a593Smuzhiyun 	       cts_cbc_encrypt(req);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
cts_cbc_decrypt(struct skcipher_request * req)179*4882a593Smuzhiyun static int cts_cbc_decrypt(struct skcipher_request *req)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
182*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
183*4882a593Smuzhiyun 	struct skcipher_request *subreq = &rctx->subreq;
184*4882a593Smuzhiyun 	int bsize = crypto_skcipher_blocksize(tfm);
185*4882a593Smuzhiyun 	u8 d[MAX_CIPHER_BLOCKSIZE * 2] __aligned(__alignof__(u32));
186*4882a593Smuzhiyun 	struct scatterlist *sg;
187*4882a593Smuzhiyun 	unsigned int offset;
188*4882a593Smuzhiyun 	u8 *space;
189*4882a593Smuzhiyun 	int lastn;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	offset = rctx->offset;
192*4882a593Smuzhiyun 	lastn = req->cryptlen - offset;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	/* 1. Decrypt Cn-1 (s) to create Dn */
197*4882a593Smuzhiyun 	scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
198*4882a593Smuzhiyun 	space = crypto_cts_reqctx_space(req);
199*4882a593Smuzhiyun 	crypto_xor(d + bsize, space, bsize);
200*4882a593Smuzhiyun 	/* 2. Pad Cn with zeros at the end to create C of length BB */
201*4882a593Smuzhiyun 	memset(d, 0, bsize);
202*4882a593Smuzhiyun 	scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
203*4882a593Smuzhiyun 	/* 3. Exclusive-or Dn with C to create Xn */
204*4882a593Smuzhiyun 	/* 4. Select the first Ln bytes of Xn to create Pn */
205*4882a593Smuzhiyun 	crypto_xor(d + bsize, d, lastn);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	/* 5. Append the tail (BB - Ln) bytes of Xn to Cn to create En */
208*4882a593Smuzhiyun 	memcpy(d + lastn, d + bsize + lastn, bsize - lastn);
209*4882a593Smuzhiyun 	/* 6. Decrypt En to create Pn-1 */
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
212*4882a593Smuzhiyun 	memzero_explicit(d, sizeof(d));
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	skcipher_request_set_callback(subreq, req->base.flags &
215*4882a593Smuzhiyun 					      CRYPTO_TFM_REQ_MAY_BACKLOG,
216*4882a593Smuzhiyun 				      cts_cbc_crypt_done, req);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	skcipher_request_set_crypt(subreq, sg, sg, bsize, space);
219*4882a593Smuzhiyun 	return crypto_skcipher_decrypt(subreq);
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun 
crypto_cts_decrypt_done(struct crypto_async_request * areq,int err)222*4882a593Smuzhiyun static void crypto_cts_decrypt_done(struct crypto_async_request *areq, int err)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	struct skcipher_request *req = areq->data;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	if (err)
227*4882a593Smuzhiyun 		goto out;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	err = cts_cbc_decrypt(req);
230*4882a593Smuzhiyun 	if (err == -EINPROGRESS || err == -EBUSY)
231*4882a593Smuzhiyun 		return;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun out:
234*4882a593Smuzhiyun 	skcipher_request_complete(req, err);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
crypto_cts_decrypt(struct skcipher_request * req)237*4882a593Smuzhiyun static int crypto_cts_decrypt(struct skcipher_request *req)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
240*4882a593Smuzhiyun 	struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
241*4882a593Smuzhiyun 	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
242*4882a593Smuzhiyun 	struct skcipher_request *subreq = &rctx->subreq;
243*4882a593Smuzhiyun 	int bsize = crypto_skcipher_blocksize(tfm);
244*4882a593Smuzhiyun 	unsigned int nbytes = req->cryptlen;
245*4882a593Smuzhiyun 	unsigned int offset;
246*4882a593Smuzhiyun 	u8 *space;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	skcipher_request_set_tfm(subreq, ctx->child);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	if (nbytes < bsize)
251*4882a593Smuzhiyun 		return -EINVAL;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	if (nbytes == bsize) {
254*4882a593Smuzhiyun 		skcipher_request_set_callback(subreq, req->base.flags,
255*4882a593Smuzhiyun 					      req->base.complete,
256*4882a593Smuzhiyun 					      req->base.data);
257*4882a593Smuzhiyun 		skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
258*4882a593Smuzhiyun 					   req->iv);
259*4882a593Smuzhiyun 		return crypto_skcipher_decrypt(subreq);
260*4882a593Smuzhiyun 	}
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	skcipher_request_set_callback(subreq, req->base.flags,
263*4882a593Smuzhiyun 				      crypto_cts_decrypt_done, req);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	space = crypto_cts_reqctx_space(req);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	offset = rounddown(nbytes - 1, bsize);
268*4882a593Smuzhiyun 	rctx->offset = offset;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	if (offset <= bsize)
271*4882a593Smuzhiyun 		memcpy(space, req->iv, bsize);
272*4882a593Smuzhiyun 	else
273*4882a593Smuzhiyun 		scatterwalk_map_and_copy(space, req->src, offset - 2 * bsize,
274*4882a593Smuzhiyun 					 bsize, 0);
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	skcipher_request_set_crypt(subreq, req->src, req->dst,
277*4882a593Smuzhiyun 				   offset, req->iv);
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	return crypto_skcipher_decrypt(subreq) ?:
280*4882a593Smuzhiyun 	       cts_cbc_decrypt(req);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
crypto_cts_init_tfm(struct crypto_skcipher * tfm)283*4882a593Smuzhiyun static int crypto_cts_init_tfm(struct crypto_skcipher *tfm)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun 	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
286*4882a593Smuzhiyun 	struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
287*4882a593Smuzhiyun 	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
288*4882a593Smuzhiyun 	struct crypto_skcipher *cipher;
289*4882a593Smuzhiyun 	unsigned reqsize;
290*4882a593Smuzhiyun 	unsigned bsize;
291*4882a593Smuzhiyun 	unsigned align;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	cipher = crypto_spawn_skcipher(spawn);
294*4882a593Smuzhiyun 	if (IS_ERR(cipher))
295*4882a593Smuzhiyun 		return PTR_ERR(cipher);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	ctx->child = cipher;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	align = crypto_skcipher_alignmask(tfm);
300*4882a593Smuzhiyun 	bsize = crypto_skcipher_blocksize(cipher);
301*4882a593Smuzhiyun 	reqsize = ALIGN(sizeof(struct crypto_cts_reqctx) +
302*4882a593Smuzhiyun 			crypto_skcipher_reqsize(cipher),
303*4882a593Smuzhiyun 			crypto_tfm_ctx_alignment()) +
304*4882a593Smuzhiyun 		  (align & ~(crypto_tfm_ctx_alignment() - 1)) + bsize;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	crypto_skcipher_set_reqsize(tfm, reqsize);
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	return 0;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
crypto_cts_exit_tfm(struct crypto_skcipher * tfm)311*4882a593Smuzhiyun static void crypto_cts_exit_tfm(struct crypto_skcipher *tfm)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	crypto_free_skcipher(ctx->child);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun 
crypto_cts_free(struct skcipher_instance * inst)318*4882a593Smuzhiyun static void crypto_cts_free(struct skcipher_instance *inst)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun 	crypto_drop_skcipher(skcipher_instance_ctx(inst));
321*4882a593Smuzhiyun 	kfree(inst);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
crypto_cts_create(struct crypto_template * tmpl,struct rtattr ** tb)324*4882a593Smuzhiyun static int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	struct crypto_skcipher_spawn *spawn;
327*4882a593Smuzhiyun 	struct skcipher_instance *inst;
328*4882a593Smuzhiyun 	struct skcipher_alg *alg;
329*4882a593Smuzhiyun 	u32 mask;
330*4882a593Smuzhiyun 	int err;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
333*4882a593Smuzhiyun 	if (err)
334*4882a593Smuzhiyun 		return err;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
337*4882a593Smuzhiyun 	if (!inst)
338*4882a593Smuzhiyun 		return -ENOMEM;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	spawn = skcipher_instance_ctx(inst);
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst),
343*4882a593Smuzhiyun 				   crypto_attr_alg_name(tb[1]), 0, mask);
344*4882a593Smuzhiyun 	if (err)
345*4882a593Smuzhiyun 		goto err_free_inst;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	alg = crypto_spawn_skcipher_alg(spawn);
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	err = -EINVAL;
350*4882a593Smuzhiyun 	if (crypto_skcipher_alg_ivsize(alg) != alg->base.cra_blocksize)
351*4882a593Smuzhiyun 		goto err_free_inst;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	if (strncmp(alg->base.cra_name, "cbc(", 4))
354*4882a593Smuzhiyun 		goto err_free_inst;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	err = crypto_inst_setname(skcipher_crypto_instance(inst), "cts",
357*4882a593Smuzhiyun 				  &alg->base);
358*4882a593Smuzhiyun 	if (err)
359*4882a593Smuzhiyun 		goto err_free_inst;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	inst->alg.base.cra_priority = alg->base.cra_priority;
362*4882a593Smuzhiyun 	inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
363*4882a593Smuzhiyun 	inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	inst->alg.ivsize = alg->base.cra_blocksize;
366*4882a593Smuzhiyun 	inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
367*4882a593Smuzhiyun 	inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
368*4882a593Smuzhiyun 	inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	inst->alg.base.cra_ctxsize = sizeof(struct crypto_cts_ctx);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	inst->alg.init = crypto_cts_init_tfm;
373*4882a593Smuzhiyun 	inst->alg.exit = crypto_cts_exit_tfm;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	inst->alg.setkey = crypto_cts_setkey;
376*4882a593Smuzhiyun 	inst->alg.encrypt = crypto_cts_encrypt;
377*4882a593Smuzhiyun 	inst->alg.decrypt = crypto_cts_decrypt;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	inst->free = crypto_cts_free;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	err = skcipher_register_instance(tmpl, inst);
382*4882a593Smuzhiyun 	if (err) {
383*4882a593Smuzhiyun err_free_inst:
384*4882a593Smuzhiyun 		crypto_cts_free(inst);
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun 	return err;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun static struct crypto_template crypto_cts_tmpl = {
390*4882a593Smuzhiyun 	.name = "cts",
391*4882a593Smuzhiyun 	.create = crypto_cts_create,
392*4882a593Smuzhiyun 	.module = THIS_MODULE,
393*4882a593Smuzhiyun };
394*4882a593Smuzhiyun 
crypto_cts_module_init(void)395*4882a593Smuzhiyun static int __init crypto_cts_module_init(void)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun 	return crypto_register_template(&crypto_cts_tmpl);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun 
crypto_cts_module_exit(void)400*4882a593Smuzhiyun static void __exit crypto_cts_module_exit(void)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun 	crypto_unregister_template(&crypto_cts_tmpl);
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun subsys_initcall(crypto_cts_module_init);
406*4882a593Smuzhiyun module_exit(crypto_cts_module_exit);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
409*4882a593Smuzhiyun MODULE_DESCRIPTION("CTS-CBC CipherText Stealing for CBC");
410*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("cts");
411