arc4.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2005 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview ARC4 streamcipher implementation. A description of the
  16. * algorithm can be found at:
  17. * http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt.
  18. *
  19. * Usage:
  20. * <code>
  21. * var arc4 = new goog.crypt.Arc4();
  22. * arc4.setKey(key);
  23. * arc4.discard(1536);
  24. * arc4.crypt(bytes);
  25. * </code>
  26. *
  27. * Note: For converting between strings and byte arrays, goog.crypt.base64 may
  28. * be useful.
  29. *
  30. */
  31. goog.provide('goog.crypt.Arc4');
  32. goog.require('goog.asserts');
  33. /**
  34. * ARC4 streamcipher implementation.
  35. * @constructor
  36. * @final
  37. * @struct
  38. */
  39. goog.crypt.Arc4 = function() {
  40. /**
  41. * A permutation of all 256 possible bytes.
  42. * @type {Array<number>}
  43. * @private
  44. */
  45. this.state_ = [];
  46. /**
  47. * 8 bit index pointer into this.state_.
  48. * @type {number}
  49. * @private
  50. */
  51. this.index1_ = 0;
  52. /**
  53. * 8 bit index pointer into this.state_.
  54. * @type {number}
  55. * @private
  56. */
  57. this.index2_ = 0;
  58. };
  59. /**
  60. * Initialize the cipher for use with new key.
  61. * @param {Array<number>} key A byte array containing the key.
  62. * @param {number=} opt_length Indicates # of bytes to take from the key.
  63. */
  64. goog.crypt.Arc4.prototype.setKey = function(key, opt_length) {
  65. goog.asserts.assertArray(key, 'Key parameter must be a byte array');
  66. if (!opt_length) {
  67. opt_length = key.length;
  68. }
  69. var state = this.state_;
  70. for (var i = 0; i < 256; ++i) {
  71. state[i] = i;
  72. }
  73. var j = 0;
  74. for (var i = 0; i < 256; ++i) {
  75. j = (j + state[i] + key[i % opt_length]) & 255;
  76. var tmp = state[i];
  77. state[i] = state[j];
  78. state[j] = tmp;
  79. }
  80. this.index1_ = 0;
  81. this.index2_ = 0;
  82. };
  83. /**
  84. * Discards n bytes of the keystream.
  85. * These days 1536 is considered a decent amount to drop to get the key state
  86. * warmed-up enough for secure usage. This is not done in the constructor to
  87. * preserve efficiency for use cases that do not need this.
  88. * NOTE: Discard is identical to crypt without actually xoring any data. It's
  89. * unfortunate to have this code duplicated, but this was done for performance
  90. * reasons. Alternatives which were attempted:
  91. * 1. Create a temp array of the correct length and pass it to crypt. This
  92. * works but needlessly allocates an array. But more importantly this
  93. * requires choosing an array type (Array or Uint8Array) in discard, and
  94. * choosing a different type than will be passed to crypt by the client
  95. * code hurts the javascript engines ability to optimize crypt (7x hit in
  96. * v8).
  97. * 2. Make data option in crypt so discard can pass null, this has a huge
  98. * perf hit for crypt.
  99. * @param {number} length Number of bytes to disregard from the stream.
  100. */
  101. goog.crypt.Arc4.prototype.discard = function(length) {
  102. var i = this.index1_;
  103. var j = this.index2_;
  104. var state = this.state_;
  105. for (var n = 0; n < length; ++n) {
  106. i = (i + 1) & 255;
  107. j = (j + state[i]) & 255;
  108. var tmp = state[i];
  109. state[i] = state[j];
  110. state[j] = tmp;
  111. }
  112. this.index1_ = i;
  113. this.index2_ = j;
  114. };
  115. /**
  116. * En- or decrypt (same operation for streamciphers like ARC4)
  117. * @param {Array<number>|Uint8Array} data The data to be xor-ed in place.
  118. * @param {number=} opt_length The number of bytes to crypt.
  119. */
  120. goog.crypt.Arc4.prototype.crypt = function(data, opt_length) {
  121. if (!opt_length) {
  122. opt_length = data.length;
  123. }
  124. var i = this.index1_;
  125. var j = this.index2_;
  126. var state = this.state_;
  127. for (var n = 0; n < opt_length; ++n) {
  128. i = (i + 1) & 255;
  129. j = (j + state[i]) & 255;
  130. var tmp = state[i];
  131. state[i] = state[j];
  132. state[j] = tmp;
  133. data[n] ^= state[(state[i] + state[j]) & 255];
  134. }
  135. this.index1_ = i;
  136. this.index2_ = j;
  137. };