Permalink
Browse files

Improve AES KATs support. Update Cabal

Ignore-this: 66c99030535b56ed80ce8f48ce392788

darcs-hash:20100813142741-cef97-142e5d8dd7c849f5248c114f4a8a8beef0a117ff.gz
  • Loading branch information...
1 parent 4c373d2 commit 1c06c59035d697e2d1242f9e4485c2faf8994151 @TomMD committed Aug 13, 2010
Showing with 66 additions and 24 deletions.
  1. +38 −5 Test/Crypto.hs
  2. +28 −19 crypto-api.cabal
View
@@ -23,6 +23,7 @@ module Test.Crypto
, parseProperty
, parseRecord
, parseCount
+ , getAES_KATs
) where
import Test.QuickCheck
@@ -31,7 +32,7 @@ import Data.Crypto.Modes
import qualified Data.ByteString.Lazy.Char8 as LC
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString as B
-import Control.Monad (forM)
+import Control.Monad (forM, liftM)
import Data.Word (Word8)
import Data.Either (rights)
import Data.Maybe (maybeToList)
@@ -42,6 +43,7 @@ import Data.Maybe (fromJust)
import Text.Parsec
import Text.Parsec.ByteString
import System.Directory (getDirectoryContents)
+import Paths_crypto_api
instance Arbitrary Word8 where
arbitrary = (arbitrary :: Gen Int) >>= return . fromIntegral
@@ -109,7 +111,9 @@ makeHashPropTests d =
, T (prop_OutputLengthIsByteAligned d) "prop_OuputLengthIsByteAligned"
]
-makeBlockCipherTests = []
+-- |FIXME make some generic blockcipher tests
+makeBlockCipherPropTests :: BlockCipher k => k -> [Test]
+makeBlockCipherPropTests _ = []
data KAT i o = K i (i -> o) o
@@ -122,6 +126,7 @@ runKATs = all goodKAT
toD :: Hash c d => d -> String -> d
toD d str = (Bin.decode $ L.fromChunks [hexStringToBS str]) `asTypeOf` d
+hexStringToBS :: String -> B.ByteString
hexStringToBS [] = B.empty
hexStringToBS (_:[]) = error "Not an even number of hex characters in alledged 'digest'"
hexStringToBS (a:b:xs) = B.cons (rHex (a:b:[])) (hexStringToBS xs)
@@ -133,7 +138,8 @@ cogStr = "The quick brown fox jumps over the lazy cog"
getAES_KATs :: BlockCipher k => k -> IO [KAT B.ByteString B.ByteString]
getAES_KATs k = do
- files <- getDirectoryContents "KAT_AES"
+ dataDir <- getDataFileName ""
+ files <- getDirectoryContents dataDir
recEs <- mapM (parseFromFile parseKATs) files
let recs = map snd (rights recEs)
testTypes = map getTestSig files :: [String]
@@ -169,8 +175,30 @@ nistTestToKAT_AES eK ("ECBd", tests) =
let realKey = (fromJust . buildKey . hexStringToBS $ k) `asTypeOf` eK
return (K (hexStringToBS ct) (decryptBlock realKey) (hexStringToBS pt))
-nistTestToKAT_AES ek ("CBCe", tests) = undefined
-nistTestToKAT_AES ek ("CBCd", tests) = undefined
+nistTestToKAT_AES ek ("CBCe", tests) =
+ let ks = map testToKAT tests
+ in concatMap maybeToList ks
+ where
+ testToKAT :: NistTest -> Maybe (KAT B.ByteString B.ByteString)
+ testToKAT t = do
+ ct <- lookup "CIPHERTEXT" t
+ pt <- lookup "PLAINTEXT" t
+ k <- lookup "KEY" t
+ Right iv <- liftM (Ser.decode . hexStringToBS) (lookup "IV" t)
+ let realKey = (fromJust . buildKey . hexStringToBS $ k) `asTypeOf` ek
+ return (K (hexStringToBS pt) (fst . cbc' realKey iv) (hexStringToBS ct))
+
+nistTestToKAT_AES ek ("CBCd", tests) =
+ let ks = map testToKAT tests
+ in concatMap maybeToList ks
+ where
+ testToKAT t = do
+ ct <- lookup "CIPHERTEXT" t
+ pt <- lookup "PLAINTEXT" t
+ k <- lookup "KEY" t
+ (Right iv) <- liftM (Ser.decode . hexStringToBS) (lookup "IV" t)
+ let realKey = (fromJust . buildKey . hexStringToBS $ k) `asTypeOf` ek
+ return (K (hexStringToBS ct) (fst . unCbc' realKey iv) (hexStringToBS pt))
nistTestToKAT_AES eK _ = []
type Properties = [(String, String)]
@@ -298,6 +326,11 @@ makeSHA384Tests = makeHashTests "sha384" sha384KATs
makeSHA512Tests :: Hash c d => d -> [Test]
makeSHA512Tests = makeHashTests "sha512" sha512KATs
+makeAESTests :: BlockCipher k => k -> IO [Test]
+makeAESTests k = do
+ kats <- getAES_KATs k
+ return (T (runKATs kats) "AES-KATs" : makeBlockCipherPropTests k)
+
-- |Run a single test
runTest :: Test -> IO ()
runTest (T a s) = do
View
@@ -1,21 +1,21 @@
-name: crypto-api
-version: 0.0.0.1
-license: BSD3
-license-file: LICENSE
-author: Thomas DuBuisson <thomas.dubuisson@gmail.com>, Dominic Steinitz
-maintainer: Thomas DuBuisson <thomas.dubuisson@gmail.com>
-description: A generic interface for cryptographic operations.
- Maintainers of hash and cipher implementations are
- encouraged to add instances for the classes defined
- in Data.Crypto.Classes. Crypto users are similarly
- encoraged to use the interfaces defined in the Classes
- module.
-synopsis: A generic interface for cryptographic operations
-category: Data, Cryptography
-stability: stable
-build-type: Simple
-cabal-version: >= 1.6
-tested-with: GHC == 6.12.1
+name: crypto-api
+version: 0.0.0.1
+license: BSD3
+license-file: LICENSE
+author: Thomas DuBuisson <thomas.dubuisson@gmail.com>, Dominic Steinitz
+maintainer: Thomas DuBuisson <thomas.dubuisson@gmail.com>
+description: A generic interface for cryptographic operations.
+ Maintainers of hash and cipher implementations are
+ encouraged to add instances for the classes defined
+ in Data.Crypto.Classes. Crypto users are similarly
+ encoraged to use the interfaces defined in the Classes
+ module.
+synopsis: A generic interface for cryptographic operations
+category: Data, Cryptography
+stability: stable
+build-type: Simple
+cabal-version: >= 1.6
+tested-with: GHC == 6.12.1
extra-source-files:
flag test
@@ -26,19 +26,28 @@ flag alg-tests
description: Include Test.Crypto module for testing hash and cipher instances
default: True
+flag benchmarks
+ description: Include benchmarking tools for cryptographic operations
+ default: False
+
Library
Build-Depends: base == 4.*,
bytestring >= 0.9 && < 0.10,
binary >= 0.4.0 && <0.6.0,
cereal >= 0.2 && <0.3,
tagged,
random
- ghc-options: -O2 -funfolding-use-threshold66 -funfolding-creation-threshold66 -fexcess-precision -funbox-strict-fields
+ ghc-options: -O2 -funfolding-use-threshold66 -funfolding-creation-threshold66 -fexcess-precision -funbox-strict-fields
hs-source-dirs:
exposed-modules: Data.Crypto.Classes, Data.Crypto.Types, Data.Crypto.HMAC, Data.LargeWord, Data.Crypto.Modes
if flag(alg-tests)
exposed-modules: Test.Crypto
build-depends: QuickCheck >= 2.0, parsec >= 3.1, directory >= 1.0.1.0
+ data-files: Test/KAT_AES/*.txt
+ if flag(benchmarks)
+ exposed-modes: Benchmark.Crypto
+ build-depends: criterion >= 0.5
+
Executable test
main-is: Test/Main.hs

0 comments on commit 1c06c59

Please sign in to comment.