X-Git-Url: https://git.cryptolib.org/?p=labortage2013badge.git;a=blobdiff_plain;f=firmware%2Fmain.c;h=247fcedcc018921775c429881e827774a63136ae;hp=5bcb9345f1a0bb06390884dde38c1f7d00d5f4f7;hb=46e9b3d1c8a23c3884c8edf4ace96ff650333648;hpb=9774c6cbab2971904929c1ad28c34664b355c3f9 diff --git a/firmware/main.c b/firmware/main.c index 5bcb934..247fced 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -15,10 +15,11 @@ different I/O pins for USB. Please note that USB D+ must be the INT0 pin, or at least be connected to INT0 as well. */ -#define BUTTON_PIN 4 - +#define BUTTON_PIN 5 +#define DEBOUNCE_DELAY 50 #define SIMPLE_COUNTER 1 #define NO_CHECK 1 +#define ALLOW_SECRET_READ 0 #include #include @@ -34,6 +35,7 @@ at least be connected to INT0 as well. #include "usbdrv.h" #include "requests.h" /* The custom request numbers we use */ #include "hotp.h" +#include "special_functions.h" #if !SIMPLE_COUNTER #include "percnt2.h" #endif @@ -48,6 +50,8 @@ at least be connected to INT0 as well. #define STATE_RELEASE_KEY 2 #define STATE_NEXT 3 +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) + PROGMEM const char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ 0x09, 0x06, /* USAGE (Keyboard) */ @@ -207,14 +211,23 @@ void counter_init(void) { static void token_generate(void) { + uint16_t s_length_b; + uint8_t digits; counter_inc(); eeprom_busy_wait(); eeprom_read_block(secret, secret_ee, 32); eeprom_busy_wait(); + s_length_b = eeprom_read_word(&secret_length_ee); + if (s_length_b > 256) { + s_length_b = 256; + } + eeprom_busy_wait(); + digits = eeprom_read_byte(&digits_ee); #if SIMPLE_COUNTER - hotp(token, secret, eeprom_read_word(&secret_length_ee), eeprom_read_dword(&counter_ee), eeprom_read_byte(&digits_ee)); + eeprom_busy_wait(); + hotp(token, secret, s_length_b, eeprom_read_dword(&counter_ee) - 1, digits); #else - hotp(token, secret, eeprom_read_word(&secret_length_ee), percnt_get(0), eeprom_read_byte(&digits_ee)); + hotp(token, secret, s_length_b, percnt_get(0) - 1, digits); #endif memory_clean(); } @@ -223,7 +236,6 @@ void token_generate(void) { static void buildReport(uchar send_key) { keyboard_report.modifier = 0; - switch (send_key) { case '1' ... '9': keyboard_report.keycode[0] = KEY_1 + (send_key-'1'); @@ -237,13 +249,13 @@ void buildReport(uchar send_key) { } static -int8_t button_get_debounced(volatile uint8_t debounce_count) { +int8_t button_get_debounced(volatile int8_t debounce_count) { uint8_t v; v = PINB & _BV(BUTTON_PIN); - while (debounce_count-- && v == (PINB & _BV(BUTTON_PIN))) { + while (debounce_count-- && (v == (PINB & _BV(BUTTON_PIN)))) { ; } - if (debounce_count) { + if (debounce_count != -1) { return -1; } return v ? 0 : 1; @@ -330,20 +342,23 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) return 0; case CUSTOM_RQ_SET_DBG: return USB_NO_MSG; - case CUSTOM_RQ_GET_DBG:{ - usbMsgLen_t len = 8; - if(len > rq->wLength.word){ - len = rq->wLength.word; - } + case CUSTOM_RQ_GET_DBG: usbMsgPtr = dbg_buffer; - return len; - } + return MIN(8, rq->wLength.word); case CUSTOM_RQ_RESET: soft_reset((uint8_t)(rq->wValue.word)); break; case CUSTOM_RQ_READ_BUTTON: - uni_buffer.w8[0] = button_get_debounced(25); + uni_buffer.w8[0] = button_get_debounced(DEBOUNCE_DELAY); return 1; +#if ALLOW_SECRET_READ + case CUSTOM_RQ_GET_SECRET: + uni_buffer.w8[0] = 0; + return USB_NO_MSG; +#else +#endif + default: + return 0; } } @@ -376,14 +391,26 @@ uchar usbFunctionWrite(uchar *data, uchar len) len = sizeof(dbg_buffer); } memcpy(dbg_buffer, data, len); - return 1; - default: - return 1; } - return 0; + return 1; } + uchar usbFunctionRead(uchar *data, uchar len){ - return 0; +#if ALLOW_SECRET_READ || 1 + uchar r; + uint8_t s_length_B; + switch(current_command){ + case CUSTOM_RQ_GET_SECRET: + eeprom_busy_wait(); + s_length_B = (eeprom_read_word(&secret_length_ee) + 7) / 8; + r = MIN(len, s_length_B - uni_buffer.w8[0]); + eeprom_busy_wait(); + eeprom_read_block(data, secret_ee + uni_buffer.w8[0], r); + uni_buffer.w8[0] += r; + return r; + } +#endif + return 0; } static void calibrateOscillator(void) @@ -440,8 +467,6 @@ int main(void) * additional hardware initialization. */ - DDRB &= ~_BV(BUTTON_PIN); /* make button pin input */ - PORTB |= _BV(BUTTON_PIN); /* turn on pull-up resistor */ counter_init(); usbInit(); usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */ @@ -452,14 +477,17 @@ int main(void) usbDeviceConnect(); sei(); + DDRB &= ~_BV(BUTTON_PIN); /* make button pin input */ + PORTB |= _BV(BUTTON_PIN); /* turn on pull-up resistor */ for(;;){ /* main event loop */ wdt_reset(); usbPoll(); - i = button_get_debounced(25); + i = button_get_debounced(DEBOUNCE_DELAY); if (i != -1) { if (last_stable_button_state == 0 && i == 1) { + token_generate(); key_state = STATE_SEND_KEY; } last_stable_button_state = i;