blob: ef802f6e964218f06d00b035fc66d960cd4ce700 [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
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020017#include <crypto/algapi.h>
Herbert Xu043a4402016-11-22 20:08:27 +080018#include <crypto/internal/skcipher.h>
David Howells91652be2006-12-16 12:09:02 +110019#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
David Howells91652be2006-12-16 12:09:02 +110023#include <linux/slab.h>
Gideon Israel Dsouzad8c34b92016-12-31 21:26:23 +053024#include <linux/compiler.h>
David Howells91652be2006-12-16 12:09:02 +110025
26struct crypto_pcbc_ctx {
27 struct crypto_cipher *child;
David Howells91652be2006-12-16 12:09:02 +110028};
29
Herbert Xu043a4402016-11-22 20:08:27 +080030static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key,
David Howells91652be2006-12-16 12:09:02 +110031 unsigned int keylen)
32{
Herbert Xu043a4402016-11-22 20:08:27 +080033 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent);
David Howells91652be2006-12-16 12:09:02 +110034 struct crypto_cipher *child = ctx->child;
35 int err;
36
37 crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
Herbert Xu043a4402016-11-22 20:08:27 +080038 crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
39 CRYPTO_TFM_REQ_MASK);
David Howells91652be2006-12-16 12:09:02 +110040 err = crypto_cipher_setkey(child, key, keylen);
Herbert Xu043a4402016-11-22 20:08:27 +080041 crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
42 CRYPTO_TFM_RES_MASK);
David Howells91652be2006-12-16 12:09:02 +110043 return err;
44}
45
Herbert Xu043a4402016-11-22 20:08:27 +080046static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
47 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +080048 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +110049{
David Howells91652be2006-12-16 12:09:02 +110050 int bsize = crypto_cipher_blocksize(tfm);
51 unsigned int nbytes = walk->nbytes;
52 u8 *src = walk->src.virt.addr;
53 u8 *dst = walk->dst.virt.addr;
54 u8 *iv = walk->iv;
55
56 do {
Herbert Xud0b90072007-11-20 17:49:49 +080057 crypto_xor(iv, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +080058 crypto_cipher_encrypt_one(tfm, dst, iv);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +010059 crypto_xor_cpy(iv, dst, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110060
61 src += bsize;
62 dst += bsize;
63 } while ((nbytes -= bsize) >= bsize);
64
65 return nbytes;
66}
67
Herbert Xu043a4402016-11-22 20:08:27 +080068static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
69 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +080070 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +110071{
David Howells91652be2006-12-16 12:09:02 +110072 int bsize = crypto_cipher_blocksize(tfm);
73 unsigned int nbytes = walk->nbytes;
74 u8 *src = walk->src.virt.addr;
75 u8 *iv = walk->iv;
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020076 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE];
David Howells91652be2006-12-16 12:09:02 +110077
78 do {
79 memcpy(tmpbuf, src, bsize);
Herbert Xud0b90072007-11-20 17:49:49 +080080 crypto_xor(iv, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +080081 crypto_cipher_encrypt_one(tfm, src, iv);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +010082 crypto_xor_cpy(iv, tmpbuf, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110083
84 src += bsize;
85 } while ((nbytes -= bsize) >= bsize);
86
87 memcpy(walk->iv, iv, bsize);
88
89 return nbytes;
90}
91
Herbert Xu043a4402016-11-22 20:08:27 +080092static int crypto_pcbc_encrypt(struct skcipher_request *req)
David Howells91652be2006-12-16 12:09:02 +110093{
Herbert Xu043a4402016-11-22 20:08:27 +080094 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
95 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
David Howells91652be2006-12-16 12:09:02 +110096 struct crypto_cipher *child = ctx->child;
Herbert Xu043a4402016-11-22 20:08:27 +080097 struct skcipher_walk walk;
98 unsigned int nbytes;
David Howells91652be2006-12-16 12:09:02 +110099 int err;
100
Herbert Xu043a4402016-11-22 20:08:27 +0800101 err = skcipher_walk_virt(&walk, req, false);
David Howells91652be2006-12-16 12:09:02 +1100102
103 while ((nbytes = walk.nbytes)) {
104 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu043a4402016-11-22 20:08:27 +0800105 nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800106 child);
David Howells91652be2006-12-16 12:09:02 +1100107 else
Herbert Xu043a4402016-11-22 20:08:27 +0800108 nbytes = crypto_pcbc_encrypt_segment(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800109 child);
Herbert Xu043a4402016-11-22 20:08:27 +0800110 err = skcipher_walk_done(&walk, nbytes);
David Howells91652be2006-12-16 12:09:02 +1100111 }
112
113 return err;
114}
115
Herbert Xu043a4402016-11-22 20:08:27 +0800116static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
117 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800118 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100119{
David Howells91652be2006-12-16 12:09:02 +1100120 int bsize = crypto_cipher_blocksize(tfm);
121 unsigned int nbytes = walk->nbytes;
122 u8 *src = walk->src.virt.addr;
123 u8 *dst = walk->dst.virt.addr;
124 u8 *iv = walk->iv;
125
126 do {
Herbert Xu043a4402016-11-22 20:08:27 +0800127 crypto_cipher_decrypt_one(tfm, dst, src);
Herbert Xud0b90072007-11-20 17:49:49 +0800128 crypto_xor(dst, iv, bsize);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100129 crypto_xor_cpy(iv, dst, src, bsize);
David Howells91652be2006-12-16 12:09:02 +1100130
131 src += bsize;
132 dst += bsize;
133 } while ((nbytes -= bsize) >= bsize);
134
135 memcpy(walk->iv, iv, bsize);
136
137 return nbytes;
138}
139
Herbert Xu043a4402016-11-22 20:08:27 +0800140static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
141 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800142 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100143{
David Howells91652be2006-12-16 12:09:02 +1100144 int bsize = crypto_cipher_blocksize(tfm);
145 unsigned int nbytes = walk->nbytes;
146 u8 *src = walk->src.virt.addr;
147 u8 *iv = walk->iv;
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +0200148 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32));
David Howells91652be2006-12-16 12:09:02 +1100149
150 do {
151 memcpy(tmpbuf, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +0800152 crypto_cipher_decrypt_one(tfm, src, src);
Herbert Xud0b90072007-11-20 17:49:49 +0800153 crypto_xor(src, iv, bsize);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100154 crypto_xor_cpy(iv, src, tmpbuf, bsize);
David Howells91652be2006-12-16 12:09:02 +1100155
156 src += bsize;
157 } while ((nbytes -= bsize) >= bsize);
158
159 memcpy(walk->iv, iv, bsize);
160
161 return nbytes;
162}
163
Herbert Xu043a4402016-11-22 20:08:27 +0800164static int crypto_pcbc_decrypt(struct skcipher_request *req)
David Howells91652be2006-12-16 12:09:02 +1100165{
Herbert Xu043a4402016-11-22 20:08:27 +0800166 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
167 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
David Howells91652be2006-12-16 12:09:02 +1100168 struct crypto_cipher *child = ctx->child;
Herbert Xu043a4402016-11-22 20:08:27 +0800169 struct skcipher_walk walk;
170 unsigned int nbytes;
David Howells91652be2006-12-16 12:09:02 +1100171 int err;
172
Herbert Xu043a4402016-11-22 20:08:27 +0800173 err = skcipher_walk_virt(&walk, req, false);
David Howells91652be2006-12-16 12:09:02 +1100174
175 while ((nbytes = walk.nbytes)) {
176 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu043a4402016-11-22 20:08:27 +0800177 nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800178 child);
David Howells91652be2006-12-16 12:09:02 +1100179 else
Herbert Xu043a4402016-11-22 20:08:27 +0800180 nbytes = crypto_pcbc_decrypt_segment(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800181 child);
Herbert Xu043a4402016-11-22 20:08:27 +0800182 err = skcipher_walk_done(&walk, nbytes);
David Howells91652be2006-12-16 12:09:02 +1100183 }
184
185 return err;
186}
187
Herbert Xu043a4402016-11-22 20:08:27 +0800188static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100189{
Herbert Xu043a4402016-11-22 20:08:27 +0800190 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
191 struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
192 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
Herbert Xu2e306ee2006-12-17 10:05:58 +1100193 struct crypto_cipher *cipher;
David Howells91652be2006-12-16 12:09:02 +1100194
Herbert Xu2e306ee2006-12-17 10:05:58 +1100195 cipher = crypto_spawn_cipher(spawn);
196 if (IS_ERR(cipher))
197 return PTR_ERR(cipher);
David Howells91652be2006-12-16 12:09:02 +1100198
Herbert Xu2e306ee2006-12-17 10:05:58 +1100199 ctx->child = cipher;
David Howells91652be2006-12-16 12:09:02 +1100200 return 0;
201}
202
Herbert Xu043a4402016-11-22 20:08:27 +0800203static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100204{
Herbert Xu043a4402016-11-22 20:08:27 +0800205 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
206
David Howells91652be2006-12-16 12:09:02 +1100207 crypto_free_cipher(ctx->child);
208}
209
Herbert Xu043a4402016-11-22 20:08:27 +0800210static void crypto_pcbc_free(struct skcipher_instance *inst)
David Howells91652be2006-12-16 12:09:02 +1100211{
Herbert Xu043a4402016-11-22 20:08:27 +0800212 crypto_drop_skcipher(skcipher_instance_ctx(inst));
213 kfree(inst);
214}
215
216static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
217{
218 struct skcipher_instance *inst;
219 struct crypto_attr_type *algt;
220 struct crypto_spawn *spawn;
David Howells91652be2006-12-16 12:09:02 +1100221 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +1100222 int err;
David Howells91652be2006-12-16 12:09:02 +1100223
Herbert Xu043a4402016-11-22 20:08:27 +0800224 algt = crypto_get_attr_type(tb);
225 if (IS_ERR(algt))
226 return PTR_ERR(algt);
Herbert Xuebc610e2007-01-01 18:37:02 +1100227
Herbert Xu043a4402016-11-22 20:08:27 +0800228 if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) &
229 ~CRYPTO_ALG_INTERNAL)
230 return -EINVAL;
231
232 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
233 if (!inst)
234 return -ENOMEM;
235
236 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER |
237 (algt->type & CRYPTO_ALG_INTERNAL),
238 CRYPTO_ALG_TYPE_MASK |
239 (algt->mask & CRYPTO_ALG_INTERNAL));
240 err = PTR_ERR(alg);
David Howells91652be2006-12-16 12:09:02 +1100241 if (IS_ERR(alg))
Herbert Xu043a4402016-11-22 20:08:27 +0800242 goto err_free_inst;
David Howells91652be2006-12-16 12:09:02 +1100243
Herbert Xu043a4402016-11-22 20:08:27 +0800244 spawn = skcipher_instance_ctx(inst);
245 err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
246 CRYPTO_ALG_TYPE_MASK);
247 crypto_mod_put(alg);
248 if (err)
249 goto err_free_inst;
David Howells91652be2006-12-16 12:09:02 +1100250
Herbert Xu043a4402016-11-22 20:08:27 +0800251 err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg);
252 if (err)
253 goto err_drop_spawn;
254
255 inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL;
256 inst->alg.base.cra_priority = alg->cra_priority;
257 inst->alg.base.cra_blocksize = alg->cra_blocksize;
258 inst->alg.base.cra_alignmask = alg->cra_alignmask;
David Howells91652be2006-12-16 12:09:02 +1100259
Herbert Xu043a4402016-11-22 20:08:27 +0800260 inst->alg.ivsize = alg->cra_blocksize;
261 inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
262 inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
David Howells91652be2006-12-16 12:09:02 +1100263
Herbert Xu043a4402016-11-22 20:08:27 +0800264 inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
David Howells91652be2006-12-16 12:09:02 +1100265
Herbert Xu043a4402016-11-22 20:08:27 +0800266 inst->alg.init = crypto_pcbc_init_tfm;
267 inst->alg.exit = crypto_pcbc_exit_tfm;
David Howells91652be2006-12-16 12:09:02 +1100268
Herbert Xu043a4402016-11-22 20:08:27 +0800269 inst->alg.setkey = crypto_pcbc_setkey;
270 inst->alg.encrypt = crypto_pcbc_encrypt;
271 inst->alg.decrypt = crypto_pcbc_decrypt;
David Howells91652be2006-12-16 12:09:02 +1100272
Herbert Xu043a4402016-11-22 20:08:27 +0800273 inst->free = crypto_pcbc_free;
David Howells91652be2006-12-16 12:09:02 +1100274
Herbert Xu043a4402016-11-22 20:08:27 +0800275 err = skcipher_register_instance(tmpl, inst);
276 if (err)
277 goto err_drop_spawn;
278
279out:
280 return err;
281
282err_drop_spawn:
283 crypto_drop_spawn(spawn);
284err_free_inst:
David Howells91652be2006-12-16 12:09:02 +1100285 kfree(inst);
Herbert Xu043a4402016-11-22 20:08:27 +0800286 goto out;
David Howells91652be2006-12-16 12:09:02 +1100287}
288
289static struct crypto_template crypto_pcbc_tmpl = {
290 .name = "pcbc",
Herbert Xu043a4402016-11-22 20:08:27 +0800291 .create = crypto_pcbc_create,
David Howells91652be2006-12-16 12:09:02 +1100292 .module = THIS_MODULE,
293};
294
295static int __init crypto_pcbc_module_init(void)
296{
297 return crypto_register_template(&crypto_pcbc_tmpl);
298}
299
300static void __exit crypto_pcbc_module_exit(void)
301{
302 crypto_unregister_template(&crypto_pcbc_tmpl);
303}
304
305module_init(crypto_pcbc_module_init);
306module_exit(crypto_pcbc_module_exit);
307
308MODULE_LICENSE("GPL");
309MODULE_DESCRIPTION("PCBC block cipher algorithm");
Kees Cook4943ba12014-11-24 16:32:38 -0800310MODULE_ALIAS_CRYPTO("pcbc");