crypto.js 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var sharedUtil = require('../shared/util.js');
  17. var corePrimitives = require('./primitives.js');
  18. var coreStream = require('./stream.js');
  19. var PasswordException = sharedUtil.PasswordException;
  20. var PasswordResponses = sharedUtil.PasswordResponses;
  21. var bytesToString = sharedUtil.bytesToString;
  22. var warn = sharedUtil.warn;
  23. var error = sharedUtil.error;
  24. var assert = sharedUtil.assert;
  25. var isInt = sharedUtil.isInt;
  26. var stringToBytes = sharedUtil.stringToBytes;
  27. var utf8StringToString = sharedUtil.utf8StringToString;
  28. var Name = corePrimitives.Name;
  29. var isName = corePrimitives.isName;
  30. var isDict = corePrimitives.isDict;
  31. var DecryptStream = coreStream.DecryptStream;
  32. var ARCFourCipher = function ARCFourCipherClosure() {
  33. function ARCFourCipher(key) {
  34. this.a = 0;
  35. this.b = 0;
  36. var s = new Uint8Array(256);
  37. var i, j = 0, tmp, keyLength = key.length;
  38. for (i = 0; i < 256; ++i) {
  39. s[i] = i;
  40. }
  41. for (i = 0; i < 256; ++i) {
  42. tmp = s[i];
  43. j = j + tmp + key[i % keyLength] & 0xFF;
  44. s[i] = s[j];
  45. s[j] = tmp;
  46. }
  47. this.s = s;
  48. }
  49. ARCFourCipher.prototype = {
  50. encryptBlock: function ARCFourCipher_encryptBlock(data) {
  51. var i, n = data.length, tmp, tmp2;
  52. var a = this.a, b = this.b, s = this.s;
  53. var output = new Uint8Array(n);
  54. for (i = 0; i < n; ++i) {
  55. a = a + 1 & 0xFF;
  56. tmp = s[a];
  57. b = b + tmp & 0xFF;
  58. tmp2 = s[b];
  59. s[a] = tmp2;
  60. s[b] = tmp;
  61. output[i] = data[i] ^ s[tmp + tmp2 & 0xFF];
  62. }
  63. this.a = a;
  64. this.b = b;
  65. return output;
  66. }
  67. };
  68. ARCFourCipher.prototype.decryptBlock = ARCFourCipher.prototype.encryptBlock;
  69. return ARCFourCipher;
  70. }();
  71. var calculateMD5 = function calculateMD5Closure() {
  72. var r = new Uint8Array([
  73. 7,
  74. 12,
  75. 17,
  76. 22,
  77. 7,
  78. 12,
  79. 17,
  80. 22,
  81. 7,
  82. 12,
  83. 17,
  84. 22,
  85. 7,
  86. 12,
  87. 17,
  88. 22,
  89. 5,
  90. 9,
  91. 14,
  92. 20,
  93. 5,
  94. 9,
  95. 14,
  96. 20,
  97. 5,
  98. 9,
  99. 14,
  100. 20,
  101. 5,
  102. 9,
  103. 14,
  104. 20,
  105. 4,
  106. 11,
  107. 16,
  108. 23,
  109. 4,
  110. 11,
  111. 16,
  112. 23,
  113. 4,
  114. 11,
  115. 16,
  116. 23,
  117. 4,
  118. 11,
  119. 16,
  120. 23,
  121. 6,
  122. 10,
  123. 15,
  124. 21,
  125. 6,
  126. 10,
  127. 15,
  128. 21,
  129. 6,
  130. 10,
  131. 15,
  132. 21,
  133. 6,
  134. 10,
  135. 15,
  136. 21
  137. ]);
  138. var k = new Int32Array([
  139. -680876936,
  140. -389564586,
  141. 606105819,
  142. -1044525330,
  143. -176418897,
  144. 1200080426,
  145. -1473231341,
  146. -45705983,
  147. 1770035416,
  148. -1958414417,
  149. -42063,
  150. -1990404162,
  151. 1804603682,
  152. -40341101,
  153. -1502002290,
  154. 1236535329,
  155. -165796510,
  156. -1069501632,
  157. 643717713,
  158. -373897302,
  159. -701558691,
  160. 38016083,
  161. -660478335,
  162. -405537848,
  163. 568446438,
  164. -1019803690,
  165. -187363961,
  166. 1163531501,
  167. -1444681467,
  168. -51403784,
  169. 1735328473,
  170. -1926607734,
  171. -378558,
  172. -2022574463,
  173. 1839030562,
  174. -35309556,
  175. -1530992060,
  176. 1272893353,
  177. -155497632,
  178. -1094730640,
  179. 681279174,
  180. -358537222,
  181. -722521979,
  182. 76029189,
  183. -640364487,
  184. -421815835,
  185. 530742520,
  186. -995338651,
  187. -198630844,
  188. 1126891415,
  189. -1416354905,
  190. -57434055,
  191. 1700485571,
  192. -1894986606,
  193. -1051523,
  194. -2054922799,
  195. 1873313359,
  196. -30611744,
  197. -1560198380,
  198. 1309151649,
  199. -145523070,
  200. -1120210379,
  201. 718787259,
  202. -343485551
  203. ]);
  204. function hash(data, offset, length) {
  205. var h0 = 1732584193, h1 = -271733879, h2 = -1732584194, h3 = 271733878;
  206. var paddedLength = length + 72 & ~63;
  207. var padded = new Uint8Array(paddedLength);
  208. var i, j, n;
  209. for (i = 0; i < length; ++i) {
  210. padded[i] = data[offset++];
  211. }
  212. padded[i++] = 0x80;
  213. n = paddedLength - 8;
  214. while (i < n) {
  215. padded[i++] = 0;
  216. }
  217. padded[i++] = length << 3 & 0xFF;
  218. padded[i++] = length >> 5 & 0xFF;
  219. padded[i++] = length >> 13 & 0xFF;
  220. padded[i++] = length >> 21 & 0xFF;
  221. padded[i++] = length >>> 29 & 0xFF;
  222. padded[i++] = 0;
  223. padded[i++] = 0;
  224. padded[i++] = 0;
  225. var w = new Int32Array(16);
  226. for (i = 0; i < paddedLength;) {
  227. for (j = 0; j < 16; ++j, i += 4) {
  228. w[j] = padded[i] | padded[i + 1] << 8 | padded[i + 2] << 16 | padded[i + 3] << 24;
  229. }
  230. var a = h0, b = h1, c = h2, d = h3, f, g;
  231. for (j = 0; j < 64; ++j) {
  232. if (j < 16) {
  233. f = b & c | ~b & d;
  234. g = j;
  235. } else if (j < 32) {
  236. f = d & b | ~d & c;
  237. g = 5 * j + 1 & 15;
  238. } else if (j < 48) {
  239. f = b ^ c ^ d;
  240. g = 3 * j + 5 & 15;
  241. } else {
  242. f = c ^ (b | ~d);
  243. g = 7 * j & 15;
  244. }
  245. var tmp = d, rotateArg = a + f + k[j] + w[g] | 0, rotate = r[j];
  246. d = c;
  247. c = b;
  248. b = b + (rotateArg << rotate | rotateArg >>> 32 - rotate) | 0;
  249. a = tmp;
  250. }
  251. h0 = h0 + a | 0;
  252. h1 = h1 + b | 0;
  253. h2 = h2 + c | 0;
  254. h3 = h3 + d | 0;
  255. }
  256. return new Uint8Array([
  257. h0 & 0xFF,
  258. h0 >> 8 & 0xFF,
  259. h0 >> 16 & 0xFF,
  260. h0 >>> 24 & 0xFF,
  261. h1 & 0xFF,
  262. h1 >> 8 & 0xFF,
  263. h1 >> 16 & 0xFF,
  264. h1 >>> 24 & 0xFF,
  265. h2 & 0xFF,
  266. h2 >> 8 & 0xFF,
  267. h2 >> 16 & 0xFF,
  268. h2 >>> 24 & 0xFF,
  269. h3 & 0xFF,
  270. h3 >> 8 & 0xFF,
  271. h3 >> 16 & 0xFF,
  272. h3 >>> 24 & 0xFF
  273. ]);
  274. }
  275. return hash;
  276. }();
  277. var Word64 = function Word64Closure() {
  278. function Word64(highInteger, lowInteger) {
  279. this.high = highInteger | 0;
  280. this.low = lowInteger | 0;
  281. }
  282. Word64.prototype = {
  283. and: function Word64_and(word) {
  284. this.high &= word.high;
  285. this.low &= word.low;
  286. },
  287. xor: function Word64_xor(word) {
  288. this.high ^= word.high;
  289. this.low ^= word.low;
  290. },
  291. or: function Word64_or(word) {
  292. this.high |= word.high;
  293. this.low |= word.low;
  294. },
  295. shiftRight: function Word64_shiftRight(places) {
  296. if (places >= 32) {
  297. this.low = this.high >>> places - 32 | 0;
  298. this.high = 0;
  299. } else {
  300. this.low = this.low >>> places | this.high << 32 - places;
  301. this.high = this.high >>> places | 0;
  302. }
  303. },
  304. shiftLeft: function Word64_shiftLeft(places) {
  305. if (places >= 32) {
  306. this.high = this.low << places - 32;
  307. this.low = 0;
  308. } else {
  309. this.high = this.high << places | this.low >>> 32 - places;
  310. this.low = this.low << places;
  311. }
  312. },
  313. rotateRight: function Word64_rotateRight(places) {
  314. var low, high;
  315. if (places & 32) {
  316. high = this.low;
  317. low = this.high;
  318. } else {
  319. low = this.low;
  320. high = this.high;
  321. }
  322. places &= 31;
  323. this.low = low >>> places | high << 32 - places;
  324. this.high = high >>> places | low << 32 - places;
  325. },
  326. not: function Word64_not() {
  327. this.high = ~this.high;
  328. this.low = ~this.low;
  329. },
  330. add: function Word64_add(word) {
  331. var lowAdd = (this.low >>> 0) + (word.low >>> 0);
  332. var highAdd = (this.high >>> 0) + (word.high >>> 0);
  333. if (lowAdd > 0xFFFFFFFF) {
  334. highAdd += 1;
  335. }
  336. this.low = lowAdd | 0;
  337. this.high = highAdd | 0;
  338. },
  339. copyTo: function Word64_copyTo(bytes, offset) {
  340. bytes[offset] = this.high >>> 24 & 0xFF;
  341. bytes[offset + 1] = this.high >> 16 & 0xFF;
  342. bytes[offset + 2] = this.high >> 8 & 0xFF;
  343. bytes[offset + 3] = this.high & 0xFF;
  344. bytes[offset + 4] = this.low >>> 24 & 0xFF;
  345. bytes[offset + 5] = this.low >> 16 & 0xFF;
  346. bytes[offset + 6] = this.low >> 8 & 0xFF;
  347. bytes[offset + 7] = this.low & 0xFF;
  348. },
  349. assign: function Word64_assign(word) {
  350. this.high = word.high;
  351. this.low = word.low;
  352. }
  353. };
  354. return Word64;
  355. }();
  356. var calculateSHA256 = function calculateSHA256Closure() {
  357. function rotr(x, n) {
  358. return x >>> n | x << 32 - n;
  359. }
  360. function ch(x, y, z) {
  361. return x & y ^ ~x & z;
  362. }
  363. function maj(x, y, z) {
  364. return x & y ^ x & z ^ y & z;
  365. }
  366. function sigma(x) {
  367. return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22);
  368. }
  369. function sigmaPrime(x) {
  370. return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25);
  371. }
  372. function littleSigma(x) {
  373. return rotr(x, 7) ^ rotr(x, 18) ^ x >>> 3;
  374. }
  375. function littleSigmaPrime(x) {
  376. return rotr(x, 17) ^ rotr(x, 19) ^ x >>> 10;
  377. }
  378. var k = [
  379. 0x428a2f98,
  380. 0x71374491,
  381. 0xb5c0fbcf,
  382. 0xe9b5dba5,
  383. 0x3956c25b,
  384. 0x59f111f1,
  385. 0x923f82a4,
  386. 0xab1c5ed5,
  387. 0xd807aa98,
  388. 0x12835b01,
  389. 0x243185be,
  390. 0x550c7dc3,
  391. 0x72be5d74,
  392. 0x80deb1fe,
  393. 0x9bdc06a7,
  394. 0xc19bf174,
  395. 0xe49b69c1,
  396. 0xefbe4786,
  397. 0x0fc19dc6,
  398. 0x240ca1cc,
  399. 0x2de92c6f,
  400. 0x4a7484aa,
  401. 0x5cb0a9dc,
  402. 0x76f988da,
  403. 0x983e5152,
  404. 0xa831c66d,
  405. 0xb00327c8,
  406. 0xbf597fc7,
  407. 0xc6e00bf3,
  408. 0xd5a79147,
  409. 0x06ca6351,
  410. 0x14292967,
  411. 0x27b70a85,
  412. 0x2e1b2138,
  413. 0x4d2c6dfc,
  414. 0x53380d13,
  415. 0x650a7354,
  416. 0x766a0abb,
  417. 0x81c2c92e,
  418. 0x92722c85,
  419. 0xa2bfe8a1,
  420. 0xa81a664b,
  421. 0xc24b8b70,
  422. 0xc76c51a3,
  423. 0xd192e819,
  424. 0xd6990624,
  425. 0xf40e3585,
  426. 0x106aa070,
  427. 0x19a4c116,
  428. 0x1e376c08,
  429. 0x2748774c,
  430. 0x34b0bcb5,
  431. 0x391c0cb3,
  432. 0x4ed8aa4a,
  433. 0x5b9cca4f,
  434. 0x682e6ff3,
  435. 0x748f82ee,
  436. 0x78a5636f,
  437. 0x84c87814,
  438. 0x8cc70208,
  439. 0x90befffa,
  440. 0xa4506ceb,
  441. 0xbef9a3f7,
  442. 0xc67178f2
  443. ];
  444. function hash(data, offset, length) {
  445. var h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a, h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19;
  446. var paddedLength = Math.ceil((length + 9) / 64) * 64;
  447. var padded = new Uint8Array(paddedLength);
  448. var i, j, n;
  449. for (i = 0; i < length; ++i) {
  450. padded[i] = data[offset++];
  451. }
  452. padded[i++] = 0x80;
  453. n = paddedLength - 8;
  454. while (i < n) {
  455. padded[i++] = 0;
  456. }
  457. padded[i++] = 0;
  458. padded[i++] = 0;
  459. padded[i++] = 0;
  460. padded[i++] = length >>> 29 & 0xFF;
  461. padded[i++] = length >> 21 & 0xFF;
  462. padded[i++] = length >> 13 & 0xFF;
  463. padded[i++] = length >> 5 & 0xFF;
  464. padded[i++] = length << 3 & 0xFF;
  465. var w = new Uint32Array(64);
  466. for (i = 0; i < paddedLength;) {
  467. for (j = 0; j < 16; ++j) {
  468. w[j] = padded[i] << 24 | padded[i + 1] << 16 | padded[i + 2] << 8 | padded[i + 3];
  469. i += 4;
  470. }
  471. for (j = 16; j < 64; ++j) {
  472. w[j] = littleSigmaPrime(w[j - 2]) + w[j - 7] + littleSigma(w[j - 15]) + w[j - 16] | 0;
  473. }
  474. var a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7, t1, t2;
  475. for (j = 0; j < 64; ++j) {
  476. t1 = h + sigmaPrime(e) + ch(e, f, g) + k[j] + w[j];
  477. t2 = sigma(a) + maj(a, b, c);
  478. h = g;
  479. g = f;
  480. f = e;
  481. e = d + t1 | 0;
  482. d = c;
  483. c = b;
  484. b = a;
  485. a = t1 + t2 | 0;
  486. }
  487. h0 = h0 + a | 0;
  488. h1 = h1 + b | 0;
  489. h2 = h2 + c | 0;
  490. h3 = h3 + d | 0;
  491. h4 = h4 + e | 0;
  492. h5 = h5 + f | 0;
  493. h6 = h6 + g | 0;
  494. h7 = h7 + h | 0;
  495. }
  496. return new Uint8Array([
  497. h0 >> 24 & 0xFF,
  498. h0 >> 16 & 0xFF,
  499. h0 >> 8 & 0xFF,
  500. h0 & 0xFF,
  501. h1 >> 24 & 0xFF,
  502. h1 >> 16 & 0xFF,
  503. h1 >> 8 & 0xFF,
  504. h1 & 0xFF,
  505. h2 >> 24 & 0xFF,
  506. h2 >> 16 & 0xFF,
  507. h2 >> 8 & 0xFF,
  508. h2 & 0xFF,
  509. h3 >> 24 & 0xFF,
  510. h3 >> 16 & 0xFF,
  511. h3 >> 8 & 0xFF,
  512. h3 & 0xFF,
  513. h4 >> 24 & 0xFF,
  514. h4 >> 16 & 0xFF,
  515. h4 >> 8 & 0xFF,
  516. h4 & 0xFF,
  517. h5 >> 24 & 0xFF,
  518. h5 >> 16 & 0xFF,
  519. h5 >> 8 & 0xFF,
  520. h5 & 0xFF,
  521. h6 >> 24 & 0xFF,
  522. h6 >> 16 & 0xFF,
  523. h6 >> 8 & 0xFF,
  524. h6 & 0xFF,
  525. h7 >> 24 & 0xFF,
  526. h7 >> 16 & 0xFF,
  527. h7 >> 8 & 0xFF,
  528. h7 & 0xFF
  529. ]);
  530. }
  531. return hash;
  532. }();
  533. var calculateSHA512 = function calculateSHA512Closure() {
  534. function ch(result, x, y, z, tmp) {
  535. result.assign(x);
  536. result.and(y);
  537. tmp.assign(x);
  538. tmp.not();
  539. tmp.and(z);
  540. result.xor(tmp);
  541. }
  542. function maj(result, x, y, z, tmp) {
  543. result.assign(x);
  544. result.and(y);
  545. tmp.assign(x);
  546. tmp.and(z);
  547. result.xor(tmp);
  548. tmp.assign(y);
  549. tmp.and(z);
  550. result.xor(tmp);
  551. }
  552. function sigma(result, x, tmp) {
  553. result.assign(x);
  554. result.rotateRight(28);
  555. tmp.assign(x);
  556. tmp.rotateRight(34);
  557. result.xor(tmp);
  558. tmp.assign(x);
  559. tmp.rotateRight(39);
  560. result.xor(tmp);
  561. }
  562. function sigmaPrime(result, x, tmp) {
  563. result.assign(x);
  564. result.rotateRight(14);
  565. tmp.assign(x);
  566. tmp.rotateRight(18);
  567. result.xor(tmp);
  568. tmp.assign(x);
  569. tmp.rotateRight(41);
  570. result.xor(tmp);
  571. }
  572. function littleSigma(result, x, tmp) {
  573. result.assign(x);
  574. result.rotateRight(1);
  575. tmp.assign(x);
  576. tmp.rotateRight(8);
  577. result.xor(tmp);
  578. tmp.assign(x);
  579. tmp.shiftRight(7);
  580. result.xor(tmp);
  581. }
  582. function littleSigmaPrime(result, x, tmp) {
  583. result.assign(x);
  584. result.rotateRight(19);
  585. tmp.assign(x);
  586. tmp.rotateRight(61);
  587. result.xor(tmp);
  588. tmp.assign(x);
  589. tmp.shiftRight(6);
  590. result.xor(tmp);
  591. }
  592. var k = [
  593. new Word64(0x428a2f98, 0xd728ae22),
  594. new Word64(0x71374491, 0x23ef65cd),
  595. new Word64(0xb5c0fbcf, 0xec4d3b2f),
  596. new Word64(0xe9b5dba5, 0x8189dbbc),
  597. new Word64(0x3956c25b, 0xf348b538),
  598. new Word64(0x59f111f1, 0xb605d019),
  599. new Word64(0x923f82a4, 0xaf194f9b),
  600. new Word64(0xab1c5ed5, 0xda6d8118),
  601. new Word64(0xd807aa98, 0xa3030242),
  602. new Word64(0x12835b01, 0x45706fbe),
  603. new Word64(0x243185be, 0x4ee4b28c),
  604. new Word64(0x550c7dc3, 0xd5ffb4e2),
  605. new Word64(0x72be5d74, 0xf27b896f),
  606. new Word64(0x80deb1fe, 0x3b1696b1),
  607. new Word64(0x9bdc06a7, 0x25c71235),
  608. new Word64(0xc19bf174, 0xcf692694),
  609. new Word64(0xe49b69c1, 0x9ef14ad2),
  610. new Word64(0xefbe4786, 0x384f25e3),
  611. new Word64(0x0fc19dc6, 0x8b8cd5b5),
  612. new Word64(0x240ca1cc, 0x77ac9c65),
  613. new Word64(0x2de92c6f, 0x592b0275),
  614. new Word64(0x4a7484aa, 0x6ea6e483),
  615. new Word64(0x5cb0a9dc, 0xbd41fbd4),
  616. new Word64(0x76f988da, 0x831153b5),
  617. new Word64(0x983e5152, 0xee66dfab),
  618. new Word64(0xa831c66d, 0x2db43210),
  619. new Word64(0xb00327c8, 0x98fb213f),
  620. new Word64(0xbf597fc7, 0xbeef0ee4),
  621. new Word64(0xc6e00bf3, 0x3da88fc2),
  622. new Word64(0xd5a79147, 0x930aa725),
  623. new Word64(0x06ca6351, 0xe003826f),
  624. new Word64(0x14292967, 0x0a0e6e70),
  625. new Word64(0x27b70a85, 0x46d22ffc),
  626. new Word64(0x2e1b2138, 0x5c26c926),
  627. new Word64(0x4d2c6dfc, 0x5ac42aed),
  628. new Word64(0x53380d13, 0x9d95b3df),
  629. new Word64(0x650a7354, 0x8baf63de),
  630. new Word64(0x766a0abb, 0x3c77b2a8),
  631. new Word64(0x81c2c92e, 0x47edaee6),
  632. new Word64(0x92722c85, 0x1482353b),
  633. new Word64(0xa2bfe8a1, 0x4cf10364),
  634. new Word64(0xa81a664b, 0xbc423001),
  635. new Word64(0xc24b8b70, 0xd0f89791),
  636. new Word64(0xc76c51a3, 0x0654be30),
  637. new Word64(0xd192e819, 0xd6ef5218),
  638. new Word64(0xd6990624, 0x5565a910),
  639. new Word64(0xf40e3585, 0x5771202a),
  640. new Word64(0x106aa070, 0x32bbd1b8),
  641. new Word64(0x19a4c116, 0xb8d2d0c8),
  642. new Word64(0x1e376c08, 0x5141ab53),
  643. new Word64(0x2748774c, 0xdf8eeb99),
  644. new Word64(0x34b0bcb5, 0xe19b48a8),
  645. new Word64(0x391c0cb3, 0xc5c95a63),
  646. new Word64(0x4ed8aa4a, 0xe3418acb),
  647. new Word64(0x5b9cca4f, 0x7763e373),
  648. new Word64(0x682e6ff3, 0xd6b2b8a3),
  649. new Word64(0x748f82ee, 0x5defb2fc),
  650. new Word64(0x78a5636f, 0x43172f60),
  651. new Word64(0x84c87814, 0xa1f0ab72),
  652. new Word64(0x8cc70208, 0x1a6439ec),
  653. new Word64(0x90befffa, 0x23631e28),
  654. new Word64(0xa4506ceb, 0xde82bde9),
  655. new Word64(0xbef9a3f7, 0xb2c67915),
  656. new Word64(0xc67178f2, 0xe372532b),
  657. new Word64(0xca273ece, 0xea26619c),
  658. new Word64(0xd186b8c7, 0x21c0c207),
  659. new Word64(0xeada7dd6, 0xcde0eb1e),
  660. new Word64(0xf57d4f7f, 0xee6ed178),
  661. new Word64(0x06f067aa, 0x72176fba),
  662. new Word64(0x0a637dc5, 0xa2c898a6),
  663. new Word64(0x113f9804, 0xbef90dae),
  664. new Word64(0x1b710b35, 0x131c471b),
  665. new Word64(0x28db77f5, 0x23047d84),
  666. new Word64(0x32caab7b, 0x40c72493),
  667. new Word64(0x3c9ebe0a, 0x15c9bebc),
  668. new Word64(0x431d67c4, 0x9c100d4c),
  669. new Word64(0x4cc5d4be, 0xcb3e42b6),
  670. new Word64(0x597f299c, 0xfc657e2a),
  671. new Word64(0x5fcb6fab, 0x3ad6faec),
  672. new Word64(0x6c44198c, 0x4a475817)
  673. ];
  674. function hash(data, offset, length, mode384) {
  675. mode384 = !!mode384;
  676. var h0, h1, h2, h3, h4, h5, h6, h7;
  677. if (!mode384) {
  678. h0 = new Word64(0x6a09e667, 0xf3bcc908);
  679. h1 = new Word64(0xbb67ae85, 0x84caa73b);
  680. h2 = new Word64(0x3c6ef372, 0xfe94f82b);
  681. h3 = new Word64(0xa54ff53a, 0x5f1d36f1);
  682. h4 = new Word64(0x510e527f, 0xade682d1);
  683. h5 = new Word64(0x9b05688c, 0x2b3e6c1f);
  684. h6 = new Word64(0x1f83d9ab, 0xfb41bd6b);
  685. h7 = new Word64(0x5be0cd19, 0x137e2179);
  686. } else {
  687. h0 = new Word64(0xcbbb9d5d, 0xc1059ed8);
  688. h1 = new Word64(0x629a292a, 0x367cd507);
  689. h2 = new Word64(0x9159015a, 0x3070dd17);
  690. h3 = new Word64(0x152fecd8, 0xf70e5939);
  691. h4 = new Word64(0x67332667, 0xffc00b31);
  692. h5 = new Word64(0x8eb44a87, 0x68581511);
  693. h6 = new Word64(0xdb0c2e0d, 0x64f98fa7);
  694. h7 = new Word64(0x47b5481d, 0xbefa4fa4);
  695. }
  696. var paddedLength = Math.ceil((length + 17) / 128) * 128;
  697. var padded = new Uint8Array(paddedLength);
  698. var i, j, n;
  699. for (i = 0; i < length; ++i) {
  700. padded[i] = data[offset++];
  701. }
  702. padded[i++] = 0x80;
  703. n = paddedLength - 16;
  704. while (i < n) {
  705. padded[i++] = 0;
  706. }
  707. padded[i++] = 0;
  708. padded[i++] = 0;
  709. padded[i++] = 0;
  710. padded[i++] = 0;
  711. padded[i++] = 0;
  712. padded[i++] = 0;
  713. padded[i++] = 0;
  714. padded[i++] = 0;
  715. padded[i++] = 0;
  716. padded[i++] = 0;
  717. padded[i++] = 0;
  718. padded[i++] = length >>> 29 & 0xFF;
  719. padded[i++] = length >> 21 & 0xFF;
  720. padded[i++] = length >> 13 & 0xFF;
  721. padded[i++] = length >> 5 & 0xFF;
  722. padded[i++] = length << 3 & 0xFF;
  723. var w = new Array(80);
  724. for (i = 0; i < 80; i++) {
  725. w[i] = new Word64(0, 0);
  726. }
  727. var a = new Word64(0, 0), b = new Word64(0, 0), c = new Word64(0, 0);
  728. var d = new Word64(0, 0), e = new Word64(0, 0), f = new Word64(0, 0);
  729. var g = new Word64(0, 0), h = new Word64(0, 0);
  730. var t1 = new Word64(0, 0), t2 = new Word64(0, 0);
  731. var tmp1 = new Word64(0, 0), tmp2 = new Word64(0, 0), tmp3;
  732. for (i = 0; i < paddedLength;) {
  733. for (j = 0; j < 16; ++j) {
  734. w[j].high = padded[i] << 24 | padded[i + 1] << 16 | padded[i + 2] << 8 | padded[i + 3];
  735. w[j].low = padded[i + 4] << 24 | padded[i + 5] << 16 | padded[i + 6] << 8 | padded[i + 7];
  736. i += 8;
  737. }
  738. for (j = 16; j < 80; ++j) {
  739. tmp3 = w[j];
  740. littleSigmaPrime(tmp3, w[j - 2], tmp2);
  741. tmp3.add(w[j - 7]);
  742. littleSigma(tmp1, w[j - 15], tmp2);
  743. tmp3.add(tmp1);
  744. tmp3.add(w[j - 16]);
  745. }
  746. a.assign(h0);
  747. b.assign(h1);
  748. c.assign(h2);
  749. d.assign(h3);
  750. e.assign(h4);
  751. f.assign(h5);
  752. g.assign(h6);
  753. h.assign(h7);
  754. for (j = 0; j < 80; ++j) {
  755. t1.assign(h);
  756. sigmaPrime(tmp1, e, tmp2);
  757. t1.add(tmp1);
  758. ch(tmp1, e, f, g, tmp2);
  759. t1.add(tmp1);
  760. t1.add(k[j]);
  761. t1.add(w[j]);
  762. sigma(t2, a, tmp2);
  763. maj(tmp1, a, b, c, tmp2);
  764. t2.add(tmp1);
  765. tmp3 = h;
  766. h = g;
  767. g = f;
  768. f = e;
  769. d.add(t1);
  770. e = d;
  771. d = c;
  772. c = b;
  773. b = a;
  774. tmp3.assign(t1);
  775. tmp3.add(t2);
  776. a = tmp3;
  777. }
  778. h0.add(a);
  779. h1.add(b);
  780. h2.add(c);
  781. h3.add(d);
  782. h4.add(e);
  783. h5.add(f);
  784. h6.add(g);
  785. h7.add(h);
  786. }
  787. var result;
  788. if (!mode384) {
  789. result = new Uint8Array(64);
  790. h0.copyTo(result, 0);
  791. h1.copyTo(result, 8);
  792. h2.copyTo(result, 16);
  793. h3.copyTo(result, 24);
  794. h4.copyTo(result, 32);
  795. h5.copyTo(result, 40);
  796. h6.copyTo(result, 48);
  797. h7.copyTo(result, 56);
  798. } else {
  799. result = new Uint8Array(48);
  800. h0.copyTo(result, 0);
  801. h1.copyTo(result, 8);
  802. h2.copyTo(result, 16);
  803. h3.copyTo(result, 24);
  804. h4.copyTo(result, 32);
  805. h5.copyTo(result, 40);
  806. }
  807. return result;
  808. }
  809. return hash;
  810. }();
  811. var calculateSHA384 = function calculateSHA384Closure() {
  812. function hash(data, offset, length) {
  813. return calculateSHA512(data, offset, length, true);
  814. }
  815. return hash;
  816. }();
  817. var NullCipher = function NullCipherClosure() {
  818. function NullCipher() {
  819. }
  820. NullCipher.prototype = {
  821. decryptBlock: function NullCipher_decryptBlock(data) {
  822. return data;
  823. }
  824. };
  825. return NullCipher;
  826. }();
  827. var AES128Cipher = function AES128CipherClosure() {
  828. var rcon = new Uint8Array([
  829. 0x8d,
  830. 0x01,
  831. 0x02,
  832. 0x04,
  833. 0x08,
  834. 0x10,
  835. 0x20,
  836. 0x40,
  837. 0x80,
  838. 0x1b,
  839. 0x36,
  840. 0x6c,
  841. 0xd8,
  842. 0xab,
  843. 0x4d,
  844. 0x9a,
  845. 0x2f,
  846. 0x5e,
  847. 0xbc,
  848. 0x63,
  849. 0xc6,
  850. 0x97,
  851. 0x35,
  852. 0x6a,
  853. 0xd4,
  854. 0xb3,
  855. 0x7d,
  856. 0xfa,
  857. 0xef,
  858. 0xc5,
  859. 0x91,
  860. 0x39,
  861. 0x72,
  862. 0xe4,
  863. 0xd3,
  864. 0xbd,
  865. 0x61,
  866. 0xc2,
  867. 0x9f,
  868. 0x25,
  869. 0x4a,
  870. 0x94,
  871. 0x33,
  872. 0x66,
  873. 0xcc,
  874. 0x83,
  875. 0x1d,
  876. 0x3a,
  877. 0x74,
  878. 0xe8,
  879. 0xcb,
  880. 0x8d,
  881. 0x01,
  882. 0x02,
  883. 0x04,
  884. 0x08,
  885. 0x10,
  886. 0x20,
  887. 0x40,
  888. 0x80,
  889. 0x1b,
  890. 0x36,
  891. 0x6c,
  892. 0xd8,
  893. 0xab,
  894. 0x4d,
  895. 0x9a,
  896. 0x2f,
  897. 0x5e,
  898. 0xbc,
  899. 0x63,
  900. 0xc6,
  901. 0x97,
  902. 0x35,
  903. 0x6a,
  904. 0xd4,
  905. 0xb3,
  906. 0x7d,
  907. 0xfa,
  908. 0xef,
  909. 0xc5,
  910. 0x91,
  911. 0x39,
  912. 0x72,
  913. 0xe4,
  914. 0xd3,
  915. 0xbd,
  916. 0x61,
  917. 0xc2,
  918. 0x9f,
  919. 0x25,
  920. 0x4a,
  921. 0x94,
  922. 0x33,
  923. 0x66,
  924. 0xcc,
  925. 0x83,
  926. 0x1d,
  927. 0x3a,
  928. 0x74,
  929. 0xe8,
  930. 0xcb,
  931. 0x8d,
  932. 0x01,
  933. 0x02,
  934. 0x04,
  935. 0x08,
  936. 0x10,
  937. 0x20,
  938. 0x40,
  939. 0x80,
  940. 0x1b,
  941. 0x36,
  942. 0x6c,
  943. 0xd8,
  944. 0xab,
  945. 0x4d,
  946. 0x9a,
  947. 0x2f,
  948. 0x5e,
  949. 0xbc,
  950. 0x63,
  951. 0xc6,
  952. 0x97,
  953. 0x35,
  954. 0x6a,
  955. 0xd4,
  956. 0xb3,
  957. 0x7d,
  958. 0xfa,
  959. 0xef,
  960. 0xc5,
  961. 0x91,
  962. 0x39,
  963. 0x72,
  964. 0xe4,
  965. 0xd3,
  966. 0xbd,
  967. 0x61,
  968. 0xc2,
  969. 0x9f,
  970. 0x25,
  971. 0x4a,
  972. 0x94,
  973. 0x33,
  974. 0x66,
  975. 0xcc,
  976. 0x83,
  977. 0x1d,
  978. 0x3a,
  979. 0x74,
  980. 0xe8,
  981. 0xcb,
  982. 0x8d,
  983. 0x01,
  984. 0x02,
  985. 0x04,
  986. 0x08,
  987. 0x10,
  988. 0x20,
  989. 0x40,
  990. 0x80,
  991. 0x1b,
  992. 0x36,
  993. 0x6c,
  994. 0xd8,
  995. 0xab,
  996. 0x4d,
  997. 0x9a,
  998. 0x2f,
  999. 0x5e,
  1000. 0xbc,
  1001. 0x63,
  1002. 0xc6,
  1003. 0x97,
  1004. 0x35,
  1005. 0x6a,
  1006. 0xd4,
  1007. 0xb3,
  1008. 0x7d,
  1009. 0xfa,
  1010. 0xef,
  1011. 0xc5,
  1012. 0x91,
  1013. 0x39,
  1014. 0x72,
  1015. 0xe4,
  1016. 0xd3,
  1017. 0xbd,
  1018. 0x61,
  1019. 0xc2,
  1020. 0x9f,
  1021. 0x25,
  1022. 0x4a,
  1023. 0x94,
  1024. 0x33,
  1025. 0x66,
  1026. 0xcc,
  1027. 0x83,
  1028. 0x1d,
  1029. 0x3a,
  1030. 0x74,
  1031. 0xe8,
  1032. 0xcb,
  1033. 0x8d,
  1034. 0x01,
  1035. 0x02,
  1036. 0x04,
  1037. 0x08,
  1038. 0x10,
  1039. 0x20,
  1040. 0x40,
  1041. 0x80,
  1042. 0x1b,
  1043. 0x36,
  1044. 0x6c,
  1045. 0xd8,
  1046. 0xab,
  1047. 0x4d,
  1048. 0x9a,
  1049. 0x2f,
  1050. 0x5e,
  1051. 0xbc,
  1052. 0x63,
  1053. 0xc6,
  1054. 0x97,
  1055. 0x35,
  1056. 0x6a,
  1057. 0xd4,
  1058. 0xb3,
  1059. 0x7d,
  1060. 0xfa,
  1061. 0xef,
  1062. 0xc5,
  1063. 0x91,
  1064. 0x39,
  1065. 0x72,
  1066. 0xe4,
  1067. 0xd3,
  1068. 0xbd,
  1069. 0x61,
  1070. 0xc2,
  1071. 0x9f,
  1072. 0x25,
  1073. 0x4a,
  1074. 0x94,
  1075. 0x33,
  1076. 0x66,
  1077. 0xcc,
  1078. 0x83,
  1079. 0x1d,
  1080. 0x3a,
  1081. 0x74,
  1082. 0xe8,
  1083. 0xcb,
  1084. 0x8d
  1085. ]);
  1086. var s = new Uint8Array([
  1087. 0x63,
  1088. 0x7c,
  1089. 0x77,
  1090. 0x7b,
  1091. 0xf2,
  1092. 0x6b,
  1093. 0x6f,
  1094. 0xc5,
  1095. 0x30,
  1096. 0x01,
  1097. 0x67,
  1098. 0x2b,
  1099. 0xfe,
  1100. 0xd7,
  1101. 0xab,
  1102. 0x76,
  1103. 0xca,
  1104. 0x82,
  1105. 0xc9,
  1106. 0x7d,
  1107. 0xfa,
  1108. 0x59,
  1109. 0x47,
  1110. 0xf0,
  1111. 0xad,
  1112. 0xd4,
  1113. 0xa2,
  1114. 0xaf,
  1115. 0x9c,
  1116. 0xa4,
  1117. 0x72,
  1118. 0xc0,
  1119. 0xb7,
  1120. 0xfd,
  1121. 0x93,
  1122. 0x26,
  1123. 0x36,
  1124. 0x3f,
  1125. 0xf7,
  1126. 0xcc,
  1127. 0x34,
  1128. 0xa5,
  1129. 0xe5,
  1130. 0xf1,
  1131. 0x71,
  1132. 0xd8,
  1133. 0x31,
  1134. 0x15,
  1135. 0x04,
  1136. 0xc7,
  1137. 0x23,
  1138. 0xc3,
  1139. 0x18,
  1140. 0x96,
  1141. 0x05,
  1142. 0x9a,
  1143. 0x07,
  1144. 0x12,
  1145. 0x80,
  1146. 0xe2,
  1147. 0xeb,
  1148. 0x27,
  1149. 0xb2,
  1150. 0x75,
  1151. 0x09,
  1152. 0x83,
  1153. 0x2c,
  1154. 0x1a,
  1155. 0x1b,
  1156. 0x6e,
  1157. 0x5a,
  1158. 0xa0,
  1159. 0x52,
  1160. 0x3b,
  1161. 0xd6,
  1162. 0xb3,
  1163. 0x29,
  1164. 0xe3,
  1165. 0x2f,
  1166. 0x84,
  1167. 0x53,
  1168. 0xd1,
  1169. 0x00,
  1170. 0xed,
  1171. 0x20,
  1172. 0xfc,
  1173. 0xb1,
  1174. 0x5b,
  1175. 0x6a,
  1176. 0xcb,
  1177. 0xbe,
  1178. 0x39,
  1179. 0x4a,
  1180. 0x4c,
  1181. 0x58,
  1182. 0xcf,
  1183. 0xd0,
  1184. 0xef,
  1185. 0xaa,
  1186. 0xfb,
  1187. 0x43,
  1188. 0x4d,
  1189. 0x33,
  1190. 0x85,
  1191. 0x45,
  1192. 0xf9,
  1193. 0x02,
  1194. 0x7f,
  1195. 0x50,
  1196. 0x3c,
  1197. 0x9f,
  1198. 0xa8,
  1199. 0x51,
  1200. 0xa3,
  1201. 0x40,
  1202. 0x8f,
  1203. 0x92,
  1204. 0x9d,
  1205. 0x38,
  1206. 0xf5,
  1207. 0xbc,
  1208. 0xb6,
  1209. 0xda,
  1210. 0x21,
  1211. 0x10,
  1212. 0xff,
  1213. 0xf3,
  1214. 0xd2,
  1215. 0xcd,
  1216. 0x0c,
  1217. 0x13,
  1218. 0xec,
  1219. 0x5f,
  1220. 0x97,
  1221. 0x44,
  1222. 0x17,
  1223. 0xc4,
  1224. 0xa7,
  1225. 0x7e,
  1226. 0x3d,
  1227. 0x64,
  1228. 0x5d,
  1229. 0x19,
  1230. 0x73,
  1231. 0x60,
  1232. 0x81,
  1233. 0x4f,
  1234. 0xdc,
  1235. 0x22,
  1236. 0x2a,
  1237. 0x90,
  1238. 0x88,
  1239. 0x46,
  1240. 0xee,
  1241. 0xb8,
  1242. 0x14,
  1243. 0xde,
  1244. 0x5e,
  1245. 0x0b,
  1246. 0xdb,
  1247. 0xe0,
  1248. 0x32,
  1249. 0x3a,
  1250. 0x0a,
  1251. 0x49,
  1252. 0x06,
  1253. 0x24,
  1254. 0x5c,
  1255. 0xc2,
  1256. 0xd3,
  1257. 0xac,
  1258. 0x62,
  1259. 0x91,
  1260. 0x95,
  1261. 0xe4,
  1262. 0x79,
  1263. 0xe7,
  1264. 0xc8,
  1265. 0x37,
  1266. 0x6d,
  1267. 0x8d,
  1268. 0xd5,
  1269. 0x4e,
  1270. 0xa9,
  1271. 0x6c,
  1272. 0x56,
  1273. 0xf4,
  1274. 0xea,
  1275. 0x65,
  1276. 0x7a,
  1277. 0xae,
  1278. 0x08,
  1279. 0xba,
  1280. 0x78,
  1281. 0x25,
  1282. 0x2e,
  1283. 0x1c,
  1284. 0xa6,
  1285. 0xb4,
  1286. 0xc6,
  1287. 0xe8,
  1288. 0xdd,
  1289. 0x74,
  1290. 0x1f,
  1291. 0x4b,
  1292. 0xbd,
  1293. 0x8b,
  1294. 0x8a,
  1295. 0x70,
  1296. 0x3e,
  1297. 0xb5,
  1298. 0x66,
  1299. 0x48,
  1300. 0x03,
  1301. 0xf6,
  1302. 0x0e,
  1303. 0x61,
  1304. 0x35,
  1305. 0x57,
  1306. 0xb9,
  1307. 0x86,
  1308. 0xc1,
  1309. 0x1d,
  1310. 0x9e,
  1311. 0xe1,
  1312. 0xf8,
  1313. 0x98,
  1314. 0x11,
  1315. 0x69,
  1316. 0xd9,
  1317. 0x8e,
  1318. 0x94,
  1319. 0x9b,
  1320. 0x1e,
  1321. 0x87,
  1322. 0xe9,
  1323. 0xce,
  1324. 0x55,
  1325. 0x28,
  1326. 0xdf,
  1327. 0x8c,
  1328. 0xa1,
  1329. 0x89,
  1330. 0x0d,
  1331. 0xbf,
  1332. 0xe6,
  1333. 0x42,
  1334. 0x68,
  1335. 0x41,
  1336. 0x99,
  1337. 0x2d,
  1338. 0x0f,
  1339. 0xb0,
  1340. 0x54,
  1341. 0xbb,
  1342. 0x16
  1343. ]);
  1344. var inv_s = new Uint8Array([
  1345. 0x52,
  1346. 0x09,
  1347. 0x6a,
  1348. 0xd5,
  1349. 0x30,
  1350. 0x36,
  1351. 0xa5,
  1352. 0x38,
  1353. 0xbf,
  1354. 0x40,
  1355. 0xa3,
  1356. 0x9e,
  1357. 0x81,
  1358. 0xf3,
  1359. 0xd7,
  1360. 0xfb,
  1361. 0x7c,
  1362. 0xe3,
  1363. 0x39,
  1364. 0x82,
  1365. 0x9b,
  1366. 0x2f,
  1367. 0xff,
  1368. 0x87,
  1369. 0x34,
  1370. 0x8e,
  1371. 0x43,
  1372. 0x44,
  1373. 0xc4,
  1374. 0xde,
  1375. 0xe9,
  1376. 0xcb,
  1377. 0x54,
  1378. 0x7b,
  1379. 0x94,
  1380. 0x32,
  1381. 0xa6,
  1382. 0xc2,
  1383. 0x23,
  1384. 0x3d,
  1385. 0xee,
  1386. 0x4c,
  1387. 0x95,
  1388. 0x0b,
  1389. 0x42,
  1390. 0xfa,
  1391. 0xc3,
  1392. 0x4e,
  1393. 0x08,
  1394. 0x2e,
  1395. 0xa1,
  1396. 0x66,
  1397. 0x28,
  1398. 0xd9,
  1399. 0x24,
  1400. 0xb2,
  1401. 0x76,
  1402. 0x5b,
  1403. 0xa2,
  1404. 0x49,
  1405. 0x6d,
  1406. 0x8b,
  1407. 0xd1,
  1408. 0x25,
  1409. 0x72,
  1410. 0xf8,
  1411. 0xf6,
  1412. 0x64,
  1413. 0x86,
  1414. 0x68,
  1415. 0x98,
  1416. 0x16,
  1417. 0xd4,
  1418. 0xa4,
  1419. 0x5c,
  1420. 0xcc,
  1421. 0x5d,
  1422. 0x65,
  1423. 0xb6,
  1424. 0x92,
  1425. 0x6c,
  1426. 0x70,
  1427. 0x48,
  1428. 0x50,
  1429. 0xfd,
  1430. 0xed,
  1431. 0xb9,
  1432. 0xda,
  1433. 0x5e,
  1434. 0x15,
  1435. 0x46,
  1436. 0x57,
  1437. 0xa7,
  1438. 0x8d,
  1439. 0x9d,
  1440. 0x84,
  1441. 0x90,
  1442. 0xd8,
  1443. 0xab,
  1444. 0x00,
  1445. 0x8c,
  1446. 0xbc,
  1447. 0xd3,
  1448. 0x0a,
  1449. 0xf7,
  1450. 0xe4,
  1451. 0x58,
  1452. 0x05,
  1453. 0xb8,
  1454. 0xb3,
  1455. 0x45,
  1456. 0x06,
  1457. 0xd0,
  1458. 0x2c,
  1459. 0x1e,
  1460. 0x8f,
  1461. 0xca,
  1462. 0x3f,
  1463. 0x0f,
  1464. 0x02,
  1465. 0xc1,
  1466. 0xaf,
  1467. 0xbd,
  1468. 0x03,
  1469. 0x01,
  1470. 0x13,
  1471. 0x8a,
  1472. 0x6b,
  1473. 0x3a,
  1474. 0x91,
  1475. 0x11,
  1476. 0x41,
  1477. 0x4f,
  1478. 0x67,
  1479. 0xdc,
  1480. 0xea,
  1481. 0x97,
  1482. 0xf2,
  1483. 0xcf,
  1484. 0xce,
  1485. 0xf0,
  1486. 0xb4,
  1487. 0xe6,
  1488. 0x73,
  1489. 0x96,
  1490. 0xac,
  1491. 0x74,
  1492. 0x22,
  1493. 0xe7,
  1494. 0xad,
  1495. 0x35,
  1496. 0x85,
  1497. 0xe2,
  1498. 0xf9,
  1499. 0x37,
  1500. 0xe8,
  1501. 0x1c,
  1502. 0x75,
  1503. 0xdf,
  1504. 0x6e,
  1505. 0x47,
  1506. 0xf1,
  1507. 0x1a,
  1508. 0x71,
  1509. 0x1d,
  1510. 0x29,
  1511. 0xc5,
  1512. 0x89,
  1513. 0x6f,
  1514. 0xb7,
  1515. 0x62,
  1516. 0x0e,
  1517. 0xaa,
  1518. 0x18,
  1519. 0xbe,
  1520. 0x1b,
  1521. 0xfc,
  1522. 0x56,
  1523. 0x3e,
  1524. 0x4b,
  1525. 0xc6,
  1526. 0xd2,
  1527. 0x79,
  1528. 0x20,
  1529. 0x9a,
  1530. 0xdb,
  1531. 0xc0,
  1532. 0xfe,
  1533. 0x78,
  1534. 0xcd,
  1535. 0x5a,
  1536. 0xf4,
  1537. 0x1f,
  1538. 0xdd,
  1539. 0xa8,
  1540. 0x33,
  1541. 0x88,
  1542. 0x07,
  1543. 0xc7,
  1544. 0x31,
  1545. 0xb1,
  1546. 0x12,
  1547. 0x10,
  1548. 0x59,
  1549. 0x27,
  1550. 0x80,
  1551. 0xec,
  1552. 0x5f,
  1553. 0x60,
  1554. 0x51,
  1555. 0x7f,
  1556. 0xa9,
  1557. 0x19,
  1558. 0xb5,
  1559. 0x4a,
  1560. 0x0d,
  1561. 0x2d,
  1562. 0xe5,
  1563. 0x7a,
  1564. 0x9f,
  1565. 0x93,
  1566. 0xc9,
  1567. 0x9c,
  1568. 0xef,
  1569. 0xa0,
  1570. 0xe0,
  1571. 0x3b,
  1572. 0x4d,
  1573. 0xae,
  1574. 0x2a,
  1575. 0xf5,
  1576. 0xb0,
  1577. 0xc8,
  1578. 0xeb,
  1579. 0xbb,
  1580. 0x3c,
  1581. 0x83,
  1582. 0x53,
  1583. 0x99,
  1584. 0x61,
  1585. 0x17,
  1586. 0x2b,
  1587. 0x04,
  1588. 0x7e,
  1589. 0xba,
  1590. 0x77,
  1591. 0xd6,
  1592. 0x26,
  1593. 0xe1,
  1594. 0x69,
  1595. 0x14,
  1596. 0x63,
  1597. 0x55,
  1598. 0x21,
  1599. 0x0c,
  1600. 0x7d
  1601. ]);
  1602. var mixCol = new Uint8Array(256);
  1603. for (var i = 0; i < 256; i++) {
  1604. if (i < 128) {
  1605. mixCol[i] = i << 1;
  1606. } else {
  1607. mixCol[i] = i << 1 ^ 0x1b;
  1608. }
  1609. }
  1610. var mix = new Uint32Array([
  1611. 0x00000000,
  1612. 0x0e090d0b,
  1613. 0x1c121a16,
  1614. 0x121b171d,
  1615. 0x3824342c,
  1616. 0x362d3927,
  1617. 0x24362e3a,
  1618. 0x2a3f2331,
  1619. 0x70486858,
  1620. 0x7e416553,
  1621. 0x6c5a724e,
  1622. 0x62537f45,
  1623. 0x486c5c74,
  1624. 0x4665517f,
  1625. 0x547e4662,
  1626. 0x5a774b69,
  1627. 0xe090d0b0,
  1628. 0xee99ddbb,
  1629. 0xfc82caa6,
  1630. 0xf28bc7ad,
  1631. 0xd8b4e49c,
  1632. 0xd6bde997,
  1633. 0xc4a6fe8a,
  1634. 0xcaaff381,
  1635. 0x90d8b8e8,
  1636. 0x9ed1b5e3,
  1637. 0x8ccaa2fe,
  1638. 0x82c3aff5,
  1639. 0xa8fc8cc4,
  1640. 0xa6f581cf,
  1641. 0xb4ee96d2,
  1642. 0xbae79bd9,
  1643. 0xdb3bbb7b,
  1644. 0xd532b670,
  1645. 0xc729a16d,
  1646. 0xc920ac66,
  1647. 0xe31f8f57,
  1648. 0xed16825c,
  1649. 0xff0d9541,
  1650. 0xf104984a,
  1651. 0xab73d323,
  1652. 0xa57ade28,
  1653. 0xb761c935,
  1654. 0xb968c43e,
  1655. 0x9357e70f,
  1656. 0x9d5eea04,
  1657. 0x8f45fd19,
  1658. 0x814cf012,
  1659. 0x3bab6bcb,
  1660. 0x35a266c0,
  1661. 0x27b971dd,
  1662. 0x29b07cd6,
  1663. 0x038f5fe7,
  1664. 0x0d8652ec,
  1665. 0x1f9d45f1,
  1666. 0x119448fa,
  1667. 0x4be30393,
  1668. 0x45ea0e98,
  1669. 0x57f11985,
  1670. 0x59f8148e,
  1671. 0x73c737bf,
  1672. 0x7dce3ab4,
  1673. 0x6fd52da9,
  1674. 0x61dc20a2,
  1675. 0xad766df6,
  1676. 0xa37f60fd,
  1677. 0xb16477e0,
  1678. 0xbf6d7aeb,
  1679. 0x955259da,
  1680. 0x9b5b54d1,
  1681. 0x894043cc,
  1682. 0x87494ec7,
  1683. 0xdd3e05ae,
  1684. 0xd33708a5,
  1685. 0xc12c1fb8,
  1686. 0xcf2512b3,
  1687. 0xe51a3182,
  1688. 0xeb133c89,
  1689. 0xf9082b94,
  1690. 0xf701269f,
  1691. 0x4de6bd46,
  1692. 0x43efb04d,
  1693. 0x51f4a750,
  1694. 0x5ffdaa5b,
  1695. 0x75c2896a,
  1696. 0x7bcb8461,
  1697. 0x69d0937c,
  1698. 0x67d99e77,
  1699. 0x3daed51e,
  1700. 0x33a7d815,
  1701. 0x21bccf08,
  1702. 0x2fb5c203,
  1703. 0x058ae132,
  1704. 0x0b83ec39,
  1705. 0x1998fb24,
  1706. 0x1791f62f,
  1707. 0x764dd68d,
  1708. 0x7844db86,
  1709. 0x6a5fcc9b,
  1710. 0x6456c190,
  1711. 0x4e69e2a1,
  1712. 0x4060efaa,
  1713. 0x527bf8b7,
  1714. 0x5c72f5bc,
  1715. 0x0605bed5,
  1716. 0x080cb3de,
  1717. 0x1a17a4c3,
  1718. 0x141ea9c8,
  1719. 0x3e218af9,
  1720. 0x302887f2,
  1721. 0x223390ef,
  1722. 0x2c3a9de4,
  1723. 0x96dd063d,
  1724. 0x98d40b36,
  1725. 0x8acf1c2b,
  1726. 0x84c61120,
  1727. 0xaef93211,
  1728. 0xa0f03f1a,
  1729. 0xb2eb2807,
  1730. 0xbce2250c,
  1731. 0xe6956e65,
  1732. 0xe89c636e,
  1733. 0xfa877473,
  1734. 0xf48e7978,
  1735. 0xdeb15a49,
  1736. 0xd0b85742,
  1737. 0xc2a3405f,
  1738. 0xccaa4d54,
  1739. 0x41ecdaf7,
  1740. 0x4fe5d7fc,
  1741. 0x5dfec0e1,
  1742. 0x53f7cdea,
  1743. 0x79c8eedb,
  1744. 0x77c1e3d0,
  1745. 0x65daf4cd,
  1746. 0x6bd3f9c6,
  1747. 0x31a4b2af,
  1748. 0x3fadbfa4,
  1749. 0x2db6a8b9,
  1750. 0x23bfa5b2,
  1751. 0x09808683,
  1752. 0x07898b88,
  1753. 0x15929c95,
  1754. 0x1b9b919e,
  1755. 0xa17c0a47,
  1756. 0xaf75074c,
  1757. 0xbd6e1051,
  1758. 0xb3671d5a,
  1759. 0x99583e6b,
  1760. 0x97513360,
  1761. 0x854a247d,
  1762. 0x8b432976,
  1763. 0xd134621f,
  1764. 0xdf3d6f14,
  1765. 0xcd267809,
  1766. 0xc32f7502,
  1767. 0xe9105633,
  1768. 0xe7195b38,
  1769. 0xf5024c25,
  1770. 0xfb0b412e,
  1771. 0x9ad7618c,
  1772. 0x94de6c87,
  1773. 0x86c57b9a,
  1774. 0x88cc7691,
  1775. 0xa2f355a0,
  1776. 0xacfa58ab,
  1777. 0xbee14fb6,
  1778. 0xb0e842bd,
  1779. 0xea9f09d4,
  1780. 0xe49604df,
  1781. 0xf68d13c2,
  1782. 0xf8841ec9,
  1783. 0xd2bb3df8,
  1784. 0xdcb230f3,
  1785. 0xcea927ee,
  1786. 0xc0a02ae5,
  1787. 0x7a47b13c,
  1788. 0x744ebc37,
  1789. 0x6655ab2a,
  1790. 0x685ca621,
  1791. 0x42638510,
  1792. 0x4c6a881b,
  1793. 0x5e719f06,
  1794. 0x5078920d,
  1795. 0x0a0fd964,
  1796. 0x0406d46f,
  1797. 0x161dc372,
  1798. 0x1814ce79,
  1799. 0x322bed48,
  1800. 0x3c22e043,
  1801. 0x2e39f75e,
  1802. 0x2030fa55,
  1803. 0xec9ab701,
  1804. 0xe293ba0a,
  1805. 0xf088ad17,
  1806. 0xfe81a01c,
  1807. 0xd4be832d,
  1808. 0xdab78e26,
  1809. 0xc8ac993b,
  1810. 0xc6a59430,
  1811. 0x9cd2df59,
  1812. 0x92dbd252,
  1813. 0x80c0c54f,
  1814. 0x8ec9c844,
  1815. 0xa4f6eb75,
  1816. 0xaaffe67e,
  1817. 0xb8e4f163,
  1818. 0xb6edfc68,
  1819. 0x0c0a67b1,
  1820. 0x02036aba,
  1821. 0x10187da7,
  1822. 0x1e1170ac,
  1823. 0x342e539d,
  1824. 0x3a275e96,
  1825. 0x283c498b,
  1826. 0x26354480,
  1827. 0x7c420fe9,
  1828. 0x724b02e2,
  1829. 0x605015ff,
  1830. 0x6e5918f4,
  1831. 0x44663bc5,
  1832. 0x4a6f36ce,
  1833. 0x587421d3,
  1834. 0x567d2cd8,
  1835. 0x37a10c7a,
  1836. 0x39a80171,
  1837. 0x2bb3166c,
  1838. 0x25ba1b67,
  1839. 0x0f853856,
  1840. 0x018c355d,
  1841. 0x13972240,
  1842. 0x1d9e2f4b,
  1843. 0x47e96422,
  1844. 0x49e06929,
  1845. 0x5bfb7e34,
  1846. 0x55f2733f,
  1847. 0x7fcd500e,
  1848. 0x71c45d05,
  1849. 0x63df4a18,
  1850. 0x6dd64713,
  1851. 0xd731dcca,
  1852. 0xd938d1c1,
  1853. 0xcb23c6dc,
  1854. 0xc52acbd7,
  1855. 0xef15e8e6,
  1856. 0xe11ce5ed,
  1857. 0xf307f2f0,
  1858. 0xfd0efffb,
  1859. 0xa779b492,
  1860. 0xa970b999,
  1861. 0xbb6bae84,
  1862. 0xb562a38f,
  1863. 0x9f5d80be,
  1864. 0x91548db5,
  1865. 0x834f9aa8,
  1866. 0x8d4697a3
  1867. ]);
  1868. function expandKey128(cipherKey) {
  1869. var b = 176, result = new Uint8Array(b);
  1870. result.set(cipherKey);
  1871. for (var j = 16, i = 1; j < b; ++i) {
  1872. var t1 = result[j - 3], t2 = result[j - 2], t3 = result[j - 1], t4 = result[j - 4];
  1873. t1 = s[t1];
  1874. t2 = s[t2];
  1875. t3 = s[t3];
  1876. t4 = s[t4];
  1877. t1 = t1 ^ rcon[i];
  1878. for (var n = 0; n < 4; ++n) {
  1879. result[j] = t1 ^= result[j - 16];
  1880. j++;
  1881. result[j] = t2 ^= result[j - 16];
  1882. j++;
  1883. result[j] = t3 ^= result[j - 16];
  1884. j++;
  1885. result[j] = t4 ^= result[j - 16];
  1886. j++;
  1887. }
  1888. }
  1889. return result;
  1890. }
  1891. function decrypt128(input, key) {
  1892. var state = new Uint8Array(16);
  1893. state.set(input);
  1894. var i, j, k;
  1895. var t, u, v;
  1896. for (j = 0, k = 160; j < 16; ++j, ++k) {
  1897. state[j] ^= key[k];
  1898. }
  1899. for (i = 9; i >= 1; --i) {
  1900. t = state[13];
  1901. state[13] = state[9];
  1902. state[9] = state[5];
  1903. state[5] = state[1];
  1904. state[1] = t;
  1905. t = state[14];
  1906. u = state[10];
  1907. state[14] = state[6];
  1908. state[10] = state[2];
  1909. state[6] = t;
  1910. state[2] = u;
  1911. t = state[15];
  1912. u = state[11];
  1913. v = state[7];
  1914. state[15] = state[3];
  1915. state[11] = t;
  1916. state[7] = u;
  1917. state[3] = v;
  1918. for (j = 0; j < 16; ++j) {
  1919. state[j] = inv_s[state[j]];
  1920. }
  1921. for (j = 0, k = i * 16; j < 16; ++j, ++k) {
  1922. state[j] ^= key[k];
  1923. }
  1924. for (j = 0; j < 16; j += 4) {
  1925. var s0 = mix[state[j]], s1 = mix[state[j + 1]], s2 = mix[state[j + 2]], s3 = mix[state[j + 3]];
  1926. t = s0 ^ s1 >>> 8 ^ s1 << 24 ^ s2 >>> 16 ^ s2 << 16 ^ s3 >>> 24 ^ s3 << 8;
  1927. state[j] = t >>> 24 & 0xFF;
  1928. state[j + 1] = t >> 16 & 0xFF;
  1929. state[j + 2] = t >> 8 & 0xFF;
  1930. state[j + 3] = t & 0xFF;
  1931. }
  1932. }
  1933. t = state[13];
  1934. state[13] = state[9];
  1935. state[9] = state[5];
  1936. state[5] = state[1];
  1937. state[1] = t;
  1938. t = state[14];
  1939. u = state[10];
  1940. state[14] = state[6];
  1941. state[10] = state[2];
  1942. state[6] = t;
  1943. state[2] = u;
  1944. t = state[15];
  1945. u = state[11];
  1946. v = state[7];
  1947. state[15] = state[3];
  1948. state[11] = t;
  1949. state[7] = u;
  1950. state[3] = v;
  1951. for (j = 0; j < 16; ++j) {
  1952. state[j] = inv_s[state[j]];
  1953. state[j] ^= key[j];
  1954. }
  1955. return state;
  1956. }
  1957. function encrypt128(input, key) {
  1958. var t, u, v, k;
  1959. var state = new Uint8Array(16);
  1960. state.set(input);
  1961. for (j = 0; j < 16; ++j) {
  1962. state[j] ^= key[j];
  1963. }
  1964. for (i = 1; i < 10; i++) {
  1965. for (j = 0; j < 16; ++j) {
  1966. state[j] = s[state[j]];
  1967. }
  1968. v = state[1];
  1969. state[1] = state[5];
  1970. state[5] = state[9];
  1971. state[9] = state[13];
  1972. state[13] = v;
  1973. v = state[2];
  1974. u = state[6];
  1975. state[2] = state[10];
  1976. state[6] = state[14];
  1977. state[10] = v;
  1978. state[14] = u;
  1979. v = state[3];
  1980. u = state[7];
  1981. t = state[11];
  1982. state[3] = state[15];
  1983. state[7] = v;
  1984. state[11] = u;
  1985. state[15] = t;
  1986. for (var j = 0; j < 16; j += 4) {
  1987. var s0 = state[j + 0], s1 = state[j + 1];
  1988. var s2 = state[j + 2], s3 = state[j + 3];
  1989. t = s0 ^ s1 ^ s2 ^ s3;
  1990. state[j + 0] ^= t ^ mixCol[s0 ^ s1];
  1991. state[j + 1] ^= t ^ mixCol[s1 ^ s2];
  1992. state[j + 2] ^= t ^ mixCol[s2 ^ s3];
  1993. state[j + 3] ^= t ^ mixCol[s3 ^ s0];
  1994. }
  1995. for (j = 0, k = i * 16; j < 16; ++j, ++k) {
  1996. state[j] ^= key[k];
  1997. }
  1998. }
  1999. for (j = 0; j < 16; ++j) {
  2000. state[j] = s[state[j]];
  2001. }
  2002. v = state[1];
  2003. state[1] = state[5];
  2004. state[5] = state[9];
  2005. state[9] = state[13];
  2006. state[13] = v;
  2007. v = state[2];
  2008. u = state[6];
  2009. state[2] = state[10];
  2010. state[6] = state[14];
  2011. state[10] = v;
  2012. state[14] = u;
  2013. v = state[3];
  2014. u = state[7];
  2015. t = state[11];
  2016. state[3] = state[15];
  2017. state[7] = v;
  2018. state[11] = u;
  2019. state[15] = t;
  2020. for (j = 0, k = 160; j < 16; ++j, ++k) {
  2021. state[j] ^= key[k];
  2022. }
  2023. return state;
  2024. }
  2025. function AES128Cipher(key) {
  2026. this.key = expandKey128(key);
  2027. this.buffer = new Uint8Array(16);
  2028. this.bufferPosition = 0;
  2029. }
  2030. function decryptBlock2(data, finalize) {
  2031. var i, j, ii, sourceLength = data.length, buffer = this.buffer, bufferLength = this.bufferPosition, result = [], iv = this.iv;
  2032. for (i = 0; i < sourceLength; ++i) {
  2033. buffer[bufferLength] = data[i];
  2034. ++bufferLength;
  2035. if (bufferLength < 16) {
  2036. continue;
  2037. }
  2038. var plain = decrypt128(buffer, this.key);
  2039. for (j = 0; j < 16; ++j) {
  2040. plain[j] ^= iv[j];
  2041. }
  2042. iv = buffer;
  2043. result.push(plain);
  2044. buffer = new Uint8Array(16);
  2045. bufferLength = 0;
  2046. }
  2047. this.buffer = buffer;
  2048. this.bufferLength = bufferLength;
  2049. this.iv = iv;
  2050. if (result.length === 0) {
  2051. return new Uint8Array([]);
  2052. }
  2053. var outputLength = 16 * result.length;
  2054. if (finalize) {
  2055. var lastBlock = result[result.length - 1];
  2056. var psLen = lastBlock[15];
  2057. if (psLen <= 16) {
  2058. for (i = 15, ii = 16 - psLen; i >= ii; --i) {
  2059. if (lastBlock[i] !== psLen) {
  2060. psLen = 0;
  2061. break;
  2062. }
  2063. }
  2064. outputLength -= psLen;
  2065. result[result.length - 1] = lastBlock.subarray(0, 16 - psLen);
  2066. }
  2067. }
  2068. var output = new Uint8Array(outputLength);
  2069. for (i = 0, j = 0, ii = result.length; i < ii; ++i, j += 16) {
  2070. output.set(result[i], j);
  2071. }
  2072. return output;
  2073. }
  2074. AES128Cipher.prototype = {
  2075. decryptBlock: function AES128Cipher_decryptBlock(data, finalize) {
  2076. var i, sourceLength = data.length;
  2077. var buffer = this.buffer, bufferLength = this.bufferPosition;
  2078. for (i = 0; bufferLength < 16 && i < sourceLength; ++i, ++bufferLength) {
  2079. buffer[bufferLength] = data[i];
  2080. }
  2081. if (bufferLength < 16) {
  2082. this.bufferLength = bufferLength;
  2083. return new Uint8Array([]);
  2084. }
  2085. this.iv = buffer;
  2086. this.buffer = new Uint8Array(16);
  2087. this.bufferLength = 0;
  2088. this.decryptBlock = decryptBlock2;
  2089. return this.decryptBlock(data.subarray(16), finalize);
  2090. },
  2091. encrypt: function AES128Cipher_encrypt(data, iv) {
  2092. var i, j, ii, sourceLength = data.length, buffer = this.buffer, bufferLength = this.bufferPosition, result = [];
  2093. if (!iv) {
  2094. iv = new Uint8Array(16);
  2095. }
  2096. for (i = 0; i < sourceLength; ++i) {
  2097. buffer[bufferLength] = data[i];
  2098. ++bufferLength;
  2099. if (bufferLength < 16) {
  2100. continue;
  2101. }
  2102. for (j = 0; j < 16; ++j) {
  2103. buffer[j] ^= iv[j];
  2104. }
  2105. var cipher = encrypt128(buffer, this.key);
  2106. iv = cipher;
  2107. result.push(cipher);
  2108. buffer = new Uint8Array(16);
  2109. bufferLength = 0;
  2110. }
  2111. this.buffer = buffer;
  2112. this.bufferLength = bufferLength;
  2113. this.iv = iv;
  2114. if (result.length === 0) {
  2115. return new Uint8Array([]);
  2116. }
  2117. var outputLength = 16 * result.length;
  2118. var output = new Uint8Array(outputLength);
  2119. for (i = 0, j = 0, ii = result.length; i < ii; ++i, j += 16) {
  2120. output.set(result[i], j);
  2121. }
  2122. return output;
  2123. }
  2124. };
  2125. return AES128Cipher;
  2126. }();
  2127. var AES256Cipher = function AES256CipherClosure() {
  2128. var s = new Uint8Array([
  2129. 0x63,
  2130. 0x7c,
  2131. 0x77,
  2132. 0x7b,
  2133. 0xf2,
  2134. 0x6b,
  2135. 0x6f,
  2136. 0xc5,
  2137. 0x30,
  2138. 0x01,
  2139. 0x67,
  2140. 0x2b,
  2141. 0xfe,
  2142. 0xd7,
  2143. 0xab,
  2144. 0x76,
  2145. 0xca,
  2146. 0x82,
  2147. 0xc9,
  2148. 0x7d,
  2149. 0xfa,
  2150. 0x59,
  2151. 0x47,
  2152. 0xf0,
  2153. 0xad,
  2154. 0xd4,
  2155. 0xa2,
  2156. 0xaf,
  2157. 0x9c,
  2158. 0xa4,
  2159. 0x72,
  2160. 0xc0,
  2161. 0xb7,
  2162. 0xfd,
  2163. 0x93,
  2164. 0x26,
  2165. 0x36,
  2166. 0x3f,
  2167. 0xf7,
  2168. 0xcc,
  2169. 0x34,
  2170. 0xa5,
  2171. 0xe5,
  2172. 0xf1,
  2173. 0x71,
  2174. 0xd8,
  2175. 0x31,
  2176. 0x15,
  2177. 0x04,
  2178. 0xc7,
  2179. 0x23,
  2180. 0xc3,
  2181. 0x18,
  2182. 0x96,
  2183. 0x05,
  2184. 0x9a,
  2185. 0x07,
  2186. 0x12,
  2187. 0x80,
  2188. 0xe2,
  2189. 0xeb,
  2190. 0x27,
  2191. 0xb2,
  2192. 0x75,
  2193. 0x09,
  2194. 0x83,
  2195. 0x2c,
  2196. 0x1a,
  2197. 0x1b,
  2198. 0x6e,
  2199. 0x5a,
  2200. 0xa0,
  2201. 0x52,
  2202. 0x3b,
  2203. 0xd6,
  2204. 0xb3,
  2205. 0x29,
  2206. 0xe3,
  2207. 0x2f,
  2208. 0x84,
  2209. 0x53,
  2210. 0xd1,
  2211. 0x00,
  2212. 0xed,
  2213. 0x20,
  2214. 0xfc,
  2215. 0xb1,
  2216. 0x5b,
  2217. 0x6a,
  2218. 0xcb,
  2219. 0xbe,
  2220. 0x39,
  2221. 0x4a,
  2222. 0x4c,
  2223. 0x58,
  2224. 0xcf,
  2225. 0xd0,
  2226. 0xef,
  2227. 0xaa,
  2228. 0xfb,
  2229. 0x43,
  2230. 0x4d,
  2231. 0x33,
  2232. 0x85,
  2233. 0x45,
  2234. 0xf9,
  2235. 0x02,
  2236. 0x7f,
  2237. 0x50,
  2238. 0x3c,
  2239. 0x9f,
  2240. 0xa8,
  2241. 0x51,
  2242. 0xa3,
  2243. 0x40,
  2244. 0x8f,
  2245. 0x92,
  2246. 0x9d,
  2247. 0x38,
  2248. 0xf5,
  2249. 0xbc,
  2250. 0xb6,
  2251. 0xda,
  2252. 0x21,
  2253. 0x10,
  2254. 0xff,
  2255. 0xf3,
  2256. 0xd2,
  2257. 0xcd,
  2258. 0x0c,
  2259. 0x13,
  2260. 0xec,
  2261. 0x5f,
  2262. 0x97,
  2263. 0x44,
  2264. 0x17,
  2265. 0xc4,
  2266. 0xa7,
  2267. 0x7e,
  2268. 0x3d,
  2269. 0x64,
  2270. 0x5d,
  2271. 0x19,
  2272. 0x73,
  2273. 0x60,
  2274. 0x81,
  2275. 0x4f,
  2276. 0xdc,
  2277. 0x22,
  2278. 0x2a,
  2279. 0x90,
  2280. 0x88,
  2281. 0x46,
  2282. 0xee,
  2283. 0xb8,
  2284. 0x14,
  2285. 0xde,
  2286. 0x5e,
  2287. 0x0b,
  2288. 0xdb,
  2289. 0xe0,
  2290. 0x32,
  2291. 0x3a,
  2292. 0x0a,
  2293. 0x49,
  2294. 0x06,
  2295. 0x24,
  2296. 0x5c,
  2297. 0xc2,
  2298. 0xd3,
  2299. 0xac,
  2300. 0x62,
  2301. 0x91,
  2302. 0x95,
  2303. 0xe4,
  2304. 0x79,
  2305. 0xe7,
  2306. 0xc8,
  2307. 0x37,
  2308. 0x6d,
  2309. 0x8d,
  2310. 0xd5,
  2311. 0x4e,
  2312. 0xa9,
  2313. 0x6c,
  2314. 0x56,
  2315. 0xf4,
  2316. 0xea,
  2317. 0x65,
  2318. 0x7a,
  2319. 0xae,
  2320. 0x08,
  2321. 0xba,
  2322. 0x78,
  2323. 0x25,
  2324. 0x2e,
  2325. 0x1c,
  2326. 0xa6,
  2327. 0xb4,
  2328. 0xc6,
  2329. 0xe8,
  2330. 0xdd,
  2331. 0x74,
  2332. 0x1f,
  2333. 0x4b,
  2334. 0xbd,
  2335. 0x8b,
  2336. 0x8a,
  2337. 0x70,
  2338. 0x3e,
  2339. 0xb5,
  2340. 0x66,
  2341. 0x48,
  2342. 0x03,
  2343. 0xf6,
  2344. 0x0e,
  2345. 0x61,
  2346. 0x35,
  2347. 0x57,
  2348. 0xb9,
  2349. 0x86,
  2350. 0xc1,
  2351. 0x1d,
  2352. 0x9e,
  2353. 0xe1,
  2354. 0xf8,
  2355. 0x98,
  2356. 0x11,
  2357. 0x69,
  2358. 0xd9,
  2359. 0x8e,
  2360. 0x94,
  2361. 0x9b,
  2362. 0x1e,
  2363. 0x87,
  2364. 0xe9,
  2365. 0xce,
  2366. 0x55,
  2367. 0x28,
  2368. 0xdf,
  2369. 0x8c,
  2370. 0xa1,
  2371. 0x89,
  2372. 0x0d,
  2373. 0xbf,
  2374. 0xe6,
  2375. 0x42,
  2376. 0x68,
  2377. 0x41,
  2378. 0x99,
  2379. 0x2d,
  2380. 0x0f,
  2381. 0xb0,
  2382. 0x54,
  2383. 0xbb,
  2384. 0x16
  2385. ]);
  2386. var inv_s = new Uint8Array([
  2387. 0x52,
  2388. 0x09,
  2389. 0x6a,
  2390. 0xd5,
  2391. 0x30,
  2392. 0x36,
  2393. 0xa5,
  2394. 0x38,
  2395. 0xbf,
  2396. 0x40,
  2397. 0xa3,
  2398. 0x9e,
  2399. 0x81,
  2400. 0xf3,
  2401. 0xd7,
  2402. 0xfb,
  2403. 0x7c,
  2404. 0xe3,
  2405. 0x39,
  2406. 0x82,
  2407. 0x9b,
  2408. 0x2f,
  2409. 0xff,
  2410. 0x87,
  2411. 0x34,
  2412. 0x8e,
  2413. 0x43,
  2414. 0x44,
  2415. 0xc4,
  2416. 0xde,
  2417. 0xe9,
  2418. 0xcb,
  2419. 0x54,
  2420. 0x7b,
  2421. 0x94,
  2422. 0x32,
  2423. 0xa6,
  2424. 0xc2,
  2425. 0x23,
  2426. 0x3d,
  2427. 0xee,
  2428. 0x4c,
  2429. 0x95,
  2430. 0x0b,
  2431. 0x42,
  2432. 0xfa,
  2433. 0xc3,
  2434. 0x4e,
  2435. 0x08,
  2436. 0x2e,
  2437. 0xa1,
  2438. 0x66,
  2439. 0x28,
  2440. 0xd9,
  2441. 0x24,
  2442. 0xb2,
  2443. 0x76,
  2444. 0x5b,
  2445. 0xa2,
  2446. 0x49,
  2447. 0x6d,
  2448. 0x8b,
  2449. 0xd1,
  2450. 0x25,
  2451. 0x72,
  2452. 0xf8,
  2453. 0xf6,
  2454. 0x64,
  2455. 0x86,
  2456. 0x68,
  2457. 0x98,
  2458. 0x16,
  2459. 0xd4,
  2460. 0xa4,
  2461. 0x5c,
  2462. 0xcc,
  2463. 0x5d,
  2464. 0x65,
  2465. 0xb6,
  2466. 0x92,
  2467. 0x6c,
  2468. 0x70,
  2469. 0x48,
  2470. 0x50,
  2471. 0xfd,
  2472. 0xed,
  2473. 0xb9,
  2474. 0xda,
  2475. 0x5e,
  2476. 0x15,
  2477. 0x46,
  2478. 0x57,
  2479. 0xa7,
  2480. 0x8d,
  2481. 0x9d,
  2482. 0x84,
  2483. 0x90,
  2484. 0xd8,
  2485. 0xab,
  2486. 0x00,
  2487. 0x8c,
  2488. 0xbc,
  2489. 0xd3,
  2490. 0x0a,
  2491. 0xf7,
  2492. 0xe4,
  2493. 0x58,
  2494. 0x05,
  2495. 0xb8,
  2496. 0xb3,
  2497. 0x45,
  2498. 0x06,
  2499. 0xd0,
  2500. 0x2c,
  2501. 0x1e,
  2502. 0x8f,
  2503. 0xca,
  2504. 0x3f,
  2505. 0x0f,
  2506. 0x02,
  2507. 0xc1,
  2508. 0xaf,
  2509. 0xbd,
  2510. 0x03,
  2511. 0x01,
  2512. 0x13,
  2513. 0x8a,
  2514. 0x6b,
  2515. 0x3a,
  2516. 0x91,
  2517. 0x11,
  2518. 0x41,
  2519. 0x4f,
  2520. 0x67,
  2521. 0xdc,
  2522. 0xea,
  2523. 0x97,
  2524. 0xf2,
  2525. 0xcf,
  2526. 0xce,
  2527. 0xf0,
  2528. 0xb4,
  2529. 0xe6,
  2530. 0x73,
  2531. 0x96,
  2532. 0xac,
  2533. 0x74,
  2534. 0x22,
  2535. 0xe7,
  2536. 0xad,
  2537. 0x35,
  2538. 0x85,
  2539. 0xe2,
  2540. 0xf9,
  2541. 0x37,
  2542. 0xe8,
  2543. 0x1c,
  2544. 0x75,
  2545. 0xdf,
  2546. 0x6e,
  2547. 0x47,
  2548. 0xf1,
  2549. 0x1a,
  2550. 0x71,
  2551. 0x1d,
  2552. 0x29,
  2553. 0xc5,
  2554. 0x89,
  2555. 0x6f,
  2556. 0xb7,
  2557. 0x62,
  2558. 0x0e,
  2559. 0xaa,
  2560. 0x18,
  2561. 0xbe,
  2562. 0x1b,
  2563. 0xfc,
  2564. 0x56,
  2565. 0x3e,
  2566. 0x4b,
  2567. 0xc6,
  2568. 0xd2,
  2569. 0x79,
  2570. 0x20,
  2571. 0x9a,
  2572. 0xdb,
  2573. 0xc0,
  2574. 0xfe,
  2575. 0x78,
  2576. 0xcd,
  2577. 0x5a,
  2578. 0xf4,
  2579. 0x1f,
  2580. 0xdd,
  2581. 0xa8,
  2582. 0x33,
  2583. 0x88,
  2584. 0x07,
  2585. 0xc7,
  2586. 0x31,
  2587. 0xb1,
  2588. 0x12,
  2589. 0x10,
  2590. 0x59,
  2591. 0x27,
  2592. 0x80,
  2593. 0xec,
  2594. 0x5f,
  2595. 0x60,
  2596. 0x51,
  2597. 0x7f,
  2598. 0xa9,
  2599. 0x19,
  2600. 0xb5,
  2601. 0x4a,
  2602. 0x0d,
  2603. 0x2d,
  2604. 0xe5,
  2605. 0x7a,
  2606. 0x9f,
  2607. 0x93,
  2608. 0xc9,
  2609. 0x9c,
  2610. 0xef,
  2611. 0xa0,
  2612. 0xe0,
  2613. 0x3b,
  2614. 0x4d,
  2615. 0xae,
  2616. 0x2a,
  2617. 0xf5,
  2618. 0xb0,
  2619. 0xc8,
  2620. 0xeb,
  2621. 0xbb,
  2622. 0x3c,
  2623. 0x83,
  2624. 0x53,
  2625. 0x99,
  2626. 0x61,
  2627. 0x17,
  2628. 0x2b,
  2629. 0x04,
  2630. 0x7e,
  2631. 0xba,
  2632. 0x77,
  2633. 0xd6,
  2634. 0x26,
  2635. 0xe1,
  2636. 0x69,
  2637. 0x14,
  2638. 0x63,
  2639. 0x55,
  2640. 0x21,
  2641. 0x0c,
  2642. 0x7d
  2643. ]);
  2644. var mixCol = new Uint8Array(256);
  2645. for (var i = 0; i < 256; i++) {
  2646. if (i < 128) {
  2647. mixCol[i] = i << 1;
  2648. } else {
  2649. mixCol[i] = i << 1 ^ 0x1b;
  2650. }
  2651. }
  2652. var mix = new Uint32Array([
  2653. 0x00000000,
  2654. 0x0e090d0b,
  2655. 0x1c121a16,
  2656. 0x121b171d,
  2657. 0x3824342c,
  2658. 0x362d3927,
  2659. 0x24362e3a,
  2660. 0x2a3f2331,
  2661. 0x70486858,
  2662. 0x7e416553,
  2663. 0x6c5a724e,
  2664. 0x62537f45,
  2665. 0x486c5c74,
  2666. 0x4665517f,
  2667. 0x547e4662,
  2668. 0x5a774b69,
  2669. 0xe090d0b0,
  2670. 0xee99ddbb,
  2671. 0xfc82caa6,
  2672. 0xf28bc7ad,
  2673. 0xd8b4e49c,
  2674. 0xd6bde997,
  2675. 0xc4a6fe8a,
  2676. 0xcaaff381,
  2677. 0x90d8b8e8,
  2678. 0x9ed1b5e3,
  2679. 0x8ccaa2fe,
  2680. 0x82c3aff5,
  2681. 0xa8fc8cc4,
  2682. 0xa6f581cf,
  2683. 0xb4ee96d2,
  2684. 0xbae79bd9,
  2685. 0xdb3bbb7b,
  2686. 0xd532b670,
  2687. 0xc729a16d,
  2688. 0xc920ac66,
  2689. 0xe31f8f57,
  2690. 0xed16825c,
  2691. 0xff0d9541,
  2692. 0xf104984a,
  2693. 0xab73d323,
  2694. 0xa57ade28,
  2695. 0xb761c935,
  2696. 0xb968c43e,
  2697. 0x9357e70f,
  2698. 0x9d5eea04,
  2699. 0x8f45fd19,
  2700. 0x814cf012,
  2701. 0x3bab6bcb,
  2702. 0x35a266c0,
  2703. 0x27b971dd,
  2704. 0x29b07cd6,
  2705. 0x038f5fe7,
  2706. 0x0d8652ec,
  2707. 0x1f9d45f1,
  2708. 0x119448fa,
  2709. 0x4be30393,
  2710. 0x45ea0e98,
  2711. 0x57f11985,
  2712. 0x59f8148e,
  2713. 0x73c737bf,
  2714. 0x7dce3ab4,
  2715. 0x6fd52da9,
  2716. 0x61dc20a2,
  2717. 0xad766df6,
  2718. 0xa37f60fd,
  2719. 0xb16477e0,
  2720. 0xbf6d7aeb,
  2721. 0x955259da,
  2722. 0x9b5b54d1,
  2723. 0x894043cc,
  2724. 0x87494ec7,
  2725. 0xdd3e05ae,
  2726. 0xd33708a5,
  2727. 0xc12c1fb8,
  2728. 0xcf2512b3,
  2729. 0xe51a3182,
  2730. 0xeb133c89,
  2731. 0xf9082b94,
  2732. 0xf701269f,
  2733. 0x4de6bd46,
  2734. 0x43efb04d,
  2735. 0x51f4a750,
  2736. 0x5ffdaa5b,
  2737. 0x75c2896a,
  2738. 0x7bcb8461,
  2739. 0x69d0937c,
  2740. 0x67d99e77,
  2741. 0x3daed51e,
  2742. 0x33a7d815,
  2743. 0x21bccf08,
  2744. 0x2fb5c203,
  2745. 0x058ae132,
  2746. 0x0b83ec39,
  2747. 0x1998fb24,
  2748. 0x1791f62f,
  2749. 0x764dd68d,
  2750. 0x7844db86,
  2751. 0x6a5fcc9b,
  2752. 0x6456c190,
  2753. 0x4e69e2a1,
  2754. 0x4060efaa,
  2755. 0x527bf8b7,
  2756. 0x5c72f5bc,
  2757. 0x0605bed5,
  2758. 0x080cb3de,
  2759. 0x1a17a4c3,
  2760. 0x141ea9c8,
  2761. 0x3e218af9,
  2762. 0x302887f2,
  2763. 0x223390ef,
  2764. 0x2c3a9de4,
  2765. 0x96dd063d,
  2766. 0x98d40b36,
  2767. 0x8acf1c2b,
  2768. 0x84c61120,
  2769. 0xaef93211,
  2770. 0xa0f03f1a,
  2771. 0xb2eb2807,
  2772. 0xbce2250c,
  2773. 0xe6956e65,
  2774. 0xe89c636e,
  2775. 0xfa877473,
  2776. 0xf48e7978,
  2777. 0xdeb15a49,
  2778. 0xd0b85742,
  2779. 0xc2a3405f,
  2780. 0xccaa4d54,
  2781. 0x41ecdaf7,
  2782. 0x4fe5d7fc,
  2783. 0x5dfec0e1,
  2784. 0x53f7cdea,
  2785. 0x79c8eedb,
  2786. 0x77c1e3d0,
  2787. 0x65daf4cd,
  2788. 0x6bd3f9c6,
  2789. 0x31a4b2af,
  2790. 0x3fadbfa4,
  2791. 0x2db6a8b9,
  2792. 0x23bfa5b2,
  2793. 0x09808683,
  2794. 0x07898b88,
  2795. 0x15929c95,
  2796. 0x1b9b919e,
  2797. 0xa17c0a47,
  2798. 0xaf75074c,
  2799. 0xbd6e1051,
  2800. 0xb3671d5a,
  2801. 0x99583e6b,
  2802. 0x97513360,
  2803. 0x854a247d,
  2804. 0x8b432976,
  2805. 0xd134621f,
  2806. 0xdf3d6f14,
  2807. 0xcd267809,
  2808. 0xc32f7502,
  2809. 0xe9105633,
  2810. 0xe7195b38,
  2811. 0xf5024c25,
  2812. 0xfb0b412e,
  2813. 0x9ad7618c,
  2814. 0x94de6c87,
  2815. 0x86c57b9a,
  2816. 0x88cc7691,
  2817. 0xa2f355a0,
  2818. 0xacfa58ab,
  2819. 0xbee14fb6,
  2820. 0xb0e842bd,
  2821. 0xea9f09d4,
  2822. 0xe49604df,
  2823. 0xf68d13c2,
  2824. 0xf8841ec9,
  2825. 0xd2bb3df8,
  2826. 0xdcb230f3,
  2827. 0xcea927ee,
  2828. 0xc0a02ae5,
  2829. 0x7a47b13c,
  2830. 0x744ebc37,
  2831. 0x6655ab2a,
  2832. 0x685ca621,
  2833. 0x42638510,
  2834. 0x4c6a881b,
  2835. 0x5e719f06,
  2836. 0x5078920d,
  2837. 0x0a0fd964,
  2838. 0x0406d46f,
  2839. 0x161dc372,
  2840. 0x1814ce79,
  2841. 0x322bed48,
  2842. 0x3c22e043,
  2843. 0x2e39f75e,
  2844. 0x2030fa55,
  2845. 0xec9ab701,
  2846. 0xe293ba0a,
  2847. 0xf088ad17,
  2848. 0xfe81a01c,
  2849. 0xd4be832d,
  2850. 0xdab78e26,
  2851. 0xc8ac993b,
  2852. 0xc6a59430,
  2853. 0x9cd2df59,
  2854. 0x92dbd252,
  2855. 0x80c0c54f,
  2856. 0x8ec9c844,
  2857. 0xa4f6eb75,
  2858. 0xaaffe67e,
  2859. 0xb8e4f163,
  2860. 0xb6edfc68,
  2861. 0x0c0a67b1,
  2862. 0x02036aba,
  2863. 0x10187da7,
  2864. 0x1e1170ac,
  2865. 0x342e539d,
  2866. 0x3a275e96,
  2867. 0x283c498b,
  2868. 0x26354480,
  2869. 0x7c420fe9,
  2870. 0x724b02e2,
  2871. 0x605015ff,
  2872. 0x6e5918f4,
  2873. 0x44663bc5,
  2874. 0x4a6f36ce,
  2875. 0x587421d3,
  2876. 0x567d2cd8,
  2877. 0x37a10c7a,
  2878. 0x39a80171,
  2879. 0x2bb3166c,
  2880. 0x25ba1b67,
  2881. 0x0f853856,
  2882. 0x018c355d,
  2883. 0x13972240,
  2884. 0x1d9e2f4b,
  2885. 0x47e96422,
  2886. 0x49e06929,
  2887. 0x5bfb7e34,
  2888. 0x55f2733f,
  2889. 0x7fcd500e,
  2890. 0x71c45d05,
  2891. 0x63df4a18,
  2892. 0x6dd64713,
  2893. 0xd731dcca,
  2894. 0xd938d1c1,
  2895. 0xcb23c6dc,
  2896. 0xc52acbd7,
  2897. 0xef15e8e6,
  2898. 0xe11ce5ed,
  2899. 0xf307f2f0,
  2900. 0xfd0efffb,
  2901. 0xa779b492,
  2902. 0xa970b999,
  2903. 0xbb6bae84,
  2904. 0xb562a38f,
  2905. 0x9f5d80be,
  2906. 0x91548db5,
  2907. 0x834f9aa8,
  2908. 0x8d4697a3
  2909. ]);
  2910. function expandKey256(cipherKey) {
  2911. var b = 240, result = new Uint8Array(b);
  2912. var r = 1;
  2913. result.set(cipherKey);
  2914. for (var j = 32, i = 1; j < b; ++i) {
  2915. if (j % 32 === 16) {
  2916. t1 = s[t1];
  2917. t2 = s[t2];
  2918. t3 = s[t3];
  2919. t4 = s[t4];
  2920. } else if (j % 32 === 0) {
  2921. var t1 = result[j - 3], t2 = result[j - 2], t3 = result[j - 1], t4 = result[j - 4];
  2922. t1 = s[t1];
  2923. t2 = s[t2];
  2924. t3 = s[t3];
  2925. t4 = s[t4];
  2926. t1 = t1 ^ r;
  2927. if ((r <<= 1) >= 256) {
  2928. r = (r ^ 0x1b) & 0xFF;
  2929. }
  2930. }
  2931. for (var n = 0; n < 4; ++n) {
  2932. result[j] = t1 ^= result[j - 32];
  2933. j++;
  2934. result[j] = t2 ^= result[j - 32];
  2935. j++;
  2936. result[j] = t3 ^= result[j - 32];
  2937. j++;
  2938. result[j] = t4 ^= result[j - 32];
  2939. j++;
  2940. }
  2941. }
  2942. return result;
  2943. }
  2944. function decrypt256(input, key) {
  2945. var state = new Uint8Array(16);
  2946. state.set(input);
  2947. var i, j, k;
  2948. var t, u, v;
  2949. for (j = 0, k = 224; j < 16; ++j, ++k) {
  2950. state[j] ^= key[k];
  2951. }
  2952. for (i = 13; i >= 1; --i) {
  2953. t = state[13];
  2954. state[13] = state[9];
  2955. state[9] = state[5];
  2956. state[5] = state[1];
  2957. state[1] = t;
  2958. t = state[14];
  2959. u = state[10];
  2960. state[14] = state[6];
  2961. state[10] = state[2];
  2962. state[6] = t;
  2963. state[2] = u;
  2964. t = state[15];
  2965. u = state[11];
  2966. v = state[7];
  2967. state[15] = state[3];
  2968. state[11] = t;
  2969. state[7] = u;
  2970. state[3] = v;
  2971. for (j = 0; j < 16; ++j) {
  2972. state[j] = inv_s[state[j]];
  2973. }
  2974. for (j = 0, k = i * 16; j < 16; ++j, ++k) {
  2975. state[j] ^= key[k];
  2976. }
  2977. for (j = 0; j < 16; j += 4) {
  2978. var s0 = mix[state[j]], s1 = mix[state[j + 1]], s2 = mix[state[j + 2]], s3 = mix[state[j + 3]];
  2979. t = s0 ^ s1 >>> 8 ^ s1 << 24 ^ s2 >>> 16 ^ s2 << 16 ^ s3 >>> 24 ^ s3 << 8;
  2980. state[j] = t >>> 24 & 0xFF;
  2981. state[j + 1] = t >> 16 & 0xFF;
  2982. state[j + 2] = t >> 8 & 0xFF;
  2983. state[j + 3] = t & 0xFF;
  2984. }
  2985. }
  2986. t = state[13];
  2987. state[13] = state[9];
  2988. state[9] = state[5];
  2989. state[5] = state[1];
  2990. state[1] = t;
  2991. t = state[14];
  2992. u = state[10];
  2993. state[14] = state[6];
  2994. state[10] = state[2];
  2995. state[6] = t;
  2996. state[2] = u;
  2997. t = state[15];
  2998. u = state[11];
  2999. v = state[7];
  3000. state[15] = state[3];
  3001. state[11] = t;
  3002. state[7] = u;
  3003. state[3] = v;
  3004. for (j = 0; j < 16; ++j) {
  3005. state[j] = inv_s[state[j]];
  3006. state[j] ^= key[j];
  3007. }
  3008. return state;
  3009. }
  3010. function encrypt256(input, key) {
  3011. var t, u, v, k;
  3012. var state = new Uint8Array(16);
  3013. state.set(input);
  3014. for (j = 0; j < 16; ++j) {
  3015. state[j] ^= key[j];
  3016. }
  3017. for (i = 1; i < 14; i++) {
  3018. for (j = 0; j < 16; ++j) {
  3019. state[j] = s[state[j]];
  3020. }
  3021. v = state[1];
  3022. state[1] = state[5];
  3023. state[5] = state[9];
  3024. state[9] = state[13];
  3025. state[13] = v;
  3026. v = state[2];
  3027. u = state[6];
  3028. state[2] = state[10];
  3029. state[6] = state[14];
  3030. state[10] = v;
  3031. state[14] = u;
  3032. v = state[3];
  3033. u = state[7];
  3034. t = state[11];
  3035. state[3] = state[15];
  3036. state[7] = v;
  3037. state[11] = u;
  3038. state[15] = t;
  3039. for (var j = 0; j < 16; j += 4) {
  3040. var s0 = state[j + 0], s1 = state[j + 1];
  3041. var s2 = state[j + 2], s3 = state[j + 3];
  3042. t = s0 ^ s1 ^ s2 ^ s3;
  3043. state[j + 0] ^= t ^ mixCol[s0 ^ s1];
  3044. state[j + 1] ^= t ^ mixCol[s1 ^ s2];
  3045. state[j + 2] ^= t ^ mixCol[s2 ^ s3];
  3046. state[j + 3] ^= t ^ mixCol[s3 ^ s0];
  3047. }
  3048. for (j = 0, k = i * 16; j < 16; ++j, ++k) {
  3049. state[j] ^= key[k];
  3050. }
  3051. }
  3052. for (j = 0; j < 16; ++j) {
  3053. state[j] = s[state[j]];
  3054. }
  3055. v = state[1];
  3056. state[1] = state[5];
  3057. state[5] = state[9];
  3058. state[9] = state[13];
  3059. state[13] = v;
  3060. v = state[2];
  3061. u = state[6];
  3062. state[2] = state[10];
  3063. state[6] = state[14];
  3064. state[10] = v;
  3065. state[14] = u;
  3066. v = state[3];
  3067. u = state[7];
  3068. t = state[11];
  3069. state[3] = state[15];
  3070. state[7] = v;
  3071. state[11] = u;
  3072. state[15] = t;
  3073. for (j = 0, k = 224; j < 16; ++j, ++k) {
  3074. state[j] ^= key[k];
  3075. }
  3076. return state;
  3077. }
  3078. function AES256Cipher(key) {
  3079. this.key = expandKey256(key);
  3080. this.buffer = new Uint8Array(16);
  3081. this.bufferPosition = 0;
  3082. }
  3083. function decryptBlock2(data, finalize) {
  3084. var i, j, ii, sourceLength = data.length, buffer = this.buffer, bufferLength = this.bufferPosition, result = [], iv = this.iv;
  3085. for (i = 0; i < sourceLength; ++i) {
  3086. buffer[bufferLength] = data[i];
  3087. ++bufferLength;
  3088. if (bufferLength < 16) {
  3089. continue;
  3090. }
  3091. var plain = decrypt256(buffer, this.key);
  3092. for (j = 0; j < 16; ++j) {
  3093. plain[j] ^= iv[j];
  3094. }
  3095. iv = buffer;
  3096. result.push(plain);
  3097. buffer = new Uint8Array(16);
  3098. bufferLength = 0;
  3099. }
  3100. this.buffer = buffer;
  3101. this.bufferLength = bufferLength;
  3102. this.iv = iv;
  3103. if (result.length === 0) {
  3104. return new Uint8Array([]);
  3105. }
  3106. var outputLength = 16 * result.length;
  3107. if (finalize) {
  3108. var lastBlock = result[result.length - 1];
  3109. var psLen = lastBlock[15];
  3110. if (psLen <= 16) {
  3111. for (i = 15, ii = 16 - psLen; i >= ii; --i) {
  3112. if (lastBlock[i] !== psLen) {
  3113. psLen = 0;
  3114. break;
  3115. }
  3116. }
  3117. outputLength -= psLen;
  3118. result[result.length - 1] = lastBlock.subarray(0, 16 - psLen);
  3119. }
  3120. }
  3121. var output = new Uint8Array(outputLength);
  3122. for (i = 0, j = 0, ii = result.length; i < ii; ++i, j += 16) {
  3123. output.set(result[i], j);
  3124. }
  3125. return output;
  3126. }
  3127. AES256Cipher.prototype = {
  3128. decryptBlock: function AES256Cipher_decryptBlock(data, finalize, iv) {
  3129. var i, sourceLength = data.length;
  3130. var buffer = this.buffer, bufferLength = this.bufferPosition;
  3131. if (iv) {
  3132. this.iv = iv;
  3133. } else {
  3134. for (i = 0; bufferLength < 16 && i < sourceLength; ++i, ++bufferLength) {
  3135. buffer[bufferLength] = data[i];
  3136. }
  3137. if (bufferLength < 16) {
  3138. this.bufferLength = bufferLength;
  3139. return new Uint8Array([]);
  3140. }
  3141. this.iv = buffer;
  3142. data = data.subarray(16);
  3143. }
  3144. this.buffer = new Uint8Array(16);
  3145. this.bufferLength = 0;
  3146. this.decryptBlock = decryptBlock2;
  3147. return this.decryptBlock(data, finalize);
  3148. },
  3149. encrypt: function AES256Cipher_encrypt(data, iv) {
  3150. var i, j, ii, sourceLength = data.length, buffer = this.buffer, bufferLength = this.bufferPosition, result = [];
  3151. if (!iv) {
  3152. iv = new Uint8Array(16);
  3153. }
  3154. for (i = 0; i < sourceLength; ++i) {
  3155. buffer[bufferLength] = data[i];
  3156. ++bufferLength;
  3157. if (bufferLength < 16) {
  3158. continue;
  3159. }
  3160. for (j = 0; j < 16; ++j) {
  3161. buffer[j] ^= iv[j];
  3162. }
  3163. var cipher = encrypt256(buffer, this.key);
  3164. this.iv = cipher;
  3165. result.push(cipher);
  3166. buffer = new Uint8Array(16);
  3167. bufferLength = 0;
  3168. }
  3169. this.buffer = buffer;
  3170. this.bufferLength = bufferLength;
  3171. this.iv = iv;
  3172. if (result.length === 0) {
  3173. return new Uint8Array([]);
  3174. }
  3175. var outputLength = 16 * result.length;
  3176. var output = new Uint8Array(outputLength);
  3177. for (i = 0, j = 0, ii = result.length; i < ii; ++i, j += 16) {
  3178. output.set(result[i], j);
  3179. }
  3180. return output;
  3181. }
  3182. };
  3183. return AES256Cipher;
  3184. }();
  3185. var PDF17 = function PDF17Closure() {
  3186. function compareByteArrays(array1, array2) {
  3187. if (array1.length !== array2.length) {
  3188. return false;
  3189. }
  3190. for (var i = 0; i < array1.length; i++) {
  3191. if (array1[i] !== array2[i]) {
  3192. return false;
  3193. }
  3194. }
  3195. return true;
  3196. }
  3197. function PDF17() {
  3198. }
  3199. PDF17.prototype = {
  3200. checkOwnerPassword: function PDF17_checkOwnerPassword(password, ownerValidationSalt, userBytes, ownerPassword) {
  3201. var hashData = new Uint8Array(password.length + 56);
  3202. hashData.set(password, 0);
  3203. hashData.set(ownerValidationSalt, password.length);
  3204. hashData.set(userBytes, password.length + ownerValidationSalt.length);
  3205. var result = calculateSHA256(hashData, 0, hashData.length);
  3206. return compareByteArrays(result, ownerPassword);
  3207. },
  3208. checkUserPassword: function PDF17_checkUserPassword(password, userValidationSalt, userPassword) {
  3209. var hashData = new Uint8Array(password.length + 8);
  3210. hashData.set(password, 0);
  3211. hashData.set(userValidationSalt, password.length);
  3212. var result = calculateSHA256(hashData, 0, hashData.length);
  3213. return compareByteArrays(result, userPassword);
  3214. },
  3215. getOwnerKey: function PDF17_getOwnerKey(password, ownerKeySalt, userBytes, ownerEncryption) {
  3216. var hashData = new Uint8Array(password.length + 56);
  3217. hashData.set(password, 0);
  3218. hashData.set(ownerKeySalt, password.length);
  3219. hashData.set(userBytes, password.length + ownerKeySalt.length);
  3220. var key = calculateSHA256(hashData, 0, hashData.length);
  3221. var cipher = new AES256Cipher(key);
  3222. return cipher.decryptBlock(ownerEncryption, false, new Uint8Array(16));
  3223. },
  3224. getUserKey: function PDF17_getUserKey(password, userKeySalt, userEncryption) {
  3225. var hashData = new Uint8Array(password.length + 8);
  3226. hashData.set(password, 0);
  3227. hashData.set(userKeySalt, password.length);
  3228. var key = calculateSHA256(hashData, 0, hashData.length);
  3229. var cipher = new AES256Cipher(key);
  3230. return cipher.decryptBlock(userEncryption, false, new Uint8Array(16));
  3231. }
  3232. };
  3233. return PDF17;
  3234. }();
  3235. var PDF20 = function PDF20Closure() {
  3236. function concatArrays(array1, array2) {
  3237. var t = new Uint8Array(array1.length + array2.length);
  3238. t.set(array1, 0);
  3239. t.set(array2, array1.length);
  3240. return t;
  3241. }
  3242. function calculatePDF20Hash(password, input, userBytes) {
  3243. var k = calculateSHA256(input, 0, input.length).subarray(0, 32);
  3244. var e = [0];
  3245. var i = 0;
  3246. while (i < 64 || e[e.length - 1] > i - 32) {
  3247. var arrayLength = password.length + k.length + userBytes.length;
  3248. var k1 = new Uint8Array(arrayLength * 64);
  3249. var array = concatArrays(password, k);
  3250. array = concatArrays(array, userBytes);
  3251. for (var j = 0, pos = 0; j < 64; j++, pos += arrayLength) {
  3252. k1.set(array, pos);
  3253. }
  3254. var cipher = new AES128Cipher(k.subarray(0, 16));
  3255. e = cipher.encrypt(k1, k.subarray(16, 32));
  3256. var remainder = 0;
  3257. for (var z = 0; z < 16; z++) {
  3258. remainder *= 256 % 3;
  3259. remainder %= 3;
  3260. remainder += (e[z] >>> 0) % 3;
  3261. remainder %= 3;
  3262. }
  3263. if (remainder === 0) {
  3264. k = calculateSHA256(e, 0, e.length);
  3265. } else if (remainder === 1) {
  3266. k = calculateSHA384(e, 0, e.length);
  3267. } else if (remainder === 2) {
  3268. k = calculateSHA512(e, 0, e.length);
  3269. }
  3270. i++;
  3271. }
  3272. return k.subarray(0, 32);
  3273. }
  3274. function PDF20() {
  3275. }
  3276. function compareByteArrays(array1, array2) {
  3277. if (array1.length !== array2.length) {
  3278. return false;
  3279. }
  3280. for (var i = 0; i < array1.length; i++) {
  3281. if (array1[i] !== array2[i]) {
  3282. return false;
  3283. }
  3284. }
  3285. return true;
  3286. }
  3287. PDF20.prototype = {
  3288. hash: function PDF20_hash(password, concatBytes, userBytes) {
  3289. return calculatePDF20Hash(password, concatBytes, userBytes);
  3290. },
  3291. checkOwnerPassword: function PDF20_checkOwnerPassword(password, ownerValidationSalt, userBytes, ownerPassword) {
  3292. var hashData = new Uint8Array(password.length + 56);
  3293. hashData.set(password, 0);
  3294. hashData.set(ownerValidationSalt, password.length);
  3295. hashData.set(userBytes, password.length + ownerValidationSalt.length);
  3296. var result = calculatePDF20Hash(password, hashData, userBytes);
  3297. return compareByteArrays(result, ownerPassword);
  3298. },
  3299. checkUserPassword: function PDF20_checkUserPassword(password, userValidationSalt, userPassword) {
  3300. var hashData = new Uint8Array(password.length + 8);
  3301. hashData.set(password, 0);
  3302. hashData.set(userValidationSalt, password.length);
  3303. var result = calculatePDF20Hash(password, hashData, []);
  3304. return compareByteArrays(result, userPassword);
  3305. },
  3306. getOwnerKey: function PDF20_getOwnerKey(password, ownerKeySalt, userBytes, ownerEncryption) {
  3307. var hashData = new Uint8Array(password.length + 56);
  3308. hashData.set(password, 0);
  3309. hashData.set(ownerKeySalt, password.length);
  3310. hashData.set(userBytes, password.length + ownerKeySalt.length);
  3311. var key = calculatePDF20Hash(password, hashData, userBytes);
  3312. var cipher = new AES256Cipher(key);
  3313. return cipher.decryptBlock(ownerEncryption, false, new Uint8Array(16));
  3314. },
  3315. getUserKey: function PDF20_getUserKey(password, userKeySalt, userEncryption) {
  3316. var hashData = new Uint8Array(password.length + 8);
  3317. hashData.set(password, 0);
  3318. hashData.set(userKeySalt, password.length);
  3319. var key = calculatePDF20Hash(password, hashData, []);
  3320. var cipher = new AES256Cipher(key);
  3321. return cipher.decryptBlock(userEncryption, false, new Uint8Array(16));
  3322. }
  3323. };
  3324. return PDF20;
  3325. }();
  3326. var CipherTransform = function CipherTransformClosure() {
  3327. function CipherTransform(stringCipherConstructor, streamCipherConstructor) {
  3328. this.StringCipherConstructor = stringCipherConstructor;
  3329. this.StreamCipherConstructor = streamCipherConstructor;
  3330. }
  3331. CipherTransform.prototype = {
  3332. createStream: function CipherTransform_createStream(stream, length) {
  3333. var cipher = new this.StreamCipherConstructor();
  3334. return new DecryptStream(stream, length, function cipherTransformDecryptStream(data, finalize) {
  3335. return cipher.decryptBlock(data, finalize);
  3336. });
  3337. },
  3338. decryptString: function CipherTransform_decryptString(s) {
  3339. var cipher = new this.StringCipherConstructor();
  3340. var data = stringToBytes(s);
  3341. data = cipher.decryptBlock(data, true);
  3342. return bytesToString(data);
  3343. }
  3344. };
  3345. return CipherTransform;
  3346. }();
  3347. var CipherTransformFactory = function CipherTransformFactoryClosure() {
  3348. var defaultPasswordBytes = new Uint8Array([
  3349. 0x28,
  3350. 0xBF,
  3351. 0x4E,
  3352. 0x5E,
  3353. 0x4E,
  3354. 0x75,
  3355. 0x8A,
  3356. 0x41,
  3357. 0x64,
  3358. 0x00,
  3359. 0x4E,
  3360. 0x56,
  3361. 0xFF,
  3362. 0xFA,
  3363. 0x01,
  3364. 0x08,
  3365. 0x2E,
  3366. 0x2E,
  3367. 0x00,
  3368. 0xB6,
  3369. 0xD0,
  3370. 0x68,
  3371. 0x3E,
  3372. 0x80,
  3373. 0x2F,
  3374. 0x0C,
  3375. 0xA9,
  3376. 0xFE,
  3377. 0x64,
  3378. 0x53,
  3379. 0x69,
  3380. 0x7A
  3381. ]);
  3382. function createEncryptionKey20(revision, password, ownerPassword, ownerValidationSalt, ownerKeySalt, uBytes, userPassword, userValidationSalt, userKeySalt, ownerEncryption, userEncryption, perms) {
  3383. if (password) {
  3384. var passwordLength = Math.min(127, password.length);
  3385. password = password.subarray(0, passwordLength);
  3386. } else {
  3387. password = [];
  3388. }
  3389. var pdfAlgorithm;
  3390. if (revision === 6) {
  3391. pdfAlgorithm = new PDF20();
  3392. } else {
  3393. pdfAlgorithm = new PDF17();
  3394. }
  3395. if (pdfAlgorithm.checkUserPassword(password, userValidationSalt, userPassword)) {
  3396. return pdfAlgorithm.getUserKey(password, userKeySalt, userEncryption);
  3397. } else if (password.length && pdfAlgorithm.checkOwnerPassword(password, ownerValidationSalt, uBytes, ownerPassword)) {
  3398. return pdfAlgorithm.getOwnerKey(password, ownerKeySalt, uBytes, ownerEncryption);
  3399. }
  3400. return null;
  3401. }
  3402. function prepareKeyData(fileId, password, ownerPassword, userPassword, flags, revision, keyLength, encryptMetadata) {
  3403. var hashDataSize = 40 + ownerPassword.length + fileId.length;
  3404. var hashData = new Uint8Array(hashDataSize), i = 0, j, n;
  3405. if (password) {
  3406. n = Math.min(32, password.length);
  3407. for (; i < n; ++i) {
  3408. hashData[i] = password[i];
  3409. }
  3410. }
  3411. j = 0;
  3412. while (i < 32) {
  3413. hashData[i++] = defaultPasswordBytes[j++];
  3414. }
  3415. for (j = 0, n = ownerPassword.length; j < n; ++j) {
  3416. hashData[i++] = ownerPassword[j];
  3417. }
  3418. hashData[i++] = flags & 0xFF;
  3419. hashData[i++] = flags >> 8 & 0xFF;
  3420. hashData[i++] = flags >> 16 & 0xFF;
  3421. hashData[i++] = flags >>> 24 & 0xFF;
  3422. for (j = 0, n = fileId.length; j < n; ++j) {
  3423. hashData[i++] = fileId[j];
  3424. }
  3425. if (revision >= 4 && !encryptMetadata) {
  3426. hashData[i++] = 0xFF;
  3427. hashData[i++] = 0xFF;
  3428. hashData[i++] = 0xFF;
  3429. hashData[i++] = 0xFF;
  3430. }
  3431. var hash = calculateMD5(hashData, 0, i);
  3432. var keyLengthInBytes = keyLength >> 3;
  3433. if (revision >= 3) {
  3434. for (j = 0; j < 50; ++j) {
  3435. hash = calculateMD5(hash, 0, keyLengthInBytes);
  3436. }
  3437. }
  3438. var encryptionKey = hash.subarray(0, keyLengthInBytes);
  3439. var cipher, checkData;
  3440. if (revision >= 3) {
  3441. for (i = 0; i < 32; ++i) {
  3442. hashData[i] = defaultPasswordBytes[i];
  3443. }
  3444. for (j = 0, n = fileId.length; j < n; ++j) {
  3445. hashData[i++] = fileId[j];
  3446. }
  3447. cipher = new ARCFourCipher(encryptionKey);
  3448. checkData = cipher.encryptBlock(calculateMD5(hashData, 0, i));
  3449. n = encryptionKey.length;
  3450. var derivedKey = new Uint8Array(n), k;
  3451. for (j = 1; j <= 19; ++j) {
  3452. for (k = 0; k < n; ++k) {
  3453. derivedKey[k] = encryptionKey[k] ^ j;
  3454. }
  3455. cipher = new ARCFourCipher(derivedKey);
  3456. checkData = cipher.encryptBlock(checkData);
  3457. }
  3458. for (j = 0, n = checkData.length; j < n; ++j) {
  3459. if (userPassword[j] !== checkData[j]) {
  3460. return null;
  3461. }
  3462. }
  3463. } else {
  3464. cipher = new ARCFourCipher(encryptionKey);
  3465. checkData = cipher.encryptBlock(defaultPasswordBytes);
  3466. for (j = 0, n = checkData.length; j < n; ++j) {
  3467. if (userPassword[j] !== checkData[j]) {
  3468. return null;
  3469. }
  3470. }
  3471. }
  3472. return encryptionKey;
  3473. }
  3474. function decodeUserPassword(password, ownerPassword, revision, keyLength) {
  3475. var hashData = new Uint8Array(32), i = 0, j, n;
  3476. n = Math.min(32, password.length);
  3477. for (; i < n; ++i) {
  3478. hashData[i] = password[i];
  3479. }
  3480. j = 0;
  3481. while (i < 32) {
  3482. hashData[i++] = defaultPasswordBytes[j++];
  3483. }
  3484. var hash = calculateMD5(hashData, 0, i);
  3485. var keyLengthInBytes = keyLength >> 3;
  3486. if (revision >= 3) {
  3487. for (j = 0; j < 50; ++j) {
  3488. hash = calculateMD5(hash, 0, hash.length);
  3489. }
  3490. }
  3491. var cipher, userPassword;
  3492. if (revision >= 3) {
  3493. userPassword = ownerPassword;
  3494. var derivedKey = new Uint8Array(keyLengthInBytes), k;
  3495. for (j = 19; j >= 0; j--) {
  3496. for (k = 0; k < keyLengthInBytes; ++k) {
  3497. derivedKey[k] = hash[k] ^ j;
  3498. }
  3499. cipher = new ARCFourCipher(derivedKey);
  3500. userPassword = cipher.encryptBlock(userPassword);
  3501. }
  3502. } else {
  3503. cipher = new ARCFourCipher(hash.subarray(0, keyLengthInBytes));
  3504. userPassword = cipher.encryptBlock(ownerPassword);
  3505. }
  3506. return userPassword;
  3507. }
  3508. var identityName = Name.get('Identity');
  3509. function CipherTransformFactory(dict, fileId, password) {
  3510. var filter = dict.get('Filter');
  3511. if (!isName(filter, 'Standard')) {
  3512. error('unknown encryption method');
  3513. }
  3514. this.dict = dict;
  3515. var algorithm = dict.get('V');
  3516. if (!isInt(algorithm) || algorithm !== 1 && algorithm !== 2 && algorithm !== 4 && algorithm !== 5) {
  3517. error('unsupported encryption algorithm');
  3518. }
  3519. this.algorithm = algorithm;
  3520. var keyLength = dict.get('Length');
  3521. if (!keyLength) {
  3522. if (algorithm <= 3) {
  3523. keyLength = 40;
  3524. } else {
  3525. var cfDict = dict.get('CF');
  3526. var streamCryptoName = dict.get('StmF');
  3527. if (isDict(cfDict) && isName(streamCryptoName)) {
  3528. cfDict.suppressEncryption = true;
  3529. var handlerDict = cfDict.get(streamCryptoName.name);
  3530. keyLength = handlerDict && handlerDict.get('Length') || 128;
  3531. if (keyLength < 40) {
  3532. keyLength <<= 3;
  3533. }
  3534. }
  3535. }
  3536. }
  3537. if (!isInt(keyLength) || keyLength < 40 || keyLength % 8 !== 0) {
  3538. error('invalid key length');
  3539. }
  3540. var ownerPassword = stringToBytes(dict.get('O')).subarray(0, 32);
  3541. var userPassword = stringToBytes(dict.get('U')).subarray(0, 32);
  3542. var flags = dict.get('P');
  3543. var revision = dict.get('R');
  3544. var encryptMetadata = (algorithm === 4 || algorithm === 5) && dict.get('EncryptMetadata') !== false;
  3545. this.encryptMetadata = encryptMetadata;
  3546. var fileIdBytes = stringToBytes(fileId);
  3547. var passwordBytes;
  3548. if (password) {
  3549. if (revision === 6) {
  3550. try {
  3551. password = utf8StringToString(password);
  3552. } catch (ex) {
  3553. warn('CipherTransformFactory: ' + 'Unable to convert UTF8 encoded password.');
  3554. }
  3555. }
  3556. passwordBytes = stringToBytes(password);
  3557. }
  3558. var encryptionKey;
  3559. if (algorithm !== 5) {
  3560. encryptionKey = prepareKeyData(fileIdBytes, passwordBytes, ownerPassword, userPassword, flags, revision, keyLength, encryptMetadata);
  3561. } else {
  3562. var ownerValidationSalt = stringToBytes(dict.get('O')).subarray(32, 40);
  3563. var ownerKeySalt = stringToBytes(dict.get('O')).subarray(40, 48);
  3564. var uBytes = stringToBytes(dict.get('U')).subarray(0, 48);
  3565. var userValidationSalt = stringToBytes(dict.get('U')).subarray(32, 40);
  3566. var userKeySalt = stringToBytes(dict.get('U')).subarray(40, 48);
  3567. var ownerEncryption = stringToBytes(dict.get('OE'));
  3568. var userEncryption = stringToBytes(dict.get('UE'));
  3569. var perms = stringToBytes(dict.get('Perms'));
  3570. encryptionKey = createEncryptionKey20(revision, passwordBytes, ownerPassword, ownerValidationSalt, ownerKeySalt, uBytes, userPassword, userValidationSalt, userKeySalt, ownerEncryption, userEncryption, perms);
  3571. }
  3572. if (!encryptionKey && !password) {
  3573. throw new PasswordException('No password given', PasswordResponses.NEED_PASSWORD);
  3574. } else if (!encryptionKey && password) {
  3575. var decodedPassword = decodeUserPassword(passwordBytes, ownerPassword, revision, keyLength);
  3576. encryptionKey = prepareKeyData(fileIdBytes, decodedPassword, ownerPassword, userPassword, flags, revision, keyLength, encryptMetadata);
  3577. }
  3578. if (!encryptionKey) {
  3579. throw new PasswordException('Incorrect Password', PasswordResponses.INCORRECT_PASSWORD);
  3580. }
  3581. this.encryptionKey = encryptionKey;
  3582. if (algorithm >= 4) {
  3583. var cf = dict.get('CF');
  3584. if (isDict(cf)) {
  3585. cf.suppressEncryption = true;
  3586. }
  3587. this.cf = cf;
  3588. this.stmf = dict.get('StmF') || identityName;
  3589. this.strf = dict.get('StrF') || identityName;
  3590. this.eff = dict.get('EFF') || this.stmf;
  3591. }
  3592. }
  3593. function buildObjectKey(num, gen, encryptionKey, isAes) {
  3594. var key = new Uint8Array(encryptionKey.length + 9), i, n;
  3595. for (i = 0, n = encryptionKey.length; i < n; ++i) {
  3596. key[i] = encryptionKey[i];
  3597. }
  3598. key[i++] = num & 0xFF;
  3599. key[i++] = num >> 8 & 0xFF;
  3600. key[i++] = num >> 16 & 0xFF;
  3601. key[i++] = gen & 0xFF;
  3602. key[i++] = gen >> 8 & 0xFF;
  3603. if (isAes) {
  3604. key[i++] = 0x73;
  3605. key[i++] = 0x41;
  3606. key[i++] = 0x6C;
  3607. key[i++] = 0x54;
  3608. }
  3609. var hash = calculateMD5(key, 0, i);
  3610. return hash.subarray(0, Math.min(encryptionKey.length + 5, 16));
  3611. }
  3612. function buildCipherConstructor(cf, name, num, gen, key) {
  3613. assert(isName(name), 'Invalid crypt filter name.');
  3614. var cryptFilter = cf.get(name.name);
  3615. var cfm;
  3616. if (cryptFilter !== null && cryptFilter !== undefined) {
  3617. cfm = cryptFilter.get('CFM');
  3618. }
  3619. if (!cfm || cfm.name === 'None') {
  3620. return function cipherTransformFactoryBuildCipherConstructorNone() {
  3621. return new NullCipher();
  3622. };
  3623. }
  3624. if (cfm.name === 'V2') {
  3625. return function cipherTransformFactoryBuildCipherConstructorV2() {
  3626. return new ARCFourCipher(buildObjectKey(num, gen, key, false));
  3627. };
  3628. }
  3629. if (cfm.name === 'AESV2') {
  3630. return function cipherTransformFactoryBuildCipherConstructorAESV2() {
  3631. return new AES128Cipher(buildObjectKey(num, gen, key, true));
  3632. };
  3633. }
  3634. if (cfm.name === 'AESV3') {
  3635. return function cipherTransformFactoryBuildCipherConstructorAESV3() {
  3636. return new AES256Cipher(key);
  3637. };
  3638. }
  3639. error('Unknown crypto method');
  3640. }
  3641. CipherTransformFactory.prototype = {
  3642. createCipherTransform: function CipherTransformFactory_createCipherTransform(num, gen) {
  3643. if (this.algorithm === 4 || this.algorithm === 5) {
  3644. return new CipherTransform(buildCipherConstructor(this.cf, this.stmf, num, gen, this.encryptionKey), buildCipherConstructor(this.cf, this.strf, num, gen, this.encryptionKey));
  3645. }
  3646. var key = buildObjectKey(num, gen, this.encryptionKey, false);
  3647. var cipherConstructor = function buildCipherCipherConstructor() {
  3648. return new ARCFourCipher(key);
  3649. };
  3650. return new CipherTransform(cipherConstructor, cipherConstructor);
  3651. }
  3652. };
  3653. return CipherTransformFactory;
  3654. }();
  3655. exports.AES128Cipher = AES128Cipher;
  3656. exports.AES256Cipher = AES256Cipher;
  3657. exports.ARCFourCipher = ARCFourCipher;
  3658. exports.CipherTransformFactory = CipherTransformFactory;
  3659. exports.PDF17 = PDF17;
  3660. exports.PDF20 = PDF20;
  3661. exports.calculateMD5 = calculateMD5;
  3662. exports.calculateSHA256 = calculateSHA256;
  3663. exports.calculateSHA384 = calculateSHA384;
  3664. exports.calculateSHA512 = calculateSHA512;