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
- cipherInit :: Key cipher -> cipher
- cipherName :: cipher -> String
- cipherKeySize :: cipher -> KeySizeSpecifier
- class Cipher cipher => BlockCipher cipher where
- blockSize :: cipher -> Int
- ecbEncrypt :: cipher -> ByteString -> ByteString
- ecbDecrypt :: cipher -> ByteString -> ByteString
- cbcEncrypt :: cipher -> IV cipher -> ByteString -> ByteString
- cbcDecrypt :: cipher -> IV cipher -> ByteString -> ByteString
- cfbEncrypt :: cipher -> IV cipher -> ByteString -> ByteString
- cfbDecrypt :: cipher -> IV cipher -> ByteString -> ByteString
- ctrCombine :: cipher -> IV cipher -> ByteString -> ByteString
- xtsEncrypt :: (cipher, cipher) -> IV cipher -> DataUnitOffset -> ByteString -> ByteString
- xtsDecrypt :: (cipher, cipher) -> IV cipher -> DataUnitOffset -> ByteString -> ByteString
- aeadInit :: Byteable iv => AEADMode -> cipher -> iv -> Maybe (AEAD cipher)
- class Cipher cipher => StreamCipher cipher where
- streamCombine :: cipher -> ByteString -> (ByteString, cipher)
- 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
class Cipher cipher where Source #
Symmetric cipher class.
Methods
cipherInit :: Key cipher -> cipher Source #
Initialize a cipher context from a key
cipherName :: cipher -> String Source #
Cipher name
cipherKeySize :: cipher -> KeySizeSpecifier Source #
return the size of the key required for this cipher. Some cipher accept any size for key
Instances
class Cipher cipher => BlockCipher cipher where Source #
Symmetric block cipher class
Minimal complete definition
Methods
blockSize :: cipher -> Int Source #
Return the size of block required for this block cipher
ecbEncrypt :: cipher -> ByteString -> ByteString Source #
Encrypt blocks
the input string need to be multiple of the block size
ecbDecrypt :: cipher -> ByteString -> ByteString Source #
Decrypt blocks
the input string need to be multiple of the block size
cbcEncrypt :: cipher -> IV cipher -> ByteString -> ByteString Source #
encrypt using the CBC mode.
input need to be a multiple of the blocksize
cbcDecrypt :: cipher -> IV cipher -> ByteString -> ByteString Source #
decrypt using the CBC mode.
input need to be a multiple of the blocksize
cfbEncrypt :: cipher -> IV cipher -> ByteString -> ByteString Source #
encrypt using the CFB mode.
input need to be a multiple of the blocksize
cfbDecrypt :: cipher -> IV cipher -> ByteString -> ByteString Source #
decrypt using the CFB mode.
input need to be a multiple of the blocksize
ctrCombine :: cipher -> IV cipher -> ByteString -> ByteString Source #
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) Source #
Initialize a new AEAD State
When Nothing is returns, it means the mode is not handled.
Instances
class Cipher cipher => StreamCipher cipher where Source #
Symmetric stream cipher class
Methods
streamCombine :: cipher -> ByteString -> (ByteString, cipher) Source #
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) Source #
Create a Key for a specified cipher
Initialization Vector (IV)
an IV parametrized by the cipher
Instances
Eq (IV c) | |
Byteable (IV c) | |
Defined in Crypto.Cipher.Types.Base |
makeIV :: (Byteable b, BlockCipher c) => b -> Maybe (IV c) Source #
Create an IV for a specified block cipher
nullIV :: BlockCipher c => IV c Source #
Create an IV that is effectively representing the number 0
ivAdd :: BlockCipher c => IV c -> Int -> IV c Source #
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 Source #
Append associated data into the AEAD state
aeadEncrypt :: BlockCipher a => AEAD a -> ByteString -> (ByteString, AEAD a) Source #
Encrypt input and append into the AEAD state
aeadDecrypt :: BlockCipher a => AEAD a -> ByteString -> (ByteString, AEAD a) Source #
Decrypt input and append into the AEAD state
aeadFinalize :: BlockCipher a => AEAD a -> Int -> AuthTag Source #
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 Source # ecbEncrypt :: Blowfish -> ByteString -> ByteString Source # ecbDecrypt :: Blowfish -> ByteString -> ByteString Source # cbcEncrypt :: Blowfish -> IV Blowfish -> ByteString -> ByteString Source # cbcDecrypt :: Blowfish -> IV Blowfish -> ByteString -> ByteString Source # cfbEncrypt :: Blowfish -> IV Blowfish -> ByteString -> ByteString Source # cfbDecrypt :: Blowfish -> IV Blowfish -> ByteString -> ByteString Source # ctrCombine :: Blowfish -> IV Blowfish -> ByteString -> ByteString Source # xtsEncrypt :: (Blowfish, Blowfish) -> IV Blowfish -> DataUnitOffset -> ByteString -> ByteString Source # xtsDecrypt :: (Blowfish, Blowfish) -> IV Blowfish -> DataUnitOffset -> ByteString -> ByteString Source # aeadInit :: Byteable iv => AEADMode -> Blowfish -> iv -> Maybe (AEAD Blowfish) Source # | |
Cipher Blowfish | |
Defined in Crypto.Cipher.Blowfish |
data Blowfish64 Source #
64 bit keyed blowfish state
Instances
data Blowfish128 Source #
128 bit keyed blowfish state
Instances
data Blowfish256 Source #
256 bit keyed blowfish state
Instances
data Blowfish448 Source #
448 bit keyed blowfish state
Instances
DES Context
Instances
Eq DES | |
BlockCipher DES | |
Defined in Crypto.Cipher.DES Methods blockSize :: DES -> Int Source # ecbEncrypt :: DES -> ByteString -> ByteString Source # ecbDecrypt :: DES -> ByteString -> ByteString Source # cbcEncrypt :: DES -> IV DES -> ByteString -> ByteString Source # cbcDecrypt :: DES -> IV DES -> ByteString -> ByteString Source # cfbEncrypt :: DES -> IV DES -> ByteString -> ByteString Source # cfbDecrypt :: DES -> IV DES -> ByteString -> ByteString Source # ctrCombine :: DES -> IV DES -> ByteString -> ByteString Source # xtsEncrypt :: (DES, DES) -> IV DES -> DataUnitOffset -> ByteString -> ByteString Source # xtsDecrypt :: (DES, DES) -> IV DES -> DataUnitOffset -> ByteString -> ByteString Source # aeadInit :: Byteable iv => AEADMode -> DES -> iv -> Maybe (AEAD DES) Source # | |
Cipher DES | |
Defined in Crypto.Cipher.DES Methods cipherInit :: Key DES -> DES Source # cipherName :: DES -> String Source # cipherKeySize :: DES -> KeySizeSpecifier Source # |
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 Source # ecbEncrypt :: DES_EEE3 -> ByteString -> ByteString Source # ecbDecrypt :: DES_EEE3 -> ByteString -> ByteString Source # cbcEncrypt :: DES_EEE3 -> IV DES_EEE3 -> ByteString -> ByteString Source # cbcDecrypt :: DES_EEE3 -> IV DES_EEE3 -> ByteString -> ByteString Source # cfbEncrypt :: DES_EEE3 -> IV DES_EEE3 -> ByteString -> ByteString Source # cfbDecrypt :: DES_EEE3 -> IV DES_EEE3 -> ByteString -> ByteString Source # ctrCombine :: DES_EEE3 -> IV DES_EEE3 -> ByteString -> ByteString Source # xtsEncrypt :: (DES_EEE3, DES_EEE3) -> IV DES_EEE3 -> DataUnitOffset -> ByteString -> ByteString Source # xtsDecrypt :: (DES_EEE3, DES_EEE3) -> IV DES_EEE3 -> DataUnitOffset -> ByteString -> ByteString Source # aeadInit :: Byteable iv => AEADMode -> DES_EEE3 -> iv -> Maybe (AEAD DES_EEE3) Source # | |
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 Source # ecbEncrypt :: DES_EDE3 -> ByteString -> ByteString Source # ecbDecrypt :: DES_EDE3 -> ByteString -> ByteString Source # cbcEncrypt :: DES_EDE3 -> IV DES_EDE3 -> ByteString -> ByteString Source # cbcDecrypt :: DES_EDE3 -> IV DES_EDE3 -> ByteString -> ByteString Source # cfbEncrypt :: DES_EDE3 -> IV DES_EDE3 -> ByteString -> ByteString Source # cfbDecrypt :: DES_EDE3 -> IV DES_EDE3 -> ByteString -> ByteString Source # ctrCombine :: DES_EDE3 -> IV DES_EDE3 -> ByteString -> ByteString Source # xtsEncrypt :: (DES_EDE3, DES_EDE3) -> IV DES_EDE3 -> DataUnitOffset -> ByteString -> ByteString Source # xtsDecrypt :: (DES_EDE3, DES_EDE3) -> IV DES_EDE3 -> DataUnitOffset -> ByteString -> ByteString Source # aeadInit :: Byteable iv => AEADMode -> DES_EDE3 -> iv -> Maybe (AEAD DES_EDE3) Source # | |
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 Source # ecbEncrypt :: DES_EEE2 -> ByteString -> ByteString Source # ecbDecrypt :: DES_EEE2 -> ByteString -> ByteString Source # cbcEncrypt :: DES_EEE2 -> IV DES_EEE2 -> ByteString -> ByteString Source # cbcDecrypt :: DES_EEE2 -> IV DES_EEE2 -> ByteString -> ByteString Source # cfbEncrypt :: DES_EEE2 -> IV DES_EEE2 -> ByteString -> ByteString Source # cfbDecrypt :: DES_EEE2 -> IV DES_EEE2 -> ByteString -> ByteString Source # ctrCombine :: DES_EEE2 -> IV DES_EEE2 -> ByteString -> ByteString Source # xtsEncrypt :: (DES_EEE2, DES_EEE2) -> IV DES_EEE2 -> DataUnitOffset -> ByteString -> ByteString Source # xtsDecrypt :: (DES_EEE2, DES_EEE2) -> IV DES_EEE2 -> DataUnitOffset -> ByteString -> ByteString Source # aeadInit :: Byteable iv => AEADMode -> DES_EEE2 -> iv -> Maybe (AEAD DES_EEE2) Source # | |
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 Source # ecbEncrypt :: DES_EDE2 -> ByteString -> ByteString Source # ecbDecrypt :: DES_EDE2 -> ByteString -> ByteString Source # cbcEncrypt :: DES_EDE2 -> IV DES_EDE2 -> ByteString -> ByteString Source # cbcDecrypt :: DES_EDE2 -> IV DES_EDE2 -> ByteString -> ByteString Source # cfbEncrypt :: DES_EDE2 -> IV DES_EDE2 -> ByteString -> ByteString Source # cfbDecrypt :: DES_EDE2 -> IV DES_EDE2 -> ByteString -> ByteString Source # ctrCombine :: DES_EDE2 -> IV DES_EDE2 -> ByteString -> ByteString Source # xtsEncrypt :: (DES_EDE2, DES_EDE2) -> IV DES_EDE2 -> DataUnitOffset -> ByteString -> ByteString Source # xtsDecrypt :: (DES_EDE2, DES_EDE2) -> IV DES_EDE2 -> DataUnitOffset -> ByteString -> ByteString Source # aeadInit :: Byteable iv => AEADMode -> DES_EDE2 -> iv -> Maybe (AEAD DES_EDE2) Source # | |
Cipher DES_EDE2 | |
Defined in Crypto.Cipher.TripleDES |
data Camellia128 Source #
Camellia block cipher with 128 bit key