blob: f4c31049601c9d91d389b627b16c6ba128ecef48 [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>
Ard Biesheuvele1b2d982021-02-01 19:02:29 +010010#include <asm/unaligned.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 {
Ard Biesheuvele1b2d982021-02-01 19:02:29 +010022 __le32 pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 if (mctx->pending_len) {
65 int flen = 4 - mctx->pending_len;
66 if (flen > len)
67 flen = len;
Ard Biesheuvele1b2d982021-02-01 19:02:29 +010068 memcpy((u8 *)&mctx->pending + mctx->pending_len, data, flen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 mctx->pending_len += flen;
70 data += flen;
71 len -= flen;
72
73 if (mctx->pending_len < 4)
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Ard Biesheuvele1b2d982021-02-01 19:02:29 +010076 mctx->l ^= le32_to_cpu(mctx->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 michael_block(mctx->l, mctx->r);
78 mctx->pending_len = 0;
79 }
80
81 while (len >= 4) {
Ard Biesheuvele1b2d982021-02-01 19:02:29 +010082 mctx->l ^= get_unaligned_le32(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 michael_block(mctx->l, mctx->r);
Ard Biesheuvele1b2d982021-02-01 19:02:29 +010084 data += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 len -= 4;
86 }
87
88 if (len > 0) {
89 mctx->pending_len = len;
Ard Biesheuvele1b2d982021-02-01 19:02:29 +010090 memcpy(&mctx->pending, data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080092
93 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080097static int michael_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +080099 struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
Ard Biesheuvele1b2d982021-02-01 19:02:29 +0100100 u8 *data = (u8 *)&mctx->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /* Last block and padding (0x5a, 4..7 x 0) */
103 switch (mctx->pending_len) {
104 case 0:
105 mctx->l ^= 0x5a;
106 break;
107 case 1:
108 mctx->l ^= data[0] | 0x5a00;
109 break;
110 case 2:
111 mctx->l ^= data[0] | (data[1] << 8) | 0x5a0000;
112 break;
113 case 3:
114 mctx->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
115 0x5a000000;
116 break;
117 }
118 michael_block(mctx->l, mctx->r);
119 /* l ^= 0; */
120 michael_block(mctx->l, mctx->r);
121
Ard Biesheuvele1b2d982021-02-01 19:02:29 +0100122 put_unaligned_le32(mctx->l, out);
123 put_unaligned_le32(mctx->r, out + 4);
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800124
125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800129static int michael_setkey(struct crypto_shash *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000130 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800132 struct michael_mic_ctx *mctx = crypto_shash_ctx(tfm);
133
Eric Biggers674f3682019-12-30 21:19:36 -0600134 if (keylen != 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -EINVAL;
Herbert Xu06ace7a2005-10-30 21:25:15 +1100136
Ard Biesheuvele1b2d982021-02-01 19:02:29 +0100137 mctx->l = get_unaligned_le32(key);
138 mctx->r = get_unaligned_le32(key + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return 0;
140}
141
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800142static struct shash_alg alg = {
143 .digestsize = 8,
144 .setkey = michael_setkey,
145 .init = michael_init,
146 .update = michael_update,
147 .final = michael_final,
148 .descsize = sizeof(struct michael_mic_desc_ctx),
149 .base = {
150 .cra_name = "michael_mic",
Eric Biggersd6ebf522019-06-02 22:40:57 -0700151 .cra_driver_name = "michael_mic-generic",
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800152 .cra_blocksize = 8,
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800153 .cra_ctxsize = sizeof(struct michael_mic_ctx),
154 .cra_module = THIS_MODULE,
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static int __init michael_mic_init(void)
159{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800160 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163
164static void __exit michael_mic_exit(void)
165{
Adrian-Ken Rueegsegger19e2bf12008-12-07 19:35:38 +0800166 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169
Eric Biggersc4741b22019-04-11 21:57:42 -0700170subsys_initcall(michael_mic_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171module_exit(michael_mic_exit);
172
173MODULE_LICENSE("GPL v2");
174MODULE_DESCRIPTION("Michael MIC");
Jouni Malinen85d32e72007-03-24 17:15:30 -0700175MODULE_AUTHOR("Jouni Malinen <j@w1.fi>");
Kees Cook5d26a102014-11-20 17:05:53 -0800176MODULE_ALIAS_CRYPTO("michael_mic");