blob: 00254d76b21b64fa21baf4e61972fc83f7ad3e70 [file] [log] [blame]
Kevin Coffman76cb9522008-03-24 21:26:16 +08001/*
2 * CTS: Cipher Text Stealing mode
3 *
4 * COPYRIGHT (c) 2008
5 * The Regents of the University of Michigan
6 * ALL RIGHTS RESERVED
7 *
8 * Permission is granted to use, copy, create derivative works
9 * and redistribute this software and such derivative works
10 * for any purpose, so long as the name of The University of
11 * Michigan is not used in any advertising or publicity
12 * pertaining to the use of distribution of this software
13 * without specific, written prior authorization. If the
14 * above copyright notice or any other identification of the
15 * University of Michigan is included in any copy of any
16 * portion of this software, then the disclaimer below must
17 * also be included.
18 *
19 * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
20 * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
21 * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
22 * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
23 * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
25 * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
26 * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
27 * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
28 * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
29 * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGES.
31 */
32
33/* Derived from various:
34 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
35 */
36
37/*
38 * This is the Cipher Text Stealing mode as described by
39 * Section 8 of rfc2040 and referenced by rfc3962.
40 * rfc3962 includes errata information in its Appendix A.
41 */
42
Herbert Xu0605c412016-07-12 13:17:48 +080043#include <crypto/internal/skcipher.h>
Kevin Coffman76cb9522008-03-24 21:26:16 +080044#include <linux/err.h>
45#include <linux/init.h>
46#include <linux/kernel.h>
47#include <linux/log2.h>
48#include <linux/module.h>
49#include <linux/scatterlist.h>
50#include <crypto/scatterwalk.h>
51#include <linux/slab.h>
52
53struct crypto_cts_ctx {
Herbert Xu0605c412016-07-12 13:17:48 +080054 struct crypto_skcipher *child;
Kevin Coffman76cb9522008-03-24 21:26:16 +080055};
56
Herbert Xu0605c412016-07-12 13:17:48 +080057struct crypto_cts_reqctx {
58 struct scatterlist sg[2];
59 unsigned offset;
60 struct skcipher_request subreq;
61};
62
63static inline u8 *crypto_cts_reqctx_space(struct skcipher_request *req)
64{
65 struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
66 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
67 struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
68 struct crypto_skcipher *child = ctx->child;
69
70 return PTR_ALIGN((u8 *)(rctx + 1) + crypto_skcipher_reqsize(child),
71 crypto_skcipher_alignmask(tfm) + 1);
72}
73
74static int crypto_cts_setkey(struct crypto_skcipher *parent, const u8 *key,
Kevin Coffman76cb9522008-03-24 21:26:16 +080075 unsigned int keylen)
76{
Herbert Xu0605c412016-07-12 13:17:48 +080077 struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(parent);
78 struct crypto_skcipher *child = ctx->child;
Kevin Coffman76cb9522008-03-24 21:26:16 +080079 int err;
80
Herbert Xu0605c412016-07-12 13:17:48 +080081 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
82 crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
83 CRYPTO_TFM_REQ_MASK);
84 err = crypto_skcipher_setkey(child, key, keylen);
85 crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
86 CRYPTO_TFM_RES_MASK);
Kevin Coffman76cb9522008-03-24 21:26:16 +080087 return err;
88}
89
Herbert Xu0605c412016-07-12 13:17:48 +080090static void cts_cbc_crypt_done(struct crypto_async_request *areq, int err)
Kevin Coffman76cb9522008-03-24 21:26:16 +080091{
Herbert Xu0605c412016-07-12 13:17:48 +080092 struct skcipher_request *req = areq->data;
Kevin Coffman76cb9522008-03-24 21:26:16 +080093
Herbert Xu0605c412016-07-12 13:17:48 +080094 if (err == -EINPROGRESS)
95 return;
Kevin Coffman76cb9522008-03-24 21:26:16 +080096
Herbert Xu0605c412016-07-12 13:17:48 +080097 skcipher_request_complete(req, err);
Kevin Coffman76cb9522008-03-24 21:26:16 +080098}
99
Herbert Xu0605c412016-07-12 13:17:48 +0800100static int cts_cbc_encrypt(struct skcipher_request *req)
Kevin Coffman76cb9522008-03-24 21:26:16 +0800101{
Herbert Xu0605c412016-07-12 13:17:48 +0800102 struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
103 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
104 struct skcipher_request *subreq = &rctx->subreq;
105 int bsize = crypto_skcipher_blocksize(tfm);
106 u8 d[bsize * 2] __attribute__ ((aligned(__alignof__(u32))));
107 struct scatterlist *sg;
108 unsigned int offset;
109 int lastn;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800110
Herbert Xu0605c412016-07-12 13:17:48 +0800111 offset = rctx->offset;
112 lastn = req->cryptlen - offset;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800113
Herbert Xu0605c412016-07-12 13:17:48 +0800114 sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
115 scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
Kevin Coffman76cb9522008-03-24 21:26:16 +0800116
Herbert Xu0605c412016-07-12 13:17:48 +0800117 memset(d, 0, bsize);
118 scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
119
120 scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
121 memzero_explicit(d, sizeof(d));
122
123 skcipher_request_set_callback(subreq, req->base.flags &
124 CRYPTO_TFM_REQ_MAY_BACKLOG,
125 cts_cbc_crypt_done, req);
126 skcipher_request_set_crypt(subreq, sg, sg, bsize, req->iv);
127 return crypto_skcipher_encrypt(subreq);
Kevin Coffman76cb9522008-03-24 21:26:16 +0800128}
129
Herbert Xu0605c412016-07-12 13:17:48 +0800130static void crypto_cts_encrypt_done(struct crypto_async_request *areq, int err)
Kevin Coffman76cb9522008-03-24 21:26:16 +0800131{
Herbert Xu0605c412016-07-12 13:17:48 +0800132 struct skcipher_request *req = areq->data;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800133
Kevin Coffman76cb9522008-03-24 21:26:16 +0800134 if (err)
Herbert Xu0605c412016-07-12 13:17:48 +0800135 goto out;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800136
Herbert Xu0605c412016-07-12 13:17:48 +0800137 err = cts_cbc_encrypt(req);
138 if (err == -EINPROGRESS ||
139 (err == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
140 return;
Daniel Borkmann7185ad22014-09-07 23:23:38 +0200141
Herbert Xu0605c412016-07-12 13:17:48 +0800142out:
143 skcipher_request_complete(req, err);
Kevin Coffman76cb9522008-03-24 21:26:16 +0800144}
145
Herbert Xu0605c412016-07-12 13:17:48 +0800146static int crypto_cts_encrypt(struct skcipher_request *req)
Kevin Coffman76cb9522008-03-24 21:26:16 +0800147{
Herbert Xu0605c412016-07-12 13:17:48 +0800148 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
149 struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
150 struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
151 struct skcipher_request *subreq = &rctx->subreq;
152 int bsize = crypto_skcipher_blocksize(tfm);
153 unsigned int nbytes = req->cryptlen;
154 int cbc_blocks = (nbytes + bsize - 1) / bsize - 1;
155 unsigned int offset;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800156
Herbert Xu0605c412016-07-12 13:17:48 +0800157 skcipher_request_set_tfm(subreq, ctx->child);
Kevin Coffman76cb9522008-03-24 21:26:16 +0800158
Herbert Xu0605c412016-07-12 13:17:48 +0800159 if (cbc_blocks <= 0) {
160 skcipher_request_set_callback(subreq, req->base.flags,
161 req->base.complete,
162 req->base.data);
163 skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
164 req->iv);
165 return crypto_skcipher_encrypt(subreq);
Kevin Coffman76cb9522008-03-24 21:26:16 +0800166 }
Herbert Xu0605c412016-07-12 13:17:48 +0800167
168 offset = cbc_blocks * bsize;
169 rctx->offset = offset;
170
171 skcipher_request_set_callback(subreq, req->base.flags,
172 crypto_cts_encrypt_done, req);
173 skcipher_request_set_crypt(subreq, req->src, req->dst,
174 offset, req->iv);
175
176 return crypto_skcipher_encrypt(subreq) ?:
177 cts_cbc_encrypt(req);
Kevin Coffman76cb9522008-03-24 21:26:16 +0800178}
179
Herbert Xu0605c412016-07-12 13:17:48 +0800180static int cts_cbc_decrypt(struct skcipher_request *req)
Kevin Coffman76cb9522008-03-24 21:26:16 +0800181{
Herbert Xu0605c412016-07-12 13:17:48 +0800182 struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
183 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
184 struct skcipher_request *subreq = &rctx->subreq;
185 int bsize = crypto_skcipher_blocksize(tfm);
186 u8 d[bsize * 2] __attribute__ ((aligned(__alignof__(u32))));
187 struct scatterlist *sg;
188 unsigned int offset;
189 u8 *space;
190 int lastn;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800191
Herbert Xu0605c412016-07-12 13:17:48 +0800192 offset = rctx->offset;
193 lastn = req->cryptlen - offset;
194
195 sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
196
197 /* 1. Decrypt Cn-1 (s) to create Dn */
198 scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
199 space = crypto_cts_reqctx_space(req);
200 crypto_xor(d + bsize, space, bsize);
201 /* 2. Pad Cn with zeros at the end to create C of length BB */
202 memset(d, 0, bsize);
203 scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
204 /* 3. Exclusive-or Dn with C to create Xn */
205 /* 4. Select the first Ln bytes of Xn to create Pn */
206 crypto_xor(d + bsize, d, lastn);
207
208 /* 5. Append the tail (BB - Ln) bytes of Xn to Cn to create En */
209 memcpy(d + lastn, d + bsize + lastn, bsize - lastn);
210 /* 6. Decrypt En to create Pn-1 */
211
212 scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
213 memzero_explicit(d, sizeof(d));
214
215 skcipher_request_set_callback(subreq, req->base.flags &
216 CRYPTO_TFM_REQ_MAY_BACKLOG,
217 cts_cbc_crypt_done, req);
218
219 skcipher_request_set_crypt(subreq, sg, sg, bsize, space);
220 return crypto_skcipher_decrypt(subreq);
221}
222
223static void crypto_cts_decrypt_done(struct crypto_async_request *areq, int err)
224{
225 struct skcipher_request *req = areq->data;
226
227 if (err)
228 goto out;
229
230 err = cts_cbc_decrypt(req);
231 if (err == -EINPROGRESS ||
232 (err == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
233 return;
234
235out:
236 skcipher_request_complete(req, err);
237}
238
239static int crypto_cts_decrypt(struct skcipher_request *req)
240{
241 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
242 struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
243 struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
244 struct skcipher_request *subreq = &rctx->subreq;
245 int bsize = crypto_skcipher_blocksize(tfm);
246 unsigned int nbytes = req->cryptlen;
247 int cbc_blocks = (nbytes + bsize - 1) / bsize - 1;
248 unsigned int offset;
249 u8 *space;
250
251 skcipher_request_set_tfm(subreq, ctx->child);
252
253 if (cbc_blocks <= 0) {
254 skcipher_request_set_callback(subreq, req->base.flags,
255 req->base.complete,
256 req->base.data);
257 skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
258 req->iv);
259 return crypto_skcipher_decrypt(subreq);
260 }
261
262 skcipher_request_set_callback(subreq, req->base.flags,
263 crypto_cts_decrypt_done, req);
264
265 space = crypto_cts_reqctx_space(req);
266
267 offset = cbc_blocks * bsize;
268 rctx->offset = offset;
269
270 if (cbc_blocks <= 1)
271 memcpy(space, req->iv, bsize);
272 else
273 scatterwalk_map_and_copy(space, req->src, offset - 2 * bsize,
274 bsize, 0);
275
276 skcipher_request_set_crypt(subreq, req->src, req->dst,
277 offset, req->iv);
278
279 return crypto_skcipher_decrypt(subreq) ?:
280 cts_cbc_decrypt(req);
281}
282
283static int crypto_cts_init_tfm(struct crypto_skcipher *tfm)
284{
285 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
286 struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
287 struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
288 struct crypto_skcipher *cipher;
289 unsigned reqsize;
290 unsigned bsize;
291 unsigned align;
292
Eric Biggers60425a82016-10-28 09:52:19 -0700293 cipher = crypto_spawn_skcipher(spawn);
Kevin Coffman76cb9522008-03-24 21:26:16 +0800294 if (IS_ERR(cipher))
295 return PTR_ERR(cipher);
296
297 ctx->child = cipher;
Herbert Xu0605c412016-07-12 13:17:48 +0800298
299 align = crypto_skcipher_alignmask(tfm);
300 bsize = crypto_skcipher_blocksize(cipher);
301 reqsize = ALIGN(sizeof(struct crypto_cts_reqctx) +
302 crypto_skcipher_reqsize(cipher),
303 crypto_tfm_ctx_alignment()) +
304 (align & ~(crypto_tfm_ctx_alignment() - 1)) + bsize;
305
306 crypto_skcipher_set_reqsize(tfm, reqsize);
307
Kevin Coffman76cb9522008-03-24 21:26:16 +0800308 return 0;
309}
310
Herbert Xu0605c412016-07-12 13:17:48 +0800311static void crypto_cts_exit_tfm(struct crypto_skcipher *tfm)
Kevin Coffman76cb9522008-03-24 21:26:16 +0800312{
Herbert Xu0605c412016-07-12 13:17:48 +0800313 struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
314
315 crypto_free_skcipher(ctx->child);
Kevin Coffman76cb9522008-03-24 21:26:16 +0800316}
317
Herbert Xu0605c412016-07-12 13:17:48 +0800318static void crypto_cts_free(struct skcipher_instance *inst)
Kevin Coffman76cb9522008-03-24 21:26:16 +0800319{
Herbert Xu0605c412016-07-12 13:17:48 +0800320 crypto_drop_skcipher(skcipher_instance_ctx(inst));
321 kfree(inst);
322}
323
324static int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb)
325{
326 struct crypto_skcipher_spawn *spawn;
327 struct skcipher_instance *inst;
328 struct crypto_attr_type *algt;
329 struct skcipher_alg *alg;
330 const char *cipher_name;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800331 int err;
332
Herbert Xu0605c412016-07-12 13:17:48 +0800333 algt = crypto_get_attr_type(tb);
334 if (IS_ERR(algt))
335 return PTR_ERR(algt);
336
337 if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
338 return -EINVAL;
339
340 cipher_name = crypto_attr_alg_name(tb[1]);
341 if (IS_ERR(cipher_name))
342 return PTR_ERR(cipher_name);
343
344 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
345 if (!inst)
346 return -ENOMEM;
347
348 spawn = skcipher_instance_ctx(inst);
349
350 crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
Eric Biggersa35528e2016-10-28 09:51:13 -0700351 err = crypto_grab_skcipher(spawn, cipher_name, 0,
352 crypto_requires_sync(algt->type,
353 algt->mask));
Kevin Coffman76cb9522008-03-24 21:26:16 +0800354 if (err)
Herbert Xu0605c412016-07-12 13:17:48 +0800355 goto err_free_inst;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800356
Herbert Xu0605c412016-07-12 13:17:48 +0800357 alg = crypto_spawn_skcipher_alg(spawn);
Kevin Coffman76cb9522008-03-24 21:26:16 +0800358
Herbert Xu0605c412016-07-12 13:17:48 +0800359 err = -EINVAL;
360 if (crypto_skcipher_alg_ivsize(alg) != alg->base.cra_blocksize)
361 goto err_drop_spawn;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800362
Herbert Xu0605c412016-07-12 13:17:48 +0800363 if (strncmp(alg->base.cra_name, "cbc(", 4))
364 goto err_drop_spawn;
Herbert Xu988dc012015-01-16 19:38:17 +1100365
Herbert Xu0605c412016-07-12 13:17:48 +0800366 err = crypto_inst_setname(skcipher_crypto_instance(inst), "cts",
367 &alg->base);
368 if (err)
369 goto err_drop_spawn;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800370
Herbert Xu0605c412016-07-12 13:17:48 +0800371 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
372 inst->alg.base.cra_priority = alg->base.cra_priority;
373 inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
374 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800375
376 /* We access the data as u32s when xoring. */
Herbert Xu0605c412016-07-12 13:17:48 +0800377 inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800378
Herbert Xu0605c412016-07-12 13:17:48 +0800379 inst->alg.ivsize = alg->base.cra_blocksize;
380 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
381 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
382 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
Kevin Coffman76cb9522008-03-24 21:26:16 +0800383
Herbert Xu0605c412016-07-12 13:17:48 +0800384 inst->alg.base.cra_ctxsize = sizeof(struct crypto_cts_ctx);
Kevin Coffman76cb9522008-03-24 21:26:16 +0800385
Herbert Xu0605c412016-07-12 13:17:48 +0800386 inst->alg.init = crypto_cts_init_tfm;
387 inst->alg.exit = crypto_cts_exit_tfm;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800388
Herbert Xu0605c412016-07-12 13:17:48 +0800389 inst->alg.setkey = crypto_cts_setkey;
390 inst->alg.encrypt = crypto_cts_encrypt;
391 inst->alg.decrypt = crypto_cts_decrypt;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800392
Herbert Xu0605c412016-07-12 13:17:48 +0800393 inst->free = crypto_cts_free;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800394
Herbert Xu0605c412016-07-12 13:17:48 +0800395 err = skcipher_register_instance(tmpl, inst);
396 if (err)
397 goto err_drop_spawn;
398
399out:
400 return err;
401
402err_drop_spawn:
403 crypto_drop_skcipher(spawn);
404err_free_inst:
Kevin Coffman76cb9522008-03-24 21:26:16 +0800405 kfree(inst);
Herbert Xu0605c412016-07-12 13:17:48 +0800406 goto out;
Kevin Coffman76cb9522008-03-24 21:26:16 +0800407}
408
409static struct crypto_template crypto_cts_tmpl = {
410 .name = "cts",
Herbert Xu0605c412016-07-12 13:17:48 +0800411 .create = crypto_cts_create,
Kevin Coffman76cb9522008-03-24 21:26:16 +0800412 .module = THIS_MODULE,
413};
414
415static int __init crypto_cts_module_init(void)
416{
417 return crypto_register_template(&crypto_cts_tmpl);
418}
419
420static void __exit crypto_cts_module_exit(void)
421{
422 crypto_unregister_template(&crypto_cts_tmpl);
423}
424
425module_init(crypto_cts_module_init);
426module_exit(crypto_cts_module_exit);
427
428MODULE_LICENSE("Dual BSD/GPL");
429MODULE_DESCRIPTION("CTS-CBC CipherText Stealing for CBC");
Kees Cook4943ba12014-11-24 16:32:38 -0800430MODULE_ALIAS_CRYPTO("cts");