Skip to content

Commit

Permalink
[K32W0] use Encoding::HexToBytes to parse the ota encryption key
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Tan authored and marius-alex-tache committed Jan 12, 2024
1 parent 007b113 commit 4f7e967
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/platform/nxp/k32w/common/OTATlvProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ CHIP_ERROR OTADataAccumulator::Accumulate(ByteSpan & block)
CHIP_ERROR OTATlvProcessor::vOtaProcessInternalEncryption(MutableByteSpan & block)
{
uint8_t iv[16];
uint8_t key[16];
uint8_t dataOut[16] = { 0 };
uint8_t key[kOTAEncryptionKeyLength];
uint8_t dataOut[16] = {0};
uint32_t u32IVCount;
uint32_t Offset = 0;
uint8_t data;
Expand All @@ -119,23 +119,18 @@ CHIP_ERROR OTATlvProcessor::vOtaProcessInternalEncryption(MutableByteSpan & bloc
u32IVCount = (((uint32_t) iv[12]) << 24) | (((uint32_t) iv[13]) << 16) | (((uint32_t) iv[14]) << 8) | (iv[15]);
u32IVCount += (mIVOffset >> 4);

iv[12] = (uint8_t) ((u32IVCount >> 24) & 0xff);
iv[13] = (uint8_t) ((u32IVCount >> 16) & 0xff);
iv[14] = (uint8_t) ((u32IVCount >> 8) & 0xff);
iv[15] = (uint8_t) (u32IVCount & 0xff);
iv[12] = (uint8_t)((u32IVCount >> 24) & 0xff);
iv[13] = (uint8_t)((u32IVCount >> 16) & 0xff);
iv[14] = (uint8_t)((u32IVCount >> 8) & 0xff);
iv[15] = (uint8_t)(u32IVCount & 0xff);

size_t len = strlen(OTA_ENCRYPTION_KEY);

if (len != 32)
if (Encoding::HexToBytes(OTA_ENCRYPTION_KEY, strlen(OTA_ENCRYPTION_KEY),
key,kOTAEncryptionKeyLength) != kOTAEncryptionKeyLength)
{
return CHIP_ERROR_INVALID_ARGUMENT;
//Failed to convert the OTAEncryptionKey string to octstr type value
return CHIP_ERROR_INVALID_STRING_LENGTH;
}

for (size_t i = 0; i < len; i += 2)
{
char hex[3] = { OTA_ENCRYPTION_KEY[i], OTA_ENCRYPTION_KEY[i + 1], '\0' };
key[i / 2] = (uint8_t) strtol(hex, NULL, 16);
}
ByteSpan KEY = ByteSpan(key);
Encoding::LittleEndian::Reader reader_key(KEY.data(), KEY.size());
ReturnErrorOnFailure(reader_key.Read32(&sKey.u32register0)
Expand Down
2 changes: 2 additions & 0 deletions src/platform/nxp/k32w/common/OTATlvProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class OTATlvProcessor
#if OTA_ENCRYPTION_ENABLE
/*ota decryption*/
uint32_t mIVOffset = 0;
/* Expected byte size of the OTAEncryptionKeyLength */
static constexpr size_t kOTAEncryptionKeyLength = 16;
#endif
uint32_t mLength = 0;
uint32_t mProcessedLength = 0;
Expand Down

0 comments on commit 4f7e967

Please sign in to comment.