4 * Michael MIC (IEEE 802.11i/TKIP) keyed digest
6 * Copyright (c) 2004 Jouni Malinen <jkmaline@cc.hut.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.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 #include <linux/crypto.h>
19 struct michael_mic_ctx
{
27 static inline u32
xswap(u32 val
)
29 return ((val
& 0x00ff00ff) << 8) | ((val
& 0xff00ff00) >> 8);
33 #define michael_block(l, r) \
46 static inline u32
get_le32(const u8
*p
)
48 return p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
52 static inline void put_le32(u8
*p
, u32 v
)
61 static void michael_init(void *ctx
)
63 struct michael_mic_ctx
*mctx
= ctx
;
64 mctx
->pending_len
= 0;
68 static void michael_update(void *ctx
, const u8
*data
, unsigned int len
)
70 struct michael_mic_ctx
*mctx
= ctx
;
72 if (mctx
->pending_len
) {
73 int flen
= 4 - mctx
->pending_len
;
76 memcpy(&mctx
->pending
[mctx
->pending_len
], data
, flen
);
77 mctx
->pending_len
+= flen
;
81 if (mctx
->pending_len
< 4)
84 mctx
->l
^= get_le32(mctx
->pending
);
85 michael_block(mctx
->l
, mctx
->r
);
86 mctx
->pending_len
= 0;
90 mctx
->l
^= get_le32(data
);
91 michael_block(mctx
->l
, mctx
->r
);
97 mctx
->pending_len
= len
;
98 memcpy(mctx
->pending
, data
, len
);
103 static void michael_final(void *ctx
, u8
*out
)
105 struct michael_mic_ctx
*mctx
= ctx
;
106 u8
*data
= mctx
->pending
;
108 /* Last block and padding (0x5a, 4..7 x 0) */
109 switch (mctx
->pending_len
) {
114 mctx
->l
^= data
[0] | 0x5a00;
117 mctx
->l
^= data
[0] | (data
[1] << 8) | 0x5a0000;
120 mctx
->l
^= data
[0] | (data
[1] << 8) | (data
[2] << 16) |
124 michael_block(mctx
->l
, mctx
->r
);
126 michael_block(mctx
->l
, mctx
->r
);
128 put_le32(out
, mctx
->l
);
129 put_le32(out
+ 4, mctx
->r
);
133 static int michael_setkey(void *ctx
, const u8
*key
, unsigned int keylen
,
136 struct michael_mic_ctx
*mctx
= ctx
;
139 *flags
= CRYPTO_TFM_RES_BAD_KEY_LEN
;
142 mctx
->l
= get_le32(key
);
143 mctx
->r
= get_le32(key
+ 4);
148 static struct crypto_alg michael_mic_alg
= {
149 .cra_name
= "michael_mic",
150 .cra_flags
= CRYPTO_ALG_TYPE_DIGEST
,
152 .cra_ctxsize
= sizeof(struct michael_mic_ctx
),
153 .cra_module
= THIS_MODULE
,
154 .cra_list
= LIST_HEAD_INIT(michael_mic_alg
.cra_list
),
155 .cra_u
= { .digest
= {
157 .dia_init
= michael_init
,
158 .dia_update
= michael_update
,
159 .dia_final
= michael_final
,
160 .dia_setkey
= michael_setkey
} }
164 static int __init
michael_mic_init(void)
166 return crypto_register_alg(&michael_mic_alg
);
170 static void __exit
michael_mic_exit(void)
172 crypto_unregister_alg(&michael_mic_alg
);
176 module_init(michael_mic_init
);
177 module_exit(michael_mic_exit
);
179 MODULE_LICENSE("GPL v2");
180 MODULE_DESCRIPTION("Michael MIC");
181 MODULE_AUTHOR("Jouni Malinen <jkmaline@cc.hut.fi>");