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

Link copied to clipboard
fun derToP1363(bytes: ByteArray, fieldSizeBytes: Int): ByteArray

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

fun derToP1363(bytes: ByteArray, algorithm: Algorithm): ByteArray

Converts a DER-encoded ECDSA signature to P1363 (r || s), left-padding each component to the field size of algorithm.

Link copied to clipboard
fun fieldSizeBytes(algorithm: Algorithm): Int?

Returns the EC field element size in bytes for ECDSA-capable algorithms, or null for algorithms that are not ECDSA (Ed25519, RSA, BLS, custom).

Link copied to clipboard
fun isDer(bytes: ByteArray): Boolean

Returns true if bytes is a structurally valid DER-encoded ECDSA signature: a SEQUENCE containing exactly two positive INTEGERs, with no trailing bytes.

Link copied to clipboard
fun normalize(signature: ByteArray, algorithm: Algorithm): ByteArray

Normalizes an EC signature to the org.trustweave.kms.KeyManagementService.sign contract: P1363 encoding, with low-s for secp256k1.

Link copied to clipboard

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.

Link copied to clipboard
fun p1363SizeBytes(algorithm: Algorithm): Int?

Returns the expected P1363 (r || s) signature size in bytes for ECDSA-capable algorithms, or null for non-ECDSA algorithms.

Link copied to clipboard

Converts a P1363 (r || s) ECDSA signature to DER (SEQUENCE { INTEGER r, INTEGER s }).