Debian Codegraph @

openpgp/s2k/s2k_test.go

raw
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package s2k
6
7 import (
8 "bytes"
9 "crypto"
10 "crypto/rand"
11 "crypto/sha1"
12 _ "crypto/sha256"
13 _ "crypto/sha512"
14 "encoding/hex"
15 "testing"
16
17 _ "golang.org/x/crypto/ripemd160"
18 _ "golang.org/x/crypto/sha3"
19 )
20
21 var saltedTests = []struct {
22 in, out string
23 }{
24 {"hello", "10295ac1"},
25 {"world", "ac587a5e"},
26 {"foo", "4dda8077"},
27 {"bar", "bd8aac6b9ea9cae04eae6a91c6133b58b5d9a61c14f355516ed9370456"},
28 {"x", "f1d3f289"},
29 {"xxxxxxxxxxxxxxxxxxxxxxx", "e00d7b45"},
30 }
31
32 func TestSalted(t *testing.T) {
33 h := sha1.New()
34 salt := [4]byte{1, 2, 3, 4}
35
36 for i, test := range saltedTests {
37 expected, _ := hex.DecodeString(test.out)
38 out := make([]byte, len(expected))
39 Salted(out, h, []byte(test.in), salt[:])
40 if !bytes.Equal(expected, out) {
41 t.Errorf("#%d, got: %x want: %x", i, out, expected)
42 }
43 }
44 }
45
46 var argon2EncodeTest = []struct {
47 in uint32
48 out uint8
49 }{
50 {64 * 1024, 16},
51 {64*1024 + 1, 17},
52 {2147483647, 31},
53 {2147483649, 31},
54 {1, 3},
55 }
56
57 func TestArgon2EncodeTest(t *testing.T) {
58
59 for i, tests := range argon2EncodeTest {
60 conf := &Argon2Config{
61 Memory: tests.in,
62 DegreeOfParallelism: 1,
63 }
64 out := conf.EncodedMemory()
65 if out != tests.out {
66 t.Errorf("#%d, got: %x want: %x", i, out, tests.out)
67 }
68 }
69 }
70
71 var iteratedTests = []struct {
72 in, out string
73 }{
74 {"hello", "83126105"},
75 {"world", "6fa317f9"},
76 {"foo", "8fbc35b9"},
77 {"bar", "2af5a99b54f093789fd657f19bd245af7604d0f6ae06f66602a46a08ae"},
78 {"x", "5a684dfe"},
79 {"xxxxxxxxxxxxxxxxxxxxxxx", "18955174"},
80 }
81
82 func TestIterated(t *testing.T) {
83 h := sha1.New()
84 salt := [4]byte{4, 3, 2, 1}
85
86 for i, test := range iteratedTests {
87 expected, _ := hex.DecodeString(test.out)
88 out := make([]byte, len(expected))
89 Iterated(out, h, []byte(test.in), salt[:], 31)
90 if !bytes.Equal(expected, out) {
91 t.Errorf("#%d, got: %x want: %x", i, out, expected)
92 }
93 }
94 }
95
96 var argonTestSalt = "12345678"
97 var argon2DeriveTests = []struct {
98 in, out string
99 }{
100 {"hello", "bf69293d2961bbbebe4c64c745cf44d4"},
101 {"world", "dc1bb06234b61c9542d8cf73e2e279d3"},
102 {"foo", "7f6baa1c21f0e7eec16cf8fde866775d"},
103 {"bar", "2826332c8e62d0cf97cc08f243c5cc9135654bf3a8e46d6a4b4637e42eda2fa0"},
104 {"x", "89e5b79435132b98bbcad321532ae7e09f87ac96deca272d6012d367e6350b7d"},
105 {"xxxxxxxxxxxxxxxxxxxxxxx", "de0f978013283457e29f0682e0078ad654e7c21bc72886c914c012e56fd5dc91"},
106 }
107
108 func TestArgon2Derive(t *testing.T) {
109 salt := []byte(argonTestSalt)
110
111 for i, test := range argon2DeriveTests {
112 expected, _ := hex.DecodeString(test.out)
113 out := make([]byte, len(expected))
114 Argon2(out, []byte(test.in), salt[:], 3, 4, 16)
115 if !bytes.Equal(expected, out) {
116 t.Errorf("#%d, got: %x want: %x", i, out, expected)
117 }
118 }
119 }
120
121 var parseTests = []struct {
122 spec, in, out string
123 dummyKey bool
124 params Params
125 }{
126 /* Simple with SHA1 */
127 {"0002", "hello", "aaf4c61d", false,
128 Params{SimpleS2K, 0x02, [16]byte{}, 0, 0, 0, 0}},
129 /* Salted with SHA1 */
130 {"01020102030405060708", "hello", "f4f7d67e", false,
131 Params{SaltedS2K, 0x02, [16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0, 0, 0, 0}},
132 /* Iterated with SHA1 */
133 {"03020102030405060708f1", "hello", "f2a57b7c", false,
134 Params{IteratedSaltedS2K, 0x02, [16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0xf1, 0, 0, 0}},
135 /* Argon2 */
136 {"0401020304050607080102030405060708030410", "hello", "dabc018a", false,
137 Params{Argon2S2K, 0x00, [16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, 0, 0x03, 0x04, 0x10}},
138 /* GNU dummy S2K */
139 {"6502474e5501", "", "", true,
140 Params{GnuS2K, 0x02, [16]byte{}, 0, 0, 0, 0}},
141 }
142
143 func TestParseIntoParams(t *testing.T) {
144 for i, test := range parseTests {
145 spec, _ := hex.DecodeString(test.spec)
146 buf := bytes.NewBuffer(spec)
147 params, err := ParseIntoParams(buf)
148 if err != nil {
149 t.Errorf("%d: ParseIntoParams returned error: %s", i, err)
150 continue
151 }
152
153 if test.params.mode != params.mode || test.params.hashId != params.hashId || test.params.countByte != params.countByte ||
154 !bytes.Equal(test.params.salt(), params.salt()) {
155 t.Errorf("%d: Wrong config, got: %+v want: %+v", i, params, test.params)
156 }
157
158 if params.Dummy() != test.dummyKey {
159 t.Errorf("%d: Got GNU dummy %v, expected %v", i, params.Dummy(), test.dummyKey)
160 }
161
162 if !test.dummyKey {
163 expectedHash, _ := hex.DecodeString(test.out)
164 out := make([]byte, len(expectedHash))
165
166 f, err := params.Function()
167 if err != nil {
168 t.Errorf("%d: params.Function() returned error: %s", i, err)
169 continue
170 }
171 f(out, []byte(test.in))
172 if !bytes.Equal(out, expectedHash) {
173 t.Errorf("%d: Wrong output got: %x want: %x", i, out, expectedHash)
174 }
175 }
176
177 var reserialized bytes.Buffer
178 err = params.Serialize(&reserialized)
179 if err != nil {
180 t.Errorf("%d: params.Serialize() returned error: %s", i, err)
181 continue
182 }
183 if !bytes.Equal(reserialized.Bytes(), spec) {
184 t.Errorf("%d: Wrong reserialized got: %x want: %x", i, reserialized.Bytes(), spec)
185 }
186 if testing.Short() {
187 break
188 }
189 }
190 }
191
192 func TestSerializeSaltedOK(t *testing.T) {
193 hashes := []crypto.Hash{crypto.SHA256, crypto.SHA384, crypto.SHA512, crypto.SHA224, crypto.SHA3_256,
194 crypto.SHA3_512}
195 for _, h := range hashes {
196 params := testSerializeConfigOK(t, &Config{S2KMode: SaltedS2K, Hash: h, PassphraseIsHighEntropy: true})
197
198 if params.mode != SaltedS2K {
199 t.Fatalf("Wrong mode, expected %d got %d", SaltedS2K, params.mode)
200 }
201 }
202 }
203
204 func TestSerializeSaltedLowEntropy(t *testing.T) {
205 hashes := []crypto.Hash{crypto.SHA256, crypto.SHA384, crypto.SHA512, crypto.SHA224, crypto.SHA3_256,
206 crypto.SHA3_512}
207 for _, h := range hashes {
208 params := testSerializeConfigOK(t, &Config{S2KMode: SaltedS2K, Hash: h})
209
210 if params.mode != IteratedSaltedS2K {
211 t.Fatalf("Wrong mode, expected %d got %d", IteratedSaltedS2K, params.mode)
212 }
213
214 if params.countByte != 224 { // The default case. Corresponding to 16777216
215 t.Fatalf("Wrong count byte, expected %d got %d", 224, params.countByte)
216 }
217 }
218 }
219
220 func TestSerializeSaltedIteratedOK(t *testing.T) {
221 hashes := []crypto.Hash{crypto.SHA256, crypto.SHA384, crypto.SHA512, crypto.SHA224, crypto.SHA3_256,
222 crypto.SHA3_512}
223 // {input, expected}
224 testCounts := [][]int{{-1, 96}, {0, 224}, {1024, 96}, {65536, 96}, {4063232, 191}, {65011712, 255}}
225 for _, h := range hashes {
226 for _, c := range testCounts {
227 params := testSerializeConfigOK(t, &Config{Hash: h, S2KCount: c[0]})
228
229 if params.mode != IteratedSaltedS2K {
230 t.Fatalf("Wrong mode, expected %d got %d", IteratedSaltedS2K, params.mode)
231 }
232
233 if int(params.countByte) != c[1] {
234 t.Fatalf("Wrong count byte, expected %d got %d", c[1], params.countByte)
235 }
236 }
237 }
238 }
239
240 func TestSerializeOKArgon(t *testing.T) {
241 config := &Config{
242 S2KMode: Argon2S2K,
243 Argon2Config: &Argon2Config{NumberOfPasses: 3, DegreeOfParallelism: 4, Memory: 64 * 1024},
244 }
245
246 params := testSerializeConfigOK(t, config)
247
248 if params.mode != Argon2S2K {
249 t.Fatalf("Wrong mode, expected %d got %d", Argon2S2K, params.mode)
250 }
251 }
252
253 func testSerializeConfigOK(t *testing.T, c *Config) *Params {
254 buf := bytes.NewBuffer(nil)
255 key := make([]byte, 16)
256 passphrase := []byte("testing")
257 err := Serialize(buf, key, rand.Reader, passphrase, c)
258 if err != nil {
259 t.Fatalf("failed to serialize with config %+v: %s", c, err)
260 }
261
262 f, err := Parse(bytes.NewBuffer(buf.Bytes()))
263 if err != nil {
264 t.Fatalf("failed to reparse: %s", err)
265 }
266 key2 := make([]byte, len(key))
267 f(key2, passphrase)
268 if !bytes.Equal(key2, key) {
269 t.Errorf("keys don't match: %x (serialied) vs %x (parsed)", key, key2)
270 }
271
272 params, err := ParseIntoParams(bytes.NewBuffer(buf.Bytes()))
273 if err != nil {
274 t.Fatalf("failed to parse params: %s", err)
275 }
276
277 return params
278 }
279
280 func TestValidateArgon2Params(t *testing.T) {
281 tests := []struct {
282 params Params
283 wantErr bool
284 }{
285 {
286 params: Params{parallelism: 4, passes: 3, memoryExp: 6},
287 wantErr: false,
288 },
289 {
290 params: Params{parallelism: 0, passes: 3, memoryExp: 6},
291 wantErr: true,
292 },
293 {
294 params: Params{parallelism: 4, passes: 0, memoryExp: 6},
295 wantErr: true,
296 },
297 {
298 params: Params{parallelism: 4, passes: 3, memoryExp: 4},
299 wantErr: true,
300 },
301 {
302 params: Params{parallelism: 4, passes: 3, memoryExp: 32},
303 wantErr: true,
304 },
305 {
306 params: Params{parallelism: 4, passes: 3, memoryExp: 5},
307 wantErr: false,
308 },
309 {
310 params: Params{parallelism: 4, passes: 3, memoryExp: 31},
311 wantErr: false,
312 },
313 }
314
315 for _, tt := range tests {
316 err := validateArgon2Params(&tt.params)
317 if tt.wantErr && err == nil {
318 t.Errorf("validateArgon2Params: expected an error")
319 }
320 if !tt.wantErr && err != nil {
321 t.Error("validateArgon2Params: expected no error")
322 }
323 }
324 }