|
Packit |
141393 |
{-# LANGUAGE OverloadedStrings #-}
|
|
Packit |
141393 |
module Poly1305 (tests) where
|
|
Packit |
141393 |
|
|
Packit |
141393 |
import qualified Data.ByteString as B
|
|
Packit |
141393 |
import qualified Data.ByteString.Char8 as B ()
|
|
Packit |
141393 |
|
|
Packit |
141393 |
import Imports
|
|
Packit |
141393 |
import Crypto.Error
|
|
Packit |
141393 |
|
|
Packit |
141393 |
import qualified Crypto.MAC.Poly1305 as Poly1305
|
|
Packit |
141393 |
import qualified Data.ByteArray as B (convert)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
instance Show Poly1305.Auth where
|
|
Packit |
141393 |
show _ = "Auth"
|
|
Packit |
141393 |
|
|
Packit |
141393 |
data Chunking = Chunking Int Int
|
|
Packit |
141393 |
deriving (Show,Eq)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
instance Arbitrary Chunking where
|
|
Packit |
141393 |
arbitrary = Chunking <$> choose (1,34) <*> choose (1,2048)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
tests = testGroup "Poly1305"
|
|
Packit |
141393 |
[ testCase "V0" $
|
|
Packit |
141393 |
let key = "\x85\xd6\xbe\x78\x57\x55\x6d\x33\x7f\x44\x52\xfe\x42\xd5\x06\xa8\x01\x03\x80\x8a\xfb\x0d\xb2\xfd\x4a\xbf\xf6\xaf\x41\x49\xf5\x1b" :: ByteString
|
|
Packit |
141393 |
msg = "Cryptographic Forum Research Group" :: ByteString
|
|
Packit |
141393 |
tag = "\xa8\x06\x1d\xc1\x30\x51\x36\xc6\xc2\x2b\x8b\xaf\x0c\x01\x27\xa9" :: ByteString
|
|
Packit |
141393 |
in tag @=? B.convert (Poly1305.auth key msg)
|
|
Packit |
141393 |
, testProperty "Chunking" $ \(Chunking chunkLen totalLen) ->
|
|
Packit |
141393 |
let key = B.replicate 32 0
|
|
Packit |
141393 |
msg = B.pack $ take totalLen $ concat (replicate 10 [1..255])
|
|
Packit |
141393 |
in Poly1305.auth key msg == Poly1305.finalize (foldr (flip Poly1305.update) (throwCryptoError $ Poly1305.initialize key) (chunks chunkLen msg))
|
|
Packit |
141393 |
]
|
|
Packit |
141393 |
where
|
|
Packit |
141393 |
chunks i bs
|
|
Packit |
141393 |
| B.length bs < i = [bs]
|
|
Packit |
141393 |
| otherwise = let (b1,b2) = B.splitAt i bs in b1 : chunks i b2
|