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