1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 // Copyright (C) Merlok - 2017
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
8 //-----------------------------------------------------------------------------
9 // Command: hf list. It shows data from arm buffer.
10 //-----------------------------------------------------------------------------
12 #include "cmdhflist.h"
22 #include "iso14443crc.h"
23 #include "iso15693tools.h"
25 #include "protocols.h"
26 #include "crapto1/crapto1.h"
27 #include "mifarehost.h"
28 #include "mifaredefault.h"
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
57 static enum MifareAuthSeq MifareAuthState
;
58 static TAuthData AuthData
;
60 static void ClearAuthData() {
63 AuthData
.first_auth
= true;
69 * @brief iso14443A_CRC_check Checks CRC in command or response
73 * @return 0 : CRC-command, CRC not ok
74 * 1 : CRC-command, CRC ok
77 static uint8_t iso14443A_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
81 if(len
<= 2) return 2;
83 if(isResponse
& (len
< 6)) return 2;
85 ComputeCrc14443(CRC_14443_A
, data
, len
-2, &b1
, &b2
);
86 if (b1
!= data
[len
-2] || b2
!= data
[len
-1]) {
94 static uint8_t mifare_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
96 switch(MifareAuthState
) {
99 return iso14443A_CRC_check(isResponse
, data
, len
);
107 * @brief iso14443B_CRC_check Checks CRC in command or response
111 * @return 0 : CRC-command, CRC not ok
112 * 1 : CRC-command, CRC ok
113 * 2 : Not crc-command
115 static uint8_t iso14443B_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
119 if(len
<= 2) return 2;
121 ComputeCrc14443(CRC_14443_B
, data
, len
-2, &b1
, &b2
);
122 if(b1
!= data
[len
-2] || b2
!= data
[len
-1]) {
130 static uint8_t iso15693_CRC_check(uint8_t* d
, uint16_t n
)
132 if (n
<= 2) return 2;
134 return (Iso15693Crc(d
, n
) == ISO15693_CRC_CHECK
? 1 : 0);
139 * @brief iclass_CRC_Ok Checks CRC in command or response
143 * @return 0 : CRC-command, CRC not ok
144 * 1 : CRC-command, CRC ok
145 * 2 : Not crc-command
147 uint8_t iclass_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
149 if(len
< 4) return 2;//CRC commands (and responses) are all at least 4 bytes
153 if(!isResponse
)//Commands to tag
156 These commands should have CRC. Total length leftmost
159 12 UPDATE - unsecured, ends with CRC16
160 14 UPDATE - secured, ends with signature instead
163 if(len
== 4 || len
== 12)//Covers three of them
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];
172 These tag responses should have CRC. Total length leftmost
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]
184 4 CHECK chip_response[4]
189 In conclusion, without looking at the command; any response
190 of length 10 or 34 should have CRC
192 if(len
!= 10 && len
!= 34) return true;
194 ComputeCrc14443(CRC_ICLASS
, data
, len
-2, &b1
, &b2
);
195 return b1
== data
[len
-2] && b2
== data
[len
-1];
200 void annotateIclass(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
204 case ICLASS_CMD_ACTALL
: snprintf(exp
,size
,"ACTALL"); break;
205 case ICLASS_CMD_READ_OR_IDENTIFY
:{
207 snprintf(exp
,size
,"READ(%d)",cmd
[1]);
209 snprintf(exp
,size
,"IDENTIFY");
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;
229 void annotateIso15693(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
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;
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");
258 void annotateTopaz(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
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;
279 0E xx = SELECT ID (xx = Chip-ID)
281 08 yy = Read Block (yy = block number)
282 09 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written)
283 0C = Reset to Inventory
285 0A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate)
288 void annotateIso14443b(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
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;
307 void annotateIso14443a(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
311 case ISO14443A_CMD_WUPA
:
312 snprintf(exp
,size
,"WUPA");
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)
319 snprintf(exp
,size
,"SELECT_UID"); break;
322 snprintf(exp
,size
,"ANTICOLL"); break;
325 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2
:{
326 //95 20 = Anticollision of cascade level2
327 //95 70 = Select of cascade level2
330 snprintf(exp
,size
,"SELECT_UID-2"); break;
333 snprintf(exp
,size
,"ANTICOLL-2"); break;
336 case ISO14443A_CMD_REQA
:
337 snprintf(exp
,size
,"REQA");
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
;
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
:
352 snprintf(exp
,size
,"AUTH-A(%d)",cmd
[1]);
353 MifareAuthState
= masNt
;
355 // case MIFARE_ULEV1_VERSION : both 0x60.
356 snprintf(exp
,size
,"EV1 VERSION");
359 case MIFARE_AUTH_KEYB
:
360 MifareAuthState
= masNt
;
361 snprintf(exp
,size
,"AUTH-B(%d)",cmd
[1]);
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
:
370 snprintf(exp
,size
,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd
[1], cmd
[2], cmd
[3], cmd
[4] );
372 snprintf(exp
,size
,"PWD-AUTH");
374 case MIFARE_ULEV1_FASTREAD
:{
375 if ( cmdsize
>=3 && cmd
[2] <= 0xE6)
376 snprintf(exp
,size
,"READ RANGE (%d-%d)",cmd
[1],cmd
[2]);
378 snprintf(exp
,size
,"?");
381 case MIFARE_ULC_WRITE
:{
383 snprintf(exp
,size
,"WRITEBLOCK(%d)",cmd
[1]);
385 snprintf(exp
,size
,"?");
388 case MIFARE_ULEV1_READ_CNT
:{
390 snprintf(exp
,size
,"READ CNT(%d)",cmd
[1]);
392 snprintf(exp
,size
,"?");
395 case MIFARE_ULEV1_INCR_CNT
:{
397 snprintf(exp
,size
,"INCR(%d)",cmd
[1]);
399 snprintf(exp
,size
,"?");
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;
410 void annotateMifare(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
, uint8_t* parity
, uint8_t paritysize
, bool isResponse
) {
411 if (!isResponse
&& cmdsize
== 1) {
413 case ISO14443A_CMD_WUPA
:
414 case ISO14443A_CMD_REQA
:
415 MifareAuthState
= masNone
;
423 if (MifareAuthState
== masNone
) {
424 if (cmdsize
== 9 && cmd
[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT
&& cmd
[1] == 0x70) {
426 AuthData
.uid
= bytes_to_num(&cmd
[2], 4);
428 if (cmdsize
== 9 && cmd
[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2
&& cmd
[1] == 0x70) {
430 AuthData
.uid
= bytes_to_num(&cmd
[2], 4);
434 switch(MifareAuthState
) {
436 if (cmdsize
== 4 && isResponse
) {
437 snprintf(exp
,size
,"AUTH: nt %s", (AuthData
.first_auth
) ? "" : "(enc)");
438 MifareAuthState
= masNrAr
;
439 if (AuthData
.first_auth
) {
440 AuthData
.nt
= bytes_to_num(cmd
, 4);
442 AuthData
.nt_enc
= bytes_to_num(cmd
, 4);
443 AuthData
.nt_enc_par
= parity
[0];
447 MifareAuthState
= masError
;
451 if (cmdsize
== 8 && !isResponse
) {
452 snprintf(exp
,size
,"AUTH: nr ar (enc)");
453 MifareAuthState
= masAt
;
454 AuthData
.nr_enc
= bytes_to_num(cmd
, 4);
455 AuthData
.ar_enc
= bytes_to_num(&cmd
[4], 4);
456 AuthData
.ar_enc_par
= parity
[0] << 4;
459 MifareAuthState
= masError
;
463 if (cmdsize
== 4 && isResponse
) {
464 snprintf(exp
,size
,"AUTH: at (enc)");
465 MifareAuthState
= masAuthComplete
;
466 AuthData
.at_enc
= bytes_to_num(cmd
, 4);
467 AuthData
.at_enc_par
= parity
[0];
470 MifareAuthState
= masError
;
477 if (!isResponse
&& ((MifareAuthState
== masNone
) || (MifareAuthState
== masError
)))
478 annotateIso14443a(exp
, size
, cmd
, cmdsize
);
483 static 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);
491 crypto1_get_lfsr(revstate
, &lfsr
);
492 crypto1_destroy(revstate
);
498 static bool NTParityChk(TAuthData
*ad
, uint32_t ntx
) {
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))
506 uint32_t ar
= prng_successor(ntx
, 64);
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))
514 uint32_t at
= prng_successor(ntx
, 96);
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))
527 static 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))
537 static 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
;
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);
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
;
554 if (!(ar
== ar1
&& at
== at1
&& NTParityChk(ad
, nt1
))) {
555 crypto1_destroy(pcs
);
559 memcpy(buf
, cmd
, cmdsize
);
560 mf_crypto1_decrypt(pcs
, buf
, cmdsize
, 0);
562 crypto1_destroy(pcs
);
564 if (!CheckCrypto1Parity(cmd
, cmdsize
, buf
, parity
))
567 if(!CheckCrc14443(CRC_14443_A
, buf
, cmdsize
))
571 AuthData
.ks2
= AuthData
.ar_enc
^ ar
;
572 AuthData
.ks3
= AuthData
.at_enc
^ at
;
578 static bool DecodeMifareData(uint8_t *cmd
, uint8_t cmdsize
, uint8_t *parity
, bool isResponse
, uint8_t *mfData
, size_t *mfDataLen
) {
579 static struct Crypto1State
*traceCrypto1
;
580 static uint64_t mfLastKey
;
584 if (MifareAuthState
== masAuthComplete
) {
586 crypto1_destroy(traceCrypto1
);
590 MifareAuthState
= masFirstData
;
597 if (MifareAuthState
== masFirstData
) {
598 if (AuthData
.first_auth
) {
599 AuthData
.ks2
= AuthData
.ar_enc
^ prng_successor(AuthData
.nt
, 64);
600 AuthData
.ks3
= AuthData
.at_enc
^ prng_successor(AuthData
.nt
, 96);
602 mfLastKey
= GetCrypto1ProbableKey(&AuthData
);
603 PrintAndLog(" | * | key | probable key:%012"PRIx64
" Prng:%s ks2:%08x ks3:%08x | |",
605 validate_prng_nonce(AuthData
.nt
) ? "WEAK": "HARD",
609 AuthData
.first_auth
= false;
611 traceCrypto1
= lfsr_recovery64(AuthData
.ks2
, AuthData
.ks3
);
614 crypto1_destroy(traceCrypto1
);
618 // check last used key
620 if (NestedCheckKey(mfLastKey
, &AuthData
, cmd
, cmdsize
, parity
)) {
621 PrintAndLog(" | * | key | last used key:%012"PRIx64
" ks2:%08x ks3:%08x | |",
626 traceCrypto1
= lfsr_recovery64(AuthData
.ks2
, AuthData
.ks3
);
630 // check default keys
632 for (int defaultKeyCounter
= 0; defaultKeyCounter
< MifareDefaultKeysSize
; defaultKeyCounter
++){
633 if (NestedCheckKey(MifareDefaultKeys
[defaultKeyCounter
], &AuthData
, cmd
, cmdsize
, parity
)) {
634 PrintAndLog(" | * | key | default key:%012"PRIx64
" ks2:%08x ks3:%08x | |",
635 MifareDefaultKeys
[defaultKeyCounter
],
639 mfLastKey
= MifareDefaultKeys
[defaultKeyCounter
];
640 traceCrypto1
= lfsr_recovery64(AuthData
.ks2
, AuthData
.ks3
);
647 if (!traceCrypto1
&& validate_prng_nonce(AuthData
.nt
)) {
648 uint32_t ntx
= prng_successor(AuthData
.nt
, 90);
649 for (int i
= 0; i
< 16383; i
++) {
650 ntx
= prng_successor(ntx
, 1);
651 if (NTParityChk(&AuthData
, ntx
)){
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);
659 crypto1_destroy(pcs
);
660 if (CheckCrypto1Parity(cmd
, cmdsize
, mfData
, parity
) && CheckCrc14443(CRC_14443_A
, mfData
, cmdsize
)) {
665 mfLastKey
= GetCrypto1ProbableKey(&AuthData
);
666 PrintAndLog(" | * | key | nested probable key:%012"PRIx64
" ks2:%08x ks3:%08x | |",
671 traceCrypto1
= lfsr_recovery64(AuthData
.ks2
, AuthData
.ks3
);
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
);
681 MifareAuthState
= masError
;
683 /* TOO SLOW( needs to have more strong filter. with this filter - aprox 4 mln tests
684 uint32_t t = msclock();
687 for (uint32_t i = 0; i < 0xFFFFFFFF; i++) {
688 if (NTParityChk(&AuthData, i)){
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);
700 printf("delta=%d n=%d ks2=%x ks3=%x \n", msclock() - t1 , n, ks2, ks3);
706 printf("delta=%d n=%d\n", msclock() - t, n);
713 MifareAuthState
= masData
;
716 if (MifareAuthState
== masData
&& traceCrypto1
) {
717 memcpy(mfData
, cmd
, cmdsize
);
718 mf_crypto1_decrypt(traceCrypto1
, mfData
, cmdsize
, 0);
719 *mfDataLen
= cmdsize
;
722 return *mfDataLen
> 0;
726 bool is_last_record(uint16_t tracepos
, uint8_t *trace
, uint16_t traceLen
)
728 return(tracepos
+ sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen
);
732 bool next_record_is_response(uint16_t tracepos
, uint8_t *trace
)
734 uint16_t next_records_datalen
= *((uint16_t *)(trace
+ tracepos
+ sizeof(uint32_t) + sizeof(uint16_t)));
736 return(next_records_datalen
& 0x8000);
740 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
)
743 #define MAX_TOPAZ_READER_CMD_LEN 16
745 uint32_t last_timestamp
= timestamp
+ *duration
;
747 if ((*data_len
!= 1) || (frame
[0] == TOPAZ_WUPA
) || (frame
[0] == TOPAZ_REQA
)) return false;
749 memcpy(topaz_reader_command
, frame
, *data_len
);
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
;
766 *tracepos
= *tracepos
- next_data_len
- sizeof(uint16_t) - sizeof(uint16_t) - sizeof(uint32_t);
769 uint16_t next_parity_len
= (next_data_len
-1)/8 + 1;
770 *tracepos
+= next_parity_len
;
773 *duration
= last_timestamp
- timestamp
;
779 uint16_t printTraceLine(uint16_t tracepos
, uint16_t traceLen
, uint8_t *trace
, uint8_t protocol
, bool showWaitCycles
, bool markCRCBytes
)
782 uint16_t data_len
, parity_len
;
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;
790 if (tracepos
+ sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen
) return traceLen
;
792 first_timestamp
= *((uint32_t *)(trace
));
793 timestamp
= *((uint32_t *)(trace
+ tracepos
));
796 duration
= *((uint16_t *)(trace
+ tracepos
));
798 data_len
= *((uint16_t *)(trace
+ tracepos
));
801 if (data_len
& 0x8000) {
807 parity_len
= (data_len
-1)/8 + 1;
809 if (tracepos
+ data_len
+ parity_len
> traceLen
) {
812 uint8_t *frame
= trace
+ tracepos
;
813 tracepos
+= data_len
;
814 uint8_t *parityBytes
= trace
+ tracepos
;
815 tracepos
+= parity_len
;
817 if (protocol
== TOPAZ
&& !isResponse
) {
818 // topaz reader commands come in 1 or 9 separate frames with 7 or 8 Bits each.
820 if (merge_topaz_reader_frames(timestamp
, &duration
, &tracepos
, traceLen
, trace
, frame
, topaz_reader_command
, &data_len
)) {
821 frame
= topaz_reader_command
;
825 //Check the CRC status
826 uint8_t crcStatus
= 2;
831 crcStatus
= iclass_CRC_check(isResponse
, frame
, data_len
);
835 crcStatus
= iso14443B_CRC_check(isResponse
, frame
, data_len
);
838 crcStatus
= mifare_CRC_check(isResponse
, frame
, data_len
);
841 crcStatus
= iso14443A_CRC_check(isResponse
, frame
, data_len
);
844 crcStatus
= iso15693_CRC_check(frame
, data_len
);
850 //0 CRC-command, CRC not ok
851 //1 CRC-command, CRC ok
854 //--- Draw the data column
855 //char line[16][110];
858 for (int j
= 0; j
< data_len
&& j
/16 < 16; j
++) {
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
]);
868 snprintf(line
[j
/16]+(( j
% 16) * 4), 110, " %02x ", frame
[j
]);
874 if(crcStatus
== 0 || crcStatus
== 1)
876 char *pos1
= line
[(data_len
-2)/16]+(((data_len
-2) % 16) * 4);
878 char *pos2
= line
[(data_len
)/16]+(((data_len
) % 16) * 4);
879 sprintf(pos2
, "%c", ']');
884 sprintf(line
[0]," <empty trace - possible error>");
887 //--- Draw the CRC column
888 char *crc
= (crcStatus
== 0 ? "!crc" : (crcStatus
== 1 ? " ok " : " "));
890 EndOfTransmissionTimestamp
= timestamp
+ duration
;
892 if (protocol
== PROTO_MIFARE
)
893 annotateMifare(explanation
, sizeof(explanation
), frame
, data_len
, parityBytes
, parity_len
, isResponse
);
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;
907 int num_lines
= MIN((data_len
- 1)/16 + 1, 16);
908 for (int j
= 0; j
< num_lines
; j
++) {
910 PrintAndLog(" %10d | %10d | %s |%-64s | %s| %s",
911 (timestamp
- first_timestamp
),
912 (EndOfTransmissionTimestamp
- first_timestamp
),
913 (isResponse
? "Tag" : "Rdr"),
915 (j
== num_lines
-1) ? crc
: " ",
916 (j
== num_lines
-1) ? explanation
: "");
918 PrintAndLog(" | | |%-64s | %s| %s",
920 (j
== num_lines
-1) ? crc
: " ",
921 (j
== num_lines
-1) ? explanation
: "");
925 if (DecodeMifareData(frame
, data_len
, parityBytes
, isResponse
, mfData
, &mfDataLen
)) {
926 memset(explanation
, 0x00, sizeof(explanation
));
928 explanation
[0] = '>';
929 annotateIso14443a(&explanation
[1], sizeof(explanation
) - 1, mfData
, mfDataLen
);
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
: "");
938 if (is_last_record(tracepos
, trace
, traceLen
)) return traceLen
;
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
),
946 (next_timestamp
- EndOfTransmissionTimestamp
));
953 int CmdHFList(const char *Cmd
)
955 bool showWaitCycles
= false;
956 bool markCRCBytes
= false;
957 bool loadFromFile
= false;
958 bool saveToFile
= false;
963 char filename
[FILE_PATH_SIZE
] = {0};
964 uint8_t protocol
= 0;
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);
971 param_getstr(Cmd
, 1, filename
, sizeof(filename
));
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
));
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
));
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')) {
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;
1011 if (param1
== 'f' || param2
== 'f' || param3
== 'f') {
1012 showWaitCycles
= true;
1015 if (param1
== 'c' || param2
== 'c' || param3
== 'c') {
1016 markCRCBytes
= true;
1019 if (param1
== 'l' || param2
== 'l' || param3
== 'l') {
1020 loadFromFile
= true;
1023 if ((loadFromFile
|| saveToFile
) && strlen(filename
) == 0) {
1027 if (loadFromFile
&& saveToFile
) {
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");
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");
1057 uint32_t tracepos
= 0;
1058 uint32_t traceLen
= 0;
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
;
1064 trace
= malloc(TRACE_CHUNK_SIZE
);
1065 if (trace
== NULL
) {
1066 PrintAndLog("Cannot allocate memory for trace");
1069 if ((tracefile
= fopen(filename
,"rb")) == NULL
) {
1070 PrintAndLog("Could not open file %s", filename
);
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
);
1080 PrintAndLog("Cannot allocate memory for trace");
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
);
1098 PrintAndLog("Cannot allocate memory for trace");
1103 GetFromBigBuf(trace
, traceLen
, 0, NULL
, -1, false);
1108 FILE *tracefile
= NULL
;
1109 if ((tracefile
= fopen(filename
,"wb")) == NULL
) {
1110 PrintAndLog("Could not create file %s", filename
);
1113 fwrite(trace
, 1, traceLen
, tracefile
);
1114 PrintAndLog("Recorded Activity (TraceLen = %d bytes) written to file %s", traceLen
, filename
);
1117 PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen
);
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");
1123 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
1124 PrintAndLog("------------|------------|-----|-----------------------------------------------------------------|-----|--------------------|");
1127 while(tracepos
< traceLen
)
1129 tracepos
= printTraceLine(tracepos
, traceLen
, trace
, protocol
, showWaitCycles
, markCRCBytes
);