1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/string.h>
16#include <linux/crypto.h>
17
18
19struct michael_mic_ctx {
20 u8 pending[4];
21 size_t pending_len;
22
23 u32 l, r;
24};
25
26
27static inline u32 rotl(u32 val, int bits)
28{
29 return (val << bits) | (val >> (32 - bits));
30}
31
32
33static inline u32 rotr(u32 val, int bits)
34{
35 return (val >> bits) | (val << (32 - bits));
36}
37
38
39static inline u32 xswap(u32 val)
40{
41 return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
42}
43
44
45#define michael_block(l, r) \
46do { \
47 r ^= rotl(l, 17); \
48 l += r; \
49 r ^= xswap(l); \
50 l += r; \
51 r ^= rotl(l, 3); \
52 l += r; \
53 r ^= rotr(l, 2); \
54 l += r; \
55} while (0)
56
57
58static inline u32 get_le32(const u8 *p)
59{
60 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
61}
62
63
64static inline void put_le32(u8 *p, u32 v)
65{
66 p[0] = v;
67 p[1] = v >> 8;
68 p[2] = v >> 16;
69 p[3] = v >> 24;
70}
71
72
73static void michael_init(void *ctx)
74{
75 struct michael_mic_ctx *mctx = ctx;
76 mctx->pending_len = 0;
77}
78
79
80static void michael_update(void *ctx, const u8 *data, unsigned int len)
81{
82 struct michael_mic_ctx *mctx = ctx;
83
84 if (mctx->pending_len) {
85 int flen = 4 - mctx->pending_len;
86 if (flen > len)
87 flen = len;
88 memcpy(&mctx->pending[mctx->pending_len], data, flen);
89 mctx->pending_len += flen;
90 data += flen;
91 len -= flen;
92
93 if (mctx->pending_len < 4)
94 return;
95
96 mctx->l ^= get_le32(mctx->pending);
97 michael_block(mctx->l, mctx->r);
98 mctx->pending_len = 0;
99 }
100
101 while (len >= 4) {
102 mctx->l ^= get_le32(data);
103 michael_block(mctx->l, mctx->r);
104 data += 4;
105 len -= 4;
106 }
107
108 if (len > 0) {
109 mctx->pending_len = len;
110 memcpy(mctx->pending, data, len);
111 }
112}
113
114
115static void michael_final(void *ctx, u8 *out)
116{
117 struct michael_mic_ctx *mctx = ctx;
118 u8 *data = mctx->pending;
119
120
121 switch (mctx->pending_len) {
122 case 0:
123 mctx->l ^= 0x5a;
124 break;
125 case 1:
126 mctx->l ^= data[0] | 0x5a00;
127 break;
128 case 2:
129 mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
130 break;
131 case 3:
132 mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
133 0x5a000000;
134 break;
135 }
136 michael_block(mctx->l, mctx->r);
137
138 michael_block(mctx->l, mctx->r);
139
140 put_le32(out, mctx->l);
141 put_le32(out + 4, mctx->r);
142}
143
144
145static int michael_setkey(void *ctx, const u8 *key, unsigned int keylen,
146 u32 *flags)
147{
148 struct michael_mic_ctx *mctx = ctx;
149 if (keylen != 8) {
150 if (flags)
151 *flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
152 return -EINVAL;
153 }
154 mctx->l = get_le32(key);
155 mctx->r = get_le32(key + 4);
156 return 0;
157}
158
159
160static struct crypto_alg michael_mic_alg = {
161 .cra_name = "michael_mic",
162 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
163 .cra_blocksize = 8,
164 .cra_ctxsize = sizeof(struct michael_mic_ctx),
165 .cra_module = THIS_MODULE,
166 .cra_list = LIST_HEAD_INIT(michael_mic_alg.cra_list),
167 .cra_u = { .digest = {
168 .dia_digestsize = 8,
169 .dia_init = michael_init,
170 .dia_update = michael_update,
171 .dia_final = michael_final,
172 .dia_setkey = michael_setkey } }
173};
174
175
176static int __init michael_mic_init(void)
177{
178 return crypto_register_alg(&michael_mic_alg);
179}
180
181
182static void __exit michael_mic_exit(void)
183{
184 crypto_unregister_alg(&michael_mic_alg);
185}
186
187
188module_init(michael_mic_init);
189module_exit(michael_mic_exit);
190
191MODULE_LICENSE("GPL v2");
192MODULE_DESCRIPTION("Michael MIC");
193MODULE_AUTHOR("Jouni Malinen <jkmaline@cc.hut.fi>");
194