2018-05-26 18:31:45 +00:00
|
|
|
#ifndef LLARP_ENCRYPTED_FRAME_HPP
|
|
|
|
#define LLARP_ENCRYPTED_FRAME_HPP
|
|
|
|
|
2019-01-13 22:39:10 +00:00
|
|
|
#include <crypto/encrypted.hpp>
|
2019-01-13 16:30:07 +00:00
|
|
|
#include <crypto/types.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/buffer.hpp>
|
|
|
|
#include <util/mem.h>
|
|
|
|
#include <util/threadpool.h>
|
2018-05-26 18:31:45 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-12-20 16:49:05 +00:00
|
|
|
static constexpr size_t EncryptedFrameOverheadSize =
|
|
|
|
PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE;
|
2019-04-29 18:06:16 +00:00
|
|
|
static constexpr size_t EncryptedFrameBodySize = 128 * 6;
|
2018-12-20 16:49:05 +00:00
|
|
|
static constexpr size_t EncryptedFrameSize =
|
|
|
|
EncryptedFrameOverheadSize + EncryptedFrameBodySize;
|
2018-06-21 12:55:02 +00:00
|
|
|
|
2018-12-20 16:49:05 +00:00
|
|
|
struct EncryptedFrame : public Encrypted< EncryptedFrameSize >
|
|
|
|
{
|
|
|
|
EncryptedFrame() : EncryptedFrame(EncryptedFrameBodySize)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-14 20:13:07 +00:00
|
|
|
EncryptedFrame(size_t sz)
|
2018-12-20 16:49:05 +00:00
|
|
|
: Encrypted< EncryptedFrameSize >(std::min(sz, EncryptedFrameBodySize)
|
|
|
|
+ EncryptedFrameOverheadSize)
|
2018-06-11 13:25:10 +00:00
|
|
|
{
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 15:06:39 +00:00
|
|
|
void
|
|
|
|
Resize(size_t sz)
|
|
|
|
{
|
|
|
|
if(sz <= EncryptedFrameSize)
|
|
|
|
{
|
|
|
|
_sz = sz;
|
|
|
|
UpdateBuffer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 14:05:48 +00:00
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
DecryptInPlace(const SecretKey& seckey);
|
2018-06-11 13:25:10 +00:00
|
|
|
|
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
EncryptInPlace(const SecretKey& seckey, const PubKey& other);
|
2018-06-10 14:05:48 +00:00
|
|
|
};
|
|
|
|
|
2019-01-02 01:04:04 +00:00
|
|
|
/// TODO: can only handle 1 frame at a time
|
2018-06-10 14:05:48 +00:00
|
|
|
template < typename User >
|
|
|
|
struct AsyncFrameDecrypter
|
|
|
|
{
|
2019-04-30 12:22:15 +00:00
|
|
|
using User_ptr = std::shared_ptr< User >;
|
|
|
|
using DecryptHandler = std::function< void(llarp_buffer_t*, User_ptr) >;
|
2018-06-10 14:05:48 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
Decrypt(void* user)
|
|
|
|
{
|
|
|
|
AsyncFrameDecrypter< User >* ctx =
|
|
|
|
static_cast< AsyncFrameDecrypter< User >* >(user);
|
|
|
|
|
2019-05-28 19:45:08 +00:00
|
|
|
if(ctx->target.DecryptInPlace(ctx->seckey))
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2018-12-20 16:49:05 +00:00
|
|
|
auto buf = ctx->target.Buffer();
|
|
|
|
buf->cur = buf->base + EncryptedFrameOverheadSize;
|
2019-04-30 12:22:15 +00:00
|
|
|
ctx->result(buf, ctx->user);
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
else
|
2019-04-30 12:22:15 +00:00
|
|
|
ctx->result(nullptr, ctx->user);
|
|
|
|
ctx->user = nullptr;
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:45:08 +00:00
|
|
|
AsyncFrameDecrypter(const SecretKey& secretkey, DecryptHandler h)
|
|
|
|
: result(h), seckey(secretkey)
|
2018-06-10 14:05:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DecryptHandler result;
|
2019-04-30 12:22:15 +00:00
|
|
|
User_ptr user;
|
2019-01-02 01:04:04 +00:00
|
|
|
const SecretKey& seckey;
|
2018-12-20 16:49:05 +00:00
|
|
|
EncryptedFrame target;
|
2018-06-20 12:34:48 +00:00
|
|
|
|
2018-06-10 14:05:48 +00:00
|
|
|
void
|
2018-12-20 16:49:05 +00:00
|
|
|
AsyncDecrypt(llarp_threadpool* worker, const EncryptedFrame& frame,
|
2019-04-30 12:22:15 +00:00
|
|
|
User_ptr u)
|
2018-06-10 14:05:48 +00:00
|
|
|
{
|
2019-04-30 12:22:15 +00:00
|
|
|
target = frame;
|
|
|
|
user = u;
|
2018-06-10 14:05:48 +00:00
|
|
|
llarp_threadpool_queue_job(worker, {this, &Decrypt});
|
|
|
|
}
|
|
|
|
};
|
2018-06-14 20:13:07 +00:00
|
|
|
} // namespace llarp
|
2018-05-26 18:31:45 +00:00
|
|
|
|
|
|
|
#endif
|