UNPKG

1.47 kBJavaScriptView Raw
1;(function (root, factory, undef) {
2 if (typeof exports === "object") {
3 // CommonJS
4 module.exports = exports = factory(require("./core"), require("./cipher-core"));
5 }
6 else if (typeof define === "function" && define.amd) {
7 // AMD
8 define(["./core", "./cipher-core"], factory);
9 }
10 else {
11 // Global (browser)
12 factory(root.CryptoJS);
13 }
14}(this, function (CryptoJS) {
15
16 /**
17 * Counter block mode.
18 */
19 CryptoJS.mode.CTR = (function () {
20 var CTR = CryptoJS.lib.BlockCipherMode.extend();
21
22 var Encryptor = CTR.Encryptor = CTR.extend({
23 processBlock: function (words, offset) {
24 // Shortcuts
25 var cipher = this._cipher
26 var blockSize = cipher.blockSize;
27 var iv = this._iv;
28 var counter = this._counter;
29
30 // Generate keystream
31 if (iv) {
32 counter = this._counter = iv.slice(0);
33
34 // Remove IV for subsequent blocks
35 this._iv = undefined;
36 }
37 var keystream = counter.slice(0);
38 cipher.encryptBlock(keystream, 0);
39
40 // Increment counter
41 counter[blockSize - 1] = (counter[blockSize - 1] + 1) | 0
42
43 // Encrypt
44 for (var i = 0; i < blockSize; i++) {
45 words[offset + i] ^= keystream[i];
46 }
47 }
48 });
49
50 CTR.Decryptor = Encryptor;
51
52 return CTR;
53 }());
54
55
56 return CryptoJS.mode.CTR;
57
58}));
\No newline at end of file