mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-06-18 20:35:57 +02:00
Add procedure to compute unique ID at startup.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -37,14 +37,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) {
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
11
src/main.c
11
src/main.c
@@ -313,7 +313,18 @@ void execute_tasks() {
|
|||||||
led_blinking_task();
|
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) {
|
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
|
#ifndef ENABLE_EMULATION
|
||||||
usb_init();
|
usb_init();
|
||||||
|
|
||||||
|
|||||||
@@ -156,4 +156,12 @@ 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
|
||||||
|
|
||||||
|
#if defined(ENABLE_EMULATION)
|
||||||
|
#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[];
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -101,7 +101,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) {
|
||||||
@@ -284,38 +283,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;
|
||||||
|
|||||||
@@ -427,15 +427,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;
|
||||||
|
|||||||
Reference in New Issue
Block a user