blob: 46195e0d0f4d1d30dd20b3bdc8f987ea14176643 [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 *
Jouni Malinen85d32e72007-03-24 17:15:30 -07006 * Copyright (c) 2004 Jouni Malinen <j@w1.fi>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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 */
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080012#include <crypto/internal/hash.h>
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>
Herbert Xu06ace7a2005-10-30 21:25:15 +110017#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19
20struct michael_mic_ctx {
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080021 u32 l, r;
22};
23
24struct michael_mic_desc_ctx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 u8 pending[4];
26 size_t pending_len;
27
28 u32 l, r;
29};
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static 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
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080050static int michael_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080052 struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
53 struct michael_mic_ctx *ctx = crypto_shash_ctx(desc->tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 mctx->pending_len = 0;
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080055 mctx->l = ctx->l;
56 mctx->r = ctx->r;
57
58 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080062static int michael_update(struct shash_desc *desc, const u8 *data,
Herbert Xu6c2bb982006-05-16 22:09:29 +100063 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080065 struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
Herbert Xu06ace7a2005-10-30 21:25:15 +110066 const __le32 *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
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)
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Herbert Xu06ace7a2005-10-30 21:25:15 +110080 src = (const __le32 *)mctx->pending;
81 mctx->l ^= le32_to_cpup(src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 michael_block(mctx->l, mctx->r);
83 mctx->pending_len = 0;
84 }
85
Herbert Xu06ace7a2005-10-30 21:25:15 +110086 src = (const __le32 *)data;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 while (len >= 4) {
Herbert Xu06ace7a2005-10-30 21:25:15 +110089 mctx->l ^= le32_to_cpup(src++);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 michael_block(mctx->l, mctx->r);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 len -= 4;
92 }
93
94 if (len > 0) {
95 mctx->pending_len = len;
Herbert Xu06ace7a2005-10-30 21:25:15 +110096 memcpy(mctx->pending, src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080098
99 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800103static int michael_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800105 struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 u8 *data = mctx->pending;
Herbert Xu06ace7a2005-10-30 21:25:15 +1100107 __le32 *dst = (__le32 *)out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
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
Herbert Xu06ace7a2005-10-30 21:25:15 +1100129 dst[0] = cpu_to_le32(mctx->l);
130 dst[1] = cpu_to_le32(mctx->r);
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800131
132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800136static int michael_setkey(struct crypto_shash *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000137 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800139 struct michael_mic_ctx *mctx = crypto_shash_ctx(tfm);
140
Herbert Xu06ace7a2005-10-30 21:25:15 +1100141 const __le32 *data = (const __le32 *)key;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (keylen != 8) {
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800144 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -EINVAL;
146 }
Herbert Xu06ace7a2005-10-30 21:25:15 +1100147
148 mctx->l = le32_to_cpu(data[0]);
149 mctx->r = le32_to_cpu(data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return 0;
151}
152
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800153static 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167};
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static int __init michael_mic_init(void)
170{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800171 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174
175static void __exit michael_mic_exit(void)
176{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800177 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180
181module_init(michael_mic_init);
182module_exit(michael_mic_exit);
183
184MODULE_LICENSE("GPL v2");
185MODULE_DESCRIPTION("Michael MIC");
Jouni Malinen85d32e72007-03-24 17:15:30 -0700186MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -0800187MODULE_ALIAS_CRYPTO("michael_mic");