Skip to content

Commit

Permalink
comments integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Alami-Amine committed Dec 23, 2024
1 parent 6a9068f commit db95d7c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/lib/support/CodeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@
*
* @brief
* This is for use when the calling function returns a Variant type. It returns a CHIP_ERROR variant with the corresponding error
* code if the expression returns an error. For a CHIP_ERROR expression, this means any value other
* than CHIP_NO_ERROR. For an integer expression, this means non-zero.
* code if the expression returns an error.
*
* Example usage:
*
Expand Down
4 changes: 2 additions & 2 deletions src/protocols/secure_channel/CASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ CHIP_ERROR CASESession::PrepareSigma2Resume(EncodeSigma2ResumeInputs & outSigma2
outSigma2ResData.resumptionId = mNewResumptionId;

ReturnErrorOnFailure(GenerateSigmaResumeMIC(ByteSpan(mInitiatorRandom), mNewResumptionId, ByteSpan(kKDFS2RKeyInfo),
ByteSpan(kResume2MIC_Nonce), outSigma2ResData.resumeMICSpan));
ByteSpan(kResume2MIC_Nonce), outSigma2ResData.resumeMIC));

outSigma2ResData.responderMrpConfig = &mLocalMRPConfig.Value();

Expand Down Expand Up @@ -1191,7 +1191,7 @@ CHIP_ERROR CASESession::EncodeSigma2Resume(System::PacketBufferHandle & msgR2Res

ReturnErrorOnFailure(tlvWriter.StartContainer(AnonymousTag(), kTLVType_Structure, outerContainerType));
ReturnErrorOnFailure(tlvWriter.Put(AsTlvContextTag(Sigma2ResumeTags::kResumptionID), input.resumptionId));
ReturnErrorOnFailure(tlvWriter.Put(AsTlvContextTag(Sigma2ResumeTags::kSigma2ResumeMIC), input.resumeMICSpan));
ReturnErrorOnFailure(tlvWriter.Put(AsTlvContextTag(Sigma2ResumeTags::kSigma2ResumeMIC), input.resumeMIC));
ReturnErrorOnFailure(tlvWriter.Put(AsTlvContextTag(Sigma2ResumeTags::kResponderSessionID), input.responderSessionId));

ReturnErrorOnFailure(
Expand Down
21 changes: 10 additions & 11 deletions src/protocols/secure_channel/CASESession.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,14 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
// Helper Enum for use in HandleSigma1_and_SendSigma2
enum class Step : uint8_t
{
kNone,
kSendSigma2,
kSendSigma2Resume,
};
// Making NextStep a Variant allows HandleSigma() to return either a Step value (indicating
// the next Sigma step to send) or a CHIP_ERROR (indicating a failure that will trigger
// a Status Report).
using NextStep = Variant<Step, CHIP_ERROR>;
// This struct only serves as a base struct for EncodedSigma1Inputs and ParsedSigma1
// This struct only serves as a base struct for EncodeSigma1Inputs and ParsedSigma1
struct Sigma1Param
{
ByteSpan initiatorRandom;
Expand Down Expand Up @@ -242,31 +241,31 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
{
ByteSpan resumptionId;
uint8_t sigma2ResumeMICBuffer[Crypto::CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES];
MutableByteSpan resumeMICSpan{ sigma2ResumeMICBuffer };
MutableByteSpan resumeMIC{ sigma2ResumeMICBuffer };
uint16_t responderSessionId;
const ReliableMessageProtocolConfig * responderMrpConfig;
};

/**
* @brief Encodes a Sigma1 message into TLV format and allocates a buffer for it, which is owned by the PacketBufferHandle
*outparam.
* outparam.
*
* @param outMsg PacketBufferHandle passed by reference. A new buffer will be allocated and assigned to it within the
*method.
* method.
*
* @param inParam a struct containing all the values that will be encoded into TLV format
*
**/
static CHIP_ERROR EncodeSigma1(System::PacketBufferHandle & outMsg, EncodeSigma1Inputs & inParam);

/**
* Parse a sigma1 message. This function will return success only if the
* Parse a Sigma1 message. This function will return success only if the
* message passes schema checks. Specifically:
* * The tags come in order.
* * The required tags are present.
* * The values for the tags that are present satisfy schema requirements
* (e.g. constraints on octet string lengths)
* * Either resumptionID and initiatorResume1MICBuffer are both present or both
* * Either resumptionID and initiatorResume1MICBuffer are both present or both are
* absent.
*
* On success, the members of outParam will be set to the values corresponding to the message.
Expand All @@ -280,10 +279,10 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,

/**
* @brief Encodes a Sigma2 message into TLV format and allocates a buffer for it, which is owned by the PacketBufferHandle
*outparam.
* outparam.
*
* @param outMsg PacketBufferHandle passed by reference. A new buffer will be allocated and assigned to it within the
*method.
* method.
*
* @param inParam a struct containing all the values that will be encoded into TLV format
*
Expand All @@ -293,10 +292,10 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,

/**
* @brief Encodes a Sigma2_Resume message into TLV format and allocates a buffer for it, which is owned by the
*PacketBufferHandle outparam.
* PacketBufferHandle outparam.
*
* @param outMsg PacketBufferHandle passed by reference. A new buffer will be allocated and assigned to it within the
*method.
* method.
*
* @param inParam a struct containing all the values that will be encoded into TLV format
*
Expand Down
34 changes: 17 additions & 17 deletions src/protocols/secure_channel/tests/TestCASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,10 +888,10 @@ TEST_F(TestCASESession, EncodeSigma1Test)

uint8_t random[32];
EXPECT_EQ(chip::Crypto::DRBG_get_bytes(&random[0], sizeof(random)), CHIP_NO_ERROR);
encodeParams.initiatorRandom = ByteSpan(random, sizeof(random));
encodeParams.initiatorRandom = ByteSpan(random);
encodeParams.initiatorSessionId = 7315;
uint8_t destinationId[32] = { 0xDE, 0xAD };
encodeParams.destinationId = ByteSpan(destinationId, sizeof(destinationId));
encodeParams.destinationId = ByteSpan(destinationId);

ReliableMessageProtocolConfig mrpConfig = GetDefaultMRPConfig();
encodeParams.initiatorMrpConfig = &mrpConfig;
Expand Down Expand Up @@ -937,15 +937,15 @@ TEST_F(TestCASESession, EncodeSigma1Test)
tlvReader.Init(std::move(msg1));

CASESessionAccess session;
CASESessionAccess::ParsedSigma1 parseParams;
CASESessionAccess::ParsedSigma1 parsedMessage;

EXPECT_EQ(CHIP_NO_ERROR, session.ParseSigma1(tlvReader, parseParams));
EXPECT_EQ(CHIP_NO_ERROR, session.ParseSigma1(tlvReader, parsedMessage));

// compare parsed values with original values
EXPECT_TRUE(parseParams.initiatorRandom.data_equal(encodeParams.initiatorRandom));
EXPECT_EQ(parseParams.initiatorSessionId, encodeParams.initiatorSessionId);
EXPECT_TRUE(parseParams.destinationId.data_equal(encodeParams.destinationId));
EXPECT_TRUE(parseParams.initiatorEphPubKey.data_equal(
EXPECT_TRUE(parsedMessage.initiatorRandom.data_equal(encodeParams.initiatorRandom));
EXPECT_EQ(parsedMessage.initiatorSessionId, encodeParams.initiatorSessionId);
EXPECT_TRUE(parsedMessage.destinationId.data_equal(encodeParams.destinationId));
EXPECT_TRUE(parsedMessage.initiatorEphPubKey.data_equal(
ByteSpan(encodeParams.initiatorEphPubKey->ConstBytes(), encodeParams.initiatorEphPubKey->Length())));
}

Expand All @@ -971,20 +971,20 @@ TEST_F(TestCASESession, EncodeSigma1Test)
tlvReader.Init(std::move(msg2));

CASESessionAccess session;
CASESessionAccess::ParsedSigma1 parseParams;
CASESessionAccess::ParsedSigma1 parsedMessage;

EXPECT_EQ(CHIP_NO_ERROR, session.ParseSigma1(tlvReader, parseParams));
EXPECT_EQ(CHIP_NO_ERROR, session.ParseSigma1(tlvReader, parsedMessage));

// RoundTrip
EXPECT_TRUE(parseParams.initiatorRandom.data_equal(encodeParams.initiatorRandom));
EXPECT_EQ(parseParams.initiatorSessionId, encodeParams.initiatorSessionId);
EXPECT_TRUE(parseParams.destinationId.data_equal(encodeParams.destinationId));
EXPECT_TRUE(parseParams.initiatorEphPubKey.data_equal(
EXPECT_TRUE(parsedMessage.initiatorRandom.data_equal(encodeParams.initiatorRandom));
EXPECT_EQ(parsedMessage.initiatorSessionId, encodeParams.initiatorSessionId);
EXPECT_TRUE(parsedMessage.destinationId.data_equal(encodeParams.destinationId));
EXPECT_TRUE(parsedMessage.initiatorEphPubKey.data_equal(
ByteSpan(encodeParams.initiatorEphPubKey->ConstBytes(), encodeParams.initiatorEphPubKey->Length())));

EXPECT_TRUE(parseParams.resumptionId.data_equal(encodeParams.resumptionId));
EXPECT_TRUE(parseParams.initiatorResumeMIC.data_equal(encodeParams.initiatorResumeMIC));
EXPECT_TRUE(parseParams.initiatorMrpParamsPresent);
EXPECT_TRUE(parsedMessage.resumptionId.data_equal(encodeParams.resumptionId));
EXPECT_TRUE(parsedMessage.initiatorResumeMIC.data_equal(encodeParams.initiatorResumeMIC));
EXPECT_TRUE(parsedMessage.initiatorMrpParamsPresent);
}
// Release EphemeralKeyPair
gDeviceOperationalKeystore.ReleaseEphemeralKeypair(ephemeralKey);
Expand Down

0 comments on commit db95d7c

Please sign in to comment.