From 08233d44a6cfde878d7e10bca38ae935ed1c8fd5 Mon Sep 17 00:00:00 2001 From: Mauro Date: Wed, 30 Jun 2021 07:57:22 +0000 Subject: [HICN-713] Transport Library Major Refactoring 2 Co-authored-by: Luca Muscariello Co-authored-by: Michele Papalini Co-authored-by: Olivier Roques Co-authored-by: Giulio Grassi Signed-off-by: Mauro Sardara Change-Id: I5b2c667bad66feb45abdb5effe22ed0f6c85d1c2 --- .../includes/hicn/transport/auth/crypto_hash.h | 151 +++++++++------------ 1 file changed, 66 insertions(+), 85 deletions(-) (limited to 'libtransport/includes/hicn/transport/auth/crypto_hash.h') diff --git a/libtransport/includes/hicn/transport/auth/crypto_hash.h b/libtransport/includes/hicn/transport/auth/crypto_hash.h index 26c251b38..90f1627e9 100644 --- a/libtransport/includes/hicn/transport/auth/crypto_hash.h +++ b/libtransport/includes/hicn/transport/auth/crypto_hash.h @@ -16,105 +16,86 @@ #pragma once #include -#include -#include -#include +#include -extern "C" { -#include -}; +#include -#include -#include +extern "C" { +#include +} namespace transport { namespace auth { -class CryptoHasher; +typedef const EVP_MD *(*CryptoHashEVP)(void); -struct EnumClassHash { - template - std::size_t operator()(T t) const { - return static_cast(t); - } +enum class CryptoHashType : uint8_t { + UNKNOWN, + SHA256, + SHA512, + BLAKE2B512, + BLAKE2S256, }; -static std::unordered_map - hash_size_map = {{CryptoHashType::SHA_256, 32}, - {CryptoHashType::CRC32C, 4}, - {CryptoHashType::SHA_512, 64}}; +class CryptoHash { + public: + // Constructors + CryptoHash(); + CryptoHash(const CryptoHash &other); + CryptoHash(CryptoHash &&other); + CryptoHash(CryptoHashType hash_type); + CryptoHash(const uint8_t *hash, std::size_t size, CryptoHashType hash_type); + CryptoHash(const std::vector &hash, CryptoHashType hash_type); -class Signer; -class Verifier; + // Destructor + ~CryptoHash() = default; -class CryptoHash { - friend class CryptoHasher; - friend class Signer; - friend class Verifier; + // Operators + CryptoHash &operator=(const CryptoHash &other); + bool operator==(const CryptoHash &other) const; - public: - CryptoHash() : hash_(nullptr) {} - - CryptoHash(const CryptoHash& other) { - if (other.hash_) { - hash_ = parcCryptoHash_Acquire(other.hash_); - } - } - - CryptoHash(CryptoHash&& other) { - if (other.hash_) { - hash_ = parcCryptoHash_Acquire(other.hash_); - } - } - - template - CryptoHash(const T* buffer, std::size_t length, CryptoHashType hash_type) { - hash_ = parcCryptoHash_CreateFromArray( - static_cast(hash_type), buffer, length); - } - - ~CryptoHash() { - if (hash_) { - parcCryptoHash_Release(&hash_); - } - } - - CryptoHash& operator=(const CryptoHash& other) { - if (other.hash_) { - hash_ = parcCryptoHash_Acquire(other.hash_); - } - - return *this; - } - - template - utils::Array getDigest() const { - return utils::Array( - static_cast(parcBuffer_Overlay(parcCryptoHash_GetDigest(hash_), 0)), - parcBuffer_Remaining(parcCryptoHash_GetDigest(hash_))); - } - - CryptoHashType getType() { - return static_cast(parcCryptoHash_GetDigestType(hash_)); - } - - template - static bool compareBinaryDigest(const T* digest1, const T* digest2, - CryptoHashType hash_type) { - if (hash_size_map.find(hash_type) == hash_size_map.end()) { - return false; - } - - return !static_cast( - std::memcmp(digest1, digest2, hash_size_map[hash_type])); - } - - TRANSPORT_ALWAYS_INLINE void display() { - parcBuffer_Display(parcCryptoHash_GetDigest(hash_), 2); - } + // Compute the hash of given buffer + void computeDigest(const uint8_t *buffer, std::size_t len); + void computeDigest(const std::vector &buffer); + + // Compute the hash of given membuf + void computeDigest(const utils::MemBuf *buffer); + + // Return the computed hash + std::vector getDigest() const; + + // Return the computed hash as a string + std::string getStringDigest() const; + + // Return hash type + CryptoHashType getType() const; + + // Return hash size + std::size_t getSize() const; + + // Change hash type + void setType(CryptoHashType hash_type); + + // Print hash to stdout + void display(); + + // Reset hash + void reset(); + + // Return OpenSSL EVP function associated to a given hash type + static CryptoHashEVP getEVP(CryptoHashType hash_type); + + // Return hash size + static std::size_t getSize(CryptoHashType hash_type); + + // Compare two raw buffers + static bool compareDigest(const uint8_t *h1, const uint8_t *h2, + CryptoHashType hash_type); private: - PARCCryptoHash* hash_; + CryptoHashType digest_type_; + std::vector digest_; + std::size_t digest_size_; }; } // namespace auth -- cgit 1.2.3-korg