use fewer allocations

pull/173/head
Jeff Becker 6 years ago
parent 42e09b8ab9
commit e7f5eeff51
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -8,6 +8,12 @@ namespace llarp
{ {
} }
void
DHTImmeidateMessage::Clear()
{
msgs.clear();
}
bool bool
DHTImmeidateMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf) DHTImmeidateMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
{ {
@ -60,8 +66,9 @@ namespace llarp
bool bool
DHTImmeidateMessage::HandleMessage(llarp::Router *router) const DHTImmeidateMessage::HandleMessage(llarp::Router *router) const
{ {
DHTImmeidateMessage reply(session); DHTImmeidateMessage reply;
bool result = true; reply.session = session;
bool result = true;
for(auto &msg : msgs) for(auto &msg : msgs)
{ {
result &= msg->HandleMessage(router->dht, reply.msgs); result &= msg->HandleMessage(router->dht, reply.msgs);

@ -1 +1,5 @@
#include <encrypted.hpp> #include <encrypted.hpp>
#include <mem.hpp>
namespace llarp
{
} // namespace llarp

@ -4,6 +4,7 @@
#include <aligned.hpp> #include <aligned.hpp>
#include <bencode.h> #include <bencode.h>
#include <buffer.h> #include <buffer.h>
#include <mem.hpp>
#include <link_layer.hpp> #include <link_layer.hpp>
#include <vector> #include <vector>
@ -12,14 +13,55 @@
namespace llarp namespace llarp
{ {
/// encrypted buffer base type /// encrypted buffer base type
template < size_t bufsz = MAX_LINK_MSG_SIZE >
struct Encrypted struct Encrypted
{ {
Encrypted(Encrypted&& other); Encrypted(Encrypted&& other)
Encrypted(const Encrypted& other); {
Encrypted(); _sz = std::move(other._sz);
Encrypted(const byte_t* buf, size_t sz); memcpy(_buf, other._buf, _sz);
Encrypted(size_t sz); UpdateBuffer();
~Encrypted(); }
Encrypted(const Encrypted& other) : Encrypted(other.data(), other.size())
{
UpdateBuffer();
}
Encrypted()
{
Clear();
}
void
Clear()
{
_sz = 0;
UpdateBuffer();
}
Encrypted(const byte_t* buf, size_t sz)
{
if(sz <= bufsz)
{
_sz = sz;
if(buf)
memcpy(_buf, buf, sz);
else
llarp::Zero(_buf, sz);
}
else
_sz = 0;
UpdateBuffer();
}
Encrypted(size_t sz) : Encrypted(nullptr, sz)
{
}
~Encrypted()
{
}
bool bool
BEncode(llarp_buffer_t* buf) const BEncode(llarp_buffer_t* buf) const
@ -48,7 +90,7 @@ namespace llarp
Encrypted& Encrypted&
operator=(const llarp_buffer_t& buf) operator=(const llarp_buffer_t& buf)
{ {
if(buf.sz && buf.sz <= sizeof(_buf)) if(buf.sz <= sizeof(_buf))
{ {
_sz = buf.sz; _sz = buf.sz;
memcpy(_buf, buf.base, _sz); memcpy(_buf, buf.base, _sz);
@ -78,10 +120,11 @@ namespace llarp
llarp_buffer_t strbuf; llarp_buffer_t strbuf;
if(!bencode_read_string(buf, &strbuf)) if(!bencode_read_string(buf, &strbuf))
return false; return false;
if(strbuf.sz == 0 || strbuf.sz > sizeof(_buf)) if(strbuf.sz > sizeof(_buf))
return false; return false;
_sz = strbuf.sz; _sz = strbuf.sz;
memcpy(_buf, strbuf.base, _sz); if(_sz)
memcpy(_buf, strbuf.base, _sz);
UpdateBuffer(); UpdateBuffer();
return true; return true;
} }
@ -126,14 +169,14 @@ namespace llarp
void void
UpdateBuffer() UpdateBuffer()
{ {
m_Buffer.base = data(); m_Buffer.base = _buf;
m_Buffer.cur = data(); m_Buffer.cur = _buf;
m_Buffer.sz = size(); m_Buffer.sz = _sz;
} }
byte_t _buf[MAX_LINK_MSG_SIZE]; byte_t _buf[bufsz];
size_t _sz; size_t _sz;
llarp_buffer_t m_Buffer; llarp_buffer_t m_Buffer;
}; }; // namespace llarp
} // namespace llarp } // namespace llarp
#endif #endif

@ -7,12 +7,6 @@ namespace llarp
{ {
struct Crypto; struct Crypto;
struct EncryptedAck : public Encrypted
{
bool
DecryptInPlace(const byte_t* symkey, const byte_t* nonce,
llarp::Crypto* crypto);
};
} // namespace llarp } // namespace llarp
#endif #endif

@ -5,41 +5,6 @@
namespace llarp namespace llarp
{ {
Encrypted::Encrypted()
{
_sz = 0;
UpdateBuffer();
}
Encrypted::Encrypted(Encrypted&& other)
{
_sz = std::move(other._sz);
memcpy(_buf, other._buf, _sz);
UpdateBuffer();
}
Encrypted::Encrypted(const Encrypted& other)
: Encrypted(other.data(), other.size())
{
}
Encrypted::Encrypted(const byte_t* buf, size_t sz) : _sz(sz)
{
if(buf)
memcpy(_buf, buf, sz);
else
llarp::Zero(_buf, sz);
UpdateBuffer();
}
Encrypted::~Encrypted()
{
}
Encrypted::Encrypted(size_t sz) : Encrypted(nullptr, sz)
{
}
bool bool
EncryptedFrame::EncryptInPlace(const byte_t* ourSecretKey, EncryptedFrame::EncryptInPlace(const byte_t* ourSecretKey,
const byte_t* otherPubkey, const byte_t* otherPubkey,
@ -65,7 +30,7 @@ namespace llarp
llarp_buffer_t buf; llarp_buffer_t buf;
buf.base = body; buf.base = body;
buf.cur = buf.base; buf.cur = buf.base;
buf.sz = size() - EncryptedFrame::OverheadSize; buf.sz = size() - EncryptedFrameOverheadSize;
// set our pubkey // set our pubkey
memcpy(pubkey, llarp::seckey_topublic(ourSecretKey), PUBKEYSIZE); memcpy(pubkey, llarp::seckey_topublic(ourSecretKey), PUBKEYSIZE);
@ -103,12 +68,6 @@ namespace llarp
EncryptedFrame::DecryptInPlace(const byte_t* ourSecretKey, EncryptedFrame::DecryptInPlace(const byte_t* ourSecretKey,
llarp::Crypto* crypto) llarp::Crypto* crypto)
{ {
if(size() <= size_t(EncryptedFrame::OverheadSize))
{
llarp::LogWarn("encrypted frame too small, ", size(),
" <= ", size_t(EncryptedFrame::OverheadSize));
return false;
}
// format of frame is // format of frame is
// <32 bytes keyed hash of following data> // <32 bytes keyed hash of following data>
// <32 bytes nonce> // <32 bytes nonce>
@ -153,7 +112,7 @@ namespace llarp
buf.base = body; buf.base = body;
buf.cur = body; buf.cur = body;
buf.sz = size() - EncryptedFrame::OverheadSize; buf.sz = size() - EncryptedFrameOverheadSize;
if(!Decrypt(buf, shared, nonce)) if(!Decrypt(buf, shared, nonce))
{ {

@ -3,38 +3,37 @@
#include <crypto.h> #include <crypto.h>
#include <encrypted.hpp> #include <encrypted.hpp>
#include <buffer.hpp>
#include <mem.h> #include <mem.h>
#include <threadpool.h> #include <threadpool.h>
namespace llarp namespace llarp
{ {
struct EncryptedFrame : public Encrypted static constexpr size_t EncryptedFrameOverheadSize =
{ PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE;
static constexpr size_t OverheadSize = static constexpr size_t EncryptedFrameBodySize = 512;
PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE; static constexpr size_t EncryptedFrameSize =
EncryptedFrameOverheadSize + EncryptedFrameBodySize;
EncryptedFrame() : EncryptedFrame(256)
{
}
EncryptedFrame(const EncryptedFrame& other) struct EncryptedFrame : public Encrypted< EncryptedFrameSize >
: EncryptedFrame(other.data(), other.size()) {
EncryptedFrame() : EncryptedFrame(EncryptedFrameBodySize)
{ {
} }
EncryptedFrame(const byte_t* buf, size_t sz) : Encrypted(buf, sz)
{
}
EncryptedFrame(size_t sz) EncryptedFrame(size_t sz)
: Encrypted(sz + PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE) : Encrypted< EncryptedFrameSize >(std::min(sz, EncryptedFrameBodySize)
+ EncryptedFrameOverheadSize)
{ {
UpdateBuffer();
} }
EncryptedFrame& EncryptedFrame&
operator=(const EncryptedFrame& other) operator=(const EncryptedFrame& other)
{ {
_sz = other.size(); _sz = other._sz;
memcpy(data(), other.data(), size()); memcpy(data(), other.data(), size());
UpdateBuffer();
return *this; return *this;
} }
@ -58,8 +57,8 @@ namespace llarp
AsyncFrameEncrypter< User >* ctx = AsyncFrameEncrypter< User >* ctx =
static_cast< AsyncFrameEncrypter< User >* >(user); static_cast< AsyncFrameEncrypter< User >* >(user);
if(ctx->frame->EncryptInPlace(ctx->seckey, ctx->otherKey, ctx->crypto)) if(ctx->frame.EncryptInPlace(ctx->seckey, ctx->otherKey, ctx->crypto))
ctx->handler(ctx->frame, ctx->user); ctx->handler(&ctx->frame, ctx->user);
else else
{ {
ctx->handler(nullptr, ctx->user); ctx->handler(nullptr, ctx->user);
@ -69,7 +68,7 @@ namespace llarp
llarp::Crypto* crypto; llarp::Crypto* crypto;
byte_t* secretkey; byte_t* secretkey;
EncryptHandler handler; EncryptHandler handler;
EncryptedFrame* frame; EncryptedFrame frame;
User* user; User* user;
byte_t* otherKey; byte_t* otherKey;
@ -84,9 +83,10 @@ namespace llarp
{ {
// TODO: should we own otherKey? // TODO: should we own otherKey?
otherKey = other; otherKey = other;
frame = new EncryptedFrame(buf.sz); if(buf.sz > EncryptedFrameBodySize)
memcpy(frame->data() + PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE, return;
buf.base, buf.sz); memcpy(frame.data() + PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE, buf.base,
buf.sz);
user = u; user = u;
llarp_threadpool_queue_job(worker, {this, &Encrypt}); llarp_threadpool_queue_job(worker, {this, &Encrypt});
} }
@ -104,10 +104,10 @@ namespace llarp
AsyncFrameDecrypter< User >* ctx = AsyncFrameDecrypter< User >* ctx =
static_cast< AsyncFrameDecrypter< User >* >(user); static_cast< AsyncFrameDecrypter< User >* >(user);
if(ctx->target->DecryptInPlace(ctx->seckey, ctx->crypto)) if(ctx->target.DecryptInPlace(ctx->seckey, ctx->crypto))
{ {
auto buf = ctx->target->Buffer(); auto buf = ctx->target.Buffer();
buf->cur = buf->base + EncryptedFrame::OverheadSize; buf->cur = buf->base + EncryptedFrameOverheadSize;
ctx->result(buf, ctx->context); ctx->result(buf, ctx->context);
} }
else else
@ -124,10 +124,11 @@ namespace llarp
User* context; User* context;
llarp::Crypto* crypto; llarp::Crypto* crypto;
const byte_t* seckey; const byte_t* seckey;
EncryptedFrame* target; EncryptedFrame target;
void void
AsyncDecrypt(llarp_threadpool* worker, EncryptedFrame* frame, User* user) AsyncDecrypt(llarp_threadpool* worker, const EncryptedFrame& frame,
User* user)
{ {
target = frame; target = frame;
context = user; context = user;

@ -121,6 +121,12 @@ namespace llarp
return session->GotLIM(this); return session->GotLIM(this);
} }
void
LinkIntroMessage::Clear()
{
rc.Clear();
}
bool bool
LinkIntroMessage::Sign(llarp::Crypto* c, const SecretKey& k) LinkIntroMessage::Sign(llarp::Crypto* c, const SecretKey& k)
{ {

@ -2,6 +2,7 @@
#include <logger.hpp> #include <logger.hpp>
#include <messages.hpp> #include <messages.hpp>
#include <router_contact.hpp> #include <router_contact.hpp>
#include <link_message_parser.hpp>
namespace llarp namespace llarp
{ {
@ -45,29 +46,29 @@ namespace llarp
switch(*strbuf.cur) switch(*strbuf.cur)
{ {
case 'i': case 'i':
handler->msg = std::make_unique< LinkIntroMessage >(handler->from); handler->msg = &handler->holder.i;
break; break;
case 'd': case 'd':
handler->msg = handler->msg = &handler->holder.d;
std::make_unique< RelayDownstreamMessage >(handler->from);
break; break;
case 'u': case 'u':
handler->msg = handler->msg = &handler->holder.u;
std::make_unique< RelayUpstreamMessage >(handler->from);
break; break;
case 'm': case 'm':
handler->msg = std::make_unique< DHTImmeidateMessage >(handler->from); handler->msg = &handler->holder.m;
break; break;
case 'c': case 'c':
handler->msg = std::make_unique< LR_CommitMessage >(handler->from); handler->msg = &handler->holder.c;
break; break;
case 'x': case 'x':
handler->msg = std::make_unique< DiscardMessage >(handler->from); handler->msg = &handler->holder.x;
break; break;
default: default:
return false; return false;
} }
handler->firstkey = false; handler->msg->Clear();
handler->msg->session = handler->from;
handler->firstkey = false;
return true; return true;
} }
// check for last element // check for last element
@ -107,6 +108,6 @@ namespace llarp
void void
InboundMessageParser::Reset() InboundMessageParser::Reset()
{ {
msg.reset(nullptr); msg = nullptr;
} }
} // namespace llarp } // namespace llarp

@ -19,55 +19,22 @@ namespace llarp
struct ILinkMessage : public IBEncodeMessage struct ILinkMessage : public IBEncodeMessage
{ {
/// who did this message come from or is going to /// who did this message come from or is going to
ILinkSession* session; ILinkSession* session = nullptr;
uint64_t version = 0; uint64_t version = 0;
ILinkMessage() : ILinkMessage(nullptr) ILinkMessage() = default;
{
}
ILinkMessage(ILinkSession* from) : session(from)
{
}
virtual ~ILinkMessage() virtual ~ILinkMessage()
{ {
} }
virtual bool virtual bool
HandleMessage(Router* router) const = 0; HandleMessage(Router* router) const = 0;
};
struct InboundMessageParser
{
InboundMessageParser(Router* router);
dict_reader reader;
static bool virtual void
OnKey(dict_reader* r, llarp_buffer_t* buf); Clear() = 0;
/// start processig message from a link session
bool
ProcessFrom(ILinkSession* from, llarp_buffer_t buf);
/// called when the message is fully read
/// return true when the message was accepted otherwise returns false
bool
MessageDone();
/// resets internal state
void
Reset();
private:
RouterID
GetCurrentFrom();
private:
bool firstkey;
Router* router;
ILinkSession* from = nullptr;
std::unique_ptr< ILinkMessage > msg;
}; };
} // namespace llarp } // namespace llarp
#endif #endif

@ -0,0 +1,52 @@
#ifndef LLARP_LINK_MESSAGE_PARSER_HPP
#define LLARP_LINK_MESSAGE_PARSER_HPP
#include <link_message.hpp>
#include <messages.hpp>
namespace llarp
{
struct InboundMessageParser
{
InboundMessageParser(Router* router);
dict_reader reader;
static bool
OnKey(dict_reader* r, llarp_buffer_t* buf);
/// start processig message from a link session
bool
ProcessFrom(ILinkSession* from, llarp_buffer_t buf);
/// called when the message is fully read
/// return true when the message was accepted otherwise returns false
bool
MessageDone();
/// resets internal state
void
Reset();
private:
RouterID
GetCurrentFrom();
private:
bool firstkey;
Router* router;
ILinkSession* from = nullptr;
ILinkMessage* msg = nullptr;
struct msg_holder_t
{
LinkIntroMessage i;
RelayDownstreamMessage d;
RelayUpstreamMessage u;
DHTImmeidateMessage m;
LR_CommitMessage c;
DiscardMessage x;
};
msg_holder_t holder;
};
} // namespace llarp
#endif

@ -9,10 +9,6 @@ namespace llarp
{ {
struct DHTImmeidateMessage : public ILinkMessage struct DHTImmeidateMessage : public ILinkMessage
{ {
DHTImmeidateMessage(ILinkSession* parent) : ILinkMessage(parent)
{
}
DHTImmeidateMessage() : ILinkMessage() DHTImmeidateMessage() : ILinkMessage()
{ {
} }
@ -29,6 +25,9 @@ namespace llarp
bool bool
HandleMessage(llarp::Router* router) const; HandleMessage(llarp::Router* router) const;
void
Clear() override;
}; };
} // namespace llarp } // namespace llarp

@ -10,17 +10,7 @@ namespace llarp
{ {
struct DiscardMessage final : public ILinkMessage struct DiscardMessage final : public ILinkMessage
{ {
/// who did this message come from or is going to DiscardMessage() : ILinkMessage()
DiscardMessage() : ILinkMessage(nullptr)
{
}
DiscardMessage(ILinkSession* from) : ILinkMessage(from)
{
}
~DiscardMessage()
{ {
} }
@ -36,6 +26,11 @@ namespace llarp
return bencode_end(buf); return bencode_end(buf);
} }
void
Clear() override
{
}
bool bool
DecodeKey(__attribute__((unused)) llarp_buffer_t key, DecodeKey(__attribute__((unused)) llarp_buffer_t key,
__attribute__((unused)) llarp_buffer_t* buf) override __attribute__((unused)) llarp_buffer_t* buf) override

@ -16,10 +16,6 @@ namespace llarp
{ {
} }
LinkIntroMessage(ILinkSession* s) : ILinkMessage(s)
{
}
~LinkIntroMessage(); ~LinkIntroMessage();
RouterContact rc; RouterContact rc;
@ -44,6 +40,9 @@ namespace llarp
bool bool
Verify(llarp::Crypto* c) const; Verify(llarp::Crypto* c) const;
void
Clear() override;
}; };
} // namespace llarp } // namespace llarp

@ -13,7 +13,7 @@ namespace llarp
struct RelayUpstreamMessage : public ILinkMessage struct RelayUpstreamMessage : public ILinkMessage
{ {
PathID_t pathid; PathID_t pathid;
Encrypted X; Encrypted< MAX_LINK_MSG_SIZE - 128 > X;
TunnelNonce Y; TunnelNonce Y;
RelayUpstreamMessage(); RelayUpstreamMessage();
@ -21,32 +21,38 @@ namespace llarp
~RelayUpstreamMessage(); ~RelayUpstreamMessage();
bool bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf); DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
bool bool
BEncode(llarp_buffer_t* buf) const; BEncode(llarp_buffer_t* buf) const override;
bool bool
HandleMessage(llarp::Router* router) const; HandleMessage(llarp::Router* router) const override;
void
Clear() override;
}; };
struct RelayDownstreamMessage : public ILinkMessage struct RelayDownstreamMessage : public ILinkMessage
{ {
PathID_t pathid; PathID_t pathid;
Encrypted X; Encrypted< MAX_LINK_MSG_SIZE - 128 > X;
TunnelNonce Y; TunnelNonce Y;
RelayDownstreamMessage(); RelayDownstreamMessage();
RelayDownstreamMessage(ILinkSession* from); RelayDownstreamMessage(ILinkSession* from);
~RelayDownstreamMessage(); ~RelayDownstreamMessage();
bool bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf); DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
bool bool
BEncode(llarp_buffer_t* buf) const; BEncode(llarp_buffer_t* buf) const override;
bool bool
HandleMessage(llarp::Router* router) const; HandleMessage(llarp::Router* router) const override;
void
Clear() override;
}; };
} // namespace llarp } // namespace llarp

@ -53,10 +53,6 @@ namespace llarp
{ {
} }
LR_CommitMessage(ILinkSession *from) : ILinkMessage(from)
{
}
~LR_CommitMessage(); ~LR_CommitMessage();
void void

@ -11,11 +11,12 @@ namespace llarp
{ {
namespace routing namespace routing
{ {
constexpr size_t ExitPadSize = 512 - 48; constexpr size_t ExitPadSize = 512 - 48;
constexpr size_t MaxExitMTU = 1500; constexpr size_t MaxExitMTU = 1500;
constexpr size_t ExitOverhead = sizeof(uint64_t);
struct TransferTrafficMessage final : public IMessage struct TransferTrafficMessage final : public IMessage
{ {
std::vector< llarp::Encrypted > X; std::vector< llarp::Encrypted< MaxExitMTU + ExitOverhead > > X;
size_t _size = 0; size_t _size = 0;
size_t size_t

@ -80,12 +80,13 @@ namespace llarp
record.commkey = llarp::seckey_topublic(hop.commkey); record.commkey = llarp::seckey_topublic(hop.commkey);
auto buf = frame.Buffer(); auto buf = frame.Buffer();
buf->cur = buf->base + EncryptedFrame::OverheadSize; buf->cur = buf->base + EncryptedFrameOverheadSize;
// encode record // encode record
if(!record.BEncode(buf)) if(!record.BEncode(buf))
{ {
// failed to encode? // failed to encode?
llarp::LogError("Failed to generate Commit Record"); llarp::LogError("Failed to generate Commit Record");
llarp::DumpBuffer(*buf);
delete ctx; delete ctx;
return; return;
} }

@ -27,6 +27,19 @@ namespace llarp
return read; return read;
} }
void
LR_CommitMessage::Clear()
{
frames[0].Clear();
frames[1].Clear();
frames[2].Clear();
frames[3].Clear();
frames[4].Clear();
frames[5].Clear();
frames[6].Clear();
frames[7].Clear();
}
bool bool
LR_CommitMessage::BEncode(llarp_buffer_t* buf) const LR_CommitMessage::BEncode(llarp_buffer_t* buf) const
{ {
@ -235,7 +248,7 @@ namespace llarp
delete self; delete self;
return; return;
} }
buf->cur = buf->base + EncryptedFrame::OverheadSize; buf->cur = buf->base + EncryptedFrameOverheadSize;
llarp::LogDebug("decrypted LRCM from ", info.downstream); llarp::LogDebug("decrypted LRCM from ", info.downstream);
// successful decrypt // successful decrypt
if(!self->record.BDecode(buf)) if(!self->record.BDecode(buf))
@ -295,7 +308,7 @@ namespace llarp
frames[5] = self->frames[6]; frames[5] = self->frames[6];
frames[6] = self->frames[7]; frames[6] = self->frames[7];
// put our response on the end // put our response on the end
frames[7] = EncryptedFrame(sz - EncryptedFrame::OverheadSize); frames[7] = EncryptedFrame(sz - EncryptedFrameOverheadSize);
// random junk for now // random junk for now
frames[7].Randomize(); frames[7].Randomize();
self->frames = std::move(frames); self->frames = std::move(frames);
@ -325,7 +338,7 @@ namespace llarp
LRCMFrameDecrypt* frames = new LRCMFrameDecrypt(context, decrypter, this); LRCMFrameDecrypt* frames = new LRCMFrameDecrypt(context, decrypter, this);
// decrypt frames async // decrypt frames async
decrypter->AsyncDecrypt(context->Worker(), &frames->frames[0], frames); decrypter->AsyncDecrypt(context->Worker(), frames->frames[0], frames);
return true; return true;
} }
} // namespace llarp } // namespace llarp

@ -4,17 +4,18 @@
namespace llarp namespace llarp
{ {
RelayUpstreamMessage::RelayUpstreamMessage(ILinkSession *from) RelayUpstreamMessage::RelayUpstreamMessage() : ILinkMessage()
: ILinkMessage(from)
{ {
} }
RelayUpstreamMessage::RelayUpstreamMessage() : ILinkMessage() RelayUpstreamMessage::~RelayUpstreamMessage()
{ {
} }
RelayUpstreamMessage::~RelayUpstreamMessage() void
RelayUpstreamMessage::Clear()
{ {
X.Clear();
} }
bool bool
@ -63,18 +64,20 @@ namespace llarp
return false; return false;
} }
RelayDownstreamMessage::RelayDownstreamMessage(ILinkSession *from) RelayDownstreamMessage::RelayDownstreamMessage() : ILinkMessage()
: ILinkMessage(from)
{ {
} }
RelayDownstreamMessage::RelayDownstreamMessage() : ILinkMessage() RelayDownstreamMessage::~RelayDownstreamMessage()
{ {
} }
RelayDownstreamMessage::~RelayDownstreamMessage() void
RelayDownstreamMessage::Clear()
{ {
X.Clear();
} }
bool bool
RelayDownstreamMessage::BEncode(llarp_buffer_t *buf) const RelayDownstreamMessage::BEncode(llarp_buffer_t *buf) const
{ {

@ -12,7 +12,7 @@
#include <fs.hpp> #include <fs.hpp>
#include <handlers/tun.hpp> #include <handlers/tun.hpp>
#include <link_layer.hpp> #include <link_layer.hpp>
#include <link_message.hpp> #include <link_message_parser.hpp>
#include <logic.hpp> #include <logic.hpp>
#include <mem.hpp> #include <mem.hpp>
#include <nodedb.hpp> #include <nodedb.hpp>

@ -157,8 +157,8 @@ namespace llarp
const byte_t* sharedkey, const byte_t* sharedkey,
ProtocolMessage& msg) const ProtocolMessage& msg) const
{ {
Encrypted tmp = D; Encrypted_t tmp = D;
auto buf = tmp.Buffer(); auto buf = tmp.Buffer();
crypto->xchacha20(*buf, sharedkey, N); crypto->xchacha20(*buf, sharedkey, N);
return msg.BDecode(buf); return msg.BDecode(buf);
} }

@ -59,8 +59,9 @@ namespace llarp
/// outer message /// outer message
struct ProtocolFrame final : public llarp::routing::IMessage struct ProtocolFrame final : public llarp::routing::IMessage
{ {
using Encrypted_t = llarp::Encrypted< 2048 >;
llarp::PQCipherBlock C; llarp::PQCipherBlock C;
llarp::Encrypted D; Encrypted_t D;
llarp::KeyExchangeNonce N; llarp::KeyExchangeNonce N;
llarp::Signature Z; llarp::Signature Z;
llarp::service::ConvoTag T; llarp::service::ConvoTag T;

@ -11,11 +11,10 @@ using LRCR = llarp::LR_CommitRecord;
class FrameTest : public ::testing::Test class FrameTest : public ::testing::Test
{ {
public: public:
llarp::Crypto crypto; llarp::Crypto crypto;
SecretKey alice, bob; SecretKey alice, bob;
FrameTest() FrameTest() : crypto(llarp::Crypto::sodium{})
: crypto(llarp::Crypto::sodium{})
{ {
} }
@ -47,12 +46,12 @@ TEST_F(FrameTest, TestFrameCrypto)
record.txid.Fill(4); record.txid.Fill(4);
auto buf = f.Buffer(); auto buf = f.Buffer();
buf->cur = buf->base + EncryptedFrame::OverheadSize; buf->cur = buf->base + llarp::EncryptedFrameOverheadSize;
ASSERT_TRUE(record.BEncode(buf)); ASSERT_TRUE(record.BEncode(buf));
// rewind buffer // rewind buffer
buf->cur = buf->base + EncryptedFrame::OverheadSize; buf->cur = buf->base + llarp::EncryptedFrameOverheadSize;
// encrypt to alice // encrypt to alice
ASSERT_TRUE(f.EncryptInPlace(alice, llarp::seckey_topublic(bob), &crypto)); ASSERT_TRUE(f.EncryptInPlace(alice, llarp::seckey_topublic(bob), &crypto));
// decrypt from alice // decrypt from alice

Loading…
Cancel
Save