rabbit.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*!
  2. * Crypto-JS v1.1.0
  3. * http://code.google.com/p/crypto-js/
  4. * Copyright (c) 2009, Jeff Mott. All rights reserved.
  5. * http://code.google.com/p/crypto-js/wiki/License
  6. */
  7. (function(){
  8. // Shortcut
  9. var util = Crypto.util;
  10. // Inner state
  11. var x = [],
  12. c = [],
  13. b;
  14. var Rabbit = Crypto.Rabbit = {
  15. /**
  16. * Public API
  17. */
  18. encrypt: function (message, password) {
  19. var
  20. // Convert to bytes
  21. m = util.stringToBytes(message),
  22. // Generate random IV
  23. iv = util.randomBytes(8),
  24. // Generate key
  25. k = Crypto.PBKDF2(password, util.bytesToString(iv), 16, { asBytes: true });
  26. // Encrypt
  27. Rabbit._rabbit(m, k, util.bytesToWords(iv));
  28. // Return ciphertext
  29. return util.bytesToBase64(iv.concat(m));
  30. },
  31. decrypt: function (ciphertext, password) {
  32. var
  33. // Convert to bytes
  34. c = util.base64ToBytes(ciphertext),
  35. // Separate IV and message
  36. iv = c.splice(0, 8),
  37. // Generate key
  38. k = Crypto.PBKDF2(password, util.bytesToString(iv), 16, { asBytes: true });
  39. // Decrypt
  40. Rabbit._rabbit(c, k, util.bytesToWords(iv));
  41. // Return plaintext
  42. return util.bytesToString(c);
  43. },
  44. /**
  45. * Internal methods
  46. */
  47. // Encryption/decryption scheme
  48. _rabbit: function (m, k, iv) {
  49. Rabbit._keysetup(k);
  50. if (iv) Rabbit._ivsetup(iv);
  51. for (var s = [], i = 0; i < m.length; i++) {
  52. if (i % 16 == 0) {
  53. // Iterate the system
  54. Rabbit._nextstate();
  55. // Generate 16 bytes of pseudo-random data
  56. s[0] = x[0] ^ (x[5] >>> 16) ^ (x[3] << 16);
  57. s[1] = x[2] ^ (x[7] >>> 16) ^ (x[5] << 16);
  58. s[2] = x[4] ^ (x[1] >>> 16) ^ (x[7] << 16);
  59. s[3] = x[6] ^ (x[3] >>> 16) ^ (x[1] << 16);
  60. // Swap endian
  61. for (var j = 0; j < 4; j++) {
  62. s[j] = ((s[j] << 8) | (s[j] >>> 24)) & 0x00FF00FF |
  63. ((s[j] << 24) | (s[j] >>> 8)) & 0xFF00FF00;
  64. }
  65. // Convert words to bytes
  66. for (var b = 120; b >= 0; b -= 8)
  67. s[b / 8] = (s[b >>> 5] >>> (24 - b % 32)) & 0xFF;
  68. }
  69. m[i] ^= s[i % 16];
  70. }
  71. },
  72. // Key setup scheme
  73. _keysetup: function (k) {
  74. // Generate initial state values
  75. x[0] = k[0];
  76. x[2] = k[1];
  77. x[4] = k[2];
  78. x[6] = k[3];
  79. x[1] = (k[3] << 16) | (k[2] >>> 16);
  80. x[3] = (k[0] << 16) | (k[3] >>> 16);
  81. x[5] = (k[1] << 16) | (k[0] >>> 16);
  82. x[7] = (k[2] << 16) | (k[1] >>> 16);
  83. // Generate initial counter values
  84. c[0] = util.rotl(k[2], 16);
  85. c[2] = util.rotl(k[3], 16);
  86. c[4] = util.rotl(k[0], 16);
  87. c[6] = util.rotl(k[1], 16);
  88. c[1] = (k[0] & 0xFFFF0000) | (k[1] & 0xFFFF);
  89. c[3] = (k[1] & 0xFFFF0000) | (k[2] & 0xFFFF);
  90. c[5] = (k[2] & 0xFFFF0000) | (k[3] & 0xFFFF);
  91. c[7] = (k[3] & 0xFFFF0000) | (k[0] & 0xFFFF);
  92. // Clear carry bit
  93. b = 0;
  94. // Iterate the system four times
  95. for (var i = 0; i < 4; i++) Rabbit._nextstate();
  96. // Modify the counters
  97. for (var i = 0; i < 8; i++) c[i] ^= x[(i + 4) & 7];
  98. },
  99. // IV setup scheme
  100. _ivsetup: function (iv) {
  101. // Generate four subvectors
  102. var i0 = util.endian(iv[0]),
  103. i2 = util.endian(iv[1]),
  104. i1 = (i0 >>> 16) | (i2 & 0xFFFF0000),
  105. i3 = (i2 << 16) | (i0 & 0x0000FFFF);
  106. // Modify counter values
  107. c[0] ^= i0;
  108. c[1] ^= i1;
  109. c[2] ^= i2;
  110. c[3] ^= i3;
  111. c[4] ^= i0;
  112. c[5] ^= i1;
  113. c[6] ^= i2;
  114. c[7] ^= i3;
  115. // Iterate the system four times
  116. for (var i = 0; i < 4; i++) Rabbit._nextstate();
  117. },
  118. // Next-state function
  119. _nextstate: function () {
  120. // Save old counter values
  121. for (var c_old = [], i = 0; i < 8; i++) c_old[i] = c[i];
  122. // Calculate new counter values
  123. c[0] = (c[0] + 0x4D34D34D + b) >>> 0;
  124. c[1] = (c[1] + 0xD34D34D3 + ((c[0] >>> 0) < (c_old[0] >>> 0) ? 1 : 0)) >>> 0;
  125. c[2] = (c[2] + 0x34D34D34 + ((c[1] >>> 0) < (c_old[1] >>> 0) ? 1 : 0)) >>> 0;
  126. c[3] = (c[3] + 0x4D34D34D + ((c[2] >>> 0) < (c_old[2] >>> 0) ? 1 : 0)) >>> 0;
  127. c[4] = (c[4] + 0xD34D34D3 + ((c[3] >>> 0) < (c_old[3] >>> 0) ? 1 : 0)) >>> 0;
  128. c[5] = (c[5] + 0x34D34D34 + ((c[4] >>> 0) < (c_old[4] >>> 0) ? 1 : 0)) >>> 0;
  129. c[6] = (c[6] + 0x4D34D34D + ((c[5] >>> 0) < (c_old[5] >>> 0) ? 1 : 0)) >>> 0;
  130. c[7] = (c[7] + 0xD34D34D3 + ((c[6] >>> 0) < (c_old[6] >>> 0) ? 1 : 0)) >>> 0;
  131. b = (c[7] >>> 0) < (c_old[7] >>> 0) ? 1 : 0;
  132. // Calculate the g-values
  133. for (var g = [], i = 0; i < 8; i++) {
  134. var gx = (x[i] + c[i]) >>> 0;
  135. // Construct high and low argument for squaring
  136. var ga = gx & 0xFFFF,
  137. gb = gx >>> 16;
  138. // Calculate high and low result of squaring
  139. var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb,
  140. gl = (((gx & 0xFFFF0000) * gx) >>> 0) + (((gx & 0x0000FFFF) * gx) >>> 0) >>> 0;
  141. // High XOR low
  142. g[i] = gh ^ gl;
  143. }
  144. // Calculate new state values
  145. x[0] = g[0] + ((g[7] << 16) | (g[7] >>> 16)) + ((g[6] << 16) | (g[6] >>> 16));
  146. x[1] = g[1] + ((g[0] << 8) | (g[0] >>> 24)) + g[7];
  147. x[2] = g[2] + ((g[1] << 16) | (g[1] >>> 16)) + ((g[0] << 16) | (g[0] >>> 16));
  148. x[3] = g[3] + ((g[2] << 8) | (g[2] >>> 24)) + g[1];
  149. x[4] = g[4] + ((g[3] << 16) | (g[3] >>> 16)) + ((g[2] << 16) | (g[2] >>> 16));
  150. x[5] = g[5] + ((g[4] << 8) | (g[4] >>> 24)) + g[3];
  151. x[6] = g[6] + ((g[5] << 16) | (g[5] >>> 16)) + ((g[4] << 16) | (g[4] >>> 16));
  152. x[7] = g[7] + ((g[6] << 8) | (g[6] >>> 24)) + g[5];
  153. }
  154. };
  155. })();