~bac/juju-gui/trunkcopy

« back to all changes in this revision

Viewing changes to lib/cryptojs/components/tripledes.js

  • Committer: kapil.foss at gmail
  • Date: 2012-07-13 18:45:59 UTC
  • Revision ID: kapil.foss@gmail.com-20120713184559-2xl7be17egsrz0c9
reshape

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
CryptoJS v3.0.2
3
 
code.google.com/p/crypto-js
4
 
(c) 2009-2012 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 WordArray = C_lib.WordArray;
12
 
    var BlockCipher = C_lib.BlockCipher;
13
 
    var C_algo = C.algo;
14
 
 
15
 
    // Permuted Choice 1 constants
16
 
    var PC1 = [
17
 
        57, 49, 41, 33, 25, 17, 9,  1,
18
 
        58, 50, 42, 34, 26, 18, 10, 2,
19
 
        59, 51, 43, 35, 27, 19, 11, 3,
20
 
        60, 52, 44, 36, 63, 55, 47, 39,
21
 
        31, 23, 15, 7,  62, 54, 46, 38,
22
 
        30, 22, 14, 6,  61, 53, 45, 37,
23
 
        29, 21, 13, 5,  28, 20, 12, 4
24
 
    ];
25
 
 
26
 
    // Permuted Choice 2 constants
27
 
    var PC2 = [
28
 
        14, 17, 11, 24, 1,  5,
29
 
        3,  28, 15, 6,  21, 10,
30
 
        23, 19, 12, 4,  26, 8,
31
 
        16, 7,  27, 20, 13, 2,
32
 
        41, 52, 31, 37, 47, 55,
33
 
        30, 40, 51, 45, 33, 48,
34
 
        44, 49, 39, 56, 34, 53,
35
 
        46, 42, 50, 36, 29, 32
36
 
    ];
37
 
 
38
 
    // Cumulative bit shift constants
39
 
    var BIT_SHIFTS = [1,  2,  4,  6,  8,  10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28];
40
 
 
41
 
    // SBOXes and precomputed round permutation constants
42
 
    var SBOX_P = [
43
 
        {
44
 
            0x00000000: 0x00808200, 0x10000000: 0x00008000, 0x20000000: 0x00808002, 0x30000000: 0x00000002,
45
 
            0x40000000: 0x00000200, 0x50000000: 0x00808202, 0x60000000: 0x00800202, 0x70000000: 0x00800000,
46
 
            0x80000000: 0x00000202, 0x90000000: 0x00800200, 0xa0000000: 0x00008200, 0xb0000000: 0x00808000,
47
 
            0xc0000000: 0x00008002, 0xd0000000: 0x00800002, 0xe0000000: 0x00000000, 0xf0000000: 0x00008202,
48
 
            0x08000000: 0x00000000, 0x18000000: 0x00808202, 0x28000000: 0x00008202, 0x38000000: 0x00008000,
49
 
            0x48000000: 0x00808200, 0x58000000: 0x00000200, 0x68000000: 0x00808002, 0x78000000: 0x00000002,
50
 
            0x88000000: 0x00800200, 0x98000000: 0x00008200, 0xa8000000: 0x00808000, 0xb8000000: 0x00800202,
51
 
            0xc8000000: 0x00800002, 0xd8000000: 0x00008002, 0xe8000000: 0x00000202, 0xf8000000: 0x00800000,
52
 
            0x00000001: 0x00008000, 0x10000001: 0x00000002, 0x20000001: 0x00808200, 0x30000001: 0x00800000,
53
 
            0x40000001: 0x00808002, 0x50000001: 0x00008200, 0x60000001: 0x00000200, 0x70000001: 0x00800202,
54
 
            0x80000001: 0x00808202, 0x90000001: 0x00808000, 0xa0000001: 0x00800002, 0xb0000001: 0x00008202,
55
 
            0xc0000001: 0x00000202, 0xd0000001: 0x00800200, 0xe0000001: 0x00008002, 0xf0000001: 0x00000000,
56
 
            0x08000001: 0x00808202, 0x18000001: 0x00808000, 0x28000001: 0x00800000, 0x38000001: 0x00000200,
57
 
            0x48000001: 0x00008000, 0x58000001: 0x00800002, 0x68000001: 0x00000002, 0x78000001: 0x00008202,
58
 
            0x88000001: 0x00008002, 0x98000001: 0x00800202, 0xa8000001: 0x00000202, 0xb8000001: 0x00808200,
59
 
            0xc8000001: 0x00800200, 0xd8000001: 0x00000000, 0xe8000001: 0x00008200, 0xf8000001: 0x00808002
60
 
        },
61
 
        {
62
 
            0x00000000: 0x40084010, 0x01000000: 0x00004000, 0x02000000: 0x00080000, 0x03000000: 0x40080010,
63
 
            0x04000000: 0x40000010, 0x05000000: 0x40084000, 0x06000000: 0x40004000, 0x07000000: 0x00000010,
64
 
            0x08000000: 0x00084000, 0x09000000: 0x40004010, 0x0a000000: 0x40000000, 0x0b000000: 0x00084010,
65
 
            0x0c000000: 0x00080010, 0x0d000000: 0x00000000, 0x0e000000: 0x00004010, 0x0f000000: 0x40080000,
66
 
            0x00800000: 0x40004000, 0x01800000: 0x00084010, 0x02800000: 0x00000010, 0x03800000: 0x40004010,
67
 
            0x04800000: 0x40084010, 0x05800000: 0x40000000, 0x06800000: 0x00080000, 0x07800000: 0x40080010,
68
 
            0x08800000: 0x00080010, 0x09800000: 0x00000000, 0x0a800000: 0x00004000, 0x0b800000: 0x40080000,
69
 
            0x0c800000: 0x40000010, 0x0d800000: 0x00084000, 0x0e800000: 0x40084000, 0x0f800000: 0x00004010,
70
 
            0x10000000: 0x00000000, 0x11000000: 0x40080010, 0x12000000: 0x40004010, 0x13000000: 0x40084000,
71
 
            0x14000000: 0x40080000, 0x15000000: 0x00000010, 0x16000000: 0x00084010, 0x17000000: 0x00004000,
72
 
            0x18000000: 0x00004010, 0x19000000: 0x00080000, 0x1a000000: 0x00080010, 0x1b000000: 0x40000010,
73
 
            0x1c000000: 0x00084000, 0x1d000000: 0x40004000, 0x1e000000: 0x40000000, 0x1f000000: 0x40084010,
74
 
            0x10800000: 0x00084010, 0x11800000: 0x00080000, 0x12800000: 0x40080000, 0x13800000: 0x00004000,
75
 
            0x14800000: 0x40004000, 0x15800000: 0x40084010, 0x16800000: 0x00000010, 0x17800000: 0x40000000,
76
 
            0x18800000: 0x40084000, 0x19800000: 0x40000010, 0x1a800000: 0x40004010, 0x1b800000: 0x00080010,
77
 
            0x1c800000: 0x00000000, 0x1d800000: 0x00004010, 0x1e800000: 0x40080010, 0x1f800000: 0x00084000
78
 
        },
79
 
        {
80
 
            0x00000000: 0x00000104, 0x00100000: 0x00000000, 0x00200000: 0x04000100, 0x00300000: 0x00010104,
81
 
            0x00400000: 0x00010004, 0x00500000: 0x04000004, 0x00600000: 0x04010104, 0x00700000: 0x04010000,
82
 
            0x00800000: 0x04000000, 0x00900000: 0x04010100, 0x00a00000: 0x00010100, 0x00b00000: 0x04010004,
83
 
            0x00c00000: 0x04000104, 0x00d00000: 0x00010000, 0x00e00000: 0x00000004, 0x00f00000: 0x00000100,
84
 
            0x00080000: 0x04010100, 0x00180000: 0x04010004, 0x00280000: 0x00000000, 0x00380000: 0x04000100,
85
 
            0x00480000: 0x04000004, 0x00580000: 0x00010000, 0x00680000: 0x00010004, 0x00780000: 0x00000104,
86
 
            0x00880000: 0x00000004, 0x00980000: 0x00000100, 0x00a80000: 0x04010000, 0x00b80000: 0x00010104,
87
 
            0x00c80000: 0x00010100, 0x00d80000: 0x04000104, 0x00e80000: 0x04010104, 0x00f80000: 0x04000000,
88
 
            0x01000000: 0x04010100, 0x01100000: 0x00010004, 0x01200000: 0x00010000, 0x01300000: 0x04000100,
89
 
            0x01400000: 0x00000100, 0x01500000: 0x04010104, 0x01600000: 0x04000004, 0x01700000: 0x00000000,
90
 
            0x01800000: 0x04000104, 0x01900000: 0x04000000, 0x01a00000: 0x00000004, 0x01b00000: 0x00010100,
91
 
            0x01c00000: 0x04010000, 0x01d00000: 0x00000104, 0x01e00000: 0x00010104, 0x01f00000: 0x04010004,
92
 
            0x01080000: 0x04000000, 0x01180000: 0x00000104, 0x01280000: 0x04010100, 0x01380000: 0x00000000,
93
 
            0x01480000: 0x00010004, 0x01580000: 0x04000100, 0x01680000: 0x00000100, 0x01780000: 0x04010004,
94
 
            0x01880000: 0x00010000, 0x01980000: 0x04010104, 0x01a80000: 0x00010104, 0x01b80000: 0x04000004,
95
 
            0x01c80000: 0x04000104, 0x01d80000: 0x04010000, 0x01e80000: 0x00000004, 0x01f80000: 0x00010100
96
 
        },
97
 
        {
98
 
            0x00000000: 0x80401000, 0x00010000: 0x80001040, 0x00020000: 0x00401040, 0x00030000: 0x80400000,
99
 
            0x00040000: 0x00000000, 0x00050000: 0x00401000, 0x00060000: 0x80000040, 0x00070000: 0x00400040,
100
 
            0x00080000: 0x80000000, 0x00090000: 0x00400000, 0x000a0000: 0x00000040, 0x000b0000: 0x80001000,
101
 
            0x000c0000: 0x80400040, 0x000d0000: 0x00001040, 0x000e0000: 0x00001000, 0x000f0000: 0x80401040,
102
 
            0x00008000: 0x80001040, 0x00018000: 0x00000040, 0x00028000: 0x80400040, 0x00038000: 0x80001000,
103
 
            0x00048000: 0x00401000, 0x00058000: 0x80401040, 0x00068000: 0x00000000, 0x00078000: 0x80400000,
104
 
            0x00088000: 0x00001000, 0x00098000: 0x80401000, 0x000a8000: 0x00400000, 0x000b8000: 0x00001040,
105
 
            0x000c8000: 0x80000000, 0x000d8000: 0x00400040, 0x000e8000: 0x00401040, 0x000f8000: 0x80000040,
106
 
            0x00100000: 0x00400040, 0x00110000: 0x00401000, 0x00120000: 0x80000040, 0x00130000: 0x00000000,
107
 
            0x00140000: 0x00001040, 0x00150000: 0x80400040, 0x00160000: 0x80401000, 0x00170000: 0x80001040,
108
 
            0x00180000: 0x80401040, 0x00190000: 0x80000000, 0x001a0000: 0x80400000, 0x001b0000: 0x00401040,
109
 
            0x001c0000: 0x80001000, 0x001d0000: 0x00400000, 0x001e0000: 0x00000040, 0x001f0000: 0x00001000,
110
 
            0x00108000: 0x80400000, 0x00118000: 0x80401040, 0x00128000: 0x00000000, 0x00138000: 0x00401000,
111
 
            0x00148000: 0x00400040, 0x00158000: 0x80000000, 0x00168000: 0x80001040, 0x00178000: 0x00000040,
112
 
            0x00188000: 0x80000040, 0x00198000: 0x00001000, 0x001a8000: 0x80001000, 0x001b8000: 0x80400040,
113
 
            0x001c8000: 0x00001040, 0x001d8000: 0x80401000, 0x001e8000: 0x00400000, 0x001f8000: 0x00401040
114
 
        },
115
 
        {
116
 
            0x00000000: 0x00000080, 0x00001000: 0x01040000, 0x00002000: 0x00040000, 0x00003000: 0x20000000,
117
 
            0x00004000: 0x20040080, 0x00005000: 0x01000080, 0x00006000: 0x21000080, 0x00007000: 0x00040080,
118
 
            0x00008000: 0x01000000, 0x00009000: 0x20040000, 0x0000a000: 0x20000080, 0x0000b000: 0x21040080,
119
 
            0x0000c000: 0x21040000, 0x0000d000: 0x00000000, 0x0000e000: 0x01040080, 0x0000f000: 0x21000000,
120
 
            0x00000800: 0x01040080, 0x00001800: 0x21000080, 0x00002800: 0x00000080, 0x00003800: 0x01040000,
121
 
            0x00004800: 0x00040000, 0x00005800: 0x20040080, 0x00006800: 0x21040000, 0x00007800: 0x20000000,
122
 
            0x00008800: 0x20040000, 0x00009800: 0x00000000, 0x0000a800: 0x21040080, 0x0000b800: 0x01000080,
123
 
            0x0000c800: 0x20000080, 0x0000d800: 0x21000000, 0x0000e800: 0x01000000, 0x0000f800: 0x00040080,
124
 
            0x00010000: 0x00040000, 0x00011000: 0x00000080, 0x00012000: 0x20000000, 0x00013000: 0x21000080,
125
 
            0x00014000: 0x01000080, 0x00015000: 0x21040000, 0x00016000: 0x20040080, 0x00017000: 0x01000000,
126
 
            0x00018000: 0x21040080, 0x00019000: 0x21000000, 0x0001a000: 0x01040000, 0x0001b000: 0x20040000,
127
 
            0x0001c000: 0x00040080, 0x0001d000: 0x20000080, 0x0001e000: 0x00000000, 0x0001f000: 0x01040080,
128
 
            0x00010800: 0x21000080, 0x00011800: 0x01000000, 0x00012800: 0x01040000, 0x00013800: 0x20040080,
129
 
            0x00014800: 0x20000000, 0x00015800: 0x01040080, 0x00016800: 0x00000080, 0x00017800: 0x21040000,
130
 
            0x00018800: 0x00040080, 0x00019800: 0x21040080, 0x0001a800: 0x00000000, 0x0001b800: 0x21000000,
131
 
            0x0001c800: 0x01000080, 0x0001d800: 0x00040000, 0x0001e800: 0x20040000, 0x0001f800: 0x20000080
132
 
        },
133
 
        {
134
 
            0x00000000: 0x10000008, 0x00000100: 0x00002000, 0x00000200: 0x10200000, 0x00000300: 0x10202008,
135
 
            0x00000400: 0x10002000, 0x00000500: 0x00200000, 0x00000600: 0x00200008, 0x00000700: 0x10000000,
136
 
            0x00000800: 0x00000000, 0x00000900: 0x10002008, 0x00000a00: 0x00202000, 0x00000b00: 0x00000008,
137
 
            0x00000c00: 0x10200008, 0x00000d00: 0x00202008, 0x00000e00: 0x00002008, 0x00000f00: 0x10202000,
138
 
            0x00000080: 0x10200000, 0x00000180: 0x10202008, 0x00000280: 0x00000008, 0x00000380: 0x00200000,
139
 
            0x00000480: 0x00202008, 0x00000580: 0x10000008, 0x00000680: 0x10002000, 0x00000780: 0x00002008,
140
 
            0x00000880: 0x00200008, 0x00000980: 0x00002000, 0x00000a80: 0x10002008, 0x00000b80: 0x10200008,
141
 
            0x00000c80: 0x00000000, 0x00000d80: 0x10202000, 0x00000e80: 0x00202000, 0x00000f80: 0x10000000,
142
 
            0x00001000: 0x10002000, 0x00001100: 0x10200008, 0x00001200: 0x10202008, 0x00001300: 0x00002008,
143
 
            0x00001400: 0x00200000, 0x00001500: 0x10000000, 0x00001600: 0x10000008, 0x00001700: 0x00202000,
144
 
            0x00001800: 0x00202008, 0x00001900: 0x00000000, 0x00001a00: 0x00000008, 0x00001b00: 0x10200000,
145
 
            0x00001c00: 0x00002000, 0x00001d00: 0x10002008, 0x00001e00: 0x10202000, 0x00001f00: 0x00200008,
146
 
            0x00001080: 0x00000008, 0x00001180: 0x00202000, 0x00001280: 0x00200000, 0x00001380: 0x10000008,
147
 
            0x00001480: 0x10002000, 0x00001580: 0x00002008, 0x00001680: 0x10202008, 0x00001780: 0x10200000,
148
 
            0x00001880: 0x10202000, 0x00001980: 0x10200008, 0x00001a80: 0x00002000, 0x00001b80: 0x00202008,
149
 
            0x00001c80: 0x00200008, 0x00001d80: 0x00000000, 0x00001e80: 0x10000000, 0x00001f80: 0x10002008
150
 
        },
151
 
        {
152
 
            0x00000000: 0x00100000, 0x00000010: 0x02000401, 0x00000020: 0x00000400, 0x00000030: 0x00100401,
153
 
            0x00000040: 0x02100401, 0x00000050: 0x00000000, 0x00000060: 0x00000001, 0x00000070: 0x02100001,
154
 
            0x00000080: 0x02000400, 0x00000090: 0x00100001, 0x000000a0: 0x02000001, 0x000000b0: 0x02100400,
155
 
            0x000000c0: 0x02100000, 0x000000d0: 0x00000401, 0x000000e0: 0x00100400, 0x000000f0: 0x02000000,
156
 
            0x00000008: 0x02100001, 0x00000018: 0x00000000, 0x00000028: 0x02000401, 0x00000038: 0x02100400,
157
 
            0x00000048: 0x00100000, 0x00000058: 0x02000001, 0x00000068: 0x02000000, 0x00000078: 0x00000401,
158
 
            0x00000088: 0x00100401, 0x00000098: 0x02000400, 0x000000a8: 0x02100000, 0x000000b8: 0x00100001,
159
 
            0x000000c8: 0x00000400, 0x000000d8: 0x02100401, 0x000000e8: 0x00000001, 0x000000f8: 0x00100400,
160
 
            0x00000100: 0x02000000, 0x00000110: 0x00100000, 0x00000120: 0x02000401, 0x00000130: 0x02100001,
161
 
            0x00000140: 0x00100001, 0x00000150: 0x02000400, 0x00000160: 0x02100400, 0x00000170: 0x00100401,
162
 
            0x00000180: 0x00000401, 0x00000190: 0x02100401, 0x000001a0: 0x00100400, 0x000001b0: 0x00000001,
163
 
            0x000001c0: 0x00000000, 0x000001d0: 0x02100000, 0x000001e0: 0x02000001, 0x000001f0: 0x00000400,
164
 
            0x00000108: 0x00100400, 0x00000118: 0x02000401, 0x00000128: 0x02100001, 0x00000138: 0x00000001,
165
 
            0x00000148: 0x02000000, 0x00000158: 0x00100000, 0x00000168: 0x00000401, 0x00000178: 0x02100400,
166
 
            0x00000188: 0x02000001, 0x00000198: 0x02100000, 0x000001a8: 0x00000000, 0x000001b8: 0x02100401,
167
 
            0x000001c8: 0x00100401, 0x000001d8: 0x00000400, 0x000001e8: 0x02000400, 0x000001f8: 0x00100001
168
 
        },
169
 
        {
170
 
            0x00000000: 0x08000820, 0x00000001: 0x00020000, 0x00000002: 0x08000000, 0x00000003: 0x00000020,
171
 
            0x00000004: 0x00020020, 0x00000005: 0x08020820, 0x00000006: 0x08020800, 0x00000007: 0x00000800,
172
 
            0x00000008: 0x08020000, 0x00000009: 0x08000800, 0x0000000a: 0x00020800, 0x0000000b: 0x08020020,
173
 
            0x0000000c: 0x00000820, 0x0000000d: 0x00000000, 0x0000000e: 0x08000020, 0x0000000f: 0x00020820,
174
 
            0x80000000: 0x00000800, 0x80000001: 0x08020820, 0x80000002: 0x08000820, 0x80000003: 0x08000000,
175
 
            0x80000004: 0x08020000, 0x80000005: 0x00020800, 0x80000006: 0x00020820, 0x80000007: 0x00000020,
176
 
            0x80000008: 0x08000020, 0x80000009: 0x00000820, 0x8000000a: 0x00020020, 0x8000000b: 0x08020800,
177
 
            0x8000000c: 0x00000000, 0x8000000d: 0x08020020, 0x8000000e: 0x08000800, 0x8000000f: 0x00020000,
178
 
            0x00000010: 0x00020820, 0x00000011: 0x08020800, 0x00000012: 0x00000020, 0x00000013: 0x00000800,
179
 
            0x00000014: 0x08000800, 0x00000015: 0x08000020, 0x00000016: 0x08020020, 0x00000017: 0x00020000,
180
 
            0x00000018: 0x00000000, 0x00000019: 0x00020020, 0x0000001a: 0x08020000, 0x0000001b: 0x08000820,
181
 
            0x0000001c: 0x08020820, 0x0000001d: 0x00020800, 0x0000001e: 0x00000820, 0x0000001f: 0x08000000,
182
 
            0x80000010: 0x00020000, 0x80000011: 0x00000800, 0x80000012: 0x08020020, 0x80000013: 0x00020820,
183
 
            0x80000014: 0x00000020, 0x80000015: 0x08020000, 0x80000016: 0x08000000, 0x80000017: 0x08000820,
184
 
            0x80000018: 0x08020820, 0x80000019: 0x08000020, 0x8000001a: 0x08000800, 0x8000001b: 0x00000000,
185
 
            0x8000001c: 0x00020800, 0x8000001d: 0x00000820, 0x8000001e: 0x00020020, 0x8000001f: 0x08020800
186
 
        }
187
 
    ];
188
 
 
189
 
    // Masks that select the SBOX input
190
 
    var SBOX_MASK = [
191
 
        0xf8000001, 0x1f800000, 0x01f80000, 0x001f8000,
192
 
        0x0001f800, 0x00001f80, 0x000001f8, 0x8000001f
193
 
    ];
194
 
 
195
 
    /**
196
 
     * DES block cipher algorithm.
197
 
     */
198
 
    var DES = C_algo.DES = BlockCipher.extend({
199
 
        _doReset: function () {
200
 
            // Shortcuts
201
 
            var key = this._key;
202
 
            var keyWords = key.words;
203
 
 
204
 
            // Select 56 bits according to PC1
205
 
            var keyBits = [];
206
 
            for (var i = 0; i < 56; i++) {
207
 
                var keyBitPos = PC1[i] - 1;
208
 
                keyBits[i] = (keyWords[keyBitPos >>> 5] >>> (31 - keyBitPos % 32)) & 1;
209
 
            }
210
 
 
211
 
            // Assemble 16 subkeys
212
 
            var subKeys = this._subKeys = [];
213
 
            for (var nSubKey = 0; nSubKey < 16; nSubKey++) {
214
 
                // Create subkey
215
 
                var subKey = subKeys[nSubKey] = [];
216
 
 
217
 
                // Shortcut
218
 
                var bitShift = BIT_SHIFTS[nSubKey];
219
 
 
220
 
                // Select 48 bits according to PC2
221
 
                for (var i = 0; i < 24; i++) {
222
 
                    // Select from the left 28 key bits
223
 
                    subKey[(i / 6) | 0] |= keyBits[((PC2[i] - 1) + bitShift) % 28] << (31 - i % 6);
224
 
 
225
 
                    // Select from the right 28 key bits
226
 
                    subKey[4 + ((i / 6) | 0)] |= keyBits[28 + (((PC2[i + 24] - 1) + bitShift) % 28)] << (31 - i % 6);
227
 
                }
228
 
 
229
 
                // Since each subkey is applied to an expanded 32-bit input,
230
 
                // the subkey can be broken into 8 values scaled to 32-bits,
231
 
                // which allows the key to be used without expansion
232
 
                subKey[0] = (subKey[0] << 1) | (subKey[0] >>> 31);
233
 
                for (var i = 1; i < 7; i++) {
234
 
                    subKey[i] = subKey[i] >>> ((i - 1) * 4 + 3);
235
 
                }
236
 
                subKey[7] = (subKey[7] << 5) | (subKey[7] >>> 27);
237
 
            }
238
 
 
239
 
            // Compute inverse subkeys
240
 
            var invSubKeys = this._invSubKeys = [];
241
 
            for (var i = 0; i < 16; i++) {
242
 
                invSubKeys[i] = subKeys[15 - i];
243
 
            }
244
 
        },
245
 
 
246
 
        encryptBlock: function (M, offset) {
247
 
            this._doCryptBlock(M, offset, this._subKeys);
248
 
        },
249
 
 
250
 
        decryptBlock: function (M, offset) {
251
 
            this._doCryptBlock(M, offset, this._invSubKeys);
252
 
        },
253
 
 
254
 
        _doCryptBlock: function (M, offset, subKeys) {
255
 
            // Get input
256
 
            this._lBlock = M[offset];
257
 
            this._rBlock = M[offset + 1];
258
 
 
259
 
            // Initial permutation
260
 
            exchangeLR.call(this, 4,  0x0f0f0f0f);
261
 
            exchangeLR.call(this, 16, 0x0000ffff);
262
 
            exchangeRL.call(this, 2,  0x33333333);
263
 
            exchangeRL.call(this, 8,  0x00ff00ff);
264
 
            exchangeLR.call(this, 1,  0x55555555);
265
 
 
266
 
            // Rounds
267
 
            for (var round = 0; round < 16; round++) {
268
 
                // Shortcuts
269
 
                var subKey = subKeys[round];
270
 
                var lBlock = this._lBlock;
271
 
                var rBlock = this._rBlock;
272
 
 
273
 
                // Feistel function
274
 
                var f = 0;
275
 
                for (var i = 0; i < 8; i++) {
276
 
                    f |= SBOX_P[i][((rBlock ^ subKey[i]) & SBOX_MASK[i]) >>> 0];
277
 
                }
278
 
                this._lBlock = rBlock;
279
 
                this._rBlock = lBlock ^ f;
280
 
            }
281
 
 
282
 
            // Undo swap from last round
283
 
            var t = this._lBlock;
284
 
            this._lBlock = this._rBlock;
285
 
            this._rBlock = t;
286
 
 
287
 
            // Final permutation
288
 
            exchangeLR.call(this, 1,  0x55555555);
289
 
            exchangeRL.call(this, 8,  0x00ff00ff);
290
 
            exchangeRL.call(this, 2,  0x33333333);
291
 
            exchangeLR.call(this, 16, 0x0000ffff);
292
 
            exchangeLR.call(this, 4,  0x0f0f0f0f);
293
 
 
294
 
            // Set output
295
 
            M[offset] = this._lBlock;
296
 
            M[offset + 1] = this._rBlock;
297
 
        },
298
 
 
299
 
        keySize: 64/32,
300
 
 
301
 
        ivSize: 64/32,
302
 
 
303
 
        blockSize: 64/32
304
 
    });
305
 
 
306
 
    // Swap bits across the left and right words
307
 
    function exchangeLR(offset, mask) {
308
 
        var t = ((this._lBlock >>> offset) ^ this._rBlock) & mask;
309
 
        this._rBlock ^= t;
310
 
        this._lBlock ^= t << offset;
311
 
    }
312
 
 
313
 
    function exchangeRL(offset, mask) {
314
 
        var t = ((this._rBlock >>> offset) ^ this._lBlock) & mask;
315
 
        this._lBlock ^= t;
316
 
        this._rBlock ^= t << offset;
317
 
    }
318
 
 
319
 
    /**
320
 
     * Shortcut functions to the cipher's object interface.
321
 
     *
322
 
     * @example
323
 
     *
324
 
     *     var ciphertext = CryptoJS.DES.encrypt(message, key, cfg);
325
 
     *     var plaintext  = CryptoJS.DES.decrypt(ciphertext, key, cfg);
326
 
     */
327
 
    C.DES = BlockCipher._createHelper(DES);
328
 
 
329
 
    /**
330
 
     * Triple-DES block cipher algorithm.
331
 
     */
332
 
    var TripleDES = C_algo.TripleDES = BlockCipher.extend({
333
 
        _doReset: function () {
334
 
            // Shortcuts
335
 
            var key = this._key;
336
 
            var keyWords = key.words;
337
 
 
338
 
            // Create DES instances
339
 
            this._des1 = DES.createEncryptor(WordArray.create(keyWords.slice(0, 2)));
340
 
            this._des2 = DES.createEncryptor(WordArray.create(keyWords.slice(2, 4)));
341
 
            this._des3 = DES.createEncryptor(WordArray.create(keyWords.slice(4, 6)));
342
 
        },
343
 
 
344
 
        encryptBlock: function (M, offset) {
345
 
            this._des1.encryptBlock(M, offset);
346
 
            this._des2.decryptBlock(M, offset);
347
 
            this._des3.encryptBlock(M, offset);
348
 
        },
349
 
 
350
 
        decryptBlock: function (M, offset) {
351
 
            this._des3.decryptBlock(M, offset);
352
 
            this._des2.encryptBlock(M, offset);
353
 
            this._des1.decryptBlock(M, offset);
354
 
        },
355
 
 
356
 
        keySize: 192/32,
357
 
 
358
 
        ivSize: 64/32,
359
 
 
360
 
        blockSize: 64/32
361
 
    });
362
 
 
363
 
    /**
364
 
     * Shortcut functions to the cipher's object interface.
365
 
     *
366
 
     * @example
367
 
     *
368
 
     *     var ciphertext = CryptoJS.TripleDES.encrypt(message, key, cfg);
369
 
     *     var plaintext  = CryptoJS.TripleDES.decrypt(ciphertext, key, cfg);
370
 
     */
371
 
    C.TripleDES = BlockCipher._createHelper(TripleDES);
372
 
}());