Tools.hs (596B)
1 module Cryptopals.Block.Tools ( 2 Mode(..) 3 4 , detectMode 5 ) where 6 7 import qualified Data.ByteString as BS 8 import qualified Data.Set as S 9 10 data Mode = 11 ECB 12 | CBC 13 deriving (Eq, Show) 14 15 -- Assuming the ciphertext could only have been produced by AES 16 -- operating in ECB or CBC mode, guess the mode that was used. 17 detectMode :: BS.ByteString -> Mode 18 detectMode = loop mempty where 19 loop !acc bs 20 | BS.null bs = CBC 21 | otherwise = 22 let (block, rest) = BS.splitAt 16 bs 23 in if S.member block acc 24 then ECB 25 else loop (S.insert block acc) rest 26