scrypt.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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" (http://www.tarsnap.com/scrypt/scrypt.pdf).
  7. package scrypt
  8. import (
  9. "crypto/sha256"
  10. "encoding/binary"
  11. "errors"
  12. "code.google.com/p/go.crypto/pbkdf2"
  13. "code.google.com/p/go.crypto/salsa20/salsa"
  14. )
  15. const maxInt = int(^uint(0) >> 1)
  16. // blockCopy copies n bytes from src into dst.
  17. func blockCopy(dst, src []byte, n int) {
  18. copy(dst, src[:n])
  19. }
  20. // blockXOR XORs bytes from dst with n bytes from src.
  21. func blockXOR(dst, src []byte, n int) {
  22. for i, v := range src[:n] {
  23. dst[i] ^= v
  24. }
  25. }
  26. func blockMix(b, y []byte, r int) {
  27. var x [64]byte
  28. blockCopy(x[:], b[(2*r-1)*64:], 64)
  29. for i := 0; i < 2*r*64; i += 64 {
  30. blockXOR(x[:], b[i:], 64)
  31. salsa.Core208(&x, &x)
  32. blockCopy(y[i:], x[:], 64)
  33. }
  34. for i := 0; i < r; i++ {
  35. blockCopy(b[i*64:], y[i*2*64:], 64)
  36. }
  37. for i := 0; i < r; i++ {
  38. blockCopy(b[(i+r)*64:], y[(i*2+1)*64:], 64)
  39. }
  40. }
  41. func integer(b []byte, r int) uint64 {
  42. return binary.LittleEndian.Uint64(b[(2*r-1)*64:])
  43. }
  44. func smix(b []byte, r, N int, v, xy []byte) {
  45. x := xy
  46. y := xy[128*r:]
  47. blockCopy(x, b, 128*r)
  48. for i := 0; i < N; i++ {
  49. blockCopy(v[i*128*r:], x, 128*r)
  50. blockMix(x, y, r)
  51. }
  52. for i := 0; i < N; i++ {
  53. j := int(integer(x, r) & uint64(N-1))
  54. blockXOR(x, v[j*128*r:], 128*r)
  55. blockMix(x, y, r)
  56. }
  57. blockCopy(b, x, 128*r)
  58. }
  59. // Key derives a key from the password, salt, and cost parameters, returning
  60. // a byte slice of length keyLen that can be used as cryptographic key.
  61. //
  62. // N is a CPU/memory cost parameter, which must be a power of two greater than 1.
  63. // r and p must satisfy r * p < 2³⁰. If the parameters do not satisfy the
  64. // limits, the function returns a nil byte slice and an error.
  65. //
  66. // For example, you can get a derived key for e.g. AES-256 (which needs a
  67. // 32-byte key) by doing:
  68. //
  69. // dk := scrypt.Key([]byte("some password"), salt, 16384, 8, 1, 32)
  70. //
  71. // The recommended parameters for interactive logins as of 2009 are N=16384,
  72. // r=8, p=1. They should be increased as memory latency and CPU parallelism
  73. // increases. Remember to get a good random salt.
  74. func Key(password, salt []byte, N, r, p, keyLen int) ([]byte, error) {
  75. if N <= 1 || N&(N-1) != 0 {
  76. return nil, errors.New("scrypt: N must be > 1 and a power of 2")
  77. }
  78. if uint64(r)*uint64(p) >= 1<<30 || r > maxInt/128/p || r > maxInt/256 || N > maxInt/128/r {
  79. return nil, errors.New("scrypt: parameters are too large")
  80. }
  81. xy := make([]byte, 256*r)
  82. v := make([]byte, 128*r*N)
  83. b := pbkdf2.Key(password, salt, 1, p*128*r, sha256.New)
  84. for i := 0; i < p; i++ {
  85. smix(b[i*128*r:], r, N, v, xy)
  86. }
  87. return pbkdf2.Key(password, b, 1, keyLen, sha256.New), nil
  88. }