From 54b7f4205691ea5ea47253f4f371f071c3976e62 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Tue, 26 Feb 2019 01:19:35 -0500 Subject: [PATCH] passive operation works as is (refactor needed) --- .gitignore | 2 + fido2/u2f.c | 8 +- targets/stm32l432/src/ams.c | 6 +- targets/stm32l432/src/ams.h | 5 + targets/stm32l432/src/device.c | 14 +- targets/stm32l432/src/init.c | 196 +++++++++++++++++++++-- targets/stm32l432/src/nfc.c | 69 ++++---- targets/stm32l432/src/redirect.c | 5 +- targets/stm32l432/src/system_stm32l4xx.c | 2 +- 9 files changed, 250 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index 4e3c5c5..9613abd 100644 --- a/.gitignore +++ b/.gitignore @@ -80,3 +80,5 @@ env3/ .tags* targets/*/docs/ main + +builds/* diff --git a/fido2/u2f.c b/fido2/u2f.c index 5213ea8..1ae395d 100644 --- a/fido2/u2f.c +++ b/fido2/u2f.c @@ -160,7 +160,7 @@ void u2f_set_writeback_buffer(CTAP_RESPONSE * resp) _u2f_resp = resp; } -static void dump_signature_der(uint8_t * sig) +void dump_signature_der(uint8_t * sig) { uint8_t sigder[72]; int len; @@ -183,7 +183,7 @@ static void u2f_make_auth_tag(struct u2f_key_handle * kh, uint8_t * appid, uint8 memmove(tag, hashbuf, CREDENTIAL_TAG_SIZE); } -static int8_t u2f_new_keypair(struct u2f_key_handle * kh, uint8_t * appid, uint8_t * pubkey) +int8_t u2f_new_keypair(struct u2f_key_handle * kh, uint8_t * appid, uint8_t * pubkey) { ctap_generate_rng(kh->key, U2F_KEY_HANDLE_KEY_SIZE); u2f_make_auth_tag(kh, appid, kh->tag); @@ -286,6 +286,7 @@ static int16_t u2f_register(struct u2f_register_request * req, bool fromNFC) uint8_t i[] = {0x0,U2F_EC_FMT_UNCOMPRESSED}; struct u2f_key_handle key_handle; + static uint32_t count = 0; uint8_t pubkey[64]; uint8_t hash[32]; uint8_t * sig = (uint8_t*)req; @@ -300,11 +301,12 @@ static int16_t u2f_register(struct u2f_register_request * req, bool fromNFC) return U2F_SW_CONDITIONS_NOT_SATISFIED; } } - + uint32_t t1 = millis(); if ( u2f_new_keypair(&key_handle, req->app, pubkey) == -1) { return U2F_SW_INSUFFICIENT_MEMORY; } + printf1(TAG_NFC, "keygen time: %d ms\r\n", millis()-t1); crypto_sha256_init(); crypto_sha256_update(i,1); diff --git a/targets/stm32l432/src/ams.c b/targets/stm32l432/src/ams.c index d3ec927..629c68d 100644 --- a/targets/stm32l432/src/ams.c +++ b/targets/stm32l432/src/ams.c @@ -283,8 +283,8 @@ bool ams_init() delay(2); // Needs to be disabled for passive operation - // if (0) - if (1) + if (0) + // if (1) { // check connection uint8_t productType = ams_read_reg(AMS_REG_PRODUCT_TYPE); @@ -327,7 +327,7 @@ bool ams_init() ams_read_eeprom_block(AMS_CONFIG_BLOCK1_ADDR, block); printf1(TAG_NFC,"conf1: "); dump_hex1(TAG_NFC,block,4); - uint8_t ic_cfg1 = AMS_CFG1_OUTPUT_RESISTANCE_100 | AMS_CFG1_VOLTAGE_LEVEL_2V1; + uint8_t ic_cfg1 = AMS_CFG1_OUTPUT_RESISTANCE_100 | AMS_CFG1_VOLTAGE_LEVEL_2V0; uint8_t ic_cfg2 = AMS_CFG2_TUN_MOD; if (block[0] != ic_cfg1 || block[1] != ic_cfg2) diff --git a/targets/stm32l432/src/ams.h b/targets/stm32l432/src/ams.h index 1f2e3a5..d8e3cad 100644 --- a/targets/stm32l432/src/ams.h +++ b/targets/stm32l432/src/ams.h @@ -127,6 +127,11 @@ int ams_state_is_valid(uint8_t regval); #define AMS_CFG1_VOLTAGE_LEVEL_2V3 (0x04<<2) #define AMS_CFG1_VOLTAGE_LEVEL_2V4 (0x05<<2) #define AMS_CFG1_VOLTAGE_LEVEL_2V5 (0x06<<2) +#define AMS_CFG1_VOLTAGE_LEVEL_2V6 (0x07<<2) +#define AMS_CFG1_VOLTAGE_LEVEL_2V7 (0x08<<2) +#define AMS_CFG1_VOLTAGE_LEVEL_2V8 (0x09<<2) +#define AMS_CFG1_VOLTAGE_LEVEL_2V9 (0x0a<<2) +#define AMS_CFG1_VOLTAGE_LEVEL_3V0 (0x0b<<2) #define AMS_CFG1_OUTPUT_RESISTANCE_ZZ 0x00 #define AMS_CFG1_OUTPUT_RESISTANCE_100 0x01 diff --git a/targets/stm32l432/src/device.c b/targets/stm32l432/src/device.c index 7600f34..ae35a3d 100644 --- a/targets/stm32l432/src/device.c +++ b/targets/stm32l432/src/device.c @@ -25,7 +25,7 @@ #include "stm32l4xx_ll_gpio.h" #include "stm32l4xx_ll_tim.h" #include "stm32l4xx_ll_usart.h" - +#include "stm32l4xx_ll_pwr.h" #include "usbd_hid.h" #include APP_CONFIG @@ -122,17 +122,19 @@ void device_reboot() void device_init() { - // hw_init(LOW_FREQUENCY); - // isLowFreq = 1; + hw_init(LOW_FREQUENCY); + isLowFreq = 1; - hw_init(HIGH_FREQUENCY); - isLowFreq = 0; + printf1(TAG_NFC,"PWR->CR1: %04x\r\n", LL_PWR_GetRegulVoltageScaling()); + + // hw_init(HIGH_FREQUENCY); + // isLowFreq = 0; usbhid_init(); ctaphid_init(); - ctap_init( 1 ); + ctap_init( 0 ); #ifndef IS_BOOTLOADER #if BOOT_TO_DFU diff --git a/targets/stm32l432/src/init.c b/targets/stm32l432/src/init.c index cb4c4e6..c7dfcb5 100644 --- a/targets/stm32l432/src/init.c +++ b/targets/stm32l432/src/init.c @@ -48,7 +48,8 @@ #include "init.h" #include APP_CONFIG - +// KHz +#define CLOCK_RATE 24000 USBD_HandleTypeDef Solo_USBD_Device; @@ -56,7 +57,8 @@ static void LL_Init(void); #define Error_Handler() _Error_Handler(__FILE__,__LINE__) void _Error_Handler(char *file, int line); - +void SystemClock_Config_LF28(void); +void SystemClock_Config_LF48(void); void hw_init(int lowfreq) { @@ -69,7 +71,24 @@ void hw_init(int lowfreq) if (lowfreq) { + // Under voltage + LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE2); +#if CLOCK_RATE == 48000 + SystemClock_Config_LF48(); +#elif CLOCK_RATE == 32000 + SystemClock_Config_LF32(); +#elif CLOCK_RATE == 28000 + SystemClock_Config_LF28(); +#elif CLOCK_RATE == 24000 + SystemClock_Config_LF24(); +#elif CLOCK_RATE == 20000 + SystemClock_Config_LF20(); +#elif CLOCK_RATE == 16000 SystemClock_Config_LF16(); +#else +#error "Invalid clock rate selected" +#endif + LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE2); } else { @@ -515,6 +534,154 @@ void SystemClock_Config_LF32(void) } +// 28 MHz +void SystemClock_Config_LF28(void) +{ + SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_PWREN); + LL_FLASH_SetLatency(LL_FLASH_LATENCY_1); + + if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_1) + { + Error_Handler(); + } + LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1); + + LL_RCC_HSI_Enable(); + + /* Wait till HSI is ready */ + while(LL_RCC_HSI_IsReady() != 1) + { + + } + LL_RCC_HSI_SetCalibTrimming(16); + + LL_RCC_LSI_Enable(); + + /* Wait till LSI is ready */ + while(LL_RCC_LSI_IsReady() != 1) + { + + } + LL_RCC_MSI_Enable(); + + /* Wait till MSI is ready */ + while(LL_RCC_MSI_IsReady() != 1) + { + + } + LL_RCC_MSI_EnableRangeSelection(); + + LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_6); + + LL_RCC_MSI_SetCalibTrimming(0); + + LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, LL_RCC_PLLM_DIV_2, 28, LL_RCC_PLLR_DIV_8); + + LL_RCC_PLL_EnableDomain_SYS(); + + LL_RCC_PLL_Enable(); + + /* Wait till PLL is ready */ + while(LL_RCC_PLL_IsReady() != 1) + { + + } + LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL); + + /* Wait till System clock is ready */ + while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) + { + + } + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); + + LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); + + LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_8); + + LL_Init1msTick(28000000); + + LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK); + + LL_SetSystemCoreClock(28000000); + + LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2); + + LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_MSI); + + /* SysTick_IRQn interrupt configuration */ + NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); +} + +// 48 MHz +void SystemClock_Config_LF48(void) +{ + SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_PWREN); + + + LL_FLASH_SetLatency(LL_FLASH_LATENCY_2); + + if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2) + { + Error_Handler(); + } + LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1); + + LL_RCC_LSI_Enable(); + + /* Wait till LSI is ready */ + while(LL_RCC_LSI_IsReady() != 1) + { + + } + LL_RCC_MSI_Enable(); + + /* Wait till MSI is ready */ + while(LL_RCC_MSI_IsReady() != 1) + { + + } + LL_RCC_MSI_EnableRangeSelection(); + + LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_11); + + LL_RCC_MSI_SetCalibTrimming(0); + + LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_MSI); + + /* Wait till System clock is ready */ + while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_MSI) + { + + } + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); + + LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); + + LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_16); + + LL_Init1msTick(48000000); + + LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK); + + LL_SetSystemCoreClock(48000000); + + LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2); + + LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_MSI); + + /* SysTick_IRQn interrupt configuration */ + NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); + + +} + +// 20 MHz +void SystemClock_Config_LF20(void) +{ + SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_PWREN); +} + void init_usb() { // enable USB power @@ -666,17 +833,18 @@ void init_gpio(void) LL_GPIO_SetPinPull(SOLO_BUTTON_PORT,SOLO_BUTTON_PIN,LL_GPIO_PULL_UP); #ifdef SOLO_AMS_IRQ_PORT - LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC); - /**/ - LL_GPIO_InitTypeDef GPIO_InitStruct; - GPIO_InitStruct.Pin = SOLO_AMS_IRQ_PIN; - GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = LL_GPIO_PULL_NO; - LL_GPIO_Init(SOLO_AMS_IRQ_PORT, &GPIO_InitStruct); - - - LL_GPIO_SetPinMode(SOLO_AMS_IRQ_PORT,SOLO_AMS_IRQ_PIN,LL_GPIO_MODE_INPUT); - LL_GPIO_SetPinPull(SOLO_AMS_IRQ_PORT,SOLO_AMS_IRQ_PIN,LL_GPIO_PULL_UP); +// SAVE POWER + // LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC); + // /**/ + // LL_GPIO_InitTypeDef GPIO_InitStruct; + // GPIO_InitStruct.Pin = SOLO_AMS_IRQ_PIN; + // GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT; + // GPIO_InitStruct.Pull = LL_GPIO_PULL_NO; + // LL_GPIO_Init(SOLO_AMS_IRQ_PORT, &GPIO_InitStruct); + // + // + // LL_GPIO_SetPinMode(SOLO_AMS_IRQ_PORT,SOLO_AMS_IRQ_PIN,LL_GPIO_MODE_INPUT); + // LL_GPIO_SetPinPull(SOLO_AMS_IRQ_PORT,SOLO_AMS_IRQ_PIN,LL_GPIO_PULL_UP); #endif } @@ -694,7 +862,7 @@ void init_millisecond_timer(int lf) if (!lf) TIM_InitStruct.Prescaler = 48000; else - TIM_InitStruct.Prescaler = 24000; + TIM_InitStruct.Prescaler = CLOCK_RATE; TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; TIM_InitStruct.Autoreload = 90; diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 1ef554c..2eb76d5 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -8,6 +8,7 @@ #include "util.h" #include "device.h" #include "u2f.h" +#include "crypto.h" #include "ctap_errors.h" @@ -102,7 +103,7 @@ bool ams_receive_with_timeout(uint32_t timeout_ms, uint8_t * data, int maxlen, i { uint8_t len = buffer_status2 & AMS_BUF_LEN_MASK; ams_read_buffer(buf, len); - printf1(TAG_NFC_APDU, ">> "); + printf1(TAG_NFC_APDU, ">> "); dump_hex1(TAG_NFC_APDU, buf, len); *dlen = MIN(32, MIN(maxlen, len)); @@ -128,7 +129,7 @@ void nfc_write_frame(uint8_t * data, uint8_t len) ams_write_buffer(data,len); ams_write_command(AMS_CMD_TRANSMIT_BUFFER); - printf1(TAG_NFC_APDU, "<< "); + printf1(TAG_NFC_APDU, "<< "); dump_hex1(TAG_NFC_APDU, data, len); } @@ -156,7 +157,7 @@ bool nfc_write_response(uint8_t req0, uint16_t resp) return nfc_write_response_ex(req0, NULL, 0, resp); } -void nfc_write_response_chaining(uint8_t req0, uint8_t * data, int len) +void nfc_write_response_chaining(uint8_t req0, uint8_t * data, int len, int keepgoing) { uint8_t res[32 + 2]; int sendlen = 0; @@ -177,7 +178,7 @@ void nfc_write_response_chaining(uint8_t req0, uint8_t * data, int len) memcpy(&res[1], &data[sendlen], vlen); // if not a last block - if (vlen + sendlen < len) + if ((vlen + sendlen < len) || keepgoing) { res[0] |= 0x10; } @@ -187,11 +188,11 @@ void nfc_write_response_chaining(uint8_t req0, uint8_t * data, int len) sendlen += vlen; // wait for transmit (32 bytes aprox 2,5ms) - if (!ams_wait_for_tx(10)) - { - printf1(TAG_NFC, "TX timeout. slen: %d \r\n", sendlen); - break; - } + // if (!ams_wait_for_tx(10)) + // { + // printf1(TAG_NFC, "TX timeout. slen: %d \r\n", sendlen); + // break; + // } // if needs to receive R block (not a last block) if (res[0] & 0x10) @@ -200,13 +201,14 @@ void nfc_write_response_chaining(uint8_t req0, uint8_t * data, int len) int reclen; if (!ams_receive_with_timeout(100, recbuf, sizeof(recbuf), &reclen)) { - printf1(TAG_NFC, "R block RX timeout.\r\n"); + printf1(TAG_NFC, "R block RX timeout %d/%d.\r\n",sendlen,len); break; } if (reclen != 1) { - printf1(TAG_NFC, "R block length error. len: %d \r\n", reclen); + printf1(TAG_NFC, "R block length error. len: %d. %d/%d \r\n", reclen,sendlen,len); + dump_hex1(TAG_NFC, recbuf, reclen); break; } @@ -243,14 +245,14 @@ bool WTX_on(int WTX_time) { WTX_clear(); WTX_timer = millis(); - + return true; } bool WTX_off() { WTX_timer = 0; - + // read data if we sent WTX if (WTX_sent) { @@ -266,7 +268,7 @@ bool WTX_off() printf1(TAG_NFC, "WTX-off fail\n"); return false; } - + WTX_clear(); return true; } @@ -276,7 +278,7 @@ void WTX_timer_exec() // condition: (timer on) or (not expired[300ms]) if ((WTX_timer <= 0) || WTX_timer + 300 > millis()) return; - + WTX_process(10); WTX_timer = millis(); } @@ -396,9 +398,10 @@ void nfc_process_iblock(uint8_t * buf, int len) int selected; uint8_t res[32]; uint32_t t1; - + int l1; CTAP_RESPONSE ctap_resp; int status; + struct u2f_register_request * u2freq = (struct u2f_register_request *)payload; printf1(TAG_NFC,"Iblock: "); dump_hex1(TAG_NFC, buf, len); @@ -477,14 +480,24 @@ void nfc_process_iblock(uint8_t * buf, int len) } t1 = millis(); - WTX_on(WTX_TIME_DEFAULT); - u2f_request_nfc(&buf[1], len, &ctap_resp); - if (!WTX_off()) - return; - printf1(TAG_NFC, "U2F resp len: %d\r\n", ctap_resp.length); - printf1(TAG_NFC,"U2F Register processing %d (took %d)\r\n", millis(), millis() - t1); - nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length); + + // WTX_on(WTX_TIME_DEFAULT); + // SystemClock_Config_LF32(); + // delay(300); + u2f_request_nfc(&buf[1], len, &ctap_resp); + // SystemClock_Config_LF16(); + // if (!WTX_off()) + // return; + + printf1(TAG_NFC,"U2F Register P2 took %d\r\n", millis() - t1); + nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length, 0 ); + + // printf1(TAG_NFC, "U2F resp len: %d\r\n", ctap_resp.length); + + + + printf1(TAG_NFC,"U2F Register answered %d (took %d)\r\n", millis(), millis() - t1); break; @@ -505,14 +518,14 @@ void nfc_process_iblock(uint8_t * buf, int len) } t1 = millis(); - WTX_on(WTX_TIME_DEFAULT); + // WTX_on(WTX_TIME_DEFAULT); u2f_request_nfc(&buf[1], len, &ctap_resp); - if (!WTX_off()) - return; + // if (!WTX_off()) + // return; printf1(TAG_NFC, "U2F resp len: %d\r\n", ctap_resp.length); printf1(TAG_NFC,"U2F Authenticate processing %d (took %d)\r\n", millis(), millis() - t1); - nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length); + nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length, 0); printf1(TAG_NFC,"U2F Authenticate answered %d (took %d)\r\n", millis(), millis() - t1); break; @@ -545,7 +558,7 @@ void nfc_process_iblock(uint8_t * buf, int len) ctap_resp.data[ctap_resp.length - 1] = SW_SUCCESS & 0xff; printf1(TAG_NFC,"CTAP processing %d (took %d)\r\n", millis(), millis() - t1); - nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length); + nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length, 0); printf1(TAG_NFC,"CTAP answered %d (took %d)\r\n", millis(), millis() - t1); break; diff --git a/targets/stm32l432/src/redirect.c b/targets/stm32l432/src/redirect.c index ff03c1e..bc06282 100644 --- a/targets/stm32l432/src/redirect.c +++ b/targets/stm32l432/src/redirect.c @@ -42,6 +42,7 @@ void _putchar(char c) int _write (int fd, const void *buf, long int len) { uint8_t * data = (uint8_t *) buf; +#if DEBUG_LEVEL>1 static uint8_t logbuf[1000] = {0}; static int logbuflen = 0; if (logbuflen + len > sizeof(logbuf)) { @@ -56,11 +57,11 @@ int _write (int fd, const void *buf, long int len) uint8_t res = CDC_Transmit_FS(logbuf, logbuflen); if (res == USBD_OK) logbuflen = 0; - +#endif // Send out UART serial while(len--) { - // _putchar(*data++); + _putchar(*data++); } return 0; } diff --git a/targets/stm32l432/src/system_stm32l4xx.c b/targets/stm32l432/src/system_stm32l4xx.c index 7332d63..262e883 100644 --- a/targets/stm32l432/src/system_stm32l4xx.c +++ b/targets/stm32l432/src/system_stm32l4xx.c @@ -220,7 +220,7 @@ void SystemInit(void) /* Disable all interrupts */ RCC->CIER = 0x00000000U; - SystemClock_Config_LF24(); + SystemClock_Config_LF16(); }