rabbit-legacy.js 6.0 KB

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