diff --git a/CMakeLists.txt b/CMakeLists.txt index 30b9be0..1a81050 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,16 @@ # along with this program. If not, see . # - cmake_minimum_required(VERSION 3.13) + cmake_minimum_required(VERSION 3.16) + +if(ESP_PLATFORM) +set(EXTRA_COMPONENT_DIRS src) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +set(USB_ITF_CCID 1) +set(USB_ITF_HID 1) +include(pico_keys_sdk_import.cmake) +project(pico_keys_sdk) +else() if(ENABLE_EMULATION) else() @@ -28,7 +37,7 @@ set(CMAKE_CXX_STANDARD 17) if(ENABLE_EMULATION) - else() + else() pico_sdk_init() endif() @@ -70,3 +79,4 @@ pico_add_extra_outputs(pico_keys_sdk_exe) target_link_libraries(pico_keys_sdk_exe PRIVATE pico_keys_sdk pico_stdlib pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id hardware_rtc tinyusb_device tinyusb_board) endif() +endif() diff --git a/partitions.csv b/partitions.csv new file mode 100755 index 0000000..1c79321 --- /dev/null +++ b/partitions.csv @@ -0,0 +1,6 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap +nvs, data, nvs, 0x9000, 0x6000, +phy_init, data, phy, 0xf000, 0x1000, +factory, app, factory, 0x10000, 1M, +storage, data, fat, , 1M, diff --git a/pico_keys_sdk_import.cmake b/pico_keys_sdk_import.cmake index b3ec23d..0819921 100644 --- a/pico_keys_sdk_import.cmake +++ b/pico_keys_sdk_import.cmake @@ -84,7 +84,9 @@ if(USB_ITF_CCID) message(STATUS "USB CCID Interface:\t\t enabled") endif(USB_ITF_CCID) add_definitions(-DDEBUG_APDU=${DEBUG_APDU}) +if (NOT ESP_PLATFORM) add_definitions(-DMBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/config/mbedtls_config.h") +endif() message(STATUS "USB VID/PID: ${USB_VID}:${USB_PID}") @@ -263,13 +265,16 @@ if (MSVC) COMPILE_FLAGS " -W3 -wd4242 -wd4065" ) endif() +set(INTERNAL_SOURCES ${SOURCES}) +set(SOURCES ${SOURCES} ${EXTERNAL_SOURCES}) if (NOT TARGET pico_keys_sdk) if (ENABLE_EMULATION) add_impl_library(pico_keys_sdk) + elseif(ESP_PLATFORM) + add_impl_library(pico_keys_sdk) else() pico_add_library(pico_keys_sdk) endif() - set(SOURCES ${SOURCES} ${EXTERNAL_SOURCES}) target_sources(pico_keys_sdk INTERFACE ${SOURCES} ) diff --git a/sdkconfig.defaults b/sdkconfig.defaults new file mode 100755 index 0000000..12e3b2f --- /dev/null +++ b/sdkconfig.defaults @@ -0,0 +1,16 @@ +# This file was generated using idf.py save-defconfig. It can be edited manually. +# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration +# +CONFIG_TINYUSB=y +CONFIG_TINYUSB_MSC_ENABLED=y + +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +CONFIG_WL_SECTOR_SIZE_512=y +CONFIG_WL_SECTOR_MODE_PERF=y + +CONFIG_TINYUSB_CDC_ENABLED=y +# CONFIG_TINYUSB_DESC_USE_DEFAULT_PID is not set +CONFIG_TINYUSB_DESC_CUSTOM_PID=0x4001 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100755 index 0000000..9e427f1 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register( + SRCS ${INTERNAL_SOURCES} + INCLUDE_DIRS . fs rng usb + PRIV_REQUIRES bootloader_support esp_partition +) diff --git a/src/apdu.c b/src/apdu.c index 7ac3a81..999bc20 100644 --- a/src/apdu.c +++ b/src/apdu.c @@ -196,7 +196,10 @@ void apdu_thread() { card_init_core1(); while (1) { uint32_t m = 0; +#if defined(ESP_PLATFORM) +#else queue_remove_blocking(&usb_to_card_q, &m); +#endif if (m == EV_VERIFY_CMD_AVAILABLE || m == EV_MODIFY_CMD_AVAILABLE) { set_res_sw(0x6f, 0x00); @@ -213,7 +216,10 @@ done: ; finished_data_size = apdu_next(); uint32_t flag = EV_EXEC_FINISHED; +#if defined(ESP_PLATFORM) +#else queue_add_blocking(&card_to_usb_q, &flag); +#endif } //printf("EXIT !!!!!!\r\n"); if (current_app && current_app->unload) { diff --git a/src/apdu.h b/src/apdu.h index e7ae1c8..54260b1 100644 --- a/src/apdu.h +++ b/src/apdu.h @@ -19,7 +19,7 @@ #define _APDU_H_ #include -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #endif #include "compat.h" diff --git a/src/asn1.h b/src/asn1.h index 3d4c5e1..ae2e471 100644 --- a/src/asn1.h +++ b/src/asn1.h @@ -19,7 +19,7 @@ #define _ASN1_H_ #include -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #else #include diff --git a/src/esp_compat.h b/src/esp_compat.h new file mode 100644 index 0000000..dba3c20 --- /dev/null +++ b/src/esp_compat.h @@ -0,0 +1,67 @@ +/* + * This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk). + * Copyright (c) 2022 Pol Henarejos. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __ESP_COMPAT_H_ +#define __ESP_COMPAT_H_ + +#ifdef ESP_PLATFORM + +#include "freertos/freertos.h" +#include "freertos/queue.h" +typedef QueueHandle_t queue_t; +#define queue_init(a,b,c) do { *(a) = xQueueCreate(c, b); } while(0) +#define queue_add_blocking(a,b) xQueueSend(*(a), b, portMAX_DELAY) +#define queue_try_add(a,b) xQueueSend(*(a), b, 0) +#define queue_is_empty(a) (uxQueueMessagesWaiting(*(a)) == 0) +#define queue_try_remove(a,b) xQueueReceive(*(a), b, 0) +extern TaskHandle_t hcore0, hcore1; +#define multicore_launch_core1(a) xTaskCreate(a, "core1", 512, NULL, tskIDLE_PRIORITY, &hcore1) +#define multicore_reset_core1 vTaskEndScheduler +#define sleep_ms(a) vTaskDelay(a / portTICK_PERIOD_MS) +#define board_millis xTaskGetTickCount +#define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8 +typedef struct { uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES]; } pico_unique_board_id_t; +#define pico_get_unique_board_id(a) memset(a, 0, sizeof(pico_unique_board_id_t)) +#define pico_get_unique_board_id_string(a, b) \ + do { \ + pico_unique_board_id_t t; \ + pico_get_unique_board_id(&t); \ + snprintf(a, b, "%02X%02X%02X%02X%02X%02X%02X%02X", t.id[0], t.id[1], t.id[2], t.id[3], t.id[4], t.id[5], t.id[6], t.id[7]); } while(0) +typedef SemaphoreHandle_t mutex_t; +typedef SemaphoreHandle_t semaphore_t; +#define mutex_init(a) do { *(a) = xSemaphoreCreateMutex();} while(0) +#define mutex_try_enter(a,b) xSemaphoreTake(*(a), 0) +#define mutex_enter_blocking(a) xSemaphoreTake(*(a), portMAX_DELAY) +#define mutex_exit(a) xSemaphoreGive(*(a)) +#define sem_init(a,b,c) do { *(a) = xSemaphoreCreateCounting(c, b); } while(0) +#define sem_release(a) xSemaphoreGive(*(a)) +#define sem_acquire_blocking(a) xSemaphoreTake(*(a), portMAX_DELAY) +#define multicore_lockout_victim_init() (void)0 +static inline bool multicore_lockout_start_timeout_us(int a) { + vTaskSuspend(hcore1); return true; } +static inline bool multicore_lockout_end_timeout_us(int a) { + vTaskResume(hcore1); return true; } +#define save_and_disable_interrupts() 1 + +#include "esp_partition.h" +extern esp_partition_t part0; +#define flash_range_erase(a,b) esp_partition_erase_range(&part0, a, b) +#define flash_range_program(a,b,c) esp_partition_write(&part0, a, b, c) +#define restore_interrupts(a) (void)a +#endif + +#endif diff --git a/src/fs/file.h b/src/fs/file.h index 157e022..031d0ba 100644 --- a/src/fs/file.h +++ b/src/fs/file.h @@ -20,7 +20,7 @@ #define _FILE_H_ #include -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #else #include diff --git a/src/fs/flash.c b/src/fs/flash.c index c94a35e..8d83a7e 100644 --- a/src/fs/flash.c +++ b/src/fs/flash.c @@ -19,13 +19,13 @@ #include #include -#ifndef ENABLE_EMULATION -#include "pico/stdlib.h" -#include "hardware/flash.h" -#else +#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM) #define XIP_BASE 0 #define FLASH_SECTOR_SIZE 4096 #define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024) +#else +#include "pico/stdlib.h" +#include "hardware/flash.h" #endif #include "pico_keys.h" #include "file.h" diff --git a/src/fs/low_flash.c b/src/fs/low_flash.c index bb2cf5c..2383678 100644 --- a/src/fs/low_flash.c +++ b/src/fs/low_flash.c @@ -20,7 +20,7 @@ #include #include -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #include "hardware/flash.h" #include "hardware/sync.h" @@ -38,15 +38,19 @@ #define lseek _lseek #include "mman.h" #else +#ifdef ESP_PLATFORM +#else #include #include #endif #include #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 diff --git a/src/idf_component.yml b/src/idf_component.yml new file mode 100755 index 0000000..06b047d --- /dev/null +++ b/src/idf_component.yml @@ -0,0 +1,4 @@ +## IDF Component Manager Manifest File +dependencies: + espressif/esp_tinyusb: "^1.2" + idf: "^5.0" diff --git a/src/main.c b/src/main.c index f7fc085..269b30c 100644 --- a/src/main.c +++ b/src/main.c @@ -18,13 +18,15 @@ #include // Pico -#ifndef ENABLE_EMULATION -#include "pico/stdlib.h" -#else + +#if defined(ENABLE_EMULATION) #if !defined(_MSC_VER) #include #endif #include "emulation.h" +#elif defined(ESP_PLATFORM) +#else +#include "pico/stdlib.h" #endif // For memcpy @@ -170,7 +172,7 @@ int gettimeofday(struct timeval* tp, struct timezone* tzp) (void)tzp; // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC) - // until 00:00:00 January 1, 1970 + // until 00:00:00 January 1, 1970 static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL); SYSTEMTIME system_time; @@ -313,7 +315,40 @@ void execute_tasks() { led_blinking_task(); } +void core0_loop() { + while (1) { + execute_tasks(); + neug_task(); + do_flash(); +#ifndef ENABLE_EMULATION + if (board_millis() > 1000 && !is_busy()) { // wait 1 second to boot up + bool current_button_state = board_button_read(); + if (current_button_state != button_pressed_state) { + if (current_button_state == false) { // unpressed + if (button_pressed_time == 0 || button_pressed_time + 1000 > board_millis()) { + button_press++; + } + button_pressed_time = board_millis(); + } + button_pressed_state = current_button_state; + } + if (button_pressed_time > 0 && button_press > 0 && button_pressed_time + 1000 < board_millis() && button_pressed_state == false) { + if (button_pressed_cb != NULL) { + (*button_pressed_cb)(button_press); + } + button_pressed_time = button_press = 0; + } + } +#endif + } +} + +#ifdef ESP_PLATFORM +TaskHandle_t hcore0 = NULL, hcore1 = NULL; +void app_main(void) { +#else int main(void) { +#endif #ifndef ENABLE_EMULATION usb_init(); @@ -351,31 +386,12 @@ int main(void) { //ccid_prepare_receive(&ccid); - while (1) { - execute_tasks(); - neug_task(); - do_flash(); -#ifndef ENABLE_EMULATION - if (board_millis() > 1000 && !is_busy()) { // wait 1 second to boot up - bool current_button_state = board_button_read(); - if (current_button_state != button_pressed_state) { - if (current_button_state == false) { // unpressed - if (button_pressed_time == 0 || button_pressed_time + 1000 > board_millis()) { - button_press++; - } - button_pressed_time = board_millis(); - } - button_pressed_state = current_button_state; - } - if (button_pressed_time > 0 && button_press > 0 && button_pressed_time + 1000 < board_millis() && button_pressed_state == false) { - if (button_pressed_cb != NULL) { - (*button_pressed_cb)(button_press); - } - button_pressed_time = button_press = 0; - } - } +#ifdef ESP_PLATFORM + xTaskCreate(core0_loop, "core0", 512, NULL, tskIDLE_PRIORITY, &hcore0); + vTaskStartScheduler(); +#else + core0_loop(); #endif - } return 0; } diff --git a/src/pico_keys.h b/src/pico_keys.h index f9b28d9..850deb1 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -19,7 +19,7 @@ #define _PICO_KEYS_H_ #include "file.h" -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/unique_id.h" #else #include @@ -47,6 +47,14 @@ extern uint32_t board_millis(); #endif #include +#if defined(ENABLE_EMULATION) +#include +#elif defined(ESP_PLATFORM) +#include "esp_compat.h" +#else +#include "pico/util/queue.h" +#endif + extern bool wait_button(); extern void low_flash_init_core1(); diff --git a/src/rng/hwrng.c b/src/rng/hwrng.c index 1f778c2..d57859f 100644 --- a/src/rng/hwrng.c +++ b/src/rng/hwrng.c @@ -18,7 +18,17 @@ #include #include #include -#ifndef ENABLE_EMULATION +#if defined(ENABLE_EMULATION) +#include "mbedtls/entropy.h" +#include "mbedtls/ctr_drbg.h" + +mbedtls_ctr_drbg_context ctr_drbg; +extern uint32_t board_millis(); +#elif (ESP_PLATFORM) +#include "bootloader_random.h" +#include "esp_random.h" +#include "esp_compat.h" +#else #include "pico/stdlib.h" #include "hwrng.h" @@ -28,16 +38,13 @@ #include "bsp/board.h" #include "pico/time.h" -#else -#include "mbedtls/entropy.h" -#include "mbedtls/ctr_drbg.h" - -mbedtls_ctr_drbg_context ctr_drbg; -extern uint32_t board_millis(); #endif void adc_start() { -#ifndef ENABLE_EMULATION +#if defined(ENABLE_EMULATION) +#elif defined(ESP_PLATFORM) + bootloader_random_enable(); +#else adc_init(); adc_gpio_init(27); adc_select_input(1); @@ -46,7 +53,7 @@ void adc_start() { void adc_stop() { } -#ifdef ENABLE_EMULATION +#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM) uint32_t adc_read() { return 0; } @@ -76,7 +83,12 @@ static int ep_process() { ep_init(); } uint64_t word = 0x0; -#ifndef ENABLE_EMULATION + +#if defined(ENABLE_EMULATION) + mbedtls_ctr_drbg_random(&ctr_drbg, (uint8_t *) &word, sizeof(word)); +#elif defined(ESP_PLATFORM) + esp_fill_random((uint8_t *)&word, sizeof(word)); +#else for (int n = 0; n < 64; n++) { uint8_t bit1, bit2; do { @@ -86,8 +98,6 @@ static int ep_process() { } while (bit1 == bit2); word = (word << 1) | bit1; } -#else - mbedtls_ctr_drbg_random(&ctr_drbg, (uint8_t *) &word, sizeof(word)); #endif random_word ^= word ^ board_millis() ^ adc_read(); random_word *= 0x00000100000001B3; @@ -200,7 +210,11 @@ uint32_t neug_get() { void neug_wait_full() { struct rng_rb *rb = &the_ring_buffer; #ifndef ENABLE_EMULATION +#ifdef ESP_PLATFORM + uint8_t core = xTaskGetCurrentTaskHandle() == hcore1 ? 1 : 0; +#else uint core = get_core_num(); +#endif #endif while (!rb->full) { #ifndef ENABLE_EMULATION diff --git a/src/rng/hwrng.h b/src/rng/hwrng.h index 857e4ec..cc5f33f 100644 --- a/src/rng/hwrng.h +++ b/src/rng/hwrng.h @@ -21,7 +21,7 @@ #define NEUG_PRE_LOOP 32 #include -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #endif diff --git a/src/rng/random.c b/src/rng/random.c index 1e097ee..fbbe491 100644 --- a/src/rng/random.c +++ b/src/rng/random.c @@ -65,18 +65,6 @@ void random_bytes_free(const uint8_t *p) { neug_flush(); } -/* - * Return 4-byte salt - */ -void random_get_salt(uint8_t *p) { - uint32_t rnd; - - rnd = neug_get(); - memcpy(p, &rnd, sizeof(uint32_t)); - rnd = neug_get(); - memcpy(p + sizeof(uint32_t), &rnd, sizeof(uint32_t)); -} - /* * Random byte iterator diff --git a/src/rng/random.h b/src/rng/random.h index bb4ad24..b0a5d7b 100644 --- a/src/rng/random.h +++ b/src/rng/random.h @@ -32,9 +32,6 @@ void random_fini(void); const uint8_t *random_bytes_get(size_t); void random_bytes_free(const uint8_t *p); -/* 8-byte salt */ -void random_get_salt(uint8_t *p); - /* iterator returning a byta at a time */ extern int random_gen(void *arg, unsigned char *output, size_t output_len); diff --git a/src/usb/hid/hid.c b/src/usb/hid/hid.c index 016a76b..6ae835e 100644 --- a/src/usb/hid/hid.c +++ b/src/usb/hid/hid.c @@ -17,8 +17,10 @@ #ifndef ENABLE_EMULATION #include "tusb.h" +#ifndef ESP_PLATFORM #include "bsp/board.h" #endif +#endif #include "ctap_hid.h" #include "pico_keys.h" #include "pico_keys_version.h" diff --git a/src/usb/tusb_config.h b/src/usb/tusb_config.h index 64f419c..6422571 100644 --- a/src/usb/tusb_config.h +++ b/src/usb/tusb_config.h @@ -64,7 +64,7 @@ extern "C" { #endif #ifndef CFG_TUSB_OS -#define CFG_TUSB_OS OPT_OS_PICO +//#define CFG_TUSB_OS OPT_OS_PICO #endif #ifndef CFG_TUSB_DEBUG diff --git a/src/usb/usb.c b/src/usb/usb.c index 94155ed..2a67451 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -18,7 +18,7 @@ #include // Pico -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #include "pico/multicore.h" #include "tusb.h" @@ -153,7 +153,7 @@ void usb_clear_rx(uint8_t itf) { #define USB_BCD 0x0200 -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) queue_t usb_to_card_q; queue_t card_to_usb_q; #endif diff --git a/src/usb/usb.h b/src/usb/usb.h index 8872468..14883c6 100644 --- a/src/usb/usb.h +++ b/src/usb/usb.h @@ -18,12 +18,17 @@ #ifndef _USB_H_ #define _USB_H_ -#ifndef ENABLE_EMULATION -#include "pico/util/queue.h" -#else +#if defined(ENABLE_EMULATION) #include +#elif defined(ESP_PLATFORM) +#include "esp_compat.h" +#else +#include "pico/util/queue.h" #endif + +#include "esp_compat.h" + /* USB thread */ #define EV_CARD_CHANGE 1 #define EV_TX_FINISHED 2 diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index 0d01607..5b0d5ed 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -17,7 +17,9 @@ #include "tusb.h" #include "usb_descriptors.h" +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/unique_id.h" +#endif #include "pico_keys_version.h" #include "usb.h"