]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhflist.c
added destroy
[proxmark3-svn] / client / cmdhflist.c
index 0eaafa68e6e377db5bfa0d1a023cdd6699ace365..b1bd8a7ae9d289871b89d2e8773396c9feee5650 100644 (file)
@@ -23,6 +23,7 @@
 #include "protocols.h"
 #include "crapto1/crapto1.h"
 #include "mifarehost.h"
+#include "mifaredefault.h"
 
 
 enum MifareAuthSeq {
@@ -30,9 +31,9 @@ enum MifareAuthSeq {
        masNt,
        masNrAr,
        masAt,
+       masAuthComplete,
        masFirstData,
        masData,
-       masDataNested,
        masError,
 };
 static enum MifareAuthSeq MifareAuthState;
@@ -73,9 +74,6 @@ uint8_t mifare_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
 {
        switch(MifareAuthState) {
                case masNone:
-               case masFirstData:
-               case masData:
-               case masDataNested:
                case masError:
                        return iso14443A_CRC_check(isResponse, data, len);
                default:
@@ -231,7 +229,7 @@ void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8
                case masAt:
                        if (cmdsize == 4 && isResponse) {
                                snprintf(exp,size,"AUTH: at (enc)");
-                               MifareAuthState = masFirstData;
+                               MifareAuthState = masAuthComplete;
                                AuthData.at_enc = bytes_to_num(cmd, 4);
                                AuthData.at_enc_par = parity[0];
                                return;
@@ -243,14 +241,26 @@ void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8
                        break;
        }
        
-       if (!isResponse)
+       if (!isResponse && ((MifareAuthState == masNone) || (MifareAuthState == masError)))
                annotateIso14443a(exp, size, cmd, cmdsize);
        
 }
 
 bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, bool isResponse, uint8_t *mfData, size_t *mfDataLen) {
+       static struct Crypto1State *traceCrypto1;       
+       static uint64_t mfLastKey;
+       
        *mfDataLen = 0;
        
+       if (MifareAuthState == masAuthComplete) {
+               if (traceCrypto1) {
+                       crypto1_destroy(traceCrypto1);
+               }
+
+               MifareAuthState = masFirstData;
+               return false;
+       }
+       
        if (cmdsize > 32)
                return false;
        
@@ -267,27 +277,107 @@ bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, bool isResponse, uint8_t *m
                        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", 
+                       mfLastKey = lfsr;
+                       PrintAndLog("            |          * | key | probable key:%x%x Prng:%s   ks2:%08x ks3:%08x |     |", 
                                (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF), 
-                               validate_prng_nonce(AuthData.nt) ? "WEAK": "HARDEND",
+                               validate_prng_nonce(AuthData.nt) ? "WEAK": "HARD",
                                ks2,
                                ks3);
                        
                        AuthData.first_auth = false;
+
+                       traceCrypto1 = lfsr_recovery64(ks2, ks3);
                } else {
+                       printf("uid:%x nt:%x ar_enc:%x at_enc:%x\n", AuthData.uid, AuthData.nt, AuthData.ar_enc, AuthData.at_enc);
+                       
+                       // check last used key
+                       if (mfLastKey) {
+                               if (NestedCheckKey(mfLastKey, &AuthData, cmd, cmdsize)) {
+                               };
+                       }
+                       
+                       // check default keys
+                       for (int defaultKeyCounter = 0; defaultKeyCounter < MifareDefaultKeysSize; defaultKeyCounter++){
+                               if (NestedCheckKey(MifareDefaultKeys[defaultKeyCounter], &AuthData, cmd, cmdsize)) {
+                                       
+                                       break;
+                               };
+                       }
+                       
+                       // nested
+                       if (validate_prng_nonce(AuthData.nt)) {
+                       }
+                       
+                       //hardnested
                }
                
                
                
                MifareAuthState = masData;
-               return true;
        }
        
-       if (MifareAuthState == masData) {
+       if (MifareAuthState == masData && traceCrypto1) {
+               memcpy(mfData, cmd, cmdsize);
+               mf_crypto1_decrypt(traceCrypto1, mfData, cmdsize, 0);
+               *mfDataLen = cmdsize;
        }
        
        return *mfDataLen > 0;
 }
 
+bool NTParityChk(TAuthData *ad, uint32_t ntx) {
+       if (
+               (oddparity8(ntx >> 8 & 0xff) ^ (ntx & 0x01) ^ ((ad->nt_enc_par >> 5) & 0x01) ^ (ad->nt_enc & 0x01)) ||
+               (oddparity8(ntx >> 16 & 0xff) ^ (ntx >> 8 & 0x01) ^ ((ad->nt_enc_par >> 6) & 0x01) ^ (ad->nt_enc >> 8 & 0x01)) ||
+               (oddparity8(ntx >> 24 & 0xff) ^ (ntx >> 16 & 0x01) ^ ((ad->nt_enc_par >> 7) & 0x01) ^ (ad->nt_enc >> 16 & 0x01))
+               )
+               return false;
+       
+       uint32_t ar = prng_successor(ntx, 64);
+       if (
+               (oddparity8(ar >> 8 & 0xff) ^ (ar & 0x01) ^ ((ad->ar_enc_par >> 5) & 0x01) ^ (ad->ar_enc & 0x01)) ||
+               (oddparity8(ar >> 16 & 0xff) ^ (ar >> 8 & 0x01) ^ ((ad->ar_enc_par >> 6) & 0x01) ^ (ad->ar_enc >> 8 & 0x01)) ||
+               (oddparity8(ar >> 24 & 0xff) ^ (ar >> 16 & 0x01) ^ ((ad->ar_enc_par >> 7) & 0x01) ^ (ad->ar_enc >> 16 & 0x01))
+               )
+               return false;
+
+       uint32_t at = prng_successor(ntx, 96);
+       if (
+               (oddparity8(ar & 0xff) ^ (at >> 24 & 0x01) ^ ((ad->ar_enc_par >> 4) & 0x01) ^ (ad->at_enc >> 24 & 0x01)) ||
+               (oddparity8(at >> 8 & 0xff) ^ (at & 0x01) ^ ((ad->at_enc_par >> 5) & 0x01) ^ (ad->at_enc & 0x01)) ||
+               (oddparity8(at >> 16 & 0xff) ^ (at >> 8 & 0x01) ^ ((ad->at_enc_par >> 6) & 0x01) ^ (ad->at_enc >> 8 & 0x01)) ||
+               (oddparity8(at >> 24 & 0xff) ^ (at >> 16 & 0x01) ^ ((ad->at_enc_par >> 7) & 0x01) ^ (ad->at_enc >> 16 & 0x01))
+               )
+               return false;
+               
+       return true;
+}
+
+bool NestedCheckKey(uint64_t key, TAuthData *ad, uint8_t *cmd, uint8_t cmdsize) {
+       uint8_t buf[32] = {0};
+       struct Crypto1State *pcs;
+       
+       pcs = crypto1_create(key);
+       uint32_t nt1 = crypto1_word(pcs, ad->nt_enc ^ ad->uid, 1) ^ ad->nt_enc;
+       uint32_t ar = prng_successor(nt1, 64);
+       uint32_t at = prng_successor(nt1, 96);
+       printf("key> nested auth uid: %08x nt: %08x nt_parity: %s ar: %08x at: %08x\n", ad->uid, nt1, printBitsPar(&ad->nt_enc_par, 4), ar, at);
+       uint32_t nr1 = crypto1_word(pcs, ad->nr_enc, 1) ^ ad->nr_enc;
+       uint32_t ar1 = crypto1_word(pcs, 0, 0) ^ ad->ar_enc;
+       uint32_t at1 = crypto1_word(pcs, 0, 0) ^ ad->at_enc;
+       printf("key> the same key test. nr1: %08x ar1: %08x at1: %08x \n", nr1, ar1, at1);
+
+       if (NTParityChk(ad, nt1))
+               printf("key> the same key test OK. key=%x%x\n", (unsigned int)((key & 0xFFFFFFFF00000000) >> 32), (unsigned int)(key & 0xFFFFFFFF));
+       else {
+               printf("key> the same key test. check nt parity error.\n");
+               return false;
+       }
+
+       memcpy(buf, cmd, cmdsize);
+       mf_crypto1_decrypt(pcs, buf, cmdsize, 0);
+       
+       crypto1_destroy(pcs);
+       
+       return CheckCrc14443(CRC_14443_A, buf, cmdsize);
+}
\ No newline at end of file
Impressum, Datenschutz