blob: 1b182dfedc948688e7f79f10245ef88ea85eb508 [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;
Eric Biggersbb1ae0a2019-01-03 20:16:13 -080054 u8 * const iv = walk->iv;
David Howells91652be2006-12-16 12:09:02 +110055
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;
Eric Biggersbb1ae0a2019-01-03 20:16:13 -080075 u8 * const 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
David Howells91652be2006-12-16 12:09:02 +110087 return nbytes;
88}
89
Herbert Xu043a4402016-11-22 20:08:27 +080090static int crypto_pcbc_encrypt(struct skcipher_request *req)
David Howells91652be2006-12-16 12:09:02 +110091{
Herbert Xu043a4402016-11-22 20:08:27 +080092 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
93 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
David Howells91652be2006-12-16 12:09:02 +110094 struct crypto_cipher *child = ctx->child;
Herbert Xu043a4402016-11-22 20:08:27 +080095 struct skcipher_walk walk;
96 unsigned int nbytes;
David Howells91652be2006-12-16 12:09:02 +110097 int err;
98
Herbert Xu043a4402016-11-22 20:08:27 +080099 err = skcipher_walk_virt(&walk, req, false);
David Howells91652be2006-12-16 12:09:02 +1100100
101 while ((nbytes = walk.nbytes)) {
102 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu043a4402016-11-22 20:08:27 +0800103 nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800104 child);
David Howells91652be2006-12-16 12:09:02 +1100105 else
Herbert Xu043a4402016-11-22 20:08:27 +0800106 nbytes = crypto_pcbc_encrypt_segment(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800107 child);
Herbert Xu043a4402016-11-22 20:08:27 +0800108 err = skcipher_walk_done(&walk, nbytes);
David Howells91652be2006-12-16 12:09:02 +1100109 }
110
111 return err;
112}
113
Herbert Xu043a4402016-11-22 20:08:27 +0800114static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
115 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800116 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100117{
David Howells91652be2006-12-16 12:09:02 +1100118 int bsize = crypto_cipher_blocksize(tfm);
119 unsigned int nbytes = walk->nbytes;
120 u8 *src = walk->src.virt.addr;
121 u8 *dst = walk->dst.virt.addr;
Eric Biggersbb1ae0a2019-01-03 20:16:13 -0800122 u8 * const iv = walk->iv;
David Howells91652be2006-12-16 12:09:02 +1100123
124 do {
Herbert Xu043a4402016-11-22 20:08:27 +0800125 crypto_cipher_decrypt_one(tfm, dst, src);
Herbert Xud0b90072007-11-20 17:49:49 +0800126 crypto_xor(dst, iv, bsize);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100127 crypto_xor_cpy(iv, dst, src, bsize);
David Howells91652be2006-12-16 12:09:02 +1100128
129 src += bsize;
130 dst += bsize;
131 } while ((nbytes -= bsize) >= bsize);
132
David Howells91652be2006-12-16 12:09:02 +1100133 return nbytes;
134}
135
Herbert Xu043a4402016-11-22 20:08:27 +0800136static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
137 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800138 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100139{
David Howells91652be2006-12-16 12:09:02 +1100140 int bsize = crypto_cipher_blocksize(tfm);
141 unsigned int nbytes = walk->nbytes;
142 u8 *src = walk->src.virt.addr;
Eric Biggersbb1ae0a2019-01-03 20:16:13 -0800143 u8 * const iv = walk->iv;
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +0200144 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32));
David Howells91652be2006-12-16 12:09:02 +1100145
146 do {
147 memcpy(tmpbuf, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +0800148 crypto_cipher_decrypt_one(tfm, src, src);
Herbert Xud0b90072007-11-20 17:49:49 +0800149 crypto_xor(src, iv, bsize);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100150 crypto_xor_cpy(iv, src, tmpbuf, bsize);
David Howells91652be2006-12-16 12:09:02 +1100151
152 src += bsize;
153 } while ((nbytes -= bsize) >= bsize);
154
David Howells91652be2006-12-16 12:09:02 +1100155 return nbytes;
156}
157
Herbert Xu043a4402016-11-22 20:08:27 +0800158static int crypto_pcbc_decrypt(struct skcipher_request *req)
David Howells91652be2006-12-16 12:09:02 +1100159{
Herbert Xu043a4402016-11-22 20:08:27 +0800160 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
161 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
David Howells91652be2006-12-16 12:09:02 +1100162 struct crypto_cipher *child = ctx->child;
Herbert Xu043a4402016-11-22 20:08:27 +0800163 struct skcipher_walk walk;
164 unsigned int nbytes;
David Howells91652be2006-12-16 12:09:02 +1100165 int err;
166
Herbert Xu043a4402016-11-22 20:08:27 +0800167 err = skcipher_walk_virt(&walk, req, false);
David Howells91652be2006-12-16 12:09:02 +1100168
169 while ((nbytes = walk.nbytes)) {
170 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu043a4402016-11-22 20:08:27 +0800171 nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800172 child);
David Howells91652be2006-12-16 12:09:02 +1100173 else
Herbert Xu043a4402016-11-22 20:08:27 +0800174 nbytes = crypto_pcbc_decrypt_segment(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800175 child);
Herbert Xu043a4402016-11-22 20:08:27 +0800176 err = skcipher_walk_done(&walk, nbytes);
David Howells91652be2006-12-16 12:09:02 +1100177 }
178
179 return err;
180}
181
Herbert Xu043a4402016-11-22 20:08:27 +0800182static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100183{
Herbert Xu043a4402016-11-22 20:08:27 +0800184 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
185 struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
186 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
Herbert Xu2e306ee2006-12-17 10:05:58 +1100187 struct crypto_cipher *cipher;
David Howells91652be2006-12-16 12:09:02 +1100188
Herbert Xu2e306ee2006-12-17 10:05:58 +1100189 cipher = crypto_spawn_cipher(spawn);
190 if (IS_ERR(cipher))
191 return PTR_ERR(cipher);
David Howells91652be2006-12-16 12:09:02 +1100192
Herbert Xu2e306ee2006-12-17 10:05:58 +1100193 ctx->child = cipher;
David Howells91652be2006-12-16 12:09:02 +1100194 return 0;
195}
196
Herbert Xu043a4402016-11-22 20:08:27 +0800197static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100198{
Herbert Xu043a4402016-11-22 20:08:27 +0800199 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
200
David Howells91652be2006-12-16 12:09:02 +1100201 crypto_free_cipher(ctx->child);
202}
203
Herbert Xu043a4402016-11-22 20:08:27 +0800204static void crypto_pcbc_free(struct skcipher_instance *inst)
David Howells91652be2006-12-16 12:09:02 +1100205{
Herbert Xu043a4402016-11-22 20:08:27 +0800206 crypto_drop_skcipher(skcipher_instance_ctx(inst));
207 kfree(inst);
208}
209
210static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
211{
212 struct skcipher_instance *inst;
213 struct crypto_attr_type *algt;
214 struct crypto_spawn *spawn;
David Howells91652be2006-12-16 12:09:02 +1100215 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +1100216 int err;
David Howells91652be2006-12-16 12:09:02 +1100217
Herbert Xu043a4402016-11-22 20:08:27 +0800218 algt = crypto_get_attr_type(tb);
219 if (IS_ERR(algt))
220 return PTR_ERR(algt);
Herbert Xuebc610e2007-01-01 18:37:02 +1100221
Herbert Xu043a4402016-11-22 20:08:27 +0800222 if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) &
223 ~CRYPTO_ALG_INTERNAL)
224 return -EINVAL;
225
226 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
227 if (!inst)
228 return -ENOMEM;
229
230 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER |
231 (algt->type & CRYPTO_ALG_INTERNAL),
232 CRYPTO_ALG_TYPE_MASK |
233 (algt->mask & CRYPTO_ALG_INTERNAL));
234 err = PTR_ERR(alg);
David Howells91652be2006-12-16 12:09:02 +1100235 if (IS_ERR(alg))
Herbert Xu043a4402016-11-22 20:08:27 +0800236 goto err_free_inst;
David Howells91652be2006-12-16 12:09:02 +1100237
Herbert Xu043a4402016-11-22 20:08:27 +0800238 spawn = skcipher_instance_ctx(inst);
239 err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
240 CRYPTO_ALG_TYPE_MASK);
Herbert Xu043a4402016-11-22 20:08:27 +0800241 if (err)
Pan Bian2f946052018-11-22 18:00:16 +0800242 goto err_put_alg;
David Howells91652be2006-12-16 12:09:02 +1100243
Herbert Xu043a4402016-11-22 20:08:27 +0800244 err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg);
245 if (err)
246 goto err_drop_spawn;
247
248 inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL;
249 inst->alg.base.cra_priority = alg->cra_priority;
250 inst->alg.base.cra_blocksize = alg->cra_blocksize;
251 inst->alg.base.cra_alignmask = alg->cra_alignmask;
David Howells91652be2006-12-16 12:09:02 +1100252
Herbert Xu043a4402016-11-22 20:08:27 +0800253 inst->alg.ivsize = alg->cra_blocksize;
254 inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
255 inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
David Howells91652be2006-12-16 12:09:02 +1100256
Herbert Xu043a4402016-11-22 20:08:27 +0800257 inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
David Howells91652be2006-12-16 12:09:02 +1100258
Herbert Xu043a4402016-11-22 20:08:27 +0800259 inst->alg.init = crypto_pcbc_init_tfm;
260 inst->alg.exit = crypto_pcbc_exit_tfm;
David Howells91652be2006-12-16 12:09:02 +1100261
Herbert Xu043a4402016-11-22 20:08:27 +0800262 inst->alg.setkey = crypto_pcbc_setkey;
263 inst->alg.encrypt = crypto_pcbc_encrypt;
264 inst->alg.decrypt = crypto_pcbc_decrypt;
David Howells91652be2006-12-16 12:09:02 +1100265
Herbert Xu043a4402016-11-22 20:08:27 +0800266 inst->free = crypto_pcbc_free;
David Howells91652be2006-12-16 12:09:02 +1100267
Herbert Xu043a4402016-11-22 20:08:27 +0800268 err = skcipher_register_instance(tmpl, inst);
269 if (err)
270 goto err_drop_spawn;
Pan Bian2f946052018-11-22 18:00:16 +0800271 crypto_mod_put(alg);
Herbert Xu043a4402016-11-22 20:08:27 +0800272
273out:
274 return err;
275
276err_drop_spawn:
277 crypto_drop_spawn(spawn);
Pan Bian2f946052018-11-22 18:00:16 +0800278err_put_alg:
279 crypto_mod_put(alg);
Herbert Xu043a4402016-11-22 20:08:27 +0800280err_free_inst:
David Howells91652be2006-12-16 12:09:02 +1100281 kfree(inst);
Herbert Xu043a4402016-11-22 20:08:27 +0800282 goto out;
David Howells91652be2006-12-16 12:09:02 +1100283}
284
285static struct crypto_template crypto_pcbc_tmpl = {
286 .name = "pcbc",
Herbert Xu043a4402016-11-22 20:08:27 +0800287 .create = crypto_pcbc_create,
David Howells91652be2006-12-16 12:09:02 +1100288 .module = THIS_MODULE,
289};
290
291static int __init crypto_pcbc_module_init(void)
292{
293 return crypto_register_template(&crypto_pcbc_tmpl);
294}
295
296static void __exit crypto_pcbc_module_exit(void)
297{
298 crypto_unregister_template(&crypto_pcbc_tmpl);
299}
300
301module_init(crypto_pcbc_module_init);
302module_exit(crypto_pcbc_module_exit);
303
304MODULE_LICENSE("GPL");
305MODULE_DESCRIPTION("PCBC block cipher algorithm");
Kees Cook4943ba12014-11-24 16:32:38 -0800306MODULE_ALIAS_CRYPTO("pcbc");