cryptonite-0.9: Cryptography Primitives sink

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

Crypto.Cipher.Types

Contents

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

Description

symmetric cipher basic types

Synopsis

- class Cipher cipher where
  - cipherInit :: ByteArray key => key -> CryptoFailable cipher
  - cipherName :: cipher -> String
  - cipherKeySize :: cipher -> KeySizeSpecifier
- class Cipher cipher => BlockCipher cipher where
  - blockSize :: cipher -> Int
  - ecbEncrypt :: ByteArray ba => cipher -> ba -> ba
  - ecbDecrypt :: ByteArray ba => cipher -> ba -> ba
  - cbcEncrypt :: ByteArray ba => cipher -> IV cipher -> ba -> ba
  - cbcDecrypt :: ByteArray ba => cipher -> IV cipher -> ba -> ba
  - cfbEncrypt :: ByteArray ba => cipher -> IV cipher -> ba -> ba
  - cfbDecrypt :: ByteArray ba => cipher -> IV cipher -> ba -> ba
  - ctrCombine :: ByteArray ba => cipher -> IV cipher -> ba -> ba
  - aeadInit :: ByteArrayAccess iv => AEADMode -> cipher -> iv -> CryptoFailable (AEAD cipher)
- class BlockCipher cipher => BlockCipher128 cipher where
  - xtsEncrypt :: ByteArray ba => (cipher, cipher) -> IV cipher -> DataUnitOffset -> ba -> ba
  - xtsDecrypt :: ByteArray ba => (cipher, cipher) -> IV cipher -> DataUnitOffset -> ba -> ba
- class Cipher cipher => StreamCipher cipher where
  - streamCombine :: ByteArray ba => cipher -> ba -> (ba, cipher)
- type DataUnitOffset = Word32
- data KeySizeSpecifier
  - = KeySizeRange Int Int
  - | KeySizeEnum [Int]
  - | KeySizeFixed Int
- data AEADMode
  - = AEAD_OCB
  - | AEAD_CCM
  - | AEAD_EAX
  - | AEAD_CWC
  - | AEAD_GCM
- data AEADModeImpl st = AEADModeImpl {
  - aeadImplAppendHeader :: forall ba. ByteArrayAccess ba => st -> ba -> st
  - aeadImplEncrypt :: forall ba. ByteArray ba => st -> ba -> (ba, st)
  - aeadImplDecrypt :: forall ba. ByteArray ba => st -> ba -> (ba, st)
  - aeadImplFinalize :: st -> Int -> AuthTag
  }
- data AEAD cipher = forall st . AEAD {
  - aeadModeImpl :: AEADModeImpl st
  - aeadState :: st
  }
- aeadAppendHeader :: ByteArrayAccess aad => AEAD cipher -> aad -> AEAD cipher
- aeadEncrypt :: ByteArray ba => AEAD cipher -> ba -> (ba, AEAD cipher)
- aeadDecrypt :: ByteArray ba => AEAD cipher -> ba -> (ba, AEAD cipher)
- aeadFinalize :: AEAD cipher -> Int -> AuthTag
- aeadSimpleEncrypt :: (ByteArrayAccess aad, ByteArray ba) => AEAD a -> aad -> ba -> Int -> (AuthTag, ba)
- aeadSimpleDecrypt :: (ByteArrayAccess aad, ByteArray ba) => AEAD a -> aad -> ba -> AuthTag -> Maybe ba
- data IV c
- makeIV :: (ByteArrayAccess b, BlockCipher c) => b -> Maybe (IV c)
- nullIV :: BlockCipher c => IV c
- ivAdd :: BlockCipher c => IV c -> Int -> IV c
- newtype AuthTag = AuthTag {
  - unAuthTag :: Bytes
  }

# Cipher classes

class Cipher cipher where

Symmetric cipher class.

Methods

cipherInit :: ByteArray key => key -> CryptoFailable 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

Cipher Blowfish448 |
Cipher Blowfish256 |
Cipher Blowfish128 |
Cipher Blowfish64 |
Cipher Blowfish |
Cipher Camellia128 |
Cipher DES |
Cipher DES_EDE2 |
Cipher DES_EEE2 |
Cipher DES_EDE3 |
Cipher DES_EEE3 |
Cipher AES256 |
Cipher AES192 |
Cipher AES128 |

class Cipher cipher => BlockCipher cipher where

Symmetric block cipher class

Minimal complete definition

blockSize, ecbEncrypt, ecbDecrypt

Methods

blockSize :: cipher -> Int

Return the size of block required for this block cipher

ecbEncrypt :: ByteArray ba => cipher -> ba -> ba

Encrypt blocks

the input string need to be multiple of the block size

ecbDecrypt :: ByteArray ba => cipher -> ba -> ba

Decrypt blocks

the input string need to be multiple of the block size

cbcEncrypt :: ByteArray ba => cipher -> IV cipher -> ba -> ba

encrypt using the CBC mode.

input need to be a multiple of the blocksize

cbcDecrypt :: ByteArray ba => cipher -> IV cipher -> ba -> ba

decrypt using the CBC mode.

input need to be a multiple of the blocksize

cfbEncrypt :: ByteArray ba => cipher -> IV cipher -> ba -> ba

encrypt using the CFB mode.

input need to be a multiple of the blocksize

cfbDecrypt :: ByteArray ba => cipher -> IV cipher -> ba -> ba

decrypt using the CFB mode.

input need to be a multiple of the blocksize

ctrCombine :: ByteArray ba => cipher -> IV cipher -> ba -> ba

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

aeadInit :: ByteArrayAccess iv => AEADMode -> cipher -> iv -> CryptoFailable (AEAD cipher)

Initialize a new AEAD State

When Nothing is returns, it means the mode is not handled.

Instances

BlockCipher Blowfish448 |
BlockCipher Blowfish256 |
BlockCipher Blowfish128 |
BlockCipher Blowfish64 |
BlockCipher Blowfish |
BlockCipher Camellia128 |
BlockCipher DES |
BlockCipher DES_EDE2 |
BlockCipher DES_EEE2 |
BlockCipher DES_EDE3 |
BlockCipher DES_EEE3 |
BlockCipher AES256 |
BlockCipher AES192 |
BlockCipher AES128 |

class BlockCipher cipher => BlockCipher128 cipher where

class of block cipher with a 128 bits block size

Minimal complete definition

Nothing

Methods

xtsEncrypt

Arguments

:: ByteArray ba |
=> (cipher, cipher) |
-> IV cipher | Usually represent the Data Unit (e.g. disk sector)
-> DataUnitOffset | Offset in the data unit in number of blocks
-> ba | Plaintext
-> ba | 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

Arguments

:: ByteArray ba |
=> (cipher, cipher) |
-> IV cipher | Usually represent the Data Unit (e.g. disk sector)
-> DataUnitOffset | Offset in the data unit in number of blocks
-> ba | Ciphertext
-> ba | 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

Instances

BlockCipher128 AES256 |
BlockCipher128 AES192 |
BlockCipher128 AES128 |

class Cipher cipher => StreamCipher cipher where

Symmetric stream cipher class

Methods

streamCombine :: ByteArray ba => cipher -> ba -> (ba, cipher)

Combine using the stream cipher

type DataUnitOffset = Word32

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

data KeySizeSpecifier

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

Eq KeySizeSpecifier |
Show KeySizeSpecifier |

# AEAD functions

data AEADMode

AEAD Mode

Constructors

AEAD_OCB |
AEAD_CCM |
AEAD_EAX |
AEAD_CWC |
AEAD_GCM |

Instances

Eq AEADMode |
Show AEADMode |

data AEADModeImpl st

AEAD Implementation

Constructors

AEADModeImpl |
Fields aeadImplAppendHeader :: forall ba. ByteArrayAccess ba => st -> ba -> st aeadImplEncrypt :: forall ba. ByteArray ba => st -> ba -> (ba, st) aeadImplDecrypt :: forall ba. ByteArray ba => st -> ba -> (ba, st) aeadImplFinalize :: st -> Int -> AuthTag

data AEAD cipher

Authenticated Encryption with Associated Data algorithms

Constructors

forall st . AEAD |
Fields aeadModeImpl :: AEADModeImpl st aeadState :: st

aeadAppendHeader :: ByteArrayAccess aad => AEAD cipher -> aad -> AEAD cipher

Append some header information to an AEAD context

aeadEncrypt :: ByteArray ba => AEAD cipher -> ba -> (ba, AEAD cipher)

Encrypt some data and update the AEAD context

aeadDecrypt :: ByteArray ba => AEAD cipher -> ba -> (ba, AEAD cipher)

Decrypt some data and update the AEAD context

aeadFinalize :: AEAD cipher -> Int -> AuthTag

Finalize the AEAD context and return the authentication tag

aeadSimpleEncrypt

Arguments

:: (ByteArrayAccess aad, ByteArray ba) |
=> AEAD a | A new AEAD Context
-> aad | Optional Authentication data header
-> ba | Optional Plaintext
-> Int | Tag length
-> (AuthTag, ba) | Authentication tag and ciphertext

Simple AEAD encryption

aeadSimpleDecrypt

Arguments

:: (ByteArrayAccess aad, ByteArray ba) |
=> AEAD a | A new AEAD Context
-> aad | Optional Authentication data header
-> ba | Ciphertext
-> AuthTag | The authentication tag
-> Maybe ba | Plaintext

Simple AEAD decryption

# Initial Vector type and constructor

data IV c

an IV parametrized by the cipher

Instances

Eq (IV c) |
BlockCipher c => ByteArrayAccess (IV c) |

makeIV :: (ByteArrayAccess 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.

# Authentification Tag

newtype AuthTag

Authentication Tag for AE cipher mode

Constructors

AuthTag |
Fields unAuthTag :: Bytes

Instances

Eq AuthTag |
Show AuthTag |
ByteArrayAccess AuthTag |