blob: 0732715c8d915514940d29ca5f92cf336079477b [file] [log] [blame]
Herbert Xudb131ef2006-09-21 11:44:08 +10001/*
2 * ECB: Electronic CodeBook mode
3 *
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <crypto/algapi.h>
Eric Biggers52e93682019-01-03 20:16:18 -080014#include <crypto/internal/skcipher.h>
Herbert Xudb131ef2006-09-21 11:44:08 +100015#include <linux/err.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
Herbert Xudb131ef2006-09-21 11:44:08 +100019
Eric Biggers52e93682019-01-03 20:16:18 -080020static int crypto_ecb_crypt(struct skcipher_request *req,
21 struct crypto_cipher *cipher,
Herbert Xudb131ef2006-09-21 11:44:08 +100022 void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
23{
Eric Biggers52e93682019-01-03 20:16:18 -080024 const unsigned int bsize = crypto_cipher_blocksize(cipher);
25 struct skcipher_walk walk;
Herbert Xudb131ef2006-09-21 11:44:08 +100026 unsigned int nbytes;
27 int err;
28
Eric Biggers52e93682019-01-03 20:16:18 -080029 err = skcipher_walk_virt(&walk, req, false);
Herbert Xudb131ef2006-09-21 11:44:08 +100030
Eric Biggers52e93682019-01-03 20:16:18 -080031 while ((nbytes = walk.nbytes) != 0) {
32 const u8 *src = walk.src.virt.addr;
33 u8 *dst = walk.dst.virt.addr;
Herbert Xudb131ef2006-09-21 11:44:08 +100034
35 do {
Eric Biggers52e93682019-01-03 20:16:18 -080036 fn(crypto_cipher_tfm(cipher), dst, src);
Richard Hartmann5b37c192010-02-16 20:33:49 +080037
Eric Biggers52e93682019-01-03 20:16:18 -080038 src += bsize;
39 dst += bsize;
Herbert Xudb131ef2006-09-21 11:44:08 +100040 } while ((nbytes -= bsize) >= bsize);
41
Eric Biggers52e93682019-01-03 20:16:18 -080042 err = skcipher_walk_done(&walk, nbytes);
Herbert Xudb131ef2006-09-21 11:44:08 +100043 }
44
45 return err;
46}
47
Eric Biggers52e93682019-01-03 20:16:18 -080048static int crypto_ecb_encrypt(struct skcipher_request *req)
Herbert Xudb131ef2006-09-21 11:44:08 +100049{
Eric Biggers52e93682019-01-03 20:16:18 -080050 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
51 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Herbert Xudb131ef2006-09-21 11:44:08 +100052
Eric Biggers52e93682019-01-03 20:16:18 -080053 return crypto_ecb_crypt(req, cipher,
54 crypto_cipher_alg(cipher)->cia_encrypt);
Herbert Xudb131ef2006-09-21 11:44:08 +100055}
56
Eric Biggers52e93682019-01-03 20:16:18 -080057static int crypto_ecb_decrypt(struct skcipher_request *req)
Herbert Xudb131ef2006-09-21 11:44:08 +100058{
Eric Biggers52e93682019-01-03 20:16:18 -080059 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
60 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Herbert Xudb131ef2006-09-21 11:44:08 +100061
Eric Biggers52e93682019-01-03 20:16:18 -080062 return crypto_ecb_crypt(req, cipher,
63 crypto_cipher_alg(cipher)->cia_decrypt);
Herbert Xudb131ef2006-09-21 11:44:08 +100064}
65
Eric Biggers52e93682019-01-03 20:16:18 -080066static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xudb131ef2006-09-21 11:44:08 +100067{
Eric Biggers52e93682019-01-03 20:16:18 -080068 struct skcipher_instance *inst;
Herbert Xudb131ef2006-09-21 11:44:08 +100069 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +110070 int err;
Herbert Xudb131ef2006-09-21 11:44:08 +100071
Eric Biggers52e93682019-01-03 20:16:18 -080072 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
Herbert Xudb131ef2006-09-21 11:44:08 +100073 if (IS_ERR(inst))
Eric Biggers52e93682019-01-03 20:16:18 -080074 return PTR_ERR(inst);
Herbert Xudb131ef2006-09-21 11:44:08 +100075
Eric Biggers52e93682019-01-03 20:16:18 -080076 inst->alg.ivsize = 0; /* ECB mode doesn't take an IV */
Herbert Xudb131ef2006-09-21 11:44:08 +100077
Eric Biggers52e93682019-01-03 20:16:18 -080078 inst->alg.encrypt = crypto_ecb_encrypt;
79 inst->alg.decrypt = crypto_ecb_decrypt;
Herbert Xudb131ef2006-09-21 11:44:08 +100080
Eric Biggers52e93682019-01-03 20:16:18 -080081 err = skcipher_register_instance(tmpl, inst);
82 if (err)
83 inst->free(inst);
Herbert Xudb131ef2006-09-21 11:44:08 +100084 crypto_mod_put(alg);
Eric Biggers52e93682019-01-03 20:16:18 -080085 return err;
Herbert Xudb131ef2006-09-21 11:44:08 +100086}
87
88static struct crypto_template crypto_ecb_tmpl = {
89 .name = "ecb",
Eric Biggers52e93682019-01-03 20:16:18 -080090 .create = crypto_ecb_create,
Herbert Xudb131ef2006-09-21 11:44:08 +100091 .module = THIS_MODULE,
92};
93
94static int __init crypto_ecb_module_init(void)
95{
96 return crypto_register_template(&crypto_ecb_tmpl);
97}
98
99static void __exit crypto_ecb_module_exit(void)
100{
101 crypto_unregister_template(&crypto_ecb_tmpl);
102}
103
104module_init(crypto_ecb_module_init);
105module_exit(crypto_ecb_module_exit);
106
107MODULE_LICENSE("GPL");
Eric Biggers52e93682019-01-03 20:16:18 -0800108MODULE_DESCRIPTION("ECB block cipher mode of operation");
Kees Cook4943ba12014-11-24 16:32:38 -0800109MODULE_ALIAS_CRYPTO("ecb");