1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/include/llarp/encrypted_frame.hpp

140 lines
3.4 KiB
C++
Raw Normal View History

#ifndef LLARP_ENCRYPTED_FRAME_HPP
#define LLARP_ENCRYPTED_FRAME_HPP
2018-06-10 16:05:48 +02:00
#include <llarp/crypto.h>
#include <llarp/mem.h>
2018-06-10 16:05:48 +02:00
#include <llarp/threadpool.h>
2018-06-11 15:25:10 +02:00
#include <llarp/encrypted.hpp>
namespace llarp
{
2018-06-11 15:25:10 +02:00
struct EncryptedFrame : public Encrypted
2018-06-10 16:05:48 +02:00
{
static constexpr size_t OverheadSize =
2018-06-19 00:05:02 +02:00
PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE;
2018-06-21 14:55:02 +02:00
EncryptedFrame() : EncryptedFrame(256)
{
}
EncryptedFrame(const EncryptedFrame& other)
: EncryptedFrame(other.data(), other.size())
2018-06-21 14:55:02 +02:00
{
}
EncryptedFrame(const byte_t* buf, size_t sz) : Encrypted(buf, sz)
2018-06-11 15:25:10 +02:00
{
}
2018-06-14 22:13:07 +02:00
EncryptedFrame(size_t sz)
: Encrypted(sz + PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE)
2018-06-11 15:25:10 +02:00
{
}
2018-06-10 16:05:48 +02:00
2018-06-21 14:55:02 +02:00
EncryptedFrame&
operator=(const EncryptedFrame& other)
{
2018-10-19 13:34:27 +02:00
_data.resize(other.size());
memcpy(data(), other.data(), size());
2018-06-21 14:55:02 +02:00
return *this;
}
2018-06-10 16:05:48 +02:00
bool
DecryptInPlace(const byte_t* seckey, llarp_crypto* crypto);
2018-06-11 15:25:10 +02:00
bool
EncryptInPlace(const byte_t* seckey, const byte_t* other,
llarp_crypto* crypto);
2018-06-11 15:25:10 +02:00
};
/// TOOD: can only handle 1 frame at a time
template < typename User >
struct AsyncFrameEncrypter
{
using EncryptHandler = std::function< void(EncryptedFrame*, User*) >;
2018-06-10 16:05:48 +02:00
2018-06-11 15:25:10 +02:00
static void
Encrypt(void* user)
2018-06-10 16:05:48 +02:00
{
2018-06-11 15:25:10 +02:00
AsyncFrameEncrypter< User >* ctx =
static_cast< AsyncFrameEncrypter< User >* >(user);
2018-06-11 15:44:49 +02:00
if(ctx->frame->EncryptInPlace(ctx->seckey, ctx->otherKey, ctx->crypto))
2018-06-11 15:25:10 +02:00
ctx->handler(ctx->frame, ctx->user);
else
{
ctx->handler(nullptr, ctx->user);
}
2018-06-10 16:05:48 +02:00
}
2018-06-11 15:25:10 +02:00
llarp_crypto* crypto;
byte_t* secretkey;
EncryptHandler handler;
EncryptedFrame* frame;
User* user;
2018-06-11 15:44:49 +02:00
byte_t* otherKey;
2018-06-11 15:25:10 +02:00
AsyncFrameEncrypter(llarp_crypto* c, byte_t* seckey, EncryptHandler h)
: crypto(c), secretkey(seckey), handler(h)
{
}
void
2018-06-11 15:44:49 +02:00
AsyncEncrypt(llarp_threadpool* worker, llarp_buffer_t buf, byte_t* other,
User* u)
2018-06-11 15:25:10 +02:00
{
2018-06-11 15:44:49 +02:00
// TODO: should we own otherKey?
otherKey = other;
frame = new EncryptedFrame(buf.sz);
2018-06-19 19:11:24 +02:00
memcpy(frame->data() + PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE,
buf.base, buf.sz);
2018-06-11 15:25:10 +02:00
user = u;
llarp_threadpool_queue_job(worker, {this, &Encrypt});
}
2018-06-10 16:05:48 +02:00
};
2018-06-11 15:25:10 +02:00
/// TOOD: can only handle 1 frame at a time
2018-06-10 16:05:48 +02:00
template < typename User >
struct AsyncFrameDecrypter
{
using DecryptHandler = std::function< void(llarp_buffer_t*, User*) >;
2018-06-10 16:05:48 +02:00
static void
Decrypt(void* user)
{
AsyncFrameDecrypter< User >* ctx =
static_cast< AsyncFrameDecrypter< User >* >(user);
if(ctx->target->DecryptInPlace(ctx->seckey, ctx->crypto))
2018-06-21 14:55:02 +02:00
{
auto buf = ctx->target->Buffer();
buf->cur = buf->base + EncryptedFrame::OverheadSize;
ctx->result(buf, ctx->context);
}
2018-06-10 16:05:48 +02:00
else
ctx->result(nullptr, ctx->context);
}
AsyncFrameDecrypter(llarp_crypto* c, const byte_t* secretkey,
DecryptHandler h)
2018-06-10 16:05:48 +02:00
: result(h), crypto(c), seckey(secretkey)
{
}
DecryptHandler result;
User* context;
llarp_crypto* crypto;
const byte_t* seckey;
2018-06-10 16:05:48 +02:00
EncryptedFrame* target;
2018-06-20 14:34:48 +02:00
2018-06-10 16:05:48 +02:00
void
AsyncDecrypt(llarp_threadpool* worker, EncryptedFrame* frame, User* user)
{
target = frame;
context = user;
llarp_threadpool_queue_job(worker, {this, &Decrypt});
}
};
2018-06-14 22:13:07 +02:00
} // namespace llarp
#endif