aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/michael_mic.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /crypto/michael_mic.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'crypto/michael_mic.c')
-rw-r--r--crypto/michael_mic.c181
1 files changed, 181 insertions, 0 deletions
diff --git a/crypto/michael_mic.c b/crypto/michael_mic.c
new file mode 100644
index 000000000000..a470bcb3693e
--- /dev/null
+++ b/crypto/michael_mic.c
@@ -0,0 +1,181 @@
1/*
2 * Cryptographic API
3 *
4 * Michael MIC (IEEE 802.11i/TKIP) keyed digest
5 *
6 * Copyright (c) 2004 Jouni Malinen <jkmaline@cc.hut.fi>
7 *
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.
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 xswap(u32 val)
28{
29 return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
30}
31
32
33#define michael_block(l, r) \
34do { \
35 r ^= rol32(l, 17); \
36 l += r; \
37 r ^= xswap(l); \
38 l += r; \
39 r ^= rol32(l, 3); \
40 l += r; \
41 r ^= ror32(l, 2); \
42 l += r; \
43} while (0)
44
45
46static inline u32 get_le32(const u8 *p)
47{
48 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
49}
50
51
52static inline void put_le32(u8 *p, u32 v)
53{
54 p[0] = v;
55 p[1] = v >> 8;
56 p[2] = v >> 16;
57 p[3] = v >> 24;
58}
59
60
61static void michael_init(void *ctx)
62{
63 struct michael_mic_ctx *mctx = ctx;
64 mctx->pending_len = 0;
65}
66
67
68static void michael_update(void *ctx, const u8 *data, unsigned int len)
69{
70 struct michael_mic_ctx *mctx = ctx;
71
72 if (mctx->pending_len) {
73 int flen = 4 - mctx->pending_len;
74 if (flen > len)
75 flen = len;
76 memcpy(&mctx->pending[mctx->pending_len], data, flen);
77 mctx->pending_len += flen;
78 data += flen;
79 len -= flen;
80
81 if (mctx->pending_len < 4)
82 return;
83
84 mctx->l ^= get_le32(mctx->pending);
85 michael_block(mctx->l, mctx->r);
86 mctx->pending_len = 0;
87 }
88
89 while (len >= 4) {
90 mctx->l ^= get_le32(data);
91 michael_block(mctx->l, mctx->r);
92 data += 4;
93 len -= 4;
94 }
95
96 if (len > 0) {
97 mctx->pending_len = len;
98 memcpy(mctx->pending, data, len);
99 }
100}
101
102
103static void michael_final(void *ctx, u8 *out)
104{
105 struct michael_mic_ctx *mctx = ctx;
106 u8 *data = mctx->pending;
107
108 /* Last block and padding (0x5a, 4..7 x 0) */
109 switch (mctx->pending_len) {
110 case 0:
111 mctx->l ^= 0x5a;
112 break;
113 case 1:
114 mctx->l ^= data[0] | 0x5a00;
115 break;
116 case 2:
117 mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
118 break;
119 case 3:
120 mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
121 0x5a000000;
122 break;
123 }
124 michael_block(mctx->l, mctx->r);
125 /* l ^= 0; */
126 michael_block(mctx->l, mctx->r);
127
128 put_le32(out, mctx->l);
129 put_le32(out + 4, mctx->r);
130}
131
132
133static int michael_setkey(void *ctx, const u8 *key, unsigned int keylen,
134 u32 *flags)
135{
136 struct michael_mic_ctx *mctx = ctx;
137 if (keylen != 8) {
138 if (flags)
139 *flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
140 return -EINVAL;
141 }
142 mctx->l = get_le32(key);
143 mctx->r = get_le32(key + 4);
144 return 0;
145}
146
147
148static struct crypto_alg michael_mic_alg = {
149 .cra_name = "michael_mic",
150 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
151 .cra_blocksize = 8,
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 = {
156 .dia_digestsize = 8,
157 .dia_init = michael_init,
158 .dia_update = michael_update,
159 .dia_final = michael_final,
160 .dia_setkey = michael_setkey } }
161};
162
163
164static int __init michael_mic_init(void)
165{
166 return crypto_register_alg(&michael_mic_alg);
167}
168
169
170static void __exit michael_mic_exit(void)
171{
172 crypto_unregister_alg(&michael_mic_alg);
173}
174
175
176module_init(michael_mic_init);
177module_exit(michael_mic_exit);
178
179MODULE_LICENSE("GPL v2");
180MODULE_DESCRIPTION("Michael MIC");
181MODULE_AUTHOR("Jouni Malinen <jkmaline@cc.hut.fi>");