{-# LANGUAGE OverloadedStrings #-}module BlockCipher.AESSpec (spec) whereimport BlockCipherimport Control.Exception (evaluate)import qualified Crypto.Cipher.AES as AESimport Crypto.Cipher.Typesimport Crypto.Errorimport Crypto.Hash (Digest, SHA256, hash)import qualified Data.ByteArray as BAimport qualified Data.ByteArray.Encoding as BAEimport qualified Data.ByteString as Bimport qualified Data.ByteString.Char8 as B8import Data.Char (toLower)import Data.Maybeimport Importsimport qualified BlockCipher.AES.CBC as KATCBCimport qualified BlockCipher.AES.CCM as KATCCMimport qualified BlockCipher.AES.CTR as KATCTRimport qualified BlockCipher.AES.ECB as KATECBimport qualified BlockCipher.AES.GCM as KATGCMimport qualified BlockCipher.AES.GCMLong as KATGCMLongimport qualified BlockCipher.AES.OCB3 as KATOCB3import qualified BlockCipher.AES.XTS as KATXTSimport qualified Crypto.Cipher.AES.GCM as GCMimport Data.Bits (xor)import Foreign.Marshal.Alloc (allocaBytes)import Foreign.Ptr (castPtr){-instance Show AES.AES where show _ = "AES"instance Arbitrary AES.AESIV where arbitrary = AES.aesIV_ . B.pack <$> replicateM 16 arbitraryinstance Arbitrary AES.AES where arbitrary = AES.initAES . B.pack <$> replicateM 16 arbitrary-}toKatECB (k, p, c) = KAT_ECB{ecbKey = k, ecbPlaintext = p, ecbCiphertext = c}toKatCBC (k, iv, p, c) = KAT_CBC{cbcKey = k, cbcIV = iv, cbcPlaintext = p, cbcCiphertext = c}toKatCTR (k, iv, p, c) = KAT_CTR{ctrKey = k, ctrIV = iv, ctrPlaintext = p, ctrCiphertext = c}toKatXTS (k1, k2, iv, p, _, c) = KAT_XTS { xtsKey1 = k1 , xtsKey2 = k2 , xtsIV = iv , xtsPlaintext = p , xtsCiphertext = c }toKatAEAD mode (k, iv, h, p, c, taglen, tag) = KAT_AEAD { aeadMode = mode , aeadKey = k , aeadIV = iv , aeadHeader = h , aeadPlaintext = p , aeadCiphertext = c , aeadTaglen = taglen , aeadTag = tag }toKatGCM = toKatAEAD AEAD_GCMtoKatOCB = toKatAEAD AEAD_OCBtoKatCCM (k, iv, h, i, o, m) = KAT_AEAD { aeadMode = AEAD_CCM (B.length i) (ccmMVal m) CCM_L2 , aeadKey = k , aeadIV = iv , aeadHeader = h , aeadPlaintext = i , aeadCiphertext = ct , aeadTaglen = m , aeadTag = at } where ccmMVal x = fromMaybe (error $ "unsupported CCM tag length: " ++ show x) $ lookup x [ (4, CCM_M4) , (6, CCM_M6) , (8, CCM_M8) , (10, CCM_M10) , (12, CCM_M12) , (14, CCM_M14) , (16, CCM_M16) ] ctWithTag = B.drop (B.length h) o (ct, at) = B.splitAt (B.length ctWithTag - m) ctWithTagkats128 = defaultKATs { kat_ECB = map toKatECB KATECB.vectors_aes128_enc , kat_CBC = map toKatCBC KATCBC.vectors_aes128_enc , kat_CTR = map toKatCTR KATCTR.vectors_aes128_enc , kat_CFB = [ KAT_CFB { cfbKey = "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c" , cfbIV = "\xC8\xA6\x45\x37\xA0\xB3\xA9\x3F\xCD\xE3\xCD\xAD\x9F\x1C\xE5\x8B" , cfbPlaintext = "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef" , cfbCiphertext = "\x26\x75\x1f\x67\xa3\xcb\xb1\x40\xb1\x80\x8c\xf1\x87\xa4\xf4\xdf" } ] , kat_XTS = map toKatXTS KATXTS.vectors_aes128_enc , kat_AEAD = map toKatGCM KATGCM.vectors_aes128_enc ++ map toKatOCB KATOCB3.vectors_aes128_enc ++ map toKatCCM KATCCM.vectors_aes128_enc }kats192 = defaultKATs { kat_ECB = map toKatECB KATECB.vectors_aes192_enc , kat_CBC = map toKatCBC KATCBC.vectors_aes192_enc , kat_CTR = map toKatCTR KATCTR.vectors_aes192_enc , kat_AEAD = map toKatGCM KATGCM.vectors_aes192_enc ++ map toKatOCB KATOCB3.vectors_aes192_enc ++ map toKatCCM KATCCM.vectors_aes192_enc }kats256 = defaultKATs { kat_ECB = map toKatECB KATECB.vectors_aes256_enc , kat_CBC = map toKatCBC KATCBC.vectors_aes256_enc , kat_CTR = map toKatCTR KATCTR.vectors_aes256_enc , kat_XTS = map toKatXTS KATXTS.vectors_aes256_enc , kat_AEAD = map toKatGCM KATGCM.vectors_aes256_enc ++ map toKatOCB KATOCB3.vectors_aes256_enc ++ map toKatCCM KATCCM.vectors_aes256_enc }-- SP 800-38D 5.2.1.1: 1 <= len(IV) <= 2^64 - 1. A zero-length IV makes-- J0 the GHASH of the empty string, which leaks the authentication key.aeadIVLengthTests :: SpecaeadIVLengthTests = describe "AEAD IV length" $ do it "96-bit IV accepted" $ isRight (initWith (B.replicate 12 0)) `shouldBe` True it "8-bit IV accepted" $ isRight (initWith (B.replicate 1 0)) `shouldBe` True it "empty IV rejected" $ initWith B.empty `shouldBe` Left CryptoError_IvSizeInvalid where ctx = throwCryptoError (cipherInit (B.replicate 16 0)) :: AES.AES128 initWith iv = eitherCryptoError (() <$ aeadInit AEAD_GCM ctx (iv :: ByteString)) isRight = either (const False) (const True)-- RFC 7253: an OCB nonce is 1 to 15 bytes. The C keeps at most 15, so a-- longer IV used to be truncated, and IVs differing only after byte 15 gave-- the same keystream.ocbIVLengthTests :: SpecocbIVLengthTests = describe "OCB IV length" $ do it "1-byte IV accepted" $ ok (initWith (B.replicate 1 0)) `shouldBe` True it "15-byte IV accepted" $ ok (initWith (B.replicate 15 0)) `shouldBe` True it "empty IV rejected" $ initWith B.empty `shouldBe` Left CryptoError_IvSizeInvalid it "16-byte IV rejected" $ initWith (B.replicate 16 0) `shouldBe` Left CryptoError_IvSizeInvalid where ctx = throwCryptoError (cipherInit (B.replicate 16 0)) :: AES.AES128 initWith iv = eitherCryptoError (() <$ aeadInit AEAD_OCB ctx (iv :: ByteString)) ok = either (const False) (const True)-- RFC 7253 puts the tag length into the nonce, so a 12-byte OCB tag is not-- the first 12 bytes of the 16-byte one. Expected values: RFC 7253's-- TAGLEN=96 vector, and pycryptodome's OCB for the others.ocbTagLengthTests :: SpecocbTagLengthTests = describe "OCB tag length" $ do it "RFC 7253 TAGLEN 96" $ seal rfcKey rfcNonce rfcData rfcData 12 `shouldBe` hex "1792A4E31E0755FB03E31B22116E6C2DDF9EFD6E33D536F1A0124B0A55BAE884ED93481529C76B6AD0C515F4D1CDD4FDAC4F02AA" forM_ others $ \(t, expected) -> it (show t ++ "-byte tag") $ seal key nonce header message t `shouldBe` hex expected it "16 is AEAD_OCB" $ let (tag, ct) = aeadSimpleEncrypt (ocb AEAD_OCB) header message 16 in seal key nonce header message 16 `shouldBe` ct <> BA.convert tag it "opens its own" $ let sealed = seal key nonce header message 12 (ct, tag) = B.splitAt (B.length message) sealed in aeadSimpleDecrypt (ocb (AEAD_OCB_TAG 12)) header ct (AuthTag (BA.convert tag)) `shouldBe` Just message it "a tag of another length does not verify" $ let sealed = seal key nonce header message 12 (ct, tag) = B.splitAt (B.length message) sealed in aeadSimpleDecrypt (ocb (AEAD_OCB_TAG 12)) header ct (AuthTag (BA.convert (B.take 8 tag))) `shouldBe` (Nothing :: Maybe ByteString) it "refuses 0 and 17 bytes (a length in bits, such as 96, too)" $ forM_ [0, 17, 96] $ \t -> eitherCryptoError (() <$ aeadInit (AEAD_OCB_TAG t) aes (nonce :: ByteString)) `shouldBe` Left CryptoError_AuthenticationTagSizeInvalid where rfcKey = hex "0F0E0D0C0B0A09080706050403020100" rfcNonce = hex "BBAA9988776655443322110D" rfcData = B.pack [0 .. 39] key = B.pack [0 .. 15] nonce = B.pack [0 .. 11] header = "header" :: ByteString message = "authenticated message!" :: ByteString others = [ (8, "464F86B9EBAA4832A9F1C7CBBF5C556E344E45A3CCAA1DD7153FD0F81250") , (12, "1CA0A452FC72B7A30D9537C4AF253187804AA8D44D8B0211F1AAA720EBEBDC3CEF6D") , ( 16 , "8A0F0F603E4F9A6E7B566A7CB242CA8B9F9E9D0E5BB92444A51EB4EB4930783ED803927D2A0C" ) ] aes = throwCryptoError (cipherInit key) :: AES.AES128 ocb mode = throwCryptoError (aeadInit mode aes nonce) seal :: ByteString -> ByteString -> ByteString -> ByteString -> Int -> ByteString seal k n a p t = let c = throwCryptoError (cipherInit k) :: AES.AES128 st = throwCryptoError (aeadInit (AEAD_OCB_TAG t) c n) (tag, ct) = aeadSimpleEncrypt st a p t in ct <> BA.convert tag hex :: String -> ByteString hex = either error id . BAE.convertFromBase BAE.Base16 . B8.pack . map toLoweraeadTagLengthTests :: SpecaeadTagLengthTests = describe "AEAD tag length" $ do it "full tag verifies" $ openWith fullTag `shouldBe` Just message it "empty tag rejected" $ openWith B.empty `shouldBe` Nothing it "1-byte tag rejected" $ openWith (B.take 1 fullTag) `shouldBe` Nothing it "3-byte tag rejected" $ openWith (B.take 3 fullTag) `shouldBe` Nothing it "wrong tag rejected" $ openWith (B.map (+ 1) fullTag) `shouldBe` Nothing -- a truncated tag is still at or above the minimum, so the length -- taken from the tag is the peer's choice of how much to verify it "4-byte tag accepted, since the tag sets the length" $ openWith (B.take 4 fullTag) `shouldBe` Just message it "tryAeadSimpleDecrypt verifies the full tag" $ openWith' 16 fullTag `shouldBe` Just message it "tryAeadSimpleDecrypt refuses a truncated tag" $ openWith' 16 (B.take 4 fullTag) `shouldBe` Nothing it "tryAeadSimpleDecrypt refuses an overlong tag" $ openWith' 16 (fullTag `B.append` B.singleton 0) `shouldBe` Nothing it "tryAeadSimpleDecrypt refuses a length below the minimum" $ openWith' 3 (B.take 3 fullTag) `shouldBe` Nothing it "tryAeadSimpleDecrypt verifies a short tag the caller asked for" $ openWith' 8 (B.take 8 fullTag) `shouldBe` Just message it "tryAeadSimpleDecrypt refuses a wrong tag" $ openWith' 16 (B.map (+ 1) fullTag) `shouldBe` Nothing where key = B.replicate 16 0 iv = B.replicate 12 0 aad = "additional data" :: ByteString message = "authenticated message" :: ByteString ctx = throwCryptoError (cipherInit key) :: AES.AES128 aead = throwCryptoError (aeadInit AEAD_GCM ctx iv) (AuthTag tag, ciphertext) = aeadSimpleEncrypt aead aad message 16 fullTag = BA.convert tag :: ByteString openWith t = aeadSimpleDecrypt aead aad ciphertext (AuthTag (BA.convert t)) openWith' n t = tryAeadSimpleDecrypt aead aad ciphertext n (AuthTag (BA.convert t))-- A CCM context is set up for one message of a declared length. A message-- of another length used to leave the output unwritten and the tag covering-- nothing, so the tag of an empty message verified any ciphertext.ccmMisuseTests :: SpecccmMisuseTests = describe "CCM misuse" $ do it "round-trips a message of the declared length" $ open (ccm 21) ct tag `shouldBe` Just message it "refuses a ciphertext longer than declared" $ open (ccm 0) (B.replicate 32 0xab) emptyTag `shouldBe` Nothing it "refuses a ciphertext shorter than declared" $ open (ccm 32) (B.take 5 ct) tag `shouldBe` Nothing it "raises on encrypting a message of another length" $ evaluate (snd (aeadSimpleEncrypt (ccm 5) aad message 16)) `shouldThrow` anyErrorCall it "raises on encrypting after additional data given twice" $ evaluate (fst (aeadEncrypt (twice (ccm 21)) message)) `shouldThrow` anyErrorCall it "refuses to decrypt after additional data given twice" $ open' (twice (ccm 21)) ct tag `shouldBe` Nothing it "refuses a length that does not fit in L bytes" $ initCCM 65536 `shouldBe` Left CryptoError_ParameterInvalid it "refuses a negative length" $ initCCM (-1) `shouldBe` Left CryptoError_ParameterInvalid where ctx = throwCryptoError (cipherInit (B.replicate 16 0)) :: AES.AES128 nonce = B.replicate 13 7 ccm n = throwCryptoError (aeadInit (AEAD_CCM n CCM_M16 CCM_L2) ctx nonce) initCCM n = eitherCryptoError (() <$ aeadInit (AEAD_CCM n CCM_M16 CCM_L2) ctx nonce) aad = "header" :: ByteString message = "authenticated message" :: ByteString (tag, ct) = aeadSimpleEncrypt (ccm 21) aad message 16 (emptyTag, _) = aeadSimpleEncrypt (ccm 0) aad B.empty 16 :: (AuthTag, ByteString) open a c t = aeadSimpleDecrypt a aad (c :: ByteString) t :: Maybe ByteString twice a = aeadAppendHeader (aeadAppendHeader a aad) aad -- the header already appended, as aeadSimpleDecrypt would append it again open' a c (AuthTag t) = let (p, a') = aeadDecrypt a (c :: ByteString) AuthTag t' = aeadFinalize a' (BA.length t) in if BA.length t' == BA.length t && BA.constEq t t' then Just p else Nothing-- The bulk loops -- eight blocks at a time under AES-NI, six at a time in-- the assembly -- only start once the message is long enough to fill them,-- and what they leave over goes down a different path. These lengths sit-- either side of each of those boundaries, so a group that hashes the wrong-- blocks or a tail that is picked up at the wrong offset shows up here.gcmLongTests :: SpecgcmLongTests = describe "GCM long messages" $ mapM_ test KATGCMLong.vectors where test v@(klen, aadlen, ptlen, _, _) = it ( show klen ++ "-byte key, " ++ show aadlen ++ "-byte AAD, " ++ show ptlen ++ "-byte message" ) $ case klen of 16 -> run (undefined :: AES.AES128) v 24 -> run (undefined :: AES.AES192) v _ -> run (undefined :: AES.AES256) v run :: BlockCipher cipher => cipher -> KATGCMLong.KATGCMLong -> Expectation run cipherWitness (klen, aadlen, ptlen, tag, ctHash) = do BA.convert authTag `shouldBe` tag digest ciphertext `shouldBe` ctHash aeadSimpleDecrypt aead aad ciphertext authTag `shouldBe` Just plaintext where cipher = throwCryptoError (cipherInit (KATGCMLong.gcmKey klen)) `asTypeOf` cipherWitness aead = throwCryptoError (aeadInit AEAD_GCM cipher KATGCMLong.gcmIV) aad = KATGCMLong.gcmAAD aadlen plaintext = KATGCMLong.gcmPlaintext ptlen (authTag, ciphertext) = aeadSimpleEncrypt aead aad plaintext 16 digest bs = BA.convert (hash bs :: Digest SHA256) :: ByteString-- | Crypto.Cipher.AES.GCM builds the key part of the state once and does a-- whole message in one call. It has to answer exactly what the general-- interface answers, so it is run over the same vectors, and a tampered-- message has to come back as Nothing rather than as plaintext.oneShotTests :: SpeconeShotTests = describe "Crypto.Cipher.AES.GCM" $ do describe "agrees with the general interface" $ do run "AES-128" KATGCM.vectors_aes128_enc run "AES-192" KATGCM.vectors_aes192_enc run "AES-256" KATGCM.vectors_aes256_enc describe "decryptWithTag hands back the tag encrypt made" $ do runTag "AES-128" KATGCM.vectors_aes128_enc runTag "AES-192" KATGCM.vectors_aes192_enc runTag "AES-256" KATGCM.vectors_aes256_enc it "decryptWithTag gives a different tag for a tampered ciphertext" $ let ctx = ctx16 sealed = GCM.encrypt ctx iv16 B.empty message 16 :: B.ByteString body = B.take (B.length sealed - 16) sealed tag = AuthTag (BA.convert (B.drop (B.length sealed - 16) sealed)) (_, tag') = GCM.decryptWithTag ctx iv16 B.empty (flipFirst body) 16 :: (B.ByteString, AuthTag) in tag' `shouldSatisfy` (/= tag) describe "refuses a message that was interfered with" $ do it "a flipped bit in the tag" $ tamper (\(c, t) -> (c, flipFirst t)) it "a flipped bit in the ciphertext" $ tamper (\(c, t) -> (flipFirst c, t)) it "refuses input shorter than the tag" $ (GCM.decrypt ctx16 iv16 B.empty (B.replicate 8 0) 16 :: Maybe B.ByteString) `shouldBe` Nothing it "refuses a ciphertext with no authentication tag" $ let sealed = GCM.encrypt ctx16 iv16 header message 16 :: B.ByteString body = B.take (B.length sealed - 16) sealed in (GCM.decrypt ctx16 iv16 header body 0 :: Maybe B.ByteString) `shouldBe` Nothing it "does not authenticate an altered ciphertext with a zero-length tag" $ let sealed = GCM.encrypt ctx16 iv16 header message 16 :: B.ByteString body = B.take (B.length sealed - 16) sealed in (GCM.decrypt ctx16 iv16 header (flipFirst body) 0 :: Maybe B.ByteString) `shouldBe` Nothing -- Shorter than four bytes the tag authenticates next to nothing, and -- longer than sixteen there is no more tag for GCM to give. describe "refuses a tag length outside 4 to 16 bytes" $ forM_ [0, 3, 17 :: Int] $ \taglen -> describe (show taglen) $ do it "encrypt" $ evaluate (GCM.encrypt ctx16 iv16 header message taglen :: B.ByteString) `shouldThrow` (== CryptoError_AuthenticationTagSizeInvalid) it "decrypt" $ (GCM.decrypt ctx16 iv16 header plainSealed taglen :: Maybe B.ByteString) `shouldBe` Nothing it "decryptWithTag" $ evaluate ( GCM.decryptWithTag ctx16 iv16 header message taglen :: (B.ByteString, AuthTag) ) `shouldThrow` (== CryptoError_AuthenticationTagSizeInvalid) it "encryptWithMask" $ withMaskTag taglen 0 `shouldReturn` Nothing -- SP 800-38D 5.2.1.1 wants at least one byte of IV. With none, GCM's -- pre-counter block is zero and the tag of a message is -- GHASH_H(A, C) XOR E(K, 0^128) -- and E(K, 0^128) is the GHASH key H -- itself. One full tag therefore gives H away, H belongs to the key -- rather than to the nonce, and with it a tag can be forged for any -- message under any nonce the key has been used with, twelve-byte ones -- included. The general interface has refused the empty IV since -- f98cff3 and these four did not. describe "refuses a nonce of no bytes" $ do it "encrypt" $ evaluate (GCM.encrypt ctx16 B.empty header message 16 :: B.ByteString) `shouldThrow` (== CryptoError_IvSizeInvalid) -- a message that really is sealed under the empty nonce, so that this -- says something whether or not `encrypt` refuses one: before the -- check, `decrypt` returned the sixteen bytes of plaintext for it it "decrypt, on a message that is genuine under it" $ (GCM.decrypt ctx16 B.empty header emptyNonceSealed 16 :: Maybe B.ByteString) `shouldBe` Nothing it "decryptWithTag" $ evaluate ( GCM.decryptWithTag ctx16 B.empty header message 16 :: (B.ByteString, AuthTag) ) `shouldThrow` (== CryptoError_IvSizeInvalid) it "encryptWithMask, writing nothing" $ withMaskIv B.empty 16 4 `shouldReturn` Nothing -- and only the empty one: SP 800-38D allows every length from one byte up, -- so this must not become a check for twelve it "takes a nonce of one byte" $ let iv1 = B.singleton 0x77 sealed = GCM.encrypt ctx16 iv1 header message 16 :: B.ByteString in (GCM.decrypt ctx16 iv1 header sealed 16 :: Maybe B.ByteString) `shouldBe` Just message describe "header protection" $ do it "writes the ciphertext encrypt gives" $ withMask 4 `shouldReturn` Just (plainSealed, expectedMask 4) it "and at another offset" $ withMask 0 `shouldReturn` Just (plainSealed, expectedMask 0) it "refuses a sample that does not fit, writing nothing" $ do withMask (B.length plainSealed - 15) `shouldReturn` Nothing withMask (-1) `shouldReturn` Nothing -- off + 16 used to wrap and pass, and the C read 4 GiB away it "refuses an offset near maxBound" $ withMask (maxBound - 7) `shouldReturn` Nothing where run name vs = it name $ [ (key, iv) | (key, iv, aad, input, out, taglen, tag) <- vs , let ctx = throwCryptoError (GCM.newContext key) , let sealed = GCM.encrypt ctx iv aad input taglen :: B.ByteString , sealed /= out `B.append` tag || GCM.decrypt ctx iv aad sealed taglen /= Just input ] `shouldBe` [] -- The tag decryptWithTag computes has to be the one encrypt appended, and -- the body it returns the one decrypt returns, over the same vectors. runTag name vs = it name $ [ (key, iv) | (key, iv, aad, input, out, taglen, tag) <- vs , let ctx = throwCryptoError (GCM.newContext key) , let (body, tag') = GCM.decryptWithTag ctx iv aad out taglen :: (B.ByteString, AuthTag) , body /= input || tag' /= AuthTag (BA.convert tag) ] `shouldBe` [] ctx16 = throwCryptoError (GCM.newContext (B.replicate 16 0x2b)) iv16 = B.replicate 12 0x77 -- header protection keeps a key of its own, as QUIC does hpKeyBytes = B.replicate 16 0x9c hpKey = throwCryptoError (GCM.newHeaderKey hpKeyBytes) hpAes = throwCryptoError (cipherInit hpKeyBytes) :: AES.AES128 message = "a packet payload" :: B.ByteString header = "\x40\x01\x02\x03" :: B.ByteString plainSealed = GCM.encrypt ctx16 iv16 header message 16 :: B.ByteString -- ctx16, no nonce at all, `header` as the additional data and `message` -- as the plaintext, taken from the tree before the check went in emptyNonceSealed = "\x09\x25\xea\x53\x9b\x98\xf1\x0e\x02\x5f\x8a\x70\x8d\xf4\x6d\xb2\xf2\xce\x43\x31\x67\x12\x32\xef\x29\xcc\x69\x52\x12\x98\xd1\xf3" :: B.ByteString -- the buffers the caller owns, as a packet writer would have them withMask = withMaskTag 16 withMaskTag = withMaskIv iv16 withMaskIv iv taglen off = allocaBytes (B.length message + taglen) $ \outp -> allocaBytes 16 $ \maskp -> do ok <- GCM.encryptWithMask ctx16 hpKey iv header message taglen off outp maskp if ok then do sealed <- B.packCStringLen (castPtr outp, B.length message + taglen) mask <- B.packCStringLen (castPtr maskp, 16) return (Just (sealed, mask)) else return Nothing expectedMask off = ecbEncrypt hpAes (B.take 16 (B.drop off plainSealed)) flipFirst b = B.cons (B.head b `xor` 1) (B.tail b) tamper f = let sealed = GCM.encrypt ctx16 iv16 B.empty ("hello there" :: B.ByteString) 16 (c, t) = B.splitAt (B.length sealed - 16) sealed (c', t') = f (c, t) in (GCM.decrypt ctx16 iv16 B.empty (c' `B.append` t') 16 :: Maybe B.ByteString) `shouldBe` Nothing-- OCB fed in chunks must equal one call: each call used to restart the-- block index, so equal-sized chunks could be swapped under a valid tagocbChunkTests :: SpecocbChunkTests = describe "OCB in chunks" $ do it "equals one call" $ forM_ splits $ \(as, ms) -> enc (chunks as aad) (chunks ms msg) `shouldBe` enc [aad] [msg] it "does not accept swapped chunks" $ do let three = B.replicate 48 0x5a (cs, tag) = enc [] (chunks [16, 16] three) swapped = B.concat [B.drop 32 cs, B.take 16 (B.drop 16 cs), B.take 16 cs] snd (dec (chunks [16, 16] swapped)) `shouldNotBe` tag where ctx = throwCryptoError (cipherInit (B.pack [0 .. 15])) :: AES.AES128 start = throwCryptoError (aeadInit AEAD_OCB ctx (B.pack [0 .. 11])) aad = B.pack (take 77 [100 ..]) msg = B.pack (take 300 [1 ..]) -- block-aligned chunks with a partial one last, below and above the -- eight blocks the C processes in a group splits = [([16], [16]), ([16, 32], [16, 16, 16]), ([64], [160, 32]), ([], [256])] chunks [] bs = [bs] chunks (n : ns) bs = let (h, t) = B.splitAt n bs in h : chunks ns t run step as ms = let a0 = foldl' aeadAppendHeader start as go a [] = ([], a) go a (m : rest) = let (c, a') = step a m (cs, a'') = go a' rest in (c : cs, a'') (outs, a1) = go a0 ms in (B.concat outs, BA.convert (aeadFinalize a1 16) :: ByteString) enc = run aeadEncrypt dec = run aeadDecrypt ([] :: [ByteString])spec :: Specspec = do testBlockCipher128 kats128 (undefined :: AES.AES128) testBlockCipher128 kats192 (undefined :: AES.AES192) testBlockCipher128 kats256 (undefined :: AES.AES256) aeadIVLengthTests aeadTagLengthTests ccmMisuseTests ocbIVLengthTests ocbTagLengthTests ocbChunkTests gcmLongTests oneShotTests