tripledes.js 24 KB

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