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. /** @preserve
  8.  * Counter block mode compatible with  Dr Brian Gladman fileenc.c
  9.  * derived from CryptoJS.mode.CTR
  10.  * Jan Hruby jhruby.web@gmail.com
  11.  */
  12. CryptoJS.mode.CTRGladman = (function () {
  13.     var CTRGladman = CryptoJS.lib.BlockCipherMode.extend();
  14.  
  15.         function incWord(word)
  16.         {      
  17.                 if (((word >> 24) & 0xff) === 0xff) { //overflow
  18.                 var b1 = (word >> 16)&0xff;
  19.                 var b2 = (word >> 8)&0xff;
  20.                 var b3 = word & 0xff;
  21.  
  22.                 if (b1 === 0xff) // overflow b1
  23.                 {                
  24.                 b1 = 0;
  25.                 if (b2 === 0xff)
  26.                 {
  27.                         b2 = 0;
  28.                         if (b3 === 0xff)
  29.                         {
  30.                                 b3 = 0;
  31.                         }
  32.                         else
  33.                         {
  34.                                 ++b3;
  35.                         }
  36.                 }
  37.                 else
  38.                 {
  39.                         ++b2;
  40.                 }
  41.                 }
  42.                 else
  43.                 {
  44.                 ++b1;
  45.                 }
  46.  
  47.                 word = 0;                
  48.                 word += (b1 << 16);
  49.                 word += (b2 << 8);
  50.                 word += b3;      
  51.                 }
  52.                 else
  53.                 {
  54.                 word += (0x01 << 24);
  55.                 }
  56.                 return word;
  57.         }
  58.  
  59.         function incCounter(counter)
  60.         {
  61.                 if ((counter[0] = incWord(counter[0])) === 0)
  62.                 {
  63.                         // encr_data in fileenc.c from  Dr Brian Gladman's counts only with DWORD j < 8
  64.                         counter[1] = incWord(counter[1]);
  65.                 }
  66.                 return counter;
  67.         }
  68.        
  69.     var Encryptor = CTRGladman.Encryptor = CTRGladman.extend({
  70.         processBlock: function (words, offset) {
  71.             // Shortcuts
  72.             var cipher = this._cipher
  73.             var blockSize = cipher.blockSize;
  74.             var iv = this._iv;
  75.             var counter = this._counter;
  76.  
  77.             // Generate keystream
  78.             if (iv) {
  79.                 counter = this._counter = iv.slice(0);
  80.  
  81.                 // Remove IV for subsequent blocks
  82.                 this._iv = undefined;
  83.             }
  84.            
  85.                         incCounter(counter);
  86.                        
  87.                         var keystream = counter.slice(0);
  88.             cipher.encryptBlock(keystream, 0);
  89.  
  90.             // Encrypt
  91.             for (var i = 0; i < blockSize; i++) {
  92.                 words[offset + i] ^= keystream[i];
  93.             }
  94.         }
  95.     });
  96.  
  97.     CTRGladman.Decryptor = Encryptor;
  98.  
  99.     return CTRGladman;
  100. }());
  101.  
  102.  
  103.