1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 grauerfuchs
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
7 //-----------------------------------------------------------------------------
8 // HID card format packing/unpacking routines
9 //-----------------------------------------------------------------------------
16 #include "hidcardformats.h"
17 #include "hidcardformatutils.h"
18 #include "parity.h" // for parity
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 if (card
->OEM
> 0) return false; // Not used in this format
27 packed
->Length
= 26; // Set number of bits
28 packed
->bot
|= (card
->CardNumber
& 0xFFFF) << 1;
29 packed
->bot
|= (card
->FacilityCode
& 0xFF) << 17;
30 packed
->bot
|= oddparity32((packed
->bot
>> 1) & 0xFFF) & 1;
31 packed
->bot
|= (evenparity32((packed
->bot
>> 13) & 0xFFF) & 1) << 25;
32 return add_HID_header(packed
);
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;
40 (oddparity32((packed
->bot
>> 1) & 0xFFF) == (packed
->bot
& 1)) &&
41 ((evenparity32((packed
->bot
>> 13) & 0xFFF) & 1) == ((packed
->bot
>> 25) & 1));
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.
49 if (card
->IssueLevel
> 0) return false; // Not used in this format
50 if (card
->OEM
> 0) return false; // Not used in this format
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
);
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});
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.
68 if (card
->IssueLevel
> 0) return false; // Not used in this format
69 if (card
->OEM
> 0) return false; // Not used in this format
71 set_linear_field(packed
, card
->FacilityCode
, 4, 8);
72 set_linear_field(packed
, card
->CardNumber
, 12, 15);
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}))
76 set_bit_by_position(packed
,
77 evenparity32(get_linear_field(packed
, 1, 13))
79 set_bit_by_position(packed
,
80 oddparity32(get_linear_field(packed
, 0, 27))
82 return add_HID_header(packed
);
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);
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)));
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.
100 if (card
->IssueLevel
> 0) return false; // Not used in this format
101 if (card
->OEM
> 0) return false; // Not used in this format
103 set_linear_field(packed
, card
->FacilityCode
, 1, 12);
104 set_linear_field(packed
, card
->CardNumber
, 13, 16);
105 set_bit_by_position(packed
,
106 evenparity32(get_linear_field(packed
, 1, 12))
108 set_bit_by_position(packed
,
109 oddparity32(get_linear_field(packed
, 13, 16))
111 return add_HID_header(packed
);
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);
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)));
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.
127 if (card
->IssueLevel
> 0) return false; // Not used in this format
128 if (card
->OEM
> 0) return false; // Not used in this format
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
);
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);
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.
147 if (card
->IssueLevel
> 0) return false; // Not used in this format
148 if (card
->OEM
> 0) return false; // Not used in this format
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);
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);
154 return add_HID_header(packed
);
156 bool Unpack_D10202(/*in*/hidproxmessage_t
* packed
, /*out*/hidproxcard_t
* card
){
157 memset(card
, 0, sizeof(hidproxcard_t
));
158 if (packed
->Length
!= 33) return false; // Wrong length? Stop here.
159 card
->CardNumber
= get_linear_field(packed
, 8, 24);
160 card
->FacilityCode
= get_linear_field(packed
, 1, 7);
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)));
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.
171 if (card
->IssueLevel
> 0) return false; // Not used in this format
172 if (card
->OEM
> 0) return false; // Not used in this format
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;
177 packed
->mid
|= (evenparity32((packed
->mid
& 0x00000001) ^ (packed
->bot
& 0xFFFE0000)) & 1) << 1;
178 packed
->bot
|= ( oddparity32(packed
->bot
& 0x0001FFFE) & 1);
179 return add_HID_header(packed
);
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.
184 card
->CardNumber
= (packed
->bot
>> 1) & 0xFFFF;
185 card
->FacilityCode
= ((packed
->mid
& 1) << 15) | ((packed
->bot
>> 17) & 0xFF);
187 ((evenparity32((packed
->mid
& 0x00000001) ^ (packed
->bot
& 0xFFFE0000)) & 1) == ((packed
->mid
>> 1) & 1)) &&
188 ((oddparity32(packed
->bot
& 0x0001FFFE) & 1) == ((packed
->bot
& 1)));
191 bool Pack_N10002(/*in*/hidproxcard_t
* card
, /*out*/hidproxmessage_t
* packed
){
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.
195 if (card
->IssueLevel
> 0) return false; // Not used in this format
196 if (card
->OEM
> 0) return false; // Not used in this format
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
);
202 bool Unpack_N10002(/*in*/hidproxmessage_t
* packed
, /*out*/hidproxcard_t
* card
){
203 memset(card
, 0, sizeof(hidproxcard_t
));
204 if (packed
->Length
!= 34) return false; // Wrong length? Stop here.
205 card
->CardNumber
= get_linear_field(packed
, 17, 16);
206 card
->FacilityCode
= get_linear_field(packed
, 9, 8);
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.
214 if (card
->IssueLevel
> 0) return false; // Not used in this format
215 if (card
->OEM
> 0) return false; // Not used in this format
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;
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;
223 return add_HID_header(packed
);
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.
228 card
->CardNumber
= (packed
->bot
>> 1) & 0x000FFFFF;
229 card
->FacilityCode
= ((packed
->mid
& 1) << 11) | ((packed
->bot
>> 21));
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));
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.
241 if (card
->IssueLevel
> 0) return false; // Not used in this format
242 if (card
->OEM
> 0) return false; // Not used in this format
243 packed
->Length
= 36; // Set number of bits
244 // This card is BCD-encoded rather than binary. Set the 4-bit groups independently.
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);
248 set_bit_by_position(packed
, evenparity32(
249 get_nonlinear_field(packed
, 8, (uint8_t[]){0, 4, 8, 12, 16, 20, 24, 28})
251 set_bit_by_position(packed
, oddparity32(
252 get_nonlinear_field(packed
, 8, (uint8_t[]){1, 5, 9, 13, 17, 21, 25, 29})
254 set_bit_by_position(packed
, evenparity32(
255 get_nonlinear_field(packed
, 8, (uint8_t[]){2, 6, 10, 14, 18, 22, 28, 30})
257 set_bit_by_position(packed
, evenparity32(
258 get_nonlinear_field(packed
, 8, (uint8_t[]){3, 7, 11, 15, 19, 23, 29, 31})
260 return add_HID_header(packed
);
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.
266 for (uint32_t idx
= 0; idx
< 8; idx
++){
267 uint64_t val
= get_linear_field(packed
, idx
* 4, 4);
269 // Violation of BCD; Zero and exit.
270 card
->CardNumber
= 0;
273 card
->CardNumber
+= val
* pow(10, 7-idx
);
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})));
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.
289 if (card
->OEM
> 0) return false; // Not used in this format
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);
294 set_bit_by_position(packed
,
295 oddparity32(get_linear_field(packed
, 1, 17))
297 set_bit_by_position(packed
,
298 oddparity32(get_linear_field(packed
, 17, 18))
300 return add_HID_header(packed
);
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.
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);
309 (get_bit_by_position(packed
, 0) == oddparity32(get_linear_field(packed
, 1, 17))) &&
310 (get_bit_by_position(packed
, 35) == oddparity32(get_linear_field(packed
, 17, 18)));
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.
317 if (card
->CardNumber
> 0x0000FFFF) return false; // Can't encode CN.
318 if (card
->IssueLevel
> 0) return false; // Not used in this format
319 if (card
->OEM
> 0) return false; // Not used in this format
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);
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}))
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}))
329 return add_HID_header(packed
);
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.
334 card
->FacilityCode
= get_linear_field(packed
, 1, 18);
335 card
->CardNumber
= get_linear_field(packed
, 19, 16);
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})));
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))
355 set_bit_by_position(packed
,
356 oddparity32(get_linear_field(packed
, 18, 17))
358 return add_HID_header(packed
);
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);
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)));
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.
376 if (card
->IssueLevel
> 0) return false; // Not used in this format
377 if (card
->OEM
> 0) return false; // Not used in this format
378 packed
->Length
= 37; // Set number of bits
379 set_linear_field(packed
, card
->CardNumber
, 1, 35);
380 set_bit_by_position(packed
,
381 evenparity32(get_linear_field(packed
, 1, 18))
383 set_bit_by_position(packed
,
384 oddparity32(get_linear_field(packed
, 18, 18))
386 return add_HID_header(packed
);
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.
391 card
->CardNumber
= get_linear_field(packed
, 1, 35);
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)));
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.
402 if (card
->IssueLevel
> 0) return false; // Not used in this format
403 if (card
->OEM
> 0) return false; // Not used in this format
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;
408 packed
->mid
|= (evenparity32((packed
->mid
& 0x0000000F) ^ (packed
->bot
& 0xFFFC0000)) & 1) << 4;
409 packed
->bot
|= ( oddparity32(packed
->bot
& 0x0007FFFE) & 1);
410 return add_HID_header(packed
);
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.
415 card
->CardNumber
= (packed
->bot
>> 1) & 0x0007FFFF;
416 card
->FacilityCode
= ((packed
->mid
& 0xF) << 12) | ((packed
->bot
>> 20));
418 (evenparity32((packed
->mid
& 0x0000000F) ^ (packed
->bot
& 0xFFFC0000)) == ((packed
->mid
>> 4) & 1)) &&
419 (oddparity32( packed
->bot
& 0x0007FFFE) == (packed
->bot
& 1));
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.
427 if (card
->IssueLevel
> 0) return false; // Not used in this format
428 if (card
->OEM
> 0) return false; // Not used in this format
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);
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)
439 return add_HID_header(packed
);
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.
444 card
->CardNumber
= get_linear_field(packed
, 16, 16);
445 card
->FacilityCode
= get_linear_field(packed
, 4, 12);
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);
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.
459 if (card
->IssueLevel
> 0) return false; // Not used in this format
460 if (card
->OEM
> 0) return false; // Not used in this format
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;
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;
468 return add_HID_header(packed
);
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.
473 card
->CardNumber
= (packed
->bot
>> 1) & 0x007FFFFF;
474 card
->FacilityCode
= ((packed
->mid
& 0x00003FFF) << 8) | ((packed
->bot
>> 24));
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));
482 static const hidcardformat_t FormatTable
[] = {
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
498 {"P10001", Pack_P10001
, Unpack_P10001
, "HID P10001 Honeywell 40-bit"}, // from cardinfo.barkweb.com.au
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
503 void HIDListFormats(){
504 if (FormatTable
[0].Name
== NULL
)
507 PrintAndLog("%-10s %s", "Name", "Description");
508 PrintAndLog("------------------------------------------------------------");
509 while (FormatTable
[i
].Name
)
511 PrintAndLog("%-10s %-30s", FormatTable
[i
].Name
, FormatTable
[i
].Descrp
);
518 hidcardformat_t
HIDGetCardFormat(int idx
){
519 return FormatTable
[idx
];
522 int HIDFindCardFormat(const char *format
)
524 if (FormatTable
[0].Name
== NULL
)
527 while (FormatTable
[i
].Name
&& strcmp(FormatTable
[i
].Name
, format
))
532 if (FormatTable
[i
].Name
) {
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
);
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");
559 bool HIDTryUnpack(/* in */hidproxmessage_t
* packed
, /* in */bool ignoreParity
){
560 if (FormatTable
[0].Name
== NULL
)
566 memset(&card
, 0, sizeof(hidproxcard_t
));
567 while (FormatTable
[i
].Name
)
569 if (FormatTable
[i
].Unpack(packed
, &card
)){
570 if (ignoreParity
|| !FormatTable
[i
].Fields
.hasParity
|| card
.ParityValid
){
571 if (!result
) PrintAndLog("--------------------------------------------------");
573 HIDDisplayUnpackedCard(&card
, FormatTable
[i
]);
574 PrintAndLog("--------------------------------------------------");