Source: scrypt.js

  1. /** scrypt Password-Based Key-Derivation Function.
  2. *
  3. * @param {bitArray|String} password The password.
  4. * @param {bitArray|String} salt The salt. Should have lots of entropy.
  5. *
  6. * @param {Number} [N=16384] CPU/Memory cost parameter.
  7. * @param {Number} [r=8] Block size parameter.
  8. * @param {Number} [p=1] Parallelization parameter.
  9. *
  10. * @param {Number} [length] The length of the derived key. Defaults to the
  11. * output size of the hash function.
  12. * @param {Object} [Prff=sjcl.misc.hmac] The pseudorandom function family.
  13. *
  14. * @return {bitArray} The derived key.
  15. */
  16. sjcl.misc.scrypt = function (password, salt, N, r, p, length, Prff) {
  17. var SIZE_MAX = Math.pow(2, 32) - 1,
  18. self = sjcl.misc.scrypt;
  19. N = N || 16384;
  20. r = r || 8;
  21. p = p || 1;
  22. if (r * p >= Math.pow(2, 30)) {
  23. throw sjcl.exception.invalid("The parameters r, p must satisfy r * p < 2^30");
  24. }
  25. if ((N < 2) || (N & (N - 1) != 0)) {
  26. throw sjcl.exception.invalid("The parameter N must be a power of 2.");
  27. }
  28. if (N > SIZE_MAX / 128 / r) {
  29. throw sjcl.exception.invalid("N too big.");
  30. }
  31. if (r > SIZE_MAX / 128 / p) {
  32. throw sjcl.exception.invalid("r too big.");
  33. }
  34. var blocks = sjcl.misc.pbkdf2(password, salt, 1, p * 128 * r * 8, Prff),
  35. len = blocks.length / p;
  36. self.reverse(blocks);
  37. for (var i = 0; i < p; i++) {
  38. var block = blocks.slice(i * len, (i + 1) * len);
  39. self.blockcopy(self.ROMix(block, N), 0, blocks, i * len);
  40. }
  41. self.reverse(blocks);
  42. return sjcl.misc.pbkdf2(password, blocks, 1, length, Prff);
  43. };
  44. sjcl.misc.scrypt.salsa20Core = function (word, rounds) {
  45. var R = function(a, b) { return (a << b) | (a >>> (32 - b)); };
  46. var x = word.slice(0);
  47. for (var i = rounds; i > 0; i -= 2) {
  48. x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
  49. x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18);
  50. x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9);
  51. x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18);
  52. x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9);
  53. x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18);
  54. x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9);
  55. x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18);
  56. x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9);
  57. x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
  58. x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
  59. x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18);
  60. x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9);
  61. x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18);
  62. x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
  63. x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
  64. }
  65. for (i = 0; i < 16; i++) word[i] = x[i]+word[i];
  66. };
  67. sjcl.misc.scrypt.blockMix = function(blocks) {
  68. var X = blocks.slice(-16),
  69. out = [],
  70. len = blocks.length / 16,
  71. self = sjcl.misc.scrypt;
  72. for (var i = 0; i < len; i++) {
  73. self.blockxor(blocks, 16 * i, X, 0, 16);
  74. self.salsa20Core(X, 8);
  75. if ((i & 1) == 0) {
  76. self.blockcopy(X, 0, out, 8 * i);
  77. } else {
  78. self.blockcopy(X, 0, out, 8 * (i^1 + len));
  79. }
  80. }
  81. return out;
  82. };
  83. sjcl.misc.scrypt.ROMix = function(block, N) {
  84. var X = block.slice(0),
  85. V = [],
  86. self = sjcl.misc.scrypt;
  87. for (var i = 0; i < N; i++) {
  88. V.push(X.slice(0));
  89. X = self.blockMix(X);
  90. }
  91. for (i = 0; i < N; i++) {
  92. var j = X[X.length - 16] & (N - 1);
  93. self.blockxor(V[j], 0, X, 0);
  94. X = self.blockMix(X);
  95. }
  96. return X;
  97. };
  98. sjcl.misc.scrypt.reverse = function (words) { // Converts Big <-> Little Endian words
  99. for (var i in words) {
  100. var out = words[i] & 0xFF;
  101. out = (out << 8) | (words[i] >>> 8) & 0xFF;
  102. out = (out << 8) | (words[i] >>> 16) & 0xFF;
  103. out = (out << 8) | (words[i] >>> 24) & 0xFF;
  104. words[i] = out;
  105. }
  106. };
  107. sjcl.misc.scrypt.blockcopy = function (S, Si, D, Di, len) {
  108. var i;
  109. len = len || (S.length - Si);
  110. for (i = 0; i < len; i++) D[Di + i] = S[Si + i] | 0;
  111. };
  112. sjcl.misc.scrypt.blockxor = function(S, Si, D, Di, len) {
  113. var i;
  114. len = len || (S.length - Si);
  115. for (i = 0; i < len; i++) D[Di + i] = (D[Di + i] ^ S[Si + i]) | 0;
  116. };