HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulelibsodium-bindings-0.0.1.1Haskell2010

LibSodium.Bindings.SecretStream

  • 1 type
  • 16 values

Introduction

0 declarations

This high-level API encrypts a sequence of messages, or a single message split into an arbitrary number of chunks, using a secret key, with the following properties:

  • Messages cannot be truncated, removed, reordered, duplicated or modified without this being detected by the decryption functions.

  • The same sequence encrypted twice will produce different ciphertexts.

  • An authentication tag is added to each encrypted message: stream corruption will be detected early, without having to read the stream until the end.

  • Each message can include additional data (ex: timestamp, protocol version) in the computation of the authentication tag.

  • Messages can have different sizes.

  • There are no practical limits to the total length of the stream, or to the total number of individual messages.

  • Ratcheting: at any point in the stream, it is possible to "forget" the secret key used to encrypt the previous messages, and switch to a new key.

Usage

2 declarations

An encrypted stream starts with a short header, whose size is cryptoSecretStreamXChaCha20Poly1305HeaderBytes bytes. That header must be sent/stored before the sequence of encrypted messages, as it is required to decrypt the stream. The header content doesn't have to be secret and decryption with a different header would fail.

A tag is attached to each message. That tag can be any of:

A typical encrypted stream simply attaches 0 as a tag to all messages, except the last one which is tagged as cryptoSecretStreamXChaCha20Poly1305TagFinal.

Note that tags are encrypted; encrypted streams do not reveal any information about sequence boundaries (cryptoSecretStreamXChaCha20Poly1305TagPush and cryptoSecretStreamXChaCha20Poly1305TagRekey tags).

For each message, additional data can be included in the computation of the authentication tag. With this API, additional data is rarely required, and most applications can just use nullPtr and a length of 0 instead.

Encryption

3 declarations
valuecryptoSecretStreamXChaCha20Poly1305InitPush
  1. :: Ptr CryptoSecretStreamXChaCha20Poly1305State

    Cryptographic state

  2. -> Ptr CUChar

    Header buffer, must be of size cryptoSecretStreamXChaCha20Poly1305HeaderBytes.

  3. -> Ptr CUChar

    Buffer holding the secret key. Must be of size cryptoSecretStreamXChaCha20Poly1305KeyBytes.

  4. -> IO CInt

    Returns 0 on success, -1 on error.

#

Initialise the cryptographic state using the secret key, then stores the stream header into the header buffer

See: crypto_secretstream_xchacha20poly1305_init_push()

valuecryptoSecretStreamXChaCha20Poly1305Push
  1. :: Ptr CryptoSecretStreamXChaCha20Poly1305State

    Cryptographic state

  2. -> Ptr CUChar

    Buffer that receives the cipher text.

  3. -> Ptr CULLong

    If this pointer is not nullPtr, it will store the length of the cipher text. It is guaranteed to be always (messageLength + cryptoSecretStreamXChaCha20Poly1305ABytes ).

  4. -> Ptr CUChar

    Pointer to the message to encrypt

  5. -> CULLong

    Length of the message (messageLength).

  6. -> Ptr CUChar

    Additional, optional data that can be included in the computation. Can be nullPtr if you have nothing to add.

  7. -> CULLong

    Length of the additional, optional data. Can be 0 if you have nothing to add.

  8. -> CUChar

    Tag for the cipher text.

  9. -> IO CInt

    Returns 0 on success, -1 on error.

#

Encrypt a message using a cryptographic state and a tag.

Additional data can be optionally provided. The maximum length of an individual message is cryptoSecretStreamXChaCha20Poly1305MessageBytesMax bytes (~ 256 GB).

See: crypto_secretstream_xchacha20poly1305_push()

Decryption

2 declarations
valuecryptoSecretStreamXChaCha20Poly1305InitPull
  1. :: Ptr CryptoSecretStreamXChaCha20Poly1305State

    Cryptographic state

  2. -> Ptr CUChar

    Header buffer, must be of size cryptoSecretStreamXChaCha20Poly1305HeaderBytes.

  3. -> Ptr CUChar

    Buffer holding the secret key. Must be of size cryptoSecretStreamXChaCha20Poly1305KeyBytes.

  4. -> IO CInt

    Returns 0 on success, -1 if the header is invalid.

#

Initialise the cryptographic state using the secret key and a header. The secret key will not be required any more for subsequent operations

See: crypto_secretstream_xchacha20poly1305_init_pull()

valuecryptoSecretStreamXChaCha20Poly1305Pull
  1. :: Ptr CryptoSecretStreamXChaCha20Poly1305State

    Cryptographic state.

  2. -> Ptr CUChar

    Buffer that will hold the decrypted message.

  3. -> Ptr CULLong

    If this pointer is not nullPtr, it will store the length of the message. It is guaranteed to be always (cipherTextLength - cryptoSecretStreamXChaCha20Poly1305ABytes ).

  4. -> Ptr CUChar

    If this pointer is not nullPtr, the tag attached to the message is stored in that buffer.

  5. -> Ptr CUChar

    Cipher text to be decrypted.

  6. -> CULLong

    Length in bytes of the cipher text.

  7. -> Ptr CUChar

    Additional, optional data that was bundled with the cipher text will be put there. Can be nullPtr if you know that nothing was added.

  8. -> CULLong

    Length of the additional, optional data. Can be 0 if you have nothing to add.

  9. -> IO CInt

    Return 0 on success, -1 if the ciphertext appears to be invalid.

#

Decrypt a message chunk.

Applications will typically call this function in a loop, until a message with the cryptoSecretStreamXChaCha20Poly1305TagFinal tag is found.

See: crypto_secretstream_xchacha20poly1305_pull()

Rekeying

1 declaration

Rekeying happens automatically and transparently, before the internal counter of the underlying cipher wraps. Therefore, streams can be arbitrary large.

Optionally, applications for which forward secrecy is critical can attach the cryptoSecretStreamXChaCha20Poly1305TagRekey tag to a message in order to trigger an explicit rekeying.

The decryption API will automatically update the secret key if this tag is found attached to a message. Explicit rekeying can also be performed without adding a tag, by calling this function.

This updates the state, but doesn't add any information about the secret key change to the stream. If this function is used to create an encrypted stream, the decryption process must call that function at the exact same stream location.

See: crypto_secretstream_xchacha20poly1305_rekey()

Constants

0 declarations

Key, Header and State size constants

Tag constants