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

Protecting Secrets with Key Wrapping Protocols

Encapsulate sensitive keys using Hardware Security Module primitives to ensure they remain encrypted while stored in non-volatile memory.

In this lesson, you will learn how to use Key Wrapping protocols to securely store sensitive keys in non-volatile memory by encapsulating them with Hardware Security Module (HSM) primitives.

The Problem: Keys at Rest

In an IoT device, keeping a raw cryptographic key (like a TLS private key or an AES master key) in plaintext within your non-volatile storage (NVM) is a critical vulnerability. If an attacker dumps your flash memory, they gain full access to your secrets. While you could encrypt these keys with a software-based password, you then face the "chicken-and-egg" problem: where do you store the password used to decrypt the keys?

The Solution: Key Wrapping

Key Wrapping involves using a Key Encryption Key (KEK) to encrypt your sensitive Data Encryption Key (DEK). In a system with an HSM or a Hardware Root of Trust, the KEK never leaves the secure hardware boundary.

When you need to store a secret, the HSM performs the following steps:

  1. It generates or receives the DEK.
  2. It uses an internal, hardware-bound KEK to encrypt (wrap) the DEK.
  3. The resulting ciphertext (the "wrapped key") is sent to the NVM.

Because the KEK is tied to the physical silicon (often fused or stored in battery-backed SRAM), the wrapped key is useless on any other device or if extracted without the original hardware's cooperation.

Conceptual Implementation

You don't perform the cryptography in your application code; you offload the request to the HSM’s secure boundary.

// Pseudo-code for wrapping a key in a secure enclave
void store_secret(uint8_t *raw_key, size_t key_len) {
    uint8_t wrapped_key[WRAP_SIZE];
    
    // The HSM uses an internal KEK (inaccessible to the CPU) 
    // to perform the wrapping operation.
    hsm_status_t status = hsm_wrap_key(
        KEY_SLOT_INTERNAL_KEK, 
        raw_key, 
        key_len, 
        wrapped_key
    );

    if (status == HSM_SUCCESS) {
        nvm_write("my_secret.key", wrapped_key);
    }
}

By using the HSM as a black box for wrapping, your sensitive material is never exposed in plaintext to the main application processor or the NVM, effectively neutralizing the risk of memory dumping attacks.

Key Takeaway: Key Wrapping ensures that sensitive keys stored in NVM are encrypted using hardware-bound keys, preventing attackers from extracting usable secrets even if they obtain full access to the device's storage.