Skip to content

Commit

Permalink
Move digest utils
Browse files Browse the repository at this point in the history
  • Loading branch information
WildFireFlum committed Sep 12, 2022
1 parent 4a8e54e commit 541b4ea
Show file tree
Hide file tree
Showing 114 changed files with 771 additions and 680 deletions.
6 changes: 2 additions & 4 deletions bftengine/include/bcstatetransfer/SimpleBCStateTransfer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "bftengine/IStateTransfer.hpp"
#include "Metrics.hpp"
#include "kvstream.h"
#include "digest.hpp"
#include "crypto/digest.hpp"

namespace concord {
namespace storage {
Expand Down Expand Up @@ -58,9 +58,7 @@ void computeBlockDigest(const uint64_t blockId,
const uint32_t blockSize,
StateTransferDigest *outDigest);

concord::util::digest::BlockDigest computeBlockDigest(const uint64_t blockId,
const char *block,
const uint32_t blockSize);
concord::crypto::BlockDigest computeBlockDigest(const uint64_t blockId, const char *block, const uint32_t blockSize);

// This interface should be implemented by the application/storage layer.
// It is used by the state transfer module.
Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bcstatetransfer/BCStateTran.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ using std::tie;
using namespace std::placeholders;
using namespace concord::diagnostics;
using namespace concord::util;
using concord::util::digest::DigestGenerator;
using concord::crypto::DigestGenerator;

// uncomment to add debug prints
// #define BCSTATETRAN_DO_DEBUG
Expand All @@ -51,7 +51,7 @@ using concord::util::digest::DigestGenerator;

namespace bftEngine {
namespace bcst {
using concord::util::digest::BlockDigest;
using concord::crypto::BlockDigest;

void computeBlockDigest(const uint64_t blockId,
const char *block,
Expand Down
6 changes: 3 additions & 3 deletions bftengine/src/bcstatetransfer/BCStateTran.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,9 @@ class BCStateTran : public IStateTransfer {
const uint32_t blockSize,
Digest* outDigest);

static concord::util::digest::BlockDigest computeDigestOfBlock(const uint64_t blockNum,
const char* block,
const uint32_t blockSize);
static concord::crypto::BlockDigest computeDigestOfBlock(const uint64_t blockNum,
const char* block,
const uint32_t blockSize);

protected:
// A wrapper function to get a block from the IAppState and compute its digest.
Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bcstatetransfer/DataStore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
#include <set>
#include "assertUtils.hpp"
#include "storage/db_interface.h"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include "Logger.hpp"

using std::set;
using concord::storage::ITransaction;
using concord::util::digest::Digest;
using concord::crypto::Digest;

namespace bftEngine {
namespace bcst {
Expand Down
2 changes: 1 addition & 1 deletion bftengine/src/bcstatetransfer/RVBManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "RVBManager.hpp"
#include "throughput.hpp"

using concord::util::digest::DigestGenerator;
using concord::crypto::DigestGenerator;
using namespace std;
using namespace concord::util;

Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bcstatetransfer/RangeValidationTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
#include <type_traits>

#include "RangeValidationTree.hpp"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include "type_traits"
#include "throughput.hpp"

using concord::util::digest::DigestGenerator;
using concord::crypto::DigestGenerator;

using namespace std;
using namespace concord::serialize;
Expand Down
2 changes: 1 addition & 1 deletion bftengine/src/bcstatetransfer/RangeValidationTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include <cryptopp/integer.h>

#include "digest.hpp"
#include "crypto/digest.hpp"
#include "Serializable.h"
#include "Logger.hpp"
#include "Metrics.hpp"
Expand Down
2 changes: 1 addition & 1 deletion bftengine/src/bftengine/CollectorOfThresholdSignatures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "OpenTracing.hpp"
#include "PrimitiveTypes.hpp"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include "SimpleThreadPool.hpp"
#include "InternalReplicaApi.hpp"
#include "IncomingMsgsStorage.hpp"
Expand Down
2 changes: 1 addition & 1 deletion bftengine/src/bftengine/KeyExchangeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "ReplicaImp.hpp"
#include "crypto/factory.hpp"
#include "crypto/crypto.hpp"
#include "openssl/utils.hpp"
#include "crypto/openssl/certificates.hpp"
#include "crypto/cryptopp/keygen.hpp"

namespace bftEngine::impl {
Expand Down
9 changes: 5 additions & 4 deletions bftengine/src/bftengine/KeyStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,19 @@ void ClusterKeyStore::saveReplicaKeyStoreToReserevedPages(const uint16_t& repID)

// Save clients keys to res pages and sets `published` to true.
void ClientKeyStore::save(const std::string& keys) {
auto hashed_keys = concord::util::SHA3_256().digest(keys.c_str(), keys.size());
auto hashed_keys = concord::crypto::openssl::SHA3_256().digest(keys.c_str(), keys.size());
auto strHashed_keys = std::string(hashed_keys.begin(), hashed_keys.end());
ConcordAssertEQ(strHashed_keys.size(), concord::util::SHA3_256::SIZE_IN_BYTES);
ConcordAssertEQ(strHashed_keys.size(), concord::crypto::openssl::SHA3_256::SIZE_IN_BYTES);
saveReservedPage(0, strHashed_keys.size(), strHashed_keys.c_str());
published_ = true;
LOG_INFO(KEY_EX_LOG, "Clients keys were updated, size " << keys.size());
}

std::string ClientKeyStore::load() {
std::string res_page_version(concord::util::SHA3_256::SIZE_IN_BYTES, '\0');
std::string res_page_version(concord::crypto::openssl::SHA3_256::SIZE_IN_BYTES, '\0');
loadReservedPage(0, res_page_version.length(), res_page_version.data());
return res_page_version == std::string(concord::util::SHA3_256::SIZE_IN_BYTES, '\0') ? "" : res_page_version;
return res_page_version == std::string(concord::crypto::openssl::SHA3_256::SIZE_IN_BYTES, '\0') ? ""
: res_page_version;
}

} // namespace bftEngine::impl
2 changes: 1 addition & 1 deletion bftengine/src/bftengine/KeyStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "KeyExchangeMsg.hpp"
#include <map>
#include <optional>
#include "sha_hash.hpp"
#include "crypto/openssl/hash.hpp"

namespace bftEngine::impl {

Expand Down
6 changes: 3 additions & 3 deletions bftengine/src/bftengine/NullStateTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

#include "NullStateTransfer.hpp"
#include "assertUtils.hpp"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include "Logger.hpp"

using concord::util::digest::Digest;
using concord::util::digest::DigestGenerator;
using concord::crypto::Digest;
using concord::crypto::DigestGenerator;

namespace bftEngine {
namespace impl {
Expand Down
2 changes: 1 addition & 1 deletion bftengine/src/bftengine/ReplicaImp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "ReplicaForStateTransfer.hpp"
#include "CollectorOfThresholdSignatures.hpp"
#include "SeqNumInfo.hpp"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include "SimpleThreadPool.hpp"
#include "ControllerBase.hpp"
#include "RetransmissionsManager.hpp"
Expand Down
2 changes: 1 addition & 1 deletion bftengine/src/bftengine/messages/AskForCheckpointMsg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#pragma once

#include "MessageBase.hpp"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include "OpenTracing.hpp"
#include "assertUtils.hpp"

Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bftengine/messages/CheckpointMsg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
#pragma once

#include "MessageBase.hpp"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include "ReplicaConfig.hpp"
#include "Metrics.hpp"

using concord::util::digest::Digest;
using concord::crypto::Digest;

namespace bftEngine::impl {

Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bftengine/messages/NewViewMsg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#pragma once

#include "MessageBase.hpp"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include "ReplicaConfig.hpp"

using concord::util::digest::Digest;
using concord::crypto::Digest;

namespace bftEngine {
namespace impl {
Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bftengine/messages/PartialCommitProofMsg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#pragma once

#include "MessageBase.hpp"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include <memory>

using concord::util::digest::Digest;
using concord::crypto::Digest;

class IThresholdSigner;

Expand Down
2 changes: 1 addition & 1 deletion bftengine/src/bftengine/messages/PrePrepareMsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "RequestThreadPool.hpp"
#include "EpochManager.hpp"

using concord::util::digest::DigestGenerator;
using concord::crypto::DigestGenerator;

namespace bftEngine {
namespace impl {
Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bftengine/messages/PrePrepareMsg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

#include "PrimitiveTypes.hpp"
#include "assertUtils.hpp"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include "MessageBase.hpp"
#include "ReplicaConfig.hpp"

using concord::util::digest::Digest;
using concord::crypto::Digest;

namespace bftEngine {
namespace impl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <cstdint>

#include "assertUtils.hpp"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include "ReplicaConfig.hpp"
#include "MessageBase.hpp"
#include "OpenTracing.hpp"
Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bftengine/messages/SignedShareMsgs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

#pragma once

#include "digest.hpp"
#include "crypto/digest.hpp"
#include "MessageBase.hpp"
#include <memory>

using concord::util::digest::Digest;
using concord::crypto::Digest;

class IThresholdSigner;

Expand Down
2 changes: 1 addition & 1 deletion bftengine/src/bftengine/messages/ViewChangeMsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "SigManager.hpp"
#include "EpochManager.hpp"

using concord::util::digest::DigestGenerator;
using concord::crypto::DigestGenerator;

namespace bftEngine {
namespace impl {
Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bftengine/messages/ViewChangeMsg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
#pragma once

#include "MessageBase.hpp"
#include "digest.hpp"
#include "crypto/digest.hpp"
#include "OpenTracing.hpp"
#include "ReplicasInfo.hpp"
#include "ReplicaConfig.hpp"
#include "ReplicaAsksToLeaveViewMsg.hpp"

using concord::util::digest::Digest;
using concord::crypto::Digest;

namespace bftEngine {
namespace impl {
Expand Down
29 changes: 15 additions & 14 deletions bftengine/src/preprocessor/RequestProcessingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace preprocessor {
using namespace std;
using namespace bftEngine;
using namespace chrono;
using namespace concord::util;
using namespace concord::kvbc::sparse_merkle;

uint16_t RequestProcessingState::numOfRequiredEqualReplies_ = 0;
Expand Down Expand Up @@ -124,9 +123,8 @@ void RequestProcessingState::releaseResources() {
preProcessRequestMsg_.reset();
}

void RequestProcessingState::detectNonDeterministicPreProcessing(const SHA3_256::Digest &newHash,
NodeIdType newSenderId,
uint64_t reqRetryId) const {
void RequestProcessingState::detectNonDeterministicPreProcessing(
const concord::crypto::openssl::SHA3_256::Digest &newHash, NodeIdType newSenderId, uint64_t reqRetryId) const {
for (auto &hashArray : preProcessingResultHashes_)
if ((newHash != hashArray.first) && reqRetryId_ && (reqRetryId_ == reqRetryId)) {
// Compare only between matching request/reply retry ids
Expand Down Expand Up @@ -170,9 +168,10 @@ void RequestProcessingState::handlePreProcessReplyMsg(const PreProcessReplyMsgSh
}
}

SHA3_256::Digest RequestProcessingState::convertToArray(const uint8_t resultsHash[SHA3_256::SIZE_IN_BYTES]) {
SHA3_256::Digest hashArray;
for (uint64_t i = 0; i < SHA3_256::SIZE_IN_BYTES; i++) hashArray[i] = resultsHash[i];
concord::crypto::openssl::SHA3_256::Digest RequestProcessingState::convertToArray(
const uint8_t resultsHash[concord::crypto::openssl::SHA3_256::SIZE_IN_BYTES]) {
concord::crypto::openssl::SHA3_256::Digest hashArray;
for (uint64_t i = 0; i < concord::crypto::openssl::SHA3_256::SIZE_IN_BYTES; i++) hashArray[i] = resultsHash[i];
return hashArray;
}

Expand Down Expand Up @@ -205,8 +204,8 @@ bool RequestProcessingState::isReqTimedOut() const {
return false;
}

std::pair<std::string, concord::util::SHA3_256::Digest> RequestProcessingState::detectFailureDueToBlockID(
const concord::util::SHA3_256::Digest &other, uint64_t blockId) {
std::pair<std::string, concord::crypto::openssl::SHA3_256::Digest> RequestProcessingState::detectFailureDueToBlockID(
const concord::crypto::openssl::SHA3_256::Digest &other, uint64_t blockId) {
// Since this scenario is rare, a new string is allocated for safety.
std::string modifiedResult(primaryPreProcessResultData_, primaryPreProcessResultLen_);
ConcordAssertGT(modifiedResult.size(), sizeof(uint64_t));
Expand All @@ -221,11 +220,11 @@ std::pair<std::string, concord::util::SHA3_256::Digest> RequestProcessingState::
"Primary hash is different from quorum due to mismatch in block id" << KVLOG(batchCid_, reqSeqNum_, reqCid_));
return {modifiedResult, modifiedHash};
}
return {"", concord::util::SHA3_256::Digest{}};
return {"", concord::crypto::openssl::SHA3_256::Digest{}};
}

void RequestProcessingState::modifyPrimaryResult(
const std::pair<std::string, concord::util::SHA3_256::Digest> &result) {
const std::pair<std::string, concord::crypto::openssl::SHA3_256::Digest> &result) {
memcpy(const_cast<char *>(primaryPreProcessResultData_), result.first.c_str(), primaryPreProcessResultLen_);
primaryPreProcessResultHash_ = result.second;
auto sm = SigManager::instance();
Expand All @@ -241,9 +240,11 @@ void RequestProcessingState::modifyPrimaryResult(
void RequestProcessingState::reportNonEqualHashes(const unsigned char *chosenData, uint32_t chosenSize) const {
// Primary replica calculated hash is different from a hash that passed pre-execution consensus => we don't have
// correct pre-processed results.
const auto &primaryHash =
Hash(SHA3_256().digest(primaryPreProcessResultHash_.data(), primaryPreProcessResultHash_.size())).toString();
const auto &hashPassedConsensus = Hash(SHA3_256().digest(chosenData, chosenSize)).toString();
const auto &primaryHash = Hash(concord::crypto::openssl::SHA3_256().digest(primaryPreProcessResultHash_.data(),
primaryPreProcessResultHash_.size()))
.toString();
const auto &hashPassedConsensus =
Hash(concord::crypto::openssl::SHA3_256().digest(chosenData, chosenSize)).toString();
LOG_WARN(logger(),
"Primary replica pre-processing result hash: "
<< primaryHash << " is different from one passed the consensus: " << hashPassedConsensus
Expand Down
Loading

0 comments on commit 541b4ea

Please sign in to comment.