18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ECB: Electronic CodeBook mode
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
98c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h>
108c2ecf20Sopenharmony_ci#include <linux/err.h>
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic int crypto_ecb_crypt(struct skcipher_request *req,
168c2ecf20Sopenharmony_ci			    struct crypto_cipher *cipher,
178c2ecf20Sopenharmony_ci			    void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	const unsigned int bsize = crypto_cipher_blocksize(cipher);
208c2ecf20Sopenharmony_ci	struct skcipher_walk walk;
218c2ecf20Sopenharmony_ci	unsigned int nbytes;
228c2ecf20Sopenharmony_ci	int err;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	err = skcipher_walk_virt(&walk, req, false);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	while ((nbytes = walk.nbytes) != 0) {
278c2ecf20Sopenharmony_ci		const u8 *src = walk.src.virt.addr;
288c2ecf20Sopenharmony_ci		u8 *dst = walk.dst.virt.addr;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci		do {
318c2ecf20Sopenharmony_ci			fn(crypto_cipher_tfm(cipher), dst, src);
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci			src += bsize;
348c2ecf20Sopenharmony_ci			dst += bsize;
358c2ecf20Sopenharmony_ci		} while ((nbytes -= bsize) >= bsize);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci		err = skcipher_walk_done(&walk, nbytes);
388c2ecf20Sopenharmony_ci	}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	return err;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic int crypto_ecb_encrypt(struct skcipher_request *req)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
468c2ecf20Sopenharmony_ci	struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	return crypto_ecb_crypt(req, cipher,
498c2ecf20Sopenharmony_ci				crypto_cipher_alg(cipher)->cia_encrypt);
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int crypto_ecb_decrypt(struct skcipher_request *req)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
558c2ecf20Sopenharmony_ci	struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return crypto_ecb_crypt(req, cipher,
588c2ecf20Sopenharmony_ci				crypto_cipher_alg(cipher)->cia_decrypt);
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct skcipher_instance *inst;
648c2ecf20Sopenharmony_ci	int err;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	inst = skcipher_alloc_instance_simple(tmpl, tb);
678c2ecf20Sopenharmony_ci	if (IS_ERR(inst))
688c2ecf20Sopenharmony_ci		return PTR_ERR(inst);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	inst->alg.ivsize = 0; /* ECB mode doesn't take an IV */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	inst->alg.encrypt = crypto_ecb_encrypt;
738c2ecf20Sopenharmony_ci	inst->alg.decrypt = crypto_ecb_decrypt;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	err = skcipher_register_instance(tmpl, inst);
768c2ecf20Sopenharmony_ci	if (err)
778c2ecf20Sopenharmony_ci		inst->free(inst);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	return err;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic struct crypto_template crypto_ecb_tmpl = {
838c2ecf20Sopenharmony_ci	.name = "ecb",
848c2ecf20Sopenharmony_ci	.create = crypto_ecb_create,
858c2ecf20Sopenharmony_ci	.module = THIS_MODULE,
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int __init crypto_ecb_module_init(void)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	return crypto_register_template(&crypto_ecb_tmpl);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic void __exit crypto_ecb_module_exit(void)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	crypto_unregister_template(&crypto_ecb_tmpl);
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cisubsys_initcall(crypto_ecb_module_init);
998c2ecf20Sopenharmony_cimodule_exit(crypto_ecb_module_exit);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1028c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ECB block cipher mode of operation");
1038c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("ecb");
104