diff options
-rw-r--r-- | usb-i2c-sl018/tuer-rfid.c | 53 |
1 files changed, 41 insertions, 12 deletions
diff --git a/usb-i2c-sl018/tuer-rfid.c b/usb-i2c-sl018/tuer-rfid.c index 244315c..3d47edd 100644 --- a/usb-i2c-sl018/tuer-rfid.c +++ b/usb-i2c-sl018/tuer-rfid.c @@ -46,6 +46,7 @@ typedef union
{
uint64_t uint;
+ uint32_t uint32[2];
unsigned char byte[8];
} keyslot_t;
keyslot_t EEMEM keyslots[EEPROM_SIZE/sizeof(keyslot_t)];
@@ -77,6 +78,19 @@ uint8_t card_status =0; FILE usb_stream;
FILE * usb = &usb_stream;
+
+#define KEY_LEN_MAX (sizeof(keyslot_t) - 1)
+
+/* this generates a Fletcher8 checksum */
+/* code from: http://stackoverflow.com/questions/13491700/8-bit-fletcher-checksum-of-16-byte-data */
+unsigned char generate_csum(unsigned char* data)
+{
+ uint16_t sum1 = 0xf, sum2 = 0xf, len = KEY_LEN_MAX;
+ do { sum2 += ( sum1 += *data++ ); } while (--len);
+ return sum2<<4 | sum1;
+}
+
+
/*
LUFA Library
Copyright (C) Dean Camera, 2012.
@@ -162,18 +176,26 @@ unsigned char sl018_send_buffer(const unsigned char * buffer) bool check_card(unsigned char * uid, uint8_t uid_len)
{
keyslot_t card;
+ keyslot_t eeprom_ks;
card.uint=0;
for (uint8_t pos=0; pos<uid_len; pos++)
- card.byte[pos]=uid[pos];
- keyslot_t * ks;
- for(ks=keyslots;ks<keyslots+EEPROM_SIZE/sizeof(keyslot_t);ks++)
+ card.byte[pos]=uid[uid_len-pos-1];
+ card.byte[KEY_LEN_MAX]=generate_csum(&card.byte[0]);
+ keyslot_t * ks=keyslots;
+ bool valid=0;
+ for(uint8_t ks_num=0;ks_num<(EEPROM_SIZE/sizeof(card));ks_num++)
{
- if(card.uint==ks->uint)
+ eeprom_read_block(&eeprom_ks,&ks[ks_num],sizeof(eeprom_ks));
+ if(card.uint==eeprom_ks.uint)
{
- fprintf(usb,"Found\n");
- return 1;
+ valid=1;
+ // break; //this breaks security
}
}
+ if (valid)
+ {
+ return 1;
+ }
return 0;
}
@@ -230,7 +252,6 @@ bool sl018_send_buffer_check_error(const unsigned char * buffer) } else {
if(twi_message->status)
{
- CDC_Device_SendString(&VirtualSerial_CDC_Interface, "SL018 Cmd,Error: ");
fprintf(usb,"SL018 Cmd,Error: %02X,%02X\n\r",twi_message->command,twi_message->status);
return 0;
}
@@ -280,11 +301,19 @@ void card_in_action(void) fprintf(usb, "CARD IN: ");
if(sl018_send_buffer_check_error(SL018CMD_ComSelectCard))
{
- for (pos=twi_message->len - 4 ; pos< 255; pos--)
- {
- fprintf(usb,"%02X",twi_message->data[pos]);
- }
- fprintf(usb, "\n\r");
+ for (pos=twi_message->len - 4 ; pos< 255; pos--)
+ {
+ fprintf(usb,"%02X",twi_message->data[pos]);
+ }
+ fprintf(usb, "\n\r");
+ uint8_t type = twi_message->data[twi_message->len-1-sizeof(twi_message->command)-sizeof(twi_message->status)];
+ if (0<type && type < 7)
+ {
+ if(check_card(&twi_message->data[0],twi_message->len-1-sizeof(twi_message->command)-sizeof(twi_message->status)))
+ fprintf(usb,"open\n\r");
+ } else {
+ fprintf(usb,"Unknown Card Type %02x\n\r",type);
+ }
}
}
|