blob: 9d6981ca7d5d3db38b97926da14a7c40c443dddd [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * ECB: Electronic CodeBook mode
4 *
5 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
7
8#include <crypto/algapi.h>
David Brazdil0f672f62019-12-10 10:32:29 +00009#include <crypto/internal/skcipher.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010#include <linux/err.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014
David Brazdil0f672f62019-12-10 10:32:29 +000015static int crypto_ecb_crypt(struct skcipher_request *req,
16 struct crypto_cipher *cipher,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017 void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
18{
David Brazdil0f672f62019-12-10 10:32:29 +000019 const unsigned int bsize = crypto_cipher_blocksize(cipher);
20 struct skcipher_walk walk;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021 unsigned int nbytes;
22 int err;
23
David Brazdil0f672f62019-12-10 10:32:29 +000024 err = skcipher_walk_virt(&walk, req, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025
David Brazdil0f672f62019-12-10 10:32:29 +000026 while ((nbytes = walk.nbytes) != 0) {
27 const u8 *src = walk.src.virt.addr;
28 u8 *dst = walk.dst.virt.addr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029
30 do {
David Brazdil0f672f62019-12-10 10:32:29 +000031 fn(crypto_cipher_tfm(cipher), dst, src);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032
David Brazdil0f672f62019-12-10 10:32:29 +000033 src += bsize;
34 dst += bsize;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035 } while ((nbytes -= bsize) >= bsize);
36
David Brazdil0f672f62019-12-10 10:32:29 +000037 err = skcipher_walk_done(&walk, nbytes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038 }
39
40 return err;
41}
42
David Brazdil0f672f62019-12-10 10:32:29 +000043static int crypto_ecb_encrypt(struct skcipher_request *req)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044{
David Brazdil0f672f62019-12-10 10:32:29 +000045 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
46 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047
David Brazdil0f672f62019-12-10 10:32:29 +000048 return crypto_ecb_crypt(req, cipher,
49 crypto_cipher_alg(cipher)->cia_encrypt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050}
51
David Brazdil0f672f62019-12-10 10:32:29 +000052static int crypto_ecb_decrypt(struct skcipher_request *req)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053{
David Brazdil0f672f62019-12-10 10:32:29 +000054 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
55 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056
David Brazdil0f672f62019-12-10 10:32:29 +000057 return crypto_ecb_crypt(req, cipher,
58 crypto_cipher_alg(cipher)->cia_decrypt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059}
60
David Brazdil0f672f62019-12-10 10:32:29 +000061static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062{
David Brazdil0f672f62019-12-10 10:32:29 +000063 struct skcipher_instance *inst;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064 struct crypto_alg *alg;
65 int err;
66
David Brazdil0f672f62019-12-10 10:32:29 +000067 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068 if (IS_ERR(inst))
David Brazdil0f672f62019-12-10 10:32:29 +000069 return PTR_ERR(inst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070
David Brazdil0f672f62019-12-10 10:32:29 +000071 inst->alg.ivsize = 0; /* ECB mode doesn't take an IV */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072
David Brazdil0f672f62019-12-10 10:32:29 +000073 inst->alg.encrypt = crypto_ecb_encrypt;
74 inst->alg.decrypt = crypto_ecb_decrypt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075
David Brazdil0f672f62019-12-10 10:32:29 +000076 err = skcipher_register_instance(tmpl, inst);
77 if (err)
78 inst->free(inst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079 crypto_mod_put(alg);
David Brazdil0f672f62019-12-10 10:32:29 +000080 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081}
82
83static struct crypto_template crypto_ecb_tmpl = {
84 .name = "ecb",
David Brazdil0f672f62019-12-10 10:32:29 +000085 .create = crypto_ecb_create,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086 .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
David Brazdil0f672f62019-12-10 10:32:29 +000099subsys_initcall(crypto_ecb_module_init);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000100module_exit(crypto_ecb_module_exit);
101
102MODULE_LICENSE("GPL");
David Brazdil0f672f62019-12-10 10:32:29 +0000103MODULE_DESCRIPTION("ECB block cipher mode of operation");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104MODULE_ALIAS_CRYPTO("ecb");