You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/crypto/encrypted_frame.hpp

152 lines
3.7 KiB
C++

#ifndef LLARP_ENCRYPTED_FRAME_HPP
#define LLARP_ENCRYPTED_FRAME_HPP
#include <crypto/encrypted.hpp>
#include <crypto/types.hpp>
#include <util/buffer.hpp>
#include <util/mem.h>
#include <util/threadpool.h>
namespace llarp
{
struct Crypto;
static constexpr size_t EncryptedFrameOverheadSize =
PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE;
static constexpr size_t EncryptedFrameBodySize = 1024;
static constexpr size_t EncryptedFrameSize =
EncryptedFrameOverheadSize + EncryptedFrameBodySize;
struct EncryptedFrame : public Encrypted< EncryptedFrameSize >
{
EncryptedFrame() : EncryptedFrame(EncryptedFrameBodySize)
{
}
EncryptedFrame(size_t sz)
: Encrypted< EncryptedFrameSize >(std::min(sz, EncryptedFrameBodySize)
+ EncryptedFrameOverheadSize)
6 years ago
{
UpdateBuffer();
6 years ago
}
EncryptedFrame&
operator=(const EncryptedFrame& other)
{
_sz = other._sz;
6 years ago
memcpy(data(), other.data(), size());
UpdateBuffer();
return *this;
}
void
Resize(size_t sz)
{
if(sz <= EncryptedFrameSize)
{
_sz = sz;
UpdateBuffer();
}
}
bool
DecryptInPlace(const SecretKey& seckey, llarp::Crypto* crypto);
6 years ago
bool
EncryptInPlace(const SecretKey& seckey, const PubKey& other,
llarp::Crypto* crypto);
6 years ago
};
/// TODO: can only handle 1 frame at a time
6 years ago
template < typename User >
struct AsyncFrameEncrypter
{
using EncryptHandler = std::function< void(EncryptedFrame*, User*) >;
6 years ago
static void
Encrypt(void* user)
{
6 years ago
AsyncFrameEncrypter< User >* ctx =
static_cast< AsyncFrameEncrypter< User >* >(user);
if(ctx->frame.EncryptInPlace(ctx->seckey, ctx->otherKey, ctx->crypto))
ctx->handler(&ctx->frame, ctx->user);
6 years ago
else
{
ctx->handler(nullptr, ctx->user);
}
}
llarp::Crypto* crypto;
6 years ago
byte_t* secretkey;
EncryptHandler handler;
EncryptedFrame frame;
6 years ago
User* user;
6 years ago
byte_t* otherKey;
6 years ago
AsyncFrameEncrypter(llarp::Crypto* c, byte_t* seckey, EncryptHandler h)
6 years ago
: crypto(c), secretkey(seckey), handler(h)
{
}
void
6 years ago
AsyncEncrypt(llarp_threadpool* worker, llarp_buffer_t buf, byte_t* other,
User* u)
6 years ago
{
6 years ago
// TODO: should we own otherKey?
otherKey = other;
if(buf.sz > EncryptedFrameBodySize)
return;
memcpy(frame.data() + EncryptedFrameOverheadSize, buf.base, buf.sz);
6 years ago
user = u;
llarp_threadpool_queue_job(worker, {this, &Encrypt});
}
};
/// TODO: can only handle 1 frame at a time
template < typename User >
struct AsyncFrameDecrypter
{
using DecryptHandler = std::function< void(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))
{
auto buf = ctx->target.Buffer();
buf->cur = buf->base + EncryptedFrameOverheadSize;
ctx->result(buf, ctx->context);
}
else
ctx->result(nullptr, ctx->context);
}
AsyncFrameDecrypter(llarp::Crypto* c, const SecretKey& secretkey,
DecryptHandler h)
: result(h), crypto(c), seckey(secretkey)
{
}
DecryptHandler result;
User* context;
llarp::Crypto* crypto;
const SecretKey& seckey;
EncryptedFrame target;
6 years ago
void
AsyncDecrypt(llarp_threadpool* worker, const EncryptedFrame& frame,
User* user)
{
target = frame;
context = user;
llarp_threadpool_queue_job(worker, {this, &Decrypt});
}
};
} // namespace llarp
#endif