lokinet/llarp/exit/update_exit.cpp

120 lines
3.2 KiB
C++
Raw Normal View History

#include <messages/exit.hpp>
#include <crypto/crypto.hpp>
2018-12-12 02:04:32 +00:00
#include <routing/handler.hpp>
namespace llarp
{
namespace routing
{
bool
UpdateExitMessage::BEncode(llarp_buffer_t* buf) const
{
2018-11-14 12:23:08 +00:00
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictMsgType(buf, "A", "V"))
return false;
if(!BEncodeWriteDictEntry("P", P, buf))
return false;
if(!BEncodeWriteDictInt("S", S, buf))
return false;
if(!BEncodeWriteDictInt("T", T, buf))
return false;
if(!BEncodeWriteDictInt("V", version, buf))
return false;
if(!BEncodeWriteDictEntry("Z", Z, buf))
return false;
return bencode_end(buf);
}
bool
2019-02-02 23:12:42 +00:00
UpdateExitMessage::DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf)
{
2018-11-14 12:23:08 +00:00
bool read = false;
if(!BEncodeMaybeReadDictInt("S", S, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("T", T, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("V", version, read, k, buf))
return false;
if(!BEncodeMaybeReadDictEntry("P", P, read, k, buf))
return false;
if(!BEncodeMaybeReadDictEntry("Z", Z, read, k, buf))
return false;
return read;
}
bool
UpdateExitMessage::Verify(const llarp::PubKey& pk) const
2018-11-14 12:23:08 +00:00
{
2019-02-02 23:12:42 +00:00
std::array< byte_t, 512 > tmp;
llarp_buffer_t buf(tmp);
2018-11-14 12:23:08 +00:00
UpdateExitMessage copy;
copy = *this;
copy.Z.Zero();
if(!copy.BEncode(&buf))
return false;
buf.sz = buf.cur - buf.base;
return CryptoManager::instance()->verify(pk, buf, Z);
2018-11-14 12:23:08 +00:00
}
bool
UpdateExitMessage::Sign(const llarp::SecretKey& sk)
2018-11-14 12:23:08 +00:00
{
2019-02-02 23:12:42 +00:00
std::array< byte_t, 512 > tmp;
llarp_buffer_t buf(tmp);
2018-11-14 18:02:27 +00:00
Y.Randomize();
2018-11-14 12:23:08 +00:00
if(!BEncode(&buf))
return false;
buf.sz = buf.cur - buf.base;
return CryptoManager::instance()->sign(Z, sk, buf);
}
bool
UpdateExitMessage::HandleMessage(IMessageHandler* h,
AbstractRouter* r) const
{
return h->HandleUpdateExitMessage(*this, r);
}
2018-11-14 12:23:08 +00:00
bool
UpdateExitVerifyMessage::BEncode(llarp_buffer_t* buf) const
{
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictMsgType(buf, "A", "V"))
return false;
if(!BEncodeWriteDictInt("S", S, buf))
return false;
if(!BEncodeWriteDictInt("T", T, buf))
return false;
if(!BEncodeWriteDictInt("V", version, buf))
return false;
return bencode_end(buf);
}
bool
2019-02-02 23:12:42 +00:00
UpdateExitVerifyMessage::DecodeKey(const llarp_buffer_t& k,
llarp_buffer_t* buf)
2018-11-14 12:23:08 +00:00
{
bool read = false;
if(!BEncodeMaybeReadDictInt("S", S, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("T", T, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("V", version, read, k, buf))
return false;
return read;
}
bool
UpdateExitVerifyMessage::HandleMessage(IMessageHandler* h,
AbstractRouter* r) const
2018-11-14 12:23:08 +00:00
{
return h->HandleUpdateExitVerifyMessage(*this, r);
2018-11-14 12:23:08 +00:00
}
} // namespace routing
} // namespace llarp