tripledes.js 23 KB

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