| License | BSD-style |
|---|---|
| Maintainer | Vincent Hanquez <vincent@snarc.org> |
| Stability | stable |
| Portability | good |
| Safe Haskell | None |
| Language | Haskell2010 |
Crypto.Cipher.ChaChaPoly1305
Description
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 authSynopsis
- type ChaCha20Poly1305 = State
- aeadChacha20poly1305Init :: (ByteArrayAccess k, ByteArrayAccess n) => k -> n -> CryptoFailable (AEAD ChaCha20Poly1305)
- data State
- data Key
- key :: ByteArrayAccess ba => ba -> CryptoFailable Key
- data Nonce
- data XNonce
- nonce12 :: ByteArrayAccess iv => iv -> CryptoFailable Nonce
- nonce8 :: ByteArrayAccess ba => ba -> ba -> CryptoFailable Nonce
- nonce24 :: ByteArrayAccess ba => ba -> CryptoFailable XNonce
- incrementNonce :: Nonce -> Nonce
- initialize :: Key -> Nonce -> State
- initializeX :: Key -> XNonce -> State
- appendAAD :: ByteArrayAccess ba => ba -> State -> State
- finalizeAAD :: State -> State
- encrypt :: ByteArray ba => ba -> State -> (ba, State)
- decrypt :: ByteArray ba => ba -> State -> (ba, State)
- finalize :: State -> Auth
AEAD
type ChaCha20Poly1305 = State Source #
A ChaChaPoly1305 State.
aeadChacha20poly1305Init :: (ByteArrayAccess k, ByteArrayAccess n) => k -> n -> CryptoFailable (AEAD ChaCha20Poly1305) Source #
Setting up AEAD for ChaCha20Poly1305.
Low level
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.
key :: ByteArrayAccess ba => ba -> CryptoFailable Key Source #
Take thirty-two bytes for a key. A different length is reported as
CryptoError_KeySizeInvalid; nothing else about a key can be wrong.
Instances
Extended nonce for XChaChaPoly1305.
Instances
nonce12 :: ByteArrayAccess iv => iv -> CryptoFailable Nonce Source #
Nonce smart constructor 12 bytes IV, nonce constructor
Arguments
| :: ByteArrayAccess ba | |
| => ba | 4 bytes constant |
| -> ba | 8 bytes IV |
| -> CryptoFailable Nonce |
8 bytes IV, nonce constructor
nonce24 :: ByteArrayAccess ba => ba -> CryptoFailable XNonce Source #
24 bytes IV, extended nonce constructor
incrementNonce :: Nonce -> Nonce Source #
Increment a nonce
initializeX :: Key -> XNonce -> State Source #
Initialize a new XChaChaPoly1305 State
The key length needs to be 256 bits, and the nonce
procured using nonce24.
appendAAD :: ByteArrayAccess ba => ba -> State -> State Source #
Append Authenticated Data to the State and return the new modified State.
Once no further call to this function need to be make,
the user should call finalizeAAD
finalizeAAD :: State -> State Source #
Finalize the Authenticated Data and return the finalized State
encrypt :: ByteArray ba => ba -> State -> (ba, State) Source #
Encrypt a piece of data and returns the encrypted Data and the updated State.