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

Crypto.Cipher.ChaCha20

Description

A pure 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 cip = cipher key 1 non "but you can share the plaintext"
>>> cip
"\192*c\248A\204\211n\130y8\197\146k\245\178Y\197=\180_\223\138\146:^\206\&0\v[\201"
>>> cipher key 1 non cip
Right "but you can share the plaintext"

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

InvalidKey

the provided key was not 256 bits long

InvalidNonce

the provided nonce was none 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 #