efm8 bridge polled mode is much faster

This commit is contained in:
Conor Patrick
2018-07-07 17:11:13 -04:00
parent a18aa99220
commit fbbf430152
10 changed files with 291 additions and 249 deletions

File diff suppressed because one or more lines are too long

View File

@@ -49,6 +49,11 @@
<property object="PC9" propertyId="ports.settings.pinmode" value="Input pull"/>
<property object="PC9" propertyId="ports.settings.pulldirection" value="Pullup"/>
<property object="PC9" propertyId="ports.settings.pullup" value="Enabled"/>
<property object="PD10" propertyId="ports.settings.dout" value="1"/>
<property object="PD10" propertyId="ports.settings.filter" value="Enabled"/>
<property object="PD10" propertyId="ports.settings.pinmode" value="Push-pull"/>
<property object="PD10" propertyId="ports.settings.pulldirection" value="Pullup"/>
<property object="PD10" propertyId="ports.settings.pullup" value="Enabled"/>
<property object="PF4" propertyId="ports.settings.pinmode" value="Push-pull"/>
<property object="PF5" propertyId="ports.settings.pinmode" value="Push-pull"/>
<property object="PORTIO" propertyId="portio.usart0.enable.cts" value="Enabled"/>
@@ -69,7 +74,7 @@
<property object="USART1" propertyId="ABPeripheral.included" value="true"/>
<property object="USART1" propertyId="usart.mode.usartmode" value="Synchronous Mode (SPI / I2S)"/>
<property object="USART1" propertyId="usart.outputsettings.clockselect" value="Disabled"/>
<property object="USART1" propertyId="usart.synchronoussettings.baudrate" value="100000"/>
<property object="USART1" propertyId="usart.synchronoussettings.baudrate" value="140000"/>
</mode>
<modeTransition>
<property object="RESET &#x2192; DefaultMode" propertyId="modeTransition.source" value="RESET"/>

View File

@@ -334,7 +334,7 @@ extern void USART1_enter_DefaultMode_from_RESET(void) {
USART_InitSync_TypeDef initsync = USART_INITSYNC_DEFAULT;
initsync.enable = usartDisable;
initsync.baudrate = 100000;
initsync.baudrate = 140000;
initsync.databits = usartDatabits8;
initsync.master = 1;
initsync.msbf = 1;
@@ -621,6 +621,9 @@ extern void PORTIO_enter_DefaultMode_from_RESET(void) {
// [Port C Configuration]$
// $[Port D Configuration]
/* Pin PD10 is configured to Push-pull */
GPIO_PinModeSet(gpioPortD, 10, gpioModePushPull, 1);
// [Port D Configuration]$
// $[Port E Configuration]

View File

@@ -26,7 +26,6 @@ const uint8_t attestation_key[];
const uint16_t attestation_key_size;
static SHA256_CTX sha256_ctx;
mbedtls_sha256_context embed_sha256_ctx;
@@ -204,29 +203,29 @@ void crypto_ecc256_sign(uint8_t * data, int len, uint8_t * sig)
// CMU->HFBUSCLKEN0 &= ~CMU_HFBUSCLKEN0_CRYPTO;
// CRYPTO_DISABLE;
// CRYPTO_ENABLE;
mbedtls_ecp_group_init( &grp );
mbedtls_mpi_init( &d );
mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1);
mbedtls_mpi_read_binary(&d, _signing_key, 32);
// mbedtls_ecp_group_init( &grp );
// mbedtls_mpi_init( &d );
// mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1);
// mbedtls_mpi_read_binary(&d, _signing_key, 32);
//
// mbedtls_mpi r,s;
// mbedtls_mpi_init(&r);
// mbedtls_mpi_init(&s);
//
// printf("signing..\n");
// dump_hex(data,len);
// mbedtls_ecdsa_sign_det( &grp, &r, &s, &d,
// data, 32, MBEDTLS_MD_SHA256 );// Issue: this will freeze on 13th iteration..
// printf("signed\n");
//
// mbedtls_mpi_write_binary(&r,sig,32);
// mbedtls_mpi_write_binary(&s,sig+32,32);
mbedtls_mpi r,s;
mbedtls_mpi_init(&r);
mbedtls_mpi_init(&s);
printf("signing..\n");
dump_hex(data,len);
mbedtls_ecdsa_sign_det( &grp, &r, &s, &d,
data, 32, MBEDTLS_MD_SHA256 );// Issue: this will freeze on 13th iteration..
printf("signed\n");
mbedtls_mpi_write_binary(&r,sig,32);
mbedtls_mpi_write_binary(&s,sig+32,32);
// if ( uECC_sign(_signing_key, data, len, sig, _es256_curve) == 0)
// {
// printf("error, uECC failed\n");
// exit(1);
// }
if ( uECC_sign(_signing_key, data, len, sig, _es256_curve) == 0)
{
printf("error, uECC failed\n");
exit(1);
}
}
@@ -368,20 +367,20 @@ void generate_private_key(uint8_t * data, int len, uint8_t * data2, int len2, ui
crypto_sha256_update(master_secret, 32);
crypto_sha256_hmac_final(CRYPTO_MASTER_KEY, 0, privkey);
mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
mbedtls_mpi d; /*!< our secret value */
mbedtls_ecp_point Q;
mbedtls_ecp_group_init( &grp );
mbedtls_mpi_init( &d );
mbedtls_ecp_point_init(&Q);
mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1);
// mbedtls_mpi_read_binary(&d, _signing_key, 32);
hmac_vector_func(NULL, NULL, 0);
mbedtls_ecp_gen_privkey(&grp, &grp.G, &d, &Q, hmac_vector_func, privkey);
mbedtls_mpi_write_binary(&d,privkey,32);
// mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
// mbedtls_mpi d; /*!< our secret value */
// mbedtls_ecp_point Q;
//
// mbedtls_ecp_group_init( &grp );
// mbedtls_mpi_init( &d );
// mbedtls_ecp_point_init(&Q);
//
// mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1);
//
//// mbedtls_mpi_read_binary(&d, _signing_key, 32);
// hmac_vector_func(NULL, NULL, 0);
// mbedtls_ecp_gen_privkey(&grp, &grp.G, &d, &Q, hmac_vector_func, privkey);
// mbedtls_mpi_write_binary(&d,privkey,32);
}
@@ -400,41 +399,41 @@ void crypto_ecc256_derive_public_key(uint8_t * data, int len, uint8_t * x, uint8
crypto_sha256_update(master_secret, 32);
crypto_sha256_hmac_final(CRYPTO_MASTER_KEY, 0, privkey);
mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
mbedtls_mpi d; /*!< our secret value */
mbedtls_ecp_point Q;
// mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
// mbedtls_mpi d; /*!< our secret value */
// mbedtls_ecp_point Q;
//
// mbedtls_ecp_group_init( &grp );
// mbedtls_mpi_init( &d );
// mbedtls_ecp_point_init(&Q);
//
// mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1);
//
//// mbedtls_mpi_read_binary(&d, _signing_key, 32);
// hmac_vector_func(NULL, NULL, 0);
// ret= mbedtls_ecp_gen_privkey(&grp, &grp.G, &d, &Q, hmac_vector_func, privkey);
// if (ret != 0)
// {
// printf("error with priv key -0x04%x\n", -ret);
// }
//// mbedtls_mpi_write_binary(&d,privkey,32);
//
// memset(pubkey,0,sizeof(pubkey));
//
// ret = mbedtls_ecp_derive_pubkey( &grp, &grp.G,
// &d, &Q, hmac_vector_func, privkey);
//
// if (ret != 0)
// {
// printf("error with public key\n");
// }
//
// mbedtls_mpi_write_binary(&Q.X,x,32);
// mbedtls_mpi_write_binary(&Q.Y,y,32);
mbedtls_ecp_group_init( &grp );
mbedtls_mpi_init( &d );
mbedtls_ecp_point_init(&Q);
mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1);
// mbedtls_mpi_read_binary(&d, _signing_key, 32);
hmac_vector_func(NULL, NULL, 0);
ret= mbedtls_ecp_gen_privkey(&grp, &grp.G, &d, &Q, hmac_vector_func, privkey);
if (ret != 0)
{
printf("error with priv key -0x04%x\n", -ret);
}
// mbedtls_mpi_write_binary(&d,privkey,32);
memset(pubkey,0,sizeof(pubkey));
ret = mbedtls_ecp_derive_pubkey( &grp, &grp.G,
&d, &Q, hmac_vector_func, privkey);
if (ret != 0)
{
printf("error with public key\n");
}
mbedtls_mpi_write_binary(&Q.X,x,32);
mbedtls_mpi_write_binary(&Q.Y,y,32);
// uECC_compute_public_key(privkey, pubkey, _es256_curve);
// memmove(x,pubkey,32);
// memmove(y,pubkey+32,32);
uECC_compute_public_key(privkey, pubkey, _es256_curve);
memmove(x,pubkey,32);
memmove(y,pubkey+32,32);
}
void crypto_ecc256_load_key(uint8_t * data, int len, uint8_t * data2, int len2)

View File

@@ -17,6 +17,10 @@
#include "ctaphid.h"
#include "util.h"
#define MSG_AVAIL_PIN gpioPortC,9
#define RDY_PIN gpioPortC,10
#define RW_PIN gpioPortD,11
// Generate @num bytes of random numbers to @dest
// return 1 if success, error otherwise
int ctap_generate_rng(uint8_t * dst, size_t num)
@@ -88,20 +92,44 @@ void usbhid_init()
static int msgs_to_recv = 0;
static void wait_for_efm8_ready()
{
// Wait for efm8 to be ready
while (GPIO_PinInGet(RDY_PIN) == 0)
;
}
static void wait_for_efm8_busy()
{
// Wait for efm8 to be ready
while (GPIO_PinInGet(RDY_PIN) != 0)
;
}
int usbhid_recv(uint8_t * msg)
{
int i;
if (msgs_to_recv)
if (GPIO_PinInGet(MSG_AVAIL_PIN) == 0)
{
GPIO_PinOutClear(gpioPortC,10);
GPIO_PinOutClear(RW_PIN); // Drive low to indicate READ
wait_for_efm8_ready();
for (i = 0; i < 64; i++)
{
msg[i] = USART_SpiTransfer(USART1, 0);
msg[i] = USART_SpiTransfer(USART1, 'A');
// delay(1);
}
msgs_to_recv--;
printf(">> ");
dump_hex(msg,64);
GPIO_PinOutSet(RW_PIN);
wait_for_efm8_busy();
// msgs_to_recv--;
// printf(">> ");
// dump_hex(msg,64);
return 64;
}
@@ -111,24 +139,17 @@ int usbhid_recv(uint8_t * msg)
void usbhid_send(uint8_t * msg)
{
int i;
uint64_t t1 = millis();
// uint32_t t1 = millis();
USART_SpiTransfer(USART1, *msg++); // Send 1 byte
wait_for_efm8_ready();
GPIO_PinModeSet(gpioPortC, 10, gpioModeInput, 0);
// Wait for efm8 to be ready
while (GPIO_PinInGet(gpioPortC, 10) == 0)
;
GPIO_PinModeSet(gpioPortC, 10, gpioModePushPull, 0);
uint64_t t2 = millis();
// printf("wait time: %ul\n", (uint32_t)(t2-t1));
GPIO_PinOutSet(gpioPortC,10);
for (i = 0; i < HID_MESSAGE_SIZE; i++)
for (i = 1; i < HID_MESSAGE_SIZE; i++)
{
USART_SpiTransfer(USART1, *msg++);
}
GPIO_PinOutClear(gpioPortC,10);
wait_for_efm8_busy();
// uint32_t t2 = millis();
// printf("wait time: %u\n", (uint32_t)(t2-t1));
}
@@ -181,12 +202,18 @@ void device_init(void)
gpioModePushPull,
1);
// SPI R/W indicator
GPIO_PinModeSet(gpioPortC, 10, gpioModePushPull, 0);
// EFM8 RDY/BUSY
GPIO_PinModeSet(RDY_PIN, gpioModeInput, 0);
// EFM8 MSG Available
GPIO_PinModeSet(MSG_AVAIL_PIN, gpioModeInput, 0);
// SPI R/w Indicator
GPIO_PinModeSet(RW_PIN, gpioModePushPull, 1);
// USB message rdy ext int
GPIO_ExtIntConfig(gpioPortC, 9, 9, 1, 0,1);
NVIC_EnableIRQ(GPIO_ODD_IRQn);
// GPIO_ExtIntConfig(gpioPortC, 9, 9, 1, 0,1);
// NVIC_EnableIRQ(GPIO_ODD_IRQn);
printing_init();