rabbit.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. var Rabbit = C_algo.Rabbit = StreamCipher.extend({
  27. _doReset: function () {
  28. // Shortcuts
  29. var K = this._key.words;
  30. var iv = this.cfg.iv;
  31. // Swap endian
  32. for (var i = 0; i < 4; i++) {
  33. K[i] = (((K[i] << 8) | (K[i] >>> 24)) & 0x00ff00ff) |
  34. (((K[i] << 24) | (K[i] >>> 8)) & 0xff00ff00);
  35. }
  36. // Generate initial state values
  37. var X = this._X = [
  38. K[0], (K[3] << 16) | (K[2] >>> 16),
  39. K[1], (K[0] << 16) | (K[3] >>> 16),
  40. K[2], (K[1] << 16) | (K[0] >>> 16),
  41. K[3], (K[2] << 16) | (K[1] >>> 16)
  42. ];
  43. // Generate initial counter values
  44. var C = this._C = [
  45. (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff),
  46. (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff),
  47. (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff),
  48. (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff)
  49. ];
  50. // Carry bit
  51. this._b = 0;
  52. // Iterate the system four times
  53. for (var i = 0; i < 4; i++) {
  54. nextState.call(this);
  55. }
  56. // Modify the counters
  57. for (var i = 0; i < 8; i++) {
  58. C[i] ^= X[(i + 4) & 7];
  59. }
  60. // IV setup
  61. if (iv) {
  62. // Shortcuts
  63. var IV = iv.words;
  64. var IV_0 = IV[0];
  65. var IV_1 = IV[1];
  66. // Generate four subvectors
  67. var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00);
  68. var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00);
  69. var i1 = (i0 >>> 16) | (i2 & 0xffff0000);
  70. var i3 = (i2 << 16) | (i0 & 0x0000ffff);
  71. // Modify counter values
  72. C[0] ^= i0;
  73. C[1] ^= i1;
  74. C[2] ^= i2;
  75. C[3] ^= i3;
  76. C[4] ^= i0;
  77. C[5] ^= i1;
  78. C[6] ^= i2;
  79. C[7] ^= i3;
  80. // Iterate the system four times
  81. for (var i = 0; i < 4; i++) {
  82. nextState.call(this);
  83. }
  84. }
  85. },
  86. _doProcessBlock: function (M, offset) {
  87. // Shortcut
  88. var X = this._X;
  89. // Iterate the system
  90. nextState.call(this);
  91. // Generate four keystream words
  92. S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16);
  93. S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16);
  94. S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16);
  95. S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16);
  96. for (var i = 0; i < 4; i++) {
  97. // Swap endian
  98. S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) |
  99. (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00);
  100. // Encrypt
  101. M[offset + i] ^= S[i];
  102. }
  103. },
  104. blockSize: 128 / 32,
  105. ivSize: 64 / 32
  106. });
  107. function nextState() {
  108. // Shortcuts
  109. var X = this._X;
  110. var C = this._C;
  111. // Save old counter values
  112. for (var i = 0; i < 8; i++) {
  113. C_[i] = C[i];
  114. }
  115. // Calculate new counter values
  116. C[0] = (C[0] + 0x4d34d34d + this._b) | 0;
  117. C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0;
  118. C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0;
  119. C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0;
  120. C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0;
  121. C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0;
  122. C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0;
  123. C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0;
  124. this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0;
  125. // Calculate the g-values
  126. for (var i = 0; i < 8; i++) {
  127. var gx = X[i] + C[i];
  128. // Construct high and low argument for squaring
  129. var ga = gx & 0xffff;
  130. var gb = gx >>> 16;
  131. // Calculate high and low result of squaring
  132. var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb;
  133. var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0);
  134. // High XOR low
  135. G[i] = gh ^ gl;
  136. }
  137. // Calculate new state values
  138. X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0;
  139. X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0;
  140. X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0;
  141. X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0;
  142. X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0;
  143. X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0;
  144. X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0;
  145. X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0;
  146. }
  147. /**
  148. * Shortcut functions to the cipher's object interface.
  149. *
  150. * @example
  151. *
  152. * var ciphertext = CryptoJS.Rabbit.encrypt(message, key, cfg);
  153. * var plaintext = CryptoJS.Rabbit.decrypt(ciphertext, key, cfg);
  154. */
  155. C.Rabbit = StreamCipher._createHelper(Rabbit);
  156. }());
  157. return CryptoJS.Rabbit;
  158. }));