blob: 9d6981ca7d5d3db38b97926da14a7c40c443dddd [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xudb131ef2006-09-21 11:44:08 +10002/*
3 * ECB: Electronic CodeBook mode
4 *
5 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xudb131ef2006-09-21 11:44:08 +10006 */
7
8#include <crypto/algapi.h>
Eric Biggers52e93682019-01-03 20:16:18 -08009#include <crypto/internal/skcipher.h>
Herbert Xudb131ef2006-09-21 11:44:08 +100010#include <linux/err.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
Herbert Xudb131ef2006-09-21 11:44:08 +100014
Eric Biggers52e93682019-01-03 20:16:18 -080015static int crypto_ecb_crypt(struct skcipher_request *req,
16 struct crypto_cipher *cipher,
Herbert Xudb131ef2006-09-21 11:44:08 +100017 void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
18{
Eric Biggers52e93682019-01-03 20:16:18 -080019 const unsigned int bsize = crypto_cipher_blocksize(cipher);
20 struct skcipher_walk walk;
Herbert Xudb131ef2006-09-21 11:44:08 +100021 unsigned int nbytes;
22 int err;
23
Eric Biggers52e93682019-01-03 20:16:18 -080024 err = skcipher_walk_virt(&walk, req, false);
Herbert Xudb131ef2006-09-21 11:44:08 +100025
Eric Biggers52e93682019-01-03 20:16:18 -080026 while ((nbytes = walk.nbytes) != 0) {
27 const u8 *src = walk.src.virt.addr;
28 u8 *dst = walk.dst.virt.addr;
Herbert Xudb131ef2006-09-21 11:44:08 +100029
30 do {
Eric Biggers52e93682019-01-03 20:16:18 -080031 fn(crypto_cipher_tfm(cipher), dst, src);
Richard Hartmann5b37c192010-02-16 20:33:49 +080032
Eric Biggers52e93682019-01-03 20:16:18 -080033 src += bsize;
34 dst += bsize;
Herbert Xudb131ef2006-09-21 11:44:08 +100035 } while ((nbytes -= bsize) >= bsize);
36
Eric Biggers52e93682019-01-03 20:16:18 -080037 err = skcipher_walk_done(&walk, nbytes);
Herbert Xudb131ef2006-09-21 11:44:08 +100038 }
39
40 return err;
41}
42
Eric Biggers52e93682019-01-03 20:16:18 -080043static int crypto_ecb_encrypt(struct skcipher_request *req)
Herbert Xudb131ef2006-09-21 11:44:08 +100044{
Eric Biggers52e93682019-01-03 20:16:18 -080045 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
46 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Herbert Xudb131ef2006-09-21 11:44:08 +100047
Eric Biggers52e93682019-01-03 20:16:18 -080048 return crypto_ecb_crypt(req, cipher,
49 crypto_cipher_alg(cipher)->cia_encrypt);
Herbert Xudb131ef2006-09-21 11:44:08 +100050}
51
Eric Biggers52e93682019-01-03 20:16:18 -080052static int crypto_ecb_decrypt(struct skcipher_request *req)
Herbert Xudb131ef2006-09-21 11:44:08 +100053{
Eric Biggers52e93682019-01-03 20:16:18 -080054 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
55 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Herbert Xudb131ef2006-09-21 11:44:08 +100056
Eric Biggers52e93682019-01-03 20:16:18 -080057 return crypto_ecb_crypt(req, cipher,
58 crypto_cipher_alg(cipher)->cia_decrypt);
Herbert Xudb131ef2006-09-21 11:44:08 +100059}
60
Eric Biggers52e93682019-01-03 20:16:18 -080061static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xudb131ef2006-09-21 11:44:08 +100062{
Eric Biggers52e93682019-01-03 20:16:18 -080063 struct skcipher_instance *inst;
Herbert Xudb131ef2006-09-21 11:44:08 +100064 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +110065 int err;
Herbert Xudb131ef2006-09-21 11:44:08 +100066
Eric Biggers52e93682019-01-03 20:16:18 -080067 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
Herbert Xudb131ef2006-09-21 11:44:08 +100068 if (IS_ERR(inst))
Eric Biggers52e93682019-01-03 20:16:18 -080069 return PTR_ERR(inst);
Herbert Xudb131ef2006-09-21 11:44:08 +100070
Eric Biggers52e93682019-01-03 20:16:18 -080071 inst->alg.ivsize = 0; /* ECB mode doesn't take an IV */
Herbert Xudb131ef2006-09-21 11:44:08 +100072
Eric Biggers52e93682019-01-03 20:16:18 -080073 inst->alg.encrypt = crypto_ecb_encrypt;
74 inst->alg.decrypt = crypto_ecb_decrypt;
Herbert Xudb131ef2006-09-21 11:44:08 +100075
Eric Biggers52e93682019-01-03 20:16:18 -080076 err = skcipher_register_instance(tmpl, inst);
77 if (err)
78 inst->free(inst);
Herbert Xudb131ef2006-09-21 11:44:08 +100079 crypto_mod_put(alg);
Eric Biggers52e93682019-01-03 20:16:18 -080080 return err;
Herbert Xudb131ef2006-09-21 11:44:08 +100081}
82
83static struct crypto_template crypto_ecb_tmpl = {
84 .name = "ecb",
Eric Biggers52e93682019-01-03 20:16:18 -080085 .create = crypto_ecb_create,
Herbert Xudb131ef2006-09-21 11:44:08 +100086 .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
Eric Biggersc4741b22019-04-11 21:57:42 -070099subsys_initcall(crypto_ecb_module_init);
Herbert Xudb131ef2006-09-21 11:44:08 +1000100module_exit(crypto_ecb_module_exit);
101
102MODULE_LICENSE("GPL");
Eric Biggers52e93682019-01-03 20:16:18 -0800103MODULE_DESCRIPTION("ECB block cipher mode of operation");
Kees Cook4943ba12014-11-24 16:32:38 -0800104MODULE_ALIAS_CRYPTO("ecb");