From 1051690b79f718e395a9747393ecc6382a9bf590 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 20 Aug 2024 00:23:22 +0200 Subject: [PATCH] Add support to ESP32. Signed-off-by: Pol Henarejos --- CMakeLists.txt | 11 +++++++++++ pico-keys-sdk | 2 +- src/openpgp/management.c | 6 ++---- src/openpgp/management.h | 2 +- src/openpgp/openpgp.c | 16 ++++++++-------- src/openpgp/openpgp.h | 2 +- src/openpgp/piv.c | 18 +++++++----------- 7 files changed, 31 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7abe779..f84e42d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,11 @@ cmake_minimum_required(VERSION 3.13) +if(ESP_PLATFORM) +set(EXTRA_COMPONENT_DIRS src pico-keys-sdk/src) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +else() + if(ENABLE_EMULATION) else() include(pico_sdk_import.cmake) @@ -33,6 +38,7 @@ pico_sdk_init() endif() add_executable(pico_openpgp) +endif() set(SOURCES ${SOURCES} ${CMAKE_CURRENT_LIST_DIR}/src/openpgp/openpgp.c @@ -47,7 +53,11 @@ set(INCLUDES ${INCLUDES} set(USB_ITF_CCID 1) include(pico-keys-sdk/pico_keys_sdk_import.cmake) +if(ESP_PLATFORM) + project(pico_fido) +endif() +if(NOT ESP_PLATFORM) target_sources(pico_openpgp PUBLIC ${SOURCES}) target_include_directories(pico_openpgp PUBLIC ${INCLUDES}) @@ -79,3 +89,4 @@ pico_add_extra_outputs(pico_openpgp) target_link_libraries(pico_openpgp PRIVATE pico_keys_sdk pico_stdlib tinyusb_device tinyusb_board pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id hardware_rtc) endif() +endif() diff --git a/pico-keys-sdk b/pico-keys-sdk index f4ad8e1..d379a39 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit f4ad8e1af2e2657f3900f1e01db031d7d73d623b +Subproject commit d379a39bd699a679e2f5e5605af95922dc35576f diff --git a/src/openpgp/management.c b/src/openpgp/management.c index 751f8b0..337213b 100644 --- a/src/openpgp/management.c +++ b/src/openpgp/management.c @@ -41,7 +41,7 @@ int man_select(app_t *a) { return CCID_OK; } -void __attribute__((constructor)) man_ctor() { +INITIALIZER( man_ctor ) { register_app(man_select, man_aid); } @@ -79,9 +79,7 @@ int man_get_config() { res_APDU[res_APDU_size++] = CAP_PIV | CAP_OPENPGP; res_APDU[res_APDU_size++] = TAG_SERIAL; res_APDU[res_APDU_size++] = 4; -#ifndef ENABLE_EMULATION - pico_get_unique_board_id_string((char *) res_APDU + res_APDU_size, 4); -#endif + memcpy(res_APDU + res_APDU_size, pico_serial.id, 4); res_APDU_size += 4; res_APDU[res_APDU_size++] = TAG_FORM_FACTOR; res_APDU[res_APDU_size++] = 1; diff --git a/src/openpgp/management.h b/src/openpgp/management.h index 6a5ff0d..a8a6331 100644 --- a/src/openpgp/management.h +++ b/src/openpgp/management.h @@ -19,7 +19,7 @@ #define _MANAGEMENT_H_ #include -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include "pico/stdlib.h" #endif diff --git a/src/openpgp/openpgp.c b/src/openpgp/openpgp.c index 5584423..8c4c290 100644 --- a/src/openpgp/openpgp.c +++ b/src/openpgp/openpgp.c @@ -15,7 +15,12 @@ * along with this program. If not, see . */ +#ifdef ESP_PLATFORM +#include "esp_compat.h" +#define MBEDTLS_ALLOW_PRIVATE_ACCESS +#else #include "common.h" +#endif #include "openpgp.h" #include "version.h" #include "files.h" @@ -58,7 +63,6 @@ char atr_openpgp[] = { int openpgp_process_apdu(); - extern uint32_t board_button_read(void); static bool wait_button_pressed(uint16_t fid) { @@ -166,11 +170,7 @@ void scan_files() { file_t *ef; if ((ef = search_by_fid(EF_FULL_AID, NULL, SPECIFY_ANY))) { ef->data = openpgp_aid_full; -#ifndef ENABLE_EMULATION - pico_get_unique_board_id_string((char *) ef->data + 12, 4); -#else - memset((char *) ef->data + 12, 0, 4); -#endif + memcpy(ef->data + 12, pico_serial.id, 4); } bool reset_dek = false; if ((ef = search_by_fid(EF_DEK, NULL, SPECIFY_ANY))) { @@ -365,7 +365,7 @@ int openpgp_unload() { extern char __StackLimit; int heapLeft() { -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) char *p = malloc(256); // try to avoid undue fragmentation int left = &__StackLimit - p; free(p); @@ -392,7 +392,7 @@ int openpgp_select_aid(app_t *a) { return CCID_OK; } -void __attribute__((constructor)) openpgp_ctor() { +INITIALIZER( openpgp_ctor ) { ccid_atr = (uint8_t *) atr_openpgp; register_app(openpgp_select_aid, openpgp_aid); } diff --git a/src/openpgp/openpgp.h b/src/openpgp/openpgp.h index be5126c..513c366 100644 --- a/src/openpgp/openpgp.h +++ b/src/openpgp/openpgp.h @@ -19,7 +19,7 @@ #define __OPENPGP_H_ #include "stdlib.h" -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #include #endif diff --git a/src/openpgp/piv.c b/src/openpgp/piv.c index d8ae99d..34b809b 100644 --- a/src/openpgp/piv.c +++ b/src/openpgp/piv.c @@ -15,7 +15,12 @@ * along with this program. If not, see . */ +#ifdef ESP_PLATFORM +#include "esp_compat.h" +#define MBEDTLS_ALLOW_PRIVATE_ACCESS +#else #include "common.h" +#endif #include "files.h" #include "apdu.h" #include "pico_keys.h" @@ -23,9 +28,6 @@ #include "eac.h" #include "crypto_utils.h" #include "version.h" -#ifndef ENABLE_EMULATION -#include "pico/unique_id.h" -#endif #include "asn1.h" #include "mbedtls/aes.h" #include "mbedtls/des.h" @@ -77,14 +79,8 @@ uint8_t session_pwpiv[32]; int piv_process_apdu(); static int get_serial() { -#ifndef ENABLE_EMULATION - pico_unique_board_id_t unique_id; - pico_get_unique_board_id(&unique_id); - uint32_t serial = (unique_id.id[0] & 0x7F) << 24 | unique_id.id[1] << 16 | unique_id.id[2] << 8 | unique_id.id[3]; + uint32_t serial = (pico_serial.id[0] & 0x7F) << 24 | pico_serial.id[1] << 16 | pico_serial.id[2] << 8 | pico_serial.id[3]; return serial; -#else - return 0; -#endif } static int x509_create_cert(void *pk_ctx, uint8_t algo, uint8_t slot, bool attestation, uint8_t *buffer, size_t buffer_size) { @@ -311,7 +307,7 @@ int piv_select_aid(app_t *a) { return CCID_OK; } -void __attribute__((constructor)) piv_ctor() { +INITIALIZER( piv_ctor ) { register_app(piv_select_aid, piv_aid); register_app(piv_select_aid, yk_aid); }