Skip to content

Commit

Permalink
Add support to ESP32.
Browse files Browse the repository at this point in the history
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
  • Loading branch information
polhenarejos committed Aug 19, 2024
1 parent 8a5c734 commit 1051690
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -33,6 +38,7 @@ pico_sdk_init()
endif()

add_executable(pico_openpgp)
endif()

set(SOURCES ${SOURCES}
${CMAKE_CURRENT_LIST_DIR}/src/openpgp/openpgp.c
Expand All @@ -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})

Expand Down Expand Up @@ -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()
6 changes: 2 additions & 4 deletions src/openpgp/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/openpgp/management.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define _MANAGEMENT_H_

#include <stdlib.h>
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#endif

Expand Down
16 changes: 8 additions & 8 deletions src/openpgp/openpgp.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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))) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/openpgp/openpgp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define __OPENPGP_H_

#include "stdlib.h"
#ifndef ENABLE_EMULATION
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include <pico/stdlib.h>
#endif

Expand Down
18 changes: 7 additions & 11 deletions src/openpgp/piv.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#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"
#include "random.h"
#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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 1051690

Please sign in to comment.