From 65fea84df165b14a4657cc87e43aeac637dea89e Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 23 Aug 2024 13:17:10 +0200 Subject: [PATCH] Fix warnings. Signed-off-by: Pol Henarejos --- src/apdu.c | 4 ++-- src/crypto_utils.c | 18 ++++-------------- src/crypto_utils.h | 18 ++++-------------- src/fs/low_flash.c | 9 ++++----- src/main.c | 2 +- src/usb/ccid/ccid.c | 3 ++- src/usb/hid/ctap_hid.h | 16 ++++++---------- src/usb/hid/hid.c | 19 ++++++++++--------- src/usb/usb.c | 14 ++++---------- src/usb/usb.h | 15 ++++++++++----- src/usb/usb_descriptors.h | 3 ++- 11 files changed, 49 insertions(+), 72 deletions(-) diff --git a/src/apdu.c b/src/apdu.c index 9c1dda5..6b9ea03 100644 --- a/src/apdu.c +++ b/src/apdu.c @@ -48,7 +48,7 @@ int process_apdu() { if (is_chaining) { memmove(apdu.data + (chain_ptr - chain_buf), apdu.data, apdu.nc); memcpy(apdu.data, chain_buf, chain_ptr - chain_buf); - apdu.nc += chain_ptr - chain_buf; + apdu.nc += (uint16_t)(chain_ptr - chain_buf); is_chaining = false; } } @@ -180,7 +180,7 @@ uint16_t set_res_sw(uint8_t sw1, uint8_t sw2) { } #ifndef ENABLE_EMULATION -void apdu_thread() { +void apdu_thread(void) { card_init_core1(); while (1) { uint32_t m = 0; diff --git a/src/crypto_utils.c b/src/crypto_utils.c index f98d7ef..bafdeea 100644 --- a/src/crypto_utils.c +++ b/src/crypto_utils.c @@ -71,12 +71,7 @@ void generic_hash(mbedtls_md_type_t md, const uint8_t *input, size_t len, uint8_ mbedtls_md(mbedtls_md_info_from_type(md), input, len, output); } -int aes_encrypt(const uint8_t *key, - const uint8_t *iv, - int key_size, - int mode, - uint8_t *data, - int len) { +int aes_encrypt(const uint8_t *key, const uint8_t *iv, uint16_t key_size, int mode, uint8_t *data, uint16_t len) { mbedtls_aes_context aes; mbedtls_aes_init(&aes); uint8_t tmp_iv[IV_SIZE]; @@ -95,12 +90,7 @@ int aes_encrypt(const uint8_t *key, return mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, len, &iv_offset, tmp_iv, data, data); } -int aes_decrypt(const uint8_t *key, - const uint8_t *iv, - int key_size, - int mode, - uint8_t *data, - int len) { +int aes_decrypt(const uint8_t *key, const uint8_t *iv, uint16_t key_size, int mode, uint8_t *data, uint16_t len) { mbedtls_aes_context aes; mbedtls_aes_init(&aes); uint8_t tmp_iv[IV_SIZE]; @@ -120,10 +110,10 @@ int aes_decrypt(const uint8_t *key, return mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_DECRYPT, len, &iv_offset, tmp_iv, data, data); } -int aes_encrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len) { +int aes_encrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, uint16_t len) { return aes_encrypt(key, iv, 256, PICO_KEYS_AES_MODE_CFB, data, len); } -int aes_decrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len) { +int aes_decrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, uint16_t len) { return aes_decrypt(key, iv, 256, PICO_KEYS_AES_MODE_CFB, data, len); } diff --git a/src/crypto_utils.h b/src/crypto_utils.h index 9c729d5..413065f 100644 --- a/src/crypto_utils.h +++ b/src/crypto_utils.h @@ -42,20 +42,10 @@ extern void double_hash_pin(const uint8_t *pin, uint16_t len, uint8_t output[32] extern void hash_multi(const uint8_t *input, uint16_t len, uint8_t output[32]); extern void hash256(const uint8_t *input, size_t len, uint8_t output[32]); extern void generic_hash(mbedtls_md_type_t md, const uint8_t *input, size_t len, uint8_t *output); -extern int aes_encrypt(const uint8_t *key, - const uint8_t *iv, - int key_size, - int mode, - uint8_t *data, - int len); -extern int aes_decrypt(const uint8_t *key, - const uint8_t *iv, - int key_size, - int mode, - uint8_t *data, - int len); -extern int aes_encrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len); -extern int aes_decrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len); +extern int aes_encrypt(const uint8_t *key, const uint8_t *iv, uint16_t key_size, int mode, uint8_t *data, uint16_t len); +extern int aes_decrypt(const uint8_t *key, const uint8_t *iv, uint16_t key_size, int mode, uint8_t *data, uint16_t len); +extern int aes_encrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, uint16_t len); +extern int aes_decrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, uint16_t len); extern mbedtls_ecp_group_id ec_get_curve_from_prime(const uint8_t *prime, size_t prime_len); #endif diff --git a/src/fs/low_flash.c b/src/fs/low_flash.c index 8025d63..16d1714 100644 --- a/src/fs/low_flash.c +++ b/src/fs/low_flash.c @@ -32,11 +32,13 @@ #include #include #define O_RDWR _O_RDWR +#define O_CREAT _O_CREAT #define open _open #define write _write #define mode_t unsigned short #define lseek _lseek #include "mman.h" +#include #else #ifdef ESP_PLATFORM #include "esp_compat.h" @@ -50,14 +52,13 @@ const esp_partition_t *part0; #include #include #endif -#include +#endif #define FLASH_SECTOR_SIZE 4096 #define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024) #define XIP_BASE 0 int fd_map = 0; uint8_t *map = NULL; #endif -#endif #include "pico_keys.h" #include @@ -109,9 +110,7 @@ void do_flash() { //printf("WRITTING %X\n",flash_pages[r].address-XIP_BASE); uint32_t ints = save_and_disable_interrupts(); flash_range_erase(flash_pages[r].address - XIP_BASE, FLASH_SECTOR_SIZE); - flash_range_program(flash_pages[r].address - XIP_BASE, - flash_pages[r].page, - FLASH_SECTOR_SIZE); + flash_range_program(flash_pages[r].address - XIP_BASE, flash_pages[r].page, FLASH_SECTOR_SIZE); restore_interrupts(ints); while (multicore_lockout_end_timeout_us(1000) == false) { ; diff --git a/src/main.c b/src/main.c index 1ac1a3f..611b8c7 100644 --- a/src/main.c +++ b/src/main.c @@ -283,7 +283,7 @@ void led_blinking_task() { } #elif defined(CYW43_WL_GPIO_LED_PIN) cyw43_arch_gpio_put(led_color, led_state); -#elif ESP_PLATFORM +#elif defined(ESP_PLATFORM) neopixel_SetPixel(neopixel, &pixel[led_state], 1); #endif led_state ^= 1; // toggle diff --git a/src/usb/ccid/ccid.c b/src/usb/ccid/ccid.c index d3ab2a5..e73ca65 100644 --- a/src/usb/ccid/ccid.c +++ b/src/usb/ccid/ccid.c @@ -77,6 +77,7 @@ static portMUX_TYPE mutex = portMUX_INITIALIZER_UNLOCKED; #define CCID_THREAD_TERMINATED 0xffff #define CCID_ACK_TIMEOUT 0x6600 +PACK( typedef struct { uint8_t bMessageType; uint32_t dwLength; @@ -85,7 +86,7 @@ typedef struct { uint8_t abRFU0; uint16_t abRFU1; uint8_t apdu; //Actually it is an array -} __attribute__((__packed__)) ccid_header_t; +}) ccid_header_t; uint8_t ccid_status = 1; static uint8_t itf_num; diff --git a/src/usb/hid/ctap_hid.h b/src/usb/hid/ctap_hid.h index 0d662d1..4c25846 100644 --- a/src/usb/hid/ctap_hid.h +++ b/src/usb/hid/ctap_hid.h @@ -18,16 +18,9 @@ #ifndef _CTAP_HID_H_ #define _CTAP_HID_H_ -#ifdef _MSC_VER // Windows -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned int uint32_t; -typedef unsigned long int uint64_t; -#else #include #include #include -#endif #ifdef __cplusplus extern "C" { @@ -47,6 +40,7 @@ extern "C" { #define TYPE_INIT 0x80 // Initial frame identifier #define TYPE_CONT 0x00 // Continuation frame identifier +PACK( typedef struct { uint32_t cid; // Channel identifier union { @@ -62,7 +56,7 @@ typedef struct { uint8_t data[HID_RPT_SIZE - 5]; // Data payload } cont; }; -} __attribute__((__packed__)) CTAPHID_FRAME; +}) CTAPHID_FRAME; extern CTAPHID_FRAME *ctap_req, *ctap_resp; @@ -120,10 +114,12 @@ extern CTAPHID_FRAME *ctap_req, *ctap_resp; #define CAPFLAG_WINK 0x01 // Device supports WINK command #define CAPFLAG_CBOR 0x04 // Device supports CBOR command +PACK( typedef struct { uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce -} __attribute__((__packed__)) CTAPHID_INIT_REQ; +}) CTAPHID_INIT_REQ; +PACK( typedef struct { uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce uint32_t cid; // Channel identifier @@ -132,7 +128,7 @@ typedef struct { uint8_t versionMinor; // Minor version number uint8_t versionBuild; // Build version number uint8_t capFlags; // Capabilities flags -} __attribute__((__packed__)) CTAPHID_INIT_RESP; +}) CTAPHID_INIT_RESP; // CTAPHID_SYNC command defines diff --git a/src/usb/hid/hid.c b/src/usb/hid/hid.c index 5a06113..08c6c70 100644 --- a/src/usb/hid/hid.c +++ b/src/usb/hid/hid.c @@ -37,11 +37,12 @@ uint8_t (*get_version_minor)() = NULL; static usb_buffer_t hid_rx[ITF_HID_TOTAL] = {0}, hid_tx[ITF_HID_TOTAL] = {0}; +PACK( typedef struct msg_packet { uint16_t len; uint16_t current_len; uint8_t data[CTAP_MAX_PACKET_SIZE]; -} __attribute__((__packed__)) msg_packet_t; +}) msg_packet_t; msg_packet_t msg_packet = { 0 }; @@ -247,11 +248,11 @@ void tud_hid_report_complete_cb(uint8_t instance, uint8_t const *report, uint16_ #ifdef ESP_PLATFORM taskENTER_CRITICAL(&mutex); #endif - CTAPHID_FRAME *ctap_req = (CTAPHID_FRAME *) report; + CTAPHID_FRAME *req = (CTAPHID_FRAME *) report; if (last_write_result[instance] == WRITE_PENDING) { last_write_result[instance] = WRITE_SUCCESS; - if (FRAME_TYPE(ctap_req) == TYPE_INIT) { - if (ctap_req->init.cmd != CTAPHID_KEEPALIVE) { + if (FRAME_TYPE(req) == TYPE_INIT) { + if (req->init.cmd != CTAPHID_KEEPALIVE) { send_buffer_size[instance] -= MIN(64 - 7, send_buffer_size[instance]); } } @@ -260,11 +261,11 @@ void tud_hid_report_complete_cb(uint8_t instance, uint8_t const *report, uint16_ } } if (last_write_result[instance] == WRITE_SUCCESS) { - if (FRAME_TYPE(ctap_req) != TYPE_INIT || ctap_req->init.cmd != CTAPHID_KEEPALIVE) { + if (FRAME_TYPE(req) != TYPE_INIT || req->init.cmd != CTAPHID_KEEPALIVE) { if (send_buffer_size[instance] > 0) { ctap_resp = (CTAPHID_FRAME *) ((uint8_t *) ctap_resp + 64 - 5); - uint8_t seq = FRAME_TYPE(ctap_req) == TYPE_INIT ? 0 : FRAME_SEQ(ctap_req) + 1; - ctap_resp->cid = ctap_req->cid; + uint8_t seq = FRAME_TYPE(req) == TYPE_INIT ? 0 : FRAME_SEQ(req) + 1; + ctap_resp->cid = req->cid; ctap_resp->cont.seq = seq; hid_tx[ITF_HID_CTAP].r_ptr += 64 - 5; @@ -352,7 +353,7 @@ int driver_process_usb_nopacket_hid() { } extern const uint8_t fido_aid[], u2f_aid[]; -extern void apdu_thread(), cbor_thread(); +extern void apdu_thread(void), cbor_thread(void); int driver_process_usb_packet_hid(uint16_t read) { int apdu_sent = 0; @@ -552,7 +553,7 @@ int driver_process_usb_packet_hid(uint16_t read) { msg_packet.len = msg_packet.current_len = 0; last_packet_time = 0; if (apdu_sent < 0) { - return ctap_error(-apdu_sent); + return ctap_error((uint8_t)(-apdu_sent)); } send_keepalive(); } diff --git a/src/usb/usb.c b/src/usb/usb.c index 5a5d90a..577609e 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -87,7 +87,9 @@ bool is_busy() { } void usb_send_event(uint32_t flag) { +#if !defined(ENABLE_EMULATION) queue_add_blocking(&usb_to_card_q, &flag); +#endif if (flag == EV_CMD_AVAILABLE) { timeout_start(); } @@ -183,16 +185,8 @@ int card_status(uint8_t itf) { } } } +#else + (void) itf; #endif return CCID_ERR_FILE_NOT_FOUND; } - -uint8_t *usb_prepare_response(uint8_t itf) { -#ifndef ENABLE_EMULATION -#ifdef USB_ITF_CCID -#endif - return NULL; -#else - return driver_prepare_response_emul(itf); -#endif -} diff --git a/src/usb/usb.h b/src/usb/usb.h index e59ddf3..f2e2d42 100644 --- a/src/usb/usb.h +++ b/src/usb/usb.h @@ -26,8 +26,7 @@ #include "pico/util/queue.h" #endif - -#include "esp_compat.h" +#include "compat.h" /* USB thread */ #define EV_CARD_CHANGE 1 @@ -108,12 +107,18 @@ extern void driver_exec_finished_ccid(uint8_t itf, uint16_t size_next); extern void driver_exec_finished_cont_ccid(uint8_t itf, uint16_t size_next, uint16_t offset); #endif -#define USB_BUFFER_SIZE 2048 // Size of USB buffer +#ifdef ENABLE_EMULATION +extern void driver_exec_finished_emul(uint8_t itf, uint16_t size_next); +extern void driver_exec_finished_cont_emul(uint8_t itf, uint16_t size_next, uint16_t offset); +#endif +#define USB_BUFFER_SIZE 2048 // Size of USB buffer" +PACK( typedef struct { uint8_t buffer[USB_BUFFER_SIZE]; - uint16_t r_ptr, w_ptr; -} __attribute__((__packed__)) usb_buffer_t; + uint16_t r_ptr; + uint16_t w_ptr; +}) usb_buffer_t; typedef enum { WRITE_UNKNOWN = 0, diff --git a/src/usb/usb_descriptors.h b/src/usb/usb_descriptors.h index a3d4a1d..f89d7ec 100644 --- a/src/usb/usb_descriptors.h +++ b/src/usb/usb_descriptors.h @@ -18,6 +18,7 @@ #ifndef USB_DESCRIPTORS_H_ #define USB_DESCRIPTORS_H_ +PACK( struct ccid_class_descriptor { uint8_t bLength; uint8_t bDescriptorType; @@ -41,6 +42,6 @@ struct ccid_class_descriptor { uint16_t wLcdLayout; uint8_t bPINSupport; uint8_t bMaxCCIDBusySlots; -} __attribute__((__packed__)); +}); #endif /* USB_DESCRIPTORS_H_ */