Skip to Content
oxcrypto

ECDSA

Sign and verify with ECDSA over the NIST, secp256k1 and Brainpool curves.

from oxcrypto.hash.sha2 import SHA2
from oxcrypto.signature.ecdsa import ECDSA

key = ECDSA.P256.generate()
signature = key.sign(b"message", hash=SHA2.SHA256)
assert key.public_key().verify(signature, b"message", hash=SHA2.SHA256)

ECDSA has P224, P256, P384, P521, SECP256K1, BRAINPOOL_P256R1, BRAINPOOL_P384R1, BRAINPOOL_P256T1 and BRAINPOOL_P384T1.

Signing and verification

sign(data, *, hash, prehashed=False, encoding="der", deterministic=True) hashes data with a SHA-2 or SHA-3 selector from oxcrypto.hash. With prehashed=True, data must already be a digest of that hash's length. encoding is "der" (ASN.1 Ecdsa-Sig-Value) or "raw" (fixed-size r || s, as in IEEE P1363).

Signing derives the nonce with RFC 6979. With deterministic=False, as many bytes as the field size are drawn from the operating system CSPRNG and mixed into the RFC 6979 derivation as additional data (RFC 6979 section 3.6), so every signature is different and a nonce does not depend on randomness alone.

verify(signature, data, *, hash, prehashed=False, encoding="der") accepts both low-S and high-S signatures, except on secp256k1, where the upstream k256 crate produces and accepts only low-S signatures (BIP 146). ECDSA.<curve>.normalize_s(signature, *, encoding="der") returns the low-S form.

Public-key recovery

sign_recoverable returns (signature, recovery_id), and ECDSA.<curve>.recover_public_key(signature, data, *, recovery_id, hash, prehashed=False, encoding="der") returns the public key that produced the signature. recovery_id is 0 to 3. Recovery is used with secp256k1 (Ethereum and Bitcoin message signing) and works on every listed curve.

Keys

FormatPrivate keyPublic key
Rawto_bytes(), private_key_from_bytes (big-endian scalar)to_bytes(compressed=...), public_key_from_bytes (SEC1 point)
DERto_der(format="pkcs8" | "sec1", password=None), load_private_key_derto_der(compressed=...), load_public_key_der (SPKI)
PEMto_pem(...), load_private_key_pemto_pem(compressed=...), load_public_key_pem

With password, the private key is written as an encrypted PKCS#8 EncryptedPrivateKeyInfo using PBES2 with scrypt and AES-256-CBC, the pkcs8 crate's default. SEC1 private keys cannot be encrypted. A PEM private key is read according to its label: PRIVATE KEY, ENCRYPTED PRIVATE KEY or EC PRIVATE KEY. Public points are uncompressed unless compressed=True; both forms are accepted on input.

When loading an encrypted PKCS#8 key, oxcrypto checks the key-derivation parameters stored in the document before deriving any key, and raises ValueError if scrypt has N below 2, not a power of two or above 2**20, r * p >= 2**30 or needs more than 1 GiB (128 * r * (N + p) > 2**30), or if PBKDF2 has more than 10,000,000 iterations. The pkcs5 crate itself panics on N = 0 and accepts far larger costs, so a crafted key could otherwise abort or stall the process.

Upstream

CurveCrateStandardAudit
P-224, P-256, P-384, P-521p224 0.14.0-rc.15 (pre-release), p256/p384/p521 0.14.0FIPS 186-5, SEC 2No published audit
secp256k1k256 0.14.0SEC 2NCC Group review of an earlier release
brainpoolP256r1, brainpoolP384r1, brainpoolP256t1, brainpoolP384t1bp256/bp384 0.14.0RFC 5639No published audit

All curves use ecdsa 0.17.0, rfc6979 0.6.0 and elliptic-curve 0.14.3. These crates are written to perform secret-dependent operations in constant time; oxcrypto has not verified this independently. They build with Rust 1.88, the oxcrypto MSRV. p224 is a release candidate: its API may change before 0.14.0.

Tests use the C2SP Wycheproof ECDSA vectors (DER for every curve, P1363 for P-256, the Bitcoin low-S file for secp256k1), NIST CAVP FIPS 186-4 SigVer.rsp for P-224 to P-521, and the RFC 6979 appendix A.2.5 P-256 vectors. Wycheproof and CAVP have no brainpool t1 vectors, so those curves are tested against signatures and keys generated with OpenSSL 3.0.2.

See Signatures for zeroization notes.

On this page