Mark AlignedBuffer constructor as explicit

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

@ -35,7 +35,7 @@ namespace llarp
Zero();
}
AlignedBuffer(const byte_t* data)
explicit AlignedBuffer(const byte_t* data)
{
new(&val) Data;
auto& b = as_array();

@ -16,10 +16,10 @@ namespace llarp
// <32 bytes pubkey>
// <N bytes encrypted payload>
//
byte_t* hash = data();
byte_t* nonce = hash + SHORTHASHSIZE;
byte_t* pubkey = nonce + TUNNONCESIZE;
byte_t* body = pubkey + PUBKEYSIZE;
byte_t* hash = data();
byte_t* noncePtr = hash + SHORTHASHSIZE;
byte_t* pubkey = noncePtr + TUNNONCESIZE;
byte_t* body = pubkey + PUBKEYSIZE;
SharedSecret shared;
@ -33,9 +33,10 @@ namespace llarp
buf.sz = size() - EncryptedFrameOverheadSize;
// set our pubkey
memcpy(pubkey, llarp::seckey_topublic(ourSecretKey), PUBKEYSIZE);
memcpy(pubkey, seckey_topublic(ourSecretKey), PUBKEYSIZE);
// randomize nonce
crypto->randbytes(nonce, TUNNONCESIZE);
crypto->randbytes(noncePtr, TUNNONCESIZE);
TunnelNonce nonce(noncePtr);
// derive shared key
if(!DH(shared, otherPubkey, ourSecretKey, nonce))
@ -52,7 +53,7 @@ namespace llarp
}
// generate message auth
buf.base = nonce;
buf.base = noncePtr;
buf.cur = buf.base;
buf.sz = size() - SHORTHASHSIZE;
@ -74,23 +75,18 @@ namespace llarp
// <32 bytes pubkey>
// <N bytes encrypted payload>
//
byte_t* hash = data();
byte_t* nonce = hash + SHORTHASHSIZE;
byte_t* otherPubkey = nonce + TUNNONCESIZE;
byte_t* body = otherPubkey + PUBKEYSIZE;
ShortHash hash(data());
byte_t* noncePtr = data() + SHORTHASHSIZE;
PubKey otherPubkey = noncePtr + TUNNONCESIZE;
byte_t* body = data() + EncryptedFrameOverheadSize;
TunnelNonce nonce(noncePtr);
// use dh_server because we are not the creator of this message
auto DH = crypto->dh_server;
auto Decrypt = crypto->xchacha20;
auto MDS = crypto->hmac;
llarp_buffer_t buf;
buf.base = nonce;
buf.cur = buf.base;
buf.sz = size() - SHORTHASHSIZE;
SharedSecret shared;
ShortHash digest;
if(!DH(shared, otherPubkey, ourSecretKey, nonce))
{
@ -98,13 +94,19 @@ namespace llarp
return false;
}
llarp_buffer_t buf;
buf.base = noncePtr;
buf.cur = buf.base;
buf.sz = size() - SHORTHASHSIZE;
ShortHash digest;
if(!MDS(digest.data(), buf, shared))
{
llarp::LogError("Digest failed");
return false;
}
if(!std::equal(digest.begin(), digest.end(), hash))
if(!std::equal(digest.begin(), digest.end(), hash.begin()))
{
llarp::LogError("message authentication failed");
return false;

@ -911,10 +911,10 @@ namespace llarp
vec.iov_base = buf.data();
vec.iov_len = FragmentBufferSize;
buf.Randomize();
byte_t* nonce = buf.data() + FragmentHashSize;
byte_t* body = nonce + FragmentNonceSize;
byte_t* base = body;
AlignedBuffer< 24 > A = base;
byte_t* noncePtr = buf.data() + FragmentHashSize;
byte_t* body = noncePtr + FragmentNonceSize;
byte_t* base = body;
AlignedBuffer< 24 > A(base);
// skip inner nonce
body += A.size();
// put msgid
@ -932,11 +932,13 @@ namespace llarp
auto payload =
InitBuffer(base, FragmentBufferSize - FragmentOverheadSize);
TunnelNonce nonce(noncePtr);
// encrypt
if(!Crypto()->xchacha20(payload, txKey, nonce))
return false;
payload.base = nonce;
payload.base = noncePtr;
payload.cur = payload.base;
payload.sz = FragmentBufferSize - FragmentHashSize;
// key'd hash
@ -1032,7 +1034,7 @@ namespace llarp
return false;
}
// get inner nonce
AlignedBuffer< 24 > A = out.base;
AlignedBuffer< 24 > A(out.base);
// advance buffer
out.cur += A.size();
// read msgid

@ -49,7 +49,7 @@ namespace llarp
hop.nonce.Randomize();
// do key exchange
if(!ctx->crypto->dh_client(hop.shared, hop.rc.enckey, hop.commkey,
hop.nonce.data()))
hop.nonce))
{
llarp::LogError("Failed to generate shared key for path build");
delete ctx;

@ -33,7 +33,8 @@ namespace llarp
auto c = Crypto();
ASSERT_TRUE(keys.size() == PQ_KEYPAIRSIZE);
ASSERT_TRUE(c->pqe_encrypt(block, shared, pq_keypair_to_public(keys)));
ASSERT_TRUE(
c->pqe_encrypt(block, shared, PQPubKey(pq_keypair_to_public(keys))));
ASSERT_TRUE(c->pqe_decrypt(block, otherShared, pq_keypair_to_secret(keys)));
ASSERT_TRUE(otherShared == shared);
}

Loading…
Cancel
Save