1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-12-14 04:00:34 +00:00

Improve SHA256 computation

This commit is contained in:
Carles Fernandez 2023-06-02 11:33:58 +02:00
parent 7147959186
commit 9de1fd39ca
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
4 changed files with 70 additions and 25 deletions

View File

@ -22,7 +22,6 @@ set(CORE_LIBS_SOURCES
nav_message_udp_sink.cc
galileo_tow_map.cc
osnma_msg_receiver.cc
sha256.cc
)
set(CORE_LIBS_HEADERS
@ -40,7 +39,6 @@ set(CORE_LIBS_HEADERS
nav_message_monitor.h
galileo_tow_map.h
osnma_msg_receiver.h
sha256.h
)
if(ENABLE_FPGA)
@ -104,6 +102,21 @@ target_link_libraries(core_libs
Pugixml::pugixml
)
target_link_libraries(core_libs
PRIVATE
${GNUTLS_LIBRARIES}
${GNUTLS_OPENSSL_LIBRARY}
)
target_include_directories(core_libs
PRIVATE
${GNUTLS_INCLUDE_DIR}
)
if(OPENSSL_FOUND)
target_compile_definitions(core_libs PRIVATE -DUSE_OPENSSL_FALLBACK=1)
endif()
if(USE_GENERIC_LAMBDAS AND NOT GNURADIO_USES_STD_POINTERS)
target_link_libraries(core_libs PUBLIC Boost::headers)
else()

View File

@ -31,7 +31,6 @@
#include <string>
#include <typeinfo> // for typeid
#if HAS_GENERIC_LAMBDA
#else
#include <boost/bind/bind.hpp>
@ -45,6 +44,13 @@ namespace wht = boost;
namespace wht = std;
#endif
#if USE_OPENSSL_FALLBACK
#include <openssl/sha.h>
#else
#include <gnutls/crypto.h>
#include <gnutls/gnutls.h>
#endif
osnma_msg_receiver_sptr osnma_msg_receiver_make()
{
return osnma_msg_receiver_sptr(new osnma_msg_receiver());
@ -202,17 +208,17 @@ void osnma_msg_receiver::process_dsm_message(const std::vector<uint8_t>& dsm_msg
LOG(WARNING) << "OSNMA: DSM-KROOT message received.";
// DSM-KROOT message
d_osnma_data.d_dsm_kroot_message.nb_dk = get_number_blocks_index(dsm_msg[0]);
d_osnma_data.d_dsm_kroot_message.pkid = get_pkid(dsm_msg); // (dsm_msg[0] & 0b00001111);
d_osnma_data.d_dsm_kroot_message.cidkr = get_cidkr(dsm_msg); // (dsm_msg[1] & 0b11000000) >> 6;
d_osnma_data.d_dsm_kroot_message.reserved1 = get_dsm_reserved1(dsm_msg); // (dsm_msg[1] & 0b00110000) >> 4;
d_osnma_data.d_dsm_kroot_message.hf = get_hf(dsm_msg); // (dsm_msg[1] & 0b00001100) >> 2;
d_osnma_data.d_dsm_kroot_message.mf = get_mf(dsm_msg); // (dsm_msg[1] & 0b00000011);
d_osnma_data.d_dsm_kroot_message.ks = get_ks(dsm_msg); // (dsm_msg[2] & 0b11110000) >> 4;
d_osnma_data.d_dsm_kroot_message.ts = get_ts(dsm_msg); // (dsm_msg[2] & 0b00001111);
d_osnma_data.d_dsm_kroot_message.maclt = get_maclt(dsm_msg); // dsm_msg[3];
d_osnma_data.d_dsm_kroot_message.reserved = get_dsm_reserved(dsm_msg); // (dsm_msg[4] & 0b11110000) >> 4;
d_osnma_data.d_dsm_kroot_message.wn_k = get_wn_k(dsm_msg); // static_cast<uint16_t>((dsm_msg[4] & 0b00001111) << 8) + static_cast<uint16_t>(dsm_msg[5]);
d_osnma_data.d_dsm_kroot_message.towh_k = get_towh_k(dsm_msg); // dsm_msg[6];
d_osnma_data.d_dsm_kroot_message.pkid = get_pkid(dsm_msg);
d_osnma_data.d_dsm_kroot_message.cidkr = get_cidkr(dsm_msg);
d_osnma_data.d_dsm_kroot_message.reserved1 = get_dsm_reserved1(dsm_msg);
d_osnma_data.d_dsm_kroot_message.hf = get_hf(dsm_msg);
d_osnma_data.d_dsm_kroot_message.mf = get_mf(dsm_msg);
d_osnma_data.d_dsm_kroot_message.ks = get_ks(dsm_msg);
d_osnma_data.d_dsm_kroot_message.ts = get_ts(dsm_msg);
d_osnma_data.d_dsm_kroot_message.maclt = get_maclt(dsm_msg);
d_osnma_data.d_dsm_kroot_message.reserved = get_dsm_reserved(dsm_msg);
d_osnma_data.d_dsm_kroot_message.wn_k = get_wn_k(dsm_msg);
d_osnma_data.d_dsm_kroot_message.towh_k = get_towh_k(dsm_msg);
d_osnma_data.d_dsm_kroot_message.alpha = get_alpha(dsm_msg);
uint16_t bytes_lk = get_lk_bits(d_osnma_data.d_dsm_kroot_message.ks) / 8;
@ -254,12 +260,12 @@ void osnma_msg_receiver::process_dsm_message(const std::vector<uint8_t>& dsm_msg
{
M.push_back(dsm_msg[13 + bytes_lk + k]);
}
sha256.update(&M[0], M.size());
uint8_t* digest = sha256.digest();
std::vector<uint8_t> hash = computeSHA256(M);
std::vector<uint8_t> p_dk_computed;
for (uint16_t i = 0; i < l_pdk_bytes; i++)
{
p_dk_computed.push_back(digest[i]);
p_dk_computed.push_back(hash[i]);
}
if (d_osnma_data.d_dsm_kroot_message.p_dk == p_dk_computed)
{
@ -365,4 +371,30 @@ void osnma_msg_receiver::read_mack_key()
void osnma_msg_receiver::read_mack_padding()
{
}
}
std::vector<uint8_t> osnma_msg_receiver::computeSHA256(const std::vector<uint8_t>& input)
{
std::vector<uint8_t> output_vector{32};
uint8_t output[32];
const uint8_t* input_ptr = input.data();
size_t inputLength = input.size();
#if USE_OPENSSL_FALLBACK
SHA256_CTX sha256Context;
SHA256_Init(&sha256Context);
SHA256_Update(&sha256Context, input_ptr, inputLength);
SHA256_Final(output, &sha256Context);
#else
gnutls_hash_hd_t hashHandle;
gnutls_hash_init(&hashHandle, GNUTLS_DIG_SHA256);
gnutls_hash(hashHandle, input_ptr, inputLength);
gnutls_hash_output(hashHandle, output);
gnutls_hash_deinit(hashHandle, output);
#endif
for (int i = 0; i < 32; i++)
{
output_vector[i] = output[i];
}
return output_vector;
}

View File

@ -22,11 +22,10 @@
#include "galileo_inav_message.h" // for OSNMA_msg
#include "gnss_block_interface.h" // for gnss_shared_ptr
#include "osnma_data.h" // for OSNMA_data
#include "sha256.h"
#include <gnuradio/block.h> // for gr::block
#include <pmt/pmt.h> // for pmt::pmt_t
#include <array> // for std::array
#include <memory> // for std::shared_ptr
#include <gnuradio/block.h> // for gr::block
#include <pmt/pmt.h> // for pmt::pmt_t
#include <array> // for std::array
#include <memory> // for std::shared_ptr
#include <vector>
/** \addtogroup Core
@ -67,7 +66,8 @@ private:
void read_mack_info_and_tags();
void read_mack_key();
void read_mack_padding();
SHA256 sha256;
std::vector<uint8_t> computeSHA256(const std::vector<uint8_t>& input);
std::array<std::array<uint8_t, 256>, 16> d_dsm_message{};
std::array<std::array<uint8_t, 16>, 16> d_dsm_id_received{};

View File

@ -308,7 +308,7 @@ uint16_t get_lk_bits(uint8_t ks)
std::vector<uint8_t> get_kroot(const std::vector<uint8_t>& dsm_msg, uint16_t bytes_lk)
{
std::vector<uint8_t> kroot = std::vector<uint8_t>(bytes_lk, 0);
if (dsm_msg.size() > 13 + bytes_lk)
if (dsm_msg.size() > static_cast<uint64_t>(13 + bytes_lk))
{
for (uint16_t k = 0; k < bytes_lk; k++)
{