Skip to content

Commit 7a3dfb3

Browse files
committed
Compute sketch SHA256
1 parent 0545ef5 commit 7a3dfb3

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,44 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
215215
*/
216216
String const sha256_str = FlashSHA256::calc(XIP_BASE, 0x100000);
217217
#elif defined(ARDUINO_ARCH_ESP32)
218-
# warning "Compute image SHA256"
219-
String const sha256_str;
218+
SHA256 sha256;
219+
220+
uint32_t lengthLeft = ESP.getSketchSize();
221+
222+
const esp_partition_t *running = esp_ota_get_running_partition();
223+
if (!running) {
224+
DEBUG_ERROR("Partition could not be found");
225+
}
226+
const size_t bufSize = SPI_FLASH_SEC_SIZE;
227+
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
228+
uint32_t offset = 0;
229+
if(!buf.get()) {
230+
DEBUG_ERROR("Not enough memory to allocate buffer");
231+
}
232+
233+
sha256.begin();
234+
while( lengthLeft > 0) {
235+
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
236+
if (!ESP.flashRead(running->address + offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
237+
DEBUG_ERROR("Could not read buffer from flash");
238+
}
239+
sha256.update(buf.get(), readBytes);
240+
lengthLeft -= readBytes;
241+
offset += readBytes;
242+
}
243+
/* Retrieve the final hash string. */
244+
uint8_t sha256_hash[SHA256::HASH_SIZE] = {0};
245+
sha256.finalize(sha256_hash);
246+
String sha256_str;
247+
std::for_each(sha256_hash,
248+
sha256_hash + SHA256::HASH_SIZE,
249+
[&sha256_str](uint8_t const elem)
250+
{
251+
char buf[4];
252+
snprintf(buf, 4, "%02X", elem);
253+
sha256_str += buf;
254+
});
255+
DEBUG_VERBOSE("SHA256: %d bytes (of %d) read", ESP.getSketchSize() - lengthLeft, ESP.getSketchSize());
220256
#else
221257
# error "No method for SHA256 checksum calculation over application image defined for this architecture."
222258
#endif

src/tls/bearssl/dec32be.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
#include <AIoTC_Config.h>
26-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
26+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050) || defined(ARDUINO_ARCH_ESP32)
2727

2828
#include "inner.h"
2929

src/tls/bearssl/enc32be.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
#include <AIoTC_Config.h>
26-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
26+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050) || defined(ARDUINO_ARCH_ESP32)
2727

2828
#include "inner.h"
2929

src/tls/bearssl/sha2small.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
#include <AIoTC_Config.h>
26-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
26+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050) || defined(ARDUINO_ARCH_ESP32)
2727

2828
#include "inner.h"
2929

0 commit comments

Comments
 (0)