本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.7KB

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