License | BSD-style |
---|---|
Maintainer | Vincent Hanquez <vincent@snarc.org> |
Stability | stable |
Portability | good |
Safe Haskell | None |
Language | Haskell98 |
Crypto.Cipher
Contents
Description
All the cipher functionalities are available through the BlockCipher and StreamCipher classes.
A simplified example (with simplified error handling):
import Crypto.Cipher import Data.ByteString (ByteString) import qualified Data.ByteString as B initAES256 :: ByteString -> AES256 initAES256 = either (error . show) cipherInit . makeKey cbcEncryption :: AES256 -> ByteString -> ByteString -> ByteString cbcEncryption ctx ivRaw plainText = cbcEncrypt ctx iv plainText where iv = maybe (error "invalid IV") id $ ivRaw
Synopsis
- class Cipher cipher where
- class Cipher cipher => BlockCipher cipher where
- class Cipher cipher => StreamCipher cipher where
- data Key c
- makeKey :: (ToSecureMem b, Cipher c) => b -> Either KeyError (Key c)
- data IV c
- makeIV :: (Byteable b, BlockCipher c) => b -> Maybe (IV c)
- nullIV :: BlockCipher c => IV c
- ivAdd :: BlockCipher c => IV c -> Int -> IV c
- data AEAD cipher
- aeadAppendHeader :: BlockCipher a => AEAD a -> ByteString -> AEAD a
- aeadEncrypt :: BlockCipher a => AEAD a -> ByteString -> (ByteString, AEAD a)
- aeadDecrypt :: BlockCipher a => AEAD a -> ByteString -> (ByteString, AEAD a)
- aeadFinalize :: BlockCipher a => AEAD a -> Int -> AuthTag
- data AES128
- data AES192
- data AES256
- data Blowfish
- data Blowfish64
- data Blowfish128
- data Blowfish256
- data Blowfish448
- data DES
- data DES_EEE3
- data DES_EDE3
- data DES_EEE2
- data DES_EDE2
- data Camellia128
Cipher classes
Symmetric cipher class.
Minimal complete definition
Methods
cipherInit :: Key cipher -> cipher #
Initialize a cipher context from a key
cipherName :: cipher -> String #
Cipher name
cipherKeySize :: cipher -> KeySizeSpecifier #
return the size of the key required for this cipher. Some cipher accept any size for key
Instances
class Cipher cipher => BlockCipher cipher where #
Symmetric block cipher class
Minimal complete definition
Methods
Return the size of block required for this block cipher
ecbEncrypt :: cipher -> ByteString -> ByteString #
Encrypt blocks
the input string need to be multiple of the block size
ecbDecrypt :: cipher -> ByteString -> ByteString #
Decrypt blocks
the input string need to be multiple of the block size
cbcEncrypt :: cipher -> IV cipher -> ByteString -> ByteString #
encrypt using the CBC mode.
input need to be a multiple of the blocksize
cbcDecrypt :: cipher -> IV cipher -> ByteString -> ByteString #
decrypt using the CBC mode.
input need to be a multiple of the blocksize
cfbEncrypt :: cipher -> IV cipher -> ByteString -> ByteString #
encrypt using the CFB mode.
input need to be a multiple of the blocksize
cfbDecrypt :: cipher -> IV cipher -> ByteString -> ByteString #
decrypt using the CFB mode.
input need to be a multiple of the blocksize
ctrCombine :: cipher -> IV cipher -> ByteString -> ByteString #
combine using the CTR mode.
CTR mode produce a stream of randomized data that is combined (by XOR operation) with the input stream.
encryption and decryption are the same operation.
input can be of any size
Arguments
:: (cipher, cipher) | |
-> IV cipher | Usually represent the Data Unit (e.g. disk sector) |
-> DataUnitOffset | Offset in the data unit in number of blocks |
-> ByteString | Plaintext |
-> ByteString | Ciphertext |
encrypt using the XTS mode.
input need to be a multiple of the blocksize, and the cipher need to process 128 bits block only
Arguments
:: (cipher, cipher) | |
-> IV cipher | Usually represent the Data Unit (e.g. disk sector) |
-> DataUnitOffset | Offset in the data unit in number of blocks |
-> ByteString | Ciphertext |
-> ByteString | Plaintext |
decrypt using the XTS mode.
input need to be a multiple of the blocksize, and the cipher need to process 128 bits block only
aeadInit :: Byteable iv => AEADMode -> cipher -> iv -> Maybe (AEAD cipher) #
Initialize a new AEAD State
When Nothing is returns, it means the mode is not handled.
Instances
class Cipher cipher => StreamCipher cipher where #
Symmetric stream cipher class
Minimal complete definition
Methods
streamCombine :: cipher -> ByteString -> (ByteString, cipher) #
Combine using the stream cipher
Key
a Key parametrized by the cipher
Instances
Eq (Key c) | |
ToSecureMem (Key c) | |
Defined in Crypto.Cipher.Types.Base Methods toSecureMem :: Key c -> SecureMem | |
Byteable (Key c) | |
Defined in Crypto.Cipher.Types.Base |
makeKey :: (ToSecureMem b, Cipher c) => b -> Either KeyError (Key c) #
Create a Key for a specified cipher
Initialization Vector (IV)
an IV parametrized by the cipher
makeIV :: (Byteable b, BlockCipher c) => b -> Maybe (IV c) #
Create an IV for a specified block cipher
nullIV :: BlockCipher c => IV c #
Create an IV that is effectively representing the number 0
ivAdd :: BlockCipher c => IV c -> Int -> IV c #
Increment an IV by a number.
Assume the IV is in Big Endian format.
Authenticated Encryption with Associated Data (AEAD)
aeadAppendHeader :: BlockCipher a => AEAD a -> ByteString -> AEAD a #
Append associated data into the AEAD state
aeadEncrypt :: BlockCipher a => AEAD a -> ByteString -> (ByteString, AEAD a) #
Encrypt input and append into the AEAD state
aeadDecrypt :: BlockCipher a => AEAD a -> ByteString -> (ByteString, AEAD a) #
Decrypt input and append into the AEAD state
aeadFinalize :: BlockCipher a => AEAD a -> Int -> AuthTag #
Finalize the AEAD state and create an authentification tag
Cipher implementations
AES with 128 bit key
Instances
AES with 192 bit key
Instances
AES with 256 bit key
Instances
variable keyed blowfish state
Instances
BlockCipher Blowfish | |
Defined in Crypto.Cipher.Blowfish Methods blockSize :: Blowfish -> Int # ecbEncrypt :: Blowfish -> ByteString -> ByteString # ecbDecrypt :: Blowfish -> ByteString -> ByteString # cbcEncrypt :: Blowfish -> IV Blowfish -> ByteString -> ByteString # cbcDecrypt :: Blowfish -> IV Blowfish -> ByteString -> ByteString # cfbEncrypt :: Blowfish -> IV Blowfish -> ByteString -> ByteString # cfbDecrypt :: Blowfish -> IV Blowfish -> ByteString -> ByteString # ctrCombine :: Blowfish -> IV Blowfish -> ByteString -> ByteString # xtsEncrypt :: (Blowfish, Blowfish) -> IV Blowfish -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (Blowfish, Blowfish) -> IV Blowfish -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> Blowfish -> iv -> Maybe (AEAD Blowfish) # | |
Cipher Blowfish | |
Defined in Crypto.Cipher.Blowfish |
data Blowfish64 #
64 bit keyed blowfish state
Instances
BlockCipher Blowfish64 | |
Defined in Crypto.Cipher.Blowfish Methods blockSize :: Blowfish64 -> Int # ecbEncrypt :: Blowfish64 -> ByteString -> ByteString # ecbDecrypt :: Blowfish64 -> ByteString -> ByteString # cbcEncrypt :: Blowfish64 -> IV Blowfish64 -> ByteString -> ByteString # cbcDecrypt :: Blowfish64 -> IV Blowfish64 -> ByteString -> ByteString # cfbEncrypt :: Blowfish64 -> IV Blowfish64 -> ByteString -> ByteString # cfbDecrypt :: Blowfish64 -> IV Blowfish64 -> ByteString -> ByteString # ctrCombine :: Blowfish64 -> IV Blowfish64 -> ByteString -> ByteString # xtsEncrypt :: (Blowfish64, Blowfish64) -> IV Blowfish64 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (Blowfish64, Blowfish64) -> IV Blowfish64 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> Blowfish64 -> iv -> Maybe (AEAD Blowfish64) # | |
Cipher Blowfish64 | |
Defined in Crypto.Cipher.Blowfish |
data Blowfish128 #
128 bit keyed blowfish state
Instances
BlockCipher Blowfish128 | |
Defined in Crypto.Cipher.Blowfish Methods blockSize :: Blowfish128 -> Int # ecbEncrypt :: Blowfish128 -> ByteString -> ByteString # ecbDecrypt :: Blowfish128 -> ByteString -> ByteString # cbcEncrypt :: Blowfish128 -> IV Blowfish128 -> ByteString -> ByteString # cbcDecrypt :: Blowfish128 -> IV Blowfish128 -> ByteString -> ByteString # cfbEncrypt :: Blowfish128 -> IV Blowfish128 -> ByteString -> ByteString # cfbDecrypt :: Blowfish128 -> IV Blowfish128 -> ByteString -> ByteString # ctrCombine :: Blowfish128 -> IV Blowfish128 -> ByteString -> ByteString # xtsEncrypt :: (Blowfish128, Blowfish128) -> IV Blowfish128 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (Blowfish128, Blowfish128) -> IV Blowfish128 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> Blowfish128 -> iv -> Maybe (AEAD Blowfish128) # | |
Cipher Blowfish128 | |
Defined in Crypto.Cipher.Blowfish |
data Blowfish256 #
256 bit keyed blowfish state
Instances
BlockCipher Blowfish256 | |
Defined in Crypto.Cipher.Blowfish Methods blockSize :: Blowfish256 -> Int # ecbEncrypt :: Blowfish256 -> ByteString -> ByteString # ecbDecrypt :: Blowfish256 -> ByteString -> ByteString # cbcEncrypt :: Blowfish256 -> IV Blowfish256 -> ByteString -> ByteString # cbcDecrypt :: Blowfish256 -> IV Blowfish256 -> ByteString -> ByteString # cfbEncrypt :: Blowfish256 -> IV Blowfish256 -> ByteString -> ByteString # cfbDecrypt :: Blowfish256 -> IV Blowfish256 -> ByteString -> ByteString # ctrCombine :: Blowfish256 -> IV Blowfish256 -> ByteString -> ByteString # xtsEncrypt :: (Blowfish256, Blowfish256) -> IV Blowfish256 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (Blowfish256, Blowfish256) -> IV Blowfish256 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> Blowfish256 -> iv -> Maybe (AEAD Blowfish256) # | |
Cipher Blowfish256 | |
Defined in Crypto.Cipher.Blowfish |
data Blowfish448 #
448 bit keyed blowfish state
Instances
BlockCipher Blowfish448 | |
Defined in Crypto.Cipher.Blowfish Methods blockSize :: Blowfish448 -> Int # ecbEncrypt :: Blowfish448 -> ByteString -> ByteString # ecbDecrypt :: Blowfish448 -> ByteString -> ByteString # cbcEncrypt :: Blowfish448 -> IV Blowfish448 -> ByteString -> ByteString # cbcDecrypt :: Blowfish448 -> IV Blowfish448 -> ByteString -> ByteString # cfbEncrypt :: Blowfish448 -> IV Blowfish448 -> ByteString -> ByteString # cfbDecrypt :: Blowfish448 -> IV Blowfish448 -> ByteString -> ByteString # ctrCombine :: Blowfish448 -> IV Blowfish448 -> ByteString -> ByteString # xtsEncrypt :: (Blowfish448, Blowfish448) -> IV Blowfish448 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (Blowfish448, Blowfish448) -> IV Blowfish448 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> Blowfish448 -> iv -> Maybe (AEAD Blowfish448) # | |
Cipher Blowfish448 | |
Defined in Crypto.Cipher.Blowfish |
DES Context
Instances
Eq DES | |
BlockCipher DES | |
Defined in Crypto.Cipher.DES Methods ecbEncrypt :: DES -> ByteString -> ByteString # ecbDecrypt :: DES -> ByteString -> ByteString # cbcEncrypt :: DES -> IV DES -> ByteString -> ByteString # cbcDecrypt :: DES -> IV DES -> ByteString -> ByteString # cfbEncrypt :: DES -> IV DES -> ByteString -> ByteString # cfbDecrypt :: DES -> IV DES -> ByteString -> ByteString # ctrCombine :: DES -> IV DES -> ByteString -> ByteString # xtsEncrypt :: (DES, DES) -> IV DES -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (DES, DES) -> IV DES -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> DES -> iv -> Maybe (AEAD DES) # | |
Cipher DES | |
Defined in Crypto.Cipher.DES Methods cipherInit :: Key DES -> DES # cipherName :: DES -> String # cipherKeySize :: DES -> KeySizeSpecifier # |
3DES with 3 different keys used all in the same direction
Instances
Eq DES_EEE3 | |
BlockCipher DES_EEE3 | |
Defined in Crypto.Cipher.TripleDES Methods blockSize :: DES_EEE3 -> Int # ecbEncrypt :: DES_EEE3 -> ByteString -> ByteString # ecbDecrypt :: DES_EEE3 -> ByteString -> ByteString # cbcEncrypt :: DES_EEE3 -> IV DES_EEE3 -> ByteString -> ByteString # cbcDecrypt :: DES_EEE3 -> IV DES_EEE3 -> ByteString -> ByteString # cfbEncrypt :: DES_EEE3 -> IV DES_EEE3 -> ByteString -> ByteString # cfbDecrypt :: DES_EEE3 -> IV DES_EEE3 -> ByteString -> ByteString # ctrCombine :: DES_EEE3 -> IV DES_EEE3 -> ByteString -> ByteString # xtsEncrypt :: (DES_EEE3, DES_EEE3) -> IV DES_EEE3 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (DES_EEE3, DES_EEE3) -> IV DES_EEE3 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> DES_EEE3 -> iv -> Maybe (AEAD DES_EEE3) # | |
Cipher DES_EEE3 | |
Defined in Crypto.Cipher.TripleDES |
3DES with 3 different keys used in alternative direction
Instances
Eq DES_EDE3 | |
BlockCipher DES_EDE3 | |
Defined in Crypto.Cipher.TripleDES Methods blockSize :: DES_EDE3 -> Int # ecbEncrypt :: DES_EDE3 -> ByteString -> ByteString # ecbDecrypt :: DES_EDE3 -> ByteString -> ByteString # cbcEncrypt :: DES_EDE3 -> IV DES_EDE3 -> ByteString -> ByteString # cbcDecrypt :: DES_EDE3 -> IV DES_EDE3 -> ByteString -> ByteString # cfbEncrypt :: DES_EDE3 -> IV DES_EDE3 -> ByteString -> ByteString # cfbDecrypt :: DES_EDE3 -> IV DES_EDE3 -> ByteString -> ByteString # ctrCombine :: DES_EDE3 -> IV DES_EDE3 -> ByteString -> ByteString # xtsEncrypt :: (DES_EDE3, DES_EDE3) -> IV DES_EDE3 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (DES_EDE3, DES_EDE3) -> IV DES_EDE3 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> DES_EDE3 -> iv -> Maybe (AEAD DES_EDE3) # | |
Cipher DES_EDE3 | |
Defined in Crypto.Cipher.TripleDES |
3DES where the first and third keys are equal, used in the same direction
Instances
Eq DES_EEE2 | |
BlockCipher DES_EEE2 | |
Defined in Crypto.Cipher.TripleDES Methods blockSize :: DES_EEE2 -> Int # ecbEncrypt :: DES_EEE2 -> ByteString -> ByteString # ecbDecrypt :: DES_EEE2 -> ByteString -> ByteString # cbcEncrypt :: DES_EEE2 -> IV DES_EEE2 -> ByteString -> ByteString # cbcDecrypt :: DES_EEE2 -> IV DES_EEE2 -> ByteString -> ByteString # cfbEncrypt :: DES_EEE2 -> IV DES_EEE2 -> ByteString -> ByteString # cfbDecrypt :: DES_EEE2 -> IV DES_EEE2 -> ByteString -> ByteString # ctrCombine :: DES_EEE2 -> IV DES_EEE2 -> ByteString -> ByteString # xtsEncrypt :: (DES_EEE2, DES_EEE2) -> IV DES_EEE2 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (DES_EEE2, DES_EEE2) -> IV DES_EEE2 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> DES_EEE2 -> iv -> Maybe (AEAD DES_EEE2) # | |
Cipher DES_EEE2 | |
Defined in Crypto.Cipher.TripleDES |
3DES where the first and third keys are equal, used in alternative direction
Instances
Eq DES_EDE2 | |
BlockCipher DES_EDE2 | |
Defined in Crypto.Cipher.TripleDES Methods blockSize :: DES_EDE2 -> Int # ecbEncrypt :: DES_EDE2 -> ByteString -> ByteString # ecbDecrypt :: DES_EDE2 -> ByteString -> ByteString # cbcEncrypt :: DES_EDE2 -> IV DES_EDE2 -> ByteString -> ByteString # cbcDecrypt :: DES_EDE2 -> IV DES_EDE2 -> ByteString -> ByteString # cfbEncrypt :: DES_EDE2 -> IV DES_EDE2 -> ByteString -> ByteString # cfbDecrypt :: DES_EDE2 -> IV DES_EDE2 -> ByteString -> ByteString # ctrCombine :: DES_EDE2 -> IV DES_EDE2 -> ByteString -> ByteString # xtsEncrypt :: (DES_EDE2, DES_EDE2) -> IV DES_EDE2 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (DES_EDE2, DES_EDE2) -> IV DES_EDE2 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> DES_EDE2 -> iv -> Maybe (AEAD DES_EDE2) # | |
Cipher DES_EDE2 | |
Defined in Crypto.Cipher.TripleDES |
data Camellia128 #
Camellia block cipher with 128 bit key
Instances
BlockCipher Camellia128 | |
Defined in Crypto.Cipher.Camellia Methods blockSize :: Camellia128 -> Int # ecbEncrypt :: Camellia128 -> ByteString -> ByteString # ecbDecrypt :: Camellia128 -> ByteString -> ByteString # cbcEncrypt :: Camellia128 -> IV Camellia128 -> ByteString -> ByteString # cbcDecrypt :: Camellia128 -> IV Camellia128 -> ByteString -> ByteString # cfbEncrypt :: Camellia128 -> IV Camellia128 -> ByteString -> ByteString # cfbDecrypt :: Camellia128 -> IV Camellia128 -> ByteString -> ByteString # ctrCombine :: Camellia128 -> IV Camellia128 -> ByteString -> ByteString # xtsEncrypt :: (Camellia128, Camellia128) -> IV Camellia128 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (Camellia128, Camellia128) -> IV Camellia128 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> Camellia128 -> iv -> Maybe (AEAD Camellia128) # | |
Cipher Camellia128 | |
Defined in Crypto.Cipher.Camellia |