blob: 2fa03fc576fe4af88ba7062238749cf251630d50 [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
Herbert Xu043a4402016-11-22 20:08:27 +080024static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
25 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +080026 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +110027{
David Howells91652be2006-12-16 12:09:02 +110028 int bsize = crypto_cipher_blocksize(tfm);
29 unsigned int nbytes = walk->nbytes;
30 u8 *src = walk->src.virt.addr;
31 u8 *dst = walk->dst.virt.addr;
Eric Biggers251b7ae2019-01-03 20:16:13 -080032 u8 * const iv = walk->iv;
David Howells91652be2006-12-16 12:09:02 +110033
34 do {
Herbert Xud0b90072007-11-20 17:49:49 +080035 crypto_xor(iv, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +080036 crypto_cipher_encrypt_one(tfm, dst, iv);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +010037 crypto_xor_cpy(iv, dst, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110038
39 src += bsize;
40 dst += bsize;
41 } while ((nbytes -= bsize) >= bsize);
42
43 return nbytes;
44}
45
Herbert Xu043a4402016-11-22 20:08:27 +080046static int crypto_pcbc_encrypt_inplace(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;
Eric Biggers251b7ae2019-01-03 20:16:13 -080053 u8 * const iv = walk->iv;
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020054 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE];
David Howells91652be2006-12-16 12:09:02 +110055
56 do {
57 memcpy(tmpbuf, src, bsize);
Herbert Xud0b90072007-11-20 17:49:49 +080058 crypto_xor(iv, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +080059 crypto_cipher_encrypt_one(tfm, src, iv);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +010060 crypto_xor_cpy(iv, tmpbuf, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110061
62 src += bsize;
63 } while ((nbytes -= bsize) >= bsize);
64
David Howells91652be2006-12-16 12:09:02 +110065 return nbytes;
66}
67
Herbert Xu043a4402016-11-22 20:08:27 +080068static int crypto_pcbc_encrypt(struct skcipher_request *req)
David Howells91652be2006-12-16 12:09:02 +110069{
Herbert Xu043a4402016-11-22 20:08:27 +080070 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers0be487b2019-01-03 20:16:22 -080071 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Herbert Xu043a4402016-11-22 20:08:27 +080072 struct skcipher_walk walk;
73 unsigned int nbytes;
David Howells91652be2006-12-16 12:09:02 +110074 int err;
75
Herbert Xu043a4402016-11-22 20:08:27 +080076 err = skcipher_walk_virt(&walk, req, false);
David Howells91652be2006-12-16 12:09:02 +110077
78 while ((nbytes = walk.nbytes)) {
79 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu043a4402016-11-22 20:08:27 +080080 nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
Eric Biggers0be487b2019-01-03 20:16:22 -080081 cipher);
David Howells91652be2006-12-16 12:09:02 +110082 else
Herbert Xu043a4402016-11-22 20:08:27 +080083 nbytes = crypto_pcbc_encrypt_segment(req, &walk,
Eric Biggers0be487b2019-01-03 20:16:22 -080084 cipher);
Herbert Xu043a4402016-11-22 20:08:27 +080085 err = skcipher_walk_done(&walk, nbytes);
David Howells91652be2006-12-16 12:09:02 +110086 }
87
88 return err;
89}
90
Herbert Xu043a4402016-11-22 20:08:27 +080091static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
92 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +080093 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +110094{
David Howells91652be2006-12-16 12:09:02 +110095 int bsize = crypto_cipher_blocksize(tfm);
96 unsigned int nbytes = walk->nbytes;
97 u8 *src = walk->src.virt.addr;
98 u8 *dst = walk->dst.virt.addr;
Eric Biggers251b7ae2019-01-03 20:16:13 -080099 u8 * const iv = walk->iv;
David Howells91652be2006-12-16 12:09:02 +1100100
101 do {
Herbert Xu043a4402016-11-22 20:08:27 +0800102 crypto_cipher_decrypt_one(tfm, dst, src);
Herbert Xud0b90072007-11-20 17:49:49 +0800103 crypto_xor(dst, iv, bsize);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100104 crypto_xor_cpy(iv, dst, src, bsize);
David Howells91652be2006-12-16 12:09:02 +1100105
106 src += bsize;
107 dst += bsize;
108 } while ((nbytes -= bsize) >= bsize);
109
David Howells91652be2006-12-16 12:09:02 +1100110 return nbytes;
111}
112
Herbert Xu043a4402016-11-22 20:08:27 +0800113static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
114 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800115 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100116{
David Howells91652be2006-12-16 12:09:02 +1100117 int bsize = crypto_cipher_blocksize(tfm);
118 unsigned int nbytes = walk->nbytes;
119 u8 *src = walk->src.virt.addr;
Eric Biggers251b7ae2019-01-03 20:16:13 -0800120 u8 * const iv = walk->iv;
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +0200121 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32));
David Howells91652be2006-12-16 12:09:02 +1100122
123 do {
124 memcpy(tmpbuf, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +0800125 crypto_cipher_decrypt_one(tfm, src, src);
Herbert Xud0b90072007-11-20 17:49:49 +0800126 crypto_xor(src, iv, bsize);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100127 crypto_xor_cpy(iv, src, tmpbuf, bsize);
David Howells91652be2006-12-16 12:09:02 +1100128
129 src += bsize;
130 } while ((nbytes -= bsize) >= bsize);
131
David Howells91652be2006-12-16 12:09:02 +1100132 return nbytes;
133}
134
Herbert Xu043a4402016-11-22 20:08:27 +0800135static int crypto_pcbc_decrypt(struct skcipher_request *req)
David Howells91652be2006-12-16 12:09:02 +1100136{
Herbert Xu043a4402016-11-22 20:08:27 +0800137 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers0be487b2019-01-03 20:16:22 -0800138 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Herbert Xu043a4402016-11-22 20:08:27 +0800139 struct skcipher_walk walk;
140 unsigned int nbytes;
David Howells91652be2006-12-16 12:09:02 +1100141 int err;
142
Herbert Xu043a4402016-11-22 20:08:27 +0800143 err = skcipher_walk_virt(&walk, req, false);
David Howells91652be2006-12-16 12:09:02 +1100144
145 while ((nbytes = walk.nbytes)) {
146 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu043a4402016-11-22 20:08:27 +0800147 nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
Eric Biggers0be487b2019-01-03 20:16:22 -0800148 cipher);
David Howells91652be2006-12-16 12:09:02 +1100149 else
Herbert Xu043a4402016-11-22 20:08:27 +0800150 nbytes = crypto_pcbc_decrypt_segment(req, &walk,
Eric Biggers0be487b2019-01-03 20:16:22 -0800151 cipher);
Herbert Xu043a4402016-11-22 20:08:27 +0800152 err = skcipher_walk_done(&walk, nbytes);
David Howells91652be2006-12-16 12:09:02 +1100153 }
154
155 return err;
156}
157
Herbert Xu043a4402016-11-22 20:08:27 +0800158static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
159{
160 struct skcipher_instance *inst;
David Howells91652be2006-12-16 12:09:02 +1100161 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +1100162 int err;
David Howells91652be2006-12-16 12:09:02 +1100163
Eric Biggers0be487b2019-01-03 20:16:22 -0800164 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
165 if (IS_ERR(inst))
166 return PTR_ERR(inst);
Herbert Xuebc610e2007-01-01 18:37:02 +1100167
Herbert Xu043a4402016-11-22 20:08:27 +0800168 inst->alg.encrypt = crypto_pcbc_encrypt;
169 inst->alg.decrypt = crypto_pcbc_decrypt;
David Howells91652be2006-12-16 12:09:02 +1100170
Herbert Xu043a4402016-11-22 20:08:27 +0800171 err = skcipher_register_instance(tmpl, inst);
172 if (err)
Eric Biggers0be487b2019-01-03 20:16:22 -0800173 inst->free(inst);
Pan Biane5bde042018-11-22 18:00:16 +0800174 crypto_mod_put(alg);
Herbert Xu043a4402016-11-22 20:08:27 +0800175 return err;
David Howells91652be2006-12-16 12:09:02 +1100176}
177
178static struct crypto_template crypto_pcbc_tmpl = {
179 .name = "pcbc",
Herbert Xu043a4402016-11-22 20:08:27 +0800180 .create = crypto_pcbc_create,
David Howells91652be2006-12-16 12:09:02 +1100181 .module = THIS_MODULE,
182};
183
184static int __init crypto_pcbc_module_init(void)
185{
186 return crypto_register_template(&crypto_pcbc_tmpl);
187}
188
189static void __exit crypto_pcbc_module_exit(void)
190{
191 crypto_unregister_template(&crypto_pcbc_tmpl);
192}
193
194module_init(crypto_pcbc_module_init);
195module_exit(crypto_pcbc_module_exit);
196
197MODULE_LICENSE("GPL");
Eric Biggers0be487b2019-01-03 20:16:22 -0800198MODULE_DESCRIPTION("PCBC block cipher mode of operation");
Kees Cook4943ba12014-11-24 16:32:38 -0800199MODULE_ALIAS_CRYPTO("pcbc");