- size = removeParity(BitStream, idx+8, 4, 1, 88);
- // if (size != 66){
- // if (g_debugMode==1) PrintAndLog("DEBUG: Error - at parity check-tag size does not match AWID format");
- // return 0;
- // }
+ int ans = ISO11784demodBI(DemodBuffer, &size);
+ if (ans < 0){
+ if (g_debugMode) PrintAndLog("Error ISO11784Demod");
+ return 0;
+ }
+
+ //size = removeParity(BitStream, idx+11, 9, 1, 104);
+
+ //got a good demod
+ uint32_t ByteStream[13] = {0x00};
+ uint8_t xorKey=0;
+ uint8_t keyCnt=0;
+ uint8_t bitCnt=0;
+ uint8_t ByteCnt=0;
+ size_t startIdx = ans + 11; //start after preamble
+ for (size_t idx = 0; idx<size-11; idx++){
+ if ((idx+1) % 5 == 0){
+ //spacer bit - should be 0
+ if (DemodBuffer[startIdx+idx] != 0) {
+ if (g_debugMode) PrintAndLog("Error spacer not 0: %d, pos: %d",DemodBuffer[startIdx+idx],startIdx+idx);
+ return 0;
+ }
+ continue;
+ }
+ if (keyCnt<8){ //lsb first
+ xorKey = xorKey | (DemodBuffer[startIdx+idx]<<keyCnt);
+ keyCnt++;
+ if (keyCnt==8 && g_debugMode) PrintAndLog("xorKey Found: %02x", xorKey);
+ continue;
+ }
+ //lsb first
+ ByteStream[ByteCnt] = ByteStream[ByteCnt] | (DemodBuffer[startIdx+idx]<<bitCnt);
+ bitCnt++;
+ if (bitCnt % 8 == 0){
+ if (g_debugMode) PrintAndLog("byte %d: %02x",ByteCnt,ByteStream[ByteCnt]);
+ bitCnt=0;
+ ByteCnt++;
+ }
+ }
+ for (uint8_t i = 0; i < ByteCnt; i++){
+ ByteStream[i] ^= xorKey; //xor
+ if (g_debugMode) PrintAndLog("byte %d after xor: %02x", i, ByteStream[i]);
+ }
+ //now ByteStream contains 13 bytes of decrypted raw tag data
+ setDemodBuf(DemodBuffer+ans, 104, 0);