blob: de81f716cf26468bf10a696f2cfda1492c113d3e [file] [log] [blame]
David Howells91652be2006-12-16 12:09:02 +11001/*
2 * PCBC: Propagating Cipher Block Chaining mode
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 *
7 * Derived from cbc.c
8 * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
9 *
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)
13 * any later version.
14 *
15 */
16
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>
24
25struct crypto_pcbc_ctx {
26 struct crypto_cipher *child;
David Howells91652be2006-12-16 12:09:02 +110027};
28
29static int crypto_pcbc_setkey(struct crypto_tfm *parent, const u8 *key,
30 unsigned int keylen)
31{
32 struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(parent);
33 struct crypto_cipher *child = ctx->child;
34 int err;
35
36 crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
37 crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
38 CRYPTO_TFM_REQ_MASK);
39 err = crypto_cipher_setkey(child, key, keylen);
40 crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
41 CRYPTO_TFM_RES_MASK);
42 return err;
43}
44
45static int crypto_pcbc_encrypt_segment(struct blkcipher_desc *desc,
46 struct blkcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +080047 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +110048{
49 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
50 crypto_cipher_alg(tfm)->cia_encrypt;
51 int bsize = crypto_cipher_blocksize(tfm);
52 unsigned int nbytes = walk->nbytes;
53 u8 *src = walk->src.virt.addr;
54 u8 *dst = walk->dst.virt.addr;
Eric Biggersa329c152019-01-03 20:16:13 -080055 u8 * const iv = walk->iv;
David Howells91652be2006-12-16 12:09:02 +110056
57 do {
Herbert Xud0b90072007-11-20 17:49:49 +080058 crypto_xor(iv, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110059 fn(crypto_cipher_tfm(tfm), dst, iv);
60 memcpy(iv, dst, bsize);
Herbert Xud0b90072007-11-20 17:49:49 +080061 crypto_xor(iv, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110062
63 src += bsize;
64 dst += bsize;
65 } while ((nbytes -= bsize) >= bsize);
66
67 return nbytes;
68}
69
70static int crypto_pcbc_encrypt_inplace(struct blkcipher_desc *desc,
71 struct blkcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +080072 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +110073{
74 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
75 crypto_cipher_alg(tfm)->cia_encrypt;
76 int bsize = crypto_cipher_blocksize(tfm);
77 unsigned int nbytes = walk->nbytes;
78 u8 *src = walk->src.virt.addr;
Eric Biggersa329c152019-01-03 20:16:13 -080079 u8 * const iv = walk->iv;
David Howells91652be2006-12-16 12:09:02 +110080 u8 tmpbuf[bsize];
81
82 do {
83 memcpy(tmpbuf, src, bsize);
Herbert Xud0b90072007-11-20 17:49:49 +080084 crypto_xor(iv, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110085 fn(crypto_cipher_tfm(tfm), src, iv);
Herbert Xud0b90072007-11-20 17:49:49 +080086 memcpy(iv, tmpbuf, bsize);
87 crypto_xor(iv, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110088
89 src += bsize;
90 } while ((nbytes -= bsize) >= bsize);
91
David Howells91652be2006-12-16 12:09:02 +110092 return nbytes;
93}
94
95static int crypto_pcbc_encrypt(struct blkcipher_desc *desc,
96 struct scatterlist *dst, struct scatterlist *src,
97 unsigned int nbytes)
98{
99 struct blkcipher_walk walk;
100 struct crypto_blkcipher *tfm = desc->tfm;
101 struct crypto_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
102 struct crypto_cipher *child = ctx->child;
David Howells91652be2006-12-16 12:09:02 +1100103 int err;
104
105 blkcipher_walk_init(&walk, dst, src, nbytes);
106 err = blkcipher_walk_virt(desc, &walk);
107
108 while ((nbytes = walk.nbytes)) {
109 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xud0b90072007-11-20 17:49:49 +0800110 nbytes = crypto_pcbc_encrypt_inplace(desc, &walk,
111 child);
David Howells91652be2006-12-16 12:09:02 +1100112 else
Herbert Xud0b90072007-11-20 17:49:49 +0800113 nbytes = crypto_pcbc_encrypt_segment(desc, &walk,
114 child);
David Howells91652be2006-12-16 12:09:02 +1100115 err = blkcipher_walk_done(desc, &walk, nbytes);
116 }
117
118 return err;
119}
120
121static int crypto_pcbc_decrypt_segment(struct blkcipher_desc *desc,
122 struct blkcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800123 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100124{
125 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
126 crypto_cipher_alg(tfm)->cia_decrypt;
127 int bsize = crypto_cipher_blocksize(tfm);
128 unsigned int nbytes = walk->nbytes;
129 u8 *src = walk->src.virt.addr;
130 u8 *dst = walk->dst.virt.addr;
Eric Biggersa329c152019-01-03 20:16:13 -0800131 u8 * const iv = walk->iv;
David Howells91652be2006-12-16 12:09:02 +1100132
133 do {
134 fn(crypto_cipher_tfm(tfm), dst, src);
Herbert Xud0b90072007-11-20 17:49:49 +0800135 crypto_xor(dst, iv, bsize);
David Howells91652be2006-12-16 12:09:02 +1100136 memcpy(iv, src, bsize);
Herbert Xud0b90072007-11-20 17:49:49 +0800137 crypto_xor(iv, dst, bsize);
David Howells91652be2006-12-16 12:09:02 +1100138
139 src += bsize;
140 dst += bsize;
141 } while ((nbytes -= bsize) >= bsize);
142
David Howells91652be2006-12-16 12:09:02 +1100143 return nbytes;
144}
145
146static int crypto_pcbc_decrypt_inplace(struct blkcipher_desc *desc,
147 struct blkcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800148 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100149{
150 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
151 crypto_cipher_alg(tfm)->cia_decrypt;
152 int bsize = crypto_cipher_blocksize(tfm);
153 unsigned int nbytes = walk->nbytes;
154 u8 *src = walk->src.virt.addr;
Eric Biggersa329c152019-01-03 20:16:13 -0800155 u8 * const iv = walk->iv;
David Howells91652be2006-12-16 12:09:02 +1100156 u8 tmpbuf[bsize];
157
158 do {
159 memcpy(tmpbuf, src, bsize);
160 fn(crypto_cipher_tfm(tfm), src, src);
Herbert Xud0b90072007-11-20 17:49:49 +0800161 crypto_xor(src, iv, bsize);
David Howells91652be2006-12-16 12:09:02 +1100162 memcpy(iv, tmpbuf, bsize);
Herbert Xud0b90072007-11-20 17:49:49 +0800163 crypto_xor(iv, src, bsize);
David Howells91652be2006-12-16 12:09:02 +1100164
165 src += bsize;
166 } while ((nbytes -= bsize) >= bsize);
167
David Howells91652be2006-12-16 12:09:02 +1100168 return nbytes;
169}
170
171static int crypto_pcbc_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_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
178 struct crypto_cipher *child = ctx->child;
David Howells91652be2006-12-16 12:09:02 +1100179 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 Xud0b90072007-11-20 17:49:49 +0800186 nbytes = crypto_pcbc_decrypt_inplace(desc, &walk,
187 child);
David Howells91652be2006-12-16 12:09:02 +1100188 else
Herbert Xud0b90072007-11-20 17:49:49 +0800189 nbytes = crypto_pcbc_decrypt_segment(desc, &walk,
190 child);
David Howells91652be2006-12-16 12:09:02 +1100191 err = blkcipher_walk_done(desc, &walk, nbytes);
192 }
193
194 return err;
195}
196
David Howells91652be2006-12-16 12:09:02 +1100197static int crypto_pcbc_init_tfm(struct crypto_tfm *tfm)
198{
199 struct crypto_instance *inst = (void *)tfm->__crt_alg;
200 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
201 struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu2e306ee2006-12-17 10:05:58 +1100202 struct crypto_cipher *cipher;
David Howells91652be2006-12-16 12:09:02 +1100203
Herbert Xu2e306ee2006-12-17 10:05:58 +1100204 cipher = crypto_spawn_cipher(spawn);
205 if (IS_ERR(cipher))
206 return PTR_ERR(cipher);
David Howells91652be2006-12-16 12:09:02 +1100207
Herbert Xu2e306ee2006-12-17 10:05:58 +1100208 ctx->child = cipher;
David Howells91652be2006-12-16 12:09:02 +1100209 return 0;
210}
211
212static void crypto_pcbc_exit_tfm(struct crypto_tfm *tfm)
213{
214 struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm);
215 crypto_free_cipher(ctx->child);
216}
217
Herbert Xuebc610e2007-01-01 18:37:02 +1100218static struct crypto_instance *crypto_pcbc_alloc(struct rtattr **tb)
David Howells91652be2006-12-16 12:09:02 +1100219{
220 struct crypto_instance *inst;
221 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +1100222 int err;
David Howells91652be2006-12-16 12:09:02 +1100223
Herbert Xuebc610e2007-01-01 18:37:02 +1100224 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
225 if (err)
226 return ERR_PTR(err);
227
228 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
229 CRYPTO_ALG_TYPE_MASK);
David Howells91652be2006-12-16 12:09:02 +1100230 if (IS_ERR(alg))
David Howellse231c2e2008-02-07 00:15:26 -0800231 return ERR_CAST(alg);
David Howells91652be2006-12-16 12:09:02 +1100232
233 inst = crypto_alloc_instance("pcbc", alg);
234 if (IS_ERR(inst))
235 goto out_put_alg;
236
237 inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
238 inst->alg.cra_priority = alg->cra_priority;
239 inst->alg.cra_blocksize = alg->cra_blocksize;
240 inst->alg.cra_alignmask = alg->cra_alignmask;
241 inst->alg.cra_type = &crypto_blkcipher_type;
242
Herbert Xud0b90072007-11-20 17:49:49 +0800243 /* We access the data as u32s when xoring. */
244 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
245
David Howells91652be2006-12-16 12:09:02 +1100246 inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
247 inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
248 inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
249
250 inst->alg.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
251
252 inst->alg.cra_init = crypto_pcbc_init_tfm;
253 inst->alg.cra_exit = crypto_pcbc_exit_tfm;
254
255 inst->alg.cra_blkcipher.setkey = crypto_pcbc_setkey;
256 inst->alg.cra_blkcipher.encrypt = crypto_pcbc_encrypt;
257 inst->alg.cra_blkcipher.decrypt = crypto_pcbc_decrypt;
258
259out_put_alg:
260 crypto_mod_put(alg);
261 return inst;
262}
263
264static void crypto_pcbc_free(struct crypto_instance *inst)
265{
266 crypto_drop_spawn(crypto_instance_ctx(inst));
267 kfree(inst);
268}
269
270static struct crypto_template crypto_pcbc_tmpl = {
271 .name = "pcbc",
272 .alloc = crypto_pcbc_alloc,
273 .free = crypto_pcbc_free,
274 .module = THIS_MODULE,
275};
276
277static int __init crypto_pcbc_module_init(void)
278{
279 return crypto_register_template(&crypto_pcbc_tmpl);
280}
281
282static void __exit crypto_pcbc_module_exit(void)
283{
284 crypto_unregister_template(&crypto_pcbc_tmpl);
285}
286
287module_init(crypto_pcbc_module_init);
288module_exit(crypto_pcbc_module_exit);
289
290MODULE_LICENSE("GPL");
291MODULE_DESCRIPTION("PCBC block cipher algorithm");
Kees Cook4943ba12014-11-24 16:32:38 -0800292MODULE_ALIAS_CRYPTO("pcbc");