blob: 20e6220f46f6a3420e7e108b78fb6cfab9dd536d [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Cryptographic API
4 *
5 * Michael MIC (IEEE 802.11i/TKIP) keyed digest
6 *
Jouni Malinen85d32e72007-03-24 17:15:30 -07007 * Copyright (c) 2004 Jouni Malinen <j@w1.fi>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +08009#include <crypto/internal/hash.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110010#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/string.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110014#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16
17struct michael_mic_ctx {
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080018 u32 l, r;
19};
20
21struct michael_mic_desc_ctx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 u8 pending[4];
23 size_t pending_len;
24
25 u32 l, r;
26};
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static inline u32 xswap(u32 val)
29{
30 return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
31}
32
33
34#define michael_block(l, r) \
35do { \
36 r ^= rol32(l, 17); \
37 l += r; \
38 r ^= xswap(l); \
39 l += r; \
40 r ^= rol32(l, 3); \
41 l += r; \
42 r ^= ror32(l, 2); \
43 l += r; \
44} while (0)
45
46
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080047static int michael_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080049 struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
50 struct michael_mic_ctx *ctx = crypto_shash_ctx(desc->tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 mctx->pending_len = 0;
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080052 mctx->l = ctx->l;
53 mctx->r = ctx->r;
54
55 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080059static int michael_update(struct shash_desc *desc, const u8 *data,
Herbert Xu6c2bb982006-05-16 22:09:29 +100060 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080062 struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
Herbert Xu06ace7a2005-10-30 21:25:15 +110063 const __le32 *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 if (mctx->pending_len) {
66 int flen = 4 - mctx->pending_len;
67 if (flen > len)
68 flen = len;
69 memcpy(&mctx->pending[mctx->pending_len], data, flen);
70 mctx->pending_len += flen;
71 data += flen;
72 len -= flen;
73
74 if (mctx->pending_len < 4)
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Herbert Xu06ace7a2005-10-30 21:25:15 +110077 src = (const __le32 *)mctx->pending;
78 mctx->l ^= le32_to_cpup(src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 michael_block(mctx->l, mctx->r);
80 mctx->pending_len = 0;
81 }
82
Herbert Xu06ace7a2005-10-30 21:25:15 +110083 src = (const __le32 *)data;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 while (len >= 4) {
Herbert Xu06ace7a2005-10-30 21:25:15 +110086 mctx->l ^= le32_to_cpup(src++);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 michael_block(mctx->l, mctx->r);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 len -= 4;
89 }
90
91 if (len > 0) {
92 mctx->pending_len = len;
Herbert Xu06ace7a2005-10-30 21:25:15 +110093 memcpy(mctx->pending, src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080095
96 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800100static int michael_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800102 struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 u8 *data = mctx->pending;
Herbert Xu06ace7a2005-10-30 21:25:15 +1100104 __le32 *dst = (__le32 *)out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /* Last block and padding (0x5a, 4..7 x 0) */
107 switch (mctx->pending_len) {
108 case 0:
109 mctx->l ^= 0x5a;
110 break;
111 case 1:
112 mctx->l ^= data[0] | 0x5a00;
113 break;
114 case 2:
115 mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
116 break;
117 case 3:
118 mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
119 0x5a000000;
120 break;
121 }
122 michael_block(mctx->l, mctx->r);
123 /* l ^= 0; */
124 michael_block(mctx->l, mctx->r);
125
Herbert Xu06ace7a2005-10-30 21:25:15 +1100126 dst[0] = cpu_to_le32(mctx->l);
127 dst[1] = cpu_to_le32(mctx->r);
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800128
129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800133static int michael_setkey(struct crypto_shash *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000134 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800136 struct michael_mic_ctx *mctx = crypto_shash_ctx(tfm);
137
Herbert Xu06ace7a2005-10-30 21:25:15 +1100138 const __le32 *data = (const __le32 *)key;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (keylen != 8) {
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800141 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -EINVAL;
143 }
Herbert Xu06ace7a2005-10-30 21:25:15 +1100144
145 mctx->l = le32_to_cpu(data[0]);
146 mctx->r = le32_to_cpu(data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return 0;
148}
149
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800150static struct shash_alg alg = {
151 .digestsize = 8,
152 .setkey = michael_setkey,
153 .init = michael_init,
154 .update = michael_update,
155 .final = michael_final,
156 .descsize = sizeof(struct michael_mic_desc_ctx),
157 .base = {
158 .cra_name = "michael_mic",
Eric Biggersd6ebf522019-06-02 22:40:57 -0700159 .cra_driver_name = "michael_mic-generic",
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800160 .cra_blocksize = 8,
161 .cra_alignmask = 3,
162 .cra_ctxsize = sizeof(struct michael_mic_ctx),
163 .cra_module = THIS_MODULE,
164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static int __init michael_mic_init(void)
168{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800169 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172
173static void __exit michael_mic_exit(void)
174{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800175 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178
Eric Biggersc4741b22019-04-11 21:57:42 -0700179subsys_initcall(michael_mic_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180module_exit(michael_mic_exit);
181
182MODULE_LICENSE("GPL v2");
183MODULE_DESCRIPTION("Michael MIC");
Jouni Malinen85d32e72007-03-24 17:15:30 -0700184MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -0800185MODULE_ALIAS_CRYPTO("michael_mic");