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.
13 #include <asm/byteorder.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/string.h>
17 #include <linux/crypto.h>
18 #include <linux/types.h>
21 struct michael_mic_ctx
{
29 static inline u32
xswap(u32 val
)
31 return ((val
& 0x00ff00ff) << 8) | ((val
& 0xff00ff00) >> 8);
35 #define michael_block(l, r) \
48 static void michael_init(struct crypto_tfm
*tfm
)
50 struct michael_mic_ctx
*mctx
= crypto_tfm_ctx(tfm
);
51 mctx
->pending_len
= 0;
55 static void michael_update(struct crypto_tfm
*tfm
, const u8
*data
,
58 struct michael_mic_ctx
*mctx
= crypto_tfm_ctx(tfm
);
61 if (mctx
->pending_len
) {
62 int flen
= 4 - mctx
->pending_len
;
65 memcpy(&mctx
->pending
[mctx
->pending_len
], data
, flen
);
66 mctx
->pending_len
+= flen
;
70 if (mctx
->pending_len
< 4)
73 src
= (const __le32
*)mctx
->pending
;
74 mctx
->l
^= le32_to_cpup(src
);
75 michael_block(mctx
->l
, mctx
->r
);
76 mctx
->pending_len
= 0;
79 src
= (const __le32
*)data
;
82 mctx
->l
^= le32_to_cpup(src
++);
83 michael_block(mctx
->l
, mctx
->r
);
88 mctx
->pending_len
= len
;
89 memcpy(mctx
->pending
, src
, len
);
94 static void michael_final(struct crypto_tfm
*tfm
, u8
*out
)
96 struct michael_mic_ctx
*mctx
= crypto_tfm_ctx(tfm
);
97 u8
*data
= mctx
->pending
;
98 __le32
*dst
= (__le32
*)out
;
100 /* Last block and padding (0x5a, 4..7 x 0) */
101 switch (mctx
->pending_len
) {
106 mctx
->l
^= data
[0] | 0x5a00;
109 mctx
->l
^= data
[0] | (data
[1] << 8) | 0x5a0000;
112 mctx
->l
^= data
[0] | (data
[1] << 8) | (data
[2] << 16) |
116 michael_block(mctx
->l
, mctx
->r
);
118 michael_block(mctx
->l
, mctx
->r
);
120 dst
[0] = cpu_to_le32(mctx
->l
);
121 dst
[1] = cpu_to_le32(mctx
->r
);
125 static int michael_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
128 struct michael_mic_ctx
*mctx
= crypto_tfm_ctx(tfm
);
129 const __le32
*data
= (const __le32
*)key
;
132 tfm
->crt_flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
136 mctx
->l
= le32_to_cpu(data
[0]);
137 mctx
->r
= le32_to_cpu(data
[1]);
142 static struct crypto_alg michael_mic_alg
= {
143 .cra_name
= "michael_mic",
144 .cra_flags
= CRYPTO_ALG_TYPE_DIGEST
,
146 .cra_ctxsize
= sizeof(struct michael_mic_ctx
),
147 .cra_module
= THIS_MODULE
,
149 .cra_list
= LIST_HEAD_INIT(michael_mic_alg
.cra_list
),
150 .cra_u
= { .digest
= {
152 .dia_init
= michael_init
,
153 .dia_update
= michael_update
,
154 .dia_final
= michael_final
,
155 .dia_setkey
= michael_setkey
} }
159 static int __init
michael_mic_init(void)
161 return crypto_register_alg(&michael_mic_alg
);
165 static void __exit
michael_mic_exit(void)
167 crypto_unregister_alg(&michael_mic_alg
);
171 module_init(michael_mic_init
);
172 module_exit(michael_mic_exit
);
174 MODULE_LICENSE("GPL v2");
175 MODULE_DESCRIPTION("Michael MIC");
176 MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");