1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ECB: Electronic CodeBook mode
5 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
8 #include <crypto/algapi.h>
9 #include <crypto/internal/cipher.h>
10 #include <crypto/internal/skcipher.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 static int crypto_ecb_crypt(struct skcipher_request
*req
,
17 struct crypto_cipher
*cipher
,
18 void (*fn
)(struct crypto_tfm
*, u8
*, const u8
*))
20 const unsigned int bsize
= crypto_cipher_blocksize(cipher
);
21 struct skcipher_walk walk
;
25 err
= skcipher_walk_virt(&walk
, req
, false);
27 while ((nbytes
= walk
.nbytes
) != 0) {
28 const u8
*src
= walk
.src
.virt
.addr
;
29 u8
*dst
= walk
.dst
.virt
.addr
;
32 fn(crypto_cipher_tfm(cipher
), dst
, src
);
36 } while ((nbytes
-= bsize
) >= bsize
);
38 err
= skcipher_walk_done(&walk
, nbytes
);
44 static int crypto_ecb_encrypt(struct skcipher_request
*req
)
46 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
47 struct crypto_cipher
*cipher
= skcipher_cipher_simple(tfm
);
49 return crypto_ecb_crypt(req
, cipher
,
50 crypto_cipher_alg(cipher
)->cia_encrypt
);
53 static int crypto_ecb_decrypt(struct skcipher_request
*req
)
55 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
56 struct crypto_cipher
*cipher
= skcipher_cipher_simple(tfm
);
58 return crypto_ecb_crypt(req
, cipher
,
59 crypto_cipher_alg(cipher
)->cia_decrypt
);
62 static int crypto_ecb_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
64 struct skcipher_instance
*inst
;
67 inst
= skcipher_alloc_instance_simple(tmpl
, tb
);
71 inst
->alg
.ivsize
= 0; /* ECB mode doesn't take an IV */
73 inst
->alg
.encrypt
= crypto_ecb_encrypt
;
74 inst
->alg
.decrypt
= crypto_ecb_decrypt
;
76 err
= skcipher_register_instance(tmpl
, inst
);
83 static struct crypto_template crypto_ecb_tmpl
= {
85 .create
= crypto_ecb_create
,
86 .module
= THIS_MODULE
,
89 static int __init
crypto_ecb_module_init(void)
91 return crypto_register_template(&crypto_ecb_tmpl
);
94 static void __exit
crypto_ecb_module_exit(void)
96 crypto_unregister_template(&crypto_ecb_tmpl
);
99 subsys_initcall(crypto_ecb_module_init
);
100 module_exit(crypto_ecb_module_exit
);
102 MODULE_LICENSE("GPL");
103 MODULE_DESCRIPTION("ECB block cipher mode of operation");
104 MODULE_ALIAS_CRYPTO("ecb");