libsodium_crypto_sign (libsodium v3.0.1)
View SourcePublic-key signatures with Ed25519, the default crypto_sign primitive.
A key pair is a publickeybytes/0 byte public key and a
secretkeybytes/0 byte secret key (the seedbytes/0 byte seed followed by
the public key). Signatures are bytes/0 bytes and are deterministic: the
same message and key always produce the same signature.
crypto_sign/2 and open/2 work with the signature prepended to the
message, while detached/2 and verify_detached/3 keep them apart. The
streaming functions (init/0, update/2, final_create/2,
final_verify/3) compute Ed25519ph (pre-hashed with SHA-512) signatures,
which are not interchangeable with the one-shot signatures; the state is an
opaque state/0.
{PK, SK} = libsodium_crypto_sign:keypair(),
SM = libsodium_crypto_sign:crypto_sign(<<"message">>, SK),
<<"message">> = libsodium_crypto_sign:open(SM, PK),
Sig = libsodium_crypto_sign:detached(<<"message">>, SK),
0 = libsodium_crypto_sign:verify_detached(Sig, <<"message">>, PK).
Summary
Types
A crypto_sign_PUBLICKEYBYTES (32-byte) binary.
A crypto_sign_SEEDBYTES (32-byte) binary.
A crypto_sign_BYTES (64-byte) binary.
A crypto_sign_SECRETKEYBYTES (64-byte) binary.
An opaque native crypto_sign_state.
Functions
Return crypto_sign_bytes(), in bytes.
Sign M with the secret key SK, returning the bytes/0 byte
signature followed by the message.
Sign M with the secret key SK, returning only the bytes/0 byte signature.
Finish the streaming message in State and sign it with the secret key
SK, returning the bytes/0 byte Ed25519ph signature.
Finish the streaming message in State and verify its Ed25519ph
signature Sig with the public key PK. Returns 0 when the signature
is valid and -1 otherwise.
Start a streaming Ed25519ph signature, returning a new state.
Generate a random key pair. Returns {PK, SK}.
Return crypto_sign_messagebytes_max(), in bytes.
Verify the signed message SM with the public key PK and return the
message without its signature, or -1 when the signature is invalid.
Return crypto_sign_primitive() as an atom.
Return crypto_sign_publickeybytes(), in bytes.
Return crypto_sign_secretkeybytes(), in bytes.
Deterministically derive a key pair from the seedbytes/0 byte Seed.
Returns {PK, SK}.
Return crypto_sign_seedbytes(), in bytes.
Return crypto_sign_statebytes(), in bytes.
Absorb the message chunk M into State and return the updated state.
Verify the detached signature Sig of M with the public key PK.
Returns 0 when the signature is valid and -1 otherwise.
Types
-type pk() :: <<_:256>>.
A crypto_sign_PUBLICKEYBYTES (32-byte) binary.
-type seed() :: <<_:256>>.
A crypto_sign_SEEDBYTES (32-byte) binary.
-type sig() :: <<_:512>>.
A crypto_sign_BYTES (64-byte) binary.
-type sk() :: <<_:512>>.
A crypto_sign_SECRETKEYBYTES (64-byte) binary.
-type state() :: libsodium_nif:crypto_sign_state().
An opaque native crypto_sign_state.
Operations never modify a state: each returns a new one, so earlier states remain valid and can be reused or shared between processes. States cannot be serialized; they exist only within the running VM.
Functions
-spec bytes() -> Result when Result :: 64.
Return crypto_sign_bytes(), in bytes.
Sign M with the secret key SK, returning the bytes/0 byte
signature followed by the message.
Sign M with the secret key SK, returning only the bytes/0 byte signature.
Finish the streaming message in State and sign it with the secret key
SK, returning the bytes/0 byte Ed25519ph signature.
-spec final_verify(State, Sig, PK) -> Result when State :: state(), Sig :: sig(), PK :: pk(), Result :: 0 | -1.
Finish the streaming message in State and verify its Ed25519ph
signature Sig with the public key PK. Returns 0 when the signature
is valid and -1 otherwise.
-spec init() -> Result when Result :: state() | -1.
Start a streaming Ed25519ph signature, returning a new state.
Generate a random key pair. Returns {PK, SK}.
-spec messagebytes_max() -> Result when Result :: non_neg_integer().
Return crypto_sign_messagebytes_max(), in bytes.
Verify the signed message SM with the public key PK and return the
message without its signature, or -1 when the signature is invalid.
-spec primitive() -> Result when Result :: ed25519.
Return crypto_sign_primitive() as an atom.
-spec publickeybytes() -> Result when Result :: 32.
Return crypto_sign_publickeybytes(), in bytes.
-spec secretkeybytes() -> Result when Result :: 64.
Return crypto_sign_secretkeybytes(), in bytes.
Deterministically derive a key pair from the seedbytes/0 byte Seed.
Returns {PK, SK}.
-spec seedbytes() -> Result when Result :: 32.
Return crypto_sign_seedbytes(), in bytes.
-spec statebytes() -> Result when Result :: 208.
Return crypto_sign_statebytes(), in bytes.
Absorb the message chunk M into State and return the updated state.
-spec verify_detached(Sig, M, PK) -> Result when Sig :: sig(), M :: binary(), PK :: pk(), Result :: 0 | -1.
Verify the detached signature Sig of M with the public key PK.
Returns 0 when the signature is valid and -1 otherwise.