blob: b013d6fec1ebd3a7afade4234663f61ba8736bb9 [file] [log] [blame]
Herbert Xudb131ef2006-09-21 11:44:08 +10001/*
2 * CBC: Cipher Block Chaining mode
3 *
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <crypto/algapi.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/scatterlist.h>
19#include <linux/slab.h>
20
21struct crypto_cbc_ctx {
22 struct crypto_cipher *child;
Herbert Xudb131ef2006-09-21 11:44:08 +100023};
24
25static int crypto_cbc_setkey(struct crypto_tfm *parent, const u8 *key,
26 unsigned int keylen)
27{
28 struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(parent);
29 struct crypto_cipher *child = ctx->child;
30 int err;
31
32 crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
33 crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
34 CRYPTO_TFM_REQ_MASK);
35 err = crypto_cipher_setkey(child, key, keylen);
36 crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
37 CRYPTO_TFM_RES_MASK);
38 return err;
39}
40
41static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc,
42 struct blkcipher_walk *walk,
Herbert Xu3c7f0762007-11-20 17:33:39 +080043 struct crypto_cipher *tfm)
Herbert Xudb131ef2006-09-21 11:44:08 +100044{
45 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
46 crypto_cipher_alg(tfm)->cia_encrypt;
47 int bsize = crypto_cipher_blocksize(tfm);
48 unsigned int nbytes = walk->nbytes;
49 u8 *src = walk->src.virt.addr;
50 u8 *dst = walk->dst.virt.addr;
51 u8 *iv = walk->iv;
52
53 do {
Herbert Xu3c7f0762007-11-20 17:33:39 +080054 crypto_xor(iv, src, bsize);
Herbert Xudb131ef2006-09-21 11:44:08 +100055 fn(crypto_cipher_tfm(tfm), dst, iv);
56 memcpy(iv, dst, bsize);
57
58 src += bsize;
59 dst += bsize;
60 } while ((nbytes -= bsize) >= bsize);
61
62 return nbytes;
63}
64
65static int crypto_cbc_encrypt_inplace(struct blkcipher_desc *desc,
66 struct blkcipher_walk *walk,
Herbert Xu3c7f0762007-11-20 17:33:39 +080067 struct crypto_cipher *tfm)
Herbert Xudb131ef2006-09-21 11:44:08 +100068{
69 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
70 crypto_cipher_alg(tfm)->cia_encrypt;
71 int bsize = crypto_cipher_blocksize(tfm);
72 unsigned int nbytes = walk->nbytes;
73 u8 *src = walk->src.virt.addr;
74 u8 *iv = walk->iv;
75
76 do {
Herbert Xu3c7f0762007-11-20 17:33:39 +080077 crypto_xor(src, iv, bsize);
Herbert Xudb131ef2006-09-21 11:44:08 +100078 fn(crypto_cipher_tfm(tfm), src, src);
79 iv = src;
80
81 src += bsize;
82 } while ((nbytes -= bsize) >= bsize);
83
84 memcpy(walk->iv, iv, bsize);
85
86 return nbytes;
87}
88
89static int crypto_cbc_encrypt(struct blkcipher_desc *desc,
90 struct scatterlist *dst, struct scatterlist *src,
91 unsigned int nbytes)
92{
93 struct blkcipher_walk walk;
94 struct crypto_blkcipher *tfm = desc->tfm;
95 struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
96 struct crypto_cipher *child = ctx->child;
Herbert Xudb131ef2006-09-21 11:44:08 +100097 int err;
98
99 blkcipher_walk_init(&walk, dst, src, nbytes);
100 err = blkcipher_walk_virt(desc, &walk);
101
102 while ((nbytes = walk.nbytes)) {
103 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu3c7f0762007-11-20 17:33:39 +0800104 nbytes = crypto_cbc_encrypt_inplace(desc, &walk, child);
Herbert Xudb131ef2006-09-21 11:44:08 +1000105 else
Herbert Xu3c7f0762007-11-20 17:33:39 +0800106 nbytes = crypto_cbc_encrypt_segment(desc, &walk, child);
Herbert Xudb131ef2006-09-21 11:44:08 +1000107 err = blkcipher_walk_done(desc, &walk, nbytes);
108 }
109
110 return err;
111}
112
113static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc,
114 struct blkcipher_walk *walk,
Herbert Xu3c7f0762007-11-20 17:33:39 +0800115 struct crypto_cipher *tfm)
Herbert Xudb131ef2006-09-21 11:44:08 +1000116{
117 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
118 crypto_cipher_alg(tfm)->cia_decrypt;
119 int bsize = crypto_cipher_blocksize(tfm);
120 unsigned int nbytes = walk->nbytes;
121 u8 *src = walk->src.virt.addr;
122 u8 *dst = walk->dst.virt.addr;
123 u8 *iv = walk->iv;
124
125 do {
126 fn(crypto_cipher_tfm(tfm), dst, src);
Herbert Xu3c7f0762007-11-20 17:33:39 +0800127 crypto_xor(dst, iv, bsize);
Herbert Xudb131ef2006-09-21 11:44:08 +1000128 iv = src;
129
130 src += bsize;
131 dst += bsize;
132 } while ((nbytes -= bsize) >= bsize);
133
134 memcpy(walk->iv, iv, bsize);
135
136 return nbytes;
137}
138
139static int crypto_cbc_decrypt_inplace(struct blkcipher_desc *desc,
140 struct blkcipher_walk *walk,
Herbert Xu3c7f0762007-11-20 17:33:39 +0800141 struct crypto_cipher *tfm)
Herbert Xudb131ef2006-09-21 11:44:08 +1000142{
143 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
144 crypto_cipher_alg(tfm)->cia_decrypt;
145 int bsize = crypto_cipher_blocksize(tfm);
146 unsigned long alignmask = crypto_cipher_alignmask(tfm);
147 unsigned int nbytes = walk->nbytes;
148 u8 *src = walk->src.virt.addr;
149 u8 stack[bsize + alignmask];
150 u8 *first_iv = (u8 *)ALIGN((unsigned long)stack, alignmask + 1);
151
152 memcpy(first_iv, walk->iv, bsize);
153
154 /* Start of the last block. */
155 src += nbytes - nbytes % bsize - bsize;
156 memcpy(walk->iv, src, bsize);
157
158 for (;;) {
159 fn(crypto_cipher_tfm(tfm), src, src);
160 if ((nbytes -= bsize) < bsize)
161 break;
Herbert Xu3c7f0762007-11-20 17:33:39 +0800162 crypto_xor(src, src - bsize, bsize);
Herbert Xudb131ef2006-09-21 11:44:08 +1000163 src -= bsize;
164 }
165
Herbert Xu3c7f0762007-11-20 17:33:39 +0800166 crypto_xor(src, first_iv, bsize);
Herbert Xudb131ef2006-09-21 11:44:08 +1000167
168 return nbytes;
169}
170
171static int crypto_cbc_decrypt(struct blkcipher_desc *desc,
172 struct scatterlist *dst, struct scatterlist *src,
173 unsigned int nbytes)
174{
175 struct blkcipher_walk walk;
176 struct crypto_blkcipher *tfm = desc->tfm;
177 struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
178 struct crypto_cipher *child = ctx->child;
Herbert Xudb131ef2006-09-21 11:44:08 +1000179 int err;
180
181 blkcipher_walk_init(&walk, dst, src, nbytes);
182 err = blkcipher_walk_virt(desc, &walk);
183
184 while ((nbytes = walk.nbytes)) {
185 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu3c7f0762007-11-20 17:33:39 +0800186 nbytes = crypto_cbc_decrypt_inplace(desc, &walk, child);
Herbert Xudb131ef2006-09-21 11:44:08 +1000187 else
Herbert Xu3c7f0762007-11-20 17:33:39 +0800188 nbytes = crypto_cbc_decrypt_segment(desc, &walk, child);
Herbert Xudb131ef2006-09-21 11:44:08 +1000189 err = blkcipher_walk_done(desc, &walk, nbytes);
190 }
191
192 return err;
193}
194
Herbert Xudb131ef2006-09-21 11:44:08 +1000195static int crypto_cbc_init_tfm(struct crypto_tfm *tfm)
196{
197 struct crypto_instance *inst = (void *)tfm->__crt_alg;
198 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
199 struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu2e306ee2006-12-17 10:05:58 +1100200 struct crypto_cipher *cipher;
Herbert Xudb131ef2006-09-21 11:44:08 +1000201
Herbert Xu2e306ee2006-12-17 10:05:58 +1100202 cipher = crypto_spawn_cipher(spawn);
203 if (IS_ERR(cipher))
204 return PTR_ERR(cipher);
Herbert Xudb131ef2006-09-21 11:44:08 +1000205
Herbert Xu2e306ee2006-12-17 10:05:58 +1100206 ctx->child = cipher;
Herbert Xudb131ef2006-09-21 11:44:08 +1000207 return 0;
208}
209
210static void crypto_cbc_exit_tfm(struct crypto_tfm *tfm)
211{
212 struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
213 crypto_free_cipher(ctx->child);
214}
215
Herbert Xuebc610e2007-01-01 18:37:02 +1100216static struct crypto_instance *crypto_cbc_alloc(struct rtattr **tb)
Herbert Xudb131ef2006-09-21 11:44:08 +1000217{
218 struct crypto_instance *inst;
219 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +1100220 int err;
Herbert Xudb131ef2006-09-21 11:44:08 +1000221
Herbert Xuebc610e2007-01-01 18:37:02 +1100222 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
223 if (err)
224 return ERR_PTR(err);
225
226 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
227 CRYPTO_ALG_TYPE_MASK);
Herbert Xudb131ef2006-09-21 11:44:08 +1000228 if (IS_ERR(alg))
229 return ERR_PTR(PTR_ERR(alg));
230
231 inst = crypto_alloc_instance("cbc", alg);
232 if (IS_ERR(inst))
233 goto out_put_alg;
234
235 inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
236 inst->alg.cra_priority = alg->cra_priority;
237 inst->alg.cra_blocksize = alg->cra_blocksize;
238 inst->alg.cra_alignmask = alg->cra_alignmask;
239 inst->alg.cra_type = &crypto_blkcipher_type;
240
Herbert Xu3c7f0762007-11-20 17:33:39 +0800241 /* We access the data as u32s when xoring. */
242 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
243
Herbert Xudb131ef2006-09-21 11:44:08 +1000244 inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
245 inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
246 inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
247
248 inst->alg.cra_ctxsize = sizeof(struct crypto_cbc_ctx);
249
250 inst->alg.cra_init = crypto_cbc_init_tfm;
251 inst->alg.cra_exit = crypto_cbc_exit_tfm;
252
253 inst->alg.cra_blkcipher.setkey = crypto_cbc_setkey;
254 inst->alg.cra_blkcipher.encrypt = crypto_cbc_encrypt;
255 inst->alg.cra_blkcipher.decrypt = crypto_cbc_decrypt;
256
257out_put_alg:
258 crypto_mod_put(alg);
259 return inst;
260}
261
262static void crypto_cbc_free(struct crypto_instance *inst)
263{
264 crypto_drop_spawn(crypto_instance_ctx(inst));
265 kfree(inst);
266}
267
268static struct crypto_template crypto_cbc_tmpl = {
269 .name = "cbc",
270 .alloc = crypto_cbc_alloc,
271 .free = crypto_cbc_free,
272 .module = THIS_MODULE,
273};
274
275static int __init crypto_cbc_module_init(void)
276{
277 return crypto_register_template(&crypto_cbc_tmpl);
278}
279
280static void __exit crypto_cbc_module_exit(void)
281{
282 crypto_unregister_template(&crypto_cbc_tmpl);
283}
284
285module_init(crypto_cbc_module_init);
286module_exit(crypto_cbc_module_exit);
287
288MODULE_LICENSE("GPL");
289MODULE_DESCRIPTION("CBC block cipher algorithm");