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