Skip to content

Commit

Permalink
Merge pull request #109 from McEloff/ntag_new_format_gui
Browse files Browse the repository at this point in the history
Ultralight EV1/NTAG new dump format supports with new GUI compiled
  • Loading branch information
iceman1001 authored Aug 14, 2019
2 parents 89d3246 + 60264c7 commit 566fb27
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions Firmware/Chameleon-Mini/Application/MifareUltralight.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@
#define CONF_ACCESS_CNFLCK 0x40

#define CNT_MAX 2
#define CNT_SIZE 4
#define CNT_SIZE 3
#define CNT_MAX_VALUE 0x00FFFFFF
#define TEARING_SIZE 1

#define BYTES_PER_READ 16
#define PAGE_READ_MIN 0x00
Expand All @@ -78,7 +79,10 @@
#define BYTES_PER_COMPAT_WRITE 16

#define VERSION_INFO_LENGTH 8
#define VERSION_OFFSET_PAGES 3

#define SIGNATURE_LENGTH 32
#define SIGNATURE_OFFSET_PAGES 5

static enum {
UL_EV0,
Expand Down Expand Up @@ -300,15 +304,19 @@ static uint16_t AppProcess(uint8_t* const Buffer, uint16_t ByteCount)
switch (Cmd) {

case CMD_GET_VERSION: {
/* Provide hardcoded version response */
Buffer[0] = 0x00;
Buffer[1] = 0x04;
Buffer[2] = 0x03;
Buffer[3] = 0x01; /**/
Buffer[4] = 0x01;
Buffer[5] = 0x00;
Buffer[6] = PageCount == MIFARE_ULTRALIGHT_EV11_PAGES ? 0x0B : 0x0E;
Buffer[7] = 0x03;
/* Check new dump format and get version from out of pages area (skip 3 pages of counters) */
MemoryReadBlock(Buffer, (PageCount + VERSION_OFFSET_PAGES) * MIFARE_ULTRALIGHT_PAGE_SIZE, VERSION_INFO_LENGTH);
if (Buffer[6] != 0x0B && Buffer[6] != 0x0E) {
/* Provide hardcoded version response */
Buffer[0] = 0x00;
Buffer[1] = 0x04;
Buffer[2] = 0x03;
Buffer[3] = 0x01; /**/
Buffer[4] = 0x01;
Buffer[5] = 0x00;
Buffer[6] = PageCount == MIFARE_ULTRALIGHT_EV11_PAGES ? 0x0B : 0x0E;
Buffer[7] = 0x03;
}
ISO14443AAppendCRCA(Buffer, VERSION_INFO_LENGTH);
return (VERSION_INFO_LENGTH + ISO14443A_CRCA_SIZE) * 8;
}
Expand Down Expand Up @@ -371,45 +379,58 @@ static uint16_t AppProcess(uint8_t* const Buffer, uint16_t ByteCount)
return NAK_FRAME_SIZE;
}
/* Returned counter length is 3 bytes */
MemoryReadBlock(Buffer, (PageCount + CounterId) * MIFARE_ULTRALIGHT_PAGE_SIZE, 3);
ISO14443AAppendCRCA(Buffer, 3);
MemoryReadBlock(Buffer, (PageCount + CounterId) * MIFARE_ULTRALIGHT_PAGE_SIZE, CNT_SIZE);
ISO14443AAppendCRCA(Buffer, CNT_SIZE);
return (3 + ISO14443A_CRCA_SIZE) * 8;
}

case CMD_INCREMENT_CNT: {
uint8_t CounterId = Buffer[1];
uint32_t Addend = (Buffer[0]) | (Buffer[1] << 8) | ((uint32_t)Buffer[2] << 16);
uint32_t Counter;
uint32_t Counter = 0;
/* Validation */
if (CounterId > CNT_MAX) {
Buffer[0] = NAK_INVALID_ARG;
return NAK_FRAME_SIZE;
}
/* Read the value out */
MemoryReadBlock(&Counter, (PageCount + CounterId) * MIFARE_ULTRALIGHT_PAGE_SIZE, MIFARE_ULTRALIGHT_PAGE_SIZE);
MemoryReadBlock(&Counter, (PageCount + CounterId) * MIFARE_ULTRALIGHT_PAGE_SIZE, CNT_SIZE);
/* Add and check for overflow */
Counter += Addend;
if (Counter > CNT_MAX_VALUE) {
Buffer[0] = NAK_CTR_ERROR;
return NAK_FRAME_SIZE;
}
/* Update memory */
MemoryWriteBlock(&Counter, (PageCount + CounterId) * MIFARE_ULTRALIGHT_PAGE_SIZE, MIFARE_ULTRALIGHT_PAGE_SIZE);
MemoryWriteBlock(&Counter, (PageCount + CounterId) * MIFARE_ULTRALIGHT_PAGE_SIZE, CNT_SIZE);
Buffer[0] = ACK_VALUE;
return ACK_FRAME_SIZE;
}

case CMD_READ_SIG:
/* Hardcoded response */
memset(Buffer, 0xCA, SIGNATURE_LENGTH);
case CMD_READ_SIG: {
/* Check new dump format and get signature from out of pages area (skip 3 pages of counters, 2 pages VERSION) */
MemoryReadBlock(Buffer, (PageCount + SIGNATURE_OFFSET_PAGES) * MIFARE_ULTRALIGHT_PAGE_SIZE, SIGNATURE_LENGTH);
uint8_t zeros[SIGNATURE_LENGTH];
memset(zeros, 0, SIGNATURE_LENGTH);
if (memcmp(Buffer, zeros, SIGNATURE_LENGTH) == 0) {
/* Hardcoded response */
memset(Buffer, 0xCA, SIGNATURE_LENGTH);
}
ISO14443AAppendCRCA(Buffer, SIGNATURE_LENGTH);
return (SIGNATURE_LENGTH + ISO14443A_CRCA_SIZE) * 8;
}

case CMD_CHECK_TEARING_EVENT:
case CMD_CHECK_TEARING_EVENT: {
uint8_t CounterId = Buffer[1];
/* Read tearing flag from counter pages of new dump format */
MemoryReadBlock(Buffer, (PageCount + CounterId) * MIFARE_ULTRALIGHT_PAGE_SIZE + CNT_SIZE, TEARING_SIZE);
/* Hardcoded response */
Buffer[0] = 0xBD;
ISO14443AAppendCRCA(Buffer, 1);
if (Buffer[0] == 0) {
Buffer[0] = 0xBD;
}
ISO14443AAppendCRCA(Buffer, TEARING_SIZE);
return (1 + ISO14443A_CRCA_SIZE) * 8;
}

case CMD_VCSL: {
uint8_t ConfigAreaAddress = PageCount * MIFARE_ULTRALIGHT_PAGE_SIZE - CONFIG_AREA_SIZE;
Expand Down

0 comments on commit 566fb27

Please sign in to comment.