summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usb-i2c-sl018/tuer-rfid.c62
1 files changed, 35 insertions, 27 deletions
diff --git a/usb-i2c-sl018/tuer-rfid.c b/usb-i2c-sl018/tuer-rfid.c
index 2c2c3c8..664e810 100644
--- a/usb-i2c-sl018/tuer-rfid.c
+++ b/usb-i2c-sl018/tuer-rfid.c
@@ -42,9 +42,9 @@ typedef uint8_t keyslot_t[8];
keyslot_t EEMEM keystore[EEPROM_SIZE/sizeof(keyslot_t)];
-const char* SL018_cmd_tostring(const uint8_t status)
+const char* SL018_cmd_tostring(const uint8_t cmd)
{
- switch(status) {
+ switch(cmd) {
case 0x01: return "Select Mifare card";
case 0x02: return "Login to a sector";
case 0x03: return "Read a data block";
@@ -60,7 +60,7 @@ const char* SL018_cmd_tostring(const uint8_t status)
case 0x40: return "Control the red led";
case 0xF0: return "Get firmware version";
case 0xFF: return "Reset";
- default: return "unknown status";
+ default: return "unknown";
}
}
@@ -78,7 +78,20 @@ const char* SL018_status_tostring(const uint8_t status)
case 0xC: return "Load key fail";
case 0xD: return "Not authenticate";
case 0xE: return "Not a value block";
- default: return "unknown status";
+ default: return "unknown";
+ }
+}
+
+const char* SL018_tagtype_tostring(const uint8_t type)
+{
+ switch(type) {
+ case 0x1: return "Mifare 1k, 4 byte UID";
+ case 0x2: return "Mifare 1k, 7 byte UID";
+ case 0x3: return "Mifare Ultralight or NATG203, 7 byte UID";
+ case 0x4: return "Mifare 4k, 4 byte UID";
+ case 0x5: return "Mifare 4k, 7 byte UID";
+ case 0x6: return "Mifare DesFire, 7 byte UID";
+ default: return "unknown";
}
}
@@ -242,11 +255,10 @@ uint8_t sl018_cmd(const uint8_t* twi_send_buf)
void flash_keystore_from_stdio(void)
{
keyslot_t ks;
- uint8_t keyslot_pos=0;
uint8_t byte_pos=0;
fprintf(stdio,"Flashing keys:\n\r");
fflush(stdio);
- for(keyslot_pos=0;keyslot_pos<EEPROM_SIZE/sizeof(ks);) {
+ for(uint8_t ks_pos=0;ks_pos<EEPROM_SIZE/sizeof(ks);) {
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();
@@ -256,8 +268,8 @@ void flash_keystore_from_stdio(void)
bytes_received--;
if (byte_pos == sizeof(ks)) {
byte_pos=0;
- eeprom_update_block(&ks,&keystore[keyslot_pos],sizeof(ks));
- keyslot_pos++;
+ eeprom_update_block(&ks,&keystore[ks_pos],sizeof(ks));
+ ks_pos++;
fputc('.', stdio);
fflush(stdio);
led_toggle();
@@ -272,10 +284,8 @@ void flash_keystore_from_stdio(void)
void dump_keystore_to_stdio(void)
{
keyslot_t ks;
- uint8_t keyslot_pos=0;
-
- for(keyslot_pos=0;keyslot_pos<EEPROM_SIZE/sizeof(ks);keyslot_pos++) {
- eeprom_read_block(&ks,&keystore[keyslot_pos],sizeof(ks));
+ for(uint8_t ks_pos=0;ks_pos<EEPROM_SIZE/sizeof(ks);ks_pos++) {
+ eeprom_read_block(&ks,&keystore[ks_pos],sizeof(ks));
for (uint8_t i=0; i< sizeof(ks); i++)
fprintf(stdio,"%02X",ks[i]);
fprintf(stdio,"\n\r");
@@ -329,9 +339,9 @@ uint8_t generate_csum(uint8_t* data)
uint8_t compare_keyslots(const keyslot_t a, const keyslot_t b)
{
- uint8_t tmp=0, i;
+ uint8_t tmp=0;
// constant time compare
- for(i=0; i<sizeof(keyslot_t); ++i)
+ for(uint8_t i=0; i<sizeof(keyslot_t); ++i)
tmp |= a[i] ^ b[i];
return tmp;
}
@@ -340,16 +350,15 @@ bool check_card(const uint8_t * uid, uint8_t uid_len)
{
keyslot_t card, ks;
memset(card, 0, sizeof(card));
- uid_len = uid_len > sizeof(keyslot_t) ? sizeof(keyslot_t) : uid_len;
for (uint8_t pos=0; pos<uid_len; pos++)
card[pos]=uid[uid_len-pos-1];
- card[sizeof(keyslot_t) - 1]=generate_csum(card);
+ card[sizeof(keyslot_t)-1]=generate_csum(card);
bool valid=0;
- for(uint8_t ks_num=0;ks_num<(EEPROM_SIZE/sizeof(card));ks_num++) {
- eeprom_read_block(&ks,&keystore[ks_num],sizeof(ks));
+ for(uint8_t ks_pos=0;ks_pos<(EEPROM_SIZE/sizeof(ks));ks_pos++) {
+ eeprom_read_block(&ks,&keystore[ks_pos],sizeof(ks));
if(!compare_keyslots(card, ks)) {
valid=1;
- // break; // this would break security (not const time)
+ // break; // this would break security (not constant time)
}
}
return valid;
@@ -357,22 +366,21 @@ bool check_card(const uint8_t * uid, uint8_t uid_len)
void handle_card(void)
{
- uint8_t pos;
fprintf(stdio, "CARD IN: ");
if(!sl018_cmd(SL018_CMD_ComSelectCard))
{
uint8_t uid_len = twi_recv_msg->len - sizeof(twi_recv_msg->command) - sizeof(twi_recv_msg->status) - 1;
if(uid_len == 255 || uid_len > sizeof(keyslot_t) - 1) {
- fprintf(stdio,"Invalid uid_len received %d\n\r", uid_len);
+ fprintf(stdio," Invalid uid length received %d\n\r", uid_len);
return;
}
+ for (uint8_t pos=0; pos<uid_len; pos++)
+ fprintf(stdio,"%02X",twi_recv_msg->data[uid_len-pos-1]);
- for (pos=uid_len-1; pos<255; pos--)
- fprintf(stdio,"%02X",twi_recv_msg->data[pos]);
-
- fprintf(stdio, "\n\r");
uint8_t type = twi_recv_msg->data[uid_len];
- if (0<type && type < 7) {
+ fprintf(stdio, ", %s\n\r", SL018_tagtype_tostring(type));
+
+ if (0 < type && type < 7) {
if(check_card(twi_recv_msg->data,uid_len)) {
sl018_cmd(SL018_CMD_ComRedLedOn);
fprintf(stdio,"Card allowed - opening/closing door\n\r"); // TODO: open/close door!
@@ -382,7 +390,7 @@ void handle_card(void)
fprintf(stdio,"Card not found in Database\n\r");
}
} else {
- fprintf(stdio,"Unknown Card Type %02x\n\r",type);
+ fprintf(stdio,"Ignoring unknown card type %02x\n\r",type);
}
}
}