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