blob: e6f6273a7d3990589e2d6917a100e0204e7ad1ee [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xudb131ef2006-09-21 11:44:08 +10002/*
3 * CBC: Cipher Block Chaining mode
4 *
Herbert Xucc868d82016-11-22 20:08:42 +08005 * Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xudb131ef2006-09-21 11:44:08 +10006 */
7
Marcelo Cerrie6c2e652017-02-27 09:38:25 -03008#include <crypto/algapi.h>
Herbert Xucc868d82016-11-22 20:08:42 +08009#include <crypto/cbc.h>
Herbert Xu79c65d12016-11-22 20:08:39 +080010#include <crypto/internal/skcipher.h>
Herbert Xudb131ef2006-09-21 11:44:08 +100011#include <linux/err.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
Herbert Xu50b65442007-11-20 17:36:00 +080014#include <linux/log2.h>
Herbert Xudb131ef2006-09-21 11:44:08 +100015#include <linux/module.h>
Herbert Xudb131ef2006-09-21 11:44:08 +100016
Herbert Xu79c65d12016-11-22 20:08:39 +080017static inline void crypto_cbc_encrypt_one(struct crypto_skcipher *tfm,
18 const u8 *src, u8 *dst)
Herbert Xudb131ef2006-09-21 11:44:08 +100019{
Eric Biggersa5a84a92019-01-03 20:16:15 -080020 crypto_cipher_encrypt_one(skcipher_cipher_simple(tfm), dst, src);
Herbert Xu79c65d12016-11-22 20:08:39 +080021}
22
23static int crypto_cbc_encrypt(struct skcipher_request *req)
24{
25 return crypto_cbc_encrypt_walk(req, crypto_cbc_encrypt_one);
26}
27
Herbert Xu79c65d12016-11-22 20:08:39 +080028static inline void crypto_cbc_decrypt_one(struct crypto_skcipher *tfm,
29 const u8 *src, u8 *dst)
30{
Eric Biggersa5a84a92019-01-03 20:16:15 -080031 crypto_cipher_decrypt_one(skcipher_cipher_simple(tfm), dst, src);
Herbert Xu79c65d12016-11-22 20:08:39 +080032}
33
34static int crypto_cbc_decrypt(struct skcipher_request *req)
35{
36 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
37 struct skcipher_walk walk;
Herbert Xudb131ef2006-09-21 11:44:08 +100038 int err;
39
Herbert Xu79c65d12016-11-22 20:08:39 +080040 err = skcipher_walk_virt(&walk, req, false);
Herbert Xudb131ef2006-09-21 11:44:08 +100041
Herbert Xu79c65d12016-11-22 20:08:39 +080042 while (walk.nbytes) {
43 err = crypto_cbc_decrypt_blocks(&walk, tfm,
44 crypto_cbc_decrypt_one);
45 err = skcipher_walk_done(&walk, err);
Herbert Xudb131ef2006-09-21 11:44:08 +100046 }
47
48 return err;
49}
50
Herbert Xu79c65d12016-11-22 20:08:39 +080051static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
52{
53 struct skcipher_instance *inst;
Herbert Xudb131ef2006-09-21 11:44:08 +100054 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +110055 int err;
Herbert Xudb131ef2006-09-21 11:44:08 +100056
Herbert Xub3c16bf2019-12-20 13:29:40 +080057 inst = skcipher_alloc_instance_simple(tmpl, tb);
Eric Biggersa5a84a92019-01-03 20:16:15 -080058 if (IS_ERR(inst))
59 return PTR_ERR(inst);
Herbert Xu79c65d12016-11-22 20:08:39 +080060
Herbert Xub3c16bf2019-12-20 13:29:40 +080061 alg = skcipher_ialg_simple(inst);
62
Herbert Xu79c65d12016-11-22 20:08:39 +080063 err = -EINVAL;
Herbert Xu50b65442007-11-20 17:36:00 +080064 if (!is_power_of_2(alg->cra_blocksize))
Eric Biggersa5a84a92019-01-03 20:16:15 -080065 goto out_free_inst;
Herbert Xu50b65442007-11-20 17:36:00 +080066
Herbert Xu79c65d12016-11-22 20:08:39 +080067 inst->alg.encrypt = crypto_cbc_encrypt;
68 inst->alg.decrypt = crypto_cbc_decrypt;
Herbert Xudb131ef2006-09-21 11:44:08 +100069
Herbert Xu79c65d12016-11-22 20:08:39 +080070 err = skcipher_register_instance(tmpl, inst);
Herbert Xub3c16bf2019-12-20 13:29:40 +080071 if (err) {
Eric Biggersa5a84a92019-01-03 20:16:15 -080072out_free_inst:
Herbert Xub3c16bf2019-12-20 13:29:40 +080073 inst->free(inst);
74 }
75
Herbert Xu79c65d12016-11-22 20:08:39 +080076 return err;
Herbert Xudb131ef2006-09-21 11:44:08 +100077}
78
79static struct crypto_template crypto_cbc_tmpl = {
80 .name = "cbc",
Herbert Xu79c65d12016-11-22 20:08:39 +080081 .create = crypto_cbc_create,
Herbert Xudb131ef2006-09-21 11:44:08 +100082 .module = THIS_MODULE,
83};
84
85static int __init crypto_cbc_module_init(void)
86{
87 return crypto_register_template(&crypto_cbc_tmpl);
88}
89
90static void __exit crypto_cbc_module_exit(void)
91{
92 crypto_unregister_template(&crypto_cbc_tmpl);
93}
94
Eric Biggersc4741b22019-04-11 21:57:42 -070095subsys_initcall(crypto_cbc_module_init);
Herbert Xudb131ef2006-09-21 11:44:08 +100096module_exit(crypto_cbc_module_exit);
97
98MODULE_LICENSE("GPL");
Eric Biggersa5a84a92019-01-03 20:16:15 -080099MODULE_DESCRIPTION("CBC block cipher mode of operation");
Kees Cook4943ba12014-11-24 16:32:38 -0800100MODULE_ALIAS_CRYPTO("cbc");