libsodium_crypto_sign (libsodium v3.0.1)

View Source

Public-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

pk()

-type pk() :: <<_:256>>.

A crypto_sign_PUBLICKEYBYTES (32-byte) binary.

seed()

-type seed() :: <<_:256>>.

A crypto_sign_SEEDBYTES (32-byte) binary.

sig()

-type sig() :: <<_:512>>.

A crypto_sign_BYTES (64-byte) binary.

sk()

-type sk() :: <<_:512>>.

A crypto_sign_SECRETKEYBYTES (64-byte) binary.

state()

-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

bytes()

-spec bytes() -> Result when Result :: 64.

Return crypto_sign_bytes(), in bytes.

crypto_sign(M, SK)

-spec crypto_sign(M, SK) -> Result when M :: binary(), SK :: sk(), Result :: binary() | -1.

Sign M with the secret key SK, returning the bytes/0 byte signature followed by the message.

detached(M, SK)

-spec detached(M, SK) -> Result when M :: binary(), SK :: sk(), Result :: sig() | -1.

Sign M with the secret key SK, returning only the bytes/0 byte signature.

final_create(State, SK)

-spec final_create(State, SK) -> Result when State :: state(), SK :: sk(), Result :: sig() | -1.

Finish the streaming message in State and sign it with the secret key SK, returning the bytes/0 byte Ed25519ph signature.

final_verify(State, Sig, PK)

-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.

init()

-spec init() -> Result when Result :: state() | -1.

Start a streaming Ed25519ph signature, returning a new state.

keypair()

-spec keypair() -> Result when Result :: {pk(), sk()} | -1.

Generate a random key pair. Returns {PK, SK}.

messagebytes_max()

-spec messagebytes_max() -> Result when Result :: non_neg_integer().

Return crypto_sign_messagebytes_max(), in bytes.

open(SM, PK)

-spec open(SM, PK) -> Result when SM :: binary(), PK :: pk(), Result :: binary() | -1.

Verify the signed message SM with the public key PK and return the message without its signature, or -1 when the signature is invalid.

primitive()

-spec primitive() -> Result when Result :: ed25519.

Return crypto_sign_primitive() as an atom.

publickeybytes()

-spec publickeybytes() -> Result when Result :: 32.

Return crypto_sign_publickeybytes(), in bytes.

secretkeybytes()

-spec secretkeybytes() -> Result when Result :: 64.

Return crypto_sign_secretkeybytes(), in bytes.

seed_keypair(Seed)

-spec seed_keypair(Seed) -> Result when Seed :: seed(), Result :: {pk(), sk()} | -1.

Deterministically derive a key pair from the seedbytes/0 byte Seed. Returns {PK, SK}.

seedbytes()

-spec seedbytes() -> Result when Result :: 32.

Return crypto_sign_seedbytes(), in bytes.

statebytes()

-spec statebytes() -> Result when Result :: 208.

Return crypto_sign_statebytes(), in bytes.

update(State, M)

-spec update(State, M) -> Result when State :: state(), M :: binary(), Result :: state() | -1.

Absorb the message chunk M into State and return the updated state.

verify_detached(Sig, M, PK)

-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.