]>
Commit | Line | Data |
---|---|---|
4f131b53 | 1 | //----------------------------------------------------------------------------- |
0d2624a0 | 2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> |
4f131b53 | 3 | // Copyright (C) Merlok - 2017 |
4 | // | |
5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
6 | // at your option, any later version. See the LICENSE.txt file for the text of | |
7 | // the license. | |
8 | //----------------------------------------------------------------------------- | |
0d2624a0 | 9 | // Command: hf list. It shows data from arm buffer. |
4f131b53 | 10 | //----------------------------------------------------------------------------- |
11 | ||
12 | #include "cmdhflist.h" | |
13 | ||
14 | #include <stdlib.h> | |
15 | #include <stdio.h> | |
16 | #include <string.h> | |
6612a5a2 | 17 | #include <stdint.h> |
18 | #include <stdbool.h> | |
19 | #include "util.h" | |
6612a5a2 | 20 | #include "ui.h" |
d3bcdbda | 21 | #include "cliparser/cliparser.h" |
0d2624a0 | 22 | #include "comms.h" |
6612a5a2 | 23 | #include "iso14443crc.h" |
0d2624a0 | 24 | #include "iso15693tools.h" |
6612a5a2 | 25 | #include "parity.h" |
26 | #include "protocols.h" | |
7b215d14 | 27 | #include "crapto1/crapto1.h" |
fdd9395d OM |
28 | #include "mifare/mifarehost.h" |
29 | #include "mifare/mifaredefault.h" | |
0d2624a0 | 30 | #include "usb_cmd.h" |
53fb848a | 31 | #include "pcsc.h" |
4f131b53 | 32 | |
0d2624a0 | 33 | typedef struct { |
34 | uint32_t uid; // UID | |
35 | uint32_t nt; // tag challenge | |
36 | uint32_t nt_enc; // encrypted tag challenge | |
37 | uint8_t nt_enc_par; // encrypted tag challenge parity | |
38 | uint32_t nr_enc; // encrypted reader challenge | |
39 | uint32_t ar_enc; // encrypted reader response | |
40 | uint8_t ar_enc_par; // encrypted reader response parity | |
41 | uint32_t at_enc; // encrypted tag response | |
42 | uint8_t at_enc_par; // encrypted tag response parity | |
43 | bool first_auth; // is first authentication | |
53fb848a | 44 | uint32_t ks2; // ar ^ ar_enc |
0d2624a0 | 45 | uint32_t ks3; // at ^ at_enc |
46 | } TAuthData; | |
4f131b53 | 47 | |
6612a5a2 | 48 | enum MifareAuthSeq { |
49 | masNone, | |
50 | masNt, | |
51 | masNrAr, | |
52 | masAt, | |
747885a6 | 53 | masAuthComplete, |
aadc6bf1 | 54 | masFirstData, |
6612a5a2 | 55 | masData, |
6612a5a2 | 56 | masError, |
57 | }; | |
0d2624a0 | 58 | |
6612a5a2 | 59 | static enum MifareAuthSeq MifareAuthState; |
aadc6bf1 OM |
60 | static TAuthData AuthData; |
61 | ||
0d2624a0 | 62 | static void ClearAuthData() { |
aadc6bf1 OM |
63 | AuthData.uid = 0; |
64 | AuthData.nt = 0; | |
7b215d14 | 65 | AuthData.first_auth = true; |
e01bc794 OM |
66 | AuthData.ks2 = 0; |
67 | AuthData.ks3 = 0; | |
aadc6bf1 | 68 | } |
6612a5a2 | 69 | |
70 | /** | |
71 | * @brief iso14443A_CRC_check Checks CRC in command or response | |
72 | * @param isResponse | |
73 | * @param data | |
74 | * @param len | |
75 | * @return 0 : CRC-command, CRC not ok | |
76 | * 1 : CRC-command, CRC ok | |
77 | * 2 : Not crc-command | |
78 | */ | |
0d2624a0 | 79 | static uint8_t iso14443A_CRC_check(bool isResponse, uint8_t* data, uint8_t len) |
6612a5a2 | 80 | { |
81 | uint8_t b1,b2; | |
82 | ||
83 | if(len <= 2) return 2; | |
84 | ||
85 | if(isResponse & (len < 6)) return 2; | |
53fb848a | 86 | |
87 | ComputeCrc14443(CRC_14443_A, data, len-2, &b1, &b2); | |
88 | if (b1 != data[len-2] || b2 != data[len-1]) { | |
89 | return 0; | |
90 | } else { | |
91 | return 1; | |
92 | } | |
93 | } | |
94 | ||
95 | ||
96 | static uint8_t iso14443_4_CRC_check(uint8_t* data, uint8_t len) | |
97 | { | |
98 | uint8_t b1,b2; | |
99 | ||
100 | if(len <= 2) return 2; | |
101 | ||
6612a5a2 | 102 | ComputeCrc14443(CRC_14443_A, data, len-2, &b1, &b2); |
103 | if (b1 != data[len-2] || b2 != data[len-1]) { | |
104 | return 0; | |
105 | } else { | |
106 | return 1; | |
107 | } | |
108 | } | |
109 | ||
0d2624a0 | 110 | |
111 | static uint8_t mifare_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
6612a5a2 | 112 | { |
113 | switch(MifareAuthState) { | |
114 | case masNone: | |
6612a5a2 | 115 | case masError: |
116 | return iso14443A_CRC_check(isResponse, data, len); | |
117 | default: | |
118 | return 2; | |
119 | } | |
6612a5a2 | 120 | } |
121 | ||
0d2624a0 | 122 | |
123 | /** | |
124 | * @brief iso14443B_CRC_check Checks CRC in command or response | |
125 | * @param isResponse | |
126 | * @param data | |
127 | * @param len | |
128 | * @return 0 : CRC-command, CRC not ok | |
129 | * 1 : CRC-command, CRC ok | |
130 | * 2 : Not crc-command | |
131 | */ | |
132 | static uint8_t iso14443B_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
133 | { | |
134 | uint8_t b1,b2; | |
135 | ||
136 | if(len <= 2) return 2; | |
137 | ||
138 | ComputeCrc14443(CRC_14443_B, data, len-2, &b1, &b2); | |
139 | if(b1 != data[len-2] || b2 != data[len-1]) { | |
140 | return 0; | |
141 | } else { | |
142 | return 1; | |
143 | } | |
144 | } | |
145 | ||
146 | ||
53fb848a | 147 | static uint8_t iso15693_CRC_check(uint8_t* d, uint16_t n) |
0d2624a0 | 148 | { |
149 | if (n <= 2) return 2; | |
53fb848a | 150 | |
0d2624a0 | 151 | return (Iso15693Crc(d, n) == ISO15693_CRC_CHECK ? 1 : 0); |
152 | } | |
153 | ||
154 | ||
155 | /** | |
156 | * @brief iclass_CRC_Ok Checks CRC in command or response | |
157 | * @param isResponse | |
158 | * @param data | |
159 | * @param len | |
160 | * @return 0 : CRC-command, CRC not ok | |
53fb848a | 161 | * 1 : CRC-command, CRC ok |
0d2624a0 | 162 | * 2 : Not crc-command |
163 | */ | |
164 | uint8_t iclass_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
165 | { | |
166 | if(len < 4) return 2;//CRC commands (and responses) are all at least 4 bytes | |
167 | ||
168 | uint8_t b1, b2; | |
169 | ||
170 | if(!isResponse)//Commands to tag | |
171 | { | |
172 | /** | |
173 | These commands should have CRC. Total length leftmost | |
53fb848a | 174 | 4 READ |
0d2624a0 | 175 | 4 READ4 |
176 | 12 UPDATE - unsecured, ends with CRC16 | |
177 | 14 UPDATE - secured, ends with signature instead | |
178 | 4 PAGESEL | |
179 | **/ | |
180 | if(len == 4 || len == 12)//Covers three of them | |
181 | { | |
182 | //Don't include the command byte | |
183 | ComputeCrc14443(CRC_ICLASS, (data+1), len-3, &b1, &b2); | |
184 | return b1 == data[len -2] && b2 == data[len-1]; | |
185 | } | |
186 | return 2; | |
187 | }else{ | |
188 | /** | |
189 | These tag responses should have CRC. Total length leftmost | |
190 | ||
53fb848a | 191 | 10 READ data[8] crc[2] |
192 | 34 READ4 data[32]crc[2] | |
193 | 10 UPDATE data[8] crc[2] | |
194 | 10 SELECT csn[8] crc[2] | |
0d2624a0 | 195 | 10 IDENTIFY asnb[8] crc[2] |
196 | 10 PAGESEL block1[8] crc[2] | |
197 | 10 DETECT csn[8] crc[2] | |
198 | ||
199 | These should not | |
200 | ||
53fb848a | 201 | 4 CHECK chip_response[4] |
0d2624a0 | 202 | 8 READCHECK data[8] |
203 | 1 ACTALL sof[1] | |
53fb848a | 204 | 1 ACT sof[1] |
0d2624a0 | 205 | |
206 | In conclusion, without looking at the command; any response | |
207 | of length 10 or 34 should have CRC | |
208 | **/ | |
209 | if(len != 10 && len != 34) return true; | |
210 | ||
211 | ComputeCrc14443(CRC_ICLASS, data, len-2, &b1, &b2); | |
212 | return b1 == data[len -2] && b2 == data[len-1]; | |
213 | } | |
214 | } | |
215 | ||
216 | ||
3d2c9c9b | 217 | void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { |
868deeb7 | 218 | switch(cmd[0]) |
219 | { | |
3d2c9c9b | 220 | case ICLASS_CMD_ACTALL: snprintf(exp, size, "ACTALL"); break; |
221 | case ICLASS_CMD_READ_OR_IDENTIFY: { | |
222 | if (cmdsize > 1){ | |
868deeb7 | 223 | snprintf(exp,size,"READ(%d)",cmd[1]); |
3d2c9c9b | 224 | } else { |
868deeb7 | 225 | snprintf(exp,size,"IDENTIFY"); |
226 | } | |
227 | break; | |
228 | } | |
ae60ceca | 229 | case ICLASS_CMD_SELECT: snprintf(exp,size, "SELECT"); break; |
230 | case ICLASS_CMD_PAGESEL: snprintf(exp,size, "PAGESEL(%d)", cmd[1]); break; | |
231 | case ICLASS_CMD_READCHECK_KC: snprintf(exp,size, "READCHECK[Kc](%d)", cmd[1]); break; | |
232 | case ICLASS_CMD_READCHECK_KD: snprintf(exp,size, "READCHECK[Kd](%d)", cmd[1]); break; | |
233 | case ICLASS_CMD_CHECK_KC: | |
234 | case ICLASS_CMD_CHECK_KD: snprintf(exp,size, "CHECK"); break; | |
235 | case ICLASS_CMD_DETECT: snprintf(exp,size, "DETECT"); break; | |
236 | case ICLASS_CMD_HALT: snprintf(exp,size, "HALT"); break; | |
237 | case ICLASS_CMD_UPDATE: snprintf(exp,size, "UPDATE(%d)",cmd[1]); break; | |
238 | case ICLASS_CMD_ACT: snprintf(exp,size, "ACT"); break; | |
239 | case ICLASS_CMD_READ4: snprintf(exp,size, "READ4(%d)",cmd[1]); break; | |
240 | default: snprintf(exp,size, "?"); break; | |
868deeb7 | 241 | } |
242 | return; | |
243 | } | |
244 | ||
0d2624a0 | 245 | |
1f4789fe | 246 | void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { |
247 | if (cmdsize >= 2) { | |
248 | switch (cmd[1]) { | |
249 | // Mandatory Commands, all Tags must support them: | |
250 | case ISO15693_INVENTORY :snprintf(exp, size, "INVENTORY");return; | |
251 | case ISO15693_STAYQUIET :snprintf(exp, size, "STAY_QUIET");return; | |
252 | // Optional Commands, Tags may support them: | |
253 | case ISO15693_READBLOCK :snprintf(exp, size, "READBLOCK");return; | |
254 | case ISO15693_WRITEBLOCK :snprintf(exp, size, "WRITEBLOCK");return; | |
255 | case ISO15693_LOCKBLOCK :snprintf(exp, size, "LOCKBLOCK");return; | |
256 | case ISO15693_READ_MULTI_BLOCK :snprintf(exp, size, "READ_MULTI_BLOCK");return; | |
257 | case ISO15693_WRITE_MULTI_BLOCK :snprintf(exp, size, "WRITE_MULTI_BLOCK");return; | |
258 | case ISO15693_SELECT :snprintf(exp, size, "SELECT");return; | |
259 | case ISO15693_RESET_TO_READY :snprintf(exp, size, "RESET_TO_READY");return; | |
260 | case ISO15693_WRITE_AFI :snprintf(exp, size, "WRITE_AFI");return; | |
261 | case ISO15693_LOCK_AFI :snprintf(exp, size, "LOCK_AFI");return; | |
262 | case ISO15693_WRITE_DSFID :snprintf(exp, size, "WRITE_DSFID");return; | |
263 | case ISO15693_LOCK_DSFID :snprintf(exp, size, "LOCK_DSFID");return; | |
264 | case ISO15693_GET_SYSTEM_INFO :snprintf(exp, size, "GET_SYSTEM_INFO");return; | |
265 | case ISO15693_READ_MULTI_SECSTATUS :snprintf(exp, size, "READ_MULTI_SECSTATUS");return; | |
266 | default: break; | |
267 | } | |
268 | ||
269 | if (cmd[1] > ISO15693_STAYQUIET && cmd[1] < ISO15693_READBLOCK) snprintf(exp, size, "Mandatory RFU"); | |
270 | else if (cmd[1] > ISO15693_READ_MULTI_SECSTATUS && cmd[1] <= 0x9F) snprintf(exp, size, "Optional RFU"); | |
271 | else if ( cmd[1] >= 0xA0 && cmd[1] <= 0xDF ) snprintf(exp, size, "Custom command"); | |
272 | else if ( cmd[1] >= 0xE0 && cmd[1] <= 0xFF ) snprintf(exp, size, "Proprietary command"); | |
273 | } | |
868deeb7 | 274 | } |
275 | ||
276 | ||
277 | void annotateTopaz(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) | |
278 | { | |
279 | switch(cmd[0]) { | |
53fb848a | 280 | case TOPAZ_REQA :snprintf(exp, size, "REQA");break; |
281 | case TOPAZ_WUPA :snprintf(exp, size, "WUPA");break; | |
282 | case TOPAZ_RID :snprintf(exp, size, "RID");break; | |
283 | case TOPAZ_RALL :snprintf(exp, size, "RALL");break; | |
284 | case TOPAZ_READ :snprintf(exp, size, "READ");break; | |
285 | case TOPAZ_WRITE_E :snprintf(exp, size, "WRITE-E");break; | |
286 | case TOPAZ_WRITE_NE :snprintf(exp, size, "WRITE-NE");break; | |
287 | case TOPAZ_RSEG :snprintf(exp, size, "RSEG");break; | |
288 | case TOPAZ_READ8 :snprintf(exp, size, "READ8");break; | |
289 | case TOPAZ_WRITE_E8 :snprintf(exp, size, "WRITE-E8");break; | |
290 | case TOPAZ_WRITE_NE8 :snprintf(exp, size, "WRITE-NE8");break; | |
868deeb7 | 291 | default: snprintf(exp,size,"?"); break; |
292 | } | |
293 | } | |
294 | ||
295 | ||
53fb848a | 296 | void annotateIso7816(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) |
297 | { | |
298 | switch ( cmd[1] ){ | |
299 | case ISO7816_READ_BINARY :snprintf(exp, size, "READ BINARY");break; | |
300 | case ISO7816_WRITE_BINARY :snprintf(exp, size, "WRITE BINARY");break; | |
301 | case ISO7816_UPDATE_BINARY :snprintf(exp, size, "UPDATE BINARY");break; | |
302 | case ISO7816_ERASE_BINARY :snprintf(exp, size, "ERASE BINARY");break; | |
303 | case ISO7816_READ_RECORDS :snprintf(exp, size, "READ RECORD(S)");break; | |
304 | case ISO7816_WRITE_RECORD :snprintf(exp, size, "WRITE RECORD");break; | |
305 | case ISO7816_APPEND_RECORD :snprintf(exp, size, "APPEND RECORD");break; | |
306 | case ISO7816_UPDATE_DATA :snprintf(exp, size, "UPDATE DATA");break; | |
307 | case ISO7816_GET_DATA :snprintf(exp, size, "GET DATA");break; | |
308 | case ISO7816_PUT_DATA :snprintf(exp, size, "PUT DATA");break; | |
309 | case ISO7816_SELECT_FILE :snprintf(exp, size, "SELECT FILE");break; | |
310 | case ISO7816_VERIFY :snprintf(exp, size, "VERIFY");break; | |
311 | case ISO7816_INTERNAL_AUTHENTICATE :snprintf(exp, size, "INTERNAL AUTHENTICATE");break; | |
312 | case ISO7816_EXTERNAL_AUTHENTICATE :snprintf(exp, size, "EXTERNAL AUTHENTICATE");break; | |
313 | case ISO7816_GET_CHALLENGE :snprintf(exp, size, "GET CHALLENGE");break; | |
314 | case ISO7816_MANAGE_CHANNEL :snprintf(exp, size, "MANAGE CHANNEL");break; | |
315 | case ISO7816_GET_RESPONSE :snprintf(exp, size, "GET RESPONSE");break; | |
316 | case ISO7816_ENVELOPE :snprintf(exp, size, "ENVELOPE");break; | |
317 | case ISO7816_GET_PROCESSING_OPTIONS :snprintf(exp, size, "GET PROCESSING OPTIONS");break; | |
318 | default :snprintf(exp,size,"?"); break; | |
319 | } | |
320 | } | |
321 | ||
322 | ||
d3bcdbda | 323 | void annotateIso14443_4(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { |
53fb848a | 324 | // S-block |
325 | if ((cmd[0] & 0xc3) == 0xc2) { | |
326 | switch (cmd[0] & 0x30) { | |
327 | case 0x00 : snprintf(exp, size, "S-block DESELECT"); break; | |
328 | case 0x30 : snprintf(exp, size, "S-block WTX"); break; | |
329 | default : snprintf(exp, size, "S-block (RFU)"); break; | |
330 | } | |
331 | } | |
332 | // R-block (ack) | |
333 | else if ((cmd[0] & 0xe0) == 0xa0) { | |
334 | if ((cmd[0] & 0x10) == 0) | |
335 | snprintf(exp, size, "R-block ACK"); | |
336 | else | |
337 | snprintf(exp, size, "R-block NACK"); | |
338 | } | |
339 | // I-block | |
340 | else { | |
341 | int pos = 1; | |
342 | switch (cmd[0] & 0x0c) { | |
ae60ceca | 343 | case 0x08: // CID following |
53fb848a | 344 | case 0x04: // NAD following |
345 | pos = 2; | |
346 | break; | |
347 | case 0x0c: // CID and NAD following | |
348 | pos = 3; | |
349 | break; | |
350 | default: | |
351 | pos = 1; // no CID, no NAD | |
352 | break; | |
353 | } | |
354 | annotateIso7816(exp, size, &cmd[pos], cmdsize-pos); | |
355 | } | |
356 | } | |
357 | ||
358 | ||
868deeb7 | 359 | /** |
360 | 06 00 = INITIATE | |
361 | 0E xx = SELECT ID (xx = Chip-ID) | |
362 | 0B = Get UID | |
363 | 08 yy = Read Block (yy = block number) | |
364 | 09 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written) | |
365 | 0C = Reset to Inventory | |
366 | 0F = Completion | |
367 | 0A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate) | |
368 | **/ | |
369 | ||
d3bcdbda | 370 | void annotateIso14443b(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { |
868deeb7 | 371 | switch(cmd[0]){ |
372 | case ISO14443B_REQB : snprintf(exp,size,"REQB");break; | |
373 | case ISO14443B_ATTRIB : snprintf(exp,size,"ATTRIB");break; | |
374 | case ISO14443B_HALT : snprintf(exp,size,"HALT");break; | |
375 | case ISO14443B_INITIATE : snprintf(exp,size,"INITIATE");break; | |
376 | case ISO14443B_SELECT : snprintf(exp,size,"SELECT(%d)",cmd[1]);break; | |
377 | case ISO14443B_GET_UID : snprintf(exp,size,"GET UID");break; | |
378 | case ISO14443B_READ_BLK : snprintf(exp,size,"READ_BLK(%d)", cmd[1]);break; | |
379 | case ISO14443B_WRITE_BLK : snprintf(exp,size,"WRITE_BLK(%d)",cmd[1]);break; | |
380 | case ISO14443B_RESET : snprintf(exp,size,"RESET");break; | |
381 | case ISO14443B_COMPLETION : snprintf(exp,size,"COMPLETION");break; | |
382 | case ISO14443B_AUTHENTICATE : snprintf(exp,size,"AUTHENTICATE");break; | |
383 | default : snprintf(exp,size ,"?");break; | |
384 | } | |
385 | ||
386 | } | |
387 | ||
d3bcdbda | 388 | void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { |
6612a5a2 | 389 | switch(cmd[0]) |
390 | { | |
53fb848a | 391 | case ISO14443A_CMD_WUPA: |
392 | snprintf(exp,size,"WUPA"); | |
a31f7f89 | 393 | break; |
6612a5a2 | 394 | case ISO14443A_CMD_ANTICOLL_OR_SELECT:{ |
395 | // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor) | |
396 | // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK) | |
397 | if(cmd[1] == 0x70) | |
398 | { | |
399 | snprintf(exp,size,"SELECT_UID"); break; | |
400 | }else | |
401 | { | |
402 | snprintf(exp,size,"ANTICOLL"); break; | |
403 | } | |
404 | } | |
405 | case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{ | |
406 | //95 20 = Anticollision of cascade level2 | |
407 | //95 70 = Select of cascade level2 | |
408 | if(cmd[2] == 0x70) | |
409 | { | |
410 | snprintf(exp,size,"SELECT_UID-2"); break; | |
411 | }else | |
412 | { | |
413 | snprintf(exp,size,"ANTICOLL-2"); break; | |
414 | } | |
415 | } | |
53fb848a | 416 | case ISO14443A_CMD_REQA: |
417 | snprintf(exp,size,"REQA"); | |
a31f7f89 | 418 | break; |
b8dd1ef6 | 419 | case MIFARE_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break; |
420 | case MIFARE_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break; | |
53fb848a | 421 | case ISO14443A_CMD_HALT: |
422 | snprintf(exp,size,"HALT"); | |
6612a5a2 | 423 | MifareAuthState = masNone; |
424 | break; | |
53fb848a | 425 | case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break; |
426 | case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break; | |
427 | case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break; | |
428 | case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break; | |
429 | case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break; | |
6612a5a2 | 430 | case MIFARE_AUTH_KEYA: |
431 | if ( cmdsize > 3) { | |
53fb848a | 432 | snprintf(exp,size,"AUTH-A(%d)",cmd[1]); |
6612a5a2 | 433 | MifareAuthState = masNt; |
434 | } else { | |
53fb848a | 435 | // case MIFARE_ULEV1_VERSION : both 0x60. |
6612a5a2 | 436 | snprintf(exp,size,"EV1 VERSION"); |
437 | } | |
438 | break; | |
439 | case MIFARE_AUTH_KEYB: | |
440 | MifareAuthState = masNt; | |
53fb848a | 441 | snprintf(exp,size,"AUTH-B(%d)",cmd[1]); |
6612a5a2 | 442 | break; |
53fb848a | 443 | case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break; |
444 | case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break; | |
445 | case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break; | |
446 | case MIFARE_ULC_AUTH_1: snprintf(exp,size,"AUTH "); break; | |
447 | case MIFARE_ULC_AUTH_2: snprintf(exp,size,"AUTH_ANSW"); break; | |
6612a5a2 | 448 | case MIFARE_ULEV1_AUTH: |
449 | if ( cmdsize == 7 ) | |
450 | snprintf(exp,size,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd[1], cmd[2], cmd[3], cmd[4] ); | |
451 | else | |
452 | snprintf(exp,size,"PWD-AUTH"); | |
453 | break; | |
454 | case MIFARE_ULEV1_FASTREAD:{ | |
455 | if ( cmdsize >=3 && cmd[2] <= 0xE6) | |
53fb848a | 456 | snprintf(exp,size,"READ RANGE (%d-%d)",cmd[1],cmd[2]); |
6612a5a2 | 457 | else |
458 | snprintf(exp,size,"?"); | |
459 | break; | |
460 | } | |
461 | case MIFARE_ULC_WRITE:{ | |
462 | if ( cmd[1] < 0x21 ) | |
53fb848a | 463 | snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); |
6612a5a2 | 464 | else |
465 | snprintf(exp,size,"?"); | |
466 | break; | |
467 | } | |
468 | case MIFARE_ULEV1_READ_CNT:{ | |
469 | if ( cmd[1] < 5 ) | |
470 | snprintf(exp,size,"READ CNT(%d)",cmd[1]); | |
471 | else | |
472 | snprintf(exp,size,"?"); | |
473 | break; | |
474 | } | |
475 | case MIFARE_ULEV1_INCR_CNT:{ | |
476 | if ( cmd[1] < 5 ) | |
477 | snprintf(exp,size,"INCR(%d)",cmd[1]); | |
478 | else | |
479 | snprintf(exp,size,"?"); | |
480 | break; | |
481 | } | |
53fb848a | 482 | case MIFARE_ULEV1_READSIG: snprintf(exp,size,"READ_SIG"); break; |
483 | case MIFARE_ULEV1_CHECKTEAR: snprintf(exp,size,"CHK_TEARING(%d)",cmd[1]); break; | |
484 | case MIFARE_ULEV1_VCSL: snprintf(exp,size,"VCSL"); break; | |
485 | default: snprintf(exp,size,"?"); break; | |
6612a5a2 | 486 | } |
487 | return; | |
488 | } | |
489 | ||
b957bcd3 | 490 | void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8_t* parity, uint8_t paritysize, bool isResponse) { |
2d7bdee3 | 491 | if (!isResponse && cmdsize == 1) { |
492 | switch(cmd[0]) { | |
53fb848a | 493 | case ISO14443A_CMD_WUPA: |
494 | case ISO14443A_CMD_REQA: | |
2d7bdee3 | 495 | MifareAuthState = masNone; |
496 | break; | |
497 | default: | |
498 | break; | |
499 | } | |
500 | } | |
53fb848a | 501 | |
aadc6bf1 OM |
502 | // get UID |
503 | if (MifareAuthState == masNone) { | |
7b215d14 | 504 | if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT && cmd[1] == 0x70) { |
aadc6bf1 OM |
505 | ClearAuthData(); |
506 | AuthData.uid = bytes_to_num(&cmd[2], 4); | |
507 | } | |
7b215d14 | 508 | if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2 && cmd[1] == 0x70) { |
aadc6bf1 OM |
509 | ClearAuthData(); |
510 | AuthData.uid = bytes_to_num(&cmd[2], 4); | |
511 | } | |
512 | } | |
53fb848a | 513 | |
6612a5a2 | 514 | switch(MifareAuthState) { |
515 | case masNt: | |
fb30f5a1 | 516 | if (cmdsize == 4 && isResponse) { |
aadc6bf1 | 517 | snprintf(exp,size,"AUTH: nt %s", (AuthData.first_auth) ? "" : "(enc)"); |
6612a5a2 | 518 | MifareAuthState = masNrAr; |
079563a0 | 519 | if (AuthData.first_auth) { |
7b215d14 | 520 | AuthData.nt = bytes_to_num(cmd, 4); |
079563a0 | 521 | } else { |
7b215d14 | 522 | AuthData.nt_enc = bytes_to_num(cmd, 4); |
b957bcd3 | 523 | AuthData.nt_enc_par = parity[0]; |
079563a0 | 524 | } |
6612a5a2 | 525 | return; |
526 | } else { | |
527 | MifareAuthState = masError; | |
6612a5a2 | 528 | } |
529 | break; | |
530 | case masNrAr: | |
fb30f5a1 | 531 | if (cmdsize == 8 && !isResponse) { |
6c30a244 | 532 | snprintf(exp,size,"AUTH: nr ar (enc)"); |
6612a5a2 | 533 | MifareAuthState = masAt; |
7b215d14 OM |
534 | AuthData.nr_enc = bytes_to_num(cmd, 4); |
535 | AuthData.ar_enc = bytes_to_num(&cmd[4], 4); | |
b957bcd3 | 536 | AuthData.ar_enc_par = parity[0] << 4; |
6612a5a2 | 537 | return; |
538 | } else { | |
539 | MifareAuthState = masError; | |
540 | } | |
541 | break; | |
542 | case masAt: | |
fb30f5a1 | 543 | if (cmdsize == 4 && isResponse) { |
6c30a244 | 544 | snprintf(exp,size,"AUTH: at (enc)"); |
747885a6 | 545 | MifareAuthState = masAuthComplete; |
7b215d14 | 546 | AuthData.at_enc = bytes_to_num(cmd, 4); |
b957bcd3 | 547 | AuthData.at_enc_par = parity[0]; |
6612a5a2 | 548 | return; |
549 | } else { | |
550 | MifareAuthState = masError; | |
551 | } | |
552 | break; | |
553 | default: | |
554 | break; | |
555 | } | |
53fb848a | 556 | |
747885a6 | 557 | if (!isResponse && ((MifareAuthState == masNone) || (MifareAuthState == masError))) |
6612a5a2 | 558 | annotateIso14443a(exp, size, cmd, cmdsize); |
53fb848a | 559 | |
6612a5a2 | 560 | } |
7b215d14 | 561 | |
0d2624a0 | 562 | |
563 | static uint64_t GetCrypto1ProbableKey(TAuthData *ad) { | |
564 | struct Crypto1State *revstate = lfsr_recovery64(ad->ks2, ad->ks3); | |
565 | lfsr_rollback_word(revstate, 0, 0); | |
566 | lfsr_rollback_word(revstate, 0, 0); | |
567 | lfsr_rollback_word(revstate, ad->nr_enc, 1); | |
568 | lfsr_rollback_word(revstate, ad->uid ^ ad->nt, 0); | |
569 | ||
570 | uint64_t lfsr = 0; | |
571 | crypto1_get_lfsr(revstate, &lfsr); | |
572 | crypto1_destroy(revstate); | |
53fb848a | 573 | |
0d2624a0 | 574 | return lfsr; |
575 | } | |
576 | ||
577 | ||
578 | static bool NTParityChk(TAuthData *ad, uint32_t ntx) { | |
579 | if ( | |
580 | (oddparity8(ntx >> 8 & 0xff) ^ (ntx & 0x01) ^ ((ad->nt_enc_par >> 5) & 0x01) ^ (ad->nt_enc & 0x01)) || | |
581 | (oddparity8(ntx >> 16 & 0xff) ^ (ntx >> 8 & 0x01) ^ ((ad->nt_enc_par >> 6) & 0x01) ^ (ad->nt_enc >> 8 & 0x01)) || | |
582 | (oddparity8(ntx >> 24 & 0xff) ^ (ntx >> 16 & 0x01) ^ ((ad->nt_enc_par >> 7) & 0x01) ^ (ad->nt_enc >> 16 & 0x01)) | |
583 | ) | |
584 | return false; | |
53fb848a | 585 | |
0d2624a0 | 586 | uint32_t ar = prng_successor(ntx, 64); |
587 | if ( | |
588 | (oddparity8(ar >> 8 & 0xff) ^ (ar & 0x01) ^ ((ad->ar_enc_par >> 5) & 0x01) ^ (ad->ar_enc & 0x01)) || | |
589 | (oddparity8(ar >> 16 & 0xff) ^ (ar >> 8 & 0x01) ^ ((ad->ar_enc_par >> 6) & 0x01) ^ (ad->ar_enc >> 8 & 0x01)) || | |
590 | (oddparity8(ar >> 24 & 0xff) ^ (ar >> 16 & 0x01) ^ ((ad->ar_enc_par >> 7) & 0x01) ^ (ad->ar_enc >> 16 & 0x01)) | |
591 | ) | |
592 | return false; | |
593 | ||
594 | uint32_t at = prng_successor(ntx, 96); | |
595 | if ( | |
596 | (oddparity8(ar & 0xff) ^ (at >> 24 & 0x01) ^ ((ad->ar_enc_par >> 4) & 0x01) ^ (ad->at_enc >> 24 & 0x01)) || | |
597 | (oddparity8(at >> 8 & 0xff) ^ (at & 0x01) ^ ((ad->at_enc_par >> 5) & 0x01) ^ (ad->at_enc & 0x01)) || | |
598 | (oddparity8(at >> 16 & 0xff) ^ (at >> 8 & 0x01) ^ ((ad->at_enc_par >> 6) & 0x01) ^ (ad->at_enc >> 8 & 0x01)) || | |
599 | (oddparity8(at >> 24 & 0xff) ^ (at >> 16 & 0x01) ^ ((ad->at_enc_par >> 7) & 0x01) ^ (ad->at_enc >> 16 & 0x01)) | |
600 | ) | |
601 | return false; | |
53fb848a | 602 | |
0d2624a0 | 603 | return true; |
604 | } | |
605 | ||
606 | ||
607 | static bool CheckCrypto1Parity(uint8_t *cmd_enc, uint8_t cmdsize, uint8_t *cmd, uint8_t *parity_enc) { | |
608 | for (int i = 0; i < cmdsize - 1; i++) { | |
609 | if (oddparity8(cmd[i]) ^ (cmd[i + 1] & 0x01) ^ ((parity_enc[i / 8] >> (7 - i % 8)) & 0x01) ^ (cmd_enc[i + 1] & 0x01)) | |
610 | return false; | |
611 | } | |
53fb848a | 612 | |
0d2624a0 | 613 | return true; |
614 | } | |
615 | ||
616 | ||
617 | static bool NestedCheckKey(uint64_t key, TAuthData *ad, uint8_t *cmd, uint8_t cmdsize, uint8_t *parity) { | |
618 | uint8_t buf[32] = {0}; | |
619 | struct Crypto1State *pcs; | |
53fb848a | 620 | |
0d2624a0 | 621 | AuthData.ks2 = 0; |
622 | AuthData.ks3 = 0; | |
623 | ||
624 | pcs = crypto1_create(key); | |
625 | uint32_t nt1 = crypto1_word(pcs, ad->nt_enc ^ ad->uid, 1) ^ ad->nt_enc; | |
626 | uint32_t ar = prng_successor(nt1, 64); | |
627 | uint32_t at = prng_successor(nt1, 96); | |
628 | ||
629 | crypto1_word(pcs, ad->nr_enc, 1); | |
53fb848a | 630 | // uint32_t nr1 = crypto1_word(pcs, ad->nr_enc, 1) ^ ad->nr_enc; // if needs deciphered nr |
0d2624a0 | 631 | uint32_t ar1 = crypto1_word(pcs, 0, 0) ^ ad->ar_enc; |
632 | uint32_t at1 = crypto1_word(pcs, 0, 0) ^ ad->at_enc; | |
633 | ||
634 | if (!(ar == ar1 && at == at1 && NTParityChk(ad, nt1))) { | |
635 | crypto1_destroy(pcs); | |
636 | return false; | |
637 | } | |
638 | ||
639 | memcpy(buf, cmd, cmdsize); | |
640 | mf_crypto1_decrypt(pcs, buf, cmdsize, 0); | |
53fb848a | 641 | |
0d2624a0 | 642 | crypto1_destroy(pcs); |
53fb848a | 643 | |
0d2624a0 | 644 | if (!CheckCrypto1Parity(cmd, cmdsize, buf, parity)) |
645 | return false; | |
646 | ||
53fb848a | 647 | if(!CheckCrc14443(CRC_14443_A, buf, cmdsize)) |
0d2624a0 | 648 | return false; |
53fb848a | 649 | |
0d2624a0 | 650 | AuthData.nt = nt1; |
651 | AuthData.ks2 = AuthData.ar_enc ^ ar; | |
652 | AuthData.ks3 = AuthData.at_enc ^ at; | |
653 | ||
654 | return true; | |
655 | } | |
656 | ||
657 | ||
658 | static bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, uint8_t *parity, bool isResponse, uint8_t *mfData, size_t *mfDataLen) { | |
53fb848a | 659 | static struct Crypto1State *traceCrypto1; |
dca8220f | 660 | static uint64_t mfLastKey; |
53fb848a | 661 | |
7b215d14 | 662 | *mfDataLen = 0; |
53fb848a | 663 | |
747885a6 OM |
664 | if (MifareAuthState == masAuthComplete) { |
665 | if (traceCrypto1) { | |
666 | crypto1_destroy(traceCrypto1); | |
0113dcf3 | 667 | traceCrypto1 = NULL; |
747885a6 OM |
668 | } |
669 | ||
670 | MifareAuthState = masFirstData; | |
671 | return false; | |
672 | } | |
53fb848a | 673 | |
7b215d14 OM |
674 | if (cmdsize > 32) |
675 | return false; | |
53fb848a | 676 | |
7b215d14 OM |
677 | if (MifareAuthState == masFirstData) { |
678 | if (AuthData.first_auth) { | |
e01bc794 OM |
679 | AuthData.ks2 = AuthData.ar_enc ^ prng_successor(AuthData.nt, 64); |
680 | AuthData.ks3 = AuthData.at_enc ^ prng_successor(AuthData.nt, 96); | |
0113dcf3 | 681 | |
682 | mfLastKey = GetCrypto1ProbableKey(&AuthData); | |
53fb848a | 683 | PrintAndLog(" | * | key | probable key:%012"PRIx64" Prng:%s ks2:%08x ks3:%08x | |", |
0113dcf3 | 684 | mfLastKey, |
28ee794f | 685 | validate_prng_nonce(AuthData.nt) ? "WEAK": "HARD", |
e01bc794 OM |
686 | AuthData.ks2, |
687 | AuthData.ks3); | |
53fb848a | 688 | |
7b215d14 | 689 | AuthData.first_auth = false; |
747885a6 | 690 | |
e01bc794 | 691 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); |
7b215d14 | 692 | } else { |
0113dcf3 | 693 | if (traceCrypto1) { |
694 | crypto1_destroy(traceCrypto1); | |
695 | traceCrypto1 = NULL; | |
696 | } | |
697 | ||
747885a6 | 698 | // check last used key |
45b4ac09 | 699 | if (mfLastKey) { |
2d7bdee3 | 700 | if (NestedCheckKey(mfLastKey, &AuthData, cmd, cmdsize, parity)) { |
53fb848a | 701 | PrintAndLog(" | * | key | last used key:%012"PRIx64" ks2:%08x ks3:%08x | |", |
0113dcf3 | 702 | mfLastKey, |
2d7bdee3 | 703 | AuthData.ks2, |
704 | AuthData.ks3); | |
705 | ||
706 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); | |
c6a886fb | 707 | }; |
747885a6 | 708 | } |
53fb848a | 709 | |
747885a6 | 710 | // check default keys |
45b4ac09 | 711 | if (!traceCrypto1) { |
7bea1581 | 712 | for (int defaultKeyCounter = 0; defaultKeyCounter < MifareDefaultKeysSize; defaultKeyCounter++){ |
2d7bdee3 | 713 | if (NestedCheckKey(MifareDefaultKeys[defaultKeyCounter], &AuthData, cmd, cmdsize, parity)) { |
53fb848a | 714 | PrintAndLog(" | * | key | default key:%012"PRIx64" ks2:%08x ks3:%08x | |", |
0113dcf3 | 715 | MifareDefaultKeys[defaultKeyCounter], |
2d7bdee3 | 716 | AuthData.ks2, |
717 | AuthData.ks3); | |
718 | ||
0113dcf3 | 719 | mfLastKey = MifareDefaultKeys[defaultKeyCounter]; |
856da9a1 | 720 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); |
7bea1581 OM |
721 | break; |
722 | }; | |
723 | } | |
c6a886fb | 724 | } |
53fb848a | 725 | |
747885a6 | 726 | // nested |
7bea1581 | 727 | if (!traceCrypto1 && validate_prng_nonce(AuthData.nt)) { |
53fb848a | 728 | uint32_t ntx = prng_successor(AuthData.nt, 90); |
7bea1581 OM |
729 | for (int i = 0; i < 16383; i++) { |
730 | ntx = prng_successor(ntx, 1); | |
731 | if (NTParityChk(&AuthData, ntx)){ | |
732 | ||
733 | uint32_t ks2 = AuthData.ar_enc ^ prng_successor(ntx, 64); | |
734 | uint32_t ks3 = AuthData.at_enc ^ prng_successor(ntx, 96); | |
735 | struct Crypto1State *pcs = lfsr_recovery64(ks2, ks3); | |
736 | memcpy(mfData, cmd, cmdsize); | |
737 | mf_crypto1_decrypt(pcs, mfData, cmdsize, 0); | |
53fb848a | 738 | |
7bea1581 | 739 | crypto1_destroy(pcs); |
0113dcf3 | 740 | if (CheckCrypto1Parity(cmd, cmdsize, mfData, parity) && CheckCrc14443(CRC_14443_A, mfData, cmdsize)) { |
e01bc794 OM |
741 | AuthData.ks2 = ks2; |
742 | AuthData.ks3 = ks3; | |
0113dcf3 | 743 | |
744 | AuthData.nt = ntx; | |
745 | mfLastKey = GetCrypto1ProbableKey(&AuthData); | |
53fb848a | 746 | PrintAndLog(" | * | key | nested probable key:%012"PRIx64" ks2:%08x ks3:%08x | |", |
0113dcf3 | 747 | mfLastKey, |
748 | AuthData.ks2, | |
749 | AuthData.ks3); | |
750 | ||
e01bc794 | 751 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); |
7bea1581 OM |
752 | break; |
753 | } | |
53fb848a | 754 | } |
7bea1581 | 755 | } |
747885a6 | 756 | } |
53fb848a | 757 | |
747885a6 | 758 | //hardnested |
7bea1581 | 759 | if (!traceCrypto1) { |
2d7bdee3 | 760 | printf("hardnested not implemented. uid:%x nt:%x ar_enc:%x at_enc:%x\n", AuthData.uid, AuthData.nt, AuthData.ar_enc, AuthData.at_enc); |
45b4ac09 | 761 | MifareAuthState = masError; |
762 | ||
763 | /* TOO SLOW( needs to have more strong filter. with this filter - aprox 4 mln tests | |
764 | uint32_t t = msclock(); | |
765 | uint32_t t1 = t; | |
766 | int n = 0; | |
767 | for (uint32_t i = 0; i < 0xFFFFFFFF; i++) { | |
768 | if (NTParityChk(&AuthData, i)){ | |
769 | ||
770 | uint32_t ks2 = AuthData.ar_enc ^ prng_successor(i, 64); | |
771 | uint32_t ks3 = AuthData.at_enc ^ prng_successor(i, 96); | |
772 | struct Crypto1State *pcs = lfsr_recovery64(ks2, ks3); | |
773 | ||
774 | ||
775 | ||
776 | ||
777 | n++; | |
778 | ||
779 | if (!(n % 100000)) { | |
780 | printf("delta=%d n=%d ks2=%x ks3=%x \n", msclock() - t1 , n, ks2, ks3); | |
781 | t1 = msclock(); | |
782 | } | |
783 | ||
784 | } | |
785 | } | |
786 | printf("delta=%d n=%d\n", msclock() - t, n); | |
787 | */ | |
7bea1581 | 788 | } |
7b215d14 | 789 | } |
53fb848a | 790 | |
791 | ||
792 | ||
7b215d14 | 793 | MifareAuthState = masData; |
7b215d14 | 794 | } |
53fb848a | 795 | |
747885a6 OM |
796 | if (MifareAuthState == masData && traceCrypto1) { |
797 | memcpy(mfData, cmd, cmdsize); | |
798 | mf_crypto1_decrypt(traceCrypto1, mfData, cmdsize, 0); | |
799 | *mfDataLen = cmdsize; | |
7b215d14 | 800 | } |
53fb848a | 801 | |
7b215d14 OM |
802 | return *mfDataLen > 0; |
803 | } | |
804 | ||
0d2624a0 | 805 | |
d3bcdbda | 806 | bool is_last_record(uint16_t tracepos, uint8_t *trace, uint16_t traceLen) { |
0d2624a0 | 807 | return(tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen); |
808 | } | |
809 | ||
810 | ||
d3bcdbda | 811 | bool next_record_is_response(uint16_t tracepos, uint8_t *trace) { |
0d2624a0 | 812 | uint16_t next_records_datalen = *((uint16_t *)(trace + tracepos + sizeof(uint32_t) + sizeof(uint16_t))); |
0d2624a0 | 813 | return(next_records_datalen & 0x8000); |
814 | } | |
dca8220f | 815 | |
0d2624a0 | 816 | |
d3bcdbda | 817 | bool merge_topaz_reader_frames(uint32_t timestamp, uint32_t *duration, uint16_t *tracepos, uint16_t traceLen, uint8_t *trace, uint8_t *frame, uint8_t *topaz_reader_command, uint16_t *data_len) { |
0d2624a0 | 818 | |
53fb848a | 819 | #define MAX_TOPAZ_READER_CMD_LEN 16 |
0d2624a0 | 820 | |
821 | uint32_t last_timestamp = timestamp + *duration; | |
822 | ||
823 | if ((*data_len != 1) || (frame[0] == TOPAZ_WUPA) || (frame[0] == TOPAZ_REQA)) return false; | |
824 | ||
825 | memcpy(topaz_reader_command, frame, *data_len); | |
826 | ||
827 | while (!is_last_record(*tracepos, trace, traceLen) && !next_record_is_response(*tracepos, trace)) { | |
828 | uint32_t next_timestamp = *((uint32_t *)(trace + *tracepos)); | |
829 | *tracepos += sizeof(uint32_t); | |
830 | uint16_t next_duration = *((uint16_t *)(trace + *tracepos)); | |
831 | *tracepos += sizeof(uint16_t); | |
832 | uint16_t next_data_len = *((uint16_t *)(trace + *tracepos)) & 0x7FFF; | |
833 | *tracepos += sizeof(uint16_t); | |
834 | uint8_t *next_frame = (trace + *tracepos); | |
835 | *tracepos += next_data_len; | |
836 | if ((next_data_len == 1) && (*data_len + next_data_len <= MAX_TOPAZ_READER_CMD_LEN)) { | |
837 | memcpy(topaz_reader_command + *data_len, next_frame, next_data_len); | |
838 | *data_len += next_data_len; | |
839 | last_timestamp = next_timestamp + next_duration; | |
840 | } else { | |
841 | // rewind and exit | |
842 | *tracepos = *tracepos - next_data_len - sizeof(uint16_t) - sizeof(uint16_t) - sizeof(uint32_t); | |
843 | break; | |
844 | } | |
845 | uint16_t next_parity_len = (next_data_len-1)/8 + 1; | |
846 | *tracepos += next_parity_len; | |
847 | } | |
848 | ||
849 | *duration = last_timestamp - timestamp; | |
53fb848a | 850 | |
dca8220f OM |
851 | return true; |
852 | } | |
853 | ||
0d2624a0 | 854 | |
d3bcdbda | 855 | uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t protocol, bool showWaitCycles, bool markCRCBytes, uint32_t *prev_EOT, bool times_in_us) { |
0d2624a0 | 856 | bool isResponse; |
857 | uint16_t data_len, parity_len; | |
858 | uint32_t duration; | |
859 | uint8_t topaz_reader_command[9]; | |
d3bcdbda | 860 | uint32_t timestamp, first_timestamp; |
861 | uint32_t EndOfTransmissionTimestamp = 0; | |
0d2624a0 | 862 | char explanation[30] = {0}; |
863 | uint8_t mfData[32] = {0}; | |
864 | size_t mfDataLen = 0; | |
865 | ||
866 | if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen; | |
53fb848a | 867 | |
0d2624a0 | 868 | first_timestamp = *((uint32_t *)(trace)); |
869 | timestamp = *((uint32_t *)(trace + tracepos)); | |
2d7bdee3 | 870 | |
0d2624a0 | 871 | tracepos += 4; |
872 | duration = *((uint16_t *)(trace + tracepos)); | |
873 | tracepos += 2; | |
874 | data_len = *((uint16_t *)(trace + tracepos)); | |
875 | tracepos += 2; | |
2d7bdee3 | 876 | |
0d2624a0 | 877 | if (data_len & 0x8000) { |
878 | data_len &= 0x7fff; | |
879 | isResponse = true; | |
880 | } else { | |
881 | isResponse = false; | |
882 | } | |
883 | parity_len = (data_len-1)/8 + 1; | |
dca8220f | 884 | |
0d2624a0 | 885 | if (tracepos + data_len + parity_len > traceLen) { |
886 | return traceLen; | |
44964fd1 | 887 | } |
0d2624a0 | 888 | uint8_t *frame = trace + tracepos; |
889 | tracepos += data_len; | |
890 | uint8_t *parityBytes = trace + tracepos; | |
891 | tracepos += parity_len; | |
dca8220f | 892 | |
0d2624a0 | 893 | if (protocol == TOPAZ && !isResponse) { |
894 | // topaz reader commands come in 1 or 9 separate frames with 7 or 8 Bits each. | |
895 | // merge them: | |
896 | if (merge_topaz_reader_frames(timestamp, &duration, &tracepos, traceLen, trace, frame, topaz_reader_command, &data_len)) { | |
897 | frame = topaz_reader_command; | |
898 | } | |
899 | } | |
53fb848a | 900 | |
3d2c9c9b | 901 | // adjust for different time scales |
902 | if (protocol == ICLASS || protocol == ISO_15693) { | |
3d2c9c9b | 903 | duration *= 32; |
904 | } | |
ae60ceca | 905 | |
0d2624a0 | 906 | //Check the CRC status |
907 | uint8_t crcStatus = 2; | |
908 | ||
909 | if (data_len > 2) { | |
910 | switch (protocol) { | |
911 | case ICLASS: | |
912 | crcStatus = iclass_CRC_check(isResponse, frame, data_len); | |
913 | break; | |
914 | case ISO_14443B: | |
915 | case TOPAZ: | |
53fb848a | 916 | crcStatus = iso14443B_CRC_check(isResponse, frame, data_len); |
0d2624a0 | 917 | break; |
918 | case PROTO_MIFARE: | |
919 | crcStatus = mifare_CRC_check(isResponse, frame, data_len); | |
920 | break; | |
921 | case ISO_14443A: | |
922 | crcStatus = iso14443A_CRC_check(isResponse, frame, data_len); | |
923 | break; | |
53fb848a | 924 | case ISO_14443_4: |
925 | crcStatus = iso14443_4_CRC_check(frame, data_len); | |
926 | break; | |
0d2624a0 | 927 | case ISO_15693: |
928 | crcStatus = iso15693_CRC_check(frame, data_len); | |
929 | break; | |
53fb848a | 930 | default: |
0d2624a0 | 931 | break; |
932 | } | |
933 | } | |
934 | //0 CRC-command, CRC not ok | |
935 | //1 CRC-command, CRC ok | |
936 | //2 Not crc-command | |
937 | ||
938 | //--- Draw the data column | |
0d2624a0 | 939 | char line[16][110]; |
940 | ||
941 | for (int j = 0; j < data_len && j/16 < 16; j++) { | |
0d2624a0 | 942 | uint8_t parityBits = parityBytes[j>>3]; |
53fb848a | 943 | if (protocol != ISO_14443B |
944 | && protocol != ISO_15693 | |
3d2c9c9b | 945 | && protocol != ICLASS |
53fb848a | 946 | && protocol != ISO_7816_4 |
947 | && (isResponse || protocol == ISO_14443A) | |
948 | && (oddparity8(frame[j]) != ((parityBits >> (7-(j&0x0007))) & 0x01))) { | |
0d2624a0 | 949 | snprintf(line[j/16]+(( j % 16) * 4), 110, " %02x!", frame[j]); |
950 | } else { | |
951 | snprintf(line[j/16]+(( j % 16) * 4), 110, " %02x ", frame[j]); | |
952 | } | |
0d2624a0 | 953 | } |
954 | ||
955 | if (markCRCBytes) { | |
3d2c9c9b | 956 | if (crcStatus == 0 || crcStatus == 1) { //CRC-command |
0d2624a0 | 957 | char *pos1 = line[(data_len-2)/16]+(((data_len-2) % 16) * 4); |
958 | (*pos1) = '['; | |
959 | char *pos2 = line[(data_len)/16]+(((data_len) % 16) * 4); | |
960 | sprintf(pos2, "%c", ']'); | |
961 | } | |
962 | } | |
963 | ||
a8561e35 | 964 | // mark short bytes (less than 8 Bit + Parity) |
965 | if (protocol == ISO_14443A || protocol == PROTO_MIFARE) { | |
966 | if (duration < 128 * (9 * data_len)) { | |
967 | line[(data_len-1)/16][((data_len-1)%16) * 4 + 3] = '\''; | |
ae60ceca | 968 | } |
a8561e35 | 969 | } |
ae60ceca | 970 | |
0d2624a0 | 971 | if (data_len == 0) { |
ae60ceca | 972 | if (protocol == ICLASS && duration == 2048) { |
973 | sprintf(line[0], " <SOF>"); | |
1f4789fe | 974 | } else if (protocol == ISO_15693 && duration == 512) { |
975 | sprintf(line[0], " <EOF>"); | |
ae60ceca | 976 | } else { |
977 | sprintf(line[0], " <empty trace - possible error>"); | |
978 | } | |
0d2624a0 | 979 | } |
980 | ||
981 | //--- Draw the CRC column | |
982 | char *crc = (crcStatus == 0 ? "!crc" : (crcStatus == 1 ? " ok " : " ")); | |
983 | ||
0d2624a0 | 984 | if (protocol == PROTO_MIFARE) |
985 | annotateMifare(explanation, sizeof(explanation), frame, data_len, parityBytes, parity_len, isResponse); | |
53fb848a | 986 | |
3d2c9c9b | 987 | if (!isResponse) { |
0d2624a0 | 988 | switch(protocol) { |
53fb848a | 989 | case ICLASS: annotateIclass(explanation,sizeof(explanation),frame,data_len); break; |
990 | case ISO_14443A: annotateIso14443a(explanation,sizeof(explanation),frame,data_len); break; | |
991 | case ISO_14443B: annotateIso14443b(explanation,sizeof(explanation),frame,data_len); break; | |
992 | case TOPAZ: annotateTopaz(explanation,sizeof(explanation),frame,data_len); break; | |
993 | case ISO_15693: annotateIso15693(explanation,sizeof(explanation),frame,data_len); break; | |
994 | case ISO_7816_4: annotateIso7816(explanation, sizeof(explanation), frame, data_len); break; | |
995 | case ISO_14443_4: annotateIso14443_4(explanation, sizeof(explanation), frame, data_len); break; | |
996 | default: break; | |
0d2624a0 | 997 | } |
998 | } | |
2d7bdee3 | 999 | |
d3bcdbda | 1000 | uint32_t previousEndOfTransmissionTimestamp = 0; |
1001 | if (prev_EOT) { | |
1002 | if (*prev_EOT) { | |
1003 | previousEndOfTransmissionTimestamp = *prev_EOT; | |
1004 | } else { | |
1005 | previousEndOfTransmissionTimestamp = timestamp; | |
1006 | } | |
1007 | } | |
1008 | EndOfTransmissionTimestamp = timestamp + duration; | |
1009 | if (prev_EOT) *prev_EOT = EndOfTransmissionTimestamp; | |
1010 | ||
0d2624a0 | 1011 | int num_lines = MIN((data_len - 1)/16 + 1, 16); |
1012 | for (int j = 0; j < num_lines ; j++) { | |
1013 | if (j == 0) { | |
d3bcdbda | 1014 | uint32_t time1 = timestamp - first_timestamp; |
1015 | uint32_t time2 = EndOfTransmissionTimestamp - first_timestamp; | |
1016 | if (prev_EOT) { | |
1017 | time1 = timestamp - previousEndOfTransmissionTimestamp; | |
1018 | time2 = duration; | |
1019 | } | |
1020 | if (times_in_us) { | |
1021 | PrintAndLog(" %10.1f | %10.1f | %s |%-64s | %s| %s", | |
1022 | (float)time1/13.56, | |
1023 | (float)time2/13.56, | |
1024 | isResponse ? "Tag" : "Rdr", | |
1025 | line[j], | |
1026 | (j == num_lines-1) ? crc : " ", | |
1027 | (j == num_lines-1) ? explanation : ""); | |
1028 | } else { | |
1029 | PrintAndLog(" %10" PRIu32 " | %10" PRIu32 " | %s |%-64s | %s| %s", | |
1030 | time1, | |
1031 | time2, | |
1032 | isResponse ? "Tag" : "Rdr", | |
1033 | line[j], | |
1034 | (j == num_lines-1) ? crc : " ", | |
1035 | (j == num_lines-1) ? explanation : ""); | |
1036 | } | |
0d2624a0 | 1037 | } else { |
1038 | PrintAndLog(" | | |%-64s | %s| %s", | |
1039 | line[j], | |
1040 | (j == num_lines-1) ? crc : " ", | |
1041 | (j == num_lines-1) ? explanation : ""); | |
1042 | } | |
1043 | } | |
ae60ceca | 1044 | |
0d2624a0 | 1045 | if (DecodeMifareData(frame, data_len, parityBytes, isResponse, mfData, &mfDataLen)) { |
1046 | memset(explanation, 0x00, sizeof(explanation)); | |
1047 | if (!isResponse) { | |
1048 | explanation[0] = '>'; | |
1049 | annotateIso14443a(&explanation[1], sizeof(explanation) - 1, mfData, mfDataLen); | |
1050 | } | |
1051 | uint8_t crcc = iso14443A_CRC_check(isResponse, mfData, mfDataLen); | |
1052 | PrintAndLog(" | * | dec |%-64s | %-4s| %s", | |
1053 | sprint_hex(mfData, mfDataLen), | |
1054 | (crcc == 0 ? "!crc" : (crcc == 1 ? " ok " : " ")), | |
1055 | (true) ? explanation : ""); | |
1056 | }; | |
2d7bdee3 | 1057 | |
0d2624a0 | 1058 | if (is_last_record(tracepos, trace, traceLen)) return traceLen; |
53fb848a | 1059 | |
0d2624a0 | 1060 | if (showWaitCycles && !isResponse && next_record_is_response(tracepos, trace)) { |
1061 | uint32_t next_timestamp = *((uint32_t *)(trace + tracepos)); | |
3d2c9c9b | 1062 | |
0d2624a0 | 1063 | PrintAndLog(" %10d | %10d | %s | fdt (Frame Delay Time): %d", |
1064 | (EndOfTransmissionTimestamp - first_timestamp), | |
1065 | (next_timestamp - first_timestamp), | |
1066 | " ", | |
1067 | (next_timestamp - EndOfTransmissionTimestamp)); | |
1068 | } | |
1069 | ||
1070 | return tracepos; | |
2d7bdee3 | 1071 | } |
1072 | ||
0d2624a0 | 1073 | |
d3bcdbda | 1074 | int CmdHFList(const char *Cmd) { |
1075 | ||
1076 | CLIParserInit("hf list", "\nList or save protocol data.", | |
1077 | "examples: hf list 14a -f -- interpret as ISO14443A communication and display Frame Delay Times\n"\ | |
1078 | " hf list iclass -- interpret as iClass trace\n"\ | |
1079 | " hf list -s myCardTrace.trc -- save trace for later use\n"\ | |
1080 | " hf list 14a -l myCardTrace.trc -- load trace and interpret as ISO14443A communication\n"); | |
1081 | void* argtable[] = { | |
1082 | arg_param_begin, | |
1083 | arg_lit0("f", "fdt", "display fdt (frame delay times)"), | |
1084 | arg_lit0("r", "relative", "show relative times (gap and duration)"), | |
1085 | arg_lit0("c", "crc" , "mark CRC bytes"), | |
1086 | arg_lit0("p", "pcsc", "show trace buffer from PCSC card reader instead of PM3"), | |
1087 | arg_str0("l", "load", "<filename>", "load trace from file"), | |
1088 | arg_str0("s", "save", "<filename>", "save trace to file"), | |
1089 | arg_lit0("u", "us", "display times in microseconds instead of clock cycles"), | |
1090 | arg_str0(NULL, NULL, "<protocol>", "protocol to interpret. Possible values:\n"\ | |
1091 | "\traw - just show raw data without annotations (default)\n"\ | |
1092 | "\t14a - interpret data as ISO14443A communications\n"\ | |
1093 | "\tmf - interpret data as ISO14443A communications and decrypt Mifare Crypto1 stream\n"\ | |
1094 | "\t14b - interpret data as ISO14443B communications\n"\ | |
1095 | "\t15 - interpret data as ISO15693 communications\n"\ | |
1096 | "\ticlass - interpret data as iClass communications\n"\ | |
1097 | "\ttopaz - interpret data as Topaz communications\n"\ | |
1098 | "\t7816 - interpret data as 7816-4 APDU communications\n"\ | |
1099 | "\t14-4 - interpret data as ISO14443-4 communications"), | |
1100 | arg_param_end | |
1101 | }; | |
0113dcf3 | 1102 | |
d3bcdbda | 1103 | if (CLIParserParseString(Cmd, argtable, arg_getsize(argtable), true)){ |
1104 | CLIParserFree(); | |
1105 | return 0; | |
0d2624a0 | 1106 | } |
0113dcf3 | 1107 | |
d3bcdbda | 1108 | bool showWaitCycles = arg_get_lit(1); |
1109 | bool relative_times = arg_get_lit(2); | |
1110 | bool markCRCBytes = arg_get_lit(3); | |
1111 | bool PCSCtrace = arg_get_lit(4); | |
1112 | bool loadFromFile = arg_get_str_len(5); | |
1113 | bool saveToFile = arg_get_str_len(6); | |
1114 | bool times_in_us = arg_get_lit(7); | |
1115 | ||
1116 | uint32_t previous_EOT = 0; | |
1117 | uint32_t *prev_EOT = NULL; | |
1118 | if (relative_times) { | |
1119 | prev_EOT = &previous_EOT; | |
0d2624a0 | 1120 | } |
d3bcdbda | 1121 | |
fef3084e | 1122 | char load_filename[FILE_PATH_SIZE+1] = {0}; |
d3bcdbda | 1123 | if (loadFromFile) { |
1124 | strncpy(load_filename, arg_get_str(5)->sval[0], FILE_PATH_SIZE); | |
53fb848a | 1125 | } |
fef3084e | 1126 | char save_filename[FILE_PATH_SIZE+1] = {0}; |
d3bcdbda | 1127 | if (saveToFile) { |
1128 | strncpy(save_filename, arg_get_str(6)->sval[0], FILE_PATH_SIZE); | |
0d2624a0 | 1129 | } |
d3bcdbda | 1130 | |
1131 | uint8_t protocol = -1; | |
1132 | if (arg_get_str_len(8)) { | |
1133 | if (strcmp(arg_get_str(8)->sval[0], "iclass") == 0) protocol = ICLASS; | |
1134 | else if(strcmp(arg_get_str(8)->sval[0], "14a") == 0) protocol = ISO_14443A; | |
1135 | else if(strcmp(arg_get_str(8)->sval[0], "mf") == 0) protocol = PROTO_MIFARE; | |
1136 | else if(strcmp(arg_get_str(8)->sval[0], "14b") == 0) protocol = ISO_14443B; | |
1137 | else if(strcmp(arg_get_str(8)->sval[0], "topaz") == 0) protocol = TOPAZ; | |
1138 | else if(strcmp(arg_get_str(8)->sval[0], "7816") == 0) protocol = ISO_7816_4; | |
1139 | else if(strcmp(arg_get_str(8)->sval[0], "14-4") == 0) protocol = ISO_14443_4; | |
1140 | else if(strcmp(arg_get_str(8)->sval[0], "15") == 0) protocol = ISO_15693; | |
1141 | else if(strcmp(arg_get_str(8)->sval[0], "raw") == 0) protocol = -1;//No crc, no annotations | |
1142 | else { | |
1143 | PrintAndLog("hf list: invalid argument \"%s\"\nTry 'hf list --help' for more information.", arg_get_str(8)->sval[0]); | |
1144 | CLIParserFree(); | |
1145 | return 0; | |
1146 | } | |
0d2624a0 | 1147 | } |
53fb848a | 1148 | |
d3bcdbda | 1149 | CLIParserFree(); |
0d2624a0 | 1150 | |
1151 | ||
1152 | uint8_t *trace; | |
1153 | uint32_t tracepos = 0; | |
1154 | uint32_t traceLen = 0; | |
d3bcdbda | 1155 | |
0d2624a0 | 1156 | if (loadFromFile) { |
53fb848a | 1157 | #define TRACE_CHUNK_SIZE (1<<16) // 64K to start with. Will be enough for BigBuf and some room for future extensions |
0d2624a0 | 1158 | FILE *tracefile = NULL; |
1159 | size_t bytes_read; | |
1160 | trace = malloc(TRACE_CHUNK_SIZE); | |
1161 | if (trace == NULL) { | |
1162 | PrintAndLog("Cannot allocate memory for trace"); | |
1163 | return 2; | |
1164 | } | |
d3bcdbda | 1165 | if ((tracefile = fopen(load_filename,"rb")) == NULL) { |
1166 | PrintAndLog("Could not open file %s", load_filename); | |
0d2624a0 | 1167 | free(trace); |
1168 | return 0; | |
1169 | } | |
1170 | while (!feof(tracefile)) { | |
1171 | bytes_read = fread(trace+traceLen, 1, TRACE_CHUNK_SIZE, tracefile); | |
1172 | traceLen += bytes_read; | |
1173 | if (!feof(tracefile)) { | |
1174 | uint8_t *p = realloc(trace, traceLen + TRACE_CHUNK_SIZE); | |
1175 | if (p == NULL) { | |
1176 | PrintAndLog("Cannot allocate memory for trace"); | |
1177 | free(trace); | |
1178 | fclose(tracefile); | |
1179 | return 2; | |
1180 | } | |
1181 | trace = p; | |
1182 | } | |
1183 | } | |
1184 | fclose(tracefile); | |
53fb848a | 1185 | } else if (PCSCtrace) { |
1186 | trace = pcsc_get_trace_addr(); | |
1187 | traceLen = pcsc_get_traceLen(); | |
0d2624a0 | 1188 | } else { |
1189 | trace = malloc(USB_CMD_DATA_SIZE); | |
1190 | // Query for the size of the trace | |
1191 | UsbCommand response; | |
d3bcdbda | 1192 | if (!(GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0, &response, 500, false))) { |
1193 | return 1; | |
1194 | } | |
0d2624a0 | 1195 | traceLen = response.arg[2]; |
1196 | if (traceLen > USB_CMD_DATA_SIZE) { | |
1197 | uint8_t *p = realloc(trace, traceLen); | |
1198 | if (p == NULL) { | |
1199 | PrintAndLog("Cannot allocate memory for trace"); | |
1200 | free(trace); | |
1201 | return 2; | |
1202 | } | |
1203 | trace = p; | |
d3bcdbda | 1204 | if (!(GetFromBigBuf(trace, traceLen, 0, NULL, 500, false))) { |
1205 | return 1; | |
1206 | } | |
0d2624a0 | 1207 | } |
1208 | } | |
1209 | ||
1210 | if (saveToFile) { | |
1211 | FILE *tracefile = NULL; | |
d3bcdbda | 1212 | if ((tracefile = fopen(save_filename,"wb")) == NULL) { |
1213 | PrintAndLog("Could not create file %s", save_filename); | |
0d2624a0 | 1214 | return 1; |
1215 | } | |
1216 | fwrite(trace, 1, traceLen, tracefile); | |
d3bcdbda | 1217 | PrintAndLog("Recorded Activity (TraceLen = %d bytes) written to file %s", traceLen, save_filename); |
0d2624a0 | 1218 | fclose(tracefile); |
1219 | } else { | |
1220 | PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen); | |
1221 | PrintAndLog(""); | |
d3bcdbda | 1222 | if (relative_times) { |
1223 | PrintAndLog("Gap = time between transfers. Duration = duration of data transfer. Src = Source of transfer"); | |
1224 | } else { | |
1225 | PrintAndLog("Start = Start of Frame, End = End of Frame. Src = Source of transfer"); | |
1226 | } | |
1227 | if (times_in_us) { | |
1228 | PrintAndLog("All times are in microseconds"); | |
1229 | } else { | |
1230 | PrintAndLog("All times are in carrier periods (1/13.56Mhz)"); | |
1231 | } | |
0d2624a0 | 1232 | PrintAndLog(""); |
d3bcdbda | 1233 | if (relative_times) { |
1234 | PrintAndLog(" Gap | Duration | Src | Data (! denotes parity error, ' denotes short bytes) | CRC | Annotation |"); | |
1235 | } else { | |
1236 | PrintAndLog(" Start | End | Src | Data (! denotes parity error, ' denotes short bytes) | CRC | Annotation |"); | |
1237 | } | |
0d2624a0 | 1238 | PrintAndLog("------------|------------|-----|-----------------------------------------------------------------|-----|--------------------|"); |
1239 | ||
1240 | ClearAuthData(); | |
d3bcdbda | 1241 | while(tracepos < traceLen) { |
1242 | tracepos = printTraceLine(tracepos, traceLen, trace, protocol, showWaitCycles, markCRCBytes, prev_EOT, times_in_us); | |
0d2624a0 | 1243 | } |
1244 | } | |
1245 | ||
1246 | free(trace); | |
1247 | return 0; | |
0113dcf3 | 1248 | } |
0d2624a0 | 1249 |