libsodium_crypto_kem (libsodium v3.0.1)
View SourcePost-quantum key encapsulation. This is the recommended default KEM; it
currently uses X-Wing (libsodium_crypto_kem_xwing), a hybrid of
ML-KEM-768 and X25519 that stays secure if either component is broken.
A recipient publishes a publickeybytes/0 byte public key PK. A sender
calls enc/1 to obtain a ciphertextbytes/0 byte ciphertext CT and a
sharedsecretbytes/0 byte shared secret SS, and sends CT to the
recipient, who recovers the same SS with dec/2 and their secret key.
Seeds passed to seed_keypair/1 must be secret and uniformly random.
{PK, SK} = libsodium_crypto_kem:keypair(),
{CT, SS} = libsodium_crypto_kem:enc(PK),
SS = libsodium_crypto_kem:dec(CT, SK).
Summary
Types
A crypto_kem_CIPHERTEXTBYTES (1120-byte) binary.
A crypto_kem_PUBLICKEYBYTES (1216-byte) binary.
A crypto_kem_SEEDBYTES (32-byte) binary.
A crypto_kem_SECRETKEYBYTES (32-byte) binary.
A crypto_kem_SHAREDSECRETBYTES (32-byte) binary.
Functions
Return crypto_kem_ciphertextbytes(), in bytes.
Decapsulate the ciphertext CT with the secret key SK and return the
shared secret SS.
Encapsulate a fresh random shared secret to the public key PK.
Returns {CT, SS}, where CT is sent to the holder of the secret
key, or -1 when PK is invalid.
Generate a random key pair. Returns {PK, SK}.
Return crypto_kem_primitive() as an atom.
Return crypto_kem_publickeybytes(), in bytes.
Return crypto_kem_secretkeybytes(), in bytes.
Deterministically derive a key pair from the seedbytes/0 byte Seed,
which must be secret and uniformly random. Returns {PK, SK}.
Return crypto_kem_seedbytes(), in bytes.
Return crypto_kem_sharedsecretbytes(), in bytes.
Types
-type ct() :: <<_:8960>>.
A crypto_kem_CIPHERTEXTBYTES (1120-byte) binary.
-type pk() :: <<_:9728>>.
A crypto_kem_PUBLICKEYBYTES (1216-byte) binary.
-type seed() :: <<_:256>>.
A crypto_kem_SEEDBYTES (32-byte) binary.
-type sk() :: <<_:256>>.
A crypto_kem_SECRETKEYBYTES (32-byte) binary.
-type ss() :: <<_:256>>.
A crypto_kem_SHAREDSECRETBYTES (32-byte) binary.
Functions
-spec ciphertextbytes() -> Result when Result :: 1120.
Return crypto_kem_ciphertextbytes(), in bytes.
Decapsulate the ciphertext CT with the secret key SK and return the
shared secret SS.
A tampered or mismatched CT does not fail: implicit rejection returns
a pseudorandom secret unrelated to the sender's, so the mismatch only
surfaces when the secrets are used. Returns -1 only when the X25519
part of CT is a low-order point.
Encapsulate a fresh random shared secret to the public key PK.
Returns {CT, SS}, where CT is sent to the holder of the secret
key, or -1 when PK is invalid.
Generate a random key pair. Returns {PK, SK}.
-spec primitive() -> Result when Result :: xwing.
Return crypto_kem_primitive() as an atom.
-spec publickeybytes() -> Result when Result :: 1216.
Return crypto_kem_publickeybytes(), in bytes.
-spec secretkeybytes() -> Result when Result :: 32.
Return crypto_kem_secretkeybytes(), in bytes.
Deterministically derive a key pair from the seedbytes/0 byte Seed,
which must be secret and uniformly random. Returns {PK, SK}.
-spec seedbytes() -> Result when Result :: 32.
Return crypto_kem_seedbytes(), in bytes.