mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-06-02 19:33:01 +02:00
Add procedure to compute unique ID at startup.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
idf_component_register(
|
idf_component_register(
|
||||||
SRCS ${INTERNAL_SOURCES}
|
SRCS ${INTERNAL_SOURCES}
|
||||||
INCLUDE_DIRS . fs rng usb ../mbedtls/include
|
INCLUDE_DIRS . fs rng usb ../mbedtls/include
|
||||||
REQUIRES bootloader_support esp_partition esp_tinyusb
|
REQUIRES bootloader_support esp_partition esp_tinyusb efuse
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -40,14 +40,9 @@ void hash_multi(const uint8_t *input, uint16_t len, uint8_t output[32]) {
|
|||||||
mbedtls_sha256_context ctx;
|
mbedtls_sha256_context ctx;
|
||||||
mbedtls_sha256_init(&ctx);
|
mbedtls_sha256_init(&ctx);
|
||||||
uint16_t iters = 256;
|
uint16_t iters = 256;
|
||||||
#ifndef ENABLE_EMULATION
|
|
||||||
pico_unique_board_id_t unique_id;
|
|
||||||
|
|
||||||
pico_get_unique_board_id(&unique_id);
|
|
||||||
#endif
|
|
||||||
mbedtls_sha256_starts(&ctx, 0);
|
mbedtls_sha256_starts(&ctx, 0);
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
mbedtls_sha256_update(&ctx, unique_id.id, sizeof(unique_id.id));
|
mbedtls_sha256_update(&ctx, pico_serial.id, sizeof(pico_serial.id));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while (iters > len) {
|
while (iters > len) {
|
||||||
|
|||||||
@@ -38,12 +38,6 @@ static inline uint32_t board_millis(void) {
|
|||||||
}
|
}
|
||||||
#define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8
|
#define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8
|
||||||
typedef struct { uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES]; } pico_unique_board_id_t;
|
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 mutex_t;
|
||||||
typedef SemaphoreHandle_t semaphore_t;
|
typedef SemaphoreHandle_t semaphore_t;
|
||||||
#define mutex_init(a) do { *(a) = xSemaphoreCreateMutex();} while(0)
|
#define mutex_init(a) do { *(a) = xSemaphoreCreateMutex();} while(0)
|
||||||
|
|||||||
@@ -263,6 +263,9 @@ void scan_region(bool persistent) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
void wait_flash_finish();
|
void wait_flash_finish();
|
||||||
|
#ifndef ENABLE_EMULATION
|
||||||
|
extern uint16_t usb_vid, usb_pid;
|
||||||
|
#endif
|
||||||
void scan_flash() {
|
void scan_flash() {
|
||||||
initialize_flash(false); //soft initialization
|
initialize_flash(false); //soft initialization
|
||||||
if (*(uintptr_t *) flash_read(end_rom_pool) == 0xffffffff &&
|
if (*(uintptr_t *) flash_read(end_rom_pool) == 0xffffffff &&
|
||||||
@@ -278,6 +281,12 @@ void scan_flash() {
|
|||||||
printf("SCAN\n");
|
printf("SCAN\n");
|
||||||
scan_region(true);
|
scan_region(true);
|
||||||
scan_region(false);
|
scan_region(false);
|
||||||
|
#ifndef ENABLE_EMULATION
|
||||||
|
file_t *ef_vp = search_dynamic_file(EF_VP);
|
||||||
|
if (file_has_data(ef_vp)) {
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *file_read(const uint8_t *addr) {
|
uint8_t *file_read(const uint8_t *addr) {
|
||||||
|
|||||||
@@ -67,6 +67,10 @@
|
|||||||
#define EF_SKDFS 0x6045
|
#define EF_SKDFS 0x6045
|
||||||
#define EF_META 0xE010
|
#define EF_META 0xE010
|
||||||
|
|
||||||
|
#ifndef ENABLE_EMULATION
|
||||||
|
#define EF_VP 0xE020
|
||||||
|
#endif
|
||||||
|
|
||||||
#define MAX_DEPTH 4
|
#define MAX_DEPTH 4
|
||||||
|
|
||||||
typedef PACK(struct file {
|
typedef PACK(struct file {
|
||||||
|
|||||||
24
src/main.c
24
src/main.c
@@ -360,18 +360,23 @@ void core0_loop() {
|
|||||||
|
|
||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
#include "tinyusb.h"
|
#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;
|
extern const tinyusb_config_t tusb_cfg;
|
||||||
TaskHandle_t hcore0 = NULL, hcore1 = NULL;
|
TaskHandle_t hcore0 = NULL, hcore1 = NULL;
|
||||||
|
char pico_serial_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1];
|
||||||
|
pico_unique_board_id_t pico_serial;
|
||||||
int app_main() {
|
int app_main() {
|
||||||
#else
|
#else
|
||||||
int main(void) {
|
int main(void) {
|
||||||
#endif
|
#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 ENABLE_EMULATION
|
||||||
#ifdef ESP_PLATFORM
|
|
||||||
tinyusb_driver_install(&tusb_cfg);
|
|
||||||
#endif
|
|
||||||
usb_init();
|
|
||||||
#ifndef ESP_PLATFORM
|
#ifndef ESP_PLATFORM
|
||||||
board_init();
|
board_init();
|
||||||
stdio_init_all();
|
stdio_init_all();
|
||||||
@@ -392,7 +397,6 @@ int main(void) {
|
|||||||
|
|
||||||
led_off_all();
|
led_off_all();
|
||||||
|
|
||||||
|
|
||||||
//prepare_ccid();
|
//prepare_ccid();
|
||||||
#else
|
#else
|
||||||
emul_init("127.0.0.1", 35963);
|
emul_init("127.0.0.1", 35963);
|
||||||
@@ -402,8 +406,18 @@ int main(void) {
|
|||||||
|
|
||||||
low_flash_init();
|
low_flash_init();
|
||||||
|
|
||||||
|
scan_flash();
|
||||||
|
|
||||||
init_rtc();
|
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);
|
//ccid_prepare_receive(&ccid);
|
||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
neopixel = neopixel_Init(1, GPIO_NUM_48);
|
neopixel = neopixel_Init(1, GPIO_NUM_48);
|
||||||
|
|||||||
@@ -166,4 +166,7 @@ extern uint32_t button_timeout;
|
|||||||
#define CCID_WRONG_PADDING -1011
|
#define CCID_WRONG_PADDING -1011
|
||||||
#define CCID_VERIFICATION_FAILED -1012
|
#define CCID_VERIFICATION_FAILED -1012
|
||||||
|
|
||||||
|
extern pico_unique_board_id_t pico_serial;
|
||||||
|
extern char pico_serial_str[];
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -84,7 +84,6 @@ struct ccid_header {
|
|||||||
|
|
||||||
uint8_t ccid_status = 1;
|
uint8_t ccid_status = 1;
|
||||||
static uint8_t itf_num;
|
static uint8_t itf_num;
|
||||||
extern tusb_desc_endpoint_t const desc_ep3;
|
|
||||||
|
|
||||||
void ccid_write_offset(uint8_t itf, uint16_t size, uint16_t offset) {
|
void ccid_write_offset(uint8_t itf, uint16_t size, uint16_t offset) {
|
||||||
if (*usb_get_tx(itf) + offset != 0x81) {
|
if (*usb_get_tx(itf) + offset != 0x81) {
|
||||||
@@ -267,38 +266,31 @@ uint8_t *driver_prepare_response_ccid(uint8_t itf) {
|
|||||||
#define MAX_USB_POWER 1
|
#define MAX_USB_POWER 1
|
||||||
|
|
||||||
static void ccid_init_cb(void) {
|
static void ccid_init_cb(void) {
|
||||||
TU_LOG1("-------- CCID INIT\n");
|
|
||||||
vendord_init();
|
vendord_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ccid_reset_cb(uint8_t rhport) {
|
static void ccid_reset_cb(uint8_t rhport) {
|
||||||
TU_LOG1("-------- CCID RESET\n");
|
|
||||||
itf_num = 0;
|
itf_num = 0;
|
||||||
vendord_reset(rhport);
|
vendord_reset(rhport);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len) {
|
static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len) {
|
||||||
uint8_t *itf_vendor = (uint8_t *) malloc(sizeof(uint8_t) * max_len);
|
uint8_t *itf_vendor = (uint8_t *) malloc(sizeof(uint8_t) * max_len);
|
||||||
//TU_LOG1("-------- CCID OPEN\n");
|
TU_VERIFY( itf_desc->bInterfaceClass == TUSB_CLASS_SMART_CARD && itf_desc->bInterfaceSubClass == 0 && itf_desc->bInterfaceProtocol == 0, 0);
|
||||||
TU_VERIFY(
|
|
||||||
itf_desc->bInterfaceClass == TUSB_CLASS_SMART_CARD && itf_desc->bInterfaceSubClass == 0 && itf_desc->bInterfaceProtocol == 0,
|
|
||||||
0);
|
|
||||||
|
|
||||||
//vendord_open expects a CLASS_VENDOR interface class
|
//vendord_open expects a CLASS_VENDOR interface class
|
||||||
|
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + 3 * sizeof(tusb_desc_endpoint_t);
|
||||||
memcpy(itf_vendor, itf_desc, sizeof(uint8_t) * max_len);
|
memcpy(itf_vendor, itf_desc, sizeof(uint8_t) * max_len);
|
||||||
((tusb_desc_interface_t *) itf_vendor)->bInterfaceClass = TUSB_CLASS_VENDOR_SPECIFIC;
|
((tusb_desc_interface_t *) itf_vendor)->bInterfaceClass = TUSB_CLASS_VENDOR_SPECIFIC;
|
||||||
((tusb_desc_interface_t *) itf_vendor)->bNumEndpoints -= 1;
|
((tusb_desc_interface_t *) itf_vendor)->bNumEndpoints -= 1;
|
||||||
vendord_open(rhport,
|
vendord_open(rhport, (tusb_desc_interface_t *) itf_vendor, max_len - sizeof(tusb_desc_endpoint_t));
|
||||||
(tusb_desc_interface_t *) itf_vendor,
|
tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *)((uint8_t *)itf_desc + drv_len - sizeof(tusb_desc_endpoint_t));
|
||||||
max_len - sizeof(tusb_desc_endpoint_t));
|
TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0);
|
||||||
TU_ASSERT(usbd_edpt_open(rhport, &desc_ep3), 0);
|
|
||||||
free(itf_vendor);
|
free(itf_vendor);
|
||||||
|
|
||||||
uint8_t msg[] = { 0x50, 0x03 };
|
uint8_t msg[] = { 0x50, 0x03 };
|
||||||
usbd_edpt_xfer(rhport, desc_ep3.bEndpointAddress, msg, sizeof(msg));
|
usbd_edpt_xfer(rhport, desc_ep->bEndpointAddress, msg, sizeof(msg));
|
||||||
|
|
||||||
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
|
|
||||||
3 * sizeof(tusb_desc_endpoint_t);
|
|
||||||
TU_VERIFY(max_len >= drv_len, 0);
|
TU_VERIFY(max_len >= drv_len, 0);
|
||||||
|
|
||||||
itf_num = itf_desc->bInterfaceNumber;
|
itf_num = itf_desc->bInterfaceNumber;
|
||||||
|
|||||||
@@ -429,15 +429,12 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
|||||||
memset(ctap_resp, 0, 64);
|
memset(ctap_resp, 0, 64);
|
||||||
ctap_resp->cid = ctap_req->cid;
|
ctap_resp->cid = ctap_req->cid;
|
||||||
ctap_resp->init.cmd = ctap_req->init.cmd;
|
ctap_resp->init.cmd = ctap_req->init.cmd;
|
||||||
#ifndef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
pico_unique_board_id_t rpiid;
|
|
||||||
pico_get_unique_board_id(&rpiid);
|
|
||||||
#else
|
|
||||||
struct {
|
struct {
|
||||||
uint8_t id[8];
|
uint8_t id[8];
|
||||||
} rpiid = { 0 };
|
} rpiid = { 0 };
|
||||||
#endif
|
#endif
|
||||||
memcpy(ctap_resp->init.data, rpiid.id, sizeof(rpiid.id));
|
memcpy(ctap_resp->init.data, pico_serial.id, sizeof(pico_serial.id));
|
||||||
ctap_resp->init.bcntl = 16;
|
ctap_resp->init.bcntl = 16;
|
||||||
hid_write(64);
|
hid_write(64);
|
||||||
msg_packet.len = msg_packet.current_len = 0;
|
msg_packet.len = msg_packet.current_len = 0;
|
||||||
|
|||||||
@@ -21,12 +21,12 @@
|
|||||||
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
|
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
|
||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
#include "pico/multicore.h"
|
#include "pico/multicore.h"
|
||||||
#include "tusb.h"
|
|
||||||
#include "bsp/board.h"
|
#include "bsp/board.h"
|
||||||
#endif
|
#endif
|
||||||
#include "pico_keys.h"
|
#include "pico_keys.h"
|
||||||
#include "usb.h"
|
#include "usb.h"
|
||||||
#include "apdu.h"
|
#include "apdu.h"
|
||||||
|
#include "tusb.h"
|
||||||
|
|
||||||
// For memcpy
|
// For memcpy
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -158,10 +158,15 @@ queue_t usb_to_card_q;
|
|||||||
queue_t card_to_usb_q;
|
queue_t card_to_usb_q;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void usb_init() {
|
extern uint16_t usb_vid, usb_pid;
|
||||||
|
extern tusb_desc_device_t desc_device;
|
||||||
|
void usb_init()
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
queue_init(&card_to_usb_q, sizeof(uint32_t), 64);
|
queue_init(&card_to_usb_q, sizeof(uint32_t), 64);
|
||||||
queue_init(&usb_to_card_q, sizeof(uint32_t), 64);
|
queue_init(&usb_to_card_q, sizeof(uint32_t), 64);
|
||||||
|
desc_device.idVendor = usb_vid;
|
||||||
|
desc_device.idProduct = usb_pid;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user