kryptos/ecdsa
Elliptic Curve Digital Signature Algorithm (ECDSA).
ECDSA provides digital signatures using elliptic curve cryptography, offering strong security with smaller key sizes compared to RSA.
Example
import kryptos/ec
import kryptos/ecdsa
import kryptos/hash
let #(private_key, public_key) = ec.generate_key_pair(ec.P256)
let message = <<"hello world":utf8>>
let signature = ecdsa.sign(private_key, message, hash.Sha256)
let valid = ecdsa.verify(public_key, message, signature, hash.Sha256)
// valid == True
Values
pub fn der_to_rs(
der_sig: BitArray,
curve: ec.Curve,
) -> Result(BitArray, Nil)
Converts a DER-encoded ECDSA signature to R||S format.
R||S format concatenates the r and s integer values, each padded to the curve’s coordinate size with leading zeros.
Parameters
der: A DER-encoded ECDSA signaturecurve: The elliptic curve used for the signature
Returns
Ok(rs_signature) on success, Error(Nil) if the DER is malformed
or contains trailing garbage.
Example
import kryptos/ec
import kryptos/ecdsa
import kryptos/hash
let #(private_key, _public_key) = ec.generate_key_pair(ec.P256)
let der_sig = ecdsa.sign(private_key, <<"hello":utf8>>, hash.Sha256)
let assert Ok(rs_sig) = ecdsa.der_to_rs(der_sig, ec.P256)
pub fn rs_to_der(
rs: BitArray,
curve: ec.Curve,
) -> Result(BitArray, Nil)
Converts an R||S format signature to DER encoding.
Parameters
rs: An R||S format signature (2 * coordinate_size bytes)curve: The elliptic curve used for the signature
Returns
Ok(der_signature) on success, Error(Nil) if input length is invalid.
Example
import kryptos/ec
import kryptos/ecdsa
import kryptos/hash
let #(private_key, _public_key) = ec.generate_key_pair(ec.P256)
let rs_sig = ecdsa.sign_rs(private_key, <<"hello":utf8>>, hash.Sha256)
let assert Ok(der_sig) = ecdsa.rs_to_der(rs_sig, ec.P256)
pub fn sign(
private_key: ec.PrivateKey,
message: BitArray,
hash: hash.HashAlgorithm,
) -> BitArray
Signs a message using ECDSA with the specified hash algorithm.
The message is hashed internally using the provided algorithm before signing. Signatures may be non-deterministic depending on platform (Erlang uses random nonces, some platforms may use deterministic RFC 6979 nonces).
Parameters
private_key: An elliptic curve private key fromec.generate_key_pairmessage: The message to sign (any length)hash: The hash algorithm to use (e.g.,Sha256,Sha384,Sha512)
Returns
A DER-encoded ECDSA signature.
pub fn sign_rs(
private_key: ec.PrivateKey,
message: BitArray,
hash: hash.HashAlgorithm,
) -> BitArray
Signs a message and returns the signature in R||S format (IEEE P1363).
In R||S format, the signature is the concatenation of r and s values, each padded to the curve’s coordinate size.
Parameters
private_key: An elliptic curve private keymessage: The message to signhash: The hash algorithm to use
Returns
An R||S format signature (2 * coordinate_size bytes).
Example
import kryptos/ec
import kryptos/ecdsa
import kryptos/hash
let #(private_key, _public_key) = ec.generate_key_pair(ec.P256)
let signature = ecdsa.sign_rs(private_key, <<"hello":utf8>>, hash.Sha256)
pub fn verify(
public_key: ec.PublicKey,
message: BitArray,
signature signature: BitArray,
hash hash: hash.HashAlgorithm,
) -> Bool
Verifies an ECDSA signature against a message.
The message is hashed internally using the provided algorithm before verification. The same hash algorithm used during signing must be used for verification.
Parameters
public_key: The elliptic curve public key corresponding to the signing keymessage: The original message that was signedsignature: The DER-encoded signature to verifyhash: The hash algorithm used during signing
Returns
True if the signature is valid, False otherwise.
pub fn verify_rs(
public_key: ec.PublicKey,
message: BitArray,
signature: BitArray,
hash: hash.HashAlgorithm,
) -> Bool
Verifies an R||S format signature against a message.
The R||S format is the concatenation of r and s values, each padded to the curve’s coordinate size.
Parameters
public_key: The public key corresponding to the signing keymessage: The original message that was signedsignature: The R||S format signature to verifyhash: The hash algorithm used during signing
Returns
True if the signature is valid, False otherwise.
Example
import kryptos/ec
import kryptos/ecdsa
import kryptos/hash
let #(private_key, public_key) = ec.generate_key_pair(ec.P256)
let message = <<"hello":utf8>>
let signature = ecdsa.sign_rs(private_key, message, hash.Sha256)
let valid = ecdsa.verify_rs(public_key, message, signature, hash.Sha256)
// valid == True