fix compile warnings, add flash locking

This commit is contained in:
Conor Patrick
2018-10-28 20:09:12 -04:00
parent 707a930d33
commit 5993aa792a
14 changed files with 107 additions and 69 deletions

View File

@@ -4,15 +4,10 @@
#define DEBUG_UART USART1
#define DEBUG_LEVEL 1
#define DEBUG_LEVEL 1
#define NON_BLOCK_PRINTING 0
//#define PRINTING_USE_VCOM
//#define USING_DEV_BOARD
//#define ENABLE_U2F_EXTENSIONS
@@ -35,6 +30,7 @@ void hw_init(void);
#define SOLO_BUTTON_PORT GPIOA
#define SOLO_BUTTON_PIN LL_GPIO_PIN_0
#define SKIP_BUTTON_CHECK 1
#define SKIP_BUTTON_CHECK_WITH_DELAY 1
#define SKIP_BUTTON_CHECK_FAST 0
#endif

View File

@@ -19,6 +19,7 @@
#include "ctap.h"
#include "device.h"
#include "app.h"
#include "log.h"
typedef enum
@@ -39,14 +40,6 @@ typedef enum
} mbedtls_ecp_group_id;
const uint8_t attestation_cert_der[];
const uint16_t attestation_cert_der_size;
const uint8_t attestation_key[];
const uint16_t attestation_key_size;
static SHA256_CTX sha256_ctx;
static const struct uECC_Curve_t * _es256_curve = NULL;
static const uint8_t * _signing_key = NULL;
@@ -105,10 +98,15 @@ void crypto_sha256_hmac_init(uint8_t * key, uint32_t klen, uint8_t * hmac)
key = master_secret;
klen = sizeof(master_secret)/2;
}
else if (key == CRYPTO_TRANSPORT_KEY)
{
key = transport_secret;
klen = 32;
}
if(klen > 64)
{
printf("Error, key size must be <= 64\n");
printf2(TAG_ERR, "Error, key size must be <= 64\n");
exit(1);
}
@@ -138,7 +136,7 @@ void crypto_sha256_hmac_final(uint8_t * key, uint32_t klen, uint8_t * hmac)
if(klen > 64)
{
printf("Error, key size must be <= 64\n");
printf2(TAG_ERR, "Error, key size must be <= 64\n");
exit(1);
}
memmove(buf, key, klen);
@@ -172,7 +170,7 @@ void crypto_ecc256_sign(uint8_t * data, int len, uint8_t * sig)
{
if ( uECC_sign(_signing_key, data, len, sig, _es256_curve) == 0)
{
printf("error, uECC failed\n");
printf2(TAG_ERR, "error, uECC failed\n");
exit(1);
}
}
@@ -209,19 +207,19 @@ void crypto_ecdsa_sign(uint8_t * data, int len, uint8_t * sig, int MBEDTLS_ECP_I
if (_key_len != 32) goto fail;
break;
default:
printf("error, invalid ECDSA alg specifier\n");
printf2(TAG_ERR, "error, invalid ECDSA alg specifier\n");
exit(1);
}
if ( uECC_sign(_signing_key, data, len, sig, curve) == 0)
{
printf("error, uECC failed\n");
printf2(TAG_ERR, "error, uECC failed\n");
exit(1);
}
return;
fail:
printf("error, invalid key length\n");
printf2(TAG_ERR, "error, invalid key length\n");
exit(1);
}
@@ -261,7 +259,7 @@ void crypto_ecc256_make_key_pair(uint8_t * pubkey, uint8_t * privkey)
{
if (uECC_make_key(pubkey, privkey, _es256_curve) != 1)
{
printf("Error, uECC_make_key failed\n");
printf2(TAG_ERR, "Error, uECC_make_key failed\n");
exit(1);
}
}
@@ -270,7 +268,7 @@ void crypto_ecc256_shared_secret(const uint8_t * pubkey, const uint8_t * privkey
{
if (uECC_shared_secret(pubkey, privkey, shared_secret, _es256_curve) != 1)
{
printf("Error, uECC_shared_secret failed\n");
printf2(TAG_ERR, "Error, uECC_shared_secret failed\n");
exit(1);
}

View File

@@ -95,6 +95,7 @@ void device_init()
hw_init();
LL_GPIO_SetPinMode(SOLO_BUTTON_PORT,SOLO_BUTTON_PIN,LL_GPIO_MODE_INPUT);
LL_GPIO_SetPinPull(SOLO_BUTTON_PORT,SOLO_BUTTON_PIN,LL_GPIO_PULL_UP);
flash_option_bytes_init(0);
printf1(TAG_GEN,"hello solo\r\n");
}
@@ -337,9 +338,8 @@ static int handle_packets()
int ctap_user_presence_test()
{
int oldstatus = __device_status;
int ret;
#if SKIP_BUTTON_CHECK
#if SKIP_BUTTON_CHECK_WITH_DELAY
int i=500;
while(i--)
{
@@ -348,17 +348,15 @@ int ctap_user_presence_test()
if (ret) return ret;
}
goto done;
#elif SKIP_BUTTON_CHECK_FAST
delay(2);
ret = handle_packets();
if (ret) return ret;
goto done;
#endif
uint32_t t1 = millis();
led_rgb(0xff3520);
#if USE_BUTTON_DELAY
delay(3000);
led_rgb(0x001040);
delay(50);
goto done;
#endif
while (IS_BUTTON_PRESSED())
{
if (t1 + 5000 < millis())

View File

@@ -4,7 +4,7 @@
#include "fifo.h"
FIFO_CREATE(debug,1024,1)
FIFO_CREATE(debug,4096,1)
FIFO_CREATE(hidmsg,100,64)

View File

@@ -6,6 +6,7 @@
#include "app.h"
#include "flash.h"
#include "log.h"
#include "device.h"
static void flash_unlock()
{
@@ -15,13 +16,57 @@ static void flash_unlock()
FLASH->KEYR = 0xCDEF89AB;
}
}
// Locks flash and turns off DFU
void flash_option_bytes_init(int boot_from_dfu)
{
#if DEBUG_LEVEL
uint32_t val = 0xfffff8aa;
#else
uint32_t val = 0xfffff8b9;
#endif
if (!boot_from_dfu)
{
val &= ~(1<<26); // nSWBOOT0 = 0 (boot from nBoot0)
}
val &= ~(1<<25); // SRAM2_RST = 1 (erase sram on reset)
val &= ~(1<<24); // SRAM2_PE = 1 (parity check en)
if (FLASH->OPTR == val)
{
return;
}
__disable_irq();
while (FLASH->SR & (1<<16))
;
flash_unlock();
if (FLASH->CR & (1<<30))
{
FLASH->OPTKEYR = 0x08192A3B;
FLASH->OPTKEYR = 0x4C5D6E7F;
}
FLASH->OPTR =val;
FLASH->CR |= (1<<17);
while (FLASH->SR & (1<<16))
;
flash_lock();
__enable_irq();
}
void flash_erase_page(uint8_t page)
{
__disable_irq();
flash_unlock();
// Wait if flash is busy
while (FLASH->SR & (1<<16))
;
flash_unlock();
FLASH->SR = FLASH->SR;
// enable flash erase and select page
@@ -72,6 +117,8 @@ void flash_write(uint32_t addr, uint8_t * data, size_t sz)
{
int i;
uint8_t buf[8];
while (FLASH->SR & (1<<16))
;
flash_unlock();
// dword align

View File

@@ -4,6 +4,7 @@
void flash_erase_page(uint8_t page);
void flash_write_dword(uint32_t addr, uint64_t data);
void flash_write(uint32_t addr, uint8_t * data, size_t sz);
void flash_option_bytes_init(int boot_from_dfu);
#define FLASH_PAGE_SIZE 2048

View File

@@ -6,6 +6,7 @@
#include "led.h"
#include "device.h"
#include "log.h"
void led_rgb(uint32_t hex)
{
@@ -46,9 +47,9 @@ void led_test_colors()
while(1)
{
printf("%d: %lu\r\n", j++, millis());
printf1(TAG_GREEN, "%d: %lu\r\n", j++, millis());
printf("white pulse\r\n");
printf1(TAG_GREEN,"white pulse\r\n");
time = millis();
while((millis() - time) < 5000)
{
@@ -56,7 +57,7 @@ void led_test_colors()
led_rgb(i | (i << 8) | (i << 16));
}
printf("blue pulse\r\n");
printf1(TAG_GREEN,"blue pulse\r\n");
time = millis();
while((millis() - time) < 5000)
{
@@ -64,7 +65,7 @@ void led_test_colors()
led_rgb(i);
}
printf("green pulse\r\n");
printf1(TAG_GREEN,"green pulse\r\n");
time = millis();
while((millis() - time) < 5000)
{
@@ -72,7 +73,7 @@ void led_test_colors()
led_rgb(i<<8);
}
printf("red pulse\r\n");
printf1(TAG_GREEN,"red pulse\r\n");
time = millis();
while((millis() - time) < 5000)
{
@@ -80,7 +81,7 @@ void led_test_colors()
led_rgb(i<<16);
}
printf("purple pulse\r\n");
printf1(TAG_GREEN,"purple pulse\r\n");
time = millis();
while((millis() - time) < 5000)
{
@@ -88,7 +89,7 @@ void led_test_colors()
led_rgb((i<<16) | i);
}
printf("orange pulse\r\n");
printf1(TAG_GREEN,"orange pulse\r\n");
time = millis();
while((millis() - time) < 5000)
{
@@ -96,7 +97,7 @@ void led_test_colors()
led_rgb((i<<16) | (i<<8));
}
printf("yellow pulse\r\n");
printf1(TAG_GREEN,"yellow pulse\r\n");
time = millis();
while((millis() - time) < 5000)
{
@@ -105,5 +106,3 @@ void led_test_colors()
}
}
}

View File

@@ -3,7 +3,7 @@
#include "app.h"
#include "fifo.h"
#if DEBUG_LEVEL>0
void _putchar(char c)
{
@@ -16,6 +16,8 @@ void _putchar(char c)
#endif
}
int _write (int fd, const void *buf, long int len)
{
uint8_t * data = (uint8_t *) buf;
@@ -26,3 +28,4 @@ int _write (int fd, const void *buf, long int len)
return 0;
}
#endif