-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[ICD] Add Check-In payload generation #27615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jepenven-silabs
merged 10 commits into
project-chip:master
from
jepenven-silabs:icd_checkin_protocol
Sep 1, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
39b8832
Add Check-In payload generation
jepenven-silabs c55422d
Applied PR comments
jepenven-silabs f53bd0e
Fix last comment
jepenven-silabs 0ad5d5d
address comments
jepenven-silabs 74c2762
address comments
jepenven-silabs ef095a3
fix comments
jepenven-silabs 03216ec
fix payload representation type
jepenven-silabs 38ce96c
Fix OpenIotSDK failure
jepenven-silabs af0a814
fix last test issue
jepenven-silabs 8bd1b14
fix asan
jepenven-silabs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jepenven-silabs marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* This file implements the Matter Checkin protocol. | ||
*/ | ||
|
||
#include "CheckinMessage.h" | ||
#include <lib/core/CHIPCore.h> | ||
|
||
#include <lib/core/CHIPEncoding.h> | ||
#include <protocols/secure_channel/Constants.h> | ||
|
||
namespace chip { | ||
namespace Protocols { | ||
namespace SecureChannel { | ||
|
||
CHIP_ERROR CheckinMessage::GenerateCheckinMessagePayload(Crypto::Aes128KeyHandle & key, CounterType counter, | ||
const ByteSpan & appData, MutableByteSpan & output) | ||
{ | ||
VerifyOrReturnError(appData.size() <= sMaxAppDataSize, CHIP_ERROR_INVALID_ARGUMENT); | ||
VerifyOrReturnError(output.size() >= (appData.size() + sMinPayloadSize), CHIP_ERROR_INVALID_ARGUMENT); | ||
|
||
CHIP_ERROR err = CHIP_NO_ERROR; | ||
uint8_t * appDataStartPtr = output.data() + CHIP_CRYPTO_AEAD_NONCE_LENGTH_BYTES; | ||
Encoding::LittleEndian::Put32(appDataStartPtr, counter); | ||
|
||
chip::Crypto::HMAC_sha shaHandler; | ||
uint8_t nonceWorkBuffer[CHIP_CRYPTO_HASH_LEN_BYTES] = { 0 }; | ||
|
||
ReturnErrorOnFailure(shaHandler.HMAC_SHA256(key.As<Aes128KeyByteArray>(), sizeof(Aes128KeyByteArray), appDataStartPtr, | ||
sizeof(CounterType), nonceWorkBuffer, CHIP_CRYPTO_HASH_LEN_BYTES)); | ||
|
||
static_assert(sizeof(nonceWorkBuffer) >= CHIP_CRYPTO_AEAD_NONCE_LENGTH_BYTES, "We're reading off the end of our buffer."); | ||
memcpy(output.data(), nonceWorkBuffer, CHIP_CRYPTO_AEAD_NONCE_LENGTH_BYTES); | ||
jepenven-silabs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// In place encryption to save some RAM | ||
memcpy(appDataStartPtr + sizeof(CounterType), appData.data(), appData.size()); | ||
|
||
uint8_t * micPtr = appDataStartPtr + sizeof(CounterType) + appData.size(); | ||
ReturnErrorOnFailure(Crypto::AES_CCM_encrypt(appDataStartPtr, sizeof(CounterType) + appData.size(), nullptr, 0, key, | ||
output.data(), CHIP_CRYPTO_AEAD_NONCE_LENGTH_BYTES, appDataStartPtr, micPtr, | ||
CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES)); | ||
|
||
output.reduce_size(appData.size() + sMinPayloadSize); | ||
|
||
return err; | ||
} | ||
|
||
CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, ByteSpan & payload, CounterType & counter, | ||
MutableByteSpan & appData) | ||
{ | ||
VerifyOrReturnError(payload.size() >= sMinPayloadSize, CHIP_ERROR_INVALID_ARGUMENT); | ||
VerifyOrReturnError(payload.size() <= (sMinPayloadSize + sMaxAppDataSize), CHIP_ERROR_INVALID_ARGUMENT); | ||
|
||
CHIP_ERROR err = CHIP_NO_ERROR; | ||
size_t appDataSize = GetAppDataSize(payload); | ||
|
||
// To prevent workbuffer usage, appData size needs to be large enough to hold both the appData and the counter | ||
VerifyOrReturnError(appData.size() >= sizeof(CounterType) + appDataSize, CHIP_ERROR_INVALID_ARGUMENT); | ||
|
||
ByteSpan nonce = payload.SubSpan(0, CHIP_CRYPTO_AEAD_NONCE_LENGTH_BYTES); | ||
ByteSpan encryptedData = payload.SubSpan(CHIP_CRYPTO_AEAD_NONCE_LENGTH_BYTES, sizeof(CounterType) + appDataSize); | ||
ByteSpan mic = | ||
payload.SubSpan(CHIP_CRYPTO_AEAD_NONCE_LENGTH_BYTES + sizeof(CounterType) + appDataSize, CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES); | ||
|
||
err = Crypto::AES_CCM_decrypt(encryptedData.data(), encryptedData.size(), nullptr, 0, mic.data(), mic.size(), key, nonce.data(), | ||
nonce.size(), appData.data()); | ||
|
||
ReturnErrorOnFailure(err); | ||
|
||
counter = Encoding::LittleEndian::Get32(appData.data()); | ||
// Shift to remove the counter from the appData | ||
memmove(appData.data(), sizeof(CounterType) + appData.data(), appDataSize); | ||
|
||
appData.reduce_size(appDataSize); | ||
return err; | ||
} | ||
|
||
size_t CheckinMessage::GetAppDataSize(ByteSpan & payload) | ||
{ | ||
return (payload.size() <= sMinPayloadSize) ? 0 : payload.size() - sMinPayloadSize; | ||
} | ||
|
||
} // namespace SecureChannel | ||
} // namespace Protocols | ||
} // namespace chip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* This file implements the Matter Checkin protocol. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <crypto/CHIPCryptoPAL.h> | ||
#include <lib/support/Span.h> | ||
#include <stdint.h> | ||
|
||
namespace chip { | ||
namespace Protocols { | ||
namespace SecureChannel { | ||
using namespace Crypto; | ||
|
||
using CounterType = uint32_t; | ||
|
||
/** | ||
* @brief Implement section 4.18.2 of the spec regarding | ||
* Check-in message payload | ||
* | ||
*/ | ||
class DLL_EXPORT CheckinMessage | ||
{ | ||
public: | ||
~CheckinMessage(); | ||
/** | ||
* @brief Generate Check-in Message payload | ||
* | ||
* @param key Key with which to encrypt the check-in payload | ||
* @param counter Check-in counter | ||
* @param appData Application Data to incorporate within the Check-in message. Allowed to be empty. | ||
* @param output Buffer in Which to store the generated payload. SUFFICIENT SPACE MUST BE ALLOCATED by the caller | ||
* Required Buffer Size is : GetCheckinPayloadSize(appData.size()) | ||
* @return CHIP_ERROR | ||
*/ | ||
static CHIP_ERROR GenerateCheckinMessagePayload(Crypto::Aes128KeyHandle & key, CounterType counter, const ByteSpan & appData, | ||
MutableByteSpan & output); | ||
|
||
/** | ||
* @brief Parse Check-in Message payload | ||
* | ||
* @param key Key with which to decrypt the check-in payload | ||
* @param payload The received payload to decrypt and parse | ||
* @param counter The counter value retrieved from the payload | ||
* @param appData The optional application data decrypted. The size of appData must be at least the size of | ||
* GetAppDataSize(payload) + sizeof(CounterType) | ||
* @return CHIP_ERROR | ||
*/ | ||
static CHIP_ERROR ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, ByteSpan & payload, CounterType & counter, | ||
MutableByteSpan & appData); | ||
|
||
static inline size_t GetCheckinPayloadSize(size_t appDataSize) { return appDataSize + sMinPayloadSize; } | ||
|
||
/** | ||
* @brief Get the App Data Size | ||
* | ||
* @param payload The undecrypted payload | ||
* @return size_t size in byte of the application data from the payload | ||
*/ | ||
static size_t GetAppDataSize(ByteSpan & payload); | ||
|
||
static constexpr uint16_t sMinPayloadSize = | ||
CHIP_CRYPTO_AEAD_NONCE_LENGTH_BYTES + sizeof(CounterType) + CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES; | ||
|
||
// Issue #28603 | ||
static constexpr uint16_t sMaxAppDataSize = 1024; | ||
}; | ||
|
||
} // namespace SecureChannel | ||
} // namespace Protocols | ||
} // namespace chip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.