]> git.zerfleddert.de Git - proxmark3-svn/blob - client/hidcardformats.c
Update lfops.c
[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 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);
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;
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.
49 if (card->IssueLevel > 0) return false; // Not used in this format
50 if (card->OEM > 0) return false; // Not used in this format
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});
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.
68 if (card->IssueLevel > 0) return false; // Not used in this format
69 if (card->OEM > 0) return false; // Not used in this format
70 packed->Length = 28;
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}))
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);
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);
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.
100 if (card->IssueLevel > 0) return false; // Not used in this format
101 if (card->OEM > 0) return false; // Not used in this format
102 packed->Length = 30;
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))
107 , 0);
108 set_bit_by_position(packed,
109 oddparity32(get_linear_field(packed, 13, 16))
110 , 29);
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);
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.
127 if (card->IssueLevel > 0) return false; // Not used in this format
128 if (card->OEM > 0) return false; // Not used in this format
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);
140 return true;
141 }
142
143 bool Pack_Kastle(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
144 memset(packed, 0, sizeof(hidproxmessage_t));
145 if (card->FacilityCode > 0x00FF) return false; // Can't encode FC.
146 if (card->CardNumber > 0x0000FFFF) return false; // Can't encode CN.
147 if (card->IssueLevel > 0x001F) return false; // IL is only 5 bits.
148 if (card->OEM > 0) return false; // Not used in this format
149 packed->Length = 32; // Set number of bits
150 set_bit_by_position(packed, 1, 1); // Always 1
151 set_linear_field(packed, card->IssueLevel, 2, 5);
152 set_linear_field(packed, card->FacilityCode, 7, 8);
153 set_linear_field(packed, card->CardNumber, 15, 16);
154 set_bit_by_position(packed, evenparity32(get_linear_field(packed, 1, 16)), 0);
155 set_bit_by_position(packed, oddparity32(get_linear_field(packed, 14, 17)), 31);
156 return add_HID_header(packed);
157 }
158 bool Unpack_Kastle(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
159 memset(card, 0, sizeof(hidproxcard_t));
160 if (packed->Length != 32) return false; // Wrong length? Stop here.
161 if (get_bit_by_position(packed, 1) != 1) return false; // Always 1 in this format
162 card->IssueLevel = get_linear_field(packed, 2, 5);
163 card->FacilityCode = get_linear_field(packed, 7, 8);
164 card->CardNumber = get_linear_field(packed, 15, 16);
165 card->ParityValid =
166 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 16))) &&
167 (get_bit_by_position(packed, 31) == oddparity32(get_linear_field(packed, 14, 17)));
168 return true;
169 }
170
171 bool Pack_D10202(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
172 memset(packed, 0, sizeof(hidproxmessage_t));
173 if (card->FacilityCode > 0x007F) return false; // Can't encode FC.
174 if (card->CardNumber > 0x00FFFFFF) return false; // Can't encode CN.
175 if (card->IssueLevel > 0) return false; // Not used in this format
176 if (card->OEM > 0) return false; // Not used in this format
177 packed->Length = 33; // Set number of bits
178 set_linear_field(packed, card->FacilityCode, 1, 7);
179 set_linear_field(packed, card->CardNumber, 8, 24);
180 set_bit_by_position(packed, evenparity32(get_linear_field(packed, 1, 16)), 0);
181 set_bit_by_position(packed, oddparity32(get_linear_field(packed, 16, 16)), 32);
182 return add_HID_header(packed);
183 }
184 bool Unpack_D10202(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
185 memset(card, 0, sizeof(hidproxcard_t));
186 if (packed->Length != 33) return false; // Wrong length? Stop here.
187 card->CardNumber = get_linear_field(packed, 8, 24);
188 card->FacilityCode = get_linear_field(packed, 1, 7);
189 card->ParityValid =
190 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 16))) &&
191 (get_bit_by_position(packed, 32) == oddparity32(get_linear_field(packed, 16, 16)));
192 return true;
193 }
194
195 bool Pack_H10306(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
196 memset(packed, 0, sizeof(hidproxmessage_t));
197 if (card->FacilityCode > 0xFFFF) return false; // Can't encode FC.
198 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
199 if (card->IssueLevel > 0) return false; // Not used in this format
200 if (card->OEM > 0) return false; // Not used in this format
201 packed->Length = 34; // Set number of bits
202 packed->bot |= (card->CardNumber & 0xFFFF) << 1;
203 packed->bot |= (card->FacilityCode & 0x7FFF) << 17;
204 packed->mid |= (card->FacilityCode & 0x8000) >> 15;
205 packed->mid |= (evenparity32((packed->mid & 0x00000001) ^ (packed->bot & 0xFFFE0000)) & 1) << 1;
206 packed->bot |= ( oddparity32(packed->bot & 0x0001FFFE) & 1);
207 return add_HID_header(packed);
208 }
209 bool Unpack_H10306(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
210 memset(card, 0, sizeof(hidproxcard_t));
211 if (packed->Length != 34) return false; // Wrong length? Stop here.
212 card->CardNumber = (packed->bot >> 1) & 0xFFFF;
213 card->FacilityCode = ((packed->mid & 1) << 15) | ((packed->bot >> 17) & 0xFF);
214 card->ParityValid =
215 ((evenparity32((packed->mid & 0x00000001) ^ (packed->bot & 0xFFFE0000)) & 1) == ((packed->mid >> 1) & 1)) &&
216 ((oddparity32(packed->bot & 0x0001FFFE) & 1) == ((packed->bot & 1)));
217 return true;
218 }
219 bool Pack_N10002(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
220 memset(packed, 0, sizeof(hidproxmessage_t));
221 if (card->FacilityCode > 0xFF) return false; // Can't encode FC.
222 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
223 if (card->IssueLevel > 0) return false; // Not used in this format
224 if (card->OEM > 0) return false; // Not used in this format
225 packed->Length = 34; // Set number of bits
226 set_linear_field(packed, card->FacilityCode, 9, 8);
227 set_linear_field(packed, card->CardNumber, 17, 16);
228 return add_HID_header(packed);
229 }
230 bool Unpack_N10002(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
231 memset(card, 0, sizeof(hidproxcard_t));
232 if (packed->Length != 34) return false; // Wrong length? Stop here.
233 card->CardNumber = get_linear_field(packed, 17, 16);
234 card->FacilityCode = get_linear_field(packed, 9, 8);
235 return true;
236 }
237
238 bool Pack_C1k35s(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
239 memset(packed, 0, sizeof(hidproxmessage_t));
240 if (card->FacilityCode > 0xFFF) return false; // Can't encode FC.
241 if (card->CardNumber > 0xFFFFF) return false; // Can't encode CN.
242 if (card->IssueLevel > 0) return false; // Not used in this format
243 if (card->OEM > 0) return false; // Not used in this format
244 packed->Length = 35; // Set number of bits
245 packed->bot |= (card->CardNumber & 0x000FFFFF) << 1;
246 packed->bot |= (card->FacilityCode & 0x000007FF) << 21;
247 packed->mid |= (card->FacilityCode & 0x00000800) >> 11;
248 packed->mid |= (evenparity32((packed->mid & 0x00000001) ^ (packed->bot & 0xB6DB6DB6)) & 1) << 1;
249 packed->bot |= ( oddparity32((packed->mid & 0x00000003) ^ (packed->bot & 0x6DB6DB6C)) & 1);
250 packed->mid |= ( oddparity32((packed->mid & 0x00000003) ^ (packed->bot & 0xFFFFFFFF)) & 1) << 2;
251 return add_HID_header(packed);
252 }
253 bool Unpack_C1k35s(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
254 memset(card, 0, sizeof(hidproxcard_t));
255 if (packed->Length != 35) return false; // Wrong length? Stop here.
256 card->CardNumber = (packed->bot >> 1) & 0x000FFFFF;
257 card->FacilityCode = ((packed->mid & 1) << 11) | ((packed->bot >> 21));
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 if (card->OEM > 0) return false; // Not used in this format
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 set_bit_by_position(packed, evenparity32(
277 get_nonlinear_field(packed, 8, (uint8_t[]){0, 4, 8, 12, 16, 20, 24, 28})
278 ), 32);
279 set_bit_by_position(packed, oddparity32(
280 get_nonlinear_field(packed, 8, (uint8_t[]){1, 5, 9, 13, 17, 21, 25, 29})
281 ), 33);
282 set_bit_by_position(packed, evenparity32(
283 get_nonlinear_field(packed, 8, (uint8_t[]){2, 6, 10, 14, 18, 22, 28, 30})
284 ), 34);
285 set_bit_by_position(packed, evenparity32(
286 get_nonlinear_field(packed, 8, (uint8_t[]){3, 7, 11, 15, 19, 23, 29, 31})
287 ), 35);
288 return add_HID_header(packed);
289 }
290 bool Unpack_H10320(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
291 memset(card, 0, sizeof(hidproxcard_t));
292 if (packed->Length != 36) return false; // Wrong length? Stop here.
293 // This card is BCD-encoded rather than binary. Get the 4-bit groups independently.
294 for (uint32_t idx = 0; idx < 8; idx++){
295 uint64_t val = get_linear_field(packed, idx * 4, 4);
296 if (val > 9){
297 // Violation of BCD; Zero and exit.
298 card->CardNumber = 0;
299 return false;
300 } else {
301 card->CardNumber += val * pow(10, 7-idx);
302 }
303 }
304 card->ParityValid =
305 (get_bit_by_position(packed, 32) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]){0, 4, 8, 12, 16, 20, 24, 28}))) &&
306 (get_bit_by_position(packed, 33) == oddparity32(get_nonlinear_field(packed, 8, (uint8_t[]){1, 5, 9, 13, 17, 21, 25, 29}))) &&
307 (get_bit_by_position(packed, 34) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]){2, 6, 10, 14, 18, 22, 28, 30}))) &&
308 (get_bit_by_position(packed, 35) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]){3, 7, 11, 15, 19, 23, 29, 31})));
309 return true;
310 }
311
312 bool Pack_S12906(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
313 memset(packed, 0, sizeof(hidproxmessage_t));
314 if (card->FacilityCode > 0xFF) return false; // Can't encode FC.
315 if (card->IssueLevel > 0x03) return false; // Can't encode IL.
316 if (card->CardNumber > 0x00FFFFFF) return false; // Can't encode CN.
317 if (card->OEM > 0) return false; // Not used in this format
318 packed->Length = 36; // Set number of bits
319 set_linear_field(packed, card->FacilityCode, 1, 8);
320 set_linear_field(packed, card->IssueLevel, 9, 2);
321 set_linear_field(packed, card->CardNumber, 11, 24);
322 set_bit_by_position(packed,
323 oddparity32(get_linear_field(packed, 1, 17))
324 , 0);
325 set_bit_by_position(packed,
326 oddparity32(get_linear_field(packed, 17, 18))
327 , 35);
328 return add_HID_header(packed);
329 }
330 bool Unpack_S12906(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
331 memset(card, 0, sizeof(hidproxcard_t));
332 if (packed->Length != 36) return false; // Wrong length? Stop here.
333 card->FacilityCode = get_linear_field(packed, 1, 8);
334 card->IssueLevel = get_linear_field(packed, 9, 2);
335 card->CardNumber = get_linear_field(packed, 11, 24);
336 card->ParityValid =
337 (get_bit_by_position(packed, 0) == oddparity32(get_linear_field(packed, 1, 17))) &&
338 (get_bit_by_position(packed, 35) == oddparity32(get_linear_field(packed, 17, 18)));
339 return true;
340 }
341
342 bool Pack_Sie36(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
343 memset(packed, 0, sizeof(hidproxmessage_t));
344 if (card->FacilityCode > 0x0003FFFF) 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 > 0) return false; // Not used in this format
348 packed->Length = 36; // Set number of bits
349 set_linear_field(packed, card->FacilityCode, 1, 18);
350 set_linear_field(packed, card->CardNumber, 19, 16);
351 set_bit_by_position(packed,
352 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}))
353 , 0);
354 set_bit_by_position(packed,
355 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}))
356 , 35);
357 return add_HID_header(packed);
358 }
359 bool Unpack_Sie36(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
360 memset(card, 0, sizeof(hidproxcard_t));
361 if (packed->Length != 36) return false; // Wrong length? Stop here.
362 card->FacilityCode = get_linear_field(packed, 1, 18);
363 card->CardNumber = get_linear_field(packed, 19, 16);
364 card->ParityValid =
365 (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}))) &&
366 (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})));
367 return true;
368 }
369
370 bool Pack_C15001(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
371 memset(packed, 0, sizeof(hidproxmessage_t));
372 if (card->FacilityCode > 0x000000FF) return false; // Can't encode FC.
373 if (card->CardNumber > 0x0000FFFF) return false; // Can't encode CN.
374 if (card->IssueLevel > 0) return false; // Not used in this format
375 if (card->OEM > 0x000003FF) return false; // Can't encode OEM.
376 packed->Length = 36; // Set number of bits
377 set_linear_field(packed, card->OEM, 1, 10);
378 set_linear_field(packed, card->FacilityCode, 11, 8);
379 set_linear_field(packed, card->CardNumber, 19, 16);
380 set_bit_by_position(packed,
381 evenparity32(get_linear_field(packed, 1, 17))
382 , 0);
383 set_bit_by_position(packed,
384 oddparity32(get_linear_field(packed, 18, 17))
385 , 35);
386 return add_HID_header(packed);
387 }
388 bool Unpack_C15001(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
389 memset(card, 0, sizeof(hidproxcard_t));
390 if (packed->Length != 36) return false; // Wrong length? Stop here.
391 card->OEM = get_linear_field(packed, 1, 10);
392 card->FacilityCode = get_linear_field(packed, 11, 8);
393 card->CardNumber = get_linear_field(packed, 19, 16);
394 card->ParityValid =
395 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 17))) &&
396 (get_bit_by_position(packed, 35) == oddparity32(get_linear_field(packed, 18, 17)));
397 return true;
398 }
399
400 bool Pack_H10302(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
401 memset(packed, 0, sizeof(hidproxmessage_t));
402 if (card->FacilityCode > 0) return false; // Can't encode FC. (none in this format)
403 if (card->CardNumber > 0x00000007FFFFFFFF) return false; // Can't encode CN.
404 if (card->IssueLevel > 0) return false; // Not used in this format
405 if (card->OEM > 0) return false; // Not used in this format
406 packed->Length = 37; // Set number of bits
407 set_linear_field(packed, card->CardNumber, 1, 35);
408 set_bit_by_position(packed,
409 evenparity32(get_linear_field(packed, 1, 18))
410 , 0);
411 set_bit_by_position(packed,
412 oddparity32(get_linear_field(packed, 18, 18))
413 , 36);
414 return add_HID_header(packed);
415 }
416 bool Unpack_H10302(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
417 memset(card, 0, sizeof(hidproxcard_t));
418 if (packed->Length != 37) return false; // Wrong length? Stop here.
419 card->CardNumber = get_linear_field(packed, 1, 35);
420 card->ParityValid =
421 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 18))) &&
422 (get_bit_by_position(packed, 36) == oddparity32(get_linear_field(packed, 18, 18)));
423 return true;
424 }
425
426 bool Pack_H10304(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
427 memset(packed, 0, sizeof(hidproxmessage_t));
428 if (card->FacilityCode > 0x0000FFFF) return false; // Can't encode FC.
429 if (card->CardNumber > 0x0007FFFF) return false; // Can't encode CN.
430 if (card->IssueLevel > 0) return false; // Not used in this format
431 if (card->OEM > 0) return false; // Not used in this format
432 packed->Length = 37; // Set number of bits
433 packed->bot |= (card->CardNumber & 0x0007FFFF) << 1;
434 packed->bot |= (card->FacilityCode & 0x00000FFF) << 20;
435 packed->mid |= (card->FacilityCode & 0x0000F000) >> 12;
436 packed->mid |= (evenparity32((packed->mid & 0x0000000F) ^ (packed->bot & 0xFFFC0000)) & 1) << 4;
437 packed->bot |= ( oddparity32(packed->bot & 0x0007FFFE) & 1);
438 return add_HID_header(packed);
439 }
440 bool Unpack_H10304(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
441 memset(card, 0, sizeof(hidproxcard_t));
442 if (packed->Length != 37) return false; // Wrong length? Stop here.
443 card->CardNumber = (packed->bot >> 1) & 0x0007FFFF;
444 card->FacilityCode = ((packed->mid & 0xF) << 12) | ((packed->bot >> 20));
445 card->ParityValid =
446 (evenparity32((packed->mid & 0x0000000F) ^ (packed->bot & 0xFFFC0000)) == ((packed->mid >> 4) & 1)) &&
447 (oddparity32( packed->bot & 0x0007FFFE) == (packed->bot & 1));
448 return true;
449 }
450
451 bool Pack_P10001(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
452 memset(packed, 0, sizeof(hidproxmessage_t));
453 if (card->FacilityCode > 0xFFF) return false; // Can't encode FC.
454 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
455 if (card->IssueLevel > 0) return false; // Not used in this format
456 if (card->OEM > 0) return false; // Not used in this format
457 packed->Length = 40; // Set number of bits
458 set_linear_field(packed, 0xF, 0, 4);
459 set_linear_field(packed, card->FacilityCode, 4, 12);
460 set_linear_field(packed, card->CardNumber, 16, 16);
461 set_linear_field(packed,
462 get_linear_field(packed, 0, 8) ^
463 get_linear_field(packed, 8, 8) ^
464 get_linear_field(packed, 16, 8) ^
465 get_linear_field(packed, 24, 8)
466 , 32, 8);
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 card->CardNumber = get_linear_field(packed, 16, 16);
473 card->FacilityCode = get_linear_field(packed, 4, 12);
474 card->ParityValid = (
475 get_linear_field(packed, 0, 8) ^
476 get_linear_field(packed, 8, 8) ^
477 get_linear_field(packed, 16, 8) ^
478 get_linear_field(packed, 24, 8)
479 ) == get_linear_field(packed, 32, 8);
480 return true;
481 }
482
483 bool Pack_C1k48s(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){
484 memset(packed, 0, sizeof(hidproxmessage_t));
485 if (card->FacilityCode > 0x003FFFFF) return false; // Can't encode FC.
486 if (card->CardNumber > 0x007FFFFF) return false; // Can't encode CN.
487 if (card->IssueLevel > 0) return false; // Not used in this format
488 if (card->OEM > 0) return false; // Not used in this format
489 packed->Length = 48; // Set number of bits
490 packed->bot |= (card->CardNumber & 0x007FFFFF) << 1;
491 packed->bot |= (card->FacilityCode & 0x000000FF) << 24;
492 packed->mid |= (card->FacilityCode & 0x003FFF00) >> 8;
493 packed->mid |= (evenparity32((packed->mid & 0x00001B6D) ^ (packed->bot & 0xB6DB6DB6)) & 1) << 14;
494 packed->bot |= ( oddparity32((packed->mid & 0x000036DB) ^ (packed->bot & 0x6DB6DB6C)) & 1);
495 packed->mid |= ( oddparity32((packed->mid & 0x00007FFF) ^ (packed->bot & 0xFFFFFFFF)) & 1) << 15;
496 return add_HID_header(packed);
497 }
498 bool Unpack_C1k48s(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){
499 memset(card, 0, sizeof(hidproxcard_t));
500 if (packed->Length != 48) return false; // Wrong length? Stop here.
501 card->CardNumber = (packed->bot >> 1) & 0x007FFFFF;
502 card->FacilityCode = ((packed->mid & 0x00003FFF) << 8) | ((packed->bot >> 24));
503 card->ParityValid =
504 (evenparity32((packed->mid & 0x00001B6D) ^ (packed->bot & 0xB6DB6DB6)) == ((packed->mid >> 14) & 1)) &&
505 ( oddparity32((packed->mid & 0x000036DB) ^ (packed->bot & 0x6DB6DB6C)) == ((packed->bot >> 0) & 1)) &&
506 ( oddparity32((packed->mid & 0x00007FFF) ^ (packed->bot & 0xFFFFFFFF)) == ((packed->mid >> 15) & 1));
507 return true;
508 }
509
510 static const hidcardformat_t FormatTable[] = {
511 {"H10301", Pack_H10301, Unpack_H10301, "HID H10301 26-bit", {1, 1, 0, 0, 1}}, // imported from old pack/unpack
512 {"Tecom27", Pack_Tecom27, Unpack_Tecom27, "Tecom 27-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
513 {"2804W", Pack_2804W, Unpack_2804W, "2804 Wiegand", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
514 {"ATSW30", Pack_ATSW30, Unpack_ATSW30, "ATS Wiegand 30-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
515 {"ADT31", Pack_ADT31, Unpack_ADT31, "HID ADT 31-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
516 {"Kastle", Pack_Kastle, Unpack_Kastle, "Kastle 32-bit", {1, 1, 1, 0, 1}}, // from @xilni; PR #23 on RfidResearchGroup/proxmark3
517 {"D10202", Pack_D10202, Unpack_D10202, "HID D10202 33-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
518 {"H10306", Pack_H10306, Unpack_H10306, "HID H10306 34-bit", {1, 1, 0, 0, 1}}, // imported from old pack/unpack
519 {"N10002", Pack_N10002, Unpack_N10002, "HID N10002 34-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
520 {"C1k35s", Pack_C1k35s, Unpack_C1k35s, "HID Corporate 1000 35-bit standard layout", {1, 1, 0, 0, 1}}, // imported from old pack/unpack
521 {"C15001", Pack_C15001, Unpack_C15001, "HID KeySpan 36-bit", {1, 1, 0, 1, 1}}, // from Proxmark forums
522 {"S12906", Pack_S12906, Unpack_S12906, "HID Simplex 36-bit", {1, 1, 1, 0, 1}}, // from cardinfo.barkweb.com.au
523 {"Sie36", Pack_Sie36, Unpack_Sie36, "HID 36-bit Siemens", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
524 {"H10320", Pack_H10320, Unpack_H10320, "HID H10320 36-bit BCD", {1, 0, 0, 0, 1}}, // from Proxmark forums
525 {"H10302", Pack_H10302, Unpack_H10302, "HID H10302 37-bit huge ID", {1, 0, 0, 0, 1}}, // from Proxmark forums
526 {"H10304", Pack_H10304, Unpack_H10304, "HID H10304 37-bit", {1, 1, 0, 0, 1}}, // imported from old pack/unpack
527 {"P10001", Pack_P10001, Unpack_P10001, "HID P10001 Honeywell 40-bit"}, // from cardinfo.barkweb.com.au
528 {"C1k48s", Pack_C1k48s, Unpack_C1k48s, "HID Corporate 1000 48-bit standard layout", {1, 1, 0, 0, 1}}, // imported from old pack/unpack
529 {NULL, NULL, NULL, NULL, {0, 0, 0, 0, 0}} // Must null terminate array
530 };
531
532 void HIDListFormats(){
533 if (FormatTable[0].Name == NULL)
534 return;
535 int i = 0;
536 PrintAndLog("%-10s %s", "Name", "Description");
537 PrintAndLog("------------------------------------------------------------");
538 while (FormatTable[i].Name)
539 {
540 PrintAndLog("%-10s %-30s", FormatTable[i].Name, FormatTable[i].Descrp);
541 ++i;
542 }
543 PrintAndLog("");
544 return;
545 }
546
547 hidcardformat_t HIDGetCardFormat(int idx){
548 return FormatTable[idx];
549 }
550
551 int HIDFindCardFormat(const char *format)
552 {
553 if (FormatTable[0].Name == NULL)
554 return -1;
555 int i = 0;
556 while (FormatTable[i].Name && strcmp(FormatTable[i].Name, format))
557 {
558 ++i;
559 }
560
561 if (FormatTable[i].Name) {
562 return i;
563 } else {
564 return -1;
565 }
566 }
567
568 bool HIDPack(/* in */int FormatIndex, /* in */hidproxcard_t* card, /* out */hidproxmessage_t* packed){
569 memset(packed, 0, sizeof(hidproxmessage_t));
570 if (FormatIndex < 0 || FormatIndex >= (sizeof(FormatTable) / sizeof(FormatTable[0]))) return false;
571 return FormatTable[FormatIndex].Pack(card, packed);
572 }
573
574 void HIDDisplayUnpackedCard(hidproxcard_t* card, const hidcardformat_t format){
575 PrintAndLog(" Format: %s (%s)", format.Name, format.Descrp);
576 if (format.Fields.hasFacilityCode)
577 PrintAndLog("Facility Code: %d",card->FacilityCode);
578 if (format.Fields.hasCardNumber)
579 PrintAndLog(" Card Number: %d",card->CardNumber);
580 if (format.Fields.hasIssueLevel)
581 PrintAndLog(" Issue Level: %d",card->IssueLevel);
582 if (format.Fields.hasOEMCode)
583 PrintAndLog(" OEM Code: %d",card->OEM);
584 if (format.Fields.hasParity)
585 PrintAndLog(" Parity: %s",card->ParityValid ? "Valid" : "Invalid");
586 }
587
588 bool HIDTryUnpack(/* in */hidproxmessage_t* packed, /* in */bool ignoreParity){
589 if (FormatTable[0].Name == NULL)
590 return false;
591
592 bool result = false;
593 int i = 0;
594 hidproxcard_t card;
595 memset(&card, 0, sizeof(hidproxcard_t));
596 while (FormatTable[i].Name)
597 {
598 if (FormatTable[i].Unpack(packed, &card)){
599 if (ignoreParity || !FormatTable[i].Fields.hasParity || card.ParityValid){
600 if (!result) PrintAndLog("--------------------------------------------------");
601 result = true;
602 HIDDisplayUnpackedCard(&card, FormatTable[i]);
603 PrintAndLog("--------------------------------------------------");
604 }
605 }
606 ++i;
607 }
608 return result;
609 }
Impressum, Datenschutz