2018-05-26 18:31:45 +00:00
|
|
|
#ifndef LLARP_ENCRYPTED_FRAME_HPP
|
|
|
|
#define LLARP_ENCRYPTED_FRAME_HPP
|
|
|
|
|
2018-06-10 14:05:48 +00:00
|
|
|
#include <llarp/crypto.h>
|
2018-05-26 18:31:45 +00:00
|
|
|
#include <llarp/mem.h>
|
2018-06-10 14:05:48 +00:00
|
|
|
#include <llarp/threadpool.h>
|
2018-06-11 13:25:10 +00:00
|
|
|
#include <llarp/encrypted.hpp>
|
2018-05-26 18:31:45 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-06-11 13:25:10 +00:00
|
|
|
struct EncryptedFrame : public Encrypted
|
2018-06-10 14:05:48 +00:00
|
|
|
{
|
2018-06-11 13:25:10 +00:00
|
|
|
static constexpr size_t OverheadSize = sizeof(llarp_pubkey_t)
|
|
|
|
+ sizeof(llarp_tunnel_nonce_t) + sizeof(llarp_shorthash_t);
|
|
|
|
|
|
|
|
EncryptedFrame() = default;
|
|
|
|
EncryptedFrame(byte_t* buf, size_t sz) : Encrypted(buf, sz)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
EncryptedFrame(size_t sz) : Encrypted(sz + OverheadSize)
|
|
|
|
{
|
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
|
|
|
|
bool
|
2018-06-11 13:25:10 +00:00
|
|
|
DecryptInPlace(byte_t* seckey, llarp_crypto* crypto);
|
|
|
|
|
|
|
|
bool
|
2018-06-11 13:44:49 +00:00
|
|
|
EncryptInPlace(byte_t* seckey, byte_t* other, llarp_crypto* crypto);
|
2018-06-11 13:25:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// TOOD: can only handle 1 frame at a time
|
|
|
|
template < typename User >
|
|
|
|
struct AsyncFrameEncrypter
|
|
|
|
{
|
|
|
|
typedef void (*EncryptHandler)(EncryptedFrame*, User*);
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-06-11 13:25:10 +00:00
|
|
|
static void
|
|
|
|
Encrypt(void* user)
|
2018-06-10 14:05:48 +00:00
|
|
|
{
|
2018-06-11 13:25:10 +00:00
|
|
|
AsyncFrameEncrypter< User >* ctx =
|
|
|
|
static_cast< AsyncFrameEncrypter< User >* >(user);
|
|
|
|
|
2018-06-11 13:44:49 +00:00
|
|
|
if(ctx->frame->EncryptInPlace(ctx->seckey, ctx->otherKey, ctx->crypto))
|
2018-06-11 13:25:10 +00:00
|
|
|
ctx->handler(ctx->frame, ctx->user);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete ctx->frame;
|
|
|
|
ctx->handler(nullptr, ctx->user);
|
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 13:25:10 +00:00
|
|
|
llarp_crypto* crypto;
|
|
|
|
byte_t* secretkey;
|
|
|
|
EncryptHandler handler;
|
|
|
|
EncryptedFrame* frame;
|
|
|
|
User* user;
|
2018-06-11 13:44:49 +00:00
|
|
|
byte_t* otherKey;
|
2018-06-11 13:25:10 +00:00
|
|
|
|
|
|
|
AsyncFrameEncrypter(llarp_crypto* c, byte_t* seckey, EncryptHandler h)
|
|
|
|
: crypto(c), secretkey(seckey), handler(h)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-06-11 13:44:49 +00:00
|
|
|
AsyncEncrypt(llarp_threadpool* worker, llarp_buffer_t buf, byte_t* other,
|
|
|
|
User* u)
|
2018-06-11 13:25:10 +00:00
|
|
|
{
|
2018-06-11 13:44:49 +00:00
|
|
|
// TODO: should we own otherKey?
|
|
|
|
otherKey = other;
|
|
|
|
frame = new EncryptedFrame(buf.sz);
|
2018-06-11 13:25:10 +00:00
|
|
|
memcpy(frame->data + EncryptedFrame::OverheadSize, buf.base, buf.sz);
|
|
|
|
user = u;
|
|
|
|
llarp_threadpool_queue_job(worker, {this, &Encrypt});
|
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
};
|
|
|
|
|
2018-06-11 13:25:10 +00:00
|
|
|
/// TOOD: can only handle 1 frame at a time
|
2018-06-10 14:05:48 +00:00
|
|
|
template < typename User >
|
|
|
|
struct AsyncFrameDecrypter
|
|
|
|
{
|
|
|
|
typedef void (*DecryptHandler)(llarp_buffer_t*, User*);
|
|
|
|
|
|
|
|
static void
|
|
|
|
Decrypt(void* user)
|
|
|
|
{
|
|
|
|
AsyncFrameDecrypter< User >* ctx =
|
|
|
|
static_cast< AsyncFrameDecrypter< User >* >(user);
|
|
|
|
|
|
|
|
if(ctx->target->DecryptInPlace(ctx->seckey, ctx->crypto))
|
|
|
|
ctx->result(ctx->target->Buffer(), ctx->context);
|
|
|
|
else
|
|
|
|
ctx->result(nullptr, ctx->context);
|
2018-06-11 13:25:10 +00:00
|
|
|
delete ctx->target;
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 13:25:10 +00:00
|
|
|
AsyncFrameDecrypter(llarp_crypto* c, byte_t* secretkey, DecryptHandler h)
|
2018-06-10 14:05:48 +00:00
|
|
|
: result(h), crypto(c), seckey(secretkey)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DecryptHandler result;
|
|
|
|
User* context;
|
|
|
|
llarp_crypto* crypto;
|
2018-06-11 13:25:10 +00:00
|
|
|
byte_t* seckey;
|
2018-06-10 14:05:48 +00:00
|
|
|
EncryptedFrame* target;
|
|
|
|
void
|
|
|
|
AsyncDecrypt(llarp_threadpool* worker, EncryptedFrame* frame, User* user)
|
|
|
|
{
|
|
|
|
target = frame;
|
|
|
|
context = user;
|
|
|
|
llarp_threadpool_queue_job(worker, {this, &Decrypt});
|
|
|
|
}
|
|
|
|
};
|
2018-05-26 18:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|