4 * Michael MIC (IEEE 802.11i/TKIP) keyed digest
6 * Copyright (c) 2004 Jouni Malinen <j@w1.fi>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <crypto/internal/hash.h>
13 #include <asm/byteorder.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
20 struct michael_mic_ctx
{
24 struct michael_mic_desc_ctx
{
31 static inline u32
xswap(u32 val
)
33 return ((val
& 0x00ff00ff) << 8) | ((val
& 0xff00ff00) >> 8);
37 #define michael_block(l, r) \
50 static int michael_init(struct shash_desc
*desc
)
52 struct michael_mic_desc_ctx
*mctx
= shash_desc_ctx(desc
);
53 struct michael_mic_ctx
*ctx
= crypto_shash_ctx(desc
->tfm
);
54 mctx
->pending_len
= 0;
62 static int michael_update(struct shash_desc
*desc
, const u8
*data
,
65 struct michael_mic_desc_ctx
*mctx
= shash_desc_ctx(desc
);
68 if (mctx
->pending_len
) {
69 int flen
= 4 - mctx
->pending_len
;
72 memcpy(&mctx
->pending
[mctx
->pending_len
], data
, flen
);
73 mctx
->pending_len
+= flen
;
77 if (mctx
->pending_len
< 4)
80 src
= (const __le32
*)mctx
->pending
;
81 mctx
->l
^= le32_to_cpup(src
);
82 michael_block(mctx
->l
, mctx
->r
);
83 mctx
->pending_len
= 0;
86 src
= (const __le32
*)data
;
89 mctx
->l
^= le32_to_cpup(src
++);
90 michael_block(mctx
->l
, mctx
->r
);
95 mctx
->pending_len
= len
;
96 memcpy(mctx
->pending
, src
, len
);
103 static int michael_final(struct shash_desc
*desc
, u8
*out
)
105 struct michael_mic_desc_ctx
*mctx
= shash_desc_ctx(desc
);
106 u8
*data
= mctx
->pending
;
107 __le32
*dst
= (__le32
*)out
;
109 /* Last block and padding (0x5a, 4..7 x 0) */
110 switch (mctx
->pending_len
) {
115 mctx
->l
^= data
[0] | 0x5a00;
118 mctx
->l
^= data
[0] | (data
[1] << 8) | 0x5a0000;
121 mctx
->l
^= data
[0] | (data
[1] << 8) | (data
[2] << 16) |
125 michael_block(mctx
->l
, mctx
->r
);
127 michael_block(mctx
->l
, mctx
->r
);
129 dst
[0] = cpu_to_le32(mctx
->l
);
130 dst
[1] = cpu_to_le32(mctx
->r
);
136 static int michael_setkey(struct crypto_shash
*tfm
, const u8
*key
,
139 struct michael_mic_ctx
*mctx
= crypto_shash_ctx(tfm
);
141 const __le32
*data
= (const __le32
*)key
;
144 crypto_shash_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
148 mctx
->l
= le32_to_cpu(data
[0]);
149 mctx
->r
= le32_to_cpu(data
[1]);
153 static struct shash_alg alg
= {
155 .setkey
= michael_setkey
,
156 .init
= michael_init
,
157 .update
= michael_update
,
158 .final
= michael_final
,
159 .descsize
= sizeof(struct michael_mic_desc_ctx
),
161 .cra_name
= "michael_mic",
164 .cra_ctxsize
= sizeof(struct michael_mic_ctx
),
165 .cra_module
= THIS_MODULE
,
169 static int __init
michael_mic_init(void)
171 return crypto_register_shash(&alg
);
175 static void __exit
michael_mic_exit(void)
177 crypto_unregister_shash(&alg
);
181 subsys_initcall(michael_mic_init
);
182 module_exit(michael_mic_exit
);
184 MODULE_LICENSE("GPL v2");
185 MODULE_DESCRIPTION("Michael MIC");
186 MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
187 MODULE_ALIAS_CRYPTO("michael_mic");