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