Crypto.Cipher.AES

cipher-aes-0.2.11: Fast AES cipher implementation with advanced mode of operations

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

Crypto.Cipher.AES

Contents

- block cipher data types
- IV
- Authenticated encryption block cipher types
- creation
- misc
- encryption
- decryption

Description

Synopsis

- data AES
- data AES128
- data AES192
- data AES256
- data AESIV
- aesIV_ :: ByteString -> AESIV
- data AESGCM
- initAES :: Byteable b => b -> AES
- initKey :: Byteable b => b -> AES
- genCTR :: Byteable iv => AES -> iv -> Int -> ByteString
- genCounter :: AES -> AESIV -> Int -> (ByteString, AESIV)
- encryptECB :: AES -> ByteString -> ByteString
- encryptCBC :: Byteable iv => AES -> iv -> ByteString -> ByteString
- encryptCTR :: Byteable iv => AES -> iv -> ByteString -> ByteString
- encryptXTS :: Byteable iv => (AES, AES) -> iv -> Word32 -> ByteString -> ByteString
- encryptGCM :: Byteable iv => AES -> iv -> ByteString -> ByteString -> (ByteString, AuthTag)
- encryptOCB :: Byteable iv => AES -> iv -> ByteString -> ByteString -> (ByteString, AuthTag)
- decryptECB :: AES -> ByteString -> ByteString
- decryptCBC :: Byteable iv => AES -> iv -> ByteString -> ByteString
- decryptCTR :: Byteable iv => AES -> iv -> ByteString -> ByteString
- decryptXTS :: Byteable iv => (AES, AES) -> iv -> Word32 -> ByteString -> ByteString
- decryptGCM :: Byteable iv => AES -> iv -> ByteString -> ByteString -> (ByteString, AuthTag)
- decryptOCB :: Byteable iv => AES -> iv -> ByteString -> ByteString -> (ByteString, AuthTag)

# block cipher data types

data AES #

AES Context (pre-processed key)

Instances

BlockCipher AES # |
Methods blockSize :: AES -> Int # ecbEncrypt :: AES -> ByteString -> ByteString # ecbDecrypt :: AES -> ByteString -> ByteString # cbcEncrypt :: AES -> IV AES -> ByteString -> ByteString # cbcDecrypt :: AES -> IV AES -> ByteString -> ByteString # cfbEncrypt :: AES -> IV AES -> ByteString -> ByteString # cfbDecrypt :: AES -> IV AES -> ByteString -> ByteString # ctrCombine :: AES -> IV AES -> ByteString -> ByteString # xtsEncrypt :: (AES, AES) -> IV AES -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (AES, AES) -> IV AES -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> AES -> iv -> Maybe (AEAD AES) #
Cipher AES # |
Methods cipherInit :: Key AES -> AES # cipherName :: AES -> String # cipherKeySize :: AES -> KeySizeSpecifier #
AEADModeImpl AES AESGCM # |
Methods aeadStateAppendHeader :: AES -> AESGCM -> ByteString -> AESGCM # aeadStateEncrypt :: AES -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateDecrypt :: AES -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateFinalize :: AES -> AESGCM -> Int -> AuthTag #

data AES128 #

AES with 128 bit key

Instances

BlockCipher AES128 # |
Methods blockSize :: AES128 -> Int # ecbEncrypt :: AES128 -> ByteString -> ByteString # ecbDecrypt :: AES128 -> ByteString -> ByteString # cbcEncrypt :: AES128 -> IV AES128 -> ByteString -> ByteString # cbcDecrypt :: AES128 -> IV AES128 -> ByteString -> ByteString # cfbEncrypt :: AES128 -> IV AES128 -> ByteString -> ByteString # cfbDecrypt :: AES128 -> IV AES128 -> ByteString -> ByteString # ctrCombine :: AES128 -> IV AES128 -> ByteString -> ByteString # xtsEncrypt :: (AES128, AES128) -> IV AES128 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (AES128, AES128) -> IV AES128 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> AES128 -> iv -> Maybe (AEAD AES128) #
Cipher AES128 # |
Methods cipherInit :: Key AES128 -> AES128 # cipherName :: AES128 -> String # cipherKeySize :: AES128 -> KeySizeSpecifier #
AEADModeImpl AES128 AESGCM # |
Methods aeadStateAppendHeader :: AES128 -> AESGCM -> ByteString -> AESGCM # aeadStateEncrypt :: AES128 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateDecrypt :: AES128 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateFinalize :: AES128 -> AESGCM -> Int -> AuthTag #

data AES192 #

AES with 192 bit key

Instances

BlockCipher AES192 # |
Methods blockSize :: AES192 -> Int # ecbEncrypt :: AES192 -> ByteString -> ByteString # ecbDecrypt :: AES192 -> ByteString -> ByteString # cbcEncrypt :: AES192 -> IV AES192 -> ByteString -> ByteString # cbcDecrypt :: AES192 -> IV AES192 -> ByteString -> ByteString # cfbEncrypt :: AES192 -> IV AES192 -> ByteString -> ByteString # cfbDecrypt :: AES192 -> IV AES192 -> ByteString -> ByteString # ctrCombine :: AES192 -> IV AES192 -> ByteString -> ByteString # xtsEncrypt :: (AES192, AES192) -> IV AES192 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (AES192, AES192) -> IV AES192 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> AES192 -> iv -> Maybe (AEAD AES192) #
Cipher AES192 # |
Methods cipherInit :: Key AES192 -> AES192 # cipherName :: AES192 -> String # cipherKeySize :: AES192 -> KeySizeSpecifier #
AEADModeImpl AES192 AESGCM # |
Methods aeadStateAppendHeader :: AES192 -> AESGCM -> ByteString -> AESGCM # aeadStateEncrypt :: AES192 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateDecrypt :: AES192 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateFinalize :: AES192 -> AESGCM -> Int -> AuthTag #

data AES256 #

AES with 256 bit key

Instances

BlockCipher AES256 # |
Methods blockSize :: AES256 -> Int # ecbEncrypt :: AES256 -> ByteString -> ByteString # ecbDecrypt :: AES256 -> ByteString -> ByteString # cbcEncrypt :: AES256 -> IV AES256 -> ByteString -> ByteString # cbcDecrypt :: AES256 -> IV AES256 -> ByteString -> ByteString # cfbEncrypt :: AES256 -> IV AES256 -> ByteString -> ByteString # cfbDecrypt :: AES256 -> IV AES256 -> ByteString -> ByteString # ctrCombine :: AES256 -> IV AES256 -> ByteString -> ByteString # xtsEncrypt :: (AES256, AES256) -> IV AES256 -> DataUnitOffset -> ByteString -> ByteString # xtsDecrypt :: (AES256, AES256) -> IV AES256 -> DataUnitOffset -> ByteString -> ByteString # aeadInit :: Byteable iv => AEADMode -> AES256 -> iv -> Maybe (AEAD AES256) #
Cipher AES256 # |
Methods cipherInit :: Key AES256 -> AES256 # cipherName :: AES256 -> String # cipherKeySize :: AES256 -> KeySizeSpecifier #
AEADModeImpl AES256 AESGCM # |
Methods aeadStateAppendHeader :: AES256 -> AESGCM -> ByteString -> AESGCM # aeadStateEncrypt :: AES256 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateDecrypt :: AES256 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateFinalize :: AES256 -> AESGCM -> Int -> AuthTag #

# IV

data AESIV #

AES IV is always 16 bytes

Instances

Eq AESIV # |
Methods (==) :: AESIV -> AESIV -> Bool # (/=) :: AESIV -> AESIV -> Bool #
Show AESIV # |
Methods showsPrec :: Int -> AESIV -> ShowS # show :: AESIV -> String # showList :: [AESIV] -> ShowS #
Byteable AESIV # |
Methods toBytes :: AESIV -> ByteString # byteableLength :: AESIV -> Int # withBytePtr :: AESIV -> (Ptr Word8 -> IO b) -> IO b #

aesIV_ :: ByteString -> AESIV #

convert a bytestring to an AESIV

# Authenticated encryption block cipher types

data AESGCM #

AESGCM State

Instances

AEADModeImpl AES256 AESGCM # |
Methods aeadStateAppendHeader :: AES256 -> AESGCM -> ByteString -> AESGCM # aeadStateEncrypt :: AES256 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateDecrypt :: AES256 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateFinalize :: AES256 -> AESGCM -> Int -> AuthTag #
AEADModeImpl AES192 AESGCM # |
Methods aeadStateAppendHeader :: AES192 -> AESGCM -> ByteString -> AESGCM # aeadStateEncrypt :: AES192 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateDecrypt :: AES192 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateFinalize :: AES192 -> AESGCM -> Int -> AuthTag #
AEADModeImpl AES128 AESGCM # |
Methods aeadStateAppendHeader :: AES128 -> AESGCM -> ByteString -> AESGCM # aeadStateEncrypt :: AES128 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateDecrypt :: AES128 -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateFinalize :: AES128 -> AESGCM -> Int -> AuthTag #
AEADModeImpl AES AESGCM # |
Methods aeadStateAppendHeader :: AES -> AESGCM -> ByteString -> AESGCM # aeadStateEncrypt :: AES -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateDecrypt :: AES -> AESGCM -> ByteString -> (ByteString, AESGCM) # aeadStateFinalize :: AES -> AESGCM -> Int -> AuthTag #

# creation

initAES :: Byteable b => b -> AES #

Initialize a new context with a key

Key need to be of length 16, 24 or 32 bytes. any other values will cause undefined behavior

initKey :: Byteable b => b -> AES #

Deprecated: use initAES

# misc

genCTR #

Arguments

:: Byteable iv |
=> AES | Cipher Key.
-> iv | usually a 128 bit integer.
-> Int | length of bytes required.
-> ByteString |

generate a counter mode pad. this is generally xor-ed to an input to make the standard counter mode block operations.

if the length requested is not a multiple of the block cipher size, more data will be returned, so that the returned bytestring is a multiple of the block cipher size.

genCounter :: AES -> AESIV -> Int -> (ByteString, AESIV) #

generate a counter mode pad. this is generally xor-ed to an input to make the standard counter mode block operations.

if the length requested is not a multiple of the block cipher size, more data will be returned, so that the returned bytestring is a multiple of the block cipher size.

Similiar to `genCTR` but also return the next IV for continuation

# encryption

encryptECB :: AES -> ByteString -> ByteString #

encrypt using Electronic Code Book (ECB)

encryptCBC #

Arguments

:: Byteable iv |
=> AES | AES Context
-> iv | Initial vector of AES block size
-> ByteString | plaintext
-> ByteString | ciphertext

encrypt using Cipher Block Chaining (CBC)

encryptCTR #

Arguments

:: Byteable iv |
=> AES | AES Context
-> iv | initial vector of AES block size (usually representing a 128 bit integer)
-> ByteString | plaintext input
-> ByteString | ciphertext output

encrypt using Counter mode (CTR)

in CTR mode encryption and decryption is the same operation.

encryptXTS #

Arguments

:: Byteable iv |
=> (AES, AES) | AES cipher and tweak context
-> iv | a 128 bits IV, typically a sector or a block offset in XTS
-> Word32 | number of rounds to skip, also seen a 16 byte offset in the sector or block.
-> ByteString | input to encrypt
-> ByteString | output encrypted

encrypt using XTS

the first key is the normal block encryption key the second key is used for the initial block tweak

encryptGCM #

Arguments

:: Byteable iv |
=> AES | AES Context
-> iv | IV initial vector of any size
-> ByteString | data to authenticate (AAD)
-> ByteString | data to encrypt
-> (ByteString, AuthTag) | ciphertext and tag

encrypt using Galois counter mode (GCM) return the encrypted bytestring and the tag associated

note: encrypted data is identical to CTR mode in GCM, however a tag is also computed.

encryptOCB #

Arguments

:: Byteable iv |
=> AES | AES Context
-> iv | IV initial vector of any size
-> ByteString | data to authenticate (AAD)
-> ByteString | data to encrypt
-> (ByteString, AuthTag) | ciphertext and tag

encrypt using OCB v3 return the encrypted bytestring and the tag associated

# decryption

decryptECB :: AES -> ByteString -> ByteString #

decrypt using Electronic Code Book (ECB)

decryptCBC :: Byteable iv => AES -> iv -> ByteString -> ByteString #

decrypt using Cipher block chaining (CBC)

decryptCTR #

Arguments

:: Byteable iv |
=> AES | AES Context
-> iv | initial vector, usually representing a 128 bit integer
-> ByteString | ciphertext input
-> ByteString | plaintext output

decrypt using Counter mode (CTR).

in CTR mode encryption and decryption is the same operation.

decryptXTS #

Arguments

:: Byteable iv |
=> (AES, AES) | AES cipher and tweak context
-> iv | a 128 bits IV, typically a sector or a block offset in XTS
-> Word32 | number of rounds to skip, also seen a 16 byte offset in the sector or block.
-> ByteString | input to decrypt
-> ByteString | output decrypted

decrypt using XTS

decryptGCM #

Arguments

:: Byteable iv |
=> AES | Key
-> iv | IV initial vector of any size
-> ByteString | data to authenticate (AAD)
-> ByteString | data to decrypt
-> (ByteString, AuthTag) | plaintext and tag

decrypt using Galois Counter Mode (GCM)

decryptOCB #

Arguments

:: Byteable iv |
=> AES | Key
-> iv | IV initial vector of any size
-> ByteString | data to authenticate (AAD)
-> ByteString | data to decrypt
-> (ByteString, AuthTag) | plaintext and tag

decrypt using Offset Codebook Mode (OCB)