Debian Codegraph @

yescrypt/yescrypt.go

raw symbols SCIP
1 // Copyright 2012-2020 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 // Package yescrypt implements the scrypt key derivation function as defined in
7 // Colin Percival's paper "Stronger Key Derivation via Sequential Memory-Hard
8 // Functions", as well as Solar Designer's yescrypt.
9
10 // yescrypt support sponsored by Sandfly Security https://sandflysecurity.com -
11 // Agentless Security for Linux
12
13 package yescrypt
14
15 import (
16 "crypto/hmac"
17 "crypto/sha256"
18 "encoding/binary"
19 "errors"
20 "math/bits"
21
22 "github.com/go-crypt/x/pbkdf2"
23 )
24
25 const maxInt = int(^uint(0) >> 1)
26
27 // blockCopy copies n numbers from src into dst.
28 func blockCopy(dst, src []uint64, n int) {
29 copy(dst, src[:n])
30 }
31
32 // blockXOR XORs numbers from dst with n numbers from src.
33 func blockXOR(dst, src []uint64, n int) {
34 for i, v := range src[:n] {
35 dst[i] ^= v
36 }
37 }
38
39 // salsaXOR applies Salsa20/8 to the XOR of 16 numbers from tmp and in,
40 // and puts the result into both tmp and out.
41 func salsaXOR(tmp *[8]uint64, in, out []uint64, rounds int) {
42 d0 := tmp[0] ^ in[0]
43 d1 := tmp[1] ^ in[1]
44 d2 := tmp[2] ^ in[2]
45 d3 := tmp[3] ^ in[3]
46 d4 := tmp[4] ^ in[4]
47 d5 := tmp[5] ^ in[5]
48 d6 := tmp[6] ^ in[6]
49 d7 := tmp[7] ^ in[7]
50
51 x0, x1 := uint32(d0), uint32(d6>>32)
52 x2, x3 := uint32(d5), uint32(d3>>32)
53 x4, x5 := uint32(d2), uint32(d0>>32)
54 x6, x7 := uint32(d7), uint32(d5>>32)
55 x8, x9 := uint32(d4), uint32(d2>>32)
56 x10, x11 := uint32(d1), uint32(d7>>32)
57 x12, x13 := uint32(d6), uint32(d4>>32)
58 x14, x15 := uint32(d3), uint32(d1>>32)
59
60 for i := 0; i < rounds; i += 2 {
61 x4 ^= bits.RotateLeft32(x0+x12, 7)
62 x8 ^= bits.RotateLeft32(x4+x0, 9)
63 x12 ^= bits.RotateLeft32(x8+x4, 13)
64 x0 ^= bits.RotateLeft32(x12+x8, 18)
65
66 x9 ^= bits.RotateLeft32(x5+x1, 7)
67 x13 ^= bits.RotateLeft32(x9+x5, 9)
68 x1 ^= bits.RotateLeft32(x13+x9, 13)
69 x5 ^= bits.RotateLeft32(x1+x13, 18)
70
71 x14 ^= bits.RotateLeft32(x10+x6, 7)
72 x2 ^= bits.RotateLeft32(x14+x10, 9)
73 x6 ^= bits.RotateLeft32(x2+x14, 13)
74 x10 ^= bits.RotateLeft32(x6+x2, 18)
75
76 x3 ^= bits.RotateLeft32(x15+x11, 7)
77 x7 ^= bits.RotateLeft32(x3+x15, 9)
78 x11 ^= bits.RotateLeft32(x7+x3, 13)
79 x15 ^= bits.RotateLeft32(x11+x7, 18)
80
81 x1 ^= bits.RotateLeft32(x0+x3, 7)
82 x2 ^= bits.RotateLeft32(x1+x0, 9)
83 x3 ^= bits.RotateLeft32(x2+x1, 13)
84 x0 ^= bits.RotateLeft32(x3+x2, 18)
85
86 x6 ^= bits.RotateLeft32(x5+x4, 7)
87 x7 ^= bits.RotateLeft32(x6+x5, 9)
88 x4 ^= bits.RotateLeft32(x7+x6, 13)
89 x5 ^= bits.RotateLeft32(x4+x7, 18)
90
91 x11 ^= bits.RotateLeft32(x10+x9, 7)
92 x8 ^= bits.RotateLeft32(x11+x10, 9)
93 x9 ^= bits.RotateLeft32(x8+x11, 13)
94 x10 ^= bits.RotateLeft32(x9+x8, 18)
95
96 x12 ^= bits.RotateLeft32(x15+x14, 7)
97 x13 ^= bits.RotateLeft32(x12+x15, 9)
98 x14 ^= bits.RotateLeft32(x13+x12, 13)
99 x15 ^= bits.RotateLeft32(x14+x13, 18)
100 }
101
102 d0 = uint64(uint32(d0)+x0) | uint64(uint32(d0>>32)+x5)<<32
103 d1 = uint64(uint32(d1)+x10) | uint64(uint32(d1>>32)+x15)<<32
104 d2 = uint64(uint32(d2)+x4) | uint64(uint32(d2>>32)+x9)<<32
105 d3 = uint64(uint32(d3)+x14) | uint64(uint32(d3>>32)+x3)<<32
106 d4 = uint64(uint32(d4)+x8) | uint64(uint32(d4>>32)+x13)<<32
107 d5 = uint64(uint32(d5)+x2) | uint64(uint32(d5>>32)+x7)<<32
108 d6 = uint64(uint32(d6)+x12) | uint64(uint32(d6>>32)+x1)<<32
109 d7 = uint64(uint32(d7)+x6) | uint64(uint32(d7>>32)+x11)<<32
110
111 out[0], tmp[0] = d0, d0
112 out[1], tmp[1] = d1, d1
113 out[2], tmp[2] = d2, d2
114 out[3], tmp[3] = d3, d3
115 out[4], tmp[4] = d4, d4
116 out[5], tmp[5] = d5, d5
117 out[6], tmp[6] = d6, d6
118 out[7], tmp[7] = d7, d7
119 }
120
121 func blockMix(tmp *[8]uint64, in, out []uint64, r int) {
122 blockCopy(tmp[:], in[(2*r-1)*8:], 8)
123 for i := 0; i < 2*r; i += 2 {
124 salsaXOR(tmp, in[i*8:], out[i*4:], 8)
125 salsaXOR(tmp, in[i*8+8:], out[i*4+r*8:], 8)
126 }
127 }
128
129 // These were tunable at design time, but they must meet certain constraints
130 const (
131 PWXsimple = 2
132 PWXgather = 4
133 PWXrounds = 6
134 Swidth = 8
135 )
136
137 // Derived values. These were never tunable on their own.
138 const (
139 PWXbytes = PWXgather * PWXsimple * 8
140 PWXwords = PWXbytes / 8
141 Sbytes = 3 * (1 << Swidth) * PWXsimple * 8
142 Swords = Sbytes / 8
143 Smask = (((1 << Swidth) - 1) * PWXsimple * 8)
144 )
145
146 type pwxformCtx struct {
147 S0, S1, S2 []uint64
148 w uint32
149 }
150
151 func pwxform(X *[PWXwords]uint64, ctx *pwxformCtx) {
152 S0, S1, S2, w := ctx.S0, ctx.S1, ctx.S2, ctx.w
153
154 for i := 0; i < PWXrounds; i++ {
155 for j := 0; j < PWXgather; j++ {
156 // Unrolled inner loop for PWXsimple=2
157 x := X[j*PWXsimple]
158 xl := uint32(x)
159 xh := uint32(x >> 32)
160 x = uint64(xh) * uint64(xl)
161 xl = (xl & Smask) / 8
162 xh = (xh & Smask) / 8
163 x = (x + S0[xl]) ^ S1[xh]
164 X[j*PWXsimple] = x
165 y := X[j*PWXsimple+1]
166 y = ((y>>32)*uint64(uint32(y)) + S0[xl+1]) ^ S1[xh+1]
167 X[j*PWXsimple+1] = y
168 if i != 0 && i != PWXrounds-1 {
169 S2[w] = x
170 S2[w+1] = y
171 w += 2
172 }
173 }
174 }
175
176 ctx.S0, ctx.S1, ctx.S2 = S2, S0, S1
177 ctx.w = w & ((1<<Swidth)*PWXsimple - 1)
178 }
179
180 func blockMixPwxform(X *[PWXwords]uint64, B []uint64, r int, ctx *pwxformCtx) {
181 r1 := 128 * r / PWXbytes
182 blockCopy(X[:], B[(r1-1)*PWXwords:], PWXwords)
183 for i := 0; i < r1; i++ {
184 blockXOR(X[:], B[i*PWXwords:], PWXwords)
185 pwxform(X, ctx)
186 blockCopy(B[i*PWXwords:], X[:], PWXwords)
187 }
188 i := (r1 - 1) * PWXbytes / 64
189 *X = [PWXwords]uint64{} // We don't need the XOR, so set X to zeroes
190 salsaXOR(X, B[i*PWXwords:], B[i*PWXwords:], 2)
191 }
192
193 func integer(b []uint64, r int) uint32 {
194 j := (2*r - 1) * 8
195 return uint32(b[j])
196 }
197
198 func p2floor(x uint32) uint32 {
199 for x&(x-1) != 0 {
200 x &= x - 1
201 }
202 return x
203 }
204
205 func wrap(x, i uint32) uint32 {
206 n := p2floor(i)
207 return (x & (n - 1)) + (i - n)
208 }
209
210 func smix(b []byte, r, N, Nloop int, v, xy []uint64, ctx *pwxformCtx) {
211 var tmp [8]uint64
212 R := 16 * r
213 x := xy
214 y := xy[R:]
215
216 j := 0
217 for i := 0; i < R; i++ {
218 lo := binary.LittleEndian.Uint32(b[(j & ^63)|((j*5)&63):])
219 j += 4
220 hi := binary.LittleEndian.Uint32(b[(j & ^63)|((j*5)&63):])
221 j += 4
222 x[i] = uint64(lo) | uint64(hi)<<32
223 }
224 if ctx != nil {
225 for i := 0; i < N; i++ {
226 blockCopy(v[i*R:], x, R)
227 if i > 1 {
228 j := int(wrap(integer(x, r), uint32(i)))
229 blockXOR(x, v[j*R:], R)
230 }
231 blockMixPwxform(&tmp, x, r, ctx)
232 }
233 for i := 0; i < Nloop; i++ {
234 j := int(integer(x, r) & uint32(N-1))
235 blockXOR(x, v[j*R:], R)
236 blockCopy(v[j*R:], x, R)
237 blockMixPwxform(&tmp, x, r, ctx)
238 }
239 } else {
240 for i := 0; i < N; i += 2 {
241 blockCopy(v[i*R:], x, R)
242 blockMix(&tmp, x, y, r)
243
244 blockCopy(v[(i+1)*R:], y, R)
245 blockMix(&tmp, y, x, r)
246 }
247 for i := 0; i < Nloop; i += 2 {
248 j := int(integer(x, r) & uint32(N-1))
249 blockXOR(x, v[j*R:], R)
250 blockMix(&tmp, x, y, r)
251
252 j = int(integer(y, r) & uint32(N-1))
253 blockXOR(y, v[j*R:], R)
254 blockMix(&tmp, y, x, r)
255 }
256 }
257 j = 0
258 for _, v := range x[:R] {
259 binary.LittleEndian.PutUint32(b[(j & ^63)|((j*5)&63):], uint32(v))
260 j += 4
261 binary.LittleEndian.PutUint32(b[(j & ^63)|((j*5)&63):], uint32(v>>32))
262 j += 4
263 }
264 }
265
266 func smixYescrypt(b []byte, r, N int, v, xy []uint64, passwordSha256 []byte) {
267 var ctx pwxformCtx
268 var S [Swords]uint64
269 smix(b, 1, Sbytes/128, 0, S[:], xy, nil)
270 ctx.S2 = S[:]
271 ctx.S1 = S[(1<<Swidth)*PWXsimple:]
272 ctx.S0 = S[(1<<Swidth)*PWXsimple*2:]
273 h := hmac.New(sha256.New, b[64*(2*r-1):])
274 h.Write(passwordSha256)
275 copy(passwordSha256, h.Sum(nil))
276 smix(b, r, N, ((N+2)/3+1) & ^1, v, xy, &ctx)
277 }
278
279 func deriveKey(password, salt []byte, N, r, p, keyLen int) ([]byte, error) {
280 if N <= 1 || N&(N-1) != 0 {
281 return nil, errors.New("yescrypt: N must be > 1 and a power of 2")
282 }
283
284 if r <= 0 {
285 return nil, errors.New("yescrypt: r must be > 0")
286 }
287
288 if p != 1 {
289 return nil, errors.New("yescrypt: p must be 1")
290 }
291
292 if uint64(r)*uint64(p) >= 1<<30 || r > maxInt/128/p || r > maxInt/256 || N > maxInt/128/r {
293 return nil, errors.New("(ye)scrypt: parameters are too large")
294 }
295
296 ppassword := &password
297 pass := 1
298 prehash := []byte("yescrypt-prehash")
299
300 v := make([]uint64, 16*N*r)
301 var key []byte
302
303 xy := make([]uint64, 16*max(r, 2))
304 if N/p >= 0x100 && N/p*r >= 0x20000 {
305 pass = 0
306 N >>= 6
307 }
308
309 for pass <= 1 {
310 if pass == 1 {
311 prehash = prehash[:8]
312 }
313
314 h := hmac.New(sha256.New, prehash)
315 h.Write(*ppassword)
316 passwordSha256 := h.Sum(nil)
317 ppassword = &passwordSha256
318
319 b := pbkdf2.Key(*ppassword, salt, 1, p*128*r, sha256.New)
320
321 copy(*ppassword, b[:32])
322 smixYescrypt(b, r, N, v, xy, *ppassword)
323
324 key = pbkdf2.Key(*ppassword, b, 1, max(keyLen, 32), sha256.New)
325
326 if pass == 0 {
327 copy(*ppassword, key[:32])
328 N <<= 6
329 } else {
330 h1 := hmac.New(sha256.New, key[:32])
331 h1.Write([]byte("Client Key"))
332 h2 := sha256.New()
333 h2.Write(h1.Sum(nil))
334 copy(key, h2.Sum(nil))
335 }
336
337 pass++
338 }
339
340 return key[:keyLen], nil
341 }
342
343 // Key computes native yescrypt assuming reference yescrypt's current default
344 // flags (as of yescrypt 1.1.0), p=1 (which it currently requires), t=0, and no
345 // ROM. Example usage:
346 //
347 // dk, err := yescrypt.Key([]byte("some password"), salt, 32768, 8, 1, 32)
348 //
349 // The set of parameters accepted by Key will likely change in future versions
350 // of this Go module to support more yescrypt functionality.
351 func Key(password, salt []byte, N, r, p, keyLen int) ([]byte, error) {
352 return deriveKey(password, salt, N, r, p, keyLen)
353 }