blob: 129f79d0336585abbbc08754a8fb1946c3152de6 [file] [log] [blame]
Herbert Xudb131ef2006-09-21 11:44:08 +10001/*
2 * CBC: Cipher Block Chaining mode
3 *
Herbert Xucc868d82016-11-22 20:08:42 +08004 * Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xudb131ef2006-09-21 11:44:08 +10005 *
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 Cerrie6c2e652017-02-27 09:38:25 -030013#include <crypto/algapi.h>
Herbert Xucc868d82016-11-22 20:08:42 +080014#include <crypto/cbc.h>
Herbert Xu79c65d12016-11-22 20:08:39 +080015#include <crypto/internal/skcipher.h>
Herbert Xudb131ef2006-09-21 11:44:08 +100016#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
Herbert Xu50b65442007-11-20 17:36:00 +080019#include <linux/log2.h>
Herbert Xudb131ef2006-09-21 11:44:08 +100020#include <linux/module.h>
Herbert Xudb131ef2006-09-21 11:44:08 +100021
Herbert Xu79c65d12016-11-22 20:08:39 +080022static inline void crypto_cbc_encrypt_one(struct crypto_skcipher *tfm,
23 const u8 *src, u8 *dst)
Herbert Xudb131ef2006-09-21 11:44:08 +100024{
Eric Biggersa5a84a92019-01-03 20:16:15 -080025 crypto_cipher_encrypt_one(skcipher_cipher_simple(tfm), dst, src);
Herbert Xu79c65d12016-11-22 20:08:39 +080026}
27
28static int crypto_cbc_encrypt(struct skcipher_request *req)
29{
30 return crypto_cbc_encrypt_walk(req, crypto_cbc_encrypt_one);
31}
32
Herbert Xu79c65d12016-11-22 20:08:39 +080033static inline void crypto_cbc_decrypt_one(struct crypto_skcipher *tfm,
34 const u8 *src, u8 *dst)
35{
Eric Biggersa5a84a92019-01-03 20:16:15 -080036 crypto_cipher_decrypt_one(skcipher_cipher_simple(tfm), dst, src);
Herbert Xu79c65d12016-11-22 20:08:39 +080037}
38
39static int crypto_cbc_decrypt(struct skcipher_request *req)
40{
41 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
42 struct skcipher_walk walk;
Herbert Xudb131ef2006-09-21 11:44:08 +100043 int err;
44
Herbert Xu79c65d12016-11-22 20:08:39 +080045 err = skcipher_walk_virt(&walk, req, false);
Herbert Xudb131ef2006-09-21 11:44:08 +100046
Herbert Xu79c65d12016-11-22 20:08:39 +080047 while (walk.nbytes) {
48 err = crypto_cbc_decrypt_blocks(&walk, tfm,
49 crypto_cbc_decrypt_one);
50 err = skcipher_walk_done(&walk, err);
Herbert Xudb131ef2006-09-21 11:44:08 +100051 }
52
53 return err;
54}
55
Herbert Xu79c65d12016-11-22 20:08:39 +080056static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
57{
58 struct skcipher_instance *inst;
Herbert Xudb131ef2006-09-21 11:44:08 +100059 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +110060 int err;
Herbert Xudb131ef2006-09-21 11:44:08 +100061
Eric Biggersa5a84a92019-01-03 20:16:15 -080062 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
63 if (IS_ERR(inst))
64 return PTR_ERR(inst);
Herbert Xu79c65d12016-11-22 20:08:39 +080065
66 err = -EINVAL;
Herbert Xu50b65442007-11-20 17:36:00 +080067 if (!is_power_of_2(alg->cra_blocksize))
Eric Biggersa5a84a92019-01-03 20:16:15 -080068 goto out_free_inst;
Herbert Xu50b65442007-11-20 17:36:00 +080069
Herbert Xu79c65d12016-11-22 20:08:39 +080070 inst->alg.encrypt = crypto_cbc_encrypt;
71 inst->alg.decrypt = crypto_cbc_decrypt;
Herbert Xudb131ef2006-09-21 11:44:08 +100072
Herbert Xu79c65d12016-11-22 20:08:39 +080073 err = skcipher_register_instance(tmpl, inst);
74 if (err)
Eric Biggersa5a84a92019-01-03 20:16:15 -080075 goto out_free_inst;
76 goto out_put_alg;
Herbert Xu79c65d12016-11-22 20:08:39 +080077
Eric Biggersa5a84a92019-01-03 20:16:15 -080078out_free_inst:
79 inst->free(inst);
80out_put_alg:
81 crypto_mod_put(alg);
Herbert Xu79c65d12016-11-22 20:08:39 +080082 return err;
Herbert Xudb131ef2006-09-21 11:44:08 +100083}
84
85static struct crypto_template crypto_cbc_tmpl = {
86 .name = "cbc",
Herbert Xu79c65d12016-11-22 20:08:39 +080087 .create = crypto_cbc_create,
Herbert Xudb131ef2006-09-21 11:44:08 +100088 .module = THIS_MODULE,
89};
90
91static int __init crypto_cbc_module_init(void)
92{
93 return crypto_register_template(&crypto_cbc_tmpl);
94}
95
96static void __exit crypto_cbc_module_exit(void)
97{
98 crypto_unregister_template(&crypto_cbc_tmpl);
99}
100
Eric Biggersc4741b22019-04-11 21:57:42 -0700101subsys_initcall(crypto_cbc_module_init);
Herbert Xudb131ef2006-09-21 11:44:08 +1000102module_exit(crypto_cbc_module_exit);
103
104MODULE_LICENSE("GPL");
Eric Biggersa5a84a92019-01-03 20:16:15 -0800105MODULE_DESCRIPTION("CBC block cipher mode of operation");
Kees Cook4943ba12014-11-24 16:32:38 -0800106MODULE_ALIAS_CRYPTO("cbc");