Skip to content

Commit

Permalink
Compute sketch SHA256
Browse files Browse the repository at this point in the history
  • Loading branch information
pennam committed Jan 19, 2023
1 parent 0545ef5 commit 7a3dfb3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
40 changes: 38 additions & 2 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,44 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
*/
String const sha256_str = FlashSHA256::calc(XIP_BASE, 0x100000);
#elif defined(ARDUINO_ARCH_ESP32)
# warning "Compute image SHA256"
String const sha256_str;
SHA256 sha256;

uint32_t lengthLeft = ESP.getSketchSize();

const esp_partition_t *running = esp_ota_get_running_partition();
if (!running) {
DEBUG_ERROR("Partition could not be found");
}
const size_t bufSize = SPI_FLASH_SEC_SIZE;
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
uint32_t offset = 0;
if(!buf.get()) {
DEBUG_ERROR("Not enough memory to allocate buffer");
}

sha256.begin();
while( lengthLeft > 0) {
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
if (!ESP.flashRead(running->address + offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
DEBUG_ERROR("Could not read buffer from flash");
}
sha256.update(buf.get(), readBytes);
lengthLeft -= readBytes;
offset += readBytes;
}
/* Retrieve the final hash string. */
uint8_t sha256_hash[SHA256::HASH_SIZE] = {0};
sha256.finalize(sha256_hash);
String sha256_str;
std::for_each(sha256_hash,
sha256_hash + SHA256::HASH_SIZE,
[&sha256_str](uint8_t const elem)
{
char buf[4];
snprintf(buf, 4, "%02X", elem);
sha256_str += buf;
});
DEBUG_VERBOSE("SHA256: %d bytes (of %d) read", ESP.getSketchSize() - lengthLeft, ESP.getSketchSize());
#else
# error "No method for SHA256 checksum calculation over application image defined for this architecture."
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/tls/bearssl/dec32be.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

#include <AIoTC_Config.h>
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050) || defined(ARDUINO_ARCH_ESP32)

#include "inner.h"

Expand Down
2 changes: 1 addition & 1 deletion src/tls/bearssl/enc32be.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

#include <AIoTC_Config.h>
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050) || defined(ARDUINO_ARCH_ESP32)

#include "inner.h"

Expand Down
2 changes: 1 addition & 1 deletion src/tls/bearssl/sha2small.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

#include <AIoTC_Config.h>
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050) || defined(ARDUINO_ARCH_ESP32)

#include "inner.h"

Expand Down

0 comments on commit 7a3dfb3

Please sign in to comment.