HORIZON HASKELLDocslts/ghc-9.14.x3e2d5682026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 3e2d568 · 2026-10-11

Modulecrypton-2.1.7Haskell2010

Crypto.Cipher.ChaChaPoly1305

A simple AEAD scheme using ChaCha20 and Poly1305. See RFC 8439.

The State is not modified in place, so each function changing the State, returns a new State.

Authenticated Data need to be added before any call to encrypt or decrypt, and once all the data has been added, then finalizeAAD need to be called.

Once finalizeAAD has been called, no further appendAAD call should be make.

import Data.ByteString.Char8 as B
import Data.ByteArray
import Crypto.Error
import Crypto.Cipher.ChaChaPoly1305 as C

encrypt
    :: ByteString -- nonce (12 random bytes)
    -> ByteString -- symmetric key
    -> ByteString -- optional associated data (won't be encrypted)
    -> ByteString -- input plaintext to be encrypted
    -> CryptoFailable ByteString -- ciphertext with a 128-bit tag attached
encrypt nonce key header plaintext = do
    st1 <- C.initialize <$> C.key key <*> C.nonce12 nonce
    let
        st2 = C.finalizeAAD $ C.appendAAD header st1
        (out, st3) = C.encrypt plaintext st2
        auth = C.finalize st3
    return $ out `B.append` Data.ByteArray.convert auth
  • 5 types
  • 13 values
  • Packagecrypton-2.1.7
  • Exports18
  • LanguageHaskell2010
  • LicenceBSD-3-Clause AND MIT AND ISC
  • SourceChaChaPoly1305.hs

AEAD

2 declarations

Low level

16 declarations
datadata State
#

A ChaChaPoly1305 State.

The state is immutable, and only new state can be created

newtypenewtype Key
#

Initialize a new ChaChaPoly1305 State

The key length need to be 256 bits, and the nonce procured using either nonce8 or nonce12 | A ChaCha20Poly1305 key: thirty-two bytes, checked once here rather than at every use, so that initialize and initializeX cannot fail.

Instances3Eq, NFData, ByteArrayAccess
  • Eq KeyDefined in crypton-2.1.7 · Crypto.Cipher.ChaChaPoly1305
  • NFData KeyDefined in crypton-2.1.7 · Crypto.Cipher.ChaChaPoly1305
  • ByteArrayAccess KeyDefined in crypton-2.1.7 · Crypto.Cipher.ChaChaPoly1305
newtypenewtype XNonce
#

Extended nonce for XChaChaPoly1305.

Instances1ByteArrayAccess
valueencrypt :: ByteArray ba => ba -> State -> (ba, State)
#

Encrypt a piece of data and returns the encrypted Data and the updated State.

valuedecrypt :: ByteArray ba => ba -> State -> (ba, State)
#

Decrypt a piece of data and returns the decrypted Data and the updated State.