rabbit-legacy.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ;(function (root, factory, undef) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core"));
  5. } else if (typeof define === "function" && define.amd) {
  6. // AMD
  7. define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory);
  8. } else {
  9. // Global (browser)
  10. factory(root.CryptoJS);
  11. }
  12. }(this, function (CryptoJS) {
  13. (function () {
  14. // Shortcuts
  15. var C = CryptoJS;
  16. var C_lib = C.lib;
  17. var StreamCipher = C_lib.StreamCipher;
  18. var C_algo = C.algo;
  19. // Reusable objects
  20. var S = [];
  21. var C_ = [];
  22. var G = [];
  23. /**
  24. * Rabbit stream cipher algorithm.
  25. *
  26. * This is a legacy version that neglected to convert the key to little-endian.
  27. * This error doesn't affect the cipher's security,
  28. * but it does affect its compatibility with other implementations.
  29. */
  30. var RabbitLegacy = C_algo.RabbitLegacy = StreamCipher.extend({
  31. _doReset: function () {
  32. // Shortcuts
  33. var K = this._key.words;
  34. var iv = this.cfg.iv;
  35. // Generate initial state values
  36. var X = this._X = [
  37. K[0], (K[3] << 16) | (K[2] >>> 16),
  38. K[1], (K[0] << 16) | (K[3] >>> 16),
  39. K[2], (K[1] << 16) | (K[0] >>> 16),
  40. K[3], (K[2] << 16) | (K[1] >>> 16)
  41. ];
  42. // Generate initial counter values
  43. var C = this._C = [
  44. (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff),
  45. (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff),
  46. (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff),
  47. (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff)
  48. ];
  49. // Carry bit
  50. this._b = 0;
  51. // Iterate the system four times
  52. for (var i = 0; i < 4; i++) {
  53. nextState.call(this);
  54. }
  55. // Modify the counters
  56. for (var i = 0; i < 8; i++) {
  57. C[i] ^= X[(i + 4) & 7];
  58. }
  59. // IV setup
  60. if (iv) {
  61. // Shortcuts
  62. var IV = iv.words;
  63. var IV_0 = IV[0];
  64. var IV_1 = IV[1];
  65. // Generate four subvectors
  66. var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00);
  67. var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00);
  68. var i1 = (i0 >>> 16) | (i2 & 0xffff0000);
  69. var i3 = (i2 << 16) | (i0 & 0x0000ffff);
  70. // Modify counter values
  71. C[0] ^= i0;
  72. C[1] ^= i1;
  73. C[2] ^= i2;
  74. C[3] ^= i3;
  75. C[4] ^= i0;
  76. C[5] ^= i1;
  77. C[6] ^= i2;
  78. C[7] ^= i3;
  79. // Iterate the system four times
  80. for (var i = 0; i < 4; i++) {
  81. nextState.call(this);
  82. }
  83. }
  84. },
  85. _doProcessBlock: function (M, offset) {
  86. // Shortcut
  87. var X = this._X;
  88. // Iterate the system
  89. nextState.call(this);
  90. // Generate four keystream words
  91. S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16);
  92. S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16);
  93. S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16);
  94. S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16);
  95. for (var i = 0; i < 4; i++) {
  96. // Swap endian
  97. S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) |
  98. (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00);
  99. // Encrypt
  100. M[offset + i] ^= S[i];
  101. }
  102. },
  103. blockSize: 128 / 32,
  104. ivSize: 64 / 32
  105. });
  106. function nextState() {
  107. // Shortcuts
  108. var X = this._X;
  109. var C = this._C;
  110. // Save old counter values
  111. for (var i = 0; i < 8; i++) {
  112. C_[i] = C[i];
  113. }
  114. // Calculate new counter values
  115. C[0] = (C[0] + 0x4d34d34d + this._b) | 0;
  116. C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0;
  117. C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0;
  118. C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0;
  119. C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0;
  120. C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0;
  121. C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0;
  122. C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0;
  123. this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0;
  124. // Calculate the g-values
  125. for (var i = 0; i < 8; i++) {
  126. var gx = X[i] + C[i];
  127. // Construct high and low argument for squaring
  128. var ga = gx & 0xffff;
  129. var gb = gx >>> 16;
  130. // Calculate high and low result of squaring
  131. var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb;
  132. var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0);
  133. // High XOR low
  134. G[i] = gh ^ gl;
  135. }
  136. // Calculate new state values
  137. X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0;
  138. X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0;
  139. X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0;
  140. X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0;
  141. X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0;
  142. X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0;
  143. X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0;
  144. X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0;
  145. }
  146. /**
  147. * Shortcut functions to the cipher's object interface.
  148. *
  149. * @example
  150. *
  151. * var ciphertext = CryptoJS.RabbitLegacy.encrypt(message, key, cfg);
  152. * var plaintext = CryptoJS.RabbitLegacy.decrypt(ciphertext, key, cfg);
  153. */
  154. C.RabbitLegacy = StreamCipher._createHelper(RabbitLegacy);
  155. }());
  156. return CryptoJS.RabbitLegacy;
  157. }));