From ec10d674b2e61eabb22e2789921a96d73bf27fbd Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 29 Jan 2015 20:34:54 -0800 Subject: [PATCH] Double SHA-3 as good crypto practice. --- libwhisper/Common.cpp | 22 ++++++++++++++++++---- libwhisper/Common.h | 16 ++++++++++------ libwhisper/Message.cpp | 13 ++----------- libwhisper/Message.h | 6 +++--- libwhisper/WhisperHost.h | 2 +- 5 files changed, 34 insertions(+), 25 deletions(-) diff --git a/libwhisper/Common.cpp b/libwhisper/Common.cpp index bbc7ecdf7..f17ad638b 100644 --- a/libwhisper/Common.cpp +++ b/libwhisper/Common.cpp @@ -28,12 +28,26 @@ using namespace dev; using namespace dev::p2p; 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()); for (auto const& h: m_parts) - ret.push_back(TopicPart(h)); + ret.push_back(collapse(h)); return ret; } @@ -75,7 +89,7 @@ TopicMask BuildTopicMask::toTopicMask() const TopicMask ret; ret.reserve(m_parts.size()); for (auto const& h: m_parts) - ret.push_back(make_pair(TopicPart(h), ~TopicPart())); + ret.push_back(make_pair(collapse(h), ~CollapsedTopicPart())); return ret; } diff --git a/libwhisper/Common.h b/libwhisper/Common.h index e19c65ea9..8180b0ec4 100644 --- a/libwhisper/Common.h +++ b/libwhisper/Common.h @@ -59,11 +59,15 @@ enum WhisperPacket PacketCount }; -using TopicPart = FixedHash<4>; +using CollapsedTopicPart = FixedHash<4>; +using FullTopicPart = h256; -using Topic = std::vector; +using CollapsedTopic = std::vector; using FullTopic = h256s; +CollapsedTopicPart collapse(FullTopicPart const& _fullTopicPart); +CollapsedTopic collapse(FullTopic const& _fullTopic); + class BuildTopic { public: @@ -75,9 +79,9 @@ public: 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(); } - Topic toTopic() const; + CollapsedTopic toTopic() const; FullTopic toFullTopic() const { return m_parts; } protected: @@ -86,14 +90,14 @@ protected: h256s m_parts; }; -using TopicMask = std::vector>; +using TopicMask = std::vector>; using TopicMasks = std::vector; class TopicFilter { public: 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(TopicMasks const& _m): m_topicMasks(_m) {} TopicFilter(RLP const& _r)//: m_topicMasks(_r.toVector>()) diff --git a/libwhisper/Message.cpp b/libwhisper/Message.cpp index 74ce9475d..07bcea0c1 100644 --- a/libwhisper/Message.cpp +++ b/libwhisper/Message.cpp @@ -26,15 +26,6 @@ using namespace dev; using namespace dev::p2p; 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) { try @@ -51,7 +42,7 @@ Message::Message(Envelope const& _e, FullTopic const& _fk, Secret const& _s) Secret topicSecret; // 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 i = 0; i < _e.topic().size(); ++i) 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 { - Topic topic = collapse(_fullTopic); + CollapsedTopic topic = collapse(_fullTopic); Envelope ret(time(0) + _ttl, _ttl, topic); bytes input(1 + m_payload.size()); diff --git a/libwhisper/Message.h b/libwhisper/Message.h index b4b88b472..7e5df5a95 100644 --- a/libwhisper/Message.h +++ b/libwhisper/Message.h @@ -71,7 +71,7 @@ public: unsigned sent() const { return m_expiry - m_ttl; } unsigned expiry() const { return m_expiry; } 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; } Message open(FullTopic const& _ft, Secret const& _s = Secret()) const; @@ -80,13 +80,13 @@ public: void proveWork(unsigned _ms); 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_ttl = 0; u256 m_nonce; - Topic m_topic; + CollapsedTopic m_topic; bytes m_data; }; diff --git a/libwhisper/WhisperHost.h b/libwhisper/WhisperHost.h index 8111c6449..b6e683778 100644 --- a/libwhisper/WhisperHost.h +++ b/libwhisper/WhisperHost.h @@ -49,7 +49,7 @@ public: WhisperHost(); virtual ~WhisperHost(); - unsigned protocolVersion() const { return 1; } + unsigned protocolVersion() const { return 2; } virtual void inject(Envelope const& _e, WhisperPeer* _from = nullptr) override;