cryptocipher-0.6.2: Symmetrical block and stream ciphers.

LicenseBSD-style
MaintainerVincent Hanquez <vincent@snarc.org>
Stabilitystable
Portabilitygood
Safe HaskellNone
LanguageHaskell98

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

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
Cipher AES 
Instance details

Defined in Crypto.Cipher.AES

Cipher AES128 
Instance details

Defined in Crypto.Cipher.AES

Cipher AES192 
Instance details

Defined in Crypto.Cipher.AES

Cipher AES256 
Instance details

Defined in Crypto.Cipher.AES

Cipher Blowfish 
Instance details

Defined in Crypto.Cipher.Blowfish

Cipher Blowfish64 
Instance details

Defined in Crypto.Cipher.Blowfish

Cipher Blowfish128 
Instance details

Defined in Crypto.Cipher.Blowfish

Cipher Blowfish256 
Instance details

Defined in Crypto.Cipher.Blowfish

Cipher Blowfish448 
Instance details

Defined in Crypto.Cipher.Blowfish

Cipher Camellia128 
Instance details

Defined in Crypto.Cipher.Camellia

Cipher DES_EEE3 
Instance details

Defined in Crypto.Cipher.TripleDES

Cipher DES_EDE3 
Instance details

Defined in Crypto.Cipher.TripleDES

Cipher DES_EEE2 
Instance details

Defined in Crypto.Cipher.TripleDES

Cipher DES_EDE2 
Instance details

Defined in Crypto.Cipher.TripleDES

Cipher DES 
Instance details

Defined in Crypto.Cipher.DES

class Cipher cipher => BlockCipher cipher where Source #

Symmetric block cipher class

Minimal complete definition

blockSize, ecbEncrypt, ecbDecrypt

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

xtsEncrypt Source #

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

xtsDecrypt Source #

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
BlockCipher AES 
Instance details

Defined in Crypto.Cipher.AES

BlockCipher AES128 
Instance details

Defined in Crypto.Cipher.AES

BlockCipher AES192 
Instance details

Defined in Crypto.Cipher.AES

BlockCipher AES256 
Instance details

Defined in Crypto.Cipher.AES

BlockCipher Blowfish 
Instance details

Defined in Crypto.Cipher.Blowfish

BlockCipher Blowfish64 
Instance details

Defined in Crypto.Cipher.Blowfish

BlockCipher Blowfish128 
Instance details

Defined in Crypto.Cipher.Blowfish

BlockCipher Blowfish256 
Instance details

Defined in Crypto.Cipher.Blowfish

BlockCipher Blowfish448 
Instance details

Defined in Crypto.Cipher.Blowfish

BlockCipher Camellia128 
Instance details

Defined in Crypto.Cipher.Camellia

BlockCipher DES_EEE3 
Instance details

Defined in Crypto.Cipher.TripleDES

BlockCipher DES_EDE3 
Instance details

Defined in Crypto.Cipher.TripleDES

BlockCipher DES_EEE2 
Instance details

Defined in Crypto.Cipher.TripleDES

BlockCipher DES_EDE2 
Instance details

Defined in Crypto.Cipher.TripleDES

BlockCipher DES 
Instance details

Defined in Crypto.Cipher.DES

class Cipher cipher => StreamCipher cipher where Source #

Symmetric stream cipher class

Methods

streamCombine :: cipher -> ByteString -> (ByteString, cipher) Source #

Combine using the stream cipher

Key

data Key c Source #

a Key parametrized by the cipher

Instances
Eq (Key c) 
Instance details

Defined in Crypto.Cipher.Types.Base

Methods

(==) :: Key c -> Key c -> Bool Source #

(/=) :: Key c -> Key c -> Bool Source #

ToSecureMem (Key c) 
Instance details

Defined in Crypto.Cipher.Types.Base

Methods

toSecureMem :: Key c -> SecureMem

Byteable (Key c) 
Instance details

Defined in Crypto.Cipher.Types.Base

Methods

toBytes :: Key c -> ByteString

byteableLength :: Key c -> Int

withBytePtr :: Key c -> (Ptr Word8 -> IO b) -> IO b

makeKey :: (ToSecureMem b, Cipher c) => b -> Either KeyError (Key c) Source #

Create a Key for a specified cipher

Initialization Vector (IV)

data IV c Source #

an IV parametrized by the cipher

Instances
Eq (IV c) 
Instance details

Defined in Crypto.Cipher.Types.Base

Methods

(==) :: IV c -> IV c -> Bool Source #

(/=) :: IV c -> IV c -> Bool Source #

Byteable (IV c) 
Instance details

Defined in Crypto.Cipher.Types.Base

Methods

toBytes :: IV c -> ByteString

byteableLength :: IV c -> Int

withBytePtr :: IV c -> (Ptr Word8 -> IO b) -> IO b

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)

data AEAD cipher Source #

Authenticated Encryption with Associated Data algorithms

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

data AES128 Source #

AES with 128 bit key

Instances
BlockCipher AES128 
Instance details

Defined in Crypto.Cipher.AES

Cipher AES128 
Instance details

Defined in Crypto.Cipher.AES

AEADModeImpl AES128 AESOCB 
Instance details

Defined in Crypto.Cipher.AES

Methods

aeadStateAppendHeader :: AES128 -> AESOCB -> ByteString -> AESOCB Source #

aeadStateEncrypt :: AES128 -> AESOCB -> ByteString -> (ByteString, AESOCB) Source #

aeadStateDecrypt :: AES128 -> AESOCB -> ByteString -> (ByteString, AESOCB) Source #

aeadStateFinalize :: AES128 -> AESOCB -> Int -> AuthTag Source #

AEADModeImpl AES128 AESGCM 
Instance details

Defined in Crypto.Cipher.AES

data AES192 Source #

AES with 192 bit key

Instances
BlockCipher AES192 
Instance details

Defined in Crypto.Cipher.AES

Cipher AES192 
Instance details

Defined in Crypto.Cipher.AES

AEADModeImpl AES192 AESOCB 
Instance details

Defined in Crypto.Cipher.AES

Methods

aeadStateAppendHeader :: AES192 -> AESOCB -> ByteString -> AESOCB Source #

aeadStateEncrypt :: AES192 -> AESOCB -> ByteString -> (ByteString, AESOCB) Source #

aeadStateDecrypt :: AES192 -> AESOCB -> ByteString -> (ByteString, AESOCB) Source #

aeadStateFinalize :: AES192 -> AESOCB -> Int -> AuthTag Source #

AEADModeImpl AES192 AESGCM 
Instance details

Defined in Crypto.Cipher.AES

data AES256 Source #

AES with 256 bit key

Instances
BlockCipher AES256 
Instance details

Defined in Crypto.Cipher.AES

Cipher AES256 
Instance details

Defined in Crypto.Cipher.AES

AEADModeImpl AES256 AESOCB 
Instance details

Defined in Crypto.Cipher.AES

Methods

aeadStateAppendHeader :: AES256 -> AESOCB -> ByteString -> AESOCB Source #

aeadStateEncrypt :: AES256 -> AESOCB -> ByteString -> (ByteString, AESOCB) Source #

aeadStateDecrypt :: AES256 -> AESOCB -> ByteString -> (ByteString, AESOCB) Source #

aeadStateFinalize :: AES256 -> AESOCB -> Int -> AuthTag Source #

AEADModeImpl AES256 AESGCM 
Instance details

Defined in Crypto.Cipher.AES