refactor to use AlignedBuffer for public key, shared secret, nonce, secret key etc

pull/1/head
Jeff Becker 6 years ago
parent b354dbd492
commit a8ab66f4f8
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -203,7 +203,7 @@ main(int argc, char *argv[])
llarp_crypto crypt;
llarp_crypto_libsodium_init(&crypt);
fs::path ident_keyfile = "identity.key";
llarp_seckey_t identity;
byte_t identity[SECKEYSIZE];
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity);
// get identity public key
uint8_t *pubkey = llarp::seckey_topublic(identity);
@ -251,7 +251,7 @@ main(int argc, char *argv[])
llarp_crypto crypt;
llarp_crypto_libsodium_init(&crypt);
fs::path ident_keyfile = "identity.key";
llarp_seckey_t identity;
byte_t identity[SECKEYSIZE];
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity);
// get identity public key
uint8_t *pubkey = llarp::seckey_topublic(identity);

@ -22,7 +22,7 @@ struct llarp_ai
{
uint16_t rank;
char dialect[MAX_AI_DIALECT_SIZE + 1];
llarp_pubkey_t enc_key;
byte_t enc_key[PUBKEYSIZE];
struct in6_addr ip;
uint16_t port;
};

@ -1,6 +1,7 @@
#ifndef LLARP_ALIGNED_HPP
#define LLARP_ALIGNED_HPP
#include <llarp/bencode.h>
#include <llarp/crypto.h>
#include <sodium.h>
#include <iomanip>
@ -59,6 +60,12 @@ namespace llarp
return sz;
}
size_t
size()
{
return sz;
}
void
Zero()
{
@ -106,6 +113,24 @@ namespace llarp
return &b[0];
}
bool
BEncode(llarp_buffer_t* buf) const
{
return bencode_write_bytestring(buf, b, sz);
}
bool
BDecode(llarp_buffer_t* buf)
{
llarp_buffer_t strbuf;
if(!bencode_read_string(buf, &strbuf))
return false;
if(strbuf.sz != sz)
return false;
memcpy(b, strbuf.base, sz);
return true;
}
private:
union {
byte_t b[sz];

@ -0,0 +1,101 @@
#ifndef LLARP_BENCODE_HPP
#define LLARP_BENCODE_HPP
#include <llarp/bencode.h>
namespace llarp
{
bool
BEncodeWriteDictMsgType(llarp_buffer_t* buf, const char* k, const char* t)
{
return bencode_write_bytestring(buf, k, 1)
&& bencode_write_bytestring(buf, t, 1);
}
template < typename Obj_t >
bool
BEncodeWriteDictEntry(const char* k, const Obj_t& o, llarp_buffer_t* buf)
{
return bencode_write_bytestring(buf, k, 1) && o.BEncode(buf);
}
template < typename Item_t >
bool
BEncodeRead(Item_t& item, llarp_buffer_t* buf);
template < typename Item_t >
bool
BEncodeMaybeReadDictEntry(const char* k, Item_t& item, bool& read,
llarp_buffer_t key, llarp_buffer_t* buf)
{
llarp_buffer_t strbuf;
if(llarp_buffer_eq(key, k))
{
if(!bencode_read_string(buf, &strbuf))
return false;
if(!item.BDecode(buf))
return false;
read = true;
return true;
}
return true;
}
template < typename Item_t >
bool
BEncodeMaybeReadVersion(const char* k, Item_t& item, uint64_t expect,
bool& read, llarp_buffer_t key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, k))
{
if(!bencode_read_integer(buf, &item))
return false;
read = item == expect;
return true;
}
return true;
}
template < typename Iter >
bool
BEncodeWriteList(Iter itr, Iter end, llarp_buffer_t* buf)
{
if(!bencode_start_list(buf))
return false;
while(itr != end)
if(!itr->BEncode(buf))
return false;
else
++itr;
return bencode_end(buf);
}
template < typename List_t >
bool
BEncodeReadList(List_t& result, llarp_buffer_t* buf)
{
if(*buf->cur != 'l') // ensure is a list
return false;
buf->cur++;
while(llarp_buffer_size_left(*buf) && *buf->cur != 'e')
{
if(!result.emplace(result.end())->BDecode(buf))
return false;
}
if(*buf->cur != 'e') // make sure we're at a list end
return false;
buf->cur++;
return true;
}
template < typename List_t >
bool
BEncodeWriteDictList(const char* k, List_t& list, llarp_buffer_t* buf)
{
return bencode_write_bytestring(buf, k, 1)
&& BEncodeWriteList(list.begin(), list.end(), buf);
}
}
#endif

@ -27,6 +27,7 @@ extern "C" {
#define TUNNONCESIZE 32
#define HMACSIZE 32
/*
typedef byte_t llarp_pubkey_t[PUBKEYSIZE];
typedef byte_t llarp_seckey_t[SECKEYSIZE];
typedef byte_t llarp_nonce_t[NONCESIZE];
@ -37,6 +38,7 @@ typedef byte_t llarp_hmac_t[HMACSIZE];
typedef byte_t llarp_hmacsec_t[HMACSECSIZE];
typedef byte_t llarp_sig_t[SIGSIZE];
typedef byte_t llarp_tunnel_nonce_t[TUNNONCESIZE];
*/
/// label functors
@ -47,8 +49,8 @@ typedef bool (*llarp_path_dh_func)(byte_t *, byte_t *, byte_t *, byte_t *);
typedef bool (*llarp_transport_dh_func)(byte_t *, byte_t *, byte_t *, byte_t *);
/// SD/SE(buffer, key, nonce)
typedef bool (*llarp_sym_cipher_func)(llarp_buffer_t, llarp_sharedkey_t,
llarp_nonce_t);
typedef bool (*llarp_sym_cipher_func)(llarp_buffer_t, const byte_t *,
const byte_t *);
/// H(result, body)
typedef bool (*llarp_hash_func)(byte_t *, llarp_buffer_t);

@ -29,6 +29,18 @@ namespace llarp
return sz;
}
};
typedef AlignedBuffer< SECKEYSIZE > SecretKey;
typedef AlignedBuffer< SHORTHASHSIZE > ShortHash;
typedef AlignedBuffer< SIGSIZE > Signature;
typedef AlignedBuffer< TUNNONCESIZE > TunnelNonce;
typedef AlignedBuffer< 24 > SymmNonce;
typedef AlignedBuffer< 32 > SymmKey;
}
#endif

@ -50,7 +50,7 @@ struct llarp_router_lookup_job
void* user;
llarp_rotuer_lookup_handler hook;
struct llarp_dht_context* dht;
llarp_pubkey_t target;
byte_t target[PUBKEYSIZE];
bool found;
struct llarp_rc result;
};

@ -1,6 +1,7 @@
#ifndef LLARP_ENCCRYPTED_HPP
#define LLARP_ENCCRYPTED_HPP
#include <llarp/bencode.h>
#include <llarp/buffer.h>
namespace llarp
@ -13,6 +14,28 @@ namespace llarp
Encrypted(size_t sz);
~Encrypted();
bool
BEncode(llarp_buffer_t* buf) const
{
return bencode_write_bytestring(buf, data, size);
}
bool
BDecode(llarp_buffer_t* buf)
{
llarp_buffer_t strbuf;
if(!bencode_read_string(buf, &strbuf))
return false;
if(strbuf.sz == 0)
return false;
if(data)
delete[] data;
size = strbuf.sz;
data = new byte_t[size];
memcpy(data, strbuf.base, size);
return true;
}
llarp_buffer_t*
Buffer()
{

@ -10,8 +10,8 @@ namespace llarp
{
struct EncryptedFrame : public Encrypted
{
static constexpr size_t OverheadSize = sizeof(llarp_pubkey_t)
+ sizeof(llarp_tunnel_nonce_t) + sizeof(llarp_shorthash_t);
static constexpr size_t OverheadSize =
PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE;
EncryptedFrame() = default;
EncryptedFrame(byte_t* buf, size_t sz) : Encrypted(buf, sz)

@ -19,7 +19,7 @@ struct llarp_xi
{
struct in6_addr address;
struct in6_addr netmask;
llarp_pubkey_t pubkey;
byte_t pubkey[PUBKEYSIZE];
};
bool

@ -2,7 +2,6 @@
#define LLARP_IWP_H_
#include <llarp/crypto.h>
#include <llarp/link.h>
#include <llarp/router_identity.h>
#ifdef __cplusplus
extern "C" {

@ -5,7 +5,6 @@
#include <llarp/ev.h>
#include <llarp/logic.h>
#include <llarp/mem.h>
#include <llarp/obmd.h>
#include <stdbool.h>
#include <stdint.h>

@ -10,7 +10,6 @@
namespace llarp
{
struct ILinkMessage;
typedef std::queue< ILinkMessage* > SendQueue;

@ -1,6 +1,6 @@
#ifndef LLARP_RELAY_COMMIT_HPP
#define LLARP_RELAY_COMMIT_HPP
#include <llarp/crypto.h>
#include <llarp/crypto.hpp>
#include <llarp/encrypted_ack.hpp>
#include <llarp/encrypted_frame.hpp>
#include <llarp/link_message.hpp>
@ -11,12 +11,12 @@ namespace llarp
{
struct LR_CommitRecord
{
llarp_pubkey_t commkey;
llarp_pubkey_t nextHop;
llarp_tunnel_nonce_t tunnelNonce;
PubKey commkey;
PubKey nextHop;
TunnelNonce tunnelNonce;
PathID_t txid;
PathSymKey_t downstreamReplyKey;
SymmNonce_t downstreamReplyNonce;
SymmKey downstreamReplyKey;
SymmNonce downstreamReplyNonce;
uint64_t version;
bool
@ -57,6 +57,7 @@ namespace llarp
struct LR_CommitMessage : public ILinkMessage
{
std::vector< EncryptedFrame > frames;
EncryptedFrame lasthopFrame;
std::vector< EncryptedAck > acks;
uint64_t version;
@ -76,13 +77,6 @@ namespace llarp
bool
HandleMessage(llarp_router *router) const;
private:
static bool
DecodeEncryptedFrame(list_reader *r, bool buf);
bool
DecodeEncryptedFrameList(llarp_buffer_t *buf);
};
}

@ -50,14 +50,14 @@ llarp_nodedb_iterate_all(struct llarp_nodedb *n, struct llarp_nodedb_iter i);
*/
bool
llarp_nodedb_find_rc(struct llarp_nodedb *n, struct llarp_rc *rc,
llarp_pubkey_t k);
const byte_t *k);
/**
return true if we have a rc with rc.k of value k on disk
otherwise return false
*/
bool
llarp_nodedb_has_rc(struct llarp_nodedb *n, llarp_pubkey_t k);
llarp_nodedb_has_rc(struct llarp_nodedb *n, const byte_t *k);
/**
put an rc into the node db

@ -1,36 +0,0 @@
#ifndef LLARP_OBMD_H_
#define LLARP_OBMD_H_
#include <llarp/buffer.h>
#include <llarp/crypto.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/* forward decl */
struct llarp_link;
/**
* link layer outbound message sending muxer
*/
struct llarp_link_dispatcher;
struct llarp_link_dispatcher *
llarp_init_link_dispatcher();
void
llarp_free_link_dispatcher(struct llarp_link_dispatcher **dispatcher);
void
llarp_link_sendto(struct llarp_link_dispatcher *dispatcher,
llarp_pubkey_t pubkey, llarp_buffer_t msg);
void
llarp_link_register(struct llarp_link_dispatcher *dispatcher,
struct llarp_link *link);
#ifdef __cplusplus
}
#endif
#endif

@ -63,8 +63,8 @@ namespace llarp
struct TransitHop
{
PathSymKey_t rxKey;
PathSymKey_t txKey;
SharedSecret rxKey;
SharedSecret txKey;
llarp_time_t started;
llarp_proto_version_t version;
};
@ -76,9 +76,9 @@ namespace llarp
/// router identity key
PubKey encryptionKey;
/// shared secret at this hop
PathSymKey_t shared;
SharedSecret shared;
/// nonce for key exchange
PathNonce_t nonce;
TunnelNonce nonce;
};
struct Path

@ -6,12 +6,6 @@
namespace llarp
{
typedef AlignedBuffer< 16 > PathID_t;
typedef AlignedBuffer< 32 > PathSymKey_t;
typedef AlignedBuffer< 32 > PathNonce_t;
typedef AlignedBuffer< 24 > SymmNonce_t;
}
#endif

@ -16,11 +16,11 @@ struct llarp_rc
{
struct llarp_ai_list *addrs;
// public encryption public key
llarp_pubkey_t enckey;
byte_t enckey[PUBKEYSIZE];
// public signing public key
llarp_pubkey_t pubkey;
byte_t pubkey[PUBKEYSIZE];
struct llarp_xi_list *exits;
llarp_sig_t signature;
byte_t signature[SIGSIZE];
uint64_t last_updated;
};

@ -1,46 +0,0 @@
#ifndef LLARP_ROUTER_IDENT_H
#define LLARP_ROUTER_IDENT_H
#include <llarp/address_info.h>
#include <llarp/crypto.h>
#include <llarp/exit_info.h>
#include <llarp/router_contact.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* rotuer identity private info
*/
struct llarp_router_ident
{
struct llarp_crypto *crypto;
llarp_seckey_t signkey;
uint64_t updated;
uint16_t version;
struct llarp_ai_list *addrs;
struct llarp_xi_list *exits;
};
void
llarp_router_ident_new(struct llarp_router_ident **ri,
struct llarp_crypto *crypto);
void
llarp_router_ident_append_ai(struct llarp_router_ident *ri,
struct llarp_ai *ai);
bool
llarp_router_ident_bdecode(struct llarp_router_ident *ri, llarp_buffer_t *buf);
bool
llarp_router_ident_bencode(struct llarp_router_ident *ri, llarp_buffer_t *buf);
void
llarp_router_ident_free(struct llarp_router_ident **ri);
bool
llarp_router_ident_generate_rc(struct llarp_router_ident *ri,
struct llarp_rc **rc);
#ifdef __cplusplus
}
#endif
#endif

@ -56,10 +56,10 @@ llarp_ai_decode_key(struct dict_reader *r, llarp_buffer_t *key)
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != sizeof(llarp_pubkey_t))
if(strbuf.sz != PUBKEYSIZE)
return false;
memcpy(ai->enc_key, strbuf.base, sizeof(llarp_pubkey_t));
memcpy(ai->enc_key, strbuf.base, PUBKEYSIZE);
return true;
}
@ -154,7 +154,7 @@ llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff)
/* encryption key */
if(!bencode_write_bytestring(buff, "e", 1))
return false;
if(!bencode_write_bytestring(buff, ai->enc_key, sizeof(llarp_pubkey_t)))
if(!bencode_write_bytestring(buff, ai->enc_key, PUBKEYSIZE))
return false;
/** ip */
ipstr = inet_ntop(AF_INET6, &ai->ip, ipbuff, sizeof(ipbuff));

@ -132,6 +132,8 @@ bencode_read_dict(llarp_buffer_t* buff, struct dict_reader* r)
if(!r->on_key(r, &strbuf)) // check for early abort
return false;
}
else
return false;
}
if(*r->buffer->cur != 'e') // make sure we're at dictionary end

@ -41,9 +41,9 @@ namespace iwp
gen_intro(void *user)
{
iwp_async_intro *intro = static_cast< iwp_async_intro * >(user);
llarp_sharedkey_t sharedkey;
llarp_shorthash_t e_k;
llarp_nonce_t n;
llarp::SharedSecret sharedkey;
llarp::ShortHash e_k;
llarp::SymmNonce n;
llarp_crypto *crypto = intro->iwp->crypto;
byte_t tmp[64];
// S = TKE(a.k, b.k, n)
@ -76,10 +76,10 @@ namespace iwp
{
iwp_async_intro *intro = static_cast< iwp_async_intro * >(user);
auto crypto = intro->iwp->crypto;
llarp_sharedkey_t sharedkey;
llarp_shorthash_t e_K;
llarp_hmac_t h;
llarp_nonce_t N;
llarp::SharedSecret sharedkey;
llarp::ShortHash e_K;
llarp::SharedSecret h;
llarp::SymmNonce N;
byte_t tmp[64];
auto OurPK = llarp::seckey_topublic(intro->secretkey);
// e_k = HS(b.k + n)
@ -127,8 +127,8 @@ namespace iwp
auto crypto = introack->iwp->crypto;
auto logic = introack->iwp->logic;
llarp_hmac_t digest;
llarp_sharedkey_t sharedkey;
llarp::ShortHash digest;
llarp::SharedSecret sharedkey;
auto hmac = introack->buf;
auto body = introack->buf + 32;
@ -169,7 +169,7 @@ namespace iwp
gen_introack(void *user)
{
iwp_async_introack *introack = static_cast< iwp_async_introack * >(user);
llarp_sharedkey_t sharedkey;
llarp::SharedSecret sharedkey;
auto crypto = introack->iwp->crypto;
auto pubkey = introack->remote_pubkey;
auto secretkey = introack->secretkey;
@ -221,8 +221,8 @@ namespace iwp
auto token = session->token;
auto K = session->sessionkey;
llarp_sharedkey_t e_K;
llarp_shorthash_t T;
llarp::SharedSecret e_K;
llarp::ShortHash T;
byte_t tmp[64];
llarp_buffer_t buf;
@ -271,8 +271,8 @@ namespace iwp
auto token = session->token;
auto K = session->sessionkey;
llarp_sharedkey_t e_K;
llarp_shorthash_t T;
llarp::SharedSecret e_K;
llarp::ShortHash T;
byte_t tmp[64];
@ -334,7 +334,7 @@ namespace iwp
byte_t *nonce = frame->buf + 32;
byte_t *body = frame->buf + 64;
llarp_sharedkey_t digest;
llarp::ShortHash digest;
llarp_buffer_t buf;
buf.base = nonce;

@ -9,7 +9,7 @@ namespace llarp
namespace sodium
{
static bool
xchacha20(llarp_buffer_t buff, llarp_sharedkey_t k, llarp_nonce_t n)
xchacha20(llarp_buffer_t buff, const byte_t *k, const byte_t *n)
{
return crypto_stream_xchacha20_xor(buff.base, buff.base, buff.sz, n, k)
== 0;
@ -19,7 +19,7 @@ namespace llarp
dh(uint8_t *out, uint8_t *client_pk, uint8_t *server_pk, uint8_t *pubkey,
uint8_t *secret)
{
llarp_sharedkey_t shared;
llarp::SharedSecret shared;
crypto_generichash_state h;
const size_t outsz = SHAREDKEYSIZE;
@ -60,7 +60,7 @@ namespace llarp
static bool
transport_dh_client(uint8_t *shared, uint8_t *pk, uint8_t *sk, uint8_t *n)
{
llarp_sharedkey_t dh_result;
llarp::SharedSecret dh_result;
if(dh(dh_result, llarp::seckey_topublic(sk), pk, pk, sk))
{
return crypto_generichash(shared, 32, n, 32, dh_result, 32) != -1;
@ -71,7 +71,7 @@ namespace llarp
static bool
transport_dh_server(uint8_t *shared, uint8_t *pk, uint8_t *sk, uint8_t *n)
{
llarp_sharedkey_t dh_result;
llarp::SharedSecret dh_result;
if(dh(dh_result, pk, llarp::seckey_topublic(sk), pk, sk))
{
return crypto_generichash(shared, 32, n, 32, dh_result, 32) != -1;

@ -36,11 +36,11 @@ namespace llarp
// <N bytes encrypted payload>
//
byte_t* hash = data;
byte_t* nonce = hash + sizeof(llarp_shorthash_t);
byte_t* pubkey = nonce + sizeof(llarp_tunnel_nonce_t);
byte_t* body = pubkey + sizeof(llarp_pubkey_t);
byte_t* nonce = hash + SHORTHASHSIZE;
byte_t* pubkey = nonce + TUNNONCESIZE;
byte_t* body = pubkey + PUBKEYSIZE;
llarp_sharedkey_t shared;
SharedSecret shared;
auto DH = crypto->dh_client;
auto Encrypt = crypto->xchacha20;
@ -52,10 +52,9 @@ namespace llarp
buf.sz = size - OverheadSize;
// set our pubkey
memcpy(pubkey, llarp::seckey_topublic(ourSecretKey),
sizeof(llarp_pubkey_t));
memcpy(pubkey, llarp::seckey_topublic(ourSecretKey), PUBKEYSIZE);
// randomize nonce
crypto->randbytes(nonce, sizeof(llarp_tunnel_nonce_t));
crypto->randbytes(nonce, TUNNONCESIZE);
// derive shared key
if(!DH(shared, otherPubkey, nonce, ourSecretKey))
@ -73,7 +72,7 @@ namespace llarp
// generate message auth
buf.base = nonce;
buf.cur = buf.base;
buf.sz = size - sizeof(llarp_shorthash_t);
buf.sz = size - SHORTHASHSIZE;
if(!MDS(hash, buf, shared))
{
@ -98,9 +97,9 @@ namespace llarp
// <N bytes encrypted payload>
//
byte_t* hash = data;
byte_t* nonce = hash + sizeof(llarp_shorthash_t);
byte_t* otherPubkey = nonce + sizeof(llarp_tunnel_nonce_t);
byte_t* body = otherPubkey + sizeof(llarp_pubkey_t);
byte_t* nonce = hash + SHORTHASHSIZE;
byte_t* otherPubkey = nonce + TUNNONCESIZE;
byte_t* body = otherPubkey + PUBKEYSIZE;
// use dh_server becuase we are not the creator of this message
auto DH = crypto->dh_server;
@ -110,10 +109,10 @@ namespace llarp
llarp_buffer_t buf;
buf.base = nonce;
buf.cur = buf.base;
buf.sz = size - sizeof(llarp_shorthash_t);
buf.sz = size - SHORTHASHSIZE;
llarp_sharedkey_t shared;
llarp_shorthash_t digest;
SharedSecret shared;
ShortHash digest;
if(!DH(shared, otherPubkey, nonce, ourSecretKey))
{
@ -127,7 +126,7 @@ namespace llarp
return false;
}
if(memcmp(digest, hash, sizeof(llarp_shorthash_t)))
if(memcmp(digest, hash, digest.size()))
{
llarp::Error("message authentication failed");
return false;

@ -82,7 +82,7 @@ llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff)
/** public key */
if(!bencode_write_bytestring(buff, "k", 1))
return false;
if(!bencode_write_bytestring(buff, xi->pubkey, sizeof(llarp_pubkey_t)))
if(!bencode_write_bytestring(buff, xi->pubkey, PUBKEYSIZE))
return false;
/** version */
@ -128,9 +128,9 @@ llarp_xi_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
{
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != sizeof(llarp_pubkey_t))
if(strbuf.sz != PUBKEYSIZE)
return false;
memcpy(xi->pubkey, strbuf.base, sizeof(llarp_pubkey_t));
memcpy(xi->pubkey, strbuf.base, PUBKEYSIZE);
return true;
}

@ -669,9 +669,9 @@ namespace iwp
llarp_rc *our_router = nullptr;
llarp_rc remote_router;
llarp_seckey_t eph_seckey;
llarp_pubkey_t remote;
llarp_sharedkey_t sessionkey;
llarp::SecretKey eph_seckey;
llarp::PubKey remote;
llarp::SharedSecret sessionkey;
llarp_link_establish_job *establish_job = nullptr;
@ -707,7 +707,7 @@ namespace iwp
: udp(u), crypto(c), iwp(i), logic(l), addr(a), state(eInitial)
{
if(seckey)
memcpy(eph_seckey, seckey, sizeof(llarp_seckey_t));
eph_seckey = seckey;
else
{
c->encryption_keygen(eph_seckey);
@ -735,7 +735,7 @@ namespace iwp
{
session *self = static_cast< session * >(s->impl);
auto id = self->frame.txids++;
llarp_shorthash_t digest;
llarp::ShortHash digest;
self->crypto->shorthash(digest, msg);
transit_message *m = new transit_message(msg, digest, id);
self->add_outbound_message(id, m);
@ -804,7 +804,7 @@ namespace iwp
send_LIM()
{
llarp::Debug("send LIM");
llarp_shorthash_t digest;
llarp::ShortHash digest;
// 64 bytes overhead for link message
byte_t tmp[MAX_RC_SIZE + 64];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
@ -1239,7 +1239,7 @@ namespace iwp
SessionMap_t m_Connected;
mtx_t m_Connected_Mutex;
llarp_seckey_t seckey;
llarp::SecretKey seckey;
server(llarp_router *r, llarp_crypto *c, llarp_logic *l,
llarp_threadpool *w)
@ -1429,7 +1429,7 @@ namespace iwp
std::ifstream f(keyfile);
if(f.is_open())
{
f.read((char *)seckey, sizeof(llarp_seckey_t));
f.read((char *)seckey.data(), seckey.size());
return true;
}
return false;
@ -1443,7 +1443,7 @@ namespace iwp
std::ofstream f(fname);
if(f.is_open())
{
f.write((char *)seckey, sizeof(llarp_seckey_t));
f.write((char *)seckey.data(), seckey.size());
return true;
}
return false;
@ -1504,7 +1504,7 @@ namespace iwp
auto rxmsg = rx[id];
if(rxmsg->reassemble(msg))
{
llarp_shorthash_t digest;
llarp::ShortHash digest;
auto buf = llarp::Buffer< decltype(msg) >(msg);
router->crypto.shorthash(digest, buf);
if(memcmp(digest, rxmsg->msginfo.hash(), 32))

@ -1,7 +1,7 @@
#include <llarp/nodedb.h>
#include <llarp/crypto.hpp>
#include <llarp/router_contact.h>
#include <fstream>
#include <llarp/crypto.hpp>
#include <unordered_map>
#include "buffer.hpp"
#include "fs.hpp"
@ -16,7 +16,7 @@ struct llarp_nodedb
}
llarp_crypto *crypto;
std::unordered_map< llarp::PubKey, llarp_rc * , llarp::PubKeyHash> entries;
std::unordered_map< llarp::PubKey, llarp_rc *, llarp::PubKeyHash > entries;
void
Clear()
@ -168,7 +168,7 @@ llarp_nodedb_async_verify(struct llarp_nodedb *nodedb,
bool
llarp_nodedb_find_rc(struct llarp_nodedb *nodedb, struct llarp_rc *dst,
llarp_pubkey_t k)
const byte_t *k)
{
return false;
}

@ -1,3 +1,4 @@
#include <llarp/bencode.hpp>
#include <llarp/messages/relay_commit.hpp>
#include "logger.hpp"
#include "router.hpp"
@ -13,46 +14,22 @@ namespace llarp
{
if(llarp_buffer_eq(key, "c"))
{
return DecodeEncryptedFrameList(buf);
return BEncodeReadList(frames, buf);
}
return false;
}
bool
LR_CommitMessage::DecodeEncryptedFrame(list_reader* l, bool item)
{
LR_CommitMessage* self = static_cast< LR_CommitMessage* >(l->user);
// end of list
if(!item)
if(llarp_buffer_eq(key, "f"))
{
bool success = self->frames.size() > 0;
if(!success)
llarp::Error("no encrypted frames read in LRCM");
return success;
return lasthopFrame.BDecode(buf);
}
llarp_buffer_t strbuf;
if(!bencode_read_string(l->buffer, &strbuf))
return false;
if(strbuf.sz <= EncryptedFrame::OverheadSize)
if(llarp_buffer_eq(key, "r"))
{
llarp::Error("Encrypted Frame In LRCM too short, ", strbuf.sz,
" <= ", EncryptedFrame::OverheadSize);
return false;
return BEncodeReadList(acks, buf);
}
// emplace new frame
self->frames.emplace_back(strbuf.cur, strbuf.sz);
return true;
}
bool read = false;
if(!BEncodeMaybeReadVersion("v", version, LLARP_PROTO_VERSION, read, key,
buf))
return false;
bool
LR_CommitMessage::DecodeEncryptedFrameList(llarp_buffer_t* buf)
{
list_reader r;
r.user = this;
r.on_item = &DecodeEncryptedFrame;
return bencode_read_list(buf, &r);
return read;
}
bool
@ -61,20 +38,19 @@ namespace llarp
if(!bencode_start_dict(buf))
return false;
// msg type
if(!bencode_write_bytestring(buf, "a", 1))
if(!BEncodeWriteDictMsgType(buf, "a", "c"))
return false;
if(!bencode_write_bytestring(buf, "c", 1))
return false;
// frames
if(!bencode_write_bytestring(buf, "c", 1))
if(!BEncodeWriteDictList("c", frames, buf))
return false;
// last hop
if(!BEncodeWriteDictEntry("f", lasthopFrame, buf))
return false;
if(!bencode_start_list(buf))
// acks
if(!BEncodeWriteDictList("r", acks, buf))
return false;
for(const auto& frame : frames)
if(!bencode_write_bytestring(buf, frame.data, frame.size))
return false;
if(!bencode_end(buf))
// version
if(!bencode_write_version_entry(buf))
return false;
return bencode_end(buf);
@ -89,7 +65,25 @@ namespace llarp
bool
LR_CommitRecord::BEncode(llarp_buffer_t* buf) const
{
return false;
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictEntry("c", commkey, buf))
return false;
if(!BEncodeWriteDictEntry("i", nextHop, buf))
return false;
if(!BEncodeWriteDictEntry("n", tunnelNonce, buf))
return false;
if(!BEncodeWriteDictEntry("p", txid, buf))
return false;
if(!BEncodeWriteDictEntry("s", downstreamReplyKey, buf))
return false;
if(!BEncodeWriteDictEntry("u", downstreamReplyNonce, buf))
return false;
if(!bencode_write_version_entry(buf))
return false;
return bencode_end(buf);
}
bool
@ -98,77 +92,29 @@ namespace llarp
if(!key)
return true;
llarp_buffer_t strbuf;
LR_CommitRecord* self = static_cast< LR_CommitRecord* >(r->user);
if(llarp_buffer_eq(*key, "c"))
{
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != sizeof(llarp_pubkey_t))
return false;
memcpy(self->commkey, strbuf.base, strbuf.sz);
return true;
}
if(llarp_buffer_eq(*key, "i"))
{
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != sizeof(llarp_pubkey_t))
return false;
memcpy(self->nextHop, strbuf.base, strbuf.sz);
return true;
}
if(llarp_buffer_eq(*key, "n"))
{
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != sizeof(llarp_tunnel_nonce_t))
return false;
memcpy(self->tunnelNonce, strbuf.base, strbuf.sz);
return true;
}
bool read = false;
if(llarp_buffer_eq(*key, "p"))
{
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != self->txid.size())
return false;
memcpy(self->txid, strbuf.base, strbuf.sz);
return true;
}
if(llarp_buffer_eq(*key, "s"))
{
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != self->downstreamReplyKey.size())
return false;
memcpy(self->downstreamReplyKey, strbuf.base, strbuf.sz);
return true;
}
if(llarp_buffer_eq(*key, "u"))
{
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != self->downstreamReplyNonce.size())
return false;
memcpy(self->downstreamReplyNonce, strbuf.base, strbuf.sz);
return true;
}
if(llarp_buffer_eq(*key, "v"))
{
if(!bencode_read_integer(r->buffer, &self->version))
return false;
return self->version == LLARP_PROTO_VERSION;
}
if(!BEncodeMaybeReadDictEntry("c", self->commkey, read, *key, r->buffer))
return false;
if(!BEncodeMaybeReadDictEntry("i", self->nextHop, read, *key, r->buffer))
return false;
if(BEncodeMaybeReadDictEntry("n", self->tunnelNonce, read, *key, r->buffer))
return false;
if(!BEncodeMaybeReadDictEntry("p", self->txid, read, *key, r->buffer))
return false;
if(!BEncodeMaybeReadDictEntry("s", self->downstreamReplyKey, read, *key,
r->buffer))
return false;
if(!BEncodeMaybeReadDictEntry("u", self->downstreamReplyNonce, read, *key,
r->buffer))
return false;
if(!BEncodeMaybeReadVersion("v", self->version, LLARP_PROTO_VERSION, read,
*key, r->buffer))
return false;
return false;
return read;
}
bool

@ -71,10 +71,8 @@ llarp_router::SendToOrQueue(const llarp::RouterID &remote,
{
llarp_rc rc;
llarp_rc_clear(&rc);
llarp_pubkey_t k;
memcpy(k, remote, sizeof(llarp_pubkey_t));
if(!llarp_nodedb_find_rc(nodedb, &rc, k))
if(!llarp_nodedb_find_rc(nodedb, &rc, remote))
{
llarp::Warn("cannot find router ", remote, " locally so we are dropping ",
msgs.size(), " messages to them");
@ -175,7 +173,7 @@ llarp_router::EnsureEncryptionKey()
encryption_keyfile, " ", ec);
return false;
}
f.write((char *)encryption, sizeof(llarp_seckey_t));
f.write((char *)encryption.data(), encryption.size());
}
std::ifstream f(encryption_keyfile, std::ios::binary);
if(!f.is_open())
@ -183,7 +181,7 @@ llarp_router::EnsureEncryptionKey()
llarp::Error("could not read ", encryption_keyfile);
return false;
}
f.read((char *)encryption, sizeof(llarp_seckey_t));
f.read((char *)encryption.data(), encryption.size());
return true;
}
@ -482,7 +480,7 @@ llarp_router::Run()
llarp_ai_list_pushback(rc.addrs, &addr);
};
// set public keys
memcpy(rc.enckey, llarp::seckey_topublic(encryption), sizeof(llarp_pubkey_t));
memcpy(rc.enckey, llarp::seckey_topublic(encryption), PUBKEYSIZE);
llarp_rc_set_pubkey(&rc, pubkey());
llarp_rc_sign(&crypto, identity, &rc);
@ -719,13 +717,13 @@ llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *fpath,
std::ofstream f(path, std::ios::binary);
if(f.is_open())
{
f.write((char *)secretkey, sizeof(llarp_seckey_t));
f.write((char *)secretkey, SECKEYSIZE);
}
}
std::ifstream f(path, std::ios::binary);
if(f.is_open())
{
f.read((char *)secretkey, sizeof(llarp_seckey_t));
f.read((char *)secretkey, SECKEYSIZE);
return true;
}
llarp::Info("failed to get identity key");

@ -54,8 +54,8 @@ struct llarp_router
llarp_logic *logic;
llarp_crypto crypto;
llarp::PathContext paths;
llarp_seckey_t identity;
llarp_seckey_t encryption;
llarp::SecretKey identity;
llarp::SecretKey encryption;
llarp_threadpool *disk;
llarp_dht_context *dht = nullptr;

@ -1,7 +1,7 @@
#include <llarp/bencode.h>
#include <llarp/router_contact.h>
#include <llarp/version.h>
#include <llarp/crypto.hpp>
#include "buffer.hpp"
#include "logger.hpp"
@ -49,9 +49,9 @@ llarp_rc_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
{
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != sizeof(llarp_pubkey_t))
if(strbuf.sz != PUBKEYSIZE)
return false;
memcpy(rc->enckey, strbuf.base, sizeof(llarp_pubkey_t));
memcpy(rc->enckey, strbuf.base, PUBKEYSIZE);
return true;
}
@ -59,9 +59,9 @@ llarp_rc_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
{
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != sizeof(llarp_pubkey_t))
if(strbuf.sz != PUBKEYSIZE)
return false;
memcpy(rc->pubkey, strbuf.base, sizeof(llarp_pubkey_t));
memcpy(rc->pubkey, strbuf.base, PUBKEYSIZE);
return true;
}
@ -93,9 +93,9 @@ llarp_rc_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
{
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != sizeof(llarp_sig_t))
if(strbuf.sz != SIGSIZE)
return false;
memcpy(rc->signature, strbuf.base, sizeof(llarp_sig_t));
memcpy(rc->signature, strbuf.base, SIGSIZE);
return true;
}
@ -107,9 +107,9 @@ llarp_rc_copy(struct llarp_rc *dst, const struct llarp_rc *src)
{
llarp_rc_free(dst);
llarp_rc_clear(dst);
memcpy(dst->pubkey, src->pubkey, sizeof(llarp_pubkey_t));
memcpy(dst->enckey, src->enckey, sizeof(llarp_pubkey_t));
memcpy(dst->signature, src->signature, sizeof(llarp_sig_t));
memcpy(dst->pubkey, src->pubkey, PUBKEYSIZE);
memcpy(dst->enckey, src->enckey, PUBKEYSIZE);
memcpy(dst->signature, src->signature, SIGSIZE);
dst->last_updated = src->last_updated;
if(src->addrs)
@ -135,15 +135,15 @@ bool
llarp_rc_verify_sig(struct llarp_crypto *crypto, struct llarp_rc *rc)
{
bool result = false;
llarp_sig_t sig;
llarp::Signature sig;
byte_t tmp[MAX_RC_SIZE];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
// copy sig
memcpy(sig, rc->signature, sizeof(llarp_sig_t));
memcpy(sig, rc->signature, SIGSIZE);
// zero sig
size_t sz = 0;
while(sz < sizeof(llarp_sig_t))
while(sz < SIGSIZE)
rc->signature[sz++] = 0;
// bencode
@ -156,7 +156,7 @@ llarp_rc_verify_sig(struct llarp_crypto *crypto, struct llarp_rc *rc)
else
llarp::Warn(__FILE__, "RC encode failed");
// restore sig
memcpy(rc->signature, sig, sizeof(llarp_sig_t));
memcpy(rc->signature, sig, SIGSIZE);
return result;
}
@ -179,13 +179,13 @@ llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff)
/* write encryption pubkey */
if(!bencode_write_bytestring(buff, "e", 1))
return false;
if(!bencode_write_bytestring(buff, rc->enckey, sizeof(llarp_pubkey_t)))
if(!bencode_write_bytestring(buff, rc->enckey, PUBKEYSIZE))
return false;
/* write signing pubkey */
if(!bencode_write_bytestring(buff, "k", 1))
return false;
if(!bencode_write_bytestring(buff, rc->pubkey, sizeof(llarp_pubkey_t)))
if(!bencode_write_bytestring(buff, rc->pubkey, PUBKEYSIZE))
return false;
/* write last updated */
@ -210,7 +210,7 @@ llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff)
/* write signature */
if(!bencode_write_bytestring(buff, "z", 1))
return false;
if(!bencode_write_bytestring(buff, rc->signature, sizeof(llarp_sig_t)))
if(!bencode_write_bytestring(buff, rc->signature, SIGSIZE))
return false;
return bencode_end(buff);
}

Loading…
Cancel
Save