]>
Commit | Line | Data |
---|---|---|
b62a5a84 | 1 | // Merlok, 2011, 2012\r |
f397b5cc M |
2 | // people from mifare@nethemba.com, 2010\r |
3 | //\r | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or,\r | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of\r | |
6 | // the license.\r | |
7 | //-----------------------------------------------------------------------------\r | |
55acbb2a | 8 | // mifare commands\r |
f397b5cc M |
9 | //-----------------------------------------------------------------------------\r |
10 | \r | |
4cb4b588 | 11 | #include "mifarehost.h"\r |
12 | \r | |
f397b5cc | 13 | #include <stdio.h>\r |
7906cb41 | 14 | #include <stdlib.h>\r |
31b6e9af | 15 | #include <string.h>\r |
9492e0b0 | 16 | #include <pthread.h>\r |
7cb8516c | 17 | \r |
33443e7c | 18 | #include "crapto1/crapto1.h"\r |
902cb3c0 | 19 | #include "proxmark3.h"\r |
7cb8516c | 20 | #include "usb_cmd.h"\r |
21 | #include "cmdmain.h"\r | |
22 | #include "ui.h"\r | |
a37725fa | 23 | #include "parity.h"\r |
7cb8516c | 24 | #include "util.h"\r |
25 | #include "iso14443crc.h"\r | |
7cb8516c | 26 | \r |
7906cb41 F |
27 | #include "mifare.h"\r |
28 | \r | |
7cb8516c | 29 | // mifare tracer flags used in mfTraceDecode()\r |
30 | #define TRACE_IDLE 0x00\r | |
31 | #define TRACE_AUTH1 0x01\r | |
32 | #define TRACE_AUTH2 0x02\r | |
33 | #define TRACE_AUTH_OK 0x03\r | |
34 | #define TRACE_READ_DATA 0x04\r | |
35 | #define TRACE_WRITE_OK 0x05\r | |
36 | #define TRACE_WRITE_DATA 0x06\r | |
37 | #define TRACE_ERROR 0xFF\r | |
38 | \r | |
f397b5cc | 39 | \r |
7779d73c | 40 | static int compare_uint64(const void *a, const void *b) {\r |
41 | // didn't work: (the result is truncated to 32 bits)\r | |
42 | //return (*(int64_t*)b - *(int64_t*)a);\r | |
43 | \r | |
44 | // better:\r | |
45 | if (*(uint64_t*)b == *(uint64_t*)a) return 0;\r | |
46 | else if (*(uint64_t*)b < *(uint64_t*)a) return 1;\r | |
47 | else return -1;\r | |
48 | }\r | |
49 | \r | |
50 | \r | |
51 | // create the intersection (common members) of two sorted lists. Lists are terminated by -1. Result will be in list1. Number of elements is returned.\r | |
52 | static uint32_t intersection(uint64_t *list1, uint64_t *list2)\r | |
53 | {\r | |
54 | if (list1 == NULL || list2 == NULL) {\r | |
55 | return 0;\r | |
56 | }\r | |
57 | uint64_t *p1, *p2, *p3;\r | |
7906cb41 | 58 | p1 = p3 = list1;\r |
7779d73c | 59 | p2 = list2;\r |
60 | \r | |
61 | while ( *p1 != -1 && *p2 != -1 ) {\r | |
62 | if (compare_uint64(p1, p2) == 0) {\r | |
63 | *p3++ = *p1++;\r | |
64 | p2++;\r | |
65 | }\r | |
66 | else {\r | |
67 | while (compare_uint64(p1, p2) < 0) ++p1;\r | |
68 | while (compare_uint64(p1, p2) > 0) ++p2;\r | |
69 | }\r | |
70 | }\r | |
71 | *p3 = -1;\r | |
72 | return p3 - list1;\r | |
73 | }\r | |
74 | \r | |
75 | \r | |
76 | // Darkside attack (hf mf mifare)\r | |
664e132f | 77 | static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint32_t ar, uint64_t par_info, uint64_t ks_info, uint64_t **keys) {\r |
7779d73c | 78 | struct Crypto1State *states;\r |
664e132f | 79 | uint32_t i, pos;\r |
7779d73c | 80 | uint8_t bt, ks3x[8], par[8][8];\r |
81 | uint64_t key_recovered;\r | |
664e132f | 82 | uint64_t *keylist;\r |
7779d73c | 83 | \r |
84 | // Reset the last three significant bits of the reader nonce\r | |
85 | nr &= 0xffffff1f;\r | |
86 | \r | |
87 | for (pos=0; pos<8; pos++) {\r | |
88 | ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f;\r | |
89 | bt = (par_info >> (pos*8)) & 0xff;\r | |
90 | for (i=0; i<8; i++) {\r | |
91 | par[7-pos][i] = (bt >> i) & 0x01;\r | |
92 | }\r | |
93 | }\r | |
94 | \r | |
664e132f | 95 | states = lfsr_common_prefix(nr, ar, ks3x, par, (par_info == 0));\r |
7779d73c | 96 | \r |
97 | if (states == NULL) {\r | |
98 | *keys = NULL;\r | |
99 | return 0;\r | |
100 | }\r | |
101 | \r | |
102 | keylist = (uint64_t*)states;\r | |
7906cb41 | 103 | \r |
7779d73c | 104 | for (i = 0; keylist[i]; i++) {\r |
105 | lfsr_rollback_word(states+i, uid^nt, 0);\r | |
106 | crypto1_get_lfsr(states+i, &key_recovered);\r | |
107 | keylist[i] = key_recovered;\r | |
108 | }\r | |
109 | keylist[i] = -1;\r | |
110 | \r | |
111 | *keys = keylist;\r | |
112 | return i;\r | |
113 | }\r | |
114 | \r | |
115 | \r | |
116 | int mfDarkside(uint64_t *key)\r | |
117 | {\r | |
118 | uint32_t uid = 0;\r | |
664e132f | 119 | uint32_t nt = 0, nr = 0, ar = 0;\r |
7779d73c | 120 | uint64_t par_list = 0, ks_list = 0;\r |
121 | uint64_t *keylist = NULL, *last_keylist = NULL;\r | |
122 | uint32_t keycount = 0;\r | |
123 | int16_t isOK = 0;\r | |
124 | \r | |
125 | UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};\r | |
126 | \r | |
127 | // message\r | |
128 | printf("-------------------------------------------------------------------------\n");\r | |
129 | printf("Executing command. Expected execution time: 25sec on average\n");\r | |
130 | printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");\r | |
131 | printf("-------------------------------------------------------------------------\n");\r | |
132 | \r | |
7906cb41 | 133 | \r |
7779d73c | 134 | while (true) {\r |
135 | clearCommandBuffer();\r | |
136 | SendCommand(&c);\r | |
7906cb41 | 137 | \r |
7779d73c | 138 | //flush queue\r |
139 | while (ukbhit()) {\r | |
140 | int c = getchar(); (void) c;\r | |
141 | }\r | |
7906cb41 | 142 | \r |
7779d73c | 143 | // wait cycle\r |
144 | while (true) {\r | |
145 | printf(".");\r | |
146 | fflush(stdout);\r | |
147 | if (ukbhit()) {\r | |
148 | return -5;\r | |
149 | break;\r | |
150 | }\r | |
7906cb41 | 151 | \r |
7779d73c | 152 | UsbCommand resp;\r |
153 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {\r | |
154 | isOK = resp.arg[0];\r | |
155 | if (isOK < 0) {\r | |
156 | return isOK;\r | |
157 | }\r | |
158 | uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);\r | |
159 | nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);\r | |
160 | par_list = bytes_to_num(resp.d.asBytes + 8, 8);\r | |
161 | ks_list = bytes_to_num(resp.d.asBytes + 16, 8);\r | |
664e132f | 162 | nr = (uint32_t)bytes_to_num(resp.d.asBytes + 24, 4);\r |
163 | ar = (uint32_t)bytes_to_num(resp.d.asBytes + 28, 4);\r | |
7779d73c | 164 | break;\r |
165 | }\r | |
7906cb41 | 166 | }\r |
7779d73c | 167 | \r |
168 | if (par_list == 0 && c.arg[0] == true) {\r | |
169 | PrintAndLog("Parity is all zero. Most likely this card sends NACK on every failed authentication.");\r | |
7779d73c | 170 | }\r |
171 | c.arg[0] = false;\r | |
172 | \r | |
664e132f | 173 | keycount = nonce2key(uid, nt, nr, ar, par_list, ks_list, &keylist);\r |
7779d73c | 174 | \r |
175 | if (keycount == 0) {\r | |
7906cb41 | 176 | PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt);\r |
7779d73c | 177 | PrintAndLog("This is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");\r |
178 | continue;\r | |
179 | }\r | |
180 | \r | |
d86ee73d | 181 | if (par_list == 0) {\r |
182 | qsort(keylist, keycount, sizeof(*keylist), compare_uint64);\r | |
183 | keycount = intersection(last_keylist, keylist);\r | |
184 | if (keycount == 0) {\r | |
185 | free(last_keylist);\r | |
186 | last_keylist = keylist;\r | |
187 | continue;\r | |
188 | }\r | |
7779d73c | 189 | }\r |
190 | \r | |
191 | if (keycount > 1) {\r | |
192 | PrintAndLog("Found %u possible keys. Trying to authenticate with each of them ...\n", keycount);\r | |
193 | } else {\r | |
194 | PrintAndLog("Found a possible key. Trying to authenticate...\n");\r | |
7906cb41 | 195 | }\r |
7779d73c | 196 | \r |
197 | *key = -1;\r | |
198 | uint8_t keyBlock[USB_CMD_DATA_SIZE];\r | |
199 | int max_keys = USB_CMD_DATA_SIZE/6;\r | |
200 | for (int i = 0; i < keycount; i += max_keys) {\r | |
201 | int size = keycount - i > max_keys ? max_keys : keycount - i;\r | |
202 | for (int j = 0; j < size; j++) {\r | |
d86ee73d | 203 | if (par_list == 0) {\r |
02515642 | 204 | num_to_bytes(last_keylist[i*max_keys + j], 6, keyBlock+(j*6));\r |
d86ee73d | 205 | } else {\r |
02515642 | 206 | num_to_bytes(keylist[i*max_keys + j], 6, keyBlock+(j*6));\r |
7779d73c | 207 | }\r |
208 | }\r | |
209 | if (!mfCheckKeys(0, 0, false, size, keyBlock, key)) {\r | |
210 | break;\r | |
211 | }\r | |
7906cb41 F |
212 | }\r |
213 | \r | |
7779d73c | 214 | if (*key != -1) {\r |
215 | free(last_keylist);\r | |
216 | free(keylist);\r | |
217 | break;\r | |
218 | } else {\r | |
219 | PrintAndLog("Authentication failed. Trying again...");\r | |
220 | free(last_keylist);\r | |
221 | last_keylist = keylist;\r | |
222 | }\r | |
223 | }\r | |
7906cb41 | 224 | \r |
7779d73c | 225 | return 0;\r |
226 | }\r | |
227 | \r | |
228 | \r | |
7cb8516c | 229 | int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){\r |
230 | \r | |
84ca27b3 | 231 | *key = -1;\r |
7cb8516c | 232 | \r |
275d9e61 | 233 | UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType & 0xff) << 8)), clear_trace, keycnt}}; \r |
7cb8516c | 234 | memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r |
235 | SendCommand(&c);\r | |
236 | \r | |
237 | UsbCommand resp;\r | |
275d9e61 | 238 | if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1; \r |
7cb8516c | 239 | if ((resp.arg[0] & 0xff) != 0x01) return 2;\r |
240 | *key = bytes_to_num(resp.d.asBytes, 6);\r | |
241 | return 0;\r | |
242 | }\r | |
243 | \r | |
275d9e61 OM |
244 | int mfCheckKeysSec(uint8_t sectorCnt, uint8_t keyType, uint8_t timeout14a, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, sector_t * e_sector){\r |
245 | \r | |
246 | uint8_t keyPtr = 0;\r | |
247 | \r | |
248 | if (e_sector == NULL)\r | |
249 | return -1;\r | |
250 | \r | |
251 | UsbCommand c = {CMD_MIFARE_CHKKEYS, {((sectorCnt & 0xff) | ((keyType & 0xff) << 8)), (clear_trace | 0x02)|((timeout14a & 0xff) << 8), keycnt}}; \r | |
252 | memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r | |
253 | SendCommand(&c);\r | |
254 | \r | |
255 | UsbCommand resp;\r | |
256 | if (!WaitForResponseTimeoutW(CMD_ACK, &resp, MAX(3000, 1000 + 13 * sectorCnt * keycnt * (keyType == 2 ? 2 : 1)), false)) return 1; // timeout: 13 ms / fail auth\r | |
257 | if ((resp.arg[0] & 0xff) != 0x01) return 2;\r | |
258 | \r | |
259 | bool foundAKey = false;\r | |
260 | for(int sec = 0; sec < sectorCnt; sec++){\r | |
261 | for(int keyAB = 0; keyAB < 2; keyAB++){\r | |
262 | keyPtr = *(resp.d.asBytes + keyAB * 40 + sec);\r | |
263 | if (keyPtr){\r | |
264 | e_sector[sec].foundKey[keyAB] = true;\r | |
265 | e_sector[sec].Key[keyAB] = bytes_to_num(keyBlock + (keyPtr - 1) * 6, 6);\r | |
266 | foundAKey = true;\r | |
267 | }\r | |
268 | }\r | |
269 | }\r | |
270 | return foundAKey ? 0 : 3;\r | |
271 | }\r | |
272 | \r | |
9492e0b0 | 273 | // Compare 16 Bits out of cryptostate\r |
274 | int Compare16Bits(const void * a, const void * b) {\r | |
275 | if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;\r | |
276 | else if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1;\r | |
277 | else return -1;\r | |
f397b5cc M |
278 | }\r |
279 | \r | |
7906cb41 | 280 | typedef\r |
9492e0b0 | 281 | struct {\r |
282 | union {\r | |
283 | struct Crypto1State *slhead;\r | |
284 | uint64_t *keyhead;\r | |
3f7c993d | 285 | } head;\r |
9492e0b0 | 286 | union {\r |
287 | struct Crypto1State *sltail;\r | |
288 | uint64_t *keytail;\r | |
3f7c993d | 289 | } tail;\r |
9492e0b0 | 290 | uint32_t len;\r |
291 | uint32_t uid;\r | |
292 | uint32_t blockNo;\r | |
293 | uint32_t keyType;\r | |
294 | uint32_t nt;\r | |
295 | uint32_t ks1;\r | |
296 | } StateList_t;\r | |
297 | \r | |
298 | \r | |
299 | // wrapper function for multi-threaded lfsr_recovery32\r | |
f921c113 | 300 | void\r |
301 | #ifdef __has_attribute\r | |
302 | #if __has_attribute(force_align_arg_pointer)\r | |
303 | __attribute__((force_align_arg_pointer)) \r | |
304 | #endif\r | |
305 | #endif\r | |
306 | *nested_worker_thread(void *arg)\r | |
9492e0b0 | 307 | {\r |
308 | struct Crypto1State *p1;\r | |
309 | StateList_t *statelist = arg;\r | |
310 | \r | |
3f7c993d | 311 | statelist->head.slhead = lfsr_recovery32(statelist->ks1, statelist->nt ^ statelist->uid);\r |
312 | for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; p1++);\r | |
313 | statelist->len = p1 - statelist->head.slhead;\r | |
314 | statelist->tail.sltail = --p1;\r | |
315 | qsort(statelist->head.slhead, statelist->len, sizeof(uint64_t), Compare16Bits);\r | |
7906cb41 | 316 | \r |
3f7c993d | 317 | return statelist->head.slhead;\r |
f397b5cc M |
318 | }\r |
319 | \r | |
f921c113 | 320 | \r |
7906cb41 | 321 | int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *resultKey, bool calibrate)\r |
f397b5cc | 322 | {\r |
dc8ba239 | 323 | uint16_t i;\r |
f397b5cc | 324 | uint32_t uid;\r |
902cb3c0 | 325 | UsbCommand resp;\r |
f397b5cc | 326 | \r |
9492e0b0 | 327 | StateList_t statelists[2];\r |
328 | struct Crypto1State *p1, *p2, *p3, *p4;\r | |
7906cb41 | 329 | \r |
f397b5cc | 330 | // flush queue\r |
dc8ba239 | 331 | WaitForResponseTimeout(CMD_ACK, NULL, 100);\r |
7906cb41 | 332 | \r |
9492e0b0 | 333 | UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}};\r |
f397b5cc | 334 | memcpy(c.d.asBytes, key, 6);\r |
9492e0b0 | 335 | SendCommand(&c);\r |
f397b5cc | 336 | \r |
dc8ba239 | 337 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r |
338 | return -1;\r | |
339 | }\r | |
f397b5cc | 340 | \r |
dc8ba239 | 341 | if (resp.arg[0]) {\r |
342 | return resp.arg[0]; // error during nested\r | |
343 | }\r | |
7906cb41 | 344 | \r |
dc8ba239 | 345 | memcpy(&uid, resp.d.asBytes, 4);\r |
346 | PrintAndLog("uid:%08x trgbl=%d trgkey=%x", uid, (uint16_t)resp.arg[2] & 0xff, (uint16_t)resp.arg[2] >> 8);\r | |
7906cb41 | 347 | \r |
dc8ba239 | 348 | for (i = 0; i < 2; i++) {\r |
349 | statelists[i].blockNo = resp.arg[2] & 0xff;\r | |
350 | statelists[i].keyType = (resp.arg[2] >> 8) & 0xff;\r | |
351 | statelists[i].uid = uid;\r | |
352 | memcpy(&statelists[i].nt, (void *)(resp.d.asBytes + 4 + i * 8 + 0), 4);\r | |
353 | memcpy(&statelists[i].ks1, (void *)(resp.d.asBytes + 4 + i * 8 + 4), 4);\r | |
f397b5cc | 354 | }\r |
7906cb41 | 355 | \r |
f397b5cc | 356 | // calc keys\r |
7906cb41 | 357 | \r |
9492e0b0 | 358 | pthread_t thread_id[2];\r |
7906cb41 | 359 | \r |
9492e0b0 | 360 | // create and run worker threads\r |
361 | for (i = 0; i < 2; i++) {\r | |
362 | pthread_create(thread_id + i, NULL, nested_worker_thread, &statelists[i]);\r | |
f397b5cc | 363 | }\r |
7906cb41 | 364 | \r |
9492e0b0 | 365 | // wait for threads to terminate:\r |
366 | for (i = 0; i < 2; i++) {\r | |
3f7c993d | 367 | pthread_join(thread_id[i], (void*)&statelists[i].head.slhead);\r |
f397b5cc M |
368 | }\r |
369 | \r | |
f397b5cc | 370 | \r |
9492e0b0 | 371 | // the first 16 Bits of the cryptostate already contain part of our key.\r |
372 | // Create the intersection of the two lists based on these 16 Bits and\r | |
373 | // roll back the cryptostate\r | |
7906cb41 | 374 | p1 = p3 = statelists[0].head.slhead;\r |
3f7c993d | 375 | p2 = p4 = statelists[1].head.slhead;\r |
376 | while (p1 <= statelists[0].tail.sltail && p2 <= statelists[1].tail.sltail) {\r | |
9492e0b0 | 377 | if (Compare16Bits(p1, p2) == 0) {\r |
378 | struct Crypto1State savestate, *savep = &savestate;\r | |
379 | savestate = *p1;\r | |
3f7c993d | 380 | while(Compare16Bits(p1, savep) == 0 && p1 <= statelists[0].tail.sltail) {\r |
9492e0b0 | 381 | *p3 = *p1;\r |
382 | lfsr_rollback_word(p3, statelists[0].nt ^ statelists[0].uid, 0);\r | |
383 | p3++;\r | |
384 | p1++;\r | |
385 | }\r | |
386 | savestate = *p2;\r | |
3f7c993d | 387 | while(Compare16Bits(p2, savep) == 0 && p2 <= statelists[1].tail.sltail) {\r |
9492e0b0 | 388 | *p4 = *p2;\r |
389 | lfsr_rollback_word(p4, statelists[1].nt ^ statelists[1].uid, 0);\r | |
390 | p4++;\r | |
391 | p2++;\r | |
392 | }\r | |
393 | }\r | |
394 | else {\r | |
395 | while (Compare16Bits(p1, p2) == -1) p1++;\r | |
396 | while (Compare16Bits(p1, p2) == 1) p2++;\r | |
397 | }\r | |
f397b5cc | 398 | }\r |
7779d73c | 399 | *(uint64_t*)p3 = -1;\r |
400 | *(uint64_t*)p4 = -1;\r | |
3f7c993d | 401 | statelists[0].len = p3 - statelists[0].head.slhead;\r |
402 | statelists[1].len = p4 - statelists[1].head.slhead;\r | |
403 | statelists[0].tail.sltail=--p3;\r | |
404 | statelists[1].tail.sltail=--p4;\r | |
9492e0b0 | 405 | \r |
406 | // the statelists now contain possible keys. The key we are searching for must be in the\r | |
407 | // intersection of both lists. Create the intersection:\r | |
7779d73c | 408 | qsort(statelists[0].head.keyhead, statelists[0].len, sizeof(uint64_t), compare_uint64);\r |
409 | qsort(statelists[1].head.keyhead, statelists[1].len, sizeof(uint64_t), compare_uint64);\r | |
410 | statelists[0].len = intersection(statelists[0].head.keyhead, statelists[1].head.keyhead);\r | |
9492e0b0 | 411 | \r |
412 | memset(resultKey, 0, 6);\r | |
413 | // The list may still contain several key candidates. Test each of them with mfCheckKeys\r | |
414 | for (i = 0; i < statelists[0].len; i++) {\r | |
415 | uint8_t keyBlock[6];\r | |
416 | uint64_t key64;\r | |
3f7c993d | 417 | crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);\r |
9492e0b0 | 418 | num_to_bytes(key64, 6, keyBlock);\r |
419 | key64 = 0;\r | |
5330f532 | 420 | if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, 1, keyBlock, &key64)) {\r |
9492e0b0 | 421 | num_to_bytes(key64, 6, resultKey);\r |
422 | break;\r | |
423 | }\r | |
424 | }\r | |
7906cb41 | 425 | \r |
3f7c993d | 426 | free(statelists[0].head.slhead);\r |
427 | free(statelists[1].head.slhead);\r | |
7906cb41 | 428 | \r |
f397b5cc M |
429 | return 0;\r |
430 | }\r | |
431 | \r | |
55acbb2a M |
432 | // EMULATOR\r |
433 | \r | |
8556b852 | 434 | int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r |
545a1f38 | 435 | UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};\r |
902cb3c0 | 436 | SendCommand(&c);\r |
3a05a1e7 OM |
437 | \r |
438 | UsbCommand resp;\r | |
902cb3c0 | 439 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;\r |
440 | memcpy(data, resp.d.asBytes, blocksCount * 16);\r | |
8556b852 M |
441 | return 0;\r |
442 | }\r | |
443 | \r | |
444 | int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r | |
545a1f38 | 445 | UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, 0}};\r |
7906cb41 | 446 | memcpy(c.d.asBytes, data, blocksCount * 16);\r |
545a1f38 | 447 | SendCommand(&c);\r |
8556b852 M |
448 | return 0;\r |
449 | }\r | |
450 | \r | |
55acbb2a M |
451 | // "MAGIC" CARD\r |
452 | \r | |
7cb8516c | 453 | int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {\r |
454 | uint8_t isOK = 0;\r | |
3bba7dea | 455 | \r |
7cb8516c | 456 | UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, 0, blockNo}};\r |
457 | SendCommand(&c);\r | |
3a05a1e7 | 458 | \r |
14d76577 | 459 | UsbCommand resp;\r |
7cb8516c | 460 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r |
461 | isOK = resp.arg[0] & 0xff;\r | |
462 | memcpy(data, resp.d.asBytes, 16);\r | |
463 | if (!isOK) return 2;\r | |
464 | } else {\r | |
465 | PrintAndLog("Command execute timeout");\r | |
466 | return 1;\r | |
80b1b53f | 467 | }\r |
7cb8516c | 468 | return 0;\r |
f774db95 M |
469 | }\r |
470 | \r | |
6a1f2d82 | 471 | int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uint8_t params) {\r |
0675f200 | 472 | \r |
3fe4ff4f | 473 | uint8_t isOK = 0;\r |
474 | UsbCommand c = {CMD_MIFARE_CSETBLOCK, {wantWipe, params & (0xFE | (uid == NULL ? 0:1)), blockNo}};\r | |
7906cb41 | 475 | memcpy(c.d.asBytes, data, 16);\r |
545a1f38 | 476 | SendCommand(&c);\r |
3a05a1e7 | 477 | \r |
14d76577 | 478 | UsbCommand resp;\r |
3a05a1e7 | 479 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r |
902cb3c0 | 480 | isOK = resp.arg[0] & 0xff;\r |
7906cb41 | 481 | if (uid != NULL)\r |
80b1b53f | 482 | memcpy(uid, resp.d.asBytes, 4);\r |
7906cb41 | 483 | if (!isOK)\r |
80b1b53f | 484 | return 2;\r |
0675f200 M |
485 | } else {\r |
486 | PrintAndLog("Command execute timeout");\r | |
487 | return 1;\r | |
488 | }\r | |
3a05a1e7 | 489 | \r |
0675f200 M |
490 | return 0;\r |
491 | }\r | |
545a1f38 | 492 | \r |
3a05a1e7 OM |
493 | int mfCWipe(uint32_t numSectors, bool gen1b, bool wantWipe, bool wantFill) {\r |
494 | uint8_t isOK = 0;\r | |
495 | uint8_t cmdParams = wantWipe + wantFill * 0x02 + gen1b * 0x04;\r | |
496 | UsbCommand c = {CMD_MIFARE_CWIPE, {numSectors, cmdParams, 0}};\r | |
497 | SendCommand(&c);\r | |
498 | \r | |
499 | UsbCommand resp;\r | |
500 | WaitForResponse(CMD_ACK,&resp);\r | |
501 | isOK = resp.arg[0] & 0xff;\r | |
502 | \r | |
503 | return isOK;\r | |
504 | }\r | |
505 | \r | |
506 | int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID) {\r | |
7cb8516c | 507 | uint8_t oldblock0[16] = {0x00};\r |
508 | uint8_t block0[16] = {0x00};\r | |
3a05a1e7 | 509 | int gen = 0, res;\r |
7906cb41 F |
510 | \r |
511 | gen = mfCIdentify();\r | |
512 | \r | |
3a05a1e7 OM |
513 | /* generation 1a magic card by default */\r |
514 | uint8_t cmdParams = CSETBLOCK_SINGLE_OPER;\r | |
7906cb41 F |
515 | if (gen == 2) {\r |
516 | /* generation 1b magic card */\r | |
3a05a1e7 | 517 | cmdParams = CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B;\r |
7906cb41 | 518 | }\r |
3a05a1e7 OM |
519 | \r |
520 | res = mfCGetBlock(0, oldblock0, cmdParams);\r | |
545a1f38 | 521 | \r |
3a05a1e7 | 522 | if (res == 0) {\r |
7cb8516c | 523 | memcpy(block0, oldblock0, 16);\r |
524 | PrintAndLog("old block 0: %s", sprint_hex(block0,16));\r | |
545a1f38 | 525 | } else {\r |
7cb8516c | 526 | PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0.");\r |
545a1f38 | 527 | }\r |
7cb8516c | 528 | \r |
529 | // fill in the new values\r | |
530 | // UID\r | |
7906cb41 | 531 | memcpy(block0, uid, 4);\r |
7cb8516c | 532 | // Mifare UID BCC\r |
3a05a1e7 | 533 | block0[4] = block0[0] ^ block0[1] ^ block0[2] ^ block0[3];\r |
7cb8516c | 534 | // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed)\r |
3a05a1e7 OM |
535 | if (sak != NULL)\r |
536 | block0[5] = sak[0];\r | |
537 | if (atqa != NULL) {\r | |
538 | block0[6] = atqa[1];\r | |
539 | block0[7] = atqa[0];\r | |
7cb8516c | 540 | }\r |
3a05a1e7 | 541 | PrintAndLog("new block 0: %s", sprint_hex(block0, 16));\r |
7906cb41 | 542 | \r |
3a05a1e7 OM |
543 | res = mfCSetBlock(0, block0, oldUID, false, cmdParams);\r |
544 | if (res) {\r | |
545 | PrintAndLog("Can't set block 0. Error: %d", res);\r | |
546 | return res;\r | |
7906cb41 | 547 | }\r |
3a05a1e7 OM |
548 | \r |
549 | return 0;\r | |
550 | }\r | |
551 | \r | |
e17660d5 OM |
552 | int mfCIdentify() {\r |
553 | UsbCommand c = {CMD_MIFARE_CIDENT, {0, 0, 0}};\r | |
3a05a1e7 | 554 | SendCommand(&c);\r |
e17660d5 | 555 | UsbCommand resp;\r |
3a05a1e7 OM |
556 | WaitForResponse(CMD_ACK,&resp);\r |
557 | \r | |
558 | uint8_t isGeneration = resp.arg[0] & 0xff;\r | |
559 | switch( isGeneration ){\r | |
560 | case 1: PrintAndLog("Chinese magic backdoor commands (GEN 1a) detected"); break;\r | |
561 | case 2: PrintAndLog("Chinese magic backdoor command (GEN 1b) detected"); break;\r | |
562 | default: PrintAndLog("No chinese magic backdoor command detected"); break;\r | |
563 | }\r | |
564 | \r | |
3a05a1e7 | 565 | return (int) isGeneration;\r |
545a1f38 | 566 | }\r |
55acbb2a | 567 | \r |
3a05a1e7 | 568 | \r |
55acbb2a M |
569 | // SNIFFER\r |
570 | \r | |
e0c635d1 M |
571 | // constants\r |
572 | static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00};\r | |
573 | \r | |
55acbb2a | 574 | // variables\r |
e4691591 | 575 | char logHexFileName[FILE_PATH_SIZE] = {0x00};\r |
e0c635d1 | 576 | static uint8_t traceCard[4096] = {0x00};\r |
e4691591 | 577 | static char traceFileName[FILE_PATH_SIZE] = {0x00};\r |
55acbb2a M |
578 | static int traceState = TRACE_IDLE;\r |
579 | static uint8_t traceCurBlock = 0;\r | |
580 | static uint8_t traceCurKey = 0;\r | |
581 | \r | |
582 | struct Crypto1State *traceCrypto1 = NULL;\r | |
583 | \r | |
584 | struct Crypto1State *revstate;\r | |
585 | uint64_t lfsr;\r | |
a37725fa | 586 | uint64_t ui64Key;\r |
55acbb2a M |
587 | uint32_t ks2;\r |
588 | uint32_t ks3;\r | |
589 | \r | |
a37725fa OM |
590 | uint32_t uid; // serial number\r |
591 | uint32_t nt; // tag challenge\r | |
592 | uint32_t nt_enc; // encrypted tag challenge\r | |
593 | uint8_t nt_enc_par; // encrypted tag challenge parity\r | |
594 | uint32_t nr_enc; // encrypted reader challenge\r | |
595 | uint32_t ar_enc; // encrypted reader response\r | |
596 | uint8_t ar_enc_par; // encrypted reader response parity\r | |
597 | uint32_t at_enc; // encrypted tag response\r | |
598 | uint8_t at_enc_par; // encrypted tag response parity\r | |
55acbb2a | 599 | \r |
e0c635d1 M |
600 | int isTraceCardEmpty(void) {\r |
601 | return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));\r | |
602 | }\r | |
55acbb2a | 603 | \r |
e0c635d1 | 604 | int isBlockEmpty(int blockN) {\r |
7906cb41 | 605 | for (int i = 0; i < 16; i++)\r |
e0c635d1 | 606 | if (traceCard[blockN * 16 + i] != 0) return 0;\r |
55acbb2a | 607 | \r |
e0c635d1 M |
608 | return 1;\r |
609 | }\r | |
610 | \r | |
611 | int isBlockTrailer(int blockN) {\r | |
3a05a1e7 | 612 | return ((blockN & 0x03) == 0x03);\r |
e0c635d1 M |
613 | }\r |
614 | \r | |
7cb8516c | 615 | int saveTraceCard(void) {\r |
616 | FILE * f;\r | |
7906cb41 | 617 | \r |
7cb8516c | 618 | if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;\r |
7906cb41 | 619 | \r |
7cb8516c | 620 | f = fopen(traceFileName, "w+");\r |
621 | if ( !f ) return 1;\r | |
7906cb41 | 622 | \r |
7cb8516c | 623 | for (int i = 0; i < 64; i++) { // blocks\r |
624 | for (int j = 0; j < 16; j++) // bytes\r | |
7906cb41 | 625 | fprintf(f, "%02x", *(traceCard + i * 16 + j));\r |
a0349483 | 626 | if (i < 63)\r |
627 | fprintf(f,"\n");\r | |
7cb8516c | 628 | }\r |
629 | fclose(f);\r | |
630 | return 0;\r | |
631 | }\r | |
632 | \r | |
e0c635d1 M |
633 | int loadTraceCard(uint8_t *tuid) {\r |
634 | FILE * f;\r | |
80b1b53f | 635 | char buf[64] = {0x00};\r |
636 | uint8_t buf8[64] = {0x00};\r | |
e0c635d1 | 637 | int i, blockNum;\r |
7906cb41 F |
638 | \r |
639 | if (!isTraceCardEmpty())\r | |
80b1b53f | 640 | saveTraceCard();\r |
7906cb41 | 641 | \r |
55acbb2a M |
642 | memset(traceCard, 0x00, 4096);\r |
643 | memcpy(traceCard, tuid + 3, 4);\r | |
80b1b53f | 644 | \r |
e0c635d1 M |
645 | FillFileNameByUID(traceFileName, tuid, ".eml", 7);\r |
646 | \r | |
647 | f = fopen(traceFileName, "r");\r | |
e6432f05 | 648 | if (!f) return 1;\r |
7906cb41 | 649 | \r |
e0c635d1 | 650 | blockNum = 0;\r |
7906cb41 | 651 | \r |
e0c635d1 | 652 | while(!feof(f)){\r |
7906cb41 | 653 | \r |
e0c635d1 | 654 | memset(buf, 0, sizeof(buf));\r |
759c16b3 | 655 | if (fgets(buf, sizeof(buf), f) == NULL) {\r |
ca4714cd MHS |
656 | PrintAndLog("File reading error.");\r |
657 | fclose(f);\r | |
759c16b3 | 658 | return 2;\r |
ca4714cd | 659 | }\r |
e0c635d1 M |
660 | \r |
661 | if (strlen(buf) < 32){\r | |
662 | if (feof(f)) break;\r | |
663 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r | |
ca4714cd | 664 | fclose(f);\r |
e0c635d1 M |
665 | return 2;\r |
666 | }\r | |
667 | for (i = 0; i < 32; i += 2)\r | |
668 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
669 | \r | |
670 | memcpy(traceCard + blockNum * 16, buf8, 16);\r | |
671 | \r | |
672 | blockNum++;\r | |
673 | }\r | |
674 | fclose(f);\r | |
3a05a1e7 | 675 | \r |
e0c635d1 M |
676 | return 0;\r |
677 | }\r | |
678 | \r | |
e0c635d1 M |
679 | int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {\r |
680 | \r | |
7906cb41 | 681 | if (traceCrypto1)\r |
80b1b53f | 682 | crypto1_destroy(traceCrypto1);\r |
683 | \r | |
e0c635d1 M |
684 | traceCrypto1 = NULL;\r |
685 | \r | |
7906cb41 | 686 | if (wantSaveToEmlFile)\r |
80b1b53f | 687 | loadTraceCard(tuid);\r |
7906cb41 | 688 | \r |
55acbb2a M |
689 | traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];\r |
690 | traceCard[5] = sak;\r | |
691 | memcpy(&traceCard[6], atqa, 2);\r | |
692 | traceCurBlock = 0;\r | |
693 | uid = bytes_to_num(tuid + 3, 4);\r | |
7906cb41 | 694 | \r |
55acbb2a | 695 | traceState = TRACE_IDLE;\r |
3a05a1e7 | 696 | \r |
55acbb2a M |
697 | return 0;\r |
698 | }\r | |
699 | \r | |
700 | void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){\r | |
701 | uint8_t bt = 0;\r | |
702 | int i;\r | |
7906cb41 | 703 | \r |
55acbb2a M |
704 | if (len != 1) {\r |
705 | for (i = 0; i < len; i++)\r | |
706 | data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];\r | |
707 | } else {\r | |
708 | bt = 0;\r | |
709 | for (i = 0; i < 4; i++)\r | |
710 | bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;\r | |
7906cb41 | 711 | \r |
55acbb2a M |
712 | data[0] = bt;\r |
713 | }\r | |
714 | return;\r | |
715 | }\r | |
716 | \r | |
a37725fa OM |
717 | bool NTParityCheck(uint32_t ntx) {\r |
718 | if (\r | |
719 | (oddparity8(ntx >> 8 & 0xff) ^ (ntx & 0x01) ^ ((nt_enc_par >> 5) & 0x01) ^ (nt_enc & 0x01)) ||\r | |
720 | (oddparity8(ntx >> 16 & 0xff) ^ (ntx >> 8 & 0x01) ^ ((nt_enc_par >> 6) & 0x01) ^ (nt_enc >> 8 & 0x01)) ||\r | |
721 | (oddparity8(ntx >> 24 & 0xff) ^ (ntx >> 16 & 0x01) ^ ((nt_enc_par >> 7) & 0x01) ^ (nt_enc >> 16 & 0x01))\r | |
722 | )\r | |
723 | return false;\r | |
724 | \r | |
725 | uint32_t ar = prng_successor(ntx, 64);\r | |
726 | if (\r | |
727 | (oddparity8(ar >> 8 & 0xff) ^ (ar & 0x01) ^ ((ar_enc_par >> 5) & 0x01) ^ (ar_enc & 0x01)) ||\r | |
728 | (oddparity8(ar >> 16 & 0xff) ^ (ar >> 8 & 0x01) ^ ((ar_enc_par >> 6) & 0x01) ^ (ar_enc >> 8 & 0x01)) ||\r | |
729 | (oddparity8(ar >> 24 & 0xff) ^ (ar >> 16 & 0x01) ^ ((ar_enc_par >> 7) & 0x01) ^ (ar_enc >> 16 & 0x01))\r | |
730 | )\r | |
731 | return false;\r | |
732 | \r | |
733 | uint32_t at = prng_successor(ntx, 96);\r | |
734 | if (\r | |
735 | (oddparity8(ar & 0xff) ^ (at >> 24 & 0x01) ^ ((ar_enc_par >> 4) & 0x01) ^ (at_enc >> 24 & 0x01)) ||\r | |
736 | (oddparity8(at >> 8 & 0xff) ^ (at & 0x01) ^ ((at_enc_par >> 5) & 0x01) ^ (at_enc & 0x01)) ||\r | |
737 | (oddparity8(at >> 16 & 0xff) ^ (at >> 8 & 0x01) ^ ((at_enc_par >> 6) & 0x01) ^ (at_enc >> 8 & 0x01)) ||\r | |
738 | (oddparity8(at >> 24 & 0xff) ^ (at >> 16 & 0x01) ^ ((at_enc_par >> 7) & 0x01) ^ (at_enc >> 16 & 0x01))\r | |
739 | )\r | |
740 | return false;\r | |
741 | \r | |
742 | return true;\r | |
743 | }\r | |
55acbb2a | 744 | \r |
a37725fa OM |
745 | \r |
746 | int mfTraceDecode(uint8_t *data_src, int len, uint8_t parity, bool wantSaveToEmlFile) {\r | |
55acbb2a M |
747 | uint8_t data[64];\r |
748 | \r | |
749 | if (traceState == TRACE_ERROR) return 1;\r | |
750 | if (len > 64) {\r | |
751 | traceState = TRACE_ERROR;\r | |
752 | return 1;\r | |
753 | }\r | |
7906cb41 | 754 | \r |
55acbb2a M |
755 | memcpy(data, data_src, len);\r |
756 | if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {\r | |
757 | mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r | |
a37725fa OM |
758 | uint8_t parity[16];\r |
759 | oddparitybuf(data, len, parity);\r | |
760 | PrintAndLog("dec> %s [%s]", sprint_hex(data, len), printBitsPar(parity, len));\r | |
7906cb41 | 761 | AddLogHex(logHexFileName, "dec> ", data, len);\r |
55acbb2a | 762 | }\r |
7906cb41 | 763 | \r |
55acbb2a | 764 | switch (traceState) {\r |
7906cb41 | 765 | case TRACE_IDLE:\r |
8b6a93df M |
766 | // check packet crc16!\r |
767 | if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {\r | |
768 | PrintAndLog("dec> CRC ERROR!!!");\r | |
7906cb41 | 769 | AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!");\r |
8b6a93df M |
770 | traceState = TRACE_ERROR; // do not decrypt the next commands\r |
771 | return 1;\r | |
772 | }\r | |
7906cb41 | 773 | \r |
55acbb2a M |
774 | // AUTHENTICATION\r |
775 | if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) {\r | |
776 | traceState = TRACE_AUTH1;\r | |
777 | traceCurBlock = data[1];\r | |
778 | traceCurKey = data[0] == 60 ? 1:0;\r | |
779 | return 0;\r | |
780 | }\r | |
781 | \r | |
782 | // READ\r | |
783 | if ((len ==4) && ((data[0] == 0x30))) {\r | |
784 | traceState = TRACE_READ_DATA;\r | |
785 | traceCurBlock = data[1];\r | |
786 | return 0;\r | |
787 | }\r | |
788 | \r | |
789 | // WRITE\r | |
790 | if ((len ==4) && ((data[0] == 0xA0))) {\r | |
791 | traceState = TRACE_WRITE_OK;\r | |
792 | traceCurBlock = data[1];\r | |
793 | return 0;\r | |
794 | }\r | |
795 | \r | |
796 | // HALT\r | |
797 | if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {\r | |
798 | traceState = TRACE_ERROR; // do not decrypt the next commands\r | |
799 | return 0;\r | |
800 | }\r | |
7906cb41 | 801 | \r |
55acbb2a M |
802 | return 0;\r |
803 | break;\r | |
7906cb41 F |
804 | \r |
805 | case TRACE_READ_DATA:\r | |
55acbb2a M |
806 | if (len == 18) {\r |
807 | traceState = TRACE_IDLE;\r | |
808 | \r | |
e0c635d1 M |
809 | if (isBlockTrailer(traceCurBlock)) {\r |
810 | memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);\r | |
811 | } else {\r | |
812 | memcpy(traceCard + traceCurBlock * 16, data, 16);\r | |
813 | }\r | |
814 | if (wantSaveToEmlFile) saveTraceCard();\r | |
55acbb2a M |
815 | return 0;\r |
816 | } else {\r | |
817 | traceState = TRACE_ERROR;\r | |
818 | return 1;\r | |
819 | }\r | |
820 | break;\r | |
821 | \r | |
7906cb41 | 822 | case TRACE_WRITE_OK:\r |
97d582a6 | 823 | if ((len == 1) && (data[0] == 0x0a)) {\r |
55acbb2a M |
824 | traceState = TRACE_WRITE_DATA;\r |
825 | \r | |
826 | return 0;\r | |
827 | } else {\r | |
828 | traceState = TRACE_ERROR;\r | |
829 | return 1;\r | |
830 | }\r | |
831 | break;\r | |
832 | \r | |
7906cb41 | 833 | case TRACE_WRITE_DATA:\r |
55acbb2a M |
834 | if (len == 18) {\r |
835 | traceState = TRACE_IDLE;\r | |
836 | \r | |
837 | memcpy(traceCard + traceCurBlock * 16, data, 16);\r | |
e0c635d1 | 838 | if (wantSaveToEmlFile) saveTraceCard();\r |
55acbb2a M |
839 | return 0;\r |
840 | } else {\r | |
841 | traceState = TRACE_ERROR;\r | |
842 | return 1;\r | |
843 | }\r | |
844 | break;\r | |
845 | \r | |
7906cb41 | 846 | case TRACE_AUTH1:\r |
55acbb2a M |
847 | if (len == 4) {\r |
848 | traceState = TRACE_AUTH2;\r | |
a37725fa OM |
849 | if (!traceCrypto1) {\r |
850 | nt = bytes_to_num(data, 4);\r | |
851 | } else {\r | |
852 | nt_enc = bytes_to_num(data, 4);\r | |
853 | nt_enc_par = parity;\r | |
854 | }\r | |
55acbb2a M |
855 | return 0;\r |
856 | } else {\r | |
857 | traceState = TRACE_ERROR;\r | |
858 | return 1;\r | |
859 | }\r | |
860 | break;\r | |
861 | \r | |
7906cb41 | 862 | case TRACE_AUTH2:\r |
55acbb2a M |
863 | if (len == 8) {\r |
864 | traceState = TRACE_AUTH_OK;\r | |
865 | \r | |
866 | nr_enc = bytes_to_num(data, 4);\r | |
867 | ar_enc = bytes_to_num(data + 4, 4);\r | |
a37725fa | 868 | ar_enc_par = parity << 4;\r |
55acbb2a M |
869 | return 0;\r |
870 | } else {\r | |
871 | traceState = TRACE_ERROR;\r | |
872 | return 1;\r | |
873 | }\r | |
874 | break;\r | |
875 | \r | |
7906cb41 | 876 | case TRACE_AUTH_OK:\r |
55acbb2a M |
877 | if (len ==4) {\r |
878 | traceState = TRACE_IDLE;\r | |
879 | \r | |
a37725fa OM |
880 | at_enc = bytes_to_num(data, 4);\r |
881 | at_enc_par = parity;\r | |
2ba37081 | 882 | if (!traceCrypto1) {\r |
2ba37081 | 883 | \r |
884 | // decode key here)\r | |
885 | ks2 = ar_enc ^ prng_successor(nt, 64);\r | |
886 | ks3 = at_enc ^ prng_successor(nt, 96);\r | |
887 | revstate = lfsr_recovery64(ks2, ks3);\r | |
888 | lfsr_rollback_word(revstate, 0, 0);\r | |
889 | lfsr_rollback_word(revstate, 0, 0);\r | |
890 | lfsr_rollback_word(revstate, nr_enc, 1);\r | |
891 | lfsr_rollback_word(revstate, uid ^ nt, 0);\r | |
892 | \r | |
893 | crypto1_get_lfsr(revstate, &lfsr);\r | |
a37725fa OM |
894 | crypto1_destroy(revstate);\r |
895 | ui64Key = lfsr;\r | |
896 | printf("key> probable key:%x%x Prng:%s ks2:%08x ks3:%08x\n", \r | |
897 | (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF), \r | |
898 | validate_prng_nonce(nt) ? "WEAK": "HARDEND",\r | |
899 | ks2,\r | |
900 | ks3);\r | |
2ba37081 | 901 | AddLogUint64(logHexFileName, "key> ", lfsr);\r |
902 | } else {\r | |
a37725fa OM |
903 | if (validate_prng_nonce(nt)) {\r |
904 | struct Crypto1State *pcs;\r | |
905 | pcs = crypto1_create(ui64Key);\r | |
906 | uint32_t nt1 = crypto1_word(pcs, nt_enc ^ uid, 1) ^ nt_enc;\r | |
907 | uint32_t ar = prng_successor(nt1, 64);\r | |
908 | uint32_t at = prng_successor(nt1, 96);\r | |
909 | printf("key> nested auth uid: %08x nt: %08x nt_parity: %s ar: %08x at: %08x\n", uid, nt1, printBitsPar(&nt_enc_par, 4), ar, at);\r | |
910 | uint32_t nr1 = crypto1_word(pcs, nr_enc, 1) ^ nr_enc;\r | |
911 | uint32_t ar1 = crypto1_word(pcs, 0, 0) ^ ar_enc;\r | |
912 | uint32_t at1 = crypto1_word(pcs, 0, 0) ^ at_enc;\r | |
913 | printf("key> the same key test. nr1: %08x ar1: %08x at1: %08x \n", nr1, ar1, at1);\r | |
914 | \r | |
915 | if (NTParityCheck(nt1))\r | |
916 | printf("key> the same key test OK. key=%x%x\n", (unsigned int)((ui64Key & 0xFFFFFFFF00000000) >> 32), (unsigned int)(ui64Key & 0xFFFFFFFF));\r | |
917 | else\r | |
918 | printf("key> the same key test. check nt parity error.\n");\r | |
919 | \r | |
920 | uint32_t ntc = prng_successor(nt, 90);\r | |
921 | uint32_t ntx = 0;\r | |
922 | int ntcnt = 0;\r | |
923 | for (int i = 0; i < 16383; i++) {\r | |
924 | ntc = prng_successor(ntc, 1);\r | |
925 | if (NTParityCheck(ntc)){\r | |
926 | if (!ntcnt)\r | |
927 | ntx = ntc;\r | |
928 | ntcnt++;\r | |
929 | } \r | |
930 | }\r | |
931 | if (ntcnt)\r | |
932 | printf("key> nt candidate=%08x nonce distance=%d candidates count=%d\n", ntx, nonce_distance(nt, ntx), ntcnt);\r | |
933 | else\r | |
934 | printf("key> don't have any nt candidate( \n");\r | |
935 | \r | |
936 | nt = ntx;\r | |
937 | ks2 = ar_enc ^ prng_successor(ntx, 64);\r | |
938 | ks3 = at_enc ^ prng_successor(ntx, 96);\r | |
939 | \r | |
940 | // decode key\r | |
941 | revstate = lfsr_recovery64(ks2, ks3);\r | |
942 | lfsr_rollback_word(revstate, 0, 0);\r | |
943 | lfsr_rollback_word(revstate, 0, 0);\r | |
944 | lfsr_rollback_word(revstate, nr_enc, 1);\r | |
945 | lfsr_rollback_word(revstate, uid ^ nt, 0);\r | |
946 | \r | |
947 | crypto1_get_lfsr(revstate, &lfsr);\r | |
948 | crypto1_destroy(revstate);\r | |
949 | ui64Key = lfsr;\r | |
950 | printf("key> probable key:%x%x ks2:%08x ks3:%08x\n", \r | |
951 | (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF),\r | |
952 | ks2,\r | |
953 | ks3);\r | |
954 | AddLogUint64(logHexFileName, "key> ", lfsr);\r | |
955 | } else { \r | |
956 | printf("key> hardnested not implemented!\n");\r | |
2ba37081 | 957 | \r |
a37725fa | 958 | crypto1_destroy(traceCrypto1);\r |
3fe4ff4f | 959 | \r |
a37725fa OM |
960 | // not implemented\r |
961 | traceState = TRACE_ERROR;\r | |
962 | }\r | |
2ba37081 | 963 | }\r |
7906cb41 | 964 | \r |
e0c635d1 M |
965 | int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r |
966 | if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r | |
7906cb41 | 967 | \r |
55acbb2a | 968 | if (traceCurKey) {\r |
e0c635d1 | 969 | num_to_bytes(lfsr, 6, traceCard + blockShift + 10);\r |
55acbb2a | 970 | } else {\r |
e0c635d1 | 971 | num_to_bytes(lfsr, 6, traceCard + blockShift);\r |
55acbb2a | 972 | }\r |
e0c635d1 | 973 | if (wantSaveToEmlFile) saveTraceCard();\r |
55acbb2a M |
974 | \r |
975 | if (traceCrypto1) {\r | |
976 | crypto1_destroy(traceCrypto1);\r | |
977 | }\r | |
7906cb41 | 978 | \r |
55acbb2a M |
979 | // set cryptosystem state\r |
980 | traceCrypto1 = lfsr_recovery64(ks2, ks3);\r | |
55acbb2a M |
981 | return 0;\r |
982 | } else {\r | |
983 | traceState = TRACE_ERROR;\r | |
984 | return 1;\r | |
985 | }\r | |
986 | break;\r | |
987 | \r | |
7906cb41 | 988 | default:\r |
55acbb2a M |
989 | traceState = TRACE_ERROR;\r |
990 | return 1;\r | |
991 | }\r | |
992 | \r | |
993 | return 0;\r | |
994 | }\r | |
1a5a73ab | 995 | \r |
3a05a1e7 OM |
996 | // DECODING\r |
997 | \r | |
1a5a73ab | 998 | int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len){\r |
999 | /*\r | |
1000 | uint32_t nt; // tag challenge\r | |
1001 | uint32_t ar_enc; // encrypted reader response\r | |
1002 | uint32_t at_enc; // encrypted tag response\r | |
1003 | */\r | |
1004 | if (traceCrypto1) {\r | |
1005 | crypto1_destroy(traceCrypto1);\r | |
1006 | }\r | |
1007 | ks2 = ar_enc ^ prng_successor(nt, 64);\r | |
1008 | ks3 = at_enc ^ prng_successor(nt, 96);\r | |
1009 | traceCrypto1 = lfsr_recovery64(ks2, ks3);\r | |
1010 | \r | |
1011 | mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r | |
1012 | \r | |
1013 | PrintAndLog("Decrypted data: [%s]", sprint_hex(data,len) );\r | |
1014 | crypto1_destroy(traceCrypto1);\r | |
1015 | return 0;\r | |
1016 | }\r | |
7906cb41 | 1017 | \r |
fe6bf3c5 OM |
1018 | /** validate_prng_nonce\r |
1019 | * Determine if nonce is deterministic. ie: Suspectable to Darkside attack.\r | |
1020 | * returns\r | |
1021 | * true = weak prng\r | |
1022 | * false = hardend prng\r | |
1023 | */\r | |
1024 | bool validate_prng_nonce(uint32_t nonce) {\r | |
1025 | uint16_t *dist = 0;\r | |
1026 | uint16_t x, i;\r | |
1027 | \r | |
1028 | dist = malloc(2 << 16);\r | |
1029 | if(!dist)\r | |
1030 | return -1;\r | |
1031 | \r | |
1032 | // init prng table:\r | |
1033 | for (x = i = 1; i; ++i) {\r | |
1034 | dist[(x & 0xff) << 8 | x >> 8] = i;\r | |
1035 | x = x >> 1 | (x ^ x >> 2 ^ x >> 3 ^ x >> 5) << 15;\r | |
1036 | }\r | |
1037 | \r | |
1038 | uint32_t res = (65535 - dist[nonce >> 16] + dist[nonce & 0xffff]) % 65535;\r | |
1039 | \r | |
1040 | free(dist); \r | |
1041 | return (res == 16);\r | |
1042 | }\r | |
1043 | \r | |
1044 | /* Detect Tag Prng, \r | |
1045 | * function performs a partial AUTH, where it tries to authenticate against block0, key A, but only collects tag nonce.\r | |
1046 | * the tag nonce is check to see if it has a predictable PRNG.\r | |
1047 | * @returns \r | |
1048 | * TRUE if tag uses WEAK prng (ie Now the NACK bug also needs to be present for Darkside attack)\r | |
1049 | * FALSE is tag uses HARDEND prng (ie hardnested attack possible, with known key)\r | |
1050 | */\r | |
1051 | int DetectClassicPrng(void){\r | |
1052 | \r | |
1053 | UsbCommand resp, respA; \r | |
1054 | uint8_t cmd[] = {0x60, 0x00}; // MIFARE_AUTH_KEYA\r | |
1055 | uint32_t flags = ISO14A_CONNECT | ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_RATS;\r | |
1056 | \r | |
1057 | UsbCommand c = {CMD_READER_ISO_14443a, {flags, sizeof(cmd), 0}};\r | |
1058 | memcpy(c.d.asBytes, cmd, sizeof(cmd));\r | |
1059 | \r | |
1060 | clearCommandBuffer();\r | |
1061 | SendCommand(&c);\r | |
1062 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {\r | |
1063 | PrintAndLog("PRNG UID: Reply timeout.");\r | |
1064 | return -1;\r | |
1065 | }\r | |
1066 | \r | |
1067 | // if select tag failed.\r | |
1068 | if (resp.arg[0] == 0) {\r | |
1069 | PrintAndLog("PRNG error: selecting tag failed, can't detect prng.");\r | |
1070 | return -1;\r | |
1071 | }\r | |
1072 | \r | |
1073 | if (!WaitForResponseTimeout(CMD_ACK, &respA, 5000)) {\r | |
1074 | PrintAndLog("PRNG data: Reply timeout.");\r | |
1075 | return -1;\r | |
1076 | }\r | |
1077 | \r | |
1078 | // check respA\r | |
1079 | if (respA.arg[0] != 4) {\r | |
1080 | PrintAndLog("PRNG data error: Wrong length: %d", respA.arg[0]);\r | |
1081 | return -1;\r | |
1082 | }\r | |
1083 | \r | |
1084 | uint32_t nonce = bytes_to_num(respA.d.asBytes, respA.arg[0]);\r | |
1085 | return validate_prng_nonce(nonce);\r | |
1086 | }\r |