GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / staging / ks7010 / michael_mic.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *   Driver for KeyStream wireless LAN
4  *
5  *   Copyright (C) 2005-2008 KeyStream Corp.
6  *   Copyright (C) 2009 Renesas Technology Corp.
7  */
8
9 #include <asm/unaligned.h>
10 #include <linux/bitops.h>
11 #include <linux/string.h>
12 #include "michael_mic.h"
13
14
15 // Reset the state to the empty message.
16 static inline void michael_clear(struct michael_mic *mic)
17 {
18         mic->l = mic->k0;
19         mic->r = mic->k1;
20         mic->m_bytes = 0;
21 }
22
23 static void michael_init(struct michael_mic *mic, u8 *key)
24 {
25         // Set the key
26         mic->k0 = get_unaligned_le32(key);
27         mic->k1 = get_unaligned_le32(key + 4);
28
29         //clear();
30         michael_clear(mic);
31 }
32
33 static inline void michael_block(struct michael_mic *mic)
34 {
35         mic->r ^= rol32(mic->l, 17);
36         mic->l += mic->r;
37         mic->r ^= ((mic->l & 0xff00ff00) >> 8) |
38                   ((mic->l & 0x00ff00ff) << 8);
39         mic->l += mic->r;
40         mic->r ^= rol32(mic->l, 3);
41         mic->l += mic->r;
42         mic->r ^= ror32(mic->l, 2);
43         mic->l += mic->r;
44 }
45
46 static void michael_append(struct michael_mic *mic, u8 *src, int bytes)
47 {
48         int addlen;
49
50         if (mic->m_bytes) {
51                 addlen = 4 - mic->m_bytes;
52                 if (addlen > bytes)
53                         addlen = bytes;
54                 memcpy(&mic->m[mic->m_bytes], src, addlen);
55                 mic->m_bytes += addlen;
56                 src += addlen;
57                 bytes -= addlen;
58
59                 if (mic->m_bytes < 4)
60                         return;
61
62                 mic->l ^= get_unaligned_le32(mic->m);
63                 michael_block(mic);
64                 mic->m_bytes = 0;
65         }
66
67         while (bytes >= 4) {
68                 mic->l ^= get_unaligned_le32(src);
69                 michael_block(mic);
70                 src += 4;
71                 bytes -= 4;
72         }
73
74         if (bytes > 0) {
75                 mic->m_bytes = bytes;
76                 memcpy(mic->m, src, bytes);
77         }
78 }
79
80 static void michael_get_mic(struct michael_mic *mic, u8 *dst)
81 {
82         u8 *data = mic->m;
83
84         switch (mic->m_bytes) {
85         case 0:
86                 mic->l ^= 0x5a;
87                 break;
88         case 1:
89                 mic->l ^= data[0] | 0x5a00;
90                 break;
91         case 2:
92                 mic->l ^= data[0] | (data[1] << 8) | 0x5a0000;
93                 break;
94         case 3:
95                 mic->l ^= data[0] | (data[1] << 8) | (data[2] << 16) |
96                     0x5a000000;
97                 break;
98         }
99         michael_block(mic);
100         michael_block(mic);
101         // The appendByte function has already computed the result.
102         put_unaligned_le32(mic->l, dst);
103         put_unaligned_le32(mic->r, dst + 4);
104
105         // Reset to the empty message.
106         michael_clear(mic);
107 }
108
109 void michael_mic_function(struct michael_mic *mic, u8 *key,
110                           u8 *data, unsigned int len, u8 priority, u8 *result)
111 {
112         u8 pad_data[4] = { priority, 0, 0, 0 };
113         // Compute the MIC value
114         /*
115          * IEEE802.11i  page 47
116          * Figure 43g TKIP MIC processing format
117          * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
118          * |6 |6 |1       |3 |M   |1 |1 |1 |1 |1 |1 |1 |1 | Octet
119          * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
120          * |DA|SA|Priority|0 |Data|M0|M1|M2|M3|M4|M5|M6|M7|
121          * +--+--+--------+--+----+--+--+--+--+--+--+--+--+
122          */
123         michael_init(mic, key);
124         michael_append(mic, data, 12);  /* |DA|SA| */
125         michael_append(mic, pad_data, 4);       /* |Priority|0|0|0| */
126         michael_append(mic, data + 12, len - 12);       /* |Data| */
127         michael_get_mic(mic, result);
128 }