ECDSA DER signatures and fixed-width r‖s
An ECDSA signature is two integers, r and s. In files and protocols it is most often DER-encoded as SEQUENCE { r INTEGER, s INTEGER }. JOSE (JWS), WebCrypto and several language libraries instead want the raw form: r and s padded to the curve width and concatenated. Miss that difference and the signature lengths will not line up.
What a DER signature looks like
30 44 02 2002 20 ← typical 72-byte P-256 signature
- The outer 0x30 is SEQUENCE, followed by a two-byte length.
- Each INTEGER has its own 0x02 and length byte.
- If the top bit of r or s is set, a 0x00 is prepended so the value is not read as negative. A 32-byte value can therefore occupy 33 bytes.
- The total DER length moves between roughly 70 and 72 bytes. It is not fixed.
Converting to fixed-width r‖s
- Pick the curve width: 32 bytes for P-256, 48 for P-384, 66 for P-521.
- Take r and s from the DER and strip any leading 0x00 padding.
- Left-pad each value to the curve width with zeros.
- Concatenate r then s. The result is 64, 96 or 132 bytes.
| Curve | Coordinate bytes | r‖s length | Typical OID |
|---|---|---|---|
| P-256 | 32 | 64 | 1.2.840.10045.3.1.7 |
| P-384 | 48 | 96 | 1.3.132.0.34 |
| P-521 | 66 | 132 | 1.3.132.0.35 |
Going the other way, split the raw value into r and s, wrap each as an INTEGER with a 0x00 when the top bit is set, and wrap both in a SEQUENCE. The length changes, so never copy the result into a fixed-size buffer.
What usually goes wrong
- Treating a DER signature as raw and expecting 64 bytes while receiving 71.
- Forgetting to left-pad a short r, which shifts every following byte.
- Copying an s that was encoded as negative and failing verification.
- Comparing an RSA (PKCS#1 v1.5) signature with an ECDSA one at the same length.
- Using standard Base64 where base64url is expected.
Before verifying
- Check that the signature algorithm is an ECDSA OID such as ecdsa-with-SHA256.
- Check exactly which bytes were signed. JWS signs header.payload; a certificate signs tbsCertificate.
- Check that r and s are at least 1 and below the curve order.
- Check that DER has correct lengths and INTEGER padding, or that raw has the exact fixed length.
asn1lab shows r and s in decimal and hex and builds the r‖s value for the curve width you select. It does not verify the signature; use a separate verifier for that.
Questions
Why is one signature 70 bytes and another 72?
The sizes of r and s vary with their values, and a set top bit adds a 0x00 sign byte. DER uses the minimal encoding, so the length is not fixed.
Why are JWS signatures always the same length?
JWS requires fixed-width r‖s rather than DER. ES256 is 64 bytes, ES384 is 96 and ES512 is 132.
Can r and s be swapped?
Swapping them makes the signature fail. The order is fixed by the specification.
Open the ECDSA sample and watch r and s turn into the fixed-width value.
Open the toolThis guide follows ITU-T X.690 (DER/BER), RFC 5280 (X.509), RFC 5208 (PKCS#8), RFC 5480 (EC public keys), RFC 5915 (EC private keys) and RFC 3279 (ECDSA signatures). Every example is synthetic.