Commit | Line | Data |
---|---|---|
333b0d7e KM |
1 | /* |
2 | * Copyright (C)2006 USAGI/WIDE Project | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | * | |
18 | * Author: | |
19 | * Kazunori Miyazawa <miyazawa@linux-ipv6.org> | |
20 | */ | |
21 | ||
3106caab | 22 | #include <crypto/internal/hash.h> |
333b0d7e KM |
23 | #include <linux/err.h> |
24 | #include <linux/kernel.h> | |
333b0d7e | 25 | |
5b37538a AB |
26 | static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101, |
27 | 0x02020202, 0x02020202, 0x02020202, 0x02020202, | |
28 | 0x03030303, 0x03030303, 0x03030303, 0x03030303}; | |
333b0d7e KM |
29 | /* |
30 | * +------------------------ | |
31 | * | <parent tfm> | |
32 | * +------------------------ | |
33 | * | crypto_xcbc_ctx | |
34 | * +------------------------ | |
35 | * | odds (block size) | |
36 | * +------------------------ | |
37 | * | prev (block size) | |
38 | * +------------------------ | |
39 | * | key (block size) | |
40 | * +------------------------ | |
41 | * | consts (block size * 3) | |
42 | * +------------------------ | |
43 | */ | |
44 | struct crypto_xcbc_ctx { | |
6b701dde | 45 | struct crypto_cipher *child; |
333b0d7e KM |
46 | u8 *odds; |
47 | u8 *prev; | |
48 | u8 *key; | |
49 | u8 *consts; | |
333b0d7e KM |
50 | unsigned int keylen; |
51 | unsigned int len; | |
52 | }; | |
53 | ||
3106caab | 54 | static int _crypto_xcbc_digest_setkey(struct crypto_shash *parent, |
333b0d7e KM |
55 | struct crypto_xcbc_ctx *ctx) |
56 | { | |
3106caab | 57 | int bs = crypto_shash_blocksize(parent); |
333b0d7e KM |
58 | int err = 0; |
59 | u8 key1[bs]; | |
60 | ||
61 | if ((err = crypto_cipher_setkey(ctx->child, ctx->key, ctx->keylen))) | |
62 | return err; | |
63 | ||
6b701dde | 64 | crypto_cipher_encrypt_one(ctx->child, key1, ctx->consts); |
333b0d7e KM |
65 | |
66 | return crypto_cipher_setkey(ctx->child, key1, bs); | |
67 | } | |
68 | ||
3106caab | 69 | static int crypto_xcbc_digest_setkey(struct crypto_shash *parent, |
333b0d7e KM |
70 | const u8 *inkey, unsigned int keylen) |
71 | { | |
3106caab | 72 | struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent); |
333b0d7e | 73 | |
6b701dde | 74 | if (keylen != crypto_cipher_blocksize(ctx->child)) |
333b0d7e KM |
75 | return -EINVAL; |
76 | ||
77 | ctx->keylen = keylen; | |
78 | memcpy(ctx->key, inkey, keylen); | |
79 | ctx->consts = (u8*)ks; | |
80 | ||
81 | return _crypto_xcbc_digest_setkey(parent, ctx); | |
82 | } | |
83 | ||
3106caab | 84 | static int crypto_xcbc_digest_init(struct shash_desc *pdesc) |
333b0d7e | 85 | { |
3106caab HX |
86 | struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(pdesc->tfm); |
87 | int bs = crypto_shash_blocksize(pdesc->tfm); | |
333b0d7e KM |
88 | |
89 | ctx->len = 0; | |
90 | memset(ctx->odds, 0, bs); | |
91 | memset(ctx->prev, 0, bs); | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
3106caab HX |
96 | static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p, |
97 | unsigned int len) | |
333b0d7e | 98 | { |
3106caab HX |
99 | struct crypto_shash *parent = pdesc->tfm; |
100 | struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent); | |
6b701dde | 101 | struct crypto_cipher *tfm = ctx->child; |
3106caab HX |
102 | int bs = crypto_shash_blocksize(parent); |
103 | ||
104 | /* checking the data can fill the block */ | |
105 | if ((ctx->len + len) <= bs) { | |
106 | memcpy(ctx->odds + ctx->len, p, len); | |
107 | ctx->len += len; | |
108 | return 0; | |
1edcf2e1 | 109 | } |
333b0d7e | 110 | |
3106caab HX |
111 | /* filling odds with new data and encrypting it */ |
112 | memcpy(ctx->odds + ctx->len, p, bs - ctx->len); | |
113 | len -= bs - ctx->len; | |
114 | p += bs - ctx->len; | |
333b0d7e | 115 | |
b588ef6e | 116 | crypto_xor(ctx->prev, ctx->odds, bs); |
3106caab HX |
117 | crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev); |
118 | ||
119 | /* clearing the length */ | |
120 | ctx->len = 0; | |
121 | ||
122 | /* encrypting the rest of data */ | |
123 | while (len > bs) { | |
b588ef6e | 124 | crypto_xor(ctx->prev, p, bs); |
3106caab HX |
125 | crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev); |
126 | p += bs; | |
127 | len -= bs; | |
128 | } | |
129 | ||
130 | /* keeping the surplus of blocksize */ | |
131 | if (len) { | |
132 | memcpy(ctx->odds, p, len); | |
133 | ctx->len = len; | |
134 | } | |
135 | ||
136 | return 0; | |
fb469840 HX |
137 | } |
138 | ||
3106caab | 139 | static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out) |
333b0d7e | 140 | { |
3106caab HX |
141 | struct crypto_shash *parent = pdesc->tfm; |
142 | struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent); | |
6b701dde | 143 | struct crypto_cipher *tfm = ctx->child; |
3106caab | 144 | int bs = crypto_shash_blocksize(parent); |
333b0d7e KM |
145 | int err = 0; |
146 | ||
147 | if (ctx->len == bs) { | |
148 | u8 key2[bs]; | |
149 | ||
150 | if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0) | |
151 | return err; | |
152 | ||
6b701dde HX |
153 | crypto_cipher_encrypt_one(tfm, key2, |
154 | (u8 *)(ctx->consts + bs)); | |
333b0d7e | 155 | |
b588ef6e HX |
156 | crypto_xor(ctx->prev, ctx->odds, bs); |
157 | crypto_xor(ctx->prev, key2, bs); | |
333b0d7e KM |
158 | _crypto_xcbc_digest_setkey(parent, ctx); |
159 | ||
6b701dde | 160 | crypto_cipher_encrypt_one(tfm, out, ctx->prev); |
333b0d7e KM |
161 | } else { |
162 | u8 key3[bs]; | |
163 | unsigned int rlen; | |
164 | u8 *p = ctx->odds + ctx->len; | |
165 | *p = 0x80; | |
166 | p++; | |
167 | ||
168 | rlen = bs - ctx->len -1; | |
169 | if (rlen) | |
170 | memset(p, 0, rlen); | |
171 | ||
172 | if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0) | |
173 | return err; | |
174 | ||
6b701dde HX |
175 | crypto_cipher_encrypt_one(tfm, key3, |
176 | (u8 *)(ctx->consts + bs * 2)); | |
333b0d7e | 177 | |
b588ef6e HX |
178 | crypto_xor(ctx->prev, ctx->odds, bs); |
179 | crypto_xor(ctx->prev, key3, bs); | |
333b0d7e KM |
180 | |
181 | _crypto_xcbc_digest_setkey(parent, ctx); | |
182 | ||
6b701dde | 183 | crypto_cipher_encrypt_one(tfm, out, ctx->prev); |
333b0d7e KM |
184 | } |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
333b0d7e KM |
189 | static int xcbc_init_tfm(struct crypto_tfm *tfm) |
190 | { | |
2e306ee0 | 191 | struct crypto_cipher *cipher; |
333b0d7e KM |
192 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
193 | struct crypto_spawn *spawn = crypto_instance_ctx(inst); | |
3106caab HX |
194 | struct crypto_xcbc_ctx *ctx = crypto_tfm_ctx(tfm); |
195 | int bs = crypto_tfm_alg_blocksize(tfm); | |
333b0d7e | 196 | |
2e306ee0 HX |
197 | cipher = crypto_spawn_cipher(spawn); |
198 | if (IS_ERR(cipher)) | |
199 | return PTR_ERR(cipher); | |
333b0d7e KM |
200 | |
201 | switch(bs) { | |
202 | case 16: | |
333b0d7e KM |
203 | break; |
204 | default: | |
205 | return -EINVAL; | |
206 | } | |
207 | ||
2e306ee0 | 208 | ctx->child = cipher; |
333b0d7e KM |
209 | ctx->odds = (u8*)(ctx+1); |
210 | ctx->prev = ctx->odds + bs; | |
211 | ctx->key = ctx->prev + bs; | |
212 | ||
213 | return 0; | |
214 | }; | |
215 | ||
216 | static void xcbc_exit_tfm(struct crypto_tfm *tfm) | |
217 | { | |
3106caab | 218 | struct crypto_xcbc_ctx *ctx = crypto_tfm_ctx(tfm); |
333b0d7e KM |
219 | crypto_free_cipher(ctx->child); |
220 | } | |
221 | ||
3106caab | 222 | static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb) |
333b0d7e | 223 | { |
3106caab | 224 | struct shash_instance *inst; |
333b0d7e | 225 | struct crypto_alg *alg; |
ebc610e5 HX |
226 | int err; |
227 | ||
3106caab | 228 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH); |
ebc610e5 | 229 | if (err) |
3106caab | 230 | return err; |
ebc610e5 HX |
231 | |
232 | alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, | |
233 | CRYPTO_ALG_TYPE_MASK); | |
333b0d7e | 234 | if (IS_ERR(alg)) |
3106caab | 235 | return PTR_ERR(alg); |
333b0d7e KM |
236 | |
237 | switch(alg->cra_blocksize) { | |
238 | case 16: | |
239 | break; | |
240 | default: | |
1b87887d | 241 | goto out_put_alg; |
333b0d7e KM |
242 | } |
243 | ||
3106caab | 244 | inst = shash_alloc_instance("xcbc", alg); |
b5ebd44e | 245 | err = PTR_ERR(inst); |
333b0d7e KM |
246 | if (IS_ERR(inst)) |
247 | goto out_put_alg; | |
248 | ||
3106caab HX |
249 | err = crypto_init_spawn(shash_instance_ctx(inst), alg, |
250 | shash_crypto_instance(inst), | |
251 | CRYPTO_ALG_TYPE_MASK); | |
252 | if (err) | |
253 | goto out_free_inst; | |
254 | ||
255 | inst->alg.base.cra_priority = alg->cra_priority; | |
256 | inst->alg.base.cra_blocksize = alg->cra_blocksize; | |
257 | inst->alg.base.cra_alignmask = alg->cra_alignmask; | |
258 | ||
259 | inst->alg.digestsize = alg->cra_blocksize; | |
260 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_xcbc_ctx) + | |
261 | ALIGN(alg->cra_blocksize * 3, | |
262 | sizeof(void *)); | |
263 | inst->alg.base.cra_init = xcbc_init_tfm; | |
264 | inst->alg.base.cra_exit = xcbc_exit_tfm; | |
265 | ||
266 | inst->alg.init = crypto_xcbc_digest_init; | |
267 | inst->alg.update = crypto_xcbc_digest_update; | |
268 | inst->alg.final = crypto_xcbc_digest_final; | |
269 | inst->alg.setkey = crypto_xcbc_digest_setkey; | |
270 | ||
271 | err = shash_register_instance(tmpl, inst); | |
272 | if (err) { | |
273 | out_free_inst: | |
274 | shash_free_instance(shash_crypto_instance(inst)); | |
275 | } | |
333b0d7e KM |
276 | |
277 | out_put_alg: | |
278 | crypto_mod_put(alg); | |
3106caab | 279 | return err; |
333b0d7e KM |
280 | } |
281 | ||
282 | static struct crypto_template crypto_xcbc_tmpl = { | |
283 | .name = "xcbc", | |
3106caab HX |
284 | .create = xcbc_create, |
285 | .free = shash_free_instance, | |
333b0d7e KM |
286 | .module = THIS_MODULE, |
287 | }; | |
288 | ||
289 | static int __init crypto_xcbc_module_init(void) | |
290 | { | |
291 | return crypto_register_template(&crypto_xcbc_tmpl); | |
292 | } | |
293 | ||
294 | static void __exit crypto_xcbc_module_exit(void) | |
295 | { | |
296 | crypto_unregister_template(&crypto_xcbc_tmpl); | |
297 | } | |
298 | ||
299 | module_init(crypto_xcbc_module_init); | |
300 | module_exit(crypto_xcbc_module_exit); | |
301 | ||
302 | MODULE_LICENSE("GPL"); | |
303 | MODULE_DESCRIPTION("XCBC keyed hash algorithm"); |