Skip to content
Newer
Older
100644 289 lines (238 sloc) 7.2 KB
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
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
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
22 #include <crypto/internal/hash.h>
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
23 #include <linux/err.h>
24 #include <linux/kernel.h>
4bb33cc @paulgortmaker crypto: add module.h to those files that are explicitly using it
paulgortmaker authored May 27, 2011
25 #include <linux/module.h>
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
26
5b37538 @AdrianBunk [CRYPTO] xcbc: Make needlessly global code static
AdrianBunk authored Nov 17, 2006
27 static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
28 0x02020202, 0x02020202, 0x02020202, 0x02020202,
29 0x03030303, 0x03030303, 0x03030303, 0x03030303};
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
30
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
31 /*
32 * +------------------------
33 * | <parent tfm>
34 * +------------------------
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
35 * | xcbc_tfm_ctx
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
36 * +------------------------
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
37 * | consts (block size * 2)
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
38 * +------------------------
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
39 */
40 struct xcbc_tfm_ctx {
41 struct crypto_cipher *child;
42 u8 ctx[];
43 };
44
45 /*
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
46 * +------------------------
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
47 * | <shash desc>
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
48 * +------------------------
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
49 * | xcbc_desc_ctx
50 * +------------------------
51 * | odds (block size)
52 * +------------------------
53 * | prev (block size)
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
54 * +------------------------
55 */
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
56 struct xcbc_desc_ctx {
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
57 unsigned int len;
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
58 u8 ctx[];
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
59 };
60
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
61 static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
62 const u8 *inkey, unsigned int keylen)
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
63 {
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
64 unsigned long alignmask = crypto_shash_alignmask(parent);
65 struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
66 int bs = crypto_shash_blocksize(parent);
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
67 u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
68 int err = 0;
69 u8 key1[bs];
70
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
71 if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
72 return err;
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
73
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
74 crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
75 crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
76 crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
77
78 return crypto_cipher_setkey(ctx->child, key1, bs);
79
80 }
81
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
82 static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
83 {
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
84 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
85 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
86 int bs = crypto_shash_blocksize(pdesc->tfm);
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
87 u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
88
89 ctx->len = 0;
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
90 memset(prev, 0, bs);
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
91
92 return 0;
93 }
94
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
95 static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
96 unsigned int len)
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
97 {
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
98 struct crypto_shash *parent = pdesc->tfm;
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
99 unsigned long alignmask = crypto_shash_alignmask(parent);
100 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
101 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
102 struct crypto_cipher *tfm = tctx->child;
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
103 int bs = crypto_shash_blocksize(parent);
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
104 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
105 u8 *prev = odds + bs;
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
106
107 /* checking the data can fill the block */
108 if ((ctx->len + len) <= bs) {
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
109 memcpy(odds + ctx->len, p, len);
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
110 ctx->len += len;
111 return 0;
1edcf2e [CRYPTO] xcbc: Fix crash when ipsec uses xcbc-mac with big data chunk
Joy Latten authored Apr 2, 2008
112 }
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
113
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
114 /* filling odds with new data and encrypting it */
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
115 memcpy(odds + ctx->len, p, bs - ctx->len);
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
116 len -= bs - ctx->len;
117 p += bs - ctx->len;
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
118
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
119 crypto_xor(prev, odds, bs);
120 crypto_cipher_encrypt_one(tfm, prev, prev);
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
121
122 /* clearing the length */
123 ctx->len = 0;
124
125 /* encrypting the rest of data */
126 while (len > bs) {
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
127 crypto_xor(prev, p, bs);
128 crypto_cipher_encrypt_one(tfm, prev, prev);
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
129 p += bs;
130 len -= bs;
131 }
132
133 /* keeping the surplus of blocksize */
134 if (len) {
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
135 memcpy(odds, p, len);
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
136 ctx->len = len;
137 }
138
139 return 0;
fb46984 @herbertx [CRYPTO] all: Check for usage in hard IRQ context
herbertx authored Dec 10, 2006
140 }
141
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
142 static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
143 {
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
144 struct crypto_shash *parent = pdesc->tfm;
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
145 unsigned long alignmask = crypto_shash_alignmask(parent);
146 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
147 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
148 struct crypto_cipher *tfm = tctx->child;
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
149 int bs = crypto_shash_blocksize(parent);
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
150 u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
151 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
152 u8 *prev = odds + bs;
153 unsigned int offset = 0;
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
154
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
155 if (ctx->len != bs) {
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
156 unsigned int rlen;
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
157 u8 *p = odds + ctx->len;
158
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
159 *p = 0x80;
160 p++;
161
162 rlen = bs - ctx->len -1;
163 if (rlen)
164 memset(p, 0, rlen);
165
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
166 offset += bs;
167 }
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
168
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
169 crypto_xor(prev, odds, bs);
170 crypto_xor(prev, consts + offset, bs);
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
171
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
172 crypto_cipher_encrypt_one(tfm, out, prev);
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
173
174 return 0;
175 }
176
177 static int xcbc_init_tfm(struct crypto_tfm *tfm)
178 {
2e306ee @herbertx [CRYPTO] api: Add type-safe spawns
herbertx authored Dec 17, 2006
179 struct crypto_cipher *cipher;
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
180 struct crypto_instance *inst = (void *)tfm->__crt_alg;
181 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
182 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
183
2e306ee @herbertx [CRYPTO] api: Add type-safe spawns
herbertx authored Dec 16, 2006
184 cipher = crypto_spawn_cipher(spawn);
185 if (IS_ERR(cipher))
186 return PTR_ERR(cipher);
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
187
2e306ee @herbertx [CRYPTO] api: Add type-safe spawns
herbertx authored Dec 16, 2006
188 ctx->child = cipher;
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
189
190 return 0;
191 };
192
193 static void xcbc_exit_tfm(struct crypto_tfm *tfm)
194 {
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
195 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
196 crypto_free_cipher(ctx->child);
197 }
198
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
199 static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
200 {
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
201 struct shash_instance *inst;
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
202 struct crypto_alg *alg;
36f87a4 crypto: xcbc - Fix alignment calculation of xcbc_tfm_ctx
Steffen Klassert authored Aug 20, 2009
203 unsigned long alignmask;
ebc610e @herbertx [CRYPTO] templates: Pass type/mask when creating instances
herbertx authored Jan 1, 2007
204 int err;
205
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
206 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
ebc610e @herbertx [CRYPTO] templates: Pass type/mask when creating instances
herbertx authored Jan 1, 2007
207 if (err)
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
208 return err;
ebc610e @herbertx [CRYPTO] templates: Pass type/mask when creating instances
herbertx authored Jan 1, 2007
209
210 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
211 CRYPTO_ALG_TYPE_MASK);
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
212 if (IS_ERR(alg))
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
213 return PTR_ERR(alg);
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
214
215 switch(alg->cra_blocksize) {
216 case 16:
217 break;
218 default:
1b87887 @herbertx [CRYPTO] xcbc: Fix algorithm leak when block size check fails
herbertx authored Jan 1, 2008
219 goto out_put_alg;
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
220 }
221
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
222 inst = shash_alloc_instance("xcbc", alg);
b5ebd44 @herbertx crypto: xcbc - Fix incorrect error value when creating instance
herbertx authored Jul 15, 2009
223 err = PTR_ERR(inst);
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
224 if (IS_ERR(inst))
225 goto out_put_alg;
226
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
227 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
228 shash_crypto_instance(inst),
229 CRYPTO_ALG_TYPE_MASK);
230 if (err)
231 goto out_free_inst;
232
36f87a4 crypto: xcbc - Fix alignment calculation of xcbc_tfm_ctx
Steffen Klassert authored Aug 20, 2009
233 alignmask = alg->cra_alignmask | 3;
234 inst->alg.base.cra_alignmask = alignmask;
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
235 inst->alg.base.cra_priority = alg->cra_priority;
236 inst->alg.base.cra_blocksize = alg->cra_blocksize;
237
238 inst->alg.digestsize = alg->cra_blocksize;
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
239 inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
240 crypto_tfm_ctx_alignment()) +
36f87a4 crypto: xcbc - Fix alignment calculation of xcbc_tfm_ctx
Steffen Klassert authored Aug 20, 2009
241 (alignmask &
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
242 ~(crypto_tfm_ctx_alignment() - 1)) +
243 alg->cra_blocksize * 2;
244
245 inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
36f87a4 crypto: xcbc - Fix alignment calculation of xcbc_tfm_ctx
Steffen Klassert authored Aug 20, 2009
246 alignmask + 1) +
ac95301 @herbertx crypto: xcbc - Fix shash conversion
herbertx authored Jul 22, 2009
247 alg->cra_blocksize * 2;
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
248 inst->alg.base.cra_init = xcbc_init_tfm;
249 inst->alg.base.cra_exit = xcbc_exit_tfm;
250
251 inst->alg.init = crypto_xcbc_digest_init;
252 inst->alg.update = crypto_xcbc_digest_update;
253 inst->alg.final = crypto_xcbc_digest_final;
254 inst->alg.setkey = crypto_xcbc_digest_setkey;
255
256 err = shash_register_instance(tmpl, inst);
257 if (err) {
258 out_free_inst:
259 shash_free_instance(shash_crypto_instance(inst));
260 }
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
261
262 out_put_alg:
263 crypto_mod_put(alg);
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
264 return err;
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
265 }
266
267 static struct crypto_template crypto_xcbc_tmpl = {
268 .name = "xcbc",
3106caa @herbertx crypto: xcbc - Switch to shash
herbertx authored Jul 12, 2009
269 .create = xcbc_create,
270 .free = shash_free_instance,
333b0d7 [CRYPTO] xcbc: New algorithm
Kazunori MIYAZAWA authored Oct 28, 2006
271 .module = THIS_MODULE,
272 };
273
274 static int __init crypto_xcbc_module_init(void)
275 {
276 return crypto_register_template(&crypto_xcbc_tmpl);
277 }
278
279 static void __exit crypto_xcbc_module_exit(void)
280 {
281 crypto_unregister_template(&crypto_xcbc_tmpl);
282 }
283
284 module_init(crypto_xcbc_module_init);
285 module_exit(crypto_xcbc_module_exit);
286
287 MODULE_LICENSE("GPL");
288 MODULE_DESCRIPTION("XCBC keyed hash algorithm");
Something went wrong with that request. Please try again.