opyeb asn1lab

asn1lab · Signatures

ECDSA DER signatures and fixed-width r‖s

SEQUENCE { r, s }, leading 0x00 padding, fixed-width serialisation, DER versus raw

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 20 02 20 ← typical 72-byte P-256 signature

Converting to fixed-width r‖s

  1. Pick the curve width: 32 bytes for P-256, 48 for P-384, 66 for P-521.
  2. Take r and s from the DER and strip any leading 0x00 padding.
  3. Left-pad each value to the curve width with zeros.
  4. Concatenate r then s. The result is 64, 96 or 132 bytes.
CurveCoordinate bytesr‖s lengthTypical OID
P-25632641.2.840.10045.3.1.7
P-38448961.3.132.0.34
P-521661321.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

Before verifying

  1. Check that the signature algorithm is an ECDSA OID such as ecdsa-with-SHA256.
  2. Check exactly which bytes were signed. JWS signs header.payload; a certificate signs tbsCertificate.
  3. Check that r and s are at least 1 and below the curve order.
  4. 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 tool

This 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.