forked from canonical/secboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbkdf2_test.go
158 lines (140 loc) · 4.68 KB
/
pbkdf2_test.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2021 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package secboot_test
import (
"crypto"
"time"
. "gopkg.in/check.v1"
. "github.com/snapcore/secboot"
"github.com/snapcore/secboot/internal/pbkdf2"
)
type pbkdf2Suite struct{}
var _ = Suite(&pbkdf2Suite{})
func (s *pbkdf2Suite) TestKDFParamsDefault(c *C) {
var expectedTime int
restore := MockPBKDF2Benchmark(func(targetDuration time.Duration, hashAlg crypto.Hash) (uint, error) {
c.Check(targetDuration, Equals, 2*time.Second)
c.Check(hashAlg, Equals, crypto.SHA256)
iter, err := pbkdf2.Benchmark(targetDuration, hashAlg)
c.Check(err, IsNil)
expectedTime = int(iter)
return iter, err
})
defer restore()
var opts PBKDF2Options
params, err := opts.KdfParams(32)
c.Assert(err, IsNil)
c.Check(params.Type, Equals, "pbkdf2")
c.Check(params.Time, Equals, expectedTime)
c.Check(params.Hash, Equals, HashAlg(crypto.SHA256))
c.Check(params.Memory, Equals, 0)
c.Check(params.CPUs, Equals, 0)
}
func (s *pbkdf2Suite) TestKDFParamsDefault48(c *C) {
var expectedTime int
restore := MockPBKDF2Benchmark(func(targetDuration time.Duration, hashAlg crypto.Hash) (uint, error) {
c.Check(targetDuration, Equals, 2*time.Second)
c.Check(hashAlg, Equals, crypto.SHA384)
iter, err := pbkdf2.Benchmark(targetDuration, hashAlg)
c.Check(err, IsNil)
expectedTime = int(iter)
return iter, err
})
defer restore()
var opts PBKDF2Options
params, err := opts.KdfParams(48)
c.Assert(err, IsNil)
c.Check(params.Type, Equals, "pbkdf2")
c.Check(params.Time, Equals, expectedTime)
c.Check(params.Hash, Equals, HashAlg(crypto.SHA384))
c.Check(params.Memory, Equals, 0)
c.Check(params.CPUs, Equals, 0)
}
func (s *pbkdf2Suite) TestKDFParamsDefault64(c *C) {
var expectedTime int
restore := MockPBKDF2Benchmark(func(targetDuration time.Duration, hashAlg crypto.Hash) (uint, error) {
c.Check(targetDuration, Equals, 2*time.Second)
c.Check(hashAlg, Equals, crypto.SHA512)
iter, err := pbkdf2.Benchmark(targetDuration, hashAlg)
c.Check(err, IsNil)
expectedTime = int(iter)
return iter, err
})
defer restore()
var opts PBKDF2Options
params, err := opts.KdfParams(64)
c.Assert(err, IsNil)
c.Check(params.Type, Equals, "pbkdf2")
c.Check(params.Time, Equals, expectedTime)
c.Check(params.Hash, Equals, HashAlg(crypto.SHA512))
c.Check(params.Memory, Equals, 0)
c.Check(params.CPUs, Equals, 0)
}
func (s *pbkdf2Suite) TestKDFParamsTargetDuration(c *C) {
var expectedTime int
restore := MockPBKDF2Benchmark(func(targetDuration time.Duration, hashAlg crypto.Hash) (uint, error) {
c.Check(targetDuration, Equals, 200*time.Millisecond)
c.Check(hashAlg, Equals, crypto.SHA256)
iter, err := pbkdf2.Benchmark(targetDuration, hashAlg)
c.Check(err, IsNil)
expectedTime = int(iter)
return iter, err
})
defer restore()
var opts PBKDF2Options
opts.TargetDuration = 200 * time.Millisecond
params, err := opts.KdfParams(32)
c.Assert(err, IsNil)
c.Check(params.Type, Equals, "pbkdf2")
c.Check(params.Time, Equals, expectedTime)
c.Check(params.Hash, Equals, HashAlg(crypto.SHA256))
c.Check(params.Memory, Equals, 0)
c.Check(params.CPUs, Equals, 0)
}
func (s *pbkdf2Suite) TestKDFParamsForceIterations(c *C) {
var opts PBKDF2Options
opts.ForceIterations = 2000
params, err := opts.KdfParams(32)
c.Assert(err, IsNil)
c.Check(params, DeepEquals, &KdfParams{
Type: "pbkdf2",
Time: 2000,
Hash: HashAlg(crypto.SHA256),
})
}
func (s *pbkdf2Suite) TestKDFParamsCustomHash(c *C) {
var expectedTime int
restore := MockPBKDF2Benchmark(func(targetDuration time.Duration, hashAlg crypto.Hash) (uint, error) {
c.Check(targetDuration, Equals, 2*time.Second)
c.Check(hashAlg, Equals, crypto.SHA512)
iter, err := pbkdf2.Benchmark(targetDuration, hashAlg)
c.Check(err, IsNil)
expectedTime = int(iter)
return iter, err
})
defer restore()
var opts PBKDF2Options
opts.HashAlg = crypto.SHA512
params, err := opts.KdfParams(32)
c.Assert(err, IsNil)
c.Check(params.Type, Equals, "pbkdf2")
c.Check(params.Time, Equals, expectedTime)
c.Check(params.Hash, Equals, HashAlg(crypto.SHA512))
c.Check(params.Memory, Equals, 0)
c.Check(params.CPUs, Equals, 0)
}