Merge branch 'esp32'

This commit is contained in:
Pol Henarejos 2024-06-19 22:01:46 +02:00
commit 59597a0a68
No known key found for this signature in database
GPG key ID: C0095B7870A4CCD3
29 changed files with 487 additions and 378 deletions

View file

@ -15,7 +15,19 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
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)
set(USB_VID 0x20a0)
set(USB_PID 0x4230)
set(DEBUG_APDU 1)
include(pico_keys_sdk_import.cmake)
project(pico_keys_sdk)
else()
if(ENABLE_EMULATION)
else()
@ -28,7 +40,7 @@
set(CMAKE_CXX_STANDARD 17)
if(ENABLE_EMULATION)
else()
else()
pico_sdk_init()
endif()
@ -70,3 +82,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()

7
partitions.csv Executable file
View file

@ -0,0 +1,7 @@
# 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,
part0, 0x40, 0x1, 0x200000, 1M,
1 # Name, Type, SubType, Offset, Size, Flags
2 # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3 nvs, data, nvs, 0x9000, 0x6000
4 phy_init, data, phy, 0xf000, 0x1000
5 factory, app, factory, 0x10000, 1M,
6 part0, 0x40, 0x1, 0x200000, 1M,

View file

@ -51,6 +51,15 @@ elseif (VIDPID STREQUAL "GnuPG")
set(USB_PID 0x2440)
endif()
if(ESP_PLATFORM)
if (DEFINED CONFIG_TINYUSB_DESC_CUSTOM_VID)
set(USB_VID CONFIG_TINYUSB_DESC_CUSTOM_VID)
endif()
if(DEFINED CONFIG_TINYUSB_DESC_CUSTOM_PID)
set(USB_PID CONFIG_TINYUSB_DESC_CUSTOM_PID)
endif()
endif()
if (NOT DEFINED USB_VID)
set(USB_VID 0xFEFF)
endif()
@ -82,13 +91,21 @@ endif(USB_ITF_HID)
if(USB_ITF_CCID)
add_definitions(-DUSB_ITF_CCID=1)
message(STATUS "USB CCID Interface:\t\t enabled")
if(USB_ITF_WCID)
add_definitions(-DUSB_ITF_WCID=1)
message(STATUS "USB WebCCID Interface:\t enabled")
endif(USB_ITF_WCID)
endif(USB_ITF_CCID)
add_definitions(-DDEBUG_APDU=${DEBUG_APDU})
add_definitions(-DMBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/config/mbedtls_config.h")
if (NOT ESP_PLATFORM)
add_definitions(-DMBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/config/mbedtls_config.h")
else()
add_definitions(-DCFG_TUSB_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/src/usb/tusb_config.h")
endif()
message(STATUS "USB VID/PID: ${USB_VID}:${USB_PID}")
set(EXTERNAL_SOURCES
set(MBEDTLS_SOURCES
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/aes.c
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/asn1parse.c
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/asn1write.c
@ -159,7 +176,15 @@ set(INCLUDES ${INCLUDES}
)
if(USB_ITF_HID OR ENABLE_EMULATION)
set(EXTERNAL_SOURCES ${EXTERNAL_SOURCES}
set(MBEDTLS_SOURCES ${MBEDTLS_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/x509write_crt.c
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/x509_create.c
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/x509write_csr.c
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/pk.c
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/pk_wrap.c
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/pkwrite.c
)
set(CBOR_SOURCES
${CMAKE_CURRENT_LIST_DIR}/tinycbor/src/cborencoder.c
${CMAKE_CURRENT_LIST_DIR}/tinycbor/src/cborparser.c
${CMAKE_CURRENT_LIST_DIR}/tinycbor/src/cborparser_dup_string.c
@ -212,7 +237,7 @@ if (ENABLE_EMULATION)
set(SOURCES ${SOURCES}
${CMAKE_CURRENT_LIST_DIR}/src/usb/emulation/emulation.c
)
set(EXTERNAL_SOURCES ${EXTERNAL_SOURCES}
set(MBEDTLS_SOURCES ${MBEDTLS_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/ctr_drbg.c
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/entropy.c
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/entropy_poll.c
@ -238,6 +263,10 @@ else()
${CMAKE_CURRENT_LIST_DIR}/src/usb/usb_descriptors.c
)
endif()
set(EXTERNAL_SOURCES ${CBOR_SOURCES})
if(NOT ESP_PLATFORM)
set(EXTERNAL_SOURCES ${EXTERNAL_SOURCES} ${MBEDTLS_SOURCES})
endif()
if (MSVC)
target_compile_options(pico_hsm PUBLIC
-wd4820
@ -263,13 +292,14 @@ 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)
if (ENABLE_EMULATION OR 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}
)

13
sdkconfig.defaults Executable file
View file

@ -0,0 +1,13 @@
# This file was generated using idf.py save-defconfig. It can be edited manually.
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
#
IGNORE_UNKNOWN_FILES_FOR_MANAGED_COMPONENTS=1
CONFIG_TINYUSB=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

5
src/CMakeLists.txt Executable file
View file

@ -0,0 +1,5 @@
idf_component_register(
SRCS ${INTERNAL_SOURCES}
INCLUDE_DIRS . fs rng usb ../mbedtls/include
REQUIRES bootloader_support esp_partition esp_tinyusb efuse
)

View file

@ -19,6 +19,9 @@
#include "pico_keys.h"
#include "usb.h"
#include <stdio.h>
#ifdef ESP_PLATFORM
#include "esp_compat.h"
#endif
uint8_t *rdata_gr = NULL;
uint16_t rdata_bk = 0x0;
@ -196,7 +199,9 @@ void apdu_thread() {
card_init_core1();
while (1) {
uint32_t m = 0;
#ifndef ENABLE_EMULATION
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,13 +218,21 @@ done: ;
finished_data_size = apdu_next();
uint32_t flag = EV_EXEC_FINISHED;
#ifndef ENABLE_EMULATION
queue_add_blocking(&card_to_usb_q, &flag);
#endif
#ifdef ESP_PLATFORM
vTaskDelay(pdMS_TO_TICKS(10));
#endif
}
//printf("EXIT !!!!!!\n");
if (current_app && current_app->unload) {
current_app->unload();
current_app = NULL;
}
#ifdef ESP_PLATFORM
vTaskDelete(NULL);
#endif
}
#endif

View file

@ -19,7 +19,7 @@
#define _APDU_H_
#include <stdlib.h>
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#endif
#include "compat.h"

View file

@ -19,7 +19,7 @@
#define _ASN1_H_
#include <stdlib.h>
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#else
#include <stdint.h>

View file

@ -15,7 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ENABLE_EMULATION
#if defined(ENABLE_EMULATION)
#elif defined(ESP_PLATFORM)
#include "esp_compat.h"
#else
#include <pico/unique_id.h>
#endif
#include "mbedtls/md.h"

View file

@ -18,10 +18,6 @@
#ifndef _CRYPTO_UTILS_H_
#define _CRYPTO_UTILS_H_
#include "stdlib.h"
#ifndef ENABLE_EMULATION
#include "pico/stdlib.h"
#endif
#include "mbedtls/ecp.h"
#include "mbedtls/md.h"

View file

@ -103,10 +103,8 @@ int sm_unwrap() {
if (r != CCID_OK) {
return r;
}
uint16_t le = sm_get_le();
if (le >= 0) {
apdu.ne = le;
}
apdu.ne = sm_get_le();
uint8_t *body = NULL;
uint16_t body_size = 0;
bool is87 = false;
@ -306,12 +304,12 @@ int sm_verify() {
}
uint16_t sm_remove_padding(const uint8_t *data, uint16_t data_len) {
uint16_t i = data_len - 1;
int32_t i = data_len - 1;
for (; i >= 0 && data[i] == 0; i--) {
;
}
if (i < 0 || data[i] != 0x80) {
return 0;
}
return i;
return (uint16_t)i;
}

View file

@ -18,10 +18,6 @@
#ifndef _EAC_H_
#define _EAC_H_
#include <stdlib.h>
#ifndef ENABLE_EMULATION
#include "pico/stdlib.h"
#endif
#include "pico_keys.h"
typedef enum MSE_protocol {

56
src/esp_compat.h Normal file
View file

@ -0,0 +1,56 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#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_remove_blocking(a,b) xQueueReceive(*(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((void(*)(void *))a, "core1", 4096, NULL, CONFIG_TINYUSB_TASK_PRIORITY + 2, &hcore1)
#define multicore_reset_core1() do { if (hcore1) { eTaskState e = eTaskGetState(hcore1); if (e == eRunning) { vTaskDelete(hcore1); }} }while(0)
#define sleep_ms(a) vTaskDelay(a / portTICK_PERIOD_MS)
static inline uint32_t board_millis(void) {
return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ );
}
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; }
#endif
#endif

View file

@ -20,7 +20,7 @@
#define _FILE_H_
#include <stdlib.h>
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#else
#include <stdbool.h>

View file

@ -19,14 +19,18 @@
#include <stdint.h>
#include <string.h>
#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
#ifdef ESP_PLATFORM
#define PICO_FLASH_SIZE_BYTES (1 * 1024 * 1024)
#else
#define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024)
#endif
#else
#include "pico/stdlib.h"
#include "hardware/flash.h"
#endif
#include "pico_keys.h"
#include "file.h"
#include <stdio.h>
@ -38,7 +42,11 @@
* | |
* ------------------------------------------------------
*/
#ifdef ESP_PLATFORM
#define FLASH_TARGET_OFFSET 0
#else
#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES >> 1) // DATA starts at the mid of flash
#endif
#define FLASH_DATA_HEADER_SIZE (sizeof(uintptr_t) + sizeof(uint32_t))
#define FLASH_PERMANENT_REGION (4 * FLASH_SECTOR_SIZE) // 4 sectors (16kb) of permanent memory

View file

@ -20,7 +20,7 @@
#include <stdlib.h>
#include <stdio.h>
#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,26 @@
#define lseek _lseek
#include "mman.h"
#else
#ifdef ESP_PLATFORM
#include "esp_compat.h"
#include "esp_partition.h"
const esp_partition_t *part0;
#define save_and_disable_interrupts() 1
#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
#else
#include <unistd.h>
#include <sys/mman.h>
#endif
#include <fcntl.h>
#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 <string.h>
@ -87,15 +98,15 @@ void do_flash() {
if (mutex_try_enter(&mtx_flash, NULL) == true) {
#endif
if (locked_out == true && flash_available == true && ready_pages > 0) {
//printf(" DO_FLASH AVAILABLE\r\n");
//printf(" DO_FLASH AVAILABLE\n");
for (int r = 0; r < TOTAL_FLASH_PAGES; r++) {
if (flash_pages[r].ready == true) {
#ifndef ENABLE_EMULATION
//printf("WRITTING %X\r\n",flash_pages[r].address-XIP_BASE);
//printf("WRITTING %X\n",flash_pages[r].address-XIP_BASE);
while (multicore_lockout_start_timeout_us(1000) == false) {
;
}
//printf("WRITTING %X\r\n",flash_pages[r].address-XIP_BASE);
//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,
@ -105,7 +116,7 @@ void do_flash() {
while (multicore_lockout_end_timeout_us(1000) == false) {
;
}
//printf("WRITEN %X !\r\n",flash_pages[r].address);
//printf("WRITEN %X !\n",flash_pages[r].address);
#else
memcpy(map + flash_pages[r].address, flash_pages[r].page, FLASH_SECTOR_SIZE);
#endif
@ -117,7 +128,7 @@ void do_flash() {
while (multicore_lockout_start_timeout_us(1000) == false) {
;
}
//printf("WRITTING\r\n");
//printf("WRITTING\n");
flash_range_erase(flash_pages[r].address - XIP_BASE,
flash_pages[r].page_size ? ((int) (flash_pages[r].page_size /
FLASH_SECTOR_SIZE)) *
@ -140,6 +151,10 @@ void do_flash() {
}
}
flash_available = false;
#ifdef ESP_PLATFORM
esp_partition_munmap(fd_map);
esp_partition_mmap(part0, 0, part0->size, ESP_PARTITION_MMAP_DATA, (const void **)&map, (esp_partition_mmap_handle_t *)&fd_map);
#endif
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
}
@ -150,14 +165,18 @@ sem_release(&sem_wait);
//this function has to be called from the core 0
void low_flash_init() {
memset(flash_pages, 0, sizeof(page_flash_t) * TOTAL_FLASH_PAGES);
#ifndef ENABLE_EMULATION
mutex_init(&mtx_flash);
sem_init(&sem_wait, 0, 1);
#else
#if defined(ENABLE_EMULATION)
fd_map = open("memory.flash", O_RDWR | O_CREAT, (mode_t) 0600);
lseek(fd_map, PICO_FLASH_SIZE_BYTES - 1, SEEK_SET);
write(fd_map, "", 1);
map = mmap(0, PICO_FLASH_SIZE_BYTES, PROT_READ | PROT_WRITE, MAP_SHARED, fd_map, 0);
#else
mutex_init(&mtx_flash);
sem_init(&sem_wait, 0, 1);
#if defined(ESP_PLATFORM)
part0 = esp_partition_find_first(0x40, 0x1, "part0");
esp_partition_mmap(part0, 0, part0->size, ESP_PARTITION_MMAP_DATA, (const void **)&map, (esp_partition_mmap_handle_t *)&fd_map);
#endif
#endif
}
@ -198,12 +217,12 @@ page_flash_t *find_free_page(uintptr_t addr) {
flash_pages[r].address == addr_alg) { //first available
p = &flash_pages[r];
if (!flash_pages[r].ready && !flash_pages[r].erase) {
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
memcpy(p->page, (uint8_t *) addr_alg, FLASH_SECTOR_SIZE);
#else
memcpy(p->page,
(addr >= start_data_pool &&
addr <= end_rom_pool) ? (uint8_t *) (map + addr_alg) : (uint8_t *) addr_alg,
addr <= end_rom_pool + sizeof(uintptr_t)) ? (uint8_t *) (map + addr_alg) : (uint8_t *) addr_alg,
FLASH_SECTOR_SIZE);
#endif
ready_pages++;
@ -230,18 +249,18 @@ int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len) {
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
printf("ERROR: ALL FLASH PAGES CACHED\r\n");
printf("ERROR: ALL FLASH PAGES CACHED\n");
return CCID_ERR_NO_MEMORY;
}
if (!(p = find_free_page(addr))) {
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
printf("ERROR: FLASH CANNOT FIND A PAGE (rare error)\r\n");
printf("ERROR: FLASH CANNOT FIND A PAGE (rare error)\n");
return CCID_ERR_MEMORY_FATAL;
}
memcpy(&p->page[addr & (FLASH_SECTOR_SIZE - 1)], data, len);
//printf("Flash: modified page %X with data %x at [%x] (top page %X)\r\n",addr_alg,data,addr&(FLASH_SECTOR_SIZE-1),addr);
//printf("Flash: modified page %X with data %x at [%x]\n",(uintptr_t)addr,(uintptr_t)data,addr&(FLASH_SECTOR_SIZE-1));
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
@ -279,8 +298,9 @@ uint8_t *flash_read(uintptr_t addr) {
uint8_t *v = (uint8_t *) addr;
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#else
if (addr >= start_data_pool && addr <= end_rom_pool) {
#endif
#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM)
if (addr >= start_data_pool && addr <= end_rom_pool + sizeof(uintptr_t)) {
v += (uintptr_t) map;
}
#endif
@ -317,11 +337,11 @@ int flash_erase_page(uintptr_t addr, size_t page_size) {
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
printf("ERROR: ALL FLASH PAGES CACHED\r\n");
printf("ERROR: ALL FLASH PAGES CACHED\n");
return CCID_ERR_NO_MEMORY;
}
if (!(p = find_free_page(addr))) {
printf("ERROR: FLASH CANNOT FIND A PAGE (rare error)\r\n");
printf("ERROR: FLASH CANNOT FIND A PAGE (rare error)\n");
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif

5
src/idf_component.yml Executable file
View file

@ -0,0 +1,5 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp_tinyusb: "^1.4.4"
#espressif/tinyusb: "^0.15.0"
zorxx/neopixel: "^1.0.4"

View file

@ -16,33 +16,19 @@
*/
#include <stdio.h>
#include <stdlib.h>
// Pico
#ifndef ENABLE_EMULATION
#include "pico/stdlib.h"
#else
#if defined(ENABLE_EMULATION)
#if !defined(_MSC_VER)
#include <sys/time.h>
#endif
#include "emulation.h"
#endif
// For memcpy
#include <string.h>
#ifndef ENABLE_EMULATION
// Include descriptor struct definitions
//#include "usb_common.h"
// USB register definitions from pico-sdk
#include "hardware/regs/usb.h"
// USB hardware struct definitions from pico-sdk
#include "hardware/structs/usb.h"
// For interrupt enable and numbers
#include "hardware/irq.h"
// For resetting the USB controller
#include "hardware/resets.h"
#include "pico/multicore.h"
#elif defined(ESP_PLATFORM)
#include "tusb.h"
#else
#include "pico/stdlib.h"
#endif
#include "random.h"
@ -99,11 +85,14 @@ static inline void ws2812_program_init(PIO pio,
}
#endif
#ifndef ENABLE_EMULATION
#if defined(ENABLE_EMULATION)
#else
#include "usb.h"
#ifndef ESP_PLATFORM
#include "hardware/rtc.h"
#include "bsp/board.h"
#endif
#endif
extern void do_flash();
extern void low_flash_init();
@ -194,6 +183,11 @@ uint32_t board_millis() {
}
#else
#ifdef ESP_PLATFORM
bool board_button_read() {
return true;
}
#endif
bool button_pressed_state = false;
uint32_t button_pressed_time = 0;
uint8_t button_press = 0;
@ -229,6 +223,22 @@ bool wait_button() {
struct apdu apdu;
#ifdef ESP_PLATFORM
#include "driver/gpio.h"
#include "neopixel.h"
tNeopixelContext neopixel = NULL;
tNeopixel pixel[] =
{
{ 0, NP_RGB(0, 0, 0) }, /* off */
{ 0, NP_RGB(255, 0, 255) }, /* magenta */
{ 0, NP_RGB(255, 0, 0) }, /* green */
{ 0, NP_RGB(0, 255, 0) }, /* red */
{ 0, NP_RGB(0, 0, 255) }, /* red */
{ 0, NP_RGB(255, 255, 0) }, /* yellow */
{ 0, NP_RGB(0, 255, 255) }, /* cyan */
{ 0, NP_RGB(255, 255, 255) }, /* white */
};
#endif
void led_blinking_task() {
static uint32_t start_ms = 0;
static uint8_t led_state = false;
@ -261,6 +271,8 @@ void led_blinking_task() {
}
#elif defined(CYW43_WL_GPIO_LED_PIN)
cyw43_arch_gpio_put(led_color, led_state);
#elif ESP_PLATFORM
neopixel_SetPixel(neopixel, &pixel[led_state], 1);
#endif
led_state ^= 1; // toggle
}
@ -287,7 +299,9 @@ void led_off_all() {
}
void init_rtc() {
#ifndef ENABLE_EMULATION
#if defined(ENABLE_EMULATION)
#elif defined(ESP_PLATFORM)
#else
rtc_init();
datetime_t dt = {
.year = 2020,
@ -304,64 +318,16 @@ void init_rtc() {
extern void neug_task();
extern void usb_task();
void execute_tasks() {
void execute_tasks()
{
usb_task();
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
tud_task(); // tinyusb device task
#endif
led_blinking_task();
}
char pico_serial_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1];
pico_unique_board_id_t pico_serial;
#ifdef ENABLE_EMULATION
#define pico_get_unique_board_id(a) memset(a, 0, sizeof(*(a)))
#endif
int main(void) {
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++) {
snprintf(&pico_serial_str[2 * i], 3, "%02X", pico_serial.id[i]);
}
#ifndef ENABLE_EMULATION
usb_init();
board_init();
stdio_init_all();
#ifdef PIMORONI_TINY2040
gpio_init(TINY2040_LED_R_PIN);
gpio_set_dir(TINY2040_LED_R_PIN, GPIO_OUT);
gpio_init(TINY2040_LED_G_PIN);
gpio_set_dir(TINY2040_LED_G_PIN, GPIO_OUT);
gpio_init(TINY2040_LED_B_PIN);
gpio_set_dir(TINY2040_LED_B_PIN, GPIO_OUT);
#elif defined(PICO_DEFAULT_LED_PIN)
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
#elif defined(CYW43_WL_GPIO_LED_PIN)
cyw43_arch_init();
#endif
led_off_all();
tusb_init();
//prepare_ccid();
#else
emul_init("127.0.0.1", 35963);
#endif
random_init();
low_flash_init();
init_rtc();
//ccid_prepare_receive(&ccid);
void core0_loop() {
while (1) {
execute_tasks();
neug_task();
@ -385,8 +351,90 @@ int main(void) {
button_pressed_time = button_press = 0;
}
}
#endif
#ifdef ESP_PLATFORM
vTaskDelay(pdMS_TO_TICKS(10));
#endif
}
}
char pico_serial_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1];
pico_unique_board_id_t pico_serial;
#ifdef ESP_PLATFORM
#include "tinyusb.h"
#include "esp_efuse.h"
#define pico_get_unique_board_id(a) do { uint32_t value; esp_efuse_read_block(EFUSE_BLK1, &value, 0, 32); memcpy((uint8_t *)(a), &value, sizeof(uint32_t)); esp_efuse_read_block(EFUSE_BLK1, &value, 32, 32); memcpy((uint8_t *)(a)+4, &value, sizeof(uint32_t)); } while(0)
extern const tinyusb_config_t tusb_cfg;
TaskHandle_t hcore0 = NULL, hcore1 = NULL;
int app_main() {
#else
#ifdef ENABLE_EMULATION
#define pico_get_unique_board_id(a) memset(a, 0, sizeof(*(a)))
#endif
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++) {
snprintf(&pico_serial_str[2 * i], 3, "%02X", pico_serial.id[i]);
}
#ifndef ENABLE_EMULATION
#ifndef ESP_PLATFORM
board_init();
stdio_init_all();
#endif
#ifdef PIMORONI_TINY2040
gpio_init(TINY2040_LED_R_PIN);
gpio_set_dir(TINY2040_LED_R_PIN, GPIO_OUT);
gpio_init(TINY2040_LED_G_PIN);
gpio_set_dir(TINY2040_LED_G_PIN, GPIO_OUT);
gpio_init(TINY2040_LED_B_PIN);
gpio_set_dir(TINY2040_LED_B_PIN, GPIO_OUT);
#elif defined(PICO_DEFAULT_LED_PIN)
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
#elif defined(CYW43_WL_GPIO_LED_PIN)
cyw43_arch_init();
#endif
led_off_all();
//prepare_ccid();
#else
emul_init("127.0.0.1", 35963);
#endif
random_init();
low_flash_init();
scan_flash();
init_rtc();
#ifndef ENABLE_EMULATION
usb_init();
#ifdef ESP_PLATFORM
tusb_cfg.string_descriptor[3] = pico_serial_str;
tinyusb_driver_install(&tusb_cfg);
#endif
#endif
//ccid_prepare_receive(&ccid);
#ifdef ESP_PLATFORM
uint8_t gpio = GPIO_NUM_48;
if (file_has_data(ef_phy)) {
gpio = file_get_data(ef_phy)[PHY_LED_GPIO];
}
neopixel = neopixel_Init(1, gpio);
#endif
#ifdef ESP_PLATFORM
xTaskCreate(core0_loop, "core0", 4096, NULL, CONFIG_TINYUSB_TASK_PRIORITY + 1, &hcore0);
#else
core0_loop();
#endif
return 0;
}

View file

@ -19,11 +19,11 @@
#define _PICO_KEYS_H_
#include "file.h"
#ifndef ENABLE_EMULATION
#include "pico/unique_id.h"
#else
#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM)
#include <stdint.h>
#ifdef ENABLE_EMULATION
extern uint32_t board_millis();
#endif
#if !defined(MIN)
#if defined(_MSC_VER)
#define MIN(a,b) (((a)<(b))?(a):(b))
@ -44,17 +44,27 @@ extern uint32_t board_millis();
_a > _b ? _a : _b; })
#endif
#endif
#else
#include "pico/unique_id.h"
#endif
#include <string.h>
#if defined(ENABLE_EMULATION)
#include <stdbool.h>
#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();
static inline const uint16_t make_uint16_t(uint8_t b1, uint8_t b2) {
static inline uint16_t make_uint16_t(uint8_t b1, uint8_t b2) {
return (b1 << 8) | b2;
}
static inline const uint16_t get_uint16_t(const uint8_t *b, uint16_t offset) {
static inline uint16_t get_uint16_t(const uint8_t *b, uint16_t offset) {
return make_uint16_t(b[offset], b[offset + 1]);
}
static inline void put_uint16_t(uint16_t n, uint8_t *b) {
@ -155,12 +165,11 @@ extern uint32_t button_timeout;
#define CCID_WRONG_PADDING -1011
#define CCID_VERIFICATION_FAILED -1012
#if defined(ENABLE_EMULATION)
#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM)
#define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8
typedef struct { uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES]; } pico_unique_board_id_t;
#endif
extern pico_unique_board_id_t pico_serial;
extern char pico_serial_str[];
extern char pico_serial_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1];
#endif

View file

@ -18,7 +18,17 @@
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#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

View file

@ -21,7 +21,7 @@
#define NEUG_PRE_LOOP 32
#include <stdlib.h>
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#endif

View file

@ -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

View file

@ -21,9 +21,6 @@
#include <stdlib.h>
#include <stdint.h>
#ifndef ENABLE_EMULATION
#include "pico/stdlib.h"
#endif
void random_init(void);
void random_fini(void);
@ -32,9 +29,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);

View file

@ -15,28 +15,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
// Pico
#include "pico/stdlib.h"
// For memcpy
#include <string.h>
// Include descriptor struct definitions
//#include "usb_common.h"
// USB register definitions from pico-sdk
#include "hardware/regs/usb.h"
// USB hardware struct definitions from pico-sdk
#include "hardware/structs/usb.h"
// For interrupt enable and numbers
#include "hardware/irq.h"
// For resetting the USB controller
#include "hardware/resets.h"
#include "random.h"
#include "pico_keys.h"
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "hardware/rtc.h"
#endif
#include "tusb.h"
#include "ccid.h"
#include "device/usbd_pvt.h"

View file

@ -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"

View file

@ -26,8 +26,6 @@
#ifndef _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_
#include "usb.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -47,7 +45,7 @@ extern "C" {
#endif
//--------------------------------------------------------------------
// COMMON CONFIGURATION
// Common Configuration
//--------------------------------------------------------------------
// defined by compiler flags for flexibility
@ -55,20 +53,20 @@ extern "C" {
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || \
CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \
CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#ifndef CFG_TUSB_OS
#if CFG_TUSB_MCU == OPT_MCU_RP2040
#define CFG_TUSB_OS OPT_OS_PICO
#elif CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
#define CFG_TUSB_OS OPT_OS_FREERTOS
#endif
#endif
#ifndef CFG_TUSB_OS
#define CFG_TUSB_OS OPT_OS_PICO
#ifndef CFG_TUSB_RHPORT0_MODE
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED)
#endif
#ifndef CFG_TUSB_DEBUG
#define CFG_TUSB_DEBUG 1
#define CFG_TUSB_DEBUG 0
#endif
// Enable Device stack
@ -120,6 +118,12 @@ extern "C" {
// HID buffer size Should be sufficient to hold ID (if any) + Data
#define CFG_TUD_HID_EP_BUFSIZE 64
// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
// CDC Endpoint transfer buffer size, more is faster
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#ifdef __cplusplus
}

View file

@ -18,7 +18,7 @@
#include <stdio.h>
// Pico
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "bsp/board.h"
@ -26,6 +26,13 @@
#include "pico_keys.h"
#include "usb.h"
#include "apdu.h"
#ifndef ENABLE_EMULATION
#include "tusb.h"
#endif
#ifndef ENABLE_EMULATION
#include "tusb.h"
#endif
#ifndef ENABLE_EMULATION
#include "tusb.h"
@ -156,7 +163,7 @@ void usb_clear_rx(uint8_t itf) {
#define USB_BCD 0x0200
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION)
queue_t usb_to_card_q;
queue_t card_to_usb_q;
#endif

View file

@ -18,12 +18,17 @@
#ifndef _USB_H_
#define _USB_H_
#ifndef ENABLE_EMULATION
#include "pico/util/queue.h"
#else
#if defined(ENABLE_EMULATION)
#include <stdbool.h>
#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

View file

@ -17,7 +17,12 @@
#include "tusb.h"
#include "usb_descriptors.h"
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/unique_id.h"
#endif
#ifdef ESP_PLATFORM
#include "tinyusb.h"
#endif
#include "pico_keys_version.h"
#include "usb.h"
#include "pico_keys.h"
@ -59,144 +64,33 @@ tusb_desc_device_t desc_device = {
.bNumConfigurations = 1
};
#ifndef ESP_PLATFORM
uint8_t const *tud_descriptor_device_cb(void) {
return (uint8_t const *) &desc_device;
}
#endif
//--------------------------------------------------------------------+
// Configuration Descriptor
//--------------------------------------------------------------------+
tusb_desc_configuration_t const desc_config = {
.bLength = sizeof(tusb_desc_configuration_t),
.bDescriptorType = TUSB_DESC_CONFIGURATION,
.wTotalLength = (sizeof(tusb_desc_configuration_t)
#ifdef USB_ITF_CCID
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
3 * sizeof(tusb_desc_endpoint_t))
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
2 * sizeof(tusb_desc_endpoint_t))
#endif
#define TUD_INTERFACE_DESC_LEN 9
#define TUD_ENDPOINT_DESC_LEN 7
#define TUSB_SMARTCARD_LEN 54
#define TUSB_SMARTCARD_CCID_DESC_LEN (TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 3 * TUD_ENDPOINT_DESC_LEN)
#define TUSB_SMARTCARD_WCID_DESC_LEN (TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 2 * TUD_ENDPOINT_DESC_LEN)
enum {
TUSB_DESC_TOTAL_LEN = TUD_CONFIG_DESC_LEN
#ifdef USB_ITF_HID
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
#endif
),
.bNumInterfaces = ITF_TOTAL,
.bConfigurationValue = 1,
.iConfiguration = 4,
.bmAttributes = USB_CONFIG_ATT_ONE | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP,
.bMaxPower = TUSB_DESC_CONFIG_POWER_MA(MAX_USB_POWER + 1),
};
#ifdef USB_ITF_CCID
static const struct ccid_class_descriptor desc_ccid = {
.bLength = sizeof(struct ccid_class_descriptor),
.bDescriptorType = 0x21,
.bcdCCID = (0x0110),
.bMaxSlotIndex = 0,
.bVoltageSupport = 0x01, // 5.0V
.dwProtocols = (0x02), // T=1
.dwDefaultClock = (0xDFC),
.dwMaximumClock = (0xDFC),
.bNumClockSupport = 0,
.dwDataRate = (0x2580),
.dwMaxDataRate = (0x2580),
.bNumDataRatesSupported = 0,
.dwMaxIFSD = (0xFE), // IFSD is handled by the real reader driver
.dwSynchProtocols = (0),
.dwMechanical = (0),
.dwFeatures = 0x40840, //USB-ICC, short & extended APDU
.dwMaxCCIDMessageLength = 65544 + 10,
.bClassGetResponse = 0xFF,
.bclassEnvelope = 0xFF,
.wLcdLayout = 0x0,
.bPINSupport = 0x0,
.bMaxCCIDBusySlots = 0x01,
};
tusb_desc_interface_t const desc_interface = {
.bLength = sizeof(tusb_desc_interface_t),
.bDescriptorType = TUSB_DESC_INTERFACE,
.bInterfaceNumber = ITF_CCID,
.bAlternateSetting = 0,
.bNumEndpoints = 3,
.bInterfaceClass = TUSB_CLASS_SMART_CARD,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = ITF_CCID + 5,
};
tusb_desc_interface_t const desc_interface_wcid = {
.bLength = sizeof(tusb_desc_interface_t),
.bDescriptorType = TUSB_DESC_INTERFACE,
.bInterfaceNumber = ITF_WCID,
.bAlternateSetting = 0,
.bNumEndpoints = 2,
.bInterfaceClass = 0xFF,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = ITF_WCID + 5,
};
tusb_desc_endpoint_t const desc_ep1 = {
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = TUSB_DIR_IN_MASK | 1,
.bmAttributes.xfer = TUSB_XFER_BULK,
.wMaxPacketSize = (64),
.bInterval = 0
};
tusb_desc_endpoint_t const desc_ep2 = {
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = 1,
.bmAttributes.xfer = TUSB_XFER_BULK,
.wMaxPacketSize = (64),
.bInterval = 0
};
tusb_desc_endpoint_t const desc_ep3 = {
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = TUSB_DIR_IN_MASK | 2,
.bmAttributes.xfer = TUSB_XFER_INTERRUPT,
.wMaxPacketSize = (64),
.bInterval = 0
};
tusb_desc_endpoint_t const desc_ep1_wcid = {
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = (TUSB_DIR_IN_MASK | 1) + 3,
.bmAttributes.xfer = TUSB_XFER_BULK,
.wMaxPacketSize = (64),
.bInterval = 0
};
tusb_desc_endpoint_t const desc_ep2_wcid = {
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = 1 + 3,
.bmAttributes.xfer = TUSB_XFER_BULK,
.wMaxPacketSize = (64),
.bInterval = 0
};
+ TUSB_SMARTCARD_CCID_DESC_LEN
#ifdef USB_ITF_WCID
+ TUSB_SMARTCARD_WCID_DESC_LEN
#endif
static uint8_t desc_config_extended[sizeof(tusb_desc_configuration_t)
#ifdef USB_ITF_CCID
+ (sizeof(tusb_desc_interface_t) +
sizeof(struct ccid_class_descriptor) + 3 *
sizeof(tusb_desc_endpoint_t))
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
2 * sizeof(tusb_desc_endpoint_t))
#endif
#ifdef USB_ITF_HID
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
#endif
];
};
#ifdef USB_ITF_HID
uint8_t const desc_hid_report[] = {
@ -205,23 +99,40 @@ uint8_t const desc_hid_report[] = {
uint8_t const desc_hid_report_kb[] = {
TUD_HID_REPORT_DESC_KEYBOARD(HID_USAGE(HID_USAGE_DESKTOP_GAMEPAD), HID_LOGICAL_MIN(0), HID_LOGICAL_MAX_N(255, 2), HID_REPORT_COUNT(8), HID_REPORT_SIZE(8), HID_FEATURE( HID_DATA | HID_VARIABLE | HID_ABSOLUTE), )
};
#define EPNUM_HID 0x03
#define EPNUM_HID 0x04
#endif
static uint8_t desc_hid[] = {
TUD_HID_INOUT_DESCRIPTOR(ITF_HID,
ITF_HID + 5,
HID_ITF_PROTOCOL_NONE,
sizeof(desc_hid_report),
EPNUM_HID,
0x80 | EPNUM_HID,
CFG_TUD_HID_EP_BUFSIZE,
10)
#ifdef USB_ITF_CCID
#ifdef USB_ITF_WCID
#define TUD_SMARTCARD_DESCRIPTOR_WEB(_itf, _strix, _epout, _epin, _epsize) \
9, TUSB_DESC_INTERFACE, _itf, 0, 2, 0xFF, 0, 0, _strix, \
54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
#endif
#define TUD_SMARTCARD_DESCRIPTOR(_itf, _strix, _epout, _epin, _epint, _epsize) \
9, TUSB_DESC_INTERFACE, _itf, 0, 3, TUSB_CLASS_SMART_CARD, 0, 0, _strix, \
54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \
7, TUSB_DESC_ENDPOINT, _epint, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), 0
#endif
const uint8_t desc_config[] = {
TUD_CONFIG_DESCRIPTOR(1, ITF_TOTAL, 4, TUSB_DESC_TOTAL_LEN, USB_CONFIG_ATT_ONE | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
#ifdef USB_ITF_HID
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),
TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD + 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report_kb), TUSB_DIR_IN_MASK | (EPNUM_HID + 1), 16, 5),
#endif
#ifdef USB_ITF_CCID
TUD_SMARTCARD_DESCRIPTOR(ITF_CCID, ITF_CCID+5, 1, TUSB_DIR_IN_MASK | 1, TUSB_DIR_IN_MASK | 2, 64),
#ifdef USB_ITF_WCID
TUD_SMARTCARD_DESCRIPTOR_WEB(ITF_WCID, ITF_WCID+5, 3, TUSB_DIR_IN_MASK | 3, 64),
#endif
#endif
};
static uint8_t desc_hid_kb[] = {
TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD + 5, HID_ITF_PROTOCOL_NONE,
sizeof(desc_hid_report_kb), 0x80 | (EPNUM_HID + 1), 16, 5)
};
#ifdef USB_ITF_HID
#include "apdu.h"
uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {
printf("report_cb %d\n", itf);
@ -235,50 +146,24 @@ uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {
return NULL;
}
#endif
#ifndef ESP_PLATFORM
uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
(void) index; // for multiple configurations
static uint8_t initd = 0;
if (initd == 0) {
uint8_t *p = desc_config_extended;
memcpy(p, &desc_config, sizeof(tusb_desc_configuration_t));
p += sizeof(tusb_desc_configuration_t);
#ifdef USB_ITF_HID
memcpy(p, &desc_hid, sizeof(desc_hid)); p += sizeof(desc_hid);
memcpy(p, &desc_hid_kb, sizeof(desc_hid_kb)); p += sizeof(desc_hid_kb);
#endif
#ifdef USB_ITF_CCID
memcpy(p, &desc_interface, sizeof(tusb_desc_interface_t));
p += sizeof(tusb_desc_interface_t);
memcpy(p, &desc_ccid, sizeof(struct ccid_class_descriptor));
p += sizeof(struct ccid_class_descriptor);
memcpy(p, &desc_ep1, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
memcpy(p, &desc_ep2, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
memcpy(p, &desc_ep3, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
memcpy(p, &desc_interface_wcid, sizeof(tusb_desc_interface_t));
p += sizeof(tusb_desc_interface_t);
memcpy(p, &desc_ccid, sizeof(struct ccid_class_descriptor));
p += sizeof(struct ccid_class_descriptor);
memcpy(p, &desc_ep1_wcid, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
memcpy(p, &desc_ep2_wcid, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
#endif
initd = 1;
}
return (const uint8_t *) desc_config_extended;
printf("tud_descriptor_configuration_cb %d\n",index);
return desc_config;
}
#endif
#ifdef USB_ITF_WCID
#define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN + TUD_BOS_MICROSOFT_OS_DESC_LEN)
#define MS_OS_20_DESC_LEN 0xB2
enum
{
VENDOR_REQUEST_WEBUSB = 1,
VENDOR_REQUEST_MICROSOFT = 2
};
#define URL "picokeys.com/pki/"
#define URL "www.picokeys.com/pki/"
static bool web_serial_connected = false;
const tusb_desc_webusb_url_t desc_url =
@ -369,10 +254,9 @@ uint8_t const desc_bos[] = {
};
uint8_t const *tud_descriptor_bos_cb(void) {
printf("!!!!!!!!!!!! tud_descriptor_bos_cb\n");
return desc_bos;
}
#endif
//--------------------------------------------------------------------+
// String Descriptors
//--------------------------------------------------------------------+
@ -390,10 +274,21 @@ char const *string_desc_arr [] = {
#endif
#ifdef USB_ITF_CCID
, "Pico Key CCID Interface"
#ifdef USB_ITF_WCID
, "Pico Key WebCCID Interface"
#endif
#endif
};
#ifdef ESP_PLATFORM
const tinyusb_config_t tusb_cfg = {
.device_descriptor = &desc_device,
.string_descriptor = string_desc_arr,
.string_descriptor_count = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]),
.external_phy = false,
.configuration_descriptor = desc_config,
};
#else
static uint16_t _desc_str[32];
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
@ -414,12 +309,8 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
}
const char *str = string_desc_arr[index];
char unique_id_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1];
if (index == 3) {
pico_unique_board_id_t unique_id;
pico_get_unique_board_id(&unique_id);
pico_get_unique_board_id_string(unique_id_str, 2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1);
str = unique_id_str;
str = pico_serial_str;
}
chr_count = strlen(str);
@ -437,3 +328,4 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
return _desc_str;
}
#endif