aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/includes/hicn/transport/auth/crypto_hash.h
diff options
context:
space:
mode:
Diffstat (limited to 'libtransport/includes/hicn/transport/auth/crypto_hash.h')
-rw-r--r--libtransport/includes/hicn/transport/auth/crypto_hash.h151
1 files changed, 66 insertions, 85 deletions
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 <hicn/transport/errors/runtime_exception.h>
-#include <hicn/transport/portability/portability.h>
-#include <hicn/transport/auth/crypto_hash_type.h>
-#include <hicn/transport/utils/array.h>
+#include <hicn/transport/utils/membuf.h>
-extern "C" {
-#include <parc/security/parc_CryptoHash.h>
-};
+#include <iomanip>
-#include <cstring>
-#include <unordered_map>
+extern "C" {
+#include <openssl/evp.h>
+}
namespace transport {
namespace auth {
-class CryptoHasher;
+typedef const EVP_MD *(*CryptoHashEVP)(void);
-struct EnumClassHash {
- template <typename T>
- std::size_t operator()(T t) const {
- return static_cast<std::size_t>(t);
- }
+enum class CryptoHashType : uint8_t {
+ UNKNOWN,
+ SHA256,
+ SHA512,
+ BLAKE2B512,
+ BLAKE2S256,
};
-static std::unordered_map<CryptoHashType, std::size_t, EnumClassHash>
- 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<uint8_t> &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 <typename T>
- CryptoHash(const T* buffer, std::size_t length, CryptoHashType hash_type) {
- hash_ = parcCryptoHash_CreateFromArray(
- static_cast<PARCCryptoHashType>(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 <typename T>
- utils::Array<T> getDigest() const {
- return utils::Array<T>(
- static_cast<T*>(parcBuffer_Overlay(parcCryptoHash_GetDigest(hash_), 0)),
- parcBuffer_Remaining(parcCryptoHash_GetDigest(hash_)));
- }
-
- CryptoHashType getType() {
- return static_cast<CryptoHashType>(parcCryptoHash_GetDigestType(hash_));
- }
-
- template <typename T>
- 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<bool>(
- 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<uint8_t> &buffer);
+
+ // Compute the hash of given membuf
+ void computeDigest(const utils::MemBuf *buffer);
+
+ // Return the computed hash
+ std::vector<uint8_t> 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<uint8_t> digest_;
+ std::size_t digest_size_;
};
} // namespace auth