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