github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/crypto/scrypt/scrypt.go (about)

     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).
     4  
     5  // import "golang.org/x/crypto/scrypt"
     6  package scrypt
     7  
     8  import (
     9  	"crypto/sha256"
    10  	"errors"
    11  
    12  	"golang.org/x/crypto/pbkdf2"
    13  )
    14  
    15  const maxInt = int(^uint(0) >> 1)
    16  
    17  // blockCopy copies n numbers from src into dst.
    18  func blockCopy(dst, src []uint32, n int) {
    19  	copy(dst, src[:n])
    20  }
    21  
    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] {
    25  		dst[i] ^= v
    26  	}
    27  }
    28  
    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) {
    32  	w0 := tmp[0] ^ in[0]
    33  	w1 := tmp[1] ^ in[1]
    34  	w2 := tmp[2] ^ in[2]
    35  	w3 := tmp[3] ^ in[3]
    36  	w4 := tmp[4] ^ in[4]
    37  	w5 := tmp[5] ^ in[5]
    38  	w6 := tmp[6] ^ in[6]
    39  	w7 := tmp[7] ^ in[7]
    40  	w8 := tmp[8] ^ in[8]
    41  	w9 := tmp[9] ^ in[9]
    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]
    48  
    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
    51  
    52  	for i := 0; i < 8; i += 2 {
    53  		u := x0 + x12
    54  		x4 ^= u<<7 | u>>(32-7)
    55  		u = x4 + x0
    56  		x8 ^= u<<9 | u>>(32-9)
    57  		u = x8 + x4
    58  		x12 ^= u<<13 | u>>(32-13)
    59  		u = x12 + x8
    60  		x0 ^= u<<18 | u>>(32-18)
    61  
    62  		u = x5 + x1
    63  		x9 ^= u<<7 | u>>(32-7)
    64  		u = x9 + x5
    65  		x13 ^= u<<9 | u>>(32-9)
    66  		u = x13 + x9
    67  		x1 ^= u<<13 | u>>(32-13)
    68  		u = x1 + x13
    69  		x5 ^= u<<18 | u>>(32-18)
    70  
    71  		u = x10 + x6
    72  		x14 ^= u<<7 | u>>(32-7)
    73  		u = x14 + x10
    74  		x2 ^= u<<9 | u>>(32-9)
    75  		u = x2 + x14
    76  		x6 ^= u<<13 | u>>(32-13)
    77  		u = x6 + x2
    78  		x10 ^= u<<18 | u>>(32-18)
    79  
    80  		u = x15 + x11
    81  		x3 ^= u<<7 | u>>(32-7)
    82  		u = x3 + x15
    83  		x7 ^= u<<9 | u>>(32-9)
    84  		u = x7 + x3
    85  		x11 ^= u<<13 | u>>(32-13)
    86  		u = x11 + x7
    87  		x15 ^= u<<18 | u>>(32-18)
    88  
    89  		u = x0 + x3
    90  		x1 ^= u<<7 | u>>(32-7)
    91  		u = x1 + x0
    92  		x2 ^= u<<9 | u>>(32-9)
    93  		u = x2 + x1
    94  		x3 ^= u<<13 | u>>(32-13)
    95  		u = x3 + x2
    96  		x0 ^= u<<18 | u>>(32-18)
    97  
    98  		u = x5 + x4
    99  		x6 ^= u<<7 | u>>(32-7)
   100  		u = x6 + x5
   101  		x7 ^= u<<9 | u>>(32-9)
   102  		u = x7 + x6
   103  		x4 ^= u<<13 | u>>(32-13)
   104  		u = x4 + x7
   105  		x5 ^= u<<18 | u>>(32-18)
   106  
   107  		u = x10 + x9
   108  		x11 ^= u<<7 | u>>(32-7)
   109  		u = x11 + x10
   110  		x8 ^= u<<9 | u>>(32-9)
   111  		u = x8 + x11
   112  		x9 ^= u<<13 | u>>(32-13)
   113  		u = x9 + x8
   114  		x10 ^= u<<18 | u>>(32-18)
   115  
   116  		u = x15 + x14
   117  		x12 ^= u<<7 | u>>(32-7)
   118  		u = x12 + x15
   119  		x13 ^= u<<9 | u>>(32-9)
   120  		u = x13 + x12
   121  		x14 ^= u<<13 | u>>(32-13)
   122  		u = x14 + x13
   123  		x15 ^= u<<18 | u>>(32-18)
   124  	}
   125  	x0 += w0
   126  	x1 += w1
   127  	x2 += w2
   128  	x3 += w3
   129  	x4 += w4
   130  	x5 += w5
   131  	x6 += w6
   132  	x7 += w7
   133  	x8 += w8
   134  	x9 += w9
   135  	x10 += w10
   136  	x11 += w11
   137  	x12 += w12
   138  	x13 += w13
   139  	x14 += w14
   140  	x15 += w15
   141  
   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
   158  }
   159  
   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:])
   165  	}
   166  }
   167  
   168  func integer(b []uint32, r int) uint64 {
   169  	j := (2*r - 1) * 16
   170  	return uint64(b[j]) | uint64(b[j+1])<<32
   171  }
   172  
   173  func smix(b []byte, r, N int, v, xy []uint32) {
   174  	var tmp [16]uint32
   175  	x := xy
   176  	y := xy[32*r:]
   177  
   178  	j := 0
   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
   181  		j += 4
   182  	}
   183  	for i := 0; i < N; i += 2 {
   184  		blockCopy(v[i*(32*r):], x, 32*r)
   185  		blockMix(&tmp, x, y, r)
   186  
   187  		blockCopy(v[(i+1)*(32*r):], y, 32*r)
   188  		blockMix(&tmp, y, x, r)
   189  	}
   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)
   194  
   195  		j = int(integer(y, r) & uint64(N-1))
   196  		blockXOR(y, v[j*(32*r):], 32*r)
   197  		blockMix(&tmp, y, x, r)
   198  	}
   199  	j = 0
   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)
   205  		j += 4
   206  	}
   207  }
   208  
   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.
   211  //
   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.
   215  //
   216  // For example, you can get a derived key for e.g. AES-256 (which needs a
   217  // 32-byte key) by doing:
   218  //
   219  //      dk, err := scrypt.Key([]byte("some password"), salt, 16384, 8, 1, 32)
   220  //
   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")
   228  	}
   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")
   231  	}
   232  
   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)
   236  
   237  	for i := 0; i < p; i++ {
   238  		smix(b[i*128*r:], r, N, v, xy)
   239  	}
   240  
   241  	return pbkdf2.Key(password, b, 1, keyLen, sha256.New), nil
   242  }