b013d6fec1ebd3a7afade4234663f61ba8736bb9
[deliverable/linux.git] / crypto / cbc.c
1 /*
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
21 struct crypto_cbc_ctx {
22 struct crypto_cipher *child;
23 };
24
25 static 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
41 static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc,
42 struct blkcipher_walk *walk,
43 struct crypto_cipher *tfm)
44 {
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 {
54 crypto_xor(iv, src, bsize);
55 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
65 static int crypto_cbc_encrypt_inplace(struct blkcipher_desc *desc,
66 struct blkcipher_walk *walk,
67 struct crypto_cipher *tfm)
68 {
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 {
77 crypto_xor(src, iv, bsize);
78 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
89 static 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;
97 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)
104 nbytes = crypto_cbc_encrypt_inplace(desc, &walk, child);
105 else
106 nbytes = crypto_cbc_encrypt_segment(desc, &walk, child);
107 err = blkcipher_walk_done(desc, &walk, nbytes);
108 }
109
110 return err;
111 }
112
113 static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc,
114 struct blkcipher_walk *walk,
115 struct crypto_cipher *tfm)
116 {
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);
127 crypto_xor(dst, iv, bsize);
128 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
139 static int crypto_cbc_decrypt_inplace(struct blkcipher_desc *desc,
140 struct blkcipher_walk *walk,
141 struct crypto_cipher *tfm)
142 {
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;
162 crypto_xor(src, src - bsize, bsize);
163 src -= bsize;
164 }
165
166 crypto_xor(src, first_iv, bsize);
167
168 return nbytes;
169 }
170
171 static 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;
179 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)
186 nbytes = crypto_cbc_decrypt_inplace(desc, &walk, child);
187 else
188 nbytes = crypto_cbc_decrypt_segment(desc, &walk, child);
189 err = blkcipher_walk_done(desc, &walk, nbytes);
190 }
191
192 return err;
193 }
194
195 static 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);
200 struct crypto_cipher *cipher;
201
202 cipher = crypto_spawn_cipher(spawn);
203 if (IS_ERR(cipher))
204 return PTR_ERR(cipher);
205
206 ctx->child = cipher;
207 return 0;
208 }
209
210 static 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
216 static struct crypto_instance *crypto_cbc_alloc(struct rtattr **tb)
217 {
218 struct crypto_instance *inst;
219 struct crypto_alg *alg;
220 int err;
221
222 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);
228 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
241 /* We access the data as u32s when xoring. */
242 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
243
244 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
257 out_put_alg:
258 crypto_mod_put(alg);
259 return inst;
260 }
261
262 static void crypto_cbc_free(struct crypto_instance *inst)
263 {
264 crypto_drop_spawn(crypto_instance_ctx(inst));
265 kfree(inst);
266 }
267
268 static 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
275 static int __init crypto_cbc_module_init(void)
276 {
277 return crypto_register_template(&crypto_cbc_tmpl);
278 }
279
280 static void __exit crypto_cbc_module_exit(void)
281 {
282 crypto_unregister_template(&crypto_cbc_tmpl);
283 }
284
285 module_init(crypto_cbc_module_init);
286 module_exit(crypto_cbc_module_exit);
287
288 MODULE_LICENSE("GPL");
289 MODULE_DESCRIPTION("CBC block cipher algorithm");
This page took 0.121468 seconds and 5 git commands to generate.