Skip to content

Commit

Permalink
* make format
Browse files Browse the repository at this point in the history
* snode to snode direct traffic

* wire up dns on service node tun
  • Loading branch information
majestrate committed Dec 13, 2018
1 parent e787165 commit be234e4
Show file tree
Hide file tree
Showing 34 changed files with 539 additions and 202 deletions.
6 changes: 3 additions & 3 deletions daemon/rcutil.cpp
Expand Up @@ -261,7 +261,7 @@ main(int argc, char *argv[])

if(verifyMode)
{
llarp::Crypto crypto;
llarp::Crypto crypto;
llarp_crypto_init(&crypto);
if(!rc.Read(rcfname))
{
Expand Down Expand Up @@ -327,7 +327,7 @@ main(int argc, char *argv[])
// this is the only one...
if(listMode)
{
llarp::Crypto crypto;
llarp::Crypto crypto;
llarp_crypto_init(&crypto);
auto nodedb = llarp_nodedb_new(&crypto);
llarp_nodedb_iter itr;
Expand Down Expand Up @@ -467,7 +467,7 @@ main(int argc, char *argv[])

if(listMode)
{
llarp::Crypto crypto;
llarp::Crypto crypto;
// no longer used?
// llarp_crypto_libsodium_init(&crypto);
llarp_crypto_init(&crypto);
Expand Down
4 changes: 2 additions & 2 deletions include/llarp.hpp
Expand Up @@ -21,8 +21,8 @@ namespace llarp

int num_nethreads = 1;
bool singleThreaded = false;
std::unique_ptr<llarp::Crypto> crypto;
llarp::Router *router = nullptr;
std::unique_ptr< llarp::Crypto > crypto;
llarp::Router *router = nullptr;
llarp_threadpool *worker = nullptr;
llarp::Logic *logic = nullptr;
llarp_config *config = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions llarp/address_info.cpp
Expand Up @@ -25,15 +25,15 @@ namespace llarp
}

bool
operator==(const AddressInfo& lhs, const AddressInfo& rhs)
operator==(const AddressInfo &lhs, const AddressInfo &rhs)
{
// we don't care about rank
return lhs.pubkey == rhs.pubkey && lhs.port == rhs.port
&& lhs.dialect == rhs.dialect && lhs.ip == rhs.ip;
}

bool
operator<(const AddressInfo& lhs, const AddressInfo& rhs)
operator<(const AddressInfo &lhs, const AddressInfo &rhs)
{
return lhs.rank < rhs.rank || lhs.ip < rhs.ip || lhs.port < rhs.port;
}
Expand Down
1 change: 0 additions & 1 deletion llarp/address_info.hpp
Expand Up @@ -84,7 +84,6 @@ namespace llarp
bool
operator<(const AddressInfo& lhs, const AddressInfo& rhs);


} // namespace llarp

#endif
4 changes: 2 additions & 2 deletions llarp/context.cpp
Expand Up @@ -14,7 +14,6 @@
#include <pthread_np.h>
#endif


namespace llarp
{
Context::~Context()
Expand Down Expand Up @@ -81,7 +80,8 @@ namespace llarp
int
Context::LoadDatabase()
{
crypto = std::unique_ptr<llarp::Crypto>(new llarp::Crypto{llarp::Crypto::sodium{}});
crypto = std::unique_ptr< llarp::Crypto >(
new llarp::Crypto{llarp::Crypto::sodium{}});
nodedb = new llarp_nodedb(crypto.get());

if(!llarp_nodedb::ensure_dir(nodedb_dir.c_str()))
Expand Down
44 changes: 44 additions & 0 deletions llarp/crypto.cpp
@@ -1,4 +1,6 @@
#include <crypto.hpp>
#include <fstream>
#include <buffer.hpp>

namespace llarp
{
Expand All @@ -14,4 +16,46 @@ namespace llarp
char buf[(PUBKEYSIZE + 1) * 2] = {0};
return HexEncode(*this, buf);
}

bool
SecretKey::LoadFromFile(const char* fname)
{
std::ifstream f;
f.open(fname, std::ios::binary);
if(!f.is_open())
return false;
size_t sz = 0;
f.seekg(0, std::ios::end);
sz = f.tellg();
f.seekg(0, std::ios::beg);
if(sz == size())
{
// is raw buffer
f.read((char*)data(), 64);
return true;
}
byte_t tmp[128];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(sz > sizeof(tmp))
return false;
f.read((char*)tmp, sz);
return BDecode(&buf);
}

bool
SecretKey::SaveToFile(const char* fname) const
{
byte_t tmp[128];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(!BEncode(&buf))
return false;

std::ofstream f;
f.open(fname, std::ios::binary);
if(!f.is_open())
return false;
f.write((char*)buf.base, buf.cur - buf.base);
return true;
}

} // namespace llarp
180 changes: 92 additions & 88 deletions llarp/crypto.h
Expand Up @@ -14,17 +14,17 @@
* potentially allow libssl support in the future
*/

static constexpr uint32_t PUBKEYSIZE = 32;
static constexpr uint32_t SECKEYSIZE = 64;
static constexpr uint32_t NONCESIZE = 24;
static constexpr uint32_t PUBKEYSIZE = 32;
static constexpr uint32_t SECKEYSIZE = 64;
static constexpr uint32_t NONCESIZE = 24;
static constexpr uint32_t SHAREDKEYSIZE = 32;
static constexpr uint32_t HASHSIZE = 64;
static constexpr uint32_t HASHSIZE = 64;
static constexpr uint32_t SHORTHASHSIZE = 32;
static constexpr uint32_t HMACSECSIZE = 32;
static constexpr uint32_t SIGSIZE = 64;
static constexpr uint32_t TUNNONCESIZE = 32;
static constexpr uint32_t HMACSIZE = 32;
static constexpr uint32_t PATHIDSIZE = 16;
static constexpr uint32_t HMACSECSIZE = 32;
static constexpr uint32_t SIGSIZE = 64;
static constexpr uint32_t TUNNONCESIZE = 32;
static constexpr uint32_t HMACSIZE = 32;
static constexpr uint32_t PATHIDSIZE = 16;

#include <libntrup/ntru.h>

Expand All @@ -35,84 +35,88 @@ static constexpr uint32_t PATHIDSIZE = 16;

namespace llarp
{
/// label functors

/// PKE(result, publickey, secretkey, nonce)
using path_dh_func = std::function<bool(byte_t *, const byte_t *, const byte_t *,
const byte_t *)>;

/// TKE(result, publickey, secretkey, nonce)
using transport_dh_func = std::function<bool(byte_t *, const byte_t *,
const byte_t *, const byte_t *)>;

/// SD/SE(buffer, key, nonce)
using sym_cipher_func = std::function<bool(llarp_buffer_t, const byte_t *,
const byte_t *)>;

/// H(result, body)
using hash_func = std::function<bool(byte_t *, llarp_buffer_t)>;

/// SH(result, body)
using shorthash_func = std::function<bool(byte_t *, llarp_buffer_t)>;

/// MDS(result, body, shared_secret)
using hmac_func = std::function<bool(byte_t *, llarp_buffer_t, const byte_t *)>;

/// S(sig, secretkey, body)
using sign_func = std::function<bool(byte_t *, const byte_t *, llarp_buffer_t)>;

/// V(pubkey, body, sig)
using verify_func = std::function<bool(const byte_t *, llarp_buffer_t,
const byte_t *)>;

/// library crypto configuration
struct Crypto
{
/// xchacha symettric cipher
sym_cipher_func xchacha20;
/// path dh creator's side
path_dh_func dh_client;
/// path dh relay side
path_dh_func dh_server;
/// transport dh client side
transport_dh_func transport_dh_client;
/// transport dh server side
transport_dh_func transport_dh_server;
/// blake2b 512 bit
hash_func hash;
/// blake2b 256 bit
shorthash_func shorthash;
/// blake2s 256 bit hmac
hmac_func hmac;
/// ed25519 sign
sign_func sign;
/// ed25519 verify
verify_func verify;
/// randomize buffer
std::function<void(llarp_buffer_t)> randomize;
/// randomizer memory
std::function<void(void *, size_t)> randbytes;
/// generate signing keypair
std::function<void(byte_t *)> identity_keygen;
/// generate encryption keypair
std::function<void(byte_t *)> encryption_keygen;
/// generate post quantum encrytion key
std::function<void(byte_t *)> pqe_keygen;
/// post quantum decrypt (buffer, sharedkey_dst, sec)
std::function<bool(const byte_t *, byte_t *, const byte_t *)> pqe_decrypt;
/// post quantum encrypt (buffer, sharedkey_dst, pub)
std::function<bool(byte_t *, byte_t *, const byte_t *)> pqe_encrypt;

// Give a basic type tag for the constructor to pick libsodium
struct sodium {};

Crypto(Crypto::sodium tag);
};

/// return random 64bit unsigned interger
uint64_t
randint();

}
/// label functors

/// PKE(result, publickey, secretkey, nonce)
using path_dh_func = std::function< bool(byte_t *, const byte_t *,
const byte_t *, const byte_t *) >;

/// TKE(result, publickey, secretkey, nonce)
using transport_dh_func = std::function< bool(
byte_t *, const byte_t *, const byte_t *, const byte_t *) >;

/// SD/SE(buffer, key, nonce)
using sym_cipher_func =
std::function< bool(llarp_buffer_t, const byte_t *, const byte_t *) >;

/// H(result, body)
using hash_func = std::function< bool(byte_t *, llarp_buffer_t) >;

/// SH(result, body)
using shorthash_func = std::function< bool(byte_t *, llarp_buffer_t) >;

/// MDS(result, body, shared_secret)
using hmac_func =
std::function< bool(byte_t *, llarp_buffer_t, const byte_t *) >;

/// S(sig, secretkey, body)
using sign_func =
std::function< bool(byte_t *, const byte_t *, llarp_buffer_t) >;

/// V(pubkey, body, sig)
using verify_func =
std::function< bool(const byte_t *, llarp_buffer_t, const byte_t *) >;

/// library crypto configuration
struct Crypto
{
/// xchacha symettric cipher
sym_cipher_func xchacha20;
/// path dh creator's side
path_dh_func dh_client;
/// path dh relay side
path_dh_func dh_server;
/// transport dh client side
transport_dh_func transport_dh_client;
/// transport dh server side
transport_dh_func transport_dh_server;
/// blake2b 512 bit
hash_func hash;
/// blake2b 256 bit
shorthash_func shorthash;
/// blake2s 256 bit hmac
hmac_func hmac;
/// ed25519 sign
sign_func sign;
/// ed25519 verify
verify_func verify;
/// randomize buffer
std::function< void(llarp_buffer_t) > randomize;
/// randomizer memory
std::function< void(void *, size_t) > randbytes;
/// generate signing keypair
std::function< void(byte_t *) > identity_keygen;
/// generate encryption keypair
std::function< void(byte_t *) > encryption_keygen;
/// generate post quantum encrytion key
std::function< void(byte_t *) > pqe_keygen;
/// post quantum decrypt (buffer, sharedkey_dst, sec)
std::function< bool(const byte_t *, byte_t *, const byte_t *) > pqe_decrypt;
/// post quantum encrypt (buffer, sharedkey_dst, pub)
std::function< bool(byte_t *, byte_t *, const byte_t *) > pqe_encrypt;

// Give a basic type tag for the constructor to pick libsodium
struct sodium
{
};

Crypto(Crypto::sodium tag);
};

/// return random 64bit unsigned interger
uint64_t
randint();

} // namespace llarp

#endif
6 changes: 6 additions & 0 deletions llarp/crypto.hpp
Expand Up @@ -60,6 +60,12 @@ namespace llarp
return out << "[secretkey]";
}

bool
LoadFromFile(const char* fname);

bool
SaveToFile(const char* fname) const;

SecretKey&
operator=(const byte_t* ptr)
{
Expand Down

0 comments on commit be234e4

Please sign in to comment.