1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
-----------------------------------------------------------------------------
-- |
-- Module : Codec.Encryption.AES
-- Copyright : (c) Dominic Steinitz 2004
-- License : BSD-style (see the file ReadMe.tex)
--
-- Maintainer : dominic.steinitz@blueyonder.co.uk
-- Stability : experimental
-- Portability : portable
--
-- Takes the AES module supplied by Lukasz Anforowicz and wraps it so it can
-- used with the standard modes.
--
-----------------------------------------------------------------------------
module Codec.Encryption.AES (
-- * Function Types
encrypt, decrypt, AESKey) where
import Codec.Encryption.AESAux
import Data.LargeWord
import Codec.Utils
import Data.Word
import Data.Bits
class (Bits a, Integral a) => AESKeyIndirection a
class AESKeyIndirection a => AESKey a
instance AESKeyIndirection Word128
instance AESKeyIndirection Word192
instance AESKeyIndirection Word256
instance AESKey Word128
instance AESKey Word192
instance AESKey Word256
-- | Basic AES encryption which takes a key and a block of plaintext
-- and returns the encrypted block of ciphertext according to the standard.
encrypt :: AESKey a => a -> Word128 -> Word128
encrypt k p =
case bitSize k of
128 -> f aes128Encrypt k p
192 -> f aes192Encrypt k p
256 -> f aes256Encrypt k p
f g k p =
fromIntegral $ fromOctets 256 $
g (i2osp (bitSize k `div` bitSize (0::Octet)) $ fromIntegral k)
(i2osp (bitSize p `div` bitSize (0::Octet)) $ fromIntegral p)
-- | Basic AES decryption which takes a key and a block of ciphertext and
-- returns the decrypted block of plaintext according to the standard.
decrypt :: AESKey a => a -> Word128 -> Word128
decrypt k p =
case bitSize k of
128 -> f aes128Decrypt k p
192 -> f aes192Decrypt k p
256 -> f aes256Decrypt k p
|