1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * ECB: Electronic CodeBook mode
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <crypto/algapi.h>
9*4882a593Smuzhiyun #include <crypto/internal/cipher.h>
10*4882a593Smuzhiyun #include <crypto/internal/skcipher.h>
11*4882a593Smuzhiyun #include <linux/err.h>
12*4882a593Smuzhiyun #include <linux/init.h>
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun
crypto_ecb_crypt(struct skcipher_request * req,struct crypto_cipher * cipher,void (* fn)(struct crypto_tfm *,u8 *,const u8 *))16*4882a593Smuzhiyun static int crypto_ecb_crypt(struct skcipher_request *req,
17*4882a593Smuzhiyun struct crypto_cipher *cipher,
18*4882a593Smuzhiyun void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun const unsigned int bsize = crypto_cipher_blocksize(cipher);
21*4882a593Smuzhiyun struct skcipher_walk walk;
22*4882a593Smuzhiyun unsigned int nbytes;
23*4882a593Smuzhiyun int err;
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun err = skcipher_walk_virt(&walk, req, false);
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun while ((nbytes = walk.nbytes) != 0) {
28*4882a593Smuzhiyun const u8 *src = walk.src.virt.addr;
29*4882a593Smuzhiyun u8 *dst = walk.dst.virt.addr;
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun do {
32*4882a593Smuzhiyun fn(crypto_cipher_tfm(cipher), dst, src);
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun src += bsize;
35*4882a593Smuzhiyun dst += bsize;
36*4882a593Smuzhiyun } while ((nbytes -= bsize) >= bsize);
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun err = skcipher_walk_done(&walk, nbytes);
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun return err;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
crypto_ecb_encrypt(struct skcipher_request * req)44*4882a593Smuzhiyun static int crypto_ecb_encrypt(struct skcipher_request *req)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
47*4882a593Smuzhiyun struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun return crypto_ecb_crypt(req, cipher,
50*4882a593Smuzhiyun crypto_cipher_alg(cipher)->cia_encrypt);
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
crypto_ecb_decrypt(struct skcipher_request * req)53*4882a593Smuzhiyun static int crypto_ecb_decrypt(struct skcipher_request *req)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
56*4882a593Smuzhiyun struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun return crypto_ecb_crypt(req, cipher,
59*4882a593Smuzhiyun crypto_cipher_alg(cipher)->cia_decrypt);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
crypto_ecb_create(struct crypto_template * tmpl,struct rtattr ** tb)62*4882a593Smuzhiyun static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun struct skcipher_instance *inst;
65*4882a593Smuzhiyun int err;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun inst = skcipher_alloc_instance_simple(tmpl, tb);
68*4882a593Smuzhiyun if (IS_ERR(inst))
69*4882a593Smuzhiyun return PTR_ERR(inst);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun inst->alg.ivsize = 0; /* ECB mode doesn't take an IV */
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun inst->alg.encrypt = crypto_ecb_encrypt;
74*4882a593Smuzhiyun inst->alg.decrypt = crypto_ecb_decrypt;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun err = skcipher_register_instance(tmpl, inst);
77*4882a593Smuzhiyun if (err)
78*4882a593Smuzhiyun inst->free(inst);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun return err;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun static struct crypto_template crypto_ecb_tmpl = {
84*4882a593Smuzhiyun .name = "ecb",
85*4882a593Smuzhiyun .create = crypto_ecb_create,
86*4882a593Smuzhiyun .module = THIS_MODULE,
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun
crypto_ecb_module_init(void)89*4882a593Smuzhiyun static int __init crypto_ecb_module_init(void)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun return crypto_register_template(&crypto_ecb_tmpl);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
crypto_ecb_module_exit(void)94*4882a593Smuzhiyun static void __exit crypto_ecb_module_exit(void)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun crypto_unregister_template(&crypto_ecb_tmpl);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun subsys_initcall(crypto_ecb_module_init);
100*4882a593Smuzhiyun module_exit(crypto_ecb_module_exit);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun MODULE_LICENSE("GPL");
103*4882a593Smuzhiyun MODULE_DESCRIPTION("ECB block cipher mode of operation");
104*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("ecb");
105