tripledes.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. ;(function (root, factory, undef) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core"));
  5. } else if (typeof define === "function" && define.amd) {
  6. // AMD
  7. define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory);
  8. } else {
  9. // Global (browser)
  10. factory(root.CryptoJS);
  11. }
  12. }(this, function (CryptoJS) {
  13. (function () {
  14. // Shortcuts
  15. var C = CryptoJS;
  16. var C_lib = C.lib;
  17. var WordArray = C_lib.WordArray;
  18. var BlockCipher = C_lib.BlockCipher;
  19. var C_algo = C.algo;
  20. // Permuted Choice 1 constants
  21. var PC1 = [
  22. 57, 49, 41, 33, 25, 17, 9, 1,
  23. 58, 50, 42, 34, 26, 18, 10, 2,
  24. 59, 51, 43, 35, 27, 19, 11, 3,
  25. 60, 52, 44, 36, 63, 55, 47, 39,
  26. 31, 23, 15, 7, 62, 54, 46, 38,
  27. 30, 22, 14, 6, 61, 53, 45, 37,
  28. 29, 21, 13, 5, 28, 20, 12, 4
  29. ];
  30. // Permuted Choice 2 constants
  31. var PC2 = [
  32. 14, 17, 11, 24, 1, 5,
  33. 3, 28, 15, 6, 21, 10,
  34. 23, 19, 12, 4, 26, 8,
  35. 16, 7, 27, 20, 13, 2,
  36. 41, 52, 31, 37, 47, 55,
  37. 30, 40, 51, 45, 33, 48,
  38. 44, 49, 39, 56, 34, 53,
  39. 46, 42, 50, 36, 29, 32
  40. ];
  41. // Cumulative bit shift constants
  42. var BIT_SHIFTS = [1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28];
  43. // SBOXes and round permutation constants
  44. var SBOX_P = [
  45. {
  46. 0x0: 0x808200,
  47. 0x10000000: 0x8000,
  48. 0x20000000: 0x808002,
  49. 0x30000000: 0x2,
  50. 0x40000000: 0x200,
  51. 0x50000000: 0x808202,
  52. 0x60000000: 0x800202,
  53. 0x70000000: 0x800000,
  54. 0x80000000: 0x202,
  55. 0x90000000: 0x800200,
  56. 0xa0000000: 0x8200,
  57. 0xb0000000: 0x808000,
  58. 0xc0000000: 0x8002,
  59. 0xd0000000: 0x800002,
  60. 0xe0000000: 0x0,
  61. 0xf0000000: 0x8202,
  62. 0x8000000: 0x0,
  63. 0x18000000: 0x808202,
  64. 0x28000000: 0x8202,
  65. 0x38000000: 0x8000,
  66. 0x48000000: 0x808200,
  67. 0x58000000: 0x200,
  68. 0x68000000: 0x808002,
  69. 0x78000000: 0x2,
  70. 0x88000000: 0x800200,
  71. 0x98000000: 0x8200,
  72. 0xa8000000: 0x808000,
  73. 0xb8000000: 0x800202,
  74. 0xc8000000: 0x800002,
  75. 0xd8000000: 0x8002,
  76. 0xe8000000: 0x202,
  77. 0xf8000000: 0x800000,
  78. 0x1: 0x8000,
  79. 0x10000001: 0x2,
  80. 0x20000001: 0x808200,
  81. 0x30000001: 0x800000,
  82. 0x40000001: 0x808002,
  83. 0x50000001: 0x8200,
  84. 0x60000001: 0x200,
  85. 0x70000001: 0x800202,
  86. 0x80000001: 0x808202,
  87. 0x90000001: 0x808000,
  88. 0xa0000001: 0x800002,
  89. 0xb0000001: 0x8202,
  90. 0xc0000001: 0x202,
  91. 0xd0000001: 0x800200,
  92. 0xe0000001: 0x8002,
  93. 0xf0000001: 0x0,
  94. 0x8000001: 0x808202,
  95. 0x18000001: 0x808000,
  96. 0x28000001: 0x800000,
  97. 0x38000001: 0x200,
  98. 0x48000001: 0x8000,
  99. 0x58000001: 0x800002,
  100. 0x68000001: 0x2,
  101. 0x78000001: 0x8202,
  102. 0x88000001: 0x8002,
  103. 0x98000001: 0x800202,
  104. 0xa8000001: 0x202,
  105. 0xb8000001: 0x808200,
  106. 0xc8000001: 0x800200,
  107. 0xd8000001: 0x0,
  108. 0xe8000001: 0x8200,
  109. 0xf8000001: 0x808002
  110. },
  111. {
  112. 0x0: 0x40084010,
  113. 0x1000000: 0x4000,
  114. 0x2000000: 0x80000,
  115. 0x3000000: 0x40080010,
  116. 0x4000000: 0x40000010,
  117. 0x5000000: 0x40084000,
  118. 0x6000000: 0x40004000,
  119. 0x7000000: 0x10,
  120. 0x8000000: 0x84000,
  121. 0x9000000: 0x40004010,
  122. 0xa000000: 0x40000000,
  123. 0xb000000: 0x84010,
  124. 0xc000000: 0x80010,
  125. 0xd000000: 0x0,
  126. 0xe000000: 0x4010,
  127. 0xf000000: 0x40080000,
  128. 0x800000: 0x40004000,
  129. 0x1800000: 0x84010,
  130. 0x2800000: 0x10,
  131. 0x3800000: 0x40004010,
  132. 0x4800000: 0x40084010,
  133. 0x5800000: 0x40000000,
  134. 0x6800000: 0x80000,
  135. 0x7800000: 0x40080010,
  136. 0x8800000: 0x80010,
  137. 0x9800000: 0x0,
  138. 0xa800000: 0x4000,
  139. 0xb800000: 0x40080000,
  140. 0xc800000: 0x40000010,
  141. 0xd800000: 0x84000,
  142. 0xe800000: 0x40084000,
  143. 0xf800000: 0x4010,
  144. 0x10000000: 0x0,
  145. 0x11000000: 0x40080010,
  146. 0x12000000: 0x40004010,
  147. 0x13000000: 0x40084000,
  148. 0x14000000: 0x40080000,
  149. 0x15000000: 0x10,
  150. 0x16000000: 0x84010,
  151. 0x17000000: 0x4000,
  152. 0x18000000: 0x4010,
  153. 0x19000000: 0x80000,
  154. 0x1a000000: 0x80010,
  155. 0x1b000000: 0x40000010,
  156. 0x1c000000: 0x84000,
  157. 0x1d000000: 0x40004000,
  158. 0x1e000000: 0x40000000,
  159. 0x1f000000: 0x40084010,
  160. 0x10800000: 0x84010,
  161. 0x11800000: 0x80000,
  162. 0x12800000: 0x40080000,
  163. 0x13800000: 0x4000,
  164. 0x14800000: 0x40004000,
  165. 0x15800000: 0x40084010,
  166. 0x16800000: 0x10,
  167. 0x17800000: 0x40000000,
  168. 0x18800000: 0x40084000,
  169. 0x19800000: 0x40000010,
  170. 0x1a800000: 0x40004010,
  171. 0x1b800000: 0x80010,
  172. 0x1c800000: 0x0,
  173. 0x1d800000: 0x4010,
  174. 0x1e800000: 0x40080010,
  175. 0x1f800000: 0x84000
  176. },
  177. {
  178. 0x0: 0x104,
  179. 0x100000: 0x0,
  180. 0x200000: 0x4000100,
  181. 0x300000: 0x10104,
  182. 0x400000: 0x10004,
  183. 0x500000: 0x4000004,
  184. 0x600000: 0x4010104,
  185. 0x700000: 0x4010000,
  186. 0x800000: 0x4000000,
  187. 0x900000: 0x4010100,
  188. 0xa00000: 0x10100,
  189. 0xb00000: 0x4010004,
  190. 0xc00000: 0x4000104,
  191. 0xd00000: 0x10000,
  192. 0xe00000: 0x4,
  193. 0xf00000: 0x100,
  194. 0x80000: 0x4010100,
  195. 0x180000: 0x4010004,
  196. 0x280000: 0x0,
  197. 0x380000: 0x4000100,
  198. 0x480000: 0x4000004,
  199. 0x580000: 0x10000,
  200. 0x680000: 0x10004,
  201. 0x780000: 0x104,
  202. 0x880000: 0x4,
  203. 0x980000: 0x100,
  204. 0xa80000: 0x4010000,
  205. 0xb80000: 0x10104,
  206. 0xc80000: 0x10100,
  207. 0xd80000: 0x4000104,
  208. 0xe80000: 0x4010104,
  209. 0xf80000: 0x4000000,
  210. 0x1000000: 0x4010100,
  211. 0x1100000: 0x10004,
  212. 0x1200000: 0x10000,
  213. 0x1300000: 0x4000100,
  214. 0x1400000: 0x100,
  215. 0x1500000: 0x4010104,
  216. 0x1600000: 0x4000004,
  217. 0x1700000: 0x0,
  218. 0x1800000: 0x4000104,
  219. 0x1900000: 0x4000000,
  220. 0x1a00000: 0x4,
  221. 0x1b00000: 0x10100,
  222. 0x1c00000: 0x4010000,
  223. 0x1d00000: 0x104,
  224. 0x1e00000: 0x10104,
  225. 0x1f00000: 0x4010004,
  226. 0x1080000: 0x4000000,
  227. 0x1180000: 0x104,
  228. 0x1280000: 0x4010100,
  229. 0x1380000: 0x0,
  230. 0x1480000: 0x10004,
  231. 0x1580000: 0x4000100,
  232. 0x1680000: 0x100,
  233. 0x1780000: 0x4010004,
  234. 0x1880000: 0x10000,
  235. 0x1980000: 0x4010104,
  236. 0x1a80000: 0x10104,
  237. 0x1b80000: 0x4000004,
  238. 0x1c80000: 0x4000104,
  239. 0x1d80000: 0x4010000,
  240. 0x1e80000: 0x4,
  241. 0x1f80000: 0x10100
  242. },
  243. {
  244. 0x0: 0x80401000,
  245. 0x10000: 0x80001040,
  246. 0x20000: 0x401040,
  247. 0x30000: 0x80400000,
  248. 0x40000: 0x0,
  249. 0x50000: 0x401000,
  250. 0x60000: 0x80000040,
  251. 0x70000: 0x400040,
  252. 0x80000: 0x80000000,
  253. 0x90000: 0x400000,
  254. 0xa0000: 0x40,
  255. 0xb0000: 0x80001000,
  256. 0xc0000: 0x80400040,
  257. 0xd0000: 0x1040,
  258. 0xe0000: 0x1000,
  259. 0xf0000: 0x80401040,
  260. 0x8000: 0x80001040,
  261. 0x18000: 0x40,
  262. 0x28000: 0x80400040,
  263. 0x38000: 0x80001000,
  264. 0x48000: 0x401000,
  265. 0x58000: 0x80401040,
  266. 0x68000: 0x0,
  267. 0x78000: 0x80400000,
  268. 0x88000: 0x1000,
  269. 0x98000: 0x80401000,
  270. 0xa8000: 0x400000,
  271. 0xb8000: 0x1040,
  272. 0xc8000: 0x80000000,
  273. 0xd8000: 0x400040,
  274. 0xe8000: 0x401040,
  275. 0xf8000: 0x80000040,
  276. 0x100000: 0x400040,
  277. 0x110000: 0x401000,
  278. 0x120000: 0x80000040,
  279. 0x130000: 0x0,
  280. 0x140000: 0x1040,
  281. 0x150000: 0x80400040,
  282. 0x160000: 0x80401000,
  283. 0x170000: 0x80001040,
  284. 0x180000: 0x80401040,
  285. 0x190000: 0x80000000,
  286. 0x1a0000: 0x80400000,
  287. 0x1b0000: 0x401040,
  288. 0x1c0000: 0x80001000,
  289. 0x1d0000: 0x400000,
  290. 0x1e0000: 0x40,
  291. 0x1f0000: 0x1000,
  292. 0x108000: 0x80400000,
  293. 0x118000: 0x80401040,
  294. 0x128000: 0x0,
  295. 0x138000: 0x401000,
  296. 0x148000: 0x400040,
  297. 0x158000: 0x80000000,
  298. 0x168000: 0x80001040,
  299. 0x178000: 0x40,
  300. 0x188000: 0x80000040,
  301. 0x198000: 0x1000,
  302. 0x1a8000: 0x80001000,
  303. 0x1b8000: 0x80400040,
  304. 0x1c8000: 0x1040,
  305. 0x1d8000: 0x80401000,
  306. 0x1e8000: 0x400000,
  307. 0x1f8000: 0x401040
  308. },
  309. {
  310. 0x0: 0x80,
  311. 0x1000: 0x1040000,
  312. 0x2000: 0x40000,
  313. 0x3000: 0x20000000,
  314. 0x4000: 0x20040080,
  315. 0x5000: 0x1000080,
  316. 0x6000: 0x21000080,
  317. 0x7000: 0x40080,
  318. 0x8000: 0x1000000,
  319. 0x9000: 0x20040000,
  320. 0xa000: 0x20000080,
  321. 0xb000: 0x21040080,
  322. 0xc000: 0x21040000,
  323. 0xd000: 0x0,
  324. 0xe000: 0x1040080,
  325. 0xf000: 0x21000000,
  326. 0x800: 0x1040080,
  327. 0x1800: 0x21000080,
  328. 0x2800: 0x80,
  329. 0x3800: 0x1040000,
  330. 0x4800: 0x40000,
  331. 0x5800: 0x20040080,
  332. 0x6800: 0x21040000,
  333. 0x7800: 0x20000000,
  334. 0x8800: 0x20040000,
  335. 0x9800: 0x0,
  336. 0xa800: 0x21040080,
  337. 0xb800: 0x1000080,
  338. 0xc800: 0x20000080,
  339. 0xd800: 0x21000000,
  340. 0xe800: 0x1000000,
  341. 0xf800: 0x40080,
  342. 0x10000: 0x40000,
  343. 0x11000: 0x80,
  344. 0x12000: 0x20000000,
  345. 0x13000: 0x21000080,
  346. 0x14000: 0x1000080,
  347. 0x15000: 0x21040000,
  348. 0x16000: 0x20040080,
  349. 0x17000: 0x1000000,
  350. 0x18000: 0x21040080,
  351. 0x19000: 0x21000000,
  352. 0x1a000: 0x1040000,
  353. 0x1b000: 0x20040000,
  354. 0x1c000: 0x40080,
  355. 0x1d000: 0x20000080,
  356. 0x1e000: 0x0,
  357. 0x1f000: 0x1040080,
  358. 0x10800: 0x21000080,
  359. 0x11800: 0x1000000,
  360. 0x12800: 0x1040000,
  361. 0x13800: 0x20040080,
  362. 0x14800: 0x20000000,
  363. 0x15800: 0x1040080,
  364. 0x16800: 0x80,
  365. 0x17800: 0x21040000,
  366. 0x18800: 0x40080,
  367. 0x19800: 0x21040080,
  368. 0x1a800: 0x0,
  369. 0x1b800: 0x21000000,
  370. 0x1c800: 0x1000080,
  371. 0x1d800: 0x40000,
  372. 0x1e800: 0x20040000,
  373. 0x1f800: 0x20000080
  374. },
  375. {
  376. 0x0: 0x10000008,
  377. 0x100: 0x2000,
  378. 0x200: 0x10200000,
  379. 0x300: 0x10202008,
  380. 0x400: 0x10002000,
  381. 0x500: 0x200000,
  382. 0x600: 0x200008,
  383. 0x700: 0x10000000,
  384. 0x800: 0x0,
  385. 0x900: 0x10002008,
  386. 0xa00: 0x202000,
  387. 0xb00: 0x8,
  388. 0xc00: 0x10200008,
  389. 0xd00: 0x202008,
  390. 0xe00: 0x2008,
  391. 0xf00: 0x10202000,
  392. 0x80: 0x10200000,
  393. 0x180: 0x10202008,
  394. 0x280: 0x8,
  395. 0x380: 0x200000,
  396. 0x480: 0x202008,
  397. 0x580: 0x10000008,
  398. 0x680: 0x10002000,
  399. 0x780: 0x2008,
  400. 0x880: 0x200008,
  401. 0x980: 0x2000,
  402. 0xa80: 0x10002008,
  403. 0xb80: 0x10200008,
  404. 0xc80: 0x0,
  405. 0xd80: 0x10202000,
  406. 0xe80: 0x202000,
  407. 0xf80: 0x10000000,
  408. 0x1000: 0x10002000,
  409. 0x1100: 0x10200008,
  410. 0x1200: 0x10202008,
  411. 0x1300: 0x2008,
  412. 0x1400: 0x200000,
  413. 0x1500: 0x10000000,
  414. 0x1600: 0x10000008,
  415. 0x1700: 0x202000,
  416. 0x1800: 0x202008,
  417. 0x1900: 0x0,
  418. 0x1a00: 0x8,
  419. 0x1b00: 0x10200000,
  420. 0x1c00: 0x2000,
  421. 0x1d00: 0x10002008,
  422. 0x1e00: 0x10202000,
  423. 0x1f00: 0x200008,
  424. 0x1080: 0x8,
  425. 0x1180: 0x202000,
  426. 0x1280: 0x200000,
  427. 0x1380: 0x10000008,
  428. 0x1480: 0x10002000,
  429. 0x1580: 0x2008,
  430. 0x1680: 0x10202008,
  431. 0x1780: 0x10200000,
  432. 0x1880: 0x10202000,
  433. 0x1980: 0x10200008,
  434. 0x1a80: 0x2000,
  435. 0x1b80: 0x202008,
  436. 0x1c80: 0x200008,
  437. 0x1d80: 0x0,
  438. 0x1e80: 0x10000000,
  439. 0x1f80: 0x10002008
  440. },
  441. {
  442. 0x0: 0x100000,
  443. 0x10: 0x2000401,
  444. 0x20: 0x400,
  445. 0x30: 0x100401,
  446. 0x40: 0x2100401,
  447. 0x50: 0x0,
  448. 0x60: 0x1,
  449. 0x70: 0x2100001,
  450. 0x80: 0x2000400,
  451. 0x90: 0x100001,
  452. 0xa0: 0x2000001,
  453. 0xb0: 0x2100400,
  454. 0xc0: 0x2100000,
  455. 0xd0: 0x401,
  456. 0xe0: 0x100400,
  457. 0xf0: 0x2000000,
  458. 0x8: 0x2100001,
  459. 0x18: 0x0,
  460. 0x28: 0x2000401,
  461. 0x38: 0x2100400,
  462. 0x48: 0x100000,
  463. 0x58: 0x2000001,
  464. 0x68: 0x2000000,
  465. 0x78: 0x401,
  466. 0x88: 0x100401,
  467. 0x98: 0x2000400,
  468. 0xa8: 0x2100000,
  469. 0xb8: 0x100001,
  470. 0xc8: 0x400,
  471. 0xd8: 0x2100401,
  472. 0xe8: 0x1,
  473. 0xf8: 0x100400,
  474. 0x100: 0x2000000,
  475. 0x110: 0x100000,
  476. 0x120: 0x2000401,
  477. 0x130: 0x2100001,
  478. 0x140: 0x100001,
  479. 0x150: 0x2000400,
  480. 0x160: 0x2100400,
  481. 0x170: 0x100401,
  482. 0x180: 0x401,
  483. 0x190: 0x2100401,
  484. 0x1a0: 0x100400,
  485. 0x1b0: 0x1,
  486. 0x1c0: 0x0,
  487. 0x1d0: 0x2100000,
  488. 0x1e0: 0x2000001,
  489. 0x1f0: 0x400,
  490. 0x108: 0x100400,
  491. 0x118: 0x2000401,
  492. 0x128: 0x2100001,
  493. 0x138: 0x1,
  494. 0x148: 0x2000000,
  495. 0x158: 0x100000,
  496. 0x168: 0x401,
  497. 0x178: 0x2100400,
  498. 0x188: 0x2000001,
  499. 0x198: 0x2100000,
  500. 0x1a8: 0x0,
  501. 0x1b8: 0x2100401,
  502. 0x1c8: 0x100401,
  503. 0x1d8: 0x400,
  504. 0x1e8: 0x2000400,
  505. 0x1f8: 0x100001
  506. },
  507. {
  508. 0x0: 0x8000820,
  509. 0x1: 0x20000,
  510. 0x2: 0x8000000,
  511. 0x3: 0x20,
  512. 0x4: 0x20020,
  513. 0x5: 0x8020820,
  514. 0x6: 0x8020800,
  515. 0x7: 0x800,
  516. 0x8: 0x8020000,
  517. 0x9: 0x8000800,
  518. 0xa: 0x20800,
  519. 0xb: 0x8020020,
  520. 0xc: 0x820,
  521. 0xd: 0x0,
  522. 0xe: 0x8000020,
  523. 0xf: 0x20820,
  524. 0x80000000: 0x800,
  525. 0x80000001: 0x8020820,
  526. 0x80000002: 0x8000820,
  527. 0x80000003: 0x8000000,
  528. 0x80000004: 0x8020000,
  529. 0x80000005: 0x20800,
  530. 0x80000006: 0x20820,
  531. 0x80000007: 0x20,
  532. 0x80000008: 0x8000020,
  533. 0x80000009: 0x820,
  534. 0x8000000a: 0x20020,
  535. 0x8000000b: 0x8020800,
  536. 0x8000000c: 0x0,
  537. 0x8000000d: 0x8020020,
  538. 0x8000000e: 0x8000800,
  539. 0x8000000f: 0x20000,
  540. 0x10: 0x20820,
  541. 0x11: 0x8020800,
  542. 0x12: 0x20,
  543. 0x13: 0x800,
  544. 0x14: 0x8000800,
  545. 0x15: 0x8000020,
  546. 0x16: 0x8020020,
  547. 0x17: 0x20000,
  548. 0x18: 0x0,
  549. 0x19: 0x20020,
  550. 0x1a: 0x8020000,
  551. 0x1b: 0x8000820,
  552. 0x1c: 0x8020820,
  553. 0x1d: 0x20800,
  554. 0x1e: 0x820,
  555. 0x1f: 0x8000000,
  556. 0x80000010: 0x20000,
  557. 0x80000011: 0x800,
  558. 0x80000012: 0x8020020,
  559. 0x80000013: 0x20820,
  560. 0x80000014: 0x20,
  561. 0x80000015: 0x8020000,
  562. 0x80000016: 0x8000000,
  563. 0x80000017: 0x8000820,
  564. 0x80000018: 0x8020820,
  565. 0x80000019: 0x8000020,
  566. 0x8000001a: 0x8000800,
  567. 0x8000001b: 0x0,
  568. 0x8000001c: 0x20800,
  569. 0x8000001d: 0x820,
  570. 0x8000001e: 0x20020,
  571. 0x8000001f: 0x8020800
  572. }
  573. ];
  574. // Masks that select the SBOX input
  575. var SBOX_MASK = [
  576. 0xf8000001, 0x1f800000, 0x01f80000, 0x001f8000,
  577. 0x0001f800, 0x00001f80, 0x000001f8, 0x8000001f
  578. ];
  579. /**
  580. * DES block cipher algorithm.
  581. */
  582. var DES = C_algo.DES = BlockCipher.extend({
  583. _doReset: function () {
  584. // Shortcuts
  585. var key = this._key;
  586. var keyWords = key.words;
  587. // Select 56 bits according to PC1
  588. var keyBits = [];
  589. for (var i = 0; i < 56; i++) {
  590. var keyBitPos = PC1[i] - 1;
  591. keyBits[i] = (keyWords[keyBitPos >>> 5] >>> (31 - keyBitPos % 32)) & 1;
  592. }
  593. // Assemble 16 subkeys
  594. var subKeys = this._subKeys = [];
  595. for (var nSubKey = 0; nSubKey < 16; nSubKey++) {
  596. // Create subkey
  597. var subKey = subKeys[nSubKey] = [];
  598. // Shortcut
  599. var bitShift = BIT_SHIFTS[nSubKey];
  600. // Select 48 bits according to PC2
  601. for (var i = 0; i < 24; i++) {
  602. // Select from the left 28 key bits
  603. subKey[(i / 6) | 0] |= keyBits[((PC2[i] - 1) + bitShift) % 28] << (31 - i % 6);
  604. // Select from the right 28 key bits
  605. subKey[4 + ((i / 6) | 0)] |= keyBits[28 + (((PC2[i + 24] - 1) + bitShift) % 28)] << (31 - i % 6);
  606. }
  607. // Since each subkey is applied to an expanded 32-bit input,
  608. // the subkey can be broken into 8 values scaled to 32-bits,
  609. // which allows the key to be used without expansion
  610. subKey[0] = (subKey[0] << 1) | (subKey[0] >>> 31);
  611. for (var i = 1; i < 7; i++) {
  612. subKey[i] = subKey[i] >>> ((i - 1) * 4 + 3);
  613. }
  614. subKey[7] = (subKey[7] << 5) | (subKey[7] >>> 27);
  615. }
  616. // Compute inverse subkeys
  617. var invSubKeys = this._invSubKeys = [];
  618. for (var i = 0; i < 16; i++) {
  619. invSubKeys[i] = subKeys[15 - i];
  620. }
  621. },
  622. encryptBlock: function (M, offset) {
  623. this._doCryptBlock(M, offset, this._subKeys);
  624. },
  625. decryptBlock: function (M, offset) {
  626. this._doCryptBlock(M, offset, this._invSubKeys);
  627. },
  628. _doCryptBlock: function (M, offset, subKeys) {
  629. // Get input
  630. this._lBlock = M[offset];
  631. this._rBlock = M[offset + 1];
  632. // Initial permutation
  633. exchangeLR.call(this, 4, 0x0f0f0f0f);
  634. exchangeLR.call(this, 16, 0x0000ffff);
  635. exchangeRL.call(this, 2, 0x33333333);
  636. exchangeRL.call(this, 8, 0x00ff00ff);
  637. exchangeLR.call(this, 1, 0x55555555);
  638. // Rounds
  639. for (var round = 0; round < 16; round++) {
  640. // Shortcuts
  641. var subKey = subKeys[round];
  642. var lBlock = this._lBlock;
  643. var rBlock = this._rBlock;
  644. // Feistel function
  645. var f = 0;
  646. for (var i = 0; i < 8; i++) {
  647. f |= SBOX_P[i][((rBlock ^ subKey[i]) & SBOX_MASK[i]) >>> 0];
  648. }
  649. this._lBlock = rBlock;
  650. this._rBlock = lBlock ^ f;
  651. }
  652. // Undo swap from last round
  653. var t = this._lBlock;
  654. this._lBlock = this._rBlock;
  655. this._rBlock = t;
  656. // Final permutation
  657. exchangeLR.call(this, 1, 0x55555555);
  658. exchangeRL.call(this, 8, 0x00ff00ff);
  659. exchangeRL.call(this, 2, 0x33333333);
  660. exchangeLR.call(this, 16, 0x0000ffff);
  661. exchangeLR.call(this, 4, 0x0f0f0f0f);
  662. // Set output
  663. M[offset] = this._lBlock;
  664. M[offset + 1] = this._rBlock;
  665. },
  666. keySize: 64 / 32,
  667. ivSize: 64 / 32,
  668. blockSize: 64 / 32
  669. });
  670. // Swap bits across the left and right words
  671. function exchangeLR(offset, mask) {
  672. var t = ((this._lBlock >>> offset) ^ this._rBlock) & mask;
  673. this._rBlock ^= t;
  674. this._lBlock ^= t << offset;
  675. }
  676. function exchangeRL(offset, mask) {
  677. var t = ((this._rBlock >>> offset) ^ this._lBlock) & mask;
  678. this._lBlock ^= t;
  679. this._rBlock ^= t << offset;
  680. }
  681. /**
  682. * Shortcut functions to the cipher's object interface.
  683. *
  684. * @example
  685. *
  686. * var ciphertext = CryptoJS.DES.encrypt(message, key, cfg);
  687. * var plaintext = CryptoJS.DES.decrypt(ciphertext, key, cfg);
  688. */
  689. C.DES = BlockCipher._createHelper(DES);
  690. /**
  691. * Triple-DES block cipher algorithm.
  692. */
  693. var TripleDES = C_algo.TripleDES = BlockCipher.extend({
  694. _doReset: function () {
  695. // Shortcuts
  696. var key = this._key;
  697. var keyWords = key.words;
  698. // Create DES instances
  699. this._des1 = DES.createEncryptor(WordArray.create(keyWords.slice(0, 2)));
  700. this._des2 = DES.createEncryptor(WordArray.create(keyWords.slice(2, 4)));
  701. this._des3 = DES.createEncryptor(WordArray.create(keyWords.slice(4, 6)));
  702. },
  703. encryptBlock: function (M, offset) {
  704. this._des1.encryptBlock(M, offset);
  705. this._des2.decryptBlock(M, offset);
  706. this._des3.encryptBlock(M, offset);
  707. },
  708. decryptBlock: function (M, offset) {
  709. this._des3.decryptBlock(M, offset);
  710. this._des2.encryptBlock(M, offset);
  711. this._des1.decryptBlock(M, offset);
  712. },
  713. keySize: 192 / 32,
  714. ivSize: 64 / 32,
  715. blockSize: 64 / 32
  716. });
  717. /**
  718. * Shortcut functions to the cipher's object interface.
  719. *
  720. * @example
  721. *
  722. * var ciphertext = CryptoJS.TripleDES.encrypt(message, key, cfg);
  723. * var plaintext = CryptoJS.TripleDES.decrypt(ciphertext, key, cfg);
  724. */
  725. C.TripleDES = BlockCipher._createHelper(TripleDES);
  726. }());
  727. return CryptoJS.TripleDES;
  728. }));