@@ -110,6 +110,8 @@ constexpr chip::TLV::Tag AsTlvContextTag(Enum e)
110
110
return chip::TLV::ContextTag (chip::to_underlying (e));
111
111
}
112
112
113
+ constexpr size_t kCaseOverheadForFutureTbeData = 128 ;
114
+
113
115
} // namespace
114
116
115
117
namespace chip {
@@ -1520,15 +1522,12 @@ CHIP_ERROR CASESession::HandleSigma2(System::PacketBufferHandle && msg)
1520
1522
GetRemoteSessionParameters ());
1521
1523
}
1522
1524
1523
- size_t msgR2EncryptedLen = parsedSigma2.msgR2Encrypted .AllocatedSize () - CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES;
1524
-
1525
- ReturnErrorOnFailure (AES_CCM_decrypt (parsedSigma2.msgR2Encrypted .Get (), msgR2EncryptedLen, nullptr , 0 ,
1526
- parsedSigma2.msgR2Encrypted .Get () + msgR2EncryptedLen, CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES,
1527
- sr2k.KeyHandle (), kTBEData2_Nonce , kTBEDataNonceLength ,
1528
- parsedSigma2.msgR2Encrypted .Get ()));
1525
+ ReturnErrorOnFailure (AES_CCM_decrypt (parsedSigma2.msgR2EncryptedPayload .data (), parsedSigma2.msgR2EncryptedPayload .size (),
1526
+ nullptr , 0 , parsedSigma2.msgR2Mic .data (), parsedSigma2.msgR2Mic .size (), sr2k.KeyHandle (),
1527
+ kTBEData2_Nonce , kTBEDataNonceLength , parsedSigma2.msgR2EncryptedPayload .data ()));
1529
1528
1530
1529
ContiguousBufferTLVReader decryptedDataTlvReader;
1531
- decryptedDataTlvReader.Init (parsedSigma2.msgR2Encrypted . Get (), msgR2EncryptedLen );
1530
+ decryptedDataTlvReader.Init (parsedSigma2.msgR2EncryptedPayload . data (), parsedSigma2. msgR2EncryptedPayload . size () );
1532
1531
ParsedSigma2TBEData parsedSigma2TBEData;
1533
1532
ReturnErrorOnFailure (ParseSigma2TBEData (decryptedDataTlvReader, parsedSigma2TBEData));
1534
1533
@@ -1600,8 +1599,6 @@ CHIP_ERROR CASESession::ParseSigma2(ContiguousBufferTLVReader & tlvReader, Parse
1600
1599
// Generate decrypted data
1601
1600
ReturnErrorOnFailure (tlvReader.Next (AsTlvContextTag (Sigma2Tags::kEncrypted2 )));
1602
1601
1603
- constexpr size_t kCaseOverheadForFutureTbeData = 128 ;
1604
-
1605
1602
size_t maxMsgR2SignedEncLen = EstimateStructOverhead (kMaxCHIPCertLength , // responderNOC
1606
1603
kMaxCHIPCertLength , // responderICAC
1607
1604
kMax_ECDSA_Signature_Length , // signature
@@ -1615,9 +1612,13 @@ CHIP_ERROR CASESession::ParseSigma2(ContiguousBufferTLVReader & tlvReader, Parse
1615
1612
VerifyOrReturnError (msgR2EncryptedLenWithTag <= maxMsgR2SignedEncLen, CHIP_ERROR_INVALID_TLV_ELEMENT);
1616
1613
VerifyOrReturnError (msgR2EncryptedLenWithTag > CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES, CHIP_ERROR_INVALID_TLV_ELEMENT);
1617
1614
VerifyOrReturnError (outParsedSigma2.msgR2Encrypted .Alloc (msgR2EncryptedLenWithTag), CHIP_ERROR_NO_MEMORY);
1618
-
1619
1615
ReturnErrorOnFailure (tlvReader.GetBytes (outParsedSigma2.msgR2Encrypted .Get (), outParsedSigma2.msgR2Encrypted .AllocatedSize ()));
1620
1616
1617
+ size_t msgR2EncryptedPayloadLen = msgR2EncryptedLenWithTag - CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES;
1618
+ outParsedSigma2.msgR2EncryptedPayload = MutableByteSpan (outParsedSigma2.msgR2Encrypted .Get (), msgR2EncryptedPayloadLen);
1619
+ outParsedSigma2.msgR2Mic =
1620
+ ByteSpan (outParsedSigma2.msgR2Encrypted .Get () + msgR2EncryptedPayloadLen, CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES);
1621
+
1621
1622
// Retrieve responderSessionParams if present
1622
1623
CHIP_ERROR err = tlvReader.Next ();
1623
1624
if (err == CHIP_NO_ERROR && tlvReader.GetTag () == AsTlvContextTag (Sigma2Tags::kResponderSessionParams ))
@@ -1662,11 +1663,10 @@ CHIP_ERROR CASESession::ParseSigma2TBEData(ContiguousBufferTLVReader & decrypted
1662
1663
}
1663
1664
1664
1665
VerifyOrReturnError (decryptedDataTlvReader.GetTag () == AsTlvContextTag (TBEDataTags::kSignature ), CHIP_ERROR_INVALID_TLV_TAG);
1665
- // TODO verify if the below modification in the check is correct (also for Sigma 3)
1666
1666
// tbsData2Signature's length should equal kMax_ECDSA_Signature_Length as per the Specification
1667
- VerifyOrReturnError (outParsedSigma2TBE. tbsData2Signature . Capacity () == decryptedDataTlvReader.GetLength (),
1668
- CHIP_ERROR_INVALID_TLV_ELEMENT);
1669
- outParsedSigma2TBE.tbsData2Signature .SetLength (decryptedDataTlvReader. GetLength () );
1667
+ size_t signatureLen = decryptedDataTlvReader.GetLength ();
1668
+ VerifyOrReturnError (outParsedSigma2TBE. tbsData2Signature . Capacity () == signatureLen, CHIP_ERROR_INVALID_TLV_ELEMENT);
1669
+ outParsedSigma2TBE.tbsData2Signature .SetLength (signatureLen );
1670
1670
ReturnErrorOnFailure (decryptedDataTlvReader.GetBytes (outParsedSigma2TBE.tbsData2Signature .Bytes (),
1671
1671
outParsedSigma2TBE.tbsData2Signature .Length ()));
1672
1672
@@ -1893,16 +1893,12 @@ CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg)
1893
1893
{
1894
1894
MATTER_TRACE_SCOPE (" HandleSigma3" , " CASESession" );
1895
1895
CHIP_ERROR err = CHIP_NO_ERROR;
1896
- System::PacketBufferTLVReader tlvReader;
1897
1896
ContiguousBufferTLVReader decryptedDataTlvReader;
1898
1897
TLVType containerType = kTLVType_Structure ;
1899
1898
1900
1899
const uint8_t * buf = msg->Start ();
1901
1900
const size_t bufLen = msg->DataLength ();
1902
1901
1903
- Platform::ScopedMemoryBufferWithSize<uint8_t > msgR3Encrypted;
1904
- size_t msgR3EncryptedLen = 0 ;
1905
-
1906
1902
AutoReleaseSessionKey sr3k (*mSessionManager ->GetSessionKeystore ());
1907
1903
1908
1904
uint8_t msg_salt[kIPKSize + kSHA256_Hash_Length ];
@@ -1925,32 +1921,39 @@ CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg)
1925
1921
1926
1922
VerifyOrExit (mEphemeralKey != nullptr , err = CHIP_ERROR_INTERNAL);
1927
1923
1928
- tlvReader.Init (std::move (msg));
1929
-
1930
- SuccessOrExit (err = ParseSigma3 (tlvReader, msgR3Encrypted));
1931
-
1932
1924
// Step 1
1925
+ // msgR3Encrypted will be allocated and initialised within ParseSigma3()
1926
+ Platform::ScopedMemoryBufferWithSize<uint8_t > msgR3Encrypted;
1927
+ // both msgR3EncryptedPayload and msgR3Mic will become backed by msgR3Encrypted in ParseSigma3()
1928
+ MutableByteSpan msgR3EncryptedPayload;
1929
+ ByteSpan msgR3Mic;
1933
1930
{
1931
+ System::PacketBufferTLVReader tlvReader;
1932
+ tlvReader.Init (std::move (msg));
1933
+ SuccessOrExit (err = ParseSigma3 (tlvReader, msgR3Encrypted, msgR3EncryptedPayload, msgR3Mic));
1934
+
1935
+ // Generate the S3K key
1934
1936
MutableByteSpan saltSpan (msg_salt);
1935
1937
SuccessOrExit (err = ConstructSaltSigma3 (ByteSpan (mIPK ), saltSpan));
1936
1938
SuccessOrExit (err = DeriveSigmaKey (saltSpan, ByteSpan (kKDFSR3Info ), sr3k));
1937
- }
1938
-
1939
- SuccessOrExit (err = mCommissioningHash .AddData (ByteSpan{ buf, bufLen }));
1940
1939
1940
+ // Add Sigma3 to the TranscriptHash which will be used to generate the Session Encryption Keys
1941
+ SuccessOrExit (err = mCommissioningHash .AddData (ByteSpan{ buf, bufLen }));
1942
+ }
1941
1943
// Step 2 - Decrypt data blob
1942
- msgR3EncryptedLen = msgR3Encrypted.AllocatedSize () - CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES;
1943
-
1944
- SuccessOrExit (err = AES_CCM_decrypt (msgR3Encrypted.Get (), msgR3EncryptedLen, nullptr , 0 ,
1945
- msgR3Encrypted.Get () + msgR3EncryptedLen, CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES,
1946
- sr3k.KeyHandle (), kTBEData3_Nonce , kTBEDataNonceLength , msgR3Encrypted.Get ()));
1944
+ SuccessOrExit (err = AES_CCM_decrypt (msgR3EncryptedPayload.data (), msgR3EncryptedPayload.size (), nullptr , 0 , msgR3Mic.data (),
1945
+ msgR3Mic.size (), sr3k.KeyHandle (), kTBEData3_Nonce , kTBEDataNonceLength ,
1946
+ msgR3EncryptedPayload.data ()));
1947
1947
1948
- decryptedDataTlvReader.Init (msgR3Encrypted. Get (), msgR3EncryptedLen );
1948
+ decryptedDataTlvReader.Init (msgR3EncryptedPayload. data (), msgR3EncryptedPayload. size () );
1949
1949
SuccessOrExit (err = ParseSigma3TBEData (decryptedDataTlvReader, data));
1950
1950
1951
1951
// Step 3 - Construct Sigma3 TBS Data
1952
- data.msgR3SignedLen = TLV::EstimateStructOverhead (data.initiatorNOC .size (), data.initiatorICAC .size (),
1953
- kP256_PublicKey_Length , kP256_PublicKey_Length );
1952
+ data.msgR3SignedLen = TLV::EstimateStructOverhead (data.initiatorNOC .size (), // initiatorNOC
1953
+ data.initiatorICAC .size (), // initiatorICAC
1954
+ kP256_PublicKey_Length , // initiatorEphPubKey
1955
+ kP256_PublicKey_Length // responderEphPubKey
1956
+ );
1954
1957
1955
1958
VerifyOrExit (data.msgR3Signed .Alloc (data.msgR3SignedLen ), err = CHIP_ERROR_NO_MEMORY);
1956
1959
@@ -2007,7 +2010,8 @@ CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg)
2007
2010
}
2008
2011
2009
2012
CHIP_ERROR CASESession::ParseSigma3 (ContiguousBufferTLVReader & tlvReader,
2010
- Platform::ScopedMemoryBufferWithSize<uint8_t > & msgR3Encrypted)
2013
+ Platform::ScopedMemoryBufferWithSize<uint8_t > & outMsgR3Encrypted,
2014
+ MutableByteSpan & outMsgR3EncryptedPayload, ByteSpan & outMsgR3Mic)
2011
2015
{
2012
2016
TLVType containerType = kTLVType_Structure ;
2013
2017
@@ -2017,8 +2021,6 @@ CHIP_ERROR CASESession::ParseSigma3(ContiguousBufferTLVReader & tlvReader,
2017
2021
// Fetch encrypted data
2018
2022
ReturnErrorOnFailure (tlvReader.Next (AsTlvContextTag (Sigma3Tags::kEncrypted3 )));
2019
2023
2020
- constexpr size_t kCaseOverheadForFutureTbeData = 128 ;
2021
-
2022
2024
size_t maxMsgR3SignedEncLen = EstimateStructOverhead (kMaxCHIPCertLength , // initiatorNOC
2023
2025
kMaxCHIPCertLength , // initiatorICAC
2024
2026
kMax_ECDSA_Signature_Length , // signature
@@ -2030,9 +2032,12 @@ CHIP_ERROR CASESession::ParseSigma3(ContiguousBufferTLVReader & tlvReader,
2030
2032
// Validate we did not receive a buffer larger than legal
2031
2033
VerifyOrReturnError (msgR3EncryptedLenWithTag <= maxMsgR3SignedEncLen, CHIP_ERROR_INVALID_TLV_ELEMENT);
2032
2034
VerifyOrReturnError (msgR3EncryptedLenWithTag > CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES, CHIP_ERROR_INVALID_TLV_ELEMENT);
2033
- VerifyOrReturnError (msgR3Encrypted.Alloc (msgR3EncryptedLenWithTag), CHIP_ERROR_NO_MEMORY);
2035
+ VerifyOrReturnError (outMsgR3Encrypted.Alloc (msgR3EncryptedLenWithTag), CHIP_ERROR_NO_MEMORY);
2036
+ ReturnErrorOnFailure (tlvReader.GetBytes (outMsgR3Encrypted.Get (), outMsgR3Encrypted.AllocatedSize ()));
2034
2037
2035
- ReturnErrorOnFailure (tlvReader.GetBytes (msgR3Encrypted.Get (), msgR3Encrypted.AllocatedSize ()));
2038
+ size_t msgR3EncryptedPayloadLen = msgR3EncryptedLenWithTag - CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES;
2039
+ outMsgR3EncryptedPayload = MutableByteSpan (outMsgR3Encrypted.Get (), msgR3EncryptedPayloadLen);
2040
+ outMsgR3Mic = ByteSpan (outMsgR3Encrypted.Get () + msgR3EncryptedPayloadLen, CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES);
2036
2041
2037
2042
ReturnErrorOnFailure (tlvReader.ExitContainer (containerType));
2038
2043
@@ -2060,9 +2065,9 @@ CHIP_ERROR CASESession::ParseSigma3TBEData(ContiguousBufferTLVReader & decrypted
2060
2065
}
2061
2066
2062
2067
VerifyOrReturnError (decryptedDataTlvReader.GetTag () == AsTlvContextTag (TBEDataTags::kSignature ), CHIP_ERROR_INVALID_TLV_TAG);
2063
- VerifyOrReturnError (outHandleSigma3TBEData. tbsData3Signature . Capacity () == decryptedDataTlvReader.GetLength (),
2064
- CHIP_ERROR_INVALID_TLV_ELEMENT);
2065
- outHandleSigma3TBEData.tbsData3Signature .SetLength (decryptedDataTlvReader. GetLength () );
2068
+ size_t signatureLen = decryptedDataTlvReader.GetLength ();
2069
+ VerifyOrReturnError (outHandleSigma3TBEData. tbsData3Signature . Capacity () == signatureLen, CHIP_ERROR_INVALID_TLV_ELEMENT);
2070
+ outHandleSigma3TBEData.tbsData3Signature .SetLength (signatureLen );
2066
2071
ReturnErrorOnFailure (decryptedDataTlvReader.GetBytes (outHandleSigma3TBEData.tbsData3Signature .Bytes (),
2067
2072
outHandleSigma3TBEData.tbsData3Signature .Length ()));
2068
2073
0 commit comments