Mark more AlignedBuffer constructors as explicit

pull/190/head
Michael 5 years ago
parent 7dd40015f3
commit 516466f5be
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -45,7 +45,7 @@ namespace llarp
} }
} }
AlignedBuffer(const Data& buf) explicit AlignedBuffer(const Data& buf)
{ {
new(&val) Data; new(&val) Data;
std::copy(buf.begin(), buf.end(), begin()); std::copy(buf.begin(), buf.end(), begin());

@ -48,10 +48,17 @@ namespace llarp
PubKey() : AlignedBuffer< SIZE >() PubKey() : AlignedBuffer< SIZE >()
{ {
} }
PubKey(const byte_t *ptr) : AlignedBuffer< SIZE >(ptr)
explicit PubKey(const byte_t *ptr) : AlignedBuffer< SIZE >(ptr)
{
}
explicit PubKey(const Data &data) : AlignedBuffer< SIZE >(data)
{ {
} }
PubKey(const Data &data) : AlignedBuffer< SIZE >(data)
explicit PubKey(const AlignedBuffer< SIZE > &other)
: AlignedBuffer< SIZE >(other)
{ {
} }
@ -89,6 +96,12 @@ namespace llarp
return out << "[secretkey]"; return out << "[secretkey]";
} }
PubKey
toPublic() const
{
return PubKey(data() + 32);
}
bool bool
LoadFromFile(const char *fname); LoadFromFile(const char *fname);

@ -21,8 +21,7 @@ namespace llarp
xchacha20(llarp_buffer_t buff, const SharedSecret &k, const TunnelNonce &n) xchacha20(llarp_buffer_t buff, const SharedSecret &k, const TunnelNonce &n)
{ {
return crypto_stream_xchacha20_xor(buff.base, buff.base, buff.sz, return crypto_stream_xchacha20_xor(buff.base, buff.base, buff.sz,
n.data(), n.data(), k.data())
k.data())
== 0; == 0;
} }
@ -32,8 +31,7 @@ namespace llarp
{ {
if(in.sz > out.sz) if(in.sz > out.sz)
return false; return false;
return crypto_stream_xchacha20_xor(out.base, in.base, in.sz, n, return crypto_stream_xchacha20_xor(out.base, in.base, in.sz, n, k.data())
k.data())
== 0; == 0;
} }
@ -45,8 +43,7 @@ namespace llarp
crypto_generichash_state h; crypto_generichash_state h;
const size_t outsz = SHAREDKEYSIZE; const size_t outsz = SHAREDKEYSIZE;
if(crypto_scalarmult_curve25519(shared.data(), if(crypto_scalarmult_curve25519(shared.data(), usSec.data(), themPub))
usSec.data(), themPub))
return false; return false;
crypto_generichash_blake2b_init(&h, nullptr, 0U, outsz); crypto_generichash_blake2b_init(&h, nullptr, 0U, outsz);
crypto_generichash_blake2b_update(&h, client_pk.data(), 32); crypto_generichash_blake2b_update(&h, client_pk.data(), 32);
@ -62,11 +59,9 @@ namespace llarp
{ {
llarp::SharedSecret dh_result; llarp::SharedSecret dh_result;
if(dh(dh_result, llarp::seckey_topublic(sk), pk.data(), if(dh(dh_result, sk.toPublic(), pk, pk.data(), sk))
pk.data(), sk))
{ {
return crypto_generichash_blake2b(shared.data(), 32, return crypto_generichash_blake2b(shared.data(), 32, n.data(), 32,
n.data(), 32,
dh_result.data(), 32) dh_result.data(), 32)
!= -1; != -1;
} }
@ -79,11 +74,9 @@ namespace llarp
const SecretKey &sk, const TunnelNonce &n) const SecretKey &sk, const TunnelNonce &n)
{ {
llarp::SharedSecret dh_result; llarp::SharedSecret dh_result;
if(dh(dh_result, pk, llarp::seckey_topublic(sk), pk.data(), if(dh(dh_result, pk, sk.toPublic(), pk.data(), sk))
sk))
{ {
return crypto_generichash_blake2b(shared.data(), 32, return crypto_generichash_blake2b(shared.data(), 32, n.data(), 32,
n.data(), 32,
dh_result.data(), 32) dh_result.data(), 32)
!= -1; != -1;
} }
@ -102,9 +95,8 @@ namespace llarp
static bool static bool
shorthash(ShortHash &result, llarp_buffer_t buff) shorthash(ShortHash &result, llarp_buffer_t buff)
{ {
return crypto_generichash_blake2b(result.data(), return crypto_generichash_blake2b(result.data(), ShortHash::SIZE,
ShortHash::SIZE, buff.base, buff.sz, buff.base, buff.sz, nullptr, 0)
nullptr, 0)
!= -1; != -1;
} }
@ -119,17 +111,16 @@ namespace llarp
static bool static bool
sign(Signature &result, const SecretKey &secret, llarp_buffer_t buff) sign(Signature &result, const SecretKey &secret, llarp_buffer_t buff)
{ {
int rc = int rc = crypto_sign_detached(result.data(), nullptr, buff.base, buff.sz,
crypto_sign_detached(result.data(), nullptr, buff.base, secret.data());
buff.sz, secret.data());
return rc != -1; return rc != -1;
} }
static bool static bool
verify(const PubKey &pub, llarp_buffer_t buff, const Signature &sig) verify(const PubKey &pub, llarp_buffer_t buff, const Signature &sig)
{ {
int rc = crypto_sign_verify_detached(sig.data(), buff.base, int rc = crypto_sign_verify_detached(sig.data(), buff.base, buff.sz,
buff.sz, pub.data()); pub.data());
return rc != -1; return rc != -1;
} }
@ -173,17 +164,14 @@ namespace llarp
encrypt(PQCipherBlock &ciphertext, SharedSecret &sharedkey, encrypt(PQCipherBlock &ciphertext, SharedSecret &sharedkey,
const PQPubKey &pubkey) const PQPubKey &pubkey)
{ {
return crypto_kem_enc(ciphertext.data(), return crypto_kem_enc(ciphertext.data(), sharedkey.data(), pubkey.data())
sharedkey.data(),
pubkey.data())
!= -1; != -1;
} }
bool bool
decrypt(const PQCipherBlock &ciphertext, SharedSecret &sharedkey, decrypt(const PQCipherBlock &ciphertext, SharedSecret &sharedkey,
const byte_t *secretkey) const byte_t *secretkey)
{ {
return crypto_kem_dec(sharedkey.data(), return crypto_kem_dec(sharedkey.data(), ciphertext.data(), secretkey)
ciphertext.data(), secretkey)
!= -1; != -1;
} }

@ -22,7 +22,7 @@ llarp_dht_context_free(struct llarp_dht_context *ctx)
void void
__llarp_dht_remove_peer(struct llarp_dht_context *ctx, const byte_t *id) __llarp_dht_remove_peer(struct llarp_dht_context *ctx, const byte_t *id)
{ {
ctx->impl.nodes->DelNode(id); ctx->impl.nodes->DelNode(llarp::dht::Key_t(id));
} }
void void
@ -34,7 +34,7 @@ llarp_dht_allow_transit(llarp_dht_context *ctx)
void void
llarp_dht_context_start(struct llarp_dht_context *ctx, const byte_t *key) llarp_dht_context_start(struct llarp_dht_context *ctx, const byte_t *key)
{ {
ctx->impl.Init(key, ctx->parent, 20000); ctx->impl.Init(llarp::dht::Key_t(key), ctx->parent, 20000);
} }
void void

@ -109,7 +109,7 @@ namespace llarp
return false; return false;
} }
const Key_t targetKey = target.as_array(); const Key_t targetKey{target};
if((prevPeer ^ targetKey) < (peer ^ targetKey)) if((prevPeer ^ targetKey) < (peer ^ targetKey))
{ {
// next peer is not closer // next peer is not closer
@ -168,7 +168,7 @@ namespace llarp
LookupRouter(const RouterID& target, RouterLookupHandler result) LookupRouter(const RouterID& target, RouterLookupHandler result)
{ {
Key_t askpeer; Key_t askpeer;
if(!nodes->FindClosest(target.as_array(), askpeer)) if(!nodes->FindClosest(Key_t(target), askpeer))
return false; return false;
LookupRouterRecursive(target, OurKey(), 0, askpeer, result); LookupRouterRecursive(target, OurKey(), 0, askpeer, result);
return true; return true;

@ -18,7 +18,7 @@ namespace llarp
DHTImmeidateMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf) DHTImmeidateMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
{ {
if(llarp_buffer_eq(key, "m")) if(llarp_buffer_eq(key, "m"))
return llarp::dht::DecodeMesssageList(session->GetPubKey().as_array(), return llarp::dht::DecodeMesssageList(dht::Key_t(session->GetPubKey()),
buf, msgs); buf, msgs);
if(llarp_buffer_eq(key, "v")) if(llarp_buffer_eq(key, "v"))
{ {

@ -16,26 +16,25 @@ namespace llarp
auto &dht = ctx->impl; auto &dht = ctx->impl;
/// lookup for us, send an immeidate reply /// lookup for us, send an immeidate reply
Key_t us = dht.OurKey(); Key_t us = dht.OurKey();
Key_t k{K};
if(K == us) if(K == us)
{ {
auto path = dht.router->paths.GetByUpstream(K, pathID); auto path = dht.router->paths.GetByUpstream(K, pathID);
if(path) if(path)
{ {
replies.emplace_back(new GotRouterMessage(K.as_array(), txid, replies.emplace_back(
{dht.router->rc()}, false)); new GotRouterMessage(k, txid, {dht.router->rc()}, false));
return true; return true;
} }
return false; return false;
} }
Key_t peer; Key_t peer;
Key_t k = K.as_array();
// check if we know this in our nodedb first // check if we know this in our nodedb first
RouterContact found; RouterContact found;
if(dht.router->nodedb->Get(K, found)) if(dht.router->nodedb->Get(K, found))
{ {
replies.emplace_back( replies.emplace_back(new GotRouterMessage(k, txid, {found}, false));
new GotRouterMessage(K.as_array(), txid, {found}, false));
return true; return true;
} }
// lookup if we don't have it in our nodedb // lookup if we don't have it in our nodedb
@ -156,16 +155,16 @@ namespace llarp
return false; return false;
} }
RouterContact found; RouterContact found;
Key_t k{K};
if(exploritory) if(exploritory)
return dht.HandleExploritoryRouterLookup(From, txid, K, replies); return dht.HandleExploritoryRouterLookup(From, txid, K, replies);
else if(dht.router->nodedb->Get(K, found)) else if(dht.router->nodedb->Get(K, found))
{ {
replies.emplace_back( replies.emplace_back(new GotRouterMessage(k, txid, {found}, false));
new GotRouterMessage(K.as_array(), txid, {found}, false));
return true; return true;
} }
else else
dht.LookupRouterRelayed(From, txid, K.as_array(), !iterative, replies); dht.LookupRouterRelayed(From, txid, k, !iterative, replies);
return true; return true;
} }
} // namespace dht } // namespace dht

@ -9,18 +9,22 @@ namespace llarp
{ {
namespace dht namespace dht
{ {
struct Key_t : public llarp::AlignedBuffer< 32 > struct Key_t : public AlignedBuffer< 32 >
{ {
Key_t(const byte_t* buf) : llarp::AlignedBuffer< SIZE >(buf) explicit Key_t(const byte_t* buf) : AlignedBuffer< SIZE >(buf)
{ {
} }
Key_t(const std::array< byte_t, SIZE >& val) explicit Key_t(const Data& val) : AlignedBuffer< SIZE >(val)
: llarp::AlignedBuffer< SIZE >(val)
{ {
} }
Key_t() : llarp::AlignedBuffer< SIZE >() explicit Key_t(const AlignedBuffer< SIZE >& val)
: AlignedBuffer< SIZE >(val)
{
}
Key_t() : AlignedBuffer< SIZE >()
{ {
} }
@ -28,8 +32,7 @@ namespace llarp
operator^(const Key_t& other) const operator^(const Key_t& other) const
{ {
Key_t dist; Key_t dist;
std::transform(begin(), end(), std::transform(begin(), end(), other.begin(), dist.begin(),
other.begin(), dist.begin(),
std::bit_xor< byte_t >()); std::bit_xor< byte_t >());
return dist; return dist;
} }

@ -20,10 +20,8 @@ namespace llarp
ID.Zero(); ID.Zero();
} }
RCNode(const llarp::RouterContact& other) RCNode(const llarp::RouterContact& other) : rc(other), ID(other.pubkey)
{ {
rc = other;
ID = other.pubkey.as_array();
} }
bool bool

@ -46,7 +46,7 @@ decode_request_name(const std::string &name, llarp::AlignedBuffer< 32 > &addr,
{ {
return false; return false;
} }
addr = snodeAddr.as_array(); addr = snodeAddr;
isSNode = true; isSNode = true;
} }
else else
@ -55,7 +55,7 @@ decode_request_name(const std::string &name, llarp::AlignedBuffer< 32 > &addr,
{ {
return false; return false;
} }
addr = serviceAddr.as_array(); addr = serviceAddr;
isSNode = false; isSNode = false;
} }
return true; return true;
@ -330,7 +330,7 @@ ReverseHandlerIter(struct llarp::service::Context::endpoint_iter *endpointCfg)
} }
else else
{ {
llarp::service::Address saddr = addr.as_array(); llarp::service::Address saddr(addr);
// llarp::LogInfo("Returning [", saddr.ToString(), "]"); // llarp::LogInfo("Returning [", saddr.ToString(), "]");
writesend_dnss_revresponse(saddr.ToString(), context->request); writesend_dnss_revresponse(saddr.ToString(), context->request);
} }
@ -457,7 +457,7 @@ llarp_dotlokilookup_handler(std::string name,
} }
else else
{ {
if(tun->HasPathToService(addr.as_array())) if(tun->HasPathToService(llarp::service::Address(addr)))
{ {
llarp_dotlokilookup_checkQuery(qr, 0, 0); llarp_dotlokilookup_checkQuery(qr, 0, 0);
response->dontSendResponse = true; // will send it shortly response->dontSendResponse = true; // will send it shortly

@ -33,7 +33,7 @@ namespace llarp
buf.sz = size() - EncryptedFrameOverheadSize; buf.sz = size() - EncryptedFrameOverheadSize;
// set our pubkey // set our pubkey
memcpy(pubkey, seckey_topublic(ourSecretKey), PUBKEYSIZE); memcpy(pubkey, ourSecretKey.toPublic().data(), PUBKEYSIZE);
// randomize nonce // randomize nonce
crypto->randbytes(noncePtr, TUNNONCESIZE); crypto->randbytes(noncePtr, TUNNONCESIZE);
TunnelNonce nonce(noncePtr); TunnelNonce nonce(noncePtr);
@ -76,10 +76,10 @@ namespace llarp
// <N bytes encrypted payload> // <N bytes encrypted payload>
// //
ShortHash hash(data()); ShortHash hash(data());
byte_t* noncePtr = data() + SHORTHASHSIZE; byte_t* noncePtr = data() + SHORTHASHSIZE;
PubKey otherPubkey = noncePtr + TUNNONCESIZE; byte_t* body = data() + EncryptedFrameOverheadSize;
byte_t* body = data() + EncryptedFrameOverheadSize;
TunnelNonce nonce(noncePtr); TunnelNonce nonce(noncePtr);
PubKey otherPubkey(noncePtr + TUNNONCESIZE);
// use dh_server because we are not the creator of this message // use dh_server because we are not the creator of this message
auto DH = crypto->dh_server; auto DH = crypto->dh_server;

@ -96,7 +96,8 @@ namespace llarp
if(r.FromString(msg.questions[0].qname)) if(r.FromString(msg.questions[0].qname))
{ {
huint32_t ip; huint32_t ip;
if(m_SNodeKeys.find(r.as_array()) == m_SNodeKeys.end()) PubKey pubKey(r);
if(m_SNodeKeys.find(pubKey) == m_SNodeKeys.end())
{ {
// we do not have it mapped // we do not have it mapped
// map it // map it
@ -106,7 +107,7 @@ namespace llarp
else else
{ {
// we have it mapped already as a service node // we have it mapped already as a service node
auto itr = m_KeyToIP.find(r.as_array()); auto itr = m_KeyToIP.find(pubKey);
if(itr != m_KeyToIP.end()) if(itr != m_KeyToIP.end())
{ {
ip = itr->second; ip = itr->second;
@ -485,18 +486,19 @@ namespace llarp
huint32_t huint32_t
ExitEndpoint::ObtainServiceNodeIP(const llarp::RouterID &other) ExitEndpoint::ObtainServiceNodeIP(const llarp::RouterID &other)
{ {
huint32_t ip = GetIPForIdent(other.as_array()); PubKey pubKey(other);
if(m_SNodeKeys.emplace(other.as_array()).second) huint32_t ip = GetIPForIdent(pubKey);
if(m_SNodeKeys.emplace(pubKey).second)
{ {
// this is a new service node make an outbound session to them // this is a new service node make an outbound session to them
m_SNodeSessions.insert( m_SNodeSessions.emplace(
std::make_pair(other, other,
std::unique_ptr< llarp::exit::SNodeSession >( std::unique_ptr< llarp::exit::SNodeSession >(
new llarp::exit::SNodeSession( new llarp::exit::SNodeSession(
other, other,
std::bind(&ExitEndpoint::QueueSNodePacket, std::bind(&ExitEndpoint::QueueSNodePacket, this,
this, std::placeholders::_1, ip), std::placeholders::_1, ip),
Router(), 2, 1, true)))); Router(), 2, 1, true)));
} }
return ip; return ip;
} }

@ -261,8 +261,8 @@ namespace llarp
reply(msg); reply(msg);
return true; return true;
} }
llarp::service::Address addr = llarp::service::Address addr(
ObtainAddrForIP< llarp::service::Address >(ip, true); ObtainAddrForIP< llarp::service::Address >(ip, true));
if(!addr.IsZero()) if(!addr.IsZero())
{ {
msg.AddAReply(addr.ToString(".snode")); msg.AddAReply(addr.ToString(".snode"));
@ -348,9 +348,9 @@ namespace llarp
} }
llarp::LogInfo(Name() + " map ", addr.ToString(), " to ", ip); llarp::LogInfo(Name() + " map ", addr.ToString(), " to ", ip);
m_IPToAddr[ip] = addr.as_array(); m_IPToAddr[ip] = addr;
m_AddrToIP[addr.as_array()] = ip; m_AddrToIP[addr] = ip;
m_SNodes[addr.as_array()] = SNode; m_SNodes[addr] = SNode;
MarkIPActiveForever(ip); MarkIPActiveForever(ip);
return true; return true;
} }

@ -111,7 +111,7 @@ namespace llarp
return addr; return addr;
} }
// found // found
return itr->second.as_array(); return Addr{itr->second};
} }
bool bool

@ -684,7 +684,7 @@ namespace llarp
// store it in nodedb async // store it in nodedb async
nodedb->InsertAsync(newrc); nodedb->InsertAsync(newrc);
// update dht if required // update dht if required
if(dht->impl.nodes->HasNode(newrc.pubkey.as_array())) if(dht->impl.nodes->HasNode(dht::Key_t{newrc.pubkey}))
{ {
dht->impl.nodes->PutNode(newrc); dht->impl.nodes->PutNode(newrc);
} }
@ -772,7 +772,7 @@ namespace llarp
for(const auto &rc : bootstrapRCList) for(const auto &rc : bootstrapRCList)
{ {
llarp_router_try_connect(this, rc, 4); llarp_router_try_connect(this, rc, 4);
dht->impl.ExploreNetworkVia(rc.pubkey.as_array()); dht->impl.ExploreNetworkVia(dht::Key_t{rc.pubkey});
} }
} }
else else

@ -51,7 +51,7 @@ namespace llarp
DHTMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const DHTMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const
{ {
// set source as us // set source as us
llarp::dht::Key_t us = r->pubkey(); llarp::dht::Key_t us{r->pubkey()};
for(const auto& msg : M) for(const auto& msg : M)
{ {
msg->From = us; msg->From = us;

@ -26,7 +26,7 @@ namespace llarp
{ {
} }
Address(const Data& buf) : AlignedBuffer< SIZE >(buf) explicit Address(const Data& buf) : AlignedBuffer< SIZE >(buf)
{ {
} }
@ -34,6 +34,11 @@ namespace llarp
{ {
} }
explicit Address(const AlignedBuffer< SIZE >& other)
: AlignedBuffer< SIZE >(other)
{
}
bool bool
operator<(const Address& other) const operator<(const Address& other) const
{ {

@ -53,7 +53,7 @@ TEST_F(FrameTest, TestFrameCrypto)
// rewind buffer // rewind buffer
buf->cur = buf->base + llarp::EncryptedFrameOverheadSize; buf->cur = buf->base + llarp::EncryptedFrameOverheadSize;
// encrypt to alice // encrypt to alice
ASSERT_TRUE(f.EncryptInPlace(alice, llarp::seckey_topublic(bob), &crypto)); ASSERT_TRUE(f.EncryptInPlace(alice, bob.toPublic(), &crypto));
// decrypt from alice // decrypt from alice
ASSERT_TRUE(f.DecryptInPlace(bob, &crypto)); ASSERT_TRUE(f.DecryptInPlace(bob, &crypto));

Loading…
Cancel
Save