Browse Source

Double SHA-3 as good crypto practice.

cl-refactor
Gav Wood 10 years ago
parent
commit
ec10d674b2
  1. 22
      libwhisper/Common.cpp
  2. 16
      libwhisper/Common.h
  3. 13
      libwhisper/Message.cpp
  4. 6
      libwhisper/Message.h
  5. 2
      libwhisper/WhisperHost.h

22
libwhisper/Common.cpp

@ -28,12 +28,26 @@ using namespace dev;
using namespace dev::p2p; using namespace dev::p2p;
using namespace dev::shh; using namespace dev::shh;
Topic BuildTopic::toTopic() const CollapsedTopicPart dev::shh::collapse(FullTopicPart const& _p)
{ {
Topic ret; return CollapsedTopicPart(sha3(_p));
}
CollapsedTopic dev::shh::collapse(FullTopic const& _fullTopic)
{
CollapsedTopic ret;
ret.reserve(_fullTopic.size());
for (auto const& ft: _fullTopic)
ret.push_back(collapse(ft));
return ret;
}
CollapsedTopic BuildTopic::toTopic() const
{
CollapsedTopic ret;
ret.reserve(m_parts.size()); ret.reserve(m_parts.size());
for (auto const& h: m_parts) for (auto const& h: m_parts)
ret.push_back(TopicPart(h)); ret.push_back(collapse(h));
return ret; return ret;
} }
@ -75,7 +89,7 @@ TopicMask BuildTopicMask::toTopicMask() const
TopicMask ret; TopicMask ret;
ret.reserve(m_parts.size()); ret.reserve(m_parts.size());
for (auto const& h: m_parts) for (auto const& h: m_parts)
ret.push_back(make_pair(TopicPart(h), ~TopicPart())); ret.push_back(make_pair(collapse(h), ~CollapsedTopicPart()));
return ret; return ret;
} }

16
libwhisper/Common.h

@ -59,11 +59,15 @@ enum WhisperPacket
PacketCount PacketCount
}; };
using TopicPart = FixedHash<4>; using CollapsedTopicPart = FixedHash<4>;
using FullTopicPart = h256;
using Topic = std::vector<TopicPart>; using CollapsedTopic = std::vector<CollapsedTopicPart>;
using FullTopic = h256s; using FullTopic = h256s;
CollapsedTopicPart collapse(FullTopicPart const& _fullTopicPart);
CollapsedTopic collapse(FullTopic const& _fullTopic);
class BuildTopic class BuildTopic
{ {
public: public:
@ -75,9 +79,9 @@ public:
BuildTopic& shiftRaw(h256 const& _part) { m_parts.push_back(_part); return *this; } BuildTopic& shiftRaw(h256 const& _part) { m_parts.push_back(_part); return *this; }
operator Topic() const { return toTopic(); } operator CollapsedTopic() const { return toTopic(); }
operator FullTopic() const { return toFullTopic(); } operator FullTopic() const { return toFullTopic(); }
Topic toTopic() const; CollapsedTopic toTopic() const;
FullTopic toFullTopic() const { return m_parts; } FullTopic toFullTopic() const { return m_parts; }
protected: protected:
@ -86,14 +90,14 @@ protected:
h256s m_parts; h256s m_parts;
}; };
using TopicMask = std::vector<std::pair<TopicPart, TopicPart>>; using TopicMask = std::vector<std::pair<CollapsedTopicPart, CollapsedTopicPart>>;
using TopicMasks = std::vector<TopicMask>; using TopicMasks = std::vector<TopicMask>;
class TopicFilter class TopicFilter
{ {
public: public:
TopicFilter() {} TopicFilter() {}
TopicFilter(FullTopic const& _m) { m_topicMasks.push_back(TopicMask()); for (auto const& h: _m) m_topicMasks.back().push_back(std::make_pair(TopicPart(h), h ? ~TopicPart() : TopicPart())); } TopicFilter(FullTopic const& _m) { m_topicMasks.push_back(TopicMask()); for (auto const& h: _m) m_topicMasks.back().push_back(std::make_pair(collapse(h), h ? ~CollapsedTopicPart() : CollapsedTopicPart())); }
TopicFilter(TopicMask const& _m): m_topicMasks(1, _m) {} TopicFilter(TopicMask const& _m): m_topicMasks(1, _m) {}
TopicFilter(TopicMasks const& _m): m_topicMasks(_m) {} TopicFilter(TopicMasks const& _m): m_topicMasks(_m) {}
TopicFilter(RLP const& _r)//: m_topicMasks(_r.toVector<std::vector<>>()) TopicFilter(RLP const& _r)//: m_topicMasks(_r.toVector<std::vector<>>())

13
libwhisper/Message.cpp

@ -26,15 +26,6 @@ using namespace dev;
using namespace dev::p2p; using namespace dev::p2p;
using namespace dev::shh; using namespace dev::shh;
Topic collapse(FullTopic const& _fullTopic)
{
Topic ret;
ret.reserve(_fullTopic.size());
for (auto const& ft: _fullTopic)
ret.push_back(TopicPart(ft));
return ret;
}
Message::Message(Envelope const& _e, FullTopic const& _fk, Secret const& _s) Message::Message(Envelope const& _e, FullTopic const& _fk, Secret const& _s)
{ {
try try
@ -51,7 +42,7 @@ Message::Message(Envelope const& _e, FullTopic const& _fk, Secret const& _s)
Secret topicSecret; Secret topicSecret;
// determine topicSecret/topicIndex from knowledge of the collapsed topics (which give the order) and our full-size filter topic. // determine topicSecret/topicIndex from knowledge of the collapsed topics (which give the order) and our full-size filter topic.
Topic knownTopic = collapse(_fk); CollapsedTopic knownTopic = collapse(_fk);
for (unsigned ti = 0; ti < _fk.size() && !topicSecret; ++ti) for (unsigned ti = 0; ti < _fk.size() && !topicSecret; ++ti)
for (unsigned i = 0; i < _e.topic().size(); ++i) for (unsigned i = 0; i < _e.topic().size(); ++i)
if (_e.topic()[i] == knownTopic[ti]) if (_e.topic()[i] == knownTopic[ti])
@ -105,7 +96,7 @@ bool Message::populate(bytes const& _data)
Envelope Message::seal(Secret _from, FullTopic const& _fullTopic, unsigned _ttl, unsigned _workToProve) const Envelope Message::seal(Secret _from, FullTopic const& _fullTopic, unsigned _ttl, unsigned _workToProve) const
{ {
Topic topic = collapse(_fullTopic); CollapsedTopic topic = collapse(_fullTopic);
Envelope ret(time(0) + _ttl, _ttl, topic); Envelope ret(time(0) + _ttl, _ttl, topic);
bytes input(1 + m_payload.size()); bytes input(1 + m_payload.size());

6
libwhisper/Message.h

@ -71,7 +71,7 @@ public:
unsigned sent() const { return m_expiry - m_ttl; } unsigned sent() const { return m_expiry - m_ttl; }
unsigned expiry() const { return m_expiry; } unsigned expiry() const { return m_expiry; }
unsigned ttl() const { return m_ttl; } unsigned ttl() const { return m_ttl; }
Topic const& topic() const { return m_topic; } CollapsedTopic const& topic() const { return m_topic; }
bytes const& data() const { return m_data; } bytes const& data() const { return m_data; }
Message open(FullTopic const& _ft, Secret const& _s = Secret()) const; Message open(FullTopic const& _ft, Secret const& _s = Secret()) const;
@ -80,13 +80,13 @@ public:
void proveWork(unsigned _ms); void proveWork(unsigned _ms);
private: private:
Envelope(unsigned _exp, unsigned _ttl, Topic const& _topic): m_expiry(_exp), m_ttl(_ttl), m_topic(_topic) {} Envelope(unsigned _exp, unsigned _ttl, CollapsedTopic const& _topic): m_expiry(_exp), m_ttl(_ttl), m_topic(_topic) {}
unsigned m_expiry = 0; unsigned m_expiry = 0;
unsigned m_ttl = 0; unsigned m_ttl = 0;
u256 m_nonce; u256 m_nonce;
Topic m_topic; CollapsedTopic m_topic;
bytes m_data; bytes m_data;
}; };

2
libwhisper/WhisperHost.h

@ -49,7 +49,7 @@ public:
WhisperHost(); WhisperHost();
virtual ~WhisperHost(); virtual ~WhisperHost();
unsigned protocolVersion() const { return 1; } unsigned protocolVersion() const { return 2; }
virtual void inject(Envelope const& _e, WhisperPeer* _from = nullptr) override; virtual void inject(Envelope const& _e, WhisperPeer* _from = nullptr) override;

Loading…
Cancel
Save