Debian Codegraph @

yescrypt/yescrypt_test.go

raw symbols SCIP
1 // Copyright 2012-2017 The Go Authors. All rights reserved.
2 // Copyright 2024 Solar Designer. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 // yescrypt support sponsored by Sandfly Security https://sandflysecurity.com -
7 // Agentless Security for Linux
8
9 package yescrypt
10
11 import (
12 "bytes"
13 "testing"
14 )
15
16 type testVector struct {
17 password string
18 salt string
19 N, r, p int
20 output []byte
21 }
22
23 var good = []testVector{
24 {
25 "p",
26 "s",
27 16, 8, 1,
28 []byte{
29 0xc8, 0xc7, 0xff, 0x11, 0x22, 0xb0, 0xb2, 0x91,
30 0xc3, 0xf2, 0x60, 0x89, 0x48, 0x78, 0x2c, 0xd6,
31 0x89, 0xcc, 0x45, 0x57, 0x90, 0x17, 0xaa, 0xa5,
32 0xff, 0x8b, 0xaa, 0x74, 0xa6, 0x32, 0xec, 0x99,
33 },
34 },
35 {
36 "p",
37 "s",
38 16, 8, 1,
39 []byte{
40 0xc8, 0xc7, 0xff, 0x11, 0x22, 0xb0, 0xb2, 0x91,
41 },
42 },
43 {
44 "",
45 "",
46 4, 1, 1,
47 []byte{
48 0x0c, 0xd5, 0xaf, 0x76, 0xeb, 0x24, 0x1d, 0xf8,
49 0x11, 0x9a, 0x9a, 0x12, 0x2a, 0xe3, 0x69, 0x20,
50 0xbc, 0xc7, 0xf4, 0x14, 0xb9, 0xc0, 0xd5, 0x8f,
51 0x45, 0x00, 0x80, 0x60, 0xda, 0xde, 0x46, 0xb0,
52 0xc8, 0x09, 0x22, 0xbd, 0xcc, 0x16, 0xa3, 0xab,
53 },
54 },
55 }
56
57 var bad = []testVector{
58 {"p", "s", 0, 1, 1, nil}, // N == 0
59 {"p", "s", 1, 1, 1, nil}, // N == 1
60 {"p", "s", 7, 8, 1, nil}, // N is not power of 2
61 {"p", "s", 16, maxInt / 2, maxInt / 2, nil}, // p * r too large
62 {"p", "s", 16, 0, 1, nil}, // r too small
63 {"p", "s", 16, 1, 0, nil}, // p too small
64 {
65 "password",
66 "salt",
67 16, 100, 100,
68 []byte{
69 0x88, 0xbd, 0x5e, 0xdb, 0x52, 0xd1, 0xdd, 0x0, 0x18,
70 0x87, 0x72, 0xad, 0x36, 0x17, 0x12, 0x90, 0x22, 0x4e,
71 0x74, 0x82, 0x95, 0x25, 0xb1, 0x8d, 0x73, 0x23, 0xa5,
72 0x7f, 0x91, 0x96, 0x3c, 0x37,
73 },
74 },
75 }
76
77 func TestKey(t *testing.T) {
78 for i, v := range good {
79 k, err := Key([]byte(v.password), []byte(v.salt), v.N, v.r, v.p, len(v.output))
80 if err != nil {
81 t.Errorf("%d: got unexpected error: %s", i, err)
82 }
83 if !bytes.Equal(k, v.output) {
84 t.Errorf("%d: expected %x, got %x", i, v.output, k)
85 }
86 }
87 for i, v := range bad {
88 _, err := Key([]byte(v.password), []byte(v.salt), v.N, v.r, v.p, 32)
89 if err == nil {
90 t.Errorf("%d: expected error, got nil", i)
91 }
92 }
93 }
94
95 var sink []byte
96
97 type testVectorHash struct {
98 password string
99 hash string
100 }
101
102 /* Generated with:
103 #include <crypt.h>
104 #include <stdio.h>
105
106 int main(void)
107 {
108 for (int count = 1; count <= 11; count++) {
109 const char *setting = crypt_gensalt("$y$", count, NULL, 0);
110 char pw[8];
111 snprintf(pw, sizeof(pw), "test%d", count);
112 printf("\t{\"%s\", \"%s\"},\n", pw, crypt(pw, setting));
113 }
114 for (int saltlen = 0; saltlen <= 24; saltlen++) {
115 char pw[16], setting[32];
116 snprintf(pw, sizeof(pw), "salt length %d", saltlen);
117 snprintf(setting, sizeof(setting), "$y$j7.$%.*s", saltlen, "////////////////////////");
118 printf("\t{\"%s\", \"%s\"},\n", pw, crypt(pw, setting) ?: setting);
119 }
120 return 0;
121 }
122 */
123
124 var hashes = []testVectorHash{
125 {"test1", "$y$j75$z7ztFz2FayrKI79/jEwlL.$u5x/j193MQ09wbFaRGYr0AH/A/jh3kunjuhYRVRNkmC"},
126 {"test2", "$y$j85$uFLpki6/G99e8OAxVooij1$64Rji3LKk1v85LYVULHKh2YKeKoDu0ADrGt4l1JhQy8"},
127 {"test3", "$y$j7T$aoovSEloTaFiZVMrFisfy.$wLTAPbITTB/XIpAGwcX0xxRCFEcDgPWpXTsij0SEbC5"},
128 {"test4", "$y$j8T$P9xODwGnzlle5VHuP1/qA1$bAd4BXv1GBqNQZFzR0Ey42/w0/DFmnFkX1fRpjalAO2"},
129 {"test5", "$y$j9T$fqIAg4Vpv9o1MKgWMnyax.$BxkUx27fLJlPOfyIfNEBPzjrDQ95LXKgN5OJii3GL7."},
130 {"test6", "$y$jAT$70Rw91iJgO8Uzi3CLWfOo1$aYky8YP.XurVMdZfcXHY1do2RZ7Caav5iliKEkmJjhD"},
131 {"test7", "$y$jBT$/YBmADVZsSMw3xfv8M76X0$14s2oH3zHEKh44d5eRVxmDvF8jgM/8SXd8mI4NBQIS1"},
132 /*
133 {"test8", "$y$jCT$JXJjOzzWl5vilbjjonN5N1$.HyIln8Y5//hXrEaSMIFPSHBAE24eR392XzBtFELSv9"},
134 {"test9", "$y$jDT$pPCKQ.Jzpv90Nh7H.ioA9/$6fSLU.2dsGXHY1DZuRncQtItRzZNx9oClKgMpSEkyW0"},
135 {"test10", "$y$jET$YA8uEtDHnx9Sv9OvDcwv81$zfxVIsTgxs6v/qL.nTBEE.o9du75wuSHnF5Sai6K.s7"},
136 {"test11", "$y$jFT$c1pwXe.GpcUUeOK7BV6yX1$lys4J0.caCkYP6ZpfUj.2zuJAiTpMFj0O9HhN59/QI."},
137 */
138 {"salt length 0", "$y$j7.$$cmp7v9bzgyAhctAaiyqG56MBYN2IYzfI5LvybJCKacD"},
139 {"salt length 1", "$y$j7.$/"},
140 {"salt length 2", "$y$j7.$//$DcruwIS63Fs/rFjEN0XX6h83bZyXgBTDICvINmSWVp5"},
141 {"salt length 3", "$y$j7.$///$uo0SD4Xn0Bn1leZVH50teLu3Rje5GAIA.BKYA/jL3/C"},
142 {"salt length 4", "$y$j7.$////$WXGIKO.4sthsRPnpY0/.OhyrlEkLcS1pymEGTbJA/l."},
143 {"salt length 5", "$y$j7.$/////"},
144 {"salt length 6", "$y$j7.$//////$9/wDzXoL4.VS3Ztb.NPiOu4wTpTBKrnJTxBwH1fK70A"},
145 {"salt length 7", "$y$j7.$///////$iFpkxOqqnskGorbt2d.daPYT2vUWCRsisu0jr4sNF1."},
146 {"salt length 8", "$y$j7.$////////$caDf7LpLxRuDzYqMUDha1Nvm9zX2M89hTuCi.33hpMA"},
147 {"salt length 9", "$y$j7.$/////////"},
148 {"salt length 10", "$y$j7.$//////////$w8.ijckeAcw8QTQFtNDzf6GAbpM0GS1tPF9.moe8DA6"},
149 {"salt length 11", "$y$j7.$///////////$aNlOwAA3WuQ2GScDBr/fTD34oO0ZN/BksJ8d6ilH4O8"},
150 {"salt length 12", "$y$j7.$////////////$9YMdFtGt/uJi6XsLbYVhRfnneKsMgoos2r.7fZ8Xn.."},
151 {"salt length 13", "$y$j7.$/////////////"},
152 {"salt length 14", "$y$j7.$//////////////$fPBNb956TRLinNI/LHoThcqdVO5gIGIg/nRpmyzB/T/"},
153 {"salt length 15", "$y$j7.$///////////////$lzaOauYlT250iKS8qtIlo8Ail.PbSHjKSjRpPsEqcI6"},
154 {"salt length 16", "$y$j7.$////////////////$DlYtorMMW/M1IdFxGPdfFS.STo61kYy/eHnOanwVvrC"},
155 {"salt length 17", "$y$j7.$/////////////////"},
156 {"salt length 18", "$y$j7.$//////////////////$3P/0DYS.t.P2VC4rnF9kWURChONU4ehShDJyGUIoYZ9"},
157 {"salt length 19", "$y$j7.$///////////////////$.M.DTam6fr/7j36F7Mo0g3QGSYAD7PbZkwe8X9bJyd8"},
158 {"salt length 20", "$y$j7.$////////////////////$dHX43Z/x85XNKoOu4UDromlyoPcD9isScOP8ZeW6l27"},
159 {"salt length 21", "$y$j7.$/////////////////////"},
160 {"salt length 22", "$y$j7.$//////////////////////$XXf4WJAUsQCV6TUulO3H/f3OWOuK8j8FX9ZtJluydw4"},
161 {"salt length 23", "$y$j7.$///////////////////////$FOmK9/DdyesVtGrimp4GNqRPMQ5V6Z8/wfRPWZ.XhjA"},
162 {"salt length 24", "$y$j7.$////////////////////////$xW7NvvbWPmxoFVWCDe.WNwrrSfuN/iVvy/05.lD/MO9"},
163 // More expected errors
164 {"", ""},
165 {"", "$y$.7.$$"},
166 {"", "$y$j..$$"},
167 }
168
169 func TestHash(t *testing.T) {
170 for i, v := range hashes {
171 cut := len(v.hash)
172 if cut > 29 && (i&1) != 0 {
173 cut = 29 + i/2
174 }
175 hash, err := Hash([]byte(v.password), []byte(v.hash)[:cut])
176 if len(hash) < 51 {
177 if err == nil {
178 t.Errorf("%d: expected error, got %s, nil", i, hash)
179 }
180 continue
181 }
182 if err != nil {
183 t.Errorf("Hash %d: got unexpected error: %s", i, err)
184 }
185 if string(hash) != v.hash {
186 t.Errorf("Hash %d: expected %s, got %s", i, v.hash, hash)
187 }
188 }
189 }
190