Front end of the Slack clone application.

nacl-fast.js 61KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389
  1. (function(nacl) {
  2. 'use strict';
  3. // Ported in 2014 by Dmitry Chestnykh and Devi Mandiri.
  4. // Public domain.
  5. //
  6. // Implementation derived from TweetNaCl version 20140427.
  7. // See for details: http://tweetnacl.cr.yp.to/
  8. var gf = function(init) {
  9. var i, r = new Float64Array(16);
  10. if (init) for (i = 0; i < init.length; i++) r[i] = init[i];
  11. return r;
  12. };
  13. // Pluggable, initialized in high-level API below.
  14. var randombytes = function(/* x, n */) { throw new Error('no PRNG'); };
  15. var _0 = new Uint8Array(16);
  16. var _9 = new Uint8Array(32); _9[0] = 9;
  17. var gf0 = gf(),
  18. gf1 = gf([1]),
  19. _121665 = gf([0xdb41, 1]),
  20. D = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]),
  21. D2 = gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406]),
  22. X = gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169]),
  23. Y = gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666]),
  24. I = gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]);
  25. function ts64(x, i, h, l) {
  26. x[i] = (h >> 24) & 0xff;
  27. x[i+1] = (h >> 16) & 0xff;
  28. x[i+2] = (h >> 8) & 0xff;
  29. x[i+3] = h & 0xff;
  30. x[i+4] = (l >> 24) & 0xff;
  31. x[i+5] = (l >> 16) & 0xff;
  32. x[i+6] = (l >> 8) & 0xff;
  33. x[i+7] = l & 0xff;
  34. }
  35. function vn(x, xi, y, yi, n) {
  36. var i,d = 0;
  37. for (i = 0; i < n; i++) d |= x[xi+i]^y[yi+i];
  38. return (1 & ((d - 1) >>> 8)) - 1;
  39. }
  40. function crypto_verify_16(x, xi, y, yi) {
  41. return vn(x,xi,y,yi,16);
  42. }
  43. function crypto_verify_32(x, xi, y, yi) {
  44. return vn(x,xi,y,yi,32);
  45. }
  46. function core_salsa20(o, p, k, c) {
  47. var j0 = c[ 0] & 0xff | (c[ 1] & 0xff)<<8 | (c[ 2] & 0xff)<<16 | (c[ 3] & 0xff)<<24,
  48. j1 = k[ 0] & 0xff | (k[ 1] & 0xff)<<8 | (k[ 2] & 0xff)<<16 | (k[ 3] & 0xff)<<24,
  49. j2 = k[ 4] & 0xff | (k[ 5] & 0xff)<<8 | (k[ 6] & 0xff)<<16 | (k[ 7] & 0xff)<<24,
  50. j3 = k[ 8] & 0xff | (k[ 9] & 0xff)<<8 | (k[10] & 0xff)<<16 | (k[11] & 0xff)<<24,
  51. j4 = k[12] & 0xff | (k[13] & 0xff)<<8 | (k[14] & 0xff)<<16 | (k[15] & 0xff)<<24,
  52. j5 = c[ 4] & 0xff | (c[ 5] & 0xff)<<8 | (c[ 6] & 0xff)<<16 | (c[ 7] & 0xff)<<24,
  53. j6 = p[ 0] & 0xff | (p[ 1] & 0xff)<<8 | (p[ 2] & 0xff)<<16 | (p[ 3] & 0xff)<<24,
  54. j7 = p[ 4] & 0xff | (p[ 5] & 0xff)<<8 | (p[ 6] & 0xff)<<16 | (p[ 7] & 0xff)<<24,
  55. j8 = p[ 8] & 0xff | (p[ 9] & 0xff)<<8 | (p[10] & 0xff)<<16 | (p[11] & 0xff)<<24,
  56. j9 = p[12] & 0xff | (p[13] & 0xff)<<8 | (p[14] & 0xff)<<16 | (p[15] & 0xff)<<24,
  57. j10 = c[ 8] & 0xff | (c[ 9] & 0xff)<<8 | (c[10] & 0xff)<<16 | (c[11] & 0xff)<<24,
  58. j11 = k[16] & 0xff | (k[17] & 0xff)<<8 | (k[18] & 0xff)<<16 | (k[19] & 0xff)<<24,
  59. j12 = k[20] & 0xff | (k[21] & 0xff)<<8 | (k[22] & 0xff)<<16 | (k[23] & 0xff)<<24,
  60. j13 = k[24] & 0xff | (k[25] & 0xff)<<8 | (k[26] & 0xff)<<16 | (k[27] & 0xff)<<24,
  61. j14 = k[28] & 0xff | (k[29] & 0xff)<<8 | (k[30] & 0xff)<<16 | (k[31] & 0xff)<<24,
  62. j15 = c[12] & 0xff | (c[13] & 0xff)<<8 | (c[14] & 0xff)<<16 | (c[15] & 0xff)<<24;
  63. var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7,
  64. x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14,
  65. x15 = j15, u;
  66. for (var i = 0; i < 20; i += 2) {
  67. u = x0 + x12 | 0;
  68. x4 ^= u<<7 | u>>>(32-7);
  69. u = x4 + x0 | 0;
  70. x8 ^= u<<9 | u>>>(32-9);
  71. u = x8 + x4 | 0;
  72. x12 ^= u<<13 | u>>>(32-13);
  73. u = x12 + x8 | 0;
  74. x0 ^= u<<18 | u>>>(32-18);
  75. u = x5 + x1 | 0;
  76. x9 ^= u<<7 | u>>>(32-7);
  77. u = x9 + x5 | 0;
  78. x13 ^= u<<9 | u>>>(32-9);
  79. u = x13 + x9 | 0;
  80. x1 ^= u<<13 | u>>>(32-13);
  81. u = x1 + x13 | 0;
  82. x5 ^= u<<18 | u>>>(32-18);
  83. u = x10 + x6 | 0;
  84. x14 ^= u<<7 | u>>>(32-7);
  85. u = x14 + x10 | 0;
  86. x2 ^= u<<9 | u>>>(32-9);
  87. u = x2 + x14 | 0;
  88. x6 ^= u<<13 | u>>>(32-13);
  89. u = x6 + x2 | 0;
  90. x10 ^= u<<18 | u>>>(32-18);
  91. u = x15 + x11 | 0;
  92. x3 ^= u<<7 | u>>>(32-7);
  93. u = x3 + x15 | 0;
  94. x7 ^= u<<9 | u>>>(32-9);
  95. u = x7 + x3 | 0;
  96. x11 ^= u<<13 | u>>>(32-13);
  97. u = x11 + x7 | 0;
  98. x15 ^= u<<18 | u>>>(32-18);
  99. u = x0 + x3 | 0;
  100. x1 ^= u<<7 | u>>>(32-7);
  101. u = x1 + x0 | 0;
  102. x2 ^= u<<9 | u>>>(32-9);
  103. u = x2 + x1 | 0;
  104. x3 ^= u<<13 | u>>>(32-13);
  105. u = x3 + x2 | 0;
  106. x0 ^= u<<18 | u>>>(32-18);
  107. u = x5 + x4 | 0;
  108. x6 ^= u<<7 | u>>>(32-7);
  109. u = x6 + x5 | 0;
  110. x7 ^= u<<9 | u>>>(32-9);
  111. u = x7 + x6 | 0;
  112. x4 ^= u<<13 | u>>>(32-13);
  113. u = x4 + x7 | 0;
  114. x5 ^= u<<18 | u>>>(32-18);
  115. u = x10 + x9 | 0;
  116. x11 ^= u<<7 | u>>>(32-7);
  117. u = x11 + x10 | 0;
  118. x8 ^= u<<9 | u>>>(32-9);
  119. u = x8 + x11 | 0;
  120. x9 ^= u<<13 | u>>>(32-13);
  121. u = x9 + x8 | 0;
  122. x10 ^= u<<18 | u>>>(32-18);
  123. u = x15 + x14 | 0;
  124. x12 ^= u<<7 | u>>>(32-7);
  125. u = x12 + x15 | 0;
  126. x13 ^= u<<9 | u>>>(32-9);
  127. u = x13 + x12 | 0;
  128. x14 ^= u<<13 | u>>>(32-13);
  129. u = x14 + x13 | 0;
  130. x15 ^= u<<18 | u>>>(32-18);
  131. }
  132. x0 = x0 + j0 | 0;
  133. x1 = x1 + j1 | 0;
  134. x2 = x2 + j2 | 0;
  135. x3 = x3 + j3 | 0;
  136. x4 = x4 + j4 | 0;
  137. x5 = x5 + j5 | 0;
  138. x6 = x6 + j6 | 0;
  139. x7 = x7 + j7 | 0;
  140. x8 = x8 + j8 | 0;
  141. x9 = x9 + j9 | 0;
  142. x10 = x10 + j10 | 0;
  143. x11 = x11 + j11 | 0;
  144. x12 = x12 + j12 | 0;
  145. x13 = x13 + j13 | 0;
  146. x14 = x14 + j14 | 0;
  147. x15 = x15 + j15 | 0;
  148. o[ 0] = x0 >>> 0 & 0xff;
  149. o[ 1] = x0 >>> 8 & 0xff;
  150. o[ 2] = x0 >>> 16 & 0xff;
  151. o[ 3] = x0 >>> 24 & 0xff;
  152. o[ 4] = x1 >>> 0 & 0xff;
  153. o[ 5] = x1 >>> 8 & 0xff;
  154. o[ 6] = x1 >>> 16 & 0xff;
  155. o[ 7] = x1 >>> 24 & 0xff;
  156. o[ 8] = x2 >>> 0 & 0xff;
  157. o[ 9] = x2 >>> 8 & 0xff;
  158. o[10] = x2 >>> 16 & 0xff;
  159. o[11] = x2 >>> 24 & 0xff;
  160. o[12] = x3 >>> 0 & 0xff;
  161. o[13] = x3 >>> 8 & 0xff;
  162. o[14] = x3 >>> 16 & 0xff;
  163. o[15] = x3 >>> 24 & 0xff;
  164. o[16] = x4 >>> 0 & 0xff;
  165. o[17] = x4 >>> 8 & 0xff;
  166. o[18] = x4 >>> 16 & 0xff;
  167. o[19] = x4 >>> 24 & 0xff;
  168. o[20] = x5 >>> 0 & 0xff;
  169. o[21] = x5 >>> 8 & 0xff;
  170. o[22] = x5 >>> 16 & 0xff;
  171. o[23] = x5 >>> 24 & 0xff;
  172. o[24] = x6 >>> 0 & 0xff;
  173. o[25] = x6 >>> 8 & 0xff;
  174. o[26] = x6 >>> 16 & 0xff;
  175. o[27] = x6 >>> 24 & 0xff;
  176. o[28] = x7 >>> 0 & 0xff;
  177. o[29] = x7 >>> 8 & 0xff;
  178. o[30] = x7 >>> 16 & 0xff;
  179. o[31] = x7 >>> 24 & 0xff;
  180. o[32] = x8 >>> 0 & 0xff;
  181. o[33] = x8 >>> 8 & 0xff;
  182. o[34] = x8 >>> 16 & 0xff;
  183. o[35] = x8 >>> 24 & 0xff;
  184. o[36] = x9 >>> 0 & 0xff;
  185. o[37] = x9 >>> 8 & 0xff;
  186. o[38] = x9 >>> 16 & 0xff;
  187. o[39] = x9 >>> 24 & 0xff;
  188. o[40] = x10 >>> 0 & 0xff;
  189. o[41] = x10 >>> 8 & 0xff;
  190. o[42] = x10 >>> 16 & 0xff;
  191. o[43] = x10 >>> 24 & 0xff;
  192. o[44] = x11 >>> 0 & 0xff;
  193. o[45] = x11 >>> 8 & 0xff;
  194. o[46] = x11 >>> 16 & 0xff;
  195. o[47] = x11 >>> 24 & 0xff;
  196. o[48] = x12 >>> 0 & 0xff;
  197. o[49] = x12 >>> 8 & 0xff;
  198. o[50] = x12 >>> 16 & 0xff;
  199. o[51] = x12 >>> 24 & 0xff;
  200. o[52] = x13 >>> 0 & 0xff;
  201. o[53] = x13 >>> 8 & 0xff;
  202. o[54] = x13 >>> 16 & 0xff;
  203. o[55] = x13 >>> 24 & 0xff;
  204. o[56] = x14 >>> 0 & 0xff;
  205. o[57] = x14 >>> 8 & 0xff;
  206. o[58] = x14 >>> 16 & 0xff;
  207. o[59] = x14 >>> 24 & 0xff;
  208. o[60] = x15 >>> 0 & 0xff;
  209. o[61] = x15 >>> 8 & 0xff;
  210. o[62] = x15 >>> 16 & 0xff;
  211. o[63] = x15 >>> 24 & 0xff;
  212. }
  213. function core_hsalsa20(o,p,k,c) {
  214. var j0 = c[ 0] & 0xff | (c[ 1] & 0xff)<<8 | (c[ 2] & 0xff)<<16 | (c[ 3] & 0xff)<<24,
  215. j1 = k[ 0] & 0xff | (k[ 1] & 0xff)<<8 | (k[ 2] & 0xff)<<16 | (k[ 3] & 0xff)<<24,
  216. j2 = k[ 4] & 0xff | (k[ 5] & 0xff)<<8 | (k[ 6] & 0xff)<<16 | (k[ 7] & 0xff)<<24,
  217. j3 = k[ 8] & 0xff | (k[ 9] & 0xff)<<8 | (k[10] & 0xff)<<16 | (k[11] & 0xff)<<24,
  218. j4 = k[12] & 0xff | (k[13] & 0xff)<<8 | (k[14] & 0xff)<<16 | (k[15] & 0xff)<<24,
  219. j5 = c[ 4] & 0xff | (c[ 5] & 0xff)<<8 | (c[ 6] & 0xff)<<16 | (c[ 7] & 0xff)<<24,
  220. j6 = p[ 0] & 0xff | (p[ 1] & 0xff)<<8 | (p[ 2] & 0xff)<<16 | (p[ 3] & 0xff)<<24,
  221. j7 = p[ 4] & 0xff | (p[ 5] & 0xff)<<8 | (p[ 6] & 0xff)<<16 | (p[ 7] & 0xff)<<24,
  222. j8 = p[ 8] & 0xff | (p[ 9] & 0xff)<<8 | (p[10] & 0xff)<<16 | (p[11] & 0xff)<<24,
  223. j9 = p[12] & 0xff | (p[13] & 0xff)<<8 | (p[14] & 0xff)<<16 | (p[15] & 0xff)<<24,
  224. j10 = c[ 8] & 0xff | (c[ 9] & 0xff)<<8 | (c[10] & 0xff)<<16 | (c[11] & 0xff)<<24,
  225. j11 = k[16] & 0xff | (k[17] & 0xff)<<8 | (k[18] & 0xff)<<16 | (k[19] & 0xff)<<24,
  226. j12 = k[20] & 0xff | (k[21] & 0xff)<<8 | (k[22] & 0xff)<<16 | (k[23] & 0xff)<<24,
  227. j13 = k[24] & 0xff | (k[25] & 0xff)<<8 | (k[26] & 0xff)<<16 | (k[27] & 0xff)<<24,
  228. j14 = k[28] & 0xff | (k[29] & 0xff)<<8 | (k[30] & 0xff)<<16 | (k[31] & 0xff)<<24,
  229. j15 = c[12] & 0xff | (c[13] & 0xff)<<8 | (c[14] & 0xff)<<16 | (c[15] & 0xff)<<24;
  230. var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7,
  231. x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14,
  232. x15 = j15, u;
  233. for (var i = 0; i < 20; i += 2) {
  234. u = x0 + x12 | 0;
  235. x4 ^= u<<7 | u>>>(32-7);
  236. u = x4 + x0 | 0;
  237. x8 ^= u<<9 | u>>>(32-9);
  238. u = x8 + x4 | 0;
  239. x12 ^= u<<13 | u>>>(32-13);
  240. u = x12 + x8 | 0;
  241. x0 ^= u<<18 | u>>>(32-18);
  242. u = x5 + x1 | 0;
  243. x9 ^= u<<7 | u>>>(32-7);
  244. u = x9 + x5 | 0;
  245. x13 ^= u<<9 | u>>>(32-9);
  246. u = x13 + x9 | 0;
  247. x1 ^= u<<13 | u>>>(32-13);
  248. u = x1 + x13 | 0;
  249. x5 ^= u<<18 | u>>>(32-18);
  250. u = x10 + x6 | 0;
  251. x14 ^= u<<7 | u>>>(32-7);
  252. u = x14 + x10 | 0;
  253. x2 ^= u<<9 | u>>>(32-9);
  254. u = x2 + x14 | 0;
  255. x6 ^= u<<13 | u>>>(32-13);
  256. u = x6 + x2 | 0;
  257. x10 ^= u<<18 | u>>>(32-18);
  258. u = x15 + x11 | 0;
  259. x3 ^= u<<7 | u>>>(32-7);
  260. u = x3 + x15 | 0;
  261. x7 ^= u<<9 | u>>>(32-9);
  262. u = x7 + x3 | 0;
  263. x11 ^= u<<13 | u>>>(32-13);
  264. u = x11 + x7 | 0;
  265. x15 ^= u<<18 | u>>>(32-18);
  266. u = x0 + x3 | 0;
  267. x1 ^= u<<7 | u>>>(32-7);
  268. u = x1 + x0 | 0;
  269. x2 ^= u<<9 | u>>>(32-9);
  270. u = x2 + x1 | 0;
  271. x3 ^= u<<13 | u>>>(32-13);
  272. u = x3 + x2 | 0;
  273. x0 ^= u<<18 | u>>>(32-18);
  274. u = x5 + x4 | 0;
  275. x6 ^= u<<7 | u>>>(32-7);
  276. u = x6 + x5 | 0;
  277. x7 ^= u<<9 | u>>>(32-9);
  278. u = x7 + x6 | 0;
  279. x4 ^= u<<13 | u>>>(32-13);
  280. u = x4 + x7 | 0;
  281. x5 ^= u<<18 | u>>>(32-18);
  282. u = x10 + x9 | 0;
  283. x11 ^= u<<7 | u>>>(32-7);
  284. u = x11 + x10 | 0;
  285. x8 ^= u<<9 | u>>>(32-9);
  286. u = x8 + x11 | 0;
  287. x9 ^= u<<13 | u>>>(32-13);
  288. u = x9 + x8 | 0;
  289. x10 ^= u<<18 | u>>>(32-18);
  290. u = x15 + x14 | 0;
  291. x12 ^= u<<7 | u>>>(32-7);
  292. u = x12 + x15 | 0;
  293. x13 ^= u<<9 | u>>>(32-9);
  294. u = x13 + x12 | 0;
  295. x14 ^= u<<13 | u>>>(32-13);
  296. u = x14 + x13 | 0;
  297. x15 ^= u<<18 | u>>>(32-18);
  298. }
  299. o[ 0] = x0 >>> 0 & 0xff;
  300. o[ 1] = x0 >>> 8 & 0xff;
  301. o[ 2] = x0 >>> 16 & 0xff;
  302. o[ 3] = x0 >>> 24 & 0xff;
  303. o[ 4] = x5 >>> 0 & 0xff;
  304. o[ 5] = x5 >>> 8 & 0xff;
  305. o[ 6] = x5 >>> 16 & 0xff;
  306. o[ 7] = x5 >>> 24 & 0xff;
  307. o[ 8] = x10 >>> 0 & 0xff;
  308. o[ 9] = x10 >>> 8 & 0xff;
  309. o[10] = x10 >>> 16 & 0xff;
  310. o[11] = x10 >>> 24 & 0xff;
  311. o[12] = x15 >>> 0 & 0xff;
  312. o[13] = x15 >>> 8 & 0xff;
  313. o[14] = x15 >>> 16 & 0xff;
  314. o[15] = x15 >>> 24 & 0xff;
  315. o[16] = x6 >>> 0 & 0xff;
  316. o[17] = x6 >>> 8 & 0xff;
  317. o[18] = x6 >>> 16 & 0xff;
  318. o[19] = x6 >>> 24 & 0xff;
  319. o[20] = x7 >>> 0 & 0xff;
  320. o[21] = x7 >>> 8 & 0xff;
  321. o[22] = x7 >>> 16 & 0xff;
  322. o[23] = x7 >>> 24 & 0xff;
  323. o[24] = x8 >>> 0 & 0xff;
  324. o[25] = x8 >>> 8 & 0xff;
  325. o[26] = x8 >>> 16 & 0xff;
  326. o[27] = x8 >>> 24 & 0xff;
  327. o[28] = x9 >>> 0 & 0xff;
  328. o[29] = x9 >>> 8 & 0xff;
  329. o[30] = x9 >>> 16 & 0xff;
  330. o[31] = x9 >>> 24 & 0xff;
  331. }
  332. function crypto_core_salsa20(out,inp,k,c) {
  333. core_salsa20(out,inp,k,c);
  334. }
  335. function crypto_core_hsalsa20(out,inp,k,c) {
  336. core_hsalsa20(out,inp,k,c);
  337. }
  338. var sigma = new Uint8Array([101, 120, 112, 97, 110, 100, 32, 51, 50, 45, 98, 121, 116, 101, 32, 107]);
  339. // "expand 32-byte k"
  340. function crypto_stream_salsa20_xor(c,cpos,m,mpos,b,n,k) {
  341. var z = new Uint8Array(16), x = new Uint8Array(64);
  342. var u, i;
  343. for (i = 0; i < 16; i++) z[i] = 0;
  344. for (i = 0; i < 8; i++) z[i] = n[i];
  345. while (b >= 64) {
  346. crypto_core_salsa20(x,z,k,sigma);
  347. for (i = 0; i < 64; i++) c[cpos+i] = m[mpos+i] ^ x[i];
  348. u = 1;
  349. for (i = 8; i < 16; i++) {
  350. u = u + (z[i] & 0xff) | 0;
  351. z[i] = u & 0xff;
  352. u >>>= 8;
  353. }
  354. b -= 64;
  355. cpos += 64;
  356. mpos += 64;
  357. }
  358. if (b > 0) {
  359. crypto_core_salsa20(x,z,k,sigma);
  360. for (i = 0; i < b; i++) c[cpos+i] = m[mpos+i] ^ x[i];
  361. }
  362. return 0;
  363. }
  364. function crypto_stream_salsa20(c,cpos,b,n,k) {
  365. var z = new Uint8Array(16), x = new Uint8Array(64);
  366. var u, i;
  367. for (i = 0; i < 16; i++) z[i] = 0;
  368. for (i = 0; i < 8; i++) z[i] = n[i];
  369. while (b >= 64) {
  370. crypto_core_salsa20(x,z,k,sigma);
  371. for (i = 0; i < 64; i++) c[cpos+i] = x[i];
  372. u = 1;
  373. for (i = 8; i < 16; i++) {
  374. u = u + (z[i] & 0xff) | 0;
  375. z[i] = u & 0xff;
  376. u >>>= 8;
  377. }
  378. b -= 64;
  379. cpos += 64;
  380. }
  381. if (b > 0) {
  382. crypto_core_salsa20(x,z,k,sigma);
  383. for (i = 0; i < b; i++) c[cpos+i] = x[i];
  384. }
  385. return 0;
  386. }
  387. function crypto_stream(c,cpos,d,n,k) {
  388. var s = new Uint8Array(32);
  389. crypto_core_hsalsa20(s,n,k,sigma);
  390. var sn = new Uint8Array(8);
  391. for (var i = 0; i < 8; i++) sn[i] = n[i+16];
  392. return crypto_stream_salsa20(c,cpos,d,sn,s);
  393. }
  394. function crypto_stream_xor(c,cpos,m,mpos,d,n,k) {
  395. var s = new Uint8Array(32);
  396. crypto_core_hsalsa20(s,n,k,sigma);
  397. var sn = new Uint8Array(8);
  398. for (var i = 0; i < 8; i++) sn[i] = n[i+16];
  399. return crypto_stream_salsa20_xor(c,cpos,m,mpos,d,sn,s);
  400. }
  401. /*
  402. * Port of Andrew Moon's Poly1305-donna-16. Public domain.
  403. * https://github.com/floodyberry/poly1305-donna
  404. */
  405. var poly1305 = function(key) {
  406. this.buffer = new Uint8Array(16);
  407. this.r = new Uint16Array(10);
  408. this.h = new Uint16Array(10);
  409. this.pad = new Uint16Array(8);
  410. this.leftover = 0;
  411. this.fin = 0;
  412. var t0, t1, t2, t3, t4, t5, t6, t7;
  413. t0 = key[ 0] & 0xff | (key[ 1] & 0xff) << 8; this.r[0] = ( t0 ) & 0x1fff;
  414. t1 = key[ 2] & 0xff | (key[ 3] & 0xff) << 8; this.r[1] = ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
  415. t2 = key[ 4] & 0xff | (key[ 5] & 0xff) << 8; this.r[2] = ((t1 >>> 10) | (t2 << 6)) & 0x1f03;
  416. t3 = key[ 6] & 0xff | (key[ 7] & 0xff) << 8; this.r[3] = ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
  417. t4 = key[ 8] & 0xff | (key[ 9] & 0xff) << 8; this.r[4] = ((t3 >>> 4) | (t4 << 12)) & 0x00ff;
  418. this.r[5] = ((t4 >>> 1)) & 0x1ffe;
  419. t5 = key[10] & 0xff | (key[11] & 0xff) << 8; this.r[6] = ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
  420. t6 = key[12] & 0xff | (key[13] & 0xff) << 8; this.r[7] = ((t5 >>> 11) | (t6 << 5)) & 0x1f81;
  421. t7 = key[14] & 0xff | (key[15] & 0xff) << 8; this.r[8] = ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
  422. this.r[9] = ((t7 >>> 5)) & 0x007f;
  423. this.pad[0] = key[16] & 0xff | (key[17] & 0xff) << 8;
  424. this.pad[1] = key[18] & 0xff | (key[19] & 0xff) << 8;
  425. this.pad[2] = key[20] & 0xff | (key[21] & 0xff) << 8;
  426. this.pad[3] = key[22] & 0xff | (key[23] & 0xff) << 8;
  427. this.pad[4] = key[24] & 0xff | (key[25] & 0xff) << 8;
  428. this.pad[5] = key[26] & 0xff | (key[27] & 0xff) << 8;
  429. this.pad[6] = key[28] & 0xff | (key[29] & 0xff) << 8;
  430. this.pad[7] = key[30] & 0xff | (key[31] & 0xff) << 8;
  431. };
  432. poly1305.prototype.blocks = function(m, mpos, bytes) {
  433. var hibit = this.fin ? 0 : (1 << 11);
  434. var t0, t1, t2, t3, t4, t5, t6, t7, c;
  435. var d0, d1, d2, d3, d4, d5, d6, d7, d8, d9;
  436. var h0 = this.h[0],
  437. h1 = this.h[1],
  438. h2 = this.h[2],
  439. h3 = this.h[3],
  440. h4 = this.h[4],
  441. h5 = this.h[5],
  442. h6 = this.h[6],
  443. h7 = this.h[7],
  444. h8 = this.h[8],
  445. h9 = this.h[9];
  446. var r0 = this.r[0],
  447. r1 = this.r[1],
  448. r2 = this.r[2],
  449. r3 = this.r[3],
  450. r4 = this.r[4],
  451. r5 = this.r[5],
  452. r6 = this.r[6],
  453. r7 = this.r[7],
  454. r8 = this.r[8],
  455. r9 = this.r[9];
  456. while (bytes >= 16) {
  457. t0 = m[mpos+ 0] & 0xff | (m[mpos+ 1] & 0xff) << 8; h0 += ( t0 ) & 0x1fff;
  458. t1 = m[mpos+ 2] & 0xff | (m[mpos+ 3] & 0xff) << 8; h1 += ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
  459. t2 = m[mpos+ 4] & 0xff | (m[mpos+ 5] & 0xff) << 8; h2 += ((t1 >>> 10) | (t2 << 6)) & 0x1fff;
  460. t3 = m[mpos+ 6] & 0xff | (m[mpos+ 7] & 0xff) << 8; h3 += ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
  461. t4 = m[mpos+ 8] & 0xff | (m[mpos+ 9] & 0xff) << 8; h4 += ((t3 >>> 4) | (t4 << 12)) & 0x1fff;
  462. h5 += ((t4 >>> 1)) & 0x1fff;
  463. t5 = m[mpos+10] & 0xff | (m[mpos+11] & 0xff) << 8; h6 += ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
  464. t6 = m[mpos+12] & 0xff | (m[mpos+13] & 0xff) << 8; h7 += ((t5 >>> 11) | (t6 << 5)) & 0x1fff;
  465. t7 = m[mpos+14] & 0xff | (m[mpos+15] & 0xff) << 8; h8 += ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
  466. h9 += ((t7 >>> 5)) | hibit;
  467. c = 0;
  468. d0 = c;
  469. d0 += h0 * r0;
  470. d0 += h1 * (5 * r9);
  471. d0 += h2 * (5 * r8);
  472. d0 += h3 * (5 * r7);
  473. d0 += h4 * (5 * r6);
  474. c = (d0 >>> 13); d0 &= 0x1fff;
  475. d0 += h5 * (5 * r5);
  476. d0 += h6 * (5 * r4);
  477. d0 += h7 * (5 * r3);
  478. d0 += h8 * (5 * r2);
  479. d0 += h9 * (5 * r1);
  480. c += (d0 >>> 13); d0 &= 0x1fff;
  481. d1 = c;
  482. d1 += h0 * r1;
  483. d1 += h1 * r0;
  484. d1 += h2 * (5 * r9);
  485. d1 += h3 * (5 * r8);
  486. d1 += h4 * (5 * r7);
  487. c = (d1 >>> 13); d1 &= 0x1fff;
  488. d1 += h5 * (5 * r6);
  489. d1 += h6 * (5 * r5);
  490. d1 += h7 * (5 * r4);
  491. d1 += h8 * (5 * r3);
  492. d1 += h9 * (5 * r2);
  493. c += (d1 >>> 13); d1 &= 0x1fff;
  494. d2 = c;
  495. d2 += h0 * r2;
  496. d2 += h1 * r1;
  497. d2 += h2 * r0;
  498. d2 += h3 * (5 * r9);
  499. d2 += h4 * (5 * r8);
  500. c = (d2 >>> 13); d2 &= 0x1fff;
  501. d2 += h5 * (5 * r7);
  502. d2 += h6 * (5 * r6);
  503. d2 += h7 * (5 * r5);
  504. d2 += h8 * (5 * r4);
  505. d2 += h9 * (5 * r3);
  506. c += (d2 >>> 13); d2 &= 0x1fff;
  507. d3 = c;
  508. d3 += h0 * r3;
  509. d3 += h1 * r2;
  510. d3 += h2 * r1;
  511. d3 += h3 * r0;
  512. d3 += h4 * (5 * r9);
  513. c = (d3 >>> 13); d3 &= 0x1fff;
  514. d3 += h5 * (5 * r8);
  515. d3 += h6 * (5 * r7);
  516. d3 += h7 * (5 * r6);
  517. d3 += h8 * (5 * r5);
  518. d3 += h9 * (5 * r4);
  519. c += (d3 >>> 13); d3 &= 0x1fff;
  520. d4 = c;
  521. d4 += h0 * r4;
  522. d4 += h1 * r3;
  523. d4 += h2 * r2;
  524. d4 += h3 * r1;
  525. d4 += h4 * r0;
  526. c = (d4 >>> 13); d4 &= 0x1fff;
  527. d4 += h5 * (5 * r9);
  528. d4 += h6 * (5 * r8);
  529. d4 += h7 * (5 * r7);
  530. d4 += h8 * (5 * r6);
  531. d4 += h9 * (5 * r5);
  532. c += (d4 >>> 13); d4 &= 0x1fff;
  533. d5 = c;
  534. d5 += h0 * r5;
  535. d5 += h1 * r4;
  536. d5 += h2 * r3;
  537. d5 += h3 * r2;
  538. d5 += h4 * r1;
  539. c = (d5 >>> 13); d5 &= 0x1fff;
  540. d5 += h5 * r0;
  541. d5 += h6 * (5 * r9);
  542. d5 += h7 * (5 * r8);
  543. d5 += h8 * (5 * r7);
  544. d5 += h9 * (5 * r6);
  545. c += (d5 >>> 13); d5 &= 0x1fff;
  546. d6 = c;
  547. d6 += h0 * r6;
  548. d6 += h1 * r5;
  549. d6 += h2 * r4;
  550. d6 += h3 * r3;
  551. d6 += h4 * r2;
  552. c = (d6 >>> 13); d6 &= 0x1fff;
  553. d6 += h5 * r1;
  554. d6 += h6 * r0;
  555. d6 += h7 * (5 * r9);
  556. d6 += h8 * (5 * r8);
  557. d6 += h9 * (5 * r7);
  558. c += (d6 >>> 13); d6 &= 0x1fff;
  559. d7 = c;
  560. d7 += h0 * r7;
  561. d7 += h1 * r6;
  562. d7 += h2 * r5;
  563. d7 += h3 * r4;
  564. d7 += h4 * r3;
  565. c = (d7 >>> 13); d7 &= 0x1fff;
  566. d7 += h5 * r2;
  567. d7 += h6 * r1;
  568. d7 += h7 * r0;
  569. d7 += h8 * (5 * r9);
  570. d7 += h9 * (5 * r8);
  571. c += (d7 >>> 13); d7 &= 0x1fff;
  572. d8 = c;
  573. d8 += h0 * r8;
  574. d8 += h1 * r7;
  575. d8 += h2 * r6;
  576. d8 += h3 * r5;
  577. d8 += h4 * r4;
  578. c = (d8 >>> 13); d8 &= 0x1fff;
  579. d8 += h5 * r3;
  580. d8 += h6 * r2;
  581. d8 += h7 * r1;
  582. d8 += h8 * r0;
  583. d8 += h9 * (5 * r9);
  584. c += (d8 >>> 13); d8 &= 0x1fff;
  585. d9 = c;
  586. d9 += h0 * r9;
  587. d9 += h1 * r8;
  588. d9 += h2 * r7;
  589. d9 += h3 * r6;
  590. d9 += h4 * r5;
  591. c = (d9 >>> 13); d9 &= 0x1fff;
  592. d9 += h5 * r4;
  593. d9 += h6 * r3;
  594. d9 += h7 * r2;
  595. d9 += h8 * r1;
  596. d9 += h9 * r0;
  597. c += (d9 >>> 13); d9 &= 0x1fff;
  598. c = (((c << 2) + c)) | 0;
  599. c = (c + d0) | 0;
  600. d0 = c & 0x1fff;
  601. c = (c >>> 13);
  602. d1 += c;
  603. h0 = d0;
  604. h1 = d1;
  605. h2 = d2;
  606. h3 = d3;
  607. h4 = d4;
  608. h5 = d5;
  609. h6 = d6;
  610. h7 = d7;
  611. h8 = d8;
  612. h9 = d9;
  613. mpos += 16;
  614. bytes -= 16;
  615. }
  616. this.h[0] = h0;
  617. this.h[1] = h1;
  618. this.h[2] = h2;
  619. this.h[3] = h3;
  620. this.h[4] = h4;
  621. this.h[5] = h5;
  622. this.h[6] = h6;
  623. this.h[7] = h7;
  624. this.h[8] = h8;
  625. this.h[9] = h9;
  626. };
  627. poly1305.prototype.finish = function(mac, macpos) {
  628. var g = new Uint16Array(10);
  629. var c, mask, f, i;
  630. if (this.leftover) {
  631. i = this.leftover;
  632. this.buffer[i++] = 1;
  633. for (; i < 16; i++) this.buffer[i] = 0;
  634. this.fin = 1;
  635. this.blocks(this.buffer, 0, 16);
  636. }
  637. c = this.h[1] >>> 13;
  638. this.h[1] &= 0x1fff;
  639. for (i = 2; i < 10; i++) {
  640. this.h[i] += c;
  641. c = this.h[i] >>> 13;
  642. this.h[i] &= 0x1fff;
  643. }
  644. this.h[0] += (c * 5);
  645. c = this.h[0] >>> 13;
  646. this.h[0] &= 0x1fff;
  647. this.h[1] += c;
  648. c = this.h[1] >>> 13;
  649. this.h[1] &= 0x1fff;
  650. this.h[2] += c;
  651. g[0] = this.h[0] + 5;
  652. c = g[0] >>> 13;
  653. g[0] &= 0x1fff;
  654. for (i = 1; i < 10; i++) {
  655. g[i] = this.h[i] + c;
  656. c = g[i] >>> 13;
  657. g[i] &= 0x1fff;
  658. }
  659. g[9] -= (1 << 13);
  660. mask = (c ^ 1) - 1;
  661. for (i = 0; i < 10; i++) g[i] &= mask;
  662. mask = ~mask;
  663. for (i = 0; i < 10; i++) this.h[i] = (this.h[i] & mask) | g[i];
  664. this.h[0] = ((this.h[0] ) | (this.h[1] << 13) ) & 0xffff;
  665. this.h[1] = ((this.h[1] >>> 3) | (this.h[2] << 10) ) & 0xffff;
  666. this.h[2] = ((this.h[2] >>> 6) | (this.h[3] << 7) ) & 0xffff;
  667. this.h[3] = ((this.h[3] >>> 9) | (this.h[4] << 4) ) & 0xffff;
  668. this.h[4] = ((this.h[4] >>> 12) | (this.h[5] << 1) | (this.h[6] << 14)) & 0xffff;
  669. this.h[5] = ((this.h[6] >>> 2) | (this.h[7] << 11) ) & 0xffff;
  670. this.h[6] = ((this.h[7] >>> 5) | (this.h[8] << 8) ) & 0xffff;
  671. this.h[7] = ((this.h[8] >>> 8) | (this.h[9] << 5) ) & 0xffff;
  672. f = this.h[0] + this.pad[0];
  673. this.h[0] = f & 0xffff;
  674. for (i = 1; i < 8; i++) {
  675. f = (((this.h[i] + this.pad[i]) | 0) + (f >>> 16)) | 0;
  676. this.h[i] = f & 0xffff;
  677. }
  678. mac[macpos+ 0] = (this.h[0] >>> 0) & 0xff;
  679. mac[macpos+ 1] = (this.h[0] >>> 8) & 0xff;
  680. mac[macpos+ 2] = (this.h[1] >>> 0) & 0xff;
  681. mac[macpos+ 3] = (this.h[1] >>> 8) & 0xff;
  682. mac[macpos+ 4] = (this.h[2] >>> 0) & 0xff;
  683. mac[macpos+ 5] = (this.h[2] >>> 8) & 0xff;
  684. mac[macpos+ 6] = (this.h[3] >>> 0) & 0xff;
  685. mac[macpos+ 7] = (this.h[3] >>> 8) & 0xff;
  686. mac[macpos+ 8] = (this.h[4] >>> 0) & 0xff;
  687. mac[macpos+ 9] = (this.h[4] >>> 8) & 0xff;
  688. mac[macpos+10] = (this.h[5] >>> 0) & 0xff;
  689. mac[macpos+11] = (this.h[5] >>> 8) & 0xff;
  690. mac[macpos+12] = (this.h[6] >>> 0) & 0xff;
  691. mac[macpos+13] = (this.h[6] >>> 8) & 0xff;
  692. mac[macpos+14] = (this.h[7] >>> 0) & 0xff;
  693. mac[macpos+15] = (this.h[7] >>> 8) & 0xff;
  694. };
  695. poly1305.prototype.update = function(m, mpos, bytes) {
  696. var i, want;
  697. if (this.leftover) {
  698. want = (16 - this.leftover);
  699. if (want > bytes)
  700. want = bytes;
  701. for (i = 0; i < want; i++)
  702. this.buffer[this.leftover + i] = m[mpos+i];
  703. bytes -= want;
  704. mpos += want;
  705. this.leftover += want;
  706. if (this.leftover < 16)
  707. return;
  708. this.blocks(this.buffer, 0, 16);
  709. this.leftover = 0;
  710. }
  711. if (bytes >= 16) {
  712. want = bytes - (bytes % 16);
  713. this.blocks(m, mpos, want);
  714. mpos += want;
  715. bytes -= want;
  716. }
  717. if (bytes) {
  718. for (i = 0; i < bytes; i++)
  719. this.buffer[this.leftover + i] = m[mpos+i];
  720. this.leftover += bytes;
  721. }
  722. };
  723. function crypto_onetimeauth(out, outpos, m, mpos, n, k) {
  724. var s = new poly1305(k);
  725. s.update(m, mpos, n);
  726. s.finish(out, outpos);
  727. return 0;
  728. }
  729. function crypto_onetimeauth_verify(h, hpos, m, mpos, n, k) {
  730. var x = new Uint8Array(16);
  731. crypto_onetimeauth(x,0,m,mpos,n,k);
  732. return crypto_verify_16(h,hpos,x,0);
  733. }
  734. function crypto_secretbox(c,m,d,n,k) {
  735. var i;
  736. if (d < 32) return -1;
  737. crypto_stream_xor(c,0,m,0,d,n,k);
  738. crypto_onetimeauth(c, 16, c, 32, d - 32, c);
  739. for (i = 0; i < 16; i++) c[i] = 0;
  740. return 0;
  741. }
  742. function crypto_secretbox_open(m,c,d,n,k) {
  743. var i;
  744. var x = new Uint8Array(32);
  745. if (d < 32) return -1;
  746. crypto_stream(x,0,32,n,k);
  747. if (crypto_onetimeauth_verify(c, 16,c, 32,d - 32,x) !== 0) return -1;
  748. crypto_stream_xor(m,0,c,0,d,n,k);
  749. for (i = 0; i < 32; i++) m[i] = 0;
  750. return 0;
  751. }
  752. function set25519(r, a) {
  753. var i;
  754. for (i = 0; i < 16; i++) r[i] = a[i]|0;
  755. }
  756. function car25519(o) {
  757. var i, v, c = 1;
  758. for (i = 0; i < 16; i++) {
  759. v = o[i] + c + 65535;
  760. c = Math.floor(v / 65536);
  761. o[i] = v - c * 65536;
  762. }
  763. o[0] += c-1 + 37 * (c-1);
  764. }
  765. function sel25519(p, q, b) {
  766. var t, c = ~(b-1);
  767. for (var i = 0; i < 16; i++) {
  768. t = c & (p[i] ^ q[i]);
  769. p[i] ^= t;
  770. q[i] ^= t;
  771. }
  772. }
  773. function pack25519(o, n) {
  774. var i, j, b;
  775. var m = gf(), t = gf();
  776. for (i = 0; i < 16; i++) t[i] = n[i];
  777. car25519(t);
  778. car25519(t);
  779. car25519(t);
  780. for (j = 0; j < 2; j++) {
  781. m[0] = t[0] - 0xffed;
  782. for (i = 1; i < 15; i++) {
  783. m[i] = t[i] - 0xffff - ((m[i-1]>>16) & 1);
  784. m[i-1] &= 0xffff;
  785. }
  786. m[15] = t[15] - 0x7fff - ((m[14]>>16) & 1);
  787. b = (m[15]>>16) & 1;
  788. m[14] &= 0xffff;
  789. sel25519(t, m, 1-b);
  790. }
  791. for (i = 0; i < 16; i++) {
  792. o[2*i] = t[i] & 0xff;
  793. o[2*i+1] = t[i]>>8;
  794. }
  795. }
  796. function neq25519(a, b) {
  797. var c = new Uint8Array(32), d = new Uint8Array(32);
  798. pack25519(c, a);
  799. pack25519(d, b);
  800. return crypto_verify_32(c, 0, d, 0);
  801. }
  802. function par25519(a) {
  803. var d = new Uint8Array(32);
  804. pack25519(d, a);
  805. return d[0] & 1;
  806. }
  807. function unpack25519(o, n) {
  808. var i;
  809. for (i = 0; i < 16; i++) o[i] = n[2*i] + (n[2*i+1] << 8);
  810. o[15] &= 0x7fff;
  811. }
  812. function A(o, a, b) {
  813. for (var i = 0; i < 16; i++) o[i] = a[i] + b[i];
  814. }
  815. function Z(o, a, b) {
  816. for (var i = 0; i < 16; i++) o[i] = a[i] - b[i];
  817. }
  818. function M(o, a, b) {
  819. var v, c,
  820. t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0,
  821. t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0,
  822. t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0,
  823. t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0,
  824. b0 = b[0],
  825. b1 = b[1],
  826. b2 = b[2],
  827. b3 = b[3],
  828. b4 = b[4],
  829. b5 = b[5],
  830. b6 = b[6],
  831. b7 = b[7],
  832. b8 = b[8],
  833. b9 = b[9],
  834. b10 = b[10],
  835. b11 = b[11],
  836. b12 = b[12],
  837. b13 = b[13],
  838. b14 = b[14],
  839. b15 = b[15];
  840. v = a[0];
  841. t0 += v * b0;
  842. t1 += v * b1;
  843. t2 += v * b2;
  844. t3 += v * b3;
  845. t4 += v * b4;
  846. t5 += v * b5;
  847. t6 += v * b6;
  848. t7 += v * b7;
  849. t8 += v * b8;
  850. t9 += v * b9;
  851. t10 += v * b10;
  852. t11 += v * b11;
  853. t12 += v * b12;
  854. t13 += v * b13;
  855. t14 += v * b14;
  856. t15 += v * b15;
  857. v = a[1];
  858. t1 += v * b0;
  859. t2 += v * b1;
  860. t3 += v * b2;
  861. t4 += v * b3;
  862. t5 += v * b4;
  863. t6 += v * b5;
  864. t7 += v * b6;
  865. t8 += v * b7;
  866. t9 += v * b8;
  867. t10 += v * b9;
  868. t11 += v * b10;
  869. t12 += v * b11;
  870. t13 += v * b12;
  871. t14 += v * b13;
  872. t15 += v * b14;
  873. t16 += v * b15;
  874. v = a[2];
  875. t2 += v * b0;
  876. t3 += v * b1;
  877. t4 += v * b2;
  878. t5 += v * b3;
  879. t6 += v * b4;
  880. t7 += v * b5;
  881. t8 += v * b6;
  882. t9 += v * b7;
  883. t10 += v * b8;
  884. t11 += v * b9;
  885. t12 += v * b10;
  886. t13 += v * b11;
  887. t14 += v * b12;
  888. t15 += v * b13;
  889. t16 += v * b14;
  890. t17 += v * b15;
  891. v = a[3];
  892. t3 += v * b0;
  893. t4 += v * b1;
  894. t5 += v * b2;
  895. t6 += v * b3;
  896. t7 += v * b4;
  897. t8 += v * b5;
  898. t9 += v * b6;
  899. t10 += v * b7;
  900. t11 += v * b8;
  901. t12 += v * b9;
  902. t13 += v * b10;
  903. t14 += v * b11;
  904. t15 += v * b12;
  905. t16 += v * b13;
  906. t17 += v * b14;
  907. t18 += v * b15;
  908. v = a[4];
  909. t4 += v * b0;
  910. t5 += v * b1;
  911. t6 += v * b2;
  912. t7 += v * b3;
  913. t8 += v * b4;
  914. t9 += v * b5;
  915. t10 += v * b6;
  916. t11 += v * b7;
  917. t12 += v * b8;
  918. t13 += v * b9;
  919. t14 += v * b10;
  920. t15 += v * b11;
  921. t16 += v * b12;
  922. t17 += v * b13;
  923. t18 += v * b14;
  924. t19 += v * b15;
  925. v = a[5];
  926. t5 += v * b0;
  927. t6 += v * b1;
  928. t7 += v * b2;
  929. t8 += v * b3;
  930. t9 += v * b4;
  931. t10 += v * b5;
  932. t11 += v * b6;
  933. t12 += v * b7;
  934. t13 += v * b8;
  935. t14 += v * b9;
  936. t15 += v * b10;
  937. t16 += v * b11;
  938. t17 += v * b12;
  939. t18 += v * b13;
  940. t19 += v * b14;
  941. t20 += v * b15;
  942. v = a[6];
  943. t6 += v * b0;
  944. t7 += v * b1;
  945. t8 += v * b2;
  946. t9 += v * b3;
  947. t10 += v * b4;
  948. t11 += v * b5;
  949. t12 += v * b6;
  950. t13 += v * b7;
  951. t14 += v * b8;
  952. t15 += v * b9;
  953. t16 += v * b10;
  954. t17 += v * b11;
  955. t18 += v * b12;
  956. t19 += v * b13;
  957. t20 += v * b14;
  958. t21 += v * b15;
  959. v = a[7];
  960. t7 += v * b0;
  961. t8 += v * b1;
  962. t9 += v * b2;
  963. t10 += v * b3;
  964. t11 += v * b4;
  965. t12 += v * b5;
  966. t13 += v * b6;
  967. t14 += v * b7;
  968. t15 += v * b8;
  969. t16 += v * b9;
  970. t17 += v * b10;
  971. t18 += v * b11;
  972. t19 += v * b12;
  973. t20 += v * b13;
  974. t21 += v * b14;
  975. t22 += v * b15;
  976. v = a[8];
  977. t8 += v * b0;
  978. t9 += v * b1;
  979. t10 += v * b2;
  980. t11 += v * b3;
  981. t12 += v * b4;
  982. t13 += v * b5;
  983. t14 += v * b6;
  984. t15 += v * b7;
  985. t16 += v * b8;
  986. t17 += v * b9;
  987. t18 += v * b10;
  988. t19 += v * b11;
  989. t20 += v * b12;
  990. t21 += v * b13;
  991. t22 += v * b14;
  992. t23 += v * b15;
  993. v = a[9];
  994. t9 += v * b0;
  995. t10 += v * b1;
  996. t11 += v * b2;
  997. t12 += v * b3;
  998. t13 += v * b4;
  999. t14 += v * b5;
  1000. t15 += v * b6;
  1001. t16 += v * b7;
  1002. t17 += v * b8;
  1003. t18 += v * b9;
  1004. t19 += v * b10;
  1005. t20 += v * b11;
  1006. t21 += v * b12;
  1007. t22 += v * b13;
  1008. t23 += v * b14;
  1009. t24 += v * b15;
  1010. v = a[10];
  1011. t10 += v * b0;
  1012. t11 += v * b1;
  1013. t12 += v * b2;
  1014. t13 += v * b3;
  1015. t14 += v * b4;
  1016. t15 += v * b5;
  1017. t16 += v * b6;
  1018. t17 += v * b7;
  1019. t18 += v * b8;
  1020. t19 += v * b9;
  1021. t20 += v * b10;
  1022. t21 += v * b11;
  1023. t22 += v * b12;
  1024. t23 += v * b13;
  1025. t24 += v * b14;
  1026. t25 += v * b15;
  1027. v = a[11];
  1028. t11 += v * b0;
  1029. t12 += v * b1;
  1030. t13 += v * b2;
  1031. t14 += v * b3;
  1032. t15 += v * b4;
  1033. t16 += v * b5;
  1034. t17 += v * b6;
  1035. t18 += v * b7;
  1036. t19 += v * b8;
  1037. t20 += v * b9;
  1038. t21 += v * b10;
  1039. t22 += v * b11;
  1040. t23 += v * b12;
  1041. t24 += v * b13;
  1042. t25 += v * b14;
  1043. t26 += v * b15;
  1044. v = a[12];
  1045. t12 += v * b0;
  1046. t13 += v * b1;
  1047. t14 += v * b2;
  1048. t15 += v * b3;
  1049. t16 += v * b4;
  1050. t17 += v * b5;
  1051. t18 += v * b6;
  1052. t19 += v * b7;
  1053. t20 += v * b8;
  1054. t21 += v * b9;
  1055. t22 += v * b10;
  1056. t23 += v * b11;
  1057. t24 += v * b12;
  1058. t25 += v * b13;
  1059. t26 += v * b14;
  1060. t27 += v * b15;
  1061. v = a[13];
  1062. t13 += v * b0;
  1063. t14 += v * b1;
  1064. t15 += v * b2;
  1065. t16 += v * b3;
  1066. t17 += v * b4;
  1067. t18 += v * b5;
  1068. t19 += v * b6;
  1069. t20 += v * b7;
  1070. t21 += v * b8;
  1071. t22 += v * b9;
  1072. t23 += v * b10;
  1073. t24 += v * b11;
  1074. t25 += v * b12;
  1075. t26 += v * b13;
  1076. t27 += v * b14;
  1077. t28 += v * b15;
  1078. v = a[14];
  1079. t14 += v * b0;
  1080. t15 += v * b1;
  1081. t16 += v * b2;
  1082. t17 += v * b3;
  1083. t18 += v * b4;
  1084. t19 += v * b5;
  1085. t20 += v * b6;
  1086. t21 += v * b7;
  1087. t22 += v * b8;
  1088. t23 += v * b9;
  1089. t24 += v * b10;
  1090. t25 += v * b11;
  1091. t26 += v * b12;
  1092. t27 += v * b13;
  1093. t28 += v * b14;
  1094. t29 += v * b15;
  1095. v = a[15];
  1096. t15 += v * b0;
  1097. t16 += v * b1;
  1098. t17 += v * b2;
  1099. t18 += v * b3;
  1100. t19 += v * b4;
  1101. t20 += v * b5;
  1102. t21 += v * b6;
  1103. t22 += v * b7;
  1104. t23 += v * b8;
  1105. t24 += v * b9;
  1106. t25 += v * b10;
  1107. t26 += v * b11;
  1108. t27 += v * b12;
  1109. t28 += v * b13;
  1110. t29 += v * b14;
  1111. t30 += v * b15;
  1112. t0 += 38 * t16;
  1113. t1 += 38 * t17;
  1114. t2 += 38 * t18;
  1115. t3 += 38 * t19;
  1116. t4 += 38 * t20;
  1117. t5 += 38 * t21;
  1118. t6 += 38 * t22;
  1119. t7 += 38 * t23;
  1120. t8 += 38 * t24;
  1121. t9 += 38 * t25;
  1122. t10 += 38 * t26;
  1123. t11 += 38 * t27;
  1124. t12 += 38 * t28;
  1125. t13 += 38 * t29;
  1126. t14 += 38 * t30;
  1127. // t15 left as is
  1128. // first car
  1129. c = 1;
  1130. v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
  1131. v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
  1132. v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
  1133. v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
  1134. v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
  1135. v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
  1136. v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
  1137. v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
  1138. v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
  1139. v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
  1140. v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
  1141. v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
  1142. v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
  1143. v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
  1144. v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
  1145. v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
  1146. t0 += c-1 + 37 * (c-1);
  1147. // second car
  1148. c = 1;
  1149. v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
  1150. v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
  1151. v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
  1152. v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
  1153. v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
  1154. v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
  1155. v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
  1156. v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
  1157. v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
  1158. v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
  1159. v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
  1160. v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
  1161. v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
  1162. v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
  1163. v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
  1164. v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
  1165. t0 += c-1 + 37 * (c-1);
  1166. o[ 0] = t0;
  1167. o[ 1] = t1;
  1168. o[ 2] = t2;
  1169. o[ 3] = t3;
  1170. o[ 4] = t4;
  1171. o[ 5] = t5;
  1172. o[ 6] = t6;
  1173. o[ 7] = t7;
  1174. o[ 8] = t8;
  1175. o[ 9] = t9;
  1176. o[10] = t10;
  1177. o[11] = t11;
  1178. o[12] = t12;
  1179. o[13] = t13;
  1180. o[14] = t14;
  1181. o[15] = t15;
  1182. }
  1183. function S(o, a) {
  1184. M(o, a, a);
  1185. }
  1186. function inv25519(o, i) {
  1187. var c = gf();
  1188. var a;
  1189. for (a = 0; a < 16; a++) c[a] = i[a];
  1190. for (a = 253; a >= 0; a--) {
  1191. S(c, c);
  1192. if(a !== 2 && a !== 4) M(c, c, i);
  1193. }
  1194. for (a = 0; a < 16; a++) o[a] = c[a];
  1195. }
  1196. function pow2523(o, i) {
  1197. var c = gf();
  1198. var a;
  1199. for (a = 0; a < 16; a++) c[a] = i[a];
  1200. for (a = 250; a >= 0; a--) {
  1201. S(c, c);
  1202. if(a !== 1) M(c, c, i);
  1203. }
  1204. for (a = 0; a < 16; a++) o[a] = c[a];
  1205. }
  1206. function crypto_scalarmult(q, n, p) {
  1207. var z = new Uint8Array(32);
  1208. var x = new Float64Array(80), r, i;
  1209. var a = gf(), b = gf(), c = gf(),
  1210. d = gf(), e = gf(), f = gf();
  1211. for (i = 0; i < 31; i++) z[i] = n[i];
  1212. z[31]=(n[31]&127)|64;
  1213. z[0]&=248;
  1214. unpack25519(x,p);
  1215. for (i = 0; i < 16; i++) {
  1216. b[i]=x[i];
  1217. d[i]=a[i]=c[i]=0;
  1218. }
  1219. a[0]=d[0]=1;
  1220. for (i=254; i>=0; --i) {
  1221. r=(z[i>>>3]>>>(i&7))&1;
  1222. sel25519(a,b,r);
  1223. sel25519(c,d,r);
  1224. A(e,a,c);
  1225. Z(a,a,c);
  1226. A(c,b,d);
  1227. Z(b,b,d);
  1228. S(d,e);
  1229. S(f,a);
  1230. M(a,c,a);
  1231. M(c,b,e);
  1232. A(e,a,c);
  1233. Z(a,a,c);
  1234. S(b,a);
  1235. Z(c,d,f);
  1236. M(a,c,_121665);
  1237. A(a,a,d);
  1238. M(c,c,a);
  1239. M(a,d,f);
  1240. M(d,b,x);
  1241. S(b,e);
  1242. sel25519(a,b,r);
  1243. sel25519(c,d,r);
  1244. }
  1245. for (i = 0; i < 16; i++) {
  1246. x[i+16]=a[i];
  1247. x[i+32]=c[i];
  1248. x[i+48]=b[i];
  1249. x[i+64]=d[i];
  1250. }
  1251. var x32 = x.subarray(32);
  1252. var x16 = x.subarray(16);
  1253. inv25519(x32,x32);
  1254. M(x16,x16,x32);
  1255. pack25519(q,x16);
  1256. return 0;
  1257. }
  1258. function crypto_scalarmult_base(q, n) {
  1259. return crypto_scalarmult(q, n, _9);
  1260. }
  1261. function crypto_box_keypair(y, x) {
  1262. randombytes(x, 32);
  1263. return crypto_scalarmult_base(y, x);
  1264. }
  1265. function crypto_box_beforenm(k, y, x) {
  1266. var s = new Uint8Array(32);
  1267. crypto_scalarmult(s, x, y);
  1268. return crypto_core_hsalsa20(k, _0, s, sigma);
  1269. }
  1270. var crypto_box_afternm = crypto_secretbox;
  1271. var crypto_box_open_afternm = crypto_secretbox_open;
  1272. function crypto_box(c, m, d, n, y, x) {
  1273. var k = new Uint8Array(32);
  1274. crypto_box_beforenm(k, y, x);
  1275. return crypto_box_afternm(c, m, d, n, k);
  1276. }
  1277. function crypto_box_open(m, c, d, n, y, x) {
  1278. var k = new Uint8Array(32);
  1279. crypto_box_beforenm(k, y, x);
  1280. return crypto_box_open_afternm(m, c, d, n, k);
  1281. }
  1282. var K = [
  1283. 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
  1284. 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
  1285. 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
  1286. 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
  1287. 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
  1288. 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
  1289. 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
  1290. 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
  1291. 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
  1292. 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
  1293. 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
  1294. 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
  1295. 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
  1296. 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
  1297. 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
  1298. 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
  1299. 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
  1300. 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
  1301. 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
  1302. 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
  1303. 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
  1304. 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
  1305. 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
  1306. 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
  1307. 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
  1308. 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
  1309. 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
  1310. 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
  1311. 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
  1312. 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
  1313. 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
  1314. 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
  1315. 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
  1316. 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
  1317. 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
  1318. 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
  1319. 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
  1320. 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
  1321. 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
  1322. 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
  1323. ];
  1324. function crypto_hashblocks_hl(hh, hl, m, n) {
  1325. var wh = new Int32Array(16), wl = new Int32Array(16),
  1326. bh0, bh1, bh2, bh3, bh4, bh5, bh6, bh7,
  1327. bl0, bl1, bl2, bl3, bl4, bl5, bl6, bl7,
  1328. th, tl, i, j, h, l, a, b, c, d;
  1329. var ah0 = hh[0],
  1330. ah1 = hh[1],
  1331. ah2 = hh[2],
  1332. ah3 = hh[3],
  1333. ah4 = hh[4],
  1334. ah5 = hh[5],
  1335. ah6 = hh[6],
  1336. ah7 = hh[7],
  1337. al0 = hl[0],
  1338. al1 = hl[1],
  1339. al2 = hl[2],
  1340. al3 = hl[3],
  1341. al4 = hl[4],
  1342. al5 = hl[5],
  1343. al6 = hl[6],
  1344. al7 = hl[7];
  1345. var pos = 0;
  1346. while (n >= 128) {
  1347. for (i = 0; i < 16; i++) {
  1348. j = 8 * i + pos;
  1349. wh[i] = (m[j+0] << 24) | (m[j+1] << 16) | (m[j+2] << 8) | m[j+3];
  1350. wl[i] = (m[j+4] << 24) | (m[j+5] << 16) | (m[j+6] << 8) | m[j+7];
  1351. }
  1352. for (i = 0; i < 80; i++) {
  1353. bh0 = ah0;
  1354. bh1 = ah1;
  1355. bh2 = ah2;
  1356. bh3 = ah3;
  1357. bh4 = ah4;
  1358. bh5 = ah5;
  1359. bh6 = ah6;
  1360. bh7 = ah7;
  1361. bl0 = al0;
  1362. bl1 = al1;
  1363. bl2 = al2;
  1364. bl3 = al3;
  1365. bl4 = al4;
  1366. bl5 = al5;
  1367. bl6 = al6;
  1368. bl7 = al7;
  1369. // add
  1370. h = ah7;
  1371. l = al7;
  1372. a = l & 0xffff; b = l >>> 16;
  1373. c = h & 0xffff; d = h >>> 16;
  1374. // Sigma1
  1375. h = ((ah4 >>> 14) | (al4 << (32-14))) ^ ((ah4 >>> 18) | (al4 << (32-18))) ^ ((al4 >>> (41-32)) | (ah4 << (32-(41-32))));
  1376. l = ((al4 >>> 14) | (ah4 << (32-14))) ^ ((al4 >>> 18) | (ah4 << (32-18))) ^ ((ah4 >>> (41-32)) | (al4 << (32-(41-32))));
  1377. a += l & 0xffff; b += l >>> 16;
  1378. c += h & 0xffff; d += h >>> 16;
  1379. // Ch
  1380. h = (ah4 & ah5) ^ (~ah4 & ah6);
  1381. l = (al4 & al5) ^ (~al4 & al6);
  1382. a += l & 0xffff; b += l >>> 16;
  1383. c += h & 0xffff; d += h >>> 16;
  1384. // K
  1385. h = K[i*2];
  1386. l = K[i*2+1];
  1387. a += l & 0xffff; b += l >>> 16;
  1388. c += h & 0xffff; d += h >>> 16;
  1389. // w
  1390. h = wh[i%16];
  1391. l = wl[i%16];
  1392. a += l & 0xffff; b += l >>> 16;
  1393. c += h & 0xffff; d += h >>> 16;
  1394. b += a >>> 16;
  1395. c += b >>> 16;
  1396. d += c >>> 16;
  1397. th = c & 0xffff | d << 16;
  1398. tl = a & 0xffff | b << 16;
  1399. // add
  1400. h = th;
  1401. l = tl;
  1402. a = l & 0xffff; b = l >>> 16;
  1403. c = h & 0xffff; d = h >>> 16;
  1404. // Sigma0
  1405. h = ((ah0 >>> 28) | (al0 << (32-28))) ^ ((al0 >>> (34-32)) | (ah0 << (32-(34-32)))) ^ ((al0 >>> (39-32)) | (ah0 << (32-(39-32))));
  1406. l = ((al0 >>> 28) | (ah0 << (32-28))) ^ ((ah0 >>> (34-32)) | (al0 << (32-(34-32)))) ^ ((ah0 >>> (39-32)) | (al0 << (32-(39-32))));
  1407. a += l & 0xffff; b += l >>> 16;
  1408. c += h & 0xffff; d += h >>> 16;
  1409. // Maj
  1410. h = (ah0 & ah1) ^ (ah0 & ah2) ^ (ah1 & ah2);
  1411. l = (al0 & al1) ^ (al0 & al2) ^ (al1 & al2);
  1412. a += l & 0xffff; b += l >>> 16;
  1413. c += h & 0xffff; d += h >>> 16;
  1414. b += a >>> 16;
  1415. c += b >>> 16;
  1416. d += c >>> 16;
  1417. bh7 = (c & 0xffff) | (d << 16);
  1418. bl7 = (a & 0xffff) | (b << 16);
  1419. // add
  1420. h = bh3;
  1421. l = bl3;
  1422. a = l & 0xffff; b = l >>> 16;
  1423. c = h & 0xffff; d = h >>> 16;
  1424. h = th;
  1425. l = tl;
  1426. a += l & 0xffff; b += l >>> 16;
  1427. c += h & 0xffff; d += h >>> 16;
  1428. b += a >>> 16;
  1429. c += b >>> 16;
  1430. d += c >>> 16;
  1431. bh3 = (c & 0xffff) | (d << 16);
  1432. bl3 = (a & 0xffff) | (b << 16);
  1433. ah1 = bh0;
  1434. ah2 = bh1;
  1435. ah3 = bh2;
  1436. ah4 = bh3;
  1437. ah5 = bh4;
  1438. ah6 = bh5;
  1439. ah7 = bh6;
  1440. ah0 = bh7;
  1441. al1 = bl0;
  1442. al2 = bl1;
  1443. al3 = bl2;
  1444. al4 = bl3;
  1445. al5 = bl4;
  1446. al6 = bl5;
  1447. al7 = bl6;
  1448. al0 = bl7;
  1449. if (i%16 === 15) {
  1450. for (j = 0; j < 16; j++) {
  1451. // add
  1452. h = wh[j];
  1453. l = wl[j];
  1454. a = l & 0xffff; b = l >>> 16;
  1455. c = h & 0xffff; d = h >>> 16;
  1456. h = wh[(j+9)%16];
  1457. l = wl[(j+9)%16];
  1458. a += l & 0xffff; b += l >>> 16;
  1459. c += h & 0xffff; d += h >>> 16;
  1460. // sigma0
  1461. th = wh[(j+1)%16];
  1462. tl = wl[(j+1)%16];
  1463. h = ((th >>> 1) | (tl << (32-1))) ^ ((th >>> 8) | (tl << (32-8))) ^ (th >>> 7);
  1464. l = ((tl >>> 1) | (th << (32-1))) ^ ((tl >>> 8) | (th << (32-8))) ^ ((tl >>> 7) | (th << (32-7)));
  1465. a += l & 0xffff; b += l >>> 16;
  1466. c += h & 0xffff; d += h >>> 16;
  1467. // sigma1
  1468. th = wh[(j+14)%16];
  1469. tl = wl[(j+14)%16];
  1470. h = ((th >>> 19) | (tl << (32-19))) ^ ((tl >>> (61-32)) | (th << (32-(61-32)))) ^ (th >>> 6);
  1471. l = ((tl >>> 19) | (th << (32-19))) ^ ((th >>> (61-32)) | (tl << (32-(61-32)))) ^ ((tl >>> 6) | (th << (32-6)));
  1472. a += l & 0xffff; b += l >>> 16;
  1473. c += h & 0xffff; d += h >>> 16;
  1474. b += a >>> 16;
  1475. c += b >>> 16;
  1476. d += c >>> 16;
  1477. wh[j] = (c & 0xffff) | (d << 16);
  1478. wl[j] = (a & 0xffff) | (b << 16);
  1479. }
  1480. }
  1481. }
  1482. // add
  1483. h = ah0;
  1484. l = al0;
  1485. a = l & 0xffff; b = l >>> 16;
  1486. c = h & 0xffff; d = h >>> 16;
  1487. h = hh[0];
  1488. l = hl[0];
  1489. a += l & 0xffff; b += l >>> 16;
  1490. c += h & 0xffff; d += h >>> 16;
  1491. b += a >>> 16;
  1492. c += b >>> 16;
  1493. d += c >>> 16;
  1494. hh[0] = ah0 = (c & 0xffff) | (d << 16);
  1495. hl[0] = al0 = (a & 0xffff) | (b << 16);
  1496. h = ah1;
  1497. l = al1;
  1498. a = l & 0xffff; b = l >>> 16;
  1499. c = h & 0xffff; d = h >>> 16;
  1500. h = hh[1];
  1501. l = hl[1];
  1502. a += l & 0xffff; b += l >>> 16;
  1503. c += h & 0xffff; d += h >>> 16;
  1504. b += a >>> 16;
  1505. c += b >>> 16;
  1506. d += c >>> 16;
  1507. hh[1] = ah1 = (c & 0xffff) | (d << 16);
  1508. hl[1] = al1 = (a & 0xffff) | (b << 16);
  1509. h = ah2;
  1510. l = al2;
  1511. a = l & 0xffff; b = l >>> 16;
  1512. c = h & 0xffff; d = h >>> 16;
  1513. h = hh[2];
  1514. l = hl[2];
  1515. a += l & 0xffff; b += l >>> 16;
  1516. c += h & 0xffff; d += h >>> 16;
  1517. b += a >>> 16;
  1518. c += b >>> 16;
  1519. d += c >>> 16;
  1520. hh[2] = ah2 = (c & 0xffff) | (d << 16);
  1521. hl[2] = al2 = (a & 0xffff) | (b << 16);
  1522. h = ah3;
  1523. l = al3;
  1524. a = l & 0xffff; b = l >>> 16;
  1525. c = h & 0xffff; d = h >>> 16;
  1526. h = hh[3];
  1527. l = hl[3];
  1528. a += l & 0xffff; b += l >>> 16;
  1529. c += h & 0xffff; d += h >>> 16;
  1530. b += a >>> 16;
  1531. c += b >>> 16;
  1532. d += c >>> 16;
  1533. hh[3] = ah3 = (c & 0xffff) | (d << 16);
  1534. hl[3] = al3 = (a & 0xffff) | (b << 16);
  1535. h = ah4;
  1536. l = al4;
  1537. a = l & 0xffff; b = l >>> 16;
  1538. c = h & 0xffff; d = h >>> 16;
  1539. h = hh[4];
  1540. l = hl[4];
  1541. a += l & 0xffff; b += l >>> 16;
  1542. c += h & 0xffff; d += h >>> 16;
  1543. b += a >>> 16;
  1544. c += b >>> 16;
  1545. d += c >>> 16;
  1546. hh[4] = ah4 = (c & 0xffff) | (d << 16);
  1547. hl[4] = al4 = (a & 0xffff) | (b << 16);
  1548. h = ah5;
  1549. l = al5;
  1550. a = l & 0xffff; b = l >>> 16;
  1551. c = h & 0xffff; d = h >>> 16;
  1552. h = hh[5];
  1553. l = hl[5];
  1554. a += l & 0xffff; b += l >>> 16;
  1555. c += h & 0xffff; d += h >>> 16;
  1556. b += a >>> 16;
  1557. c += b >>> 16;
  1558. d += c >>> 16;
  1559. hh[5] = ah5 = (c & 0xffff) | (d << 16);
  1560. hl[5] = al5 = (a & 0xffff) | (b << 16);
  1561. h = ah6;
  1562. l = al6;
  1563. a = l & 0xffff; b = l >>> 16;
  1564. c = h & 0xffff; d = h >>> 16;
  1565. h = hh[6];
  1566. l = hl[6];
  1567. a += l & 0xffff; b += l >>> 16;
  1568. c += h & 0xffff; d += h >>> 16;
  1569. b += a >>> 16;
  1570. c += b >>> 16;
  1571. d += c >>> 16;
  1572. hh[6] = ah6 = (c & 0xffff) | (d << 16);
  1573. hl[6] = al6 = (a & 0xffff) | (b << 16);
  1574. h = ah7;
  1575. l = al7;
  1576. a = l & 0xffff; b = l >>> 16;
  1577. c = h & 0xffff; d = h >>> 16;
  1578. h = hh[7];
  1579. l = hl[7];
  1580. a += l & 0xffff; b += l >>> 16;
  1581. c += h & 0xffff; d += h >>> 16;
  1582. b += a >>> 16;
  1583. c += b >>> 16;
  1584. d += c >>> 16;
  1585. hh[7] = ah7 = (c & 0xffff) | (d << 16);
  1586. hl[7] = al7 = (a & 0xffff) | (b << 16);
  1587. pos += 128;
  1588. n -= 128;
  1589. }
  1590. return n;
  1591. }
  1592. function crypto_hash(out, m, n) {
  1593. var hh = new Int32Array(8),
  1594. hl = new Int32Array(8),
  1595. x = new Uint8Array(256),
  1596. i, b = n;
  1597. hh[0] = 0x6a09e667;
  1598. hh[1] = 0xbb67ae85;
  1599. hh[2] = 0x3c6ef372;
  1600. hh[3] = 0xa54ff53a;
  1601. hh[4] = 0x510e527f;
  1602. hh[5] = 0x9b05688c;
  1603. hh[6] = 0x1f83d9ab;
  1604. hh[7] = 0x5be0cd19;
  1605. hl[0] = 0xf3bcc908;
  1606. hl[1] = 0x84caa73b;
  1607. hl[2] = 0xfe94f82b;
  1608. hl[3] = 0x5f1d36f1;
  1609. hl[4] = 0xade682d1;
  1610. hl[5] = 0x2b3e6c1f;
  1611. hl[6] = 0xfb41bd6b;
  1612. hl[7] = 0x137e2179;
  1613. crypto_hashblocks_hl(hh, hl, m, n);
  1614. n %= 128;
  1615. for (i = 0; i < n; i++) x[i] = m[b-n+i];
  1616. x[n] = 128;
  1617. n = 256-128*(n<112?1:0);
  1618. x[n-9] = 0;
  1619. ts64(x, n-8, (b / 0x20000000) | 0, b << 3);
  1620. crypto_hashblocks_hl(hh, hl, x, n);
  1621. for (i = 0; i < 8; i++) ts64(out, 8*i, hh[i], hl[i]);
  1622. return 0;
  1623. }
  1624. function add(p, q) {
  1625. var a = gf(), b = gf(), c = gf(),
  1626. d = gf(), e = gf(), f = gf(),
  1627. g = gf(), h = gf(), t = gf();
  1628. Z(a, p[1], p[0]);
  1629. Z(t, q[1], q[0]);
  1630. M(a, a, t);
  1631. A(b, p[0], p[1]);
  1632. A(t, q[0], q[1]);
  1633. M(b, b, t);
  1634. M(c, p[3], q[3]);
  1635. M(c, c, D2);
  1636. M(d, p[2], q[2]);
  1637. A(d, d, d);
  1638. Z(e, b, a);
  1639. Z(f, d, c);
  1640. A(g, d, c);
  1641. A(h, b, a);
  1642. M(p[0], e, f);
  1643. M(p[1], h, g);
  1644. M(p[2], g, f);
  1645. M(p[3], e, h);
  1646. }
  1647. function cswap(p, q, b) {
  1648. var i;
  1649. for (i = 0; i < 4; i++) {
  1650. sel25519(p[i], q[i], b);
  1651. }
  1652. }
  1653. function pack(r, p) {
  1654. var tx = gf(), ty = gf(), zi = gf();
  1655. inv25519(zi, p[2]);
  1656. M(tx, p[0], zi);
  1657. M(ty, p[1], zi);
  1658. pack25519(r, ty);
  1659. r[31] ^= par25519(tx) << 7;
  1660. }
  1661. function scalarmult(p, q, s) {
  1662. var b, i;
  1663. set25519(p[0], gf0);
  1664. set25519(p[1], gf1);
  1665. set25519(p[2], gf1);
  1666. set25519(p[3], gf0);
  1667. for (i = 255; i >= 0; --i) {
  1668. b = (s[(i/8)|0] >> (i&7)) & 1;
  1669. cswap(p, q, b);
  1670. add(q, p);
  1671. add(p, p);
  1672. cswap(p, q, b);
  1673. }
  1674. }
  1675. function scalarbase(p, s) {
  1676. var q = [gf(), gf(), gf(), gf()];
  1677. set25519(q[0], X);
  1678. set25519(q[1], Y);
  1679. set25519(q[2], gf1);
  1680. M(q[3], X, Y);
  1681. scalarmult(p, q, s);
  1682. }
  1683. function crypto_sign_keypair(pk, sk, seeded) {
  1684. var d = new Uint8Array(64);
  1685. var p = [gf(), gf(), gf(), gf()];
  1686. var i;
  1687. if (!seeded) randombytes(sk, 32);
  1688. crypto_hash(d, sk, 32);
  1689. d[0] &= 248;
  1690. d[31] &= 127;
  1691. d[31] |= 64;
  1692. scalarbase(p, d);
  1693. pack(pk, p);
  1694. for (i = 0; i < 32; i++) sk[i+32] = pk[i];
  1695. return 0;
  1696. }
  1697. var L = new Float64Array([0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10]);
  1698. function modL(r, x) {
  1699. var carry, i, j, k;
  1700. for (i = 63; i >= 32; --i) {
  1701. carry = 0;
  1702. for (j = i - 32, k = i - 12; j < k; ++j) {
  1703. x[j] += carry - 16 * x[i] * L[j - (i - 32)];
  1704. carry = (x[j] + 128) >> 8;
  1705. x[j] -= carry * 256;
  1706. }
  1707. x[j] += carry;
  1708. x[i] = 0;
  1709. }
  1710. carry = 0;
  1711. for (j = 0; j < 32; j++) {
  1712. x[j] += carry - (x[31] >> 4) * L[j];
  1713. carry = x[j] >> 8;
  1714. x[j] &= 255;
  1715. }
  1716. for (j = 0; j < 32; j++) x[j] -= carry * L[j];
  1717. for (i = 0; i < 32; i++) {
  1718. x[i+1] += x[i] >> 8;
  1719. r[i] = x[i] & 255;
  1720. }
  1721. }
  1722. function reduce(r) {
  1723. var x = new Float64Array(64), i;
  1724. for (i = 0; i < 64; i++) x[i] = r[i];
  1725. for (i = 0; i < 64; i++) r[i] = 0;
  1726. modL(r, x);
  1727. }
  1728. // Note: difference from C - smlen returned, not passed as argument.
  1729. function crypto_sign(sm, m, n, sk) {
  1730. var d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64);
  1731. var i, j, x = new Float64Array(64);
  1732. var p = [gf(), gf(), gf(), gf()];
  1733. crypto_hash(d, sk, 32);
  1734. d[0] &= 248;
  1735. d[31] &= 127;
  1736. d[31] |= 64;
  1737. var smlen = n + 64;
  1738. for (i = 0; i < n; i++) sm[64 + i] = m[i];
  1739. for (i = 0; i < 32; i++) sm[32 + i] = d[32 + i];
  1740. crypto_hash(r, sm.subarray(32), n+32);
  1741. reduce(r);
  1742. scalarbase(p, r);
  1743. pack(sm, p);
  1744. for (i = 32; i < 64; i++) sm[i] = sk[i];
  1745. crypto_hash(h, sm, n + 64);
  1746. reduce(h);
  1747. for (i = 0; i < 64; i++) x[i] = 0;
  1748. for (i = 0; i < 32; i++) x[i] = r[i];
  1749. for (i = 0; i < 32; i++) {
  1750. for (j = 0; j < 32; j++) {
  1751. x[i+j] += h[i] * d[j];
  1752. }
  1753. }
  1754. modL(sm.subarray(32), x);
  1755. return smlen;
  1756. }
  1757. function unpackneg(r, p) {
  1758. var t = gf(), chk = gf(), num = gf(),
  1759. den = gf(), den2 = gf(), den4 = gf(),
  1760. den6 = gf();
  1761. set25519(r[2], gf1);
  1762. unpack25519(r[1], p);
  1763. S(num, r[1]);
  1764. M(den, num, D);
  1765. Z(num, num, r[2]);
  1766. A(den, r[2], den);
  1767. S(den2, den);
  1768. S(den4, den2);
  1769. M(den6, den4, den2);
  1770. M(t, den6, num);
  1771. M(t, t, den);
  1772. pow2523(t, t);
  1773. M(t, t, num);
  1774. M(t, t, den);
  1775. M(t, t, den);
  1776. M(r[0], t, den);
  1777. S(chk, r[0]);
  1778. M(chk, chk, den);
  1779. if (neq25519(chk, num)) M(r[0], r[0], I);
  1780. S(chk, r[0]);
  1781. M(chk, chk, den);
  1782. if (neq25519(chk, num)) return -1;
  1783. if (par25519(r[0]) === (p[31]>>7)) Z(r[0], gf0, r[0]);
  1784. M(r[3], r[0], r[1]);
  1785. return 0;
  1786. }
  1787. function crypto_sign_open(m, sm, n, pk) {
  1788. var i, mlen;
  1789. var t = new Uint8Array(32), h = new Uint8Array(64);
  1790. var p = [gf(), gf(), gf(), gf()],
  1791. q = [gf(), gf(), gf(), gf()];
  1792. mlen = -1;
  1793. if (n < 64) return -1;
  1794. if (unpackneg(q, pk)) return -1;
  1795. for (i = 0; i < n; i++) m[i] = sm[i];
  1796. for (i = 0; i < 32; i++) m[i+32] = pk[i];
  1797. crypto_hash(h, m, n);
  1798. reduce(h);
  1799. scalarmult(p, q, h);
  1800. scalarbase(q, sm.subarray(32));
  1801. add(p, q);
  1802. pack(t, p);
  1803. n -= 64;
  1804. if (crypto_verify_32(sm, 0, t, 0)) {
  1805. for (i = 0; i < n; i++) m[i] = 0;
  1806. return -1;
  1807. }
  1808. for (i = 0; i < n; i++) m[i] = sm[i + 64];
  1809. mlen = n;
  1810. return mlen;
  1811. }
  1812. var crypto_secretbox_KEYBYTES = 32,
  1813. crypto_secretbox_NONCEBYTES = 24,
  1814. crypto_secretbox_ZEROBYTES = 32,
  1815. crypto_secretbox_BOXZEROBYTES = 16,
  1816. crypto_scalarmult_BYTES = 32,
  1817. crypto_scalarmult_SCALARBYTES = 32,
  1818. crypto_box_PUBLICKEYBYTES = 32,
  1819. crypto_box_SECRETKEYBYTES = 32,
  1820. crypto_box_BEFORENMBYTES = 32,
  1821. crypto_box_NONCEBYTES = crypto_secretbox_NONCEBYTES,
  1822. crypto_box_ZEROBYTES = crypto_secretbox_ZEROBYTES,
  1823. crypto_box_BOXZEROBYTES = crypto_secretbox_BOXZEROBYTES,
  1824. crypto_sign_BYTES = 64,
  1825. crypto_sign_PUBLICKEYBYTES = 32,
  1826. crypto_sign_SECRETKEYBYTES = 64,
  1827. crypto_sign_SEEDBYTES = 32,
  1828. crypto_hash_BYTES = 64;
  1829. nacl.lowlevel = {
  1830. crypto_core_hsalsa20: crypto_core_hsalsa20,
  1831. crypto_stream_xor: crypto_stream_xor,
  1832. crypto_stream: crypto_stream,
  1833. crypto_stream_salsa20_xor: crypto_stream_salsa20_xor,
  1834. crypto_stream_salsa20: crypto_stream_salsa20,
  1835. crypto_onetimeauth: crypto_onetimeauth,
  1836. crypto_onetimeauth_verify: crypto_onetimeauth_verify,
  1837. crypto_verify_16: crypto_verify_16,
  1838. crypto_verify_32: crypto_verify_32,
  1839. crypto_secretbox: crypto_secretbox,
  1840. crypto_secretbox_open: crypto_secretbox_open,
  1841. crypto_scalarmult: crypto_scalarmult,
  1842. crypto_scalarmult_base: crypto_scalarmult_base,
  1843. crypto_box_beforenm: crypto_box_beforenm,
  1844. crypto_box_afternm: crypto_box_afternm,
  1845. crypto_box: crypto_box,
  1846. crypto_box_open: crypto_box_open,
  1847. crypto_box_keypair: crypto_box_keypair,
  1848. crypto_hash: crypto_hash,
  1849. crypto_sign: crypto_sign,
  1850. crypto_sign_keypair: crypto_sign_keypair,
  1851. crypto_sign_open: crypto_sign_open,
  1852. crypto_secretbox_KEYBYTES: crypto_secretbox_KEYBYTES,
  1853. crypto_secretbox_NONCEBYTES: crypto_secretbox_NONCEBYTES,
  1854. crypto_secretbox_ZEROBYTES: crypto_secretbox_ZEROBYTES,
  1855. crypto_secretbox_BOXZEROBYTES: crypto_secretbox_BOXZEROBYTES,
  1856. crypto_scalarmult_BYTES: crypto_scalarmult_BYTES,
  1857. crypto_scalarmult_SCALARBYTES: crypto_scalarmult_SCALARBYTES,
  1858. crypto_box_PUBLICKEYBYTES: crypto_box_PUBLICKEYBYTES,
  1859. crypto_box_SECRETKEYBYTES: crypto_box_SECRETKEYBYTES,
  1860. crypto_box_BEFORENMBYTES: crypto_box_BEFORENMBYTES,
  1861. crypto_box_NONCEBYTES: crypto_box_NONCEBYTES,
  1862. crypto_box_ZEROBYTES: crypto_box_ZEROBYTES,
  1863. crypto_box_BOXZEROBYTES: crypto_box_BOXZEROBYTES,
  1864. crypto_sign_BYTES: crypto_sign_BYTES,
  1865. crypto_sign_PUBLICKEYBYTES: crypto_sign_PUBLICKEYBYTES,
  1866. crypto_sign_SECRETKEYBYTES: crypto_sign_SECRETKEYBYTES,
  1867. crypto_sign_SEEDBYTES: crypto_sign_SEEDBYTES,
  1868. crypto_hash_BYTES: crypto_hash_BYTES
  1869. };
  1870. /* High-level API */
  1871. function checkLengths(k, n) {
  1872. if (k.length !== crypto_secretbox_KEYBYTES) throw new Error('bad key size');
  1873. if (n.length !== crypto_secretbox_NONCEBYTES) throw new Error('bad nonce size');
  1874. }
  1875. function checkBoxLengths(pk, sk) {
  1876. if (pk.length !== crypto_box_PUBLICKEYBYTES) throw new Error('bad public key size');
  1877. if (sk.length !== crypto_box_SECRETKEYBYTES) throw new Error('bad secret key size');
  1878. }
  1879. function checkArrayTypes() {
  1880. var t, i;
  1881. for (i = 0; i < arguments.length; i++) {
  1882. if ((t = Object.prototype.toString.call(arguments[i])) !== '[object Uint8Array]')
  1883. throw new TypeError('unexpected type ' + t + ', use Uint8Array');
  1884. }
  1885. }
  1886. function cleanup(arr) {
  1887. for (var i = 0; i < arr.length; i++) arr[i] = 0;
  1888. }
  1889. // TODO: Completely remove this in v0.15.
  1890. if (!nacl.util) {
  1891. nacl.util = {};
  1892. nacl.util.decodeUTF8 = nacl.util.encodeUTF8 = nacl.util.encodeBase64 = nacl.util.decodeBase64 = function() {
  1893. throw new Error('nacl.util moved into separate package: https://github.com/dchest/tweetnacl-util-js');
  1894. };
  1895. }
  1896. nacl.randomBytes = function(n) {
  1897. var b = new Uint8Array(n);
  1898. randombytes(b, n);
  1899. return b;
  1900. };
  1901. nacl.secretbox = function(msg, nonce, key) {
  1902. checkArrayTypes(msg, nonce, key);
  1903. checkLengths(key, nonce);
  1904. var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);
  1905. var c = new Uint8Array(m.length);
  1906. for (var i = 0; i < msg.length; i++) m[i+crypto_secretbox_ZEROBYTES] = msg[i];
  1907. crypto_secretbox(c, m, m.length, nonce, key);
  1908. return c.subarray(crypto_secretbox_BOXZEROBYTES);
  1909. };
  1910. nacl.secretbox.open = function(box, nonce, key) {
  1911. checkArrayTypes(box, nonce, key);
  1912. checkLengths(key, nonce);
  1913. var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);
  1914. var m = new Uint8Array(c.length);
  1915. for (var i = 0; i < box.length; i++) c[i+crypto_secretbox_BOXZEROBYTES] = box[i];
  1916. if (c.length < 32) return false;
  1917. if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0) return false;
  1918. return m.subarray(crypto_secretbox_ZEROBYTES);
  1919. };
  1920. nacl.secretbox.keyLength = crypto_secretbox_KEYBYTES;
  1921. nacl.secretbox.nonceLength = crypto_secretbox_NONCEBYTES;
  1922. nacl.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;
  1923. nacl.scalarMult = function(n, p) {
  1924. checkArrayTypes(n, p);
  1925. if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');
  1926. if (p.length !== crypto_scalarmult_BYTES) throw new Error('bad p size');
  1927. var q = new Uint8Array(crypto_scalarmult_BYTES);
  1928. crypto_scalarmult(q, n, p);
  1929. return q;
  1930. };
  1931. nacl.scalarMult.base = function(n) {
  1932. checkArrayTypes(n);
  1933. if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');
  1934. var q = new Uint8Array(crypto_scalarmult_BYTES);
  1935. crypto_scalarmult_base(q, n);
  1936. return q;
  1937. };
  1938. nacl.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;
  1939. nacl.scalarMult.groupElementLength = crypto_scalarmult_BYTES;
  1940. nacl.box = function(msg, nonce, publicKey, secretKey) {
  1941. var k = nacl.box.before(publicKey, secretKey);
  1942. return nacl.secretbox(msg, nonce, k);
  1943. };
  1944. nacl.box.before = function(publicKey, secretKey) {
  1945. checkArrayTypes(publicKey, secretKey);
  1946. checkBoxLengths(publicKey, secretKey);
  1947. var k = new Uint8Array(crypto_box_BEFORENMBYTES);
  1948. crypto_box_beforenm(k, publicKey, secretKey);
  1949. return k;
  1950. };
  1951. nacl.box.after = nacl.secretbox;
  1952. nacl.box.open = function(msg, nonce, publicKey, secretKey) {
  1953. var k = nacl.box.before(publicKey, secretKey);
  1954. return nacl.secretbox.open(msg, nonce, k);
  1955. };
  1956. nacl.box.open.after = nacl.secretbox.open;
  1957. nacl.box.keyPair = function() {
  1958. var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
  1959. var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
  1960. crypto_box_keypair(pk, sk);
  1961. return {publicKey: pk, secretKey: sk};
  1962. };
  1963. nacl.box.keyPair.fromSecretKey = function(secretKey) {
  1964. checkArrayTypes(secretKey);
  1965. if (secretKey.length !== crypto_box_SECRETKEYBYTES)
  1966. throw new Error('bad secret key size');
  1967. var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
  1968. crypto_scalarmult_base(pk, secretKey);
  1969. return {publicKey: pk, secretKey: new Uint8Array(secretKey)};
  1970. };
  1971. nacl.box.publicKeyLength = crypto_box_PUBLICKEYBYTES;
  1972. nacl.box.secretKeyLength = crypto_box_SECRETKEYBYTES;
  1973. nacl.box.sharedKeyLength = crypto_box_BEFORENMBYTES;
  1974. nacl.box.nonceLength = crypto_box_NONCEBYTES;
  1975. nacl.box.overheadLength = nacl.secretbox.overheadLength;
  1976. nacl.sign = function(msg, secretKey) {
  1977. checkArrayTypes(msg, secretKey);
  1978. if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
  1979. throw new Error('bad secret key size');
  1980. var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length);
  1981. crypto_sign(signedMsg, msg, msg.length, secretKey);
  1982. return signedMsg;
  1983. };
  1984. nacl.sign.open = function(signedMsg, publicKey) {
  1985. if (arguments.length !== 2)
  1986. throw new Error('nacl.sign.open accepts 2 arguments; did you mean to use nacl.sign.detached.verify?');
  1987. checkArrayTypes(signedMsg, publicKey);
  1988. if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
  1989. throw new Error('bad public key size');
  1990. var tmp = new Uint8Array(signedMsg.length);
  1991. var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);
  1992. if (mlen < 0) return null;
  1993. var m = new Uint8Array(mlen);
  1994. for (var i = 0; i < m.length; i++) m[i] = tmp[i];
  1995. return m;
  1996. };
  1997. nacl.sign.detached = function(msg, secretKey) {
  1998. var signedMsg = nacl.sign(msg, secretKey);
  1999. var sig = new Uint8Array(crypto_sign_BYTES);
  2000. for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];
  2001. return sig;
  2002. };
  2003. nacl.sign.detached.verify = function(msg, sig, publicKey) {
  2004. checkArrayTypes(msg, sig, publicKey);
  2005. if (sig.length !== crypto_sign_BYTES)
  2006. throw new Error('bad signature size');
  2007. if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
  2008. throw new Error('bad public key size');
  2009. var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
  2010. var m = new Uint8Array(crypto_sign_BYTES + msg.length);
  2011. var i;
  2012. for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];
  2013. for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i];
  2014. return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0);
  2015. };
  2016. nacl.sign.keyPair = function() {
  2017. var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
  2018. var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
  2019. crypto_sign_keypair(pk, sk);
  2020. return {publicKey: pk, secretKey: sk};
  2021. };
  2022. nacl.sign.keyPair.fromSecretKey = function(secretKey) {
  2023. checkArrayTypes(secretKey);
  2024. if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
  2025. throw new Error('bad secret key size');
  2026. var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
  2027. for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32+i];
  2028. return {publicKey: pk, secretKey: new Uint8Array(secretKey)};
  2029. };
  2030. nacl.sign.keyPair.fromSeed = function(seed) {
  2031. checkArrayTypes(seed);
  2032. if (seed.length !== crypto_sign_SEEDBYTES)
  2033. throw new Error('bad seed size');
  2034. var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
  2035. var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
  2036. for (var i = 0; i < 32; i++) sk[i] = seed[i];
  2037. crypto_sign_keypair(pk, sk, true);
  2038. return {publicKey: pk, secretKey: sk};
  2039. };
  2040. nacl.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES;
  2041. nacl.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES;
  2042. nacl.sign.seedLength = crypto_sign_SEEDBYTES;
  2043. nacl.sign.signatureLength = crypto_sign_BYTES;
  2044. nacl.hash = function(msg) {
  2045. checkArrayTypes(msg);
  2046. var h = new Uint8Array(crypto_hash_BYTES);
  2047. crypto_hash(h, msg, msg.length);
  2048. return h;
  2049. };
  2050. nacl.hash.hashLength = crypto_hash_BYTES;
  2051. nacl.verify = function(x, y) {
  2052. checkArrayTypes(x, y);
  2053. // Zero length arguments are considered not equal.
  2054. if (x.length === 0 || y.length === 0) return false;
  2055. if (x.length !== y.length) return false;
  2056. return (vn(x, 0, y, 0, x.length) === 0) ? true : false;
  2057. };
  2058. nacl.setPRNG = function(fn) {
  2059. randombytes = fn;
  2060. };
  2061. (function() {
  2062. // Initialize PRNG if environment provides CSPRNG.
  2063. // If not, methods calling randombytes will throw.
  2064. var crypto = typeof self !== 'undefined' ? (self.crypto || self.msCrypto) : null;
  2065. if (crypto && crypto.getRandomValues) {
  2066. // Browsers.
  2067. var QUOTA = 65536;
  2068. nacl.setPRNG(function(x, n) {
  2069. var i, v = new Uint8Array(n);
  2070. for (i = 0; i < n; i += QUOTA) {
  2071. crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
  2072. }
  2073. for (i = 0; i < n; i++) x[i] = v[i];
  2074. cleanup(v);
  2075. });
  2076. } else if (typeof require !== 'undefined') {
  2077. // Node.js.
  2078. crypto = require('crypto');
  2079. if (crypto && crypto.randomBytes) {
  2080. nacl.setPRNG(function(x, n) {
  2081. var i, v = crypto.randomBytes(n);
  2082. for (i = 0; i < n; i++) x[i] = v[i];
  2083. cleanup(v);
  2084. });
  2085. }
  2086. }
  2087. })();
  2088. })(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {}));