blob: dd96bcf4d4b6255831df54098df1c3022cf7b4a2 [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
Eric Biggersa5a84a92019-01-03 20:16:15 -080057 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
58 if (IS_ERR(inst))
59 return PTR_ERR(inst);
Herbert Xu79c65d12016-11-22 20:08:39 +080060
61 err = -EINVAL;
Herbert Xu50b65442007-11-20 17:36:00 +080062 if (!is_power_of_2(alg->cra_blocksize))
Eric Biggersa5a84a92019-01-03 20:16:15 -080063 goto out_free_inst;
Herbert Xu50b65442007-11-20 17:36:00 +080064
Herbert Xu79c65d12016-11-22 20:08:39 +080065 inst->alg.encrypt = crypto_cbc_encrypt;
66 inst->alg.decrypt = crypto_cbc_decrypt;
Herbert Xudb131ef2006-09-21 11:44:08 +100067
Herbert Xu79c65d12016-11-22 20:08:39 +080068 err = skcipher_register_instance(tmpl, inst);
69 if (err)
Eric Biggersa5a84a92019-01-03 20:16:15 -080070 goto out_free_inst;
71 goto out_put_alg;
Herbert Xu79c65d12016-11-22 20:08:39 +080072
Eric Biggersa5a84a92019-01-03 20:16:15 -080073out_free_inst:
74 inst->free(inst);
75out_put_alg:
76 crypto_mod_put(alg);
Herbert Xu79c65d12016-11-22 20:08:39 +080077 return err;
Herbert Xudb131ef2006-09-21 11:44:08 +100078}
79
80static struct crypto_template crypto_cbc_tmpl = {
81 .name = "cbc",
Herbert Xu79c65d12016-11-22 20:08:39 +080082 .create = crypto_cbc_create,
Herbert Xudb131ef2006-09-21 11:44:08 +100083 .module = THIS_MODULE,
84};
85
86static int __init crypto_cbc_module_init(void)
87{
88 return crypto_register_template(&crypto_cbc_tmpl);
89}
90
91static void __exit crypto_cbc_module_exit(void)
92{
93 crypto_unregister_template(&crypto_cbc_tmpl);
94}
95
Eric Biggersc4741b22019-04-11 21:57:42 -070096subsys_initcall(crypto_cbc_module_init);
Herbert Xudb131ef2006-09-21 11:44:08 +100097module_exit(crypto_cbc_module_exit);
98
99MODULE_LICENSE("GPL");
Eric Biggersa5a84a92019-01-03 20:16:15 -0800100MODULE_DESCRIPTION("CBC block cipher mode of operation");
Kees Cook4943ba12014-11-24 16:32:38 -0800101MODULE_ALIAS_CRYPTO("cbc");