EcdsaSignatureCodec
Transcodes ECDSA signatures between ASN.1 DER (SEQUENCE { INTEGER r, INTEGER s }) and IEEE P1363 (raw fixed-width r || s) encodings, and normalizes secp256k1 signatures to low-s form.
Why this exists. Different KMS backends emit ECDSA signatures in different encodings: JCA providers, AWS KMS, Google Cloud KMS and Vault (marshaling_algorithm=asn1) return DER, while Azure Key Vault returns raw P1363. The TrustWeave org.trustweave.kms.KeyManagementService contract requires every provider to return P1363 for EC keys, so plugins use this codec to transcode before returning a signature.
Low-s normalization. ECDSA signatures are malleable: (r, s) and (r, n - s) are both valid. Ethereum (EIP-2) and Bitcoin reject high-s signatures, so secp256k1 signatures are normalized to s <= n/2. The transformed signature still verifies against the same key and message.
Caveat on detection. isDer performs a full structural parse, so the probability of a raw P1363 signature being misclassified as DER is negligible (it would have to start with a valid SEQUENCE header and contain two well-formed nested INTEGERs whose lengths sum exactly to the payload size), but it is not zero. Callers that know the encoding should transcode explicitly instead of relying on detection.
All operations are pure Kotlin + BigInteger; no crypto provider is required.
Functions
Converts a DER-encoded ECDSA signature to P1363 (r || s), left-padding each component to fieldSizeBytes bytes (32 for P-256/secp256k1, 48 for P-384, 66 for P-521).
Converts a DER-encoded ECDSA signature to P1363 (r || s), left-padding each component to the field size of algorithm.
Returns the EC field element size in bytes for ECDSA-capable algorithms, or null for algorithms that are not ECDSA (Ed25519, RSA, BLS, custom).
Normalizes a 64-byte P1363 secp256k1 signature to low-s form: if s > n/2, s is replaced with n - s. The normalized signature still verifies against the same key and message; Ethereum (EIP-2) and Bitcoin require this form.
Returns the expected P1363 (r || s) signature size in bytes for ECDSA-capable algorithms, or null for non-ECDSA algorithms.
Converts a P1363 (r || s) ECDSA signature to DER (SEQUENCE { INTEGER r, INTEGER s }).