From 45cdc5c54d4c7dd9212197b358d95e940489284e Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Mon, 4 Jun 2018 19:35:34 -0400 Subject: [PATCH] power tests --- convert_log_to_c.py | 34 +++++++++ ctap.c | 1 + ctap_test.py | 29 ++++---- log.c | 13 ++-- log.h | 26 +++---- main.c | 161 ++++++++++++++++++++++++++++++++++++++++-- nrf52840/Makefile | 43 +---------- nrf52840/app_config.h | 12 ++-- nrf52840/crypto.c | 39 +--------- nrf52840/device.c | 7 +- nrf52840/retarget.c | 20 ++++-- nrf52840/usb.c | 55 ++++++++------- 12 files changed, 283 insertions(+), 157 deletions(-) create mode 100644 convert_log_to_c.py diff --git a/convert_log_to_c.py b/convert_log_to_c.py new file mode 100644 index 0000000..34dda7b --- /dev/null +++ b/convert_log_to_c.py @@ -0,0 +1,34 @@ +import sys +from sys import argv + +if len(argv) != 2: + print("usage: %s " % argv[0]); + sys.exit(1) + +log = open(argv[1]).readlines() + +nums = [] + +for x in log: + parse = [] + for i in x.split(' '): + try: + n = int(i,16) + parse.append(n) + except: + pass + if len(parse) == 0: + continue + assert(len(parse) == 64) + nums.append(parse) + +hexlines = [] + +for l in nums: + s = '' + for x in l: + s += '\\x%02x' % x + hexlines.append(s) + +for x in hexlines: + print('"'+x+'"') diff --git a/ctap.c b/ctap.c index 60e4cbf..c4bcac2 100644 --- a/ctap.c +++ b/ctap.c @@ -1196,6 +1196,7 @@ done: } printf1(TAG_CTAP,"cbor output structure: %d bytes\n", resp->length); + return status; } diff --git a/ctap_test.py b/ctap_test.py index 13f432c..23c560f 100644 --- a/ctap_test.py +++ b/ctap_test.py @@ -270,12 +270,12 @@ class Tester(): print('PASS: Test not cont') print('Check random cont ignored') - self.send_data(CTAPHID.INIT, '\x11\x22\x33\x44\x55\x66\x77\x88') - self.send_raw('\x01\x10\x00') - try: - cmd,r = self.recv_raw() # timeout response - except socket.timeout: - pass + #self.send_data(CTAPHID.INIT, '\x11\x22\x33\x44\x55\x66\x77\x88') + #self.send_raw('\x01\x10\x00') + #try: + #cmd,r = self.recv_raw() # timeout response + #except socket.timeout: + #pass print('PASS: random cont') print('Check busy') @@ -330,11 +330,12 @@ class Tester(): print('PASS: busy interleaved') - #print('Test idle') - #try: - #cmd,resp = self.recv_raw() - #except socket.timeout: - #print('Pass: Idle') + print('Test idle, wait for timeout') + sys.stdout.flush() + try: + cmd,resp = self.recv_raw() + except socket.timeout: + print('Pass: Idle') print('Test cid 0 is invalid') self.set_cid('\x00\x00\x00\x00') @@ -397,7 +398,7 @@ class Tester(): self.ctap.reset() - for i in range(0,2048): + for i in range(0,2048**2): creds = [] print(i) @@ -552,8 +553,8 @@ if __name__ == '__main__': t.find_device() #t.test_hid() #t.test_fido2() - #t.test_fido2_simple() - t.test_fido2_brute_force() + t.test_fido2_simple() + #t.test_fido2_brute_force() diff --git a/log.c b/log.c index 4f8d707..e2dfb64 100644 --- a/log.c +++ b/log.c @@ -17,6 +17,7 @@ struct logtag }; struct logtag tagtable[] = { + {TAG_GEN,""}, {TAG_MC,"MC"}, {TAG_GA,"GA"}, {TAG_CP,"CP"}, @@ -26,9 +27,10 @@ struct logtag tagtable[] = { {TAG_U2F,"U2F"}, {TAG_DUMP,"DUMP"}, {TAG_HID,"HID"}, - {TAG_GREEN,"\x1b[32mDEBUG\x1b[0m"}, - {TAG_RED,"\x1b[31mDEBUG\x1b[0m"}, - {TAG_TIME,"\x1b[33mTIME\x1b[0m"}, + {TAG_USB,"USB"}, + {TAG_GREEN,"DEBUG"}, + {TAG_RED,"DEBUG"}, + {TAG_TIME,"TIME"}, }; @@ -49,14 +51,14 @@ void LOG(uint32_t tag, const char * filename, int num, const char * fmt, ...) { if (tag & tagtable[i].tagn) { - printf("[%s] ", tagtable[i].tag); + if (tagtable[i].tag[0]) printf("[%s] ", tagtable[i].tag); i = 0; break; } } if (i != 0) { - printf("INVALID LOG TAG\n"); + printf2(TAG_ERR,"INVALID LOG TAG\n"); exit(1); } set_logging_tag(tag); @@ -78,5 +80,6 @@ void LOG_HEX(uint32_t tag, uint8_t * data, int length) { return; } + set_logging_tag(tag); dump_hex(data,length); } diff --git a/log.h b/log.h index d64e73e..a4e43a5 100644 --- a/log.h +++ b/log.h @@ -11,18 +11,20 @@ void set_logging_tag(uint32_t tag); typedef enum { - TAG_MC = (1 << 0), - TAG_GA = (1 << 1), - TAG_CP = (1 << 2), - TAG_ERR = (1 << 3), - TAG_PARSE= (1 << 4), - TAG_CTAP = (1 << 5), - TAG_U2F = (1 << 6), - TAG_DUMP = (1 << 7), - TAG_GREEN = (1 << 8), - TAG_RED= (1 << 9), - TAG_TIME= (1 << 10), - TAG_HID = (1 << 11), + TAG_GEN = (1 << 0), + TAG_MC = (1 << 1), + TAG_GA = (1 << 2), + TAG_CP = (1 << 3), + TAG_ERR = (1 << 4), + TAG_PARSE= (1 << 5), + TAG_CTAP = (1 << 6), + TAG_U2F = (1 << 7), + TAG_DUMP = (1 << 8), + TAG_GREEN = (1 << 9), + TAG_RED= (1 << 10), + TAG_TIME= (1 << 11), + TAG_HID = (1 << 12), + TAG_USB = (1 << 13), TAG_FILENO = (1<<31) } LOG_TAG; diff --git a/main.c b/main.c index 312a8b3..9bc606e 100644 --- a/main.c +++ b/main.c @@ -5,12 +5,13 @@ #include "cbor.h" #include "device.h" #include "ctaphid.h" +#include "bsp.h" #include "util.h" #include "log.h" #include "ctap.h" -#ifndef TEST +#if !defined(TEST) && !defined(TEST_POWER) int main(int argc, char * argv[]) { @@ -21,31 +22,35 @@ int main(int argc, char * argv[]) uint8_t hidmsg[64]; set_logging_mask( + /*0*/ + /*TAG_GEN|*/ /*TAG_MC |*/ /*TAG_GA |*/ /*TAG_CP |*/ - /*TAG_CTAP|*/ + TAG_CTAP| + /*TAG_HID|*/ /*TAG_U2F|*/ /*TAG_PARSE |*/ /*TAG_TIME|*/ - /*TAG_DUMP|*/ + TAG_DUMP| /*TAG_GREEN|*/ /*TAG_RED|*/ TAG_ERR ); - printf("init device\n"); + printf1(TAG_GEN,"init device\n"); device_init(); - printf("init ctaphid\n"); + printf1(TAG_GEN,"init ctaphid\n"); ctaphid_init(); - printf("init ctap\n"); + printf1(TAG_GEN,"init ctap\n"); ctap_init(); memset(hidmsg,0,sizeof(hidmsg)); - printf("recv'ing hid msg \n"); + printf1(TAG_GEN,"recv'ing hid msg \n"); + while(1) { @@ -79,3 +84,145 @@ int main(int argc, char * argv[]) } #endif + +#ifdef TEST_POWER + + +#define BUTT NRF_GPIO_PIN_MAP(0,11) +#define TRIG NRF_GPIO_PIN_MAP(0,30) + +int main(int argc, char * argv[]) +{ + int count = 0; + uint64_t t1 = 0; + uint64_t t2 = 0; + uint64_t accum = 0; + uint8_t hidmsg[64]; + + set_logging_mask( + 0 + /*TAG_GEN|*/ + /*TAG_MC |*/ + /*TAG_GA |*/ + /*TAG_CP |*/ + /*TAG_CTAP|*/ + /*TAG_HID|*/ + /*TAG_U2F|*/ + /*TAG_PARSE |*/ + /*TAG_TIME|*/ + /*TAG_DUMP|*/ + /*TAG_GREEN|*/ + /*TAG_RED|*/ + /*TAG_ERR*/ + ); + + device_init(); + ctaphid_init(); + ctap_init(); + + nrf_gpio_cfg_input(BUTT, NRF_GPIO_PIN_PULLUP); + nrf_gpio_cfg_output(TRIG); + nrf_gpio_pin_clear(TRIG); + + memset(hidmsg,0,sizeof(hidmsg)); + + printf1(TAG_GEN,"recv'ing hid msg \n"); + + + while(1) + { + if (millis() - t1 > 100) + { + /*printf("heartbeat %ld\n", beat++);*/ + heartbeat(); + t1 = millis(); + } + + if (usbhid_recv(hidmsg) > 0) + { + printf1(TAG_DUMP,"%d>> ",count++); dump_hex1(TAG_DUMP, hidmsg,sizeof(hidmsg)); + t2 = millis(); + ctaphid_handle_packet(hidmsg); + accum += millis() - t2; + printf1(TAG_TIME,"accum: %lu\n", (uint32_t)accum); + memset(hidmsg, 0, sizeof(hidmsg)); + } + else + { + /*main_loop_delay();*/ + } + ctaphid_check_timeouts(); + } + + // Should never get here + usbhid_close(); + printf("done\n"); + return 0; +} + +void delay(int ms) +{ + uint64_t t1; + t1 = millis(); + while(millis()-t1 < ms) + ; +} + +void ctaphid_write_block(uint8_t * data) +{ + // Don't actually use usb + /*usbhid_send(data);*/ +} + +uint8_t hidcmds[][64] = {"\x03\x00\x00\x00\x86\x00\x08\x2d\x73\x95\x80\x2e\xbb\x44\x8d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", +"\x03\x00\x00\x00\x90\x00\x01\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", +"\x03\x00\x00\x00\x90\x00\x75\x01\xa5\x01\x58\x20\xe5\x64\xed\x22\x01\xda\x3c\x2d\xab\x85\x3c\x1d\x04\x62\x67\x8a\x40\xaa\x1c\x3f\x31\x22\x21\x23\x35\xd5\xf8\xe4\x26\xc8\xd1\xa0\x02\xa2\x62\x69\x64\x6b\x65\x78\x61\x6d\x70\x6c\x6f\x2e\x6f\x72\x67\x64\x6e\x61", +"\x03\x00\x00\x00\x00\x6d\x65\x65\x45\x78\x61\x52\x50\x03\xa2\x62\x69\x64\x47\x75\x73\x65\x65\x5f\x6f\x64\x64\x6e\x61\x6d\x65\x67\x41\x42\x20\x55\x73\x65\x72\x04\x81\xa2\x63\x61\x6c\x67\x26\x64\x74\x79\x70\x65\x6a\x70\x75\x62\x6c\x69\x63\x2d\x6b\x65\x79\x05", +"\x03\x00\x00\x00\x01\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", +"\x03\x00\x00\x00\x90\x00\x01\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", +"\x03\x00\x00\x00\x90\x00\xf2\x02\xa3\x01\x6b\x65\x78\x61\x6d\x70\x6c\x6f\x2e\x6f\x72\x67\x02\x58\x20\xc4\x74\xef\xcd\xb3\xf9\x61\x18\xdd\xfb\x2f\xe5\x7b\x05\xd2\xf1\x23\xba\x20\x7c\x87\xea\x9e\xfd\x07\xa3\xe3\x21\xdc\x60\x9f\x63\x03\x81\xa2\x62\x69\x64\x58", +"\x03\x00\x00\x00\x00\xa8\x6a\x7e\x7b\xe7\x95\x63\xb1\x38\x2e\x2f\x03\xd6\x6c\xf5\xa3\xa9\x00\x00\x00\x00\x00\x00\x00\x00\x08\x24\x17\x4e\x23\xfd\x17\x7f\xb3\x18\x7c\x74\xe0\xbe\x35\x35\x94\x02\x7f\x61\xe3\x1c\xae\x73\xb4\x02\x7f\xf6\x7e\xc1\x36\x43\x2b\xc2", +"\x03\x00\x00\x00\x01\x60\xdd\xb8\x0b\x4a\xbb\x6f\x61\xac\xc3\xc6\x37\x9d\x33\x71\xe3\xcf\xa2\x4f\x5c\x11\x97\x44\xf8\x87\xb0\xd2\x67\xe8\x9a\xeb\x61\x39\x29\x61\xbf\xf7\xd1\xe1\xa9\x79\x26\x4a\x1a\xe7\x26\x60\xa4\x3d\x9f\x5c\xfe\x57\xbc\x4a\x74\x71\xf5\x67", +"\x03\x00\x00\x00\x02\xdd\x8d\x40\x49\x1e\x2d\x28\x15\x18\xbd\xf2\xe2\xef\x61\xbc\xb6\x04\x6f\x51\xdb\xe6\xc6\xd0\x9f\xbb\x06\xae\xac\xe4\xf6\xbd\x05\x85\xd3\x31\x5b\x98\x75\x2a\x4a\x31\xea\x5d\xc8\x78\x15\xee\xbe\x56\x0b\x43\x64\x74\x79\x70\x65\x6a\x70\x75", +"\x03\x00\x00\x00\x03\x62\x6c\x69\x63\x2d\x6b\x65\x79\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"}; + +int usbhid_recv(uint8_t * msg) +{ + static int lastval = 0; + static int reading = 0; + int val; + // button1 == p0.11 + // button2 == p0.12 + // button3 == p0.24 + // button4 == p0.25 + if (!reading) + { + delay(1); + nrf_gpio_pin_clear(TRIG); + val = nrf_gpio_pin_read(BUTT); + if (val == 0) + { + if (lastval != 0) + { + printf1(TAG_GEN, "button!\n"); + /*printf1(TAG_GEN,"size of array: %d elements", sizeof(hidcmds)/64);*/ + reading = 1; + } + } + lastval = val; + } + else + { + nrf_gpio_pin_set(TRIG); + memmove(msg, hidcmds[reading-1], 64); + reading++; + if (reading-1 == sizeof(hidcmds)/64) + { + reading = 0; + } + return 64; + } + return 0; +} + +#endif diff --git a/nrf52840/Makefile b/nrf52840/Makefile index 47e894e..96c936f 100644 --- a/nrf52840/Makefile +++ b/nrf52840/Makefile @@ -76,9 +76,6 @@ SRC_FILES += \ $(SDK_ROOT)/components/libraries/bsp/bsp.c \ $(SDK_ROOT)/components/libraries/bsp/bsp_cli.c \ \ - $(SDK_ROOT)/components/libraries/crypto/backend/nrf_hw/nrf_hw_backend_init.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/nrf_hw/nrf_hw_backend_rng.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/nrf_hw/nrf_hw_backend_rng_mbedtls.c \ $(SDK_ROOT)/external/cifra_AES128-EAX/blockwise.c \ $(SDK_ROOT)/external/cifra_AES128-EAX/cifra_cmac.c \ $(SDK_ROOT)/external/cifra_AES128-EAX/cifra_eax_aes.c \ @@ -88,38 +85,9 @@ SRC_FILES += \ $(SDK_ROOT)/external/cifra_AES128-EAX/modes.c \ $(SDK_ROOT)/external/fprintf/nrf_fprintf.c \ $(SDK_ROOT)/external/fprintf/nrf_fprintf_format.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/micro_ecc/micro_ecc_backend_ecc.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/micro_ecc/micro_ecc_backend_ecdh.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/micro_ecc/micro_ecc_backend_ecdsa.c \ $(SDK_ROOT)/integration/nrfx/legacy/nrf_drv_rng.c \ $(SDK_ROOT)/modules/nrfx/drivers/src/prs/nrfx_prs.c \ $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_rng.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cifra/cifra_backend_aes_aead.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_aead.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_aes.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_aes_shared.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_ecc.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_ecdh.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_ecdsa.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_error.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_hash.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_hkdf.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_hmac.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_init.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_rng.c \ - $(SDK_ROOT)/components/libraries/crypto/nrf_crypto_shared.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_aes.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_aes_aead.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_chacha_poly_aead.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_ecc.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_ecdh.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_ecdsa.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_hash.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_hmac.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_init.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_mutex.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_rng.c \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310/cc310_backend_shared.c \ @@ -193,8 +161,6 @@ INC_FOLDERS += \ $(SDK_ROOT)/modules/nrfx/hal \ $(SDK_ROOT)/external/fprintf \ \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310 \ - $(SDK_ROOT)/components/libraries/crypto/backend/cifra \ $(SDK_ROOT)/external/fprintf \ $(PROJ_DIR) \ $(SDK_ROOT)/components/libraries/experimental_section_vars \ @@ -209,7 +175,6 @@ INC_FOLDERS += \ $(SDK_ROOT)/components/libraries/mem_manager \ $(SDK_ROOT)/external/nrf_oberon \ $(SDK_ROOT)/components/libraries/atomic \ - $(SDK_ROOT)/components/libraries/crypto/backend/nrf_sw \ $(SDK_ROOT)/components/libraries/strerror \ $(SDK_ROOT)/integration/nrfx \ $(SDK_ROOT)/modules/nrfx/drivers/include \ @@ -218,22 +183,15 @@ INC_FOLDERS += \ $(SDK_ROOT)/components/libraries/util \ $(SDK_ROOT)/modules/nrfx \ $(SDK_ROOT)/components/drivers_nrf/nrf_soc_nosd \ - $(SDK_ROOT)/external/micro-ecc/micro-ecc \ $(SDK_ROOT)/external/segger_rtt \ $(SDK_ROOT)/modules/nrfx/mdk \ $(SDK_ROOT)/modules/nrfx/hal \ $(SDK_ROOT)/components/libraries/mutex \ - $(SDK_ROOT)/components/libraries/crypto/backend/micro_ecc \ - $(SDK_ROOT)/components/libraries/crypto/backend/mbedtls \ $(SDK_ROOT)/components/libraries/queue \ - $(SDK_ROOT)/components/libraries/crypto/backend/nrf_hw \ $(SDK_ROOT)/integration/nrfx/legacy \ $(SDK_ROOT)/external/cifra_AES128-EAX \ - $(SDK_ROOT)/external/nrf_tls/mbedtls/nrf_crypto/config \ $(SDK_ROOT)/components/boards \ $(SDK_ROOT)/external/nrf_cc310/include \ - $(SDK_ROOT)/components/libraries/crypto/backend/cc310_bl \ - $(SDK_ROOT)/components/libraries/crypto/backend/oberon \ # Libraries common to all targets @@ -256,6 +214,7 @@ CFLAGS += -DNRF52840_XXAA #CFLAGS += -DSTUB_CTAPHID #CFLAGS += -DSTUB_CTAP CFLAGS += -DuECC_PLATFORM=5 +CFLAGS += -DTEST_POWER CFLAGS += -std=gnu11 CFLAGS += -mcpu=cortex-m4 CFLAGS += -mthumb -mabi=aapcs diff --git a/nrf52840/app_config.h b/nrf52840/app_config.h index 786d1d6..2dd1a56 100644 --- a/nrf52840/app_config.h +++ b/nrf52840/app_config.h @@ -6,22 +6,22 @@ // or this value is actually used. It depends on which one is bigger. #ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_UP -#define SEGGER_RTT_CONFIG_BUFFER_SIZE_UP 64 +#define SEGGER_RTT_CONFIG_BUFFER_SIZE_UP 4096 #endif // SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS - Size of upstream buffer. #ifndef SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS -#define SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS 8 +#define SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS 16 #endif // SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN - Size of upstream buffer. #ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN -#define SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN 64 +#define SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN 8 #endif // SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS - Size of upstream buffer. #ifndef SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS -#define SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS 8 +#define SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS 2 #endif // SEGGER_RTT_CONFIG_DEFAULT_MODE - RTT behavior if the buffer is full. @@ -36,9 +36,9 @@ // <2=> BLOCK_IF_FIFO_FULL #ifndef SEGGER_RTT_CONFIG_DEFAULT_MODE -#define SEGGER_RTT_CONFIG_DEFAULT_MODE 2 +#define SEGGER_RTT_CONFIG_DEFAULT_MODE 1 -#define APP_FIFO_ENABLED 1 +#define APP_FIFO_ENABLED 2 #endif diff --git a/nrf52840/crypto.c b/nrf52840/crypto.c index 204ed2c..2e0f245 100644 --- a/nrf52840/crypto.c +++ b/nrf52840/crypto.c @@ -179,40 +179,6 @@ void crypto_ecc256_init() ret = SaSi_LibInit(); if (ret != SA_SILIB_RET_OK) { printf("Failed SaSi_LibInit - ret = 0x%x\n", ret); - /*switch(ret)*/ - /*{*/ - /*case SA_SILIB_RET_OK:*/ - /*printf("SA_SILIB_RET_OK\n");*/ - /*break;*/ - - /*case SA_SILIB_RET_EINVAL_CTX_PTR:*/ - /*printf("SA_SILIB_RET_EINVAL_CTX_PTR\n");*/ - /*break;*/ - - /*case SA_SILIB_RET_EINVAL_WORK_BUF_PTR:*/ - /*printf("SA_SILIB_RET_EINVAL_WORK_BUF_PTR\n");*/ - /*break;*/ - - /*case SA_SILIB_RET_HAL:*/ - /*printf("SA_SILIB_RET_HAL\n");*/ - /*break;*/ - - /*case SA_SILIB_RET_PAL:*/ - /*printf("SA_SILIB_RET_PAL\n");*/ - /*break;*/ - - /*case SA_SILIB_RET_EINVAL_HW_VERSION :*/ - /*printf("SA_SILIB_RET_EINVAL_HW_VERSION \n");*/ - /*break;*/ - - /*case SA_SILIB_RET_EINVAL_HW_SIGNATURE:*/ - /*printf("SA_SILIB_RET_EINVAL_HW_SIGNATURE\n");*/ - /*break;*/ - - /*case SA_SILIB_RESERVE32B:*/ - /*printf("SA_SILIB_RESERVE32B\n");*/ - /*break;*/ - /*}*/ exit(1); } @@ -223,8 +189,7 @@ void crypto_ecc256_init() ret = CRYS_RndInit(&rndState_ptr, &rndWorkBuff_ptr); if (ret != SA_SILIB_RET_OK) { printf("Failed CRYS_RndInit - ret = 0x%x\n", ret); - while(1) - ; + exit(1); } _es256_curve = CRYS_ECPKI_GetEcDomain(CRYS_ECPKI_DomainID_secp256r1); @@ -374,7 +339,7 @@ void derive_private_key_pair(uint8_t * data, int len, uint8_t * data2, int len2, break; } - // There isn't a CC310 function for calculating a public key from a private + // There isn't a CC310 function for calculating a public key from a private, // so to get around it, we can "fix" the RNG input to GenKeyPair if(pubkey != NULL) diff --git a/nrf52840/device.c b/nrf52840/device.c index 700c4e4..220bbc6 100644 --- a/nrf52840/device.c +++ b/nrf52840/device.c @@ -168,17 +168,21 @@ app_fifo_t USBHID_RECV_FIFO; void usbhid_init() { +#ifndef TEST_POWER app_fifo_init(&USBHID_RECV_FIFO, fifo_buf, sizeof(fifo_buf)); usb_init(); +#endif } // Receive 64 byte USB HID message, don't block, return size of packet, return 0 if nothing +#ifndef TEST_POWER int usbhid_recv(uint8_t * msg) { uint32_t size = 64; app_fifo_read(&USBHID_RECV_FIFO, msg, &size); return size; } +#endif // Send 64 byte USB HID message @@ -212,12 +216,13 @@ void heartbeat() nrf_gpio_pin_toggle(LED_4); } - +#ifndef TEST_POWER void ctaphid_write_block(uint8_t * data) { printf1(TAG_DUMP,"<< "); dump_hex1(TAG_DUMP,data, 64); usbhid_send(data); } +#endif int ctap_user_presence_test() diff --git a/nrf52840/retarget.c b/nrf52840/retarget.c index 2663db5..9ba151d 100644 --- a/nrf52840/retarget.c +++ b/nrf52840/retarget.c @@ -13,6 +13,8 @@ void set_logging_tag(uint32_t tag) term++; tag = tag >> 1; } + + _SEGGER_TERM = term; } #if defined(__CC_ARM) @@ -44,12 +46,23 @@ int __putchar(int ch, FILE * p_file) int _write(int file, const char * p_char, int len) { int i; + static int lastterm = -1; + /*char buf[2];*/ + /*buf[1] = 0;*/ UNUSED_PARAMETER(file); + if (_SEGGER_TERM != lastterm) + { + SEGGER_RTT_SetTerminal(_SEGGER_TERM); + lastterm = _SEGGER_TERM; + } + for (i = 0; i < len; i++) { - SEGGER_RTT_PutChar(_SEGGER_TERM, *p_char++); + /*buf[0] = *p_char++;*/ + SEGGER_RTT_PutChar(0, *p_char++); + /*SEGGER_RTT_TerminalOut(_SEGGER_TERM, buf);*/ } return len; @@ -58,11 +71,6 @@ int _write(int file, const char * p_char, int 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; } diff --git a/nrf52840/usb.c b/nrf52840/usb.c index 52f2779..33845e8 100644 --- a/nrf52840/usb.c +++ b/nrf52840/usb.c @@ -58,6 +58,7 @@ #include "nrf_cli.h" #include "util.h" +#include "log.h" #include "usb.h" #define BTN_DATA_SEND 0 @@ -448,7 +449,7 @@ static void respond_setup_data( ret = nrf_drv_usbd_ep_transfer(NRF_DRV_USBD_EPIN0, &transfer); if (ret != NRF_SUCCESS) { - printf("Transfer starting failed: %lu", (uint32_t)ret); + printf1(TAG_USB,"Transfer starting failed: %lu", (uint32_t)ret); } ASSERT(ret == NRF_SUCCESS); UNUSED_VARIABLE(ret); @@ -538,7 +539,7 @@ static void usbd_setup_GetStatus(nrf_drv_usbd_setup_t const * const p_setup) default: break; // Just go to stall } - printf("Unknown status: 0x%2x", p_setup->bmRequestType); + printf1(TAG_USB,"Unknown status: 0x%2x", p_setup->bmRequestType); nrf_drv_usbd_setup_stall(); } @@ -568,7 +569,7 @@ static void usbd_setup_ClearFeature(nrf_drv_usbd_setup_t const * const p_setup) } } } - printf("Unknown feature to clear"); + printf1(TAG_USB,"Unknown feature to clear"); nrf_drv_usbd_setup_stall(); } @@ -598,7 +599,7 @@ static void usbd_setup_SetFeature(nrf_drv_usbd_setup_t const * const p_setup) } } } - printf("Unknown feature to set"); + printf1(TAG_USB, "Unknown feature to set"); nrf_drv_usbd_setup_stall(); } @@ -673,7 +674,7 @@ static void usbd_setup_GetDescriptor(nrf_drv_usbd_setup_t const * const p_setup) if ((p_setup->bmRequestType) == 0x80) { // Which endpoint? - printf("endpoint descriptor: %d\n", ((p_setup->wValue) & 0xFF)); + printf1(TAG_USB,"endpoint descriptor: %d\n", ((p_setup->wValue) & 0xFF)); if (((p_setup->wValue) & 0xFF) == 1) { respond_setup_data( @@ -725,7 +726,7 @@ static void usbd_setup_GetDescriptor(nrf_drv_usbd_setup_t const * const p_setup) break; // Not supported - go to stall } - printf("Unknown : 0x%02x, type: 0x%02x or value: 0x%02x\n", + printf1(TAG_USB,"Unknown : 0x%02x, type: 0x%02x or value: 0x%02x\n", p_setup->wValue >> 8, p_setup->bmRequestType, p_setup->wValue & 0xFF); @@ -765,7 +766,7 @@ static void usbd_setup_SetConfig(nrf_drv_usbd_setup_t const * const p_setup) } } } - printf("Wrong configuration: Index: 0x%2x, Value: 0x%2x.", + printf1(TAG_USB,"Wrong configuration: Index: 0x%2x, Value: 0x%2x.", p_setup->wIndex, p_setup->wValue); nrf_drv_usbd_setup_stall(); @@ -779,7 +780,7 @@ static void usbd_setup_SetIdle(nrf_drv_usbd_setup_t const * const p_setup) nrf_drv_usbd_setup_clear(); return; } - printf("Set Idle wrong type: 0x%2x.", p_setup->bmRequestType); + printf1(TAG_USB,"Set Idle wrong type: 0x%2x.", p_setup->bmRequestType); nrf_drv_usbd_setup_stall(); } @@ -787,7 +788,7 @@ static void usbd_setup_SetInterface( nrf_drv_usbd_setup_t const * const p_setup) { //no alternate setting is supported - STALL always - printf("No alternate interfaces supported."); + printf1(TAG_USB,"No alternate interfaces supported."); nrf_drv_usbd_setup_stall(); } @@ -800,7 +801,7 @@ static void usbd_setup_SetProtocol( nrf_drv_usbd_setup_clear(); return; } - printf("Set Protocol wrong type: 0x%2x.", p_setup->bmRequestType); + printf1(TAG_USB,"Set Protocol wrong type: 0x%2x.", p_setup->bmRequestType); nrf_drv_usbd_setup_stall(); } @@ -818,15 +819,15 @@ static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event) switch (p_event->type) { case NRF_DRV_USBD_EVT_SUSPEND: - printf("SUSPEND state detected\n"); + printf1(TAG_USB,"SUSPEND state detected\n"); m_usbd_suspend_state_req = true; break; case NRF_DRV_USBD_EVT_RESUME: - printf("RESUMING from suspend\n"); + printf1(TAG_USB,"RESUMING from suspend\n"); m_usbd_suspend_state_req = false; break; case NRF_DRV_USBD_EVT_WUREQ: - printf("RemoteWU initiated\n"); + printf1(TAG_USB,"RemoteWU initiated\n"); m_usbd_suspend_state_req = false; break; case NRF_DRV_USBD_EVT_RESET: @@ -859,7 +860,7 @@ static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event) app_fifo_write(&USBHID_RECV_FIFO, buf, &size); if (size != 64) { - printf("Error, USB FIFO is full\n"); + printf2(TAG_ERR,"Error, USB FIFO is full\n"); APP_ERROR_CHECK(NRF_ERROR_NO_MEM); } break; @@ -869,16 +870,16 @@ static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event) transfer.size = nrf_drv_usbd_epout_size_get(NRF_DRV_USBD_EPOUT1);; if (transfer.size > 64) { - printf("Error, invalid transfer size\n"); + printf2(TAG_ERR,"Error, invalid transfer size\n"); return; } nrf_drv_usbd_ep_transfer(NRF_DRV_USBD_EPOUT1, &transfer); break; case NRF_USBD_EP_OVERLOAD: - printf("NRF_USBD_EP_OVERLOAD\n"); + printf1(TAG_ERR,"NRF_USBD_EP_OVERLOAD\n"); break; case NRF_USBD_EP_ABORTED: - printf("NRF_USBD_EP_ABORTED\n"); + printf1(TAG_ERR,"NRF_USBD_EP_ABORTED\n"); break; default: break; @@ -898,11 +899,11 @@ static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event) else if (NRF_USBD_EP_ABORTED == p_event->data.eptransfer.status) { /* Just ignore */ - printf("Transfer aborted event on EPIN0\n"); + printf1(TAG_USB,"Transfer aborted event on EPIN0\n"); } else { - printf("Transfer failed on EPIN0: %d", p_event->data.eptransfer.status); + printf1(TAG_USB,"Transfer failed on EPIN0: %d", p_event->data.eptransfer.status); nrf_drv_usbd_setup_stall(); } } @@ -923,11 +924,11 @@ static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event) else if (NRF_USBD_EP_ABORTED == p_event->data.eptransfer.status) { /* Just ignore */ - printf("Transfer aborted event on EPOUT0\n"); + printf1(TAG_USB,"Transfer aborted event on EPOUT0\n"); } else { - printf("Transfer failed on EPOUT0: %d", p_event->data.eptransfer.status); + printf1(TAG_USB,"Transfer failed on EPOUT0: %d", p_event->data.eptransfer.status); nrf_drv_usbd_setup_stall(); } } @@ -979,19 +980,19 @@ static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event) } else { - printf("Command 0xB. Unknown request: 0x%2x", setup.bmRequestType); + printf1(TAG_USB,"Command 0xB. Unknown request: 0x%2x", setup.bmRequestType); nrf_drv_usbd_setup_stall(); } break; default: - printf("Unknown request: 0x%2x", setup.bmRequest); + printf1(TAG_USB,"Unknown request: 0x%2x", setup.bmRequest); nrf_drv_usbd_setup_stall(); return; } break; } default: - printf("unknown usb event\n"); + printf1(TAG_USB,"unknown usb event\n"); break; } } @@ -1003,14 +1004,14 @@ static void power_usb_event_handler(nrf_drv_power_usb_evt_t event) switch (event) { case NRF_DRV_POWER_USB_EVT_DETECTED: - printf("USB power detected\n"); + printf1(TAG_USB,"USB power detected\n"); if (!nrf_drv_usbd_is_enabled()) { nrf_drv_usbd_enable(); } break; case NRF_DRV_POWER_USB_EVT_REMOVED: - printf("USB power removed\n"); + printf1(TAG_USB,"USB power removed\n"); m_usbd_configured = false; m_send_mouse_position = false; if (nrf_drv_usbd_is_started()) @@ -1026,7 +1027,7 @@ static void power_usb_event_handler(nrf_drv_power_usb_evt_t event) bsp_board_led_off(LED_USB_POWER); break; case NRF_DRV_POWER_USB_EVT_READY: - printf("USB ready\n"); + printf1(TAG_USB,"USB ready\n"); bsp_board_led_on(LED_USB_POWER); if (!nrf_drv_usbd_is_started()) {