many new features, but a bit to big
authorbg <daniel.otte@rub.de>
Fri, 18 Oct 2013 00:22:58 +0000 (02:22 +0200)
committerbg <daniel.otte@rub.de>
Fri, 18 Oct 2013 00:22:58 +0000 (02:22 +0200)
firmware/Makefile
firmware/main.c
firmware/requests.h

index 2172ea9..c669ba5 100644 (file)
@@ -7,7 +7,7 @@
 # License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
 # This Revision: $Id: Makefile 692 2008-11-07 15:07:40Z cs $
 
-DEVICE  = attiny45 #attiny45
+DEVICE  = attiny85 #attiny45
 F_CPU   = 16500000
 FUSE_L  = 0xe1# 1110.0001
 FUSE_H  = 0x9d# 1001.1101
@@ -17,7 +17,7 @@ AVRDUDE_DW  = avrdude -c jtag2dw  -P usb -p $(DEVICE) # edit this line for your
 
 CFLAGS  = -Iusbdrv -I. -DDEBUG_LEVEL=0
 OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o \
-          special_functions.o
+          percnt2.o sha1-asm.o hmac-sha1.o hotp.o special_functions.o
 
 COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE)
 COMPILEPP = avr-g++ -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE)
index a266a59..4502655 100644 (file)
@@ -38,8 +38,8 @@ different port or bit, change the macros below:
 #include "oddebug.h"        /* This is also an example for using debug macros */
 #include "requests.h"       /* The custom request numbers we use */
 #include "special_functions.h"
-
-void update_pwm(void);
+#include "hotp.h"
+#include "percnt2.h"
 
 /* ------------------------------------------------------------------------- */
 /* ----------------------------- USB interface ----------------------------- */
@@ -79,6 +79,11 @@ PROGMEM const char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH]
     0xc0                           // END_COLLECTION
 };
 
+uint16_t secret_length_ee EEMEM = 0;
+uint8_t  secret_ee[32] EEMEM;
+uint8_t  reset_counter_ee EEMEM = 0;
+uint8_t  digits_ee EEMEM = 8;
+
 /* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
  * 10 Keyboard/Keypad Page for more codes.
  */
@@ -145,10 +150,14 @@ PROGMEM const char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH]
 #define CAPS_LOCK 2
 #define SCROLL_LOCK 4
 
+static uint8_t dbg_buffer[8];
 
-#define UNI_BUFFER_SIZE 16
+static uint8_t secret[32];
+static uint16_t secret_length_b;
+static char token[10];
 
-static uint8_t dbg_buffer[8];
+
+#define UNI_BUFFER_SIZE 36
 
 static union {
        uint8_t  w8[UNI_BUFFER_SIZE];
@@ -169,6 +178,7 @@ typedef struct {
 #define STATE_WAIT 0
 #define STATE_SEND_KEY 1
 #define STATE_RELEASE_KEY 2
+#define STATE_NEXT 3
 
 
 static keyboard_report_t keyboard_report; // sent to PC
@@ -177,13 +187,87 @@ static uchar key_state = STATE_WAIT;
 volatile static uchar LED_state = 0xff; // received from PC
 /* ------------------------------------------------------------------------- */
 
+void memory_clean(void) {
+    memset(secret, 0, 32);
+    secret_length_b = 0;
+}
+
+uint8_t secret_set(void){
+    uint8_t r;
+    union {
+        uint8_t w8[32];
+        uint16_t w16[16];
+    } read_back;
+    const uint8_t length_B = (secret_length_b + 7) / 8;
+
+    eeprom_busy_wait();
+    eeprom_write_block(secret, secret_ee, length_B);
+    eeprom_busy_wait();
+    eeprom_read_block(read_back.w8, secret_ee, length_B);
+    r = memcmp(secret, read_back.w8, length_B);
+    memory_clean();
+    memset(read_back.w8, 0, 32);
+    if (r) {
+        return 1;
+    }
+    eeprom_busy_wait();
+    eeprom_write_word(&secret_length_ee, secret_length_b);
+    eeprom_busy_wait();
+    r = eeprom_read_word(&secret_length_ee) == secret_length_b;
+    memory_clean();
+    *read_back.w16 = 0;
+    if (!r) {
+        return 1;
+    }
+    return 0;
+}
+
+void token_generate(void) {
+    percnt_inc(0);
+    eeprom_busy_wait();
+    eeprom_read_block(secret, secret_ee, 32);
+    eeprom_busy_wait();
+    hotp(token, secret, eeprom_read_word(&secret_length_ee), percnt_get(0), eeprom_read_byte(&digits_ee));
+    memory_clean();
+}
+
+void counter_reset(void) {
+    uint8_t reset_counter;
+    eeprom_busy_wait();
+    reset_counter = eeprom_read_byte(&reset_counter_ee);
+    percnt_reset(0);
+    eeprom_busy_wait();
+    eeprom_write_byte(&reset_counter_ee, reset_counter + 1);
+}
+
+void counter_init(void) {
+    eeprom_busy_wait();
+    if (eeprom_read_byte(&reset_counter_ee) == 0) {
+        counter_reset();
+    }
+    percnt_init(0);
+}
+
 void buildReport(uchar send_key) {
     keyboard_report.modifier = 0;
 
-    if(send_key >= 'a' && send_key <= 'z')
-        keyboard_report.keycode[0] = 4 + (send_key - 'a');
-    else
+    switch (send_key) {
+    case 'A' ... 'Z':
+        keyboard_report.modifier = MOD_SHIFT_LEFT;
+        keyboard_report.keycode[0] = KEY_A + (send_key-'A');
+        break;
+    case 'a' ... 'z':
+        keyboard_report.keycode[0] = KEY_A + (send_key-'a');
+        break;
+    case '1' ... '9':
+        keyboard_report.keycode[0] = KEY_1 + (send_key-'1');
+        break;
+    case '0':
+        keyboard_report.keycode[0] = KEY_0;
+        break;
+    default:
         keyboard_report.keycode[0] = 0;
+    }
 }
 
 uint8_t read_button(void){
@@ -242,6 +326,45 @@ usbMsgLen_t usbFunctionSetup(uchar data[8])
                current_command = rq->bRequest;
        switch(rq->bRequest)
                {
+       case CUSTOM_RQ_SET_SECRET:
+           secret_length_b = rq->wValue.word;
+           if (secret_length_b > 256) {
+               secret_length_b = 256;
+           }
+           uni_buffer.w8[0] = 0;
+           return USB_NO_MSG;
+       case CUSTOM_RQ_INC_COUNTER:
+           percnt_inc(0);
+           return 0;
+       case CUSTOM_RQ_GET_COUNTER:
+           uni_buffer.w32[0] = percnt_get(0);
+           usbMsgPtr = (usbMsgPtr_t)uni_buffer.w32;
+           return 4;
+       case CUSTOM_RQ_RESET_COUNTER:
+           counter_reset();
+           return 0;
+        case CUSTOM_RQ_GET_RESET_COUNTER:
+           eeprom_busy_wait();
+            uni_buffer.w8[0] = eeprom_read_byte(&reset_counter_ee);
+            usbMsgPtr = uni_buffer.w8;
+            return 1;
+       case CUSTOM_RQ_SET_DIGITS:
+           if (rq->wValue.bytes[0] > 9) {
+               rq->wValue.bytes[0] = 9;
+           }
+           eeprom_busy_wait();
+           eeprom_write_byte(&digits_ee, rq->wValue.bytes[0]);
+           return 0;
+       case CUSTOM_RQ_GET_DIGITS:
+           eeprom_busy_wait();
+            uni_buffer.w8[0] = eeprom_read_byte(&digits_ee);
+            usbMsgPtr = uni_buffer.w8;
+            return 1;
+       case CUSTOM_RQ_GET_TOKEN:
+           token_generate();
+           usbMsgPtr = token;
+           return strlen(token);
+
        case CUSTOM_RQ_PRESS_BUTTON:
            key_state = STATE_SEND_KEY;
            return 0;
@@ -263,13 +386,14 @@ usbMsgLen_t usbFunctionSetup(uchar data[8])
                        return rq->wLength.word;
                case CUSTOM_RQ_WRITE_MEM:
                case CUSTOM_RQ_EXEC_SPM:
-                       uni_buffer_fill = 4;
+/*                     uni_buffer_fill = 4;
                        uni_buffer.w16[0] = rq->wValue.word;
                        uni_buffer.w16[1] = rq->wLength.word;
                        return USB_NO_MSG;
-               case CUSTOM_RQ_READ_FLASH:
+*/             case CUSTOM_RQ_READ_FLASH:
                        uni_buffer.w16[0] = rq->wValue.word;
                        uni_buffer.w16[1] = rq->wLength.word;
+            uni_buffer_fill = 4;
                        return USB_NO_MSG;
                case CUSTOM_RQ_RESET:
                        soft_reset((uint8_t)(rq->wValue.word));
@@ -297,6 +421,18 @@ uchar usbFunctionWrite(uchar *data, uchar len)
            if (data[0] != LED_state)
                LED_state = data[0];
            return 1; // Data read, not expecting more
+       case CUSTOM_RQ_SET_SECRET:
+        {
+            if (uni_buffer.w8[0] < (secret_length_b + 7) / 8) {
+                memcpy(&secret[uni_buffer.w8[0]], data, len);
+                uni_buffer.w8[0] += len;
+            }
+            if (uni_buffer.w8[0] >= (secret_length_b + 7) / 8) {
+                secret_set();
+                return 1;
+            }
+            return 0;
+        }
        case CUSTOM_RQ_SET_DBG:
                if(len > sizeof(dbg_buffer)){
                        len = sizeof(dbg_buffer);
@@ -388,9 +524,12 @@ void usbEventResetReady(void)
 
 /* ------------------------------------------------------------------------- */
 
+char key_seq[] = "Hello World";
+
 int main(void)
 {
-       uchar   i;
+       uchar  i;
+       size_t idx = 0;
 
     wdt_enable(WDTO_1S);
     /* Even if you don't use the watchdog, turn it off here. On newer devices,
@@ -401,8 +540,6 @@ int main(void)
      * additional hardware initialization.
      */
 
-    memset(&keyboard_report, 0, sizeof(keyboard_report));
-
     init_temperature_sensor();
     usbInit();
     usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
@@ -418,19 +555,25 @@ int main(void)
     sei();
 
     for(;;){                /* main event loop */
-       //      update_pwm();
-               
         wdt_reset();
         usbPoll();
 
         if(usbInterruptIsReady() && key_state != STATE_WAIT){
             switch(key_state) {
             case STATE_SEND_KEY:
-                buildReport('x');
+                buildReport(key_seq[idx]);
                 key_state = STATE_RELEASE_KEY; // release next
                 break;
             case STATE_RELEASE_KEY:
                 buildReport(0);
+                ++idx;
+                if (key_seq[idx] == '\0') {
+                    idx = 0;
+                    key_state = STATE_WAIT;
+                } else {
+                    key_state = STATE_SEND_KEY;
+                }
+                break;
             default:
                 key_state = STATE_WAIT; // should not happen
             }
index 358982c..7cee31d 100644 (file)
 
 #define CUSTOM_RQ_PRESS_BUTTON 0x04
 
-#define CUSTOM_RQ_CLR_DBG      0x05
-#define CUSTOM_RQ_SET_DBG      0x06
-#define CUSTOM_RQ_GET_DBG      0x07
-
-#define CUSTOM_RQ_GET_ADC      0x08
-#define CUSTOM_RQ_READ_MEM     0x10
-#define CUSTOM_RQ_WRITE_MEM    0x11
-#define CUSTOM_RQ_READ_FLASH   0x12
-#define CUSTOM_RQ_EXEC_SPM     0x13
-#define CUSTOM_RQ_RESET        0x14
-#define CUSTOM_RQ_READ_BUTTON  0x15
-#define CUSTOM_RQ_READ_TMPSENS 0x16
-#define LED_WRITE              0x40
+#define CUSTOM_RQ_CLR_DBG           0x05
+#define CUSTOM_RQ_SET_DBG           0x06
+#define CUSTOM_RQ_GET_DBG           0x07
+
+#define CUSTOM_RQ_GET_ADC           0x08
+#define CUSTOM_RQ_READ_MEM          0x10
+#define CUSTOM_RQ_WRITE_MEM         0x11
+#define CUSTOM_RQ_READ_FLASH        0x12
+#define CUSTOM_RQ_EXEC_SPM          0x13
+#define CUSTOM_RQ_RESET             0x14
+#define CUSTOM_RQ_READ_BUTTON       0x15
+#define CUSTOM_RQ_READ_TMPSENS      0x16
+#define LED_WRITE                   0x40
+
+#define CUSTOM_RQ_SET_SECRET        0x60
+#define CUSTOM_RQ_INC_COUNTER       0x62
+#define CUSTOM_RQ_GET_COUNTER       0x63
+#define CUSTOM_RQ_RESET_COUNTER     0x64
+#define CUSTOM_RQ_GET_RESET_COUNTER 0x65
+#define CUSTOM_RQ_SET_DIGITS        0x66
+#define CUSTOM_RQ_GET_DIGITS        0x67
+
+#define CUSTOM_RQ_GET_TOKEN         0x69