2006-09-21 08:44:08 +07:00
|
|
|
/*
|
|
|
|
* ECB: Electronic CodeBook mode
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <crypto/algapi.h>
|
2019-01-04 11:16:18 +07:00
|
|
|
#include <crypto/internal/skcipher.h>
|
2006-09-21 08:44:08 +07:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
static int crypto_ecb_crypt(struct skcipher_request *req,
|
|
|
|
struct crypto_cipher *cipher,
|
2006-09-21 08:44:08 +07:00
|
|
|
void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
|
|
|
|
{
|
2019-01-04 11:16:18 +07:00
|
|
|
const unsigned int bsize = crypto_cipher_blocksize(cipher);
|
|
|
|
struct skcipher_walk walk;
|
2006-09-21 08:44:08 +07:00
|
|
|
unsigned int nbytes;
|
|
|
|
int err;
|
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
err = skcipher_walk_virt(&walk, req, false);
|
2006-09-21 08:44:08 +07:00
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
while ((nbytes = walk.nbytes) != 0) {
|
|
|
|
const u8 *src = walk.src.virt.addr;
|
|
|
|
u8 *dst = walk.dst.virt.addr;
|
2006-09-21 08:44:08 +07:00
|
|
|
|
|
|
|
do {
|
2019-01-04 11:16:18 +07:00
|
|
|
fn(crypto_cipher_tfm(cipher), dst, src);
|
2010-02-16 19:33:49 +07:00
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
src += bsize;
|
|
|
|
dst += bsize;
|
2006-09-21 08:44:08 +07:00
|
|
|
} while ((nbytes -= bsize) >= bsize);
|
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
err = skcipher_walk_done(&walk, nbytes);
|
2006-09-21 08:44:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
static int crypto_ecb_encrypt(struct skcipher_request *req)
|
2006-09-21 08:44:08 +07:00
|
|
|
{
|
2019-01-04 11:16:18 +07:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
|
2006-09-21 08:44:08 +07:00
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
return crypto_ecb_crypt(req, cipher,
|
|
|
|
crypto_cipher_alg(cipher)->cia_encrypt);
|
2006-09-21 08:44:08 +07:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
static int crypto_ecb_decrypt(struct skcipher_request *req)
|
2006-09-21 08:44:08 +07:00
|
|
|
{
|
2019-01-04 11:16:18 +07:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
|
2006-09-21 08:44:08 +07:00
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
return crypto_ecb_crypt(req, cipher,
|
|
|
|
crypto_cipher_alg(cipher)->cia_decrypt);
|
2006-09-21 08:44:08 +07:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
|
2006-09-21 08:44:08 +07:00
|
|
|
{
|
2019-01-04 11:16:18 +07:00
|
|
|
struct skcipher_instance *inst;
|
2006-09-21 08:44:08 +07:00
|
|
|
struct crypto_alg *alg;
|
2007-01-01 14:37:02 +07:00
|
|
|
int err;
|
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
|
2006-09-21 08:44:08 +07:00
|
|
|
if (IS_ERR(inst))
|
2019-01-04 11:16:18 +07:00
|
|
|
return PTR_ERR(inst);
|
2006-09-21 08:44:08 +07:00
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
inst->alg.ivsize = 0; /* ECB mode doesn't take an IV */
|
2006-09-21 08:44:08 +07:00
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
inst->alg.encrypt = crypto_ecb_encrypt;
|
|
|
|
inst->alg.decrypt = crypto_ecb_decrypt;
|
2006-09-21 08:44:08 +07:00
|
|
|
|
2019-01-04 11:16:18 +07:00
|
|
|
err = skcipher_register_instance(tmpl, inst);
|
|
|
|
if (err)
|
|
|
|
inst->free(inst);
|
2006-09-21 08:44:08 +07:00
|
|
|
crypto_mod_put(alg);
|
2019-01-04 11:16:18 +07:00
|
|
|
return err;
|
2006-09-21 08:44:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct crypto_template crypto_ecb_tmpl = {
|
|
|
|
.name = "ecb",
|
2019-01-04 11:16:18 +07:00
|
|
|
.create = crypto_ecb_create,
|
2006-09-21 08:44:08 +07:00
|
|
|
.module = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init crypto_ecb_module_init(void)
|
|
|
|
{
|
|
|
|
return crypto_register_template(&crypto_ecb_tmpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit crypto_ecb_module_exit(void)
|
|
|
|
{
|
|
|
|
crypto_unregister_template(&crypto_ecb_tmpl);
|
|
|
|
}
|
|
|
|
|
2019-04-12 11:57:42 +07:00
|
|
|
subsys_initcall(crypto_ecb_module_init);
|
2006-09-21 08:44:08 +07:00
|
|
|
module_exit(crypto_ecb_module_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
2019-01-04 11:16:18 +07:00
|
|
|
MODULE_DESCRIPTION("ECB block cipher mode of operation");
|
2014-11-25 07:32:38 +07:00
|
|
|
MODULE_ALIAS_CRYPTO("ecb");
|