crypto-api-0.13.2: A generic interface for cryptographic operations

MaintainerThomas.DuBuisson@gmail.com
Stabilitybeta
Portabilityportable
Safe HaskellNone
LanguageHaskell98

Crypto.Classes

Contents

Description

This is the heart of the crypto-api package. By making (or having) an instance of Hash, AsymCipher, BlockCipher or StreamCipher you provide (or obtain) access to any infrastructure built on these primitives include block cipher modes of operation, hashing, hmac, signing, etc. These classes allow users to build routines that are agnostic to the algorithm used so changing algorithms is as simple as changing a type signature.

Synopsis

Hash class and helper functions

class (Serialize d, Eq d, Ord d) => Hash ctx d | d -> ctx, ctx -> d where #

The Hash class is intended as the generic interface targeted by maintainers of Haskell digest implementations. Using this generic interface, higher level functions such as hash and hash' provide a useful API for comsumers of hash implementations.

Any instantiated implementation must handle unaligned data.

Minimum complete definition: outputLength, blockLength, initialCtx, updateCtx, and finalize.

Minimal complete definition

outputLength, blockLength, initialCtx, updateCtx, finalize

Methods

outputLength :: Tagged d BitLength #

blockLength :: Tagged d BitLength #

initialCtx :: ctx #

updateCtx :: ctx -> ByteString -> ctx #

finalize :: ctx -> ByteString -> d #

hash :: Hash ctx d => ByteString -> d #

Hash a lazy ByteString, creating a digest

hash' :: Hash ctx d => ByteString -> d #

Hash a strict ByteString, creating a digest

hashFunc' :: Hash c d => d -> ByteString -> d #

Obtain a strict hash function whose result is the same type as the given digest, which is discarded. If the type is already inferred then consider using the hash' function instead.

hashFunc :: Hash c d => d -> ByteString -> d #

Obtain a lazy hash function whose result is the same type as the given digest, which is discarded. If the type is already inferred then consider using the hash function instead.

Cipher classes and helper functions

class Serialize k => BlockCipher k where #

The BlockCipher class is intended as the generic interface targeted by maintainers of Haskell cipher implementations.

Minimum complete definition: blockSize, encryptBlock, decryptBlock, buildKey, and keyLength.

Instances must handle unaligned data

Minimal complete definition

blockSize, encryptBlock, decryptBlock, buildKey, keyLength

Methods

blockSize :: Tagged k BitLength #

encryptBlock :: k -> ByteString -> ByteString #

decryptBlock :: k -> ByteString -> ByteString #

buildKey :: ByteString -> Maybe k #

keyLength :: Tagged k BitLength #

ecb :: k -> ByteString -> ByteString #

Electronic Cookbook (encryption)

unEcb :: k -> ByteString -> ByteString #

Electronic Cookbook (decryption)

cbc :: k -> IV k -> ByteString -> (ByteString, IV k) #

Cipherblock Chaining (encryption)

unCbc :: k -> IV k -> ByteString -> (ByteString, IV k) #

Cipherblock Chaining (decryption)

ctr :: k -> IV k -> ByteString -> (ByteString, IV k) #

Counter (encryption)

unCtr :: k -> IV k -> ByteString -> (ByteString, IV k) #

Counter (decryption)

ctrLazy :: k -> IV k -> ByteString -> (ByteString, IV k) #

Counter (encryption)

unCtrLazy :: k -> IV k -> ByteString -> (ByteString, IV k) #

Counter (decryption)

cfb :: k -> IV k -> ByteString -> (ByteString, IV k) #

Ciphertext feedback (encryption)

unCfb :: k -> IV k -> ByteString -> (ByteString, IV k) #

Ciphertext feedback (decryption)

ofb :: k -> IV k -> ByteString -> (ByteString, IV k) #

Output feedback (encryption)

unOfb :: k -> IV k -> ByteString -> (ByteString, IV k) #

Output feedback (decryption)

cbcLazy :: k -> IV k -> ByteString -> (ByteString, IV k) #

Cipher block chaining encryption for lazy bytestrings

unCbcLazy :: k -> IV k -> ByteString -> (ByteString, IV k) #

Cipher block chaining decryption for lazy bytestrings

sivLazy :: k -> k -> [ByteString] -> ByteString -> Maybe ByteString #

SIV (Synthetic IV) mode for lazy bytestrings. The third argument is the optional list of bytestrings to be authenticated but not encrypted As required by the specification this algorithm may return nothing when certain constraints aren't met.

unSivLazy :: k -> k -> [ByteString] -> ByteString -> Maybe ByteString #

SIV (Synthetic IV) for lazy bytestrings. The third argument is the optional list of bytestrings to be authenticated but not encrypted. As required by the specification this algorithm may return nothing when authentication fails.

siv :: k -> k -> [ByteString] -> ByteString -> Maybe ByteString #

SIV (Synthetic IV) mode for strict bytestrings. First argument is the optional list of bytestrings to be authenticated but not encrypted. As required by the specification this algorithm may return nothing when certain constraints aren't met.

unSiv :: k -> k -> [ByteString] -> ByteString -> Maybe ByteString #

SIV (Synthetic IV) for strict bytestrings First argument is the optional list of bytestrings to be authenticated but not encrypted As required by the specification this algorithm may return nothing when authentication fails.

ecbLazy :: k -> ByteString -> ByteString #

Cook book mode - not really a mode at all. If you don't know what you're doing, don't use this mode^H^H^H^H library.

unEcbLazy :: k -> ByteString -> ByteString #

ECB decrypt, complementary to ecb.

cfbLazy :: k -> IV k -> ByteString -> (ByteString, IV k) #

Ciphertext feed-back encryption mode for lazy bytestrings (with s == blockSize)

unCfbLazy :: k -> IV k -> ByteString -> (ByteString, IV k) #

Ciphertext feed-back decryption mode for lazy bytestrings (with s == blockSize)

ofbLazy :: k -> IV k -> ByteString -> (ByteString, IV k) #

Output feedback mode for lazy bytestrings

unOfbLazy :: k -> IV k -> ByteString -> (ByteString, IV k) #

Output feedback mode for lazy bytestrings

blockSizeBytes :: BlockCipher k => Tagged k ByteLength #

The number of bytes in a block cipher block

keyLengthBytes :: BlockCipher k => Tagged k ByteLength #

The number of bytes in a block cipher key (assuming it is an even multiple of 8 bits)

buildKeyIO :: BlockCipher k => IO k #

Build a symmetric key using the system entropy (see Entropy)

buildKeyGen :: (BlockCipher k, CryptoRandomGen g) => g -> Either GenError (k, g) #

Build a symmetric key using a given CryptoRandomGen

class Serialize k => StreamCipher k iv | k -> iv where #

A stream cipher class. Instance are expected to work on messages as small as one byte The length of the resulting cipher text should be equal to the length of the input message.

buildStreamKeyIO :: StreamCipher k iv => IO k #

Build a stream key using the system random generator

buildStreamKeyGen :: (StreamCipher k iv, CryptoRandomGen g) => g -> Either GenError (k, g) #

Build a stream key using the provided random generator

class AsymCipher p v | p -> v, v -> p where #

Asymetric ciphers (common ones being RSA or EC based)

buildKeyPairIO :: AsymCipher p v => BitLength -> IO (Either GenError (p, v)) #

Build a pair of asymmetric keys using the system random generator.

buildKeyPairGen :: (CryptoRandomGen g, AsymCipher p v) => BitLength -> g -> Either GenError ((p, v), g) #

Flipped buildKeyPair for ease of use with state monads.

class (Serialize p, Serialize v) => Signing p v | p -> v, v -> p where #

A class for signing operations which inherently can not be as generic as asymetric ciphers (ex: DSA).

buildSigningKeyPairIO :: Signing p v => BitLength -> IO (Either GenError (p, v)) #

Build a signing key using the system random generator

buildSigningKeyPairGen :: (Signing p v, CryptoRandomGen g) => BitLength -> g -> Either GenError ((p, v), g) #

Flipped buildSigningPair for ease of use with state monads.

Misc helper functions

encode :: Serialize a => a -> ByteString #

Encode a value using binary serialization to a strict ByteString.

zeroIV :: BlockCipher k => IV k #

Obtain an IV made only of zeroes

incIV :: BlockCipher k => IV k -> IV k #

Increase an IV by one. This is way faster than decoding, increasing, encoding

getIV :: (BlockCipher k, CryptoRandomGen g) => g -> Either GenError (IV k, g) #

Obtain an IV using the provided CryptoRandomGenerator.

getIVIO :: BlockCipher k => IO (IV k) #

Obtain an IV using the system entropy (see Entropy)

Orphan instances

BlockCipher k => Serialize (IV k) # 

Methods

put :: Putter (IV k) #

get :: Get (IV k) #