2023-02-19 23:51:50 +00:00
|
|
|
module WebRTCTests where
|
|
|
|
|
2023-02-24 20:55:59 +00:00
|
|
|
import Control.Monad.Except
|
2023-02-19 23:51:50 +00:00
|
|
|
import Crypto.Random (getRandomBytes)
|
|
|
|
import qualified Data.ByteString.Base64.URL as U
|
|
|
|
import qualified Data.ByteString.Char8 as B
|
|
|
|
import Simplex.Chat.Mobile.WebRTC
|
2023-02-25 17:52:23 +00:00
|
|
|
import qualified Simplex.Messaging.Crypto as C
|
2023-02-19 23:51:50 +00:00
|
|
|
import Test.Hspec
|
|
|
|
|
|
|
|
webRTCTests :: Spec
|
|
|
|
webRTCTests = describe "WebRTC crypto" $ do
|
|
|
|
it "encrypts and decrypts media" $ do
|
|
|
|
key <- U.encode <$> getRandomBytes 32
|
|
|
|
frame <- getRandomBytes 1000
|
2023-02-24 20:55:59 +00:00
|
|
|
Right frame' <- runExceptT $ chatEncryptMedia key $ frame <> B.replicate reservedSize '\NUL'
|
2023-02-19 23:51:50 +00:00
|
|
|
B.length frame' `shouldBe` B.length frame + reservedSize
|
2023-02-24 20:55:59 +00:00
|
|
|
Right frame'' <- runExceptT $ chatDecryptMedia key frame'
|
2023-02-19 23:51:50 +00:00
|
|
|
frame'' `shouldBe` frame <> B.replicate reservedSize '\NUL'
|
2023-02-24 20:55:59 +00:00
|
|
|
it "should fail on invalid frame size" $ do
|
|
|
|
key <- U.encode <$> getRandomBytes 32
|
|
|
|
frame <- getRandomBytes 10
|
2023-02-25 17:52:23 +00:00
|
|
|
runExceptT (chatEncryptMedia key frame) `shouldReturn` Left "frame has no [reserved space for] IV and/or auth tag"
|
|
|
|
runExceptT (chatDecryptMedia key frame) `shouldReturn` Left "frame has no [reserved space for] IV and/or auth tag"
|
2023-02-24 20:55:59 +00:00
|
|
|
it "should fail on invalid key" $ do
|
|
|
|
let key = B.replicate 32 '#'
|
|
|
|
frame <- (<> B.replicate reservedSize '\NUL') <$> getRandomBytes 100
|
|
|
|
runExceptT (chatEncryptMedia key frame) `shouldReturn` Left "invalid key: invalid character at offset: 0"
|
|
|
|
runExceptT (chatDecryptMedia key frame) `shouldReturn` Left "invalid key: invalid character at offset: 0"
|
|
|
|
it "should fail on invalid auth tag" $ do
|
|
|
|
key <- U.encode <$> getRandomBytes 32
|
|
|
|
frame <- getRandomBytes 1000
|
|
|
|
Right frame' <- runExceptT $ chatEncryptMedia key $ frame <> B.replicate reservedSize '\NUL'
|
|
|
|
Right frame'' <- runExceptT $ chatDecryptMedia key frame'
|
|
|
|
frame'' `shouldBe` frame <> B.replicate reservedSize '\NUL'
|
2023-02-25 17:52:23 +00:00
|
|
|
let (encFrame, rest) = B.splitAt (B.length frame' - reservedSize) frame
|
|
|
|
(_tag, iv) = B.splitAt C.authTagSize rest
|
|
|
|
badFrame = encFrame <> B.replicate C.authTagSize '\NUL' <> iv
|
2023-02-24 20:55:59 +00:00
|
|
|
runExceptT (chatDecryptMedia key badFrame) `shouldReturn` Left "AESDecryptError"
|