Skip to content

Commit

Permalink
Review comments addressed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pankaj authored and arc-vmware committed May 25, 2022
1 parent e23b438 commit 16c6c50
Show file tree
Hide file tree
Showing 23 changed files with 446 additions and 82 deletions.
12 changes: 6 additions & 6 deletions bftengine/src/bcstatetransfer/BCStateTran.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3591,17 +3591,17 @@ void BCStateTran::checkStoredCheckpoints(uint64_t firstStoredCheckpoint, uint64_
void BCStateTran::computeDigestOfPage(
const uint32_t pageId, const uint64_t checkpointNumber, const char *page, uint32_t pageSize, Digest &outDigest) {
DigestGenerator digestGenerator;
digestGenerator.updateDigest(reinterpret_cast<const char *>(&pageId), sizeof(pageId));
digestGenerator.updateDigest(reinterpret_cast<const char *>(&checkpointNumber), sizeof(checkpointNumber));
digestGenerator.update(reinterpret_cast<const char *>(&pageId), sizeof(pageId));
digestGenerator.update(reinterpret_cast<const char *>(&checkpointNumber), sizeof(checkpointNumber));
if (checkpointNumber > 0) {
digestGenerator.updateDigest(page, pageSize);
digestGenerator.update(page, pageSize);
}
digestGenerator.writeDigest(reinterpret_cast<char *>(&outDigest));
}

void BCStateTran::computeDigestOfPagesDescriptor(const DataStore::ResPagesDescriptor *pagesDesc, Digest &outDigest) {
DigestGenerator digestGenerator;
digestGenerator.updateDigest(reinterpret_cast<const char *>(pagesDesc), pagesDesc->size());
digestGenerator.update(reinterpret_cast<const char *>(pagesDesc), pagesDesc->size());
digestGenerator.writeDigest(reinterpret_cast<char *>(&outDigest));
}

Expand All @@ -3612,8 +3612,8 @@ void BCStateTran::computeDigestOfBlockImpl(const uint64_t blockNum,
ConcordAssertGT(blockNum, 0);
ConcordAssertGT(blockSize, 0);
DigestGenerator digestGenerator;
digestGenerator.updateDigest(reinterpret_cast<const char *>(&blockNum), sizeof(blockNum));
digestGenerator.updateDigest(block, blockSize);
digestGenerator.update(reinterpret_cast<const char *>(&blockNum), sizeof(blockNum));
digestGenerator.update(block, blockSize);
digestGenerator.writeDigest(outDigest);
}

Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bcstatetransfer/RVBManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,8 @@ void RVBManager::computeDigestOfBlock(const uint64_t block_id,
ConcordAssertGT(block_id, 0);
ConcordAssertGT(block_size, 0);
DigestGenerator digest_generator;
digest_generator.updateDigest(reinterpret_cast<const char*>(&block_id), sizeof(block_id));
digest_generator.updateDigest(block, block_size);
digest_generator.update(reinterpret_cast<const char*>(&block_id), sizeof(block_id));
digest_generator.update(block, block_size);
digest_generator.writeDigest(out_digest);
}

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 @@ -150,8 +150,8 @@ const shared_ptr<char[]> RVBNode::computeNodeInitialValue(NodeInfo& node_info, c
ConcordAssertGT(node_info.id(), 0);
DigestGenerator digest_generator;

digest_generator.updateDigest(reinterpret_cast<const char*>(&node_info.id_data_), sizeof(node_info.id_data_));
digest_generator.updateDigest(data, data_size);
digest_generator.update(reinterpret_cast<const char*>(&node_info.id_data_), sizeof(node_info.id_data_));
digest_generator.update(data, data_size);
// TODO - Use default_delete in case memleak is reported by ASAN
static std::shared_ptr<char[]> out_digest_buff(new char[NodeVal::kDigestContextOutputSize]);
digest_generator.writeDigest(out_digest_buff.get());
Expand Down
2 changes: 1 addition & 1 deletion bftengine/src/bftengine/NullStateTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void NullStateTransfer::getDigestOfCheckpoint(uint64_t checkpointNumber,

Digest d;
DigestGenerator digestGenerator;
digestGenerator.computeDigest((char*)&checkpointNumber, sizeof(checkpointNumber), (char*)&d, sizeof(d));
digestGenerator.compute((char*)&checkpointNumber, sizeof(checkpointNumber), (char*)&d, sizeof(d));

memset(outStateDigest, 0, sizeOfDigestBuffer);
memset(outFullStateDigest, 0, sizeOfDigestBuffer);
Expand Down
22 changes: 6 additions & 16 deletions bftengine/src/bftengine/ReplicaImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1340,13 +1340,8 @@ void ReplicaImp::sendPartialProof(SeqNumInfo &seqNumInfo) {
else
commitSigner = CryptoManager::instance().thresholdSignerForOptimisticCommit(seqNum);

<<<<<<< HEAD
Digest tmpDigest;
Digest digestHelper;
=======
Digest tmpDigest, digestHelper;
>>>>>>> Digest generation using OpenSSL library (SHA2_256 & SHA3_256 algos).
digestHelper.calcCombination(ppDigest, getCurrentView(), seqNum, tmpDigest);
ppDigest.calcCombination(getCurrentView(), seqNum, tmpDigest);

const auto &span_context = pp->spanContext<std::remove_pointer<decltype(pp)>::type>();
part = new PartialCommitProofMsg(
Expand Down Expand Up @@ -1419,8 +1414,8 @@ void ReplicaImp::sendCommitPartial(const SeqNum s) {

LOG_INFO(CNSUS, "Sending CommitPartialMsg, sequence number:" << pp->seqNumber());

Digest digest, digestHelper;
digestHelper.digestOfDigest(pp->digestOfRequests(), digest);
Digest digest;
pp->digestOfRequests().digestOfDigest(digest);

auto prepareFullMsg = seqNumInfo.getValidPrepareFullMsg();

Expand Down Expand Up @@ -4095,13 +4090,8 @@ ReplicaImp::ReplicaImp(const LoadedReplicaData &ld,
else
commitSigner = CryptoManager::instance().thresholdSignerForOptimisticCommit(seqNum);

<<<<<<< HEAD
Digest tmpDigest;
Digest digestHelper;
=======
Digest tmpDigest, digestHelper;
>>>>>>> Digest generation using OpenSSL library (SHA2_256 & SHA3_256 algos).
digestHelper.calcCombination(ppDigest, getCurrentView(), seqNum, tmpDigest);
ppDigest.calcCombination(getCurrentView(), seqNum, tmpDigest);

PartialCommitProofMsg *p = new PartialCommitProofMsg(
config_.getreplicaId(), getCurrentView(), seqNum, pathInPrePrepare, tmpDigest, commitSigner);
Expand Down Expand Up @@ -4130,8 +4120,8 @@ ReplicaImp::ReplicaImp(const LoadedReplicaData &ld,
throw;
}

Digest digest, digestHelper;
digestHelper.digestOfDigest(e.getPrePrepareMsg()->digestOfRequests(), digest);
Digest digest;
e.getPrePrepareMsg()->digestOfRequests().digestOfDigest(digest);
CommitPartialMsg *c = CommitPartialMsg::create(getCurrentView(),
s,
config_.getreplicaId(),
Expand Down
9 changes: 3 additions & 6 deletions bftengine/src/bftengine/SeqNumInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ bool SeqNumInfo::addMsg(PrePrepareMsg* m, bool directAdd, bool isTimeCorrect) {

// set expected
Digest tmpDigest;
Digest digestHelper;
digestHelper.calcCombination(m->digestOfRequests(), m->viewNumber(), m->seqNumber(), tmpDigest);
m->digestOfRequests().calcCombination(m->viewNumber(), m->seqNumber(), tmpDigest);
if (!directAdd)
prepareSigCollector->setExpected(m->seqNumber(), m->viewNumber(), tmpDigest);
else
Expand All @@ -130,8 +129,7 @@ bool SeqNumInfo::addSelfMsg(PrePrepareMsg* m, bool directAdd) {

// set expected
Digest tmpDigest;
Digest digestHelper;
digestHelper.calcCombination(m->digestOfRequests(), m->viewNumber(), m->seqNumber(), tmpDigest);
m->digestOfRequests().calcCombination(m->viewNumber(), m->seqNumber(), tmpDigest);
if (!directAdd)
prepareSigCollector->setExpected(m->seqNumber(), m->viewNumber(), tmpDigest);
else
Expand Down Expand Up @@ -197,8 +195,7 @@ bool SeqNumInfo::addSelfCommitPartialMsgAndDigest(CommitPartialMsg* m, Digest& c
ConcordAssert(!forcedCompleted);

Digest tmpDigest;
Digest digestHelper;
digestHelper.calcCombination(commitDigest, m->viewNumber(), m->seqNumber(), tmpDigest);
commitDigest.calcCombination(m->viewNumber(), m->seqNumber(), tmpDigest);
bool r;
if (!directAdd) {
commitMsgsCollector->setExpected(m->seqNumber(), m->viewNumber(), tmpDigest);
Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bftengine/ViewChangeSafetyLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ bool ViewChangeSafetyLogic::computeRestrictionsForSeqNum(SeqNum s,

for (SlowElem slow : slowPathCertificates) {
ConcordAssert(s == slow.seqNum());
Digest d, digestHelper;
digestHelper.calcCombination(slow.prePrepreDigest(), slow.certificateView(), slow.seqNum(), d);
Digest d;
slow.prePrepreDigest().calcCombination(slow.certificateView(), slow.seqNum(), d);

bool valid = CryptoManager::instance().thresholdVerifierForSlowPathCommit(s)->verify(
d.content(), DIGEST_SIZE, slow.certificateSig(), slow.certificateSigLength());
Expand Down
4 changes: 2 additions & 2 deletions bftengine/src/bftengine/messages/PrePrepareMsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void PrePrepareMsg::calculateDigestOfRequests(Digest& digest) const {
tasks.push_back(threadPool.async(
[&sigOrDigestOfRequest, &digestBuffer, local_id](auto* request, auto requestLength) {
DigestGenerator digestGenerator;
digestGenerator.computeDigest(
digestGenerator.compute(
request, requestLength, digestBuffer.get() + local_id * sizeof(Digest), sizeof(Digest));
sigOrDigestOfRequest[local_id].first = digestBuffer.get() + local_id * sizeof(Digest);
sigOrDigestOfRequest[local_id].second = sizeof(Digest);
Expand All @@ -80,7 +80,7 @@ void PrePrepareMsg::calculateDigestOfRequests(Digest& digest) const {

// compute and set digest
DigestGenerator digestGenerator;
digestGenerator.computeDigest(sigOrDig.c_str(), sigOrDig.size(), (char*)&digest, sizeof(Digest));
digestGenerator.compute(sigOrDig.c_str(), sigOrDig.size(), (char*)&digest, sizeof(Digest));
} catch (std::out_of_range& ex) {
throw std::runtime_error(__PRETTY_FUNCTION__ + std::string(": digest threadpool"));
}
Expand Down
3 changes: 1 addition & 2 deletions bftengine/src/bftengine/messages/SignedShareMsgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ SignedShareBase* SignedShareBase::create(int16_t type,
m->b()->thresSigLength = (uint16_t)sigLen;

Digest tmpDigest;
Digest digestHelper;
digestHelper.calcCombination(digest, v, s, tmpDigest);
digest.calcCombination(v, s, tmpDigest);

auto position = m->body() + sizeof(Header);
std::memcpy(position, spanContext.data().data(), spanContext.data().size());
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 @@ -52,7 +52,7 @@ void ViewChangeMsg::getMsgDigest(Digest& outDigest) const {
auto bodySize = getBodySize();
bodySize += b()->sizeOfAllComplaints;
DigestGenerator digestGenerator;
digestGenerator.computeDigest(body(), bodySize, (char*)outDigest.content(), sizeof(Digest));
digestGenerator.compute(body(), bodySize, (char*)outDigest.content(), sizeof(Digest));
}

uint32_t ViewChangeMsg::getBodySize() const {
Expand Down
4 changes: 2 additions & 2 deletions bftengine/tests/messages/PrePrepareMsg_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ TEST_F(PrePrepareMsgTestFixture, finalize_and_validate) {
msg.addRequest(crm->body(), crm->size());
Digest d;
DigestGenerator digestGenerator;
digestGenerator.computeDigest(crm->body(), crm->size(), (char*)&d, sizeof(Digest));
digestGenerator.compute(crm->body(), crm->size(), (char*)&d, sizeof(Digest));
dv.push_back({d.content(), sizeof(Digest)});
}
EXPECT_NO_THROW(msg.finishAddingRequests()); // create the digest
Expand All @@ -151,7 +151,7 @@ TEST_F(PrePrepareMsgTestFixture, finalize_and_validate) {
}
Digest d;
DigestGenerator digestGenerator;
digestGenerator.computeDigest(dod.c_str(), dod.size(), (char*)&d, sizeof(Digest));
digestGenerator.compute(dod.c_str(), dod.size(), (char*)&d, sizeof(Digest));
EXPECT_EQ(d, msg.digestOfRequests());
EXPECT_NO_THROW(msg.validate(replicaInfo)); // validate the same digest
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool concord::kvbc::strategy::MangledPreProcessResultMsgStrategy::changeMessage(
} else {
Digest d;
DigestGenerator digestGenerator;
digestGenerator.computeDigest(req.body(), req.size(), reinterpret_cast<char*>(&d), sizeof(Digest));
digestGenerator.compute(req.body(), req.size(), reinterpret_cast<char*>(&d), sizeof(Digest));
sigOrDigestOfRequest[idx].append(d.content(), sizeof(Digest));
}
idx++;
Expand All @@ -71,7 +71,7 @@ bool concord::kvbc::strategy::MangledPreProcessResultMsgStrategy::changeMessage(

Digest d;
DigestGenerator digestGenerator;
digestGenerator.computeDigest(sigOrDig.c_str(), sigOrDig.size(), reinterpret_cast<char*>(&d), sizeof(Digest));
digestGenerator.compute(sigOrDig.c_str(), sigOrDig.size(), reinterpret_cast<char*>(&d), sizeof(Digest));
nmsg.digestOfRequests() = d;
LOG_INFO(logger_,
"Finally the PrePrepare Message with correlation id : "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool ShufflePrePrepareMsgStrategy::changeMessage(std::shared_ptr<MessageBase>& m
} else {
Digest d;
DigestGenerator digestGenerator;
digestGenerator.computeDigest(req.body(), req.size(), reinterpret_cast<char*>(&d), sizeof(Digest));
digestGenerator.compute(req.body(), req.size(), reinterpret_cast<char*>(&d), sizeof(Digest));
if (idx == swapIdx) {
sigOrDigestOfRequest[idx + 1].append(d.content(), sizeof(Digest));
} else if (idx == (swapIdx + 1)) {
Expand All @@ -98,7 +98,7 @@ bool ShufflePrePrepareMsgStrategy::changeMessage(std::shared_ptr<MessageBase>& m

Digest d;
DigestGenerator digestGenerator;
digestGenerator.computeDigest(sigOrDig.c_str(), sigOrDig.size(), reinterpret_cast<char*>(&d), sizeof(Digest));
digestGenerator.compute(sigOrDig.c_str(), sigOrDig.size(), reinterpret_cast<char*>(&d), sizeof(Digest));
nmsg.digestOfRequests() = d;
LOG_INFO(logger_,
"Finally the PrePrepare Message with correlation id : "
Expand Down
144 changes: 144 additions & 0 deletions util/include/DigestImpl.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Concord
//
// Copyright (c) 2022 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache 2.0 license (the "License"). You may not use this product except in
// compliance with the Apache 2.0 License.
//
// This product may include a number of subcomponents with separate copyright notices and license terms. Your use of
// these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE
// file.

#pragma once

#include <type_traits>
#include <openssl/evp.h>
#include <cstdint>
#include <memory>

#include "sha_hash.hpp"
#include "DigestType.hpp"
#include "hex_tools.h"

namespace concord::util::digest {

class DigestUtil {
public:
static size_t digestLength();
static bool compute(const char* input, size_t inputLength, char* outBufferForDigest, size_t lengthOfBufferForDigest);

class Context {
public:
Context();
void update(const char* data, size_t len);
void writeDigest(char* outDigest); // write digest to outDigest, and invalidate the Context object
~Context();

private:
void* internalState;
};
};

class DigestCreator {
public:
virtual ~DigestCreator() = default;

virtual void init() = 0;
virtual void compute() = 0;
virtual void update() = 0;
virtual void final() = 0;
};

template <typename CREATOR, typename = std::enable_if_t<std::is_base_of_v<DigestCreator, CREATOR>>>
class DigestHolder {
public:
DigestHolder() { std::memset(d, 0, DIGEST_SIZE); }
DigestHolder(unsigned char initVal) { std::memset(d, initVal, DIGEST_SIZE); }
DigestHolder(const char* other) { std::memcpy(d, other, DIGEST_SIZE); }
DigestHolder(char* buf, size_t len) { DigestUtil::compute(buf, len, (char*)d, DIGEST_SIZE); }
DigestHolder(const DigestHolder& other) { std::memcpy(d, other.d, DIGEST_SIZE); }

char* content() const { return (char*)d; } // Can be replaced by getForUpdate().
void makeZero() { std::memset(d, 0, DIGEST_SIZE); }
std::string toString() const { return concordUtils::bufferToHex(d, DIGEST_SIZE, false); }
void print() { printf("digest=[%s]", toString().c_str()); }
const char* const get() const { return d; }
char* getForUpdate() { return d; }

bool isZero() const {
for (int i = 0; i < DIGEST_SIZE; ++i) {
if (d[i] != 0) return false;
}
return true;
}

int hash() const {
uint64_t* p = (uint64_t*)d;
int h = (int)p[0];
return h;
}

bool operator==(const DigestHolder& other) const {
int r = std::memcmp(d, other.d, DIGEST_SIZE);
return (r == 0);
}

bool operator!=(const DigestHolder& other) const {
int r = std::memcmp(d, other.d, DIGEST_SIZE);
return (r != 0);
}

DigestHolder& operator=(const DigestHolder& other) {
if (this == &other) {
return *this;
}
std::memcpy(d, other.d, DIGEST_SIZE);
return *this;
}

static void digestOfDigest(const DigestHolder& inDigest, DigestHolder& outDigest) {
DigestUtil::compute(inDigest.d, sizeof(DigestHolder), outDigest.d, sizeof(DigestHolder));
}

static void calcCombination(const DigestHolder& inDigest, int64_t inDataA, int64_t inDataB, DigestHolder& outDigest) {
const size_t X = ((DIGEST_SIZE / sizeof(uint64_t)) / 2);

std::memcpy(outDigest.d, inDigest.d, DIGEST_SIZE);

uint64_t* ptr = (uint64_t*)outDigest.d;
size_t locationA = ptr[0] % X;
size_t locationB = (ptr[0] >> 8) % X;
ptr[locationA] = ptr[locationA] ^ (inDataA);
ptr[locationB] = ptr[locationB] ^ (inDataB);
}

private:
char d[DIGEST_SIZE]; // DIGEST_SIZE should be >= 8 bytes; // Stores digest.
};

// Implements digest using Crypto++ library.
class CryptoppDigestCreator : public DigestCreator {
public:
void init() override {}
void compute() override {}
void update() override {}
void final() override {}
virtual ~CryptoppDigestCreator() = default;
};

// Implements digest using OpenSSL library.
template <typename SHACTX,
typename = std::enable_if_t<std::is_same_v<SHACTX, concord::util::SHA2_256> ||
std::is_same_v<SHACTX, concord::util::SHA3_256>>>
class OpenSSLDigestCreator : public DigestCreator {
public:
virtual ~OpenSSLDigestCreator() = default;
void init() override {}
void compute() override {}
void update() override {}
void final() override {}

private:
SHACTX hash_ctx_;
};
} // namespace concord::util::digest
Loading

0 comments on commit 16c6c50

Please sign in to comment.