embassy-crypto-driver

Crates

git

Versions

default

Flavors

Skip to main content

X25519

Trait X25519 

Source
pub trait X25519 {
    // Required methods
    fn generate_keypair(
        rng: &mut dyn Rng,
    ) -> Result<(X25519PrivateKey, X25519PublicKey), CryptoError>;
    fn public_key(k: X25519PrivateKey) -> Result<X25519PublicKey, CryptoError>;
    fn ecdh_shared_secret(
        k: X25519PrivateKey,
        peer: X25519PublicKey,
    ) -> Result<[u8; 32], CryptoError>;
}
Expand description

X25519 (Curve25519) ECDH operations for TLS 1.3.

Each method corresponds to one operation a TLS 1.3 stack performs, so a hardware backend can implement it end-to-end where the peripheral natively supports the whole operation. Everything below this layer (point encoding, hashing, HKDF) stays in software in embassy-crypto, composing with Sha256, HmacSha256, Aes128Gcm and Aes128Cmac.

§Entropy

X25519::generate_keypair draws the ephemeral scalar from the caller-provided Rng; hardware with an internal entropy source may ignore it, but must document that. For deterministic known-answer tests, callers inject a deterministic Rng.

§Contract

  • All keys use the canonical RFC 7748 encodings (32 bytes, little-endian); other inputs return CryptoError::InvalidKey.
  • X25519 accepts all 32-byte u-coordinates as peer public keys, so no point validation is required before X25519::ecdh_shared_secret.
  • No secret-dependent timing w.r.t. private scalars.
  • Implementations wipe scalar copies they materialize in RAM.

Required Methods§

Source

fn generate_keypair( rng: &mut dyn Rng, ) -> Result<(X25519PrivateKey, X25519PublicKey), CryptoError>

Generate a fresh keypair: scalar k, Q = X25519(k, 9).

Used for TLS 1.3 ECDHE keygen.

Source

fn public_key(k: X25519PrivateKey) -> Result<X25519PublicKey, CryptoError>

Derive the public key from a known private scalar.

No RNG: used for persistent keys (e.g. a TLS static ECDH identity loaded from flash) where the caller already holds k.

Source

fn ecdh_shared_secret( k: X25519PrivateKey, peer: X25519PublicKey, ) -> Result<[u8; 32], CryptoError>

X25519 shared secret: X25519(k, peer), 32 bytes.

This is the TLS 1.3 ecdhe_shared_secret (fed into HKDF via HmacSha256).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§