| Copyright | (c) 2026 Jared Tobin |
|---|---|
| License | MIT |
| Maintainer | Jared Tobin <jared@ppad.tech> |
| Safe Haskell | None |
| Language | Haskell2010 |
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
- data Rounds
- keystream :: Rounds -> ByteString -> Word32 -> ByteString -> Int -> Either Error ByteString
- data Error
- data FeedForward
- _keystream_into :: Rounds -> Ptr Word8 -> Word32 -> Ptr Word8 -> Ptr Word8 -> Int -> IO ()
- _interleaved_into :: Rounds -> FeedForward -> Ptr Word8 -> Word32 -> Ptr Word8 -> Ptr Word8 -> Int -> IO ()
- _interleaved_pure_into :: Rounds -> FeedForward -> Ptr Word8 -> Word32 -> Ptr Word8 -> Ptr Word8 -> Int -> IO ()
Round counts
The number of ChaCha rounds.
Keystream
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
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 |
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
| Show FeedForward Source # | |
Defined in Crypto.Cipher.ChaCha Methods showsPrec :: Int -> FeedForward -> ShowS # show :: FeedForward -> String # showList :: [FeedForward] -> ShowS # | |
| Eq FeedForward Source # | |
Defined in Crypto.Cipher.ChaCha | |
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.
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.