Commit | Line | Data |
---|---|---|
db131ef9 HX |
1 | /* |
2 | * CBC: Cipher Block Chaining mode | |
3 | * | |
cc868d82 | 4 | * Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au> |
db131ef9 HX |
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 | ||
e6c2e65c | 13 | #include <crypto/algapi.h> |
cc868d82 | 14 | #include <crypto/cbc.h> |
79c65d17 | 15 | #include <crypto/internal/skcipher.h> |
db131ef9 HX |
16 | #include <linux/err.h> |
17 | #include <linux/init.h> | |
18 | #include <linux/kernel.h> | |
50b6544e | 19 | #include <linux/log2.h> |
db131ef9 | 20 | #include <linux/module.h> |
db131ef9 | 21 | |
79c65d17 HX |
22 | static inline void crypto_cbc_encrypt_one(struct crypto_skcipher *tfm, |
23 | const u8 *src, u8 *dst) | |
24 | { | |
a5a84a9d | 25 | crypto_cipher_encrypt_one(skcipher_cipher_simple(tfm), dst, src); |
79c65d17 HX |
26 | } |
27 | ||
28 | static int crypto_cbc_encrypt(struct skcipher_request *req) | |
db131ef9 | 29 | { |
79c65d17 HX |
30 | return crypto_cbc_encrypt_walk(req, crypto_cbc_encrypt_one); |
31 | } | |
32 | ||
79c65d17 HX |
33 | static inline void crypto_cbc_decrypt_one(struct crypto_skcipher *tfm, |
34 | const u8 *src, u8 *dst) | |
35 | { | |
a5a84a9d | 36 | crypto_cipher_decrypt_one(skcipher_cipher_simple(tfm), dst, src); |
79c65d17 HX |
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; | |
db131ef9 HX |
43 | int err; |
44 | ||
79c65d17 | 45 | err = skcipher_walk_virt(&walk, req, false); |
db131ef9 | 46 | |
79c65d17 HX |
47 | while (walk.nbytes) { |
48 | err = crypto_cbc_decrypt_blocks(&walk, tfm, | |
49 | crypto_cbc_decrypt_one); | |
50 | err = skcipher_walk_done(&walk, err); | |
db131ef9 HX |
51 | } |
52 | ||
53 | return err; | |
54 | } | |
55 | ||
79c65d17 HX |
56 | static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb) |
57 | { | |
58 | struct skcipher_instance *inst; | |
db131ef9 | 59 | struct crypto_alg *alg; |
ebc610e5 HX |
60 | int err; |
61 | ||
a5a84a9d EB |
62 | inst = skcipher_alloc_instance_simple(tmpl, tb, &alg); |
63 | if (IS_ERR(inst)) | |
64 | return PTR_ERR(inst); | |
50b6544e | 65 | |
79c65d17 HX |
66 | err = -EINVAL; |
67 | if (!is_power_of_2(alg->cra_blocksize)) | |
a5a84a9d | 68 | goto out_free_inst; |
db131ef9 | 69 | |
79c65d17 HX |
70 | inst->alg.encrypt = crypto_cbc_encrypt; |
71 | inst->alg.decrypt = crypto_cbc_decrypt; | |
db131ef9 | 72 | |
79c65d17 HX |
73 | err = skcipher_register_instance(tmpl, inst); |
74 | if (err) | |
a5a84a9d EB |
75 | goto out_free_inst; |
76 | goto out_put_alg; | |
79c65d17 | 77 | |
a5a84a9d EB |
78 | out_free_inst: |
79 | inst->free(inst); | |
80 | out_put_alg: | |
e5bde04c | 81 | crypto_mod_put(alg); |
a5a84a9d | 82 | return err; |
db131ef9 HX |
83 | } |
84 | ||
85 | static struct crypto_template crypto_cbc_tmpl = { | |
86 | .name = "cbc", | |
79c65d17 | 87 | .create = crypto_cbc_create, |
db131ef9 HX |
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 | ||
c4741b23 | 101 | subsys_initcall(crypto_cbc_module_init); |
db131ef9 HX |
102 | module_exit(crypto_cbc_module_exit); |
103 | ||
104 | MODULE_LICENSE("GPL"); | |
a5a84a9d | 105 | MODULE_DESCRIPTION("CBC block cipher mode of operation"); |
4943ba16 | 106 | MODULE_ALIAS_CRYPTO("cbc"); |