mode-ctr.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. /**
  8. * Counter block mode.
  9. */
  10. CryptoJS.mode.CTR = (function () {
  11. var CTR = CryptoJS.lib.BlockCipherMode.extend();
  12. var Encryptor = CTR.Encryptor = CTR.extend({
  13. processBlock: function (words, offset) {
  14. // Shortcuts
  15. var cipher = this._cipher
  16. var blockSize = cipher.blockSize;
  17. var iv = this._iv;
  18. var counter = this._counter;
  19. // Generate keystream
  20. if (iv) {
  21. counter = this._counter = iv.slice(0);
  22. // Remove IV for subsequent blocks
  23. this._iv = undefined;
  24. }
  25. var keystream = counter.slice(0);
  26. cipher.encryptBlock(keystream, 0);
  27. // Increment counter
  28. counter[blockSize - 1] = (counter[blockSize - 1] + 1) | 0
  29. // Encrypt
  30. for (var i = 0; i < blockSize; i++) {
  31. words[offset + i] ^= keystream[i];
  32. }
  33. }
  34. });
  35. CTR.Decryptor = Encryptor;
  36. return CTR;
  37. }());