Subversion Repositories cryptochat

Rev

View as "text/javascript" | Blame | Last modification | View Log | RSS feed

  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.  
  14.     /**
  15.      * RC4 stream cipher algorithm.
  16.      */
  17.     var RC4 = C_algo.RC4 = StreamCipher.extend({
  18.         _doReset: function () {
  19.             // Shortcuts
  20.             var key = this._key;
  21.             var keyWords = key.words;
  22.             var keySigBytes = key.sigBytes;
  23.  
  24.             // Init sbox
  25.             var S = this._S = [];
  26.             for (var i = 0; i < 256; i++) {
  27.                 S[i] = i;
  28.             }
  29.  
  30.             // Key setup
  31.             for (var i = 0, j = 0; i < 256; i++) {
  32.                 var keyByteIndex = i % keySigBytes;
  33.                 var keyByte = (keyWords[keyByteIndex >>> 2] >>> (24 - (keyByteIndex % 4) * 8)) & 0xff;
  34.  
  35.                 j = (j + S[i] + keyByte) % 256;
  36.  
  37.                 // Swap
  38.                 var t = S[i];
  39.                 S[i] = S[j];
  40.                 S[j] = t;
  41.             }
  42.  
  43.             // Counters
  44.             this._i = this._j = 0;
  45.         },
  46.  
  47.         _doProcessBlock: function (M, offset) {
  48.             M[offset] ^= generateKeystreamWord.call(this);
  49.         },
  50.  
  51.         keySize: 256/32,
  52.  
  53.         ivSize: 0
  54.     });
  55.  
  56.     function generateKeystreamWord() {
  57.         // Shortcuts
  58.         var S = this._S;
  59.         var i = this._i;
  60.         var j = this._j;
  61.  
  62.         // Generate keystream word
  63.         var keystreamWord = 0;
  64.         for (var n = 0; n < 4; n++) {
  65.             i = (i + 1) % 256;
  66.             j = (j + S[i]) % 256;
  67.  
  68.             // Swap
  69.             var t = S[i];
  70.             S[i] = S[j];
  71.             S[j] = t;
  72.  
  73.             keystreamWord |= S[(S[i] + S[j]) % 256] << (24 - n * 8);
  74.         }
  75.  
  76.         // Update counters
  77.         this._i = i;
  78.         this._j = j;
  79.  
  80.         return keystreamWord;
  81.     }
  82.  
  83.     /**
  84.      * Shortcut functions to the cipher's object interface.
  85.      *
  86.      * @example
  87.      *
  88.      *     var ciphertext = CryptoJS.RC4.encrypt(message, key, cfg);
  89.      *     var plaintext  = CryptoJS.RC4.decrypt(ciphertext, key, cfg);
  90.      */
  91.     C.RC4 = StreamCipher._createHelper(RC4);
  92.  
  93.     /**
  94.      * Modified RC4 stream cipher algorithm.
  95.      */
  96.     var RC4Drop = C_algo.RC4Drop = RC4.extend({
  97.         /**
  98.          * Configuration options.
  99.          *
  100.          * @property {number} drop The number of keystream words to drop. Default 192
  101.          */
  102.         cfg: RC4.cfg.extend({
  103.             drop: 192
  104.         }),
  105.  
  106.         _doReset: function () {
  107.             RC4._doReset.call(this);
  108.  
  109.             // Drop
  110.             for (var i = this.cfg.drop; i > 0; i--) {
  111.                 generateKeystreamWord.call(this);
  112.             }
  113.         }
  114.     });
  115.  
  116.     /**
  117.      * Shortcut functions to the cipher's object interface.
  118.      *
  119.      * @example
  120.      *
  121.      *     var ciphertext = CryptoJS.RC4Drop.encrypt(message, key, cfg);
  122.      *     var plaintext  = CryptoJS.RC4Drop.decrypt(ciphertext, key, cfg);
  123.      */
  124.     C.RC4Drop = StreamCipher._createHelper(RC4Drop);
  125. }());
  126.