crypto-cipher-tests 0.0.2 → 0.0.3
raw patch · 3 files changed
+42/−25 lines, 3 filesdep ~crypto-cipher-typesPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: crypto-cipher-types
API changes (from Hackage documentation)
Files
- Crypto/Cipher/Tests/KATs.hs +23/−17
- Crypto/Cipher/Tests/Properties.hs +16/−5
- crypto-cipher-tests.cabal +3/−3
Crypto/Cipher/Tests/KATs.hs view
@@ -11,34 +11,34 @@ -- | ECB KAT data KAT_ECB = KAT_ECB- { ecbKey :: ByteString- , ecbPlaintext :: ByteString- , ecbCiphertext :: ByteString+ { ecbKey :: ByteString -- ^ Key+ , ecbPlaintext :: ByteString -- ^ Plaintext+ , ecbCiphertext :: ByteString -- ^ Ciphertext } deriving (Show,Eq) -- | CBC KAT data KAT_CBC = KAT_CBC- { cbcKey :: ByteString- , cbcIV :: ByteString- , cbcPlaintext :: ByteString- , cbcCiphertext :: ByteString+ { cbcKey :: ByteString -- ^ Key+ , cbcIV :: ByteString -- ^ IV+ , cbcPlaintext :: ByteString -- ^ Plaintext+ , cbcCiphertext :: ByteString -- ^ Ciphertext } deriving (Show,Eq) -- | CTR KAT data KAT_CTR = KAT_CTR- { ctrKey :: ByteString- , ctrIV :: ByteString- , ctrPlaintext :: ByteString- , ctrCiphertext :: ByteString+ { ctrKey :: ByteString -- ^ Key+ , ctrIV :: ByteString -- ^ IV (usually represented as a 128 bits integer)+ , ctrPlaintext :: ByteString -- ^ Plaintext + , ctrCiphertext :: ByteString -- ^ Ciphertext } deriving (Show,Eq) -- | XTS KAT data KAT_XTS = KAT_XTS- { xtsKey1 :: ByteString- , xtsKey2 :: ByteString- , xtsIV :: ByteString- , xtsPlaintext :: ByteString- , xtsCiphertext :: ByteString+ { xtsKey1 :: ByteString -- ^ 1st XTS key+ , xtsKey2 :: ByteString -- ^ 2nd XTS key+ , xtsIV :: ByteString -- ^ XTS IV+ , xtsPlaintext :: ByteString -- ^ plaintext+ , xtsCiphertext :: ByteString -- ^ Ciphertext } deriving (Show,Eq) -- | AEAD KAT@@ -53,6 +53,8 @@ , aeadTag :: AuthTag -- ^ expected tag } deriving (Show,Eq) +-- | all the KATs. use defaultKATs to prevent compilation error+-- from future expansion of this data structure data KATs = KATs { kat_ECB :: [KAT_ECB] , kat_CBC :: [KAT_CBC]@@ -61,6 +63,7 @@ , kat_AEAD :: [KAT_AEAD] } deriving (Show,Eq) +-- | the empty KATs defaultKATs :: KATs defaultKATs = KATs { kat_ECB = []@@ -70,6 +73,7 @@ , kat_AEAD = [] } +-- | tests related to KATs testKATs :: BlockCipher cipher => KATs -> cipher -> Test testKATs kats cipher = testGroup "KAT" ( maybeGroup makeECBTest "ECB" (kat_ECB kats)@@ -124,7 +128,9 @@ nbs = [0..] cipherMakeKey :: BlockCipher cipher => cipher -> ByteString -> Key cipher- cipherMakeKey _ bs = fromJust $ makeKey bs+ cipherMakeKey c bs = case makeKey bs of+ Left e -> error ("invalid key " ++ show bs ++ " for " ++ show (cipherName c) ++ " " ++ show e)+ Right k -> k cipherMakeIV :: BlockCipher cipher => cipher -> ByteString -> IV cipher cipherMakeIV _ bs = fromJust $ makeIV bs
Crypto/Cipher/Tests/Properties.hs view
@@ -45,21 +45,29 @@ instance Show (AEADUnit a) where show (AEADUnit key iv aad b) = "AEAD(key=" ++ show (toBytes key) ++ ",iv=" ++ show iv ++ ",aad=" ++ show (toBytes aad) ++ ",input=" ++ show b ++ ")" +-- | Generate an arbitrary valid key for a specific block cipher generateKey :: BlockCipher a => Gen (Key a) generateKey = keyFromCipher undefined where keyFromCipher :: BlockCipher a => a -> Gen (Key a)- keyFromCipher cipher = case cipherKeySize cipher of- Just sz -> fromJust . makeKey . B.pack <$> replicateM sz arbitrary- Nothing -> fromJust . makeKey . B.pack <$> (choose (1,66) >>= \sz -> replicateM sz arbitrary)+ keyFromCipher cipher = do+ sz <- case cipherKeySize cipher of+ KeySizeRange low high -> choose (low, high)+ KeySizeFixed v -> return v+ KeySizeEnum l -> elements l+ either (error . show) id . makeKey . B.pack <$> replicateM sz arbitrary +-- | Generate an arbitrary valid IV for a specific block cipher generateIv :: BlockCipher a => Gen (IV a) generateIv = ivFromCipher undefined where ivFromCipher :: BlockCipher a => a -> Gen (IV a) ivFromCipher cipher = fromJust . makeIV . B.pack <$> replicateM (blockSize cipher) arbitrary +-- | Generate an arbitrary valid IV for AEAD for a specific block cipher generateIvAEAD :: Gen B.ByteString generateIvAEAD = choose (12,90) >>= \sz -> (B.pack <$> replicateM sz arbitrary) +-- | Generate a plaintext multiple of 16 bytes. TODO replace by one function that use the blockSize+-- cipher instance generatePlaintextMultiple16 :: Gen B.ByteString generatePlaintextMultiple16 = choose (1,128) >>= \size -> replicateM (size*16) arbitrary >>= return . B.pack @@ -92,6 +100,8 @@ <*> generatePlaintext <*> generatePlaintext +-- | Test a generic block cipher for properties+-- related to block cipher modes. testModes :: BlockCipher a => a -> [Test] testModes cipher = [ testGroup "decrypt.encrypt==id"@@ -125,8 +135,9 @@ testProperty_CTR (CTRUnit (cipherInit -> ctx) testIV plaintext) = plaintext `assertEq` ctrCombine ctx testIV (ctrCombine ctx testIV plaintext) - testProperty_XTS (XTSUnit (cipherInit -> ctx1) (cipherInit -> ctx2) testIV plaintext) =- plaintext `assertEq` xtsDecrypt (ctx1, ctx2) testIV 0 (xtsEncrypt (ctx1, ctx2) testIV 0 plaintext)+ testProperty_XTS (XTSUnit (cipherInit -> ctx1) (cipherInit -> ctx2) testIV plaintext)+ | blockSize ctx1 == 16 = plaintext `assertEq` xtsDecrypt (ctx1, ctx2) testIV 0 (xtsEncrypt (ctx1, ctx2) testIV 0 plaintext)+ | otherwise = True testProperty_AEAD mode (AEADUnit (cipherInit -> ctx) testIV aad plaintext) = case aeadInit mode ctx testIV of
crypto-cipher-tests.cabal view
@@ -1,5 +1,5 @@ Name: crypto-cipher-tests-Version: 0.0.2+Version: 0.0.3 Synopsis: Generic cryptography cipher tests Description: Generic cryptography cipher tests License: BSD3@@ -27,7 +27,7 @@ , bytestring , byteable >= 0.1.1 , securemem >= 0.1.1- , crypto-cipher-types+ , crypto-cipher-types >= 0.0.3 ghc-options: -Wall -fwarn-tabs Test-Suite test-crypto-cipher-dummy@@ -37,7 +37,7 @@ Build-Depends: base >= 3 && < 5 , bytestring , byteable- , crypto-cipher-types+ , crypto-cipher-types >= 0.0.3 , crypto-cipher-tests , mtl , QuickCheck >= 2