Key Derivation & Signatures (crypto KDF & PKI)
The examples below are API fragments. Byte inputs such as password, salt, message, and private/public keys are borrowed Span<u8> values whose owners must remain live for the call. Checked operations return typed errors; a service should propagate or handle them at its application boundary.
Argon2id — Password Hashing
Argon2id is a memory-hard password derivation function. Its resistance to password guessing depends on the chosen memory/time costs and the deployment's resource budget; the algorithm name alone is not a password-storage policy.
import { Argon2id, Argon2Parameters } from "crypto";
// password and salt are Span<u8> values.
let parameters = Argon2Parameters.recommended();
let hash = match Argon2id.derive(password, salt, parameters, 32) {
Ok(value) => value,
Err(error) => { $panic(error); },
};recommended() currently chooses 64 MiB (memoryKiB = 65536), three passes, and four lanes, matching the memory-constrained option in RFC 9106 section 4. Benchmark costs on the authentication service and bound concurrent derivations. Store the salt, algorithm/version, cost parameters and output alongside the account record so verification can reproduce the same derivation and upgrade old parameters. Salts are not secrets; choose a fresh cryptographically random salt for each password record. The facade returns derived bytes, not a complete self-describing password-record format.
PBKDF2
Legacy-compatible password derivation. Use Argon2id for new applications.
import { Pbkdf2Sha256 } from "crypto";
let key = match Pbkdf2Sha256.derive(password, salt, iterations, 32) {
Ok(value) => value,
Err(error) => { $panic(error); },
};HKDF
HKDF extracts and expands existing keying material. info binds output to a protocol purpose, such as separate encryption and authentication keys. It does not add password-guessing resistance and must not replace a password KDF. See RFC 5869 section 3.
import { HkdfSha256 } from "crypto";
let derived = match HkdfSha256.derive(salt, inputKeyMaterial, info, 32) {
Ok(value) => value,
Err(error) => { $panic(error); },
};HkdfSha256 supports at most 255 * 32 output bytes per expansion; invalid lengths return a typed error. Choose the output length required by the consuming algorithm and use distinct, unambiguous info values for different purposes.
Ed25519 — Digital Signatures
Ed25519 signs messages with a 32-byte private seed. publicKey returns a 32-byte public key and sign returns a 64-byte signature. Keep the seed secret; a signature does not encrypt the message.
import { Ed25519 } from "crypto";
// privateSeed and message are Span<u8> values; the seed must be 32 bytes.
let publicKey = match Ed25519.publicKey(privateSeed) {
Ok(value) => value,
Err(error) => { $panic(error); },
};
let signature = match Ed25519.sign(privateSeed, message) {
Ok(value) => value,
Err(error) => { $panic(error); },
};Verification uses Ed25519.verify(publicKey, message, signature) with byte spans and returns Result<bool, CryptoError>. Treat only Ok(true) as acceptance: Ok(false) is an invalid signature, while Err reports a dimension or other API error. The implementation checks canonical encodings and rejects small-order points. Verification of a signature does not establish who owns the supplied public key; that binding belongs to the protocol.
X25519 — Key Exchange
Elliptic-curve Diffie-Hellman over Curve25519 for establishing shared secrets.
import { X25519 } from "crypto";
let sharedSecret = match X25519.sharedSecret(myPrivateKey, peerPublicKey) {
Ok(value) => value,
Err(error) => { $panic(error); },
};Both inputs and the shared-secret output are 32 bytes. X25519 alone does not authenticate the peer. Use the result within an authenticated key-exchange protocol and derive application keys with its prescribed KDF and context; do not treat the raw shared value as a complete secure channel. See RFC 7748 section 6.
Protocol owners that already have fixed storage should avoid a temporary Vec<u8> and use the exact caller-owned API:
let! publicKeyStorage = [0u8; 32];
let! publicOut = unsafe {
Span.of<u8>(&publicKeyStorage as Ptr<u8>, 32 as usize)
};
X25519.publicKeyTo(privateKey, &publicOut!)?;
let! sharedSecretStorage = [0u8; 32];
let! sharedOut = unsafe {
Span.of<u8>(&sharedSecretStorage as Ptr<u8>, 32 as usize)
};
X25519.sharedSecretTo(privateKey, peerPublicKey, &sharedOut!)?;Both destinations must be exactly 32 bytes and must not overlap either input. The shared-secret path rejects RFC 7748's all-zero, non-contributory result and clears the destination before returning InvalidPublicKey. Neither path allocates.
Curve performance architecture
X25519 and Ed25519 share the pure-Vex 5×51 field implementation. Multiplication uses Vex u128 values, allowing the backend to select native wide multiply instructions without a C/Rust FFI boundary. Squaring has its own symmetric schedule, small coefficients avoid sparse full-field multiplication, and the inversion chain reuses its field slot. On the 2026-08-18 Apple M2 Max O3 development run, X25519 public-key derivation measured 33.8–35.7 µs with zero allocations. Ed25519 uses a constant-flow four-bit window: every table entry is visited in public order and arithmetic limb masks perform the selection, so no secret digit becomes a memory index or branch. The same run measured 1 KiB signing at 159.6–162.3 µs and verification at 125.0–128.6 µs, both with zero allocation. These measurements are regression baselines, not API promises.