2 * PCBC: Propagating Cipher Block Chaining mode
4 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
17 #include <crypto/algapi.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/scatterlist.h>
23 #include <linux/slab.h>
25 struct crypto_pcbc_ctx {
26 struct crypto_cipher *child;
27 void (*xor)(u8 *dst, const u8 *src, unsigned int bs);
30 static int crypto_pcbc_setkey(struct crypto_tfm *parent, const u8 *key,
33 struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(parent);
34 struct crypto_cipher *child = ctx->child;
37 crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
38 crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
40 err = crypto_cipher_setkey(child, key, keylen);
41 crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
46 static int crypto_pcbc_encrypt_segment(struct blkcipher_desc *desc,
47 struct blkcipher_walk *walk,
48 struct crypto_cipher *tfm,
49 void (*xor)(u8 *, const u8 *,
52 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
53 crypto_cipher_alg(tfm)->cia_encrypt;
54 int bsize = crypto_cipher_blocksize(tfm);
55 unsigned int nbytes = walk->nbytes;
56 u8 *src = walk->src.virt.addr;
57 u8 *dst = walk->dst.virt.addr;
62 fn(crypto_cipher_tfm(tfm), dst, iv);
63 memcpy(iv, dst, bsize);
68 } while ((nbytes -= bsize) >= bsize);
73 static int crypto_pcbc_encrypt_inplace(struct blkcipher_desc *desc,
74 struct blkcipher_walk *walk,
75 struct crypto_cipher *tfm,
76 void (*xor)(u8 *, const u8 *,
79 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
80 crypto_cipher_alg(tfm)->cia_encrypt;
81 int bsize = crypto_cipher_blocksize(tfm);
82 unsigned int nbytes = walk->nbytes;
83 u8 *src = walk->src.virt.addr;
88 memcpy(tmpbuf, src, bsize);
89 xor(iv, tmpbuf, bsize);
90 fn(crypto_cipher_tfm(tfm), src, iv);
91 memcpy(iv, src, bsize);
92 xor(iv, tmpbuf, bsize);
95 } while ((nbytes -= bsize) >= bsize);
97 memcpy(walk->iv, iv, bsize);
102 static int crypto_pcbc_encrypt(struct blkcipher_desc *desc,
103 struct scatterlist *dst, struct scatterlist *src,
106 struct blkcipher_walk walk;
107 struct crypto_blkcipher *tfm = desc->tfm;
108 struct crypto_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
109 struct crypto_cipher *child = ctx->child;
110 void (*xor)(u8 *, const u8 *, unsigned int bs) = ctx->xor;
113 blkcipher_walk_init(&walk, dst, src, nbytes);
114 err = blkcipher_walk_virt(desc, &walk);
116 while ((nbytes = walk.nbytes)) {
117 if (walk.src.virt.addr == walk.dst.virt.addr)
118 nbytes = crypto_pcbc_encrypt_inplace(desc, &walk, child,
121 nbytes = crypto_pcbc_encrypt_segment(desc, &walk, child,
123 err = blkcipher_walk_done(desc, &walk, nbytes);
129 static int crypto_pcbc_decrypt_segment(struct blkcipher_desc *desc,
130 struct blkcipher_walk *walk,
131 struct crypto_cipher *tfm,
132 void (*xor)(u8 *, const u8 *,
135 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
136 crypto_cipher_alg(tfm)->cia_decrypt;
137 int bsize = crypto_cipher_blocksize(tfm);
138 unsigned int nbytes = walk->nbytes;
139 u8 *src = walk->src.virt.addr;
140 u8 *dst = walk->dst.virt.addr;
144 fn(crypto_cipher_tfm(tfm), dst, src);
146 memcpy(iv, src, bsize);
151 } while ((nbytes -= bsize) >= bsize);
153 memcpy(walk->iv, iv, bsize);
158 static int crypto_pcbc_decrypt_inplace(struct blkcipher_desc *desc,
159 struct blkcipher_walk *walk,
160 struct crypto_cipher *tfm,
161 void (*xor)(u8 *, const u8 *,
164 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
165 crypto_cipher_alg(tfm)->cia_decrypt;
166 int bsize = crypto_cipher_blocksize(tfm);
167 unsigned int nbytes = walk->nbytes;
168 u8 *src = walk->src.virt.addr;
173 memcpy(tmpbuf, src, bsize);
174 fn(crypto_cipher_tfm(tfm), src, src);
176 memcpy(iv, tmpbuf, bsize);
180 } while ((nbytes -= bsize) >= bsize);
182 memcpy(walk->iv, iv, bsize);
187 static int crypto_pcbc_decrypt(struct blkcipher_desc *desc,
188 struct scatterlist *dst, struct scatterlist *src,
191 struct blkcipher_walk walk;
192 struct crypto_blkcipher *tfm = desc->tfm;
193 struct crypto_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
194 struct crypto_cipher *child = ctx->child;
195 void (*xor)(u8 *, const u8 *, unsigned int bs) = ctx->xor;
198 blkcipher_walk_init(&walk, dst, src, nbytes);
199 err = blkcipher_walk_virt(desc, &walk);
201 while ((nbytes = walk.nbytes)) {
202 if (walk.src.virt.addr == walk.dst.virt.addr)
203 nbytes = crypto_pcbc_decrypt_inplace(desc, &walk, child,
206 nbytes = crypto_pcbc_decrypt_segment(desc, &walk, child,
208 err = blkcipher_walk_done(desc, &walk, nbytes);
214 static void xor_byte(u8 *a, const u8 *b, unsigned int bs)
221 static void xor_quad(u8 *dst, const u8 *src, unsigned int bs)
231 static void xor_64(u8 *a, const u8 *b, unsigned int bs)
233 ((u32 *)a)[0] ^= ((u32 *)b)[0];
234 ((u32 *)a)[1] ^= ((u32 *)b)[1];
237 static void xor_128(u8 *a, const u8 *b, unsigned int bs)
239 ((u32 *)a)[0] ^= ((u32 *)b)[0];
240 ((u32 *)a)[1] ^= ((u32 *)b)[1];
241 ((u32 *)a)[2] ^= ((u32 *)b)[2];
242 ((u32 *)a)[3] ^= ((u32 *)b)[3];
245 static int crypto_pcbc_init_tfm(struct crypto_tfm *tfm)
247 struct crypto_instance *inst = (void *)tfm->__crt_alg;
248 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
249 struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm);
250 struct crypto_cipher *cipher;
252 switch (crypto_tfm_alg_blocksize(tfm)) {
262 if (crypto_tfm_alg_blocksize(tfm) % 4)
268 cipher = crypto_spawn_cipher(spawn);
270 return PTR_ERR(cipher);
276 static void crypto_pcbc_exit_tfm(struct crypto_tfm *tfm)
278 struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm);
279 crypto_free_cipher(ctx->child);
282 static struct crypto_instance *crypto_pcbc_alloc(void *param, unsigned int len)
284 struct crypto_instance *inst;
285 struct crypto_alg *alg;
287 alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
288 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
290 return ERR_PTR(PTR_ERR(alg));
292 inst = crypto_alloc_instance("pcbc", alg);
296 inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
297 inst->alg.cra_priority = alg->cra_priority;
298 inst->alg.cra_blocksize = alg->cra_blocksize;
299 inst->alg.cra_alignmask = alg->cra_alignmask;
300 inst->alg.cra_type = &crypto_blkcipher_type;
302 if (!(alg->cra_blocksize % 4))
303 inst->alg.cra_alignmask |= 3;
304 inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
305 inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
306 inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
308 inst->alg.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
310 inst->alg.cra_init = crypto_pcbc_init_tfm;
311 inst->alg.cra_exit = crypto_pcbc_exit_tfm;
313 inst->alg.cra_blkcipher.setkey = crypto_pcbc_setkey;
314 inst->alg.cra_blkcipher.encrypt = crypto_pcbc_encrypt;
315 inst->alg.cra_blkcipher.decrypt = crypto_pcbc_decrypt;
322 static void crypto_pcbc_free(struct crypto_instance *inst)
324 crypto_drop_spawn(crypto_instance_ctx(inst));
328 static struct crypto_template crypto_pcbc_tmpl = {
330 .alloc = crypto_pcbc_alloc,
331 .free = crypto_pcbc_free,
332 .module = THIS_MODULE,
335 static int __init crypto_pcbc_module_init(void)
337 return crypto_register_template(&crypto_pcbc_tmpl);
340 static void __exit crypto_pcbc_module_exit(void)
342 crypto_unregister_template(&crypto_pcbc_tmpl);
345 module_init(crypto_pcbc_module_init);
346 module_exit(crypto_pcbc_module_exit);
348 MODULE_LICENSE("GPL");
349 MODULE_DESCRIPTION("PCBC block cipher algorithm");