diff --git a/src/platform/nxp/k32w/common/OTATlvProcessor.cpp b/src/platform/nxp/k32w/common/OTATlvProcessor.cpp index c313fd0c13989f..ed79d0c39e53ad 100644 --- a/src/platform/nxp/k32w/common/OTATlvProcessor.cpp +++ b/src/platform/nxp/k32w/common/OTATlvProcessor.cpp @@ -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; @@ -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) diff --git a/src/platform/nxp/k32w/common/OTATlvProcessor.h b/src/platform/nxp/k32w/common/OTATlvProcessor.h index e412e1cdd1c5fa..13a2df115d90d9 100644 --- a/src/platform/nxp/k32w/common/OTATlvProcessor.h +++ b/src/platform/nxp/k32w/common/OTATlvProcessor.h @@ -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;