From 516466f5be6678345c28388b33fec32601b0fd5d Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 2 Jan 2019 01:04:08 +0000 Subject: [PATCH] Mark more AlignedBuffer constructors as explicit --- llarp/aligned.hpp | 2 +- llarp/crypto.hpp | 17 +++++++++++-- llarp/crypto_libsodium.cpp | 42 +++++++++++-------------------- llarp/dht.cpp | 4 +-- llarp/dht/context.hpp | 4 +-- llarp/dht/dht_immediate.cpp | 2 +- llarp/dht/find_router.cpp | 15 ++++++----- llarp/dht/key.hpp | 17 +++++++------ llarp/dht/node.hpp | 4 +-- llarp/dns_dotlokilookup.cpp | 8 +++--- llarp/encrypted_frame.cpp | 8 +++--- llarp/handlers/exit.cpp | 26 ++++++++++--------- llarp/handlers/tun.cpp | 10 ++++---- llarp/handlers/tun.hpp | 2 +- llarp/router.cpp | 4 +-- llarp/routing/dht_message.cpp | 2 +- llarp/service/address.hpp | 7 +++++- test/encrypted_frame_unittest.cpp | 2 +- 18 files changed, 92 insertions(+), 84 deletions(-) diff --git a/llarp/aligned.hpp b/llarp/aligned.hpp index dff47e44a..0f8f3078d 100644 --- a/llarp/aligned.hpp +++ b/llarp/aligned.hpp @@ -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()); diff --git a/llarp/crypto.hpp b/llarp/crypto.hpp index 063e7e7fc..1f2472efc 100644 --- a/llarp/crypto.hpp +++ b/llarp/crypto.hpp @@ -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); diff --git a/llarp/crypto_libsodium.cpp b/llarp/crypto_libsodium.cpp index b1a276492..48af8bf65 100644 --- a/llarp/crypto_libsodium.cpp +++ b/llarp/crypto_libsodium.cpp @@ -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; } diff --git a/llarp/dht.cpp b/llarp/dht.cpp index eea23c2d1..45683f750 100644 --- a/llarp/dht.cpp +++ b/llarp/dht.cpp @@ -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 diff --git a/llarp/dht/context.hpp b/llarp/dht/context.hpp index 248886c95..f0cd5f2cd 100644 --- a/llarp/dht/context.hpp +++ b/llarp/dht/context.hpp @@ -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; diff --git a/llarp/dht/dht_immediate.cpp b/llarp/dht/dht_immediate.cpp index be36c7df5..42149dba0 100644 --- a/llarp/dht/dht_immediate.cpp +++ b/llarp/dht/dht_immediate.cpp @@ -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")) { diff --git a/llarp/dht/find_router.cpp b/llarp/dht/find_router.cpp index 2afa29089..7e6eb1af3 100644 --- a/llarp/dht/find_router.cpp +++ b/llarp/dht/find_router.cpp @@ -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 diff --git a/llarp/dht/key.hpp b/llarp/dht/key.hpp index 21c3f730f..400553187 100644 --- a/llarp/dht/key.hpp +++ b/llarp/dht/key.hpp @@ -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; } diff --git a/llarp/dht/node.hpp b/llarp/dht/node.hpp index fbf10d8f6..a0fb201bc 100644 --- a/llarp/dht/node.hpp +++ b/llarp/dht/node.hpp @@ -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 diff --git a/llarp/dns_dotlokilookup.cpp b/llarp/dns_dotlokilookup.cpp index e5653c05f..2a52e5f53 100644 --- a/llarp/dns_dotlokilookup.cpp +++ b/llarp/dns_dotlokilookup.cpp @@ -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 diff --git a/llarp/encrypted_frame.cpp b/llarp/encrypted_frame.cpp index 2aaf46a0c..91e0000fc 100644 --- a/llarp/encrypted_frame.cpp +++ b/llarp/encrypted_frame.cpp @@ -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 // // 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; diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index 0dfa8c32a..ef4b0101f 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -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; } diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 60305e74f..69525d6b3 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -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; } diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index b0f1746aa..64b24f56a 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -111,7 +111,7 @@ namespace llarp return addr; } // found - return itr->second.as_array(); + return Addr{itr->second}; } bool diff --git a/llarp/router.cpp b/llarp/router.cpp index 02158ab75..3e0897d15 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -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 diff --git a/llarp/routing/dht_message.cpp b/llarp/routing/dht_message.cpp index b75893dbf..772158a95 100644 --- a/llarp/routing/dht_message.cpp +++ b/llarp/routing/dht_message.cpp @@ -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; diff --git a/llarp/service/address.hpp b/llarp/service/address.hpp index c7322a2a9..5c0a276b1 100644 --- a/llarp/service/address.hpp +++ b/llarp/service/address.hpp @@ -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 { diff --git a/test/encrypted_frame_unittest.cpp b/test/encrypted_frame_unittest.cpp index f89e2f5b8..f4885c09f 100644 --- a/test/encrypted_frame_unittest.cpp +++ b/test/encrypted_frame_unittest.cpp @@ -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));