seal: ECDSA signature r/s shorter than 32 bytes is written wrong (der_to_raw copies 32-len bytes), ~1 in 256 seals do not verify
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- c
- Domain
- cryptography, security
Research direction
The bug is in der_to_raw() in bintool/mbedtls_wrapper.c, in the short-INTEGER branches near lines 169 and 179, where the copy length is 32 - b instead of b. Read those branches and the DER length handling above them, then check whether PR #358 already rewrites this function before changing it. Done means r and s are right-aligned with the correct bytes for short DER integers, and a signature with a leading-zero-short r or s verifies against the image hash.
Written by the indexing model from the issue text.
Description
picotool seal --sign intermittently writes a SIGNATURE block item that does not verify. It happens whenever the DER encoding of ECDSA's r or s is shorter than 32 bytes, i.e. the 32-byte value has a leading zero byte and the next byte is below 0x80. For a random 256-bit value that is 1/512; with two values per signature about 1 seal in 256 is affected. The ECDSA nonce is random, so re-running the same seal command on the same input gives a different signature and the failure looks random.
The HASH_VALUE item is correct in every bad image; only the signature is wrong. An image sealed this way does not pass signature verification, so it will not boot on an RP2350 with secure boot enabled.
Version: picotool 2.3.0 (tag 2.3.0, 6f6458d). The same code is on master and develop today (bintool/mbedtls_wrapper.c lines 169 and 179 at ba3df40).
Repro
- Any RP2350 ELF and a secp256k1 private key in PEM form.
- Seal it repeatedly; each run draws a fresh nonce:
for i in $(seq 2000); do picotool seal --quiet in.elf out_$i.elf key.pem --sign --hash done - Verify each signature independently, e.g. with Python
cryptography: take the image's SHA-256 (the HASH_VALUE item, which is correct) and the 64-byter || sfrom the SIGNATURE item, and callpublic_key.verify(encode_dss_signature(r, s), digest, ec.ECDSA(Prehashed(SHA256()))).
Observed
Windows builds (GCC 16.1, mbedtls from pico-sdk 2.3.0 lib/mbedtls, 0bebf8b), same input ELF and key throughout:
| build | seals | fail verification |
|---|---|---|
| 2.3.0, built from source | 2000 | 9 (0.45%; expected 1/256 = 0.39%) |
| 2.3.0, pico-sdk-tools Windows release binary | 2000 | 7 |
develop at ba3df40, built from source |
2000 | 7 |
develop at ba3df40 + the fix below |
2000 | 0 |
An earlier independent run of 150 seals of 2.3.0 showed 2 failures.
Every failing signature has the same shape: one half is 00 XX followed by 30 zero bytes, e.g.
r = 007f000000000000000000000000000000000000000000000000000000000000
s = efc6bedd550f00ebc968cfbba229c34573a58cbb914fe7a27d304e8ac667eb1c
Root cause
der_to_raw() in bintool/mbedtls_wrapper.c, the branch for a DER INTEGER shorter than 32 bytes:
} else {
memset(r, 0, sizeof(r));
memcpy(r + (32 - b2), sig->der + 4, (32 - b2)); // copies 32-b2 bytes, should copy b2
}
...
} else {
memset(s, 0, sizeof(r));
memcpy(s + (32 - b3), sig->der + 6 + b2, (32 - b3)); // same, for s
}
The destination offset 32 - b is right (right-align, zero-pad on the left), but the length is 32 - b instead of b. For the usual short case b == 31 this copies one byte instead of 31, which produces exactly the 00 XX 00..00 pattern above.
A two-line fix PR follows. Note that #358 rewrites der_to_raw() with a read_der_integer() helper that copies len bytes, so it would also fix this in passing; the separate PR is the minimal standalone change in case a point release wants it on its own.
- Dominant language
- C++
- Stars
- 1k
- Forks
- 199
- Avg merge
- 7d 18h
- Merged PRs (30d)
- 4
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from raspberrypi/picotool
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
raspberrypi/picotool#371 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
raspberrypi/picotool#347 ·
-
Difficulty 4/5 1-2 days Newbie friendliness 55/100
raspberrypi/picotool#374 · 1 comment ·
-
Add 'partition' options to 'save' and 'verify' commandsPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 68/100
raspberrypi/picotool#348 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
raspberrypi/picotool#330 · 1 comment ·
All issues in raspberrypi/picotool
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
distante/esphome-components#50 · 1 comment ·
Maintainers usually reply within 1 day
-
good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
bradley499/sss-guis#45 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
feat
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day