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