Merge branch 'writeback-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / crypto / michael_mic.c
CommitLineData
1da177e4
LT
1/*
2 * Cryptographic API
3 *
4 * Michael MIC (IEEE 802.11i/TKIP) keyed digest
5 *
85d32e7b 6 * Copyright (c) 2004 Jouni Malinen <j@w1.fi>
1da177e4
LT
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 */
19e2bf14 12#include <crypto/internal/hash.h>
06ace7a9 13#include <asm/byteorder.h>
1da177e4
LT
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/string.h>
06ace7a9 17#include <linux/types.h>
1da177e4
LT
18
19
20struct michael_mic_ctx {
19e2bf14
AKR
21 u32 l, r;
22};
23
24struct michael_mic_desc_ctx {
1da177e4
LT
25 u8 pending[4];
26 size_t pending_len;
27
28 u32 l, r;
29};
30
1da177e4
LT
31static inline u32 xswap(u32 val)
32{
33 return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
34}
35
36
37#define michael_block(l, r) \
38do { \
39 r ^= rol32(l, 17); \
40 l += r; \
41 r ^= xswap(l); \
42 l += r; \
43 r ^= rol32(l, 3); \
44 l += r; \
45 r ^= ror32(l, 2); \
46 l += r; \
47} while (0)
48
49
19e2bf14 50static int michael_init(struct shash_desc *desc)
1da177e4 51{
19e2bf14
AKR
52 struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
53 struct michael_mic_ctx *ctx = crypto_shash_ctx(desc->tfm);
1da177e4 54 mctx->pending_len = 0;
19e2bf14
AKR
55 mctx->l = ctx->l;
56 mctx->r = ctx->r;
57
58 return 0;
1da177e4
LT
59}
60
61
19e2bf14 62static int michael_update(struct shash_desc *desc, const u8 *data,
6c2bb98b 63 unsigned int len)
1da177e4 64{
19e2bf14 65 struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
06ace7a9 66 const __le32 *src;
1da177e4
LT
67
68 if (mctx->pending_len) {
69 int flen = 4 - mctx->pending_len;
70 if (flen > len)
71 flen = len;
72 memcpy(&mctx->pending[mctx->pending_len], data, flen);
73 mctx->pending_len += flen;
74 data += flen;
75 len -= flen;
76
77 if (mctx->pending_len < 4)
19e2bf14 78 return 0;
1da177e4 79
06ace7a9
HX
80 src = (const __le32 *)mctx->pending;
81 mctx->l ^= le32_to_cpup(src);
1da177e4
LT
82 michael_block(mctx->l, mctx->r);
83 mctx->pending_len = 0;
84 }
85
06ace7a9
HX
86 src = (const __le32 *)data;
87
1da177e4 88 while (len >= 4) {
06ace7a9 89 mctx->l ^= le32_to_cpup(src++);
1da177e4 90 michael_block(mctx->l, mctx->r);
1da177e4
LT
91 len -= 4;
92 }
93
94 if (len > 0) {
95 mctx->pending_len = len;
06ace7a9 96 memcpy(mctx->pending, src, len);
1da177e4 97 }
19e2bf14
AKR
98
99 return 0;
1da177e4
LT
100}
101
102
19e2bf14 103static int michael_final(struct shash_desc *desc, u8 *out)
1da177e4 104{
19e2bf14 105 struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
1da177e4 106 u8 *data = mctx->pending;
06ace7a9 107 __le32 *dst = (__le32 *)out;
1da177e4
LT
108
109 /* Last block and padding (0x5a, 4..7 x 0) */
110 switch (mctx->pending_len) {
111 case 0:
112 mctx->l ^= 0x5a;
113 break;
114 case 1:
115 mctx->l ^= data[0] | 0x5a00;
116 break;
117 case 2:
118 mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
119 break;
120 case 3:
121 mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
122 0x5a000000;
123 break;
124 }
125 michael_block(mctx->l, mctx->r);
126 /* l ^= 0; */
127 michael_block(mctx->l, mctx->r);
128
06ace7a9
HX
129 dst[0] = cpu_to_le32(mctx->l);
130 dst[1] = cpu_to_le32(mctx->r);
19e2bf14
AKR
131
132 return 0;
1da177e4
LT
133}
134
135
19e2bf14 136static int michael_setkey(struct crypto_shash *tfm, const u8 *key,
560c06ae 137 unsigned int keylen)
1da177e4 138{
19e2bf14
AKR
139 struct michael_mic_ctx *mctx = crypto_shash_ctx(tfm);
140
06ace7a9
HX
141 const __le32 *data = (const __le32 *)key;
142
1da177e4 143 if (keylen != 8) {
19e2bf14 144 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1da177e4
LT
145 return -EINVAL;
146 }
06ace7a9
HX
147
148 mctx->l = le32_to_cpu(data[0]);
149 mctx->r = le32_to_cpu(data[1]);
1da177e4
LT
150 return 0;
151}
152
19e2bf14
AKR
153static struct shash_alg alg = {
154 .digestsize = 8,
155 .setkey = michael_setkey,
156 .init = michael_init,
157 .update = michael_update,
158 .final = michael_final,
159 .descsize = sizeof(struct michael_mic_desc_ctx),
160 .base = {
161 .cra_name = "michael_mic",
162 .cra_blocksize = 8,
163 .cra_alignmask = 3,
164 .cra_ctxsize = sizeof(struct michael_mic_ctx),
165 .cra_module = THIS_MODULE,
166 }
1da177e4
LT
167};
168
1da177e4
LT
169static int __init michael_mic_init(void)
170{
19e2bf14 171 return crypto_register_shash(&alg);
1da177e4
LT
172}
173
174
175static void __exit michael_mic_exit(void)
176{
19e2bf14 177 crypto_unregister_shash(&alg);
1da177e4
LT
178}
179
180
181module_init(michael_mic_init);
182module_exit(michael_mic_exit);
183
184MODULE_LICENSE("GPL v2");
185MODULE_DESCRIPTION("Michael MIC");
85d32e7b 186MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
This page took 0.659927 seconds and 5 git commands to generate.