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