Skip to content

Ed25519, Ed448, X25519 and X448 stack residue check and wipes - #196

Draft
dghgit wants to merge 16 commits into
release/0.1.4alphafrom
ci/ed-stack-check
Draft

dghgit wants to merge 16 commits into
release/0.1.4alphafrom
ci/ed-stack-check

Conversation

@dghgit

@dghgit dghgit commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Draft, opened so the ecdsa-stack workflow's arm64 leg runs on these changes before they are squashed onto the release branch; every wipe here is sized on x86-64 only.

  • dev_scripts/ecdsa_stack_harness gains Ed25519, Ed448, X25519 and X448: keygen, decoding, encoding, pk(), clone, every signing form (pre-hash and context included) and X agreement, searched for the private key, h, s, prefix, r, K and the clamped scalar (bytes and limbs), and compared over two runs with different keys. The RFC 8032 and RFC 7748 vectors and S = r + k·s mod L are checked first.
  • Ed25519 and Ed448 fixed-base multiplication runs in a wiped frame, as X448's already did.
  • Ed/X keygen, keygen_from_seed, decoding and clone go through in_wiped_frame; their by-value copies of the key are no longer left behind.
  • Outputs are byte-identical; about +0.27% instructions on a mix of decoding, signing and agreement. Stack tables re-measured.

x86-64 (rustc 1.97 and 1.88): 790/791 ok, 0 FAIL; only X25519's documented scalar-multiplication residue is reported as left. The Ed25519 docs record a word of SHA-512 state after from_bytes_out on arm64, so the arm64 leg may fail on that row.

Assisted-by: Claude:claude-opus-5-5

scalar_mult_base_encoded runs in a frame of its own and overwrites the
stack it used (3072 bytes for Ed25519, 4096 for Ed448), as X448's
ladder does. A comparison of two runs over different keys found limbs
of the point's coordinates and of the normalizing inversion left after
keygen_out, from_bytes_out and every form of signing. Outputs are
byte-identical (old-vs-new differential over 300 keys); the ec tests,
RFC 8032 vectors and Wycheproof suites pass.

Assisted-by: Claude:claude-opus-5-5
keygen, keygen_from_seed, the trait's from_bytes and clone build the
key in a frame of their own whose stack is overwritten afterwards, as
the ECDSA and ECC CDH keys do; the move out of that frame left whole
copies of the private key, its digest, s and prefix behind. The Ed
keygen wipe also covers the public key's decoding, so that two runs
over different keys leave the same stack. Outputs are byte-identical;
the ec tests, RFC 8032 / RFC 7748 vectors and Wycheproof suites pass.

Assisted-by: Claude:claude-opus-5-5
The ECDSA stack harness gains an ed module: EdDSA key generation,
decoding, copies, encodings, pk and every form of signing (one-shots,
pre-hash one-shots and streaming final), and X25519 and X448 keys and
agreement, searched for the private key, its digest, s, prefix, the
nonce digest, r and K, each checked against a real output (RFC 8032
and RFC 7748 vectors, S = r + k s mod L, encode == seed, the peer's K),
and compared over two keys. X25519's scalar-multiplication residue is
reported as left, by decision. 792 ok on rustc 1.97 and 1.88; with the
ed wipes zeroed, 61 rows fail on both. CLAUDE.md names the constants.

Assisted-by: Claude:claude-opus-5-5
The new wipes deepen key generation, private-key decoding and signing:
by 1136 to 1680 bytes for Ed25519, 1416 to 2280 for Ed448, and by up to
416 bytes for the by-value XDH keys. Measured as before (deepest frame
chain below the mem bench's measure wrapper, rustc 1.97 nightly, x86-64);
the base commit reproduces the old figures exactly.

Assisted-by: Claude:claude-opus-5-5
X25519's and X448's K and clamped scalar are also searched as field
limbs (radix 51, radix 56); with X448's ladder wipe zeroed the search
finds two limbs of K. EdDSA keygen is keygen_out, still compared, then
pk's decoding of the public key, whose square-root intermediates are
public and differ by key; adding the decoded x to the public image did
not explain them (407 to 372 bytes), so keygen is searched, not
compared, as pk already was.

Assisted-by: Claude:claude-opus-5-5
KEYGEN_STACK_WORDS was sized to also erase the public key's decoding,
which is public. Sized by the copies of the key pair it removes: 1024
bytes for Ed25519 (384 needed, 320 too few, rustc 1.97 and 1.88) and
1536 for Ed448 (768 and 640 on 1.97, 640 and 512 on 1.88). The stack
tables do not move; keygen peaks in the multiplication's wipe. The
Ed25519 docs say the check was measured on x86-64, not yet arm64.

Assisted-by: Claude:claude-opus-5-5
@hubot
hubot deleted the ci/ed-stack-check branch November 27, 2026 06:21
…d registers zeroed

On arm64 (the first run of PR #196's arm64 legs) both harnesses found
words of the key in frames the library does not own: SM2 encode,
encode_out, clone and derive_pk (dB and dA), the encryptions (k), and a
word of k or a key in the register saves of the operation's first
frames, which also made two runs differ past SAVED_REGISTERS (SM2
decryption with C2 changed, ECDSA P-384 and brainpoolP384r1 final,
the DHKEM encaps and decaps rows, Ed448 from_bytes, from_bytes_out and
ph signing). The disassembly shows the source: main holds a word of the
secret it just computed with in x28 or x22, and run_deep, measure or a
library frame saves it. arm64 has ten callee-saved registers to x86-64's
six, so main keeps more there.

scrubbed.rs is a small assembly trampoline (aarch64 and x86-64) that
saves the callee-saved registers in its own frame, above everything
searched, zeroes them and makes the call. Scan::run also paints from
that frame, so the slot measure's 64-byte frame leaves unwritten holds
paint rather than an earlier setup call's residue (the ECDSA
create_signer finding, a limb from the from_bytes before it).
STACK_DETAIL=1 prints where each finding lies, as the symmetric harness
does.

With this alone every SM2 row passes on arm64 (rustc 1.100 under
qemu-user, which reproduces CI's arm64 output); the ECDSA harness still
finds the from_bytes limb and the Ed25519 signing comparison, fixed in
the next two commits. x86-64 passes with rustc 1.97, 1.88 and 1.100.

Assisted-by: Claude:claude-opus-5-5
…order

The comparison of two runs counted a differing run as public only when
it was one substring of the image it was given. On arm64 Ed25519 signing
(sign_deterministic, sign_deterministic_out, both ph forms and the ph
final) left R and k's 64-byte digest next to each other, both public,
in the other order from the image, and failed with 96 bytes differing.
A run is now public when it is a sequence of pieces of the image, each
a word or longer but the last, at the same places in both runs.

Assisted-by: Claude:claude-opus-5-5
ECDSA, ECC CDH and DHKEM from_bytes built the key from
from_borrowed_limbs, whose scalar was moved into the Result: the
memory it was reduced into stayed in the decoding closure's frame,
unwiped. On arm64 that frame's top slot lies under the 8-byte padding
of the wipe's own frame (between its saved registers and its array),
which the wipe never writes, so check_ecdsa_stack.sh found one limb of
d 136 bytes below the call on P-256, P-384, secp256k1 and the
brainpool curves; on x86-64 the slot fell inside the array.

SecretScalar gains from_borrowed_limbs_into, which reduces into a
scalar the caller holds, and the decodings go through
curve::private_key_from_be_bytes: it reduces into a local that is wiped
when dropped and hands the key a copy, so the result does not depend on
where the compiler puts a moved-from temporary. Outputs are
byte-identical: decoding, re-encoding, deterministic signing and the
DHKEM decode over 3003 inputs per curve (2144 to 441 accepted) hash the
same before and after on all seven curves; the ec test suite passes.

Assisted-by: Claude:claude-opus-5-5
With the harness and decoding fixes, check_ecdsa_stack.sh and
check_sm2_stack.sh pass on arm64 Linux (rustc 1.100, and nightly 1.97).
The SM2 signing wipes, which said they were not measured on arm64, now
give the depth there with their body taken out: 1504, 3240 and 2904
bytes for create_signer, sign_out and the final (4096 wiped), 2760 and
3064 for from_bytes and keygen (5120 wiped). The Ed25519 docs no longer
predict a SHA-512 word after from_bytes_out on arm64: since the SHA-512
rewrite none is found there. The Ed448 docs and the release notes say
the same of arm64.

Assisted-by: Claude:claude-opus-5-5
…re tightened

Review points on the arm64 harness fixes:

- The trampoline is one file, dev_scripts/stack_scrubbed_call.rs,
  included by both harnesses through #[path]. Its assembly is built only
  for aarch64 and x86-64 Linux, so any other target reaches the
  compile_error! rather than an assembler error.
- check_ecdsa_stack.sh and check_sm2_stack.sh say that what the caller's
  own callee-saved registers held is out of scope, the harness zeroing
  them first. check_sm2_stack.sh also says the key wipe has room over
  arm64.
- SAVED_REGISTERS, re-measured with the registers zeroed and the
  allowance set to 0. ECDSA: the furthest difference is 208 bytes on
  arm64 with rustc 1.100 (Ed25519 sign staging the signature it returns,
  which is public), 136 with 1.97, and 128 on x86-64 with 1.97 and 1.88,
  so 512 becomes 256. SM2: nothing differs within reach on either target,
  so 256 becomes 64.
- In the ECDSA comparison, a final piece shorter than a word must carry
  on from where the previous piece ended in the image.

check_ecdsa_stack.sh and check_sm2_stack.sh pass on arm64 (rustc 1.100
and 1.97, under qemu-user) and x86-64 (1.97 and 1.88);
check_symmetric_stack.sh and check_rsa_stack.sh pass on x86-64.

Assisted-by: Claude:claude-opus-5-5
Review of 6ae975c: private_key_from_be_bytes passed key a clone by
value. That stayed out of the frame only because key was inlined. key
now takes &C::Scalar and makes the copy itself. If it is not inlined, d
sits in memory as the local that is wiped on drop, and the copy is
built in key's own frame, below this one, where the wipe reaches.
private_scalar_from_be_bytes, now used only by curve_tests.rs, delegates
to it, so the check and the load are written once. The ECDSA and ECC CDH
from_bytes docs describe what runs rather than pointing at that function.

The assembly of P-256's decoding (rustc 1.100) now reduces in registers
and stores d only into the Result through the return pointer, on both
aarch64 and x86-64; the local's only stores are the volatile
zeroization. The decoding differential (3003 inputs per curve, all
seven curves) is byte-identical, and the ec tests pass. Also:
from_borrowed_limbs_into's implementor rationale moves to a comment, and
a long line in sm2/mod.rs is rewrapped.

Assisted-by: Claude:claude-opus-5-5
@dghgit dghgit closed this Nov 27, 2026
@dghgit dghgit reopened this Nov 27, 2026
CI's x86-64 leg (rustc 1.100) failed Ed448 sign_deterministic: 23
bytes differing 352 bytes below the call. That depth is past the new 256
SAVED_REGISTERS and was inside the old 512. Dumping the bytes shows they
are the signature, which is public: bytes 23..39 then 0..7 of R || S in
both runs, staged in chunks for the by-value return. The 7-byte piece
does not continue where the 16-byte piece ended, so d97acac's rule
refused it. A piece now needs to be four bytes rather than eight. The
chance of two runs both matching four bytes at one place by accident is
2^-64. A shorter last piece must still continue the previous one.

With SAVED_REGISTERS at 0, rustc 1.100 on x86-64 also tops out at 128
bytes (brainpoolP512r1 derive_pk), so 256 stands, and its comment now
lists 1.100. check_ecdsa_stack.sh passes on x86-64 (1.100, 1.97, 1.88)
and arm64 (1.100, 1.97); check_sm2_stack.sh on x86-64 1.100 and 1.88.

Assisted-by: Claude:claude-opus-5-5
dghgit added a commit that referenced this pull request Nov 27, 2026
…eck, and the stack checks on arm64

check_ecdsa_stack.sh now covers Ed25519, Ed448, X25519 and X448:
keygen, decoding, encoding, clone, every signing form and agreement.
It found residue, fixed here: Ed25519 and Ed448 fixed-base
multiplication runs in a wiped frame, and keygen, decoding and clone
of all four go through in_wiped_frame. X25519's documented
scalar-multiplication residue stays reported as left.

The first arm64 CI run of the ECDSA and SM2 checks failed. Most rows
were the harness's own callee-saved registers, so both harnesses now
call each operation through a shared trampoline that zeroes them. One
was real: EC private-key decoding left a limb of d in an alignment gap
of the wipe's own frame. Decoding now reduces into a wiped local and
builds the key from a reference to it, which the assembly on both
targets shows keeps d off the stack. Outputs are byte-identical. Every
stack check passes on x86-64 and arm64 (draft PR #196).

Reviewed independently, twice; findings fixed.

Assisted-by: Claude:claude-opus-5-5

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant