File: Poly1305.hs

package info (click to toggle)
haskell-crypton 1.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,548 kB
  • sloc: haskell: 26,764; ansic: 22,294; makefile: 6
file content (48 lines) | stat: -rw-r--r-- 1,669 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{-# LANGUAGE OverloadedStrings #-}

module Poly1305 (tests) where

import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B ()

import Crypto.Error
import Imports

import qualified Crypto.MAC.Poly1305 as Poly1305
import qualified Data.ByteArray as B (convert)

instance Show Poly1305.Auth where
    show _ = "Auth"

data Chunking = Chunking Int Int
    deriving (Show, Eq)

instance Arbitrary Chunking where
    arbitrary = Chunking <$> choose (1, 34) <*> choose (1, 2048)

tests =
    testGroup
        "Poly1305"
        [ testCase "V0" $
            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
                msg = "Cryptographic Forum Research Group" :: ByteString
                tag =
                    "\xa8\x06\x1d\xc1\x30\x51\x36\xc6\xc2\x2b\x8b\xaf\x0c\x01\x27\xa9" :: ByteString
             in tag @=? B.convert (Poly1305.auth key msg)
        , testProperty "Chunking" $ \(Chunking chunkLen totalLen) ->
            let key = B.replicate 32 0
                msg = B.pack $ take totalLen $ concat (replicate 10 [1 .. 255])
             in Poly1305.auth key msg
                    == Poly1305.finalize
                        ( foldr
                            (flip Poly1305.update)
                            (throwCryptoError $ Poly1305.initialize key)
                            (chunks chunkLen msg)
                        )
        ]
  where
    chunks i bs
        | B.length bs < i = [bs]
        | otherwise = let (b1, b2) = B.splitAt i bs in b1 : chunks i b2