tripledes.js 21 KB

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