Fix any remaning aliasing rules with propper memcpy wrappers

This commit is contained in:
Francisco Blas (klondike) Izquierdo Riera 2014-12-30 15:37:24 +01:00
parent 1636187e26
commit 700c53e60a
11 changed files with 140 additions and 94 deletions

View file

@ -299,18 +299,18 @@ namespace transport
uint8_t buf[96 + 18];
uint8_t * payload = buf + sizeof (SSUHeader);
*(uint32_t *)payload = htobe32 (iTag);
htobe32buf (payload, iTag);
payload += 4;
*payload = 0; // no address
payload++;
*(uint16_t *)payload = 0; // port = 0
htobuf16(payload, 0); // port = 0
payload += 2;
*payload = 0; // challenge
payload++;
memcpy (payload, (const uint8_t *)address->key, 32);
payload += 32;
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
*(uint32_t *)payload = htobe32 (rnd.GenerateWord32 ()); // nonce
htobe32buf (payload, rnd.GenerateWord32 ()); // nonce
uint8_t iv[16];
rnd.GenerateBlock (iv, 16); // random iv
@ -358,7 +358,7 @@ namespace transport
s.Insert (payload, 16); // remote endpoint IP V6
payload += 16;
}
*(uint16_t *)(payload) = htobe16 (m_RemoteEndpoint.port ());
htobe16buf (payload, m_RemoteEndpoint.port ());
s.Insert (payload, 2); // remote port
payload += 2;
if (address->host.is_v4 ())
@ -373,9 +373,9 @@ namespace transport
if (!relayTag) relayTag = 1;
m_Server.AddRelay (relayTag, m_RemoteEndpoint);
}
*(uint32_t *)(payload) = htobe32 (relayTag);
htobe32buf (payload, relayTag);
payload += 4; // relay tag
*(uint32_t *)(payload) = htobe32 (i2p::util::GetSecondsSinceEpoch ()); // signed on time
htobe32buf (payload, i2p::util::GetSecondsSinceEpoch ()); // signed on time
payload += 4;
s.Insert (payload - 8, 8); // relayTag and signed on time
s.Sign (i2p::context.GetPrivateKeys (), payload); // DSA signature
@ -404,12 +404,12 @@ namespace transport
*payload = 1; // 1 fragment
payload++; // info
size_t identLen = i2p::context.GetIdentity ().GetFullLen (); // 387+ bytes
*(uint16_t *)(payload) = htobe16 (identLen);
htobe16buf (payload, identLen);
payload += 2; // cursize
i2p::context.GetIdentity ().ToBuffer (payload, identLen);
payload += identLen;
uint32_t signedOnTime = i2p::util::GetSecondsSinceEpoch ();
*(uint32_t *)(payload) = htobe32 (signedOnTime); // signed on time
htobe32buf (payload, signedOnTime); // signed on time
payload += 4;
auto signatureLen = i2p::context.GetIdentity ().GetSignatureLen ();
size_t paddingSize = ((payload - buf) + signatureLen)%16;
@ -476,9 +476,9 @@ namespace transport
}
*payload = 4;
payload++; // size
*(uint32_t *)payload = htobe32 (to.address ().to_v4 ().to_ulong ()); // Charlie's IP
htobe32buf (payload, to.address ().to_v4 ().to_ulong ()); // Charlie's IP
payload += 4; // address
*(uint16_t *)payload = htobe16 (to.port ()); // Charlie's port
htobe16buf (payload, to.port ()); // Charlie's port
payload += 2; // port
// Alice
bool isV4 = from.address ().is_v4 (); // Alice's
@ -496,9 +496,9 @@ namespace transport
memcpy (payload, from.address ().to_v6 ().to_bytes ().data (), 16); // Alice's IP V6
payload += 16; // address
}
*(uint16_t *)payload = htobe16 (from.port ()); // Alice's port
htobe16buf (payload, from.port ()); // Alice's port
payload += 2; // port
*(uint32_t *)payload = htobe32 (nonce);
htobe32buf (payload, nonce);
if (m_State == eSessionStateEstablished)
{
@ -531,9 +531,9 @@ namespace transport
uint8_t * payload = buf + sizeof (SSUHeader);
*payload = 4;
payload++; // size
*(uint32_t *)payload = htobe32 (from.address ().to_v4 ().to_ulong ()); // Alice's IP
htobe32buf (payload, from.address ().to_v4 ().to_ulong ()); // Alice's IP
payload += 4; // address
*(uint16_t *)payload = htobe16 (from.port ()); // Alice's port
htobe16buf (payload, from.port ()); // Alice's port
payload += 2; // port
*payload = 0; // challenge size
uint8_t iv[16];
@ -612,7 +612,7 @@ namespace transport
encryption.Encrypt (encrypted, encryptedLen, encrypted);
// assume actual buffer size is 18 (16 + 2) bytes more
memcpy (buf + len, iv, 16);
*(uint16_t *)(buf + len + 16) = htobe16 (encryptedLen);
htobe16buf (buf + len + 16, encryptedLen);
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, macKey, header->mac);
}
@ -633,7 +633,7 @@ namespace transport
m_SessionKeyEncryption.Encrypt (encrypted, encryptedLen, encrypted);
// assume actual buffer size is 18 (16 + 2) bytes more
memcpy (buf + len, header->iv, 16);
*(uint16_t *)(buf + len + 16) = htobe16 (encryptedLen);
htobe16buf (buf + len + 16, encryptedLen);
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, m_MacKey, header->mac);
}
@ -682,7 +682,7 @@ namespace transport
uint16_t encryptedLen = len - (encrypted - buf);
// assume actual buffer size is 18 (16 + 2) bytes more
memcpy (buf + len, header->iv, 16);
*(uint16_t *)(buf + len + 16) = htobe16 (encryptedLen);
htobe16buf (buf + len + 16, encryptedLen);
uint8_t digest[16];
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, macKey, digest);
return !memcmp (header->mac, digest, 16);
@ -849,9 +849,9 @@ namespace transport
uint8_t size = *buf;
buf++; // size
uint32_t address = (size == 4) ? *(uint32_t *)buf : 0; // use it as is
uint32_t address = (size == 4) ? buf32toh(buf) : 0; // use it as is
buf += size; // address
uint16_t port = *(uint16_t *)buf; // use it as is
uint16_t port = buf16toh(buf); // use it as is
buf += 2; // port
uint8_t * introKey = buf;
if (port && !address)
@ -915,13 +915,13 @@ namespace transport
uint8_t buf[80 + 18];
uint8_t iv[16];
uint8_t * payload = buf + sizeof (SSUHeader);
*(uint32_t *)payload = htobe32 (nonce);
htobe32buf (payload, nonce);
payload += 4; // nonce
if (address)
{
*payload = 4;
payload++; // size
*(uint32_t *)payload = htobe32 (address);
htobe32buf (payload, address);
payload += 4; // address
}
else
@ -929,7 +929,7 @@ namespace transport
*payload = 0;
payload++; //size
}
*(uint16_t *)payload = htobe16 (port);
htobe16buf (payload, port);
payload += 2; // port
memcpy (payload, introKey, 32); // intro key