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

Crypto.Cipher.ChaCha

Description

ChaCha keystreams with 8, 12, or 20 rounds, using the state layout of RFC 8439.

The reduced-round variants are intended for uses such as random number generation; use Crypto.Cipher.ChaCha20 for encryption.

Synopsis

Round counts

data Rounds Source #

The number of ChaCha rounds.

Constructors

ChaCha8

8 rounds

ChaCha12

12 rounds

ChaCha20

20 rounds, as specified by RFC 8439

Instances

Instances details
Show Rounds Source # 
Instance details

Defined in Crypto.Cipher.ChaCha

Eq Rounds Source # 
Instance details

Defined in Crypto.Cipher.ChaCha

Methods

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

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

Keystream

keystream Source #

Arguments

:: Rounds

number of rounds

-> ByteString

256-bit key

-> Word32

32-bit counter

-> ByteString

96-bit nonce

-> Int

number of bytes

-> Either Error ByteString

keystream

The ChaCha keystream with the given number of rounds, starting at the given block counter.

The key must be exactly 256 bits, and the nonce exactly 96 bits. The counter must leave room for the requested number of bytes; non-positive numbers of bytes yield the empty string.

>>> import qualified Data.ByteString as BS
>>> import qualified Data.ByteString.Base16 as B16
>>> let key = BS.replicate 32 0
>>> let non = BS.replicate 12 0
>>> fmap B16.encode (keystream ChaCha8 key 0 non 16)
Right "3e00ef2f895f40d67f5bb8e81f09a5a1"

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 #

data FeedForward Source #

Which words of the input state are added to a block's output.

Constructors

FeedForwardAll

all words, as standard

FeedForwardKey

the key words only, omitting the public constant, counter, and nonce words, as in ChaCha8Rand

Instances

Instances details
Show FeedForward Source # 
Instance details

Defined in Crypto.Cipher.ChaCha

Eq FeedForward Source # 
Instance details

Defined in Crypto.Cipher.ChaCha

_keystream_into Source #

Arguments

:: Rounds

number of rounds

-> Ptr Word8

256-bit key

-> Word32

32-bit counter

-> Ptr Word8

96-bit nonce

-> Ptr Word8

destination

-> Int

number of bytes

-> IO () 

Write keystream bytes to the destination pointer, for the key and nonce at the given pointers.

Unchecked: the key and nonce pointers must address 32 and 12 bytes respectively, the destination must have room for the given number of bytes and must not overlap the key or nonce, and the counter must leave room for the bytes.

_interleaved_into Source #

Arguments

:: Rounds

number of rounds

-> FeedForward

final addition of the input state

-> Ptr Word8

256-bit key

-> Word32

32-bit counter

-> Ptr Word8

96-bit nonce

-> Ptr Word8

destination

-> Int

number of four-block groups

-> IO () 

Write groups of four consecutive keystream blocks to the destination pointer, interleaved word by word: within each 256-byte group, word i of block j is at byte offset 16 * i + 4 * j. This is the layout that four-way SIMD produces naturally.

Unchecked: the key and nonce pointers must address 32 and 12 bytes respectively, the destination must have room for 256 bytes per group and must not overlap the key or nonce, and the counter must leave room for four blocks per group.

_interleaved_pure_into :: Rounds -> FeedForward -> Ptr Word8 -> Word32 -> Ptr Word8 -> Ptr Word8 -> Int -> IO () Source #

A pure Haskell _interleaved_into, with the same preconditions.