WIP FPC-III support
[linux/fpc-iii.git] / crypto / cbc.c
blob0d9509dff891d54439152c7378b3ac9983098104
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * CBC: Cipher Block Chaining mode
5 * Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au>
6 */
8 #include <crypto/algapi.h>
9 #include <crypto/internal/skcipher.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/log2.h>
14 #include <linux/module.h>
16 static int crypto_cbc_encrypt_segment(struct skcipher_walk *walk,
17 struct crypto_skcipher *skcipher)
19 unsigned int bsize = crypto_skcipher_blocksize(skcipher);
20 void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
21 unsigned int nbytes = walk->nbytes;
22 u8 *src = walk->src.virt.addr;
23 u8 *dst = walk->dst.virt.addr;
24 struct crypto_cipher *cipher;
25 struct crypto_tfm *tfm;
26 u8 *iv = walk->iv;
28 cipher = skcipher_cipher_simple(skcipher);
29 tfm = crypto_cipher_tfm(cipher);
30 fn = crypto_cipher_alg(cipher)->cia_encrypt;
32 do {
33 crypto_xor(iv, src, bsize);
34 fn(tfm, dst, iv);
35 memcpy(iv, dst, bsize);
37 src += bsize;
38 dst += bsize;
39 } while ((nbytes -= bsize) >= bsize);
41 return nbytes;
44 static int crypto_cbc_encrypt_inplace(struct skcipher_walk *walk,
45 struct crypto_skcipher *skcipher)
47 unsigned int bsize = crypto_skcipher_blocksize(skcipher);
48 void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
49 unsigned int nbytes = walk->nbytes;
50 u8 *src = walk->src.virt.addr;
51 struct crypto_cipher *cipher;
52 struct crypto_tfm *tfm;
53 u8 *iv = walk->iv;
55 cipher = skcipher_cipher_simple(skcipher);
56 tfm = crypto_cipher_tfm(cipher);
57 fn = crypto_cipher_alg(cipher)->cia_encrypt;
59 do {
60 crypto_xor(src, iv, bsize);
61 fn(tfm, src, src);
62 iv = src;
64 src += bsize;
65 } while ((nbytes -= bsize) >= bsize);
67 memcpy(walk->iv, iv, bsize);
69 return nbytes;
72 static int crypto_cbc_encrypt(struct skcipher_request *req)
74 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
75 struct skcipher_walk walk;
76 int err;
78 err = skcipher_walk_virt(&walk, req, false);
80 while (walk.nbytes) {
81 if (walk.src.virt.addr == walk.dst.virt.addr)
82 err = crypto_cbc_encrypt_inplace(&walk, skcipher);
83 else
84 err = crypto_cbc_encrypt_segment(&walk, skcipher);
85 err = skcipher_walk_done(&walk, err);
88 return err;
91 static int crypto_cbc_decrypt_segment(struct skcipher_walk *walk,
92 struct crypto_skcipher *skcipher)
94 unsigned int bsize = crypto_skcipher_blocksize(skcipher);
95 void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
96 unsigned int nbytes = walk->nbytes;
97 u8 *src = walk->src.virt.addr;
98 u8 *dst = walk->dst.virt.addr;
99 struct crypto_cipher *cipher;
100 struct crypto_tfm *tfm;
101 u8 *iv = walk->iv;
103 cipher = skcipher_cipher_simple(skcipher);
104 tfm = crypto_cipher_tfm(cipher);
105 fn = crypto_cipher_alg(cipher)->cia_decrypt;
107 do {
108 fn(tfm, dst, src);
109 crypto_xor(dst, iv, bsize);
110 iv = src;
112 src += bsize;
113 dst += bsize;
114 } while ((nbytes -= bsize) >= bsize);
116 memcpy(walk->iv, iv, bsize);
118 return nbytes;
121 static int crypto_cbc_decrypt_inplace(struct skcipher_walk *walk,
122 struct crypto_skcipher *skcipher)
124 unsigned int bsize = crypto_skcipher_blocksize(skcipher);
125 void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
126 unsigned int nbytes = walk->nbytes;
127 u8 *src = walk->src.virt.addr;
128 u8 last_iv[MAX_CIPHER_BLOCKSIZE];
129 struct crypto_cipher *cipher;
130 struct crypto_tfm *tfm;
132 cipher = skcipher_cipher_simple(skcipher);
133 tfm = crypto_cipher_tfm(cipher);
134 fn = crypto_cipher_alg(cipher)->cia_decrypt;
136 /* Start of the last block. */
137 src += nbytes - (nbytes & (bsize - 1)) - bsize;
138 memcpy(last_iv, src, bsize);
140 for (;;) {
141 fn(tfm, src, src);
142 if ((nbytes -= bsize) < bsize)
143 break;
144 crypto_xor(src, src - bsize, bsize);
145 src -= bsize;
148 crypto_xor(src, walk->iv, bsize);
149 memcpy(walk->iv, last_iv, bsize);
151 return nbytes;
154 static int crypto_cbc_decrypt(struct skcipher_request *req)
156 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
157 struct skcipher_walk walk;
158 int err;
160 err = skcipher_walk_virt(&walk, req, false);
162 while (walk.nbytes) {
163 if (walk.src.virt.addr == walk.dst.virt.addr)
164 err = crypto_cbc_decrypt_inplace(&walk, skcipher);
165 else
166 err = crypto_cbc_decrypt_segment(&walk, skcipher);
167 err = skcipher_walk_done(&walk, err);
170 return err;
173 static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
175 struct skcipher_instance *inst;
176 struct crypto_alg *alg;
177 int err;
179 inst = skcipher_alloc_instance_simple(tmpl, tb);
180 if (IS_ERR(inst))
181 return PTR_ERR(inst);
183 alg = skcipher_ialg_simple(inst);
185 err = -EINVAL;
186 if (!is_power_of_2(alg->cra_blocksize))
187 goto out_free_inst;
189 inst->alg.encrypt = crypto_cbc_encrypt;
190 inst->alg.decrypt = crypto_cbc_decrypt;
192 err = skcipher_register_instance(tmpl, inst);
193 if (err) {
194 out_free_inst:
195 inst->free(inst);
198 return err;
201 static struct crypto_template crypto_cbc_tmpl = {
202 .name = "cbc",
203 .create = crypto_cbc_create,
204 .module = THIS_MODULE,
207 static int __init crypto_cbc_module_init(void)
209 return crypto_register_template(&crypto_cbc_tmpl);
212 static void __exit crypto_cbc_module_exit(void)
214 crypto_unregister_template(&crypto_cbc_tmpl);
217 subsys_initcall(crypto_cbc_module_init);
218 module_exit(crypto_cbc_module_exit);
220 MODULE_LICENSE("GPL");
221 MODULE_DESCRIPTION("CBC block cipher mode of operation");
222 MODULE_ALIAS_CRYPTO("cbc");