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 <asm/byteorder.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
);
65 if (mctx
->pending_len
) {
66 int flen
= 4 - mctx
->pending_len
;
69 memcpy(&mctx
->pending
[mctx
->pending_len
], data
, flen
);
70 mctx
->pending_len
+= flen
;
74 if (mctx
->pending_len
< 4)
77 src
= (const __le32
*)mctx
->pending
;
78 mctx
->l
^= le32_to_cpup(src
);
79 michael_block(mctx
->l
, mctx
->r
);
80 mctx
->pending_len
= 0;
83 src
= (const __le32
*)data
;
86 mctx
->l
^= le32_to_cpup(src
++);
87 michael_block(mctx
->l
, mctx
->r
);
92 mctx
->pending_len
= len
;
93 memcpy(mctx
->pending
, src
, len
);
100 static int michael_final(struct shash_desc
*desc
, u8
*out
)
102 struct michael_mic_desc_ctx
*mctx
= shash_desc_ctx(desc
);
103 u8
*data
= mctx
->pending
;
104 __le32
*dst
= (__le32
*)out
;
106 /* Last block and padding (0x5a, 4..7 x 0) */
107 switch (mctx
->pending_len
) {
112 mctx
->l
^= data
[0] | 0x5a00;
115 mctx
->l
^= data
[0] | (data
[1] << 8) | 0x5a0000;
118 mctx
->l
^= data
[0] | (data
[1] << 8) | (data
[2] << 16) |
122 michael_block(mctx
->l
, mctx
->r
);
124 michael_block(mctx
->l
, mctx
->r
);
126 dst
[0] = cpu_to_le32(mctx
->l
);
127 dst
[1] = cpu_to_le32(mctx
->r
);
133 static int michael_setkey(struct crypto_shash
*tfm
, const u8
*key
,
136 struct michael_mic_ctx
*mctx
= crypto_shash_ctx(tfm
);
138 const __le32
*data
= (const __le32
*)key
;
141 crypto_shash_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
145 mctx
->l
= le32_to_cpu(data
[0]);
146 mctx
->r
= le32_to_cpu(data
[1]);
150 static struct shash_alg alg
= {
152 .setkey
= michael_setkey
,
153 .init
= michael_init
,
154 .update
= michael_update
,
155 .final
= michael_final
,
156 .descsize
= sizeof(struct michael_mic_desc_ctx
),
158 .cra_name
= "michael_mic",
159 .cra_driver_name
= "michael_mic-generic",
162 .cra_ctxsize
= sizeof(struct michael_mic_ctx
),
163 .cra_module
= THIS_MODULE
,
167 static int __init
michael_mic_init(void)
169 return crypto_register_shash(&alg
);
173 static void __exit
michael_mic_exit(void)
175 crypto_unregister_shash(&alg
);
179 subsys_initcall(michael_mic_init
);
180 module_exit(michael_mic_exit
);
182 MODULE_LICENSE("GPL v2");
183 MODULE_DESCRIPTION("Michael MIC");
184 MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
185 MODULE_ALIAS_CRYPTO("michael_mic");