]>
Commit | Line | Data |
---|---|---|
91652be5 DH |
1 | /* |
2 | * PCBC: Propagating Cipher Block Chaining mode | |
3 | * | |
4 | * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. | |
5 | * Written by David Howells ([email protected]) | |
6 | * | |
7 | * Derived from cbc.c | |
8 | * - Copyright (c) 2006 Herbert Xu <[email protected]> | |
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 | ||
6650c4de | 17 | #include <crypto/algapi.h> |
043a4400 | 18 | #include <crypto/internal/skcipher.h> |
91652be5 DH |
19 | #include <linux/err.h> |
20 | #include <linux/init.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/module.h> | |
91652be5 | 23 | #include <linux/slab.h> |
d8c34b94 | 24 | #include <linux/compiler.h> |
91652be5 DH |
25 | |
26 | struct crypto_pcbc_ctx { | |
27 | struct crypto_cipher *child; | |
91652be5 DH |
28 | }; |
29 | ||
043a4400 | 30 | static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key, |
91652be5 DH |
31 | unsigned int keylen) |
32 | { | |
043a4400 | 33 | struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent); |
91652be5 DH |
34 | struct crypto_cipher *child = ctx->child; |
35 | int err; | |
36 | ||
37 | crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); | |
043a4400 HX |
38 | crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) & |
39 | CRYPTO_TFM_REQ_MASK); | |
91652be5 | 40 | err = crypto_cipher_setkey(child, key, keylen); |
043a4400 HX |
41 | crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) & |
42 | CRYPTO_TFM_RES_MASK); | |
91652be5 DH |
43 | return err; |
44 | } | |
45 | ||
043a4400 HX |
46 | static int crypto_pcbc_encrypt_segment(struct skcipher_request *req, |
47 | struct skcipher_walk *walk, | |
d0b9007a | 48 | struct crypto_cipher *tfm) |
91652be5 | 49 | { |
91652be5 DH |
50 | int bsize = crypto_cipher_blocksize(tfm); |
51 | unsigned int nbytes = walk->nbytes; | |
52 | u8 *src = walk->src.virt.addr; | |
53 | u8 *dst = walk->dst.virt.addr; | |
54 | u8 *iv = walk->iv; | |
55 | ||
56 | do { | |
d0b9007a | 57 | crypto_xor(iv, src, bsize); |
043a4400 | 58 | crypto_cipher_encrypt_one(tfm, dst, iv); |
45fe93df | 59 | crypto_xor_cpy(iv, dst, src, bsize); |
91652be5 DH |
60 | |
61 | src += bsize; | |
62 | dst += bsize; | |
63 | } while ((nbytes -= bsize) >= bsize); | |
64 | ||
65 | return nbytes; | |
66 | } | |
67 | ||
043a4400 HX |
68 | static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req, |
69 | struct skcipher_walk *walk, | |
d0b9007a | 70 | struct crypto_cipher *tfm) |
91652be5 | 71 | { |
91652be5 DH |
72 | int bsize = crypto_cipher_blocksize(tfm); |
73 | unsigned int nbytes = walk->nbytes; | |
74 | u8 *src = walk->src.virt.addr; | |
75 | u8 *iv = walk->iv; | |
6650c4de | 76 | u8 tmpbuf[MAX_CIPHER_BLOCKSIZE]; |
91652be5 DH |
77 | |
78 | do { | |
79 | memcpy(tmpbuf, src, bsize); | |
d0b9007a | 80 | crypto_xor(iv, src, bsize); |
043a4400 | 81 | crypto_cipher_encrypt_one(tfm, src, iv); |
45fe93df | 82 | crypto_xor_cpy(iv, tmpbuf, src, bsize); |
91652be5 DH |
83 | |
84 | src += bsize; | |
85 | } while ((nbytes -= bsize) >= bsize); | |
86 | ||
87 | memcpy(walk->iv, iv, bsize); | |
88 | ||
89 | return nbytes; | |
90 | } | |
91 | ||
043a4400 | 92 | static int crypto_pcbc_encrypt(struct skcipher_request *req) |
91652be5 | 93 | { |
043a4400 HX |
94 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
95 | struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); | |
91652be5 | 96 | struct crypto_cipher *child = ctx->child; |
043a4400 HX |
97 | struct skcipher_walk walk; |
98 | unsigned int nbytes; | |
91652be5 DH |
99 | int err; |
100 | ||
043a4400 | 101 | err = skcipher_walk_virt(&walk, req, false); |
91652be5 DH |
102 | |
103 | while ((nbytes = walk.nbytes)) { | |
104 | if (walk.src.virt.addr == walk.dst.virt.addr) | |
043a4400 | 105 | nbytes = crypto_pcbc_encrypt_inplace(req, &walk, |
d0b9007a | 106 | child); |
91652be5 | 107 | else |
043a4400 | 108 | nbytes = crypto_pcbc_encrypt_segment(req, &walk, |
d0b9007a | 109 | child); |
043a4400 | 110 | err = skcipher_walk_done(&walk, nbytes); |
91652be5 DH |
111 | } |
112 | ||
113 | return err; | |
114 | } | |
115 | ||
043a4400 HX |
116 | static int crypto_pcbc_decrypt_segment(struct skcipher_request *req, |
117 | struct skcipher_walk *walk, | |
d0b9007a | 118 | struct crypto_cipher *tfm) |
91652be5 | 119 | { |
91652be5 DH |
120 | int bsize = crypto_cipher_blocksize(tfm); |
121 | unsigned int nbytes = walk->nbytes; | |
122 | u8 *src = walk->src.virt.addr; | |
123 | u8 *dst = walk->dst.virt.addr; | |
124 | u8 *iv = walk->iv; | |
125 | ||
126 | do { | |
043a4400 | 127 | crypto_cipher_decrypt_one(tfm, dst, src); |
d0b9007a | 128 | crypto_xor(dst, iv, bsize); |
45fe93df | 129 | crypto_xor_cpy(iv, dst, src, bsize); |
91652be5 DH |
130 | |
131 | src += bsize; | |
132 | dst += bsize; | |
133 | } while ((nbytes -= bsize) >= bsize); | |
134 | ||
135 | memcpy(walk->iv, iv, bsize); | |
136 | ||
137 | return nbytes; | |
138 | } | |
139 | ||
043a4400 HX |
140 | static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req, |
141 | struct skcipher_walk *walk, | |
d0b9007a | 142 | struct crypto_cipher *tfm) |
91652be5 | 143 | { |
91652be5 DH |
144 | int bsize = crypto_cipher_blocksize(tfm); |
145 | unsigned int nbytes = walk->nbytes; | |
146 | u8 *src = walk->src.virt.addr; | |
147 | u8 *iv = walk->iv; | |
6650c4de | 148 | u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32)); |
91652be5 DH |
149 | |
150 | do { | |
151 | memcpy(tmpbuf, src, bsize); | |
043a4400 | 152 | crypto_cipher_decrypt_one(tfm, src, src); |
d0b9007a | 153 | crypto_xor(src, iv, bsize); |
45fe93df | 154 | crypto_xor_cpy(iv, src, tmpbuf, bsize); |
91652be5 DH |
155 | |
156 | src += bsize; | |
157 | } while ((nbytes -= bsize) >= bsize); | |
158 | ||
159 | memcpy(walk->iv, iv, bsize); | |
160 | ||
161 | return nbytes; | |
162 | } | |
163 | ||
043a4400 | 164 | static int crypto_pcbc_decrypt(struct skcipher_request *req) |
91652be5 | 165 | { |
043a4400 HX |
166 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
167 | struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); | |
91652be5 | 168 | struct crypto_cipher *child = ctx->child; |
043a4400 HX |
169 | struct skcipher_walk walk; |
170 | unsigned int nbytes; | |
91652be5 DH |
171 | int err; |
172 | ||
043a4400 | 173 | err = skcipher_walk_virt(&walk, req, false); |
91652be5 DH |
174 | |
175 | while ((nbytes = walk.nbytes)) { | |
176 | if (walk.src.virt.addr == walk.dst.virt.addr) | |
043a4400 | 177 | nbytes = crypto_pcbc_decrypt_inplace(req, &walk, |
d0b9007a | 178 | child); |
91652be5 | 179 | else |
043a4400 | 180 | nbytes = crypto_pcbc_decrypt_segment(req, &walk, |
d0b9007a | 181 | child); |
043a4400 | 182 | err = skcipher_walk_done(&walk, nbytes); |
91652be5 DH |
183 | } |
184 | ||
185 | return err; | |
186 | } | |
187 | ||
043a4400 | 188 | static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm) |
91652be5 | 189 | { |
043a4400 HX |
190 | struct skcipher_instance *inst = skcipher_alg_instance(tfm); |
191 | struct crypto_spawn *spawn = skcipher_instance_ctx(inst); | |
192 | struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); | |
2e306ee0 | 193 | struct crypto_cipher *cipher; |
91652be5 | 194 | |
2e306ee0 HX |
195 | cipher = crypto_spawn_cipher(spawn); |
196 | if (IS_ERR(cipher)) | |
197 | return PTR_ERR(cipher); | |
91652be5 | 198 | |
2e306ee0 | 199 | ctx->child = cipher; |
91652be5 DH |
200 | return 0; |
201 | } | |
202 | ||
043a4400 | 203 | static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm) |
91652be5 | 204 | { |
043a4400 HX |
205 | struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
206 | ||
91652be5 DH |
207 | crypto_free_cipher(ctx->child); |
208 | } | |
209 | ||
043a4400 HX |
210 | static void crypto_pcbc_free(struct skcipher_instance *inst) |
211 | { | |
212 | crypto_drop_skcipher(skcipher_instance_ctx(inst)); | |
213 | kfree(inst); | |
214 | } | |
215 | ||
216 | static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb) | |
91652be5 | 217 | { |
043a4400 HX |
218 | struct skcipher_instance *inst; |
219 | struct crypto_attr_type *algt; | |
220 | struct crypto_spawn *spawn; | |
91652be5 | 221 | struct crypto_alg *alg; |
ebc610e5 HX |
222 | int err; |
223 | ||
043a4400 HX |
224 | algt = crypto_get_attr_type(tb); |
225 | if (IS_ERR(algt)) | |
226 | return PTR_ERR(algt); | |
227 | ||
228 | if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) & | |
229 | ~CRYPTO_ALG_INTERNAL) | |
230 | return -EINVAL; | |
91652be5 | 231 | |
043a4400 HX |
232 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
233 | if (!inst) | |
234 | return -ENOMEM; | |
235 | ||
236 | alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER | | |
237 | (algt->type & CRYPTO_ALG_INTERNAL), | |
238 | CRYPTO_ALG_TYPE_MASK | | |
239 | (algt->mask & CRYPTO_ALG_INTERNAL)); | |
240 | err = PTR_ERR(alg); | |
91652be5 | 241 | if (IS_ERR(alg)) |
043a4400 HX |
242 | goto err_free_inst; |
243 | ||
244 | spawn = skcipher_instance_ctx(inst); | |
245 | err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst), | |
246 | CRYPTO_ALG_TYPE_MASK); | |
043a4400 | 247 | if (err) |
e5bde04c | 248 | goto err_put_alg; |
91652be5 | 249 | |
043a4400 HX |
250 | err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg); |
251 | if (err) | |
252 | goto err_drop_spawn; | |
91652be5 | 253 | |
043a4400 HX |
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; | |
91652be5 | 258 | |
043a4400 HX |
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; | |
91652be5 | 262 | |
043a4400 | 263 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx); |
91652be5 | 264 | |
043a4400 HX |
265 | inst->alg.init = crypto_pcbc_init_tfm; |
266 | inst->alg.exit = crypto_pcbc_exit_tfm; | |
91652be5 | 267 | |
043a4400 HX |
268 | inst->alg.setkey = crypto_pcbc_setkey; |
269 | inst->alg.encrypt = crypto_pcbc_encrypt; | |
270 | inst->alg.decrypt = crypto_pcbc_decrypt; | |
91652be5 | 271 | |
043a4400 | 272 | inst->free = crypto_pcbc_free; |
91652be5 | 273 | |
043a4400 HX |
274 | err = skcipher_register_instance(tmpl, inst); |
275 | if (err) | |
276 | goto err_drop_spawn; | |
e5bde04c | 277 | crypto_mod_put(alg); |
043a4400 HX |
278 | |
279 | out: | |
280 | return err; | |
281 | ||
282 | err_drop_spawn: | |
283 | crypto_drop_spawn(spawn); | |
e5bde04c PB |
284 | err_put_alg: |
285 | crypto_mod_put(alg); | |
043a4400 | 286 | err_free_inst: |
91652be5 | 287 | kfree(inst); |
043a4400 | 288 | goto out; |
91652be5 DH |
289 | } |
290 | ||
291 | static struct crypto_template crypto_pcbc_tmpl = { | |
292 | .name = "pcbc", | |
043a4400 | 293 | .create = crypto_pcbc_create, |
91652be5 DH |
294 | .module = THIS_MODULE, |
295 | }; | |
296 | ||
297 | static int __init crypto_pcbc_module_init(void) | |
298 | { | |
299 | return crypto_register_template(&crypto_pcbc_tmpl); | |
300 | } | |
301 | ||
302 | static void __exit crypto_pcbc_module_exit(void) | |
303 | { | |
304 | crypto_unregister_template(&crypto_pcbc_tmpl); | |
305 | } | |
306 | ||
307 | module_init(crypto_pcbc_module_init); | |
308 | module_exit(crypto_pcbc_module_exit); | |
309 | ||
310 | MODULE_LICENSE("GPL"); | |
311 | MODULE_DESCRIPTION("PCBC block cipher algorithm"); | |
4943ba16 | 312 | MODULE_ALIAS_CRYPTO("pcbc"); |