2 * Copyright (C)2006 USAGI/WIDE Project
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.
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.
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
19 * Kazunori Miyazawa <miyazawa@linux-ipv6.org>
22 #include <crypto/internal/hash.h>
23 #include <linux/err.h>
24 #include <linux/kernel.h>
26 static u_int32_t ks
[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
27 0x02020202, 0x02020202, 0x02020202, 0x02020202,
28 0x03030303, 0x03030303, 0x03030303, 0x03030303};
31 * +------------------------
33 * +------------------------
35 * +------------------------
36 * | consts (block size * 2)
37 * +------------------------
40 struct crypto_cipher
*child
;
45 * +------------------------
47 * +------------------------
49 * +------------------------
51 * +------------------------
53 * +------------------------
55 struct xcbc_desc_ctx
{
60 static int crypto_xcbc_digest_setkey(struct crypto_shash
*parent
,
61 const u8
*inkey
, unsigned int keylen
)
63 unsigned long alignmask
= crypto_shash_alignmask(parent
);
64 struct xcbc_tfm_ctx
*ctx
= crypto_shash_ctx(parent
);
65 int bs
= crypto_shash_blocksize(parent
);
66 u8
*consts
= PTR_ALIGN(&ctx
->ctx
[0], alignmask
+ 1);
70 if ((err
= crypto_cipher_setkey(ctx
->child
, inkey
, keylen
)))
73 crypto_cipher_encrypt_one(ctx
->child
, consts
, (u8
*)ks
+ bs
);
74 crypto_cipher_encrypt_one(ctx
->child
, consts
+ bs
, (u8
*)ks
+ bs
* 2);
75 crypto_cipher_encrypt_one(ctx
->child
, key1
, (u8
*)ks
);
77 return crypto_cipher_setkey(ctx
->child
, key1
, bs
);
81 static int crypto_xcbc_digest_init(struct shash_desc
*pdesc
)
83 unsigned long alignmask
= crypto_shash_alignmask(pdesc
->tfm
);
84 struct xcbc_desc_ctx
*ctx
= shash_desc_ctx(pdesc
);
85 int bs
= crypto_shash_blocksize(pdesc
->tfm
);
86 u8
*prev
= PTR_ALIGN(&ctx
->ctx
[0], alignmask
+ 1) + bs
;
94 static int crypto_xcbc_digest_update(struct shash_desc
*pdesc
, const u8
*p
,
97 struct crypto_shash
*parent
= pdesc
->tfm
;
98 unsigned long alignmask
= crypto_shash_alignmask(parent
);
99 struct xcbc_tfm_ctx
*tctx
= crypto_shash_ctx(parent
);
100 struct xcbc_desc_ctx
*ctx
= shash_desc_ctx(pdesc
);
101 struct crypto_cipher
*tfm
= tctx
->child
;
102 int bs
= crypto_shash_blocksize(parent
);
103 u8
*odds
= PTR_ALIGN(&ctx
->ctx
[0], alignmask
+ 1);
104 u8
*prev
= odds
+ bs
;
106 /* checking the data can fill the block */
107 if ((ctx
->len
+ len
) <= bs
) {
108 memcpy(odds
+ ctx
->len
, p
, len
);
113 /* filling odds with new data and encrypting it */
114 memcpy(odds
+ ctx
->len
, p
, bs
- ctx
->len
);
115 len
-= bs
- ctx
->len
;
118 crypto_xor(prev
, odds
, bs
);
119 crypto_cipher_encrypt_one(tfm
, prev
, prev
);
121 /* clearing the length */
124 /* encrypting the rest of data */
126 crypto_xor(prev
, p
, bs
);
127 crypto_cipher_encrypt_one(tfm
, prev
, prev
);
132 /* keeping the surplus of blocksize */
134 memcpy(odds
, p
, len
);
141 static int crypto_xcbc_digest_final(struct shash_desc
*pdesc
, u8
*out
)
143 struct crypto_shash
*parent
= pdesc
->tfm
;
144 unsigned long alignmask
= crypto_shash_alignmask(parent
);
145 struct xcbc_tfm_ctx
*tctx
= crypto_shash_ctx(parent
);
146 struct xcbc_desc_ctx
*ctx
= shash_desc_ctx(pdesc
);
147 struct crypto_cipher
*tfm
= tctx
->child
;
148 int bs
= crypto_shash_blocksize(parent
);
149 u8
*consts
= PTR_ALIGN(&tctx
->ctx
[0], alignmask
+ 1);
150 u8
*odds
= PTR_ALIGN(&ctx
->ctx
[0], alignmask
+ 1);
151 u8
*prev
= odds
+ bs
;
152 unsigned int offset
= 0;
154 if (ctx
->len
!= bs
) {
156 u8
*p
= odds
+ ctx
->len
;
161 rlen
= bs
- ctx
->len
-1;
168 crypto_xor(prev
, odds
, bs
);
169 crypto_xor(prev
, consts
+ offset
, bs
);
171 crypto_cipher_encrypt_one(tfm
, out
, prev
);
176 static int xcbc_init_tfm(struct crypto_tfm
*tfm
)
178 struct crypto_cipher
*cipher
;
179 struct crypto_instance
*inst
= (void *)tfm
->__crt_alg
;
180 struct crypto_spawn
*spawn
= crypto_instance_ctx(inst
);
181 struct xcbc_tfm_ctx
*ctx
= crypto_tfm_ctx(tfm
);
183 cipher
= crypto_spawn_cipher(spawn
);
185 return PTR_ERR(cipher
);
192 static void xcbc_exit_tfm(struct crypto_tfm
*tfm
)
194 struct xcbc_tfm_ctx
*ctx
= crypto_tfm_ctx(tfm
);
195 crypto_free_cipher(ctx
->child
);
198 static int xcbc_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
200 struct shash_instance
*inst
;
201 struct crypto_alg
*alg
;
202 unsigned long alignmask
;
205 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_SHASH
);
209 alg
= crypto_get_attr_alg(tb
, CRYPTO_ALG_TYPE_CIPHER
,
210 CRYPTO_ALG_TYPE_MASK
);
214 switch(alg
->cra_blocksize
) {
221 inst
= shash_alloc_instance("xcbc", alg
);
226 err
= crypto_init_spawn(shash_instance_ctx(inst
), alg
,
227 shash_crypto_instance(inst
),
228 CRYPTO_ALG_TYPE_MASK
);
232 alignmask
= alg
->cra_alignmask
| 3;
233 inst
->alg
.base
.cra_alignmask
= alignmask
;
234 inst
->alg
.base
.cra_priority
= alg
->cra_priority
;
235 inst
->alg
.base
.cra_blocksize
= alg
->cra_blocksize
;
237 inst
->alg
.digestsize
= alg
->cra_blocksize
;
238 inst
->alg
.descsize
= ALIGN(sizeof(struct xcbc_desc_ctx
),
239 crypto_tfm_ctx_alignment()) +
241 ~(crypto_tfm_ctx_alignment() - 1)) +
242 alg
->cra_blocksize
* 2;
244 inst
->alg
.base
.cra_ctxsize
= ALIGN(sizeof(struct xcbc_tfm_ctx
),
246 alg
->cra_blocksize
* 2;
247 inst
->alg
.base
.cra_init
= xcbc_init_tfm
;
248 inst
->alg
.base
.cra_exit
= xcbc_exit_tfm
;
250 inst
->alg
.init
= crypto_xcbc_digest_init
;
251 inst
->alg
.update
= crypto_xcbc_digest_update
;
252 inst
->alg
.final
= crypto_xcbc_digest_final
;
253 inst
->alg
.setkey
= crypto_xcbc_digest_setkey
;
255 err
= shash_register_instance(tmpl
, inst
);
258 shash_free_instance(shash_crypto_instance(inst
));
266 static struct crypto_template crypto_xcbc_tmpl
= {
268 .create
= xcbc_create
,
269 .free
= shash_free_instance
,
270 .module
= THIS_MODULE
,
273 static int __init
crypto_xcbc_module_init(void)
275 return crypto_register_template(&crypto_xcbc_tmpl
);
278 static void __exit
crypto_xcbc_module_exit(void)
280 crypto_unregister_template(&crypto_xcbc_tmpl
);
283 module_init(crypto_xcbc_module_init
);
284 module_exit(crypto_xcbc_module_exit
);
286 MODULE_LICENSE("GPL");
287 MODULE_DESCRIPTION("XCBC keyed hash algorithm");