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