GNSS-SDR 0.0.21
An Open Source GNSS Software Defined Receiver
Loading...
Searching...
No Matches
gnss_crypto.h
Go to the documentation of this file.
1/*!
2 * \file gnss_crypto.h
3 * \brief Class for computing cryptographic functions
4 * \author Carles Fernandez, 2023-2024. cfernandez(at)cttc.es
5 * Cesare Ghionoiu Martinez, 2023-2024. c.ghionoiu-martinez@tu-braunschweig.de
6 *
7 *
8 * -----------------------------------------------------------------------------
9 *
10 * GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
11 * This file is part of GNSS-SDR.
12 *
13 * Copyright (C) 2010-2024 (see AUTHORS file for a list of contributors)
14 * SPDX-License-Identifier: GPL-3.0-or-later
15 *
16 * -----------------------------------------------------------------------------
17 */
18
19#ifndef GNSS_SDR_GNSS_CRYPTO_H
20#define GNSS_SDR_GNSS_CRYPTO_H
21
22#include <cstdint>
23#include <string>
24#include <vector>
25#if USE_GNUTLS_FALLBACK
26#include <gnutls/abstract.h>
27#include <gnutls/gnutls.h>
28#else // OpenSSL
29#include <openssl/ec.h>
30#endif
31
32/** \addtogroup Core
33 * \{ */
34/** \addtogroup Core_Receiver_Library
35 * \{ */
36
37/*!
38 * \brief Class implementing cryptographic functions
39 * for Navigation Message Authentication
40 */
42{
43public:
44 Gnss_Crypto(); //!< Default constructor
45
46 /*!
47 * Constructor with a .crt or .pem file for the ECDSA Public Key
48 * and a XML file for the Merkle Tree root.
49 * Files can be downloaded by registering at https://www.gsc-europa.eu/
50 */
51 Gnss_Crypto(const std::string& certFilePath, const std::string& merkleTreePath);
52 ~Gnss_Crypto(); //!< Default destructor
53
54 bool have_public_key() const; //!< Returns true if the ECDSA Public Key is already loaded
55
56 /*!
57 * Stores the ECDSA Public Key in a .pem file, which is read in a following run if the .crt file is not found
58 */
59 bool store_public_key(const std::string& pubKeyFilePath) const;
60
61 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)
62 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)
63
64 std::vector<uint8_t> compute_SHA_256(const std::vector<uint8_t>& input) const; //!< Computes SHA-256 hash
65 std::vector<uint8_t> compute_SHA3_256(const std::vector<uint8_t>& input) const; //!< Computes SHA3-256 hash
66 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
67 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
68
69 std::vector<uint8_t> get_merkle_root() const; //!< Gets the Merkle Tree root node (\f$ x_{4,0} \f$)
70 std::string get_public_key_type() const; //!< Gets the ECDSA Public Key type (ECDSA P-256 / ECDSA P-521 / Unknown)
71
72 void set_public_key(const std::vector<uint8_t>& publickey); //!< Sets the ECDSA Public Key (publickey compressed format)
73 void set_public_key_type(const std::string& public_key_type); //!< Sets the ECDSA Public Key type (ECDSA P-256 / ECDSA P-521)
74 void set_merkle_root(const std::vector<uint8_t>& v); //!< Sets the Merkle Tree root node x(\f$ x_{4,0} \f$)
75 void read_merkle_xml(const std::string& merkleFilePath); //!> Reads the XML file provided from the GSC OSNMA server
76
77private:
78 void readPublicKeyFromPEM(const std::string& pemFilePath);
79 bool readPublicKeyFromCRT(const std::string& crtFilePath);
80 bool convert_raw_to_der_ecdsa(const std::vector<uint8_t>& raw_signature, std::vector<uint8_t>& der_signature) const;
81 std::vector<uint8_t> convert_from_hex_str(const std::string& input) const; // TODO - deprecate if OSNMA helper is to do this operation
82#if USE_GNUTLS_FALLBACK
83 void decompress_public_key_secp256r1(const std::vector<uint8_t>& compressed_key, std::vector<uint8_t>& x, std::vector<uint8_t>& y) const;
84 void decompress_public_key_secp521r1(const std::vector<uint8_t>& compressed_key, std::vector<uint8_t>& x, std::vector<uint8_t>& y) const;
85 bool pubkey_copy(gnutls_pubkey_t src, gnutls_pubkey_t* dest);
86 gnutls_pubkey_t d_PublicKey{};
87#else // OpenSSL
88#if USE_OPENSSL_3
89 bool pubkey_copy(EVP_PKEY* src, EVP_PKEY** dest);
90 EVP_PKEY* d_PublicKey{};
91#else // OpenSSL 1.x
92 bool pubkey_copy(EC_KEY* src, EC_KEY** dest);
93 EC_KEY* d_PublicKey = nullptr;
94#endif
95#endif
96 std::vector<uint8_t> d_x_4_0;
97 std::string d_PublicKeyType;
98};
99
100/** \} */
101/** \} */
102
103#endif // GNSS_SDR_GNSS_CRYPTO_H
bool store_public_key(const std::string &pubKeyFilePath) const
std::string get_public_key_type() const
Gets the ECDSA Public Key type (ECDSA P-256 / ECDSA P-521 / Unknown).
bool have_public_key() const
Returns true if the ECDSA Public Key is already loaded.
std::vector< uint8_t > get_merkle_root() const
Gets the Merkle Tree root node ( ).
Gnss_Crypto(const std::string &certFilePath, const std::string &merkleTreePath)
void set_public_key(const std::vector< uint8_t > &publickey)
Sets the ECDSA Public Key (publickey compressed format).
void set_public_key_type(const std::string &public_key_type)
Sets the ECDSA Public Key type (ECDSA P-256 / ECDSA P-521).
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.
~Gnss_Crypto()
Default destructor.
void set_merkle_root(const std::vector< uint8_t > &v)
Sets the Merkle Tree root node x( ).
std::vector< uint8_t > compute_SHA3_256(const std::vector< uint8_t > &input) const
Computes SHA3-256 hash.
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 > 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 > compute_SHA_256(const std::vector< uint8_t > &input) const
Computes SHA-256 hash.
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).
Gnss_Crypto()
Default constructor.