blob: 4f6ab23e14adfb765332394495412a25957ac28f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Herbert Xu06ace7a2005-10-30 21:25:15 +110013#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/string.h>
17#include <linux/crypto.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110018#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20
21struct michael_mic_ctx {
22 u8 pending[4];
23 size_t pending_len;
24
25 u32 l, r;
26};
27
28
29static inline u32 xswap(u32 val)
30{
31 return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
32}
33
34
35#define michael_block(l, r) \
36do { \
37 r ^= rol32(l, 17); \
38 l += r; \
39 r ^= xswap(l); \
40 l += r; \
41 r ^= rol32(l, 3); \
42 l += r; \
43 r ^= ror32(l, 2); \
44 l += r; \
45} while (0)
46
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static void michael_init(void *ctx)
49{
50 struct michael_mic_ctx *mctx = ctx;
51 mctx->pending_len = 0;
52}
53
54
55static void michael_update(void *ctx, const u8 *data, unsigned int len)
56{
57 struct michael_mic_ctx *mctx = ctx;
Herbert Xu06ace7a2005-10-30 21:25:15 +110058 const __le32 *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 if (mctx->pending_len) {
61 int flen = 4 - mctx->pending_len;
62 if (flen > len)
63 flen = len;
64 memcpy(&mctx->pending[mctx->pending_len], data, flen);
65 mctx->pending_len += flen;
66 data += flen;
67 len -= flen;
68
69 if (mctx->pending_len < 4)
70 return;
71
Herbert Xu06ace7a2005-10-30 21:25:15 +110072 src = (const __le32 *)mctx->pending;
73 mctx->l ^= le32_to_cpup(src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 michael_block(mctx->l, mctx->r);
75 mctx->pending_len = 0;
76 }
77
Herbert Xu06ace7a2005-10-30 21:25:15 +110078 src = (const __le32 *)data;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 while (len >= 4) {
Herbert Xu06ace7a2005-10-30 21:25:15 +110081 mctx->l ^= le32_to_cpup(src++);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 michael_block(mctx->l, mctx->r);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 len -= 4;
84 }
85
86 if (len > 0) {
87 mctx->pending_len = len;
Herbert Xu06ace7a2005-10-30 21:25:15 +110088 memcpy(mctx->pending, src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90}
91
92
93static void michael_final(void *ctx, u8 *out)
94{
95 struct michael_mic_ctx *mctx = ctx;
96 u8 *data = mctx->pending;
Herbert Xu06ace7a2005-10-30 21:25:15 +110097 __le32 *dst = (__le32 *)out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /* Last block and padding (0x5a, 4..7 x 0) */
100 switch (mctx->pending_len) {
101 case 0:
102 mctx->l ^= 0x5a;
103 break;
104 case 1:
105 mctx->l ^= data[0] | 0x5a00;
106 break;
107 case 2:
108 mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
109 break;
110 case 3:
111 mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
112 0x5a000000;
113 break;
114 }
115 michael_block(mctx->l, mctx->r);
116 /* l ^= 0; */
117 michael_block(mctx->l, mctx->r);
118
Herbert Xu06ace7a2005-10-30 21:25:15 +1100119 dst[0] = cpu_to_le32(mctx->l);
120 dst[1] = cpu_to_le32(mctx->r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123
124static int michael_setkey(void *ctx, const u8 *key, unsigned int keylen,
125 u32 *flags)
126{
127 struct michael_mic_ctx *mctx = ctx;
Herbert Xu06ace7a2005-10-30 21:25:15 +1100128 const __le32 *data = (const __le32 *)key;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (keylen != 8) {
131 if (flags)
132 *flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
133 return -EINVAL;
134 }
Herbert Xu06ace7a2005-10-30 21:25:15 +1100135
136 mctx->l = le32_to_cpu(data[0]);
137 mctx->r = le32_to_cpu(data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139}
140
141
142static struct crypto_alg michael_mic_alg = {
143 .cra_name = "michael_mic",
144 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
145 .cra_blocksize = 8,
146 .cra_ctxsize = sizeof(struct michael_mic_ctx),
147 .cra_module = THIS_MODULE,
148 .cra_list = LIST_HEAD_INIT(michael_mic_alg.cra_list),
149 .cra_u = { .digest = {
150 .dia_digestsize = 8,
151 .dia_init = michael_init,
152 .dia_update = michael_update,
153 .dia_final = michael_final,
154 .dia_setkey = michael_setkey } }
155};
156
157
158static int __init michael_mic_init(void)
159{
160 return crypto_register_alg(&michael_mic_alg);
161}
162
163
164static void __exit michael_mic_exit(void)
165{
166 crypto_unregister_alg(&michael_mic_alg);
167}
168
169
170module_init(michael_mic_init);
171module_exit(michael_mic_exit);
172
173MODULE_LICENSE("GPL v2");
174MODULE_DESCRIPTION("Michael MIC");
175MODULE_AUTHOR("Jouni Malinen <jkmaline@cc.hut.fi>");