162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * ECB: Electronic CodeBook mode
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <crypto/algapi.h>
962306a36Sopenharmony_ci#include <crypto/internal/cipher.h>
1062306a36Sopenharmony_ci#include <crypto/internal/skcipher.h>
1162306a36Sopenharmony_ci#include <linux/err.h>
1262306a36Sopenharmony_ci#include <linux/init.h>
1362306a36Sopenharmony_ci#include <linux/kernel.h>
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistatic int crypto_ecb_crypt(struct skcipher_request *req,
1762306a36Sopenharmony_ci			    struct crypto_cipher *cipher,
1862306a36Sopenharmony_ci			    void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
1962306a36Sopenharmony_ci{
2062306a36Sopenharmony_ci	const unsigned int bsize = crypto_cipher_blocksize(cipher);
2162306a36Sopenharmony_ci	struct skcipher_walk walk;
2262306a36Sopenharmony_ci	unsigned int nbytes;
2362306a36Sopenharmony_ci	int err;
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci	err = skcipher_walk_virt(&walk, req, false);
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci	while ((nbytes = walk.nbytes) != 0) {
2862306a36Sopenharmony_ci		const u8 *src = walk.src.virt.addr;
2962306a36Sopenharmony_ci		u8 *dst = walk.dst.virt.addr;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci		do {
3262306a36Sopenharmony_ci			fn(crypto_cipher_tfm(cipher), dst, src);
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci			src += bsize;
3562306a36Sopenharmony_ci			dst += bsize;
3662306a36Sopenharmony_ci		} while ((nbytes -= bsize) >= bsize);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci		err = skcipher_walk_done(&walk, nbytes);
3962306a36Sopenharmony_ci	}
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	return err;
4262306a36Sopenharmony_ci}
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cistatic int crypto_ecb_encrypt(struct skcipher_request *req)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
4762306a36Sopenharmony_ci	struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	return crypto_ecb_crypt(req, cipher,
5062306a36Sopenharmony_ci				crypto_cipher_alg(cipher)->cia_encrypt);
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic int crypto_ecb_decrypt(struct skcipher_request *req)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
5662306a36Sopenharmony_ci	struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	return crypto_ecb_crypt(req, cipher,
5962306a36Sopenharmony_ci				crypto_cipher_alg(cipher)->cia_decrypt);
6062306a36Sopenharmony_ci}
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_cistatic int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	struct skcipher_instance *inst;
6562306a36Sopenharmony_ci	int err;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	inst = skcipher_alloc_instance_simple(tmpl, tb);
6862306a36Sopenharmony_ci	if (IS_ERR(inst))
6962306a36Sopenharmony_ci		return PTR_ERR(inst);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	inst->alg.ivsize = 0; /* ECB mode doesn't take an IV */
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	inst->alg.encrypt = crypto_ecb_encrypt;
7462306a36Sopenharmony_ci	inst->alg.decrypt = crypto_ecb_decrypt;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	err = skcipher_register_instance(tmpl, inst);
7762306a36Sopenharmony_ci	if (err)
7862306a36Sopenharmony_ci		inst->free(inst);
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	return err;
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistatic struct crypto_template crypto_ecb_tmpl = {
8462306a36Sopenharmony_ci	.name = "ecb",
8562306a36Sopenharmony_ci	.create = crypto_ecb_create,
8662306a36Sopenharmony_ci	.module = THIS_MODULE,
8762306a36Sopenharmony_ci};
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic int __init crypto_ecb_module_init(void)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	return crypto_register_template(&crypto_ecb_tmpl);
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistatic void __exit crypto_ecb_module_exit(void)
9562306a36Sopenharmony_ci{
9662306a36Sopenharmony_ci	crypto_unregister_template(&crypto_ecb_tmpl);
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cisubsys_initcall(crypto_ecb_module_init);
10062306a36Sopenharmony_cimodule_exit(crypto_ecb_module_exit);
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciMODULE_LICENSE("GPL");
10362306a36Sopenharmony_ciMODULE_DESCRIPTION("ECB block cipher mode of operation");
10462306a36Sopenharmony_ciMODULE_ALIAS_CRYPTO("ecb");
105