Skip to content

Commit

Permalink
Merge pull request #143 from shinhub/memory_lib
Browse files Browse the repository at this point in the history
Rewrite flash and memory management codes
  • Loading branch information
iceman1001 authored Sep 14, 2019
2 parents 91f1cf8 + ff16fc9 commit c25ffde
Show file tree
Hide file tree
Showing 22 changed files with 1,148 additions and 759 deletions.
18 changes: 9 additions & 9 deletions Firmware/ChameleonMini/Application/Detection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "ISO14443-3A.h"
#include "../Codec/ISO14443-2A.h"
#include "../Memory.h"
#include "../Memory/Memory.h"
#include "../LED.h"
#include "../Random.h"
#include "../Settings.h"
Expand Down Expand Up @@ -78,7 +78,7 @@ uint16_t MifareDetectionAppProcess(uint8_t* Buffer, uint16_t BitCount)
if (ISO14443AWakeUp(Buffer, &BitCount, CardATQAValue, true)) {
State = STATE_READY;
return BitCount;
}
}
break;

case STATE_READY:
Expand All @@ -88,15 +88,15 @@ uint16_t MifareDetectionAppProcess(uint8_t* Buffer, uint16_t BitCount)
} else if (Buffer[0] == ISO14443A_CMD_SELECT_CL1) {
/* Load UID CL1 and perform anti-collision */
uint8_t UidCL1[4];
MemoryReadBlock(UidCL1, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
AppMemoryRead(UidCL1, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
if (ISO14443ASelect(Buffer, &BitCount, UidCL1, CardSAKValue)) {
State = STATE_ACTIVE;
}
return BitCount;
} else {
/* Unknown command. Enter HALT state. */
State = STATE_HALT;
}
}
break;

case STATE_ACTIVE:
Expand All @@ -109,7 +109,7 @@ uint16_t MifareDetectionAppProcess(uint8_t* Buffer, uint16_t BitCount)

/* Generate a random nonce and read UID and key from memory */
RandomGetBuffer(CardNonce, sizeof(CardNonce));

memcpy(data_save, Buffer, 4);
memcpy(data_save+4, CardNonce, 4);

Expand All @@ -130,7 +130,7 @@ uint16_t MifareDetectionAppProcess(uint8_t* Buffer, uint16_t BitCount)

case STATE_AUTHING:
State = STATE_IDLE;

// Save information
memcpy(data_save + 8, Buffer, 8);

Expand All @@ -143,16 +143,16 @@ uint16_t MifareDetectionAppProcess(uint8_t* Buffer, uint16_t BitCount)
// KEY A) 0,1,2,3,4,5 * 16 + 4096
// KEY B) 0,1,2,3,4,5 * 16 + +112 + 4096
// 5*16 ?
MemoryWriteBlock(data_save, (turn_flaga * MEM_BYTES_PER_BLOCK) + 4096, MEM_BYTES_PER_BLOCK);
AppMemoryWrite(data_save, (turn_flaga * MEM_BYTES_PER_BLOCK) + 4096, MEM_BYTES_PER_BLOCK);
turn_flaga++;
turn_flaga = turn_flaga % 6;
} else {
MemoryWriteBlock(data_save, (turn_flagb * MEM_BYTES_PER_BLOCK) + 112 + 4096, MEM_BYTES_PER_BLOCK);
AppMemoryWrite(data_save, (turn_flagb * MEM_BYTES_PER_BLOCK) + 112 + 4096, MEM_BYTES_PER_BLOCK);
turn_flagb++;
turn_flagb = turn_flagb % 6;
}
break;

default:
break;
}
Expand Down
44 changes: 22 additions & 22 deletions Firmware/ChameleonMini/Application/MifareClassic.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "ISO14443-3A.h"
#include "../Codec/ISO14443-2A.h"
#include "../Memory.h"
#include "../Memory/Memory.h"
#include "Crypto1.h"

#define MFCLASSIC_1K_ATQA_VALUE 0x0004
Expand Down Expand Up @@ -520,7 +520,7 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount)
retSize = ACK_NAK_FRAME_SIZE;
} else if (Buffer[0] == CMD_READ) {
/* Read command. Read data from memory and append CRCA. */
MemoryReadBlock(Buffer, (uint16_t) Buffer[1] * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
AppMemoryRead(Buffer, (uint16_t) Buffer[1] * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
ISO14443AAppendCRCA(Buffer, MEM_BYTES_PER_BLOCK);
retSize = (CMD_READ_RESPONSE_FRAME_SIZE + ISO14443A_CRCA_SIZE)
* BITS_PER_BYTE;
Expand All @@ -541,7 +541,7 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount)
if (ISO14443ACheckCRCA(Buffer, MEM_BYTES_PER_BLOCK)) {
/* CRC check passed. Write data into memory and send ACK. */
if (!ActiveConfiguration.ReadOnly) {
MemoryWriteBlock(Buffer, CurrentAddress * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
AppMemoryWrite(Buffer, CurrentAddress * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
}
Buffer[0] = ACK_VALUE;
} else {
Expand All @@ -558,13 +558,13 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount)
/* Load UID CL1 and perform anticollision */
uint8_t UidCL1[ISO14443A_CL_UID_SIZE];
if (is7BitsUID) {
MemoryReadBlock(&UidCL1[1], MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE-1);
AppMemoryRead(&UidCL1[1], MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE-1);
UidCL1[0] = ISO14443A_UID0_CT;
if (ISO14443ASelect(Buffer, &BitCount, UidCL1, SAK_CL1_VALUE)) {
State = STATE_READY2;
}
} else {
MemoryReadBlock(UidCL1, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
AppMemoryRead(UidCL1, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
if (ISO14443ASelect(Buffer, &BitCount, UidCL1, CardSAKValue)) {
/* TODO: Access control not implemented yet
* AccessAddress = MEM_INVALID_ADDRESS; // invalid, force reload */
Expand All @@ -583,7 +583,7 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount)
if (Buffer[0] == ISO14443A_CMD_SELECT_CL2) {
/* Load UID CL2 and perform anticollision */
uint8_t UidCL2[ISO14443A_CL_UID_SIZE];
MemoryReadBlock(UidCL2, MEM_UID_CL2_ADDRESS, MEM_UID_CL2_SIZE);
AppMemoryRead(UidCL2, MEM_UID_CL2_ADDRESS, MEM_UID_CL2_SIZE);
if (ISO14443ASelect(Buffer, &BitCount, UidCL2, CardSAKValue)) {
/* TODO: Access control not implemented yet
AccessAddress = MEM_INVALID_ADDRESS; // invalid, force reload */
Expand Down Expand Up @@ -621,11 +621,11 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount)
/* Generate a random nonce and read UID and key from memory */
//RandomGetBuffer(CardNonce, sizeof(CardNonce));
if (is7BitsUID) {
MemoryReadBlock(Uid, MEM_UID_CL2_ADDRESS, MEM_UID_CL2_SIZE);
AppMemoryRead(Uid, MEM_UID_CL2_ADDRESS, MEM_UID_CL2_SIZE);
} else {
MemoryReadBlock(Uid, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
AppMemoryRead(Uid, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
}
MemoryReadBlock(Key, KeyAddress, MEM_KEY_SIZE);
AppMemoryRead(Key, KeyAddress, MEM_KEY_SIZE);
/* Precalculate the reader response from card-nonce */
memcpy(ReaderResponse, CardNonceSuccessor1, 4);
/* Precalculate our response from the reader response */
Expand Down Expand Up @@ -703,7 +703,7 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount)
} else {
if (Buffer[0] == CMD_READ) {
/* Read command. Read data from memory and append CRCA. */
MemoryReadBlock(Buffer, (uint16_t) Buffer[1] * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
AppMemoryRead(Buffer, (uint16_t) Buffer[1] * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
ISO14443AAppendCRCA(Buffer, MEM_BYTES_PER_BLOCK);
/* Encrypt and calculate parity bits. */
for (uint8_t i=0; i<(ISO14443A_CRCA_SIZE + MEM_BYTES_PER_BLOCK); i++) {
Expand All @@ -728,7 +728,7 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount)
} else if (Buffer[0] == CMD_TRANSFER) {
/* Write back the global block buffer to the desired block address */
if (!ActiveConfiguration.ReadOnly) {
MemoryWriteBlock(BlockBuffer, (uint16_t) Buffer[1] * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
AppMemoryWrite(BlockBuffer, (uint16_t) Buffer[1] * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
} else {
/* In read only mode, silently ignore the write */
}
Expand Down Expand Up @@ -769,11 +769,11 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount)
/* Generate a random nonce and read UID and key from memory */
//RandomGetBuffer(CardNonce, sizeof(CardNonce));
if (is7BitsUID) {
MemoryReadBlock(Uid, MEM_UID_CL2_ADDRESS, MEM_UID_CL2_SIZE);
AppMemoryRead(Uid, MEM_UID_CL2_ADDRESS, MEM_UID_CL2_SIZE);
} else {
MemoryReadBlock(Uid, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
AppMemoryRead(Uid, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
}
MemoryReadBlock(Key, KeyAddress, MEM_KEY_SIZE);
AppMemoryRead(Key, KeyAddress, MEM_KEY_SIZE);
/* Precalculate the reader response from card-nonce */
for (uint8_t i=0; i<sizeof(ReaderResponse); i++) {
ReaderResponse[i] = CardNonce[i];
Expand Down Expand Up @@ -814,7 +814,7 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount)
}
if (ISO14443ACheckCRCA(Buffer, MEM_BYTES_PER_BLOCK)) {
if (!ActiveConfiguration.ReadOnly) {
MemoryWriteBlock(Buffer, CurrentAddress * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
AppMemoryWrite(Buffer, CurrentAddress * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
} else {
/* Silently ignore in ReadOnly mode */
}
Expand All @@ -839,7 +839,7 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount)
Buffer[i] ^= Crypto1Byte();
}
if (ISO14443ACheckCRCA(Buffer, MEM_VALUE_SIZE )) {
MemoryReadBlock(BlockBuffer, (uint16_t) CurrentAddress * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
AppMemoryRead(BlockBuffer, (uint16_t) CurrentAddress * MEM_BYTES_PER_BLOCK, MEM_BYTES_PER_BLOCK);
if (CheckValueIntegrity(BlockBuffer)) {
uint32_t ParamValue;
uint32_t BlockValue;
Expand Down Expand Up @@ -879,21 +879,21 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount)
void MifareClassicGetUid(ConfigurationUidType Uid)
{
if (is7BitsUID) {
MemoryReadBlock(&Uid[0], MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE-1);
MemoryReadBlock(&Uid[3], MEM_UID_CL2_ADDRESS, MEM_UID_CL2_SIZE);
AppMemoryRead(&Uid[0], MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE-1);
AppMemoryRead(&Uid[3], MEM_UID_CL2_ADDRESS, MEM_UID_CL2_SIZE);
} else {
MemoryReadBlock(Uid, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
AppMemoryRead(Uid, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
}
}

void MifareClassicSetUid(ConfigurationUidType Uid)
{
if (is7BitsUID) {
MemoryWriteBlock(Uid, MEM_UID_CL1_ADDRESS, ActiveConfiguration.UidSize);
AppMemoryWrite(Uid, MEM_UID_CL1_ADDRESS, ActiveConfiguration.UidSize);
} else {
uint8_t BCC = Uid[0] ^ Uid[1] ^ Uid[2] ^ Uid[3];
MemoryWriteBlock(Uid, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
MemoryWriteBlock(&BCC, MEM_UID_BCC1_ADDRESS, ISO14443A_CL_BCC_SIZE);
AppMemoryWrite(Uid, MEM_UID_CL1_ADDRESS, MEM_UID_CL1_SIZE);
AppMemoryWrite(&BCC, MEM_UID_BCC1_ADDRESS, ISO14443A_CL_BCC_SIZE);
}
}

Expand Down
Loading

0 comments on commit c25ffde

Please sign in to comment.