From 18cf6a5392345e72ff907320509efe03307e75e3 Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Tue, 7 Dec 2010 09:15:34 +0000 Subject: [PATCH] test ciphers in a basic fashion for now. --- Tests.hs | 2 ++ Tests/Ciphers.hs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 Tests/Ciphers.hs diff --git a/Tests.hs b/Tests.hs index 7f9a30f..df79fa3 100644 --- a/Tests.hs +++ b/Tests.hs @@ -2,7 +2,9 @@ import qualified Tests.Marshal as Marshal import qualified Tests.Connection as Connection +import qualified Tests.Ciphers as Ciphers main = do Marshal.runTests + Ciphers.runTests Connection.runTests diff --git a/Tests/Ciphers.hs b/Tests/Ciphers.hs new file mode 100644 index 0000000..afb2f10 --- /dev/null +++ b/Tests/Ciphers.hs @@ -0,0 +1,40 @@ +module Tests.Ciphers + ( runTests + ) where + +import Data.Word +import Control.Applicative ((<$>)) + +import Tests.Common +import Test.QuickCheck + +import qualified Data.ByteString as B +import Network.TLS.Cipher + +arbitraryKey :: Cipher -> Gen [Word8] +arbitraryKey cipher = vector (fromIntegral $ cipherKeySize cipher) + +arbitraryIV :: Cipher -> Gen [Word8] +arbitraryIV cipher = vector (fromIntegral $ cipherIVSize cipher) + +arbitraryText :: Cipher -> Gen [Word8] +arbitraryText cipher = vector (fromIntegral $ cipherPaddingSize cipher) + +cipher_test cipher = run_test n t + where + n = ("cipher: " ++ cipherName cipher ++ ": decrypt . encrypt = id") + t = case cipherF cipher of + CipherBlockF enc dec -> do + key <- B.pack <$> arbitraryKey cipher + iv <- B.pack <$> arbitraryIV cipher + t <- B.pack <$> arbitraryText cipher + return $ block enc dec key iv t + CipherStreamF ktoi enc dec -> do + key <- B.pack <$> arbitraryKey cipher + t <- B.pack <$> arbitraryText cipher + return $ stream ktoi enc dec key t + block e d key iv t = (d key iv . e key iv) t == t + stream ktoi e d key t = (fst . d iv . fst . e iv) t == t + where iv = ktoi key + +runTests = mapM_ cipher_test supportedCiphers