Skip to content
This repository has been archived by the owner on Mar 18, 2023. It is now read-only.

Commit

Permalink
Stashing code that correctly makes it through to complete RATS ; Now …
Browse files Browse the repository at this point in the history
…need to work on emsec#313
  • Loading branch information
maxieds committed Jan 16, 2022
1 parent 4478c6d commit 344238b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ static uint16_t ISO144434ProcessBlock(uint8_t* Buffer, uint16_t ByteCount, uint1
const char *debugPrintStr = PSTR("ISO14443-4: length fail");
LogDebuggingMsg(debugPrintStr);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}
ByteCount -= 2;

Expand All @@ -101,7 +100,6 @@ static uint16_t ISO144434ProcessBlock(uint8_t* Buffer, uint16_t ByteCount, uint1
DEBUG_PRINT_P(debugPrintStr, *(uint16_t*)&Buffer[ByteCount],
ISO14443AUpdateCRCA(Buffer, ByteCount, 0x00));
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}

switch (Iso144434State) {
Expand All @@ -112,7 +110,6 @@ static uint16_t ISO144434ProcessBlock(uint8_t* Buffer, uint16_t ByteCount, uint1
const char *debugPrintStr = PSTR("ISO14443-4: NOT RATS");
LogDebuggingMsg(debugPrintStr);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}
/* Process RATS.
* NOTE: ATS bytes are tailored to Chameleon implementation and differ from DESFire spec.
Expand Down Expand Up @@ -148,13 +145,11 @@ static uint16_t ISO144434ProcessBlock(uint8_t* Buffer, uint16_t ByteCount, uint1
const char *debugPrintStr = PSTR("ISO14443-4: NEW CARD ID");
LogDebuggingMsg(debugPrintStr);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}
}
}
case ISO14443_4_STATE_LAST: {
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}
}

Expand All @@ -166,8 +161,6 @@ static uint16_t ISO144434ProcessBlock(uint8_t* Buffer, uint16_t ByteCount, uint1
/* Not currently supported => the frame is ignored */
const char *debugPrintStr = PSTR("ISO144434ProcessBlock: ISO14443_PCB_I_BLOCK -- %d");
DEBUG_PRINT_P(debugPrintStr, __LINE__);
//return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}
/* 7.5.3.2, rule D: toggle on each I-block */
Iso144434BlockNumber = MyBlockNumber = !MyBlockNumber;
Expand All @@ -176,7 +169,6 @@ static uint16_t ISO144434ProcessBlock(uint8_t* Buffer, uint16_t ByteCount, uint1
const char *debugPrintStr = PSTR("ISO144434ProcessBlock: ISO14443_PCB_I_BLOCK -- %d");
DEBUG_PRINT_P(debugPrintStr, __LINE__);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}

/* Build the prologue for the response */
Expand All @@ -194,7 +186,6 @@ static uint16_t ISO144434ProcessBlock(uint8_t* Buffer, uint16_t ByteCount, uint1
const char *debugPrintStr = PSTR("ISO14443-4: APP_NO_RESP");
LogDebuggingMsg(debugPrintStr);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}
ByteCount += PrologueLength;
const char *debugPrintStr = PSTR("ISO14443-4: I-BLK");
Expand All @@ -209,7 +200,6 @@ static uint16_t ISO144434ProcessBlock(uint8_t* Buffer, uint16_t ByteCount, uint1
const char *debugPrintStr = PSTR("ISO144434ProcessBlock: ISO14443_PCB_R_BLOCK -- %d");
DEBUG_PRINT_P(debugPrintStr, __LINE__);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}
if (PCB & ISO14443_PCB_R_BLOCK_ACKNAK_MASK) {
/* 7.5.4.3, rule 12 */
Expand All @@ -222,7 +212,6 @@ static uint16_t ISO144434ProcessBlock(uint8_t* Buffer, uint16_t ByteCount, uint1
const char *debugPrintStr = PSTR("ISO144434ProcessBlock: ISO14443_PCB_R_BLOCK -- %d");
DEBUG_PRINT_P(debugPrintStr, __LINE__);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}
const char *debugPrintStr6 = PSTR("ISO14443-4: R-BLK");
LogDebuggingMsg(debugPrintStr6);
Expand All @@ -241,19 +230,16 @@ static uint16_t ISO144434ProcessBlock(uint8_t* Buffer, uint16_t ByteCount, uint1
const char *debugPrintStr = PSTR("ISO14443-4: S-BLK");
LogDebuggingMsg(debugPrintStr);
return GetAndSetBufferCRCA(Buffer, ByteCount);
//break;
}
const char *debugPrintStr5 = PSTR("ISO14443-4: PCB_S_BLK NO_RESP");
LogDebuggingMsg(debugPrintStr5);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}

}

/* Fall through (default handling when there is no response to register/return to the sender): */
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetBufferCRCA(Buffer, ByteCount);

}

Expand Down Expand Up @@ -305,7 +291,6 @@ uint16_t ISO144433APiccProcess(uint8_t* Buffer, uint16_t BitCount) {

if(BitCount == 0) {
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}

uint8_t Cmd = Buffer[0];
Expand All @@ -322,17 +307,18 @@ uint16_t ISO144433APiccProcess(uint8_t* Buffer, uint16_t BitCount) {
else if (Cmd == ISO14443A_CMD_REQA || Cmd == ISO14443A_CMD_WUPA) {
ISO144433ASwitchState(ISO14443_3A_STATE_IDLE);
CheckStateRetryCount(true);
ISO144434Reset();
decrementRetryCount = false;
}
else if (ISO144433AIsHalt(Buffer, BitCount)) {
LogEntry(LOG_INFO_APP_CMD_HALT, NULL, 0);
ISO144433ASwitchState(ISO14443_3A_STATE_HALT);
LastReaderSentCmd = Cmd;
CheckStateRetryCount(true);
ISO144434Reset();
const char *logMsg = PSTR("ISO14443-3: HALTING");
LogDebuggingMsg(logMsg);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}
LastReaderSentCmd = Cmd;
if (decrementRetryCount && StateRetryCount > 0) {
Expand Down Expand Up @@ -367,7 +353,6 @@ uint16_t ISO144433APiccProcess(uint8_t* Buffer, uint16_t BitCount) {
const char *debugPrintStr = PSTR("ISO14443-4 (IDLE): ATQA");
LogDebuggingMsg(debugPrintStr);
return ISO14443A_ATQA_FRAME_SIZE_BYTES * BITS_PER_BYTE;
//return GetAndSetBufferCRCA(Buffer, ISO14443A_ATQA_FRAME_SIZE_BYTES);

case ISO14443_3A_STATE_READY1:
if (Cmd == ISO14443A_CMD_SELECT_CL1) {
Expand All @@ -392,8 +377,6 @@ uint16_t ISO144433APiccProcess(uint8_t* Buffer, uint16_t BitCount) {
LogDebuggingMsg(debugPrintStr);
}
return BitCount;
//uint16_t ByteCount = (BitCount + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
//return GetAndSetBufferCRCA(Buffer, ByteCount);
}
const char *debugPrintStr4 = PSTR("ISO14443-4: RDY1, NOT SLCT CMD");
LogDebuggingMsg(debugPrintStr4);
Expand All @@ -416,8 +399,6 @@ uint16_t ISO144433APiccProcess(uint8_t* Buffer, uint16_t BitCount) {
LogDebuggingMsg(debugPrintStr);
}
return BitCount;
//uint16_t ByteCount = (BitCount + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
//return GetAndSetBufferCRCA(Buffer, ByteCount);
}
const char *debugPrintStr3 = PSTR("RDY2, NOT SLCT CMD");
LogDebuggingMsg(debugPrintStr3);
Expand All @@ -431,15 +412,13 @@ uint16_t ISO144433APiccProcess(uint8_t* Buffer, uint16_t BitCount) {
const char *logMsg = PSTR("ISO14443-3: Got HALT");
LogDebuggingMsg(logMsg);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}
/* Forward to ISO/IEC 14443-4 processing code */
uint16_t ReturnBits = ISO144434ProcessBlock(Buffer, (BitCount + BITS_PER_BYTE - 1) / BITS_PER_BYTE, BitCount);
//uint16_t ReturnBytes = (ReturnBits + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
uint16_t ByteCount = (BitCount + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
uint16_t ReturnBits = ISO144434ProcessBlock(Buffer, ByteCount, BitCount);
const char *debugPrintStr2 = PSTR("ISO14443-4: ACTIVE RET");
LogDebuggingMsg(debugPrintStr2);
return ReturnBits;
//return GetAndSetBufferCRCA(Buffer, ReturnBytes);

default:
break;
Expand All @@ -452,12 +431,10 @@ uint16_t ISO144433APiccProcess(uint8_t* Buffer, uint16_t BitCount) {
const char *logMsg = PSTR("ISO14443-3: RESET TO IDLE 0x%02x");
DEBUG_PRINT_P(logMsg, Cmd);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);
}
const char *debugPrintStr = PSTR("ISO14443-4: UNK-CMD NO_RESP");
LogDebuggingMsg(debugPrintStr);
return ISO14443A_APP_NO_RESPONSE;
//return GetAndSetNoResponseCRCA(Buffer);

}

Expand Down
17 changes: 1 addition & 16 deletions Firmware/Chameleon-Mini/Application/MifareDESFire.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ This notice must be retained at the top of all source files where indicated.
*/
#define __DESFire_RoundToBytes(bitCount) ((unsigned int) (bitCount + BITS_PER_BYTE - 1) / BITS_PER_BYTE)

#define __DESFireTruncateCRCA(rawBitCount, truncQ) ({ \
uint16_t resultingBitCount = rawBitCount; \
do { \
if(truncQ) \
resultingBitCount = MAX(0, (int) (rawBitCount - ISO14443A_CRC_FRAME_SIZE)); \
} while(0); \
resultingBitCount; \
})

DesfireStateType DesfireState = DESFIRE_HALT;
DesfireStateType DesfirePreviousState = DESFIRE_IDLE;
bool DesfireFromHalt = false;
Expand Down Expand Up @@ -246,18 +237,12 @@ uint16_t MifareDesfireAppProcess(uint8_t* Buffer, uint16_t BitCount) {
return MifareDesfireProcess(Buffer, BitCount);
}
else {
/* TODO: Clean up this code !!! */
//uint8_t Cmd = Buffer[0];
//bool truncateCRCA = (Cmd == ISO14443A_CMD_REQA || Cmd == ISO14443A_CMD_WUPA || Cmd == ISO14443A_CMD_RNAK ||
// IsCmdSelectRound1(Buffer));
bool truncateCRCA = false;
uint16_t ProcessedBitCount = ISO144433APiccProcess(Buffer, BitCount);
if(ProcessedBitCount == ISO14443A_APP_NO_RESPONSE) {
const char *debugNoResponse = PSTR("APP_NO_RESP");
LogDebuggingMsg(debugNoResponse);
}
//truncateCRCA = truncateCRCA && (ProcessedBitCount != 0);
return __DESFireTruncateCRCA(ProcessedBitCount, truncateCRCA);
return ProcessedBitCount;
}
}

Expand Down
3 changes: 2 additions & 1 deletion Firmware/Chameleon-Mini/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ SETTINGS += -DDESFIRE_MEMORY_LIMITED_TESTING
#Set a minimum incoming/outgoing log size so we do not spam the
#Chameleon Mini logs to much by logging everything:
SETTINGS += -DDESFIRE_MIN_INCOMING_LOGSIZE=1
SETTINGS += -DDESFIRE_MIN_OUTGOING_LOGSIZE=1
#SETTINGS += -DDESFIRE_MIN_OUTGOING_LOGSIZE=1
SETTINGS += -DDESFIRE_MIN_OUTGOING_LOGSIZE=0

#Enable printing of crypto tests when a new DESFire emulation instance is started:
#SETTINGS += -DDESFIRE_RUN_CRYPTO_TESTING_PROCEDURE
Expand Down

0 comments on commit 344238b

Please sign in to comment.