cryptopals

Matasano's cryptopals challenges (cryptopals.com).
Log | Files | Refs | README | LICENSE

commit 892e529ea8cd73b8c927125c0aae1ab7c842230d
parent ff01c61677c0e9206e7e0549fede3f3571b5c04c
Author: Jared Tobin <jared@jtobin.io>
Date:   Wed,  2 Aug 2023 22:14:04 -0230

Add 4.25.

Diffstat:
MREADME.md | 1+
Adata/s4/q25_input.txt | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adocs/s4.md | 26++++++++++++++++++++++++++
Mlib/Cryptopals/Stream/Attacks.hs | 63++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 153 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md @@ -11,4 +11,5 @@ to build binaries. Use `cabal install` to (I think) dump them in * [Problem Set 1](docs/s1.md) * [Problem Set 2](docs/s2.md) * [Problem Set 3](docs/s3.md) +* [Problem Set 4](docs/s4.md) diff --git a/data/s4/q25_input.txt b/data/s4/q25_input.txt @@ -0,0 +1,64 @@ +CRIwqt4+szDbqkNY+I0qbDe3LQz0wiw0SuxBQtAM5TDdMbjCMD/venUDW9BL +PEXODbk6a48oMbAY6DDZsuLbc0uR9cp9hQ0QQGATyyCESq2NSsvhx5zKlLtz +dsnfK5ED5srKjK7Fz4Q38/ttd+stL/9WnDzlJvAo7WBsjI5YJc2gmAYayNfm +CW2lhZE/ZLG0CBD2aPw0W417QYb4cAIOW92jYRiJ4PTsBBHDe8o4JwqaUac6 +rqdi833kbyAOV/Y2RMbN0oDb9Rq8uRHvbrqQJaJieaswEtMkgUt3P5Ttgeh7 +J+hE6TR0uHot8WzHyAKNbUWHoi/5zcRCUipvVOYLoBZXlNu4qnwoCZRSBgvC +wTdz3Cbsp/P2wXB8tiz6l9rL2bLhBt13Qxyhhu0H0+JKj6soSeX5ZD1Rpilp +9ncR1tHW8+uurQKyXN4xKeGjaKLOejr2xDIw+aWF7GszU4qJhXBnXTIUUNUf +RlwEpS6FZcsMzemQF30ezSJHfpW7DVHzw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bbw4OTCFX2HKmWEiVBB0O3J9xwXWpxN1Vr5CDi75Fq +NhxYCjgSJzWOUD34Y1dAfcj57VINmQVEWyc8Tch8vg9MnHGCOfOjRqp0VGyA +S15AVD2QS1V6fhRimJSVyT6QuGb8tKRsl2N+a2Xze36vgMhw7XK7zh//jC2H diff --git a/docs/s4.md b/docs/s4.md @@ -0,0 +1,26 @@ +### Set 4 + +#### 4.25 + +If we can control the offset and plaintext input, then we can ask +the oracle to encrypt a plaintext with the same length of the +ciphertext at offset 0. *Et voilĂ *, there's our keystream, which we +can just xor with the original ciphertext in order to decrypt it. +Cryptopals.Stream.Attacks.rawrCtrAttack implements such an "attack": + + rawrCtrAttack :: IO BS.ByteString + rawrCtrAttack = do + cip <- rawrCtrOracle (maxBound :: Int) mempty + let l = BS.length cip + p = BS.replicate l 65 + + new <- rawrCtrOracle 0 p + let ks = new `CU.fixedXor` p + + pure $ ks `CU.fixedXor` cip + +Which gives the expected plaintext: + + > fmap (BS.take 33) rawrCtrAttack + "I'm back and I'm ringin' the bell" + diff --git a/lib/Cryptopals/Stream/Attacks.hs b/lib/Cryptopals/Stream/Attacks.hs @@ -1,18 +1,37 @@ module Cryptopals.Stream.Attacks where import Control.Monad +import Control.Monad.Primitive import qualified Control.Monad.ST as ST import qualified Data.Binary.Put as BP import qualified Data.ByteString as BS +import qualified Data.ByteString.Char8 as B8 import qualified Data.ByteString.Base64 as B64 import qualified Data.ByteString.Lazy as BSL import qualified Data.Text as T import qualified Data.Time.Clock.System as TS +import qualified Cryptopals.AES as AES import qualified Cryptopals.Stream.RNG.MT19937 as MT import qualified Cryptopals.Util as CU -import GHC.Word (Word16, Word8) +import GHC.Word (Word64, Word16, Word8) import qualified System.Random.MWC as MWC +bytes :: PrimMonad m => Int -> MWC.Gen (PrimState m) -> m BS.ByteString +bytes n gen = fmap BS.pack $ replicateM n (MWC.uniform gen) + +-- | An unknown AES key. +consistentKey :: BS.ByteString +consistentKey = ST.runST $ do + gen <- MWC.create + bytes 16 gen + +consistentNonce :: Word64 +consistentNonce = ST.runST $ do + gen <- MWC.create + MWC.uniformR (0, 0xffffffffffffffff) gen + +-- MT19937-related attacks + keystream :: Int -> MT.Gen -> BS.ByteString keystream nb g = let l = nb `quot` 4 + nb `rem` 4 @@ -72,3 +91,45 @@ isPwnt token = do let g = MT.seed s ks = keystream 16 g pure $ token == B64.encodeBase64 ks + +-- CTR attacks + +ctrEdit + :: BS.ByteString + -> BS.ByteString + -> Word64 + -> Int + -> BS.ByteString + -> BS.ByteString +ctrEdit cip key non off new = + let (pre, _) = BS.splitAt off cip + ced = AES.encryptCtrAES128 non key new + in pre <> ced + +rawrCtrInput :: IO BS.ByteString +rawrCtrInput = do + raw <- B8.readFile "data/s4/q25_input.txt" + let bs = B64.decodeBase64Lenient . mconcat .B8.lines $ raw + let pay = AES.decryptEcbAES128 "YELLOW SUBMARINE" bs + pure $ AES.encryptCtrAES128 consistentNonce consistentKey pay + +rawrCtrOracle :: Int -> BS.ByteString -> IO BS.ByteString +rawrCtrOracle off pay = do + let k = consistentKey + n = consistentNonce + + cip <- rawrCtrInput + + pure $ ctrEdit cip k n off pay + +rawrCtrAttack :: IO BS.ByteString +rawrCtrAttack = do + cip <- rawrCtrOracle (maxBound :: Int) mempty + let l = BS.length cip + p = BS.replicate l 65 + + new <- rawrCtrOracle 0 p + let ks = new `CU.fixedXor` p + + pure $ ks `CU.fixedXor` cip +