]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
db131ef9 HX |
2 | /* |
3 | * ECB: Electronic CodeBook mode | |
4 | * | |
5 | * Copyright (c) 2006 Herbert Xu <[email protected]> | |
db131ef9 HX |
6 | */ |
7 | ||
0eb76ba2 | 8 | #include <crypto/internal/cipher.h> |
52e9368f | 9 | #include <crypto/internal/skcipher.h> |
db131ef9 HX |
10 | #include <linux/err.h> |
11 | #include <linux/init.h> | |
12 | #include <linux/kernel.h> | |
13 | #include <linux/module.h> | |
32a8dc4a | 14 | #include <linux/slab.h> |
db131ef9 | 15 | |
32a8dc4a HX |
16 | static int crypto_ecb_crypt(struct crypto_cipher *cipher, const u8 *src, |
17 | u8 *dst, unsigned nbytes, bool final, | |
db131ef9 HX |
18 | void (*fn)(struct crypto_tfm *, u8 *, const u8 *)) |
19 | { | |
52e9368f | 20 | const unsigned int bsize = crypto_cipher_blocksize(cipher); |
db131ef9 | 21 | |
32a8dc4a HX |
22 | while (nbytes >= bsize) { |
23 | fn(crypto_cipher_tfm(cipher), dst, src); | |
db131ef9 | 24 | |
32a8dc4a HX |
25 | src += bsize; |
26 | dst += bsize; | |
5b37c19e | 27 | |
32a8dc4a | 28 | nbytes -= bsize; |
db131ef9 HX |
29 | } |
30 | ||
32a8dc4a | 31 | return nbytes && final ? -EINVAL : nbytes; |
db131ef9 HX |
32 | } |
33 | ||
32a8dc4a HX |
34 | static int crypto_ecb_encrypt2(struct crypto_lskcipher *tfm, const u8 *src, |
35 | u8 *dst, unsigned len, u8 *iv, bool final) | |
db131ef9 | 36 | { |
32a8dc4a HX |
37 | struct crypto_cipher **ctx = crypto_lskcipher_ctx(tfm); |
38 | struct crypto_cipher *cipher = *ctx; | |
db131ef9 | 39 | |
32a8dc4a | 40 | return crypto_ecb_crypt(cipher, src, dst, len, final, |
52e9368f | 41 | crypto_cipher_alg(cipher)->cia_encrypt); |
db131ef9 HX |
42 | } |
43 | ||
32a8dc4a HX |
44 | static int crypto_ecb_decrypt2(struct crypto_lskcipher *tfm, const u8 *src, |
45 | u8 *dst, unsigned len, u8 *iv, bool final) | |
db131ef9 | 46 | { |
32a8dc4a HX |
47 | struct crypto_cipher **ctx = crypto_lskcipher_ctx(tfm); |
48 | struct crypto_cipher *cipher = *ctx; | |
db131ef9 | 49 | |
32a8dc4a | 50 | return crypto_ecb_crypt(cipher, src, dst, len, final, |
52e9368f | 51 | crypto_cipher_alg(cipher)->cia_decrypt); |
db131ef9 HX |
52 | } |
53 | ||
32a8dc4a HX |
54 | static int lskcipher_setkey_simple2(struct crypto_lskcipher *tfm, |
55 | const u8 *key, unsigned int keylen) | |
56 | { | |
57 | struct crypto_cipher **ctx = crypto_lskcipher_ctx(tfm); | |
58 | struct crypto_cipher *cipher = *ctx; | |
59 | ||
60 | crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK); | |
61 | crypto_cipher_set_flags(cipher, crypto_lskcipher_get_flags(tfm) & | |
62 | CRYPTO_TFM_REQ_MASK); | |
63 | return crypto_cipher_setkey(cipher, key, keylen); | |
64 | } | |
65 | ||
66 | static int lskcipher_init_tfm_simple2(struct crypto_lskcipher *tfm) | |
67 | { | |
68 | struct lskcipher_instance *inst = lskcipher_alg_instance(tfm); | |
69 | struct crypto_cipher **ctx = crypto_lskcipher_ctx(tfm); | |
70 | struct crypto_cipher_spawn *spawn; | |
71 | struct crypto_cipher *cipher; | |
72 | ||
73 | spawn = lskcipher_instance_ctx(inst); | |
74 | cipher = crypto_spawn_cipher(spawn); | |
75 | if (IS_ERR(cipher)) | |
76 | return PTR_ERR(cipher); | |
77 | ||
78 | *ctx = cipher; | |
79 | return 0; | |
80 | } | |
81 | ||
82 | static void lskcipher_exit_tfm_simple2(struct crypto_lskcipher *tfm) | |
83 | { | |
84 | struct crypto_cipher **ctx = crypto_lskcipher_ctx(tfm); | |
85 | ||
86 | crypto_free_cipher(*ctx); | |
87 | } | |
88 | ||
89 | static void lskcipher_free_instance_simple2(struct lskcipher_instance *inst) | |
90 | { | |
91 | crypto_drop_cipher(lskcipher_instance_ctx(inst)); | |
92 | kfree(inst); | |
93 | } | |
94 | ||
95 | static struct lskcipher_instance *lskcipher_alloc_instance_simple2( | |
96 | struct crypto_template *tmpl, struct rtattr **tb) | |
97 | { | |
98 | struct crypto_cipher_spawn *spawn; | |
99 | struct lskcipher_instance *inst; | |
100 | struct crypto_alg *cipher_alg; | |
101 | u32 mask; | |
102 | int err; | |
103 | ||
104 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_LSKCIPHER, &mask); | |
105 | if (err) | |
106 | return ERR_PTR(err); | |
107 | ||
108 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); | |
109 | if (!inst) | |
110 | return ERR_PTR(-ENOMEM); | |
111 | spawn = lskcipher_instance_ctx(inst); | |
112 | ||
113 | err = crypto_grab_cipher(spawn, lskcipher_crypto_instance(inst), | |
114 | crypto_attr_alg_name(tb[1]), 0, mask); | |
115 | if (err) | |
116 | goto err_free_inst; | |
117 | cipher_alg = crypto_spawn_cipher_alg(spawn); | |
118 | ||
119 | err = crypto_inst_setname(lskcipher_crypto_instance(inst), tmpl->name, | |
120 | cipher_alg); | |
121 | if (err) | |
122 | goto err_free_inst; | |
123 | ||
124 | inst->free = lskcipher_free_instance_simple2; | |
125 | ||
126 | /* Default algorithm properties, can be overridden */ | |
127 | inst->alg.co.base.cra_blocksize = cipher_alg->cra_blocksize; | |
128 | inst->alg.co.base.cra_alignmask = cipher_alg->cra_alignmask; | |
129 | inst->alg.co.base.cra_priority = cipher_alg->cra_priority; | |
130 | inst->alg.co.min_keysize = cipher_alg->cra_cipher.cia_min_keysize; | |
131 | inst->alg.co.max_keysize = cipher_alg->cra_cipher.cia_max_keysize; | |
132 | inst->alg.co.ivsize = cipher_alg->cra_blocksize; | |
133 | ||
134 | /* Use struct crypto_cipher * by default, can be overridden */ | |
135 | inst->alg.co.base.cra_ctxsize = sizeof(struct crypto_cipher *); | |
136 | inst->alg.setkey = lskcipher_setkey_simple2; | |
137 | inst->alg.init = lskcipher_init_tfm_simple2; | |
138 | inst->alg.exit = lskcipher_exit_tfm_simple2; | |
139 | ||
140 | return inst; | |
141 | ||
142 | err_free_inst: | |
143 | lskcipher_free_instance_simple2(inst); | |
144 | return ERR_PTR(err); | |
145 | } | |
146 | ||
147 | static int crypto_ecb_create2(struct crypto_template *tmpl, struct rtattr **tb) | |
db131ef9 | 148 | { |
32a8dc4a | 149 | struct lskcipher_instance *inst; |
ebc610e5 HX |
150 | int err; |
151 | ||
32a8dc4a | 152 | inst = lskcipher_alloc_instance_simple2(tmpl, tb); |
db131ef9 | 153 | if (IS_ERR(inst)) |
52e9368f | 154 | return PTR_ERR(inst); |
db131ef9 | 155 | |
32a8dc4a HX |
156 | /* ECB mode doesn't take an IV */ |
157 | inst->alg.co.ivsize = 0; | |
158 | ||
159 | inst->alg.encrypt = crypto_ecb_encrypt2; | |
160 | inst->alg.decrypt = crypto_ecb_decrypt2; | |
161 | ||
162 | err = lskcipher_register_instance(tmpl, inst); | |
163 | if (err) | |
164 | inst->free(inst); | |
165 | ||
166 | return err; | |
167 | } | |
168 | ||
169 | static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb) | |
170 | { | |
171 | struct crypto_lskcipher_spawn *spawn; | |
172 | struct lskcipher_alg *cipher_alg; | |
173 | struct lskcipher_instance *inst; | |
174 | int err; | |
175 | ||
176 | inst = lskcipher_alloc_instance_simple(tmpl, tb); | |
177 | if (IS_ERR(inst)) { | |
178 | err = crypto_ecb_create2(tmpl, tb); | |
179 | return err; | |
180 | } | |
181 | ||
182 | spawn = lskcipher_instance_ctx(inst); | |
183 | cipher_alg = crypto_lskcipher_spawn_alg(spawn); | |
184 | ||
185 | /* ECB mode doesn't take an IV */ | |
186 | inst->alg.co.ivsize = 0; | |
187 | if (cipher_alg->co.ivsize) | |
188 | return -EINVAL; | |
db131ef9 | 189 | |
32a8dc4a HX |
190 | inst->alg.co.base.cra_ctxsize = cipher_alg->co.base.cra_ctxsize; |
191 | inst->alg.setkey = cipher_alg->setkey; | |
192 | inst->alg.encrypt = cipher_alg->encrypt; | |
193 | inst->alg.decrypt = cipher_alg->decrypt; | |
194 | inst->alg.init = cipher_alg->init; | |
195 | inst->alg.exit = cipher_alg->exit; | |
db131ef9 | 196 | |
32a8dc4a | 197 | err = lskcipher_register_instance(tmpl, inst); |
52e9368f EB |
198 | if (err) |
199 | inst->free(inst); | |
b3c16bfc | 200 | |
52e9368f | 201 | return err; |
db131ef9 HX |
202 | } |
203 | ||
204 | static struct crypto_template crypto_ecb_tmpl = { | |
205 | .name = "ecb", | |
52e9368f | 206 | .create = crypto_ecb_create, |
db131ef9 HX |
207 | .module = THIS_MODULE, |
208 | }; | |
209 | ||
210 | static int __init crypto_ecb_module_init(void) | |
211 | { | |
212 | return crypto_register_template(&crypto_ecb_tmpl); | |
213 | } | |
214 | ||
215 | static void __exit crypto_ecb_module_exit(void) | |
216 | { | |
217 | crypto_unregister_template(&crypto_ecb_tmpl); | |
218 | } | |
219 | ||
c4741b23 | 220 | subsys_initcall(crypto_ecb_module_init); |
db131ef9 HX |
221 | module_exit(crypto_ecb_module_exit); |
222 | ||
223 | MODULE_LICENSE("GPL"); | |
52e9368f | 224 | MODULE_DESCRIPTION("ECB block cipher mode of operation"); |
4943ba16 | 225 | MODULE_ALIAS_CRYPTO("ecb"); |
32a8dc4a | 226 | MODULE_IMPORT_NS(CRYPTO_INTERNAL); |