lokinet/llarp/encrypted.hpp

183 lines
2.8 KiB
C++
Raw Normal View History

2018-06-11 13:25:10 +00:00
#ifndef LLARP_ENCCRYPTED_HPP
#define LLARP_ENCCRYPTED_HPP
#include <aligned.hpp>
#include <bencode.h>
#include <buffer.h>
2018-12-20 16:49:05 +00:00
#include <mem.hpp>
2018-12-20 15:03:48 +00:00
#include <link_layer.hpp>
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)
{
_sz = std::move(other._sz);
memcpy(_buf, other._buf, _sz);
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)
memcpy(_buf, buf, sz);
else
llarp::Zero(_buf, sz);
}
else
_sz = 0;
UpdateBuffer();
}
Encrypted(size_t sz) : Encrypted(nullptr, sz)
{
}
~Encrypted()
{
}
2018-06-11 13:25:10 +00:00
bool
BEncode(llarp_buffer_t* buf) const
{
2018-12-20 15:03:48 +00:00
return bencode_write_bytestring(buf, _buf, _sz);
}
2018-09-17 15:32:37 +00:00
bool
operator==(const Encrypted& other) const
{
2018-12-20 15:03:48 +00:00
return _sz == other._sz && memcmp(_buf, other._buf, _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)
{
return (*this) = other.Buffer();
}
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
{
2018-12-20 16:49:05 +00:00
if(buf.sz <= sizeof(_buf))
2018-08-14 21:17:18 +00:00
{
2018-12-20 15:03:48 +00:00
_sz = buf.sz;
memcpy(_buf, 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)
{
2018-06-21 12:55:02 +00:00
size_t idx = 0;
while(idx < _sz)
2018-12-20 15:03:48 +00:00
_buf[idx++] = 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)
2018-12-20 15:03:48 +00:00
randombytes(_buf, _sz);
2018-06-12 16:45:12 +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))
return false;
2018-12-20 15:03:48 +00:00
_sz = strbuf.sz;
2018-12-20 16:49:05 +00:00
if(_sz)
memcpy(_buf, strbuf.base, _sz);
2018-06-21 12:55:02 +00:00
UpdateBuffer();
return true;
}
2018-06-11 13:25:10 +00:00
llarp_buffer_t*
Buffer()
{
return &m_Buffer;
}
2018-06-22 00:25:30 +00:00
llarp_buffer_t
Buffer() const
{
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()
{
2018-12-20 15:03:48 +00:00
return _buf;
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
{
2018-12-20 15:03:48 +00:00
return _buf;
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()
{
2018-12-20 16:49:05 +00:00
m_Buffer.base = _buf;
m_Buffer.cur = _buf;
m_Buffer.sz = _sz;
2018-06-21 12:55:02 +00:00
}
2018-12-20 16:49:05 +00:00
byte_t _buf[bufsz];
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