rabbit-legacy.js 5.0 KB

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