Skip to content

Commit

Permalink
Merge pull request #165 from shinhub/mf_clean
Browse files Browse the repository at this point in the history
Mifare Clean
  • Loading branch information
iceman1001 committed Nov 5, 2019
2 parents db81ad5 + e3d47be commit 4a54102
Show file tree
Hide file tree
Showing 3 changed files with 354 additions and 356 deletions.
95 changes: 89 additions & 6 deletions Firmware/ChameleonMini/Application/ISO14443-3A.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,92 @@ bool ISO14443ACheckCRCA(const void* Buffer, uint16_t ByteCount)
}
*/

/* Coded in H to allow exportable inlining
INLINE bool ISO14443ASelect(void* Buffer, uint16_t* BitCount, uint8_t* UidCL, uint8_t SAKValue);
INLINE bool ISO14443AWakeUp(void* Buffer, uint16_t* BitCount, uint16_t ATQAValue, bool FromHalt);
INLINE bool ISO14443AIsWakeUp(uint8_t* Buffer, bool FromHalt);
INLINE void ISO14443ASetWakeUpResponse(uint8_t* Buffer, uint16_t ATQAValue);
*/
bool ISO14443AIsWakeUp(uint8_t* Buffer, bool FromHalt) {
return ( ((!FromHalt) && (Buffer[0] == ISO14443A_CMD_REQA))
|| (Buffer[0] == ISO14443A_CMD_WUPA) );
}

void ISO14443ASetWakeUpResponse(uint8_t* Buffer, uint16_t ATQAValue) {
Buffer[0] = (ATQAValue >> 0) & 0x00FF;
Buffer[1] = (ATQAValue >> 8) & 0x00FF;
}

bool ISO14443AWakeUp(void* Buffer, uint16_t* BitCount, uint16_t ATQAValue, bool FromHalt)
{
bool ret = false;
uint8_t* DataPtr = (uint8_t*) Buffer;

if ( ISO14443AIsWakeUp(DataPtr, FromHalt) ) {
ISO14443ASetWakeUpResponse(DataPtr, ATQAValue);

*BitCount = ISO14443A_ATQA_FRAME_SIZE;

ret = true;
} else {
*BitCount = 0;
}
return ret;
}

bool ISO14443ASelect(void* Buffer, uint16_t* BitCount, uint8_t* UidCL, uint8_t SAKValue)
{
bool ret = false;
uint8_t* DataPtr = (uint8_t*) Buffer;
uint8_t NVB = DataPtr[1];
//uint8_t CollisionByteCount = (NVB >> 4) & 0x0F;
//uint8_t CollisionBitCount = (NVB >> 0) & 0x0F;

switch (NVB) {
case ISO14443A_NVB_AC_START:
/* Start of anticollision procedure.
* Send whole UID CLn + BCC */
DataPtr[0] = UidCL[0];
DataPtr[1] = UidCL[1];
DataPtr[2] = UidCL[2];
DataPtr[3] = UidCL[3];
DataPtr[4] = ISO14443A_CALC_BCC(DataPtr);

*BitCount = ISO14443A_CL_FRAME_SIZE;

break;

case ISO14443A_NVB_AC_END:
/* End of anticollision procedure.
* Send SAK CLn if we are selected. */
if ( (DataPtr[2] == UidCL[0]) &&
(DataPtr[3] == UidCL[1]) &&
(DataPtr[4] == UidCL[2]) &&
(DataPtr[5] == UidCL[3]) ) {

DataPtr[0] = SAKValue;
ISO14443AAppendCRCA(Buffer, 1);

*BitCount = ISO14443A_SAK_FRAME_SIZE;
ret = true;
} else {
/* We have not been selected. Don't send anything. */
*BitCount = 0;
}
break;
default:
{
uint8_t CollisionBitCount = NVB & 0x0f;
if (CollisionBitCount == 0) {
/* Full-byte anticollision frame supports */
uint8_t CollisionByteCount = ((NVB >> 4) & 0x0f) - 2;
/* Check for our UID is selecting */
if (memcmp(UidCL, &DataPtr[2], CollisionByteCount) != 0) {
*BitCount = 0;
}
memcpy(DataPtr, &UidCL[CollisionByteCount], 4 - CollisionByteCount);
/* Calc original BCC */
DataPtr[4 - CollisionByteCount] = ISO14443A_CALC_BCC(UidCL);
*BitCount = (5 - CollisionByteCount) * BITS_PER_BYTE;
} else {
/* Partial-byte anticollision frame not supported */
*BitCount = 0;
}
}
}
return ret;
}
104 changes: 4 additions & 100 deletions Firmware/ChameleonMini/Application/ISO14443-3A.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,105 +47,9 @@

void ISO14443AAppendCRCA(void* Buffer, uint16_t ByteCount);
bool ISO14443ACheckCRCA(const void* Buffer, uint16_t ByteCount);

/* Coded here to allow exportable inlining */
INLINE bool ISO14443ASelect(void* Buffer, uint16_t* BitCount, uint8_t* UidCL, uint8_t SAKValue);
INLINE bool ISO14443AWakeUp(void* Buffer, uint16_t* BitCount, uint16_t ATQAValue, bool FromHalt);
INLINE bool ISO14443AIsWakeUp(uint8_t* Buffer, bool FromHalt);
INLINE void ISO14443ASetWakeUpResponse(uint8_t* Buffer, uint16_t ATQAValue);

INLINE
bool ISO14443ASelect(void* Buffer, uint16_t* BitCount, uint8_t* UidCL, uint8_t SAKValue)
{
bool ret = false;
uint8_t* DataPtr = (uint8_t*) Buffer;
uint8_t NVB = DataPtr[1];
//uint8_t CollisionByteCount = (NVB >> 4) & 0x0F;
//uint8_t CollisionBitCount = (NVB >> 0) & 0x0F;

switch (NVB) {
case ISO14443A_NVB_AC_START:
/* Start of anticollision procedure.
* Send whole UID CLn + BCC */
DataPtr[0] = UidCL[0];
DataPtr[1] = UidCL[1];
DataPtr[2] = UidCL[2];
DataPtr[3] = UidCL[3];
DataPtr[4] = ISO14443A_CALC_BCC(DataPtr);

*BitCount = ISO14443A_CL_FRAME_SIZE;

break;

case ISO14443A_NVB_AC_END:
/* End of anticollision procedure.
* Send SAK CLn if we are selected. */
if ( (DataPtr[2] == UidCL[0]) &&
(DataPtr[3] == UidCL[1]) &&
(DataPtr[4] == UidCL[2]) &&
(DataPtr[5] == UidCL[3]) ) {

DataPtr[0] = SAKValue;
ISO14443AAppendCRCA(Buffer, 1);

*BitCount = ISO14443A_SAK_FRAME_SIZE;
ret = true;
} else {
/* We have not been selected. Don't send anything. */
*BitCount = 0;
}
break;
default:
{
uint8_t CollisionBitCount = NVB & 0x0f;
if (CollisionBitCount == 0) {
/* Full-byte anticollision frame supports */
uint8_t CollisionByteCount = ((NVB >> 4) & 0x0f) - 2;
/* Check for our UID is selecting */
if (memcmp(UidCL, &DataPtr[2], CollisionByteCount) != 0) {
*BitCount = 0;
}
memcpy(DataPtr, &UidCL[CollisionByteCount], 4 - CollisionByteCount);
/* Calc original BCC */
DataPtr[4 - CollisionByteCount] = ISO14443A_CALC_BCC(UidCL);
*BitCount = (5 - CollisionByteCount) * BITS_PER_BYTE;
} else {
/* Partial-byte anticollision frame not supported */
*BitCount = 0;
}
}
}
return ret;
}

INLINE
bool ISO14443AIsWakeUp(uint8_t* Buffer, bool FromHalt) {
return ( ((!FromHalt) && (Buffer[0] == ISO14443A_CMD_REQA))
|| (Buffer[0] == ISO14443A_CMD_WUPA) );
}

INLINE
void ISO14443ASetWakeUpResponse(uint8_t* Buffer, uint16_t ATQAValue) {
Buffer[0] = (ATQAValue >> 0) & 0x00FF;
Buffer[1] = (ATQAValue >> 8) & 0x00FF;
}

INLINE
bool ISO14443AWakeUp(void* Buffer, uint16_t* BitCount, uint16_t ATQAValue, bool FromHalt)
{
bool ret = false;
uint8_t* DataPtr = (uint8_t*) Buffer;

if ( ISO14443AIsWakeUp(DataPtr, FromHalt) ) {
ISO14443ASetWakeUpResponse(DataPtr, ATQAValue);

*BitCount = ISO14443A_ATQA_FRAME_SIZE;

ret = true;
} else {
*BitCount = 0;
}
return ret;
}
bool ISO14443AIsWakeUp(uint8_t* Buffer, bool FromHalt);
void ISO14443ASetWakeUpResponse(uint8_t* Buffer, uint16_t ATQAValue);
bool ISO14443AWakeUp(void* Buffer, uint16_t* BitCount, uint16_t ATQAValue, bool FromHalt);
bool ISO14443ASelect(void* Buffer, uint16_t* BitCount, uint8_t* UidCL, uint8_t SAKValue);

#endif
Loading

0 comments on commit 4a54102

Please sign in to comment.