]> git.ipfire.org Git - thirdparty/linux.git/blob - crypto/cbc.c
Merge tag 'printk-for-5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/pmladek...
[thirdparty/linux.git] / crypto / cbc.c
1 /*
2 * CBC: Cipher Block Chaining mode
3 *
4 * Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13 #include <crypto/algapi.h>
14 #include <crypto/cbc.h>
15 #include <crypto/internal/skcipher.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/log2.h>
20 #include <linux/module.h>
21
22 static inline void crypto_cbc_encrypt_one(struct crypto_skcipher *tfm,
23 const u8 *src, u8 *dst)
24 {
25 crypto_cipher_encrypt_one(skcipher_cipher_simple(tfm), dst, src);
26 }
27
28 static int crypto_cbc_encrypt(struct skcipher_request *req)
29 {
30 return crypto_cbc_encrypt_walk(req, crypto_cbc_encrypt_one);
31 }
32
33 static inline void crypto_cbc_decrypt_one(struct crypto_skcipher *tfm,
34 const u8 *src, u8 *dst)
35 {
36 crypto_cipher_decrypt_one(skcipher_cipher_simple(tfm), dst, src);
37 }
38
39 static int crypto_cbc_decrypt(struct skcipher_request *req)
40 {
41 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
42 struct skcipher_walk walk;
43 int err;
44
45 err = skcipher_walk_virt(&walk, req, false);
46
47 while (walk.nbytes) {
48 err = crypto_cbc_decrypt_blocks(&walk, tfm,
49 crypto_cbc_decrypt_one);
50 err = skcipher_walk_done(&walk, err);
51 }
52
53 return err;
54 }
55
56 static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
57 {
58 struct skcipher_instance *inst;
59 struct crypto_alg *alg;
60 int err;
61
62 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
63 if (IS_ERR(inst))
64 return PTR_ERR(inst);
65
66 err = -EINVAL;
67 if (!is_power_of_2(alg->cra_blocksize))
68 goto out_free_inst;
69
70 inst->alg.encrypt = crypto_cbc_encrypt;
71 inst->alg.decrypt = crypto_cbc_decrypt;
72
73 err = skcipher_register_instance(tmpl, inst);
74 if (err)
75 goto out_free_inst;
76 goto out_put_alg;
77
78 out_free_inst:
79 inst->free(inst);
80 out_put_alg:
81 crypto_mod_put(alg);
82 return err;
83 }
84
85 static struct crypto_template crypto_cbc_tmpl = {
86 .name = "cbc",
87 .create = crypto_cbc_create,
88 .module = THIS_MODULE,
89 };
90
91 static int __init crypto_cbc_module_init(void)
92 {
93 return crypto_register_template(&crypto_cbc_tmpl);
94 }
95
96 static void __exit crypto_cbc_module_exit(void)
97 {
98 crypto_unregister_template(&crypto_cbc_tmpl);
99 }
100
101 module_init(crypto_cbc_module_init);
102 module_exit(crypto_cbc_module_exit);
103
104 MODULE_LICENSE("GPL");
105 MODULE_DESCRIPTION("CBC block cipher mode of operation");
106 MODULE_ALIAS_CRYPTO("cbc");