Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 895 of 93612. IoT Connectivity and SecurityFree lesson

Cryptographic Hash Functions for Integrity

Learners will compute SHA-256 hashes to verify the integrity of firmware images and sensitive configuration data stored in flash memory.

In this lesson, you will learn how to leverage SHA-256 to ensure that your embedded device's firmware and configuration data have not been corrupted or tampered with while stored in flash memory.

The Role of Cryptographic Hashes in Integrity

In an IoT context, "integrity" means ensuring that the data you read from flash memory is identical to the data written during the provisioning or update process. Bit-flips caused by ionizing radiation, wear-leveling errors in NAND flash, or malicious modifications by an attacker are all threats to this integrity.

A cryptographic hash function like SHA-256 acts as a "digital fingerprint." It takes an arbitrary amount of data as input and produces a fixed-size 256-bit output. The critical property here is the avalanche effect: even a single bit change in the firmware image will result in a completely different, unpredictable hash value.

Verifying Flash Integrity

To verify the integrity of a firmware image, you store a "golden hash" in a write-protected region of your flash or an immutable ROM header. During the boot process, the bootloader performs the following steps:

  1. Read: The bootloader reads the firmware binary from the application partition of the flash.
  2. Compute: The CPU processes this data through the SHA-256 algorithm.
  3. Compare: The resulting hash is compared against the pre-computed "golden hash."
// Conceptual verification flow
uint8_t calculated_hash[32];
sha256_context ctx;

sha256_init(&ctx);
// Read firmware from Flash memory address 0x08008000
sha256_update(&ctx, (uint8_t*)0x08008000, FIRMWARE_SIZE);
sha256_final(&ctx, calculated_hash);

if (memcmp(calculated_hash, golden_hash, 32) == 0) {
    boot_firmware(); // Integrity verified
} else {
    enter_recovery_mode(); // Corruption detected
}

Because SHA-256 is computationally expensive, high-performance embedded systems often utilize hardware acceleration blocks (dedicated cryptographic accelerators) to compute these hashes without taxing the main application processor. By verifying the hash before execution, you prevent the device from running compromised or corrupted code, which is a foundational requirement for building a secure IoT platform.

Key Takeaway: Cryptographic hash functions transform a large binary payload into a unique fingerprint, allowing the system to instantly detect unauthorized or accidental modifications in flash memory before execution.