crypto-cipher-types-0.0.9: Generic cryptography cipher types

License | BSD-style
Maintainer | Vincent Hanquez <vincent@snarc.org>
Stability | Stable
Portability | Excellent
Safe Haskell | None
Language | Haskell98

Crypto.Cipher.Types

Contents

- Cipher classes
- AEAD functions
- Key type and constructor
- Initial Vector type and constructor
- Authentification Tag

Description

symmetric cipher basic types

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)
- type DataUnitOffset = Word32
- data KeySizeSpecifier
  - = KeySizeRange Int Int
  - | KeySizeEnum [Int]
  - | KeySizeFixed Int
- data KeyError
  - = KeyErrorTooSmall
  - | KeyErrorTooBig
  - | KeyErrorInvalid String
- data AEAD cipher = AEAD cipher (AEADState cipher)
- data AEADState cipher = AEADModeImpl cipher st => AEADState st
- data AEADMode
  - = AEAD_OCB
  - | AEAD_CCM
  - | AEAD_EAX
  - | AEAD_CWC
  - | AEAD_GCM
- class BlockCipher cipher => AEADModeImpl cipher state where
  - aeadStateAppendHeader :: cipher -> state -> ByteString -> state
  - aeadStateEncrypt :: cipher -> state -> ByteString -> (ByteString, state)
  - aeadStateDecrypt :: cipher -> state -> ByteString -> (ByteString, state)
  - aeadStateFinalize :: cipher -> state -> Int -> AuthTag
- cfb8Encrypt :: BlockCipher a => a -> IV a -> ByteString -> ByteString
- cfb8Decrypt :: BlockCipher a => a -> IV a -> ByteString -> ByteString
- 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
- aeadSimpleEncrypt :: BlockCipher a => AEAD a -> ByteString -> ByteString -> Int -> (AuthTag, ByteString)
- aeadSimpleDecrypt :: BlockCipher a => AEAD a -> ByteString -> ByteString -> AuthTag -> Maybe ByteString
- 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
- newtype AuthTag = AuthTag ByteString

# 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

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.

class Cipher cipher => StreamCipher cipher where Source #

Symmetric stream cipher class

Methods

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

Combine using the stream cipher

type DataUnitOffset = Word32 Source #

Offset inside an XTS data unit, measured in block size.

data KeySizeSpecifier Source #

Different specifier for key size in bytes

Constructors

KeySizeRange Int Int | in the range [min,max]
KeySizeEnum [Int] | one of the specified values
KeySizeFixed Int | a specific size

#### Instances

Instances details

Show KeySizeSpecifier Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods showsPrec :: Int -> KeySizeSpecifier -> ShowS Source # show :: KeySizeSpecifier -> String Source # showList :: [KeySizeSpecifier] -> ShowS Source #
Eq KeySizeSpecifier Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods (==) :: KeySizeSpecifier -> KeySizeSpecifier -> Bool Source # (/=) :: KeySizeSpecifier -> KeySizeSpecifier -> Bool Source #

data KeyError Source #

Possible Error that can be reported when initializating a key

Constructors

KeyErrorTooSmall |
KeyErrorTooBig |
KeyErrorInvalid String |

#### Instances

Instances details

Show KeyError Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods showsPrec :: Int -> KeyError -> ShowS Source # show :: KeyError -> String Source # showList :: [KeyError] -> ShowS Source #
Eq KeyError Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods (==) :: KeyError -> KeyError -> Bool Source # (/=) :: KeyError -> KeyError -> Bool Source #

data AEAD cipher Source #

Authenticated Encryption with Associated Data algorithms

Constructors

AEAD cipher (AEADState cipher) |

data AEADState cipher Source #

Wrapper for any AEADState

Constructors

AEADModeImpl cipher st => AEADState st |

data AEADMode Source #

AEAD Mode

Constructors

AEAD_OCB |
AEAD_CCM |
AEAD_EAX |
AEAD_CWC |
AEAD_GCM |

#### Instances

Instances details

Show AEADMode Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods showsPrec :: Int -> AEADMode -> ShowS Source # show :: AEADMode -> String Source # showList :: [AEADMode] -> ShowS Source #
Eq AEADMode Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods (==) :: AEADMode -> AEADMode -> Bool Source # (/=) :: AEADMode -> AEADMode -> Bool Source #

class BlockCipher cipher => AEADModeImpl cipher state where Source #

Class of AEAD Mode implementation

Methods

aeadStateAppendHeader :: cipher -> state -> ByteString -> state Source #

aeadStateEncrypt :: cipher -> state -> ByteString -> (ByteString, state) Source #

aeadStateDecrypt :: cipher -> state -> ByteString -> (ByteString, state) Source #

aeadStateFinalize :: cipher -> state -> Int -> AuthTag Source #

cfb8Encrypt :: BlockCipher a => a -> IV a -> ByteString -> ByteString Source #

Encrypt using CFB mode in 8 bit output

Effectively turn a Block cipher in CFB mode into a Stream cipher

cfb8Decrypt :: BlockCipher a => a -> IV a -> ByteString -> ByteString Source #

Decrypt using CFB mode in 8 bit output

Effectively turn a Block cipher in CFB mode into a Stream cipher

# AEAD functions

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

aeadSimpleEncrypt Source #

Arguments

:: BlockCipher a |
=> AEAD a | A new AEAD Context
-> ByteString | Optional Authentified Header
-> ByteString | Optional Plaintext
-> Int | Tag length
-> (AuthTag, ByteString) | Authentification tag and ciphertext

Simple AEAD encryption

aeadSimpleDecrypt Source #

Arguments

:: BlockCipher a |
=> AEAD a | A new AEAD Context
-> ByteString | Optional Authentified Header
-> ByteString | Optional Plaintext
-> AuthTag | Tag length
-> Maybe ByteString | Plaintext

Simple AEAD decryption

# Key type and constructor

data Key c Source #

a Key parametrized by the cipher

#### Instances

Instances details

Byteable (Key c) Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods toBytes :: Key c -> ByteString Source # byteableLength :: Key c -> Int Source # withBytePtr :: Key c -> (Ptr Word8 -> IO b) -> IO b Source #
Eq (Key c) Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods (==) :: Key c -> Key c -> Bool Source # (/=) :: Key c -> Key c -> Bool Source #
ToSecureMem (Key c) Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods toSecureMem :: Key c -> SecureMem Source #

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

Create a Key for a specified cipher

# Initial Vector type and constructor

data IV c Source #

an IV parametrized by the cipher

#### Instances

Instances details

Byteable (IV c) Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods toBytes :: IV c -> ByteString Source # byteableLength :: IV c -> Int Source # withBytePtr :: IV c -> (Ptr Word8 -> IO b) -> IO b Source #
Eq (IV c) Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods (==) :: IV c -> IV c -> Bool Source # (/=) :: IV c -> IV c -> Bool Source #

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.

# Authentification Tag

newtype AuthTag Source #

Authentification Tag for AE cipher mode

Constructors

AuthTag ByteString |

#### Instances

Instances details

Byteable AuthTag Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods toBytes :: AuthTag -> ByteString Source # byteableLength :: AuthTag -> Int Source # withBytePtr :: AuthTag -> (Ptr Word8 -> IO b) -> IO b Source #
Show AuthTag Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods showsPrec :: Int -> AuthTag -> ShowS Source # show :: AuthTag -> String Source # showList :: [AuthTag] -> ShowS Source #
Eq AuthTag Source # |
Instance details Defined in Crypto.Cipher.Types.Base Methods (==) :: AuthTag -> AuthTag -> Bool Source # (/=) :: AuthTag -> AuthTag -> Bool Source #