mirror of
https://github.com/gnss-sdr/gnss-sdr
synced 2025-01-31 11:19:18 +00:00
Improve consistency of public API member function names in Gnss_Crypto
This commit is contained in:
parent
cb0c0d02fb
commit
d687193156
@ -457,11 +457,11 @@ void osnma_msg_receiver::process_dsm_message(const std::vector<uint8_t>& dsm_msg
|
|||||||
std::vector<uint8_t> hash;
|
std::vector<uint8_t> hash;
|
||||||
if (d_osnma_data.d_dsm_kroot_message.hf == 0) // Table 8.
|
if (d_osnma_data.d_dsm_kroot_message.hf == 0) // Table 8.
|
||||||
{
|
{
|
||||||
hash = d_crypto->computeSHA256(MSG);
|
hash = d_crypto->compute_SHA_256(MSG);
|
||||||
}
|
}
|
||||||
else if (d_osnma_data.d_dsm_kroot_message.hf == 2)
|
else if (d_osnma_data.d_dsm_kroot_message.hf == 2)
|
||||||
{
|
{
|
||||||
hash = d_crypto->computeSHA3_256(MSG);
|
hash = d_crypto->compute_SHA3_256(MSG);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -483,7 +483,7 @@ void osnma_msg_receiver::process_dsm_message(const std::vector<uint8_t>& dsm_msg
|
|||||||
<< ", WN=" << static_cast<uint32_t>(d_osnma_data.d_dsm_kroot_message.wn_k)
|
<< ", WN=" << static_cast<uint32_t>(d_osnma_data.d_dsm_kroot_message.wn_k)
|
||||||
<< ", TOW=" << static_cast<uint32_t>(d_osnma_data.d_dsm_kroot_message.towh_k) * 3600;
|
<< ", TOW=" << static_cast<uint32_t>(d_osnma_data.d_dsm_kroot_message.towh_k) * 3600;
|
||||||
local_time_verification(osnma_msg);
|
local_time_verification(osnma_msg);
|
||||||
d_kroot_verified = d_crypto->verify_signature(message, d_osnma_data.d_dsm_kroot_message.ds);
|
d_kroot_verified = d_crypto->verify_signature_ecdsa_p256(message, d_osnma_data.d_dsm_kroot_message.ds);
|
||||||
if (d_kroot_verified)
|
if (d_kroot_verified)
|
||||||
{
|
{
|
||||||
std::cout << "Galileo OSNMA: KROOT authentication successful!" << std::endl;
|
std::cout << "Galileo OSNMA: KROOT authentication successful!" << std::endl;
|
||||||
@ -1040,7 +1040,7 @@ bool osnma_msg_receiver::verify_dsm_pkr(DSM_PKR_message message)
|
|||||||
|
|
||||||
computed_merkle_root = compute_merke_root(message, base_leaf);
|
computed_merkle_root = compute_merke_root(message, base_leaf);
|
||||||
|
|
||||||
if (computed_merkle_root == d_crypto->getMerkleRoot())
|
if (computed_merkle_root == d_crypto->get_merkle_root())
|
||||||
{
|
{
|
||||||
LOG(INFO) << "Galileo OSNMA: DSM-PKR verification :: SUCCESS!." << std::endl;
|
LOG(INFO) << "Galileo OSNMA: DSM-PKR verification :: SUCCESS!." << std::endl;
|
||||||
return true;
|
return true;
|
||||||
@ -1053,7 +1053,7 @@ bool osnma_msg_receiver::verify_dsm_pkr(DSM_PKR_message message)
|
|||||||
}
|
}
|
||||||
std::vector<uint8_t> osnma_msg_receiver::compute_merke_root(const DSM_PKR_message& dsm_pkr_message, const std::vector<uint8_t>& m_i) const
|
std::vector<uint8_t> osnma_msg_receiver::compute_merke_root(const DSM_PKR_message& dsm_pkr_message, const std::vector<uint8_t>& m_i) const
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> x_next, x_current = d_crypto->computeSHA256(m_i);
|
std::vector<uint8_t> x_next, x_current = d_crypto->compute_SHA_256(m_i);
|
||||||
for (size_t i = 0; i < 4; i++)
|
for (size_t i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
x_next.clear();
|
x_next.clear();
|
||||||
@ -1073,7 +1073,7 @@ std::vector<uint8_t> osnma_msg_receiver::compute_merke_root(const DSM_PKR_messag
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Compute the next node.
|
// Compute the next node.
|
||||||
x_current = d_crypto->computeSHA256(x_next);
|
x_current = d_crypto->compute_SHA_256(x_next);
|
||||||
}
|
}
|
||||||
return x_current;
|
return x_current;
|
||||||
}
|
}
|
||||||
@ -1116,11 +1116,11 @@ bool osnma_msg_receiver::verify_tag(Tag& tag)
|
|||||||
|
|
||||||
if (d_osnma_data.d_dsm_kroot_message.mf == 0) // C: HMAC-SHA-256
|
if (d_osnma_data.d_dsm_kroot_message.mf == 0) // C: HMAC-SHA-256
|
||||||
{
|
{
|
||||||
mac = d_crypto->computeHMAC_SHA_256(applicable_key, m);
|
mac = d_crypto->compute_HMAC_SHA_256(applicable_key, m);
|
||||||
}
|
}
|
||||||
else if (d_osnma_data.d_dsm_kroot_message.mf == 1) // C: CMAC-AES
|
else if (d_osnma_data.d_dsm_kroot_message.mf == 1) // C: CMAC-AES
|
||||||
{
|
{
|
||||||
mac = d_crypto->computeCMAC_AES(applicable_key, m);
|
mac = d_crypto->compute_CMAC_AES(applicable_key, m);
|
||||||
}
|
}
|
||||||
|
|
||||||
// truncate the computed mac: trunc(l_t, mac(K,m)) Eq. 23 ICD
|
// truncate the computed mac: trunc(l_t, mac(K,m)) Eq. 23 ICD
|
||||||
@ -1484,11 +1484,11 @@ bool osnma_msg_receiver::verify_macseq(const MACK_message& mack)
|
|||||||
std::vector<uint8_t> mac;
|
std::vector<uint8_t> mac;
|
||||||
if (d_osnma_data.d_dsm_kroot_message.mf == 0) // C: HMAC-SHA-256
|
if (d_osnma_data.d_dsm_kroot_message.mf == 0) // C: HMAC-SHA-256
|
||||||
{
|
{
|
||||||
mac = d_crypto->computeHMAC_SHA_256(applicable_key, m);
|
mac = d_crypto->compute_HMAC_SHA_256(applicable_key, m);
|
||||||
}
|
}
|
||||||
else if (d_osnma_data.d_dsm_kroot_message.mf == 1) // C: CMAC-AES
|
else if (d_osnma_data.d_dsm_kroot_message.mf == 1) // C: CMAC-AES
|
||||||
{
|
{
|
||||||
mac = d_crypto->computeCMAC_AES(applicable_key, m);
|
mac = d_crypto->compute_CMAC_AES(applicable_key, m);
|
||||||
}
|
}
|
||||||
// Truncate the twelve MSBits and compare with received MACSEQ
|
// Truncate the twelve MSBits and compare with received MACSEQ
|
||||||
uint16_t mac_msb = 0;
|
uint16_t mac_msb = 0;
|
||||||
@ -1598,11 +1598,11 @@ std::vector<uint8_t> osnma_msg_receiver::hash_chain(uint32_t num_of_hashes_neede
|
|||||||
std::vector<uint8_t> hash;
|
std::vector<uint8_t> hash;
|
||||||
if (d_osnma_data.d_dsm_kroot_message.hf == 0) // Table 8.
|
if (d_osnma_data.d_dsm_kroot_message.hf == 0) // Table 8.
|
||||||
{
|
{
|
||||||
hash = d_crypto->computeSHA256(msg);
|
hash = d_crypto->compute_SHA_256(msg);
|
||||||
}
|
}
|
||||||
else if (d_osnma_data.d_dsm_kroot_message.hf == 2)
|
else if (d_osnma_data.d_dsm_kroot_message.hf == 2)
|
||||||
{
|
{
|
||||||
hash = d_crypto->computeSHA3_256(msg);
|
hash = d_crypto->compute_SHA3_256(msg);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1729,11 +1729,11 @@ std::vector<MACK_tag_and_info> osnma_msg_receiver::verify_macseq_new(const MACK_
|
|||||||
std::vector<uint8_t> mac;
|
std::vector<uint8_t> mac;
|
||||||
if (d_osnma_data.d_dsm_kroot_message.mf == 0) // C: HMAC-SHA-256
|
if (d_osnma_data.d_dsm_kroot_message.mf == 0) // C: HMAC-SHA-256
|
||||||
{
|
{
|
||||||
mac = d_crypto->computeHMAC_SHA_256(applicable_key, m);
|
mac = d_crypto->compute_HMAC_SHA_256(applicable_key, m);
|
||||||
}
|
}
|
||||||
else if (d_osnma_data.d_dsm_kroot_message.mf == 1) // C: CMAC-AES
|
else if (d_osnma_data.d_dsm_kroot_message.mf == 1) // C: CMAC-AES
|
||||||
{
|
{
|
||||||
mac = d_crypto->computeCMAC_AES(applicable_key, m);
|
mac = d_crypto->compute_CMAC_AES(applicable_key, m);
|
||||||
}
|
}
|
||||||
// Truncate the twelve MSBits and compare with received MACSEQ
|
// Truncate the twelve MSBits and compare with received MACSEQ
|
||||||
uint16_t mac_msb = 0;
|
uint16_t mac_msb = 0;
|
||||||
|
@ -133,9 +133,73 @@ bool Gnss_Crypto::have_public_key() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Gnss_Crypto::verify_signature(const std::vector<uint8_t>& message, const std::vector<uint8_t>& signature) const
|
bool Gnss_Crypto::store_public_key(const std::string& pubKeyFilePath) const
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> digest = this->computeSHA256(message);
|
if (!have_public_key())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::ofstream pubKeyFile(pubKeyFilePath, std::ios::binary);
|
||||||
|
if (!pubKeyFile.is_open())
|
||||||
|
{
|
||||||
|
LOG(INFO) << "Unable to open file: " << pubKeyFilePath;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#if USE_GNUTLS_FALLBACK
|
||||||
|
gnutls_datum_t pem_data;
|
||||||
|
#if HAVE_GNUTLS_PUBKEY_EXPORT2
|
||||||
|
int ret = gnutls_pubkey_export2(d_PublicKey, GNUTLS_X509_FMT_PEM, &pem_data);
|
||||||
|
#else
|
||||||
|
size_t output_stata_size;
|
||||||
|
int ret = gnutls_pubkey_export(d_PublicKey, GNUTLS_X509_FMT_PEM, &pem_data, &output_stata_size);
|
||||||
|
#endif
|
||||||
|
if (ret != GNUTLS_E_SUCCESS)
|
||||||
|
{
|
||||||
|
LOG(INFO) << "GnuTLS: Failed to export public key: " << gnutls_strerror(ret);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKeyFile.write((const char*)pem_data.data, pem_data.size);
|
||||||
|
pubKeyFile.close();
|
||||||
|
gnutls_free(pem_data.data);
|
||||||
|
#else // OpenSSL
|
||||||
|
BIO* bio = BIO_new(BIO_s_mem());
|
||||||
|
if (!bio)
|
||||||
|
{
|
||||||
|
LOG(INFO) << "OpenSSL: Failed to create BIO";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#if USE_OPENSSL_3
|
||||||
|
if (!PEM_write_bio_PUBKEY(bio, d_PublicKey))
|
||||||
|
#else // OpenSSL 1.x
|
||||||
|
if (!PEM_write_bio_EC_PUBKEY(bio, d_PublicKey))
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
LOG(INFO) << "OpenSSL: Failed to write public key to BIO";
|
||||||
|
BIO_free(bio);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* bio_data;
|
||||||
|
auto bio_len = BIO_get_mem_data(bio, &bio_data);
|
||||||
|
if (bio_len <= 0)
|
||||||
|
{
|
||||||
|
LOG(INFO) << "OpenSSL: Failed to get BIO data";
|
||||||
|
BIO_free(bio);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKeyFile.write(bio_data, bio_len);
|
||||||
|
pubKeyFile.close();
|
||||||
|
BIO_free(bio);
|
||||||
|
#endif
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool Gnss_Crypto::verify_signature_ecdsa_p256(const std::vector<uint8_t>& message, const std::vector<uint8_t>& signature) const
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> digest = this->compute_SHA_256(message);
|
||||||
if (!have_public_key())
|
if (!have_public_key())
|
||||||
{
|
{
|
||||||
std::cerr << "Galileo OSNMA KROOT verification error: Public key is not available" << std::endl;
|
std::cerr << "Galileo OSNMA KROOT verification error: Public key is not available" << std::endl;
|
||||||
@ -396,130 +460,7 @@ bool Gnss_Crypto::verify_signature_ecdsa_p521(const std::vector<uint8_t>& messag
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Gnss_Crypto::store_public_key(const std::string& pubKeyFilePath) const
|
std::vector<uint8_t> Gnss_Crypto::compute_SHA_256(const std::vector<uint8_t>& input) const
|
||||||
{
|
|
||||||
if (!have_public_key())
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::ofstream pubKeyFile(pubKeyFilePath, std::ios::binary);
|
|
||||||
if (!pubKeyFile.is_open())
|
|
||||||
{
|
|
||||||
LOG(INFO) << "Unable to open file: " << pubKeyFilePath;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#if USE_GNUTLS_FALLBACK
|
|
||||||
gnutls_datum_t pem_data;
|
|
||||||
#if HAVE_GNUTLS_PUBKEY_EXPORT2
|
|
||||||
int ret = gnutls_pubkey_export2(d_PublicKey, GNUTLS_X509_FMT_PEM, &pem_data);
|
|
||||||
#else
|
|
||||||
size_t output_stata_size;
|
|
||||||
int ret = gnutls_pubkey_export(d_PublicKey, GNUTLS_X509_FMT_PEM, &pem_data, &output_stata_size);
|
|
||||||
#endif
|
|
||||||
if (ret != GNUTLS_E_SUCCESS)
|
|
||||||
{
|
|
||||||
LOG(INFO) << "GnuTLS: Failed to export public key: " << gnutls_strerror(ret);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
pubKeyFile.write((const char*)pem_data.data, pem_data.size);
|
|
||||||
pubKeyFile.close();
|
|
||||||
gnutls_free(pem_data.data);
|
|
||||||
#else // OpenSSL
|
|
||||||
BIO* bio = BIO_new(BIO_s_mem());
|
|
||||||
if (!bio)
|
|
||||||
{
|
|
||||||
LOG(INFO) << "OpenSSL: Failed to create BIO";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#if USE_OPENSSL_3
|
|
||||||
if (!PEM_write_bio_PUBKEY(bio, d_PublicKey))
|
|
||||||
#else // OpenSSL 1.x
|
|
||||||
if (!PEM_write_bio_EC_PUBKEY(bio, d_PublicKey))
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
LOG(INFO) << "OpenSSL: Failed to write public key to BIO";
|
|
||||||
BIO_free(bio);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
char* bio_data;
|
|
||||||
auto bio_len = BIO_get_mem_data(bio, &bio_data);
|
|
||||||
if (bio_len <= 0)
|
|
||||||
{
|
|
||||||
LOG(INFO) << "OpenSSL: Failed to get BIO data";
|
|
||||||
BIO_free(bio);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
pubKeyFile.write(bio_data, bio_len);
|
|
||||||
pubKeyFile.close();
|
|
||||||
BIO_free(bio);
|
|
||||||
#endif
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
std::vector<uint8_t> Gnss_Crypto::getPublicKey() const
|
|
||||||
{
|
|
||||||
if (!have_public_key())
|
|
||||||
{
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
#if USE_GNUTLS_FALLBACK
|
|
||||||
gnutls_datum_t pem_data = {nullptr, 0};
|
|
||||||
|
|
||||||
int ret = gnutls_pubkey_export2(d_PublicKey, GNUTLS_X509_FMT_PEM, &pem_data);
|
|
||||||
if (ret != GNUTLS_E_SUCCESS)
|
|
||||||
{
|
|
||||||
LOG(INFO) << "GnuTLS: Failed to export public key to PEM format.";
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
std::vector<uint8_t> output(pem_data.data, pem_data.data + pem_data.size);
|
|
||||||
|
|
||||||
// Free the allocated memory by gnutls_pubkey_export2
|
|
||||||
gnutls_free(pem_data.data);
|
|
||||||
#else // OpenSSL
|
|
||||||
// Create a BIO for the memory buffer
|
|
||||||
BIO* mem = BIO_new(BIO_s_mem());
|
|
||||||
if (!mem)
|
|
||||||
{
|
|
||||||
LOG(INFO) << "OpenSSL: Failed to create BIO.";
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
#if USE_OPENSSL_3
|
|
||||||
if (!PEM_write_bio_PUBKEY(mem, d_PublicKey))
|
|
||||||
#else // OpenSSL 1.x
|
|
||||||
if (!PEM_write_bio_EC_PUBKEY(mem, d_PublicKey))
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
BIO_free(mem);
|
|
||||||
LOG(INFO) << "OpenSSL: Failed to write public key to PEM format.";
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the length of the data in the BIO
|
|
||||||
BUF_MEM* mem_ptr;
|
|
||||||
BIO_get_mem_ptr(mem, &mem_ptr);
|
|
||||||
|
|
||||||
// Copy the data from the BIO to a std::vector<uint8_t>
|
|
||||||
std::vector<uint8_t> output(mem_ptr->length);
|
|
||||||
memcpy(output.data(), mem_ptr->data, mem_ptr->length);
|
|
||||||
|
|
||||||
// Clean up the BIO
|
|
||||||
BIO_free(mem);
|
|
||||||
#endif
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
std::vector<uint8_t> Gnss_Crypto::getMerkleRoot() const
|
|
||||||
{
|
|
||||||
return d_x_4_0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
std::vector<uint8_t> Gnss_Crypto::computeSHA256(const std::vector<uint8_t>& input) const
|
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> output(32); // SHA256 hash size
|
std::vector<uint8_t> output(32); // SHA256 hash size
|
||||||
#if USE_GNUTLS_FALLBACK
|
#if USE_GNUTLS_FALLBACK
|
||||||
@ -564,7 +505,7 @@ std::vector<uint8_t> Gnss_Crypto::computeSHA256(const std::vector<uint8_t>& inpu
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::vector<uint8_t> Gnss_Crypto::computeSHA3_256(const std::vector<uint8_t>& input) const
|
std::vector<uint8_t> Gnss_Crypto::compute_SHA3_256(const std::vector<uint8_t>& input) const
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> output(32); // SHA256 hash size
|
std::vector<uint8_t> output(32); // SHA256 hash size
|
||||||
#if USE_GNUTLS_FALLBACK
|
#if USE_GNUTLS_FALLBACK
|
||||||
@ -598,7 +539,7 @@ std::vector<uint8_t> Gnss_Crypto::computeSHA3_256(const std::vector<uint8_t>& in
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::vector<uint8_t> Gnss_Crypto::computeHMAC_SHA_256(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const
|
std::vector<uint8_t> Gnss_Crypto::compute_HMAC_SHA_256(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> output(32);
|
std::vector<uint8_t> output(32);
|
||||||
#if USE_GNUTLS_FALLBACK
|
#if USE_GNUTLS_FALLBACK
|
||||||
@ -686,7 +627,7 @@ std::vector<uint8_t> Gnss_Crypto::computeHMAC_SHA_256(const std::vector<uint8_t>
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::vector<uint8_t> Gnss_Crypto::computeCMAC_AES(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const
|
std::vector<uint8_t> Gnss_Crypto::compute_CMAC_AES(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> output(16);
|
std::vector<uint8_t> output(16);
|
||||||
#if USE_GNUTLS_FALLBACK
|
#if USE_GNUTLS_FALLBACK
|
||||||
@ -829,6 +770,65 @@ std::vector<uint8_t> Gnss_Crypto::computeCMAC_AES(const std::vector<uint8_t>& ke
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<uint8_t> Gnss_Crypto::get_public_key() const
|
||||||
|
{
|
||||||
|
if (!have_public_key())
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
#if USE_GNUTLS_FALLBACK
|
||||||
|
gnutls_datum_t pem_data = {nullptr, 0};
|
||||||
|
|
||||||
|
int ret = gnutls_pubkey_export2(d_PublicKey, GNUTLS_X509_FMT_PEM, &pem_data);
|
||||||
|
if (ret != GNUTLS_E_SUCCESS)
|
||||||
|
{
|
||||||
|
LOG(INFO) << "GnuTLS: Failed to export public key to PEM format.";
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
std::vector<uint8_t> output(pem_data.data, pem_data.data + pem_data.size);
|
||||||
|
|
||||||
|
// Free the allocated memory by gnutls_pubkey_export2
|
||||||
|
gnutls_free(pem_data.data);
|
||||||
|
#else // OpenSSL
|
||||||
|
// Create a BIO for the memory buffer
|
||||||
|
BIO* mem = BIO_new(BIO_s_mem());
|
||||||
|
if (!mem)
|
||||||
|
{
|
||||||
|
LOG(INFO) << "OpenSSL: Failed to create BIO.";
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
#if USE_OPENSSL_3
|
||||||
|
if (!PEM_write_bio_PUBKEY(mem, d_PublicKey))
|
||||||
|
#else // OpenSSL 1.x
|
||||||
|
if (!PEM_write_bio_EC_PUBKEY(mem, d_PublicKey))
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
BIO_free(mem);
|
||||||
|
LOG(INFO) << "OpenSSL: Failed to write public key to PEM format.";
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the length of the data in the BIO
|
||||||
|
BUF_MEM* mem_ptr;
|
||||||
|
BIO_get_mem_ptr(mem, &mem_ptr);
|
||||||
|
|
||||||
|
// Copy the data from the BIO to a std::vector<uint8_t>
|
||||||
|
std::vector<uint8_t> output(mem_ptr->length);
|
||||||
|
memcpy(output.data(), mem_ptr->data, mem_ptr->length);
|
||||||
|
|
||||||
|
// Clean up the BIO
|
||||||
|
BIO_free(mem);
|
||||||
|
#endif
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<uint8_t> Gnss_Crypto::get_merkle_root() const
|
||||||
|
{
|
||||||
|
return d_x_4_0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Gnss_Crypto::set_public_key(const std::vector<uint8_t>& publicKey)
|
void Gnss_Crypto::set_public_key(const std::vector<uint8_t>& publicKey)
|
||||||
{
|
{
|
||||||
#if USE_GNUTLS_FALLBACK
|
#if USE_GNUTLS_FALLBACK
|
||||||
@ -884,7 +884,7 @@ void Gnss_Crypto::set_public_key(const std::vector<uint8_t>& publicKey)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Gnss_Crypto::setMerkleRoot(const std::vector<uint8_t>& v)
|
void Gnss_Crypto::set_merkle_root(const std::vector<uint8_t>& v)
|
||||||
{
|
{
|
||||||
d_x_4_0 = v;
|
d_x_4_0 = v;
|
||||||
}
|
}
|
||||||
|
@ -53,24 +53,24 @@ public:
|
|||||||
|
|
||||||
bool have_public_key() const; //!< Returns true if the ECDSA Public Key is already loaded
|
bool have_public_key() const; //!< Returns true if the ECDSA Public Key is already loaded
|
||||||
|
|
||||||
bool verify_signature(const std::vector<uint8_t>& message, const std::vector<uint8_t>& signature) const; //!< Verify ECDSA-P256 signature (message in hex, signature in raw format)
|
|
||||||
bool verify_signature_ecdsa_p521(const std::vector<uint8_t>& message, const std::vector<uint8_t>& signature) const; //!< Verify ECDSA-P521 signature (message in hex, signature in raw format)
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Stores the ECDSA Public Key in a .pem file, which is read in a following run if the .crt file is not found
|
* Stores the ECDSA Public Key in a .pem file, which is read in a following run if the .crt file is not found
|
||||||
*/
|
*/
|
||||||
bool store_public_key(const std::string& pubKeyFilePath) const;
|
bool store_public_key(const std::string& pubKeyFilePath) const;
|
||||||
|
|
||||||
std::vector<uint8_t> getPublicKey() const; //!< Gets the ECDSA Public Key in PEM format
|
bool verify_signature_ecdsa_p256(const std::vector<uint8_t>& message, const std::vector<uint8_t>& signature) const; //!< Verify ECDSA-P256 signature (message in plain hex, signature in raw format)
|
||||||
std::vector<uint8_t> getMerkleRoot() const; //!< Gets the Merkle Tree root node (\f$ x_{4,0} \f$)
|
bool verify_signature_ecdsa_p521(const std::vector<uint8_t>& message, const std::vector<uint8_t>& signature) const; //!< Verify ECDSA-P521 signature (message in plain hex, signature in raw format)
|
||||||
|
|
||||||
std::vector<uint8_t> computeSHA256(const std::vector<uint8_t>& input) const; //!< Computes SHA-256 hash
|
std::vector<uint8_t> compute_SHA_256(const std::vector<uint8_t>& input) const; //!< Computes SHA-256 hash
|
||||||
std::vector<uint8_t> computeSHA3_256(const std::vector<uint8_t>& input) const; //!< Computes SHA3-256 hash
|
std::vector<uint8_t> compute_SHA3_256(const std::vector<uint8_t>& input) const; //!< Computes SHA3-256 hash
|
||||||
std::vector<uint8_t> computeHMAC_SHA_256(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const; //!< Computes HMAC-SHA-256 message authentication code
|
std::vector<uint8_t> compute_HMAC_SHA_256(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const; //!< Computes HMAC-SHA-256 message authentication code
|
||||||
std::vector<uint8_t> computeCMAC_AES(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const; //!< Computes CMAC-AES message authentication code
|
std::vector<uint8_t> compute_CMAC_AES(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const; //!< Computes CMAC-AES message authentication code
|
||||||
|
|
||||||
|
std::vector<uint8_t> get_public_key() const; //!< Gets the ECDSA Public Key in PEM format
|
||||||
|
std::vector<uint8_t> get_merkle_root() const; //!< Gets the Merkle Tree root node (\f$ x_{4,0} \f$)
|
||||||
|
|
||||||
void set_public_key(const std::vector<uint8_t>& publickey); //!< Sets the ECDSA Public Key (publickey in PEM format)
|
void set_public_key(const std::vector<uint8_t>& publickey); //!< Sets the ECDSA Public Key (publickey in PEM format)
|
||||||
void setMerkleRoot(const std::vector<uint8_t>& v); //!< Sets the Merkle Tree root node x(\f$ x_{4,0} \f$)
|
void set_merkle_root(const std::vector<uint8_t>& v); //!< Sets the Merkle Tree root node x(\f$ x_{4,0} \f$)
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void read_merkle_xml(const std::string& merkleFilePath);
|
void read_merkle_xml(const std::string& merkleFilePath);
|
||||||
|
@ -96,7 +96,7 @@ TEST(GnssCryptoTest, VerifyPublicKeyStorage)
|
|||||||
|
|
||||||
ASSERT_EQ(content_file, content_file2);
|
ASSERT_EQ(content_file, content_file2);
|
||||||
|
|
||||||
std::vector<uint8_t> readkey = d_crypto2->getPublicKey();
|
std::vector<uint8_t> readkey = d_crypto2->get_public_key();
|
||||||
ASSERT_EQ(publicKey, readkey);
|
ASSERT_EQ(publicKey, readkey);
|
||||||
|
|
||||||
errorlib::error_code ec;
|
errorlib::error_code ec;
|
||||||
@ -116,7 +116,7 @@ TEST(GnssCryptoTest, TestComputeSHA_256)
|
|||||||
0x43, 0xAC, 0x4E, 0x43, 0xFC, 0x00, 0x4C, 0x89, 0x16, 0x04, 0xB2,
|
0x43, 0xAC, 0x4E, 0x43, 0xFC, 0x00, 0x4C, 0x89, 0x16, 0x04, 0xB2,
|
||||||
0x6F, 0x8C, 0x69, 0xE1, 0xE8, 0x3E, 0xA2, 0xAF, 0xC7, 0xC4, 0x8F};
|
0x6F, 0x8C, 0x69, 0xE1, 0xE8, 0x3E, 0xA2, 0xAF, 0xC7, 0xC4, 0x8F};
|
||||||
|
|
||||||
std::vector<uint8_t> output = d_crypto->computeSHA256(message);
|
std::vector<uint8_t> output = d_crypto->compute_SHA_256(message);
|
||||||
|
|
||||||
ASSERT_EQ(expected_output, output);
|
ASSERT_EQ(expected_output, output);
|
||||||
}
|
}
|
||||||
@ -133,7 +133,7 @@ TEST(GnssCryptoTest, TestComputeSHA3_256)
|
|||||||
0xBB, 0x2C, 0x24, 0x36, 0x72, 0x5E, 0x2E, 0x8D, 0xC7, 0x5B,
|
0xBB, 0x2C, 0x24, 0x36, 0x72, 0x5E, 0x2E, 0x8D, 0xC7, 0x5B,
|
||||||
0x99, 0xE7, 0xF6, 0xC4, 0x50, 0x5B, 0x2A, 0x93, 0x6E, 0xB6, 0x3B, 0x3F};
|
0x99, 0xE7, 0xF6, 0xC4, 0x50, 0x5B, 0x2A, 0x93, 0x6E, 0xB6, 0x3B, 0x3F};
|
||||||
|
|
||||||
std::vector<uint8_t> output = d_crypto->computeSHA3_256(message);
|
std::vector<uint8_t> output = d_crypto->compute_SHA3_256(message);
|
||||||
|
|
||||||
ASSERT_EQ(expected_output, output);
|
ASSERT_EQ(expected_output, output);
|
||||||
}
|
}
|
||||||
@ -159,7 +159,7 @@ TEST(GnssCryptoTest, TestComputeHMACSHA256)
|
|||||||
0xB2, 0x7A, 0xCC, 0x22, 0x00, 0xAA, 0xD2, 0x37,
|
0xB2, 0x7A, 0xCC, 0x22, 0x00, 0xAA, 0xD2, 0x37,
|
||||||
0xD0, 0x79, 0x06, 0x12, 0x83, 0x40, 0xB7, 0xA6};
|
0xD0, 0x79, 0x06, 0x12, 0x83, 0x40, 0xB7, 0xA6};
|
||||||
|
|
||||||
std::vector<uint8_t> output = d_crypto->computeHMAC_SHA_256(key, message);
|
std::vector<uint8_t> output = d_crypto->compute_HMAC_SHA_256(key, message);
|
||||||
|
|
||||||
ASSERT_EQ(expected_output, output);
|
ASSERT_EQ(expected_output, output);
|
||||||
}
|
}
|
||||||
@ -194,7 +194,7 @@ TEST(GnssCryptoTest, TestComputeHMACSHA256_m0)
|
|||||||
0xD2, 0xEA, 0x61, 0xE1, 0xEF, 0x09, 0x11, 0x5C,
|
0xD2, 0xEA, 0x61, 0xE1, 0xEF, 0x09, 0x11, 0x5C,
|
||||||
0xFE, 0x70, 0x68, 0x52, 0xBF, 0xF2, 0x3A, 0x83};
|
0xFE, 0x70, 0x68, 0x52, 0xBF, 0xF2, 0x3A, 0x83};
|
||||||
|
|
||||||
std::vector<uint8_t> output = d_crypto->computeHMAC_SHA_256(key, message);
|
std::vector<uint8_t> output = d_crypto->compute_HMAC_SHA_256(key, message);
|
||||||
|
|
||||||
ASSERT_EQ(expected_output, output);
|
ASSERT_EQ(expected_output, output);
|
||||||
}
|
}
|
||||||
@ -222,7 +222,7 @@ TEST(GnssCryptoTest, TestComputeHMACSHA256_adkd4)
|
|||||||
0x44, 0xAB, 0xEE, 0x4D, 0xCE, 0xB9, 0x3D, 0xCF,
|
0x44, 0xAB, 0xEE, 0x4D, 0xCE, 0xB9, 0x3D, 0xCF,
|
||||||
0x65, 0xCB, 0x3A, 0x5B, 0x81, 0x4A, 0x34, 0xE9};
|
0x65, 0xCB, 0x3A, 0x5B, 0x81, 0x4A, 0x34, 0xE9};
|
||||||
|
|
||||||
std::vector<uint8_t> output = d_crypto->computeHMAC_SHA_256(key, message);
|
std::vector<uint8_t> output = d_crypto->compute_HMAC_SHA_256(key, message);
|
||||||
|
|
||||||
ASSERT_EQ(expected_output, output);
|
ASSERT_EQ(expected_output, output);
|
||||||
}
|
}
|
||||||
@ -245,13 +245,13 @@ TEST(GnssCryptoTest, TestComputeCMAC_AES)
|
|||||||
0x07, 0x0A, 0x16, 0xB4, 0x6B, 0x4D, 0x41, 0x44,
|
0x07, 0x0A, 0x16, 0xB4, 0x6B, 0x4D, 0x41, 0x44,
|
||||||
0xF7, 0x9B, 0xDD, 0x9D, 0xD0, 0x4A, 0x28, 0x7C};
|
0xF7, 0x9B, 0xDD, 0x9D, 0xD0, 0x4A, 0x28, 0x7C};
|
||||||
|
|
||||||
std::vector<uint8_t> output = d_crypto->computeCMAC_AES(key, message);
|
std::vector<uint8_t> output = d_crypto->compute_CMAC_AES(key, message);
|
||||||
|
|
||||||
ASSERT_EQ(expected_output, output);
|
ASSERT_EQ(expected_output, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST(GnssCryptoTest, VerifySignature)
|
TEST(GnssCryptoTest, VerifySignatureP256)
|
||||||
{
|
{
|
||||||
auto d_crypto = std::make_unique<Gnss_Crypto>();
|
auto d_crypto = std::make_unique<Gnss_Crypto>();
|
||||||
|
|
||||||
@ -293,12 +293,12 @@ TEST(GnssCryptoTest, VerifySignature)
|
|||||||
0x45, 0x59, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0A};
|
0x45, 0x59, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0A};
|
||||||
|
|
||||||
d_crypto->set_public_key(publicKey);
|
d_crypto->set_public_key(publicKey);
|
||||||
bool result = d_crypto->verify_signature(message, signature);
|
bool result = d_crypto->verify_signature_ecdsa_p256(message, signature);
|
||||||
ASSERT_TRUE(result);
|
ASSERT_TRUE(result);
|
||||||
|
|
||||||
std::vector<uint8_t> wrong_signature = signature;
|
std::vector<uint8_t> wrong_signature = signature;
|
||||||
wrong_signature[1] = 1;
|
wrong_signature[1] = 1;
|
||||||
bool result2 = d_crypto->verify_signature(message, wrong_signature);
|
bool result2 = d_crypto->verify_signature_ecdsa_p256(message, wrong_signature);
|
||||||
ASSERT_TRUE(!result2);
|
ASSERT_TRUE(!result2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ TEST_F(OsnmaMsgReceiverTest, ComputeBaseLeaf)
|
|||||||
TEST_F(OsnmaMsgReceiverTest, VerifyPublicKey){ // values taken from RG A.7
|
TEST_F(OsnmaMsgReceiverTest, VerifyPublicKey){ // values taken from RG A.7
|
||||||
// Arrange
|
// Arrange
|
||||||
// ----------
|
// ----------
|
||||||
osnma->d_crypto->setMerkleRoot(helper.convert_from_hex_string("A10C440F3AA62453526DB4AF76DF8D9410D35D8277397D7053C700D192702B0D"));
|
osnma->d_crypto->set_merkle_root(helper.convert_from_hex_string("A10C440F3AA62453526DB4AF76DF8D9410D35D8277397D7053C700D192702B0D"));
|
||||||
DSM_PKR_message dsm_pkr_message;
|
DSM_PKR_message dsm_pkr_message;
|
||||||
dsm_pkr_message.npkt = 0x01;
|
dsm_pkr_message.npkt = 0x01;
|
||||||
dsm_pkr_message.npktid = 0x2;
|
dsm_pkr_message.npktid = 0x2;
|
||||||
|
Loading…
Reference in New Issue
Block a user