]>
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 | |
11 | #include <stdio.h>\r | |
873014de | 12 | #include <stdlib.h> \r |
31b6e9af | 13 | #include <string.h>\r |
9492e0b0 | 14 | #include <pthread.h>\r |
f397b5cc | 15 | #include "mifarehost.h"\r |
902cb3c0 | 16 | #include "proxmark3.h"\r |
f397b5cc | 17 | \r |
55acbb2a | 18 | // MIFARE\r |
f397b5cc | 19 | int compar_int(const void * a, const void * b) {\r |
9492e0b0 | 20 | // didn't work: (the result is truncated to 32 bits)\r |
21 | //return (*(uint64_t*)b - *(uint64_t*)a);\r | |
22 | \r | |
23 | // better:\r | |
24 | if (*(uint64_t*)b == *(uint64_t*)a) return 0;\r | |
25 | else if (*(uint64_t*)b > *(uint64_t*)a) return 1;\r | |
26 | else return -1;\r | |
f397b5cc M |
27 | }\r |
28 | \r | |
9492e0b0 | 29 | // Compare 16 Bits out of cryptostate\r |
30 | int Compare16Bits(const void * a, const void * b) {\r | |
31 | if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;\r | |
32 | else if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1;\r | |
33 | else return -1;\r | |
f397b5cc M |
34 | }\r |
35 | \r | |
9492e0b0 | 36 | typedef \r |
37 | struct {\r | |
38 | union {\r | |
39 | struct Crypto1State *slhead;\r | |
40 | uint64_t *keyhead;\r | |
3f7c993d | 41 | } head;\r |
9492e0b0 | 42 | union {\r |
43 | struct Crypto1State *sltail;\r | |
44 | uint64_t *keytail;\r | |
3f7c993d | 45 | } tail;\r |
9492e0b0 | 46 | uint32_t len;\r |
47 | uint32_t uid;\r | |
48 | uint32_t blockNo;\r | |
49 | uint32_t keyType;\r | |
50 | uint32_t nt;\r | |
51 | uint32_t ks1;\r | |
52 | } StateList_t;\r | |
53 | \r | |
54 | \r | |
55 | // wrapper function for multi-threaded lfsr_recovery32\r | |
56 | void* nested_worker_thread(void *arg)\r | |
57 | {\r | |
58 | struct Crypto1State *p1;\r | |
59 | StateList_t *statelist = arg;\r | |
60 | \r | |
3f7c993d | 61 | statelist->head.slhead = lfsr_recovery32(statelist->ks1, statelist->nt ^ statelist->uid);\r |
62 | for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; p1++);\r | |
63 | statelist->len = p1 - statelist->head.slhead;\r | |
64 | statelist->tail.sltail = --p1;\r | |
65 | qsort(statelist->head.slhead, statelist->len, sizeof(uint64_t), Compare16Bits);\r | |
f397b5cc | 66 | \r |
3f7c993d | 67 | return statelist->head.slhead;\r |
f397b5cc M |
68 | }\r |
69 | \r | |
9492e0b0 | 70 | int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t * resultKey, bool calibrate) \r |
f397b5cc | 71 | {\r |
c830303d | 72 | uint16_t i;\r |
f397b5cc | 73 | uint32_t uid;\r |
902cb3c0 | 74 | UsbCommand resp;\r |
c830303d | 75 | \r |
9492e0b0 | 76 | StateList_t statelists[2];\r |
77 | struct Crypto1State *p1, *p2, *p3, *p4;\r | |
78 | \r | |
f397b5cc | 79 | // flush queue\r |
f397b5cc | 80 | \r |
9492e0b0 | 81 | UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}};\r |
f397b5cc | 82 | memcpy(c.d.asBytes, key, 6);\r |
952f3dc7 | 83 | clearCommandBuffer();\r |
9492e0b0 | 84 | SendCommand(&c);\r |
f397b5cc | 85 | \r |
952f3dc7 | 86 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;\r |
c830303d | 87 | \r |
952f3dc7 | 88 | // error during nested\r |
89 | if (resp.arg[0]) return resp.arg[0];\r | |
90 | \r | |
0de8e387 | 91 | memcpy(&uid, resp.d.asBytes, 4);\r |
952f3dc7 | 92 | PrintAndLog("UID: %08x Block:%d Key: %c", uid, (uint16_t)resp.arg[2] & 0xff, (resp.arg[2] >> 8) ?'A':'B' );\r |
f397b5cc | 93 | \r |
0de8e387 | 94 | for (i = 0; i < 2; i++) {\r |
95 | statelists[i].blockNo = resp.arg[2] & 0xff;\r | |
96 | statelists[i].keyType = (resp.arg[2] >> 8) & 0xff;\r | |
97 | statelists[i].uid = uid;\r | |
98 | memcpy(&statelists[i].nt, (void *)(resp.d.asBytes + 4 + i * 8 + 0), 4);\r | |
99 | memcpy(&statelists[i].ks1, (void *)(resp.d.asBytes + 4 + i * 8 + 4), 4);\r | |
100 | }\r | |
f397b5cc | 101 | \r |
f397b5cc | 102 | // calc keys\r |
f397b5cc | 103 | \r |
9492e0b0 | 104 | pthread_t thread_id[2];\r |
105 | \r | |
106 | // create and run worker threads\r | |
107 | for (i = 0; i < 2; i++) {\r | |
108 | pthread_create(thread_id + i, NULL, nested_worker_thread, &statelists[i]);\r | |
f397b5cc M |
109 | }\r |
110 | \r | |
9492e0b0 | 111 | // wait for threads to terminate:\r |
112 | for (i = 0; i < 2; i++) {\r | |
3f7c993d | 113 | pthread_join(thread_id[i], (void*)&statelists[i].head.slhead);\r |
f397b5cc M |
114 | }\r |
115 | \r | |
f397b5cc | 116 | \r |
9492e0b0 | 117 | // the first 16 Bits of the cryptostate already contain part of our key.\r |
118 | // Create the intersection of the two lists based on these 16 Bits and\r | |
119 | // roll back the cryptostate\r | |
3f7c993d | 120 | p1 = p3 = statelists[0].head.slhead; \r |
121 | p2 = p4 = statelists[1].head.slhead;\r | |
122 | while (p1 <= statelists[0].tail.sltail && p2 <= statelists[1].tail.sltail) {\r | |
9492e0b0 | 123 | if (Compare16Bits(p1, p2) == 0) {\r |
124 | struct Crypto1State savestate, *savep = &savestate;\r | |
125 | savestate = *p1;\r | |
3f7c993d | 126 | while(Compare16Bits(p1, savep) == 0 && p1 <= statelists[0].tail.sltail) {\r |
9492e0b0 | 127 | *p3 = *p1;\r |
128 | lfsr_rollback_word(p3, statelists[0].nt ^ statelists[0].uid, 0);\r | |
129 | p3++;\r | |
130 | p1++;\r | |
131 | }\r | |
132 | savestate = *p2;\r | |
3f7c993d | 133 | while(Compare16Bits(p2, savep) == 0 && p2 <= statelists[1].tail.sltail) {\r |
9492e0b0 | 134 | *p4 = *p2;\r |
135 | lfsr_rollback_word(p4, statelists[1].nt ^ statelists[1].uid, 0);\r | |
136 | p4++;\r | |
137 | p2++;\r | |
138 | }\r | |
139 | }\r | |
140 | else {\r | |
141 | while (Compare16Bits(p1, p2) == -1) p1++;\r | |
142 | while (Compare16Bits(p1, p2) == 1) p2++;\r | |
143 | }\r | |
f397b5cc | 144 | }\r |
9492e0b0 | 145 | p3->even = 0; p3->odd = 0;\r |
146 | p4->even = 0; p4->odd = 0;\r | |
3f7c993d | 147 | statelists[0].len = p3 - statelists[0].head.slhead;\r |
148 | statelists[1].len = p4 - statelists[1].head.slhead;\r | |
149 | statelists[0].tail.sltail=--p3;\r | |
150 | statelists[1].tail.sltail=--p4;\r | |
9492e0b0 | 151 | \r |
152 | // the statelists now contain possible keys. The key we are searching for must be in the\r | |
153 | // intersection of both lists. Create the intersection:\r | |
3f7c993d | 154 | qsort(statelists[0].head.keyhead, statelists[0].len, sizeof(uint64_t), compar_int);\r |
155 | qsort(statelists[1].head.keyhead, statelists[1].len, sizeof(uint64_t), compar_int);\r | |
9492e0b0 | 156 | \r |
157 | uint64_t *p5, *p6, *p7;\r | |
3f7c993d | 158 | p5 = p7 = statelists[0].head.keyhead; \r |
159 | p6 = statelists[1].head.keyhead;\r | |
160 | while (p5 <= statelists[0].tail.keytail && p6 <= statelists[1].tail.keytail) {\r | |
9492e0b0 | 161 | if (compar_int(p5, p6) == 0) {\r |
162 | *p7++ = *p5++;\r | |
163 | p6++;\r | |
164 | }\r | |
165 | else {\r | |
166 | while (compar_int(p5, p6) == -1) p5++;\r | |
167 | while (compar_int(p5, p6) == 1) p6++;\r | |
168 | }\r | |
169 | }\r | |
3f7c993d | 170 | statelists[0].len = p7 - statelists[0].head.keyhead;\r |
171 | statelists[0].tail.keytail=--p7;\r | |
9492e0b0 | 172 | \r |
173 | memset(resultKey, 0, 6);\r | |
174 | // The list may still contain several key candidates. Test each of them with mfCheckKeys\r | |
175 | for (i = 0; i < statelists[0].len; i++) {\r | |
176 | uint8_t keyBlock[6];\r | |
177 | uint64_t key64;\r | |
3f7c993d | 178 | crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);\r |
9492e0b0 | 179 | num_to_bytes(key64, 6, keyBlock);\r |
180 | key64 = 0;\r | |
b10a759f | 181 | if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, 1, keyBlock, &key64)) {\r |
9492e0b0 | 182 | num_to_bytes(key64, 6, resultKey);\r |
183 | break;\r | |
184 | }\r | |
185 | }\r | |
186 | \r | |
3f7c993d | 187 | free(statelists[0].head.slhead);\r |
0de8e387 | 188 | free(statelists[1].head.slhead); \r |
f397b5cc M |
189 | return 0;\r |
190 | }\r | |
191 | \r | |
b10a759f | 192 | int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){\r |
9492e0b0 | 193 | \r |
f397b5cc M |
194 | *key = 0;\r |
195 | \r | |
b10a759f | 196 | UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType&0xff)<<8)), clear_trace, keycnt}};\r |
f397b5cc | 197 | memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r |
0de8e387 | 198 | \r |
199 | clearCommandBuffer();\r | |
9492e0b0 | 200 | SendCommand(&c);\r |
902cb3c0 | 201 | UsbCommand resp;\r |
202 | if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1;\r | |
203 | if ((resp.arg[0] & 0xff) != 0x01) return 2;\r | |
204 | *key = bytes_to_num(resp.d.asBytes, 6);\r | |
f397b5cc M |
205 | return 0;\r |
206 | }\r | |
8556b852 | 207 | \r |
55acbb2a M |
208 | // EMULATOR\r |
209 | \r | |
8556b852 | 210 | int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r |
545a1f38 | 211 | UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};\r |
0de8e387 | 212 | clearCommandBuffer();\r |
902cb3c0 | 213 | SendCommand(&c);\r |
5c065fa0 | 214 | UsbCommand resp;\r |
902cb3c0 | 215 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;\r |
216 | memcpy(data, resp.d.asBytes, blocksCount * 16);\r | |
8556b852 M |
217 | return 0;\r |
218 | }\r | |
219 | \r | |
220 | int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r | |
0de8e387 | 221 | return mfEmlSetMem_xt(data, blockNum, blocksCount, 16);\r |
222 | }\r | |
223 | \r | |
224 | int mfEmlSetMem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth) {\r | |
225 | UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, blockBtWidth}};\r | |
226 | memcpy(c.d.asBytes, data, blocksCount * blockBtWidth); \r | |
227 | \r | |
228 | clearCommandBuffer();\r | |
545a1f38 | 229 | SendCommand(&c);\r |
8556b852 M |
230 | return 0;\r |
231 | }\r | |
232 | \r | |
55acbb2a M |
233 | // "MAGIC" CARD\r |
234 | \r | |
c2731f37 | 235 | int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, uint8_t wipecard) {\r |
3bba7dea | 236 | \r |
c2731f37 | 237 | uint8_t params = MAGIC_SINGLE;\r |
238 | uint8_t block0[16];\r | |
239 | memset(block0, 0x00, sizeof(block0));\r | |
240 | \r | |
241 | \r | |
242 | int old = mfCGetBlock(0, block0, params);\r | |
3bba7dea | 243 | if (old == 0) {\r |
c2731f37 | 244 | PrintAndLog("old block 0: %s", sprint_hex(block0, sizeof(block0)));\r |
80b1b53f | 245 | } else {\r |
3bba7dea JH |
246 | PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0.");\r |
247 | }\r | |
248 | \r | |
249 | // fill in the new values\r | |
250 | // UID\r | |
251 | memcpy(block0, uid, 4); \r | |
252 | // Mifare UID BCC\r | |
253 | block0[4] = block0[0]^block0[1]^block0[2]^block0[3];\r | |
254 | // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed)\r | |
c2731f37 | 255 | if ( sak != NULL )\r |
3bba7dea | 256 | block0[5]=sak[0];\r |
c2731f37 | 257 | \r |
258 | if ( atqa != NULL ) {\r | |
3bba7dea JH |
259 | block0[6]=atqa[1];\r |
260 | block0[7]=atqa[0];\r | |
80b1b53f | 261 | }\r |
3bba7dea | 262 | PrintAndLog("new block 0: %s", sprint_hex(block0,16));\r |
c2731f37 | 263 | \r |
264 | if ( wipecard ) params |= MAGIC_WIPE; \r | |
265 | if ( oldUID == NULL) params |= MAGIC_UID;\r | |
266 | \r | |
267 | return mfCSetBlock(0, block0, oldUID, params);\r | |
f774db95 M |
268 | }\r |
269 | \r | |
c2731f37 | 270 | int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, uint8_t params) {\r |
0675f200 | 271 | \r |
1b492a97 | 272 | uint8_t isOK = 0;\r |
c2731f37 | 273 | UsbCommand c = {CMD_MIFARE_CSETBLOCK, {params, blockNo, 0}};\r |
f774db95 | 274 | memcpy(c.d.asBytes, data, 16); \r |
0de8e387 | 275 | clearCommandBuffer();\r |
276 | SendCommand(&c);\r | |
1b492a97 | 277 | UsbCommand resp;\r |
c2731f37 | 278 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r |
902cb3c0 | 279 | isOK = resp.arg[0] & 0xff;\r |
80b1b53f | 280 | if (uid != NULL) \r |
281 | memcpy(uid, resp.d.asBytes, 4);\r | |
282 | if (!isOK) \r | |
283 | return 2;\r | |
0675f200 M |
284 | } else {\r |
285 | PrintAndLog("Command execute timeout");\r | |
286 | return 1;\r | |
287 | }\r | |
288 | return 0;\r | |
289 | }\r | |
545a1f38 M |
290 | \r |
291 | int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {\r | |
292 | uint8_t isOK = 0;\r | |
c2731f37 | 293 | UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, blockNo, 0}}; \r |
0de8e387 | 294 | clearCommandBuffer();\r |
545a1f38 | 295 | SendCommand(&c);\r |
0de8e387 | 296 | UsbCommand resp;\r |
902cb3c0 | 297 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r |
298 | isOK = resp.arg[0] & 0xff;\r | |
299 | memcpy(data, resp.d.asBytes, 16);\r | |
545a1f38 M |
300 | if (!isOK) return 2;\r |
301 | } else {\r | |
302 | PrintAndLog("Command execute timeout");\r | |
303 | return 1;\r | |
304 | }\r | |
305 | return 0;\r | |
306 | }\r | |
55acbb2a M |
307 | \r |
308 | // SNIFFER\r | |
309 | \r | |
e0c635d1 M |
310 | // constants\r |
311 | static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00};\r | |
312 | \r | |
55acbb2a | 313 | // variables\r |
e4691591 | 314 | char logHexFileName[FILE_PATH_SIZE] = {0x00};\r |
e0c635d1 | 315 | static uint8_t traceCard[4096] = {0x00};\r |
e4691591 | 316 | static char traceFileName[FILE_PATH_SIZE] = {0x00};\r |
55acbb2a M |
317 | static int traceState = TRACE_IDLE;\r |
318 | static uint8_t traceCurBlock = 0;\r | |
319 | static uint8_t traceCurKey = 0;\r | |
320 | \r | |
321 | struct Crypto1State *traceCrypto1 = NULL;\r | |
322 | \r | |
046fd766 | 323 | struct Crypto1State *revstate = NULL;\r |
324 | \r | |
325 | uint64_t key = 0;\r | |
326 | uint32_t ks2 = 0;\r | |
327 | uint32_t ks3 = 0;\r | |
55acbb2a | 328 | \r |
046fd766 | 329 | uint32_t uid = 0; // serial number\r |
330 | uint32_t nt =0; // tag challenge\r | |
331 | uint32_t nr_enc =0; // encrypted reader challenge\r | |
332 | uint32_t ar_enc =0; // encrypted reader response\r | |
333 | uint32_t at_enc =0; // encrypted tag response\r | |
55acbb2a | 334 | \r |
e0c635d1 M |
335 | int isTraceCardEmpty(void) {\r |
336 | return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));\r | |
337 | }\r | |
55acbb2a | 338 | \r |
e0c635d1 M |
339 | int isBlockEmpty(int blockN) {\r |
340 | for (int i = 0; i < 16; i++) \r | |
341 | if (traceCard[blockN * 16 + i] != 0) return 0;\r | |
55acbb2a | 342 | \r |
e0c635d1 M |
343 | return 1;\r |
344 | }\r | |
345 | \r | |
346 | int isBlockTrailer(int blockN) {\r | |
347 | return ((blockN & 0x03) == 0x03);\r | |
348 | }\r | |
349 | \r | |
350 | int loadTraceCard(uint8_t *tuid) {\r | |
351 | FILE * f;\r | |
80b1b53f | 352 | char buf[64] = {0x00};\r |
353 | uint8_t buf8[64] = {0x00};\r | |
e0c635d1 M |
354 | int i, blockNum;\r |
355 | \r | |
80b1b53f | 356 | if (!isTraceCardEmpty()) \r |
357 | saveTraceCard();\r | |
358 | \r | |
55acbb2a M |
359 | memset(traceCard, 0x00, 4096);\r |
360 | memcpy(traceCard, tuid + 3, 4);\r | |
80b1b53f | 361 | \r |
e0c635d1 M |
362 | FillFileNameByUID(traceFileName, tuid, ".eml", 7);\r |
363 | \r | |
364 | f = fopen(traceFileName, "r");\r | |
664bb5ae | 365 | if (!f) return 1;\r |
e0c635d1 M |
366 | \r |
367 | blockNum = 0;\r | |
e4691591 | 368 | \r |
e0c635d1 | 369 | while(!feof(f)){\r |
e4691591 | 370 | \r |
e0c635d1 | 371 | memset(buf, 0, sizeof(buf));\r |
759c16b3 | 372 | if (fgets(buf, sizeof(buf), f) == NULL) {\r |
046fd766 | 373 | PrintAndLog("File reading error.");\r |
1b492a97 | 374 | fclose(f);\r |
759c16b3 | 375 | return 2;\r |
046fd766 | 376 | }\r |
e0c635d1 M |
377 | \r |
378 | if (strlen(buf) < 32){\r | |
379 | if (feof(f)) break;\r | |
380 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r | |
1b492a97 | 381 | fclose(f);\r |
e0c635d1 M |
382 | return 2;\r |
383 | }\r | |
384 | for (i = 0; i < 32; i += 2)\r | |
385 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
386 | \r | |
387 | memcpy(traceCard + blockNum * 16, buf8, 16);\r | |
388 | \r | |
389 | blockNum++;\r | |
390 | }\r | |
391 | fclose(f);\r | |
392 | \r | |
393 | return 0;\r | |
394 | }\r | |
395 | \r | |
396 | int saveTraceCard(void) {\r | |
397 | FILE * f;\r | |
398 | \r | |
399 | if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;\r | |
400 | \r | |
401 | f = fopen(traceFileName, "w+");\r | |
664bb5ae | 402 | if ( !f ) return 1;\r |
e4691591 | 403 | \r |
e0c635d1 M |
404 | for (int i = 0; i < 64; i++) { // blocks\r |
405 | for (int j = 0; j < 16; j++) // bytes\r | |
406 | fprintf(f, "%02x", *(traceCard + i * 16 + j)); \r | |
407 | fprintf(f,"\n");\r | |
408 | }\r | |
409 | fclose(f);\r | |
e0c635d1 M |
410 | return 0;\r |
411 | }\r | |
412 | \r | |
413 | int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {\r | |
414 | \r | |
80b1b53f | 415 | if (traceCrypto1) \r |
416 | crypto1_destroy(traceCrypto1);\r | |
417 | \r | |
e0c635d1 M |
418 | traceCrypto1 = NULL;\r |
419 | \r | |
80b1b53f | 420 | if (wantSaveToEmlFile) \r |
421 | loadTraceCard(tuid);\r | |
422 | \r | |
55acbb2a M |
423 | traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];\r |
424 | traceCard[5] = sak;\r | |
425 | memcpy(&traceCard[6], atqa, 2);\r | |
426 | traceCurBlock = 0;\r | |
427 | uid = bytes_to_num(tuid + 3, 4);\r | |
428 | \r | |
429 | traceState = TRACE_IDLE;\r | |
430 | \r | |
431 | return 0;\r | |
432 | }\r | |
433 | \r | |
434 | void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){\r | |
435 | uint8_t bt = 0;\r | |
436 | int i;\r | |
437 | \r | |
438 | if (len != 1) {\r | |
439 | for (i = 0; i < len; i++)\r | |
440 | data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];\r | |
441 | } else {\r | |
442 | bt = 0;\r | |
443 | for (i = 0; i < 4; i++)\r | |
444 | bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;\r | |
445 | \r | |
446 | data[0] = bt;\r | |
447 | }\r | |
448 | return;\r | |
449 | }\r | |
450 | \r | |
451 | \r | |
a501c82b | 452 | int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {\r |
55acbb2a M |
453 | uint8_t data[64];\r |
454 | \r | |
455 | if (traceState == TRACE_ERROR) return 1;\r | |
456 | if (len > 64) {\r | |
457 | traceState = TRACE_ERROR;\r | |
458 | return 1;\r | |
459 | }\r | |
460 | \r | |
461 | memcpy(data, data_src, len);\r | |
462 | if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {\r | |
463 | mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r | |
464 | PrintAndLog("dec> %s", sprint_hex(data, len));\r | |
465 | AddLogHex(logHexFileName, "dec> ", data, len); \r | |
466 | }\r | |
467 | \r | |
468 | switch (traceState) {\r | |
469 | case TRACE_IDLE: \r | |
8b6a93df M |
470 | // check packet crc16!\r |
471 | if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {\r | |
472 | PrintAndLog("dec> CRC ERROR!!!");\r | |
473 | AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!"); \r | |
474 | traceState = TRACE_ERROR; // do not decrypt the next commands\r | |
475 | return 1;\r | |
476 | }\r | |
55acbb2a M |
477 | \r |
478 | // AUTHENTICATION\r | |
a61b4976 | 479 | if ((len == 4) && ((data[0] == 0x60) || (data[0] == 0x61))) {\r |
55acbb2a M |
480 | traceState = TRACE_AUTH1;\r |
481 | traceCurBlock = data[1];\r | |
482 | traceCurKey = data[0] == 60 ? 1:0;\r | |
483 | return 0;\r | |
484 | }\r | |
485 | \r | |
486 | // READ\r | |
487 | if ((len ==4) && ((data[0] == 0x30))) {\r | |
488 | traceState = TRACE_READ_DATA;\r | |
489 | traceCurBlock = data[1];\r | |
490 | return 0;\r | |
491 | }\r | |
492 | \r | |
493 | // WRITE\r | |
494 | if ((len ==4) && ((data[0] == 0xA0))) {\r | |
495 | traceState = TRACE_WRITE_OK;\r | |
496 | traceCurBlock = data[1];\r | |
497 | return 0;\r | |
498 | }\r | |
499 | \r | |
500 | // HALT\r | |
501 | if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {\r | |
502 | traceState = TRACE_ERROR; // do not decrypt the next commands\r | |
503 | return 0;\r | |
504 | }\r | |
505 | \r | |
506 | return 0;\r | |
507 | break;\r | |
508 | \r | |
509 | case TRACE_READ_DATA: \r | |
510 | if (len == 18) {\r | |
511 | traceState = TRACE_IDLE;\r | |
512 | \r | |
e0c635d1 M |
513 | if (isBlockTrailer(traceCurBlock)) {\r |
514 | memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);\r | |
515 | } else {\r | |
516 | memcpy(traceCard + traceCurBlock * 16, data, 16);\r | |
517 | }\r | |
518 | if (wantSaveToEmlFile) saveTraceCard();\r | |
55acbb2a M |
519 | return 0;\r |
520 | } else {\r | |
521 | traceState = TRACE_ERROR;\r | |
522 | return 1;\r | |
523 | }\r | |
524 | break;\r | |
525 | \r | |
526 | case TRACE_WRITE_OK: \r | |
a61b4976 | 527 | if ((len == 1) && (data[0] == 0x0a)) {\r |
55acbb2a M |
528 | traceState = TRACE_WRITE_DATA;\r |
529 | \r | |
530 | return 0;\r | |
531 | } else {\r | |
532 | traceState = TRACE_ERROR;\r | |
533 | return 1;\r | |
534 | }\r | |
535 | break;\r | |
536 | \r | |
537 | case TRACE_WRITE_DATA: \r | |
538 | if (len == 18) {\r | |
539 | traceState = TRACE_IDLE;\r | |
540 | \r | |
541 | memcpy(traceCard + traceCurBlock * 16, data, 16);\r | |
e0c635d1 | 542 | if (wantSaveToEmlFile) saveTraceCard();\r |
55acbb2a M |
543 | return 0;\r |
544 | } else {\r | |
545 | traceState = TRACE_ERROR;\r | |
546 | return 1;\r | |
547 | }\r | |
548 | break;\r | |
549 | \r | |
550 | case TRACE_AUTH1: \r | |
551 | if (len == 4) {\r | |
552 | traceState = TRACE_AUTH2;\r | |
55acbb2a M |
553 | nt = bytes_to_num(data, 4);\r |
554 | return 0;\r | |
555 | } else {\r | |
556 | traceState = TRACE_ERROR;\r | |
557 | return 1;\r | |
558 | }\r | |
559 | break;\r | |
560 | \r | |
561 | case TRACE_AUTH2: \r | |
562 | if (len == 8) {\r | |
563 | traceState = TRACE_AUTH_OK;\r | |
564 | \r | |
565 | nr_enc = bytes_to_num(data, 4);\r | |
566 | ar_enc = bytes_to_num(data + 4, 4);\r | |
567 | return 0;\r | |
568 | } else {\r | |
569 | traceState = TRACE_ERROR;\r | |
570 | return 1;\r | |
571 | }\r | |
572 | break;\r | |
573 | \r | |
574 | case TRACE_AUTH_OK: \r | |
575 | if (len ==4) {\r | |
576 | traceState = TRACE_IDLE;\r | |
577 | \r | |
578 | at_enc = bytes_to_num(data, 4);\r | |
579 | \r | |
580 | // decode key here)\r | |
a61b4976 | 581 | ks2 = ar_enc ^ prng_successor(nt, 64);\r |
582 | ks3 = at_enc ^ prng_successor(nt, 96);\r | |
583 | revstate = lfsr_recovery64(ks2, ks3);\r | |
584 | lfsr_rollback_word(revstate, 0, 0);\r | |
585 | lfsr_rollback_word(revstate, 0, 0);\r | |
586 | lfsr_rollback_word(revstate, nr_enc, 1);\r | |
587 | lfsr_rollback_word(revstate, uid ^ nt, 0);\r | |
588 | \r | |
046fd766 | 589 | crypto1_get_lfsr(revstate, &key);\r |
590 | printf("Key: %012"llx"\n",key);\r | |
591 | AddLogUint64(logHexFileName, "key: ", key); \r | |
55acbb2a | 592 | \r |
e0c635d1 M |
593 | int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r |
594 | if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r | |
595 | \r | |
55acbb2a | 596 | if (traceCurKey) {\r |
046fd766 | 597 | num_to_bytes(key, 6, traceCard + blockShift + 10);\r |
55acbb2a | 598 | } else {\r |
046fd766 | 599 | num_to_bytes(key, 6, traceCard + blockShift);\r |
55acbb2a | 600 | }\r |
e0c635d1 | 601 | if (wantSaveToEmlFile) saveTraceCard();\r |
55acbb2a M |
602 | \r |
603 | if (traceCrypto1) {\r | |
604 | crypto1_destroy(traceCrypto1);\r | |
605 | }\r | |
606 | \r | |
607 | // set cryptosystem state\r | |
608 | traceCrypto1 = lfsr_recovery64(ks2, ks3);\r | |
609 | \r | |
610 | // nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;\r | |
611 | \r | |
046fd766 | 612 | /* traceCrypto1 = crypto1_create(key); // key in lfsr\r |
55acbb2a M |
613 | crypto1_word(traceCrypto1, nt ^ uid, 0);\r |
614 | crypto1_word(traceCrypto1, ar, 1);\r | |
615 | crypto1_word(traceCrypto1, 0, 0);\r | |
616 | crypto1_word(traceCrypto1, 0, 0);*/\r | |
617 | \r | |
618 | return 0;\r | |
619 | } else {\r | |
620 | traceState = TRACE_ERROR;\r | |
621 | return 1;\r | |
622 | }\r | |
623 | break;\r | |
624 | \r | |
625 | default: \r | |
626 | traceState = TRACE_ERROR;\r | |
627 | return 1;\r | |
628 | }\r | |
629 | \r | |
630 | return 0;\r | |
631 | }\r | |
e98572a1 | 632 | \r |
633 | int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len){\r | |
634 | /*\r | |
635 | uint32_t nt; // tag challenge\r | |
3bc7b13d | 636 | uint32_t nr_enc; // encrypted reader challenge\r |
e98572a1 | 637 | uint32_t ar_enc; // encrypted reader response\r |
638 | uint32_t at_enc; // encrypted tag response\r | |
639 | */\r | |
3bc7b13d | 640 | \r |
641 | struct Crypto1State *pcs = NULL;\r | |
642 | \r | |
e98572a1 | 643 | ks2 = ar_enc ^ prng_successor(nt, 64);\r |
644 | ks3 = at_enc ^ prng_successor(nt, 96);\r | |
3bc7b13d | 645 | \r |
646 | PrintAndLog("Decrypting data with:");\r | |
647 | PrintAndLog(" nt: %08x",nt);\r | |
648 | PrintAndLog(" ar_enc: %08x",ar_enc);\r | |
649 | PrintAndLog(" at_enc: %08x",at_enc);\r | |
650 | PrintAndLog("\nEncrypted data: [%s]", sprint_hex(data,len) );\r | |
651 | \r | |
652 | pcs = lfsr_recovery64(ks2, ks3);\r | |
653 | mf_crypto1_decrypt(pcs, data, len, FALSE);\r | |
e98572a1 | 654 | PrintAndLog("Decrypted data: [%s]", sprint_hex(data,len) );\r |
3bc7b13d | 655 | crypto1_destroy(pcs);\r |
e98572a1 | 656 | return 0;\r |
657 | }\r |