scrypt.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2012 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. // Package scrypt implements the scrypt key derivation function as defined in
  5. // Colin Percival's paper "Stronger Key Derivation via Sequential Memory-Hard
  6. // Functions" (https://www.tarsnap.com/scrypt/scrypt.pdf).
  7. package scrypt // import "golang.org/x/crypto/scrypt"
  8. import (
  9. "crypto/sha256"
  10. "encoding/binary"
  11. "errors"
  12. "math/bits"
  13. "golang.org/x/crypto/pbkdf2"
  14. )
  15. const maxInt = int(^uint(0) >> 1)
  16. // blockCopy copies n numbers from src into dst.
  17. func blockCopy(dst, src []uint32, n int) {
  18. copy(dst, src[:n])
  19. }
  20. // blockXOR XORs numbers from dst with n numbers from src.
  21. func blockXOR(dst, src []uint32, n int) {
  22. for i, v := range src[:n] {
  23. dst[i] ^= v
  24. }
  25. }
  26. // salsaXOR applies Salsa20/8 to the XOR of 16 numbers from tmp and in,
  27. // and puts the result into both tmp and out.
  28. func salsaXOR(tmp *[16]uint32, in, out []uint32) {
  29. w0 := tmp[0] ^ in[0]
  30. w1 := tmp[1] ^ in[1]
  31. w2 := tmp[2] ^ in[2]
  32. w3 := tmp[3] ^ in[3]
  33. w4 := tmp[4] ^ in[4]
  34. w5 := tmp[5] ^ in[5]
  35. w6 := tmp[6] ^ in[6]
  36. w7 := tmp[7] ^ in[7]
  37. w8 := tmp[8] ^ in[8]
  38. w9 := tmp[9] ^ in[9]
  39. w10 := tmp[10] ^ in[10]
  40. w11 := tmp[11] ^ in[11]
  41. w12 := tmp[12] ^ in[12]
  42. w13 := tmp[13] ^ in[13]
  43. w14 := tmp[14] ^ in[14]
  44. w15 := tmp[15] ^ in[15]
  45. x0, x1, x2, x3, x4, x5, x6, x7, x8 := w0, w1, w2, w3, w4, w5, w6, w7, w8
  46. x9, x10, x11, x12, x13, x14, x15 := w9, w10, w11, w12, w13, w14, w15
  47. for i := 0; i < 8; i += 2 {
  48. x4 ^= bits.RotateLeft32(x0+x12, 7)
  49. x8 ^= bits.RotateLeft32(x4+x0, 9)
  50. x12 ^= bits.RotateLeft32(x8+x4, 13)
  51. x0 ^= bits.RotateLeft32(x12+x8, 18)
  52. x9 ^= bits.RotateLeft32(x5+x1, 7)
  53. x13 ^= bits.RotateLeft32(x9+x5, 9)
  54. x1 ^= bits.RotateLeft32(x13+x9, 13)
  55. x5 ^= bits.RotateLeft32(x1+x13, 18)
  56. x14 ^= bits.RotateLeft32(x10+x6, 7)
  57. x2 ^= bits.RotateLeft32(x14+x10, 9)
  58. x6 ^= bits.RotateLeft32(x2+x14, 13)
  59. x10 ^= bits.RotateLeft32(x6+x2, 18)
  60. x3 ^= bits.RotateLeft32(x15+x11, 7)
  61. x7 ^= bits.RotateLeft32(x3+x15, 9)
  62. x11 ^= bits.RotateLeft32(x7+x3, 13)
  63. x15 ^= bits.RotateLeft32(x11+x7, 18)
  64. x1 ^= bits.RotateLeft32(x0+x3, 7)
  65. x2 ^= bits.RotateLeft32(x1+x0, 9)
  66. x3 ^= bits.RotateLeft32(x2+x1, 13)
  67. x0 ^= bits.RotateLeft32(x3+x2, 18)
  68. x6 ^= bits.RotateLeft32(x5+x4, 7)
  69. x7 ^= bits.RotateLeft32(x6+x5, 9)
  70. x4 ^= bits.RotateLeft32(x7+x6, 13)
  71. x5 ^= bits.RotateLeft32(x4+x7, 18)
  72. x11 ^= bits.RotateLeft32(x10+x9, 7)
  73. x8 ^= bits.RotateLeft32(x11+x10, 9)
  74. x9 ^= bits.RotateLeft32(x8+x11, 13)
  75. x10 ^= bits.RotateLeft32(x9+x8, 18)
  76. x12 ^= bits.RotateLeft32(x15+x14, 7)
  77. x13 ^= bits.RotateLeft32(x12+x15, 9)
  78. x14 ^= bits.RotateLeft32(x13+x12, 13)
  79. x15 ^= bits.RotateLeft32(x14+x13, 18)
  80. }
  81. x0 += w0
  82. x1 += w1
  83. x2 += w2
  84. x3 += w3
  85. x4 += w4
  86. x5 += w5
  87. x6 += w6
  88. x7 += w7
  89. x8 += w8
  90. x9 += w9
  91. x10 += w10
  92. x11 += w11
  93. x12 += w12
  94. x13 += w13
  95. x14 += w14
  96. x15 += w15
  97. out[0], tmp[0] = x0, x0
  98. out[1], tmp[1] = x1, x1
  99. out[2], tmp[2] = x2, x2
  100. out[3], tmp[3] = x3, x3
  101. out[4], tmp[4] = x4, x4
  102. out[5], tmp[5] = x5, x5
  103. out[6], tmp[6] = x6, x6
  104. out[7], tmp[7] = x7, x7
  105. out[8], tmp[8] = x8, x8
  106. out[9], tmp[9] = x9, x9
  107. out[10], tmp[10] = x10, x10
  108. out[11], tmp[11] = x11, x11
  109. out[12], tmp[12] = x12, x12
  110. out[13], tmp[13] = x13, x13
  111. out[14], tmp[14] = x14, x14
  112. out[15], tmp[15] = x15, x15
  113. }
  114. func blockMix(tmp *[16]uint32, in, out []uint32, r int) {
  115. blockCopy(tmp[:], in[(2*r-1)*16:], 16)
  116. for i := 0; i < 2*r; i += 2 {
  117. salsaXOR(tmp, in[i*16:], out[i*8:])
  118. salsaXOR(tmp, in[i*16+16:], out[i*8+r*16:])
  119. }
  120. }
  121. func integer(b []uint32, r int) uint64 {
  122. j := (2*r - 1) * 16
  123. return uint64(b[j]) | uint64(b[j+1])<<32
  124. }
  125. func smix(b []byte, r, N int, v, xy []uint32) {
  126. var tmp [16]uint32
  127. R := 32 * r
  128. x := xy
  129. y := xy[R:]
  130. j := 0
  131. for i := 0; i < R; i++ {
  132. x[i] = binary.LittleEndian.Uint32(b[j:])
  133. j += 4
  134. }
  135. for i := 0; i < N; i += 2 {
  136. blockCopy(v[i*R:], x, R)
  137. blockMix(&tmp, x, y, r)
  138. blockCopy(v[(i+1)*R:], y, R)
  139. blockMix(&tmp, y, x, r)
  140. }
  141. for i := 0; i < N; i += 2 {
  142. j := int(integer(x, r) & uint64(N-1))
  143. blockXOR(x, v[j*R:], R)
  144. blockMix(&tmp, x, y, r)
  145. j = int(integer(y, r) & uint64(N-1))
  146. blockXOR(y, v[j*R:], R)
  147. blockMix(&tmp, y, x, r)
  148. }
  149. j = 0
  150. for _, v := range x[:R] {
  151. binary.LittleEndian.PutUint32(b[j:], v)
  152. j += 4
  153. }
  154. }
  155. // Key derives a key from the password, salt, and cost parameters, returning
  156. // a byte slice of length keyLen that can be used as cryptographic key.
  157. //
  158. // N is a CPU/memory cost parameter, which must be a power of two greater than 1.
  159. // r and p must satisfy r * p < 2³⁰. If the parameters do not satisfy the
  160. // limits, the function returns a nil byte slice and an error.
  161. //
  162. // For example, you can get a derived key for e.g. AES-256 (which needs a
  163. // 32-byte key) by doing:
  164. //
  165. // dk, err := scrypt.Key([]byte("some password"), salt, 32768, 8, 1, 32)
  166. //
  167. // The recommended parameters for interactive logins as of 2017 are N=32768, r=8
  168. // and p=1. The parameters N, r, and p should be increased as memory latency and
  169. // CPU parallelism increases; consider setting N to the highest power of 2 you
  170. // can derive within 100 milliseconds. Remember to get a good random salt.
  171. func Key(password, salt []byte, N, r, p, keyLen int) ([]byte, error) {
  172. if N <= 1 || N&(N-1) != 0 {
  173. return nil, errors.New("scrypt: N must be > 1 and a power of 2")
  174. }
  175. if uint64(r)*uint64(p) >= 1<<30 || r > maxInt/128/p || r > maxInt/256 || N > maxInt/128/r {
  176. return nil, errors.New("scrypt: parameters are too large")
  177. }
  178. xy := make([]uint32, 64*r)
  179. v := make([]uint32, 32*N*r)
  180. b := pbkdf2.Key(password, salt, 1, p*128*r, sha256.New)
  181. for i := 0; i < p; i++ {
  182. smix(b[i*128*r:], r, N, v, xy)
  183. }
  184. return pbkdf2.Key(password, b, 1, keyLen, sha256.New), nil
  185. }