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