major refactoring/simplifying

This commit is contained in:
Conor Patrick 2018-06-02 14:44:12 -04:00
parent f681d7a19a
commit 7a12eea133
26 changed files with 714 additions and 444 deletions

2
ctap.c
View File

@ -1068,7 +1068,7 @@ void ctap_response_init(CTAP_RESPONSE * resp)
} }
uint8_t ctap_handle_packet(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp) uint8_t ctap_request(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp)
{ {
CborEncoder encoder; CborEncoder encoder;
uint8_t status = 0; uint8_t status = 0;

2
ctap.h
View File

@ -235,7 +235,7 @@ typedef struct
void ctap_response_init(CTAP_RESPONSE * resp); void ctap_response_init(CTAP_RESPONSE * resp);
uint8_t ctap_handle_packet(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp); uint8_t ctap_request(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp);
// Encodes R,S signature to 2 der sequence of two integers. Sigder must be at least 72 bytes. // Encodes R,S signature to 2 der sequence of two integers. Sigder must be at least 72 bytes.
// @return length of der signature // @return length of der signature

View File

@ -1,69 +0,0 @@
/*
* Device specific functionality defined here
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cbor.h"
#include "util.h"
#include "usbhid.h"
#include "log.h"
void ctaphid_write_block(uint8_t * data)
{
printf("<< "); dump_hex(data, 64);
usbhid_send(data);
}
int ctap_user_presence_test()
{
return 1;
}
int ctap_user_verification(uint8_t arg)
{
return 1;
}
uint32_t ctap_atomic_count(int sel)
{
static uint32_t counter1 = 25;
static uint32_t counter2 = 25;
/*return 713;*/
if (sel == 0)
{
printf1(TAG_RED,"counter1: %d\n", counter1);
return counter1++;
}
else
{
return counter2++;
}
}
int ctap_generate_rng(uint8_t * dst, size_t num)
{
FILE * urand = fopen("/dev/urandom","r");
if (urand == NULL)
{
perror("fopen");
exit(1);
}
fread(dst, 1, num, urand);
fclose(urand);
/*memset(dst,0xaa,num);*/
return 1;
}

View File

@ -1,8 +1,8 @@
#include <arpa/inet.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "device.h"
#include "ctaphid.h" #include "ctaphid.h"
#include "ctap.h" #include "ctap.h"
#include "u2f.h" #include "u2f.h"
@ -159,11 +159,6 @@ static int is_cont_pkt(CTAPHID_PACKET * pkt)
} }
static int is_timed_out()
{
return (millis() - active_cid_timestamp > 500);
}
static int buffer_packet(CTAPHID_PACKET * pkt) static int buffer_packet(CTAPHID_PACKET * pkt)
{ {
if (pkt->pkt.init.cmd & TYPE_INIT) if (pkt->pkt.init.cmd & TYPE_INIT)
@ -301,7 +296,6 @@ static void ctaphid_write(CTAPHID_WRITE_BUFFER * wb, void * _data, int len)
static void ctaphid_send_error(uint32_t cid, uint8_t error) static void ctaphid_send_error(uint32_t cid, uint8_t error)
{ {
uint8_t buf[HID_MESSAGE_SIZE];
CTAPHID_WRITE_BUFFER wb; CTAPHID_WRITE_BUFFER wb;
ctaphid_write_buffer_init(&wb); ctaphid_write_buffer_init(&wb);
@ -335,7 +329,7 @@ static void send_init_response(uint32_t oldcid, uint32_t newcid, uint8_t * nonce
} }
void u2f_hid_check_timeouts() void ctaphid_check_timeouts()
{ {
uint8_t i; uint8_t i;
for(i = 0; i < CID_MAX; i++) for(i = 0; i < CID_MAX; i++)
@ -412,6 +406,8 @@ void ctaphid_handle_packet(uint8_t * pkt_raw)
return; return;
} }
send_init_response(oldcid, newcid, pkt->pkt.init.payload); send_init_response(oldcid, newcid, pkt->pkt.init.payload);
cid_del(newcid);
return; return;
} }
else else
@ -538,7 +534,7 @@ void ctaphid_handle_packet(uint8_t * pkt_raw)
} }
ctap_response_init(&ctap_resp); ctap_response_init(&ctap_resp);
status = ctap_handle_packet(ctap_buffer, buffer_len(), &ctap_resp); status = ctap_request(ctap_buffer, buffer_len(), &ctap_resp);
ctaphid_write_buffer_init(&wb); ctaphid_write_buffer_init(&wb);
wb.cid = active_cid; wb.cid = active_cid;

View File

@ -1,7 +1,7 @@
#ifndef _CTAPHID_H_H #ifndef _CTAPHID_H_H
#define _CTAPHID_H_H #define _CTAPHID_H_H
#include "usbhid.h" #include "device.h"
#include "ctap_errors.h" #include "ctap_errors.h"
#define TYPE_INIT 0x80 #define TYPE_INIT 0x80
@ -72,7 +72,7 @@ void ctaphid_init();
void ctaphid_handle_packet(uint8_t * pkt_raw); void ctaphid_handle_packet(uint8_t * pkt_raw);
void u2f_hid_check_timeouts(); void ctaphid_check_timeouts();
#define ctaphid_packet_len(pkt) ((uint16_t)((pkt)->pkt.init.bcnth << 8) | ((pkt)->pkt.init.bcntl)) #define ctaphid_packet_len(pkt) ((uint16_t)((pkt)->pkt.init.bcnth << 8) | ((pkt)->pkt.init.bcntl))

211
device.c Normal file
View File

@ -0,0 +1,211 @@
#include <sys/time.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include "device.h"
#include "cbor.h"
#include "util.h"
#include "log.h"
int udp_server()
{
int fd;
if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror( "socket failed" );
return 1;
}
struct timeval read_timeout;
read_timeout.tv_sec = 0;
read_timeout.tv_usec = 10;
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &read_timeout, sizeof(struct timeval)) != 0)
{
perror( "setsockopt" );
exit(1);
}
struct sockaddr_in serveraddr;
memset( &serveraddr, 0, sizeof(serveraddr) );
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons( 8111 );
serveraddr.sin_addr.s_addr = htonl( INADDR_ANY );
if ( bind(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) {
perror( "bind failed" );
exit(1);
}
return fd;
}
int udp_recv(int fd, uint8_t * buf, int size)
{
fd_set input;
FD_ZERO(&input);
FD_SET(fd, &input);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 100;
int n = select(fd + 1, &input, NULL, NULL, &timeout);
if (n == -1) {
perror("select\n");
exit(1);
} else if (n == 0)
return 0;
if (!FD_ISSET(fd, &input))
{
}
int length = recvfrom( fd, buf, size, 0, NULL, 0 );
if ( length < 0 ) {
perror( "recvfrom failed" );
exit(1);
}
return length;
}
void udp_send(int fd, uint8_t * buf, int size)
{
struct sockaddr_in serveraddr;
memset( &serveraddr, 0, sizeof(serveraddr) );
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons( 7112 );
serveraddr.sin_addr.s_addr = htonl( 0x7f000001 ); // (127.0.0.1)
if (sendto( fd, buf, size, 0, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) {
perror( "sendto failed" );
exit(1);
}
}
void udp_close(int fd)
{
close(fd);
}
uint64_t millis()
{
struct timeval te;
gettimeofday(&te, NULL); // get current time
uint64_t milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
return milliseconds;
}
static int serverfd = 0;
void usbhid_init()
{
// just bridge to UDP for now for pure software testing
serverfd = udp_server();
}
// Receive 64 byte USB HID message, don't block, return size of packet, return 0 if nothing
int usbhid_recv(uint8_t * msg)
{
int l = udp_recv(serverfd, msg, HID_MESSAGE_SIZE);
/*if (l && l != HID_MESSAGE_SIZE)*/
/*{*/
/*printf("Error, recv'd message of wrong size %d", l);*/
/*exit(1);*/
/*}*/
return l;
}
// Send 64 byte USB HID message
void usbhid_send(uint8_t * msg)
{
udp_send(serverfd, msg, HID_MESSAGE_SIZE);
}
void usbhid_close()
{
udp_close(serverfd);
}
void device_init()
{
usbhid_init();
}
void main_loop_delay()
{
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 1000*1000*25;
nanosleep(&ts,NULL);
}
void heartbeat()
{
}
void ctaphid_write_block(uint8_t * data)
{
printf("<< "); dump_hex(data, 64);
usbhid_send(data);
}
int ctap_user_presence_test()
{
return 1;
}
int ctap_user_verification(uint8_t arg)
{
return 1;
}
uint32_t ctap_atomic_count(int sel)
{
static uint32_t counter1 = 25;
static uint32_t counter2 = 25;
/*return 713;*/
if (sel == 0)
{
printf1(TAG_RED,"counter1: %d\n", counter1);
return counter1++;
}
else
{
return counter2++;
}
}
int ctap_generate_rng(uint8_t * dst, size_t num)
{
FILE * urand = fopen("/dev/urandom","r");
if (urand == NULL)
{
perror("fopen");
exit(1);
}
fread(dst, 1, num, urand);
fclose(urand);
/*memset(dst,0xaa,num);*/
return 1;
}

View File

@ -1,6 +1,9 @@
#ifndef _USBHID_H #ifndef _DEVICE_H
#define _USBHID_H #define _DEVICE_H
void device_init();
uint64_t millis();
// HID message size in bytes // HID message size in bytes
#define HID_MESSAGE_SIZE 64 #define HID_MESSAGE_SIZE 64
@ -13,4 +16,8 @@ void usbhid_send(uint8_t * msg);
void usbhid_close(); void usbhid_close();
void main_loop_delay();
void heartbeat();
#endif #endif

7
log.c
View File

@ -29,6 +29,12 @@ struct logtag tagtable[] = {
{TAG_RED,"\x1b[31mDEBUG\x1b[0m"}, {TAG_RED,"\x1b[31mDEBUG\x1b[0m"},
}; };
__attribute__((weak)) void set_logging_tag(uint32_t tag)
{
// nothing
}
void LOG(uint32_t tag, const char * filename, int num, const char * fmt, ...) void LOG(uint32_t tag, const char * filename, int num, const char * fmt, ...)
{ {
int i; int i;
@ -51,6 +57,7 @@ void LOG(uint32_t tag, const char * filename, int num, const char * fmt, ...)
printf("INVALID LOG TAG\n"); printf("INVALID LOG TAG\n");
exit(1); exit(1);
} }
set_logging_tag(tag);
#ifdef ENABLE_FILE_LOGGING #ifdef ENABLE_FILE_LOGGING
if (tag & TAG_FILENO) if (tag & TAG_FILENO)
{ {

1
log.h
View File

@ -7,6 +7,7 @@
void LOG(uint32_t tag, const char * filename, int num, const char * fmt, ...); void LOG(uint32_t tag, const char * filename, int num, const char * fmt, ...);
void LOG_HEX(uint32_t tag, uint8_t * data, int length); void LOG_HEX(uint32_t tag, uint8_t * data, int length);
void set_logging_mask(uint32_t mask); void set_logging_mask(uint32_t mask);
void set_logging_tag(uint32_t tag);
typedef enum typedef enum
{ {

40
main.c
View File

@ -3,25 +3,21 @@
#include <stdint.h> #include <stdint.h>
#include "cbor.h" #include "cbor.h"
#include "usbhid.h" #include "device.h"
#include "ctaphid.h" #include "ctaphid.h"
#include "util.h" #include "util.h"
#include "log.h" #include "log.h"
#include "ctap.h" #include "ctap.h"
static void check_ret(CborError ret)
{
if (ret != CborNoError)
{
printf("CborError: %d\n", ret);
exit(1);
}
}
#ifndef TEST #ifndef TEST
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
int count = 0, beat = 0;
uint64_t t1 = 0;
uint8_t hidmsg[64];
set_logging_mask( set_logging_mask(
TAG_MC | TAG_MC |
TAG_GA | TAG_GA |
@ -35,21 +31,28 @@ int main(int argc, char * argv[])
TAG_ERR TAG_ERR
); );
printf("init usbhid\n"); printf("init device\n");
usbhid_init(); device_init();
printf("init ctaphid\n"); printf("init ctaphid\n");
ctaphid_init(); ctaphid_init();
printf("init ctap\n"); printf("init ctap\n");
ctap_init(); ctap_init();
int count = 0;
uint8_t hidmsg[64];
memset(hidmsg,0,sizeof(hidmsg)); memset(hidmsg,0,sizeof(hidmsg));
printf("recv'ing hid msg \n"); printf("recv'ing hid msg \n");
while(1) while(1)
{ {
if (millis() - t1 > 100)
{
/*printf("heartbeat %ld\n", beat++);*/
heartbeat();
t1 = millis();
}
if (usbhid_recv(hidmsg) > 0) if (usbhid_recv(hidmsg) > 0)
{ {
printf("%d>> ",count++); dump_hex(hidmsg,sizeof(hidmsg)); printf("%d>> ",count++); dump_hex(hidmsg,sizeof(hidmsg));
@ -57,12 +60,17 @@ int main(int argc, char * argv[])
ctaphid_handle_packet(hidmsg); ctaphid_handle_packet(hidmsg);
memset(hidmsg, 0, sizeof(hidmsg)); memset(hidmsg, 0, sizeof(hidmsg));
} }
u2f_hid_check_timeouts(); else
{
main_loop_delay();
}
ctaphid_check_timeouts();
} }
// Should never get here
usbhid_close(); usbhid_close();
printf("done\n"); printf("done\n");
return 0; return 0;
} }
#endif #endif

View File

@ -11,8 +11,14 @@ $(OUTPUT_DIRECTORY)/nrf52840_xxaa.out: \
# Source files common to all targets # Source files common to all targets
SRC_FILES += \ SRC_FILES += \
$(SDK_ROOT)/modules/nrfx/mdk/gcc_startup_nrf52840.S \ $(SDK_ROOT)/modules/nrfx/mdk/gcc_startup_nrf52840.S \
$(PROJ_DIR)/main.c \ $(PROJ_DIR)/../main.c \
$(PROJ_DIR)/usb.c \ $(PROJ_DIR)/usb.c \
$(PROJ_DIR)/retarget.c \
$(PROJ_DIR)/device.c \
$(PROJ_DIR)/../util.c \
$(PROJ_DIR)/../log.c \
$(PROJ_DIR)/../stubs.c \
$(PROJ_DIR)/../ctaphid.c \
$(SDK_ROOT)/components/boards/boards.c \ $(SDK_ROOT)/components/boards/boards.c \
$(SDK_ROOT)/components/libraries/util/app_error.c \ $(SDK_ROOT)/components/libraries/util/app_error.c \
$(SDK_ROOT)/components/libraries/util/app_error_handler_gcc.c \ $(SDK_ROOT)/components/libraries/util/app_error_handler_gcc.c \
@ -82,6 +88,8 @@ INC_FOLDERS += \
$(SDK_ROOT)/modules/nrfx \ $(SDK_ROOT)/modules/nrfx \
$(SDK_ROOT)/external/segger_rtt \ $(SDK_ROOT)/external/segger_rtt \
$(PROJ_DIR) \ $(PROJ_DIR) \
$(PROJ_DIR)/.. \
$(PROJ_DIR)/../tinycbor/src \
$(SDK_ROOT)/components/libraries/util \ $(SDK_ROOT)/components/libraries/util \
$(SDK_ROOT)/integration/nrfx/legacy \ $(SDK_ROOT)/integration/nrfx/legacy \
$(SDK_ROOT)/modules/nrfx/drivers/include \ $(SDK_ROOT)/modules/nrfx/drivers/include \
@ -138,13 +146,16 @@ OPT = -O3 -g3
# C flags common to all targets # C flags common to all targets
CFLAGS += $(OPT) CFLAGS += $(OPT)
CFLAGS += -DBOARD_PCA10056 CFLAGS += -DBOARD_PCA10056
CFLAGS += -DNRF52
#CFLAGS += -DBSP_DEFINES_ONLY #CFLAGS += -DBSP_DEFINES_ONLY
CFLAGS += -DCONFIG_GPIO_AS_PINRESET CFLAGS += -DCONFIG_GPIO_AS_PINRESET
CFLAGS += -DFLOAT_ABI_HARD CFLAGS += -DFLOAT_ABI_HARD
CFLAGS += -DNRF52840_XXAA CFLAGS += -DNRF52840_XXAA
#CFLAGS += -DSTUB_CTAPHID
CFLAGS += -DSTUB_CTAP
CFLAGS += -mcpu=cortex-m4 CFLAGS += -mcpu=cortex-m4
CFLAGS += -mthumb -mabi=aapcs CFLAGS += -mthumb -mabi=aapcs
CFLAGS += -Wall CFLAGS += -Wall -Wno-format
CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16 CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
# keep every function in a separate section, this allows linker to discard unused ones # keep every function in a separate section, this allows linker to discard unused ones
CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing

View File

@ -37,6 +37,9 @@
#ifndef SEGGER_RTT_CONFIG_DEFAULT_MODE #ifndef SEGGER_RTT_CONFIG_DEFAULT_MODE
#define SEGGER_RTT_CONFIG_DEFAULT_MODE 2 #define SEGGER_RTT_CONFIG_DEFAULT_MODE 2
#define APP_FIFO_ENABLED 1
#endif #endif

259
nrf52840/device.c Normal file
View File

@ -0,0 +1,259 @@
/*
* Device specific functionality here
* */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "nrf.h"
#include "nrf_error.h"
#include "nrf_drv_power.h"
#include "nrf_strerror.h"
#include "nrf_drv_rtc.h"
#include "nrf_drv_clock.h"
#include "nrf_drv_usbd.h"
#include "nrf_gpio.h"
#include "bsp.h"
#include "app_error.h"
#include "app_fifo.h"
#include "util.h"
#include "usb.h"
#include "device.h"
#include "cbor.h"
#include "log.h"
extern int _SEGGER_TERM;
void set_output_terminal(uint32_t term)
{
_SEGGER_TERM = term;
}
void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
{
error_info_t * e = (error_info_t *)info;
printf("Error: %d: %s at %d:%s\n",e->err_code, nrf_strerror_get(e->err_code), e->line_num, e->p_file_name);
while(1)
;
}
void log_resetreason(void)
{
/* Reset reason */
uint32_t rr = nrf_power_resetreas_get();
printf("Reset reasons:\n");
if (0 == rr)
{
printf("- NONE\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_RESETPIN_MASK))
{
printf("- RESETPIN\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_DOG_MASK ))
{
printf("- DOG\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_SREQ_MASK ))
{
printf("- SREQ\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_LOCKUP_MASK ))
{
printf("- LOCKUP\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_OFF_MASK ))
{
printf("- OFF\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_LPCOMP_MASK ))
{
printf("- LPCOMP\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_DIF_MASK ))
{
printf("- DIF\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_NFC_MASK ))
{
printf("- NFC\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_VBUS_MASK ))
{
printf("- VBUS\n");
}
}
static const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC0. */
uint64_t millis()
{
return (uint64_t)nrf_drv_rtc_counter_get(&rtc);
}
static void rtc_config(void)
{
uint32_t err_code;
//Initialize RTC instance
nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
config.prescaler = 32;
err_code = nrf_drv_rtc_init(&rtc, &config, NULL);
APP_ERROR_CHECK(err_code);
//Power on RTC instance
nrf_drv_rtc_enable(&rtc);
}
static void init_power_clock(void)
{
ret_code_t ret;
/* Initializing power and clock */
ret = nrf_drv_clock_init();
APP_ERROR_CHECK(ret);
ret = nrf_drv_power_init(NULL);
APP_ERROR_CHECK(ret);
nrf_drv_clock_hfclk_request(NULL);
nrf_drv_clock_lfclk_request(NULL);
while (!(nrf_drv_clock_hfclk_is_running() &&
nrf_drv_clock_lfclk_is_running()))
{
/* Just waiting */
}
}
void device_init()
{
if ((nrf_power_resetreas_get() & NRF_POWER_RESETREAS_RESETPIN_MASK) == 0)
{
// Hard reset. this is for engineering A sample to work for USB ...
nrf_power_resetreas_clear(nrf_power_resetreas_get());
nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(0,31));
nrf_gpio_pin_clear(NRF_GPIO_PIN_MAP(0,31));
while (1)
;
}
nrf_power_resetreas_clear(nrf_power_resetreas_get());
set_output_terminal(0);
init_power_clock();
rtc_config();
usbhid_init();
srand(millis());
nrf_gpio_cfg_output(LED_1);
nrf_gpio_cfg_output(LED_2);
nrf_gpio_cfg_output(LED_3);
nrf_gpio_cfg_output(LED_4);
nrf_gpio_pin_toggle(LED_2);
nrf_gpio_pin_toggle(LED_3);
}
static uint8_t fifo_buf[1024];
app_fifo_t USBHID_RECV_FIFO;
void usbhid_init()
{
app_fifo_init(&USBHID_RECV_FIFO, fifo_buf, sizeof(fifo_buf));
usb_init();
}
// Receive 64 byte USB HID message, don't block, return size of packet, return 0 if nothing
int usbhid_recv(uint8_t * msg)
{
uint32_t size = 64;
app_fifo_read(&USBHID_RECV_FIFO, msg, &size);
return size;
}
// Send 64 byte USB HID message
void usbhid_send(uint8_t * msg)
{
static nrf_drv_usbd_transfer_t transfer;
transfer.p_data.tx = msg;
transfer.size = 64;
nrf_drv_usbd_ep_transfer(
NRF_DRV_USBD_EPIN1,
&transfer);
}
void usbhid_close()
{
}
void main_loop_delay()
{
// no delay on embedded system
}
void heartbeat()
{
nrf_gpio_pin_toggle(LED_1);
nrf_gpio_pin_toggle(LED_2);
nrf_gpio_pin_toggle(LED_3);
nrf_gpio_pin_toggle(LED_4);
}
void ctaphid_write_block(uint8_t * data)
{
printf("<< "); dump_hex(data, 64);
usbhid_send(data);
}
int ctap_user_presence_test()
{
return 1;
}
int ctap_user_verification(uint8_t arg)
{
return 1;
}
uint32_t ctap_atomic_count(int sel)
{
static uint32_t counter1 = 25;
static uint32_t counter2 = 25;
/*return 713;*/
if (sel == 0)
{
printf1(TAG_RED,"counter1: %d\n", counter1);
return counter1++;
}
else
{
return counter2++;
}
}
int ctap_generate_rng(uint8_t * dst, size_t num)
{
int i;
for (i = 0; i < num; i++)
{
*dst++ = (uint8_t)rand();
}
return 1;
}

View File

@ -50,140 +50,52 @@
#define DEBUG #define DEBUG
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "nrf.h" #include "nrf.h"
#include "nrf_gpio.h"
#include "nrf_delay.h" #include "nrf_delay.h"
#include "nrf_drv_clock.h"
#include "nrf_drv_power.h"
#include "nrf_strerror.h"
#include "nrf_drv_rtc.h"
#include "nrf_drv_clock.h"
#include "SEGGER_RTT.h"
#include "boards.h" #include "boards.h"
#include "usb.h" #include "device.h"
#include "time.h"
#include "util.h"
#define COMPARE_COUNTERTIME (3UL) /**< Get Compare event COMPARE_TIME seconds after the counter starts from 0. */
#ifdef BSP_LED_0
#define TICK_EVENT_OUTPUT BSP_LED_0 /**< Pin number for indicating tick event. */
#endif
#ifndef TICK_EVENT_OUTPUT
#error "Please indicate output pin"
#endif
#ifdef BSP_LED_1
#define COMPARE_EVENT_OUTPUT BSP_LED_1 /**< Pin number for indicating compare event. */
#endif
#ifndef COMPARE_EVENT_OUTPUT
#error "Please indicate output pin"
#endif
const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC0. */
#define printf(fmt,...) SEGGER_RTT_printf(0, fmt, ##__VA_ARGS__);
/*lint -save -e14 */
void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
{
error_info_t * e = (error_info_t *)info;
SEGGER_RTT_printf(0, "Error: %d: %s at %d:%s\n",e->err_code, nrf_strerror_get(e->err_code), e->line_num, e->p_file_name);
while(1)
;
}
void log_resetreason(void);
static void init_power_clock(void)
{
ret_code_t ret;
/* Initializing power and clock */
ret = nrf_drv_clock_init();
APP_ERROR_CHECK(ret);
ret = nrf_drv_power_init(NULL);
APP_ERROR_CHECK(ret);
nrf_drv_clock_hfclk_request(NULL);
nrf_drv_clock_lfclk_request(NULL);
while (!(nrf_drv_clock_hfclk_is_running() &&
nrf_drv_clock_lfclk_is_running()))
{
/* Just waiting */
}
}
static void rtc_config(void)
{
uint32_t err_code;
//Initialize RTC instance
nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
config.prescaler = 32;
err_code = nrf_drv_rtc_init(&rtc, &config, NULL);
APP_ERROR_CHECK(err_code);
//Enable tick event & interrupt
/*nrf_drv_rtc_tick_enable(&rtc,true);*/
/*//Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds*/
/*err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);*/
/*APP_ERROR_CHECK(err_code);*/
//Power on RTC instance
nrf_drv_rtc_enable(&rtc);
}
int main(void) int main(void)
{ {
/* Configure board. */ /* Configure board. */
/*bsp_board_init(BSP_INIT_LEDS);*/ uint8_t hidmsg[64];
/*lfclk_config();*/ uint32_t count = 0;
/*SEGGER_RTT_printf(0, "Hello FIDO2\n");*/ uint32_t beat = 0;
uint32_t t1 = 0;
uint32_t count;
int i = 0;
printf("hello FIDO2\r\n"); printf("hello FIDO2\r\n");
log_resetreason(); device_init();
if ((nrf_power_resetreas_get() & NRF_POWER_RESETREAS_RESETPIN_MASK) == 0)
while(1)
{ {
// Hard reset. this is for engineering A sample to work for USB ... if (millis() - t1 > 1000)
nrf_power_resetreas_clear(nrf_power_resetreas_get());
nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(0,31));
nrf_gpio_pin_clear(NRF_GPIO_PIN_MAP(0,31));
while (1)
;
}
nrf_power_resetreas_clear(nrf_power_resetreas_get());
init_power_clock();
rtc_config();
usb_init();
while (1)
{ {
i++; printf("heartbeat %ld\n", beat++);
for (int i = 0; i < LEDS_NUMBER; i++) t1 = millis();
{
bsp_board_led_invert(i);
nrf_delay_ms(25);
} }
count = nrf_drv_rtc_counter_get(&rtc);
printf("toggle\r\n"); if (usbhid_recv(hidmsg) > 0)
SEGGER_RTT_SetTerminal(0); {
SEGGER_RTT_printf(0, "Hello World %d!\n", count); printf("%d>> ",count++); dump_hex(hidmsg,sizeof(hidmsg));
SEGGER_RTT_SetTerminal(1);
SEGGER_RTT_printf(0, "Hello World %d!\n", i); /*ctaphid_handle_packet(hidmsg);*/
memset(hidmsg, 0, sizeof(hidmsg));
} }
/*u2f_hid_check_timeouts();*/
main_loop_delay();
}
} }

73
nrf52840/retarget.c Normal file
View File

@ -0,0 +1,73 @@
#include "nrf.h"
#include "SEGGER_RTT.h"
int _SEGGER_TERM = 0;
void set_logging_tag(uint32_t tag)
{
int term = 0;
while (tag)
{
if (tag & 1)
break;
term++;
tag = tag >> 1;
}
}
#if defined(__CC_ARM)
int fgetc(FILE * p_file)
{
return '0';
}
int fputc(int ch, FILE * p_file)
{
SEGGER_RTT_PutChar(_SEGGER_TERM, ch);
return ch;
}
#elif defined(__GNUC__) && defined(__SES_ARM)
int __getchar(FILE * p_file)
{
return '0';
}
int __putchar(int ch, FILE * p_file)
{
SEGGER_RTT_PutChar(_SEGGER_TERM, ch);
return ch;
}
#elif defined(__GNUC__) && !defined(__SES_ARM)
int _write(int file, const char * p_char, int len)
{
int i;
UNUSED_PARAMETER(file);
for (i = 0; i < len; i++)
{
SEGGER_RTT_PutChar(_SEGGER_TERM, *p_char++);
}
return len;
}
int _read(int file, char * p_char, int len)
{
*p_char = '0';
/*UNUSED_PARAMETER(file);*/
/*while (app_uart_get((uint8_t *)p_char) == NRF_ERROR_NOT_FOUND)*/
/*{*/
/*// No implementation needed.*/
/*}*/
return 1;
}
#else
/*#elif defined(__ICCARM__)*/
#error "No read/write for printing implemented for compiler"
#endif

View File

@ -6844,7 +6844,7 @@
// <e> NRF_QUEUE_ENABLED - nrf_queue - Queue module // <e> NRF_QUEUE_ENABLED - nrf_queue - Queue module
//========================================================== //==========================================================
#ifndef NRF_QUEUE_ENABLED #ifndef NRF_QUEUE_ENABLED
#define NRF_QUEUE_ENABLED 1 #define NRF_QUEUE_ENABLED 0
#endif #endif
// <q> NRF_QUEUE_CLI_CMDS - Enable CLI commands specific to the module // <q> NRF_QUEUE_CLI_CMDS - Enable CLI commands specific to the module

View File

@ -52,12 +52,13 @@
#include "nrf_log_default_backends.h" #include "nrf_log_default_backends.h"
#include "app_timer.h" #include "app_timer.h"
#include "app_error.h" #include "app_error.h"
#include "app_fifo.h"
#include "bsp.h" #include "bsp.h"
#include "bsp_cli.h" #include "bsp_cli.h"
#include "nrf_cli.h" #include "nrf_cli.h"
#include "SEGGER_RTT.h" #include "util.h"
#define printf(fmt,...) SEGGER_RTT_printf(0, fmt, ##__VA_ARGS__); #include "usb.h"
#define BTN_DATA_SEND 0 #define BTN_DATA_SEND 0
#define BTN_DATA_KEY_RELEASE (bsp_event_t)(BSP_EVENT_KEY_LAST + 1) #define BTN_DATA_KEY_RELEASE (bsp_event_t)(BSP_EVENT_KEY_LAST + 1)
@ -805,20 +806,12 @@ static void usbd_setup_SetProtocol(
/** @} */ /* End of processing setup requests functions */ /** @} */ /* End of processing setup requests functions */
void dump_hex(uint8_t * buf, int size)
{
while(size--)
{
printf("%02x ", *buf++);
}
printf("\n");
}
static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event) static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event)
{ {
static uint8_t buf[64]; static uint8_t buf[64];
uint32_t size;
nrf_drv_usbd_transfer_t transfer; nrf_drv_usbd_transfer_t transfer;
memset(&transfer, 0, sizeof(nrf_drv_usbd_transfer_t)); memset(&transfer, 0, sizeof(nrf_drv_usbd_transfer_t));
@ -861,7 +854,14 @@ static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event)
{ {
case NRF_USBD_EP_OK: case NRF_USBD_EP_OK:
/*printf("NRF_USBD_EP_OK\n");*/ /*printf("NRF_USBD_EP_OK\n");*/
printf(">> ");dump_hex(buf,64); /*printf(">> "); dump_hex(buf,64);*/
size = 64;
app_fifo_write(&USBHID_RECV_FIFO, buf, &size);
if (size != 64)
{
printf("Error, USB FIFO is full\n");
APP_ERROR_CHECK(NRF_ERROR_NO_MEM);
}
break; break;
case NRF_USBD_EP_WAITING: case NRF_USBD_EP_WAITING:
/*printf("NRF_USBD_EP_WAITING\n");*/ /*printf("NRF_USBD_EP_WAITING\n");*/
@ -1038,54 +1038,6 @@ static void power_usb_event_handler(nrf_drv_power_usb_evt_t event)
} }
} }
void log_resetreason(void)
{
/* Reset reason */
uint32_t rr = nrf_power_resetreas_get();
printf("Reset reasons:\n");
if (0 == rr)
{
printf("- NONE\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_RESETPIN_MASK))
{
printf("- RESETPIN\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_DOG_MASK ))
{
printf("- DOG\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_SREQ_MASK ))
{
printf("- SREQ\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_LOCKUP_MASK ))
{
printf("- LOCKUP\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_OFF_MASK ))
{
printf("- OFF\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_LPCOMP_MASK ))
{
printf("- LPCOMP\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_DIF_MASK ))
{
printf("- DIF\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_NFC_MASK ))
{
printf("- NFC\n");
}
if (0 != (rr & NRF_POWER_RESETREAS_VBUS_MASK ))
{
printf("- VBUS\n");
}
}
void usb_init(void) void usb_init(void)
{ {
ret_code_t ret; ret_code_t ret;

View File

@ -1,6 +1,10 @@
#ifndef _USB_H #ifndef _USB_H
#define _USB_H #define _USB_H
#include "app_fifo.h"
void usb_init(void); void usb_init(void);
extern app_fifo_t USBHID_RECV_FIFO;
#endif #endif

1
nrf52840/usbhid.c Normal file
View File

@ -0,0 +1 @@

56
stubs.c Normal file
View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include "device.h"
#include "util.h"
#include "ctap.h"
#include "u2f.h"
#if defined(STUB_CTAPHID) || defined(STUB_CTAP)
void ctap_init()
{
printf("STUB: ctap_init\n");
}
#endif
#if defined(STUB_CTAPHID)
void ctaphid_init()
{
printf("STUB: ctaphid_init\n");
}
void ctaphid_handle_packet(uint8_t * hidmsg)
{
printf("STUB: ctaphid_handle_packet\n");
}
void ctaphid_check_timeouts()
{
}
#endif
#ifdef STUB_CTAP
void ctap_reset_state()
{
printf("STUB: ctap_reset_state\n");
}
void ctap_response_init(CTAP_RESPONSE * resp)
{
}
void u2f_request(struct u2f_request_apdu* req, CTAP_RESPONSE * resp)
{
printf("STUB: u2f_request\n");
}
uint8_t ctap_request(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp)
{
printf("STUB: ctap_request\n");
return 0;
}
#endif

10
time.c
View File

@ -1,10 +0,0 @@
#include <sys/time.h>
#include <stdint.h>
uint64_t millis()
{
struct timeval te;
gettimeofday(&te, NULL); // get current time
uint64_t milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
return milliseconds;
}

6
time.h
View File

@ -1,6 +0,0 @@
#ifndef _TIME_H
#define _TIME_H
uint64_t millis();
#endif

View File

@ -1,97 +0,0 @@
/*
* Used as a bridge for USBHID protocol for FIDO 2.0 and U2F to ease firmware development and testing.
*
* Client FIDO 2.0, U2F software should bind to UDP port 7112 to send/recv USBHID messages from.
*
* */
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int udp_server()
{
int fd;
if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror( "socket failed" );
return 1;
}
struct timeval read_timeout;
read_timeout.tv_sec = 0;
read_timeout.tv_usec = 10;
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &read_timeout, sizeof(struct timeval)) != 0)
{
perror( "setsockopt" );
exit(1);
}
/*fcntl(fd, F_SETFL, fcntl(fd,F_GETFL, 0)|O_NONBLOCK);*/
struct sockaddr_in serveraddr;
memset( &serveraddr, 0, sizeof(serveraddr) );
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons( 8111 );
serveraddr.sin_addr.s_addr = htonl( INADDR_ANY );
if ( bind(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) {
perror( "bind failed" );
exit(1);
}
return fd;
}
int udp_recv(int fd, uint8_t * buf, int size)
{
fd_set input;
FD_ZERO(&input);
FD_SET(fd, &input);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 100000;
int n = select(fd + 1, &input, NULL, NULL, &timeout);
if (n == -1) {
perror("select\n");
exit(1);
} else if (n == 0)
return 0;
if (!FD_ISSET(fd, &input))
{
}
int length = recvfrom( fd, buf, size, 0, NULL, 0 );
if ( length < 0 ) {
perror( "recvfrom failed" );
exit(1);
}
return length;
}
void udp_send(int fd, uint8_t * buf, int size)
{
struct sockaddr_in serveraddr;
memset( &serveraddr, 0, sizeof(serveraddr) );
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons( 7112 );
serveraddr.sin_addr.s_addr = htonl( 0x7f000001 ); // (127.0.0.1)
if (sendto( fd, buf, size, 0, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) {
perror( "sendto failed" );
exit(1);
}
}
void udp_close(int fd)
{
close(fd);
}

View File

@ -1,14 +0,0 @@
#ifndef _UDP_BRIDGE_H
#define _UDP_BRIDGE_H
int udp_server();
// Recv from anyone
int udp_recv(int fd, uint8_t * buf, int size);
// Send to 127.0.0.1:7112
void udp_send(int fd, uint8_t * buf, int size);
void udp_close(int fd);
#endif

View File

@ -1,40 +0,0 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "usbhid.h"
#include "udp_bridge.h"
static int serverfd = 0;
void usbhid_init()
{
// just bridge to UDP for now for pure software testing
serverfd = udp_server();
}
// Receive 64 byte USB HID message, don't block, return size of packet, return 0 if nothing
int usbhid_recv(uint8_t * msg)
{
int l = udp_recv(serverfd, msg, HID_MESSAGE_SIZE);
/*if (l && l != HID_MESSAGE_SIZE)*/
/*{*/
/*printf("Error, recv'd message of wrong size %d", l);*/
/*exit(1);*/
/*}*/
return l;
}
// Send 64 byte USB HID message
void usbhid_send(uint8_t * msg)
{
udp_send(serverfd, msg, HID_MESSAGE_SIZE);
}
void usbhid_close()
{
udp_close(serverfd);
}

5
util.h
View File

@ -4,7 +4,12 @@
void dump_hex(uint8_t * buf, int size); void dump_hex(uint8_t * buf, int size);
#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif
#endif #endif