From 2905dcc8c0f08ee1960b08de81b29114d4c89aeb Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 3 Apr 2022 19:57:56 +0200 Subject: [PATCH] Adding custom command to set datetime. Signed-off-by: Pol Henarejos --- CMakeLists.txt | 2 +- src/hsm/hsm2040.c | 4 ++++ src/hsm/sc_hsm.c | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e8d413..9910399 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,7 +119,7 @@ pico_add_extra_outputs(pico_hsm) #target_compile_definitions(pico_hsm PRIVATE MBEDTLS_ECDSA_DETERMINISTIC=1) -target_link_libraries(pico_hsm PRIVATE pico_stdlib tinyusb_device tinyusb_board pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id) +target_link_libraries(pico_hsm PRIVATE pico_stdlib tinyusb_device tinyusb_board pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id hardware_rtc) # #project(flash_nuke C CXX ASM) diff --git a/src/hsm/hsm2040.c b/src/hsm/hsm2040.c index 72b0a46..228b1d5 100644 --- a/src/hsm/hsm2040.c +++ b/src/hsm/hsm2040.c @@ -33,6 +33,7 @@ #include "pico/multicore.h" #include "random.h" #include "hsm2040.h" +#include "hardware/rtc.h" extern void do_flash(); extern void low_flash_init(); @@ -1628,6 +1629,7 @@ int main(void) gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); #endif #endif + led_off_all(); @@ -1638,6 +1640,8 @@ int main(void) random_init(); low_flash_init(); + + rtc_init(); while (1) { diff --git a/src/hsm/sc_hsm.c b/src/hsm/sc_hsm.c index 95f9359..3ff01d9 100644 --- a/src/hsm/sc_hsm.c +++ b/src/hsm/sc_hsm.c @@ -32,6 +32,7 @@ #include "cvcerts.h" #include "crypto_utils.h" #include "dkek.h" +#include "hardware/rtc.h" const uint8_t sc_hsm_aid[] = { 11, @@ -1676,6 +1677,24 @@ static int cmd_derive_asym() { return SW_OK(); } +static int cmd_datetime() { + if (P1(apdu) != 0x0 || P2(apdu) != 0x0) + return SW_INCORRECT_P1P2(); + if (apdu.cmd_apdu_data_len != 8) + return SW_WRONG_LENGTH(); + datetime_t dt; + dt.year = (apdu.cmd_apdu_data[0] << 8) | (apdu.cmd_apdu_data[1]); + dt.month = apdu.cmd_apdu_data[2]; + dt.day = apdu.cmd_apdu_data[3]; + dt.dotw = apdu.cmd_apdu_data[4]; + dt.hour = apdu.cmd_apdu_data[5]; + dt.min = apdu.cmd_apdu_data[6]; + dt.sec = apdu.cmd_apdu_data[7]; + if (!rtc_set_datetime(&dt)) + return SW_WRONG_DATA(); + return SW_OK(); +} + typedef struct cmd { uint8_t ins; @@ -1697,6 +1716,7 @@ typedef struct cmd #define INS_DERIVE_ASYM 0x76 #define INS_CIPHER_SYM 0x78 #define INS_CHALLENGE 0x84 +#define INS_DATETIME 0x88 #define INS_SELECT_FILE 0xA4 #define INS_READ_BINARY 0xB0 #define INS_READ_BINARY_ODD 0xB1 @@ -1724,6 +1744,7 @@ static const cmd_t cmds[] = { { INS_DECRYPT_ASYM, cmd_decrypt_asym }, { INS_CIPHER_SYM, cmd_cipher_sym }, { INS_DERIVE_ASYM, cmd_derive_asym }, + { INS_DATETIME, cmd_datetime }, { 0x00, 0x0} };