Skip to content

Commit

Permalink
Switch message header encode/decode input lengths to uint16_t. (#3449)
Browse files Browse the repository at this point in the history
This will remove some impedance mismatches around lengths later on.
  • Loading branch information
bzbarsky-apple authored Oct 27, 2020
1 parent 9299bc7 commit f6157a2
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/transport/RendezvousSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ CHIP_ERROR RendezvousSession::HandleSecureMessage(PacketBuffer * msgBuf)

payloadlen = packetHeader.GetPayloadLength();
VerifyOrExit(payloadlen <= len, err = CHIP_ERROR_INVALID_MESSAGE_LENGTH);
err = mac.Decode(packetHeader, &data[payloadlen], len - payloadlen, &taglen);
err = mac.Decode(packetHeader, &data[payloadlen], static_cast<uint16_t>(len - payloadlen), &taglen);
SuccessOrExit(err);

len = static_cast<uint16_t>(len - taglen);
Expand Down
6 changes: 3 additions & 3 deletions src/transport/SecureSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ CHIP_ERROR SecureSession::GetIV(const PacketHeader & header, uint8_t * iv, size_
}

CHIP_ERROR SecureSession::GetAdditionalAuthData(const PacketHeader & header, const Header::Flags payloadEncodeFlags, uint8_t * aad,
size_t & len)
uint16_t & len)
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t actualEncodedHeaderSize;
Expand All @@ -147,7 +147,7 @@ CHIP_ERROR SecureSession::Encrypt(const uint8_t * input, size_t input_length, ui
CHIP_ERROR error = CHIP_NO_ERROR;
uint8_t IV[kAESCCMIVLen];
uint8_t AAD[kMaxAADLen];
size_t aadLen = sizeof(AAD);
uint16_t aadLen = sizeof(AAD);

constexpr Header::EncryptionType encType = Header::EncryptionType::kAESCCMTagLen16;

Expand Down Expand Up @@ -182,7 +182,7 @@ CHIP_ERROR SecureSession::Decrypt(const uint8_t * input, size_t input_length, ui
const uint8_t * tag = mac.GetTag();
uint8_t IV[kAESCCMIVLen];
uint8_t AAD[kMaxAADLen];
size_t aadLen = sizeof(AAD);
uint16_t aadLen = sizeof(AAD);

VerifyOrExit(mKeyAvailable, error = CHIP_ERROR_INVALID_USE_OF_SESSION_KEY);
VerifyOrExit(input != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
Expand Down
2 changes: 1 addition & 1 deletion src/transport/SecureSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class DLL_EXPORT SecureSession
// The encryption operations includes AAD when message authentication tag is generated. This tag
// is used at the time of decryption to integrity check the received data.
static CHIP_ERROR GetAdditionalAuthData(const PacketHeader & header, Header::Flags payloadEncodeFlags, uint8_t * aad,
size_t & len);
uint16_t & len);
};

} // namespace chip
2 changes: 1 addition & 1 deletion src/transport/SecureSessionMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ void SecureSessionMgrBase::HandleDataReceived(const PacketHeader & packetHeader,
VerifyOrExit(
payloadlen <= len,
(ChipLogError(Inet, "Secure transport can't find MAC Tag; buffer too short"), err = CHIP_ERROR_INVALID_MESSAGE_LENGTH));
err = mac.Decode(packetHeader, &data[payloadlen], len - payloadlen, &taglen);
err = mac.Decode(packetHeader, &data[payloadlen], static_cast<uint16_t>(len - payloadlen), &taglen);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Inet, "Secure transport failed to decode MAC Tag: err %d", err));
len = static_cast<uint16_t>(len - taglen);
msg->SetDataLength(len, nullptr);
Expand Down
12 changes: 6 additions & 6 deletions src/transport/raw/MessageHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ uint16_t MessageAuthenticationCode::TagLenForEncryptionType(Header::EncryptionTy
}
}

CHIP_ERROR PacketHeader::Decode(const uint8_t * const data, size_t size, uint16_t * decode_len)
CHIP_ERROR PacketHeader::Decode(const uint8_t * const data, uint16_t size, uint16_t * decode_len)
{
CHIP_ERROR err = CHIP_NO_ERROR;
const uint8_t * p = data;
Expand Down Expand Up @@ -186,7 +186,7 @@ CHIP_ERROR PacketHeader::Decode(const uint8_t * const data, size_t size, uint16_
return err;
}

CHIP_ERROR PayloadHeader::Decode(Header::Flags flags, const uint8_t * const data, size_t size, uint16_t * decode_len)
CHIP_ERROR PayloadHeader::Decode(Header::Flags flags, const uint8_t * const data, uint16_t size, uint16_t * decode_len)
{
CHIP_ERROR err = CHIP_NO_ERROR;
const uint8_t * p = data;
Expand Down Expand Up @@ -218,7 +218,7 @@ CHIP_ERROR PayloadHeader::Decode(Header::Flags flags, const uint8_t * const data
return err;
}

CHIP_ERROR PacketHeader::Encode(uint8_t * data, size_t size, uint16_t * encode_size, Header::Flags payloadFlags) const
CHIP_ERROR PacketHeader::Encode(uint8_t * data, uint16_t size, uint16_t * encode_size, Header::Flags payloadFlags) const
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint8_t * p = data;
Expand Down Expand Up @@ -262,7 +262,7 @@ CHIP_ERROR PacketHeader::Encode(uint8_t * data, size_t size, uint16_t * encode_s
return err;
}

CHIP_ERROR PayloadHeader::Encode(uint8_t * data, size_t size, uint16_t * encode_size) const
CHIP_ERROR PayloadHeader::Encode(uint8_t * data, uint16_t size, uint16_t * encode_size) const
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint8_t * p = data;
Expand Down Expand Up @@ -290,7 +290,7 @@ Header::Flags PayloadHeader::GetEncodePacketFlags() const
return Header::Flags().Set(Header::FlagValues::kVendorIdPresent, mVendorId.HasValue());
}

CHIP_ERROR MessageAuthenticationCode::Decode(const PacketHeader & packetHeader, const uint8_t * const data, size_t size,
CHIP_ERROR MessageAuthenticationCode::Decode(const PacketHeader & packetHeader, const uint8_t * const data, uint16_t size,
uint16_t * decode_len)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -309,7 +309,7 @@ CHIP_ERROR MessageAuthenticationCode::Decode(const PacketHeader & packetHeader,
return err;
}

CHIP_ERROR MessageAuthenticationCode::Encode(const PacketHeader & packetHeader, uint8_t * data, size_t size,
CHIP_ERROR MessageAuthenticationCode::Encode(const PacketHeader & packetHeader, uint8_t * data, uint16_t size,
uint16_t * encode_size) const
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down
12 changes: 6 additions & 6 deletions src/transport/raw/MessageHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class PacketHeader
* CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size
* CHIP_ERROR_VERSION_MISMATCH if header version is not supported.
*/
CHIP_ERROR Decode(const uint8_t * data, size_t size, uint16_t * decode_size);
CHIP_ERROR Decode(const uint8_t * data, uint16_t size, uint16_t * decode_size);

/**
* Encodes a header into the given buffer.
Expand All @@ -240,7 +240,7 @@ class PacketHeader
* Possible failures:
* CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size
*/
CHIP_ERROR Encode(uint8_t * data, size_t size, uint16_t * encode_size, Header::Flags payloadFlags) const;
CHIP_ERROR Encode(uint8_t * data, uint16_t size, uint16_t * encode_size, Header::Flags payloadFlags) const;

private:
/// Represents the current encode/decode header version
Expand Down Expand Up @@ -376,7 +376,7 @@ class PayloadHeader
* CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size
* CHIP_ERROR_VERSION_MISMATCH if header version is not supported.
*/
CHIP_ERROR Decode(Header::Flags flags, const uint8_t * data, size_t size, uint16_t * decode_size);
CHIP_ERROR Decode(Header::Flags flags, const uint8_t * data, uint16_t size, uint16_t * decode_size);

/**
* Encodes the encrypted part of the header into the given buffer.
Expand All @@ -390,7 +390,7 @@ class PayloadHeader
* Possible failures:
* CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size
*/
CHIP_ERROR Encode(uint8_t * data, size_t size, uint16_t * encode_size) const;
CHIP_ERROR Encode(uint8_t * data, uint16_t size, uint16_t * encode_size) const;

/** Flags required for encoding this payload. */
Header::Flags GetEncodePacketFlags() const;
Expand Down Expand Up @@ -450,7 +450,7 @@ class MessageAuthenticationCode
* CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size
* CHIP_ERROR_VERSION_MISMATCH if header version is not supported.
*/
CHIP_ERROR Decode(const PacketHeader & packetHeader, const uint8_t * data, size_t size, uint16_t * decode_size);
CHIP_ERROR Decode(const PacketHeader & packetHeader, const uint8_t * data, uint16_t size, uint16_t * decode_size);

/**
* Encodes the Messae Authentication Tag into the given buffer.
Expand All @@ -465,7 +465,7 @@ class MessageAuthenticationCode
* Possible failures:
* CHIP_ERROR_INVALID_ARGUMENT on insufficient buffer size
*/
CHIP_ERROR Encode(const PacketHeader & packetHeader, uint8_t * data, size_t size, uint16_t * encode_size) const;
CHIP_ERROR Encode(const PacketHeader & packetHeader, uint8_t * data, uint16_t size, uint16_t * encode_size) const;

static uint16_t TagLenForEncryptionType(Header::EncryptionType encType);

Expand Down
14 changes: 7 additions & 7 deletions src/transport/raw/tests/TestMessageHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void TestPacketHeaderEncodeDecodeBounds(nlTestSuite * inSuite, void * inContext)
uint8_t buffer[64];
uint16_t unusedLen;

for (size_t shortLen = 0; shortLen < 10; shortLen++)
for (uint16_t shortLen = 0; shortLen < 10; shortLen++)
{
NL_TEST_ASSERT(inSuite, header.Encode(buffer, shortLen, &unusedLen, Header::Flags()) != CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, header.Decode(buffer, shortLen, &unusedLen) != CHIP_NO_ERROR);
Expand All @@ -204,7 +204,7 @@ void TestPacketHeaderEncodeDecodeBounds(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, header.Encode(buffer, minLen, &encoded_len, Header::Flags()) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, encoded_len == minLen);
// Verify that decoding at any smaller length fails.
for (size_t shortLen = 0; shortLen < encoded_len; shortLen++)
for (uint16_t shortLen = 0; shortLen < encoded_len; shortLen++)
{
NL_TEST_ASSERT(inSuite, header.Decode(buffer, shortLen, &unusedLen) != CHIP_NO_ERROR);
}
Expand All @@ -214,13 +214,13 @@ void TestPacketHeaderEncodeDecodeBounds(nlTestSuite * inSuite, void * inContext)

// Now test encoding/decoding with a source node id present.
header.SetSourceNodeId(1);
for (size_t shortLen = minLen; shortLen < minLen + 8; shortLen++)
for (uint16_t shortLen = minLen; shortLen < minLen + 8; shortLen++)
{
NL_TEST_ASSERT(inSuite, header.Encode(buffer, shortLen, &unusedLen, Header::Flags()) != CHIP_NO_ERROR);
}
NL_TEST_ASSERT(inSuite, header.Encode(buffer, minLen + 8, &encoded_len, Header::Flags()) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, encoded_len == minLen + 8);
for (size_t shortLen = 0; shortLen < encoded_len; shortLen++)
for (uint16_t shortLen = 0; shortLen < encoded_len; shortLen++)
{
NL_TEST_ASSERT(inSuite, header.Decode(buffer, shortLen, &unusedLen) != CHIP_NO_ERROR);
}
Expand All @@ -229,13 +229,13 @@ void TestPacketHeaderEncodeDecodeBounds(nlTestSuite * inSuite, void * inContext)

// Now test encoding/decoding with a source and destination node id present.
header.SetDestinationNodeId(1);
for (size_t shortLen = minLen; shortLen < minLen + 16; shortLen++)
for (uint16_t shortLen = minLen; shortLen < minLen + 16; shortLen++)
{
NL_TEST_ASSERT(inSuite, header.Encode(buffer, shortLen, &unusedLen, Header::Flags()) != CHIP_NO_ERROR);
}
NL_TEST_ASSERT(inSuite, header.Encode(buffer, minLen + 16, &encoded_len, Header::Flags()) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, encoded_len == minLen + 16);
for (size_t shortLen = 0; shortLen < encoded_len; shortLen++)
for (uint16_t shortLen = 0; shortLen < encoded_len; shortLen++)
{
NL_TEST_ASSERT(inSuite, header.Decode(buffer, shortLen, &unusedLen) != CHIP_NO_ERROR);
}
Expand All @@ -249,7 +249,7 @@ void TestPayloadHeaderEncodeDecodeBounds(nlTestSuite * inSuite, void * inContext
uint8_t buffer[64];
uint16_t unusedLen;

for (size_t shortLen = 0; shortLen < 6; shortLen++)
for (uint16_t shortLen = 0; shortLen < 6; shortLen++)
{
NL_TEST_ASSERT(inSuite, header.Encode(buffer, shortLen, &unusedLen) != CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, header.Decode(Header::Flags(), buffer, shortLen, &unusedLen) != CHIP_NO_ERROR);
Expand Down

0 comments on commit f6157a2

Please sign in to comment.