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