Message.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file Message.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include "Message.h"
  19. #include "BloomFilter.h"
  20. using namespace std;
  21. using namespace dev;
  22. using namespace dev::p2p;
  23. using namespace dev::shh;
  24. Message::Message(Envelope const& _e, Topics const& _t, Secret const& _s)
  25. {
  26. try
  27. {
  28. bytes b;
  29. if (_s)
  30. if (!decrypt(_s, &(_e.data()), b))
  31. return;
  32. else{}
  33. else if (!openBroadcastEnvelope(_e, _t, b))
  34. return;
  35. if (populate(b))
  36. if (_s)
  37. m_to = KeyPair(_s).pub();
  38. }
  39. catch (...) // Invalid secret? TODO: replace ... with InvalidSecret
  40. {
  41. }
  42. }
  43. bool Message::openBroadcastEnvelope(Envelope const& _e, Topics const& _fk, bytes& o_b)
  44. {
  45. // retrieve the key using the known topic and topicIndex.
  46. unsigned topicIndex = 0;
  47. Secret topicSecret;
  48. // determine topicSecret/topicIndex from knowledge of the collapsed topics (which give the order) and our full-size filter topic.
  49. AbridgedTopics knownTopic = abridge(_fk);
  50. for (unsigned ti = 0; ti < _fk.size() && !topicSecret; ++ti)
  51. for (unsigned i = 0; i < _e.topic().size(); ++i)
  52. if (_e.topic()[i] == knownTopic[ti])
  53. {
  54. topicSecret = Secret(_fk[ti]);
  55. topicIndex = i;
  56. break;
  57. }
  58. if (_e.data().size() < _e.topic().size() * h256::size)
  59. return false;
  60. unsigned index = topicIndex * 2;
  61. Secret encryptedKey(bytesConstRef(&(_e.data())).cropped(h256::size * index, h256::size));
  62. h256 salt = h256(bytesConstRef(&(_e.data())).cropped(h256::size * ++index, h256::size));
  63. Secret key = Secret(generateGamma(topicSecret, salt).makeInsecure() ^ encryptedKey.makeInsecure());
  64. bytesConstRef cipherText = bytesConstRef(&(_e.data())).cropped(h256::size * 2 * _e.topic().size());
  65. return decryptSym(key, cipherText, o_b);
  66. }
  67. bool Message::populate(bytes const& _data)
  68. {
  69. if (!_data.size())
  70. return false;
  71. byte flags = _data[0];
  72. if (!!(flags & ContainsSignature) && _data.size() >= sizeof(Signature) + 1) // has a signature
  73. {
  74. bytesConstRef payload = bytesConstRef(&_data).cropped(1, _data.size() - sizeof(Signature) - 1);
  75. h256 h = sha3(payload);
  76. Signature const& sig = *(Signature const*)&(_data[1 + payload.size()]);
  77. m_from = recover(sig, h);
  78. if (!m_from)
  79. return false;
  80. m_payload = payload.toBytes();
  81. }
  82. else
  83. m_payload = bytesConstRef(&_data).cropped(1).toBytes();
  84. return true;
  85. }
  86. Envelope Message::seal(Secret const& _from, Topics const& _fullTopics, unsigned _ttl, unsigned _workToProve) const
  87. {
  88. AbridgedTopics topics = abridge(_fullTopics);
  89. Envelope ret(utcTime() + _ttl, _ttl, topics);
  90. bytes input(1 + m_payload.size());
  91. input[0] = 0;
  92. memcpy(input.data() + 1, m_payload.data(), m_payload.size());
  93. if (_from) // needs a signature
  94. {
  95. input.resize(1 + m_payload.size() + sizeof(Signature));
  96. input[0] |= ContainsSignature;
  97. *(Signature*)&(input[1 + m_payload.size()]) = sign(_from, sha3(m_payload));
  98. // If this fails, the something is wrong with the sign-recover round-trip.
  99. assert(recover(*(Signature*)&(input[1 + m_payload.size()]), sha3(m_payload)) == KeyPair(_from).pub());
  100. }
  101. if (m_to)
  102. encrypt(m_to, &input, ret.m_data);
  103. else
  104. {
  105. // this message is for broadcast (could be read by anyone who knows at least one of the topics)
  106. // create the shared secret for encrypting the payload, then encrypt the shared secret with each topic
  107. Secret s = Secret::random();
  108. for (h256 const& t: _fullTopics)
  109. {
  110. h256 salt = h256::random();
  111. ret.m_data += (generateGamma(Secret(t), salt).makeInsecure() ^ s.makeInsecure()).ref().toBytes();
  112. ret.m_data += salt.asBytes();
  113. }
  114. bytes d;
  115. encryptSym(s, &input, d);
  116. ret.m_data += d;
  117. }
  118. ret.proveWork(_workToProve);
  119. return ret;
  120. }
  121. Envelope::Envelope(RLP const& _m)
  122. {
  123. m_expiry = _m[0].toInt<unsigned>();
  124. m_ttl = _m[1].toInt<unsigned>();
  125. m_topic = _m[2].toVector<FixedHash<4>>();
  126. m_data = _m[3].toBytes();
  127. m_nonce = _m[4].toInt<u256>();
  128. }
  129. Message Envelope::open(Topics const& _t, Secret const& _s) const
  130. {
  131. return Message(*this, _t, _s);
  132. }
  133. unsigned Envelope::workProved() const
  134. {
  135. h256 d[2];
  136. d[0] = sha3(WithoutNonce);
  137. d[1] = m_nonce;
  138. return dev::sha3(bytesConstRef(d[0].data(), 64)).firstBitSet();
  139. }
  140. void Envelope::proveWork(unsigned _ms)
  141. {
  142. h256 d[2];
  143. d[0] = sha3(WithoutNonce);
  144. unsigned bestBitSet = 0;
  145. bytesConstRef chuck(d[0].data(), 64);
  146. chrono::high_resolution_clock::time_point then = chrono::high_resolution_clock::now() + chrono::milliseconds(_ms);
  147. while (chrono::high_resolution_clock::now() < then)
  148. // do it rounds of 1024 for efficiency
  149. for (unsigned i = 0; i < 1024; ++i, ++d[1])
  150. {
  151. auto fbs = dev::sha3(chuck).firstBitSet();
  152. if (fbs > bestBitSet)
  153. {
  154. bestBitSet = fbs;
  155. m_nonce = (h256::Arith)d[1];
  156. }
  157. }
  158. }
  159. bool Envelope::matchesBloomFilter(TopicBloomFilterHash const& f) const
  160. {
  161. for (AbridgedTopic t: m_topic)
  162. if (f.contains(TopicBloomFilter::bloom(t)))
  163. return true;
  164. return false;
  165. }