.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * PCBC: Propagating Cipher Block Chaining mode |
---|
3 | 4 | * |
---|
.. | .. |
---|
6 | 7 | * |
---|
7 | 8 | * Derived from cbc.c |
---|
8 | 9 | * - 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 | 10 | */ |
---|
16 | 11 | |
---|
17 | 12 | #include <crypto/algapi.h> |
---|
| 13 | +#include <crypto/internal/cipher.h> |
---|
18 | 14 | #include <crypto/internal/skcipher.h> |
---|
19 | 15 | #include <linux/err.h> |
---|
20 | 16 | #include <linux/init.h> |
---|
21 | 17 | #include <linux/kernel.h> |
---|
22 | 18 | #include <linux/module.h> |
---|
23 | | -#include <linux/slab.h> |
---|
24 | | -#include <linux/compiler.h> |
---|
25 | | - |
---|
26 | | -struct crypto_pcbc_ctx { |
---|
27 | | - struct crypto_cipher *child; |
---|
28 | | -}; |
---|
29 | | - |
---|
30 | | -static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key, |
---|
31 | | - unsigned int keylen) |
---|
32 | | -{ |
---|
33 | | - struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent); |
---|
34 | | - struct crypto_cipher *child = ctx->child; |
---|
35 | | - int err; |
---|
36 | | - |
---|
37 | | - crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
---|
38 | | - crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) & |
---|
39 | | - CRYPTO_TFM_REQ_MASK); |
---|
40 | | - err = crypto_cipher_setkey(child, key, keylen); |
---|
41 | | - crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) & |
---|
42 | | - CRYPTO_TFM_RES_MASK); |
---|
43 | | - return err; |
---|
44 | | -} |
---|
45 | 19 | |
---|
46 | 20 | static int crypto_pcbc_encrypt_segment(struct skcipher_request *req, |
---|
47 | 21 | struct skcipher_walk *walk, |
---|
.. | .. |
---|
90 | 64 | static int crypto_pcbc_encrypt(struct skcipher_request *req) |
---|
91 | 65 | { |
---|
92 | 66 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
---|
93 | | - struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
---|
94 | | - struct crypto_cipher *child = ctx->child; |
---|
| 67 | + struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
---|
95 | 68 | struct skcipher_walk walk; |
---|
96 | 69 | unsigned int nbytes; |
---|
97 | 70 | int err; |
---|
.. | .. |
---|
101 | 74 | while ((nbytes = walk.nbytes)) { |
---|
102 | 75 | if (walk.src.virt.addr == walk.dst.virt.addr) |
---|
103 | 76 | nbytes = crypto_pcbc_encrypt_inplace(req, &walk, |
---|
104 | | - child); |
---|
| 77 | + cipher); |
---|
105 | 78 | else |
---|
106 | 79 | nbytes = crypto_pcbc_encrypt_segment(req, &walk, |
---|
107 | | - child); |
---|
| 80 | + cipher); |
---|
108 | 81 | err = skcipher_walk_done(&walk, nbytes); |
---|
109 | 82 | } |
---|
110 | 83 | |
---|
.. | .. |
---|
158 | 131 | static int crypto_pcbc_decrypt(struct skcipher_request *req) |
---|
159 | 132 | { |
---|
160 | 133 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
---|
161 | | - struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
---|
162 | | - struct crypto_cipher *child = ctx->child; |
---|
| 134 | + struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
---|
163 | 135 | struct skcipher_walk walk; |
---|
164 | 136 | unsigned int nbytes; |
---|
165 | 137 | int err; |
---|
.. | .. |
---|
169 | 141 | while ((nbytes = walk.nbytes)) { |
---|
170 | 142 | if (walk.src.virt.addr == walk.dst.virt.addr) |
---|
171 | 143 | nbytes = crypto_pcbc_decrypt_inplace(req, &walk, |
---|
172 | | - child); |
---|
| 144 | + cipher); |
---|
173 | 145 | else |
---|
174 | 146 | nbytes = crypto_pcbc_decrypt_segment(req, &walk, |
---|
175 | | - child); |
---|
| 147 | + cipher); |
---|
176 | 148 | err = skcipher_walk_done(&walk, nbytes); |
---|
177 | 149 | } |
---|
178 | 150 | |
---|
179 | 151 | return err; |
---|
180 | 152 | } |
---|
181 | 153 | |
---|
182 | | -static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm) |
---|
183 | | -{ |
---|
184 | | - struct skcipher_instance *inst = skcipher_alg_instance(tfm); |
---|
185 | | - struct crypto_spawn *spawn = skcipher_instance_ctx(inst); |
---|
186 | | - struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
---|
187 | | - struct crypto_cipher *cipher; |
---|
188 | | - |
---|
189 | | - cipher = crypto_spawn_cipher(spawn); |
---|
190 | | - if (IS_ERR(cipher)) |
---|
191 | | - return PTR_ERR(cipher); |
---|
192 | | - |
---|
193 | | - ctx->child = cipher; |
---|
194 | | - return 0; |
---|
195 | | -} |
---|
196 | | - |
---|
197 | | -static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm) |
---|
198 | | -{ |
---|
199 | | - struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
---|
200 | | - |
---|
201 | | - crypto_free_cipher(ctx->child); |
---|
202 | | -} |
---|
203 | | - |
---|
204 | | -static void crypto_pcbc_free(struct skcipher_instance *inst) |
---|
205 | | -{ |
---|
206 | | - crypto_drop_skcipher(skcipher_instance_ctx(inst)); |
---|
207 | | - kfree(inst); |
---|
208 | | -} |
---|
209 | | - |
---|
210 | 154 | static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb) |
---|
211 | 155 | { |
---|
212 | 156 | struct skcipher_instance *inst; |
---|
213 | | - struct crypto_attr_type *algt; |
---|
214 | | - struct crypto_spawn *spawn; |
---|
215 | | - struct crypto_alg *alg; |
---|
216 | 157 | int err; |
---|
217 | 158 | |
---|
218 | | - algt = crypto_get_attr_type(tb); |
---|
219 | | - if (IS_ERR(algt)) |
---|
220 | | - return PTR_ERR(algt); |
---|
| 159 | + inst = skcipher_alloc_instance_simple(tmpl, tb); |
---|
| 160 | + if (IS_ERR(inst)) |
---|
| 161 | + return PTR_ERR(inst); |
---|
221 | 162 | |
---|
222 | | - if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) & |
---|
223 | | - ~CRYPTO_ALG_INTERNAL) |
---|
224 | | - return -EINVAL; |
---|
225 | | - |
---|
226 | | - inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
---|
227 | | - if (!inst) |
---|
228 | | - return -ENOMEM; |
---|
229 | | - |
---|
230 | | - alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER | |
---|
231 | | - (algt->type & CRYPTO_ALG_INTERNAL), |
---|
232 | | - CRYPTO_ALG_TYPE_MASK | |
---|
233 | | - (algt->mask & CRYPTO_ALG_INTERNAL)); |
---|
234 | | - err = PTR_ERR(alg); |
---|
235 | | - if (IS_ERR(alg)) |
---|
236 | | - goto err_free_inst; |
---|
237 | | - |
---|
238 | | - spawn = skcipher_instance_ctx(inst); |
---|
239 | | - err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst), |
---|
240 | | - CRYPTO_ALG_TYPE_MASK); |
---|
241 | | - if (err) |
---|
242 | | - goto err_put_alg; |
---|
243 | | - |
---|
244 | | - err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg); |
---|
245 | | - if (err) |
---|
246 | | - goto err_drop_spawn; |
---|
247 | | - |
---|
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; |
---|
252 | | - |
---|
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; |
---|
256 | | - |
---|
257 | | - inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx); |
---|
258 | | - |
---|
259 | | - inst->alg.init = crypto_pcbc_init_tfm; |
---|
260 | | - inst->alg.exit = crypto_pcbc_exit_tfm; |
---|
261 | | - |
---|
262 | | - inst->alg.setkey = crypto_pcbc_setkey; |
---|
263 | 163 | inst->alg.encrypt = crypto_pcbc_encrypt; |
---|
264 | 164 | inst->alg.decrypt = crypto_pcbc_decrypt; |
---|
265 | 165 | |
---|
266 | | - inst->free = crypto_pcbc_free; |
---|
267 | | - |
---|
268 | 166 | err = skcipher_register_instance(tmpl, inst); |
---|
269 | 167 | if (err) |
---|
270 | | - goto err_drop_spawn; |
---|
271 | | - crypto_mod_put(alg); |
---|
| 168 | + inst->free(inst); |
---|
272 | 169 | |
---|
273 | | -out: |
---|
274 | 170 | return err; |
---|
275 | | - |
---|
276 | | -err_drop_spawn: |
---|
277 | | - crypto_drop_spawn(spawn); |
---|
278 | | -err_put_alg: |
---|
279 | | - crypto_mod_put(alg); |
---|
280 | | -err_free_inst: |
---|
281 | | - kfree(inst); |
---|
282 | | - goto out; |
---|
283 | 171 | } |
---|
284 | 172 | |
---|
285 | 173 | static struct crypto_template crypto_pcbc_tmpl = { |
---|
.. | .. |
---|
298 | 186 | crypto_unregister_template(&crypto_pcbc_tmpl); |
---|
299 | 187 | } |
---|
300 | 188 | |
---|
301 | | -module_init(crypto_pcbc_module_init); |
---|
| 189 | +subsys_initcall(crypto_pcbc_module_init); |
---|
302 | 190 | module_exit(crypto_pcbc_module_exit); |
---|
303 | 191 | |
---|
304 | 192 | MODULE_LICENSE("GPL"); |
---|
305 | | -MODULE_DESCRIPTION("PCBC block cipher algorithm"); |
---|
| 193 | +MODULE_DESCRIPTION("PCBC block cipher mode of operation"); |
---|
306 | 194 | MODULE_ALIAS_CRYPTO("pcbc"); |
---|
| 195 | +MODULE_IMPORT_NS(CRYPTO_INTERNAL); |
---|