diff --git a/src/apdu.c b/src/apdu.c index f5d28e7..d29f629 100644 --- a/src/apdu.c +++ b/src/apdu.c @@ -123,12 +123,12 @@ uint16_t apdu_process(uint8_t itf, const uint8_t *buffer, uint16_t buffer_size) #ifndef ENABLE_EMULATION #ifdef USB_ITF_HID if (itf == ITF_HID_CTAP) { - driver_exec_finished_cont_hid(itf, apdu.rlen + 2, rdata_gr - apdu.rdata); + driver_exec_finished_cont_hid(itf, apdu.rlen + 2, (uint16_t)(rdata_gr - apdu.rdata)); } #endif #ifdef USB_ITF_CCID if (itf == ITF_SC_CCID || itf == ITF_SC_WCID) { - driver_exec_finished_cont_ccid(itf, apdu.rlen + 2, rdata_gr - apdu.rdata); + driver_exec_finished_cont_ccid(itf, apdu.rlen + 2, (uint16_t)(rdata_gr - apdu.rdata)); } #endif #else @@ -152,12 +152,12 @@ uint16_t apdu_process(uint8_t itf, const uint8_t *buffer, uint16_t buffer_size) #ifndef ENABLE_EMULATION #ifdef USB_ITF_HID if (itf == ITF_HID_CTAP) { - driver_exec_finished_cont_hid(itf, apdu.ne + 2, rdata_gr - apdu.ne - apdu.rdata); + driver_exec_finished_cont_hid(itf, (uint16_t)(apdu.ne + 2), (uint16_t)(rdata_gr - apdu.ne - apdu.rdata)); } #endif #ifdef USB_ITF_CCID if (itf == ITF_SC_CCID || itf == ITF_SC_WCID) { - driver_exec_finished_cont_ccid(itf, apdu.ne + 2, rdata_gr - apdu.ne - apdu.rdata); + driver_exec_finished_cont_ccid(itf, (uint16_t)(apdu.ne + 2), (uint16_t)(rdata_gr - apdu.ne - apdu.rdata)); } #endif #else @@ -183,7 +183,8 @@ uint16_t set_res_sw(uint8_t sw1, uint8_t sw2) { return make_uint16_t_be(sw1, sw2); } -void apdu_thread(void) { +void *apdu_thread(void *arg) { + (void)arg; card_init_core1(); while (1) { uint32_t m = 0; @@ -219,6 +220,7 @@ done: ; #ifdef ESP_PLATFORM vTaskDelete(NULL); #endif + return NULL; } void apdu_finish() { diff --git a/src/apdu.h b/src/apdu.h index 95f5f52..d41ef71 100644 --- a/src/apdu.h +++ b/src/apdu.h @@ -73,6 +73,6 @@ extern int process_apdu(); extern uint16_t apdu_process(uint8_t, const uint8_t *buffer, uint16_t buffer_size); extern void apdu_finish(); extern uint16_t apdu_next(); -extern void apdu_thread(); +extern void *apdu_thread(void *); #endif diff --git a/src/crypto_utils.c b/src/crypto_utils.c index b52e4ab..4773ea5 100644 --- a/src/crypto_utils.c +++ b/src/crypto_utils.c @@ -135,7 +135,7 @@ int decrypt_with_aad(const uint8_t key[32], const uint8_t *in_buf, size_t in_len void double_hash_pin(const uint8_t *pin, uint16_t len, uint8_t output[32]) { uint8_t o1[32]; hash_multi(pin, len, o1); - for (int i = 0; i < sizeof(o1); i++) { + for (size_t i = 0; i < sizeof(o1); i++) { o1[i] ^= pin[i % len]; } hash_multi(o1, sizeof(o1), output); diff --git a/src/debug.h b/src/debug.h index c4661b6..809f9b6 100644 --- a/src/debug.h +++ b/src/debug.h @@ -20,16 +20,16 @@ #if defined(DEBUG_APDU) && DEBUG_APDU == 1 #define DEBUG_PAYLOAD(_p, _s) { \ - printf("Payload %s (%d bytes) [%s:%d]:\n", #_p, (int) (_s), __FILE__, __LINE__); \ - for (int _i = 0; _i < _s; _i += 16) { \ + printf("Payload %s (%zu bytes) [%s:%d]:\n", #_p, (size_t)(_s), __FILE__, __LINE__); \ + for (size_t _i = 0; _i < (size_t)(_s); _i += 16) { \ printf("%" PRIxPTR "h : ", (uintptr_t) (_i + _p)); \ - for (int _j = 0; _j < 16; _j++) { \ - if (_j < _s - _i) printf("%02X ", (_p)[_i + _j]); \ + for (size_t _j = 0; _j < 16; _j++) { \ + if (_j < (size_t)(_s) - _i) printf("%02X ", (_p)[_i + _j]); \ else printf(" "); \ if (_j == 7) printf(" "); \ } printf(": "); \ - for (int _j = 0; _j < 16; _j++) { \ - if (_j < _s - _i && (_p)[_i + _j] > 32 && (_p)[_i + _j] != 127 && (_p)[_i + _j] < 176) printf("%c", (_p)[_i + _j]); \ + for (size_t _j = 0; _j < 16; _j++) { \ + if (_j < (size_t)(_s) - _i && (_p)[_i + _j] > 32 && (_p)[_i + _j] != 127 && (_p)[_i + _j] < 176) printf("%c", (_p)[_i + _j]); \ else printf(" "); \ if (_j == 7) printf(" "); \ } \ @@ -37,9 +37,9 @@ } printf("\n"); \ } #define DEBUG_DATA(_p, _s) { \ - printf("Data %s (%d bytes) [%s:%d]:\n", #_p, (int) (_s), __FILE__, __LINE__); \ + printf("Data %s (%zu bytes) [%s:%d]:\n", #_p, (size_t)(_s), __FILE__, __LINE__); \ char *_tmp = (char *) calloc(2 * (_s) + 1, sizeof(char)); \ - for (int _i = 0; _i < (_s); _i++) { \ + for (size_t _i = 0; _i < (size_t)(_s); _i++) { \ sprintf(&_tmp[2 * _i], "%02X", (_p)[_i]); \ } \ printf("%s\n", _tmp); \ diff --git a/src/eac.c b/src/eac.c index bf5ca27..502dace 100644 --- a/src/eac.c +++ b/src/eac.c @@ -237,7 +237,7 @@ int sm_verify() { uint16_t input_len = 0; int r = 0; bool add_header = (CLA(apdu) & 0xC) == 0xC; - int data_len = (int) (apdu.nc / sm_blocksize) * sm_blocksize; + size_t data_len = (size_t)(apdu.nc / sm_blocksize) * sm_blocksize; if (data_len % sm_blocksize) { data_len += sm_blocksize; } diff --git a/src/fs/file.c b/src/fs/file.c index 6cb7ebc..4d6c53a 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -282,7 +282,7 @@ void scan_region(bool persistent) void wait_flash_finish(); void scan_flash() { initialize_flash(false); //soft initialization - uint32_t r1 = *(uintptr_t *) flash_read(end_rom_pool), r2 = *(uintptr_t *) flash_read(end_rom_pool + sizeof(uintptr_t)); + uint32_t r1 = (uint32_t)(*(uintptr_t *) flash_read(end_rom_pool)), r2 = (uint32_t)(*(uintptr_t *) flash_read(end_rom_pool + sizeof(uintptr_t))); if ((r1 == 0xffffffff || r1 == 0xefefefef) && (r2 == 0xffffffff || r2 == 0xefefefef)) { printf("First initialization (or corrupted!)\n"); uint8_t empty[sizeof(uintptr_t) * 2 + sizeof(uint32_t)]; diff --git a/src/fs/file.h b/src/fs/file.h index 0686a74..77a73e3 100644 --- a/src/fs/file.h +++ b/src/fs/file.h @@ -71,6 +71,9 @@ #define MAX_DYNAMIC_FILES 256 +#ifdef _MSC_VER +__pragma( pack(push, 1) ) +#endif typedef struct file { const uint8_t *name; uint8_t *data; //should include 2 bytes len at begining @@ -82,7 +85,13 @@ typedef struct file { #ifdef ENABLE_EMULATION uint32_t _padding; #endif -} __attribute__ ((packed)) file_t; +} +#ifdef _MSC_VER +__pragma( pack(pop) ) +#else +__attribute__ ((packed)) +#endif +file_t; extern bool file_has_data(file_t *); diff --git a/src/fs/flash.c b/src/fs/flash.c index 24f8abd..83f8ee5 100644 --- a/src/fs/flash.c +++ b/src/fs/flash.c @@ -195,15 +195,15 @@ int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len) { } uint32_t flash_free_space() { - return last_base - start_data_pool; + return (uint32_t)(last_base - start_data_pool); } uint32_t flash_used_space() { - return end_data_pool - last_base; + return (uint32_t)(end_data_pool - last_base); } uint32_t flash_total_space() { - return end_data_pool - start_data_pool; + return (uint32_t)(end_data_pool - start_data_pool); } uint32_t flash_num_files() { diff --git a/src/fs/low_flash.c b/src/fs/low_flash.c index f087416..d2124bb 100644 --- a/src/fs/low_flash.c +++ b/src/fs/low_flash.c @@ -54,8 +54,8 @@ #else #include #include - #include "queue.h" #endif + #include "queue.h" #endif #define FLASH_SECTOR_SIZE 4096 #define XIP_BASE 0 @@ -322,7 +322,7 @@ uint8_t *flash_read(uintptr_t addr) { uintptr_t flash_read_uintptr(uintptr_t addr) { uint8_t *p = flash_read(addr); uintptr_t v = 0x0; - for (int i = 0; i < sizeof(uintptr_t); i++) { + for (size_t i = 0; i < sizeof(uintptr_t); i++) { v |= (uintptr_t) p[i] << (8 * i); } return v; @@ -330,7 +330,7 @@ uintptr_t flash_read_uintptr(uintptr_t addr) { uint16_t flash_read_uint16(uintptr_t addr) { uint8_t *p = flash_read(addr); uint16_t v = 0x0; - for (int i = 0; i < sizeof(uint16_t); i++) { + for (size_t i = 0; i < sizeof(uint16_t); i++) { v |= p[i] << (8 * i); } return v; diff --git a/src/fs/otp.c b/src/fs/otp.c index ea0033e..05395a9 100644 --- a/src/fs/otp.c +++ b/src/fs/otp.c @@ -171,6 +171,9 @@ int otp_enable_secure_boot(uint8_t bootkey, bool secure_lock) { } #elif defined(ESP_PLATFORM) // TODO: Implement secure boot for ESP32-S3 +#else + (void)bootkey; + (void)secure_lock; #endif // PICO_RP2350 goto err; err: diff --git a/src/fs/phy.c b/src/fs/phy.c index fca776c..8edf0f2 100644 --- a/src/fs/phy.c +++ b/src/fs/phy.c @@ -55,7 +55,7 @@ int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len) { } if (phy->usb_product_present) { *p++ = PHY_USB_PRODUCT; - *p++ = strlen(phy->usb_product) + 1; + *p++ = (uint8_t)strlen(phy->usb_product) + 1; strcpy((char *)p, phy->usb_product); p += strlen(phy->usb_product); *p++ = '\0'; @@ -76,7 +76,7 @@ int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len) { *p++ = phy->led_driver; } - *len = p - data; + *len = (uint8_t)(p - data); return PICOKEY_OK; } diff --git a/src/main.c b/src/main.c index 03113c8..ea9682b 100644 --- a/src/main.c +++ b/src/main.c @@ -118,7 +118,6 @@ bool is_req_button_pending() { bool cancel_button = false; -#ifdef ENABLE_EMULATION #ifdef _MSC_VER #include struct timezone @@ -148,7 +147,7 @@ int gettimeofday(struct timeval* tp, struct timezone* tzp) return 0; } #endif -#else +#if !defined(ENABLE_EMULATION) #ifdef ESP_PLATFORM bool picok_board_button_read() { int boot_state = gpio_get_level(BOOT_PIN); @@ -307,7 +306,7 @@ int main(void) { #endif pico_get_unique_board_id(&pico_serial); memset(pico_serial_str, 0, sizeof(pico_serial_str)); - for (int i = 0; i < sizeof(pico_serial); i++) { + for (size_t i = 0; i < sizeof(pico_serial); i++) { snprintf(&pico_serial_str[2 * i], 3, "%02X", pico_serial.id[i]); } mbedtls_sha256(pico_serial.id, sizeof(pico_serial.id), pico_serial_hash, false); diff --git a/src/pico_keys.h b/src/pico_keys.h index b78cc77..69ea1a0 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -75,12 +75,12 @@ static inline uint16_t get_uint16_t_be(const uint8_t *b) { static inline uint16_t get_uint16_t_le(const uint8_t *b) { return make_uint16_t_le(b[0], b[1]); } -static inline uint32_t put_uint16_t_be(uint16_t n, uint8_t *b) { +static inline uint8_t put_uint16_t_be(uint16_t n, uint8_t *b) { *b++ = (n >> 8) & 0xff; *b = n & 0xff; return 2; } -static inline uint32_t put_uint16_t_le(uint16_t n, uint8_t *b) { +static inline uint8_t put_uint16_t_le(uint16_t n, uint8_t *b) { *b++ = n & 0xff; *b = (n >> 8) & 0xff; return 2; @@ -154,7 +154,6 @@ extern int flash_clear_file(file_t *file); extern int (*button_pressed_cb)(uint8_t); extern bool is_req_button_pending(); -extern uint32_t button_timeout; #define SW_BYTES_REMAINING_00() set_res_sw(0x61, 0x00) #define SW_WARNING_STATE_UNCHANGED() set_res_sw(0x62, 0x00) diff --git a/src/rescue.c b/src/rescue.c index d7fbec8..6a1b634 100644 --- a/src/rescue.c +++ b/src/rescue.c @@ -70,7 +70,7 @@ int cmd_write() { if (P1(apdu) == 0x1) { // PHY #ifndef ENABLE_EMULATION - int ret = phy_unserialize_data(apdu.data, apdu.nc, &phy_data); + int ret = phy_unserialize_data(apdu.data, (uint16_t)apdu.nc, &phy_data); if (ret == PICOKEY_OK) { if (phy_save() != PICOKEY_OK) { return SW_EXEC_ERROR(); diff --git a/src/rng/hwrng.c b/src/rng/hwrng.c index cf5981f..7e8ec12 100644 --- a/src/rng/hwrng.c +++ b/src/rng/hwrng.c @@ -24,7 +24,7 @@ #include "hwrng.h" #include "bsp/board.h" #include "pico/rand.h" -#elif (ESP_PLATFORM) +#elif defined(ESP_PLATFORM) #include "bootloader_random.h" #include "esp_random.h" #include "esp_compat.h" diff --git a/src/usb/hid/hid.c b/src/usb/hid/hid.c index f61d56e..ee5e54c 100644 --- a/src/usb/hid/hid.c +++ b/src/usb/hid/hid.c @@ -31,7 +31,6 @@ static portMUX_TYPE mutex = portMUX_INITIALIZER_UNLOCKED; #include "apdu.h" #include "usb.h" -static bool mounted = false; extern void init_fido(); bool is_nk = false; uint8_t (*get_version_major)() = NULL; @@ -48,14 +47,6 @@ typedef struct msg_packet { msg_packet_t msg_packet = { 0 }; -void tud_mount_cb() { - mounted = true; -} - -bool driver_mounted_hid() { - return mounted; -} - static uint16_t *send_buffer_size = NULL; static write_status_t *last_write_result = NULL; @@ -158,7 +149,7 @@ static bool sent_key = false; static bool keyboard_encode = false; void add_keyboard_buffer(const uint8_t *data, size_t data_len, bool encode) { - keyboard_buffer_len = MIN(sizeof(keyboard_buffer), data_len); + keyboard_buffer_len = (uint8_t)MIN(sizeof(keyboard_buffer), data_len); memcpy(keyboard_buffer, data, keyboard_buffer_len); keyboard_encode = encode; } @@ -166,7 +157,7 @@ void add_keyboard_buffer(const uint8_t *data, size_t data_len, bool encode) { void append_keyboard_buffer(const uint8_t *data, size_t data_len) { if (keyboard_buffer_len + data_len < sizeof(keyboard_buffer)) { memcpy(keyboard_buffer + keyboard_buffer_len, data, MIN(sizeof(keyboard_buffer) - keyboard_buffer_len, data_len)); - keyboard_buffer_len += MIN(sizeof(keyboard_buffer) - keyboard_buffer_len, data_len); + keyboard_buffer_len += (uint8_t)MIN(sizeof(keyboard_buffer) - keyboard_buffer_len, data_len); } } @@ -328,7 +319,7 @@ int driver_process_usb_nopacket_hid() { } extern const uint8_t fido_aid[], u2f_aid[], oath_aid[]; -extern void apdu_thread(void), cbor_thread(void); +extern void *cbor_thread(void *); int driver_process_usb_packet_hid(uint16_t read) { int apdu_sent = 0; diff --git a/src/usb/tusb_config.h b/src/usb/tusb_config.h index dffdee9..c82c21a 100644 --- a/src/usb/tusb_config.h +++ b/src/usb/tusb_config.h @@ -90,8 +90,12 @@ extern "C" { #endif #ifndef CFG_TUSB_MEM_ALIGN +#ifdef _MSC_VER +#define CFG_TUSB_MEM_ALIGN __declspec(align(4)) +#else #define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4))) #endif +#endif //-------------------------------------------------------------------- // DEVICE CONFIGURATION diff --git a/src/usb/usb.c b/src/usb/usb.c index 6cdedc5..aff9347 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -37,13 +37,15 @@ // Device specific functions static uint32_t *timeout_counter = NULL; static uint8_t card_locked_itf = 0; // no locked -static void (*card_locked_func)(void) = NULL; +static void *(*card_locked_func)(void *) = NULL; #ifndef ENABLE_EMULATION static mutex_t mutex; extern void usb_desc_setup(); #endif #if !defined(PICO_PLATFORM) && !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) -#include +#ifdef _MSC_VER +#include "pthread_win32.h" +#endif pthread_t hcore0, hcore1; #endif @@ -175,7 +177,7 @@ void card_init_core1() { uint16_t finished_data_size = 0; -void card_start(uint8_t itf, void (*func)(void)) { +void card_start(uint8_t itf, void *(*func)(void *)) { timeout_start(); if (card_locked_itf != itf || card_locked_func != func) { if (card_locked_itf != ITF_TOTAL || card_locked_func != NULL) { diff --git a/src/usb/usb.h b/src/usb/usb.h index 9f87c9c..8361b4c 100644 --- a/src/usb/usb.h +++ b/src/usb/usb.h @@ -47,7 +47,7 @@ #define EV_BUTTON_TIMEOUT 16 #define EV_BUTTON_PRESSED 32 -static const uint8_t ITF_INVALID = 0xFF; +enum { ITF_INVALID = 0xFF }; #ifdef USB_ITF_HID extern uint8_t ITF_HID_CTAP, ITF_HID_KB; @@ -77,7 +77,7 @@ extern void usb_task(); extern queue_t usb_to_card_q; extern queue_t card_to_usb_q; -extern void card_start(uint8_t, void (*func)(void)); +extern void card_start(uint8_t, void *(*func)(void *)); extern void card_exit(); extern int card_status(uint8_t itf); extern void usb_init(); diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index 67f52c8..d4c8d4a 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -167,13 +167,13 @@ void usb_desc_setup() { #ifdef USB_ITF_HID if (ITF_HID != ITF_INVALID) { TUSB_DESC_TOTAL_LEN += TUD_HID_INOUT_DESC_LEN; - const uint8_t desc[] = { TUD_HID_INOUT_DESCRIPTOR(ITF_HID, ITF_HID + 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, TUSB_DIR_IN_MASK | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10) }; + const uint8_t desc[] = { TUD_HID_INOUT_DESCRIPTOR(ITF_HID, ITF_HID + 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, (uint8_t)TUSB_DIR_IN_MASK | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10) }; memcpy(p, desc, sizeof(desc)); p += sizeof(desc); } if (ITF_KEYBOARD != ITF_INVALID) { TUSB_DESC_TOTAL_LEN += TUD_HID_DESC_LEN; - const uint8_t desc_kb[] = { TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD + 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report_kb), TUSB_DIR_IN_MASK | EPNUM_HID_KB, 16, 5) }; + const uint8_t desc_kb[] = { TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD + 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report_kb), (uint8_t)TUSB_DIR_IN_MASK | EPNUM_HID_KB, 16, 5) }; memcpy(p, desc_kb, sizeof(desc_kb)); p += sizeof(desc_kb); } @@ -373,8 +373,8 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { uint8_t buff_avail = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; if (index >= 4) { const char *product = phy_data.usb_product_present ? phy_data.usb_product : string_desc_arr[2]; - uint8_t len = MIN(strlen(product), buff_avail); - for (int ix = 0; ix < len; chr_count++, ix++) { + uint8_t len = (uint8_t)MIN(strlen(product), buff_avail); + for (size_t ix = 0; ix < len; chr_count++, ix++) { _desc_str[1 + chr_count] = product[ix]; } buff_avail -= len; @@ -383,7 +383,7 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { buff_avail--; } } - for (int ix = 0; ix < MIN(strlen(str), buff_avail); chr_count++, ix++) { + for (size_t ix = 0; ix < MIN(strlen(str), buff_avail); chr_count++, ix++) { _desc_str[1 + chr_count] = str[ix]; } }