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;
std::copy(buf.begin(), buf.end(), begin());

@ -48,10 +48,17 @@ namespace llarp
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]";
}
PubKey
toPublic() const
{
return PubKey(data() + 32);
}
bool
LoadFromFile(const char *fname);

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

@ -22,7 +22,7 @@ llarp_dht_context_free(struct llarp_dht_context *ctx)
void
__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
@ -34,7 +34,7 @@ llarp_dht_allow_transit(llarp_dht_context *ctx)
void
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

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

@ -18,7 +18,7 @@ namespace llarp
DHTImmeidateMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
{
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);
if(llarp_buffer_eq(key, "v"))
{

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

@ -9,18 +9,22 @@ namespace llarp
{
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)
: llarp::AlignedBuffer< SIZE >(val)
explicit Key_t(const Data& val) : 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
{
Key_t dist;
std::transform(begin(), end(),
other.begin(), dist.begin(),
std::transform(begin(), end(), other.begin(), dist.begin(),
std::bit_xor< byte_t >());
return dist;
}

@ -20,10 +20,8 @@ namespace llarp
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

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

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

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

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

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

@ -684,7 +684,7 @@ namespace llarp
// store it in nodedb async
nodedb->InsertAsync(newrc);
// 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);
}
@ -772,7 +772,7 @@ namespace llarp
for(const auto &rc : bootstrapRCList)
{
llarp_router_try_connect(this, rc, 4);
dht->impl.ExploreNetworkVia(rc.pubkey.as_array());
dht->impl.ExploreNetworkVia(dht::Key_t{rc.pubkey});
}
}
else

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

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

Loading…
Cancel
Save