2 * CBC: Cipher Block Chaining mode
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
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)
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>
21 struct crypto_cbc_ctx {
22 struct crypto_cipher *child;
25 static int crypto_cbc_setkey(struct crypto_tfm *parent, const u8 *key,
28 struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(parent);
29 struct crypto_cipher *child = ctx->child;
32 crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
33 crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
35 err = crypto_cipher_setkey(child, key, keylen);
36 crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
41 static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc,
42 struct blkcipher_walk *walk,
43 struct crypto_cipher *tfm)
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;
54 crypto_xor(iv, src, bsize);
55 fn(crypto_cipher_tfm(tfm), dst, iv);
56 memcpy(iv, dst, bsize);
60 } while ((nbytes -= bsize) >= bsize);
65 static int crypto_cbc_encrypt_inplace(struct blkcipher_desc *desc,
66 struct blkcipher_walk *walk,
67 struct crypto_cipher *tfm)
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;
77 crypto_xor(src, iv, bsize);
78 fn(crypto_cipher_tfm(tfm), src, src);
82 } while ((nbytes -= bsize) >= bsize);
84 memcpy(walk->iv, iv, bsize);
89 static int crypto_cbc_encrypt(struct blkcipher_desc *desc,
90 struct scatterlist *dst, struct scatterlist *src,
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;
99 blkcipher_walk_init(&walk, dst, src, nbytes);
100 err = blkcipher_walk_virt(desc, &walk);
102 while ((nbytes = walk.nbytes)) {
103 if (walk.src.virt.addr == walk.dst.virt.addr)
104 nbytes = crypto_cbc_encrypt_inplace(desc, &walk, child);
106 nbytes = crypto_cbc_encrypt_segment(desc, &walk, child);
107 err = blkcipher_walk_done(desc, &walk, nbytes);
113 static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc,
114 struct blkcipher_walk *walk,
115 struct crypto_cipher *tfm)
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;
126 fn(crypto_cipher_tfm(tfm), dst, src);
127 crypto_xor(dst, iv, bsize);
132 } while ((nbytes -= bsize) >= bsize);
134 memcpy(walk->iv, iv, bsize);
139 static int crypto_cbc_decrypt_inplace(struct blkcipher_desc *desc,
140 struct blkcipher_walk *walk,
141 struct crypto_cipher *tfm)
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);
152 memcpy(first_iv, walk->iv, bsize);
154 /* Start of the last block. */
155 src += nbytes - nbytes % bsize - bsize;
156 memcpy(walk->iv, src, bsize);
159 fn(crypto_cipher_tfm(tfm), src, src);
160 if ((nbytes -= bsize) < bsize)
162 crypto_xor(src, src - bsize, bsize);
166 crypto_xor(src, first_iv, bsize);
171 static int crypto_cbc_decrypt(struct blkcipher_desc *desc,
172 struct scatterlist *dst, struct scatterlist *src,
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;
181 blkcipher_walk_init(&walk, dst, src, nbytes);
182 err = blkcipher_walk_virt(desc, &walk);
184 while ((nbytes = walk.nbytes)) {
185 if (walk.src.virt.addr == walk.dst.virt.addr)
186 nbytes = crypto_cbc_decrypt_inplace(desc, &walk, child);
188 nbytes = crypto_cbc_decrypt_segment(desc, &walk, child);
189 err = blkcipher_walk_done(desc, &walk, nbytes);
195 static int crypto_cbc_init_tfm(struct crypto_tfm *tfm)
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;
202 cipher = crypto_spawn_cipher(spawn);
204 return PTR_ERR(cipher);
210 static void crypto_cbc_exit_tfm(struct crypto_tfm *tfm)
212 struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
213 crypto_free_cipher(ctx->child);
216 static struct crypto_instance *crypto_cbc_alloc(struct rtattr **tb)
218 struct crypto_instance *inst;
219 struct crypto_alg *alg;
222 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
226 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
227 CRYPTO_ALG_TYPE_MASK);
229 return ERR_PTR(PTR_ERR(alg));
231 inst = crypto_alloc_instance("cbc", alg);
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;
241 /* We access the data as u32s when xoring. */
242 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
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;
248 inst->alg.cra_ctxsize = sizeof(struct crypto_cbc_ctx);
250 inst->alg.cra_init = crypto_cbc_init_tfm;
251 inst->alg.cra_exit = crypto_cbc_exit_tfm;
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;
262 static void crypto_cbc_free(struct crypto_instance *inst)
264 crypto_drop_spawn(crypto_instance_ctx(inst));
268 static struct crypto_template crypto_cbc_tmpl = {
270 .alloc = crypto_cbc_alloc,
271 .free = crypto_cbc_free,
272 .module = THIS_MODULE,
275 static int __init crypto_cbc_module_init(void)
277 return crypto_register_template(&crypto_cbc_tmpl);
280 static void __exit crypto_cbc_module_exit(void)
282 crypto_unregister_template(&crypto_cbc_tmpl);
285 module_init(crypto_cbc_module_init);
286 module_exit(crypto_cbc_module_exit);
288 MODULE_LICENSE("GPL");
289 MODULE_DESCRIPTION("CBC block cipher algorithm");