libsodium_crypto_onetimeauth (libsodium v3.0.1)

View Source

One-time message authentication with the default construction, currently Poly1305.

A keybytes/0 byte key must be used to authenticate a single message only; reusing it lets an attacker forge tags. Derive a fresh key per message (for example from a stream cipher), or use libsodium_crypto_auth for a reusable key.

The streaming functions thread an opaque state/0 through init/1, update/2, and final/1; each call returns a new state and leaves its argument unchanged. Updates over large inputs yield to other processes between chunks.

Key = libsodium_crypto_onetimeauth:keygen(),
Tag = libsodium_crypto_onetimeauth:crypto_onetimeauth(<<"message">>, Key),
0 = libsodium_crypto_onetimeauth:verify(Tag, <<"message">>, Key),
-1 = libsodium_crypto_onetimeauth:verify(Tag, <<"forged">>, Key),
State0 = libsodium_crypto_onetimeauth:init(Key),
State1 = libsodium_crypto_onetimeauth:update(State0, <<"mess">>),
State2 = libsodium_crypto_onetimeauth:update(State1, <<"age">>),
Tag = libsodium_crypto_onetimeauth:final(State2).

Summary

Types

h()

A crypto_onetimeauth_BYTES (16-byte) binary.

k()

A crypto_onetimeauth_KEYBYTES (32-byte) binary.

A crypto_onetimeauth_KEYBYTES (32-byte) binary.

A crypto_onetimeauth_BYTES (16-byte) binary.

An opaque native crypto_onetimeauth_state.

Functions

Return crypto_onetimeauth_bytes(), in bytes.

Compute the bytes/0 byte tag of In under the one-time keybytes/0 byte key K.

Finish the streaming authenticator in State, returning a bytes/0 byte tag.

Start a streaming authenticator with the one-time keybytes/0 byte Key.

Return crypto_onetimeauth_keybytes(), in bytes.

Generate a random keybytes/0 byte key.

Return crypto_onetimeauth_primitive() as an atom.

Return crypto_onetimeauth_statebytes(), in bytes.

Absorb In into State and return the updated state.

Check in constant time that H is the tag of In under the key K. Returns 0 when it is valid and -1 otherwise.

Types

h()

-type h() :: <<_:128>>.

A crypto_onetimeauth_BYTES (16-byte) binary.

k()

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

A crypto_onetimeauth_KEYBYTES (32-byte) binary.

key()

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

A crypto_onetimeauth_KEYBYTES (32-byte) binary.

out()

-type out() :: <<_:128>>.

A crypto_onetimeauth_BYTES (16-byte) binary.

state()

An opaque native crypto_onetimeauth_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 :: 16.

Return crypto_onetimeauth_bytes(), in bytes.

crypto_onetimeauth(In, K)

-spec crypto_onetimeauth(In, K) -> Result when In :: binary(), K :: k(), Result :: out() | -1.

Compute the bytes/0 byte tag of In under the one-time keybytes/0 byte key K.

final(State)

-spec final(State) -> Result when State :: state(), Result :: out() | -1.

Finish the streaming authenticator in State, returning a bytes/0 byte tag.

init(Key)

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

Start a streaming authenticator with the one-time keybytes/0 byte Key.

keybytes()

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

Return crypto_onetimeauth_keybytes(), in bytes.

keygen()

-spec keygen() -> Result when Result :: k().

Generate a random keybytes/0 byte key.

primitive()

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

Return crypto_onetimeauth_primitive() as an atom.

statebytes()

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

Return crypto_onetimeauth_statebytes(), in bytes.

update(State, In)

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

Absorb In into State and return the updated state.

verify(H, In, K)

-spec verify(H, In, K) -> Result when H :: h(), In :: binary(), K :: k(), Result :: 0 | -1.

Check in constant time that H is the tag of In under the key K. Returns 0 when it is valid and -1 otherwise.