1{-# LANGUAGE OverloadedStrings #-}
2
3module BCryptPBKDF (tests) where
4
5import qualified Data.ByteString        as B
6
7import           Test.Tasty
8import           Test.Tasty.HUnit
9
10import           Crypto.KDF.BCryptPBKDF (Parameters (..), generate,
11                                         hashInternal)
12
13tests :: TestTree
14tests = testGroup "BCryptPBKDF"
15    [ testGroup "generate"
16        [ testCase "1" generate1
17        , testCase "2" generate2
18        , testCase "3" generate3
19        ]
20    , testGroup "hashInternal"
21        [ testCase "1" hashInternal1
22        ]
23    ]
24  where
25    -- test vector taken from the go implementation by @dchest
26    generate1 = expected @=? generate params pass salt
27        where
28            params   = Parameters 12 32
29            pass     = "password" :: B.ByteString
30            salt     = "salt"     :: B.ByteString
31            expected = B.pack
32                [ 0x1a, 0xe4, 0x2c, 0x05, 0xd4, 0x87, 0xbc, 0x02
33                , 0xf6, 0x49, 0x21, 0xa4, 0xeb, 0xe4, 0xea, 0x93
34                , 0xbc, 0xac, 0xfe, 0x13, 0x5f, 0xda, 0x99, 0x97
35                , 0x4c, 0x06, 0xb7, 0xb0, 0x1f, 0xae, 0x14, 0x9a
36                ] :: B.ByteString
37
38    -- test vector generated with the go implemenation by @dchest
39    generate2 = expected @=? generate params pass salt
40        where
41            params   = Parameters 7 71
42            pass     = "DieWuerdeDesMenschenIstUnantastbar" :: B.ByteString
43            salt     = "Tafelsalz"                          :: B.ByteString
44            expected = B.pack
45                [ 0x17, 0xb4, 0x76, 0xaa, 0xd7, 0x42, 0x33, 0x49
46                , 0x5c, 0xe8, 0x79, 0x49, 0x15, 0x74, 0x4c, 0x71
47                , 0xf9, 0x99, 0x66, 0x89, 0x7a, 0x60, 0xc3, 0x70
48                , 0xb4, 0x3c, 0xa8, 0x83, 0x80, 0x5a, 0x56, 0xde
49                , 0x38, 0xbc, 0x51, 0x8c, 0xd4, 0xeb, 0xd1, 0xcf
50                , 0x46, 0x0a, 0x68, 0x3d, 0xc8, 0x12, 0xcf, 0xf8
51                , 0x43, 0xce, 0x21, 0x9d, 0x98, 0x81, 0x20, 0x26
52                , 0x6e, 0x42, 0x0f, 0xaa, 0x75, 0x5d, 0x09, 0x8d
53                , 0x45, 0xda, 0xd5, 0x15, 0x6e, 0x65, 0x1d
54                ] :: B.ByteString
55
56    -- test vector generated with the go implemenation by @dchest
57    generate3 = expected @=? generate params pass salt
58        where
59            params    = Parameters 5 5
60            pass      = "ABC" :: B.ByteString
61            salt      = "DEF" :: B.ByteString
62            expected  = B.pack
63                [ 0xdd, 0x6e, 0xa0, 0x69, 0x29
64                ] :: B.ByteString
65
66    hashInternal1 = expected @=? hashInternal passHash saltHash
67        where
68            passHash = B.pack [ 0  ..  63 ] :: B.ByteString
69            saltHash = B.pack [ 64 .. 127 ] :: B.ByteString
70            expected = B.pack
71                [ 0x87, 0x90, 0x48, 0x70, 0xee, 0xf9, 0xde, 0xdd
72                , 0xf8, 0xe7, 0x61, 0x1a, 0x14, 0x01, 0x06, 0xe6
73                , 0xaa, 0xf1, 0xa3, 0x63, 0xd9, 0xa2, 0xc5, 0x04
74                , 0xdb, 0x35, 0x64, 0x43, 0x72, 0x1e, 0xb5, 0x55
75                ] :: B.ByteString
76