From 11d52495d7209d31e7f12baf83e63859c2ef6141 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 3 Sep 2024 12:12:40 +0200 Subject: [PATCH 01/60] Upgrade to MbedTLS 3.6.1 Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index bc34efe..460111d 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit bc34efed5c730dec8355d7482cd647b57e1597e7 +Subproject commit 460111d29e12e1dc6318c93084df282aef6ddceb From c433e131eb8115fcb1907b630f3a628570a3ffb2 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 3 Sep 2024 18:08:03 +0200 Subject: [PATCH 02/60] Use new led color module. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 460111d..4711ae7 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 460111d29e12e1dc6318c93084df282aef6ddceb +Subproject commit 4711ae768a0f4d589aa66cedeb05b06e4a5b3b93 From bb8fcbcd3fb9bfc10db9be2484ee597d66db7c5c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 3 Sep 2024 23:24:23 +0000 Subject: [PATCH 03/60] Bump actions/download-artifact from 3 to 4.1.7 in /.github/workflows Bumps [actions/download-artifact](https://github.com/actions/download-artifact) from 3 to 4.1.7. - [Release notes](https://github.com/actions/download-artifact/releases) - [Commits](https://github.com/actions/download-artifact/compare/v3...v4.1.7) --- updated-dependencies: - dependency-name: actions/download-artifact dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9b81817..7095d23 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -54,7 +54,7 @@ jobs: with: submodules: recursive - name: Retrieve saved image - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4.1.7 with: name: docker-artifact path: artifacts From d78e925d4939e622c1085e7a33c4ad4227a0756c Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 4 Sep 2024 01:30:38 +0200 Subject: [PATCH 04/60] Parse and set binary version Signed-off-by: Pol Henarejos --- CMakeLists.txt | 3 +++ pico-keys-sdk | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d054d9e..0172d30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,9 @@ set(SOURCES ${SOURCES} set(USB_ITF_CCID 1) set(USB_ITF_WCID 1) include(pico-keys-sdk/pico_keys_sdk_import.cmake) + +SET_VERSION(ver_major ver_minor "${CMAKE_CURRENT_LIST_DIR}/src/hsm/version.h") + if(ESP_PLATFORM) project(pico_hsm) endif() diff --git a/pico-keys-sdk b/pico-keys-sdk index 4711ae7..697e2fd 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 4711ae768a0f4d589aa66cedeb05b06e4a5b3b93 +Subproject commit 697e2fd2637e96a09f074905836b69fe5020b4a7 From c6a72e4ffbe5ed4c431fedea4c87395039af3609 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Thu, 5 Sep 2024 07:44:17 +0200 Subject: [PATCH 05/60] Fix indent Signed-off-by: Pol Henarejos --- CMakeLists.txt | 201 ++++++++++++++++++++++++------------------------- 1 file changed, 99 insertions(+), 102 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0172d30..22097f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,83 +1,81 @@ - # - # This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm). - # 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 . - # +# +# This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm). +# 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 . +# 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) + 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) -endif() + if(NOT ENABLE_EMULATION) + include(pico_sdk_import.cmake) + endif() -project(pico_hsm C CXX ASM) + project(pico_hsm C CXX ASM) -set(CMAKE_C_STANDARD 11) -set(CMAKE_CXX_STANDARD 17) + set(CMAKE_C_STANDARD 11) + set(CMAKE_CXX_STANDARD 17) -if(NOT ENABLE_EMULATION) -pico_sdk_init() -endif() + if(NOT ENABLE_EMULATION) + pico_sdk_init() + endif() -if (NOT DEFINED __FOR_CI) - set(__FOR_CI 0) -endif() -if (__FOR_CI) - add_definitions(-D__FOR_CI) -endif() + if(NOT DEFINED __FOR_CI) + set(__FOR_CI 0) + endif() + if(__FOR_CI) + add_definitions(-D__FOR_CI) + endif() -add_executable(pico_hsm) + add_executable(pico_hsm) endif() set(SOURCES ${SOURCES} - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/sc_hsm.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_select.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_list_keys.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_read_binary.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_verify.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_reset_retry.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_challenge.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_external_authenticate.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_mse.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_initialize.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_domain.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_wrap.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_keypair_gen.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_update_ef.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_delete_file.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_change_pin.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_gen.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_signature.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_unwrap.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_decrypt_asym.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_cipher_sym.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_derive_asym.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_extras.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_general_authenticate.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_session_pin.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_puk_auth.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_pso.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_bip_slip.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cvc.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/files.c - ${CMAKE_CURRENT_LIST_DIR}/src/hsm/kek.c - - ) + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/sc_hsm.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_select.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_list_keys.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_read_binary.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_verify.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_reset_retry.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_challenge.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_external_authenticate.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_mse.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_initialize.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_domain.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_wrap.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_keypair_gen.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_update_ef.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_delete_file.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_change_pin.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_gen.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_signature.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_unwrap.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_decrypt_asym.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_cipher_sym.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_derive_asym.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_extras.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_general_authenticate.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_session_pin.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_puk_auth.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_pso.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_bip_slip.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/cvc.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/files.c + ${CMAKE_CURRENT_LIST_DIR}/src/hsm/kek.c +) set(USB_ITF_CCID 1) set(USB_ITF_WCID 1) include(pico-keys-sdk/pico_keys_sdk_import.cmake) @@ -89,46 +87,45 @@ if(ESP_PLATFORM) endif() set(INCLUDES ${INCLUDES} - ${CMAKE_CURRENT_LIST_DIR}/src/hsm - ) -if(NOT ESP_PLATFORM) -target_sources(pico_hsm PUBLIC ${SOURCES}) -target_include_directories(pico_hsm PUBLIC ${INCLUDES}) - -target_compile_options(pico_hsm PUBLIC - -Wall + ${CMAKE_CURRENT_LIST_DIR}/src/hsm ) -if (NOT MSVC) +if(NOT ESP_PLATFORM) + target_sources(pico_hsm PUBLIC ${SOURCES}) + target_include_directories(pico_hsm PUBLIC ${INCLUDES}) + target_compile_options(pico_hsm PUBLIC - -Werror + -Wall ) -endif() - -if(ENABLE_EMULATION) - if (NOT MSVC) - target_compile_options(pico_hsm PUBLIC - -fdata-sections - -ffunction-sections - ) - endif() - if(APPLE) - target_link_options(pico_hsm PUBLIC - -Wl,-dead_strip - ) - elseif(MSVC) + if(NOT MSVC) target_compile_options(pico_hsm PUBLIC - -WX + -Werror ) + endif() - target_link_libraries(pico_hsm PUBLIC wsock32 ws2_32 Bcrypt) - else() - target_link_options(pico_hsm PUBLIC - -Wl,--gc-sections + if(ENABLE_EMULATION) + if(NOT MSVC) + target_compile_options(pico_hsm PUBLIC + -fdata-sections + -ffunction-sections + ) + endif() + if(APPLE) + target_link_options(pico_hsm PUBLIC + -Wl,-dead_strip + ) + elseif(MSVC) + target_compile_options(pico_hsm PUBLIC + -WX ) - endif (APPLE) - target_link_libraries(pico_hsm PRIVATE pthread m) -else() -target_link_libraries(pico_hsm PRIVATE pico_keys_sdk pico_stdlib pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id pico_aon_timer tinyusb_device tinyusb_board) -endif() + target_link_libraries(pico_hsm PUBLIC wsock32 ws2_32 Bcrypt) + else() + target_link_options(pico_hsm PUBLIC + -Wl,--gc-sections + ) + endif(APPLE) + target_link_libraries(pico_hsm PRIVATE pthread m) + else() + target_link_libraries(pico_hsm PRIVATE pico_keys_sdk pico_stdlib pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id pico_aon_timer tinyusb_device tinyusb_board) + endif() endif() From fc69f5e1b12f81033b16a834084160954cbee5e2 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Thu, 5 Sep 2024 07:46:25 +0200 Subject: [PATCH 06/60] Upgrade upload-artifact Signed-off-by: Pol Henarejos --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7095d23..6f65707 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,7 +36,7 @@ jobs: mkdir -p artifacts docker save pico-hsm-test:bullseye -o artifacts/docker-image.tar - name: Temporarily save image - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4.1.7 with: name: docker-artifact path: artifacts From 6da49336c961dfe4d84c6bb4072b2b582539fd27 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Thu, 5 Sep 2024 09:16:20 +0200 Subject: [PATCH 07/60] Use v4 for artifacts Signed-off-by: Pol Henarejos --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7095d23..31ea897 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,7 +36,7 @@ jobs: mkdir -p artifacts docker save pico-hsm-test:bullseye -o artifacts/docker-image.tar - name: Temporarily save image - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: docker-artifact path: artifacts @@ -54,7 +54,7 @@ jobs: with: submodules: recursive - name: Retrieve saved image - uses: actions/download-artifact@v4.1.7 + uses: actions/download-artifact@v4 with: name: docker-artifact path: artifacts From 95f3a464b149e3a9c9d62bb3a54d4d785b9b01ca Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 6 Sep 2024 15:16:34 +0200 Subject: [PATCH 08/60] Use internal TRNG of Pico. Signed-off-by: Pol Henarejos --- CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 22097f5..a57b82e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,7 +125,5 @@ if(NOT ESP_PLATFORM) ) endif(APPLE) target_link_libraries(pico_hsm PRIVATE pthread m) - else() - target_link_libraries(pico_hsm PRIVATE pico_keys_sdk pico_stdlib pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id pico_aon_timer tinyusb_device tinyusb_board) endif() endif() From 72eb5a2a6922d055066dd6dee7e35cd746f7bc21 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 11 Sep 2024 23:16:23 +0200 Subject: [PATCH 09/60] Enable OTP to store a permanent secret key. It can be used by HSM or Fido to protect the keys and use it as MKEK. --- src/hsm/cmd_initialize.c | 12 ++----- src/hsm/kek.c | 78 +++++++++------------------------------- 2 files changed, 19 insertions(+), 71 deletions(-) diff --git a/src/hsm/cmd_initialize.c b/src/hsm/cmd_initialize.c index e7f833c..710a330 100644 --- a/src/hsm/cmd_initialize.c +++ b/src/hsm/cmd_initialize.c @@ -219,19 +219,11 @@ int cmd_initialize() { return SW_EXEC_ERROR(); } - const uint8_t *keyid = - (const uint8_t *) "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", + const uint8_t *keyid = (const uint8_t *) "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", *label = (const uint8_t *) "ESPICOHSMTR"; - uint16_t prkd_len = asn1_build_prkd_ecc(label, - (uint16_t)strlen((const char *) label), - keyid, - 20, - 256, - res_APDU, - 4096); + uint16_t prkd_len = asn1_build_prkd_ecc(label, (uint16_t)strlen((const char *) label), keyid, 20, 256, res_APDU, 4096); fpk = search_file(EF_PRKD_DEV); ret = file_put_data(fpk, res_APDU, prkd_len); - } if (ret != 0) { return SW_EXEC_ERROR(); diff --git a/src/hsm/kek.c b/src/hsm/kek.c index b1bab61..8f065bd 100644 --- a/src/hsm/kek.c +++ b/src/hsm/kek.c @@ -29,6 +29,7 @@ #include "mbedtls/ecdsa.h" #include "mbedtls/chachapoly.h" #include "files.h" +#include "otp.h" extern bool has_session_pin, has_session_sopin; extern uint8_t session_pin[32], session_sopin[32]; @@ -72,19 +73,19 @@ int load_mkek(uint8_t *mkek) { return CCID_EXEC_ERROR; } - if (has_mkek_mask) { - for (int i = 0; i < MKEK_KEY_SIZE; i++) { - MKEK_KEY(mkek)[i] ^= mkek_mask[i]; - } - } - int ret = - aes_decrypt_cfb_256(pin, MKEK_IV(mkek), MKEK_KEY(mkek), MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE); + int ret = aes_decrypt_cfb_256(pin, MKEK_IV(mkek), MKEK_KEY(mkek), MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE); if (ret != 0) { return CCID_EXEC_ERROR; } if (crc32c(MKEK_KEY(mkek), MKEK_KEY_SIZE) != *(uint32_t *) MKEK_CHECKSUM(mkek)) { return CCID_WRONG_DKEK; } + if (has_mkek_mask || otp_key_1) { + const uint8_t *mask = otp_key_1 ? otp_key_1 : mkek_mask; + for (int i = 0; i < MKEK_KEY_SIZE; i++) { + MKEK_KEY(mkek)[i] ^= mask[i]; + } + } return CCID_OK; } @@ -94,14 +95,7 @@ int mse_decrypt_ct(uint8_t *data, size_t len) { mbedtls_chachapoly_context chatx; mbedtls_chachapoly_init(&chatx); mbedtls_chachapoly_setkey(&chatx, mse.key_enc + 12); - int ret = mbedtls_chachapoly_auth_decrypt(&chatx, - len - 16, - mse.key_enc, - mse.Qpt, - 65, - data + len - 16, - data, - data); + int ret = mbedtls_chachapoly_auth_decrypt(&chatx, len - 16, mse.key_enc, mse.Qpt, 65, data + len - 16, data, data); mbedtls_chachapoly_free(&chatx); return ret; } @@ -141,10 +135,7 @@ int store_mkek(const uint8_t *mkek) { release_mkek(tmp_mkek_pin); return CCID_ERR_FILE_NOT_FOUND; } - aes_encrypt_cfb_256(session_pin, - MKEK_IV(tmp_mkek_pin), - MKEK_KEY(tmp_mkek_pin), - MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE); + aes_encrypt_cfb_256(session_pin, MKEK_IV(tmp_mkek_pin), MKEK_KEY(tmp_mkek_pin), MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE); file_put_data(tf, tmp_mkek_pin, MKEK_SIZE); release_mkek(tmp_mkek_pin); } @@ -157,10 +148,7 @@ int store_mkek(const uint8_t *mkek) { release_mkek(tmp_mkek_sopin); return CCID_ERR_FILE_NOT_FOUND; } - aes_encrypt_cfb_256(session_sopin, - MKEK_IV(tmp_mkek_sopin), - MKEK_KEY(tmp_mkek_sopin), - MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE); + aes_encrypt_cfb_256(session_sopin, MKEK_IV(tmp_mkek_sopin), MKEK_KEY(tmp_mkek_sopin), MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE); file_put_data(tf, tmp_mkek_sopin, MKEK_SIZE); release_mkek(tmp_mkek_sopin); } @@ -278,13 +266,7 @@ int mkek_decrypt(uint8_t *data, uint16_t len) { return r; } -int dkek_encode_key(uint8_t id, - void *key_ctx, - int key_type, - uint8_t *out, - uint16_t *out_len, - const uint8_t *allowed, - uint16_t allowed_len) { +int dkek_encode_key(uint8_t id, void *key_ctx, int key_type, uint8_t *out, uint16_t *out_len, const uint8_t *allowed, uint16_t allowed_len) { if (!(key_type & PICO_KEYS_KEY_RSA) && !(key_type & PICO_KEYS_KEY_EC) && !(key_type & PICO_KEYS_KEY_AES)) { return CCID_WRONG_DATA; } @@ -386,12 +368,7 @@ int dkek_encode_key(uint8_t id, kb_len += (uint16_t)mbedtls_mpi_size(&ecdsa->grp.N); size_t olen = 0; - mbedtls_ecp_point_write_binary(&ecdsa->grp, - &ecdsa->grp.G, - MBEDTLS_ECP_PF_UNCOMPRESSED, - &olen, - kb + 8 + kb_len + 2, - sizeof(kb) - 8 - kb_len - 2); + mbedtls_ecp_point_write_binary(&ecdsa->grp, &ecdsa->grp.G, MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, kb + 8 + kb_len + 2, sizeof(kb) - 8 - kb_len - 2); put_uint16_t((uint16_t)olen, kb + 8 + kb_len); kb_len += 2 + (uint16_t)olen; @@ -399,12 +376,7 @@ int dkek_encode_key(uint8_t id, mbedtls_mpi_write_binary(&ecdsa->d, kb + 8 + kb_len, mbedtls_mpi_size(&ecdsa->d)); kb_len += (uint16_t)mbedtls_mpi_size(&ecdsa->d); - mbedtls_ecp_point_write_binary(&ecdsa->grp, - &ecdsa->Q, - MBEDTLS_ECP_PF_UNCOMPRESSED, - &olen, - kb + 8 + kb_len + 2, - sizeof(kb) - 8 - kb_len - 2); + mbedtls_ecp_point_write_binary(&ecdsa->grp, &ecdsa->Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, kb + 8 + kb_len + 2, sizeof(kb) - 8 - kb_len - 2); put_uint16_t((uint16_t)olen, kb + 8 + kb_len); kb_len += 2 + (uint16_t)olen; @@ -465,12 +437,7 @@ int dkek_encode_key(uint8_t id, memcpy(out + *out_len, kb, kb_len_pad); *out_len += kb_len_pad; - r = mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_ECB), - kmac, - 256, - out, - *out_len, - out + *out_len); + r = mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_ECB), kmac, 256, out, *out_len, out + *out_len); *out_len += 16; if (r != 0) { @@ -492,13 +459,7 @@ int dkek_type_key(const uint8_t *in) { return 0x0; } -int dkek_decode_key(uint8_t id, - void *key_ctx, - const uint8_t *in, - uint16_t in_len, - int *key_size_out, - uint8_t **allowed, - uint16_t *allowed_len) { +int dkek_decode_key(uint8_t id, void *key_ctx, const uint8_t *in, uint16_t in_len, int *key_size_out, uint8_t **allowed, uint16_t *allowed_len) { uint8_t kcv[8]; int r = 0; memset(kcv, 0, sizeof(kcv)); @@ -526,12 +487,7 @@ int dkek_decode_key(uint8_t id, } uint8_t signature[16]; - r = mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_ECB), - kmac, - 256, - in, - in_len - 16, - signature); + r = mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_ECB), kmac, 256, in, in_len - 16, signature); if (r != 0) { return CCID_WRONG_SIGNATURE; } From c3ddfe96895ea7eea4bf0f3d93a61d947a084466 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 11 Sep 2024 23:16:51 +0200 Subject: [PATCH 10/60] Use OTP Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 697e2fd..108cfec 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 697e2fd2637e96a09f074905836b69fe5020b4a7 +Subproject commit 108cfec47c8b72472acbf6d3f8cc50260bfb09bd From 33a6a7045718649dfb889073126f97b1506d7980 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 13 Sep 2024 00:27:16 +0200 Subject: [PATCH 11/60] Use sha256 hardware if available. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 108cfec..3d52921 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 108cfec47c8b72472acbf6d3f8cc50260bfb09bd +Subproject commit 3d52921ef50ee969194d2137e213c5d6b543b435 From cf44794bb659278a0c694cac1eb1774060b086db Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 13 Sep 2024 08:47:41 +0200 Subject: [PATCH 12/60] Fix build. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 9f65a2c..1bf323c 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 9f65a2cfa024b721a6b7c16863e00558ac1a6f88 +Subproject commit 1bf323c36789e7c1a9273ca7ae5f3ad221fcbef5 From 1a2bff33a8b8f5fe959a8f426117b87138d6bc88 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 16 Sep 2024 00:51:21 +0200 Subject: [PATCH 13/60] Added ESP32 OTP support. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 3d52921..8053085 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 3d52921ef50ee969194d2137e213c5d6b543b435 +Subproject commit 805308515ed0034955c9a56ec9c6225a5b838dab From 15664da61dae2d091784229627a993f7263caa1d Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 16 Sep 2024 00:54:47 +0200 Subject: [PATCH 14/60] Added ESP32 OTP support. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 8053085..739e9f1 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 805308515ed0034955c9a56ec9c6225a5b838dab +Subproject commit 739e9f1b98c4f8aacedfa67a11df87d773ebf776 From 4454c633e4db00d6aad27aa587c3778d28e2e543 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 20 Sep 2024 07:16:55 +0200 Subject: [PATCH 15/60] Autobuild esp32 Signed-off-by: Pol Henarejos --- .github/workflows/codeql.yml | 2 +- workflows/autobuild.sh | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 21598af..b1149e3 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -36,7 +36,7 @@ jobs: language: [ 'cpp', 'python' ] # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support - mode: [ 'pico', 'local' ] + mode: [ 'pico', 'esp32', 'local' ] steps: - name: Checkout repository diff --git a/workflows/autobuild.sh b/workflows/autobuild.sh index eb9b4ae..3463160 100755 --- a/workflows/autobuild.sh +++ b/workflows/autobuild.sh @@ -2,6 +2,8 @@ git submodule update --init --recursive sudo apt update + +if [[ $1 == "pico" ]]; then sudo apt install -y cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib git clone https://github.com/raspberrypi/pico-sdk cd pico-sdk @@ -9,9 +11,20 @@ git submodule update --init cd .. mkdir build cd build -if [[ $1 == "pico" ]]; then cmake -DPICO_SDK_PATH=../pico-sdk .. -else -cmake -DENABLE_EMULATION=1 .. -fi make +elif [[ $1 == "esp32" ]]; then +sudo apt install -y git wget flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0 +git clone --recursive https://github.com/espressif/esp-idf.git +cd esp-idf +./install.sh esp32s3 +. ./export.sh +cd .. +idf.py set-target esp32s3 +idf.py all +else +mkdir build +cd build +cmake -DENABLE_EMULATION=1 .. +make +fi From d0461f81f40926b74cc095e5a02f5e496185f042 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 20 Sep 2024 07:19:15 +0200 Subject: [PATCH 16/60] Upgrade codeaction to v3 Signed-off-by: Pol Henarejos --- .github/workflows/codeql.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index b1149e3..005214e 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -44,7 +44,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -71,4 +71,4 @@ jobs: ./workflows/autobuild.sh ${{ matrix.mode }} - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v3 From 4b314470b4765064145c34bc6045e63fb4d8f125 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 20 Sep 2024 07:19:47 +0200 Subject: [PATCH 17/60] Fix header in Linux. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 739e9f1..839e824 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 739e9f1b98c4f8aacedfa67a11df87d773ebf776 +Subproject commit 839e8244d95aeef8f83748f13a73781952185cca From 9f1e5d0abd85302899199fef0b65a74b16c160d6 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 24 Sep 2024 00:44:38 +0200 Subject: [PATCH 18/60] Update pico_sdk_import Signed-off-by: Pol Henarejos --- pico_sdk_import.cmake | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/pico_sdk_import.cmake b/pico_sdk_import.cmake index 28efe9e..a0721d0 100644 --- a/pico_sdk_import.cmake +++ b/pico_sdk_import.cmake @@ -18,9 +18,20 @@ if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_P message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") endif () +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG)) + set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG}) + message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')") +endif () + +if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG) + set(PICO_SDK_FETCH_FROM_GIT_TAG "master") + message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG") +endif() + set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") +set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK") if (NOT PICO_SDK_PATH) if (PICO_SDK_FETCH_FROM_GIT) @@ -29,11 +40,22 @@ if (NOT PICO_SDK_PATH) if (PICO_SDK_FETCH_FROM_GIT_PATH) get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") endif () - FetchContent_Declare( - pico_sdk - GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk - GIT_TAG master - ) + # GIT_SUBMODULES_RECURSE was added in 3.17 + if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0") + FetchContent_Declare( + pico_sdk + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + GIT_SUBMODULES_RECURSE FALSE + ) + else () + FetchContent_Declare( + pico_sdk + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + ) + endif () + if (NOT pico_sdk) message("Downloading Raspberry Pi Pico SDK") FetchContent_Populate(pico_sdk) From 1a8c8440cf9ae6a37c5103c6bfe50b5b2de1b7ac Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 30 Sep 2024 09:08:15 +0200 Subject: [PATCH 19/60] Add support for LED management Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 839e824..e2b3eac 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 839e8244d95aeef8f83748f13a73781952185cca +Subproject commit e2b3eacd89a0e857ef5c6b56ff4661fa0fba7242 From f7451f56edb3e13e49aebbd52ddb2fdf2ad0e195 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 30 Sep 2024 09:21:39 +0200 Subject: [PATCH 20/60] Add support for led dimming and max. brightness. Signed-off-by: Pol Henarejos --- src/hsm/cmd_extras.c | 6 +++--- tools/pico-hsm-tool.py | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/hsm/cmd_extras.c b/src/hsm/cmd_extras.c index 290318a..14514d3 100644 --- a/src/hsm/cmd_extras.c +++ b/src/hsm/cmd_extras.c @@ -212,7 +212,7 @@ int cmd_extras() { memcpy(tmp + PHY_VID, apdu.data, 4); opts |= PHY_OPT_VPID; } - else if (P2(apdu) == PHY_LED_GPIO || P2(apdu) == PHY_LED_MODE) { + else if (P2(apdu) == PHY_LED_GPIO || P2(apdu) == PHY_LED_BTNESS) { if (apdu.nc != 1) { return SW_WRONG_LENGTH(); } @@ -220,8 +220,8 @@ int cmd_extras() { if (P2(apdu) == PHY_LED_GPIO) { opts |= PHY_OPT_GPIO; } - else if (P2(apdu) == PHY_LED_MODE) { - opts |= PHY_OPT_LED; + else if (P2(apdu) == PHY_LED_BTNESS) { + opts |= PHY_OPT_BTNESS; } } else if (P2(apdu) == PHY_OPTS) { diff --git a/tools/pico-hsm-tool.py b/tools/pico-hsm-tool.py index 762481c..42169ad 100644 --- a/tools/pico-hsm-tool.py +++ b/tools/pico-hsm-tool.py @@ -94,11 +94,15 @@ def parse_args(): parser_phy = subparser.add_parser('phy', help='Set PHY options.') subparser_phy = parser_phy.add_subparsers(title='commands', dest='subcommand', required=True) parser_phy_vp = subparser_phy.add_parser('vidpid', help='Sets VID/PID. Use VID:PID format (e.g. 1234:5678)') - parser_phy_ledn = subparser_phy.add_parser('led', help='Sets LED GPIO number.') + parser_phy_ledn = subparser_phy.add_parser('led_gpio', help='Sets LED GPIO number.') parser_phy_optwcid = subparser_phy.add_parser('wcid', help='Enable/Disable Web CCID interface.') parser_phy_vp.add_argument('value', help='Value of the PHY option.', metavar='VAL', nargs='?') parser_phy_ledn.add_argument('value', help='Value of the PHY option.', metavar='VAL', nargs='?') parser_phy_optwcid.add_argument('value', choices=['enable', 'disable'], help='Enable/Disable Web CCID interface.', nargs='?') + parser_phy_ledbtness = subparser_phy.add_parser('led_brightness', help='Sets LED max. brightness.') + parser_phy_ledbtness.add_argument('value', help='Value of the max. brightness.', metavar='VAL', nargs='?') + parser_phy_optdimm = subparser_phy.add_parser('led_dimmable', help='Enable/Disable LED dimming.') + parser_phy_optdimm.add_argument('value', choices=['enable', 'disable'], help='Enable/Disable LED dimming.', nargs='?') parser_secure = subparser.add_parser('secure', help='Manages security of Pico HSM.') subparser_secure = parser_secure.add_subparsers(title='commands', dest='subcommand', required=True) @@ -460,10 +464,15 @@ def phy(picohsm, args): sp = val.split(':') if (len(sp) != 2): print('ERROR: VID/PID have wrong format. Use VID:PID format (e.g. 1234:5678)') + return val = int(sp[0],16).to_bytes(2, 'big') + int(sp[1],16).to_bytes(2, 'big') - elif (args.subcommand == 'led'): + elif (args.subcommand in ['led_gpio', 'led_brightness']): + if (args.subcommand == 'led_brightness'): + if (int(val) > 15 or int(val) < 0): + print('ERROR: LED brightness must be between 0 and 15.') + return val = [int(val)] - elif (args.subcommand == 'wcid'): + elif (args.subcommand in ['wcid', 'led_dimmable']): val = val == 'enable' ret = picohsm.phy(args.subcommand, val) if (ret): @@ -472,7 +481,7 @@ def phy(picohsm, args): print('Command executed successfully. Please, restart your Pico Key.') def main(args): - sys.stderr.buffer.write(b'Pico HSM Tool v1.14\n') + sys.stderr.buffer.write(b'Pico HSM Tool v1.16\n') sys.stderr.buffer.write(b'Author: Pol Henarejos\n') sys.stderr.buffer.write(b'Report bugs to https://github.com/polhenarejos/pico-hsm/issues\n') sys.stderr.buffer.write(b'\n\n') From 954eb1d8908a138e92b64c57d00975d6162c83be Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 30 Sep 2024 09:22:23 +0200 Subject: [PATCH 21/60] Add nightly builds workflow Signed-off-by: Pol Henarejos --- .github/workflows/nightly.yml | 34 ++++++++++++++++++++++++++++++++++ build_pico_hsm.sh | 13 +++++++++---- workflows/autobuild.sh | 9 +++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/nightly.yml diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml new file mode 100644 index 0000000..beee277 --- /dev/null +++ b/.github/workflows/nightly.yml @@ -0,0 +1,34 @@ +name: "Nightly deploy" + +on: + schedule: + - cron: '0 2 * * *' + workflow_dispatch: + +jobs: + nightly: + name: Deploy nightly + strategy: + fail-fast: false + matrix: + refs: [main, development] + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + ref: ${{ matrix.refs }} + submodules: 'recursive' + - name : Build + env: + PICO_SDK_PATH: ../pico-sdk + run: | + ./workflows/autobuild.sh pico + ./build_pico_hsm.sh + - name: Update nightly release + uses: pyTooling/Actions/releaser@main + with: + tag: nightly-${{ matrix.refs }} + rm: true + token: ${{ secrets.GITHUB_TOKEN }} + files: release/*.* diff --git a/build_pico_hsm.sh b/build_pico_hsm.sh index f67de9c..66af564 100755 --- a/build_pico_hsm.sh +++ b/build_pico_hsm.sh @@ -2,8 +2,14 @@ VERSION_MAJOR="4" VERSION_MINOR="2" +SUFFIX="${VERSION_MAJOR}.${VERSION_MINOR}" +#if ! [[ -z "${GITHUB_SHA}" ]]; then +# SUFFIX="${SUFFIX}.${GITHUB_SHA}" +#fi rm -rf release/* +mkdir -p build_release +mkdir -p release cd build_release for board in 0xcb_helios \ @@ -96,8 +102,7 @@ for board in 0xcb_helios \ wiznet_w5100s_evb_pico do rm -rf * - PICO_SDK_PATH=~/Devel/pico/pico-sdk cmake .. -DPICO_BOARD=$board - make -kj20 - mv pico_hsm.uf2 ../release/pico_hsm_$board-$VERSION_MAJOR.$VERSION_MINOR.uf2 - + PICO_SDK_PATH="${PICO_SDK_PATH:-../../pico-sdk}" cmake .. -DPICO_BOARD=$board + make -j`nproc` + mv pico_hsm.uf2 ../release/pico_hsm_$board-$SUFFIX.uf2 done diff --git a/workflows/autobuild.sh b/workflows/autobuild.sh index 3463160..d90e1a4 100755 --- a/workflows/autobuild.sh +++ b/workflows/autobuild.sh @@ -9,8 +9,17 @@ git clone https://github.com/raspberrypi/pico-sdk cd pico-sdk git submodule update --init cd .. +git clone https://github.com/raspberrypi/picotool +cd picotool +git submodule update --init mkdir build cd build +cmake -DPICO_SDK_PATH=../../pico-sdk .. +make -j`nproc` +sudo make install +cd ../.. +mkdir build_pico +cd build_pico cmake -DPICO_SDK_PATH=../pico-sdk .. make elif [[ $1 == "esp32" ]]; then From 4bea47facaa2988d23377fde6f8c7d394fe2a647 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 30 Sep 2024 09:25:06 +0200 Subject: [PATCH 22/60] Add nightly builds workflow Signed-off-by: Pol Henarejos --- .github/workflows/nightly.yml | 34 ++++++++++++++++++++++++++++++++++ build_pico_hsm.sh | 13 +++++++++---- workflows/autobuild.sh | 30 ++++++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/nightly.yml diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml new file mode 100644 index 0000000..beee277 --- /dev/null +++ b/.github/workflows/nightly.yml @@ -0,0 +1,34 @@ +name: "Nightly deploy" + +on: + schedule: + - cron: '0 2 * * *' + workflow_dispatch: + +jobs: + nightly: + name: Deploy nightly + strategy: + fail-fast: false + matrix: + refs: [main, development] + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + ref: ${{ matrix.refs }} + submodules: 'recursive' + - name : Build + env: + PICO_SDK_PATH: ../pico-sdk + run: | + ./workflows/autobuild.sh pico + ./build_pico_hsm.sh + - name: Update nightly release + uses: pyTooling/Actions/releaser@main + with: + tag: nightly-${{ matrix.refs }} + rm: true + token: ${{ secrets.GITHUB_TOKEN }} + files: release/*.* diff --git a/build_pico_hsm.sh b/build_pico_hsm.sh index f67de9c..66af564 100755 --- a/build_pico_hsm.sh +++ b/build_pico_hsm.sh @@ -2,8 +2,14 @@ VERSION_MAJOR="4" VERSION_MINOR="2" +SUFFIX="${VERSION_MAJOR}.${VERSION_MINOR}" +#if ! [[ -z "${GITHUB_SHA}" ]]; then +# SUFFIX="${SUFFIX}.${GITHUB_SHA}" +#fi rm -rf release/* +mkdir -p build_release +mkdir -p release cd build_release for board in 0xcb_helios \ @@ -96,8 +102,7 @@ for board in 0xcb_helios \ wiznet_w5100s_evb_pico do rm -rf * - PICO_SDK_PATH=~/Devel/pico/pico-sdk cmake .. -DPICO_BOARD=$board - make -kj20 - mv pico_hsm.uf2 ../release/pico_hsm_$board-$VERSION_MAJOR.$VERSION_MINOR.uf2 - + PICO_SDK_PATH="${PICO_SDK_PATH:-../../pico-sdk}" cmake .. -DPICO_BOARD=$board + make -j`nproc` + mv pico_hsm.uf2 ../release/pico_hsm_$board-$SUFFIX.uf2 done diff --git a/workflows/autobuild.sh b/workflows/autobuild.sh index eb9b4ae..d90e1a4 100755 --- a/workflows/autobuild.sh +++ b/workflows/autobuild.sh @@ -2,16 +2,38 @@ git submodule update --init --recursive sudo apt update + +if [[ $1 == "pico" ]]; then sudo apt install -y cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib git clone https://github.com/raspberrypi/pico-sdk cd pico-sdk git submodule update --init cd .. +git clone https://github.com/raspberrypi/picotool +cd picotool +git submodule update --init mkdir build cd build -if [[ $1 == "pico" ]]; then +cmake -DPICO_SDK_PATH=../../pico-sdk .. +make -j`nproc` +sudo make install +cd ../.. +mkdir build_pico +cd build_pico cmake -DPICO_SDK_PATH=../pico-sdk .. -else -cmake -DENABLE_EMULATION=1 .. -fi make +elif [[ $1 == "esp32" ]]; then +sudo apt install -y git wget flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0 +git clone --recursive https://github.com/espressif/esp-idf.git +cd esp-idf +./install.sh esp32s3 +. ./export.sh +cd .. +idf.py set-target esp32s3 +idf.py all +else +mkdir build +cd build +cmake -DENABLE_EMULATION=1 .. +make +fi From 9cd7bc7b3edda064a4324367130644901df84c92 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 30 Sep 2024 09:26:59 +0200 Subject: [PATCH 23/60] Fix nightly build for master branch Signed-off-by: Pol Henarejos --- .github/workflows/nightly.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index beee277..c9455e2 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - refs: [main, development] + refs: [master, development] runs-on: ubuntu-latest steps: - name: Checkout repository From 3b083f9bd704e695fbe503804db1b77e076871ee Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 30 Sep 2024 09:27:22 +0200 Subject: [PATCH 24/60] Fix nightly build for master branch Signed-off-by: Pol Henarejos --- .github/workflows/nightly.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index beee277..c9455e2 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - refs: [main, development] + refs: [master, development] runs-on: ubuntu-latest steps: - name: Checkout repository From 48d92f53a01d7e41eaf8499114c0f3f44793165d Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 1 Oct 2024 14:38:44 +0200 Subject: [PATCH 25/60] Update Dockerfile with latest versions. Signed-off-by: Pol Henarejos --- Dockerfile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index ecb2f54..d3a37bc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ WORKDIR /home/builduser VOLUME /home/builduser/release -ARG VERSION_PICO_SDK 1.4.0 +ARG VERSION_PICO_SDK 2.0.0 RUN mkdir -p /home/builduser/Devel/pico RUN cd /home/builduser/Devel/pico \ @@ -29,8 +29,8 @@ RUN cd /home/builduser/Devel/pico \ RUN pip install cryptography -ARG VERSION_MAJOR 2 -ARG VERSION_MINOR 6 +ARG VERSION_MAJOR 4 +ARG VERSION_MINOR 2 RUN cd /home/builduser \ && git clone https://github.com/polhenarejos/pico-hsm.git \ @@ -46,7 +46,7 @@ ENV PICO_SDK_PATH /home/builduser/Devel/pico/pico-sdk ARG USB_VID 0xfeff ARG USB_PID 0xfcfd -ARG PICO_BOARD waveshare_rp2040_zero +ARG PICO_BOARD waveshare_rp2040_zero RUN cd /home/builduser/pico-hsm \ && cd build_release \ From 9dc3ea323298681fd5a7aa7f0af17443291c8ca4 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 1 Oct 2024 14:38:56 +0200 Subject: [PATCH 26/60] Fix emulation alignment. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index e2b3eac..84c3efd 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit e2b3eacd89a0e857ef5c6b56ff4661fa0fba7242 +Subproject commit 84c3efd78292ebbb8473df667ab80310488d30a7 From 0193e55f7b4857d67e38fc72274a4ee4dfc0ce85 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 13 Oct 2024 20:06:58 +0200 Subject: [PATCH 27/60] Use macros in extras. Signed-off-by: Pol Henarejos --- src/hsm/cmd_extras.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/hsm/cmd_extras.c b/src/hsm/cmd_extras.c index 14514d3..efded5a 100644 --- a/src/hsm/cmd_extras.c +++ b/src/hsm/cmd_extras.c @@ -29,6 +29,15 @@ #include "mbedtls/hkdf.h" #include "mbedtls/chachapoly.h" +#define CMD_DATETIME 0xA +#define CMD_DYNOPS 0x6 +#define CMD_SECURE_LOCK 0x3A +#define SECURE_LOCK_KEY_AGREEMENT 0x1 +#define SECURE_LOCK_ENABLE 0x2 +#define SECURE_LOCK_MASK 0x3 +#define SECURE_LOCK_DISABLE 0x4 +#define CMD_PHY 0x1B + int cmd_extras() { #ifndef ENABLE_EMULATION // Only allow change PHY without PIN @@ -40,7 +49,7 @@ int cmd_extras() { if (wait_button_pressed() == true) { return SW_SECURE_MESSAGE_EXEC_ERROR(); } - if (P1(apdu) == 0xA) { //datetime operations + if (P1(apdu) == CMD_DATETIME) { //datetime operations if (P2(apdu) != 0x0) { return SW_INCORRECT_P1P2(); } @@ -84,7 +93,7 @@ int cmd_extras() { #endif } } - else if (P1(apdu) == 0x6) { //dynamic options + else if (P1(apdu) == CMD_DYNOPS) { //dynamic options if (P2(apdu) != 0x0) { return SW_INCORRECT_P1P2(); } @@ -103,11 +112,11 @@ int cmd_extras() { low_flash_available(); } } - else if (P1(apdu) == 0x3A) { // secure lock + else if (P1(apdu) == CMD_SECURE_LOCK) { // secure lock if (apdu.nc == 0) { return SW_WRONG_LENGTH(); } - if (P2(apdu) == 0x01) { // Key Agreement + if (P2(apdu) == SECURE_LOCK_KEY_AGREEMENT) { // Key Agreement mbedtls_ecdh_context hkey; mbedtls_ecdh_init(&hkey); mbedtls_ecdh_setup(&hkey, MBEDTLS_ECP_DP_SECP256R1); @@ -143,7 +152,7 @@ int cmd_extras() { mse.init = true; res_APDU_size = (uint16_t)olen; } - else if (P2(apdu) == 0x02 || P2(apdu) == 0x03 || P2(apdu) == 0x04) { + else if (P2(apdu) == SECURE_LOCK_ENABLE || P2(apdu) == SECURE_LOCK_MASK || P2(apdu) == SECURE_LOCK_DISABLE) { if (mse.init == false) { return SW_COMMAND_NOT_ALLOWED(); } @@ -152,11 +161,11 @@ int cmd_extras() { if (ret != 0) { return SW_WRONG_DATA(); } - if (P2(apdu) == 0x02 || P2(apdu) == 0x04) { // Enable + if (P2(apdu) == SECURE_LOCK_ENABLE || P2(apdu) == SECURE_LOCK_DISABLE) { // Enable uint16_t opts = get_device_options(); uint8_t newopts[] = { opts >> 8, (opts & 0xff) }; - if ((P2(apdu) == 0x02 && !(opts & HSM_OPT_SECURE_LOCK)) || - (P2(apdu) == 0x04 && (opts & HSM_OPT_SECURE_LOCK))) { + if ((P2(apdu) == SECURE_LOCK_ENABLE && !(opts & HSM_OPT_SECURE_LOCK)) || + (P2(apdu) == SECURE_LOCK_DISABLE && (opts & HSM_OPT_SECURE_LOCK))) { uint16_t tfids[] = { EF_MKEK, EF_MKEK_SO }; for (int t = 0; t < sizeof(tfids) / sizeof(uint16_t); t++) { file_t *tf = search_file(tfids[t]); @@ -171,24 +180,24 @@ int cmd_extras() { } } } - if (P2(apdu) == 0x02) { + if (P2(apdu) == SECURE_LOCK_ENABLE) { newopts[0] |= HSM_OPT_SECURE_LOCK >> 8; } - else if (P2(apdu) == 0x04) { + else if (P2(apdu) == SECURE_LOCK_DISABLE) { newopts[0] &= ~HSM_OPT_SECURE_LOCK >> 8; } file_t *tf = search_file(EF_DEVOPS); file_put_data(tf, newopts, sizeof(newopts)); low_flash_available(); } - else if (P2(apdu) == 0x03) { + else if (P2(apdu) == SECURE_LOCK_MASK) { memcpy(mkek_mask, apdu.data, apdu.nc); has_mkek_mask = true; } } } #ifndef ENABLE_EMULATION - else if (P1(apdu) == 0x1B) { // Set PHY + else if (P1(apdu) == CMD_PHY) { // Set PHY if (apdu.nc == 0) { if (file_has_data(ef_phy)) { res_APDU_size = file_get_size(ef_phy); From 08d4dc58aa29030b3d005f814639dd8a1c9fe68d Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 13 Oct 2024 20:25:59 +0200 Subject: [PATCH 28/60] Add OTP extra command to read/write OTP without bootmode. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- src/hsm/cmd_extras.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 84c3efd..6216cd2 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 84c3efd78292ebbb8473df667ab80310488d30a7 +Subproject commit 6216cd24be025d6ccd43c3358cc48ec7eac4556f diff --git a/src/hsm/cmd_extras.c b/src/hsm/cmd_extras.c index efded5a..375a1b3 100644 --- a/src/hsm/cmd_extras.c +++ b/src/hsm/cmd_extras.c @@ -37,6 +37,7 @@ #define SECURE_LOCK_MASK 0x3 #define SECURE_LOCK_DISABLE 0x4 #define CMD_PHY 0x1B +#define CMD_OTP 0x4C int cmd_extras() { #ifndef ENABLE_EMULATION @@ -249,6 +250,29 @@ int cmd_extras() { low_flash_available(); } } +#endif +#if RP2350 + else if (P1(apdu) == CMD_OTP) { + if (apdu.nc < 2) { + return SW_WRONG_LENGTH(); + } + uint16_t row = (apdu.data[0] << 8) | apdu.data[1]; + apdu.nc -= 2; + apdu.data += 2; + if (apdu.nc == 2) { + memcpy(res_APDU, otp_buffer(row), apdu.ne); + res_APDU_size = apdu.ne; + } + else { + if (!(apdu.nc % 16)) { + return SW_WRONG_DATA(); + } + int ret = otp_write_data(row, apdu.data, apdu.nc); + if (ret != 0) { + return SW_EXEC_ERROR(); + } + } + } #endif else { return SW_INCORRECT_P1P2(); From 6163b870edde1dda74a6ccfebe61af70ebf87f91 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 13 Oct 2024 20:51:18 +0200 Subject: [PATCH 29/60] Fix haders. Signed-off-by: Pol Henarejos --- src/hsm/cmd_extras.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/hsm/cmd_extras.c b/src/hsm/cmd_extras.c index 375a1b3..6b9fd90 100644 --- a/src/hsm/cmd_extras.c +++ b/src/hsm/cmd_extras.c @@ -28,6 +28,9 @@ #include "kek.h" #include "mbedtls/hkdf.h" #include "mbedtls/chachapoly.h" +#ifdef PICO_RP2350 +#include "otp.h" +#endif #define CMD_DATETIME 0xA #define CMD_DYNOPS 0x6 @@ -251,7 +254,7 @@ int cmd_extras() { } } #endif -#if RP2350 +#if PICO_RP2350 else if (P1(apdu) == CMD_OTP) { if (apdu.nc < 2) { return SW_WRONG_LENGTH(); From 812a737ff5fbc2bdff33f6099a1a8d38f7842d3c Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 13 Oct 2024 20:52:29 +0200 Subject: [PATCH 30/60] Fix length. Signed-off-by: Pol Henarejos --- src/hsm/cmd_extras.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hsm/cmd_extras.c b/src/hsm/cmd_extras.c index 6b9fd90..be01e54 100644 --- a/src/hsm/cmd_extras.c +++ b/src/hsm/cmd_extras.c @@ -260,13 +260,13 @@ int cmd_extras() { return SW_WRONG_LENGTH(); } uint16_t row = (apdu.data[0] << 8) | apdu.data[1]; - apdu.nc -= 2; - apdu.data += 2; if (apdu.nc == 2) { memcpy(res_APDU, otp_buffer(row), apdu.ne); res_APDU_size = apdu.ne; } else { + apdu.nc -= 2; + apdu.data += 2; if (!(apdu.nc % 16)) { return SW_WRONG_DATA(); } From 2b77519a3bfeccfb1dc1853e5f6dc6a4f4167349 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 15 Oct 2024 22:12:34 +0200 Subject: [PATCH 31/60] Add OTP command to Pico HSM Tool. Signed-off-by: Pol Henarejos --- tools/pico-hsm-tool.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tools/pico-hsm-tool.py b/tools/pico-hsm-tool.py index 42169ad..1b95331 100644 --- a/tools/pico-hsm-tool.py +++ b/tools/pico-hsm-tool.py @@ -135,10 +135,15 @@ def parse_args(): parser_keygen = subparser.add_parser('keygen', help='Generates private keypair or secret key.') subparser_keygen = parser_keygen.add_subparsers(title='commands', dest='subcommand', required=True) parser_keygen_aes = subparser_keygen.add_parser('aes', help='Generates an AES key.') - parser_keygen_aes.add_argument('--size', help='Specifies the size of AES key [128, 192 or 256]',choices=[128, 192, 256], default=128, type=int) + parser_keygen_aes.add_argument('--size', help='Specifies the size of AES key [128, 192 or 256]', choices=[128, 192, 256], default=128, type=int) parser_keygen_x25519 = subparser_keygen.add_parser('x25519', help='Generates a private X25519 keypair.') parser_keygen_x448 = subparser_keygen.add_parser('x448', help='Generates a private X448 keypair.') + parser_otp = subparser.add_parser('otp', help='Read/write OTP values.') + parser_otp.add_argument('subcommand', choices=['read', 'write'], help='Read/write.', nargs='?') + parser_otp.add_argument('--row', help='OTP row (in HEX)', required=True) + parser_otp.add_argument('-d', '--data', help='Data to write (in HEX) [e.g. 0011223344556677889900AABBCCDDEEFF]', required='write' in sys.argv) + args = parser.parse_args() return args @@ -480,8 +485,18 @@ def phy(picohsm, args): else: print('Command executed successfully. Please, restart your Pico Key.') +def otp(picohsm, args): + row = int(args.row, 16) + if (args.subcommand == 'read'): + ret = picohsm.otp(row=row) + print(f'OTP row {args.row}: {hexlify(ret).decode()}') + elif (args.subcommand == 'write'): + data = unhexlify(args.data) + picohsm.otp(row=row, data=data) + print(f'OTP row {args.row} written successfully.') + def main(args): - sys.stderr.buffer.write(b'Pico HSM Tool v1.16\n') + sys.stderr.buffer.write(b'Pico HSM Tool v1.18\n') sys.stderr.buffer.write(b'Author: Pol Henarejos\n') sys.stderr.buffer.write(b'Report bugs to https://github.com/polhenarejos/pico-hsm/issues\n') sys.stderr.buffer.write(b'\n\n') @@ -508,7 +523,8 @@ def main(args): keygen(picohsm, args) elif (args.command == 'phy'): phy(picohsm, args) - + elif (args.command == 'otp'): + otp(picohsm, args) def run(): args = parse_args() From 5388c1118299353ca89b5292147f5b7117970c2c Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 15 Oct 2024 22:54:18 +0200 Subject: [PATCH 32/60] Update flash memory for tests. Signed-off-by: Pol Henarejos --- tests/memory.tar.gz | Bin 9590 -> 8390144 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/tests/memory.tar.gz b/tests/memory.tar.gz index ac79a34fbe10cc42b46d5d6603469a1785e58ac6..994ea45f1d381af78d449f43fb0bbf609236e8b5 100644 GIT binary patch delta 11026 zcmeI2c~nz(7RTSq3t^XlAe4+66h)TmOIV@;LJ&#;*#rUCuoP+qL^h#9c}YM(WHBPk zD1{)5lvY$^sxa&>V6}}h1;(n(Agd(`BAYra^Imvw5KnV-q0s)PrJM?`(TF(fP?`k+YQw*`h_3m9es=*+P&m(4KSOg5XLHf%6i zW*pem+?>s^U^8KsIg7;xn6OCj$@19fn1CpBX-rf^Pz3sQ__H99KuZp_hJ=U&3^jZ` z791VSYC*4B^{Ec>Lp}qN0g?fd0g?fd0g?fd0g?fd0g?fd0g?fd0g?fd0g?fd0g?fd z0g?fd0g?fd0g?fd0g?fd0g?fd0g?fn0e=OgpaB7}0p8*QVjEahg&Gqa9RpElEmRPM z*HASB&pgilKy}aj&CCPhp8S=Gp}C%F5F`)tAqrGW0l`uwSOKO`0SW~K?C@Z>oA98L z5%5zXIE3g4!A!(W2-@=uQ7^?^-rKp@>kKReaT0_mI4%bKUg3ypiwTZ`U`ki=4|ImO3Y@6|XRsnEHmY4oL1@ zvpvXZ_MAFSoC6?E)-d$8szSRsWuorxjoF}eo~c|aQ%ltV9yxyCDWDk`nQ z3*#)eG1EYs9LbxelIK*8Bo!7X7v2A{biG60GSQh_^&+1mI(l=6p_$g;RLyXD-M&j5 zwM&bJ{RVFVfLIr%r~t466=b2{F!@V~`8R)W31q(41L$wg(G8<*&J1@{^v8d5wDN~| z?Wb&q!xslyzO4zTaqfLd@I`N4^EOF}zItZu{E*=PO6{1#% za0$kZ8JMdF{^uAzCu5$3$7S4@PS*nzk{^XR^|$vJ%?Tx~5H8 zl)86SI}u?3UnD>m>C-HOz>9<0DeT_7uBT>K8yQt!m-qjn;)rA8{WD_M3?p@Pi={99 zl%7&Tl;#%GrfkZC5Iw;l;wC7THP$;$?0pvd%gKFdbD==q@nLWW-A4rV{2u3UucX8+ zR5Q2CO6A+r2i0}ddrh+Td*02xdtz6PQASa{W{g?C{i@a&58fO8+_6a7hJbt#3L~^Rd3W;uk{dphBLk~eo&FIK12h`eAHAQWvta4&aI96=d|nDYeie? zk{P7AY@>em|SAF$@`204~41yBGLuyoO^ zK`NTAO>9ff!go5MK0}(3e|%;<%T64f(a&q}`8?nU@4r#Tw^l~(;f59{}>b=r#M{w=@e@Q2TYv=!1Iu8k3%I7b0yGuF4EgVQ319@RJ`N%xv{lIglTGmlzkTn=AaUa=`CBrd7d^y0n^lRl~0JQJ3O zz0`Z`J?SJk!AiKKf-S#NL9CPLpJ`+H@qf$dp-s z(y0@wwPM%Xa<;9>!w*Jm+Rl!3S=Bag-d3`Vw%l$vqtXtGl0KRQ4fv!b@TI3G{8B2S zxN!fT6xQ#pCefC zC+dpbcS1xUnJCUK%U}JCi+**?~W*&*jo{z9(_E}wMtvO#w2hdj=MAK)t<+*23tJ~<3DPPBzAp? z$5F!Lm@WXp~_QAgOd2SGuesnyOAk4^k~qrTz=5 C258y< delta 1517 zcmV00!P#?u7i1BjO=aEPXGC^(8+h;#S zi)iQWoW*>bk6~sw4C8d$_2+Ir78{9(CZc>~+tkmDl%JX7rL1y}Dy25%wv4K-A{Aw9 zb{~X00000000000JCZV;tyGHXW#Bw+KS>h z0N=cz#e%(c>7WQM1+%n@o0F|YGiU;WgD-R_4uuY#T`p0P)=q+ zlZzlIF$gYh847|o^9($J3qfS`hI+z z*ge{+O@D0^o~ADV0000000000000000B}#&&D|%=?2BqH8mKg*>B_gagPGt}_-3YD zd|jHHnHryc85@dmGa)+}9BZnwMPu2*hwU5zD+iC@o3?xWu_ z1SJF?==8=0o|=5BGqhi;YtC+w5|hU+atBKS;(vcO{H-cv(@2~)F-Nl1{Fi@TXc zy$>H7C)g5ZMkc0UMuvD*HYR2UW(Kx+6*eXoAcHmD++l|8Gh5Em?V_)5thCwOzcRU_ z>BM$6-@s+RPVKukd8ghT&OX=fLL+8JyKnb6BaXNioxXi2MIokI`gXVfX;m@jdZ0&N z0)IWa2+gC;jyOE3_|8l5V3xpl^N)HJPdTO4(uA0MZtdG$q%FEX*KtSr?{B`dw#~8J z*(@#ffcw3cU*q)rvW_c(ox7E_R0?dnrY%MGC@eI6gpfVT+5n0KkY||~I2buVo(1}u zkp<{k`_cpb|4&L}hdmQ1ocTigk5SFL?|&*&_k>j@o(;Lq^wG{nJ?+oMi_+ItUi`91 zb9=T*&+Az~dLx(na&7cvJ}C>X#C{TO3)k#V>w(sDI<+ z{$tOxr^!nkK9w`qHtN1MUhrk1rR$9uho<>Ic6#(rs(yCg^VeQA{PV=L&l!hj8^)bG zz0D!Re1G8AR+uy3R_}TCYwdFJc`cuJo;vg=Sv3$CcUesbmi$}qcx|C^*7KK>Zb)7E zp+0G%>s{XRI;m~8b;hbEzTZ0V-G4i8o$zw~N+RGT<-(<{Y5Q4!|Q%^Y6SmCUI`ZSChwe6FPZ! zEbu+b>Tt06`Gr^Z$7a31YJan1zlGg=J~m;eNa>YMAVWId)iFN_Yp-s2J?V2q%km<2 zE|-r>%6+UJy*5;8InFuF;K4=44?824P1SicH|YK+wI|rwJCfCi$)vU!bIu^uOFOsP|LU?LX>Pfio)H zjupZz{Q#x8u$Xh9Z;9yDTpx~$#qX+e_;nWV4S&3k`JJ>`;<=UHAGy}^O#HgB4d#wf TFbYP&C;$NRoa!q81_FQpj%WJd From 8e3801e9a2e8a1271ea0bc54c503a00689e646fb Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 15 Oct 2024 23:00:35 +0200 Subject: [PATCH 33/60] Revert "Update flash memory for tests." This reverts commit 5388c1118299353ca89b5292147f5b7117970c2c. --- tests/memory.tar.gz | Bin 8390144 -> 9590 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/tests/memory.tar.gz b/tests/memory.tar.gz index 994ea45f1d381af78d449f43fb0bbf609236e8b5..ac79a34fbe10cc42b46d5d6603469a1785e58ac6 100644 GIT binary patch delta 1517 zcmV00!P#?u7i1BjO=aEPXGC^(8+h;#S zi)iQWoW*>bk6~sw4C8d$_2+Ir78{9(CZc>~+tkmDl%JX7rL1y}Dy25%wv4K-A{Aw9 zb{~X00000000000JCZV;tyGHXW#Bw+KS>h z0N=cz#e%(c>7WQM1+%n@o0F|YGiU;WgD-R_4uuY#T`p0P)=q+ zlZzlIF$gYh847|o^9($J3qfS`hI+z z*ge{+O@D0^o~ADV0000000000000000B}#&&D|%=?2BqH8mKg*>B_gagPGt}_-3YD zd|jHHnHryc85@dmGa)+}9BZnwMPu2*hwU5zD+iC@o3?xWu_ z1SJF?==8=0o|=5BGqhi;YtC+w5|hU+atBKS;(vcO{H-cv(@2~)F-Nl1{Fi@TXc zy$>H7C)g5ZMkc0UMuvD*HYR2UW(Kx+6*eXoAcHmD++l|8Gh5Em?V_)5thCwOzcRU_ z>BM$6-@s+RPVKukd8ghT&OX=fLL+8JyKnb6BaXNioxXi2MIokI`gXVfX;m@jdZ0&N z0)IWa2+gC;jyOE3_|8l5V3xpl^N)HJPdTO4(uA0MZtdG$q%FEX*KtSr?{B`dw#~8J z*(@#ffcw3cU*q)rvW_c(ox7E_R0?dnrY%MGC@eI6gpfVT+5n0KkY||~I2buVo(1}u zkp<{k`_cpb|4&L}hdmQ1ocTigk5SFL?|&*&_k>j@o(;Lq^wG{nJ?+oMi_+ItUi`91 zb9=T*&+Az~dLx(na&7cvJ}C>X#C{TO3)k#V>w(sDI<+ z{$tOxr^!nkK9w`qHtN1MUhrk1rR$9uho<>Ic6#(rs(yCg^VeQA{PV=L&l!hj8^)bG zz0D!Re1G8AR+uy3R_}TCYwdFJc`cuJo;vg=Sv3$CcUesbmi$}qcx|C^*7KK>Zb)7E zp+0G%>s{XRI;m~8b;hbEzTZ0V-G4i8o$zw~N+RGT<-(<{Y5Q4!|Q%^Y6SmCUI`ZSChwe6FPZ! zEbu+b>Tt06`Gr^Z$7a31YJan1zlGg=J~m;eNa>YMAVWId)iFN_Yp-s2J?V2q%km<2 zE|-r>%6+UJy*5;8InFuF;K4=44?824P1SicH|YK+wI|rwJCfCi$)vU!bIu^uOFOsP|LU?LX>Pfio)H zjupZz{Q#x8u$Xh9Z;9yDTpx~$#qX+e_;nWV4S&3k`JJ>`;<=UHAGy}^O#HgB4d#wf TFbYP&C;$NRoa!q81_FQpj%WJd delta 11026 zcmeI2c~nz(7RTSq3t^XlAe4+66h)TmOIV@;LJ&#;*#rUCuoP+qL^h#9c}YM(WHBPk zD1{)5lvY$^sxa&>V6}}h1;(n(Agd(`BAYra^Imvw5KnV-q0s)PrJM?`(TF(fP?`k+YQw*`h_3m9es=*+P&m(4KSOg5XLHf%6i zW*pem+?>s^U^8KsIg7;xn6OCj$@19fn1CpBX-rf^Pz3sQ__H99KuZp_hJ=U&3^jZ` z791VSYC*4B^{Ec>Lp}qN0g?fd0g?fd0g?fd0g?fd0g?fd0g?fd0g?fd0g?fd0g?fd z0g?fd0g?fd0g?fd0g?fd0g?fd0g?fn0e=OgpaB7}0p8*QVjEahg&Gqa9RpElEmRPM z*HASB&pgilKy}aj&CCPhp8S=Gp}C%F5F`)tAqrGW0l`uwSOKO`0SW~K?C@Z>oA98L z5%5zXIE3g4!A!(W2-@=uQ7^?^-rKp@>kKReaT0_mI4%bKUg3ypiwTZ`U`ki=4|ImO3Y@6|XRsnEHmY4oL1@ zvpvXZ_MAFSoC6?E)-d$8szSRsWuorxjoF}eo~c|aQ%ltV9yxyCDWDk`nQ z3*#)eG1EYs9LbxelIK*8Bo!7X7v2A{biG60GSQh_^&+1mI(l=6p_$g;RLyXD-M&j5 zwM&bJ{RVFVfLIr%r~t466=b2{F!@V~`8R)W31q(41L$wg(G8<*&J1@{^v8d5wDN~| z?Wb&q!xslyzO4zTaqfLd@I`N4^EOF}zItZu{E*=PO6{1#% za0$kZ8JMdF{^uAzCu5$3$7S4@PS*nzk{^XR^|$vJ%?Tx~5H8 zl)86SI}u?3UnD>m>C-HOz>9<0DeT_7uBT>K8yQt!m-qjn;)rA8{WD_M3?p@Pi={99 zl%7&Tl;#%GrfkZC5Iw;l;wC7THP$;$?0pvd%gKFdbD==q@nLWW-A4rV{2u3UucX8+ zR5Q2CO6A+r2i0}ddrh+Td*02xdtz6PQASa{W{g?C{i@a&58fO8+_6a7hJbt#3L~^Rd3W;uk{dphBLk~eo&FIK12h`eAHAQWvta4&aI96=d|nDYeie? zk{P7AY@>em|SAF$@`204~41yBGLuyoO^ zK`NTAO>9ff!go5MK0}(3e|%;<%T64f(a&q}`8?nU@4r#Tw^l~(;f59{}>b=r#M{w=@e@Q2TYv=!1Iu8k3%I7b0yGuF4EgVQ319@RJ`N%xv{lIglTGmlzkTn=AaUa=`CBrd7d^y0n^lRl~0JQJ3O zz0`Z`J?SJk!AiKKf-S#NL9CPLpJ`+H@qf$dp-s z(y0@wwPM%Xa<;9>!w*Jm+Rl!3S=Bag-d3`Vw%l$vqtXtGl0KRQ4fv!b@TI3G{8B2S zxN!fT6xQ#pCefC zC+dpbcS1xUnJCUK%U}JCi+**?~W*&*jo{z9(_E}wMtvO#w2hdj=MAK)t<+*23tJ~<3DPPBzAp? z$5F!Lm@WXp~_QAgOd2SGuesnyOAk4^k~qrTz=5 C258y< From eaf3d051e3f1b0f93b0b55a1ecb8c7c91b1db87a Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 15 Oct 2024 23:16:02 +0200 Subject: [PATCH 34/60] Fix sc-hsm test. Signed-off-by: Pol Henarejos --- tests/scripts/sc_hsm_test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/scripts/sc_hsm_test.sh b/tests/scripts/sc_hsm_test.sh index 0885439..4a73ae5 100755 --- a/tests/scripts/sc_hsm_test.sh +++ b/tests/scripts/sc_hsm_test.sh @@ -14,7 +14,7 @@ test $? -eq 0 && echo -n "." || { echo -e "\t${FAIL}" exit 1 } -grep -q "338 tests performed" <<< $e && echo -n "." || { +grep -q "334 tests performed" <<< $e && echo -n "." || { echo -e "\t${FAIL}" exit 1 } From cbd9a5d296a847fb39039e0cc0c6a362913b1821 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 27 Oct 2024 01:24:36 +0200 Subject: [PATCH 35/60] Add sanitize check. Only pages 0 and 1 are allowed for reading. Signed-off-by: Pol Henarejos --- src/hsm/cmd_extras.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/hsm/cmd_extras.c b/src/hsm/cmd_extras.c index be01e54..9c90cdd 100644 --- a/src/hsm/cmd_extras.c +++ b/src/hsm/cmd_extras.c @@ -261,6 +261,9 @@ int cmd_extras() { } uint16_t row = (apdu.data[0] << 8) | apdu.data[1]; if (apdu.nc == 2) { + if (row > 0xbf) { + return SW_WRONG_DATA(); + } memcpy(res_APDU, otp_buffer(row), apdu.ne); res_APDU_size = apdu.ne; } From 9de1b4ca5d661830aed6e4f659cd2d980a2a856e Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 27 Oct 2024 01:24:49 +0200 Subject: [PATCH 36/60] Fix OTP data check size. Signed-off-by: Pol Henarejos --- src/hsm/cmd_extras.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hsm/cmd_extras.c b/src/hsm/cmd_extras.c index 9c90cdd..9561c89 100644 --- a/src/hsm/cmd_extras.c +++ b/src/hsm/cmd_extras.c @@ -270,7 +270,7 @@ int cmd_extras() { else { apdu.nc -= 2; apdu.data += 2; - if (!(apdu.nc % 16)) { + if (!(apdu.nc % 2)) { return SW_WRONG_DATA(); } int ret = otp_write_data(row, apdu.data, apdu.nc); From ccec83dfb1b10285f321ae9af7c6c6af15f999a3 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 27 Oct 2024 01:41:12 +0200 Subject: [PATCH 37/60] Fix otp write length check. Signed-off-by: Pol Henarejos --- src/hsm/cmd_extras.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hsm/cmd_extras.c b/src/hsm/cmd_extras.c index 9561c89..70aee96 100644 --- a/src/hsm/cmd_extras.c +++ b/src/hsm/cmd_extras.c @@ -270,7 +270,7 @@ int cmd_extras() { else { apdu.nc -= 2; apdu.data += 2; - if (!(apdu.nc % 2)) { + if (apdu.nc % 2) { return SW_WRONG_DATA(); } int ret = otp_write_data(row, apdu.data, apdu.nc); From 2856ec69171322389f5f19b01ee8b582e8f69106 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 27 Oct 2024 02:14:51 +0200 Subject: [PATCH 38/60] Align data in case it's not. Signed-off-by: Pol Henarejos --- src/hsm/cmd_extras.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/hsm/cmd_extras.c b/src/hsm/cmd_extras.c index 70aee96..85172a3 100644 --- a/src/hsm/cmd_extras.c +++ b/src/hsm/cmd_extras.c @@ -273,6 +273,10 @@ int cmd_extras() { if (apdu.nc % 2) { return SW_WRONG_DATA(); } + if ((uintptr_t)apdu.data & 1) { // Not aligned + memmove(apdu.data - 1, apdu.data, apdu.nc); + apdu.data--; + } int ret = otp_write_data(row, apdu.data, apdu.nc); if (ret != 0) { return SW_EXEC_ERROR(); From d1ee43baabe6ec580705f024990479e37625a30d Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 27 Oct 2024 02:37:16 +0200 Subject: [PATCH 39/60] Add secure_boot enable. Signed-off-by: Pol Henarejos --- tools/pico-hsm-tool.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tools/pico-hsm-tool.py b/tools/pico-hsm-tool.py index 1b95331..a6dbc51 100644 --- a/tools/pico-hsm-tool.py +++ b/tools/pico-hsm-tool.py @@ -140,8 +140,8 @@ def parse_args(): parser_keygen_x448 = subparser_keygen.add_parser('x448', help='Generates a private X448 keypair.') parser_otp = subparser.add_parser('otp', help='Read/write OTP values.') - parser_otp.add_argument('subcommand', choices=['read', 'write'], help='Read/write.', nargs='?') - parser_otp.add_argument('--row', help='OTP row (in HEX)', required=True) + parser_otp.add_argument('subcommand', choices=['read', 'write', 'secure_boot'], help='Read, write or enable Secure Boot', nargs='?') + parser_otp.add_argument('--row', help='OTP row (in HEX)', required='write' in sys.argv or 'read' in sys.argv) parser_otp.add_argument('-d', '--data', help='Data to write (in HEX) [e.g. 0011223344556677889900AABBCCDDEEFF]', required='write' in sys.argv) args = parser.parse_args() @@ -486,14 +486,19 @@ def phy(picohsm, args): print('Command executed successfully. Please, restart your Pico Key.') def otp(picohsm, args): - row = int(args.row, 16) if (args.subcommand == 'read'): + row = int(args.row, 16) ret = picohsm.otp(row=row) print(f'OTP row {args.row}: {hexlify(ret).decode()}') elif (args.subcommand == 'write'): + row = int(args.row, 16) data = unhexlify(args.data) picohsm.otp(row=row, data=data) print(f'OTP row {args.row} written successfully.') + elif (args.subcommand == 'secure_boot'): + script_path = os.path.dirname(os.path.abspath(__file__)) + boot_json = json.load(open(f'{script_path}/../pico-keys-sdk/config/rp2350/secure_boot.json')) + picohsm.secure_boot(boot_json['bootkey0']) def main(args): sys.stderr.buffer.write(b'Pico HSM Tool v1.18\n') From 9fa3758daddf71b56bb89f83277d2647781226f8 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 28 Oct 2024 00:15:48 +0100 Subject: [PATCH 40/60] Add support for OTP raw read/write. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- src/hsm/cmd_extras.c | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 6216cd2..62c3d0c 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 6216cd24be025d6ccd43c3358cc48ec7eac4556f +Subproject commit 62c3d0c3603e673ac4a338450db27fd3356468ac diff --git a/src/hsm/cmd_extras.c b/src/hsm/cmd_extras.c index 85172a3..03797e2 100644 --- a/src/hsm/cmd_extras.c +++ b/src/hsm/cmd_extras.c @@ -260,24 +260,37 @@ int cmd_extras() { return SW_WRONG_LENGTH(); } uint16_t row = (apdu.data[0] << 8) | apdu.data[1]; + bool israw = P2(apdu) == 0x1; if (apdu.nc == 2) { - if (row > 0xbf) { + if (row > 0xbf && row < 0xf48) { return SW_WRONG_DATA(); } - memcpy(res_APDU, otp_buffer(row), apdu.ne); + if (israw) { + memcpy(res_APDU, otp_buffer_raw(row), apdu.ne); + } + else { + memcpy(res_APDU, otp_buffer(row), apdu.ne); + } res_APDU_size = apdu.ne; } else { apdu.nc -= 2; apdu.data += 2; - if (apdu.nc % 2) { + if (apdu.nc > 1024) { + return SW_WRONG_LENGTH(); + } + if (apdu.nc % (israw ? 4 : 2)) { return SW_WRONG_DATA(); } - if ((uintptr_t)apdu.data & 1) { // Not aligned - memmove(apdu.data - 1, apdu.data, apdu.nc); - apdu.data--; + uint8_t adata[1024] __attribute__((aligned(4))); + memcpy(adata, apdu.data, apdu.nc); + int ret = 0; + if (israw) { + ret = otp_write_data_raw(row, adata, apdu.nc); + } + else { + ret = otp_write_data(row, adata, apdu.nc); } - int ret = otp_write_data(row, apdu.data, apdu.nc); if (ret != 0) { return SW_EXEC_ERROR(); } From 0ce89ae9680a2cb6f190f5ef4c7643a9a811482b Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 28 Oct 2024 00:19:55 +0100 Subject: [PATCH 41/60] Add support for lock chip on secure_boot. Signed-off-by: Pol Henarejos --- tools/pico-hsm-tool.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/pico-hsm-tool.py b/tools/pico-hsm-tool.py index a6dbc51..6a0892f 100644 --- a/tools/pico-hsm-tool.py +++ b/tools/pico-hsm-tool.py @@ -143,6 +143,7 @@ def parse_args(): parser_otp.add_argument('subcommand', choices=['read', 'write', 'secure_boot'], help='Read, write or enable Secure Boot', nargs='?') parser_otp.add_argument('--row', help='OTP row (in HEX)', required='write' in sys.argv or 'read' in sys.argv) parser_otp.add_argument('-d', '--data', help='Data to write (in HEX) [e.g. 0011223344556677889900AABBCCDDEEFF]', required='write' in sys.argv) + parser_otp.add_argument('--lock', help='Lock & protect (no other firmwares can be loaded)', action='store_true') args = parser.parse_args() return args @@ -498,7 +499,7 @@ def otp(picohsm, args): elif (args.subcommand == 'secure_boot'): script_path = os.path.dirname(os.path.abspath(__file__)) boot_json = json.load(open(f'{script_path}/../pico-keys-sdk/config/rp2350/secure_boot.json')) - picohsm.secure_boot(boot_json['bootkey0']) + picohsm.secure_boot(boot_json['bootkey0'], lock=args.lock) def main(args): sys.stderr.buffer.write(b'Pico HSM Tool v1.18\n') From 1933498a3329e4851c6d53448355e23d7a05737e Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 28 Oct 2024 00:24:17 +0100 Subject: [PATCH 42/60] Add optional bootkey index param for secure_boot. Signed-off-by: Pol Henarejos --- tools/pico-hsm-tool.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/pico-hsm-tool.py b/tools/pico-hsm-tool.py index 6a0892f..a10979a 100644 --- a/tools/pico-hsm-tool.py +++ b/tools/pico-hsm-tool.py @@ -144,6 +144,7 @@ def parse_args(): parser_otp.add_argument('--row', help='OTP row (in HEX)', required='write' in sys.argv or 'read' in sys.argv) parser_otp.add_argument('-d', '--data', help='Data to write (in HEX) [e.g. 0011223344556677889900AABBCCDDEEFF]', required='write' in sys.argv) parser_otp.add_argument('--lock', help='Lock & protect (no other firmwares can be loaded)', action='store_true') + parser_otp.add_argument('--index', help='Bootkey index [0-3]', type=int, default=0, choices=[0, 1, 2, 3]) args = parser.parse_args() return args @@ -499,7 +500,7 @@ def otp(picohsm, args): elif (args.subcommand == 'secure_boot'): script_path = os.path.dirname(os.path.abspath(__file__)) boot_json = json.load(open(f'{script_path}/../pico-keys-sdk/config/rp2350/secure_boot.json')) - picohsm.secure_boot(boot_json['bootkey0'], lock=args.lock) + picohsm.secure_boot(boot_json['bootkey0'], bootkey_index=args.index, lock=args.lock) def main(args): sys.stderr.buffer.write(b'Pico HSM Tool v1.18\n') From 68360978d6d39ee5b268cd3a7fc169580015a50a Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 28 Oct 2024 00:25:22 +0100 Subject: [PATCH 43/60] Use BOOTKEY instead of reading json. Signed-off-by: Pol Henarejos --- tools/pico-hsm-tool.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/pico-hsm-tool.py b/tools/pico-hsm-tool.py index a10979a..c582590 100644 --- a/tools/pico-hsm-tool.py +++ b/tools/pico-hsm-tool.py @@ -57,6 +57,8 @@ from argparse import RawTextHelpFormatter pin = None +BOOTKEY = [225, 209, 107, 167, 100, 171, 215, 18, 212, 239, 110, 62, 221, 116, 78, 213, 99, 140, 38, 11, 119, 28, 249, 129, 81, 17, 11, 175, 172, 155, 200, 113] + def hexy(a): return [hex(i) for i in a] @@ -498,9 +500,7 @@ def otp(picohsm, args): picohsm.otp(row=row, data=data) print(f'OTP row {args.row} written successfully.') elif (args.subcommand == 'secure_boot'): - script_path = os.path.dirname(os.path.abspath(__file__)) - boot_json = json.load(open(f'{script_path}/../pico-keys-sdk/config/rp2350/secure_boot.json')) - picohsm.secure_boot(boot_json['bootkey0'], bootkey_index=args.index, lock=args.lock) + picohsm.secure_boot(BOOTKEY, bootkey_index=args.index, lock=args.lock) def main(args): sys.stderr.buffer.write(b'Pico HSM Tool v1.18\n') From 20ef94c301ee6f0e6a0fbe353dcc95a5abf04a65 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 28 Oct 2024 00:28:49 +0100 Subject: [PATCH 44/60] Upgrade Pico HSM Tool to v2.0 Signed-off-by: Pol Henarejos --- tools/pico-hsm-tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pico-hsm-tool.py b/tools/pico-hsm-tool.py index c582590..c124a06 100644 --- a/tools/pico-hsm-tool.py +++ b/tools/pico-hsm-tool.py @@ -503,7 +503,7 @@ def otp(picohsm, args): picohsm.secure_boot(BOOTKEY, bootkey_index=args.index, lock=args.lock) def main(args): - sys.stderr.buffer.write(b'Pico HSM Tool v1.18\n') + sys.stderr.buffer.write(b'Pico HSM Tool v2.0\n') sys.stderr.buffer.write(b'Author: Pol Henarejos\n') sys.stderr.buffer.write(b'Report bugs to https://github.com/polhenarejos/pico-hsm/issues\n') sys.stderr.buffer.write(b'\n\n') From 3c6684cdabfdab30480dad92a4ae1bb1454faa4d Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 5 Nov 2024 19:29:00 +0100 Subject: [PATCH 45/60] Rename CCID_ codes to PICOKEY_ Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- src/hsm/cmd_bip_slip.c | 40 +++++----- src/hsm/cmd_change_pin.c | 4 +- src/hsm/cmd_decrypt_asym.c | 6 +- src/hsm/cmd_delete_file.c | 2 +- src/hsm/cmd_derive_asym.c | 6 +- src/hsm/cmd_extras.c | 47 +++++------ src/hsm/cmd_general_authenticate.c | 4 +- src/hsm/cmd_initialize.c | 18 ++--- src/hsm/cmd_key_domain.c | 12 +-- src/hsm/cmd_key_gen.c | 4 +- src/hsm/cmd_key_unwrap.c | 20 ++--- src/hsm/cmd_key_wrap.c | 10 +-- src/hsm/cmd_keypair_gen.c | 6 +- src/hsm/cmd_mse.c | 2 +- src/hsm/cmd_pso.c | 8 +- src/hsm/cmd_reset_retry.c | 8 +- src/hsm/cmd_signature.c | 18 ++--- src/hsm/cmd_update_ef.c | 4 +- src/hsm/cvc.c | 56 +++++++------- src/hsm/kek.c | 120 ++++++++++++++--------------- src/hsm/sc_hsm.c | 82 ++++++++++---------- 22 files changed, 235 insertions(+), 244 deletions(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 62c3d0c..6625678 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 62c3d0c3603e673ac4a338450db27fd3356468ac +Subproject commit 6625678c3059554ef9fc38c1fd0ff16fa4dbad3e diff --git a/src/hsm/cmd_bip_slip.c b/src/hsm/cmd_bip_slip.c index 20f271c..4d034c6 100644 --- a/src/hsm/cmd_bip_slip.c +++ b/src/hsm/cmd_bip_slip.c @@ -38,7 +38,7 @@ int node_derive_bip_child(const mbedtls_ecp_keypair *parent, mbedtls_mpi_init(&kchild); if (i[0] >= 0x80) { if (mbedtls_mpi_cmp_int(&parent->d, 0) == 0) { - return CCID_ERR_NULL_PARAM; + return PICOKEY_ERR_NULL_PARAM; } data[0] = 0x00; mbedtls_mpi_write_binary(&parent->d, data + 1, 32); @@ -72,19 +72,19 @@ int node_derive_bip_child(const mbedtls_ecp_keypair *parent, memcpy(cchild, iR, 32); mbedtls_mpi_free(&il); mbedtls_mpi_free(&kchild); - return CCID_OK; + return PICOKEY_OK; } int sha256_ripemd160(const uint8_t *buffer, size_t buffer_len, uint8_t *output) { mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), buffer, buffer_len, output); mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_RIPEMD160), output, 32, output); - return CCID_OK; + return PICOKEY_OK; } int sha256_sha256(const uint8_t *buffer, size_t buffer_len, uint8_t *output) { mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), buffer, buffer_len, output); mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), output, 32, output); - return CCID_OK; + return PICOKEY_OK; } int node_fingerprint_bip(mbedtls_ecp_keypair *ctx, uint8_t fingerprint[4]) { @@ -98,7 +98,7 @@ int node_fingerprint_bip(mbedtls_ecp_keypair *ctx, uint8_t fingerprint[4]) { sizeof(buffer)); sha256_ripemd160(buffer, sizeof(buffer), buffer); memcpy(fingerprint, buffer, 4); - return CCID_OK; + return PICOKEY_OK; } int node_fingerprint_slip(mbedtls_ecp_keypair *ctx, uint8_t fingerprint[4]) { @@ -106,7 +106,7 @@ int node_fingerprint_slip(mbedtls_ecp_keypair *ctx, uint8_t fingerprint[4]) { mbedtls_mpi_write_binary(&ctx->d, buffer, sizeof(buffer)); sha256_ripemd160(buffer, sizeof(buffer), buffer); memcpy(fingerprint, buffer, 4); - return CCID_OK; + return PICOKEY_OK; } int load_master_bip(uint16_t mid, mbedtls_ecp_keypair *ctx, uint8_t chain[32], @@ -115,13 +115,13 @@ int load_master_bip(uint16_t mid, mbedtls_ecp_keypair *ctx, uint8_t chain[32], mbedtls_ecp_keypair_init(ctx); file_t *ef = search_file(EF_MASTER_SEED | mid); if (!file_has_data(ef)) { - return CCID_ERR_FILE_NOT_FOUND; + return PICOKEY_ERR_FILE_NOT_FOUND; } memcpy(mkey, file_get_data(ef), sizeof(mkey)); int r = mkek_decrypt(mkey + 1, sizeof(mkey) - 1); - if (r != CCID_OK) { - return CCID_EXEC_ERROR; + if (r != PICOKEY_OK) { + return PICOKEY_EXEC_ERROR; } if (mkey[0] == 0x1 || mkey[0] == 0x2) { if (mkey[0] == 0x1) { @@ -131,7 +131,7 @@ int load_master_bip(uint16_t mid, mbedtls_ecp_keypair *ctx, uint8_t chain[32], mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1); } else { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } mbedtls_mpi_read_binary(&ctx->d, mkey + 1, 32); @@ -143,7 +143,7 @@ int load_master_bip(uint16_t mid, mbedtls_ecp_keypair *ctx, uint8_t chain[32], memcpy(chain, mkey + 1, 32); } key_type[0] = mkey[0]; - return CCID_OK; + return PICOKEY_OK; } int node_derive_path(const uint8_t *path, @@ -166,16 +166,16 @@ int node_derive_path(const uint8_t *path, for (; walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data); node++) { if (tag == 0x02) { if ((node == 0 && tag_len != 1) || (node != 0 && tag_len != 4)) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (node == 0) { - if ((r = load_master_bip(tag_data[0], ctx, chain, key_type)) != CCID_OK) { + if ((r = load_master_bip(tag_data[0], ctx, chain, key_type)) != PICOKEY_OK) { return r; } } else if (node > 0) { node_fingerprint_bip(ctx, fingerprint); - if ((r = node_derive_bip_child(ctx, chain, tag_data, ctx, chain)) != CCID_OK) { + if ((r = node_derive_bip_child(ctx, chain, tag_data, ctx, chain)) != PICOKEY_OK) { return r; } memcpy(last_node, tag_data, 4); @@ -183,7 +183,7 @@ int node_derive_path(const uint8_t *path, } else if (tag == 0x04) { if (node == 0) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } else if (node > 0) { node_fingerprint_slip(ctx, fingerprint); @@ -202,7 +202,7 @@ int node_derive_path(const uint8_t *path, if (nodes) { *nodes = node; } - return CCID_OK; + return PICOKEY_OK; } int cmd_bip_slip() { @@ -253,11 +253,11 @@ int cmd_bip_slip() { mkey[0] = p1; file_t *ef = file_new(EF_MASTER_SEED | p2); int r = mkek_encrypt(mkey + 1, sizeof(mkey) - 1); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } r = file_put_data(ef, mkey, sizeof(mkey)); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } low_flash_available(); @@ -271,7 +271,7 @@ int cmd_bip_slip() { size_t olen = 0; int r = node_derive_path(apdu.data, (uint16_t)apdu.nc, &ctx, chain, fgpt, &nodes, last_node, &key_type); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { mbedtls_ecp_keypair_free(&ctx); return SW_EXEC_ERROR(); } @@ -317,7 +317,7 @@ int cmd_bip_slip() { &nodes, last_node, &hd_keytype); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { mbedtls_ecp_keypair_free(&hd_context); return SW_EXEC_ERROR(); } diff --git a/src/hsm/cmd_change_pin.c b/src/hsm/cmd_change_pin.c index 3af69e0..80a0dd9 100644 --- a/src/hsm/cmd_change_pin.c +++ b/src/hsm/cmd_change_pin.c @@ -42,7 +42,7 @@ int cmd_change_pin() { } uint8_t mkek[MKEK_SIZE]; r = load_mkek(mkek); //loads the MKEK with old pin - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } //encrypt MKEK with new pin @@ -57,7 +57,7 @@ int cmd_change_pin() { } r = store_mkek(mkek); release_mkek(mkek); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } uint8_t dhash[33]; diff --git a/src/hsm/cmd_decrypt_asym.c b/src/hsm/cmd_decrypt_asym.c index e893a23..840bb6c 100644 --- a/src/hsm/cmd_decrypt_asym.c +++ b/src/hsm/cmd_decrypt_asym.c @@ -48,9 +48,9 @@ int cmd_decrypt_asym() { mbedtls_rsa_set_padding(&ctx, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256); } int r = load_private_key_rsa(&ctx, ef); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { mbedtls_rsa_free(&ctx); - if (r == CCID_VERIFICATION_FAILED) { + if (r == PICOKEY_VERIFICATION_FAILED) { return SW_SECURE_MESSAGE_EXEC_ERROR(); } return SW_EXEC_ERROR(); @@ -178,7 +178,7 @@ int cmd_decrypt_asym() { if (file_get_size(tf) == kdom_uid.len && memcmp(file_get_data(tf), kdom_uid.data, kdom_uid.len) == 0) { file_new(EF_DKEK + n); - if (store_dkek_key(n, res_APDU + 1) != CCID_OK) { + if (store_dkek_key(n, res_APDU + 1) != PICOKEY_OK) { return SW_EXEC_ERROR(); } mbedtls_platform_zeroize(res_APDU, 32); diff --git a/src/hsm/cmd_delete_file.c b/src/hsm/cmd_delete_file.c index 0919293..1ecbecb 100644 --- a/src/hsm/cmd_delete_file.c +++ b/src/hsm/cmd_delete_file.c @@ -38,7 +38,7 @@ int cmd_delete_file() { if (!authenticate_action(ef, ACL_OP_DELETE_SELF)) { return SW_SECURITY_STATUS_NOT_SATISFIED(); } - if (delete_file(ef) != CCID_OK) { + if (delete_file(ef) != PICOKEY_OK) { return SW_EXEC_ERROR(); } return SW_OK(); diff --git a/src/hsm/cmd_derive_asym.c b/src/hsm/cmd_derive_asym.c index 3d564ee..5d5437e 100644 --- a/src/hsm/cmd_derive_asym.c +++ b/src/hsm/cmd_derive_asym.c @@ -57,9 +57,9 @@ int cmd_derive_asym() { int r; r = load_private_key_ecdsa(&ctx, fkey); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { mbedtls_ecdsa_free(&ctx); - if (r == CCID_VERIFICATION_FAILED) { + if (r == PICOKEY_VERIFICATION_FAILED) { return SW_SECURE_MESSAGE_EXEC_ERROR(); } return SW_EXEC_ERROR(); @@ -88,7 +88,7 @@ int cmd_derive_asym() { return SW_EXEC_ERROR(); } r = store_keys(&ctx, PICO_KEYS_KEY_EC, dest_id); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { mbedtls_ecdsa_free(&ctx); return SW_EXEC_ERROR(); } diff --git a/src/hsm/cmd_extras.c b/src/hsm/cmd_extras.c index 03797e2..f0b6d58 100644 --- a/src/hsm/cmd_extras.c +++ b/src/hsm/cmd_extras.c @@ -209,47 +209,38 @@ int cmd_extras() { } } else { - uint8_t tmp[PHY_MAX_SIZE]; - memset(tmp, 0, sizeof(tmp)); - uint16_t opts = 0; - if (file_has_data(ef_phy)) { - memcpy(tmp, file_get_data(ef_phy), MIN(sizeof(tmp), file_get_size(ef_phy))); - if (file_get_size(ef_phy) >= 8) { - opts = (tmp[PHY_OPTS] << 8) | tmp[PHY_OPTS + 1]; - } - } - if (P2(apdu) == PHY_VID) { // VIDPID + if (P2(apdu) == PHY_VIDPID) { // VIDPID if (apdu.nc != 4) { return SW_WRONG_LENGTH(); } - memcpy(tmp + PHY_VID, apdu.data, 4); - opts |= PHY_OPT_VPID; + phy_data.vid = (apdu.data[0] << 8) | apdu.data[1]; + phy_data.pid = (apdu.data[2] << 8) | apdu.data[3]; + phy_data.vidpid_present = true; } - else if (P2(apdu) == PHY_LED_GPIO || P2(apdu) == PHY_LED_BTNESS) { - if (apdu.nc != 1) { - return SW_WRONG_LENGTH(); - } - tmp[P2(apdu)] = apdu.data[0]; - if (P2(apdu) == PHY_LED_GPIO) { - opts |= PHY_OPT_GPIO; - } - else if (P2(apdu) == PHY_LED_BTNESS) { - opts |= PHY_OPT_BTNESS; - } + else if (P2(apdu) == PHY_LED_GPIO) { + phy_data.led_gpio = apdu.data[0]; + phy_data.led_gpio_present = true; + } + else if (P2(apdu) == PHY_LED_BTNESS) { + phy_data.led_brightness = apdu.data[0]; + phy_data.led_brightness_present = true; } else if (P2(apdu) == PHY_OPTS) { if (apdu.nc != 2) { return SW_WRONG_LENGTH(); } - uint16_t opt = (apdu.data[0] << 8) | apdu.data[1]; - opts = (opts & ~PHY_OPT_MASK) | (opt & PHY_OPT_MASK); + phy_data.opts = (apdu.data[0] << 8) | apdu.data[1]; } else { return SW_INCORRECT_P1P2(); } - tmp[PHY_OPTS] = opts >> 8; - tmp[PHY_OPTS + 1] = opts & 0xff; - file_put_data(ef_phy, tmp, sizeof(tmp)); + uint8_t tmp[PHY_MAX_SIZE]; + uint16_t tmp_len = 0; + memset(tmp, 0, sizeof(tmp)); + if (phy_serialize_data(&phy_data, tmp, &tmp_len) != PICOKEY_OK) { + return SW_EXEC_ERROR(); + } + file_put_data(ef_phy, tmp, tmp_len); low_flash_available(); } } diff --git a/src/hsm/cmd_general_authenticate.c b/src/hsm/cmd_general_authenticate.c index eb2b339..6f80464 100644 --- a/src/hsm/cmd_general_authenticate.c +++ b/src/hsm/cmd_general_authenticate.c @@ -47,7 +47,7 @@ int cmd_general_authenticate() { mbedtls_ecdsa_context ectx; mbedtls_ecdsa_init(&ectx); r = load_private_key_ecdsa(&ectx, fkey); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { mbedtls_ecdsa_free(&ectx); return SW_EXEC_ERROR(); } @@ -106,7 +106,7 @@ int cmd_general_authenticate() { r = sm_sign(t, pubkey_len + 16, res_APDU + res_APDU_size); free(t); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } res_APDU_size += 8; diff --git a/src/hsm/cmd_initialize.c b/src/hsm/cmd_initialize.c index 710a330..ff0abaa 100644 --- a/src/hsm/cmd_initialize.c +++ b/src/hsm/cmd_initialize.c @@ -132,10 +132,10 @@ int cmd_initialize() { release_mkek(mkek); return SW_EXEC_ERROR(); } - if (ret_mkek != CCID_OK) { + if (ret_mkek != PICOKEY_OK) { ret_mkek = load_mkek(mkek); //Try again with new PIN/SO-PIN just in case some is the same } - if (store_mkek(ret_mkek == CCID_OK ? mkek : NULL) != CCID_OK) { + if (store_mkek(ret_mkek == PICOKEY_OK ? mkek : NULL) != PICOKEY_OK) { release_mkek(mkek); return SW_EXEC_ERROR(); } @@ -143,31 +143,31 @@ int cmd_initialize() { if (dkeks) { if (*dkeks > 0) { uint16_t d = *dkeks; - if (file_put_data(tf_kd, (const uint8_t *) &d, sizeof(d)) != CCID_OK) { + if (file_put_data(tf_kd, (const uint8_t *) &d, sizeof(d)) != PICOKEY_OK) { return SW_EXEC_ERROR(); } } else { int r = save_dkek_key(0, random_bytes_get(32)); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } uint16_t d = 0x0101; - if (file_put_data(tf_kd, (const uint8_t *) &d, sizeof(d)) != CCID_OK) { + if (file_put_data(tf_kd, (const uint8_t *) &d, sizeof(d)) != PICOKEY_OK) { return SW_EXEC_ERROR(); } } } else { uint16_t d = 0x0000; - if (file_put_data(tf_kd, (const uint8_t *) &d, sizeof(d)) != CCID_OK) { + if (file_put_data(tf_kd, (const uint8_t *) &d, sizeof(d)) != PICOKEY_OK) { return SW_EXEC_ERROR(); } } if (kds) { uint8_t t[MAX_KEY_DOMAINS * 2], k = MIN(*kds, MAX_KEY_DOMAINS); memset(t, 0xff, 2 * k); - if (file_put_data(tf_kd, t, 2 * k) != CCID_OK) { + if (file_put_data(tf_kd, t, 2 * k) != PICOKEY_OK) { return SW_EXEC_ERROR(); } } @@ -179,7 +179,7 @@ int cmd_initialize() { return SW_EXEC_ERROR(); } int ret = 0; - if (ret_mkek != CCID_OK || !file_has_data(fdkey)) { + if (ret_mkek != PICOKEY_OK || !file_has_data(fdkey)) { mbedtls_ecdsa_context ecdsa; mbedtls_ecdsa_init(&ecdsa); mbedtls_ecp_group_id ec_id = MBEDTLS_ECP_DP_SECP256R1; @@ -190,7 +190,7 @@ int cmd_initialize() { return SW_EXEC_ERROR(); } ret = store_keys(&ecdsa, PICO_KEYS_KEY_EC, key_id); - if (ret != CCID_OK) { + if (ret != PICOKEY_OK) { mbedtls_ecdsa_free(&ecdsa); return SW_EXEC_ERROR(); } diff --git a/src/hsm/cmd_key_domain.c b/src/hsm/cmd_key_domain.c index 3a69e51..b3149e1 100644 --- a/src/hsm/cmd_key_domain.c +++ b/src/hsm/cmd_key_domain.c @@ -72,8 +72,8 @@ int cmd_key_domain() { import_dkek_share(p2, apdu.data); if (++current_dkeks >= dkeks) { int r = save_dkek_key(p2, NULL); - if (r != CCID_OK) { - if (r == CCID_NO_LOGIN) { + if (r != PICOKEY_OK) { + if (r == PICOKEY_NO_LOGIN) { pending_save_dkek = p2; } else { @@ -86,7 +86,7 @@ int cmd_key_domain() { uint8_t t[MAX_KEY_DOMAINS * 2]; memcpy(t, kdata, tf_kd_size); t[2 * p2 + 1] = current_dkeks; - if (file_put_data(tf_kd, t, tf_kd_size) != CCID_OK) { + if (file_put_data(tf_kd, t, tf_kd_size) != PICOKEY_OK) { return SW_EXEC_ERROR(); } low_flash_available(); @@ -129,17 +129,17 @@ int cmd_key_domain() { else if (p1 == 0x4) { t[2 * p2 + 1] = current_dkeks = 0; } - if (file_put_data(tf_kd, t, tf_kd_size) != CCID_OK) { + if (file_put_data(tf_kd, t, tf_kd_size) != PICOKEY_OK) { return SW_EXEC_ERROR(); } file_t *tf = NULL; if ((tf = search_file(EF_DKEK + p2))) { - if (delete_file(tf) != CCID_OK) { + if (delete_file(tf) != PICOKEY_OK) { return SW_EXEC_ERROR(); } } if (p1 == 0x3 && (tf = search_file(EF_XKEK + p2))) { - if (delete_file(tf) != CCID_OK) { + if (delete_file(tf) != PICOKEY_OK) { return SW_EXEC_ERROR(); } } diff --git a/src/hsm/cmd_key_gen.c b/src/hsm/cmd_key_gen.c index 9389cb7..fae498a 100644 --- a/src/hsm/cmd_key_gen.c +++ b/src/hsm/cmd_key_gen.c @@ -56,10 +56,10 @@ int cmd_key_gen() { aes_type = PICO_KEYS_KEY_AES_512; } r = store_keys(aes_key, aes_type, key_id); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_MEMORY_FAILURE(); } - if (find_and_store_meta_key(key_id) != CCID_OK) { + if (find_and_store_meta_key(key_id) != PICOKEY_OK) { return SW_EXEC_ERROR(); } low_flash_available(); diff --git a/src/hsm/cmd_key_unwrap.c b/src/hsm/cmd_key_unwrap.c index 3c2f5a5..579559c 100644 --- a/src/hsm/cmd_key_unwrap.c +++ b/src/hsm/cmd_key_unwrap.c @@ -51,8 +51,8 @@ int cmd_key_unwrap() { mbedtls_rsa_init(&ctx); do { r = dkek_decode_key((uint8_t)++kdom, &ctx, data, data_len, NULL, &allowed, &allowed_len); - } while ((r == CCID_ERR_FILE_NOT_FOUND || r == CCID_WRONG_DKEK) && kdom < MAX_KEY_DOMAINS); - if (r != CCID_OK) { + } while ((r == PICOKEY_ERR_FILE_NOT_FOUND || r == PICOKEY_WRONG_DKEK) && kdom < MAX_KEY_DOMAINS); + if (r != PICOKEY_OK) { mbedtls_rsa_free(&ctx); return SW_EXEC_ERROR(); } @@ -62,7 +62,7 @@ int cmd_key_unwrap() { return SW_EXEC_ERROR(); } mbedtls_rsa_free(&ctx); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } } @@ -71,8 +71,8 @@ int cmd_key_unwrap() { mbedtls_ecdsa_init(&ctx); do { r = dkek_decode_key((uint8_t)++kdom, &ctx, data, data_len, NULL, &allowed, &allowed_len); - } while ((r == CCID_ERR_FILE_NOT_FOUND || r == CCID_WRONG_DKEK) && kdom < MAX_KEY_DOMAINS); - if (r != CCID_OK) { + } while ((r == PICOKEY_ERR_FILE_NOT_FOUND || r == PICOKEY_WRONG_DKEK) && kdom < MAX_KEY_DOMAINS); + if (r != PICOKEY_OK) { mbedtls_ecdsa_free(&ctx); return SW_EXEC_ERROR(); } @@ -82,7 +82,7 @@ int cmd_key_unwrap() { return SW_EXEC_ERROR(); } mbedtls_ecdsa_free(&ctx); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } } @@ -97,8 +97,8 @@ int cmd_key_unwrap() { &key_size, &allowed, &allowed_len); - } while ((r == CCID_ERR_FILE_NOT_FOUND || r == CCID_WRONG_DKEK) && kdom < MAX_KEY_DOMAINS); - if (r != CCID_OK) { + } while ((r == PICOKEY_ERR_FILE_NOT_FOUND || r == PICOKEY_WRONG_DKEK) && kdom < MAX_KEY_DOMAINS); + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } if (key_size == 64) { @@ -117,7 +117,7 @@ int cmd_key_unwrap() { return SW_EXEC_ERROR(); } r = store_keys(aes_key, aes_type, key_id); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } } @@ -136,7 +136,7 @@ int cmd_key_unwrap() { } r = meta_add((KEY_PREFIX << 8) | key_id, meta, meta_len); free(meta); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } } diff --git a/src/hsm/cmd_key_wrap.c b/src/hsm/cmd_key_wrap.c index 1244ed9..3825c72 100644 --- a/src/hsm/cmd_key_wrap.c +++ b/src/hsm/cmd_key_wrap.c @@ -60,9 +60,9 @@ int cmd_key_wrap() { mbedtls_rsa_context ctx; mbedtls_rsa_init(&ctx); r = load_private_key_rsa(&ctx, ef); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { mbedtls_rsa_free(&ctx); - if (r == CCID_VERIFICATION_FAILED) { + if (r == PICOKEY_VERIFICATION_FAILED) { return SW_SECURE_MESSAGE_EXEC_ERROR(); } return SW_EXEC_ERROR(); @@ -74,9 +74,9 @@ int cmd_key_wrap() { mbedtls_ecdsa_context ctx; mbedtls_ecdsa_init(&ctx); r = load_private_key_ecdsa(&ctx, ef); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { mbedtls_ecdsa_free(&ctx); - if (r == CCID_VERIFICATION_FAILED) { + if (r == PICOKEY_VERIFICATION_FAILED) { return SW_SECURE_MESSAGE_EXEC_ERROR(); } return SW_EXEC_ERROR(); @@ -110,7 +110,7 @@ int cmd_key_wrap() { r = dkek_encode_key(kdom, kdata_aes, aes_type, res_APDU, &wrap_len, meta_tag, tag_len); mbedtls_platform_zeroize(kdata_aes, sizeof(kdata_aes)); } - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } res_APDU_size = wrap_len; diff --git a/src/hsm/cmd_keypair_gen.c b/src/hsm/cmd_keypair_gen.c index c0639c4..8890a90 100644 --- a/src/hsm/cmd_keypair_gen.c +++ b/src/hsm/cmd_keypair_gen.c @@ -63,7 +63,7 @@ int cmd_keypair_gen() { return SW_EXEC_ERROR(); } ret = store_keys(&rsa, PICO_KEYS_KEY_RSA, key_id); - if (ret != CCID_OK) { + if (ret != PICOKEY_OK) { mbedtls_rsa_free(&rsa); return SW_EXEC_ERROR(); } @@ -131,7 +131,7 @@ int cmd_keypair_gen() { } ret = store_keys(&ecdsa, PICO_KEYS_KEY_EC, key_id); mbedtls_ecdsa_free(&ecdsa); - if (ret != CCID_OK) { + if (ret != PICOKEY_OK) { return SW_EXEC_ERROR(); } } @@ -141,7 +141,7 @@ int cmd_keypair_gen() { else { return SW_WRONG_DATA(); } - if (find_and_store_meta_key(key_id) != CCID_OK) { + if (find_and_store_meta_key(key_id) != PICOKEY_OK) { return SW_EXEC_ERROR(); } file_t *fpk = file_new((EE_CERTIFICATE_PREFIX << 8) | key_id); diff --git a/src/hsm/cmd_mse.c b/src/hsm/cmd_mse.c index 1e6a5a4..0d279ff 100644 --- a/src/hsm/cmd_mse.c +++ b/src/hsm/cmd_mse.c @@ -51,7 +51,7 @@ int cmd_mse() { } else { if (p2 == 0xB6) { - if (puk_store_select_chr(tag_data) == CCID_OK) { + if (puk_store_select_chr(tag_data) == PICOKEY_OK) { return SW_OK(); } } diff --git a/src/hsm/cmd_pso.c b/src/hsm/cmd_pso.c index cbaec4d..56caa68 100644 --- a/src/hsm/cmd_pso.c +++ b/src/hsm/cmd_pso.c @@ -40,11 +40,11 @@ int cmd_pso() { apdu.nc += tlv_len; } int r = cvc_verify(apdu.data, (uint16_t)apdu.nc, current_puk->cvcert, current_puk->cvcert_len); - if (r != CCID_OK) { - if (r == CCID_WRONG_DATA) { + if (r != PICOKEY_OK) { + if (r == PICOKEY_WRONG_DATA) { return SW_DATA_INVALID(); } - else if (r == CCID_WRONG_SIGNATURE) { + else if (r == PICOKEY_WRONG_SIGNATURE) { return SW_CONDITIONS_NOT_SATISFIED(); } return SW_EXEC_ERROR(); @@ -56,7 +56,7 @@ int cmd_pso() { ca_ef = file_new(fid); file_put_data(ca_ef, apdu.data, (uint16_t)apdu.nc); if (add_cert_puk_store(file_get_data(ca_ef), file_get_size(ca_ef), - false) != CCID_OK) { + false) != PICOKEY_OK) { return SW_FILE_FULL(); } diff --git a/src/hsm/cmd_reset_retry.c b/src/hsm/cmd_reset_retry.c index d17dc57..c5679c0 100644 --- a/src/hsm/cmd_reset_retry.c +++ b/src/hsm/cmd_reset_retry.c @@ -59,19 +59,19 @@ int cmd_reset_retry() { dhash[0] = newpin_len; double_hash_pin(apdu.data + (apdu.nc - newpin_len), newpin_len, dhash + 1); file_put_data(file_pin1, dhash, sizeof(dhash)); - if (pin_reset_retries(file_pin1, true) != CCID_OK) { + if (pin_reset_retries(file_pin1, true) != PICOKEY_OK) { return SW_MEMORY_FAILURE(); } uint8_t mkek[MKEK_SIZE]; int r = load_mkek(mkek); //loads the MKEK with SO pin - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } hash_multi(apdu.data + (apdu.nc - newpin_len), newpin_len, session_pin); has_session_pin = true; r = store_mkek(mkek); release_mkek(mkek); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_EXEC_ERROR(); } low_flash_available(); @@ -99,7 +99,7 @@ int cmd_reset_retry() { return SW_WRONG_LENGTH(); } } - if (pin_reset_retries(file_pin1, true) != CCID_OK) { + if (pin_reset_retries(file_pin1, true) != PICOKEY_OK) { return SW_MEMORY_FAILURE(); } return SW_OK(); diff --git a/src/hsm/cmd_signature.c b/src/hsm/cmd_signature.c index fc9bf40..69fba07 100644 --- a/src/hsm/cmd_signature.c +++ b/src/hsm/cmd_signature.c @@ -82,17 +82,17 @@ int pkcs1_strip_digest_info_prefix(mbedtls_md_type_t *algorithm, *algorithm = digest_info_prefix[i].algorithm; } if (out_dat == NULL) { - return CCID_OK; + return PICOKEY_OK; } if (*out_len < hash_len) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } memmove(out_dat, in_dat + hdr_len, hash_len); *out_len = hash_len; - return CCID_OK; + return PICOKEY_OK; } } - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } //----- @@ -142,9 +142,9 @@ int cmd_signature() { mbedtls_rsa_init(&ctx); int r = load_private_key_rsa(&ctx, fkey); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { mbedtls_rsa_free(&ctx); - if (r == CCID_VERIFICATION_FAILED) { + if (r == PICOKEY_VERIFICATION_FAILED) { return SW_SECURE_MESSAGE_EXEC_ERROR(); } return SW_EXEC_ERROR(); @@ -153,7 +153,7 @@ int cmd_signature() { if (p2 == ALGO_RSA_PKCS1) { //DigestInfo attached uint16_t nc = (uint16_t)apdu.nc; if (pkcs1_strip_digest_info_prefix(&md, apdu.data, (uint16_t)apdu.nc, apdu.data, - &nc) != CCID_OK) { //gets the MD algo id and strips it off + &nc) != PICOKEY_OK) { //gets the MD algo id and strips it off return SW_EXEC_ERROR(); } apdu.nc = nc; @@ -264,9 +264,9 @@ int cmd_signature() { md = MBEDTLS_MD_SHA512; } int r = load_private_key_ecdsa(&ctx, fkey); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { mbedtls_ecdsa_free(&ctx); - if (r == CCID_VERIFICATION_FAILED) { + if (r == PICOKEY_VERIFICATION_FAILED) { return SW_SECURE_MESSAGE_EXEC_ERROR(); } return SW_EXEC_ERROR(); diff --git a/src/hsm/cmd_update_ef.c b/src/hsm/cmd_update_ef.c index 7eba19a..1134666 100644 --- a/src/hsm/cmd_update_ef.c +++ b/src/hsm/cmd_update_ef.c @@ -78,7 +78,7 @@ int cmd_update_ef() { } if (offset == 0) { int r = file_put_data(ef, data, data_len); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_MEMORY_FAILURE(); } } @@ -92,7 +92,7 @@ int cmd_update_ef() { memcpy(data_merge + offset, data, data_len); int r = file_put_data(ef, data_merge, offset + data_len); free(data_merge); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_MEMORY_FAILURE(); } } diff --git a/src/hsm/cvc.c b/src/hsm/cvc.c index 30a88a4..f91a043 100644 --- a/src/hsm/cvc.c +++ b/src/hsm/cvc.c @@ -328,7 +328,7 @@ uint16_t asn1_cvc_aut(void *rsa_ecdsa, } mbedtls_ecdsa_context ectx; mbedtls_ecdsa_init(&ectx); - if (load_private_key_ecdsa(&ectx, fkey) != CCID_OK) { + if (load_private_key_ecdsa(&ectx, fkey) != PICOKEY_OK) { mbedtls_ecdsa_free(&ectx); return 0; } @@ -692,12 +692,12 @@ int puk_verify(const uint8_t *sig, uint16_t puk_len = 0; const uint8_t *puk = cvc_get_pub(ca, ca_len, &puk_len); if (!puk) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } uint16_t oid_len = 0; const uint8_t *oid = cvc_get_field(puk, puk_len, &oid_len, 0x6); if (!oid) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (memcmp(oid, OID_ID_TA_RSA, 9) == 0) { //RSA uint16_t t81_len = 0, t82_len = 0; @@ -706,7 +706,7 @@ int puk_verify(const uint8_t *sig, &t81_len, 0x82); if (!t81 || !t82) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } mbedtls_rsa_context rsa; mbedtls_rsa_init(&rsa); @@ -734,32 +734,32 @@ int puk_verify(const uint8_t *sig, } if (md == MBEDTLS_MD_NONE) { mbedtls_rsa_free(&rsa); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } int r = mbedtls_mpi_read_binary(&rsa.N, t81, t81_len); if (r != 0) { mbedtls_rsa_free(&rsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } r = mbedtls_mpi_read_binary(&rsa.E, t82, t82_len); if (r != 0) { mbedtls_rsa_free(&rsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } r = mbedtls_rsa_complete(&rsa); if (r != 0) { mbedtls_rsa_free(&rsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } r = mbedtls_rsa_check_pubkey(&rsa); if (r != 0) { mbedtls_rsa_free(&rsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } r = mbedtls_rsa_pkcs1_verify(&rsa, md, (unsigned int)hash_len, hash, sig); mbedtls_rsa_free(&rsa); if (r != 0) { - return CCID_WRONG_SIGNATURE; + return PICOKEY_WRONG_SIGNATURE; } } else if (memcmp(oid, OID_ID_TA_ECDSA, 9) == 0) { //ECC @@ -780,34 +780,34 @@ int puk_verify(const uint8_t *sig, md = MBEDTLS_MD_SHA512; } if (md == MBEDTLS_MD_NONE) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } uint16_t t86_len = 0; const uint8_t *t86 = cvc_get_field(puk, puk_len, &t86_len, 0x86); if (!t86) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } mbedtls_ecp_group_id ec_id = cvc_inherite_ec_group(ca, ca_len); if (ec_id == MBEDTLS_ECP_DP_NONE) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } mbedtls_ecdsa_context ecdsa; mbedtls_ecdsa_init(&ecdsa); int ret = mbedtls_ecp_group_load(&ecdsa.grp, ec_id); if (ret != 0) { mbedtls_ecdsa_free(&ecdsa); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } ret = mbedtls_ecp_point_read_binary(&ecdsa.grp, &ecdsa.Q, t86, t86_len); if (ret != 0) { mbedtls_ecdsa_free(&ecdsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } ret = mbedtls_ecp_check_pubkey(&ecdsa.grp, &ecdsa.Q); if (ret != 0) { mbedtls_ecdsa_free(&ecdsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } mbedtls_mpi r, s; mbedtls_mpi_init(&r); @@ -817,44 +817,44 @@ int puk_verify(const uint8_t *sig, mbedtls_mpi_free(&r); mbedtls_mpi_free(&s); mbedtls_ecdsa_free(&ecdsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } ret = mbedtls_mpi_read_binary(&s, sig + sig_len / 2, sig_len / 2); if (ret != 0) { mbedtls_mpi_free(&r); mbedtls_mpi_free(&s); mbedtls_ecdsa_free(&ecdsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } ret = mbedtls_ecdsa_verify(&ecdsa.grp, hash, hash_len, &ecdsa.Q, &r, &s); mbedtls_mpi_free(&r); mbedtls_mpi_free(&s); mbedtls_ecdsa_free(&ecdsa); if (ret != 0) { - return CCID_WRONG_SIGNATURE; + return PICOKEY_WRONG_SIGNATURE; } } - return CCID_OK; + return PICOKEY_OK; } int cvc_verify(const uint8_t *cert, uint16_t cert_len, const uint8_t *ca, uint16_t ca_len) { uint16_t puk_len = 0; const uint8_t *puk = cvc_get_pub(ca, ca_len, &puk_len); if (!puk) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } uint16_t oid_len = 0, cv_body_len = 0, sig_len = 0; const uint8_t *oid = cvc_get_field(puk, puk_len, &oid_len, 0x6); const uint8_t *cv_body = cvc_get_body(cert, cert_len, &cv_body_len); const uint8_t *sig = cvc_get_sig(cert, cert_len, &sig_len); if (!sig) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (!cv_body) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (!oid) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } mbedtls_md_type_t md = MBEDTLS_MD_NONE; if (memcmp(oid, OID_ID_TA_RSA, 9) == 0) { //RSA @@ -895,18 +895,18 @@ int cvc_verify(const uint8_t *cert, uint16_t cert_len, const uint8_t *ca, uint16 } } if (md == MBEDTLS_MD_NONE) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md); uint8_t hash[64], hash_len = mbedtls_md_get_size(md_info); uint8_t tlv_body = 2 + format_tlv_len(cv_body_len, NULL); int r = mbedtls_md(md_info, cv_body - tlv_body, cv_body_len + tlv_body, hash); if (r != 0) { - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } r = puk_verify(sig, sig_len, hash, hash_len, ca, ca_len); if (r != 0) { - return CCID_WRONG_SIGNATURE; + return PICOKEY_WRONG_SIGNATURE; } - return CCID_OK; + return PICOKEY_OK; } diff --git a/src/hsm/kek.c b/src/hsm/kek.c index 8f065bd..b22de83 100644 --- a/src/hsm/kek.c +++ b/src/hsm/kek.c @@ -52,7 +52,7 @@ uint32_t crc32c(const uint8_t *buf, size_t len) { int load_mkek(uint8_t *mkek) { if (has_session_pin == false && has_session_sopin == false) { - return CCID_NO_LOGIN; + return PICOKEY_NO_LOGIN; } const uint8_t *pin = NULL; if (pin == NULL && has_session_pin == true) { @@ -70,15 +70,15 @@ int load_mkek(uint8_t *mkek) { } } if (pin == NULL) { //Should never happen - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } int ret = aes_decrypt_cfb_256(pin, MKEK_IV(mkek), MKEK_KEY(mkek), MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE); if (ret != 0) { - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } if (crc32c(MKEK_KEY(mkek), MKEK_KEY_SIZE) != *(uint32_t *) MKEK_CHECKSUM(mkek)) { - return CCID_WRONG_DKEK; + return PICOKEY_WRONG_DKEK; } if (has_mkek_mask || otp_key_1) { const uint8_t *mask = otp_key_1 ? otp_key_1 : mkek_mask; @@ -86,7 +86,7 @@ int load_mkek(uint8_t *mkek) { MKEK_KEY(mkek)[i] ^= mask[i]; } } - return CCID_OK; + return PICOKEY_OK; } mse_t mse = { .init = false }; @@ -103,7 +103,7 @@ int mse_decrypt_ct(uint8_t *data, size_t len) { int load_dkek(uint8_t id, uint8_t *dkek) { file_t *tf = search_file(EF_DKEK + id); if (!file_has_data(tf)) { - return CCID_ERR_FILE_NOT_FOUND; + return PICOKEY_ERR_FILE_NOT_FOUND; } memcpy(dkek, file_get_data(tf), DKEK_KEY_SIZE); return mkek_decrypt(dkek, DKEK_KEY_SIZE); @@ -115,7 +115,7 @@ void release_mkek(uint8_t *mkek) { int store_mkek(const uint8_t *mkek) { if (has_session_pin == false && has_session_sopin == false) { - return CCID_NO_LOGIN; + return PICOKEY_NO_LOGIN; } uint8_t tmp_mkek[MKEK_SIZE]; if (mkek == NULL) { @@ -133,7 +133,7 @@ int store_mkek(const uint8_t *mkek) { if (!tf) { release_mkek(tmp_mkek); release_mkek(tmp_mkek_pin); - return CCID_ERR_FILE_NOT_FOUND; + return PICOKEY_ERR_FILE_NOT_FOUND; } aes_encrypt_cfb_256(session_pin, MKEK_IV(tmp_mkek_pin), MKEK_KEY(tmp_mkek_pin), MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE); file_put_data(tf, tmp_mkek_pin, MKEK_SIZE); @@ -146,7 +146,7 @@ int store_mkek(const uint8_t *mkek) { if (!tf) { release_mkek(tmp_mkek); release_mkek(tmp_mkek_sopin); - return CCID_ERR_FILE_NOT_FOUND; + return PICOKEY_ERR_FILE_NOT_FOUND; } aes_encrypt_cfb_256(session_sopin, MKEK_IV(tmp_mkek_sopin), MKEK_KEY(tmp_mkek_sopin), MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE); file_put_data(tf, tmp_mkek_sopin, MKEK_SIZE); @@ -154,21 +154,21 @@ int store_mkek(const uint8_t *mkek) { } low_flash_available(); release_mkek(tmp_mkek); - return CCID_OK; + return PICOKEY_OK; } int store_dkek_key(uint8_t id, uint8_t *dkek) { file_t *tf = search_file(EF_DKEK + id); if (!tf) { - return CCID_ERR_FILE_NOT_FOUND; + return PICOKEY_ERR_FILE_NOT_FOUND; } int r = mkek_encrypt(dkek, DKEK_KEY_SIZE); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } file_put_data(tf, dkek, DKEK_KEY_SIZE); low_flash_available(); - return CCID_OK; + return PICOKEY_OK; } int save_dkek_key(uint8_t id, const uint8_t *key) { @@ -176,7 +176,7 @@ int save_dkek_key(uint8_t id, const uint8_t *key) { if (!key) { file_t *tf = search_file(EF_DKEK + id); if (!tf) { - return CCID_ERR_FILE_NOT_FOUND; + return PICOKEY_ERR_FILE_NOT_FOUND; } memcpy(dkek, file_get_data(tf), DKEK_KEY_SIZE); } @@ -190,7 +190,7 @@ int import_dkek_share(uint8_t id, const uint8_t *share) { uint8_t tmp_dkek[DKEK_KEY_SIZE]; file_t *tf = search_file(EF_DKEK + id); if (!tf) { - return CCID_ERR_FILE_NOT_FOUND; + return PICOKEY_ERR_FILE_NOT_FOUND; } memset(tmp_dkek, 0, sizeof(tmp_dkek)); if (file_get_size(tf) == DKEK_KEY_SIZE) { @@ -201,7 +201,7 @@ int import_dkek_share(uint8_t id, const uint8_t *share) { } file_put_data(tf, tmp_dkek, DKEK_KEY_SIZE); low_flash_available(); - return CCID_OK; + return PICOKEY_OK; } int dkek_kcv(uint8_t id, uint8_t *kcv) { //kcv 8 bytes @@ -209,45 +209,45 @@ int dkek_kcv(uint8_t id, uint8_t *kcv) { //kcv 8 bytes memset(kcv, 0, 8); memset(hsh, 0, sizeof(hsh)); int r = load_dkek(id, dkek); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } hash256(dkek, DKEK_KEY_SIZE, hsh); mbedtls_platform_zeroize(dkek, sizeof(dkek)); memcpy(kcv, hsh, 8); - return CCID_OK; + return PICOKEY_OK; } int dkek_kenc(uint8_t id, uint8_t *kenc) { //kenc 32 bytes uint8_t dkek[DKEK_KEY_SIZE + 4]; memset(kenc, 0, 32); int r = load_dkek(id, dkek); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } memcpy(dkek + DKEK_KEY_SIZE, "\x0\x0\x0\x1", 4); hash256(dkek, sizeof(dkek), kenc); mbedtls_platform_zeroize(dkek, sizeof(dkek)); - return CCID_OK; + return PICOKEY_OK; } int dkek_kmac(uint8_t id, uint8_t *kmac) { //kmac 32 bytes uint8_t dkek[DKEK_KEY_SIZE + 4]; memset(kmac, 0, 32); int r = load_dkek(id, dkek); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } memcpy(dkek + DKEK_KEY_SIZE, "\x0\x0\x0\x2", 4); hash256(dkek, DKEK_KEY_SIZE + 4, kmac); mbedtls_platform_zeroize(dkek, sizeof(dkek)); - return CCID_OK; + return PICOKEY_OK; } int mkek_encrypt(uint8_t *data, uint16_t len) { int r; uint8_t mkek[MKEK_SIZE + 4]; - if ((r = load_mkek(mkek)) != CCID_OK) { + if ((r = load_mkek(mkek)) != PICOKEY_OK) { return r; } r = aes_encrypt_cfb_256(MKEK_KEY(mkek), MKEK_IV(mkek), data, len); @@ -258,7 +258,7 @@ int mkek_encrypt(uint8_t *data, uint16_t len) { int mkek_decrypt(uint8_t *data, uint16_t len) { int r; uint8_t mkek[MKEK_SIZE + 4]; - if ((r = load_mkek(mkek)) != CCID_OK) { + if ((r = load_mkek(mkek)) != PICOKEY_OK) { return r; } r = aes_decrypt_cfb_256(MKEK_KEY(mkek), MKEK_IV(mkek), data, len); @@ -268,7 +268,7 @@ int mkek_decrypt(uint8_t *data, uint16_t len) { int dkek_encode_key(uint8_t id, void *key_ctx, int key_type, uint8_t *out, uint16_t *out_len, const uint8_t *allowed, uint16_t allowed_len) { if (!(key_type & PICO_KEYS_KEY_RSA) && !(key_type & PICO_KEYS_KEY_EC) && !(key_type & PICO_KEYS_KEY_AES)) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } uint8_t kb[8 + 2 * 4 + 2 * 4096 / 8 + 3 + 13]; //worst case: RSA-4096 (plus, 13 bytes padding) @@ -280,21 +280,21 @@ int dkek_encode_key(uint8_t id, void *key_ctx, int key_type, uint8_t *out, uint1 uint8_t kenc[32]; memset(kenc, 0, sizeof(kenc)); r = dkek_kenc(id, kenc); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } uint8_t kcv[8]; memset(kcv, 0, sizeof(kcv)); r = dkek_kcv(id, kcv); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } uint8_t kmac[32]; memset(kmac, 0, sizeof(kmac)); r = dkek_kmac(id, kmac); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } @@ -313,10 +313,10 @@ int dkek_encode_key(uint8_t id, void *key_ctx, int key_type, uint8_t *out, uint1 } if (kb_len != 16 && kb_len != 24 && kb_len != 32 && kb_len != 64) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (*out_len < 8 + 1 + 10 + 6 + (2 + 64 + 14) + 16) { // 14 bytes padding - return CCID_WRONG_LENGTH; + return PICOKEY_WRONG_LENGTH; } put_uint16_t(kb_len, kb + 8); @@ -328,7 +328,7 @@ int dkek_encode_key(uint8_t id, void *key_ctx, int key_type, uint8_t *out, uint1 } else if (key_type & PICO_KEYS_KEY_RSA) { if (*out_len < 8 + 1 + 12 + 6 + (8 + 2 * 4 + 2 * 4096 / 8 + 3 + 13) + 16) { //13 bytes pading - return CCID_WRONG_LENGTH; + return PICOKEY_WRONG_LENGTH; } mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) key_ctx; kb_len = 0; @@ -349,7 +349,7 @@ int dkek_encode_key(uint8_t id, void *key_ctx, int key_type, uint8_t *out, uint1 } else if (key_type & PICO_KEYS_KEY_EC) { if (*out_len < 8 + 1 + 12 + 6 + (8 + 2 * 8 + 9 * 66 + 2 + 4) + 16) { //4 bytes pading - return CCID_WRONG_LENGTH; + return PICOKEY_WRONG_LENGTH; } mbedtls_ecdsa_context *ecdsa = (mbedtls_ecdsa_context *) key_ctx; kb_len = 0; @@ -430,7 +430,7 @@ int dkek_encode_key(uint8_t id, void *key_ctx, int key_type, uint8_t *out, uint1 kb[kb_len] = 0x80; } r = aes_encrypt(kenc, NULL, 256, PICO_KEYS_AES_MODE_CBC, kb, kb_len_pad); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } @@ -443,7 +443,7 @@ int dkek_encode_key(uint8_t id, void *key_ctx, int key_type, uint8_t *out, uint1 if (r != 0) { return r; } - return CCID_OK; + return PICOKEY_OK; } int dkek_type_key(const uint8_t *in) { @@ -464,54 +464,54 @@ int dkek_decode_key(uint8_t id, void *key_ctx, const uint8_t *in, uint16_t in_le int r = 0; memset(kcv, 0, sizeof(kcv)); r = dkek_kcv(id, kcv); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } uint8_t kmac[32]; memset(kmac, 0, sizeof(kmac)); r = dkek_kmac(id, kmac); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } uint8_t kenc[32]; memset(kenc, 0, sizeof(kenc)); r = dkek_kenc(id, kenc); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } if (memcmp(kcv, in, 8) != 0) { - return CCID_WRONG_DKEK; + return PICOKEY_WRONG_DKEK; } uint8_t signature[16]; r = mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_ECB), kmac, 256, in, in_len - 16, signature); if (r != 0) { - return CCID_WRONG_SIGNATURE; + return PICOKEY_WRONG_SIGNATURE; } if (memcmp(signature, in + in_len - 16, 16) != 0) { - return CCID_WRONG_SIGNATURE; + return PICOKEY_WRONG_SIGNATURE; } int key_type = in[8]; if (key_type != 5 && key_type != 6 && key_type != 12 && key_type != 15) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if ((key_type == 5 || key_type == 6) && memcmp(in + 9, "\x00\x0A\x04\x00\x7F\x00\x07\x02\x02\x02\x01\x02", 12) != 0) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (key_type == 12 && memcmp(in + 9, "\x00\x0A\x04\x00\x7F\x00\x07\x02\x02\x02\x02\x03", 12) != 0) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (key_type == 15 && memcmp(in + 9, "\x00\x08\x60\x86\x48\x01\x65\x03\x04\x01", 10) != 0) { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } uint16_t ofs = 9; @@ -535,13 +535,13 @@ int dkek_decode_key(uint8_t id, void *key_ctx, const uint8_t *in, uint16_t in_le ofs += len + 2; if ((in_len - 16 - ofs) % 16 != 0) { - return CCID_WRONG_PADDING; + return PICOKEY_WRONG_PADDING; } uint8_t kb[8 + 2 * 4 + 2 * 4096 / 8 + 3 + 13]; //worst case: RSA-4096 (plus, 13 bytes padding) memset(kb, 0, sizeof(kb)); memcpy(kb, in + ofs, in_len - 16 - ofs); r = aes_decrypt(kenc, NULL, 256, PICO_KEYS_AES_MODE_CBC, kb, in_len - 16 - ofs); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } @@ -558,14 +558,14 @@ int dkek_decode_key(uint8_t id, void *key_ctx, const uint8_t *in, uint16_t in_le r = mbedtls_mpi_read_binary(&rsa->D, kb + ofs, len); ofs += len; if (r != 0) { mbedtls_rsa_free(rsa); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } len = get_uint16_t(kb, ofs); ofs += 2; r = mbedtls_mpi_read_binary(&rsa->N, kb + ofs, len); ofs += len; if (r != 0) { mbedtls_rsa_free(rsa); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } } else if (key_type == 6) { @@ -579,7 +579,7 @@ int dkek_decode_key(uint8_t id, void *key_ctx, const uint8_t *in, uint16_t in_le r = mbedtls_mpi_read_binary(&rsa->P, kb + ofs, len); ofs += len; if (r != 0) { mbedtls_rsa_free(rsa); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } //PQ @@ -589,7 +589,7 @@ int dkek_decode_key(uint8_t id, void *key_ctx, const uint8_t *in, uint16_t in_le r = mbedtls_mpi_read_binary(&rsa->Q, kb + ofs, len); ofs += len; if (r != 0) { mbedtls_rsa_free(rsa); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } //N len = get_uint16_t(kb, ofs); ofs += len + 2; @@ -599,33 +599,33 @@ int dkek_decode_key(uint8_t id, void *key_ctx, const uint8_t *in, uint16_t in_le r = mbedtls_mpi_read_binary(&rsa->E, kb + ofs, len); ofs += len; if (r != 0) { mbedtls_rsa_free(rsa); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (key_type == 5) { r = mbedtls_rsa_import(rsa, &rsa->N, NULL, NULL, &rsa->D, &rsa->E); if (r != 0) { mbedtls_rsa_free(rsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } } else if (key_type == 6) { r = mbedtls_rsa_import(rsa, NULL, &rsa->P, &rsa->Q, NULL, &rsa->E); if (r != 0) { mbedtls_rsa_free(rsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } } r = mbedtls_rsa_complete(rsa); if (r != 0) { mbedtls_rsa_free(rsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } r = mbedtls_rsa_check_privkey(rsa); if (r != 0) { mbedtls_rsa_free(rsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } } else if (key_type == 12) { @@ -643,7 +643,7 @@ int dkek_decode_key(uint8_t id, void *key_ctx, const uint8_t *in, uint16_t in_le mbedtls_ecp_group_id ec_id = ec_get_curve_from_prime(kb + ofs, len); if (ec_id == MBEDTLS_ECP_DP_NONE) { mbedtls_ecdsa_free(ecdsa); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } ofs += len; @@ -658,7 +658,7 @@ int dkek_decode_key(uint8_t id, void *key_ctx, const uint8_t *in, uint16_t in_le r = mbedtls_ecp_read_key(ec_id, ecdsa, kb + ofs, len); if (r != 0) { mbedtls_ecdsa_free(ecdsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } ofs += len; @@ -669,17 +669,17 @@ int dkek_decode_key(uint8_t id, void *key_ctx, const uint8_t *in, uint16_t in_le r = mbedtls_ecp_mul(&ecdsa->grp, &ecdsa->Q, &ecdsa->d, &ecdsa->grp.G, random_gen, NULL); if (r != 0) { mbedtls_ecdsa_free(ecdsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } } r = mbedtls_ecp_check_pub_priv(ecdsa, ecdsa, random_gen, NULL); if (r != 0) { mbedtls_ecdsa_free(ecdsa); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } } else if (key_type == 15) { memcpy(key_ctx, kb + ofs, key_size); } - return CCID_OK; + return PICOKEY_OK; } diff --git a/src/hsm/sc_hsm.c b/src/hsm/sc_hsm.c index 5cf89cc..d13228a 100644 --- a/src/hsm/sc_hsm.c +++ b/src/hsm/sc_hsm.c @@ -85,7 +85,7 @@ int sc_hsm_select_aid(app_t *a, uint8_t force) { a->process_apdu = sc_hsm_process_apdu; a->unload = sc_hsm_unload; init_sc_hsm(); - return CCID_OK; + return PICOKEY_OK; } INITIALIZER( sc_hsm_ctor ) { @@ -178,10 +178,10 @@ uint8_t puk_status[MAX_PUK]; int add_cert_puk_store(const uint8_t *data, uint16_t data_len, bool copy) { if (data == NULL || data_len == 0) { - return CCID_ERR_NULL_PARAM; + return PICOKEY_ERR_NULL_PARAM; } if (puk_store_entries == MAX_PUK_STORE_ENTRIES) { - return CCID_ERR_MEMORY_FATAL; + return PICOKEY_ERR_MEMORY_FATAL; } puk_store[puk_store_entries].copied = copy; @@ -205,17 +205,17 @@ int add_cert_puk_store(const uint8_t *data, uint16_t data_len, bool copy) { &puk_store[puk_store_entries].puk_len); puk_store_entries++; - return CCID_OK; + return PICOKEY_OK; } int puk_store_select_chr(const uint8_t *chr) { for (int i = 0; i < puk_store_entries; i++) { if (memcmp(puk_store[i].chr, chr, puk_store[i].chr_len) == 0) { current_puk = &puk_store[i]; - return CCID_OK; + return PICOKEY_OK; } } - return CCID_ERR_FILE_NOT_FOUND; + return PICOKEY_ERR_FILE_NOT_FOUND; } void reset_puk_store() { @@ -261,7 +261,7 @@ int sc_hsm_unload() { has_session_pin = has_session_sopin = false; isUserAuthenticated = false; sm_session_pin_len = 0; - return CCID_OK; + return PICOKEY_OK; } uint16_t get_device_options() { @@ -334,16 +334,16 @@ int parse_ef_dir(const file_t *f, int mode) { int pin_reset_retries(const file_t *pin, bool force) { if (!pin) { - return CCID_ERR_NULL_PARAM; + return PICOKEY_ERR_NULL_PARAM; } const file_t *max = search_file(pin->fid + 1); const file_t *act = search_file(pin->fid + 2); if (!max || !act) { - return CCID_ERR_FILE_NOT_FOUND; + return PICOKEY_ERR_FILE_NOT_FOUND; } uint8_t retries = file_read_uint8(act); if (retries == 0 && force == false) { // blocked - return CCID_ERR_BLOCKED; + return PICOKEY_ERR_BLOCKED; } retries = file_read_uint8(max); int r = file_put_data((file_t *) act, &retries, sizeof(retries)); @@ -353,26 +353,26 @@ int pin_reset_retries(const file_t *pin, bool force) { int pin_wrong_retry(const file_t *pin) { if (!pin) { - return CCID_ERR_NULL_PARAM; + return PICOKEY_ERR_NULL_PARAM; } const file_t *act = search_file(pin->fid + 2); if (!act) { - return CCID_ERR_FILE_NOT_FOUND; + return PICOKEY_ERR_FILE_NOT_FOUND; } uint8_t retries = file_read_uint8(act); if (retries > 0) { retries -= 1; int r = file_put_data((file_t *) act, &retries, sizeof(retries)); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } low_flash_available(); if (retries == 0) { - return CCID_ERR_BLOCKED; + return PICOKEY_ERR_BLOCKED; } return retries; } - return CCID_ERR_BLOCKED; + return PICOKEY_ERR_BLOCKED; } bool pka_enabled() { @@ -391,7 +391,7 @@ uint16_t check_pin(const file_t *pin, const uint8_t *data, uint16_t len) { if (is_secured_apdu() && sm_session_pin_len > 0 && pin == file_pin1) { if (len == sm_session_pin_len && memcmp(data, sm_session_pin, len) != 0) { int retries; - if ((retries = pin_wrong_retry(pin)) < CCID_OK) { + if ((retries = pin_wrong_retry(pin)) < PICOKEY_OK) { return SW_PIN_BLOCKED(); } return set_res_sw(0x63, 0xc0 | (uint8_t)retries); @@ -405,17 +405,17 @@ uint16_t check_pin(const file_t *pin, const uint8_t *data, uint16_t len) { } if (memcmp(file_get_data(pin) + 1, dhash, sizeof(dhash)) != 0) { int retries; - if ((retries = pin_wrong_retry(pin)) < CCID_OK) { + if ((retries = pin_wrong_retry(pin)) < PICOKEY_OK) { return SW_PIN_BLOCKED(); } return set_res_sw(0x63, 0xc0 | (uint8_t)retries); } } int r = pin_reset_retries(pin, false); - if (r == CCID_ERR_BLOCKED) { + if (r == PICOKEY_ERR_BLOCKED) { return SW_PIN_BLOCKED(); } - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_MEMORY_FAILURE(); } if (pka_enabled() == false) { @@ -552,18 +552,18 @@ int store_keys(void *key_ctx, int type, uint8_t key_id) { memcpy(kdata, key_ctx, key_size); } else { - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } file_t *fpk = file_new((KEY_PREFIX << 8) | key_id); if (!fpk) { - return CCID_ERR_MEMORY_FATAL; + return PICOKEY_ERR_MEMORY_FATAL; } r = mkek_encrypt(kdata, key_size); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } r = file_put_data(fpk, kdata, (uint16_t)key_size); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return r; } char key_id_str[4] = {0}; @@ -580,7 +580,7 @@ int store_keys(void *key_ctx, int type, uint8_t key_id) { } } low_flash_available(); - return CCID_OK; + return PICOKEY_OK; } int find_and_store_meta_key(uint8_t key_id) { @@ -614,81 +614,81 @@ int find_and_store_meta_key(uint8_t key_id) { int r = meta_add((KEY_PREFIX << 8) | key_id, meta, (uint16_t)meta_size); free(meta); if (r != 0) { - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } } - return CCID_OK; + return PICOKEY_OK; } int load_private_key_rsa(mbedtls_rsa_context *ctx, file_t *fkey) { if (wait_button_pressed() == true) { // timeout - return CCID_VERIFICATION_FAILED; + return PICOKEY_VERIFICATION_FAILED; } uint16_t key_size = file_get_size(fkey); uint8_t kdata[4096 / 8]; memcpy(kdata, file_get_data(fkey), key_size); if (mkek_decrypt(kdata, key_size) != 0) { - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } if (mbedtls_mpi_read_binary(&ctx->P, kdata, key_size / 2) != 0) { mbedtls_platform_zeroize(kdata, sizeof(kdata)); mbedtls_rsa_free(ctx); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (mbedtls_mpi_read_binary(&ctx->Q, kdata + key_size / 2, key_size / 2) != 0) { mbedtls_platform_zeroize(kdata, sizeof(kdata)); mbedtls_rsa_free(ctx); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (mbedtls_mpi_lset(&ctx->E, 0x10001) != 0) { mbedtls_platform_zeroize(kdata, sizeof(kdata)); mbedtls_rsa_free(ctx); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } if (mbedtls_rsa_import(ctx, NULL, &ctx->P, &ctx->Q, NULL, &ctx->E) != 0) { mbedtls_platform_zeroize(kdata, sizeof(kdata)); mbedtls_rsa_free(ctx); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (mbedtls_rsa_complete(ctx) != 0) { mbedtls_platform_zeroize(kdata, sizeof(kdata)); mbedtls_rsa_free(ctx); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } if (mbedtls_rsa_check_privkey(ctx) != 0) { mbedtls_platform_zeroize(kdata, sizeof(kdata)); mbedtls_rsa_free(ctx); - return CCID_WRONG_DATA; + return PICOKEY_WRONG_DATA; } - return CCID_OK; + return PICOKEY_OK; } int load_private_key_ecdsa(mbedtls_ecdsa_context *ctx, file_t *fkey) { if (wait_button_pressed() == true) { // timeout - return CCID_VERIFICATION_FAILED; + return PICOKEY_VERIFICATION_FAILED; } uint16_t key_size = file_get_size(fkey); uint8_t kdata[67]; // Worst case, 521 bit + 1byte memcpy(kdata, file_get_data(fkey), key_size); if (mkek_decrypt(kdata, key_size) != 0) { - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } mbedtls_ecp_group_id gid = kdata[0]; int r = mbedtls_ecp_read_key(gid, ctx, kdata + 1, key_size - 1); if (r != 0) { mbedtls_platform_zeroize(kdata, sizeof(kdata)); mbedtls_ecdsa_free(ctx); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } mbedtls_platform_zeroize(kdata, sizeof(kdata)); r = mbedtls_ecp_mul(&ctx->grp, &ctx->Q, &ctx->d, &ctx->grp.G, random_gen, NULL); if (r != 0) { mbedtls_ecdsa_free(ctx); - return CCID_EXEC_ERROR; + return PICOKEY_EXEC_ERROR; } - return CCID_OK; + return PICOKEY_OK; } #define INS_VERIFY 0x20 @@ -754,7 +754,7 @@ static const cmd_t cmds[] = { int sc_hsm_process_apdu() { int r = sm_unwrap(); - if (r != CCID_OK) { + if (r != PICOKEY_OK) { return SW_DATA_INVALID(); } for (const cmd_t *cmd = cmds; cmd->ins != 0x00; cmd++) { From f74a374c64289f50c810b1140c6cdf5945574a25 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 5 Nov 2024 19:33:07 +0100 Subject: [PATCH 46/60] Fix version header. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 6625678..e85d77c 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 6625678c3059554ef9fc38c1fd0ff16fa4dbad3e +Subproject commit e85d77c08437e7f2ba269dc91f796ad49df1f0f8 From f5b89aed69a3d083938bc393c13b979ed189079d Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Thu, 7 Nov 2024 00:16:22 +0100 Subject: [PATCH 47/60] Use DEV key from OTP if available when initializing. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- src/hsm/cmd_initialize.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index e85d77c..cf36c29 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit e85d77c08437e7f2ba269dc91f796ad49df1f0f8 +Subproject commit cf36c2988c323226d40361b65a7b52e35def35f6 diff --git a/src/hsm/cmd_initialize.c b/src/hsm/cmd_initialize.c index ff0abaa..a94a92e 100644 --- a/src/hsm/cmd_initialize.c +++ b/src/hsm/cmd_initialize.c @@ -23,6 +23,7 @@ #include "version.h" #include "asn1.h" #include "cvc.h" +#include "otp.h" extern void scan_all(); @@ -183,8 +184,13 @@ int cmd_initialize() { mbedtls_ecdsa_context ecdsa; mbedtls_ecdsa_init(&ecdsa); mbedtls_ecp_group_id ec_id = MBEDTLS_ECP_DP_SECP256R1; - uint8_t index = 0, key_id = 0; - ret = mbedtls_ecdsa_genkey(&ecdsa, ec_id, random_gen, &index); + uint8_t key_id = 0; + if (otp_key_2) { + ret = mbedtls_ecp_read_key(MBEDTLS_ECP_DP_SECP256K1, &ecdsa, otp_key_2, 32); + } + else { + ret = mbedtls_ecdsa_genkey(&ecdsa, ec_id, random_gen, NULL); + } if (ret != 0) { mbedtls_ecdsa_free(&ecdsa); return SW_EXEC_ERROR(); From 9b9ea7cae58da017461427d1ce4f80380c8e9c12 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Thu, 7 Nov 2024 20:00:41 +0100 Subject: [PATCH 48/60] Add product and mcu to info in rescue mode. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- src/hsm/sc_hsm.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index cf36c29..758d7b8 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit cf36c2988c323226d40361b65a7b52e35def35f6 +Subproject commit 758d7b88cd5bf8457611f6237e9b9c2819adffab diff --git a/src/hsm/sc_hsm.c b/src/hsm/sc_hsm.c index d13228a..b41ab83 100644 --- a/src/hsm/sc_hsm.c +++ b/src/hsm/sc_hsm.c @@ -43,6 +43,8 @@ bool has_session_pin = false, has_session_sopin = false; const uint8_t *dev_name = NULL; uint16_t dev_name_len = 0; +uint8_t PICO_PRODUCT = 1; + static int sc_hsm_process_apdu(); static void init_sc_hsm(); From f1410bbf046abfdcf028a6b77ce90779654260e8 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 8 Nov 2024 17:45:48 +0100 Subject: [PATCH 49/60] Only request and upload the device certificate if needed. Signed-off-by: Pol Henarejos --- tools/pico-hsm-tool.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/tools/pico-hsm-tool.py b/tools/pico-hsm-tool.py index c124a06..8edd4e9 100644 --- a/tools/pico-hsm-tool.py +++ b/tools/pico-hsm-tool.py @@ -217,24 +217,27 @@ def initialize(picohsm, args): so_pin = '57621880' picohsm.initialize(pin=pin, sopin=so_pin) - response = picohsm.get_contents(DOPrefixes.EE_CERTIFICATE_PREFIX, 0x00) + try: + picohsm.select_file(0x2f02) + except APDUResponse: + response = picohsm.get_contents(DOPrefixes.EE_CERTIFICATE_PREFIX, 0x00) - cert = bytearray(response) - Y = CVC().decode(cert).pubkey().find(0x86).data() - print(f'Public Point: {hexlify(Y).decode()}') + cert = bytearray(response) + Y = CVC().decode(cert).pubkey().find(0x86).data() + print(f'Public Point: {hexlify(Y).decode()}') - pbk = base64.urlsafe_b64encode(Y) - data = urllib.parse.urlencode({'pubkey': pbk}).encode() - j = get_pki_data('cvc', data=data) - print('Device name: '+j['devname']) - dataef = base64.urlsafe_b64decode( - j['cvcert']) + base64.urlsafe_b64decode(j['dvcert']) + base64.urlsafe_b64decode(j['cacert']) + pbk = base64.urlsafe_b64encode(Y) + data = urllib.parse.urlencode({'pubkey': pbk}).encode() + j = get_pki_data('cvc', data=data) + print('Device name: '+j['devname']) + dataef = base64.urlsafe_b64decode( + j['cvcert']) + base64.urlsafe_b64decode(j['dvcert']) + base64.urlsafe_b64decode(j['cacert']) - picohsm.select_file(0x2f02) - response = picohsm.put_contents(0x0000, data=dataef) + picohsm.select_file(0x2f02) + response = picohsm.put_contents(0x0000, data=dataef) - print('Certificate uploaded successfully!') - print('') + print('Certificate uploaded successfully!') + print('') print('Note that the device is initialized with a default PIN and ' 'configuration.') print('Now you can initialize the device as usual with your chosen PIN ' From 4ec1d4d891501f29036ba19f8b77231c953bd80c Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 8 Nov 2024 17:52:13 +0100 Subject: [PATCH 50/60] Fix initialization and terminal certificate generation. Fixes #59. Signed-off-by: Pol Henarejos --- src/hsm/cmd_initialize.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/hsm/cmd_initialize.c b/src/hsm/cmd_initialize.c index a94a92e..eb61418 100644 --- a/src/hsm/cmd_initialize.c +++ b/src/hsm/cmd_initialize.c @@ -200,27 +200,26 @@ int cmd_initialize() { mbedtls_ecdsa_free(&ecdsa); return SW_EXEC_ERROR(); } - size_t cvc_len = 0; - if ((cvc_len = asn1_cvc_aut(&ecdsa, PICO_KEYS_KEY_EC, res_APDU, 4096, NULL, 0)) == 0) { + uint16_t ee_len = 0, term_len = 0; + if ((ee_len = asn1_cvc_aut(&ecdsa, PICO_KEYS_KEY_EC, res_APDU, 4096, NULL, 0)) == 0) { mbedtls_ecdsa_free(&ecdsa); return SW_EXEC_ERROR(); } file_t *fpk = search_file(EF_EE_DEV); - ret = file_put_data(fpk, res_APDU, (uint16_t)cvc_len); + ret = file_put_data(fpk, res_APDU, ee_len); if (ret != 0) { mbedtls_ecdsa_free(&ecdsa); return SW_EXEC_ERROR(); } - if ((cvc_len = asn1_cvc_cert(&ecdsa, PICO_KEYS_KEY_EC, res_APDU, 4096, NULL, 0, true)) == 0) { + if ((term_len = asn1_cvc_cert(&ecdsa, PICO_KEYS_KEY_EC, res_APDU + ee_len, 4096 - ee_len, NULL, 0, true)) == 0) { mbedtls_ecdsa_free(&ecdsa); return SW_EXEC_ERROR(); } - memcpy(res_APDU + cvc_len, res_APDU, cvc_len); mbedtls_ecdsa_free(&ecdsa); fpk = search_file(EF_TERMCA); - ret = file_put_data(fpk, res_APDU, (uint16_t)(2 * cvc_len)); + ret = file_put_data(fpk, res_APDU, ee_len + term_len); if (ret != 0) { return SW_EXEC_ERROR(); } From f301b2a1b10a2d7c6098ad82b506beb25dc1eb7e Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 8 Nov 2024 19:13:14 +0100 Subject: [PATCH 51/60] Add --no-dev-cert to do not request a certificate and use a self-signed one. Do not use if attestation is needed. Signed-off-by: Pol Henarejos --- tools/pico-hsm-tool.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/pico-hsm-tool.py b/tools/pico-hsm-tool.py index 8edd4e9..be8b8c0 100644 --- a/tools/pico-hsm-tool.py +++ b/tools/pico-hsm-tool.py @@ -69,6 +69,7 @@ def parse_args(): parser.add_argument('--pin', help='PIN number') parser_init.add_argument('--so-pin', help='SO-PIN number') parser_init.add_argument('--silent', help='Confirms initialization silently.', action='store_true') + parser_init.add_argument('--no-dev-cert', help='Do not request a device certificate (it will use a self-signed certificate). Do not use if attestation is needed.', action='store_true', default=False) parser_attestate = subparser.add_parser('attestate', help='Generates an attestation report for a private key and verifies the private key was generated in the devices or outside.') parser_attestate.add_argument('-k', '--key', help='The private key index', metavar='KEY_ID') @@ -217,9 +218,7 @@ def initialize(picohsm, args): so_pin = '57621880' picohsm.initialize(pin=pin, sopin=so_pin) - try: - picohsm.select_file(0x2f02) - except APDUResponse: + if (not args.no_dev_cert): response = picohsm.get_contents(DOPrefixes.EE_CERTIFICATE_PREFIX, 0x00) cert = bytearray(response) From 96359ebe03bc9a4b6fc8f43ed40fec2c08a0bcd9 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 8 Nov 2024 19:36:54 +0100 Subject: [PATCH 52/60] Add secure boot and secure lock commands via rescue. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 758d7b8..621d555 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 758d7b88cd5bf8457611f6237e9b9c2819adffab +Subproject commit 621d5553e116f47bcdbc6c4d0acc9b0698ffe22e From c239b4dd7c2286d23811862677ddc82e639fd94b Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 8 Nov 2024 19:46:00 +0100 Subject: [PATCH 53/60] Fix secure otp build for non rp2350. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 621d555..9018ebb 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 621d5553e116f47bcdbc6c4d0acc9b0698ffe22e +Subproject commit 9018ebb55dd9f60192233cc23b478425699893d5 From 94574338440c0ebb4574e0ae71c46dde1711fd4b Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sat, 9 Nov 2024 00:20:31 +0100 Subject: [PATCH 54/60] Add compile flags for optimization build in ESP32. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- sdkconfig.defaults | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 9018ebb..c877e51 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 9018ebb55dd9f60192233cc23b478425699893d5 +Subproject commit c877e512408f7cbd82ebd9f7ba684dda29a49487 diff --git a/sdkconfig.defaults b/sdkconfig.defaults index fdfe143..e944bdf 100755 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -9,6 +9,7 @@ CONFIG_PARTITION_TABLE_FILENAME="pico-keys-sdk/config/esp32/partitions.csv" CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y CONFIG_WL_SECTOR_SIZE_512=y CONFIG_WL_SECTOR_MODE_PERF=y +COMPILER_OPTIMIZATION="Performance" CONFIG_MBEDTLS_CMAC_C=y CONFIG_MBEDTLS_CHACHA20_C=y From 618fdb4c1aa983de308fbf4274c09fde2031b654 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sat, 9 Nov 2024 00:50:51 +0100 Subject: [PATCH 55/60] Do not request dev cert when performing tests. Signed-off-by: Pol Henarejos --- tests/scripts/func.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/scripts/func.sh b/tests/scripts/func.sh index 4a9b324..e081224 100755 --- a/tests/scripts/func.sh +++ b/tests/scripts/func.sh @@ -37,7 +37,7 @@ gen_and_delete() { test $? -eq 0 && echo -n "." || exit $? } reset() { - python3 tools/pico-hsm-tool.py --pin 648219 initialize --so-pin 57621880 --silent > /dev/null 2>&1 + python3 tools/pico-hsm-tool.py --pin 648219 initialize --so-pin 57621880 --silent --no-dev-cert > /dev/null 2>&1 test $? -eq 0 || exit $? } From bfa8891c8dfd058b10989c7483c5062d308c7df0 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sat, 9 Nov 2024 14:54:51 +0100 Subject: [PATCH 56/60] Update README. Signed-off-by: Pol Henarejos --- README.md | 62 ++++++++++++++++++++++++++++++++++++--------------- pico-keys-sdk | 2 +- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 83530d9..0a87357 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,21 @@ Uses an MKEK to securely store all keys, encrypted with an ephemeral key derived ### > Hierarchical Deterministic Key Generation Supports BIP32 for asymmetric key derivation and SLIP10 for symmetric key derivation, enabling crypto wallet deployment with infinite key generation. Supports NIST 256 and Koblitz 256 curves for master key generation.[^4] +### > One Time Programming (OTP) Storage +The OTP securely stores the MKEK (Master Key Encryption Key) and Device Key permanently, making it inaccessible from external interfaces. This ensures that the key is protected against unauthorized access and tampering. + +### > Secure Boot +Secure Boot ensures only authenticated firmware can run on the device, verifying each firmware’s digital signature to block unauthorized code. + +### > Secure Lock +Secure Lock restricts the device to the manufacturer’s firmware only, locking out debug access and preventing any further boot key installations. + +### > Rescue Interface + A built-in rescue interface allows recovery of the device if it becomes unresponsive or undetectable. This feature provides a way to restore the device to operational status without compromising security. + + ### > LED Customization + The LED can be customized to reflect device status and user preferences, offering flexible color and brightness options for an enhanced user experience. + [^1]: PKCS11 modules (`pkcs11-tool` and `sc-tool`) do not support CMAC and key derivation. It must be processed through raw APDU command (`opensc-tool -s`). [^2]: Available via SCS3 tool. See [SCS3](/doc/scs3.md "SCS3") for more information. [^3]: Imports are available only if the Pico HSM is previously initialized with a DKEK and DKEK shares are available during the import process. @@ -147,33 +162,45 @@ Supports BIP32 for asymmetric key derivation and SLIP10 for symmetric key deriva Pico HSM also supports ESP32-S3 boards, which add secure storage, flash encryption and secure boot. ### > Dynamic VID/PID -Supports setting VID & PID on-the-fly. Use `pico-hsm-tool.py` for specify VID/PID values and reboot the device. +Supports setting VID & PID on-the-fly. Use `pico-hsm-tool.py` or [Pico Commissioner](https://www.picokeys.com/pico-commissioner/ "Pico Commissioner") for specify VID/PID values and reboot the device. -### > Rescue Pico HSM Tool -Pico HSM Tool implements a new CCID stack to rescue the Pico HSM in case it has wrong VID/PID values and it is not recognized by the OS. +### > Rescue Pico HSM Tool and Commissioner +Pico HSM Tool implements a new CCID stack to rescue the Pico HSM in case it has wrong VID/PID values and it is not recognized by the OS. It can be accessed through `pico-hsm-tool.py` or [Pico Commissioner](https://www.picokeys.com/pico-commissioner/ "Pico Commissioner"). ## Security considerations -All secret keys (both asymmetric and symmetric) are encrypted and stored in the flash memory of the Raspberry Pico. The DKEK, a 256-bit AES key, is used to protect these private and secret keys. Keys are only held in RAM during signature and decryption operations, and are loaded and cleared each time to avoid potential security vulnerabilities. +All secret keys (both asymmetric and symmetric) are encrypted and stored in the flash memory. The MKEK, a 256-bit AES key, is used to protect these private and secret keys. Keys are held in RAM only during signature and decryption operations, and are loaded and cleared each time to avoid potential security vulnerabilities. -The DKEK itself is encrypted using a doubly salted and hashed PIN, and the PIN is hashed in memory during sessions. This ensures that the PIN is never stored in plain text, either in flash memory or in RAM. However, if no secure channel is used, the PIN is transmitted in plain text from the host to the HSM. +The MKEK itself is encrypted using a doubly salted and hashed PIN, and the PIN is hashed in memory during sessions. This ensures that the PIN is never stored in plaintext, neither in flash memory nor in RAM. However, if no secure channel is used, the PIN may be transmitted in plaintext from the host to the HSM. + +DKEKs are used during the export and import of private/secret keys and are part of a Key Domain. A Key Domain is a set of secret/private keys that share the same DKEK. These are also shared by the custodians and are not specific to Pico HSM. Therefore, if a key does not belong to a Key Domain (and thus lacks a DKEK), it cannot be exported. In the event that the Pico is stolen, the private and secret key contents cannot be accessed without the PIN, even if the flash memory is dumped. +### RP2350 and ESP32-S3 +RP2350 and ESP32-S3 microcontrollers are equipped with advanced security features, including Secure Boot and Secure Lock, ensuring that firmware integrity and authenticity are tightly controlled. Both devices support the storage of the Master Key Encryption Key (MKEK) in an OTP (One-Time Programmable) memory region, making it permanently inaccessible for external access or tampering. This secure, non-volatile region guarantees that critical security keys are embedded into the hardware, preventing unauthorized access and supporting robust defenses against code injection or firmware modification. Together, Secure Boot and Secure Lock enforce firmware authentication, while the MKEK in OTP memory solidifies the foundation for secure operations. + +### Secure Boot +Secure Boot is a security feature that ensures that only trusted firmware, verified through digital signatures, can be loaded onto the device during the boot process. Once enabled, Secure Boot checks every piece of firmware against a cryptographic signature before execution, rejecting any unauthorized or modified code. This prevents malicious firmware from compromising the device’s operation and integrity. With Secure Boot activated, only firmware versions signed by a trusted authority, such as the device manufacturer, will be accepted, ensuring the device remains protected from unauthorized software modifications. **This is irreversible. Once enabled, it CANNOT be disabled.** + +**IMPORTANT:** For users wishing to develop and compile custom firmware, a private-public key pair is essential. Activating Secure Boot requires users to generate and manage their own unique private-public key pair. The public key from this pair must be embedded into the device to validate all firmware. Firmware will not boot without a proper digital signature from this key pair. This means that users must sign all future firmware versions with their private key and embed the public key in the device to ensure compatibility. + +### Secure Lock +Secure Lock builds on Secure Boot by imposing an even stricter security model. Once activated, Secure Lock prevents any further installation of new boot keys, effectively locking the device to only run firmware that is authorized by the device's primary vendor—in this case, Pico Keys. In addition to preventing additional keys, Secure Lock disables debugging interfaces and puts additional safeguards in place to resist tampering and intrusion attempts. This ensures that the device operates exclusively with the original vendor’s firmware and resists unauthorized access, making it highly secure against external threats. **This is irreversible. Once enabled, it CANNOT be disabled.** + +**IMPORTANT:** Activating Secure Lock not only enables Secure Boot but also invalidates all keys except the official Pico Key. This means that only firmware signed by Pico Key will be recognized, and custom code will no longer be allowed. Once enabled, the Pico Key device will run solely on the official firmware available on the website, with no option for generating or compiling new code for the device. + ## Download -**If you own an ESP32-S3 board, go to [ESP32 support](https://www.picokeys.com/esp32-support/) for further information.** +**If you own an ESP32-S3 board, go to [ESP32 Flasher](https://www.picokeys.com/esp32-flasher/) for flashing your Pico HSM.** -Please, go to the Release page and download the UF2 file for your board. +If you own a Raspberry Pico (RP2040 or RP2350), go to [Download page](https://www.picokeys.com/getting-started/), select your vendor and model and download the proper firmware; or go to [Release page](https://www.github.com/polhenarejos/pico-hsm/releases/) and download the UF2 file for your board. -Note that UF2 files are shiped with a dummy VID/PID to avoid license issues (FEFF:FCFD). If you plan to use it with OpenSC or similar tools, you should modify Info.plist of CCID driver to add these VID/PID or use the [Pico Patcher tool](https://www.picokeys.com/pico-patcher/). - -Alternatively you can use the legacy VID/PID patcher as follows: -`./patch_vidpid.sh VID:PID input_hsm_file.uf2 output_hsm_file.uf2` +Note that UF2 files are shiped with a dummy VID/PID to avoid license issues (FEFF:FCFD). If you plan to use it with OpenSC or similar tools, you should modify Info.plist of CCID driver to add these VID/PID or use the [Pico Commissioner](https://www.picokeys.com/pico-commissioner/ "Pico Commissioner"). You can use whatever VID/PID (i.e., 234b:0000 from FISJ), but remember that you are not authorized to distribute the binary with a VID/PID that you do not own. -Note that the pure-browser option [Pico Patcher tool](https://www.picokeys.com/pico-patcher/) is the most recommended. +Note that the pure-browser option [Pico Commissioner](https://www.picokeys.com/pico-commissioner/ "Pico Commissioner") is the most recommended. -## Build +## Build for Raspberry Pico Before building, ensure you have installed the toolchain for the Pico and the Pico SDK is properly located in your drive. ``` @@ -212,9 +239,9 @@ Independent from your Linux distribution or when using another OS that supports ``` sudo docker build \ - --build-arg VERSION_PICO_SDK=1.5.0 \ - --build-arg VERSION_MAJOR=3 \ - --build-arg VERSION_MINOR=4 \ + --build-arg VERSION_PICO_SDK=2.0.0 \ + --build-arg VERSION_MAJOR=5 \ + --build-arg VERSION_MINOR=0 \ --build-arg PICO_BOARD=waveshare_rp2040_zero \ --build-arg USB_VID=0xfeff \ --build-arg USB_PID=0xfcfd \ @@ -321,8 +348,7 @@ For advanced usage scenarios, refer to the documentation and examples provided. ### Important OpenSC relies on PCSC driver, which reads a list (`Info.plist`) that contains a pair of VID/PID of supported readers. In order to be detectable, you have several options: - Use `pico-hsm-tool.py` to modify VID/PID on-the-fly. -- Use the online [Pico Patcher tool](https://www.picokeys.com/pico-patcher/). -- Patch the UF2 binary (if you just downloaded from the [Release section](https://github.com/polhenarejos/pico-hsm/releases "Release section")) +- Use the pure-browser online [Pico Commissioner](https://www.picokeys.com/pico-commissioner/ "Pico Commissioner") that commissions the Pico Key on-the-fly without external tools. - Build and configure the project with the proper VID/PID with `USB_VID` and `USB_PID` parameters in `CMake` (see [Build section](#build "Build section")). Note that you cannot distribute the patched/compiled binary if you do not own the VID/PID or have an explicit authorization. ## Credits diff --git a/pico-keys-sdk b/pico-keys-sdk index c877e51..5bce3e4 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit c877e512408f7cbd82ebd9f7ba684dda29a49487 +Subproject commit 5bce3e4c838f424c8d17728814284352f1b53bff From e4736cc4484c70ce39c6b56ecdcd629d42029608 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sat, 9 Nov 2024 14:58:06 +0100 Subject: [PATCH 57/60] Upgrade Pico Keys SDK to v7.0 Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 5bce3e4..5a52afe 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 5bce3e4c838f424c8d17728814284352f1b53bff +Subproject commit 5a52afe8267fc32293999955577b9158bc20bb34 From f3bf3cd8f49503a02c8ce19c9125277c3a58f096 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sat, 9 Nov 2024 14:58:42 +0100 Subject: [PATCH 58/60] Do not use pico patcher script anymore. Signed-off-by: Pol Henarejos --- tools/pico-hsm-patch-vidpid.sh | 94 ---------------------------------- 1 file changed, 94 deletions(-) delete mode 100755 tools/pico-hsm-patch-vidpid.sh diff --git a/tools/pico-hsm-patch-vidpid.sh b/tools/pico-hsm-patch-vidpid.sh deleted file mode 100755 index 3ca4414..0000000 --- a/tools/pico-hsm-patch-vidpid.sh +++ /dev/null @@ -1,94 +0,0 @@ -#!/bin/bash - -# -# This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm). -# 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 . -# - -VERSION_MAJOR="6" #Version of Pico Keys SDK -VERSION_MINOR="0" - -echo "----------------------------" -echo "VID/PID patcher for Pico HSM" -echo "----------------------------" -echo "" - -if [ "$#" -le 0 ]; then - echo "Usage: $0 VID:PID [input_uf2_file] [output_uf2_file]" - exit 1 -fi - -IFS=':' read -r -a ARR <<< "$1" - -if [ ${#ARR[@]} -ne 2 ]; then - echo "ERROR: Specify vendor and product ids as VID:PID (e.g., $0 CAFE:1234)" - exit 1 -fi - -VID=${ARR[0]} -PID=${ARR[1]} - -if [ ${#VID} -ne 4 ]; then - echo "ERROR: VID length must be 4 hexadecimal characters" - exit 1 -fi - -if [ ${#PID} -ne 4 ]; then - echo "ERROR: PID length must be 4 hexadecimal characters" - exit 1 -fi - -if ! [[ $VID =~ ^[0-9A-Fa-f]{1,}$ ]] ; then - echo "ERROR: VID must contain hexadecimal characters" - exit 1 -fi - -if ! [[ $PID =~ ^[0-9A-Fa-f]{1,}$ ]] ; then - echo "ERROR: PID must contain hexadecimal characters" - exit 1 -fi - -UF2_FILE_IF="hsm2040.uf2" -UF2_FILE_OF="$UF2_FILE_IF" - -if [ "$#" -ge 2 ]; then - UF2_FILE_IF="$2" - UF2_FILE_OF="$UF2_FILE_IF" -fi - -if [ "$#" -ge 3 ]; then - UF2_FILE_OF="$3" -fi - - -echo -n "Patching ${UF2_FILE_IF}... " - -if [[ ! -f "$UF2_FILE_IF" ]]; then - echo "ERROR: UF2 file ${UF2_FILE_IF} does not exist" - exit 1 -fi - -if [ "$UF2_FILE_IF" != "$UF2_FILE_OF" ]; then - cp -R $UF2_FILE_IF $UF2_FILE_OF -fi - -LITTLE_VID="\x${VID:2:2}\x${VID:0:2}" -LITTLE_PID="\x${PID:2:2}\x${PID:0:2}" - -perl -pi -e "s/\xff\xfe\xfd\xfc\x$VERSION_MINOR\x$VERSION_MAJOR\x01\x02\x03\x01/$LITTLE_VID$LITTLE_PID\x$VERSION_MINOR\x$VERSION_MAJOR\x01\x02\x03\x01/" $UF2_FILE_OF - -echo "Done!" -echo "" -echo "Patched file was saved in ${UF2_FILE_OF}" From 2f3d70fcac4637c2e811093d889ab338e7560607 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sat, 9 Nov 2024 15:16:32 +0100 Subject: [PATCH 59/60] Upgrade mbedtls v3.6.2 Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 5a52afe..8c25e9b 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 5a52afe8267fc32293999955577b9158bc20bb34 +Subproject commit 8c25e9be87f5556738550d309358198163111420 From b7c6ca58d0df1b04401a5d8b9dbb9b471c4228b0 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sat, 9 Nov 2024 15:47:40 +0100 Subject: [PATCH 60/60] Upgrade to v5.0 Signed-off-by: Pol Henarejos --- build_pico_hsm.sh | 4 ++-- src/hsm/version.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build_pico_hsm.sh b/build_pico_hsm.sh index 66af564..86b5d1e 100755 --- a/build_pico_hsm.sh +++ b/build_pico_hsm.sh @@ -1,7 +1,7 @@ #!/bin/bash -VERSION_MAJOR="4" -VERSION_MINOR="2" +VERSION_MAJOR="5" +VERSION_MINOR="0" SUFFIX="${VERSION_MAJOR}.${VERSION_MINOR}" #if ! [[ -z "${GITHUB_SHA}" ]]; then # SUFFIX="${SUFFIX}.${GITHUB_SHA}" diff --git a/src/hsm/version.h b/src/hsm/version.h index 9727568..82ef6a5 100644 --- a/src/hsm/version.h +++ b/src/hsm/version.h @@ -18,7 +18,7 @@ #ifndef __VERSION_H_ #define __VERSION_H_ -#define HSM_VERSION 0x0402 +#define HSM_VERSION 0x0500 #define HSM_VERSION_MAJOR ((HSM_VERSION >> 8) & 0xff) #define HSM_VERSION_MINOR (HSM_VERSION & 0xff)