hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/crypto/cfb.c
....@@ -20,33 +20,23 @@
2020 */
2121
2222 #include <crypto/algapi.h>
23
+#include <crypto/internal/cipher.h>
2324 #include <crypto/internal/skcipher.h>
2425 #include <linux/err.h>
2526 #include <linux/init.h>
2627 #include <linux/kernel.h>
2728 #include <linux/module.h>
28
-#include <linux/slab.h>
2929 #include <linux/string.h>
30
-#include <linux/types.h>
31
-
32
-struct crypto_cfb_ctx {
33
- struct crypto_cipher *child;
34
-};
3530
3631 static unsigned int crypto_cfb_bsize(struct crypto_skcipher *tfm)
3732 {
38
- struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm);
39
- struct crypto_cipher *child = ctx->child;
40
-
41
- return crypto_cipher_blocksize(child);
33
+ return crypto_cipher_blocksize(skcipher_cipher_simple(tfm));
4234 }
4335
4436 static void crypto_cfb_encrypt_one(struct crypto_skcipher *tfm,
4537 const u8 *src, u8 *dst)
4638 {
47
- struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm);
48
-
49
- crypto_cipher_encrypt_one(ctx->child, dst, src);
39
+ crypto_cipher_encrypt_one(skcipher_cipher_simple(tfm), dst, src);
5040 }
5141
5242 /* final encrypt and decrypt is the same */
....@@ -186,22 +176,6 @@
186176 return crypto_cfb_decrypt_segment(walk, tfm);
187177 }
188178
189
-static int crypto_cfb_setkey(struct crypto_skcipher *parent, const u8 *key,
190
- unsigned int keylen)
191
-{
192
- struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(parent);
193
- struct crypto_cipher *child = ctx->child;
194
- int err;
195
-
196
- crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
197
- crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
198
- CRYPTO_TFM_REQ_MASK);
199
- err = crypto_cipher_setkey(child, key, keylen);
200
- crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
201
- CRYPTO_TFM_RES_MASK);
202
- return err;
203
-}
204
-
205179 static int crypto_cfb_decrypt(struct skcipher_request *req)
206180 {
207181 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
....@@ -224,79 +198,20 @@
224198 return err;
225199 }
226200
227
-static int crypto_cfb_init_tfm(struct crypto_skcipher *tfm)
228
-{
229
- struct skcipher_instance *inst = skcipher_alg_instance(tfm);
230
- struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
231
- struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm);
232
- struct crypto_cipher *cipher;
233
-
234
- cipher = crypto_spawn_cipher(spawn);
235
- if (IS_ERR(cipher))
236
- return PTR_ERR(cipher);
237
-
238
- ctx->child = cipher;
239
- return 0;
240
-}
241
-
242
-static void crypto_cfb_exit_tfm(struct crypto_skcipher *tfm)
243
-{
244
- struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm);
245
-
246
- crypto_free_cipher(ctx->child);
247
-}
248
-
249
-static void crypto_cfb_free(struct skcipher_instance *inst)
250
-{
251
- crypto_drop_skcipher(skcipher_instance_ctx(inst));
252
- kfree(inst);
253
-}
254
-
255201 static int crypto_cfb_create(struct crypto_template *tmpl, struct rtattr **tb)
256202 {
257203 struct skcipher_instance *inst;
258
- struct crypto_attr_type *algt;
259
- struct crypto_spawn *spawn;
260204 struct crypto_alg *alg;
261
- u32 mask;
262205 int err;
263206
264
- err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER);
265
- if (err)
266
- return err;
207
+ inst = skcipher_alloc_instance_simple(tmpl, tb);
208
+ if (IS_ERR(inst))
209
+ return PTR_ERR(inst);
267210
268
- inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
269
- if (!inst)
270
- return -ENOMEM;
211
+ alg = skcipher_ialg_simple(inst);
271212
272
- algt = crypto_get_attr_type(tb);
273
- err = PTR_ERR(algt);
274
- if (IS_ERR(algt))
275
- goto err_free_inst;
276
-
277
- mask = CRYPTO_ALG_TYPE_MASK |
278
- crypto_requires_off(algt->type, algt->mask,
279
- CRYPTO_ALG_NEED_FALLBACK);
280
-
281
- alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, mask);
282
- err = PTR_ERR(alg);
283
- if (IS_ERR(alg))
284
- goto err_free_inst;
285
-
286
- spawn = skcipher_instance_ctx(inst);
287
- err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
288
- CRYPTO_ALG_TYPE_MASK);
289
- if (err)
290
- goto err_put_alg;
291
-
292
- err = crypto_inst_setname(skcipher_crypto_instance(inst), "cfb", alg);
293
- if (err)
294
- goto err_drop_spawn;
295
-
296
- inst->alg.base.cra_priority = alg->cra_priority;
297
- /* we're a stream cipher independend of the crypto cra_blocksize */
213
+ /* CFB mode is a stream cipher. */
298214 inst->alg.base.cra_blocksize = 1;
299
- inst->alg.base.cra_alignmask = alg->cra_alignmask;
300215
301216 /*
302217 * To simplify the implementation, configure the skcipher walk to only
....@@ -304,36 +219,14 @@
304219 */
305220 inst->alg.chunksize = alg->cra_blocksize;
306221
307
- inst->alg.ivsize = alg->cra_blocksize;
308
- inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
309
- inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
310
-
311
- inst->alg.base.cra_ctxsize = sizeof(struct crypto_cfb_ctx);
312
-
313
- inst->alg.init = crypto_cfb_init_tfm;
314
- inst->alg.exit = crypto_cfb_exit_tfm;
315
-
316
- inst->alg.setkey = crypto_cfb_setkey;
317222 inst->alg.encrypt = crypto_cfb_encrypt;
318223 inst->alg.decrypt = crypto_cfb_decrypt;
319224
320
- inst->free = crypto_cfb_free;
321
-
322225 err = skcipher_register_instance(tmpl, inst);
323226 if (err)
324
- goto err_drop_spawn;
325
- crypto_mod_put(alg);
227
+ inst->free(inst);
326228
327
-out:
328229 return err;
329
-
330
-err_drop_spawn:
331
- crypto_drop_spawn(spawn);
332
-err_put_alg:
333
- crypto_mod_put(alg);
334
-err_free_inst:
335
- kfree(inst);
336
- goto out;
337230 }
338231
339232 static struct crypto_template crypto_cfb_tmpl = {
....@@ -352,9 +245,10 @@
352245 crypto_unregister_template(&crypto_cfb_tmpl);
353246 }
354247
355
-module_init(crypto_cfb_module_init);
248
+subsys_initcall(crypto_cfb_module_init);
356249 module_exit(crypto_cfb_module_exit);
357250
358251 MODULE_LICENSE("GPL");
359
-MODULE_DESCRIPTION("CFB block cipher algorithm");
252
+MODULE_DESCRIPTION("CFB block cipher mode of operation");
360253 MODULE_ALIAS_CRYPTO("cfb");
254
+MODULE_IMPORT_NS(CRYPTO_INTERNAL);