Commit 6b2c52e
Enforce low-S canonicalization on ECDSA signatures to prevent malleability (#3)
* Enforce ECDSA low-S canonicalization to block signature malleability
JDK's SunEC ECDSA provider emits both low-S and high-S signatures, and
its verifier accepts either. That makes (r, s) and (r, n - s) both valid
for the same message, so any system that treats the signature bytes as
a stable identifier (revocation lists, credential tracking, dedup) can
be bypassed by swapping in the malleable twin.
This change:
- Normalizes every P-256 signature emitted by signEcdsaP256Raw and
SelectiveDisclosure.signEcdsaP256 to low-S (s <= n/2).
- Rejects high-S and out-of-range (r, s) values up front in
verifyEcdsaP256Raw before delegating to the JDK verifier.
- Adds regression tests covering the malleability attack (forged
high-S twin must be rejected), the low-S invariant on fresh
signatures, and malformed signature lengths.
https://claude.ai/code/session_011UnXmEtWXkFn8sz5bnNkUT
* Simplify low-S ECDSA normalization
Apply review feedback on top of the malleability fix:
- Drop the speculative BigInteger parameter from normalizeToLowS; it
was always called with P256_N and now uses the existing P256_HALF_N
constant directly instead of recomputing n >> 1 on each call.
- Extract the two JCA algorithm name strings into shared constants on
CredentialSigner, removed four duplicated string literals across
CredentialSigner and SelectiveDisclosure.
- Remove a stale WHAT-style comment and cut the low-S regression loop
from 50 to 20 iterations (2^-20 false-negative probability is already
well past the flaky-test threshold, and the JSON-LD canonicalization
per iteration was dominating test time).
https://claude.ai/code/session_011UnXmEtWXkFn8sz5bnNkUT
* Close the three review-gap holes in ECDSA malleability tests
Self-review flagged three blind spots in the original malleability
regression suite:
- No test exercised the r ∈ [1, n-1] bounds check on verify — only
the s-side high-S rejection was covered. Added
ecdsaVerificationShouldRejectOutOfRangeR to forge r == 0 and r == n
via byte-level tampering and confirm both are rejected.
- The DER fallback path in signEcdsaP256Raw/verifyEcdsaP256Raw is
effectively dead on modern JDKs, leaving derToP1363/p1363ToDer
untested. Added a direct round-trip test against a real JDK DER
signature plus a crafted high-bit-set input that exercises the
DER sign-extension branch of p1363ToDer.
- SelectiveDisclosure.signEcdsaP256 shares the low-S normalization
contract with CredentialSigner but had no test locking it in.
Added selectiveDisclosureBaseSignatureIsAlwaysLowS, which reads
the 64-byte base signature out of the fixed CBOR prefix emitted
by CborEncoder and asserts s <= n/2 across 20 iterations.
The new tests live in src/test/java/work/brodykim/signet/credential/
so they can reach the package-private helpers (derToP1363,
p1363ToDer, normalizeToLowS, P256_* constants) without widening
production visibility.
https://claude.ai/code/session_011UnXmEtWXkFn8sz5bnNkUT
* Simplify gap-coverage tests
Apply review feedback on the ECDSA test additions:
- Reuse the new verifyWithForgedSignature helper from the two earlier
forgery tests (malleable high-S variant, truncated signature),
dropping the proof-wrapping boilerplate that was duplicated three
times.
- Replace two hand-rolled fill loops in p1363ToDerHandlesHighBitSetOn
BothComponents with Arrays.fill.
- Narrow the derToP1363RoundTripsForRealJdkDerSignature throws clause
from Exception to the three specific checked exceptions the body
can throw (NoSuchAlgorithmException, InvalidKeyException,
SignatureException) plus JOSEException from key extraction.
- Extract CBOR_BASE_SIG_OFFSET and CBOR_BASE_SIG_END constants so the
intent of the bare `cbor[6..70]` slice is self-documenting.
https://claude.ai/code/session_011UnXmEtWXkFn8sz5bnNkUT
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent adf2e86 commit 6b2c52e
4 files changed
Lines changed: 392 additions & 12 deletions
File tree
- src
- main/java/work/brodykim/signet/credential
- test/java/work/brodykim/signet
- credential
Lines changed: 61 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
44 | 59 | | |
45 | 60 | | |
46 | 61 | | |
| |||
287 | 302 | | |
288 | 303 | | |
289 | 304 | | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
290 | 309 | | |
291 | 310 | | |
292 | 311 | | |
293 | 312 | | |
| 313 | + | |
294 | 314 | | |
295 | | - | |
| 315 | + | |
296 | 316 | | |
297 | 317 | | |
298 | | - | |
| 318 | + | |
299 | 319 | | |
300 | | - | |
301 | | - | |
| 320 | + | |
302 | 321 | | |
303 | 322 | | |
304 | 323 | | |
305 | | - | |
| 324 | + | |
306 | 325 | | |
| 326 | + | |
307 | 327 | | |
308 | 328 | | |
309 | 329 | | |
310 | 330 | | |
311 | 331 | | |
312 | 332 | | |
313 | 333 | | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
314 | 348 | | |
315 | 349 | | |
316 | 350 | | |
317 | | - | |
| 351 | + | |
318 | 352 | | |
319 | 353 | | |
320 | 354 | | |
321 | 355 | | |
322 | | - | |
323 | 356 | | |
324 | | - | |
| 357 | + | |
325 | 358 | | |
326 | 359 | | |
327 | 360 | | |
| |||
331 | 364 | | |
332 | 365 | | |
333 | 366 | | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
334 | 387 | | |
335 | 388 | | |
336 | 389 | | |
| |||
Lines changed: 6 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
235 | 235 | | |
236 | 236 | | |
237 | 237 | | |
| 238 | + | |
238 | 239 | | |
239 | | - | |
| 240 | + | |
240 | 241 | | |
241 | 242 | | |
242 | | - | |
| 243 | + | |
243 | 244 | | |
244 | | - | |
| 245 | + | |
245 | 246 | | |
246 | 247 | | |
247 | 248 | | |
248 | | - | |
| 249 | + | |
249 | 250 | | |
| 251 | + | |
250 | 252 | | |
251 | 253 | | |
252 | 254 | | |
| |||
Lines changed: 144 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| 13 | + | |
| 14 | + | |
12 | 15 | | |
13 | 16 | | |
14 | 17 | | |
| |||
177 | 180 | | |
178 | 181 | | |
179 | 182 | | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
180 | 324 | | |
181 | 325 | | |
182 | 326 | | |
| |||
0 commit comments