2019-05-27 00:55:01 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2006-09-20 19:44:08 -06:00
|
|
|
/*
|
|
|
|
* CBC: Cipher Block Chaining mode
|
|
|
|
*
|
2016-11-22 05:08:42 -07:00
|
|
|
* Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au>
|
2006-09-20 19:44:08 -06:00
|
|
|
*/
|
|
|
|
|
2017-02-27 05:38:25 -07:00
|
|
|
#include <crypto/algapi.h>
|
2016-11-22 05:08:42 -07:00
|
|
|
#include <crypto/cbc.h>
|
2016-11-22 05:08:39 -07:00
|
|
|
#include <crypto/internal/skcipher.h>
|
2006-09-20 19:44:08 -06:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
2007-11-20 02:36:00 -07:00
|
|
|
#include <linux/log2.h>
|
2006-09-20 19:44:08 -06:00
|
|
|
#include <linux/module.h>
|
|
|
|
|
2016-11-22 05:08:39 -07:00
|
|
|
static inline void crypto_cbc_encrypt_one(struct crypto_skcipher *tfm,
|
|
|
|
const u8 *src, u8 *dst)
|
|
|
|
{
|
2019-01-03 21:16:15 -07:00
|
|
|
crypto_cipher_encrypt_one(skcipher_cipher_simple(tfm), dst, src);
|
2016-11-22 05:08:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int crypto_cbc_encrypt(struct skcipher_request *req)
|
2006-09-20 19:44:08 -06:00
|
|
|
{
|
2016-11-22 05:08:39 -07:00
|
|
|
return crypto_cbc_encrypt_walk(req, crypto_cbc_encrypt_one);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void crypto_cbc_decrypt_one(struct crypto_skcipher *tfm,
|
|
|
|
const u8 *src, u8 *dst)
|
|
|
|
{
|
2019-01-03 21:16:15 -07:00
|
|
|
crypto_cipher_decrypt_one(skcipher_cipher_simple(tfm), dst, src);
|
2016-11-22 05:08:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int crypto_cbc_decrypt(struct skcipher_request *req)
|
|
|
|
{
|
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct skcipher_walk walk;
|
2006-09-20 19:44:08 -06:00
|
|
|
int err;
|
|
|
|
|
2016-11-22 05:08:39 -07:00
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
2006-09-20 19:44:08 -06:00
|
|
|
|
2016-11-22 05:08:39 -07:00
|
|
|
while (walk.nbytes) {
|
|
|
|
err = crypto_cbc_decrypt_blocks(&walk, tfm,
|
|
|
|
crypto_cbc_decrypt_one);
|
|
|
|
err = skcipher_walk_done(&walk, err);
|
2006-09-20 19:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-11-22 05:08:39 -07:00
|
|
|
static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
|
|
|
|
{
|
|
|
|
struct skcipher_instance *inst;
|
2006-09-20 19:44:08 -06:00
|
|
|
struct crypto_alg *alg;
|
2007-01-01 00:37:02 -07:00
|
|
|
int err;
|
|
|
|
|
2019-12-19 22:29:40 -07:00
|
|
|
inst = skcipher_alloc_instance_simple(tmpl, tb);
|
2019-01-03 21:16:15 -07:00
|
|
|
if (IS_ERR(inst))
|
|
|
|
return PTR_ERR(inst);
|
2007-11-20 02:36:00 -07:00
|
|
|
|
2019-12-19 22:29:40 -07:00
|
|
|
alg = skcipher_ialg_simple(inst);
|
|
|
|
|
2016-11-22 05:08:39 -07:00
|
|
|
err = -EINVAL;
|
|
|
|
if (!is_power_of_2(alg->cra_blocksize))
|
2019-01-03 21:16:15 -07:00
|
|
|
goto out_free_inst;
|
2006-09-20 19:44:08 -06:00
|
|
|
|
2016-11-22 05:08:39 -07:00
|
|
|
inst->alg.encrypt = crypto_cbc_encrypt;
|
|
|
|
inst->alg.decrypt = crypto_cbc_decrypt;
|
2006-09-20 19:44:08 -06:00
|
|
|
|
2016-11-22 05:08:39 -07:00
|
|
|
err = skcipher_register_instance(tmpl, inst);
|
2019-12-19 22:29:40 -07:00
|
|
|
if (err) {
|
2019-01-03 21:16:15 -07:00
|
|
|
out_free_inst:
|
2019-12-19 22:29:40 -07:00
|
|
|
inst->free(inst);
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:16:15 -07:00
|
|
|
return err;
|
2006-09-20 19:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct crypto_template crypto_cbc_tmpl = {
|
|
|
|
.name = "cbc",
|
2016-11-22 05:08:39 -07:00
|
|
|
.create = crypto_cbc_create,
|
2006-09-20 19:44:08 -06:00
|
|
|
.module = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init crypto_cbc_module_init(void)
|
|
|
|
{
|
|
|
|
return crypto_register_template(&crypto_cbc_tmpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit crypto_cbc_module_exit(void)
|
|
|
|
{
|
|
|
|
crypto_unregister_template(&crypto_cbc_tmpl);
|
|
|
|
}
|
|
|
|
|
2019-04-11 22:57:42 -06:00
|
|
|
subsys_initcall(crypto_cbc_module_init);
|
2006-09-20 19:44:08 -06:00
|
|
|
module_exit(crypto_cbc_module_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
2019-01-03 21:16:15 -07:00
|
|
|
MODULE_DESCRIPTION("CBC block cipher mode of operation");
|
2014-11-24 17:32:38 -07:00
|
|
|
MODULE_ALIAS_CRYPTO("cbc");
|