blob: 862cdb8d8b6cf262cef2b1405fe4a1cebecbe684 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells91652be2006-12-16 12:09:02 +11002/*
3 * PCBC: Propagating Cipher Block Chaining mode
4 *
5 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
7 *
8 * Derived from cbc.c
9 * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
David Howells91652be2006-12-16 12:09:02 +110010 */
11
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020012#include <crypto/algapi.h>
Herbert Xu043a4402016-11-22 20:08:27 +080013#include <crypto/internal/skcipher.h>
David Howells91652be2006-12-16 12:09:02 +110014#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
David Howells91652be2006-12-16 12:09:02 +110018
Herbert Xu043a4402016-11-22 20:08:27 +080019static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
20 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +080021 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +110022{
David Howells91652be2006-12-16 12:09:02 +110023 int bsize = crypto_cipher_blocksize(tfm);
24 unsigned int nbytes = walk->nbytes;
25 u8 *src = walk->src.virt.addr;
26 u8 *dst = walk->dst.virt.addr;
Eric Biggers251b7ae2019-01-03 20:16:13 -080027 u8 * const iv = walk->iv;
David Howells91652be2006-12-16 12:09:02 +110028
29 do {
Herbert Xud0b90072007-11-20 17:49:49 +080030 crypto_xor(iv, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +080031 crypto_cipher_encrypt_one(tfm, dst, iv);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +010032 crypto_xor_cpy(iv, dst, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110033
34 src += bsize;
35 dst += bsize;
36 } while ((nbytes -= bsize) >= bsize);
37
38 return nbytes;
39}
40
Herbert Xu043a4402016-11-22 20:08:27 +080041static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
42 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +080043 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +110044{
David Howells91652be2006-12-16 12:09:02 +110045 int bsize = crypto_cipher_blocksize(tfm);
46 unsigned int nbytes = walk->nbytes;
47 u8 *src = walk->src.virt.addr;
Eric Biggers251b7ae2019-01-03 20:16:13 -080048 u8 * const iv = walk->iv;
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020049 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE];
David Howells91652be2006-12-16 12:09:02 +110050
51 do {
52 memcpy(tmpbuf, src, bsize);
Herbert Xud0b90072007-11-20 17:49:49 +080053 crypto_xor(iv, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +080054 crypto_cipher_encrypt_one(tfm, src, iv);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +010055 crypto_xor_cpy(iv, tmpbuf, src, bsize);
David Howells91652be2006-12-16 12:09:02 +110056
57 src += bsize;
58 } while ((nbytes -= bsize) >= bsize);
59
David Howells91652be2006-12-16 12:09:02 +110060 return nbytes;
61}
62
Herbert Xu043a4402016-11-22 20:08:27 +080063static int crypto_pcbc_encrypt(struct skcipher_request *req)
David Howells91652be2006-12-16 12:09:02 +110064{
Herbert Xu043a4402016-11-22 20:08:27 +080065 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers0be487b2019-01-03 20:16:22 -080066 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Herbert Xu043a4402016-11-22 20:08:27 +080067 struct skcipher_walk walk;
68 unsigned int nbytes;
David Howells91652be2006-12-16 12:09:02 +110069 int err;
70
Herbert Xu043a4402016-11-22 20:08:27 +080071 err = skcipher_walk_virt(&walk, req, false);
David Howells91652be2006-12-16 12:09:02 +110072
73 while ((nbytes = walk.nbytes)) {
74 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu043a4402016-11-22 20:08:27 +080075 nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
Eric Biggers0be487b2019-01-03 20:16:22 -080076 cipher);
David Howells91652be2006-12-16 12:09:02 +110077 else
Herbert Xu043a4402016-11-22 20:08:27 +080078 nbytes = crypto_pcbc_encrypt_segment(req, &walk,
Eric Biggers0be487b2019-01-03 20:16:22 -080079 cipher);
Herbert Xu043a4402016-11-22 20:08:27 +080080 err = skcipher_walk_done(&walk, nbytes);
David Howells91652be2006-12-16 12:09:02 +110081 }
82
83 return err;
84}
85
Herbert Xu043a4402016-11-22 20:08:27 +080086static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
87 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +080088 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +110089{
David Howells91652be2006-12-16 12:09:02 +110090 int bsize = crypto_cipher_blocksize(tfm);
91 unsigned int nbytes = walk->nbytes;
92 u8 *src = walk->src.virt.addr;
93 u8 *dst = walk->dst.virt.addr;
Eric Biggers251b7ae2019-01-03 20:16:13 -080094 u8 * const iv = walk->iv;
David Howells91652be2006-12-16 12:09:02 +110095
96 do {
Herbert Xu043a4402016-11-22 20:08:27 +080097 crypto_cipher_decrypt_one(tfm, dst, src);
Herbert Xud0b90072007-11-20 17:49:49 +080098 crypto_xor(dst, iv, bsize);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +010099 crypto_xor_cpy(iv, dst, src, bsize);
David Howells91652be2006-12-16 12:09:02 +1100100
101 src += bsize;
102 dst += bsize;
103 } while ((nbytes -= bsize) >= bsize);
104
David Howells91652be2006-12-16 12:09:02 +1100105 return nbytes;
106}
107
Herbert Xu043a4402016-11-22 20:08:27 +0800108static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
109 struct skcipher_walk *walk,
Herbert Xud0b90072007-11-20 17:49:49 +0800110 struct crypto_cipher *tfm)
David Howells91652be2006-12-16 12:09:02 +1100111{
David Howells91652be2006-12-16 12:09:02 +1100112 int bsize = crypto_cipher_blocksize(tfm);
113 unsigned int nbytes = walk->nbytes;
114 u8 *src = walk->src.virt.addr;
Eric Biggers251b7ae2019-01-03 20:16:13 -0800115 u8 * const iv = walk->iv;
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +0200116 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32));
David Howells91652be2006-12-16 12:09:02 +1100117
118 do {
119 memcpy(tmpbuf, src, bsize);
Herbert Xu043a4402016-11-22 20:08:27 +0800120 crypto_cipher_decrypt_one(tfm, src, src);
Herbert Xud0b90072007-11-20 17:49:49 +0800121 crypto_xor(src, iv, bsize);
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100122 crypto_xor_cpy(iv, src, tmpbuf, bsize);
David Howells91652be2006-12-16 12:09:02 +1100123
124 src += bsize;
125 } while ((nbytes -= bsize) >= bsize);
126
David Howells91652be2006-12-16 12:09:02 +1100127 return nbytes;
128}
129
Herbert Xu043a4402016-11-22 20:08:27 +0800130static int crypto_pcbc_decrypt(struct skcipher_request *req)
David Howells91652be2006-12-16 12:09:02 +1100131{
Herbert Xu043a4402016-11-22 20:08:27 +0800132 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers0be487b2019-01-03 20:16:22 -0800133 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Herbert Xu043a4402016-11-22 20:08:27 +0800134 struct skcipher_walk walk;
135 unsigned int nbytes;
David Howells91652be2006-12-16 12:09:02 +1100136 int err;
137
Herbert Xu043a4402016-11-22 20:08:27 +0800138 err = skcipher_walk_virt(&walk, req, false);
David Howells91652be2006-12-16 12:09:02 +1100139
140 while ((nbytes = walk.nbytes)) {
141 if (walk.src.virt.addr == walk.dst.virt.addr)
Herbert Xu043a4402016-11-22 20:08:27 +0800142 nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
Eric Biggers0be487b2019-01-03 20:16:22 -0800143 cipher);
David Howells91652be2006-12-16 12:09:02 +1100144 else
Herbert Xu043a4402016-11-22 20:08:27 +0800145 nbytes = crypto_pcbc_decrypt_segment(req, &walk,
Eric Biggers0be487b2019-01-03 20:16:22 -0800146 cipher);
Herbert Xu043a4402016-11-22 20:08:27 +0800147 err = skcipher_walk_done(&walk, nbytes);
David Howells91652be2006-12-16 12:09:02 +1100148 }
149
150 return err;
151}
152
Herbert Xu043a4402016-11-22 20:08:27 +0800153static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
154{
155 struct skcipher_instance *inst;
David Howells91652be2006-12-16 12:09:02 +1100156 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +1100157 int err;
David Howells91652be2006-12-16 12:09:02 +1100158
Eric Biggers0be487b2019-01-03 20:16:22 -0800159 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
160 if (IS_ERR(inst))
161 return PTR_ERR(inst);
Herbert Xuebc610e2007-01-01 18:37:02 +1100162
Herbert Xu043a4402016-11-22 20:08:27 +0800163 inst->alg.encrypt = crypto_pcbc_encrypt;
164 inst->alg.decrypt = crypto_pcbc_decrypt;
David Howells91652be2006-12-16 12:09:02 +1100165
Herbert Xu043a4402016-11-22 20:08:27 +0800166 err = skcipher_register_instance(tmpl, inst);
167 if (err)
Eric Biggers0be487b2019-01-03 20:16:22 -0800168 inst->free(inst);
Pan Biane5bde042018-11-22 18:00:16 +0800169 crypto_mod_put(alg);
Herbert Xu043a4402016-11-22 20:08:27 +0800170 return err;
David Howells91652be2006-12-16 12:09:02 +1100171}
172
173static struct crypto_template crypto_pcbc_tmpl = {
174 .name = "pcbc",
Herbert Xu043a4402016-11-22 20:08:27 +0800175 .create = crypto_pcbc_create,
David Howells91652be2006-12-16 12:09:02 +1100176 .module = THIS_MODULE,
177};
178
179static int __init crypto_pcbc_module_init(void)
180{
181 return crypto_register_template(&crypto_pcbc_tmpl);
182}
183
184static void __exit crypto_pcbc_module_exit(void)
185{
186 crypto_unregister_template(&crypto_pcbc_tmpl);
187}
188
Eric Biggersc4741b22019-04-11 21:57:42 -0700189subsys_initcall(crypto_pcbc_module_init);
David Howells91652be2006-12-16 12:09:02 +1100190module_exit(crypto_pcbc_module_exit);
191
192MODULE_LICENSE("GPL");
Eric Biggers0be487b2019-01-03 20:16:22 -0800193MODULE_DESCRIPTION("PCBC block cipher mode of operation");
Kees Cook4943ba12014-11-24 16:32:38 -0800194MODULE_ALIAS_CRYPTO("pcbc");