ppad-chacha-0.3.0: A fast ChaCha20 stream cipher
Copyright(c) 2025 Jared Tobin
LicenseMIT
MaintainerJared Tobin <jared@ppad.tech>
Safe HaskellNone
LanguageHaskell2010

Crypto.Cipher.ChaCha20

Description

A fast ChaCha20 implementation, as specified by RFC 8439.

Synopsis

ChaCha20 stream cipher

cipher Source #

Arguments

:: ByteString

256-bit key

-> Word32

32-bit counter

-> ByteString

96-bit nonce

-> ByteString

arbitrary-length plaintext

-> Either Error ByteString

ciphertext

The ChaCha20 stream cipher. Generates a keystream and then XOR's the supplied input with it; use it both to encrypt plaintext and decrypt ciphertext.

Per RFC8439, the key must be exactly 256 bits, and the nonce exactly 96 bits.

>>> let key = "don't tell anyone my secret key!"
>>> let non = "or my nonce!"
>>> let Right cip = cipher key 1 non "but you can share the ciphertext"
>>> cip
"\192*c\248A\204\211n\130y8\197\146k\245\178Y\197=\180_\223\153\151+_\197\&6\SUBF\197\160"
>>> cipher key 1 non cip
Right "but you can share the ciphertext"

ChaCha20 block function

block Source #

Arguments

:: ByteString

256-bit key

-> Word32

32-bit counter

-> ByteString

96-bit nonce

-> Either Error ByteString

512-bit keystream

The ChaCha20 block function. Useful for generating a keystream.

Per RFC8439, the key must be exactly 256 bits, and the nonce exactly 96 bits.

Error information

data Error Source #

Error values.

Constructors

CounterOverflow

the counter overflowed

InvalidKey

the provided key was not 256 bits long

InvalidNonce

the provided nonce was not 96 bits long

Instances

Instances details
Show Error Source # 
Instance details

Defined in Crypto.Cipher.ChaCha20

Methods

showsPrec :: Int -> Error -> ShowS #

show :: Error -> String #

showList :: [Error] -> ShowS #

Eq Error Source # 
Instance details

Defined in Crypto.Cipher.ChaCha20

Methods

(==) :: Error -> Error -> Bool #

(/=) :: Error -> Error -> Bool #