diff --git a/CMakeLists.txt b/CMakeLists.txt index bc6f3ce..d3f966b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,11 +14,37 @@ add_executable(hsm2040) target_sources(hsm2040 PUBLIC ${CMAKE_CURRENT_LIST_DIR}/hsm2040.c ${CMAKE_CURRENT_LIST_DIR}/usb_descriptors.c -# ${CMAKE_CURRENT_LIST_DIR}/openpgp-do.c + ${CMAKE_CURRENT_LIST_DIR}/openpgp.c + ${CMAKE_CURRENT_LIST_DIR}/debug.c + ${CMAKE_CURRENT_LIST_DIR}/openpgp-do.c + ${CMAKE_CURRENT_LIST_DIR}/ac.c + ${CMAKE_CURRENT_LIST_DIR}/flash.c + ${CMAKE_CURRENT_LIST_DIR}/low_flash.c + ${CMAKE_CURRENT_LIST_DIR}/call-rsa.c + ${CMAKE_CURRENT_LIST_DIR}/call-ec_p256k1.c + ${CMAKE_CURRENT_LIST_DIR}/ecc-ed25519.c + ${CMAKE_CURRENT_LIST_DIR}/ecc-ed448.c + ${CMAKE_CURRENT_LIST_DIR}/random.c + ${CMAKE_CURRENT_LIST_DIR}/ecc-mont.c + ${CMAKE_CURRENT_LIST_DIR}/ecc-x448.c + ${CMAKE_CURRENT_LIST_DIR}/sha256.c + ${CMAKE_CURRENT_LIST_DIR}/aes.c + ${CMAKE_CURRENT_LIST_DIR}/sha512.c + ${CMAKE_CURRENT_LIST_DIR}/shake256.c + ${CMAKE_CURRENT_LIST_DIR}/rsa.c + ${CMAKE_CURRENT_LIST_DIR}/bignum.c + #${CMAKE_CURRENT_LIST_DIR}/neug.c + ${CMAKE_CURRENT_LIST_DIR}/ec_p256k1.c + ${CMAKE_CURRENT_LIST_DIR}/bn.c + ${CMAKE_CURRENT_LIST_DIR}/mod.c + ${CMAKE_CURRENT_LIST_DIR}/jpc_p256k1.c + ${CMAKE_CURRENT_LIST_DIR}/modp256k1.c + ${CMAKE_CURRENT_LIST_DIR}/p448.c + ${CMAKE_CURRENT_LIST_DIR}/mod25638.c ) target_include_directories(hsm2040 PUBLIC ${CMAKE_CURRENT_LIST_DIR}) pico_add_extra_outputs(hsm2040) -target_link_libraries(hsm2040 PRIVATE pico_stdlib tinyusb_device tinyusb_board pico_multicore) \ No newline at end of file +target_link_libraries(hsm2040 PRIVATE pico_stdlib tinyusb_device tinyusb_board pico_multicore hardware_flash hardware_sync) \ No newline at end of file diff --git a/ccid.h b/ccid.h index 6a8e241..fa6781b 100644 --- a/ccid.h +++ b/ccid.h @@ -3,65 +3,6 @@ #include "ccid-types.h" -struct apdu { - uint8_t seq; - - /* command APDU */ - uint8_t *cmd_apdu_head; /* CLS INS P1 P2 [ internal Lc ] */ - uint8_t *cmd_apdu_data; - uint16_t cmd_apdu_data_len; /* Nc, calculated by Lc field */ - uint16_t expected_res_size; /* Ne, calculated by Le field */ - - /* response APDU */ - uint16_t sw; - uint16_t res_apdu_data_len; - uint8_t *res_apdu_data; -}; - -extern struct apdu apdu; - -/* CCID thread */ -#define EV_CARD_CHANGE 1 -#define EV_TX_FINISHED 2 /* CCID Tx finished */ -#define EV_EXEC_ACK_REQUIRED 4 /* OpenPGPcard Execution ACK required */ -#define EV_EXEC_FINISHED 8 /* OpenPGPcard Execution finished */ -#define EV_RX_DATA_READY 16 /* USB Rx data available */ - -/* OpenPGPcard thread */ -#define EV_MODIFY_CMD_AVAILABLE 1 -#define EV_VERIFY_CMD_AVAILABLE 2 -#define EV_CMD_AVAILABLE 4 -#define EV_EXIT 8 -#define EV_PINPAD_INPUT_DONE 16 - - -enum ccid_state { - CCID_STATE_NOCARD, /* No card available */ - CCID_STATE_START, /* Initial */ - CCID_STATE_WAIT, /* Waiting APDU */ - - CCID_STATE_EXECUTE, /* Executing command */ - CCID_STATE_ACK_REQUIRED_0, /* Ack required (executing)*/ - CCID_STATE_ACK_REQUIRED_1, /* Waiting user's ACK (execution finished) */ - - CCID_STATE_EXITED, /* CCID Thread Terminated */ - CCID_STATE_EXEC_REQUESTED, /* Exec requested */ -}; - -#define APDU_STATE_WAIT_COMMAND 0 -#define APDU_STATE_COMMAND_CHAINING 1 -#define APDU_STATE_COMMAND_RECEIVED 2 -#define APDU_STATE_RESULT 3 -#define APDU_STATE_RESULT_GET_RESPONSE 4 - -/* Maximum cmd apdu data is key import 24+4+256+256 (proc_key_import) */ -#define MAX_CMD_APDU_DATA_SIZE (24+4+256+256) /* without header */ -/* Maximum res apdu data is public key 5+9+512 (gpg_do_public_key) */ -#define MAX_RES_APDU_DATA_SIZE (5+9+512) /* without trailer */ - -#define CCID_MSG_HEADER_SIZE 10 - - static const class_desc_ccid_t desc_ccid = { .bLength = sizeof (class_desc_ccid_t), .bDescriptorType = 0x21, diff --git a/hsm2040.c b/hsm2040.c index 449c004..b7e528f 100644 --- a/hsm2040.c +++ b/hsm2040.c @@ -19,7 +19,8 @@ #include "device/usbd_pvt.h" #include "pico/util/queue.h" #include "pico/multicore.h" - +#include "gnuk.h" +#include "config.h" // Device descriptors #include "hsm2040.h" @@ -34,6 +35,7 @@ static uint8_t itf_num; struct apdu apdu; static struct ccid ccid; +extern void openpgp_card_thread(); static uint8_t ccid_buffer[USB_BUF_SIZE]; @@ -385,14 +387,16 @@ static enum ccid_state ccid_power_on (struct ccid *c) { TU_LOG2("!!! CCID POWER ON\r\n"); uint8_t p[CCID_MSG_HEADER_SIZE+1]; /* >= size of historical_bytes -1 */ - int hist_len = 0;// historical_bytes[0]; + int hist_len = historical_bytes[0]; size_t size_atr = sizeof (ATR_head) + hist_len + 1; uint8_t xor_check = 0; int i; if (c->application == 0) { - //multicore_launch_core1(openpgp_card_thread); + multicore_fifo_push_blocking((uint32_t)&c->ccid_comm); + multicore_fifo_push_blocking((uint32_t)&c->openpgp_comm); + multicore_launch_core1(openpgp_card_thread); c->application = 1; } p[0] = CCID_DATA_BLOCK_RET; @@ -411,7 +415,7 @@ static enum ccid_state ccid_power_on (struct ccid *c) for (i = 1; i < (int)sizeof (ATR_head); i++) xor_check ^= ATR_head[i]; - //memcpy (p, historical_bytes + 1, hist_len); + memcpy (p, historical_bytes + 1, hist_len); #ifdef LIFE_CYCLE_MANAGEMENT_SUPPORT if (file_selection == 255) p[7] = 0x03; diff --git a/hsm2040.h b/hsm2040.h index 4b32b5b..026aa11 100644 --- a/hsm2040.h +++ b/hsm2040.h @@ -11,33 +11,8 @@ #define USB_REQ_CCID 0xA1 -#define USB_LL_BUF_SIZE 64 - extern const uint8_t historical_bytes[]; -#define DEBUG_INFO(s) TU_LOG2(s) - -static void put_hex (uint8_t nibble) -{ - uint8_t c; - - if (nibble < 0x0a) - c = '0' + nibble; - else - c = 'a' + nibble - 0x0a; - - TU_LOG3("%c",c); -} - -void put_byte (uint8_t b) -{ - put_hex (b >> 4); - put_hex (b &0x0f); - TU_LOG3("\r\n"); -} - -#define DEBUG_BYTE(b) put_byte(b) - #define DEBUG_PAYLOAD(p,s) { \ TU_LOG3("Payload %s (%d bytes):\r\n", #p,s);\ for (int i = 0; i < s; i += 16) {\