]> Cypherpunks.ru repositories - balloon.git/blob - balloon_test.go
87caf2094a55cbcf7404608d374ef07c81f0ce70
[balloon.git] / balloon_test.go
1 /*
2 balloon -- Balloon password hashing function
3 Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program.  If not, see
16 <http://www.gnu.org/licenses/>.
17 */
18
19 package balloon
20
21 import (
22         "bytes"
23         "crypto/rand"
24         "crypto/sha256"
25         "crypto/sha512"
26         "encoding/hex"
27         "testing"
28         "testing/quick"
29 )
30
31 func TestB(t *testing.T) {
32         f := func(passwd, salt []byte, s, t uint8) bool {
33                 if len(passwd) == 0 || len(salt) == 0 {
34                         return true
35                 }
36                 B(sha512.New(), passwd, salt, uint64(s)%16+1, uint64(t)%16+1)
37                 return true
38         }
39         if err := quick.Check(f, nil); err != nil {
40                 t.Error(err)
41         }
42 }
43
44 func TestH(t *testing.T) {
45         f := func(passwd, salt []byte, s, t, p uint8) bool {
46                 if len(passwd) == 0 || len(salt) == 0 {
47                         return true
48                 }
49                 H(sha512.New, passwd, salt, int(s)%16+1, int(t)%16+1, int(p)%8+1)
50                 return true
51         }
52         if err := quick.Check(f, nil); err != nil {
53                 t.Error(err)
54         }
55 }
56
57 func BenchmarkB(b *testing.B) {
58         passwd := make([]byte, 8)
59         rand.Read(passwd)
60         salt := make([]byte, 8)
61         rand.Read(salt)
62         h := sha512.New()
63         sCost := uint64(1 << 10 / h.Size())
64         b.ResetTimer()
65         for i := 0; i < b.N; i++ {
66                 B(h, passwd, salt, sCost, 4)
67         }
68 }
69
70 func BenchmarkH(b *testing.B) {
71         passwd := make([]byte, 8)
72         rand.Read(passwd)
73         salt := make([]byte, 8)
74         rand.Read(salt)
75         sCost := 1 << 10 / sha512.New().Size()
76         b.ResetTimer()
77         for i := 0; i < b.N; i++ {
78                 H(sha512.New, passwd, salt, sCost, 4, 4)
79         }
80 }
81
82 func mustHexDecode(s string) []byte {
83         b, err := hex.DecodeString(s)
84         if err != nil {
85                 panic(err)
86         }
87         return b
88 }
89
90 func TestVectors(t *testing.T) {
91         // taken from Nettle 3.9
92         if !bytes.Equal(
93                 B(sha256.New(), []byte("password"), []byte("salt"), 1, 1),
94                 mustHexDecode("eefda4a8a75b461fa389c1dcfaf3e9dfacbc26f81f22e6f280d15cc18c417545"),
95         ) {
96                 t.FailNow()
97         }
98
99         if !bytes.Equal(
100                 B(sha256.New(), []byte{0}, []byte{0}, 3, 3),
101                 mustHexDecode("4fc7e302ffa29ae0eac31166cee7a552d1d71135f4e0da66486fb68a749b73a4"),
102         ) {
103                 t.FailNow()
104         }
105
106         if !bytes.Equal(
107                 B(sha256.New(), nil, []byte("salt"), 3, 3),
108                 mustHexDecode("5f02f8206f9cd212485c6bdf85527b698956701ad0852106f94b94ee94577378"),
109         ) {
110                 t.FailNow()
111         }
112
113         if !bytes.Equal(
114                 B(sha256.New(), []byte("password"), nil, 3, 3),
115                 mustHexDecode("20aa99d7fe3f4df4bd98c655c5480ec98b143107a331fd491deda885c4d6a6cc"),
116         ) {
117                 t.FailNow()
118         }
119
120         if !bytes.Equal(
121                 B(sha256.New(), []byte("hunter42"), []byte("examplesalt"), 1024, 3),
122                 mustHexDecode("716043dff777b44aa7b88dcbab12c078abecfac9d289c5b5195967aa63440dfb"),
123         ) {
124                 t.FailNow()
125         }
126 }