]> cvs.zerfleddert.de Git - proxmark3-svn/commitdiff
correct 1st key calculation
authorOleg Moiseenko <olegmsn@gmail.com>
Sat, 10 Feb 2018 18:28:30 +0000 (20:28 +0200)
committerOleg Moiseenko <olegmsn@gmail.com>
Sat, 10 Feb 2018 18:28:30 +0000 (20:28 +0200)
client/cmdhf.c
client/cmdhflist.c
client/cmdhflist.h

index 3256b69a9e77b24eac2bb216f1d30a8b714fc1c7..9f3ff4e4c44f3d937943aed355051b23f4da4aa4 100644 (file)
@@ -300,6 +300,8 @@ uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, ui
        uint8_t topaz_reader_command[9];
        uint32_t timestamp, first_timestamp, EndOfTransmissionTimestamp;
        char explanation[30] = {0};
+       uint8_t mfData[32] = {0};
+       size_t mfDataLen = 0;
 
        if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen;
        
@@ -431,8 +433,12 @@ uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, ui
                }
        }
        
-//     if (DecodeMifareData(frame, data_len, isResponse)) {
-//     };
+       if (DecodeMifareData(frame, data_len, isResponse, mfData, &mfDataLen)) {
+               PrintAndLog("            |            |     |%-64s | %s| %s",
+                       sprint_hex(mfData, mfDataLen),
+                       "",
+                       (false) ? explanation : "");
+       };
 
        if (is_last_record(tracepos, trace, traceLen)) return traceLen;
        
index 5ff3192b0caec2840f749d96e876f957b8ed4a4c..0eaafa68e6e377db5bfa0d1a023cdd6699ace365 100644 (file)
@@ -21,6 +21,8 @@
 #include "iso14443crc.h"
 #include "parity.h"
 #include "protocols.h"
+#include "crapto1/crapto1.h"
+#include "mifarehost.h"
 
 
 enum MifareAuthSeq {
@@ -39,7 +41,7 @@ static TAuthData AuthData;
 void ClearAuthData() {
        AuthData.uid = 0;
        AuthData.nt = 0;
-       AuthData.first_auth = false;
+       AuthData.first_auth = true;
 }
 
 /**
@@ -189,11 +191,11 @@ void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
 void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8_t* parity, uint8_t paritysize, bool isResponse) {
        // get UID
        if (MifareAuthState == masNone) {
-               if (cmdsize == 7 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT && cmd[1] == 0x70) {
+               if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT && cmd[1] == 0x70) {
                        ClearAuthData();
                        AuthData.uid = bytes_to_num(&cmd[2], 4);
                }
-               if (cmdsize == 7 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2 && cmd[1] == 0x70) {
+               if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2 && cmd[1] == 0x70) {
                        ClearAuthData();
                        AuthData.uid = bytes_to_num(&cmd[2], 4);
                }
@@ -205,9 +207,9 @@ void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8
                                snprintf(exp,size,"AUTH: nt %s", (AuthData.first_auth) ? "" : "(enc)");
                                MifareAuthState = masNrAr;
                                if (AuthData.first_auth)
-                                       AuthData.nt = bytes_to_num(cmd, cmdsize);
+                                       AuthData.nt = bytes_to_num(cmd, 4);
                                else
-                                       AuthData.nt_enc = bytes_to_num(cmd, cmdsize);
+                                       AuthData.nt_enc = bytes_to_num(cmd, 4);
                                        AuthData.nt_enc_par = parity[0];
                                return;
                        } else {
@@ -218,8 +220,8 @@ void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8
                        if (cmdsize == 8 && !isResponse) {
                                snprintf(exp,size,"AUTH: nr ar (enc)");
                                MifareAuthState = masAt;
-                               AuthData.nr_enc = bytes_to_num(cmd, cmdsize);
-                               AuthData.ar_enc = bytes_to_num(&cmd[3], cmdsize);
+                               AuthData.nr_enc = bytes_to_num(cmd, 4);
+                               AuthData.ar_enc = bytes_to_num(&cmd[4], 4);
                                AuthData.ar_enc_par = parity[0] << 4;
                                return;
                        } else {
@@ -230,7 +232,7 @@ void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8
                        if (cmdsize == 4 && isResponse) {
                                snprintf(exp,size,"AUTH: at (enc)");
                                MifareAuthState = masFirstData;
-                               AuthData.at_enc = bytes_to_num(cmd, cmdsize);
+                               AuthData.at_enc = bytes_to_num(cmd, 4);
                                AuthData.at_enc_par = parity[0];
                                return;
                        } else {
@@ -245,3 +247,47 @@ void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8
                annotateIso14443a(exp, size, cmd, cmdsize);
        
 }
+
+bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, bool isResponse, uint8_t *mfData, size_t *mfDataLen) {
+       *mfDataLen = 0;
+       
+       if (cmdsize > 32)
+               return false;
+       
+       if (MifareAuthState == masFirstData) {
+               if (AuthData.first_auth) {
+                       uint32_t ks2 = AuthData.ar_enc ^ prng_successor(AuthData.nt, 64);
+                       uint32_t ks3 = AuthData.at_enc ^ prng_successor(AuthData.nt, 96);
+                       struct Crypto1State *revstate = lfsr_recovery64(ks2, ks3);
+                       lfsr_rollback_word(revstate, 0, 0);
+                       lfsr_rollback_word(revstate, 0, 0);
+                       lfsr_rollback_word(revstate, AuthData.nr_enc, 1);
+                       lfsr_rollback_word(revstate, AuthData.uid ^ AuthData.nt, 0);
+
+                       uint64_t lfsr = 0;
+                       crypto1_get_lfsr(revstate, &lfsr);
+                       crypto1_destroy(revstate);
+//                     LastKey = lfsr;
+                       printf("uid:%x nt:%x ar_enc:%x at_enc:%x\n", AuthData.uid, AuthData.nt, AuthData.ar_enc, AuthData.at_enc);
+                       printf("AUTH: probable key:%x%x Prng:%s ks2:%08x ks3:%08x\n", 
+                               (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF), 
+                               validate_prng_nonce(AuthData.nt) ? "WEAK": "HARDEND",
+                               ks2,
+                               ks3);
+                       
+                       AuthData.first_auth = false;
+               } else {
+               }
+               
+               
+               
+               MifareAuthState = masData;
+               return true;
+       }
+       
+       if (MifareAuthState == masData) {
+       }
+       
+       return *mfDataLen > 0;
+}
+
index 329df7dbb91e7705acab218525745d1184535106..7d95cc1c2b888f73c7cddbb8380a5a569fda9c81 100644 (file)
@@ -32,6 +32,6 @@ extern uint8_t iso14443A_CRC_check(bool isResponse, uint8_t* data, uint8_t len);
 extern uint8_t mifare_CRC_check(bool isResponse, uint8_t* data, uint8_t len);
 extern void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize);
 extern void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8_t* parity, uint8_t paritysize, bool isResponse);
-
+extern bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, bool isResponse, uint8_t *mfData, size_t *mfDataLen);
 
 #endif // CMDHFLIST
Impressum, Datenschutz