1 // SPDX-License-Identifier: GPL-2.0-only
5 * Michael MIC (IEEE 802.11i/TKIP) keyed digest
7 * Copyright (c) 2004 Jouni Malinen <j@w1.fi>
9 #include <crypto/internal/hash.h>
10 #include <linux/unaligned.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/types.h>
17 struct michael_mic_ctx
{
21 struct michael_mic_desc_ctx
{
28 static inline u32
xswap(u32 val
)
30 return ((val
& 0x00ff00ff) << 8) | ((val
& 0xff00ff00) >> 8);
34 #define michael_block(l, r) \
47 static int michael_init(struct shash_desc
*desc
)
49 struct michael_mic_desc_ctx
*mctx
= shash_desc_ctx(desc
);
50 struct michael_mic_ctx
*ctx
= crypto_shash_ctx(desc
->tfm
);
51 mctx
->pending_len
= 0;
59 static int michael_update(struct shash_desc
*desc
, const u8
*data
,
62 struct michael_mic_desc_ctx
*mctx
= shash_desc_ctx(desc
);
64 if (mctx
->pending_len
) {
65 int flen
= 4 - mctx
->pending_len
;
68 memcpy((u8
*)&mctx
->pending
+ mctx
->pending_len
, data
, flen
);
69 mctx
->pending_len
+= flen
;
73 if (mctx
->pending_len
< 4)
76 mctx
->l
^= le32_to_cpu(mctx
->pending
);
77 michael_block(mctx
->l
, mctx
->r
);
78 mctx
->pending_len
= 0;
82 mctx
->l
^= get_unaligned_le32(data
);
83 michael_block(mctx
->l
, mctx
->r
);
89 mctx
->pending_len
= len
;
90 memcpy(&mctx
->pending
, data
, len
);
97 static int michael_final(struct shash_desc
*desc
, u8
*out
)
99 struct michael_mic_desc_ctx
*mctx
= shash_desc_ctx(desc
);
100 u8
*data
= (u8
*)&mctx
->pending
;
102 /* Last block and padding (0x5a, 4..7 x 0) */
103 switch (mctx
->pending_len
) {
108 mctx
->l
^= data
[0] | 0x5a00;
111 mctx
->l
^= data
[0] | (data
[1] << 8) | 0x5a0000;
114 mctx
->l
^= data
[0] | (data
[1] << 8) | (data
[2] << 16) |
118 michael_block(mctx
->l
, mctx
->r
);
120 michael_block(mctx
->l
, mctx
->r
);
122 put_unaligned_le32(mctx
->l
, out
);
123 put_unaligned_le32(mctx
->r
, out
+ 4);
129 static int michael_setkey(struct crypto_shash
*tfm
, const u8
*key
,
132 struct michael_mic_ctx
*mctx
= crypto_shash_ctx(tfm
);
137 mctx
->l
= get_unaligned_le32(key
);
138 mctx
->r
= get_unaligned_le32(key
+ 4);
142 static struct shash_alg alg
= {
144 .setkey
= michael_setkey
,
145 .init
= michael_init
,
146 .update
= michael_update
,
147 .final
= michael_final
,
148 .descsize
= sizeof(struct michael_mic_desc_ctx
),
150 .cra_name
= "michael_mic",
151 .cra_driver_name
= "michael_mic-generic",
153 .cra_ctxsize
= sizeof(struct michael_mic_ctx
),
154 .cra_module
= THIS_MODULE
,
158 static int __init
michael_mic_init(void)
160 return crypto_register_shash(&alg
);
164 static void __exit
michael_mic_exit(void)
166 crypto_unregister_shash(&alg
);
170 subsys_initcall(michael_mic_init
);
171 module_exit(michael_mic_exit
);
173 MODULE_LICENSE("GPL v2");
174 MODULE_DESCRIPTION("Michael MIC");
175 MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
176 MODULE_ALIAS_CRYPTO("michael_mic");