From d01ea978c69205300c8afa51d4e778f2d187e5ff Mon Sep 17 00:00:00 2001 From: cesaaargm Date: Sun, 3 Dec 2023 17:45:15 +0100 Subject: [PATCH] [TAS-108] first implementation of verify_dsm_pkr() * osnma_msg_receiver : leafs computation without loop and root comparison * gnss_crypto : getMerkleRoot() to return d_x_4_0 --- src/core/libs/osnma_msg_receiver.cc | 48 +++++++++++++++++++++--- src/core/libs/osnma_msg_receiver.h | 2 +- src/core/system_parameters/gnss_crypto.h | 4 ++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/core/libs/osnma_msg_receiver.cc b/src/core/libs/osnma_msg_receiver.cc index 8f157414f..cf7e3adc9 100644 --- a/src/core/libs/osnma_msg_receiver.cc +++ b/src/core/libs/osnma_msg_receiver.cc @@ -416,8 +416,7 @@ void osnma_msg_receiver::process_dsm_message(const std::vector& dsm_msg << ", TOW=" << static_cast(d_osnma_data.d_dsm_kroot_message.towh_k) * 3600 << " received" << std::endl; // C: NPK verification against Merkle tree root. - std::vector m_0; - d_public_key_verified = verify_dsm_pkr(d_osnma_data.d_dsm_pkr_message, m_0); + d_public_key_verified = verify_dsm_pkr(d_osnma_data.d_dsm_pkr_message); } } else @@ -735,10 +734,49 @@ void osnma_msg_receiver::process_mack_message(const std::shared_ptr& // C: TODO - where m = (PRNd || PRNa || GSTsf || CTR || NMAS || NavData || P) } -bool osnma_msg_receiver::verify_dsm_pkr(DSM_PKR_message message, std::vector m0) +bool osnma_msg_receiver::verify_dsm_pkr(DSM_PKR_message message) { - // TODO concatenate message + // TODO create leafe base message m_i // TODO create function for recursively apply hash - return false; + // build base leaf m_i +// auto leaf = message.mid; + std::vector m_i; + m_i.reserve(2 + message.npk.size()); + m_i[0] = message.npkt; + m_i[1] = message.npktid; + for (uint8_t i = 2; i < m_i.size(); i++) + { + m_i.push_back(message.npk[i]); + } + + // compute intermediate leafs' values + std::vector x_0,x_1,x_2,x_3,x_4; +// uint8_t k = 0; + x_0 = d_crypto->computeSHA256(m_i); + x_0.insert(x_0.end(),message.itn.begin(),&message.itn[31]); + x_1 = d_crypto->computeSHA256(x_0); + x_1.insert(x_1.end(),&message.itn[32],&message.itn[63]); + x_2 = d_crypto->computeSHA256(x_1); + x_2.insert(x_2.end(),&message.itn[64],&message.itn[95]); + x_3 = d_crypto->computeSHA256(x_2); + x_3.insert(x_3.end(),&message.itn[96],&message.itn[127]); + // root leaf computation + x_4 = d_crypto->computeSHA256(x_3); + + // C: d_crypto->getMerkleRoot([m_0:m_15]) I realised I could have done this... + // C: ... but why computing all the possible results? I have only one leaf in each osnma message... + // verify that computed root matches merkle root + + if(x_4 == d_crypto->getMerkleRoot()) + { + std::cout << "Galileo OSNMA: DSM-PKR verified successfully! " << std::endl; + return true; + // C: NPK verification against Merkle tree root. + } + else + { + std::cout << "Galileo OSNMA: DSM-PKR verification unsuccessful !" << std::endl; + return false; + } } diff --git a/src/core/libs/osnma_msg_receiver.h b/src/core/libs/osnma_msg_receiver.h index 89522bc69..2c9cedcc7 100644 --- a/src/core/libs/osnma_msg_receiver.h +++ b/src/core/libs/osnma_msg_receiver.h @@ -65,7 +65,7 @@ private: void read_dsm_header(uint8_t dsm_header); void read_dsm_block(const std::shared_ptr& osnma_msg); void process_dsm_message(const std::vector& dsm_msg, const std::shared_ptr& osnma_msg); - bool verify_dsm_pkr(DSM_PKR_message message, std::vector input_message); + bool verify_dsm_pkr(DSM_PKR_message message); void read_mack_block(const std::shared_ptr& osnma_msg); void read_mack_header(); void read_mack_body(); diff --git a/src/core/system_parameters/gnss_crypto.h b/src/core/system_parameters/gnss_crypto.h index c544020a9..50047c440 100644 --- a/src/core/system_parameters/gnss_crypto.h +++ b/src/core/system_parameters/gnss_crypto.h @@ -50,6 +50,10 @@ public: void readPublicKeyFromPEM(const std::string& pemFilePath); void read_merkle_xml(const std::string& merkleFilePath); std::vector getMerkleRoot(const std::vector>& merkle) const; + std::vector getMerkleRoot() const + { + return d_x_4_0; + } // void set_public_key(const std::vector& publickey);