1 // Package scrypt implements the scrypt key derivation function as defined in
2 // Colin Percival's paper "Stronger Key Derivation via Sequential Memory-Hard
3 // Functions" (https://www.tarsnap.com/scrypt/scrypt.pdf).
5 // import "golang.org/x/crypto/scrypt"
12 "golang.org/x/crypto/pbkdf2"
15 const maxInt = int(^uint(0) >> 1)
17 // blockCopy copies n numbers from src into dst.
18 func blockCopy(dst, src []uint32, n int) {
22 // blockXOR XORs numbers from dst with n numbers from src.
23 func blockXOR(dst, src []uint32, n int) {
24 for i, v := range src[:n] {
29 // salsaXOR applies Salsa20/8 to the XOR of 16 numbers from tmp and in,
30 // and puts the result into both both tmp and out.
31 func salsaXOR(tmp *[16]uint32, in, out []uint32) {
42 w10 := tmp[10] ^ in[10]
43 w11 := tmp[11] ^ in[11]
44 w12 := tmp[12] ^ in[12]
45 w13 := tmp[13] ^ in[13]
46 w14 := tmp[14] ^ in[14]
47 w15 := tmp[15] ^ in[15]
49 x0, x1, x2, x3, x4, x5, x6, x7, x8 := w0, w1, w2, w3, w4, w5, w6, w7, w8
50 x9, x10, x11, x12, x13, x14, x15 := w9, w10, w11, w12, w13, w14, w15
52 for i := 0; i < 8; i += 2 {
54 x4 ^= u<<7 | u>>(32-7)
56 x8 ^= u<<9 | u>>(32-9)
58 x12 ^= u<<13 | u>>(32-13)
60 x0 ^= u<<18 | u>>(32-18)
63 x9 ^= u<<7 | u>>(32-7)
65 x13 ^= u<<9 | u>>(32-9)
67 x1 ^= u<<13 | u>>(32-13)
69 x5 ^= u<<18 | u>>(32-18)
72 x14 ^= u<<7 | u>>(32-7)
74 x2 ^= u<<9 | u>>(32-9)
76 x6 ^= u<<13 | u>>(32-13)
78 x10 ^= u<<18 | u>>(32-18)
81 x3 ^= u<<7 | u>>(32-7)
83 x7 ^= u<<9 | u>>(32-9)
85 x11 ^= u<<13 | u>>(32-13)
87 x15 ^= u<<18 | u>>(32-18)
90 x1 ^= u<<7 | u>>(32-7)
92 x2 ^= u<<9 | u>>(32-9)
94 x3 ^= u<<13 | u>>(32-13)
96 x0 ^= u<<18 | u>>(32-18)
99 x6 ^= u<<7 | u>>(32-7)
101 x7 ^= u<<9 | u>>(32-9)
103 x4 ^= u<<13 | u>>(32-13)
105 x5 ^= u<<18 | u>>(32-18)
108 x11 ^= u<<7 | u>>(32-7)
110 x8 ^= u<<9 | u>>(32-9)
112 x9 ^= u<<13 | u>>(32-13)
114 x10 ^= u<<18 | u>>(32-18)
117 x12 ^= u<<7 | u>>(32-7)
119 x13 ^= u<<9 | u>>(32-9)
121 x14 ^= u<<13 | u>>(32-13)
123 x15 ^= u<<18 | u>>(32-18)
142 out[0], tmp[0] = x0, x0
143 out[1], tmp[1] = x1, x1
144 out[2], tmp[2] = x2, x2
145 out[3], tmp[3] = x3, x3
146 out[4], tmp[4] = x4, x4
147 out[5], tmp[5] = x5, x5
148 out[6], tmp[6] = x6, x6
149 out[7], tmp[7] = x7, x7
150 out[8], tmp[8] = x8, x8
151 out[9], tmp[9] = x9, x9
152 out[10], tmp[10] = x10, x10
153 out[11], tmp[11] = x11, x11
154 out[12], tmp[12] = x12, x12
155 out[13], tmp[13] = x13, x13
156 out[14], tmp[14] = x14, x14
157 out[15], tmp[15] = x15, x15
160 func blockMix(tmp *[16]uint32, in, out []uint32, r int) {
161 blockCopy(tmp[:], in[(2*r-1)*16:], 16)
162 for i := 0; i < 2*r; i += 2 {
163 salsaXOR(tmp, in[i*16:], out[i*8:])
164 salsaXOR(tmp, in[i*16+16:], out[i*8+r*16:])
168 func integer(b []uint32, r int) uint64 {
170 return uint64(b[j]) | uint64(b[j+1])<<32
173 func smix(b []byte, r, N int, v, xy []uint32) {
179 for i := 0; i < 32*r; i++ {
180 x[i] = uint32(b[j]) | uint32(b[j+1])<<8 | uint32(b[j+2])<<16 | uint32(b[j+3])<<24
183 for i := 0; i < N; i += 2 {
184 blockCopy(v[i*(32*r):], x, 32*r)
185 blockMix(&tmp, x, y, r)
187 blockCopy(v[(i+1)*(32*r):], y, 32*r)
188 blockMix(&tmp, y, x, r)
190 for i := 0; i < N; i += 2 {
191 j := int(integer(x, r) & uint64(N-1))
192 blockXOR(x, v[j*(32*r):], 32*r)
193 blockMix(&tmp, x, y, r)
195 j = int(integer(y, r) & uint64(N-1))
196 blockXOR(y, v[j*(32*r):], 32*r)
197 blockMix(&tmp, y, x, r)
200 for _, v := range x[:32*r] {
201 b[j+0] = byte(v >> 0)
202 b[j+1] = byte(v >> 8)
203 b[j+2] = byte(v >> 16)
204 b[j+3] = byte(v >> 24)
209 // Key derives a key from the password, salt, and cost parameters, returning
210 // a byte slice of length keyLen that can be used as cryptographic key.
212 // N is a CPU/memory cost parameter, which must be a power of two greater than 1.
213 // r and p must satisfy r * p < 2³⁰. If the parameters do not satisfy the
214 // limits, the function returns a nil byte slice and an error.
216 // For example, you can get a derived key for e.g. AES-256 (which needs a
217 // 32-byte key) by doing:
219 // dk, err := scrypt.Key([]byte("some password"), salt, 16384, 8, 1, 32)
221 // The recommended parameters for interactive logins as of 2017 are N=32768, r=8
222 // and p=1. The parameters N, r, and p should be increased as memory latency and
223 // CPU parallelism increases; consider setting N to the highest power of 2 you
224 // can derive within 100 milliseconds. Remember to get a good random salt.
225 func Key(password, salt []byte, N, r, p, keyLen int) ([]byte, error) {
226 if N <= 1 || N&(N-1) != 0 {
227 return nil, errors.New("scrypt: N must be > 1 and a power of 2")
229 if uint64(r)*uint64(p) >= 1<<30 || r > maxInt/128/p || r > maxInt/256 || N > maxInt/128/r {
230 return nil, errors.New("scrypt: parameters are too large")
233 xy := make([]uint32, 64*r)
234 v := make([]uint32, 32*N*r)
235 b := pbkdf2.Key(password, salt, 1, p*128*r, sha256.New)
237 for i := 0; i < p; i++ {
238 smix(b[i*128*r:], r, N, v, xy)
241 return pbkdf2.Key(password, b, 1, keyLen, sha256.New), nil