File: AES.hs

package info (click to toggle)
haskell-cryptocipher 0.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 256 kB
  • sloc: haskell: 2,916; ansic: 142; makefile: 3
file content (75 lines) | stat: -rw-r--r-- 2,323 bytes parent folder | download
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
63
64
65
66
67
68
69
70
71
72
73
74
75
{-# LANGUAGE CPP #-}
module AES (aesTests) where

-- unfortunately due to a bug in some version of cabal
-- there's no way to have a condition cpp-options in the cabal file
-- for test suite. to run test with AESni, uncomment the following
-- #define HAVE_AESNI

import qualified Crypto.Cipher.AES.Haskell as AESHs
#ifdef HAVE_AESNI
import qualified Crypto.Cipher.AES.X86NI as AESNI
#endif

import Crypto.Classes
import qualified Crypto.Modes as CAPI

import Data.Word
import Data.List
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Control.Monad
import Control.Applicative
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.QuickCheck

newtype IV = IV ByteString
    deriving (Show,Eq)
newtype Key128 = Key128 ByteString
    deriving (Show,Eq)
newtype Message = Message ByteString
    deriving (Show,Eq)

arbitraryBS sz = B.pack <$> replicateM sz (choose (0,255) :: Gen Word8)

instance Arbitrary IV where
    arbitrary = IV <$> arbitraryBS 16

instance Arbitrary Key128 where
    arbitrary = Key128 <$> arbitraryBS 16

instance Arbitrary Message where
    arbitrary = choose (1,102) >>= \sz -> Message <$> arbitraryBS (16*sz)

ebcTests l (Key128 k, Message m) = (== 1) $ length $ nub $ map (\f -> f k m) l
cbcTests l (IV iv, Key128 k, Message m) = (== 1) $ length $ nub $ map (\f -> f k iv m) l

unright (Right r) = r
unright (Left e) = error e

aesTests =
    [ testProperty "ECB Encryption Equivalent" $ ebcTests
        [ (\k m -> AESHs.encrypt (unright $ AESHs.initKey128 k) m)
#ifdef HAVE_AESNI
        , (\k m -> AESNI.encrypt (AESNI.initKey128 k) m)
#endif
        ]
    , testProperty "CBC Encryption Equivalent" $ cbcTests
        [ (\k iv m -> AESHs.encryptCBC (unright $ AESHs.initKey128 k) iv m)
#ifdef HAVE_AESNI
        , (\k iv m -> AESNI.encryptCBC (AESNI.initKey128 k) iv m)
#endif
        ]
    , testProperty "ECB Decryption Equivalent" $ ebcTests
        [ (\k m -> AESHs.decrypt (unright $ AESHs.initKey128 k) m)
#ifdef HAVE_AESNI
        , (\k m -> AESNI.decrypt (AESNI.initKey128 k) m)
#endif
        ]
    , testProperty "CBC Decryption Equivalent" $ cbcTests
        [ (\k iv m -> AESHs.decryptCBC (unright $ AESHs.initKey128 k) iv m)
#ifdef HAVE_AESNI
        , (\k iv m -> AESNI.decryptCBC (AESNI.initKey128 k) iv m)
#endif
        ]
    ]