From 96556473bcb630f4753cced7acf3d372c84d07e0 Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Tue, 7 Feb 2023 23:37:06 +0530 Subject: [PATCH 01/12] implementing authenticated communication --- common/libraries/util/atecc_utils.c | 140 +++++++++++ common/libraries/util/atecc_utils.h | 106 +++++++++ common/libraries/util/session_utils.c | 37 +++ common/libraries/util/session_utils.h | 28 +++ .../core/controller/controller_level_four.h | 31 +-- .../device_authentication_controller.c | 217 +++--------------- 6 files changed, 345 insertions(+), 214 deletions(-) create mode 100644 common/libraries/util/atecc_utils.c create mode 100644 common/libraries/util/atecc_utils.h create mode 100644 common/libraries/util/session_utils.c create mode 100644 common/libraries/util/session_utils.h diff --git a/common/libraries/util/atecc_utils.c b/common/libraries/util/atecc_utils.c new file mode 100644 index 000000000..25a13c8a3 --- /dev/null +++ b/common/libraries/util/atecc_utils.c @@ -0,0 +1,140 @@ +#include "atecc_utils.h" +#include "sha2.h" + +static ATCA_STATUS helper_config_to_sign_internal(ATCADeviceType device_type, + struct atca_sign_internal_in_out *param, + const uint8_t *config) { + const uint8_t *value = NULL; + uint16_t slot_locked = 0; + + if (param == NULL || config == NULL || param->temp_key == NULL) { + return ATCA_BAD_PARAM; + } + + // SlotConfig[TempKeyFlags.keyId] + value = &config[20 + param->temp_key->key_id * 2]; + param->slot_config = (uint16_t) value[0] | ((uint16_t) value[1] << 8); + + // KeyConfig[TempKeyFlags.keyId] + value = &config[96 + param->temp_key->key_id * 2]; + param->key_config = (uint16_t) value[0] | ((uint16_t) value[1] << 8); + + if (device_type == ATECC108A && param->temp_key->key_id < 8) { + value = &config[52 + param->temp_key->key_id * 2]; + param->use_flag = value[0]; + param->update_count = value[0]; + } else { + param->use_flag = 0x00; + param->update_count = 0x00; + } + + //SlotLocked:TempKeyFlags.keyId + slot_locked = (uint16_t) config[88] | ((uint16_t) config[89] << 8); + param->is_slot_locked = + (slot_locked & (1 << param->temp_key->key_id)) ? false : true; + + return ATCA_SUCCESS; +} + +void helper_get_gendig_hash(atecc_slot_define_t slot, + uint8_t *data, + uint8_t *digest, + uint8_t *postfix, + atecc_data_t atecc_data) { + if (digest == NULL || data == NULL || postfix == NULL) { + return; + } + + uint8_t tempkey_init[96] = {0}; + uint8_t atecc_serial[9]; + atecc_data.status = atcab_read_serial_number(atecc_serial); + memcpy(tempkey_init, data, 32); + postfix[0] = tempkey_init[32] = 0x15; + postfix[1] = tempkey_init[33] = 0x02; + postfix[2] = tempkey_init[34] = slot; + postfix[3] = tempkey_init[35] = 0x00; + postfix[4] = tempkey_init[36] = atecc_serial[8]; + postfix[5] = tempkey_init[37] = atecc_serial[0]; + postfix[6] = tempkey_init[38] = atecc_serial[1]; + + sha256_Raw(tempkey_init, 96, digest); +} + +ATCA_STATUS helper_sign_internal_msg(struct atca_sign_internal_in_out *param, + uint8_t mode, + uint8_t priv_key_id, + uint8_t data_key_id, + atecc_data_t atecc_data) { + uint8_t msg[55]; + uint8_t cfg[128] = {0}, sn[9] = {0}; + atca_temp_key_t temp_key = {0}; + param->key_id = priv_key_id; + temp_key.key_id = data_key_id; + temp_key.gen_dig_data = 1; + temp_key.valid = 1; + temp_key.source_flag = 1; + + atecc_data.status = atcab_read_config_zone(cfg); + memcpy(temp_key.value, param->message, 32); + param->temp_key = &temp_key; + helper_config_to_sign_internal(ATECC608A, param, cfg); + atecc_data.status = atcab_read_serial_number(sn); + + if (param == NULL || param->temp_key == NULL) { + return ATCA_BAD_PARAM; + } + + memset(msg, 0, sizeof(msg)); + memcpy(&msg[0], param->temp_key->value, 32); + msg[32] = ATCA_SIGN; // Sign OpCode + msg[33] = param->mode; // Sign Mode + msg[34] = (uint8_t) (param->key_id >> 0); // Sign KeyID + msg[35] = (uint8_t) (param->key_id >> 8); + msg[36] = + (uint8_t) (param->slot_config >> 0); // SlotConfig[TempKeyFlags.keyId] + msg[37] = (uint8_t) (param->slot_config >> 8); + msg[38] = + (uint8_t) (param->key_config >> 0); // KeyConfig[TempKeyFlags.keyId] + msg[39] = (uint8_t) (param->key_config >> 8); + + //TempKeyFlags (b0-3: keyId, b4: sourceFlag, b5: GenDigData, b6: GenKeyData, b7: NoMacFlag) + msg[40] |= ((param->temp_key->key_id & 0x0F) << 0); + msg[40] |= ((param->temp_key->source_flag & 0x01) << 4); + msg[40] |= ((param->temp_key->gen_dig_data & 0x01) << 5); + msg[40] |= ((param->temp_key->gen_key_data & 0x01) << 6); + msg[40] |= ((param->temp_key->no_mac_flag & 0x01) << 7); + msg[41] = 0x00; + msg[42] = 0x00; + + // Serial Number + msg[43] = sn[8]; + memcpy(&msg[48], &sn[0], 2); + if (param->mode & SIGN_MODE_INCLUDE_SN) { + memcpy(&msg[44], &sn[4], 4); + memcpy(&msg[50], &sn[2], 2); + } + + // The bit within the SlotLocked field corresponding to the last key used in the TempKey computation is in the LSB + msg[52] = param->is_slot_locked ? 0x00 : 0x01; + + // If the slot contains a public key corresponding to a supported curve, and if PubInfo indicates this key must be + // validated before being used by Verify, and if the validity bits have a value of 0x05, then the PubKey Valid byte + // will be 0x01.In all other cases, it will be 0. + msg[53] = param->for_invalidate ? 0x01 : 0x00; + + msg[54] = 0x00; + + if (param->message) { + memcpy(param->message, msg, sizeof(msg)); + } + if (param->verify_other_data) { + memcpy(¶m->verify_other_data[0], &msg[33], 10); + memcpy(¶m->verify_other_data[10], &msg[44], 4); + memcpy(¶m->verify_other_data[14], &msg[50], 5); + } + if (param->digest) { + return atcac_sw_sha2_256(msg, sizeof(msg), param->digest); + } else { + return ATCA_SUCCESS; + } +} \ No newline at end of file diff --git a/common/libraries/util/atecc_utils.h b/common/libraries/util/atecc_utils.h new file mode 100644 index 000000000..007b6607c --- /dev/null +++ b/common/libraries/util/atecc_utils.h @@ -0,0 +1,106 @@ +/** + * @file atecc_utils.h + * @author Cypherock X1 Team + * @brief Util functions related to atecc signing + * @version 0.1 + * @date 2022-12-16 + * + * @copyright Copyright (c) 2022 HODL TECH PTE LTD + *
You may obtain a copy of license at https://mitcc.org/ + * + */ + +#ifndef ATECC_UTILS_H +#define ATECC_UTILS_H + +#pragma once + +#include "atca_host.h" +#include "stm32l4xx_it.h" + +#define SIGNATURE_SIZE 64 +#define POSTFIX1_SIZE 7 +#define POSTFIX2_SIZE 23 +#define DEVICE_SERIAL_SIZE 32 +#define AUTH_DATA_SERIAL_SIGN_MSG_SIZE (POSTFIX1_SIZE + POSTFIX2_SIZE + SIGNATURE_SIZE + DEVICE_SERIAL_SIZE) +#define AUTH_DATA_CHALLENGE_SIGN_MSG_SIZE (POSTFIX1_SIZE + POSTFIX2_SIZE + SIGNATURE_SIZE) + +/** + * @brief + * @details + * + * @see + * @since v1.0.0 + * + * @note + */ +typedef struct auth_data_struct { + uint8_t postfix1[POSTFIX1_SIZE], + postfix2[POSTFIX2_SIZE], + signature[SIGNATURE_SIZE], + serial[DEVICE_SERIAL_SIZE]; +} auth_data_t; + +typedef enum { + slot_0_unused = 0U, + slot_1_unused = 1U, + slot_2_auth_key = 2U, + slot_3_nfc_pair_key = 3U, + slot_4_unused = 4U, + slot_5_challenge = 5U, + slot_6_io_key = 6U, + slot_7_unused = 7U, + slot_8_serial = 8U, + slot_9_unused = 9U, + slot_10_unused = 10U, + slot_11_unused = 11U, + slot_12_unused = 12U, + slot_13_unused = 13U, + slot_14_unused = 14U, + slot_15_unused = 15U +} atecc_slot_define_t; + +typedef struct { + uint8_t device_serial[DEVICE_SERIAL_SIZE], retries; + ATCA_STATUS status; + ATCAIfaceCfg *cfg_atecc608a_iface; +} atecc_data_t; + +/** + * @brief + * @details + * + * @see swap_transaction_controller(), device_authentication_controller() + * + * @param slot + * @param data + * @param digest + * @param postfix + * @param atecc_data + */ +void helper_get_gendig_hash(atecc_slot_define_t slot, + uint8_t *data, + uint8_t *digest, + uint8_t *postfix, + atecc_data_t atecc_data); + +/** + * @brief + * @details + * + * @see swap_transaction_controller(), device_authentication_controller() + * + * @param param + * @param mode + * @param priv_key_id + * @param data_key_id + * @param atecc_data + * @return + */ +ATCA_STATUS helper_sign_internal_msg(struct atca_sign_internal_in_out *param, + uint8_t mode, + uint8_t priv_key_id, + uint8_t data_key_id, + atecc_data_t atecc_data); + +#endif //ATECC_UTILS_H \ No newline at end of file diff --git a/common/libraries/util/session_utils.c b/common/libraries/util/session_utils.c new file mode 100644 index 000000000..1a5f448fb --- /dev/null +++ b/common/libraries/util/session_utils.c @@ -0,0 +1,37 @@ +#include "session_utils.h" + +bool verify_session_digest(uint8_t *payload, uint16_t payload_length, + uint8_t *buffer) { + + uint8_t hash[32] = {0}; + sha256_Raw(payload, payload_length, hash); + uint8_t session_key_derv_data[12] = {0}; + HDNode session_node; + uint32_t index; + char xpub[112] = {'\0'}; + + base58_encode_check(get_card_root_xpub(), FS_KEYSTORE_XPUB_LEN, + nist256p1_info.hasher_base58, xpub, 112); + hdnode_deserialize_public((char *) xpub, + 0x0488b21e, + NIST256P1_NAME, + &session_node, + NULL); + + index = read_be(session_key_derv_data); + hdnode_public_ckd(&session_node, index); + + index = read_be(session_key_derv_data + 4); + hdnode_public_ckd(&session_node, index); + + index = read_be(session_key_derv_data + 8); + hdnode_public_ckd(&session_node, index); + + uint8_t + status = ecdsa_verify_digest(&nist256p1, + session_node.public_key, + buffer, + hash); + + return status; +} \ No newline at end of file diff --git a/common/libraries/util/session_utils.h b/common/libraries/util/session_utils.h new file mode 100644 index 000000000..00f5aaa71 --- /dev/null +++ b/common/libraries/util/session_utils.h @@ -0,0 +1,28 @@ +/** + * @file session_utils.h + * @author Cypherock X1 Team + * @brief Functions for session management + * @version 0.1 + * @date 2023-02-07 + * + * @copyright Copyright (c) 2022 HODL TECH PTE LTD + *
You may obtain a copy of license at https://mitcc.org/ + * + */ + +#ifndef SESSION_UTILS +#define SESSION_UTILS + +#pragma once + +#include +#include +#include "bip32.h" +#include "base58.h" +#include "nist256p1.h" +#include "nfc.h" + +bool verify_session_digest(uint8_t *payload, uint16_t payload_length, + uint8_t *buffer); + +#endif //SESSION_UTILS diff --git a/src/level_four/core/controller/controller_level_four.h b/src/level_four/core/controller/controller_level_four.h index 91cf277b0..8b2eadc3b 100644 --- a/src/level_four/core/controller/controller_level_four.h +++ b/src/level_four/core/controller/controller_level_four.h @@ -21,37 +21,13 @@ #include "near.h" #include "solana.h" #include "tasks.h" +#include "atecc_utils.h" +#include "session_utils.h" #define DEFAULT_ATECC_RETRIES 5 -#define DEVICE_SERIAL_SIZE 32 #define MAXIMUM_COIN_SUPPORTED (14 + 5) // 5 for segwit support -typedef enum { - slot_0_unused = 0U, - slot_1_unused = 1U, - slot_2_auth_key = 2U, - slot_3_nfc_pair_key = 3U, - slot_4_unused = 4U, - slot_5_challenge = 5U, - slot_6_io_key = 6U, - slot_7_unused = 7U, - slot_8_serial = 8U, - slot_9_unused = 9U, - slot_10_unused = 10U, - slot_11_unused = 11U, - slot_12_unused = 12U, - slot_13_unused = 13U, - slot_14_unused = 14U, - slot_15_unused = 15U -} atecc_slot_define_t; - -typedef struct { - uint8_t device_serial[DEVICE_SERIAL_SIZE], retries; - ATCA_STATUS status; - ATCAIfaceCfg *cfg_atecc608a_iface; -} atecc_data_t; - extern atecc_data_t atecc_data; typedef enum { @@ -330,6 +306,8 @@ void send_transaction_controller_b_solana(); */ void receive_transaction_controller(); +void swap_transaction_controller(); + /** * @brief Next button controller is executed for generating address using xpub. * @details This controller handles is used to generate receiving address for @@ -352,6 +330,7 @@ void receive_transaction_controller_near(); */ void receive_transaction_controller_b(); +void swap_transaction_controller_b(); /** * @brief Next button controller is executed for generating address using xpub of Ethereum. * @details This controller handles is used to generate receiving address for the ETH coins requested by desktop from a diff --git a/src/level_four/core/controller/device_authentication_controller.c b/src/level_four/core/controller/device_authentication_controller.c index ff3b3c59b..dd50343d4 100644 --- a/src/level_four/core/controller/device_authentication_controller.c +++ b/src/level_four/core/controller/device_authentication_controller.c @@ -71,28 +71,7 @@ #include "board.h" #include "stdint.h" #include "stm32l4xx_it.h" - -#define SIGNATURE_SIZE 64 -#define POSTFIX1_SIZE 7 -#define POSTFIX2_SIZE 23 -#define AUTH_DATA_SERIAL_SIGN_MSG_SIZE (POSTFIX1_SIZE + POSTFIX2_SIZE + SIGNATURE_SIZE + DEVICE_SERIAL_SIZE) -#define AUTH_DATA_CHALLENGE_SIGN_MSG_SIZE (POSTFIX1_SIZE + POSTFIX2_SIZE + SIGNATURE_SIZE) - -/** - * @brief - * @details - * - * @see - * @since v1.0.0 - * - * @note - */ -typedef struct auth_data_struct{ - uint8_t postfix1[POSTFIX1_SIZE], - postfix2[POSTFIX2_SIZE], - signature[SIGNATURE_SIZE], - serial[DEVICE_SERIAL_SIZE]; -} auth_data_t; +#include "atecc_utils.h" atecc_data_t atecc_data={0}; uint8_t challenge_no[32]={0}; @@ -103,25 +82,18 @@ extern lv_task_t *listener_task; #if (FIRMWARE_HASH_CALC == 0) static const uint8_t firmware_hash[] = -{ - 0x75, 0x36, 0x92, 0xec, 0x36, 0xad, 0xb4, 0xc7, - 0x94, 0xc9, 0x73, 0x94, 0x5e, 0xb2, 0xa9, 0x9c, - 0x16, 0x49, 0x70, 0x3e, 0xa6, 0xf7, 0x6b, 0xf2, - 0x59, 0xab, 0xb4, 0xfb, 0x83, 0x8e, 0x01, 0x3e, -}; + { + 0x75, 0x36, 0x92, 0xec, 0x36, 0xad, 0xb4, 0xc7, + 0x94, 0xc9, 0x73, 0x94, 0x5e, 0xb2, 0xa9, 0x9c, + 0x16, 0x49, 0x70, 0x3e, 0xa6, 0xf7, 0x6b, 0xf2, + 0x59, 0xab, 0xb4, 0xfb, 0x83, 0x8e, 0x01, 0x3e, + }; #else static void fw_hash_calculate(uint8_t * m_digest){ sha256_Raw((uint8_t*)APPLICATION_ADDRESS_BASE, get_fwSize(), m_digest); } #endif -static ATCA_STATUS helper_config_to_sign_internal(ATCADeviceType device_type, struct atca_sign_internal_in_out *param, const uint8_t* config); -static ATCA_STATUS helper_sign_internal_msg(struct atca_sign_internal_in_out *param, uint8_t mode, uint8_t priv_key_id, uint8_t data_key_id); -static void helper_get_gendig_hash(atecc_slot_define_t slot, uint8_t *data, uint8_t *digest, uint8_t *postfix); - - - - void __attribute__((optimize("O0"))) device_authentication_controller(){ ASSERT((atecc_data.cfg_atecc608a_iface) != NULL); @@ -167,12 +139,15 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ continue; } - helper_get_gendig_hash(slot_8_serial, auth_serial_packet.serial, tempkey_hash, auth_serial_packet.postfix1); + helper_get_gendig_hash(slot_8_serial, auth_serial_packet + .serial, tempkey_hash, auth_serial_packet.postfix1, atecc_data); sign_internal_param.message=tempkey_hash; sign_internal_param.digest=final_hash; - helper_sign_internal_msg(&sign_internal_param, SIGN_MODE_INTERNAL, slot_2_auth_key, slot_8_serial); + helper_sign_internal_msg(&sign_internal_param, + SIGN_MODE_INTERNAL, + slot_2_auth_key, slot_8_serial, atecc_data); { uint8_t result = ecdsa_verify_digest(&nist256p1, get_auth_public_key(), auth_serial_packet.signature, sign_internal_param.digest); if (atecc_data.status != ATCA_SUCCESS || result != 0){ @@ -189,7 +164,7 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ transmit_data_to_app(DEVICE_SERAIL_NO_SIGNED, (uint8_t*)&auth_serial_packet, AUTH_DATA_SERIAL_SIGN_MSG_SIZE); #if X1WALLET_INITIAL - flow_level.level_three = DEVICE_AUTH_INFINITE_WAIT; + flow_level.level_three = DEVICE_AUTH_INFINITE_WAIT; lv_task_set_prio(listener_task, LV_TASK_PRIO_MID); // explicitly enable task listener } break; @@ -199,7 +174,7 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ } break; case SIGN_CHALLENGE: { - uint8_t nonce[32] = {0}; + uint8_t nonce[32] = {0}; auth_data_t auth_challenge_packet = {0}; uint8_t io_protection_key[32] = {0}; uint8_t tempkey_hash[DEVICE_SERIAL_SIZE+POSTFIX2_SIZE] = {0}; @@ -212,7 +187,7 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ fw_hash_calculate(firmware_hash); #endif for (int i = 0 ; i < 32 ; ++i) - challenge_no[i] = challenge_no[i]^firmware_hash[i]; + challenge_no[i] = challenge_no[i]^firmware_hash[i]; atecc_data.retries = DEFAULT_ATECC_RETRIES; bool usb_irq_enable_on_entry = NVIC_GetEnableIRQ(OTG_FS_IRQn); @@ -248,12 +223,17 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ continue; } - helper_get_gendig_hash(slot_5_challenge, challenge_no, tempkey_hash, auth_challenge_packet.postfix1); + helper_get_gendig_hash(slot_5_challenge, challenge_no, + tempkey_hash, auth_challenge_packet + .postfix1, atecc_data); sign_internal_param.message=tempkey_hash; sign_internal_param.digest=final_hash; - helper_sign_internal_msg(&sign_internal_param, SIGN_MODE_INTERNAL, slot_2_auth_key, slot_5_challenge); + helper_sign_internal_msg(&sign_internal_param, + SIGN_MODE_INTERNAL, + slot_2_auth_key, slot_5_challenge, + atecc_data); //overwrite challenge slot to signature generation on same challenge memset(challenge_no, 0, sizeof(challenge_no)); @@ -279,7 +259,7 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ lv_task_set_prio(listener_task, LV_TASK_PRIO_MID); // explicitly enable task listener #endif } break; - + case AUTHENTICATION_SUCCESS: { comm_process_complete(); reset_flow_level(); @@ -293,9 +273,9 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ #else #error Specify what to build (X1WALLET_INITIAL or X1WALLET_MAIN) #endif - } break; + } break; - case AUTHENTICATION_UNSUCCESSFUL: { + case AUTHENTICATION_UNSUCCESSFUL: { comm_process_complete(); reset_flow_level(); lv_obj_clean(lv_scr_act()); @@ -310,11 +290,11 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ #else #error Specify what to build (X1WALLET_INITIAL or X1WALLET_MAIN) #endif - } break; - + } break; + default: - return; /// exit on Default case so that first boot variable is switched only on hitting a valid case - + return; /// exit on Default case so that first boot variable is switched only on hitting a valid case + } // first boot variable is switched if any of the cases are hit @@ -323,143 +303,4 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ set_auth_state(get_auth_state()); /// Since we do not have setter for boot_flag, this indirectly disables the first_boot flag } -} - -void helper_get_gendig_hash(atecc_slot_define_t slot, uint8_t *data, uint8_t *digest, uint8_t *postfix) -{ - if(digest == NULL || data == NULL || postfix == NULL){ - return; - } - - uint8_t tempkey_init[96] = {0}; - uint8_t atecc_serial[9]; - atecc_data.status = atcab_read_serial_number(atecc_serial); - memcpy(tempkey_init, data, 32); - postfix[0] = tempkey_init[32] = 0x15; - postfix[1] = tempkey_init[33] = 0x02; - postfix[2] = tempkey_init[34] = slot; - postfix[3] = tempkey_init[35] = 0x00; - postfix[4] = tempkey_init[36] = atecc_serial[8]; - postfix[5] = tempkey_init[37] = atecc_serial[0]; - postfix[6] = tempkey_init[38] = atecc_serial[1]; - - sha256_Raw(tempkey_init, 96, digest); -} - -ATCA_STATUS helper_sign_internal_msg(struct atca_sign_internal_in_out *param, uint8_t mode, uint8_t priv_key_id, uint8_t data_key_id) -{ - uint8_t msg[55]; - uint8_t cfg[128]={0}, sn[9]={0}; - atca_temp_key_t temp_key={0}; - param->key_id=priv_key_id; - temp_key.key_id=data_key_id; - temp_key.gen_dig_data=1; - temp_key.valid=1; - temp_key.source_flag=1; - - atecc_data.status = atcab_read_config_zone(cfg); - memcpy(temp_key.value, param->message, 32); - param->temp_key=&temp_key; - helper_config_to_sign_internal(ATECC608A, param, cfg); - atecc_data.status = atcab_read_serial_number(sn); - - if (param == NULL || param->temp_key == NULL) - { - return ATCA_BAD_PARAM; - } - - memset(msg, 0, sizeof(msg)); - memcpy(&msg[0], param->temp_key->value, 32); - msg[32] = ATCA_SIGN; // Sign OpCode - msg[33] = param->mode; // Sign Mode - msg[34] = (uint8_t)(param->key_id >> 0); // Sign KeyID - msg[35] = (uint8_t)(param->key_id >> 8); - msg[36] = (uint8_t)(param->slot_config >> 0); // SlotConfig[TempKeyFlags.keyId] - msg[37] = (uint8_t)(param->slot_config >> 8); - msg[38] = (uint8_t)(param->key_config >> 0); // KeyConfig[TempKeyFlags.keyId] - msg[39] = (uint8_t)(param->key_config >> 8); - - //TempKeyFlags (b0-3: keyId, b4: sourceFlag, b5: GenDigData, b6: GenKeyData, b7: NoMacFlag) - msg[40] |= ((param->temp_key->key_id & 0x0F) << 0); - msg[40] |= ((param->temp_key->source_flag & 0x01) << 4); - msg[40] |= ((param->temp_key->gen_dig_data & 0x01) << 5); - msg[40] |= ((param->temp_key->gen_key_data & 0x01) << 6); - msg[40] |= ((param->temp_key->no_mac_flag & 0x01) << 7); - msg[41] = 0x00; - msg[42] = 0x00; - - // Serial Number - msg[43] = sn[8]; - memcpy(&msg[48], &sn[0], 2); - if (param->mode & SIGN_MODE_INCLUDE_SN) - { - memcpy(&msg[44], &sn[4], 4); - memcpy(&msg[50], &sn[2], 2); - } - - // The bit within the SlotLocked field corresponding to the last key used in the TempKey computation is in the LSB - msg[52] = param->is_slot_locked ? 0x00 : 0x01; - - // If the slot contains a public key corresponding to a supported curve, and if PubInfo indicates this key must be - // validated before being used by Verify, and if the validity bits have a value of 0x05, then the PubKey Valid byte - // will be 0x01.In all other cases, it will be 0. - msg[53] = param->for_invalidate ? 0x01 : 0x00; - - msg[54] = 0x00; - - if (param->message) - { - memcpy(param->message, msg, sizeof(msg)); - } - if (param->verify_other_data) - { - memcpy(¶m->verify_other_data[0], &msg[33], 10); - memcpy(¶m->verify_other_data[10], &msg[44], 4); - memcpy(¶m->verify_other_data[14], &msg[50], 5); - } - if (param->digest) - { - return atcac_sw_sha2_256(msg, sizeof(msg), param->digest); - } - else - { - return ATCA_SUCCESS; - } -} - -ATCA_STATUS helper_config_to_sign_internal(ATCADeviceType device_type, struct atca_sign_internal_in_out *param, const uint8_t* config) -{ - const uint8_t* value = NULL; - uint16_t slot_locked = 0; - - if (param == NULL || config == NULL || param->temp_key == NULL) - { - return ATCA_BAD_PARAM; - } - - // SlotConfig[TempKeyFlags.keyId] - value = &config[20 + param->temp_key->key_id * 2]; - param->slot_config = (uint16_t)value[0] | ((uint16_t)value[1] << 8); - - // KeyConfig[TempKeyFlags.keyId] - value = &config[96 + param->temp_key->key_id * 2]; - param->key_config = (uint16_t)value[0] | ((uint16_t)value[1] << 8); - - if (device_type == ATECC108A && param->temp_key->key_id < 8) - { - value = &config[52 + param->temp_key->key_id * 2]; - param->use_flag = value[0]; - param->update_count = value[0]; - } - else - { - param->use_flag = 0x00; - param->update_count = 0x00; - } - - //SlotLocked:TempKeyFlags.keyId - slot_locked = (uint16_t)config[88] | ((uint16_t)config[89] << 8); - param->is_slot_locked = (slot_locked & (1 << param->temp_key->key_id)) ? false : true; - - return ATCA_SUCCESS; } \ No newline at end of file From ca6171ae3a19b28de495712a6dc99e4918319420 Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Sat, 11 Feb 2023 12:11:04 +0530 Subject: [PATCH 02/12] implements session initiation --- .../desktop_app_interface/communication.h | 4 + common/libraries/util/atecc_utils.c | 115 +++++++++++++++++- common/libraries/util/atecc_utils.h | 10 +- common/libraries/util/session_utils.c | 96 ++++++++++++++- common/libraries/util/session_utils.h | 39 +++++- src/controller_main.c | 35 ++++++ src/controller_main.h | 1 + 7 files changed, 286 insertions(+), 14 deletions(-) diff --git a/common/interfaces/desktop_app_interface/communication.h b/common/interfaces/desktop_app_interface/communication.h index f880c7fc6..5cfeb075e 100644 --- a/common/interfaces/desktop_app_interface/communication.h +++ b/common/interfaces/desktop_app_interface/communication.h @@ -129,6 +129,10 @@ typedef enum commandType { SIGN_MSG_RAW_MSG = 94, ///< Request by device to fetch raw message SIGN_MSG_SEND_SIG = 95, ///< Response by device for send transaction with signed transaction + SESSION_INIT_START = 100, ///< Request by desktop to start a session + SESSION_INIT_SEND_DEVICE_RANDOM = 101, ///< Response by device with device random + SESSION_INIT_RECV_SESSION_ID = 102, ///< Request by desktop to send session id + DEVICE_FLOW_RESET_REQ = 0xFF ///< unused enum } En_command_type_t; diff --git a/common/libraries/util/atecc_utils.c b/common/libraries/util/atecc_utils.c index 25a13c8a3..0abb163c8 100644 --- a/common/libraries/util/atecc_utils.c +++ b/common/libraries/util/atecc_utils.c @@ -1,5 +1,7 @@ #include "atecc_utils.h" #include "sha2.h" +#include "flash_api.h" +#include "controller_level_four.h" static ATCA_STATUS helper_config_to_sign_internal(ATCADeviceType device_type, struct atca_sign_internal_in_out *param, @@ -40,14 +42,14 @@ void helper_get_gendig_hash(atecc_slot_define_t slot, uint8_t *data, uint8_t *digest, uint8_t *postfix, - atecc_data_t atecc_data) { + atecc_data_t atecc_value) { if (digest == NULL || data == NULL || postfix == NULL) { return; } uint8_t tempkey_init[96] = {0}; uint8_t atecc_serial[9]; - atecc_data.status = atcab_read_serial_number(atecc_serial); + atecc_value.status = atcab_read_serial_number(atecc_serial); memcpy(tempkey_init, data, 32); postfix[0] = tempkey_init[32] = 0x15; postfix[1] = tempkey_init[33] = 0x02; @@ -64,7 +66,7 @@ ATCA_STATUS helper_sign_internal_msg(struct atca_sign_internal_in_out *param, uint8_t mode, uint8_t priv_key_id, uint8_t data_key_id, - atecc_data_t atecc_data) { + atecc_data_t atecc_value) { uint8_t msg[55]; uint8_t cfg[128] = {0}, sn[9] = {0}; atca_temp_key_t temp_key = {0}; @@ -74,11 +76,11 @@ ATCA_STATUS helper_sign_internal_msg(struct atca_sign_internal_in_out *param, temp_key.valid = 1; temp_key.source_flag = 1; - atecc_data.status = atcab_read_config_zone(cfg); + atecc_value.status = atcab_read_config_zone(cfg); memcpy(temp_key.value, param->message, 32); param->temp_key = &temp_key; helper_config_to_sign_internal(ATECC608A, param, cfg); - atecc_data.status = atcab_read_serial_number(sn); + atecc_value.status = atcab_read_serial_number(sn); if (param == NULL || param->temp_key == NULL) { return ATCA_BAD_PARAM; @@ -137,4 +139,107 @@ ATCA_STATUS helper_sign_internal_msg(struct atca_sign_internal_in_out *param, } else { return ATCA_SUCCESS; } +} + +auth_data_t atecc_sign(uint8_t *hash) { + uint8_t io_protection_key[32] = {0}; + uint8_t nonce[32] = {0}; + auth_data_t auth_challenge_packet = {0}; + uint8_t tempkey_hash[DEVICE_SERIAL_SIZE + POSTFIX2_SIZE] = {0}; + uint8_t final_hash[32] = {0}; + get_io_protection_key(io_protection_key); + + memset(challenge_no, 0, sizeof(challenge_no)); + for (int i = 0; i < 32; ++i) + challenge_no[i] = challenge_no[i] ^ hash[i]; + + atca_sign_internal_in_out_t sign_internal_param = {0}; + + atecc_data.retries = DEFAULT_ATECC_RETRIES; + bool usb_irq_enable_on_entry = NVIC_GetEnableIRQ(OTG_FS_IRQn); + NVIC_DisableIRQ(OTG_FS_IRQn); + do { + OTG_FS_IRQHandler(); + + if (atecc_data.status != ATCA_SUCCESS) + LOG_CRITICAL("AERR CH: %04x, count:%d", + atecc_data.status, + DEFAULT_ATECC_RETRIES - atecc_data.retries); + + if ((atecc_data.status = atcab_init(atecc_data.cfg_atecc608a_iface)) + != ATCA_SUCCESS) { + continue; + } + + atecc_data.status = atcab_write_enc(slot_5_challenge, + 0, + challenge_no, + io_protection_key, + slot_6_io_key); + if (atecc_data.status != ATCA_SUCCESS) { + continue; + } + + atecc_data.status = atcab_nonce(nonce); + if (atecc_data.status != ATCA_SUCCESS) { + continue; + } + + atecc_data.status = + atcab_gendig(ATCA_ZONE_DATA, slot_5_challenge, NULL, 0); + if (atecc_data.status != ATCA_SUCCESS) { + continue; + } + + atecc_data.status = atcab_sign_internal(slot_2_auth_key, + false, + false, + auth_challenge_packet.signature); + if (atecc_data.status != ATCA_SUCCESS) { + continue; + } + + helper_get_gendig_hash(slot_5_challenge, challenge_no, + tempkey_hash, auth_challenge_packet + .postfix1, atecc_data); + + sign_internal_param.message = tempkey_hash; + sign_internal_param.digest = final_hash; + + helper_sign_internal_msg(&sign_internal_param, + SIGN_MODE_INTERNAL, + slot_2_auth_key, slot_5_challenge, + atecc_data); + + memset(challenge_no, 0, sizeof(challenge_no)); + atecc_data.status = atcab_write_enc(slot_5_challenge, + 0, + challenge_no, + io_protection_key, + slot_6_io_key); + if (atecc_data.status != ATCA_SUCCESS) { + continue; + } + + { + uint8_t result = ecdsa_verify_digest(&nist256p1, + get_auth_public_key(), + auth_challenge_packet.signature, + sign_internal_param.digest); + if (atecc_data.status != ATCA_SUCCESS || result != 0) { + LOG_ERROR("err xxx33 fault %d verify %d", + atecc_data.status, + result); + } + } + + } while (--atecc_data.retries && atecc_data.status != ATCA_SUCCESS); + if (usb_irq_enable_on_entry == true) + NVIC_EnableIRQ(OTG_FS_IRQn); + + memcpy(auth_challenge_packet.postfix2, + &tempkey_hash[32], + POSTFIX2_SIZE); //postfix 2 (12bytes) + + return auth_challenge_packet; } \ No newline at end of file diff --git a/common/libraries/util/atecc_utils.h b/common/libraries/util/atecc_utils.h index 007b6607c..63e96abb5 100644 --- a/common/libraries/util/atecc_utils.h +++ b/common/libraries/util/atecc_utils.h @@ -76,13 +76,13 @@ typedef struct { * @param data * @param digest * @param postfix - * @param atecc_data + * @param atecc_value */ void helper_get_gendig_hash(atecc_slot_define_t slot, uint8_t *data, uint8_t *digest, uint8_t *postfix, - atecc_data_t atecc_data); + atecc_data_t atecc_value); /** * @brief @@ -94,13 +94,15 @@ void helper_get_gendig_hash(atecc_slot_define_t slot, * @param mode * @param priv_key_id * @param data_key_id - * @param atecc_data + * @param atecc_value * @return */ ATCA_STATUS helper_sign_internal_msg(struct atca_sign_internal_in_out *param, uint8_t mode, uint8_t priv_key_id, uint8_t data_key_id, - atecc_data_t atecc_data); + atecc_data_t atecc_value); + +auth_data_t atecc_sign(uint8_t *hash); #endif //ATECC_UTILS_H \ No newline at end of file diff --git a/common/libraries/util/session_utils.c b/common/libraries/util/session_utils.c index 1a5f448fb..e27224da5 100644 --- a/common/libraries/util/session_utils.c +++ b/common/libraries/util/session_utils.c @@ -1,7 +1,8 @@ #include "session_utils.h" +#include "controller_level_four.h" -bool verify_session_digest(uint8_t *payload, uint16_t payload_length, - uint8_t *buffer) { +bool verify_session_signature(uint8_t *payload, uint16_t payload_length, + uint8_t *buffer) { uint8_t hash[32] = {0}; sha256_Raw(payload, payload_length, hash); @@ -34,4 +35,93 @@ bool verify_session_digest(uint8_t *payload, uint16_t payload_length, hash); return status; -} \ No newline at end of file +} + +void append_signature(uint8_t *payload, uint16_t payload_length, Message +*message) { + uint8_t hash[32] = {0}; + sha256_Raw(payload, payload_length, hash); + auth_data_t signed_data = atecc_sign(hash); + memcpy(message->signature, signed_data.signature, SIGNATURE_SIZE); + memcpy(message->postfix1, signed_data.postfix1, POSTFIX1_SIZE); + memcpy(message->postfix2, signed_data.postfix2, POSTFIX2_SIZE); +} + +void session_pre_init(Session *session, Message *session_pre_init_details) { + random_buffer(session->device_random, DEVICE_RANDOM_SIZE); + get_device_serial(); + memcpy(session->device_id, atecc_data.device_serial, DEVICE_SERIAL_SIZE); + + session_pre_init_details->message = (uint8_t *) malloc + (DEVICE_RANDOM_SIZE + DEVICE_SERIAL_SIZE); + session_pre_init_details->message_size = DEVICE_RANDOM_SIZE + + DEVICE_SERIAL_SIZE; + + memcpy(session_pre_init_details->message, + session->device_random, + DEVICE_RANDOM_SIZE); + memcpy(session_pre_init_details->message + DEVICE_RANDOM_SIZE, + session->device_id, + DEVICE_SERIAL_SIZE); + + session_pre_init_details->signature = (uint8_t *) malloc(SIGNATURE_SIZE); + append_signature(session_pre_init_details->message, + DEVICE_RANDOM_SIZE + DEVICE_SERIAL_SIZE, + session_pre_init_details); +} + +void byte_array_to_session_message(uint8_t *data_array, uint16_t msg_size, + Message *msg) { + msg->message_size = msg_size - SIGNATURE_SIZE; + msg->message = (uint8_t *) malloc(msg->message_size); + memcpy(msg->message, data_array, msg->message_size); + msg->signature = (uint8_t *) malloc(SIGNATURE_SIZE); + memcpy(msg->signature, data_array + msg->message_size, + SIGNATURE_SIZE); +} + +uint8_t session_message_to_byte_array(Message msg, uint8_t *data_array) { + uint8_t data_array_size = 0; + memcpy(data_array, msg.message, msg.message_size); + data_array_size += msg.message_size; + memcpy(data_array + data_array_size, msg.signature, SIGNATURE_SIZE); + data_array_size += SIGNATURE_SIZE; + memcpy(data_array + data_array_size, msg.postfix1, POSTFIX1_SIZE); + data_array_size += POSTFIX1_SIZE; + memcpy(data_array + data_array_size, msg.postfix2, POSTFIX2_SIZE); + data_array_size += POSTFIX2_SIZE; + + return data_array_size; +} + +bool session_init(Session *session, Message *session_init_details) { + + memcpy(session->session_id, + session_init_details->message, + SESSION_ID_SIZE); + + uint8_t payload[SESSION_ID_SIZE + DEVICE_RANDOM_SIZE + + DEVICE_SERIAL_SIZE]; + size_t payload_length = 0; + memcpy(payload, session->session_id, SESSION_ID_SIZE); + payload_length += SESSION_ID_SIZE; + memcpy(payload + payload_length, session->device_random, + DEVICE_RANDOM_SIZE); + payload_length += DEVICE_RANDOM_SIZE; + memcpy(payload + payload_length, session->device_id, + DEVICE_SERIAL_SIZE); + payload_length += DEVICE_SERIAL_SIZE; + + if (!verify_session_signature(payload, + payload_length, + session_init_details->signature)) { + return false; + } + + if (memcmp(session_init_details->message + SESSION_ID_SIZE, + session->device_id, DEVICE_SERIAL_SIZE) != 0) { + return false; + } + + return true; +} diff --git a/common/libraries/util/session_utils.h b/common/libraries/util/session_utils.h index 00f5aaa71..f42b30a6b 100644 --- a/common/libraries/util/session_utils.h +++ b/common/libraries/util/session_utils.h @@ -21,8 +21,43 @@ #include "base58.h" #include "nist256p1.h" #include "nfc.h" +#include "atecc_utils.h" +#include "rand.h" -bool verify_session_digest(uint8_t *payload, uint16_t payload_length, - uint8_t *buffer); +#define SESSION_ID_SIZE 32 +#define DEVICE_RANDOM_SIZE 32 + +#pragma pack(push, 1) +typedef struct { + uint8_t device_random[DEVICE_RANDOM_SIZE]; + uint8_t device_id[DEVICE_SERIAL_SIZE]; + uint8_t session_id[SESSION_ID_SIZE]; +} Session; +#pragma pack(pop) + +#pragma pack(push, 1) +typedef struct { + uint16_t message_size; + uint8_t *message; + uint8_t *signature; + uint8_t postfix1[POSTFIX1_SIZE]; + uint8_t postfix2[POSTFIX2_SIZE]; +} Message; +#pragma pack(pop) + +bool verify_session_signature(uint8_t *payload, uint16_t payload_length, + uint8_t *buffer); + +void session_pre_init(Session *session, Message *session_pre_init_details); + +bool session_init(Session *session, Message *session_init_details); + +void byte_array_to_session_message(uint8_t *data_array, uint16_t msg_size, + Message *msg); + +uint8_t session_message_to_byte_array(Message msg, uint8_t *data_array); + +void append_signature(uint8_t *payload, uint16_t payload_length, Message +*message); #endif //SESSION_UTILS diff --git a/src/controller_main.c b/src/controller_main.c index 223257e5b..ceb729843 100644 --- a/src/controller_main.c +++ b/src/controller_main.c @@ -171,6 +171,9 @@ Flash_Wallet wallet_for_flash; MessageData msg_data; ui_display_node *current_display_node = NULL; +// create a new session object +Session session; + Flow_level* get_flow_level() { ASSERT((&flow_level) != NULL); @@ -692,6 +695,38 @@ void desktop_listener_task(lv_task_t* data) } clear_message_received_data(); } break; + + case SESSION_INIT_START: { + Message session_pre_init_details; + session_pre_init(&session, &session_pre_init_details); + + uint8_t *session_details_data_array = (uint8_t *) malloc + (session_pre_init_details.message_size + SIGNATURE_SIZE + + POSTFIX1_SIZE + POSTFIX2_SIZE); + uint8_t session_detail_data_array_size = + session_message_to_byte_array(session_pre_init_details, + session_details_data_array); + + // Device Random (32) + Device Serial (32) + Signature (64) + Postfix1 + Postfix2 + transmit_data_to_app(SESSION_INIT_SEND_DEVICE_RANDOM, + session_details_data_array, + session_detail_data_array_size); + + } + break; + + case SESSION_INIT_RECV_SESSION_ID: { + Message session_init_details; + byte_array_to_session_message(data_array, msg_size, + &session_init_details); + + if (!session_init(&session, &session_init_details)) { + LOG_CRITICAL("xxec %d:%d", false, __LINE__); + comm_reject_invalid_cmd(); + clear_message_received_data(); + } + } + break; #ifdef DEV_BUILD case EXPORT_ALL: { const Flash_Wallet* flash_wallet; diff --git a/src/controller_main.h b/src/controller_main.h index a3a09bf4d..5872ba16c 100644 --- a/src/controller_main.h +++ b/src/controller_main.h @@ -42,6 +42,7 @@ #include "sha2.h" #include "sys_state.h" #include "tasks_level_four.h" +#include "session_utils.h" #ifdef DEV_BUILD #define SKIP_ENTER_MNEMONICS_DEBUG From 73990e61dbe505ec5ac7eed26a4940dcc4d5e65b Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Mon, 13 Feb 2023 10:25:54 +0530 Subject: [PATCH 03/12] update: added session age field in the session details --- .../desktop_app_interface/communication.h | 4 ++-- common/libraries/util/session_utils.c | 13 ++++++++++++- common/libraries/util/session_utils.h | 3 ++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/common/interfaces/desktop_app_interface/communication.h b/common/interfaces/desktop_app_interface/communication.h index 5cfeb075e..41df03a85 100644 --- a/common/interfaces/desktop_app_interface/communication.h +++ b/common/interfaces/desktop_app_interface/communication.h @@ -130,8 +130,8 @@ typedef enum commandType { SIGN_MSG_SEND_SIG = 95, ///< Response by device for send transaction with signed transaction SESSION_INIT_START = 100, ///< Request by desktop to start a session - SESSION_INIT_SEND_DEVICE_RANDOM = 101, ///< Response by device with device random - SESSION_INIT_RECV_SESSION_ID = 102, ///< Request by desktop to send session id + SESSION_INIT_SEND_DEVICE_RANDOM = 101, ///< Response by device with device random + SESSION_INIT_RECV_SESSION_ID = 102, ///< Request by desktop to send session id DEVICE_FLOW_RESET_REQ = 0xFF ///< unused enum diff --git a/common/libraries/util/session_utils.c b/common/libraries/util/session_utils.c index e27224da5..c1a3e77c1 100644 --- a/common/libraries/util/session_utils.c +++ b/common/libraries/util/session_utils.c @@ -48,7 +48,13 @@ void append_signature(uint8_t *payload, uint16_t payload_length, Message } void session_pre_init(Session *session, Message *session_pre_init_details) { - random_buffer(session->device_random, DEVICE_RANDOM_SIZE); + // random_buffer(session->device_random, DEVICE_RANDOM_SIZE); + uint8_t hardcoded[] = {0x01, 0x0B, 0x07, 0xE6, 0x03, 0x00, 0x01, 0x00, + 0x3F, 0x00, 0x46, 0x00, 0x11, 0x50, 0x56, 0x39, + 0x55, 0x32, 0x31, 0x20, 0x8B, 0x4B, 0x4C, 0x8F, + 0x0C, 0xAD, 0x37, 0x06, 0x70, 0xEA, 0x13, 0xA9}; + + memcpy(session->device_random, hardcoded, DEVICE_RANDOM_SIZE); get_device_serial(); memcpy(session->device_id, atecc_data.device_serial, DEVICE_SERIAL_SIZE); @@ -123,5 +129,10 @@ bool session_init(Session *session, Message *session_init_details) { return false; } + session->session_age = bendian_byte_to_dec(session_init_details->message + + SESSION_ID_SIZE + + DEVICE_SERIAL_SIZE, + sizeof(session->session_age)); + return true; } diff --git a/common/libraries/util/session_utils.h b/common/libraries/util/session_utils.h index f42b30a6b..563f733a4 100644 --- a/common/libraries/util/session_utils.h +++ b/common/libraries/util/session_utils.h @@ -31,7 +31,8 @@ typedef struct { uint8_t device_random[DEVICE_RANDOM_SIZE]; uint8_t device_id[DEVICE_SERIAL_SIZE]; - uint8_t session_id[SESSION_ID_SIZE]; + uint8_t session_id[SESSION_ID_SIZE]; + uint16_t session_age; } Session; #pragma pack(pop) From 93cd0f1c2bc991b8a788b2595d0139c3005bacc4 Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Wed, 15 Feb 2023 17:07:16 +0530 Subject: [PATCH 04/12] hardcoded size of initial message transfer --- common/libraries/util/session_utils.c | 13 +++---------- common/libraries/util/session_utils.h | 2 +- src/controller_main.c | 7 ++++--- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/common/libraries/util/session_utils.c b/common/libraries/util/session_utils.c index c1a3e77c1..250ad8bec 100644 --- a/common/libraries/util/session_utils.c +++ b/common/libraries/util/session_utils.c @@ -48,13 +48,8 @@ void append_signature(uint8_t *payload, uint16_t payload_length, Message } void session_pre_init(Session *session, Message *session_pre_init_details) { - // random_buffer(session->device_random, DEVICE_RANDOM_SIZE); - uint8_t hardcoded[] = {0x01, 0x0B, 0x07, 0xE6, 0x03, 0x00, 0x01, 0x00, - 0x3F, 0x00, 0x46, 0x00, 0x11, 0x50, 0x56, 0x39, - 0x55, 0x32, 0x31, 0x20, 0x8B, 0x4B, 0x4C, 0x8F, - 0x0C, 0xAD, 0x37, 0x06, 0x70, 0xEA, 0x13, 0xA9}; + random_generate(session->device_random, DEVICE_RANDOM_SIZE); - memcpy(session->device_random, hardcoded, DEVICE_RANDOM_SIZE); get_device_serial(); memcpy(session->device_id, atecc_data.device_serial, DEVICE_SERIAL_SIZE); @@ -70,7 +65,6 @@ void session_pre_init(Session *session, Message *session_pre_init_details) { session->device_id, DEVICE_SERIAL_SIZE); - session_pre_init_details->signature = (uint8_t *) malloc(SIGNATURE_SIZE); append_signature(session_pre_init_details->message, DEVICE_RANDOM_SIZE + DEVICE_SERIAL_SIZE, session_pre_init_details); @@ -81,7 +75,6 @@ void byte_array_to_session_message(uint8_t *data_array, uint16_t msg_size, msg->message_size = msg_size - SIGNATURE_SIZE; msg->message = (uint8_t *) malloc(msg->message_size); memcpy(msg->message, data_array, msg->message_size); - msg->signature = (uint8_t *) malloc(SIGNATURE_SIZE); memcpy(msg->signature, data_array + msg->message_size, SIGNATURE_SIZE); } @@ -130,8 +123,8 @@ bool session_init(Session *session, Message *session_init_details) { } session->session_age = bendian_byte_to_dec(session_init_details->message + - SESSION_ID_SIZE + - DEVICE_SERIAL_SIZE, + SESSION_ID_SIZE + + DEVICE_SERIAL_SIZE, sizeof(session->session_age)); return true; diff --git a/common/libraries/util/session_utils.h b/common/libraries/util/session_utils.h index 563f733a4..a30449250 100644 --- a/common/libraries/util/session_utils.h +++ b/common/libraries/util/session_utils.h @@ -40,7 +40,7 @@ typedef struct { typedef struct { uint16_t message_size; uint8_t *message; - uint8_t *signature; + uint8_t signature[SIGNATURE_SIZE]; uint8_t postfix1[POSTFIX1_SIZE]; uint8_t postfix2[POSTFIX2_SIZE]; } Message; diff --git a/src/controller_main.c b/src/controller_main.c index ceb729843..7536148cd 100644 --- a/src/controller_main.c +++ b/src/controller_main.c @@ -700,9 +700,10 @@ void desktop_listener_task(lv_task_t* data) Message session_pre_init_details; session_pre_init(&session, &session_pre_init_details); - uint8_t *session_details_data_array = (uint8_t *) malloc - (session_pre_init_details.message_size + SIGNATURE_SIZE + - POSTFIX1_SIZE + POSTFIX2_SIZE); + uint8_t session_details_data_array[DEVICE_RANDOM_SIZE + + DEVICE_SERIAL_SIZE + SIGNATURE_SIZE + POSTFIX1_SIZE + + POSTFIX2_SIZE]; + uint8_t session_detail_data_array_size = session_message_to_byte_array(session_pre_init_details, session_details_data_array); From f25ad5e536eafae1f32c80b976bf0883e0aa9d5d Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Thu, 16 Feb 2023 10:05:59 +0530 Subject: [PATCH 05/12] refactored the public key derivation --- common/libraries/util/session_utils.c | 20 +++++++++++++------- common/libraries/util/session_utils.h | 11 ++++++++--- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/common/libraries/util/session_utils.c b/common/libraries/util/session_utils.c index 250ad8bec..07cd7b93b 100644 --- a/common/libraries/util/session_utils.c +++ b/common/libraries/util/session_utils.c @@ -1,12 +1,9 @@ #include "session_utils.h" #include "controller_level_four.h" -bool verify_session_signature(uint8_t *payload, uint16_t payload_length, - uint8_t *buffer) { +uint8_t session_key_derv_data[12] = {0}; - uint8_t hash[32] = {0}; - sha256_Raw(payload, payload_length, hash); - uint8_t session_key_derv_data[12] = {0}; +void derive_public_key(Session *session) { HDNode session_node; uint32_t index; char xpub[112] = {'\0'}; @@ -27,10 +24,17 @@ bool verify_session_signature(uint8_t *payload, uint16_t payload_length, index = read_be(session_key_derv_data + 8); hdnode_public_ckd(&session_node, index); +} + +bool verify_session_signature(Session *session, uint8_t *payload, uint16_t +payload_length, uint8_t *buffer) { + + uint8_t hash[32] = {0}; + sha256_Raw(payload, payload_length, hash); uint8_t status = ecdsa_verify_digest(&nist256p1, - session_node.public_key, + session->public_key, buffer, hash); @@ -50,6 +54,8 @@ void append_signature(uint8_t *payload, uint16_t payload_length, Message void session_pre_init(Session *session, Message *session_pre_init_details) { random_generate(session->device_random, DEVICE_RANDOM_SIZE); + derive_public_key(session); + get_device_serial(); memcpy(session->device_id, atecc_data.device_serial, DEVICE_SERIAL_SIZE); @@ -111,7 +117,7 @@ bool session_init(Session *session, Message *session_init_details) { DEVICE_SERIAL_SIZE); payload_length += DEVICE_SERIAL_SIZE; - if (!verify_session_signature(payload, + if (!verify_session_signature(session, payload, payload_length, session_init_details->signature)) { return false; diff --git a/common/libraries/util/session_utils.h b/common/libraries/util/session_utils.h index a30449250..46fbd5478 100644 --- a/common/libraries/util/session_utils.h +++ b/common/libraries/util/session_utils.h @@ -32,6 +32,7 @@ typedef struct { uint8_t device_random[DEVICE_RANDOM_SIZE]; uint8_t device_id[DEVICE_SERIAL_SIZE]; uint8_t session_id[SESSION_ID_SIZE]; + uint8_t public_key[33]; uint16_t session_age; } Session; #pragma pack(pop) @@ -46,15 +47,19 @@ typedef struct { } Message; #pragma pack(pop) -bool verify_session_signature(uint8_t *payload, uint16_t payload_length, - uint8_t *buffer); +extern uint8_t session_key_derv_data[12]; + +void derive_public_key(Session *session); + +bool verify_session_signature(Session *session, uint8_t *payload, uint16_t +payload_length, uint8_t *buffer); void session_pre_init(Session *session, Message *session_pre_init_details); bool session_init(Session *session, Message *session_init_details); void byte_array_to_session_message(uint8_t *data_array, uint16_t msg_size, - Message *msg); + Message *msg); uint8_t session_message_to_byte_array(Message msg, uint8_t *data_array); From 10b39c0d4768a5a7ef441331c0adab01f6130065 Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Sat, 18 Feb 2023 09:42:49 +0530 Subject: [PATCH 06/12] updated the swap initialisation flow --- .../desktop_app_interface/communication.h | 8 +- common/libraries/util/session_utils.c | 132 +++++++++++------- common/libraries/util/session_utils.h | 15 +- src/controller_main.c | 39 +++--- 4 files changed, 112 insertions(+), 82 deletions(-) diff --git a/common/interfaces/desktop_app_interface/communication.h b/common/interfaces/desktop_app_interface/communication.h index 41df03a85..d4a62e13c 100644 --- a/common/interfaces/desktop_app_interface/communication.h +++ b/common/interfaces/desktop_app_interface/communication.h @@ -129,10 +129,10 @@ typedef enum commandType { SIGN_MSG_RAW_MSG = 94, ///< Request by device to fetch raw message SIGN_MSG_SEND_SIG = 95, ///< Response by device for send transaction with signed transaction - SESSION_INIT_START = 100, ///< Request by desktop to start a session - SESSION_INIT_SEND_DEVICE_RANDOM = 101, ///< Response by device with device random - SESSION_INIT_RECV_SESSION_ID = 102, ///< Request by desktop to send session id - + SESSION_INIT = 100, ///< Request by desktop to start a session + SESSION_INIT_SEND_DETAILS = 101, ///< Response by device with device random + SESSION_ESTABLISH = 102, ///< Response by desktop with session random + SESSION_ESTABLISH_VERIFY = 103, ///< Response by device with session establishment verification DEVICE_FLOW_RESET_REQ = 0xFF ///< unused enum } En_command_type_t; diff --git a/common/libraries/util/session_utils.c b/common/libraries/util/session_utils.c index 07cd7b93b..cb4e3ccbc 100644 --- a/common/libraries/util/session_utils.c +++ b/common/libraries/util/session_utils.c @@ -3,7 +3,7 @@ uint8_t session_key_derv_data[12] = {0}; -void derive_public_key(Session *session) { +static void derive_public_key(Session *session) { HDNode session_node; uint32_t index; char xpub[112] = {'\0'}; @@ -24,9 +24,21 @@ void derive_public_key(Session *session) { index = read_be(session_key_derv_data + 8); hdnode_public_ckd(&session_node, index); + + memcpy(session->public_key, session_node.public_key, + sizeof(session->public_key)); +} + +static void derive_session_id(Session *session) { + uint8_t payload[SESSION_RANDOM_SIZE + DEVICE_RANDOM_SIZE]; + memcpy(payload, session->session_random, SESSION_RANDOM_SIZE); + memcpy(payload + SESSION_RANDOM_SIZE, session->device_random, + DEVICE_RANDOM_SIZE); + + sha256_Raw(payload, sizeof(payload), session->session_id); } -bool verify_session_signature(Session *session, uint8_t *payload, uint16_t +bool verify_session_signature(Session *session, uint8_t *payload, uint8_t payload_length, uint8_t *buffer) { uint8_t hash[32] = {0}; @@ -38,42 +50,21 @@ payload_length, uint8_t *buffer) { buffer, hash); - return status; -} + return (status == 0); +}; -void append_signature(uint8_t *payload, uint16_t payload_length, Message -*message) { +void append_signature(uint8_t *payload, uint8_t payload_length, uint8_t +*signature_details) { uint8_t hash[32] = {0}; sha256_Raw(payload, payload_length, hash); auth_data_t signed_data = atecc_sign(hash); - memcpy(message->signature, signed_data.signature, SIGNATURE_SIZE); - memcpy(message->postfix1, signed_data.postfix1, POSTFIX1_SIZE); - memcpy(message->postfix2, signed_data.postfix2, POSTFIX2_SIZE); -} - -void session_pre_init(Session *session, Message *session_pre_init_details) { - random_generate(session->device_random, DEVICE_RANDOM_SIZE); - - derive_public_key(session); - - get_device_serial(); - memcpy(session->device_id, atecc_data.device_serial, DEVICE_SERIAL_SIZE); - - session_pre_init_details->message = (uint8_t *) malloc - (DEVICE_RANDOM_SIZE + DEVICE_SERIAL_SIZE); - session_pre_init_details->message_size = DEVICE_RANDOM_SIZE + - DEVICE_SERIAL_SIZE; - - memcpy(session_pre_init_details->message, - session->device_random, - DEVICE_RANDOM_SIZE); - memcpy(session_pre_init_details->message + DEVICE_RANDOM_SIZE, - session->device_id, - DEVICE_SERIAL_SIZE); - - append_signature(session_pre_init_details->message, - DEVICE_RANDOM_SIZE + DEVICE_SERIAL_SIZE, - session_pre_init_details); + uint8_t offset = 0; + memcpy(signature_details, signed_data.signature, SIGNATURE_SIZE); + offset += SIGNATURE_SIZE; + memcpy(signature_details + offset, signed_data.postfix1, POSTFIX1_SIZE); + offset += POSTFIX1_SIZE; + memcpy(signature_details + offset, signed_data.postfix2, POSTFIX2_SIZE); + offset += POSTFIX2_SIZE; } void byte_array_to_session_message(uint8_t *data_array, uint16_t msg_size, @@ -99,39 +90,72 @@ uint8_t session_message_to_byte_array(Message msg, uint8_t *data_array) { return data_array_size; } -bool session_init(Session *session, Message *session_init_details) { +void session_pre_init(Session *session, uint8_t *session_details_data_array) { + random_generate(session->device_random, DEVICE_RANDOM_SIZE); + derive_public_key(session); - memcpy(session->session_id, - session_init_details->message, - SESSION_ID_SIZE); + get_device_serial(); + memcpy(session->device_id, atecc_data.device_serial, DEVICE_SERIAL_SIZE); - uint8_t payload[SESSION_ID_SIZE + DEVICE_RANDOM_SIZE + - DEVICE_SERIAL_SIZE]; - size_t payload_length = 0; - memcpy(payload, session->session_id, SESSION_ID_SIZE); - payload_length += SESSION_ID_SIZE; - memcpy(payload + payload_length, session->device_random, + uint8_t session_details_data_array_size = 0; + + memcpy(session_details_data_array, + session->device_random, DEVICE_RANDOM_SIZE); - payload_length += DEVICE_RANDOM_SIZE; + session_details_data_array_size += DEVICE_RANDOM_SIZE; + + memcpy(session_details_data_array + session_details_data_array_size, + session->device_id, + DEVICE_SERIAL_SIZE); + session_details_data_array_size += DEVICE_SERIAL_SIZE; + + // Payload: Device Random + Device Id + append_signature(session_details_data_array, + session_details_data_array_size, + session_details_data_array + + session_details_data_array_size); +} + +bool session_init(Session *session, uint8_t *session_init_details, uint8_t +*verification_details) { + // Message contains: Session Random + Session Age + // Signature Payload: Session Random + Session Age + Device Id + Device Random + uint8_t offset = 0; + memcpy(session->session_random, + session_init_details, + SESSION_RANDOM_SIZE); + offset += SESSION_RANDOM_SIZE; + + memcpy(&session->session_age, session_init_details + offset, sizeof + (session->session_age)); + offset += sizeof(session->session_age); + + uint8_t payload[SESSION_RANDOM_SIZE + sizeof(session->session_age) + + DEVICE_SERIAL_SIZE + DEVICE_RANDOM_SIZE]; + + size_t payload_length = 0; + memcpy(payload, session_init_details, offset); + payload_length += offset; memcpy(payload + payload_length, session->device_id, DEVICE_SERIAL_SIZE); payload_length += DEVICE_SERIAL_SIZE; + memcpy(payload + payload_length, session->device_random, + DEVICE_RANDOM_SIZE); + payload_length += DEVICE_RANDOM_SIZE; if (!verify_session_signature(session, payload, payload_length, - session_init_details->signature)) { + session_init_details + offset)) { return false; } - if (memcmp(session_init_details->message + SESSION_ID_SIZE, - session->device_id, DEVICE_SERIAL_SIZE) != 0) { - return false; - } + derive_session_id(session); - session->session_age = bendian_byte_to_dec(session_init_details->message + - SESSION_ID_SIZE + - DEVICE_SERIAL_SIZE, - sizeof(session->session_age)); + // Verification details: Device Id + Signature + Postfix1 + Postfix2 + memcpy(verification_details, session->device_id, DEVICE_SERIAL_SIZE); + offset = DEVICE_SERIAL_SIZE; + append_signature(payload, payload_length, + verification_details + offset); return true; } diff --git a/common/libraries/util/session_utils.h b/common/libraries/util/session_utils.h index 46fbd5478..4b4cecf6a 100644 --- a/common/libraries/util/session_utils.h +++ b/common/libraries/util/session_utils.h @@ -26,10 +26,12 @@ #define SESSION_ID_SIZE 32 #define DEVICE_RANDOM_SIZE 32 +#define SESSION_RANDOM_SIZE 32 #pragma pack(push, 1) typedef struct { uint8_t device_random[DEVICE_RANDOM_SIZE]; + uint8_t session_random[SESSION_RANDOM_SIZE]; uint8_t device_id[DEVICE_SERIAL_SIZE]; uint8_t session_id[SESSION_ID_SIZE]; uint8_t public_key[33]; @@ -49,21 +51,20 @@ typedef struct { extern uint8_t session_key_derv_data[12]; -void derive_public_key(Session *session); - -bool verify_session_signature(Session *session, uint8_t *payload, uint16_t +bool verify_session_signature(Session *session, uint8_t *payload, uint8_t payload_length, uint8_t *buffer); -void session_pre_init(Session *session, Message *session_pre_init_details); +void session_pre_init(Session *session, uint8_t *session_details_data_array); -bool session_init(Session *session, Message *session_init_details); +bool session_init(Session *session, uint8_t *session_init_details, uint8_t +*verification_details); void byte_array_to_session_message(uint8_t *data_array, uint16_t msg_size, Message *msg); uint8_t session_message_to_byte_array(Message msg, uint8_t *data_array); -void append_signature(uint8_t *payload, uint16_t payload_length, Message -*message); +void append_signature(uint8_t *payload, uint8_t payload_length, uint8_t +*signature_details); #endif //SESSION_UTILS diff --git a/src/controller_main.c b/src/controller_main.c index 7536148cd..22d800605 100644 --- a/src/controller_main.c +++ b/src/controller_main.c @@ -696,35 +696,40 @@ void desktop_listener_task(lv_task_t* data) clear_message_received_data(); } break; - case SESSION_INIT_START: { - Message session_pre_init_details; - session_pre_init(&session, &session_pre_init_details); - + case SESSION_INIT: { + // Send: Device Random (32) + Device Id (32) + Signature (64) + Postfix1 + Postfix2 uint8_t session_details_data_array[DEVICE_RANDOM_SIZE + - DEVICE_SERIAL_SIZE + SIGNATURE_SIZE + POSTFIX1_SIZE + - POSTFIX2_SIZE]; + DEVICE_SERIAL_SIZE + SIGNATURE_SIZE + POSTFIX1_SIZE + + POSTFIX2_SIZE]; - uint8_t session_detail_data_array_size = - session_message_to_byte_array(session_pre_init_details, - session_details_data_array); + session_pre_init(&session, session_details_data_array); - // Device Random (32) + Device Serial (32) + Signature (64) + Postfix1 + Postfix2 - transmit_data_to_app(SESSION_INIT_SEND_DEVICE_RANDOM, + transmit_data_to_app(SESSION_INIT_SEND_DETAILS, session_details_data_array, - session_detail_data_array_size); + DEVICE_RANDOM_SIZE + + DEVICE_SERIAL_SIZE + SIGNATURE_SIZE + + POSTFIX1_SIZE + POSTFIX2_SIZE); } break; - case SESSION_INIT_RECV_SESSION_ID: { - Message session_init_details; - byte_array_to_session_message(data_array, msg_size, - &session_init_details); + case SESSION_ESTABLISH: { + // Send: Device Id (32) + Signature (64) + Postfix1 + Postfix2 + uint8_t verification_details[DEVICE_SERIAL_SIZE + + SIGNATURE_SIZE + POSTFIX1_SIZE + POSTFIX2_SIZE]; - if (!session_init(&session, &session_init_details)) { + if (!session_init(&session, + data_array, + verification_details)) { LOG_CRITICAL("xxec %d:%d", false, __LINE__); comm_reject_invalid_cmd(); clear_message_received_data(); + } else { + transmit_data_to_app(SESSION_ESTABLISH_VERIFY, + verification_details, + DEVICE_SERIAL_SIZE + + SIGNATURE_SIZE + POSTFIX1_SIZE + + POSTFIX2_SIZE); } } break; From f24df9105ac40f7eb91a1448d1e02509e442a3d3 Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Mon, 20 Feb 2023 11:36:28 +0530 Subject: [PATCH 07/12] Added doxygen comments to function methods and License certificate to new files --- .../desktop_app_interface/communication.h | 1 - common/libraries/util/session_utils.c | 125 +++++++++++---- common/libraries/util/session_utils.h | 143 ++++++++++++++++-- src/controller_main.c | 9 +- 4 files changed, 228 insertions(+), 50 deletions(-) diff --git a/common/interfaces/desktop_app_interface/communication.h b/common/interfaces/desktop_app_interface/communication.h index d4a62e13c..023fff0f6 100644 --- a/common/interfaces/desktop_app_interface/communication.h +++ b/common/interfaces/desktop_app_interface/communication.h @@ -134,7 +134,6 @@ typedef enum commandType { SESSION_ESTABLISH = 102, ///< Response by desktop with session random SESSION_ESTABLISH_VERIFY = 103, ///< Response by device with session establishment verification DEVICE_FLOW_RESET_REQ = 0xFF ///< unused enum - } En_command_type_t; /** diff --git a/common/libraries/util/session_utils.c b/common/libraries/util/session_utils.c index cb4e3ccbc..a7aa4f25d 100644 --- a/common/libraries/util/session_utils.c +++ b/common/libraries/util/session_utils.c @@ -1,9 +1,70 @@ +/** + * @file session_utils.c + * @author Cypherock X1 Team + * @brief Definition of the session utility functions + * This file defines the functions used to create and manage the + * session, send authentication requests and verify the responses. + * + * @copyright Copyright (c) 2022 HODL TECH PTE LTD + *
You may obtain a copy of license at https://mitcc.org/ + * + ****************************************************************************** + * @attention + * + * (c) Copyright 2022 by HODL TECH PTE LTD + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject + * to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * + * "Commons Clause" License Condition v1.0 + * + * The Software is provided to you by the Licensor under the License, + * as defined below, subject to the following condition. + * + * Without limiting other conditions in the License, the grant of + * rights under the License will not include, and the License does not + * grant to you, the right to Sell the Software. + * + * For purposes of the foregoing, "Sell" means practicing any or all + * of the rights granted to you under the License to provide to third + * parties, for a fee or other consideration (including without + * limitation fees for hosting or consulting/ support services related + * to the Software), a product or service whose value derives, entirely + * or substantially, from the functionality of the Software. Any license + * notice or attribution required by the License must also include + * this Commons Clause License Condition notice. + * + * Software: All X1Wallet associated files. + * License: MIT + * Licensor: HODL TECH PTE LTD + * + ****************************************************************************** + */ + #include "session_utils.h" #include "controller_level_four.h" uint8_t session_key_derv_data[12] = {0}; +Session session; -static void derive_public_key(Session *session) { +static void derive_public_key() { HDNode session_node; uint32_t index; char xpub[112] = {'\0'}; @@ -25,20 +86,20 @@ static void derive_public_key(Session *session) { index = read_be(session_key_derv_data + 8); hdnode_public_ckd(&session_node, index); - memcpy(session->public_key, session_node.public_key, - sizeof(session->public_key)); + memcpy(session.public_key, session_node.public_key, + sizeof(session.public_key)); } -static void derive_session_id(Session *session) { +static void derive_session_id() { uint8_t payload[SESSION_RANDOM_SIZE + DEVICE_RANDOM_SIZE]; - memcpy(payload, session->session_random, SESSION_RANDOM_SIZE); - memcpy(payload + SESSION_RANDOM_SIZE, session->device_random, + memcpy(payload, session.session_random, SESSION_RANDOM_SIZE); + memcpy(payload + SESSION_RANDOM_SIZE, session.device_random, DEVICE_RANDOM_SIZE); - sha256_Raw(payload, sizeof(payload), session->session_id); + sha256_Raw(payload, sizeof(payload), session.session_id); } -bool verify_session_signature(Session *session, uint8_t *payload, uint8_t +bool verify_session_signature(uint8_t *payload, uint8_t payload_length, uint8_t *buffer) { uint8_t hash[32] = {0}; @@ -46,7 +107,7 @@ payload_length, uint8_t *buffer) { uint8_t status = ecdsa_verify_digest(&nist256p1, - session->public_key, + session.public_key, buffer, hash); @@ -76,36 +137,36 @@ void byte_array_to_session_message(uint8_t *data_array, uint16_t msg_size, SIGNATURE_SIZE); } -uint8_t session_message_to_byte_array(Message msg, uint8_t *data_array) { +uint8_t session_message_to_byte_array(Message message, uint8_t *data_array) { uint8_t data_array_size = 0; - memcpy(data_array, msg.message, msg.message_size); - data_array_size += msg.message_size; - memcpy(data_array + data_array_size, msg.signature, SIGNATURE_SIZE); + memcpy(data_array, message.message, message.message_size); + data_array_size += message.message_size; + memcpy(data_array + data_array_size, message.signature, SIGNATURE_SIZE); data_array_size += SIGNATURE_SIZE; - memcpy(data_array + data_array_size, msg.postfix1, POSTFIX1_SIZE); + memcpy(data_array + data_array_size, message.postfix1, POSTFIX1_SIZE); data_array_size += POSTFIX1_SIZE; - memcpy(data_array + data_array_size, msg.postfix2, POSTFIX2_SIZE); + memcpy(data_array + data_array_size, message.postfix2, POSTFIX2_SIZE); data_array_size += POSTFIX2_SIZE; return data_array_size; } -void session_pre_init(Session *session, uint8_t *session_details_data_array) { - random_generate(session->device_random, DEVICE_RANDOM_SIZE); - derive_public_key(session); +void session_pre_init(uint8_t *session_details_data_array) { + random_generate(session.device_random, DEVICE_RANDOM_SIZE); + derive_public_key(); get_device_serial(); - memcpy(session->device_id, atecc_data.device_serial, DEVICE_SERIAL_SIZE); + memcpy(session.device_id, atecc_data.device_serial, DEVICE_SERIAL_SIZE); uint8_t session_details_data_array_size = 0; memcpy(session_details_data_array, - session->device_random, + session.device_random, DEVICE_RANDOM_SIZE); session_details_data_array_size += DEVICE_RANDOM_SIZE; memcpy(session_details_data_array + session_details_data_array_size, - session->device_id, + session.device_id, DEVICE_SERIAL_SIZE); session_details_data_array_size += DEVICE_SERIAL_SIZE; @@ -116,43 +177,43 @@ void session_pre_init(Session *session, uint8_t *session_details_data_array) { + session_details_data_array_size); } -bool session_init(Session *session, uint8_t *session_init_details, uint8_t +bool session_init(uint8_t *session_init_details, uint8_t *verification_details) { // Message contains: Session Random + Session Age // Signature Payload: Session Random + Session Age + Device Id + Device Random uint8_t offset = 0; - memcpy(session->session_random, + memcpy(session.session_random, session_init_details, SESSION_RANDOM_SIZE); offset += SESSION_RANDOM_SIZE; - memcpy(&session->session_age, session_init_details + offset, sizeof - (session->session_age)); - offset += sizeof(session->session_age); + memcpy(&session.session_age, session_init_details + offset, sizeof + (session.session_age)); + offset += sizeof(session.session_age); - uint8_t payload[SESSION_RANDOM_SIZE + sizeof(session->session_age) + + uint8_t payload[SESSION_RANDOM_SIZE + sizeof(session.session_age) + DEVICE_SERIAL_SIZE + DEVICE_RANDOM_SIZE]; size_t payload_length = 0; memcpy(payload, session_init_details, offset); payload_length += offset; - memcpy(payload + payload_length, session->device_id, + memcpy(payload + payload_length, session.device_id, DEVICE_SERIAL_SIZE); payload_length += DEVICE_SERIAL_SIZE; - memcpy(payload + payload_length, session->device_random, + memcpy(payload + payload_length, session.device_random, DEVICE_RANDOM_SIZE); payload_length += DEVICE_RANDOM_SIZE; - if (!verify_session_signature(session, payload, + if (!verify_session_signature(payload, payload_length, session_init_details + offset)) { return false; } - derive_session_id(session); + derive_session_id(); // Verification details: Device Id + Signature + Postfix1 + Postfix2 - memcpy(verification_details, session->device_id, DEVICE_SERIAL_SIZE); + memcpy(verification_details, session.device_id, DEVICE_SERIAL_SIZE); offset = DEVICE_SERIAL_SIZE; append_signature(payload, payload_length, verification_details + offset); diff --git a/common/libraries/util/session_utils.h b/common/libraries/util/session_utils.h index 4b4cecf6a..6693fe8d4 100644 --- a/common/libraries/util/session_utils.h +++ b/common/libraries/util/session_utils.h @@ -1,13 +1,61 @@ /** - * @file session_utils.h + * @file session_utils.h * @author Cypherock X1 Team - * @brief Functions for session management - * @version 0.1 - * @date 2023-02-07 + * @brief Header file containing the session utility functions + * This file declares the functions used to create and manage the + * session, send authentication requests and verify the responses. * * @copyright Copyright (c) 2022 HODL TECH PTE LTD *
You may obtain a copy of license at https://mitcc.org/ * + ****************************************************************************** + * @attention + * + * (c) Copyright 2022 by HODL TECH PTE LTD + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject + * to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * + * "Commons Clause" License Condition v1.0 + * + * The Software is provided to you by the Licensor under the License, + * as defined below, subject to the following condition. + * + * Without limiting other conditions in the License, the grant of + * rights under the License will not include, and the License does not + * grant to you, the right to Sell the Software. + * + * For purposes of the foregoing, "Sell" means practicing any or all + * of the rights granted to you under the License to provide to third + * parties, for a fee or other consideration (including without + * limitation fees for hosting or consulting/ support services related + * to the Software), a product or service whose value derives, entirely + * or substantially, from the functionality of the Software. Any license + * notice or attribution required by the License must also include + * this Commons Clause License Condition notice. + * + * Software: All X1Wallet associated files. + * License: MIT + * Licensor: HODL TECH PTE LTD + * + ****************************************************************************** */ #ifndef SESSION_UTILS @@ -28,6 +76,11 @@ #define DEVICE_RANDOM_SIZE 32 #define SESSION_RANDOM_SIZE 32 + +/** + * @brief Stores the session information + * @since v1.0.0 + */ #pragma pack(push, 1) typedef struct { uint8_t device_random[DEVICE_RANDOM_SIZE]; @@ -35,10 +88,19 @@ typedef struct { uint8_t device_id[DEVICE_SERIAL_SIZE]; uint8_t session_id[SESSION_ID_SIZE]; uint8_t public_key[33]; - uint16_t session_age; + uint32_t session_age; } Session; #pragma pack(pop) +/** + * @brief A generic message structure to send and receive authenticated + * messages + * @details + * For sending an authenticated message to server all the fields have to be + * populated. For receiving an authenticated message from the server the + * postfix1 and postfix2 fields will be ignored. + * @since v1.0.0 + */ #pragma pack(push, 1) typedef struct { uint16_t message_size; @@ -50,20 +112,81 @@ typedef struct { #pragma pack(pop) extern uint8_t session_key_derv_data[12]; +extern Session session; -bool verify_session_signature(Session *session, uint8_t *payload, uint8_t -payload_length, uint8_t *buffer); +/** + * @brief Verified the signature of the payload + * @param payload The payload to be verified + * @param payload_length The length of the payload + * @param buffer The buffer to store the signature + * @return true if the signature is verified, false otherwise + * + * @see session_init() + * @since v1.0.0 + */ +bool verify_session_signature(uint8_t *payload, uint8_t payload_length, + uint8_t *buffer); -void session_pre_init(Session *session, uint8_t *session_details_data_array); +/** + * @brief Starts the session creation process + * @details It generates the device random, derives and stores the device + * public key. It also generates the payload to be sent to the server. + * @param session_details_data_array The buffer to store the payload to be sent + * + * @see SESSION_INIT + * @since v1.0.0 + */ +void session_pre_init(uint8_t *session_details_data_array); -bool session_init(Session *session, uint8_t *session_init_details, uint8_t +/** + * @brief Completes the session creation process + * @details It verifies the server response and stores the session id and + * session random. + * @param session_init_details The server response + * @param verification_details The buffer to store the details + * to be send back to the server to confirm the verification. + * @return true if the session is created, false otherwise + * + * @see SESSION_ESTABLISH + * @since v1.0.0 + */ +bool session_init(uint8_t *session_init_details, uint8_t *verification_details); +/** + * @brief Deserializes the an authentication message to Message structure + * @param data_array The serialized message + * @param msg_size The size of the serialized message + * @param msg The Message structure to store the deserialized message + * + * @since v1.0.0 + */ void byte_array_to_session_message(uint8_t *data_array, uint16_t msg_size, Message *msg); -uint8_t session_message_to_byte_array(Message msg, uint8_t *data_array); +/** + * @brief Serializes the Message structure to an authentication message to + * be sent to the server + * @param message The Message structure to be serialized + * @param data_array The buffer to store the serialized message + * @return uint8_t The size of the serialized message + * + * @since v1.0.0 + */ +uint8_t session_message_to_byte_array(Message message, uint8_t *data_array); +/** + * @brief Generates the payload to be sent to the server. + * @details It generates the signature on the payload and appends the + * signature to the payload. It further appends the postfix1 and postfix2 + * for verification on the server side. + * @param payload The payload to be sent to the server + * @param payload_length The length of the payload + * @param signature_details The buffer to store the signature and the postfixes + * + * @see session_pre_init(), session_init() + * @since v1.0.0 + */ void append_signature(uint8_t *payload, uint8_t payload_length, uint8_t *signature_details); diff --git a/src/controller_main.c b/src/controller_main.c index 22d800605..d110012e5 100644 --- a/src/controller_main.c +++ b/src/controller_main.c @@ -171,9 +171,6 @@ Flash_Wallet wallet_for_flash; MessageData msg_data; ui_display_node *current_display_node = NULL; -// create a new session object -Session session; - Flow_level* get_flow_level() { ASSERT((&flow_level) != NULL); @@ -702,7 +699,7 @@ void desktop_listener_task(lv_task_t* data) DEVICE_SERIAL_SIZE + SIGNATURE_SIZE + POSTFIX1_SIZE + POSTFIX2_SIZE]; - session_pre_init(&session, session_details_data_array); + session_pre_init(session_details_data_array); transmit_data_to_app(SESSION_INIT_SEND_DETAILS, session_details_data_array, @@ -718,9 +715,7 @@ void desktop_listener_task(lv_task_t* data) uint8_t verification_details[DEVICE_SERIAL_SIZE + SIGNATURE_SIZE + POSTFIX1_SIZE + POSTFIX2_SIZE]; - if (!session_init(&session, - data_array, - verification_details)) { + if (!session_init(data_array, verification_details)) { LOG_CRITICAL("xxec %d:%d", false, __LINE__); comm_reject_invalid_cmd(); clear_message_received_data(); From 74a203f251c9470f3b271ba188817282a97b9ab3 Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Mon, 6 Mar 2023 12:16:44 +0530 Subject: [PATCH 08/12] chore: made the atecc data a pointer in the signing methods --- common/libraries/util/atecc_utils.c | 14 +++++++------- common/libraries/util/atecc_utils.h | 4 ++-- .../controller/device_authentication_controller.c | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/common/libraries/util/atecc_utils.c b/common/libraries/util/atecc_utils.c index 0abb163c8..0fc6f0cf7 100644 --- a/common/libraries/util/atecc_utils.c +++ b/common/libraries/util/atecc_utils.c @@ -42,14 +42,14 @@ void helper_get_gendig_hash(atecc_slot_define_t slot, uint8_t *data, uint8_t *digest, uint8_t *postfix, - atecc_data_t atecc_value) { + atecc_data_t *atecc_value) { if (digest == NULL || data == NULL || postfix == NULL) { return; } uint8_t tempkey_init[96] = {0}; uint8_t atecc_serial[9]; - atecc_value.status = atcab_read_serial_number(atecc_serial); + atecc_value->status = atcab_read_serial_number(atecc_serial); memcpy(tempkey_init, data, 32); postfix[0] = tempkey_init[32] = 0x15; postfix[1] = tempkey_init[33] = 0x02; @@ -66,7 +66,7 @@ ATCA_STATUS helper_sign_internal_msg(struct atca_sign_internal_in_out *param, uint8_t mode, uint8_t priv_key_id, uint8_t data_key_id, - atecc_data_t atecc_value) { + atecc_data_t *atecc_value) { uint8_t msg[55]; uint8_t cfg[128] = {0}, sn[9] = {0}; atca_temp_key_t temp_key = {0}; @@ -76,11 +76,11 @@ ATCA_STATUS helper_sign_internal_msg(struct atca_sign_internal_in_out *param, temp_key.valid = 1; temp_key.source_flag = 1; - atecc_value.status = atcab_read_config_zone(cfg); + atecc_value->status = atcab_read_config_zone(cfg); memcpy(temp_key.value, param->message, 32); param->temp_key = &temp_key; helper_config_to_sign_internal(ATECC608A, param, cfg); - atecc_value.status = atcab_read_serial_number(sn); + atecc_value->status = atcab_read_serial_number(sn); if (param == NULL || param->temp_key == NULL) { return ATCA_BAD_PARAM; @@ -201,7 +201,7 @@ auth_data_t atecc_sign(uint8_t *hash) { helper_get_gendig_hash(slot_5_challenge, challenge_no, tempkey_hash, auth_challenge_packet - .postfix1, atecc_data); + .postfix1, &atecc_data); sign_internal_param.message = tempkey_hash; sign_internal_param.digest = final_hash; @@ -209,7 +209,7 @@ auth_data_t atecc_sign(uint8_t *hash) { helper_sign_internal_msg(&sign_internal_param, SIGN_MODE_INTERNAL, slot_2_auth_key, slot_5_challenge, - atecc_data); + &atecc_data); memset(challenge_no, 0, sizeof(challenge_no)); atecc_data.status = atcab_write_enc(slot_5_challenge, diff --git a/common/libraries/util/atecc_utils.h b/common/libraries/util/atecc_utils.h index 63e96abb5..94afccaee 100644 --- a/common/libraries/util/atecc_utils.h +++ b/common/libraries/util/atecc_utils.h @@ -82,7 +82,7 @@ void helper_get_gendig_hash(atecc_slot_define_t slot, uint8_t *data, uint8_t *digest, uint8_t *postfix, - atecc_data_t atecc_value); + atecc_data_t *atecc_value); /** * @brief @@ -101,7 +101,7 @@ ATCA_STATUS helper_sign_internal_msg(struct atca_sign_internal_in_out *param, uint8_t mode, uint8_t priv_key_id, uint8_t data_key_id, - atecc_data_t atecc_value); + atecc_data_t *atecc_value); auth_data_t atecc_sign(uint8_t *hash); diff --git a/src/level_four/core/controller/device_authentication_controller.c b/src/level_four/core/controller/device_authentication_controller.c index dd50343d4..e20df0ac7 100644 --- a/src/level_four/core/controller/device_authentication_controller.c +++ b/src/level_four/core/controller/device_authentication_controller.c @@ -140,14 +140,14 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ } helper_get_gendig_hash(slot_8_serial, auth_serial_packet - .serial, tempkey_hash, auth_serial_packet.postfix1, atecc_data); + .serial, tempkey_hash, auth_serial_packet.postfix1, &atecc_data); sign_internal_param.message=tempkey_hash; sign_internal_param.digest=final_hash; helper_sign_internal_msg(&sign_internal_param, SIGN_MODE_INTERNAL, - slot_2_auth_key, slot_8_serial, atecc_data); + slot_2_auth_key, slot_8_serial, &atecc_data); { uint8_t result = ecdsa_verify_digest(&nist256p1, get_auth_public_key(), auth_serial_packet.signature, sign_internal_param.digest); if (atecc_data.status != ATCA_SUCCESS || result != 0){ @@ -225,7 +225,7 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ helper_get_gendig_hash(slot_5_challenge, challenge_no, tempkey_hash, auth_challenge_packet - .postfix1, atecc_data); + .postfix1, &atecc_data); sign_internal_param.message=tempkey_hash; sign_internal_param.digest=final_hash; @@ -233,7 +233,7 @@ void __attribute__((optimize("O0"))) device_authentication_controller(){ helper_sign_internal_msg(&sign_internal_param, SIGN_MODE_INTERNAL, slot_2_auth_key, slot_5_challenge, - atecc_data); + &atecc_data); //overwrite challenge slot to signature generation on same challenge memset(challenge_no, 0, sizeof(challenge_no)); From 54bef20ccb662f886654c476c63ba60c05b9428e Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Mon, 6 Mar 2023 18:20:18 +0530 Subject: [PATCH 09/12] chore: updated session key derivation dataype --- common/libraries/util/session_utils.c | 8 ++++---- common/libraries/util/session_utils.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/common/libraries/util/session_utils.c b/common/libraries/util/session_utils.c index a7aa4f25d..7ca4b0a0c 100644 --- a/common/libraries/util/session_utils.c +++ b/common/libraries/util/session_utils.c @@ -61,7 +61,7 @@ #include "session_utils.h" #include "controller_level_four.h" -uint8_t session_key_derv_data[12] = {0}; +uint32_t session_key_derv_data[3] = {0}; Session session; static void derive_public_key() { @@ -77,13 +77,13 @@ static void derive_public_key() { &session_node, NULL); - index = read_be(session_key_derv_data); + index = session_key_derv_data[0]; hdnode_public_ckd(&session_node, index); - index = read_be(session_key_derv_data + 4); + index = session_key_derv_data[1]; hdnode_public_ckd(&session_node, index); - index = read_be(session_key_derv_data + 8); + index = session_key_derv_data[2]; hdnode_public_ckd(&session_node, index); memcpy(session.public_key, session_node.public_key, diff --git a/common/libraries/util/session_utils.h b/common/libraries/util/session_utils.h index 6693fe8d4..8f2b80a51 100644 --- a/common/libraries/util/session_utils.h +++ b/common/libraries/util/session_utils.h @@ -111,7 +111,7 @@ typedef struct { } Message; #pragma pack(pop) -extern uint8_t session_key_derv_data[12]; +extern uint32_t session_key_derv_data[3]; extern Session session; /** From 561b3fa29328bfea8875c90dfa71f058fb03d025 Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Fri, 17 Mar 2023 08:34:44 +0530 Subject: [PATCH 10/12] chore: fix license comments in source and header file --- common/libraries/util/atecc_utils.c | 59 +++++++++++++++++++++++++++ common/libraries/util/session_utils.h | 51 +---------------------- 2 files changed, 60 insertions(+), 50 deletions(-) diff --git a/common/libraries/util/atecc_utils.c b/common/libraries/util/atecc_utils.c index 0fc6f0cf7..e63d5bbc4 100644 --- a/common/libraries/util/atecc_utils.c +++ b/common/libraries/util/atecc_utils.c @@ -1,3 +1,62 @@ +/** + * @file atecc_utils.c + * @author Cypherock X1 Team + * @brief Definition of the ATECC signature helper functions used for + * signing and verifying the messages using the ATECC608A chip. + * + * @copyright Copyright (c) 2022 HODL TECH PTE LTD + *
You may obtain a copy of license at https://mitcc.org/ + * + ****************************************************************************** + * @attention + * + * (c) Copyright 2022 by HODL TECH PTE LTD + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject + * to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * + * "Commons Clause" License Condition v1.0 + * + * The Software is provided to you by the Licensor under the License, + * as defined below, subject to the following condition. + * + * Without limiting other conditions in the License, the grant of + * rights under the License will not include, and the License does not + * grant to you, the right to Sell the Software. + * + * For purposes of the foregoing, "Sell" means practicing any or all + * of the rights granted to you under the License to provide to third + * parties, for a fee or other consideration (including without + * limitation fees for hosting or consulting/ support services related + * to the Software), a product or service whose value derives, entirely + * or substantially, from the functionality of the Software. Any license + * notice or attribution required by the License must also include + * this Commons Clause License Condition notice. + * + * Software: All X1Wallet associated files. + * License: MIT + * Licensor: HODL TECH PTE LTD + * + ****************************************************************************** + */ + #include "atecc_utils.h" #include "sha2.h" #include "flash_api.h" diff --git a/common/libraries/util/session_utils.h b/common/libraries/util/session_utils.h index 8f2b80a51..98d4f48b0 100644 --- a/common/libraries/util/session_utils.h +++ b/common/libraries/util/session_utils.h @@ -7,55 +7,6 @@ * * @copyright Copyright (c) 2022 HODL TECH PTE LTD *
You may obtain a copy of license at https://mitcc.org/ - * - ****************************************************************************** - * @attention - * - * (c) Copyright 2022 by HODL TECH PTE LTD - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject - * to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * - * "Commons Clause" License Condition v1.0 - * - * The Software is provided to you by the Licensor under the License, - * as defined below, subject to the following condition. - * - * Without limiting other conditions in the License, the grant of - * rights under the License will not include, and the License does not - * grant to you, the right to Sell the Software. - * - * For purposes of the foregoing, "Sell" means practicing any or all - * of the rights granted to you under the License to provide to third - * parties, for a fee or other consideration (including without - * limitation fees for hosting or consulting/ support services related - * to the Software), a product or service whose value derives, entirely - * or substantially, from the functionality of the Software. Any license - * notice or attribution required by the License must also include - * this Commons Clause License Condition notice. - * - * Software: All X1Wallet associated files. - * License: MIT - * Licensor: HODL TECH PTE LTD - * - ****************************************************************************** */ #ifndef SESSION_UTILS @@ -111,7 +62,7 @@ typedef struct { } Message; #pragma pack(pop) -extern uint32_t session_key_derv_data[3]; +extern const uint32_t session_key_derv_data[3]; extern Session session; /** From d1f1eba3ac943ba78de74d21d84b6024de4ed6d3 Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Fri, 17 Mar 2023 08:35:11 +0530 Subject: [PATCH 11/12] chore: remove unnecessary value from log in session establishment --- src/controller_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controller_main.c b/src/controller_main.c index d110012e5..cb5e2dcf2 100644 --- a/src/controller_main.c +++ b/src/controller_main.c @@ -716,7 +716,7 @@ void desktop_listener_task(lv_task_t* data) SIGNATURE_SIZE + POSTFIX1_SIZE + POSTFIX2_SIZE]; if (!session_init(data_array, verification_details)) { - LOG_CRITICAL("xxec %d:%d", false, __LINE__); + LOG_CRITICAL("xxec %d", __LINE__); comm_reject_invalid_cmd(); clear_message_received_data(); } else { From 2e515be58dedc6f6550c324a32e271601383566a Mon Sep 17 00:00:00 2001 From: Arnab Sen Date: Fri, 17 Mar 2023 08:35:35 +0530 Subject: [PATCH 12/12] chore: make session key derivation data constant --- common/libraries/util/session_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/libraries/util/session_utils.c b/common/libraries/util/session_utils.c index 7ca4b0a0c..27a116880 100644 --- a/common/libraries/util/session_utils.c +++ b/common/libraries/util/session_utils.c @@ -61,7 +61,7 @@ #include "session_utils.h" #include "controller_level_four.h" -uint32_t session_key_derv_data[3] = {0}; +const uint32_t session_key_derv_data[3] = {0}; Session session; static void derive_public_key() {