]>
Commit | Line | Data |
---|---|---|
b5a5fc4d | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2018 grauerfuchs | |
3 | // | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // HID card format packing/unpacking routines | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #include <stdbool.h> | |
12 | #include <stdint.h> | |
13 | #include <inttypes.h> | |
5d643cc0 | 14 | #include <math.h> |
b5a5fc4d | 15 | #include "cmddata.h" |
16 | #include "hidcardformats.h" | |
17 | #include "hidcardformatutils.h" | |
18 | #include "parity.h" // for parity | |
19 | #include "ui.h" | |
20 | ||
21 | bool Pack_H10301(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
22 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
23 | if (card->FacilityCode > 0xFF) return false; // Can't encode FC. | |
24 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
5d643cc0 | 25 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 26 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 27 | packed->Length = 26; // Set number of bits |
28 | packed->bot |= (card->CardNumber & 0xFFFF) << 1; | |
29 | packed->bot |= (card->FacilityCode & 0xFF) << 17; | |
be59094d | 30 | packed->bot |= oddparity32((packed->bot >> 1) & 0xFFF) & 1; |
31 | packed->bot |= (evenparity32((packed->bot >> 13) & 0xFFF) & 1) << 25; | |
b5a5fc4d | 32 | return add_HID_header(packed); |
33 | } | |
34 | bool Unpack_H10301(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
35 | memset(card, 0, sizeof(hidproxcard_t)); | |
36 | if (packed->Length != 26) return false; // Wrong length? Stop here. | |
37 | card->CardNumber = (packed->bot >> 1) & 0xFFFF; | |
38 | card->FacilityCode = (packed->bot >> 17) & 0xFF; | |
b5a5fc4d | 39 | card->ParityValid = |
40 | (oddparity32((packed->bot >> 1) & 0xFFF) == (packed->bot & 1)) && | |
41 | ((evenparity32((packed->bot >> 13) & 0xFFF) & 1) == ((packed->bot >> 25) & 1)); | |
42 | return true; | |
43 | } | |
44 | ||
45 | bool Pack_Tecom27(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
46 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
47 | if (card->FacilityCode > 0x7FF) return false; // Can't encode FC. | |
48 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
5d643cc0 | 49 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 50 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 51 | packed->Length = 27; |
52 | set_nonlinear_field(packed, card->FacilityCode, 10, (uint8_t[]){15, 19, 24, 23, 22, 18, 6, 10, 14, 3, 2}); | |
53 | set_nonlinear_field(packed, card->CardNumber, 16, (uint8_t[]){0, 1, 13, 12, 9, 26, 20, 16, 17, 21, 25, 7, 8, 11, 4, 5}); | |
54 | return add_HID_header(packed); | |
55 | } | |
56 | bool Unpack_Tecom27(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
57 | memset(card, 0, sizeof(hidproxcard_t)); | |
58 | if (packed->Length != 27) return false; // Wrong length? Stop here. | |
59 | card->CardNumber = get_nonlinear_field(packed, 16, (uint8_t[]){0, 1, 13, 12, 9, 26, 20, 16, 17, 21, 25, 7, 8, 11, 4, 5}); | |
60 | card->FacilityCode = get_nonlinear_field(packed, 10, (uint8_t[]){15, 19, 24, 23, 22, 18, 6, 10, 14, 3, 2}); | |
b5a5fc4d | 61 | return true; |
62 | } | |
63 | ||
64 | bool Pack_2804W(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
65 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
66 | if (card->FacilityCode > 0x0FF) return false; // Can't encode FC. | |
67 | if (card->CardNumber > 0x7FFF) return false; // Can't encode CN. | |
5d643cc0 | 68 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 69 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 70 | packed->Length = 28; |
71 | set_linear_field(packed, card->FacilityCode, 4, 8); | |
72 | set_linear_field(packed, card->CardNumber, 12, 15); | |
be59094d | 73 | set_bit_by_position(packed, |
74 | oddparity32(get_nonlinear_field(packed, 16, (uint8_t[]){4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26})) | |
75 | , 2); | |
76 | set_bit_by_position(packed, | |
77 | evenparity32(get_linear_field(packed, 1, 13)) | |
78 | , 0); | |
79 | set_bit_by_position(packed, | |
80 | oddparity32(get_linear_field(packed, 0, 27)) | |
81 | , 27); | |
b5a5fc4d | 82 | return add_HID_header(packed); |
83 | } | |
84 | bool Unpack_2804W(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
85 | memset(card, 0, sizeof(hidproxcard_t)); | |
86 | if (packed->Length != 28) return false; // Wrong length? Stop here. | |
87 | card->FacilityCode = get_linear_field(packed, 4, 8); | |
88 | card->CardNumber = get_linear_field(packed, 12, 15); | |
b5a5fc4d | 89 | card->ParityValid = |
90 | (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 13))) && | |
91 | (get_bit_by_position(packed, 2) == oddparity32(get_nonlinear_field(packed, 16, (uint8_t[]){4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26}))) && | |
92 | (get_bit_by_position(packed, 27) == oddparity32(get_linear_field(packed, 0, 27))); | |
93 | return true; | |
94 | } | |
95 | ||
96 | bool Pack_ATSW30(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
97 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
98 | if (card->FacilityCode > 0xFFF) return false; // Can't encode FC. | |
99 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
5d643cc0 | 100 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 101 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 102 | packed->Length = 30; |
103 | set_linear_field(packed, card->FacilityCode, 1, 12); | |
104 | set_linear_field(packed, card->CardNumber, 13, 16); | |
be59094d | 105 | set_bit_by_position(packed, |
106 | evenparity32(get_linear_field(packed, 1, 12)) | |
107 | , 0); | |
108 | set_bit_by_position(packed, | |
109 | oddparity32(get_linear_field(packed, 13, 16)) | |
110 | , 29); | |
b5a5fc4d | 111 | return add_HID_header(packed); |
112 | } | |
113 | bool Unpack_ATSW30(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
114 | memset(card, 0, sizeof(hidproxcard_t)); | |
115 | if (packed->Length != 30) return false; // Wrong length? Stop here. | |
116 | card->FacilityCode = get_linear_field(packed, 1, 12); | |
117 | card->CardNumber = get_linear_field(packed, 13, 16); | |
b5a5fc4d | 118 | card->ParityValid = |
119 | (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 12))) && | |
120 | (get_bit_by_position(packed, 29) == oddparity32(get_linear_field(packed, 13, 16))); | |
121 | return true; | |
122 | } | |
123 | bool Pack_ADT31(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
124 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
125 | if (card->FacilityCode > 0x0F) return false; // Can't encode FC. | |
126 | if (card->CardNumber > 0x7FFFFF) return false; // Can't encode CN. | |
5d643cc0 | 127 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 128 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 129 | packed->Length = 31; |
130 | set_linear_field(packed, card->FacilityCode, 1, 4); | |
131 | set_linear_field(packed, card->CardNumber, 5, 23); | |
132 | // Parity not known, but 4 bits are unused. | |
133 | return add_HID_header(packed); | |
134 | } | |
135 | bool Unpack_ADT31(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
136 | memset(card, 0, sizeof(hidproxcard_t)); | |
137 | if (packed->Length != 31) return false; // Wrong length? Stop here. | |
138 | card->FacilityCode = get_linear_field(packed, 1, 4); | |
139 | card->CardNumber = get_linear_field(packed, 5, 23); | |
b5a5fc4d | 140 | return true; |
141 | } | |
142 | ||
143 | bool Pack_D10202(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
144 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
145 | if (card->FacilityCode > 0x007F) return false; // Can't encode FC. | |
146 | if (card->CardNumber > 0x00FFFFFF) return false; // Can't encode CN. | |
5d643cc0 | 147 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 148 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 149 | packed->Length = 33; // Set number of bits |
150 | set_linear_field(packed, card->FacilityCode, 1, 7); | |
151 | set_linear_field(packed, card->CardNumber, 8, 24); | |
be59094d | 152 | set_bit_by_position(packed, evenparity32(get_linear_field(packed, 1, 16)), 0); |
153 | set_bit_by_position(packed, oddparity32(get_linear_field(packed, 16, 16)), 32); | |
b5a5fc4d | 154 | return add_HID_header(packed); |
155 | } | |
156 | bool Unpack_D10202(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
157 | memset(card, 0, sizeof(hidproxcard_t)); | |
be59094d | 158 | if (packed->Length != 33) return false; // Wrong length? Stop here. |
b5a5fc4d | 159 | card->CardNumber = get_linear_field(packed, 8, 24); |
160 | card->FacilityCode = get_linear_field(packed, 1, 7); | |
b5a5fc4d | 161 | card->ParityValid = |
162 | (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 16))) && | |
163 | (get_bit_by_position(packed, 32) == oddparity32(get_linear_field(packed, 16, 16))); | |
164 | return true; | |
165 | } | |
166 | ||
b5a5fc4d | 167 | bool Pack_H10306(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ |
168 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
169 | if (card->FacilityCode > 0xFFFF) return false; // Can't encode FC. | |
170 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
5d643cc0 | 171 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 172 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 173 | packed->Length = 34; // Set number of bits |
174 | packed->bot |= (card->CardNumber & 0xFFFF) << 1; | |
175 | packed->bot |= (card->FacilityCode & 0x7FFF) << 17; | |
176 | packed->mid |= (card->FacilityCode & 0x8000) >> 15; | |
be59094d | 177 | packed->mid |= (evenparity32((packed->mid & 0x00000001) ^ (packed->bot & 0xFFFE0000)) & 1) << 1; |
178 | packed->bot |= ( oddparity32(packed->bot & 0x0001FFFE) & 1); | |
b5a5fc4d | 179 | return add_HID_header(packed); |
180 | } | |
181 | bool Unpack_H10306(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
182 | memset(card, 0, sizeof(hidproxcard_t)); | |
183 | if (packed->Length != 34) return false; // Wrong length? Stop here. | |
b5a5fc4d | 184 | card->CardNumber = (packed->bot >> 1) & 0xFFFF; |
185 | card->FacilityCode = ((packed->mid & 1) << 15) | ((packed->bot >> 17) & 0xFF); | |
b5a5fc4d | 186 | card->ParityValid = |
187 | ((evenparity32((packed->mid & 0x00000001) ^ (packed->bot & 0xFFFE0000)) & 1) == ((packed->mid >> 1) & 1)) && | |
188 | ((oddparity32(packed->bot & 0x0001FFFE) & 1) == ((packed->bot & 1))); | |
189 | return true; | |
190 | } | |
be59094d | 191 | bool Pack_N10002(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ |
b5a5fc4d | 192 | memset(packed, 0, sizeof(hidproxmessage_t)); |
193 | if (card->FacilityCode > 0xFF) return false; // Can't encode FC. | |
194 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
5d643cc0 | 195 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 196 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 197 | packed->Length = 34; // Set number of bits |
198 | set_linear_field(packed, card->FacilityCode, 9, 8); | |
199 | set_linear_field(packed, card->CardNumber, 17, 16); | |
200 | return add_HID_header(packed); | |
201 | } | |
be59094d | 202 | bool Unpack_N10002(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ |
b5a5fc4d | 203 | memset(card, 0, sizeof(hidproxcard_t)); |
204 | if (packed->Length != 34) return false; // Wrong length? Stop here. | |
b5a5fc4d | 205 | card->CardNumber = get_linear_field(packed, 17, 16); |
206 | card->FacilityCode = get_linear_field(packed, 9, 8); | |
b5a5fc4d | 207 | return true; |
208 | } | |
209 | ||
b5a5fc4d | 210 | bool Pack_C1k35s(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ |
211 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
212 | if (card->FacilityCode > 0xFFF) return false; // Can't encode FC. | |
213 | if (card->CardNumber > 0xFFFFF) return false; // Can't encode CN. | |
5d643cc0 | 214 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 215 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 216 | packed->Length = 35; // Set number of bits |
217 | packed->bot |= (card->CardNumber & 0x000FFFFF) << 1; | |
218 | packed->bot |= (card->FacilityCode & 0x000007FF) << 21; | |
219 | packed->mid |= (card->FacilityCode & 0x00000800) >> 11; | |
be59094d | 220 | packed->mid |= (evenparity32((packed->mid & 0x00000001) ^ (packed->bot & 0xB6DB6DB6)) & 1) << 1; |
221 | packed->bot |= ( oddparity32((packed->mid & 0x00000003) ^ (packed->bot & 0x6DB6DB6C)) & 1); | |
222 | packed->mid |= ( oddparity32((packed->mid & 0x00000003) ^ (packed->bot & 0xFFFFFFFF)) & 1) << 2; | |
b5a5fc4d | 223 | return add_HID_header(packed); |
224 | } | |
225 | bool Unpack_C1k35s(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
226 | memset(card, 0, sizeof(hidproxcard_t)); | |
227 | if (packed->Length != 35) return false; // Wrong length? Stop here. | |
b5a5fc4d | 228 | card->CardNumber = (packed->bot >> 1) & 0x000FFFFF; |
229 | card->FacilityCode = ((packed->mid & 1) << 11) | ((packed->bot >> 21)); | |
b5a5fc4d | 230 | card->ParityValid = |
231 | (evenparity32((packed->mid & 0x00000001) ^ (packed->bot & 0xB6DB6DB6)) == ((packed->mid >> 1) & 1)) && | |
232 | ( oddparity32((packed->mid & 0x00000003) ^ (packed->bot & 0x6DB6DB6C)) == ((packed->bot >> 0) & 1)) && | |
233 | ( oddparity32((packed->mid & 0x00000003) ^ (packed->bot & 0xFFFFFFFF)) == ((packed->mid >> 2) & 1)); | |
234 | return true; | |
235 | } | |
236 | ||
237 | bool Pack_H10320(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
238 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
239 | if (card->FacilityCode > 0) return false; // Can't encode FC. (none in this format) | |
240 | if (card->CardNumber > 99999999) return false; // Can't encode CN. | |
5d643cc0 | 241 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 242 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 243 | packed->Length = 36; // Set number of bits |
244 | // This card is BCD-encoded rather than binary. Set the 4-bit groups independently. | |
5d643cc0 | 245 | for (uint32_t idx = 0; idx < 8; idx++){ |
246 | set_linear_field(packed, (uint64_t)(card->CardNumber / pow(10, 7-idx)) % 10, idx * 4, 4); | |
247 | } | |
be59094d | 248 | set_bit_by_position(packed, evenparity32( |
249 | get_nonlinear_field(packed, 8, (uint8_t[]){0, 4, 8, 12, 16, 20, 24, 28}) | |
250 | ), 32); | |
251 | set_bit_by_position(packed, oddparity32( | |
252 | get_nonlinear_field(packed, 8, (uint8_t[]){1, 5, 9, 13, 17, 21, 25, 29}) | |
253 | ), 33); | |
254 | set_bit_by_position(packed, evenparity32( | |
255 | get_nonlinear_field(packed, 8, (uint8_t[]){2, 6, 10, 14, 18, 22, 28, 30}) | |
256 | ), 34); | |
257 | set_bit_by_position(packed, evenparity32( | |
258 | get_nonlinear_field(packed, 8, (uint8_t[]){3, 7, 11, 15, 19, 23, 29, 31}) | |
259 | ), 35); | |
b5a5fc4d | 260 | return add_HID_header(packed); |
261 | } | |
262 | bool Unpack_H10320(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
263 | memset(card, 0, sizeof(hidproxcard_t)); | |
264 | if (packed->Length != 36) return false; // Wrong length? Stop here. | |
265 | // This card is BCD-encoded rather than binary. Get the 4-bit groups independently. | |
5d643cc0 | 266 | for (uint32_t idx = 0; idx < 8; idx++){ |
267 | uint64_t val = get_linear_field(packed, idx * 4, 4); | |
268 | if (val > 9){ | |
269 | // Violation of BCD; Zero and exit. | |
270 | card->CardNumber = 0; | |
271 | return false; | |
272 | } else { | |
273 | card->CardNumber += val * pow(10, 7-idx); | |
274 | } | |
275 | } | |
b5a5fc4d | 276 | card->ParityValid = |
277 | (get_bit_by_position(packed, 32) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]){0, 4, 8, 12, 16, 20, 24, 28}))) && | |
278 | (get_bit_by_position(packed, 33) == oddparity32(get_nonlinear_field(packed, 8, (uint8_t[]){1, 5, 9, 13, 17, 21, 25, 29}))) && | |
279 | (get_bit_by_position(packed, 34) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]){2, 6, 10, 14, 18, 22, 28, 30}))) && | |
280 | (get_bit_by_position(packed, 35) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]){3, 7, 11, 15, 19, 23, 29, 31}))); | |
281 | return true; | |
282 | } | |
283 | ||
5d643cc0 | 284 | bool Pack_S12906(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ |
285 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
286 | if (card->FacilityCode > 0xFF) return false; // Can't encode FC. | |
287 | if (card->IssueLevel > 0x03) return false; // Can't encode IL. | |
288 | if (card->CardNumber > 0x00FFFFFF) return false; // Can't encode CN. | |
be59094d | 289 | if (card->OEM > 0) return false; // Not used in this format |
5d643cc0 | 290 | packed->Length = 36; // Set number of bits |
291 | set_linear_field(packed, card->FacilityCode, 1, 8); | |
292 | set_linear_field(packed, card->IssueLevel, 9, 2); | |
293 | set_linear_field(packed, card->CardNumber, 11, 24); | |
be59094d | 294 | set_bit_by_position(packed, |
295 | oddparity32(get_linear_field(packed, 1, 17)) | |
296 | , 0); | |
297 | set_bit_by_position(packed, | |
298 | oddparity32(get_linear_field(packed, 17, 18)) | |
299 | , 35); | |
5d643cc0 | 300 | return add_HID_header(packed); |
301 | } | |
302 | bool Unpack_S12906(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
303 | memset(card, 0, sizeof(hidproxcard_t)); | |
304 | if (packed->Length != 36) return false; // Wrong length? Stop here. | |
5d643cc0 | 305 | card->FacilityCode = get_linear_field(packed, 1, 8); |
306 | card->IssueLevel = get_linear_field(packed, 9, 2); | |
307 | card->CardNumber = get_linear_field(packed, 11, 24); | |
5d643cc0 | 308 | card->ParityValid = |
309 | (get_bit_by_position(packed, 0) == oddparity32(get_linear_field(packed, 1, 17))) && | |
be59094d | 310 | (get_bit_by_position(packed, 35) == oddparity32(get_linear_field(packed, 17, 18))); |
5d643cc0 | 311 | return true; |
312 | } | |
313 | ||
314 | bool Pack_Sie36(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
315 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
316 | if (card->FacilityCode > 0x0003FFFF) return false; // Can't encode FC. | |
5d643cc0 | 317 | if (card->CardNumber > 0x0000FFFF) return false; // Can't encode CN. |
318 | if (card->IssueLevel > 0) return false; // Not used in this format | |
be59094d | 319 | if (card->OEM > 0) return false; // Not used in this format |
5d643cc0 | 320 | packed->Length = 36; // Set number of bits |
321 | set_linear_field(packed, card->FacilityCode, 1, 18); | |
322 | set_linear_field(packed, card->CardNumber, 19, 16); | |
be59094d | 323 | set_bit_by_position(packed, |
324 | oddparity32(get_nonlinear_field(packed, 23, (uint8_t[]){1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19, 21, 22, 24, 25, 27, 28, 30, 31, 33, 34})) | |
325 | , 0); | |
326 | set_bit_by_position(packed, | |
327 | evenparity32(get_nonlinear_field(packed, 23, (uint8_t[]){1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34})) | |
328 | , 35); | |
5d643cc0 | 329 | return add_HID_header(packed); |
330 | } | |
331 | bool Unpack_Sie36(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
332 | memset(card, 0, sizeof(hidproxcard_t)); | |
333 | if (packed->Length != 36) return false; // Wrong length? Stop here. | |
5d643cc0 | 334 | card->FacilityCode = get_linear_field(packed, 1, 18); |
335 | card->CardNumber = get_linear_field(packed, 19, 16); | |
5d643cc0 | 336 | card->ParityValid = |
337 | (get_bit_by_position(packed, 0) == oddparity32(get_nonlinear_field(packed, 23, (uint8_t[]){1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19, 21, 22, 24, 25, 27, 28, 30, 31, 33, 34}))) && | |
338 | (get_bit_by_position(packed, 35) == oddparity32(get_nonlinear_field(packed, 23, (uint8_t[]){1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34}))); | |
5d643cc0 | 339 | return true; |
340 | } | |
341 | ||
be59094d | 342 | bool Pack_C15001(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ |
343 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
344 | if (card->FacilityCode > 0x000000FF) return false; // Can't encode FC. | |
345 | if (card->CardNumber > 0x0000FFFF) return false; // Can't encode CN. | |
346 | if (card->IssueLevel > 0) return false; // Not used in this format | |
347 | if (card->OEM > 0x000003FF) return false; // Can't encode OEM. | |
348 | packed->Length = 36; // Set number of bits | |
349 | set_linear_field(packed, card->OEM, 1, 10); | |
350 | set_linear_field(packed, card->FacilityCode, 11, 8); | |
351 | set_linear_field(packed, card->CardNumber, 19, 16); | |
352 | set_bit_by_position(packed, | |
353 | evenparity32(get_linear_field(packed, 1, 17)) | |
354 | , 0); | |
355 | set_bit_by_position(packed, | |
356 | oddparity32(get_linear_field(packed, 18, 17)) | |
357 | , 35); | |
358 | return add_HID_header(packed); | |
359 | } | |
360 | bool Unpack_C15001(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
361 | memset(card, 0, sizeof(hidproxcard_t)); | |
362 | if (packed->Length != 36) return false; // Wrong length? Stop here. | |
363 | card->OEM = get_linear_field(packed, 1, 10); | |
364 | card->FacilityCode = get_linear_field(packed, 11, 8); | |
365 | card->CardNumber = get_linear_field(packed, 19, 16); | |
366 | card->ParityValid = | |
367 | (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 17))) && | |
368 | (get_bit_by_position(packed, 35) == oddparity32(get_linear_field(packed, 18, 17))); | |
369 | return true; | |
370 | } | |
b5a5fc4d | 371 | |
372 | bool Pack_H10302(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
373 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
374 | if (card->FacilityCode > 0) return false; // Can't encode FC. (none in this format) | |
375 | if (card->CardNumber > 0x00000007FFFFFFFF) return false; // Can't encode CN. | |
5d643cc0 | 376 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 377 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 378 | packed->Length = 37; // Set number of bits |
379 | set_linear_field(packed, card->CardNumber, 1, 35); | |
be59094d | 380 | set_bit_by_position(packed, |
381 | evenparity32(get_linear_field(packed, 1, 18)) | |
382 | , 0); | |
383 | set_bit_by_position(packed, | |
384 | oddparity32(get_linear_field(packed, 18, 18)) | |
385 | , 36); | |
b5a5fc4d | 386 | return add_HID_header(packed); |
387 | } | |
388 | bool Unpack_H10302(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
389 | memset(card, 0, sizeof(hidproxcard_t)); | |
390 | if (packed->Length != 37) return false; // Wrong length? Stop here. | |
b5a5fc4d | 391 | card->CardNumber = get_linear_field(packed, 1, 35); |
b5a5fc4d | 392 | card->ParityValid = |
393 | (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 18))) && | |
394 | (get_bit_by_position(packed, 36) == oddparity32(get_linear_field(packed, 18, 18))); | |
b5a5fc4d | 395 | return true; |
396 | } | |
397 | ||
398 | bool Pack_H10304(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
399 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
400 | if (card->FacilityCode > 0x0000FFFF) return false; // Can't encode FC. | |
401 | if (card->CardNumber > 0x0007FFFF) return false; // Can't encode CN. | |
5d643cc0 | 402 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 403 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 404 | packed->Length = 37; // Set number of bits |
405 | packed->bot |= (card->CardNumber & 0x0007FFFF) << 1; | |
406 | packed->bot |= (card->FacilityCode & 0x00000FFF) << 20; | |
407 | packed->mid |= (card->FacilityCode & 0x0000F000) >> 12; | |
be59094d | 408 | packed->mid |= (evenparity32((packed->mid & 0x0000000F) ^ (packed->bot & 0xFFFC0000)) & 1) << 4; |
409 | packed->bot |= ( oddparity32(packed->bot & 0x0007FFFE) & 1); | |
b5a5fc4d | 410 | return add_HID_header(packed); |
411 | } | |
412 | bool Unpack_H10304(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
413 | memset(card, 0, sizeof(hidproxcard_t)); | |
414 | if (packed->Length != 37) return false; // Wrong length? Stop here. | |
b5a5fc4d | 415 | card->CardNumber = (packed->bot >> 1) & 0x0007FFFF; |
416 | card->FacilityCode = ((packed->mid & 0xF) << 12) | ((packed->bot >> 20)); | |
b5a5fc4d | 417 | card->ParityValid = |
418 | (evenparity32((packed->mid & 0x0000000F) ^ (packed->bot & 0xFFFC0000)) == ((packed->mid >> 4) & 1)) && | |
419 | (oddparity32( packed->bot & 0x0007FFFE) == (packed->bot & 1)); | |
420 | return true; | |
421 | } | |
422 | ||
b5a5fc4d | 423 | bool Pack_P10001(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ |
424 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
425 | if (card->FacilityCode > 0xFFF) return false; // Can't encode FC. | |
426 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
5d643cc0 | 427 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 428 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 429 | packed->Length = 40; // Set number of bits |
430 | set_linear_field(packed, 0xF, 0, 4); | |
431 | set_linear_field(packed, card->FacilityCode, 4, 12); | |
432 | set_linear_field(packed, card->CardNumber, 16, 16); | |
be59094d | 433 | set_linear_field(packed, |
434 | get_linear_field(packed, 0, 8) ^ | |
435 | get_linear_field(packed, 8, 8) ^ | |
436 | get_linear_field(packed, 16, 8) ^ | |
437 | get_linear_field(packed, 24, 8) | |
438 | , 32, 8); | |
b5a5fc4d | 439 | return add_HID_header(packed); |
440 | } | |
441 | bool Unpack_P10001(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
442 | memset(card, 0, sizeof(hidproxcard_t)); | |
443 | if (packed->Length != 40) return false; // Wrong length? Stop here. | |
b5a5fc4d | 444 | card->CardNumber = get_linear_field(packed, 16, 16); |
445 | card->FacilityCode = get_linear_field(packed, 4, 12); | |
b5a5fc4d | 446 | card->ParityValid = ( |
447 | get_linear_field(packed, 0, 8) ^ | |
448 | get_linear_field(packed, 8, 8) ^ | |
449 | get_linear_field(packed, 16, 8) ^ | |
450 | get_linear_field(packed, 24, 8) | |
451 | ) == get_linear_field(packed, 32, 8); | |
452 | return true; | |
453 | } | |
454 | ||
b5a5fc4d | 455 | bool Pack_C1k48s(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ |
456 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
457 | if (card->FacilityCode > 0x003FFFFF) return false; // Can't encode FC. | |
458 | if (card->CardNumber > 0x007FFFFF) return false; // Can't encode CN. | |
5d643cc0 | 459 | if (card->IssueLevel > 0) return false; // Not used in this format |
be59094d | 460 | if (card->OEM > 0) return false; // Not used in this format |
b5a5fc4d | 461 | packed->Length = 48; // Set number of bits |
462 | packed->bot |= (card->CardNumber & 0x007FFFFF) << 1; | |
463 | packed->bot |= (card->FacilityCode & 0x000000FF) << 24; | |
464 | packed->mid |= (card->FacilityCode & 0x003FFF00) >> 8; | |
be59094d | 465 | packed->mid |= (evenparity32((packed->mid & 0x00001B6D) ^ (packed->bot & 0xB6DB6DB6)) & 1) << 14; |
466 | packed->bot |= ( oddparity32((packed->mid & 0x000036DB) ^ (packed->bot & 0x6DB6DB6C)) & 1); | |
467 | packed->mid |= ( oddparity32((packed->mid & 0x00007FFF) ^ (packed->bot & 0xFFFFFFFF)) & 1) << 15; | |
b5a5fc4d | 468 | return add_HID_header(packed); |
469 | } | |
470 | bool Unpack_C1k48s(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
471 | memset(card, 0, sizeof(hidproxcard_t)); | |
472 | if (packed->Length != 48) return false; // Wrong length? Stop here. | |
b5a5fc4d | 473 | card->CardNumber = (packed->bot >> 1) & 0x007FFFFF; |
474 | card->FacilityCode = ((packed->mid & 0x00003FFF) << 8) | ((packed->bot >> 24)); | |
b5a5fc4d | 475 | card->ParityValid = |
476 | (evenparity32((packed->mid & 0x00001B6D) ^ (packed->bot & 0xB6DB6DB6)) == ((packed->mid >> 14) & 1)) && | |
477 | ( oddparity32((packed->mid & 0x000036DB) ^ (packed->bot & 0x6DB6DB6C)) == ((packed->bot >> 0) & 1)) && | |
478 | ( oddparity32((packed->mid & 0x00007FFF) ^ (packed->bot & 0xFFFFFFFF)) == ((packed->mid >> 15) & 1)); | |
479 | return true; | |
480 | } | |
481 | ||
482 | static const hidcardformat_t FormatTable[] = { | |
be59094d | 483 | {"H10301", Pack_H10301, Unpack_H10301, "HID H10301 26-bit", {1, 1, 0, 0, 1}}, // imported from old pack/unpack |
484 | {"Tecom27", Pack_Tecom27, Unpack_Tecom27, "Tecom 27-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au | |
485 | {"2804W", Pack_2804W, Unpack_2804W, "2804 Wiegand", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au | |
486 | {"ATSW30", Pack_ATSW30, Unpack_ATSW30, "ATS Wiegand 30-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au | |
487 | {"ADT31", Pack_ADT31, Unpack_ADT31, "HID ADT 31-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au | |
488 | {"D10202", Pack_D10202, Unpack_D10202, "HID D10202 33-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au | |
489 | {"H10306", Pack_H10306, Unpack_H10306, "HID H10306 34-bit", {1, 1, 0, 0, 1}}, // imported from old pack/unpack | |
490 | {"N10002", Pack_N10002, Unpack_N10002, "HID N10002 34-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au | |
491 | {"C1k35s", Pack_C1k35s, Unpack_C1k35s, "HID Corporate 1000 35-bit standard layout", {1, 1, 0, 0, 1}}, // imported from old pack/unpack | |
492 | {"C15001", Pack_C15001, Unpack_C15001, "HID KeySpan 36-bit", {1, 1, 0, 1, 1}}, // from Proxmark forums | |
493 | {"S12906", Pack_S12906, Unpack_S12906, "HID Simplex 36-bit", {1, 1, 1, 0, 1}}, // from cardinfo.barkweb.com.au | |
494 | {"Sie36", Pack_Sie36, Unpack_Sie36, "HID 36-bit Siemens", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au | |
495 | {"H10320", Pack_H10320, Unpack_H10320, "HID H10320 36-bit BCD", {1, 0, 0, 0, 1}}, // from Proxmark forums | |
496 | {"H10302", Pack_H10302, Unpack_H10302, "HID H10302 37-bit huge ID", {1, 0, 0, 0, 1}}, // from Proxmark forums | |
497 | {"H10304", Pack_H10304, Unpack_H10304, "HID H10304 37-bit", {1, 1, 0, 0, 1}}, // imported from old pack/unpack | |
b5a5fc4d | 498 | {"P10001", Pack_P10001, Unpack_P10001, "HID P10001 Honeywell 40-bit"}, // from cardinfo.barkweb.com.au |
be59094d | 499 | {"C1k48s", Pack_C1k48s, Unpack_C1k48s, "HID Corporate 1000 48-bit standard layout", {1, 1, 0, 0, 1}}, // imported from old pack/unpack |
500 | {NULL, NULL, NULL, NULL, {0, 0, 0, 0, 0}} // Must null terminate array | |
b5a5fc4d | 501 | }; |
502 | ||
503 | void HIDListFormats(){ | |
504 | if (FormatTable[0].Name == NULL) | |
505 | return; | |
506 | int i = 0; | |
507 | PrintAndLog("%-10s %s", "Name", "Description"); | |
508 | PrintAndLog("------------------------------------------------------------"); | |
509 | while (FormatTable[i].Name) | |
510 | { | |
be59094d | 511 | PrintAndLog("%-10s %-30s", FormatTable[i].Name, FormatTable[i].Descrp); |
b5a5fc4d | 512 | ++i; |
513 | } | |
514 | PrintAndLog(""); | |
515 | return; | |
516 | } | |
517 | ||
518 | hidcardformat_t HIDGetCardFormat(int idx){ | |
519 | return FormatTable[idx]; | |
520 | } | |
521 | ||
522 | int HIDFindCardFormat(const char *format) | |
523 | { | |
524 | if (FormatTable[0].Name == NULL) | |
525 | return -1; | |
526 | int i = 0; | |
527 | while (FormatTable[i].Name && strcmp(FormatTable[i].Name, format)) | |
528 | { | |
529 | ++i; | |
530 | } | |
531 | ||
532 | if (FormatTable[i].Name) { | |
533 | return i; | |
534 | } else { | |
535 | return -1; | |
536 | } | |
537 | } | |
538 | ||
539 | bool HIDPack(/* in */int FormatIndex, /* in */hidproxcard_t* card, /* out */hidproxmessage_t* packed){ | |
540 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
541 | if (FormatIndex < 0 || FormatIndex >= (sizeof(FormatTable) / sizeof(FormatTable[0]))) return false; | |
542 | return FormatTable[FormatIndex].Pack(card, packed); | |
543 | } | |
544 | ||
be59094d | 545 | void HIDDisplayUnpackedCard(hidproxcard_t* card, const hidcardformat_t format){ |
546 | PrintAndLog(" Format: %s (%s)", format.Name, format.Descrp); | |
547 | if (format.Fields.hasFacilityCode) | |
548 | PrintAndLog("Facility Code: %d",card->FacilityCode); | |
549 | if (format.Fields.hasCardNumber) | |
550 | PrintAndLog(" Card Number: %d",card->CardNumber); | |
551 | if (format.Fields.hasIssueLevel) | |
552 | PrintAndLog(" Issue Level: %d",card->IssueLevel); | |
553 | if (format.Fields.hasOEMCode) | |
554 | PrintAndLog(" OEM Code: %d",card->OEM); | |
555 | if (format.Fields.hasParity) | |
556 | PrintAndLog(" Parity: %s",card->ParityValid ? "Valid" : "Invalid"); | |
557 | } | |
558 | ||
5d643cc0 | 559 | bool HIDTryUnpack(/* in */hidproxmessage_t* packed, /* in */bool ignoreParity){ |
b5a5fc4d | 560 | if (FormatTable[0].Name == NULL) |
561 | return false; | |
562 | ||
563 | bool result = false; | |
564 | int i = 0; | |
565 | hidproxcard_t card; | |
566 | memset(&card, 0, sizeof(hidproxcard_t)); | |
567 | while (FormatTable[i].Name) | |
568 | { | |
569 | if (FormatTable[i].Unpack(packed, &card)){ | |
be59094d | 570 | if (ignoreParity || !FormatTable[i].Fields.hasParity || card.ParityValid){ |
571 | if (!result) PrintAndLog("--------------------------------------------------"); | |
5d643cc0 | 572 | result = true; |
be59094d | 573 | HIDDisplayUnpackedCard(&card, FormatTable[i]); |
574 | PrintAndLog("--------------------------------------------------"); | |
5d643cc0 | 575 | } |
b5a5fc4d | 576 | } |
577 | ++i; | |
578 | } | |
579 | return result; | |
580 | } |