Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 2 | /* |
| 3 | * CBC: Cipher Block Chaining mode |
| 4 | * |
Herbert Xu | cc868d8 | 2016-11-22 20:08:42 +0800 | [diff] [blame] | 5 | * Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 6 | */ |
| 7 | |
Marcelo Cerri | e6c2e65 | 2017-02-27 09:38:25 -0300 | [diff] [blame] | 8 | #include <crypto/algapi.h> |
Herbert Xu | cc868d8 | 2016-11-22 20:08:42 +0800 | [diff] [blame] | 9 | #include <crypto/cbc.h> |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 10 | #include <crypto/internal/skcipher.h> |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 11 | #include <linux/err.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/kernel.h> |
Herbert Xu | 50b6544 | 2007-11-20 17:36:00 +0800 | [diff] [blame] | 14 | #include <linux/log2.h> |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 15 | #include <linux/module.h> |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 16 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 17 | static inline void crypto_cbc_encrypt_one(struct crypto_skcipher *tfm, |
| 18 | const u8 *src, u8 *dst) |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 19 | { |
Eric Biggers | a5a84a9 | 2019-01-03 20:16:15 -0800 | [diff] [blame] | 20 | crypto_cipher_encrypt_one(skcipher_cipher_simple(tfm), dst, src); |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | static int crypto_cbc_encrypt(struct skcipher_request *req) |
| 24 | { |
| 25 | return crypto_cbc_encrypt_walk(req, crypto_cbc_encrypt_one); |
| 26 | } |
| 27 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 28 | static inline void crypto_cbc_decrypt_one(struct crypto_skcipher *tfm, |
| 29 | const u8 *src, u8 *dst) |
| 30 | { |
Eric Biggers | a5a84a9 | 2019-01-03 20:16:15 -0800 | [diff] [blame] | 31 | crypto_cipher_decrypt_one(skcipher_cipher_simple(tfm), dst, src); |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static int crypto_cbc_decrypt(struct skcipher_request *req) |
| 35 | { |
| 36 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 37 | struct skcipher_walk walk; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 38 | int err; |
| 39 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 40 | err = skcipher_walk_virt(&walk, req, false); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 41 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 42 | while (walk.nbytes) { |
| 43 | err = crypto_cbc_decrypt_blocks(&walk, tfm, |
| 44 | crypto_cbc_decrypt_one); |
| 45 | err = skcipher_walk_done(&walk, err); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | return err; |
| 49 | } |
| 50 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 51 | static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb) |
| 52 | { |
| 53 | struct skcipher_instance *inst; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 54 | struct crypto_alg *alg; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 55 | int err; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 56 | |
Eric Biggers | a5a84a9 | 2019-01-03 20:16:15 -0800 | [diff] [blame] | 57 | inst = skcipher_alloc_instance_simple(tmpl, tb, &alg); |
| 58 | if (IS_ERR(inst)) |
| 59 | return PTR_ERR(inst); |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 60 | |
| 61 | err = -EINVAL; |
Herbert Xu | 50b6544 | 2007-11-20 17:36:00 +0800 | [diff] [blame] | 62 | if (!is_power_of_2(alg->cra_blocksize)) |
Eric Biggers | a5a84a9 | 2019-01-03 20:16:15 -0800 | [diff] [blame] | 63 | goto out_free_inst; |
Herbert Xu | 50b6544 | 2007-11-20 17:36:00 +0800 | [diff] [blame] | 64 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 65 | inst->alg.encrypt = crypto_cbc_encrypt; |
| 66 | inst->alg.decrypt = crypto_cbc_decrypt; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 67 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 68 | err = skcipher_register_instance(tmpl, inst); |
| 69 | if (err) |
Eric Biggers | a5a84a9 | 2019-01-03 20:16:15 -0800 | [diff] [blame] | 70 | goto out_free_inst; |
| 71 | goto out_put_alg; |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 72 | |
Eric Biggers | a5a84a9 | 2019-01-03 20:16:15 -0800 | [diff] [blame] | 73 | out_free_inst: |
| 74 | inst->free(inst); |
| 75 | out_put_alg: |
| 76 | crypto_mod_put(alg); |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 77 | return err; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static struct crypto_template crypto_cbc_tmpl = { |
| 81 | .name = "cbc", |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame] | 82 | .create = crypto_cbc_create, |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 83 | .module = THIS_MODULE, |
| 84 | }; |
| 85 | |
| 86 | static int __init crypto_cbc_module_init(void) |
| 87 | { |
| 88 | return crypto_register_template(&crypto_cbc_tmpl); |
| 89 | } |
| 90 | |
| 91 | static void __exit crypto_cbc_module_exit(void) |
| 92 | { |
| 93 | crypto_unregister_template(&crypto_cbc_tmpl); |
| 94 | } |
| 95 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 96 | subsys_initcall(crypto_cbc_module_init); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 97 | module_exit(crypto_cbc_module_exit); |
| 98 | |
| 99 | MODULE_LICENSE("GPL"); |
Eric Biggers | a5a84a9 | 2019-01-03 20:16:15 -0800 | [diff] [blame] | 100 | MODULE_DESCRIPTION("CBC block cipher mode of operation"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 101 | MODULE_ALIAS_CRYPTO("cbc"); |