2019-01-13 22:39:10 +00:00
|
|
|
#ifndef LLARP_ENCRYPTED_HPP
|
|
|
|
#define LLARP_ENCRYPTED_HPP
|
2018-06-11 13:25:10 +00:00
|
|
|
|
2019-01-13 22:39:10 +00:00
|
|
|
#include <constants/link_layer.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/aligned.hpp>
|
|
|
|
#include <util/bencode.h>
|
2019-02-02 23:12:42 +00:00
|
|
|
#include <util/buffer.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/mem.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-06-19 17:11:24 +00:00
|
|
|
#include <vector>
|
2018-08-30 18:48:43 +00:00
|
|
|
#include <stdexcept>
|
2018-06-11 13:25:10 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
/// encrypted buffer base type
|
2018-12-20 16:49:05 +00:00
|
|
|
template < size_t bufsz = MAX_LINK_MSG_SIZE >
|
2018-06-11 13:25:10 +00:00
|
|
|
struct Encrypted
|
|
|
|
{
|
2018-12-20 16:49:05 +00:00
|
|
|
Encrypted(Encrypted&& other)
|
|
|
|
{
|
2019-01-07 12:27:29 +00:00
|
|
|
_sz = std::move(other._sz);
|
|
|
|
_buf = std::move(other._buf);
|
2018-12-20 16:49:05 +00:00
|
|
|
UpdateBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-01-07 12:27:29 +00:00
|
|
|
memcpy(_buf.data(), buf, sz);
|
2018-12-20 16:49:05 +00:00
|
|
|
else
|
2019-01-07 12:27:29 +00:00
|
|
|
_buf.Zero();
|
2018-12-20 16:49:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
_sz = 0;
|
|
|
|
UpdateBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
Encrypted(size_t sz) : Encrypted(nullptr, sz)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Encrypted()
|
|
|
|
{
|
|
|
|
}
|
2018-06-11 13:25:10 +00:00
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
bool
|
|
|
|
BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2019-01-07 12:27:29 +00:00
|
|
|
return bencode_write_bytestring(buf, data(), _sz);
|
2018-06-12 11:57:14 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 15:32:37 +00:00
|
|
|
bool
|
|
|
|
operator==(const Encrypted& other) const
|
|
|
|
{
|
2019-01-07 12:27:29 +00:00
|
|
|
return _sz == other._sz && memcmp(data(), other.data(), _sz) == 0;
|
2018-09-17 15:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator!=(const Encrypted& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:17:18 +00:00
|
|
|
Encrypted&
|
|
|
|
operator=(const Encrypted& other)
|
|
|
|
{
|
2019-02-03 00:31:10 +00:00
|
|
|
return Encrypted::operator=(llarp_buffer_t(other));
|
2018-08-14 21:17:18 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:25:30 +00:00
|
|
|
Encrypted&
|
2018-08-30 18:48:43 +00:00
|
|
|
operator=(const llarp_buffer_t& buf)
|
2018-06-22 00:25:30 +00:00
|
|
|
{
|
2019-01-07 12:27:29 +00:00
|
|
|
if(buf.sz <= _buf.size())
|
2018-08-14 21:17:18 +00:00
|
|
|
{
|
2018-12-20 15:03:48 +00:00
|
|
|
_sz = buf.sz;
|
2019-01-07 12:27:29 +00:00
|
|
|
memcpy(_buf.data(), buf.base, _sz);
|
2018-08-14 21:17:18 +00:00
|
|
|
}
|
2018-06-22 00:25:30 +00:00
|
|
|
UpdateBuffer();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-06-20 12:34:48 +00:00
|
|
|
void
|
|
|
|
Fill(byte_t fill)
|
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
std::fill(_buf.begin(), _buf.begin() + _sz, fill);
|
2018-06-20 12:34:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 16:45:12 +00:00
|
|
|
void
|
|
|
|
Randomize()
|
|
|
|
{
|
2018-09-06 11:46:19 +00:00
|
|
|
if(_sz)
|
2019-01-07 12:27:29 +00:00
|
|
|
randombytes(_buf.data(), _sz);
|
2018-06-12 16:45:12 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
if(!bencode_read_string(buf, &strbuf))
|
|
|
|
return false;
|
2018-12-20 16:49:05 +00:00
|
|
|
if(strbuf.sz > sizeof(_buf))
|
2018-06-12 11:57:14 +00:00
|
|
|
return false;
|
2018-12-20 15:03:48 +00:00
|
|
|
_sz = strbuf.sz;
|
2018-12-20 16:49:05 +00:00
|
|
|
if(_sz)
|
2019-01-07 12:27:29 +00:00
|
|
|
memcpy(_buf.data(), strbuf.base, _sz);
|
2018-06-21 12:55:02 +00:00
|
|
|
UpdateBuffer();
|
2018-06-12 11:57:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:25:10 +00:00
|
|
|
llarp_buffer_t*
|
|
|
|
Buffer()
|
|
|
|
{
|
|
|
|
return &m_Buffer;
|
|
|
|
}
|
|
|
|
|
2018-06-19 17:11:24 +00:00
|
|
|
size_t
|
|
|
|
size()
|
|
|
|
{
|
2018-12-20 15:03:48 +00:00
|
|
|
return _sz;
|
2018-06-19 17:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
size() const
|
|
|
|
{
|
2018-12-20 15:03:48 +00:00
|
|
|
return _sz;
|
2018-06-19 17:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
byte_t*
|
|
|
|
data()
|
|
|
|
{
|
2019-01-07 12:27:29 +00:00
|
|
|
return _buf.data();
|
2018-06-19 17:11:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 12:55:46 +00:00
|
|
|
const byte_t*
|
2018-10-19 11:34:27 +00:00
|
|
|
data() const
|
|
|
|
{
|
2019-01-07 12:27:29 +00:00
|
|
|
return _buf.data();
|
2018-10-19 11:34:27 +00:00
|
|
|
}
|
2018-10-23 12:55:46 +00:00
|
|
|
|
2018-06-21 12:55:02 +00:00
|
|
|
protected:
|
|
|
|
void
|
|
|
|
UpdateBuffer()
|
|
|
|
{
|
2019-01-07 12:27:29 +00:00
|
|
|
m_Buffer.base = _buf.data();
|
|
|
|
m_Buffer.cur = _buf.data();
|
2018-12-20 16:49:05 +00:00
|
|
|
m_Buffer.sz = _sz;
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
2019-01-07 12:27:29 +00:00
|
|
|
AlignedBuffer< bufsz > _buf;
|
2018-12-20 15:03:48 +00:00
|
|
|
size_t _sz;
|
2018-06-11 13:25:10 +00:00
|
|
|
llarp_buffer_t m_Buffer;
|
2018-12-20 16:49:05 +00:00
|
|
|
}; // namespace llarp
|
2018-06-19 17:11:24 +00:00
|
|
|
} // namespace llarp
|
2018-06-11 13:25:10 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
#endif
|