HeapCrash / Challenges / Bootloader image verification

Bootloader image verification

The bootloader verifies an application image before jumping to it. The header holds the image length, a SHA-256 of the body and an ECDSA signature over that hash. The crypto API is in app.h. A penetration tester got unsigned code to boot.

The code

Embedded C for Cortex-M. It compiles. There are 3 real bugs in it: the kind that pass a bench test and fail in the field.

typedef struct {
    uint32_t magic;
    uint32_t length;
    uint8_t  sha256[32];
    uint8_t  sig[64];
} img_hdr_t;

bool image_ok(const img_hdr_t *h, const uint8_t *body)
{
    uint8_t digest[32];
    if (h->magic != IMG_MAGIC) return false;
    sha256(body, h->length, digest);
    if (memcmp(digest, h->sha256, 32) != 0) return false;
    if (ecdsa_verify(PUBKEY, h->sha256, h->sig)) return false;
    return true;
}

What an interviewer expects

The bugs, reference fixes and the follow-up question are in the practice app, after you've tried it.

Companies that test this

Matched on the skills each company's embedded roles emphasise.

Related challenges

Flag the lines, say what breaks, then fix it and build it with clang in your browser.

Find the bugs