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
|
module CiphersSpec where
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Network.TLS.Cipher
import Network.TLS.Extra.Cipher
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck
spec :: Spec
spec = do
describe "ciphers" $ do
prop "can ecnrypt/decrypt" $ \(BulkTest bulk key iv t additional) -> do
let enc = bulkInit bulk BulkEncrypt key
dec = bulkInit bulk BulkDecrypt key
case (enc, dec) of
(BulkStateBlock encF, BulkStateBlock decF) -> block encF decF iv t
(BulkStateAEAD encF, BulkStateAEAD decF) -> aead encF decF iv t additional
(BulkStateStream (BulkStream encF), BulkStateStream (BulkStream decF)) -> stream encF decF t
_ -> return ()
block
:: BulkBlock
-> BulkBlock
-> BulkIV
-> ByteString
-> IO ()
block e d iv t = do
let (etxt, e_iv) = e iv t
(dtxt, d_iv) = d iv etxt
dtxt `shouldBe` t
d_iv `shouldBe` e_iv
stream
:: (ByteString -> (ByteString, BulkStream))
-> (ByteString -> (ByteString, BulkStream))
-> ByteString
-> Expectation
stream e d t = (fst . d . fst . e) t `shouldBe` t
aead
:: BulkAEAD
-> BulkAEAD
-> BulkNonce
-> ByteString
-> BulkAdditionalData
-> Expectation
aead e d iv t additional = do
let (encrypted, at) = e iv t additional
(decrypted, at2) = d iv encrypted additional
decrypted `shouldBe` t
at `shouldBe` at2
arbitraryKey :: Bulk -> Gen B.ByteString
arbitraryKey bulk = B.pack `fmap` vector (bulkKeySize bulk)
arbitraryIV :: Bulk -> Gen B.ByteString
arbitraryIV bulk = B.pack `fmap` vector (bulkIVSize bulk + bulkExplicitIV bulk)
arbitraryText :: Bulk -> Gen B.ByteString
arbitraryText bulk = B.pack `fmap` vector (bulkBlockSize bulk)
data BulkTest = BulkTest Bulk B.ByteString B.ByteString B.ByteString B.ByteString
deriving (Show, Eq)
instance Arbitrary BulkTest where
arbitrary = do
bulk <- cipherBulk `fmap` elements ciphersuite_all
BulkTest bulk
<$> arbitraryKey bulk
<*> arbitraryIV bulk
<*> arbitraryText bulk
<*> arbitraryText bulk
|