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