Loading...
1/*
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
17#include <crypto/internal/skcipher.h>
18#include <linux/err.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/compiler.h>
24
25struct crypto_pcbc_ctx {
26 struct crypto_cipher *child;
27};
28
29static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key,
30 unsigned int keylen)
31{
32 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent);
33 struct crypto_cipher *child = ctx->child;
34 int err;
35
36 crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
37 crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
38 CRYPTO_TFM_REQ_MASK);
39 err = crypto_cipher_setkey(child, key, keylen);
40 crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
41 CRYPTO_TFM_RES_MASK);
42 return err;
43}
44
45static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
46 struct skcipher_walk *walk,
47 struct crypto_cipher *tfm)
48{
49 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 {
56 crypto_xor(iv, src, bsize);
57 crypto_cipher_encrypt_one(tfm, dst, iv);
58 crypto_xor_cpy(iv, dst, src, bsize);
59
60 src += bsize;
61 dst += bsize;
62 } while ((nbytes -= bsize) >= bsize);
63
64 return nbytes;
65}
66
67static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
68 struct skcipher_walk *walk,
69 struct crypto_cipher *tfm)
70{
71 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);
79 crypto_xor(iv, src, bsize);
80 crypto_cipher_encrypt_one(tfm, src, iv);
81 crypto_xor_cpy(iv, tmpbuf, src, bsize);
82
83 src += bsize;
84 } while ((nbytes -= bsize) >= bsize);
85
86 memcpy(walk->iv, iv, bsize);
87
88 return nbytes;
89}
90
91static int crypto_pcbc_encrypt(struct skcipher_request *req)
92{
93 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
94 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
95 struct crypto_cipher *child = ctx->child;
96 struct skcipher_walk walk;
97 unsigned int nbytes;
98 int err;
99
100 err = skcipher_walk_virt(&walk, req, false);
101
102 while ((nbytes = walk.nbytes)) {
103 if (walk.src.virt.addr == walk.dst.virt.addr)
104 nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
105 child);
106 else
107 nbytes = crypto_pcbc_encrypt_segment(req, &walk,
108 child);
109 err = skcipher_walk_done(&walk, nbytes);
110 }
111
112 return err;
113}
114
115static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
116 struct skcipher_walk *walk,
117 struct crypto_cipher *tfm)
118{
119 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 {
126 crypto_cipher_decrypt_one(tfm, dst, src);
127 crypto_xor(dst, iv, bsize);
128 crypto_xor_cpy(iv, dst, src, bsize);
129
130 src += bsize;
131 dst += bsize;
132 } while ((nbytes -= bsize) >= bsize);
133
134 memcpy(walk->iv, iv, bsize);
135
136 return nbytes;
137}
138
139static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
140 struct skcipher_walk *walk,
141 struct crypto_cipher *tfm)
142{
143 int bsize = crypto_cipher_blocksize(tfm);
144 unsigned int nbytes = walk->nbytes;
145 u8 *src = walk->src.virt.addr;
146 u8 *iv = walk->iv;
147 u8 tmpbuf[bsize] __aligned(__alignof__(u32));
148
149 do {
150 memcpy(tmpbuf, src, bsize);
151 crypto_cipher_decrypt_one(tfm, src, src);
152 crypto_xor(src, iv, bsize);
153 crypto_xor_cpy(iv, src, tmpbuf, bsize);
154
155 src += bsize;
156 } while ((nbytes -= bsize) >= bsize);
157
158 memcpy(walk->iv, iv, bsize);
159
160 return nbytes;
161}
162
163static int crypto_pcbc_decrypt(struct skcipher_request *req)
164{
165 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
166 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
167 struct crypto_cipher *child = ctx->child;
168 struct skcipher_walk walk;
169 unsigned int nbytes;
170 int err;
171
172 err = skcipher_walk_virt(&walk, req, false);
173
174 while ((nbytes = walk.nbytes)) {
175 if (walk.src.virt.addr == walk.dst.virt.addr)
176 nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
177 child);
178 else
179 nbytes = crypto_pcbc_decrypt_segment(req, &walk,
180 child);
181 err = skcipher_walk_done(&walk, nbytes);
182 }
183
184 return err;
185}
186
187static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
188{
189 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);
192 struct crypto_cipher *cipher;
193
194 cipher = crypto_spawn_cipher(spawn);
195 if (IS_ERR(cipher))
196 return PTR_ERR(cipher);
197
198 ctx->child = cipher;
199 return 0;
200}
201
202static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
203{
204 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
205
206 crypto_free_cipher(ctx->child);
207}
208
209static void crypto_pcbc_free(struct skcipher_instance *inst)
210{
211 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;
220 struct crypto_alg *alg;
221 int err;
222
223 algt = crypto_get_attr_type(tb);
224 if (IS_ERR(algt))
225 return PTR_ERR(algt);
226
227 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);
240 if (IS_ERR(alg))
241 goto err_free_inst;
242
243 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;
249
250 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;
258
259 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;
262
263 inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
264
265 inst->alg.init = crypto_pcbc_init_tfm;
266 inst->alg.exit = crypto_pcbc_exit_tfm;
267
268 inst->alg.setkey = crypto_pcbc_setkey;
269 inst->alg.encrypt = crypto_pcbc_encrypt;
270 inst->alg.decrypt = crypto_pcbc_decrypt;
271
272 inst->free = crypto_pcbc_free;
273
274 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:
284 kfree(inst);
285 goto out;
286}
287
288static struct crypto_template crypto_pcbc_tmpl = {
289 .name = "pcbc",
290 .create = crypto_pcbc_create,
291 .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");
309MODULE_ALIAS_CRYPTO("pcbc");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
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>
10 */
11
12#include <crypto/algapi.h>
13#include <crypto/internal/cipher.h>
14#include <crypto/internal/skcipher.h>
15#include <linux/err.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19
20static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
21 struct skcipher_walk *walk,
22 struct crypto_cipher *tfm)
23{
24 int bsize = crypto_cipher_blocksize(tfm);
25 unsigned int nbytes = walk->nbytes;
26 u8 *src = walk->src.virt.addr;
27 u8 *dst = walk->dst.virt.addr;
28 u8 * const iv = walk->iv;
29
30 do {
31 crypto_xor(iv, src, bsize);
32 crypto_cipher_encrypt_one(tfm, dst, iv);
33 crypto_xor_cpy(iv, dst, src, bsize);
34
35 src += bsize;
36 dst += bsize;
37 } while ((nbytes -= bsize) >= bsize);
38
39 return nbytes;
40}
41
42static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
43 struct skcipher_walk *walk,
44 struct crypto_cipher *tfm)
45{
46 int bsize = crypto_cipher_blocksize(tfm);
47 unsigned int nbytes = walk->nbytes;
48 u8 *src = walk->src.virt.addr;
49 u8 * const iv = walk->iv;
50 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE];
51
52 do {
53 memcpy(tmpbuf, src, bsize);
54 crypto_xor(iv, src, bsize);
55 crypto_cipher_encrypt_one(tfm, src, iv);
56 crypto_xor_cpy(iv, tmpbuf, src, bsize);
57
58 src += bsize;
59 } while ((nbytes -= bsize) >= bsize);
60
61 return nbytes;
62}
63
64static int crypto_pcbc_encrypt(struct skcipher_request *req)
65{
66 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
67 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
68 struct skcipher_walk walk;
69 unsigned int nbytes;
70 int err;
71
72 err = skcipher_walk_virt(&walk, req, false);
73
74 while (walk.nbytes) {
75 if (walk.src.virt.addr == walk.dst.virt.addr)
76 nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
77 cipher);
78 else
79 nbytes = crypto_pcbc_encrypt_segment(req, &walk,
80 cipher);
81 err = skcipher_walk_done(&walk, nbytes);
82 }
83
84 return err;
85}
86
87static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
88 struct skcipher_walk *walk,
89 struct crypto_cipher *tfm)
90{
91 int bsize = crypto_cipher_blocksize(tfm);
92 unsigned int nbytes = walk->nbytes;
93 u8 *src = walk->src.virt.addr;
94 u8 *dst = walk->dst.virt.addr;
95 u8 * const iv = walk->iv;
96
97 do {
98 crypto_cipher_decrypt_one(tfm, dst, src);
99 crypto_xor(dst, iv, bsize);
100 crypto_xor_cpy(iv, dst, src, bsize);
101
102 src += bsize;
103 dst += bsize;
104 } while ((nbytes -= bsize) >= bsize);
105
106 return nbytes;
107}
108
109static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
110 struct skcipher_walk *walk,
111 struct crypto_cipher *tfm)
112{
113 int bsize = crypto_cipher_blocksize(tfm);
114 unsigned int nbytes = walk->nbytes;
115 u8 *src = walk->src.virt.addr;
116 u8 * const iv = walk->iv;
117 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32));
118
119 do {
120 memcpy(tmpbuf, src, bsize);
121 crypto_cipher_decrypt_one(tfm, src, src);
122 crypto_xor(src, iv, bsize);
123 crypto_xor_cpy(iv, src, tmpbuf, bsize);
124
125 src += bsize;
126 } while ((nbytes -= bsize) >= bsize);
127
128 return nbytes;
129}
130
131static int crypto_pcbc_decrypt(struct skcipher_request *req)
132{
133 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
134 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
135 struct skcipher_walk walk;
136 unsigned int nbytes;
137 int err;
138
139 err = skcipher_walk_virt(&walk, req, false);
140
141 while (walk.nbytes) {
142 if (walk.src.virt.addr == walk.dst.virt.addr)
143 nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
144 cipher);
145 else
146 nbytes = crypto_pcbc_decrypt_segment(req, &walk,
147 cipher);
148 err = skcipher_walk_done(&walk, nbytes);
149 }
150
151 return err;
152}
153
154static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
155{
156 struct skcipher_instance *inst;
157 int err;
158
159 inst = skcipher_alloc_instance_simple(tmpl, tb);
160 if (IS_ERR(inst))
161 return PTR_ERR(inst);
162
163 inst->alg.encrypt = crypto_pcbc_encrypt;
164 inst->alg.decrypt = crypto_pcbc_decrypt;
165
166 err = skcipher_register_instance(tmpl, inst);
167 if (err)
168 inst->free(inst);
169
170 return err;
171}
172
173static struct crypto_template crypto_pcbc_tmpl = {
174 .name = "pcbc",
175 .create = crypto_pcbc_create,
176 .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
189subsys_initcall(crypto_pcbc_module_init);
190module_exit(crypto_pcbc_module_exit);
191
192MODULE_LICENSE("GPL");
193MODULE_DESCRIPTION("PCBC block cipher mode of operation");
194MODULE_ALIAS_CRYPTO("pcbc");
195MODULE_IMPORT_NS("CRYPTO_INTERNAL");