blob: d9e45a9587201d7760084bd81a024c11d743dc75 [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
Herbert Xu043a4402016-11-22 20:08:27 +080017#include <crypto/internal/skcipher.h>
David Howells91652be2006-12-16 12:09:02 +110018#include <linux/err.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
David Howells91652be2006-12-16 12:09:02 +110022#include <linux/slab.h>
Gideon Israel Dsouzad8c34b92016-12-31 21:26:23 +053023#include <linux/compiler.h>
David Howells91652be2006-12-16 12:09:02 +110024
25struct crypto_pcbc_ctx {
26 struct crypto_cipher *child;
David Howells91652be2006-12-16 12:09:02 +110027};
28
Herbert Xu043a4402016-11-22 20:08:27 +080029static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key,
David Howells91652be2006-12-16 12:09:02 +110030 unsigned int keylen)
31{
Herbert Xu043a4402016-11-22 20:08:27 +080032 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent);
David Howells91652be2006-12-16 12:09:02 +110033 struct crypto_cipher *child = ctx->child;
34 int err;
35
36 crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
Herbert Xu043a4402016-11-22 20:08:27 +080037 crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
38 CRYPTO_TFM_REQ_MASK);
David Howells91652be2006-12-16 12:09:02 +110039 err = crypto_cipher_setkey(child, key, keylen);
Herbert Xu043a4402016-11-22 20:08:27 +080040 crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
41 CRYPTO_TFM_RES_MASK);
David Howells91652be2006-12-16 12:09:02 +110042 return err;
43}
44
Herbert Xu043a4402016-11-22 20:08:27 +080045static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
46 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +080047 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +110048{
David Howells91652be2006-12-16 12:09:02 +110049 int bsize = crypto_cipher_blocksize(tfm);
50 unsigned int nbytes = walk->nbytes;
51 u8 *src = walk->src.virt.addr;
52 u8 *dst = walk->dst.virt.addr;
53 u8 *iv = walk->iv;
54
55 do {
Herbert Xud0b90072007-11-20 17:49:49 +080056 crypto_xor(iv, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +080057 crypto_cipher_encrypt_one(tfm, dst, iv);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +010058 crypto_xor_cpy(iv, dst, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110059
60 src += bsize;
61 dst += bsize;
62 } while ((nbytes -= bsize) >= bsize);
63
64 return nbytes;
65}
66
Herbert Xu043a4402016-11-22 20:08:27 +080067static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
68 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +080069 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +110070{
David Howells91652be2006-12-16 12:09:02 +110071 int bsize = crypto_cipher_blocksize(tfm);
72 unsigned int nbytes = walk->nbytes;
73 u8 *src = walk->src.virt.addr;
74 u8 *iv = walk->iv;
75 u8 tmpbuf[bsize];
76
77 do {
78 memcpy(tmpbuf, src, bsize);
Herbert Xud0b90072007-11-20 17:49:49 +080079 crypto_xor(iv, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +080080 crypto_cipher_encrypt_one(tfm, src, iv);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +010081 crypto_xor_cpy(iv, tmpbuf, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110082
83 src += bsize;
84 } while ((nbytes -= bsize) >= bsize);
85
86 memcpy(walk->iv, iv, bsize);
87
88 return nbytes;
89}
90
Herbert Xu043a4402016-11-22 20:08:27 +080091static int crypto_pcbc_encrypt(struct skcipher_request *req)
David Howells91652be2006-12-16 12:09:02 +110092{
Herbert Xu043a4402016-11-22 20:08:27 +080093 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
94 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
David Howells91652be2006-12-16 12:09:02 +110095 struct crypto_cipher *child = ctx->child;
Herbert Xu043a4402016-11-22 20:08:27 +080096 struct skcipher_walk walk;
97 unsigned int nbytes;
David Howells91652be2006-12-16 12:09:02 +110098 int err;
99
Herbert Xu043a4402016-11-22 20:08:27 +0800100 err = skcipher_walk_virt(&walk, req, false);
David Howells91652be2006-12-16 12:09:02 +1100101
102 while ((nbytes = walk.nbytes)) {
103 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu043a4402016-11-22 20:08:27 +0800104 nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800105 child);
David Howells91652be2006-12-16 12:09:02 +1100106 else
Herbert Xu043a4402016-11-22 20:08:27 +0800107 nbytes = crypto_pcbc_encrypt_segment(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800108 child);
Herbert Xu043a4402016-11-22 20:08:27 +0800109 err = skcipher_walk_done(&walk, nbytes);
David Howells91652be2006-12-16 12:09:02 +1100110 }
111
112 return err;
113}
114
Herbert Xu043a4402016-11-22 20:08:27 +0800115static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
116 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800117 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100118{
David Howells91652be2006-12-16 12:09:02 +1100119 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;
123 u8 *iv = walk->iv;
124
125 do {
Herbert Xu043a4402016-11-22 20:08:27 +0800126 crypto_cipher_decrypt_one(tfm, dst, src);
Herbert Xud0b90072007-11-20 17:49:49 +0800127 crypto_xor(dst, iv, bsize);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100128 crypto_xor_cpy(iv, dst, src, bsize);
David Howells91652be2006-12-16 12:09:02 +1100129
130 src += bsize;
131 dst += bsize;
132 } while ((nbytes -= bsize) >= bsize);
133
134 memcpy(walk->iv, iv, bsize);
135
136 return nbytes;
137}
138
Herbert Xu043a4402016-11-22 20:08:27 +0800139static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
140 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800141 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100142{
David Howells91652be2006-12-16 12:09:02 +1100143 int bsize = crypto_cipher_blocksize(tfm);
144 unsigned int nbytes = walk->nbytes;
145 u8 *src = walk->src.virt.addr;
146 u8 *iv = walk->iv;
Gideon Israel Dsouzad8c34b92016-12-31 21:26:23 +0530147 u8 tmpbuf[bsize] __aligned(__alignof__(u32));
David Howells91652be2006-12-16 12:09:02 +1100148
149 do {
150 memcpy(tmpbuf, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +0800151 crypto_cipher_decrypt_one(tfm, src, src);
Herbert Xud0b90072007-11-20 17:49:49 +0800152 crypto_xor(src, iv, bsize);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100153 crypto_xor_cpy(iv, src, tmpbuf, bsize);
David Howells91652be2006-12-16 12:09:02 +1100154
155 src += bsize;
156 } while ((nbytes -= bsize) >= bsize);
157
158 memcpy(walk->iv, iv, bsize);
159
160 return nbytes;
161}
162
Herbert Xu043a4402016-11-22 20:08:27 +0800163static int crypto_pcbc_decrypt(struct skcipher_request *req)
David Howells91652be2006-12-16 12:09:02 +1100164{
Herbert Xu043a4402016-11-22 20:08:27 +0800165 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
166 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
David Howells91652be2006-12-16 12:09:02 +1100167 struct crypto_cipher *child = ctx->child;
Herbert Xu043a4402016-11-22 20:08:27 +0800168 struct skcipher_walk walk;
169 unsigned int nbytes;
David Howells91652be2006-12-16 12:09:02 +1100170 int err;
171
Herbert Xu043a4402016-11-22 20:08:27 +0800172 err = skcipher_walk_virt(&walk, req, false);
David Howells91652be2006-12-16 12:09:02 +1100173
174 while ((nbytes = walk.nbytes)) {
175 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu043a4402016-11-22 20:08:27 +0800176 nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800177 child);
David Howells91652be2006-12-16 12:09:02 +1100178 else
Herbert Xu043a4402016-11-22 20:08:27 +0800179 nbytes = crypto_pcbc_decrypt_segment(req, &walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800180 child);
Herbert Xu043a4402016-11-22 20:08:27 +0800181 err = skcipher_walk_done(&walk, nbytes);
David Howells91652be2006-12-16 12:09:02 +1100182 }
183
184 return err;
185}
186
Herbert Xu043a4402016-11-22 20:08:27 +0800187static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100188{
Herbert Xu043a4402016-11-22 20:08:27 +0800189 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
190 struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
191 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
Herbert Xu2e306ee2006-12-17 10:05:58 +1100192 struct crypto_cipher *cipher;
David Howells91652be2006-12-16 12:09:02 +1100193
Herbert Xu2e306ee2006-12-17 10:05:58 +1100194 cipher = crypto_spawn_cipher(spawn);
195 if (IS_ERR(cipher))
196 return PTR_ERR(cipher);
David Howells91652be2006-12-16 12:09:02 +1100197
Herbert Xu2e306ee2006-12-17 10:05:58 +1100198 ctx->child = cipher;
David Howells91652be2006-12-16 12:09:02 +1100199 return 0;
200}
201
Herbert Xu043a4402016-11-22 20:08:27 +0800202static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100203{
Herbert Xu043a4402016-11-22 20:08:27 +0800204 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
205
David Howells91652be2006-12-16 12:09:02 +1100206 crypto_free_cipher(ctx->child);
207}
208
Herbert Xu043a4402016-11-22 20:08:27 +0800209static void crypto_pcbc_free(struct skcipher_instance *inst)
David Howells91652be2006-12-16 12:09:02 +1100210{
Herbert Xu043a4402016-11-22 20:08:27 +0800211 crypto_drop_skcipher(skcipher_instance_ctx(inst));
212 kfree(inst);
213}
214
215static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
216{
217 struct skcipher_instance *inst;
218 struct crypto_attr_type *algt;
219 struct crypto_spawn *spawn;
David Howells91652be2006-12-16 12:09:02 +1100220 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +1100221 int err;
David Howells91652be2006-12-16 12:09:02 +1100222
Herbert Xu043a4402016-11-22 20:08:27 +0800223 algt = crypto_get_attr_type(tb);
224 if (IS_ERR(algt))
225 return PTR_ERR(algt);
Herbert Xuebc610e2007-01-01 18:37:02 +1100226
Herbert Xu043a4402016-11-22 20:08:27 +0800227 if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) &
228 ~CRYPTO_ALG_INTERNAL)
229 return -EINVAL;
230
231 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
232 if (!inst)
233 return -ENOMEM;
234
235 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER |
236 (algt->type & CRYPTO_ALG_INTERNAL),
237 CRYPTO_ALG_TYPE_MASK |
238 (algt->mask & CRYPTO_ALG_INTERNAL));
239 err = PTR_ERR(alg);
David Howells91652be2006-12-16 12:09:02 +1100240 if (IS_ERR(alg))
Herbert Xu043a4402016-11-22 20:08:27 +0800241 goto err_free_inst;
David Howells91652be2006-12-16 12:09:02 +1100242
Herbert Xu043a4402016-11-22 20:08:27 +0800243 spawn = skcipher_instance_ctx(inst);
244 err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
245 CRYPTO_ALG_TYPE_MASK);
246 crypto_mod_put(alg);
247 if (err)
248 goto err_free_inst;
David Howells91652be2006-12-16 12:09:02 +1100249
Herbert Xu043a4402016-11-22 20:08:27 +0800250 err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg);
251 if (err)
252 goto err_drop_spawn;
253
254 inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL;
255 inst->alg.base.cra_priority = alg->cra_priority;
256 inst->alg.base.cra_blocksize = alg->cra_blocksize;
257 inst->alg.base.cra_alignmask = alg->cra_alignmask;
David Howells91652be2006-12-16 12:09:02 +1100258
Herbert Xu043a4402016-11-22 20:08:27 +0800259 inst->alg.ivsize = alg->cra_blocksize;
260 inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
261 inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
David Howells91652be2006-12-16 12:09:02 +1100262
Herbert Xu043a4402016-11-22 20:08:27 +0800263 inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
David Howells91652be2006-12-16 12:09:02 +1100264
Herbert Xu043a4402016-11-22 20:08:27 +0800265 inst->alg.init = crypto_pcbc_init_tfm;
266 inst->alg.exit = crypto_pcbc_exit_tfm;
David Howells91652be2006-12-16 12:09:02 +1100267
Herbert Xu043a4402016-11-22 20:08:27 +0800268 inst->alg.setkey = crypto_pcbc_setkey;
269 inst->alg.encrypt = crypto_pcbc_encrypt;
270 inst->alg.decrypt = crypto_pcbc_decrypt;
David Howells91652be2006-12-16 12:09:02 +1100271
Herbert Xu043a4402016-11-22 20:08:27 +0800272 inst->free = crypto_pcbc_free;
David Howells91652be2006-12-16 12:09:02 +1100273
Herbert Xu043a4402016-11-22 20:08:27 +0800274 err = skcipher_register_instance(tmpl, inst);
275 if (err)
276 goto err_drop_spawn;
277
278out:
279 return err;
280
281err_drop_spawn:
282 crypto_drop_spawn(spawn);
283err_free_inst:
David Howells91652be2006-12-16 12:09:02 +1100284 kfree(inst);
Herbert Xu043a4402016-11-22 20:08:27 +0800285 goto out;
David Howells91652be2006-12-16 12:09:02 +1100286}
287
288static struct crypto_template crypto_pcbc_tmpl = {
289 .name = "pcbc",
Herbert Xu043a4402016-11-22 20:08:27 +0800290 .create = crypto_pcbc_create,
David Howells91652be2006-12-16 12:09:02 +1100291 .module = THIS_MODULE,
292};
293
294static int __init crypto_pcbc_module_init(void)
295{
296 return crypto_register_template(&crypto_pcbc_tmpl);
297}
298
299static void __exit crypto_pcbc_module_exit(void)
300{
301 crypto_unregister_template(&crypto_pcbc_tmpl);
302}
303
304module_init(crypto_pcbc_module_init);
305module_exit(crypto_pcbc_module_exit);
306
307MODULE_LICENSE("GPL");
308MODULE_DESCRIPTION("PCBC block cipher algorithm");
Kees Cook4943ba12014-11-24 16:32:38 -0800309MODULE_ALIAS_CRYPTO("pcbc");