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