UDP.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 UDP.cpp
  15. * @author Alex Leverington <nessence@gmail.com>
  16. * @date 2014
  17. */
  18. #include "UDP.h"
  19. using namespace std;
  20. using namespace dev;
  21. using namespace dev::p2p;
  22. const char* RLPXWarn::name() { return "!X!"; }
  23. const char* RLPXNote::name() { return "-X-"; }
  24. h256 RLPXDatagramFace::sign(Secret const& _k)
  25. {
  26. assert(packetType());
  27. RLPStream rlpxstream;
  28. // rlpxstream.appendRaw(toPublic(_k).asBytes()); // for mdc-based signature
  29. rlpxstream.appendRaw(bytes(1, packetType())); // prefix by 1 byte for type
  30. streamRLP(rlpxstream);
  31. bytes rlpxBytes(rlpxstream.out());
  32. bytesConstRef rlpx(&rlpxBytes);
  33. h256 sighash(dev::sha3(rlpx)); // H(type||data)
  34. Signature sig = dev::sign(_k, sighash); // S(H(type||data))
  35. data.resize(h256::size + Signature::size + rlpx.size());
  36. bytesRef rlpxHash(&data[0], h256::size);
  37. bytesRef rlpxSig(&data[h256::size], Signature::size);
  38. bytesRef rlpxPayload(&data[h256::size + Signature::size], rlpx.size());
  39. sig.ref().copyTo(rlpxSig);
  40. rlpx.copyTo(rlpxPayload);
  41. bytesConstRef signedRLPx(&data[h256::size], data.size() - h256::size);
  42. dev::sha3(signedRLPx).ref().copyTo(rlpxHash);
  43. return sighash;
  44. }
  45. Public RLPXDatagramFace::authenticate(bytesConstRef _sig, bytesConstRef _rlp)
  46. {
  47. Signature const& sig = *(Signature const*)_sig.data();
  48. return dev::recover(sig, sha3(_rlp));
  49. }