Skip to content
Snippets Groups Projects

Replace Crypto Implementation

Files
13
@@ -6,6 +6,9 @@
#include <memory>
#include <map>
#include <gnutls/crypto.h>
#include <gnutls/gnutls.h>
#include "causal/trace.hpp"
#include "causal/core/aspect.hpp"
@@ -93,6 +96,37 @@ namespace causal {
Certificate certificate;
};
EXCEPTION_ERROR(gnutls_error, "GnuTLS error")
class gnutls_init final {
private:
// Private constructor to prevent instantiation from outside
gnutls_init();
// Singleton instance
static gnutls_init* instance;
// Flag for thread-safe initialization
static std::once_flag initFlag;
// Method to initialize the library if not already initialized
static void _init();
public:
~gnutls_init();
// Delete copy constructor and assignment operator to prevent copying
gnutls_init(const gnutls_init&) = delete;
void operator=(const gnutls_init&) = delete;
// Public method to access the singleton instance
static void init();
};
std::string gnutls_derrive_sha_key(const std::string& password, u_short key_length);
EXCEPTION_ERROR(sha_hash_lenght_invalid, "hash_length should be larger than or equal to 256")
/// @brief abstract base of hasher
class hasher {
public:
@@ -101,7 +135,15 @@ namespace causal {
* @param hash_length requested SHA length
* @return SHA hash of data
*/
virtual std::string hash_sha(const std::string& data, u_short hash_length=DEFAULT_SHA_HASH_LENGTH) const = 0;
virtual std::string hash(const std::string& data, u_short hash_length=DEFAULT_SHA_HASH_LENGTH) const = 0;
};
/// @brief GnuTLS backed SHA crypto hasher
class gnutls_sha_hasher final : public hasher {
public:
gnutls_sha_hasher();
std::string hash(const std::string& data, u_short hash_length=DEFAULT_SHA_HASH_LENGTH) const override;
};
/// @brief abstract base of crypto provider
@@ -273,8 +315,10 @@ namespace causal {
};
/// @brief abstract base of symmetric coder
class crypto_coder {
class cipher {
public:
virtual ~cipher() = default;
/** @brief get hasher using coders backend
* @return hasher to use
*/
@@ -299,6 +343,23 @@ namespace causal {
virtual std::string decrypt(const std::string& encrypted_data) const = 0;
};
/// @brief GnuTLS backed AES crypto coder
class gnutls_aes_cipher final : public cipher {
private:
const std::string key, iv;
const gnutls_cipher_algorithm_t algo;
public:
const u_short key_length;
gnutls_aes_cipher(const std::string& password, const u_short key_length = 256);
const hasher& get_hasher() const override;
const std::string get_token() const override;
std::string encrypt(const std::string& data) const override;
std::string decrypt(const std::string& encrypted_data) const override;
};
/// @brief basic metadata for channels supporting identification
struct IdentifiedMessageMeta : MessageMeta {
PublicIdentity owner_id;
Loading