raaz-0.0.2: The raaz cryptographic library.

Safe HaskellNone

Raaz.Cipher.Internal

Contents

Description

This module exposes the low-level internal details of ciphers. Do not import this module unless you want to implement a new cipher or give a new implementation of an existing cipher.

Synopsis

Internals of a cipher.

Ciphers provide symmetric encryption in the raaz library and are captured by the type class Cipher. Instances of Cipher are full encryption/decryption algorithms. For a block cipher this means that one also needs to specify the CipherMode to make it an instance of the class Cipher. They are instances of the class Symmetric and the associated type Key captures the encryption key for the cipher.

Implementations of ciphers are captured by two types.

CipherI:
Values of this type that captures implementations of a cipher. This type is parameterised over the memory element that is used internally by the implementation.
SomeCipherI:
The existentially quantified version of CipherI over its memory element. By wrapping the memory element inside the existential quantifier, values of this type exposes only the interface and not the internals of the implementation. The Implementation associated type of a cipher is the type SomeCipherI

To support a new cipher, a developer needs to:

  1. Define a new type which captures the cipher. This type should be an instance of the class Cipher.
  2. Define an implementation, i.e. a value of the type SomeCipherI.
  3. Define a recommended implementation, i.e. an instance of the type class Recommendation

class (Symmetric cipher, Implementation cipher ~ SomeCipherI cipher) => Cipher cipher Source

Instances

Cipher (AES 128 CBC) 
Cipher (AES 192 CBC) 
Cipher (AES 256 CBC) 

data CipherMode Source

Block cipher modes.

Constructors

CBC

Cipher-block chaining

CTR

Counter

Cipher implementation

data CipherI cipher encMem decMem Source

The implementation of a block cipher.

Constructors

CipherI 

Fields

cipherIName :: String
 
cipherIDescription :: String
 
encryptBlocks :: Pointer -> BLOCKS cipher -> MT encMem ()

The underlying block encryption function.

decryptBlocks :: Pointer -> BLOCKS cipher -> MT decMem ()

The underlying block decryption function.

Instances

Describable (CipherI cipher encMem decMem) 

data SomeCipherI cipher Source

Some implementation of a block cipher. This type existentially quantifies over the memory used in the implementation.

Constructors

forall encMem decMem . CipherM cipher encMem decMem => SomeCipherI (CipherI cipher encMem decMem) 

Instances

Unsafe encryption and decryption.

We expose some unsafe functions to encrypt and decrypt bytestrings. These function works correctly only if the input byte string has a length which is a multiple of the block size of the cipher and hence are unsafe to use as general methods of encryption and decryption of data. Use these functions for testing and benchmarking and nothing else.

There are multiple ways to handle arbitrary sized strings like padding, cipher block stealing etc. They are not exposed here though.

unsafeEncryptSource

Arguments

:: (Cipher c, Recommendation c) 
=> c

The cipher

-> Key c

The key to use

-> IB.ByteString

The string to encrypt

-> IB.ByteString 

Encrypt using the recommended implementation. This function is unsafe because it only works correctly when the input IB.ByteString is of length which is a multiple of the block length of the cipher.

unsafeDecryptSource

Arguments

:: (Cipher c, Recommendation c) 
=> c

The cipher

-> Key c

The key to use

-> IB.ByteString

The string to encrypt

-> IB.ByteString 

Decrypt using the recommended implementation. This function is unsafe because it only works correctly when the input IB.ByteString is of length which is a multiple of the block length of the cipher.

unsafeEncrypt'Source

Arguments

:: Cipher c 
=> c

The cipher to use

-> Implementation c

The implementation to use

-> Key c

The key to use

-> IB.ByteString

The string to encrypt.

-> IB.ByteString 

Encrypt the given IB.ByteString. This function is unsafe because it only works correctly when the input IB.ByteString is of length which is a multiple of the block length of the cipher.

unsafeDecrypt'Source

Arguments

:: Cipher c 
=> c

The cipher to use

-> Implementation c

The implementation to use

-> Key c

The key to use

-> IB.ByteString

The string to encrypt.

-> IB.ByteString 

Decrypts the given IB.ByteString. This function is unsafe because it only works correctly when the input IB.ByteString is of length which is a multiple of the block length of the cipher.