/js/src/v8/crypto.js

http://github.com/zpao/v8monkey · JavaScript · 1698 lines · 1296 code · 174 blank · 228 comment · 414 complexity · 02c8ed568c4e611c04c84e9ed3b56f18 MD5 · raw file

  1. /*
  2. * Copyright (c) 2003-2005 Tom Wu
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  18. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  19. *
  20. * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  21. * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  22. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  23. * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  24. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  25. *
  26. * In addition, the following condition applies:
  27. *
  28. * All redistributions must retain an intact copy of this copyright notice
  29. * and disclaimer.
  30. */
  31. // The code has been adapted for use as a benchmark by Google.
  32. var Crypto = new BenchmarkSuite('Crypto', 266181, [
  33. new Benchmark("Encrypt", encrypt),
  34. new Benchmark("Decrypt", decrypt)
  35. ]);
  36. // Basic JavaScript BN library - subset useful for RSA encryption.
  37. // Bits per digit
  38. var dbits;
  39. var BI_DB;
  40. var BI_DM;
  41. var BI_DV;
  42. var BI_FP;
  43. var BI_FV;
  44. var BI_F1;
  45. var BI_F2;
  46. // JavaScript engine analysis
  47. var canary = 0xdeadbeefcafe;
  48. var j_lm = ((canary&0xffffff)==0xefcafe);
  49. // (public) Constructor
  50. function BigInteger(a,b,c) {
  51. this.array = new Array();
  52. if(a != null)
  53. if("number" == typeof a) this.fromNumber(a,b,c);
  54. else if(b == null && "string" != typeof a) this.fromString(a,256);
  55. else this.fromString(a,b);
  56. }
  57. // return new, unset BigInteger
  58. function nbi() { return new BigInteger(null); }
  59. // am: Compute w_j += (x*this_i), propagate carries,
  60. // c is initial carry, returns final carry.
  61. // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
  62. // We need to select the fastest one that works in this environment.
  63. // am1: use a single mult and divide to get the high bits,
  64. // max digit bits should be 26 because
  65. // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
  66. function am1(i,x,w,j,c,n) {
  67. var this_array = this.array;
  68. var w_array = w.array;
  69. while(--n >= 0) {
  70. var v = x*this_array[i++]+w_array[j]+c;
  71. c = Math.floor(v/0x4000000);
  72. w_array[j++] = v&0x3ffffff;
  73. }
  74. return c;
  75. }
  76. // am2 avoids a big mult-and-extract completely.
  77. // Max digit bits should be <= 30 because we do bitwise ops
  78. // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
  79. function am2(i,x,w,j,c,n) {
  80. var this_array = this.array;
  81. var w_array = w.array;
  82. var xl = x&0x7fff, xh = x>>15;
  83. while(--n >= 0) {
  84. var l = this_array[i]&0x7fff;
  85. var h = this_array[i++]>>15;
  86. var m = xh*l+h*xl;
  87. l = xl*l+((m&0x7fff)<<15)+w_array[j]+(c&0x3fffffff);
  88. c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
  89. w_array[j++] = l&0x3fffffff;
  90. }
  91. return c;
  92. }
  93. // Alternately, set max digit bits to 28 since some
  94. // browsers slow down when dealing with 32-bit numbers.
  95. function am3(i,x,w,j,c,n) {
  96. var this_array = this.array;
  97. var w_array = w.array;
  98. var xl = x&0x3fff, xh = x>>14;
  99. while(--n >= 0) {
  100. var l = this_array[i]&0x3fff;
  101. var h = this_array[i++]>>14;
  102. var m = xh*l+h*xl;
  103. l = xl*l+((m&0x3fff)<<14)+w_array[j]+c;
  104. c = (l>>28)+(m>>14)+xh*h;
  105. w_array[j++] = l&0xfffffff;
  106. }
  107. return c;
  108. }
  109. // This is tailored to VMs with 2-bit tagging. It makes sure
  110. // that all the computations stay within the 29 bits available.
  111. function am4(i,x,w,j,c,n) {
  112. var this_array = this.array;
  113. var w_array = w.array;
  114. var xl = x&0x1fff, xh = x>>13;
  115. while(--n >= 0) {
  116. var l = this_array[i]&0x1fff;
  117. var h = this_array[i++]>>13;
  118. var m = xh*l+h*xl;
  119. l = xl*l+((m&0x1fff)<<13)+w_array[j]+c;
  120. c = (l>>26)+(m>>13)+xh*h;
  121. w_array[j++] = l&0x3ffffff;
  122. }
  123. return c;
  124. }
  125. // am3/28 is best for SM, Rhino, but am4/26 is best for v8.
  126. // Kestrel (Opera 9.5) gets its best result with am4/26.
  127. // IE7 does 9% better with am3/28 than with am4/26.
  128. // Firefox (SM) gets 10% faster with am3/28 than with am4/26.
  129. setupEngine = function(fn, bits) {
  130. BigInteger.prototype.am = fn;
  131. dbits = bits;
  132. BI_DB = dbits;
  133. BI_DM = ((1<<dbits)-1);
  134. BI_DV = (1<<dbits);
  135. BI_FP = 52;
  136. BI_FV = Math.pow(2,BI_FP);
  137. BI_F1 = BI_FP-dbits;
  138. BI_F2 = 2*dbits-BI_FP;
  139. }
  140. // Digit conversions
  141. var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
  142. var BI_RC = new Array();
  143. var rr,vv;
  144. rr = "0".charCodeAt(0);
  145. for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
  146. rr = "a".charCodeAt(0);
  147. for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
  148. rr = "A".charCodeAt(0);
  149. for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
  150. function int2char(n) { return BI_RM.charAt(n); }
  151. function intAt(s,i) {
  152. var c = BI_RC[s.charCodeAt(i)];
  153. return (c==null)?-1:c;
  154. }
  155. // (protected) copy this to r
  156. function bnpCopyTo(r) {
  157. var this_array = this.array;
  158. var r_array = r.array;
  159. for(var i = this.t-1; i >= 0; --i) r_array[i] = this_array[i];
  160. r.t = this.t;
  161. r.s = this.s;
  162. }
  163. // (protected) set from integer value x, -DV <= x < DV
  164. function bnpFromInt(x) {
  165. var this_array = this.array;
  166. this.t = 1;
  167. this.s = (x<0)?-1:0;
  168. if(x > 0) this_array[0] = x;
  169. else if(x < -1) this_array[0] = x+DV;
  170. else this.t = 0;
  171. }
  172. // return bigint initialized to value
  173. function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
  174. // (protected) set from string and radix
  175. function bnpFromString(s,b) {
  176. var this_array = this.array;
  177. var k;
  178. if(b == 16) k = 4;
  179. else if(b == 8) k = 3;
  180. else if(b == 256) k = 8; // byte array
  181. else if(b == 2) k = 1;
  182. else if(b == 32) k = 5;
  183. else if(b == 4) k = 2;
  184. else { this.fromRadix(s,b); return; }
  185. this.t = 0;
  186. this.s = 0;
  187. var i = s.length, mi = false, sh = 0;
  188. while(--i >= 0) {
  189. var x = (k==8)?s[i]&0xff:intAt(s,i);
  190. if(x < 0) {
  191. if(s.charAt(i) == "-") mi = true;
  192. continue;
  193. }
  194. mi = false;
  195. if(sh == 0)
  196. this_array[this.t++] = x;
  197. else if(sh+k > BI_DB) {
  198. this_array[this.t-1] |= (x&((1<<(BI_DB-sh))-1))<<sh;
  199. this_array[this.t++] = (x>>(BI_DB-sh));
  200. }
  201. else
  202. this_array[this.t-1] |= x<<sh;
  203. sh += k;
  204. if(sh >= BI_DB) sh -= BI_DB;
  205. }
  206. if(k == 8 && (s[0]&0x80) != 0) {
  207. this.s = -1;
  208. if(sh > 0) this_array[this.t-1] |= ((1<<(BI_DB-sh))-1)<<sh;
  209. }
  210. this.clamp();
  211. if(mi) BigInteger.ZERO.subTo(this,this);
  212. }
  213. // (protected) clamp off excess high words
  214. function bnpClamp() {
  215. var this_array = this.array;
  216. var c = this.s&BI_DM;
  217. while(this.t > 0 && this_array[this.t-1] == c) --this.t;
  218. }
  219. // (public) return string representation in given radix
  220. function bnToString(b) {
  221. var this_array = this.array;
  222. if(this.s < 0) return "-"+this.negate().toString(b);
  223. var k;
  224. if(b == 16) k = 4;
  225. else if(b == 8) k = 3;
  226. else if(b == 2) k = 1;
  227. else if(b == 32) k = 5;
  228. else if(b == 4) k = 2;
  229. else return this.toRadix(b);
  230. var km = (1<<k)-1, d, m = false, r = "", i = this.t;
  231. var p = BI_DB-(i*BI_DB)%k;
  232. if(i-- > 0) {
  233. if(p < BI_DB && (d = this_array[i]>>p) > 0) { m = true; r = int2char(d); }
  234. while(i >= 0) {
  235. if(p < k) {
  236. d = (this_array[i]&((1<<p)-1))<<(k-p);
  237. d |= this_array[--i]>>(p+=BI_DB-k);
  238. }
  239. else {
  240. d = (this_array[i]>>(p-=k))&km;
  241. if(p <= 0) { p += BI_DB; --i; }
  242. }
  243. if(d > 0) m = true;
  244. if(m) r += int2char(d);
  245. }
  246. }
  247. return m?r:"0";
  248. }
  249. // (public) -this
  250. function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
  251. // (public) |this|
  252. function bnAbs() { return (this.s<0)?this.negate():this; }
  253. // (public) return + if this > a, - if this < a, 0 if equal
  254. function bnCompareTo(a) {
  255. var this_array = this.array;
  256. var a_array = a.array;
  257. var r = this.s-a.s;
  258. if(r != 0) return r;
  259. var i = this.t;
  260. r = i-a.t;
  261. if(r != 0) return r;
  262. while(--i >= 0) if((r=this_array[i]-a_array[i]) != 0) return r;
  263. return 0;
  264. }
  265. // returns bit length of the integer x
  266. function nbits(x) {
  267. var r = 1, t;
  268. if((t=x>>>16) != 0) { x = t; r += 16; }
  269. if((t=x>>8) != 0) { x = t; r += 8; }
  270. if((t=x>>4) != 0) { x = t; r += 4; }
  271. if((t=x>>2) != 0) { x = t; r += 2; }
  272. if((t=x>>1) != 0) { x = t; r += 1; }
  273. return r;
  274. }
  275. // (public) return the number of bits in "this"
  276. function bnBitLength() {
  277. var this_array = this.array;
  278. if(this.t <= 0) return 0;
  279. return BI_DB*(this.t-1)+nbits(this_array[this.t-1]^(this.s&BI_DM));
  280. }
  281. // (protected) r = this << n*DB
  282. function bnpDLShiftTo(n,r) {
  283. var this_array = this.array;
  284. var r_array = r.array;
  285. var i;
  286. for(i = this.t-1; i >= 0; --i) r_array[i+n] = this_array[i];
  287. for(i = n-1; i >= 0; --i) r_array[i] = 0;
  288. r.t = this.t+n;
  289. r.s = this.s;
  290. }
  291. // (protected) r = this >> n*DB
  292. function bnpDRShiftTo(n,r) {
  293. var this_array = this.array;
  294. var r_array = r.array;
  295. for(var i = n; i < this.t; ++i) r_array[i-n] = this_array[i];
  296. r.t = Math.max(this.t-n,0);
  297. r.s = this.s;
  298. }
  299. // (protected) r = this << n
  300. function bnpLShiftTo(n,r) {
  301. var this_array = this.array;
  302. var r_array = r.array;
  303. var bs = n%BI_DB;
  304. var cbs = BI_DB-bs;
  305. var bm = (1<<cbs)-1;
  306. var ds = Math.floor(n/BI_DB), c = (this.s<<bs)&BI_DM, i;
  307. for(i = this.t-1; i >= 0; --i) {
  308. r_array[i+ds+1] = (this_array[i]>>cbs)|c;
  309. c = (this_array[i]&bm)<<bs;
  310. }
  311. for(i = ds-1; i >= 0; --i) r_array[i] = 0;
  312. r_array[ds] = c;
  313. r.t = this.t+ds+1;
  314. r.s = this.s;
  315. r.clamp();
  316. }
  317. // (protected) r = this >> n
  318. function bnpRShiftTo(n,r) {
  319. var this_array = this.array;
  320. var r_array = r.array;
  321. r.s = this.s;
  322. var ds = Math.floor(n/BI_DB);
  323. if(ds >= this.t) { r.t = 0; return; }
  324. var bs = n%BI_DB;
  325. var cbs = BI_DB-bs;
  326. var bm = (1<<bs)-1;
  327. r_array[0] = this_array[ds]>>bs;
  328. for(var i = ds+1; i < this.t; ++i) {
  329. r_array[i-ds-1] |= (this_array[i]&bm)<<cbs;
  330. r_array[i-ds] = this_array[i]>>bs;
  331. }
  332. if(bs > 0) r_array[this.t-ds-1] |= (this.s&bm)<<cbs;
  333. r.t = this.t-ds;
  334. r.clamp();
  335. }
  336. // (protected) r = this - a
  337. function bnpSubTo(a,r) {
  338. var this_array = this.array;
  339. var r_array = r.array;
  340. var a_array = a.array;
  341. var i = 0, c = 0, m = Math.min(a.t,this.t);
  342. while(i < m) {
  343. c += this_array[i]-a_array[i];
  344. r_array[i++] = c&BI_DM;
  345. c >>= BI_DB;
  346. }
  347. if(a.t < this.t) {
  348. c -= a.s;
  349. while(i < this.t) {
  350. c += this_array[i];
  351. r_array[i++] = c&BI_DM;
  352. c >>= BI_DB;
  353. }
  354. c += this.s;
  355. }
  356. else {
  357. c += this.s;
  358. while(i < a.t) {
  359. c -= a_array[i];
  360. r_array[i++] = c&BI_DM;
  361. c >>= BI_DB;
  362. }
  363. c -= a.s;
  364. }
  365. r.s = (c<0)?-1:0;
  366. if(c < -1) r_array[i++] = BI_DV+c;
  367. else if(c > 0) r_array[i++] = c;
  368. r.t = i;
  369. r.clamp();
  370. }
  371. // (protected) r = this * a, r != this,a (HAC 14.12)
  372. // "this" should be the larger one if appropriate.
  373. function bnpMultiplyTo(a,r) {
  374. var this_array = this.array;
  375. var r_array = r.array;
  376. var x = this.abs(), y = a.abs();
  377. var y_array = y.array;
  378. var i = x.t;
  379. r.t = i+y.t;
  380. while(--i >= 0) r_array[i] = 0;
  381. for(i = 0; i < y.t; ++i) r_array[i+x.t] = x.am(0,y_array[i],r,i,0,x.t);
  382. r.s = 0;
  383. r.clamp();
  384. if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
  385. }
  386. // (protected) r = this^2, r != this (HAC 14.16)
  387. function bnpSquareTo(r) {
  388. var x = this.abs();
  389. var x_array = x.array;
  390. var r_array = r.array;
  391. var i = r.t = 2*x.t;
  392. while(--i >= 0) r_array[i] = 0;
  393. for(i = 0; i < x.t-1; ++i) {
  394. var c = x.am(i,x_array[i],r,2*i,0,1);
  395. if((r_array[i+x.t]+=x.am(i+1,2*x_array[i],r,2*i+1,c,x.t-i-1)) >= BI_DV) {
  396. r_array[i+x.t] -= BI_DV;
  397. r_array[i+x.t+1] = 1;
  398. }
  399. }
  400. if(r.t > 0) r_array[r.t-1] += x.am(i,x_array[i],r,2*i,0,1);
  401. r.s = 0;
  402. r.clamp();
  403. }
  404. // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
  405. // r != q, this != m. q or r may be null.
  406. function bnpDivRemTo(m,q,r) {
  407. var pm = m.abs();
  408. if(pm.t <= 0) return;
  409. var pt = this.abs();
  410. if(pt.t < pm.t) {
  411. if(q != null) q.fromInt(0);
  412. if(r != null) this.copyTo(r);
  413. return;
  414. }
  415. if(r == null) r = nbi();
  416. var y = nbi(), ts = this.s, ms = m.s;
  417. var pm_array = pm.array;
  418. var nsh = BI_DB-nbits(pm_array[pm.t-1]); // normalize modulus
  419. if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
  420. else { pm.copyTo(y); pt.copyTo(r); }
  421. var ys = y.t;
  422. var y_array = y.array;
  423. var y0 = y_array[ys-1];
  424. if(y0 == 0) return;
  425. var yt = y0*(1<<BI_F1)+((ys>1)?y_array[ys-2]>>BI_F2:0);
  426. var d1 = BI_FV/yt, d2 = (1<<BI_F1)/yt, e = 1<<BI_F2;
  427. var i = r.t, j = i-ys, t = (q==null)?nbi():q;
  428. y.dlShiftTo(j,t);
  429. var r_array = r.array;
  430. if(r.compareTo(t) >= 0) {
  431. r_array[r.t++] = 1;
  432. r.subTo(t,r);
  433. }
  434. BigInteger.ONE.dlShiftTo(ys,t);
  435. t.subTo(y,y); // "negative" y so we can replace sub with am later
  436. while(y.t < ys) y_array[y.t++] = 0;
  437. while(--j >= 0) {
  438. // Estimate quotient digit
  439. var qd = (r_array[--i]==y0)?BI_DM:Math.floor(r_array[i]*d1+(r_array[i-1]+e)*d2);
  440. if((r_array[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
  441. y.dlShiftTo(j,t);
  442. r.subTo(t,r);
  443. while(r_array[i] < --qd) r.subTo(t,r);
  444. }
  445. }
  446. if(q != null) {
  447. r.drShiftTo(ys,q);
  448. if(ts != ms) BigInteger.ZERO.subTo(q,q);
  449. }
  450. r.t = ys;
  451. r.clamp();
  452. if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder
  453. if(ts < 0) BigInteger.ZERO.subTo(r,r);
  454. }
  455. // (public) this mod a
  456. function bnMod(a) {
  457. var r = nbi();
  458. this.abs().divRemTo(a,null,r);
  459. if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
  460. return r;
  461. }
  462. // Modular reduction using "classic" algorithm
  463. function Classic(m) { this.m = m; }
  464. function cConvert(x) {
  465. if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
  466. else return x;
  467. }
  468. function cRevert(x) { return x; }
  469. function cReduce(x) { x.divRemTo(this.m,null,x); }
  470. function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  471. function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  472. Classic.prototype.convert = cConvert;
  473. Classic.prototype.revert = cRevert;
  474. Classic.prototype.reduce = cReduce;
  475. Classic.prototype.mulTo = cMulTo;
  476. Classic.prototype.sqrTo = cSqrTo;
  477. // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
  478. // justification:
  479. // xy == 1 (mod m)
  480. // xy = 1+km
  481. // xy(2-xy) = (1+km)(1-km)
  482. // x[y(2-xy)] = 1-k^2m^2
  483. // x[y(2-xy)] == 1 (mod m^2)
  484. // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
  485. // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
  486. // JS multiply "overflows" differently from C/C++, so care is needed here.
  487. function bnpInvDigit() {
  488. var this_array = this.array;
  489. if(this.t < 1) return 0;
  490. var x = this_array[0];
  491. if((x&1) == 0) return 0;
  492. var y = x&3; // y == 1/x mod 2^2
  493. y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
  494. y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8
  495. y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16
  496. // last step - calculate inverse mod DV directly;
  497. // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
  498. y = (y*(2-x*y%BI_DV))%BI_DV; // y == 1/x mod 2^dbits
  499. // we really want the negative inverse, and -DV < y < DV
  500. return (y>0)?BI_DV-y:-y;
  501. }
  502. // Montgomery reduction
  503. function Montgomery(m) {
  504. this.m = m;
  505. this.mp = m.invDigit();
  506. this.mpl = this.mp&0x7fff;
  507. this.mph = this.mp>>15;
  508. this.um = (1<<(BI_DB-15))-1;
  509. this.mt2 = 2*m.t;
  510. }
  511. // xR mod m
  512. function montConvert(x) {
  513. var r = nbi();
  514. x.abs().dlShiftTo(this.m.t,r);
  515. r.divRemTo(this.m,null,r);
  516. if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
  517. return r;
  518. }
  519. // x/R mod m
  520. function montRevert(x) {
  521. var r = nbi();
  522. x.copyTo(r);
  523. this.reduce(r);
  524. return r;
  525. }
  526. // x = x/R mod m (HAC 14.32)
  527. function montReduce(x) {
  528. var x_array = x.array;
  529. while(x.t <= this.mt2) // pad x so am has enough room later
  530. x_array[x.t++] = 0;
  531. for(var i = 0; i < this.m.t; ++i) {
  532. // faster way of calculating u0 = x[i]*mp mod DV
  533. var j = x_array[i]&0x7fff;
  534. var u0 = (j*this.mpl+(((j*this.mph+(x_array[i]>>15)*this.mpl)&this.um)<<15))&BI_DM;
  535. // use am to combine the multiply-shift-add into one call
  536. j = i+this.m.t;
  537. x_array[j] += this.m.am(0,u0,x,i,0,this.m.t);
  538. // propagate carry
  539. while(x_array[j] >= BI_DV) { x_array[j] -= BI_DV; x_array[++j]++; }
  540. }
  541. x.clamp();
  542. x.drShiftTo(this.m.t,x);
  543. if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
  544. }
  545. // r = "x^2/R mod m"; x != r
  546. function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  547. // r = "xy/R mod m"; x,y != r
  548. function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  549. Montgomery.prototype.convert = montConvert;
  550. Montgomery.prototype.revert = montRevert;
  551. Montgomery.prototype.reduce = montReduce;
  552. Montgomery.prototype.mulTo = montMulTo;
  553. Montgomery.prototype.sqrTo = montSqrTo;
  554. // (protected) true iff this is even
  555. function bnpIsEven() {
  556. var this_array = this.array;
  557. return ((this.t>0)?(this_array[0]&1):this.s) == 0;
  558. }
  559. // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
  560. function bnpExp(e,z) {
  561. if(e > 0xffffffff || e < 1) return BigInteger.ONE;
  562. var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
  563. g.copyTo(r);
  564. while(--i >= 0) {
  565. z.sqrTo(r,r2);
  566. if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
  567. else { var t = r; r = r2; r2 = t; }
  568. }
  569. return z.revert(r);
  570. }
  571. // (public) this^e % m, 0 <= e < 2^32
  572. function bnModPowInt(e,m) {
  573. var z;
  574. if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
  575. return this.exp(e,z);
  576. }
  577. // protected
  578. BigInteger.prototype.copyTo = bnpCopyTo;
  579. BigInteger.prototype.fromInt = bnpFromInt;
  580. BigInteger.prototype.fromString = bnpFromString;
  581. BigInteger.prototype.clamp = bnpClamp;
  582. BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
  583. BigInteger.prototype.drShiftTo = bnpDRShiftTo;
  584. BigInteger.prototype.lShiftTo = bnpLShiftTo;
  585. BigInteger.prototype.rShiftTo = bnpRShiftTo;
  586. BigInteger.prototype.subTo = bnpSubTo;
  587. BigInteger.prototype.multiplyTo = bnpMultiplyTo;
  588. BigInteger.prototype.squareTo = bnpSquareTo;
  589. BigInteger.prototype.divRemTo = bnpDivRemTo;
  590. BigInteger.prototype.invDigit = bnpInvDigit;
  591. BigInteger.prototype.isEven = bnpIsEven;
  592. BigInteger.prototype.exp = bnpExp;
  593. // public
  594. BigInteger.prototype.toString = bnToString;
  595. BigInteger.prototype.negate = bnNegate;
  596. BigInteger.prototype.abs = bnAbs;
  597. BigInteger.prototype.compareTo = bnCompareTo;
  598. BigInteger.prototype.bitLength = bnBitLength;
  599. BigInteger.prototype.mod = bnMod;
  600. BigInteger.prototype.modPowInt = bnModPowInt;
  601. // "constants"
  602. BigInteger.ZERO = nbv(0);
  603. BigInteger.ONE = nbv(1);
  604. // Copyright (c) 2005 Tom Wu
  605. // All Rights Reserved.
  606. // See "LICENSE" for details.
  607. // Extended JavaScript BN functions, required for RSA private ops.
  608. // (public)
  609. function bnClone() { var r = nbi(); this.copyTo(r); return r; }
  610. // (public) return value as integer
  611. function bnIntValue() {
  612. var this_array = this.array;
  613. if(this.s < 0) {
  614. if(this.t == 1) return this_array[0]-BI_DV;
  615. else if(this.t == 0) return -1;
  616. }
  617. else if(this.t == 1) return this_array[0];
  618. else if(this.t == 0) return 0;
  619. // assumes 16 < DB < 32
  620. return ((this_array[1]&((1<<(32-BI_DB))-1))<<BI_DB)|this_array[0];
  621. }
  622. // (public) return value as byte
  623. function bnByteValue() {
  624. var this_array = this.array;
  625. return (this.t==0)?this.s:(this_array[0]<<24)>>24;
  626. }
  627. // (public) return value as short (assumes DB>=16)
  628. function bnShortValue() {
  629. var this_array = this.array;
  630. return (this.t==0)?this.s:(this_array[0]<<16)>>16;
  631. }
  632. // (protected) return x s.t. r^x < DV
  633. function bnpChunkSize(r) { return Math.floor(Math.LN2*BI_DB/Math.log(r)); }
  634. // (public) 0 if this == 0, 1 if this > 0
  635. function bnSigNum() {
  636. var this_array = this.array;
  637. if(this.s < 0) return -1;
  638. else if(this.t <= 0 || (this.t == 1 && this_array[0] <= 0)) return 0;
  639. else return 1;
  640. }
  641. // (protected) convert to radix string
  642. function bnpToRadix(b) {
  643. if(b == null) b = 10;
  644. if(this.signum() == 0 || b < 2 || b > 36) return "0";
  645. var cs = this.chunkSize(b);
  646. var a = Math.pow(b,cs);
  647. var d = nbv(a), y = nbi(), z = nbi(), r = "";
  648. this.divRemTo(d,y,z);
  649. while(y.signum() > 0) {
  650. r = (a+z.intValue()).toString(b).substr(1) + r;
  651. y.divRemTo(d,y,z);
  652. }
  653. return z.intValue().toString(b) + r;
  654. }
  655. // (protected) convert from radix string
  656. function bnpFromRadix(s,b) {
  657. this.fromInt(0);
  658. if(b == null) b = 10;
  659. var cs = this.chunkSize(b);
  660. var d = Math.pow(b,cs), mi = false, j = 0, w = 0;
  661. for(var i = 0; i < s.length; ++i) {
  662. var x = intAt(s,i);
  663. if(x < 0) {
  664. if(s.charAt(i) == "-" && this.signum() == 0) mi = true;
  665. continue;
  666. }
  667. w = b*w+x;
  668. if(++j >= cs) {
  669. this.dMultiply(d);
  670. this.dAddOffset(w,0);
  671. j = 0;
  672. w = 0;
  673. }
  674. }
  675. if(j > 0) {
  676. this.dMultiply(Math.pow(b,j));
  677. this.dAddOffset(w,0);
  678. }
  679. if(mi) BigInteger.ZERO.subTo(this,this);
  680. }
  681. // (protected) alternate constructor
  682. function bnpFromNumber(a,b,c) {
  683. if("number" == typeof b) {
  684. // new BigInteger(int,int,RNG)
  685. if(a < 2) this.fromInt(1);
  686. else {
  687. this.fromNumber(a,c);
  688. if(!this.testBit(a-1)) // force MSB set
  689. this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this);
  690. if(this.isEven()) this.dAddOffset(1,0); // force odd
  691. while(!this.isProbablePrime(b)) {
  692. this.dAddOffset(2,0);
  693. if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this);
  694. }
  695. }
  696. }
  697. else {
  698. // new BigInteger(int,RNG)
  699. var x = new Array(), t = a&7;
  700. x.length = (a>>3)+1;
  701. b.nextBytes(x);
  702. if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
  703. this.fromString(x,256);
  704. }
  705. }
  706. // (public) convert to bigendian byte array
  707. function bnToByteArray() {
  708. var this_array = this.array;
  709. var i = this.t, r = new Array();
  710. r[0] = this.s;
  711. var p = BI_DB-(i*BI_DB)%8, d, k = 0;
  712. if(i-- > 0) {
  713. if(p < BI_DB && (d = this_array[i]>>p) != (this.s&BI_DM)>>p)
  714. r[k++] = d|(this.s<<(BI_DB-p));
  715. while(i >= 0) {
  716. if(p < 8) {
  717. d = (this_array[i]&((1<<p)-1))<<(8-p);
  718. d |= this_array[--i]>>(p+=BI_DB-8);
  719. }
  720. else {
  721. d = (this_array[i]>>(p-=8))&0xff;
  722. if(p <= 0) { p += BI_DB; --i; }
  723. }
  724. if((d&0x80) != 0) d |= -256;
  725. if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
  726. if(k > 0 || d != this.s) r[k++] = d;
  727. }
  728. }
  729. return r;
  730. }
  731. function bnEquals(a) { return(this.compareTo(a)==0); }
  732. function bnMin(a) { return(this.compareTo(a)<0)?this:a; }
  733. function bnMax(a) { return(this.compareTo(a)>0)?this:a; }
  734. // (protected) r = this op a (bitwise)
  735. function bnpBitwiseTo(a,op,r) {
  736. var this_array = this.array;
  737. var a_array = a.array;
  738. var r_array = r.array;
  739. var i, f, m = Math.min(a.t,this.t);
  740. for(i = 0; i < m; ++i) r_array[i] = op(this_array[i],a_array[i]);
  741. if(a.t < this.t) {
  742. f = a.s&BI_DM;
  743. for(i = m; i < this.t; ++i) r_array[i] = op(this_array[i],f);
  744. r.t = this.t;
  745. }
  746. else {
  747. f = this.s&BI_DM;
  748. for(i = m; i < a.t; ++i) r_array[i] = op(f,a_array[i]);
  749. r.t = a.t;
  750. }
  751. r.s = op(this.s,a.s);
  752. r.clamp();
  753. }
  754. // (public) this & a
  755. function op_and(x,y) { return x&y; }
  756. function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; }
  757. // (public) this | a
  758. function op_or(x,y) { return x|y; }
  759. function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; }
  760. // (public) this ^ a
  761. function op_xor(x,y) { return x^y; }
  762. function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; }
  763. // (public) this & ~a
  764. function op_andnot(x,y) { return x&~y; }
  765. function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; }
  766. // (public) ~this
  767. function bnNot() {
  768. var this_array = this.array;
  769. var r = nbi();
  770. var r_array = r.array;
  771. for(var i = 0; i < this.t; ++i) r_array[i] = BI_DM&~this_array[i];
  772. r.t = this.t;
  773. r.s = ~this.s;
  774. return r;
  775. }
  776. // (public) this << n
  777. function bnShiftLeft(n) {
  778. var r = nbi();
  779. if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r);
  780. return r;
  781. }
  782. // (public) this >> n
  783. function bnShiftRight(n) {
  784. var r = nbi();
  785. if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r);
  786. return r;
  787. }
  788. // return index of lowest 1-bit in x, x < 2^31
  789. function lbit(x) {
  790. if(x == 0) return -1;
  791. var r = 0;
  792. if((x&0xffff) == 0) { x >>= 16; r += 16; }
  793. if((x&0xff) == 0) { x >>= 8; r += 8; }
  794. if((x&0xf) == 0) { x >>= 4; r += 4; }
  795. if((x&3) == 0) { x >>= 2; r += 2; }
  796. if((x&1) == 0) ++r;
  797. return r;
  798. }
  799. // (public) returns index of lowest 1-bit (or -1 if none)
  800. function bnGetLowestSetBit() {
  801. var this_array = this.array;
  802. for(var i = 0; i < this.t; ++i)
  803. if(this_array[i] != 0) return i*BI_DB+lbit(this_array[i]);
  804. if(this.s < 0) return this.t*BI_DB;
  805. return -1;
  806. }
  807. // return number of 1 bits in x
  808. function cbit(x) {
  809. var r = 0;
  810. while(x != 0) { x &= x-1; ++r; }
  811. return r;
  812. }
  813. // (public) return number of set bits
  814. function bnBitCount() {
  815. var r = 0, x = this.s&BI_DM;
  816. for(var i = 0; i < this.t; ++i) r += cbit(this_array[i]^x);
  817. return r;
  818. }
  819. // (public) true iff nth bit is set
  820. function bnTestBit(n) {
  821. var this_array = this.array;
  822. var j = Math.floor(n/BI_DB);
  823. if(j >= this.t) return(this.s!=0);
  824. return((this_array[j]&(1<<(n%BI_DB)))!=0);
  825. }
  826. // (protected) this op (1<<n)
  827. function bnpChangeBit(n,op) {
  828. var r = BigInteger.ONE.shiftLeft(n);
  829. this.bitwiseTo(r,op,r);
  830. return r;
  831. }
  832. // (public) this | (1<<n)
  833. function bnSetBit(n) { return this.changeBit(n,op_or); }
  834. // (public) this & ~(1<<n)
  835. function bnClearBit(n) { return this.changeBit(n,op_andnot); }
  836. // (public) this ^ (1<<n)
  837. function bnFlipBit(n) { return this.changeBit(n,op_xor); }
  838. // (protected) r = this + a
  839. function bnpAddTo(a,r) {
  840. var this_array = this.array;
  841. var a_array = a.array;
  842. var r_array = r.array;
  843. var i = 0, c = 0, m = Math.min(a.t,this.t);
  844. while(i < m) {
  845. c += this_array[i]+a_array[i];
  846. r_array[i++] = c&BI_DM;
  847. c >>= BI_DB;
  848. }
  849. if(a.t < this.t) {
  850. c += a.s;
  851. while(i < this.t) {
  852. c += this_array[i];
  853. r_array[i++] = c&BI_DM;
  854. c >>= BI_DB;
  855. }
  856. c += this.s;
  857. }
  858. else {
  859. c += this.s;
  860. while(i < a.t) {
  861. c += a_array[i];
  862. r_array[i++] = c&BI_DM;
  863. c >>= BI_DB;
  864. }
  865. c += a.s;
  866. }
  867. r.s = (c<0)?-1:0;
  868. if(c > 0) r_array[i++] = c;
  869. else if(c < -1) r_array[i++] = BI_DV+c;
  870. r.t = i;
  871. r.clamp();
  872. }
  873. // (public) this + a
  874. function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; }
  875. // (public) this - a
  876. function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; }
  877. // (public) this * a
  878. function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; }
  879. // (public) this / a
  880. function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; }
  881. // (public) this % a
  882. function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; }
  883. // (public) [this/a,this%a]
  884. function bnDivideAndRemainder(a) {
  885. var q = nbi(), r = nbi();
  886. this.divRemTo(a,q,r);
  887. return new Array(q,r);
  888. }
  889. // (protected) this *= n, this >= 0, 1 < n < DV
  890. function bnpDMultiply(n) {
  891. var this_array = this.array;
  892. this_array[this.t] = this.am(0,n-1,this,0,0,this.t);
  893. ++this.t;
  894. this.clamp();
  895. }
  896. // (protected) this += n << w words, this >= 0
  897. function bnpDAddOffset(n,w) {
  898. var this_array = this.array;
  899. while(this.t <= w) this_array[this.t++] = 0;
  900. this_array[w] += n;
  901. while(this_array[w] >= BI_DV) {
  902. this_array[w] -= BI_DV;
  903. if(++w >= this.t) this_array[this.t++] = 0;
  904. ++this_array[w];
  905. }
  906. }
  907. // A "null" reducer
  908. function NullExp() {}
  909. function nNop(x) { return x; }
  910. function nMulTo(x,y,r) { x.multiplyTo(y,r); }
  911. function nSqrTo(x,r) { x.squareTo(r); }
  912. NullExp.prototype.convert = nNop;
  913. NullExp.prototype.revert = nNop;
  914. NullExp.prototype.mulTo = nMulTo;
  915. NullExp.prototype.sqrTo = nSqrTo;
  916. // (public) this^e
  917. function bnPow(e) { return this.exp(e,new NullExp()); }
  918. // (protected) r = lower n words of "this * a", a.t <= n
  919. // "this" should be the larger one if appropriate.
  920. function bnpMultiplyLowerTo(a,n,r) {
  921. var r_array = r.array;
  922. var a_array = a.array;
  923. var i = Math.min(this.t+a.t,n);
  924. r.s = 0; // assumes a,this >= 0
  925. r.t = i;
  926. while(i > 0) r_array[--i] = 0;
  927. var j;
  928. for(j = r.t-this.t; i < j; ++i) r_array[i+this.t] = this.am(0,a_array[i],r,i,0,this.t);
  929. for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a_array[i],r,i,0,n-i);
  930. r.clamp();
  931. }
  932. // (protected) r = "this * a" without lower n words, n > 0
  933. // "this" should be the larger one if appropriate.
  934. function bnpMultiplyUpperTo(a,n,r) {
  935. var r_array = r.array;
  936. var a_array = a.array;
  937. --n;
  938. var i = r.t = this.t+a.t-n;
  939. r.s = 0; // assumes a,this >= 0
  940. while(--i >= 0) r_array[i] = 0;
  941. for(i = Math.max(n-this.t,0); i < a.t; ++i)
  942. r_array[this.t+i-n] = this.am(n-i,a_array[i],r,0,0,this.t+i-n);
  943. r.clamp();
  944. r.drShiftTo(1,r);
  945. }
  946. // Barrett modular reduction
  947. function Barrett(m) {
  948. // setup Barrett
  949. this.r2 = nbi();
  950. this.q3 = nbi();
  951. BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
  952. this.mu = this.r2.divide(m);
  953. this.m = m;
  954. }
  955. function barrettConvert(x) {
  956. if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
  957. else if(x.compareTo(this.m) < 0) return x;
  958. else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
  959. }
  960. function barrettRevert(x) { return x; }
  961. // x = x mod m (HAC 14.42)
  962. function barrettReduce(x) {
  963. x.drShiftTo(this.m.t-1,this.r2);
  964. if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
  965. this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
  966. this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
  967. while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
  968. x.subTo(this.r2,x);
  969. while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
  970. }
  971. // r = x^2 mod m; x != r
  972. function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  973. // r = x*y mod m; x,y != r
  974. function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  975. Barrett.prototype.convert = barrettConvert;
  976. Barrett.prototype.revert = barrettRevert;
  977. Barrett.prototype.reduce = barrettReduce;
  978. Barrett.prototype.mulTo = barrettMulTo;
  979. Barrett.prototype.sqrTo = barrettSqrTo;
  980. // (public) this^e % m (HAC 14.85)
  981. function bnModPow(e,m) {
  982. var e_array = e.array;
  983. var i = e.bitLength(), k, r = nbv(1), z;
  984. if(i <= 0) return r;
  985. else if(i < 18) k = 1;
  986. else if(i < 48) k = 3;
  987. else if(i < 144) k = 4;
  988. else if(i < 768) k = 5;
  989. else k = 6;
  990. if(i < 8)
  991. z = new Classic(m);
  992. else if(m.isEven())
  993. z = new Barrett(m);
  994. else
  995. z = new Montgomery(m);
  996. // precomputation
  997. var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1;
  998. g[1] = z.convert(this);
  999. if(k > 1) {
  1000. var g2 = nbi();
  1001. z.sqrTo(g[1],g2);
  1002. while(n <= km) {
  1003. g[n] = nbi();
  1004. z.mulTo(g2,g[n-2],g[n]);
  1005. n += 2;
  1006. }
  1007. }
  1008. var j = e.t-1, w, is1 = true, r2 = nbi(), t;
  1009. i = nbits(e_array[j])-1;
  1010. while(j >= 0) {
  1011. if(i >= k1) w = (e_array[j]>>(i-k1))&km;
  1012. else {
  1013. w = (e_array[j]&((1<<(i+1))-1))<<(k1-i);
  1014. if(j > 0) w |= e_array[j-1]>>(BI_DB+i-k1);
  1015. }
  1016. n = k;
  1017. while((w&1) == 0) { w >>= 1; --n; }
  1018. if((i -= n) < 0) { i += BI_DB; --j; }
  1019. if(is1) { // ret == 1, don't bother squaring or multiplying it
  1020. g[w].copyTo(r);
  1021. is1 = false;
  1022. }
  1023. else {
  1024. while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; }
  1025. if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; }
  1026. z.mulTo(r2,g[w],r);
  1027. }
  1028. while(j >= 0 && (e_array[j]&(1<<i)) == 0) {
  1029. z.sqrTo(r,r2); t = r; r = r2; r2 = t;
  1030. if(--i < 0) { i = BI_DB-1; --j; }
  1031. }
  1032. }
  1033. return z.revert(r);
  1034. }
  1035. // (public) gcd(this,a) (HAC 14.54)
  1036. function bnGCD(a) {
  1037. var x = (this.s<0)?this.negate():this.clone();
  1038. var y = (a.s<0)?a.negate():a.clone();
  1039. if(x.compareTo(y) < 0) { var t = x; x = y; y = t; }
  1040. var i = x.getLowestSetBit(), g = y.getLowestSetBit();
  1041. if(g < 0) return x;
  1042. if(i < g) g = i;
  1043. if(g > 0) {
  1044. x.rShiftTo(g,x);
  1045. y.rShiftTo(g,y);
  1046. }
  1047. while(x.signum() > 0) {
  1048. if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x);
  1049. if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y);
  1050. if(x.compareTo(y) >= 0) {
  1051. x.subTo(y,x);
  1052. x.rShiftTo(1,x);
  1053. }
  1054. else {
  1055. y.subTo(x,y);
  1056. y.rShiftTo(1,y);
  1057. }
  1058. }
  1059. if(g > 0) y.lShiftTo(g,y);
  1060. return y;
  1061. }
  1062. // (protected) this % n, n < 2^26
  1063. function bnpModInt(n) {
  1064. var this_array = this.array;
  1065. if(n <= 0) return 0;
  1066. var d = BI_DV%n, r = (this.s<0)?n-1:0;
  1067. if(this.t > 0)
  1068. if(d == 0) r = this_array[0]%n;
  1069. else for(var i = this.t-1; i >= 0; --i) r = (d*r+this_array[i])%n;
  1070. return r;
  1071. }
  1072. // (public) 1/this % m (HAC 14.61)
  1073. function bnModInverse(m) {
  1074. var ac = m.isEven();
  1075. if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
  1076. var u = m.clone(), v = this.clone();
  1077. var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
  1078. while(u.signum() != 0) {
  1079. while(u.isEven()) {
  1080. u.rShiftTo(1,u);
  1081. if(ac) {
  1082. if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); }
  1083. a.rShiftTo(1,a);
  1084. }
  1085. else if(!b.isEven()) b.subTo(m,b);
  1086. b.rShiftTo(1,b);
  1087. }
  1088. while(v.isEven()) {
  1089. v.rShiftTo(1,v);
  1090. if(ac) {
  1091. if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); }
  1092. c.rShiftTo(1,c);
  1093. }
  1094. else if(!d.isEven()) d.subTo(m,d);
  1095. d.rShiftTo(1,d);
  1096. }
  1097. if(u.compareTo(v) >= 0) {
  1098. u.subTo(v,u);
  1099. if(ac) a.subTo(c,a);
  1100. b.subTo(d,b);
  1101. }
  1102. else {
  1103. v.subTo(u,v);
  1104. if(ac) c.subTo(a,c);
  1105. d.subTo(b,d);
  1106. }
  1107. }
  1108. if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
  1109. if(d.compareTo(m) >= 0) return d.subtract(m);
  1110. if(d.signum() < 0) d.addTo(m,d); else return d;
  1111. if(d.signum() < 0) return d.add(m); else return d;
  1112. }
  1113. var lowprimes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509];
  1114. var lplim = (1<<26)/lowprimes[lowprimes.length-1];
  1115. // (public) test primality with certainty >= 1-.5^t
  1116. function bnIsProbablePrime(t) {
  1117. var i, x = this.abs();
  1118. var x_array = x.array;
  1119. if(x.t == 1 && x_array[0] <= lowprimes[lowprimes.length-1]) {
  1120. for(i = 0; i < lowprimes.length; ++i)
  1121. if(x_array[0] == lowprimes[i]) return true;
  1122. return false;
  1123. }
  1124. if(x.isEven()) return false;
  1125. i = 1;
  1126. while(i < lowprimes.length) {
  1127. var m = lowprimes[i], j = i+1;
  1128. while(j < lowprimes.length && m < lplim) m *= lowprimes[j++];
  1129. m = x.modInt(m);
  1130. while(i < j) if(m%lowprimes[i++] == 0) return false;
  1131. }
  1132. return x.millerRabin(t);
  1133. }
  1134. // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
  1135. function bnpMillerRabin(t) {
  1136. var n1 = this.subtract(BigInteger.ONE);
  1137. var k = n1.getLowestSetBit();
  1138. if(k <= 0) return false;
  1139. var r = n1.shiftRight(k);
  1140. t = (t+1)>>1;
  1141. if(t > lowprimes.length) t = lowprimes.length;
  1142. var a = nbi();
  1143. for(var i = 0; i < t; ++i) {
  1144. a.fromInt(lowprimes[i]);
  1145. var y = a.modPow(r,this);
  1146. if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
  1147. var j = 1;
  1148. while(j++ < k && y.compareTo(n1) != 0) {
  1149. y = y.modPowInt(2,this);
  1150. if(y.compareTo(BigInteger.ONE) == 0) return false;
  1151. }
  1152. if(y.compareTo(n1) != 0) return false;
  1153. }
  1154. }
  1155. return true;
  1156. }
  1157. // protected
  1158. BigInteger.prototype.chunkSize = bnpChunkSize;
  1159. BigInteger.prototype.toRadix = bnpToRadix;
  1160. BigInteger.prototype.fromRadix = bnpFromRadix;
  1161. BigInteger.prototype.fromNumber = bnpFromNumber;
  1162. BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
  1163. BigInteger.prototype.changeBit = bnpChangeBit;
  1164. BigInteger.prototype.addTo = bnpAddTo;
  1165. BigInteger.prototype.dMultiply = bnpDMultiply;
  1166. BigInteger.prototype.dAddOffset = bnpDAddOffset;
  1167. BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
  1168. BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
  1169. BigInteger.prototype.modInt = bnpModInt;
  1170. BigInteger.prototype.millerRabin = bnpMillerRabin;
  1171. // public
  1172. BigInteger.prototype.clone = bnClone;
  1173. BigInteger.prototype.intValue = bnIntValue;
  1174. BigInteger.prototype.byteValue = bnByteValue;
  1175. BigInteger.prototype.shortValue = bnShortValue;
  1176. BigInteger.prototype.signum = bnSigNum;
  1177. BigInteger.prototype.toByteArray = bnToByteArray;
  1178. BigInteger.prototype.equals = bnEquals;
  1179. BigInteger.prototype.min = bnMin;
  1180. BigInteger.prototype.max = bnMax;
  1181. BigInteger.prototype.and = bnAnd;
  1182. BigInteger.prototype.or = bnOr;
  1183. BigInteger.prototype.xor = bnXor;
  1184. BigInteger.prototype.andNot = bnAndNot;
  1185. BigInteger.prototype.not = bnNot;
  1186. BigInteger.prototype.shiftLeft = bnShiftLeft;
  1187. BigInteger.prototype.shiftRight = bnShiftRight;
  1188. BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
  1189. BigInteger.prototype.bitCount = bnBitCount;
  1190. BigInteger.prototype.testBit = bnTestBit;
  1191. BigInteger.prototype.setBit = bnSetBit;
  1192. BigInteger.prototype.clearBit = bnClearBit;
  1193. BigInteger.prototype.flipBit = bnFlipBit;
  1194. BigInteger.prototype.add = bnAdd;
  1195. BigInteger.prototype.subtract = bnSubtract;
  1196. BigInteger.prototype.multiply = bnMultiply;
  1197. BigInteger.prototype.divide = bnDivide;
  1198. BigInteger.prototype.remainder = bnRemainder;
  1199. BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
  1200. BigInteger.prototype.modPow = bnModPow;
  1201. BigInteger.prototype.modInverse = bnModInverse;
  1202. BigInteger.prototype.pow = bnPow;
  1203. BigInteger.prototype.gcd = bnGCD;
  1204. BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
  1205. // BigInteger interfaces not implemented in jsbn:
  1206. // BigInteger(int signum, byte[] magnitude)
  1207. // double doubleValue()
  1208. // float floatValue()
  1209. // int hashCode()
  1210. // long longValue()
  1211. // static BigInteger valueOf(long val)
  1212. // prng4.js - uses Arcfour as a PRNG
  1213. function Arcfour() {
  1214. this.i = 0;
  1215. this.j = 0;
  1216. this.S = new Array();
  1217. }
  1218. // Initialize arcfour context from key, an array of ints, each from [0..255]
  1219. function ARC4init(key) {
  1220. var i, j, t;
  1221. for(i = 0; i < 256; ++i)
  1222. this.S[i] = i;
  1223. j = 0;
  1224. for(i = 0; i < 256; ++i) {
  1225. j = (j + this.S[i] + key[i % key.length]) & 255;
  1226. t = this.S[i];
  1227. this.S[i] = this.S[j];
  1228. this.S[j] = t;
  1229. }
  1230. this.i = 0;
  1231. this.j = 0;
  1232. }
  1233. function ARC4next() {
  1234. var t;
  1235. this.i = (this.i + 1) & 255;
  1236. this.j = (this.j + this.S[this.i]) & 255;
  1237. t = this.S[this.i];
  1238. this.S[this.i] = this.S[this.j];
  1239. this.S[this.j] = t;
  1240. return this.S[(t + this.S[this.i]) & 255];
  1241. }
  1242. Arcfour.prototype.init = ARC4init;
  1243. Arcfour.prototype.next = ARC4next;
  1244. // Plug in your RNG constructor here
  1245. function prng_newstate() {
  1246. return new Arcfour();
  1247. }
  1248. // Pool size must be a multiple of 4 and greater than 32.
  1249. // An array of bytes the size of the pool will be passed to init()
  1250. var rng_psize = 256;
  1251. // Random number generator - requires a PRNG backend, e.g. prng4.js
  1252. // For best results, put code like
  1253. // <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
  1254. // in your main HTML document.
  1255. var rng_state;
  1256. var rng_pool;
  1257. var rng_pptr;
  1258. // Mix in a 32-bit integer into the pool
  1259. function rng_seed_int(x) {
  1260. rng_pool[rng_pptr++] ^= x & 255;
  1261. rng_pool[rng_pptr++] ^= (x >> 8) & 255;
  1262. rng_pool[rng_pptr++] ^= (x >> 16) & 255;
  1263. rng_pool[rng_pptr++] ^= (x >> 24) & 255;
  1264. if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;
  1265. }
  1266. // Mix in the current time (w/milliseconds) into the pool
  1267. function rng_seed_time() {
  1268. // Use pre-computed date to avoid making the benchmark
  1269. // results dependent on the current date.
  1270. rng_seed_int(1122926989487);
  1271. }
  1272. // Initialize the pool with junk if needed.
  1273. if(rng_pool == null) {
  1274. rng_pool = new Array();
  1275. rng_pptr = 0;
  1276. var t;
  1277. while(rng_pptr < rng_psize) { // extract some randomness from Math.random()
  1278. t = Math.floor(65536 * Math.random());
  1279. rng_pool[rng_pptr++] = t >>> 8;
  1280. rng_pool[rng_pptr++] = t & 255;
  1281. }
  1282. rng_pptr = 0;
  1283. rng_seed_time();
  1284. //rng_seed_int(window.screenX);
  1285. //rng_seed_int(window.screenY);
  1286. }
  1287. function rng_get_byte() {
  1288. if(rng_state == null) {
  1289. rng_seed_time();
  1290. rng_state = prng_newstate();
  1291. rng_state.init(rng_pool);
  1292. for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
  1293. rng_pool[rng_pptr] = 0;
  1294. rng_pptr = 0;
  1295. //rng_pool = null;
  1296. }
  1297. // TODO: allow reseeding after first request
  1298. return rng_state.next();
  1299. }
  1300. function rng_get_bytes(ba) {
  1301. var i;
  1302. for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
  1303. }
  1304. function SecureRandom() {}
  1305. SecureRandom.prototype.nextBytes = rng_get_bytes;
  1306. // Depends on jsbn.js and rng.js
  1307. // convert a (hex) string to a bignum object
  1308. function parseBigInt(str,r) {
  1309. return new BigInteger(str,r);
  1310. }
  1311. function linebrk(s,n) {
  1312. var ret = "";
  1313. var i = 0;
  1314. while(i + n < s.length) {
  1315. ret += s.substring(i,i+n) + "\n";
  1316. i += n;
  1317. }
  1318. return ret + s.substring(i,s.length);
  1319. }
  1320. function byte2Hex(b) {
  1321. if(b < 0x10)
  1322. return "0" + b.toString(16);
  1323. else
  1324. return b.toString(16);
  1325. }
  1326. // PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
  1327. function pkcs1pad2(s,n) {
  1328. if(n < s.length + 11) {
  1329. alert("Message too long for RSA");
  1330. return null;
  1331. }
  1332. var ba = new Array();
  1333. var i = s.length - 1;
  1334. while(i >= 0 && n > 0) ba[--n] = s.charCodeAt(i--);
  1335. ba[--n] = 0;
  1336. var rng = new SecureRandom();
  1337. var x = new Array();
  1338. while(n > 2) { // random non-zero pad
  1339. x[0] = 0;
  1340. while(x[0] == 0) rng.nextBytes(x);
  1341. ba[--n] = x[0];
  1342. }
  1343. ba[--n] = 2;
  1344. ba[--n] = 0;
  1345. return new BigInteger(ba);
  1346. }
  1347. // "empty" RSA key constructor
  1348. function RSAKey() {
  1349. this.n = null;
  1350. this.e = 0;
  1351. this.d = null;
  1352. this.p = null;
  1353. this.q = null;
  1354. this.dmp1 = null;
  1355. this.dmq1 = null;
  1356. this.coeff = null;
  1357. }
  1358. // Set the public key fields N and e from hex strings
  1359. function RSASetPublic(N,E) {
  1360. if(N != null && E != null && N.length > 0 && E.length > 0) {
  1361. this.n = parseBigInt(N,16);
  1362. this.e = parseInt(E,16);
  1363. }
  1364. else
  1365. alert("Invalid RSA public key");
  1366. }
  1367. // Perform raw public operation on "x": return x^e (mod n)
  1368. function RSADoPublic(x) {
  1369. return x.modPowInt(this.e, this.n);
  1370. }
  1371. // Return the PKCS#1 RSA encryption of "text" as an even-length hex string
  1372. function RSAEncrypt(text) {
  1373. var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3);
  1374. if(m == null) return null;
  1375. var c = this.doPublic(m);
  1376. if(c == null) return null;
  1377. var h = c.toString(16);
  1378. if((h.length & 1) == 0) return h; else return "0" + h;
  1379. }
  1380. // Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string
  1381. //function RSAEncryptB64(text) {
  1382. // var h = this.encrypt(text);
  1383. // if(h) return hex2b64(h); else return null;
  1384. //}
  1385. // protected
  1386. RSAKey.prototype.doPublic = RSADoPublic;
  1387. // public
  1388. RSAKey.prototype.setPublic = RSASetPublic;
  1389. RSAKey.prototype.encrypt = RSAEncrypt;
  1390. //RSAKey.prototype.encrypt_b64 = RSAEncryptB64;
  1391. // Depends on rsa.js and jsbn2.js
  1392. // Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext
  1393. function pkcs1unpad2(d,n) {
  1394. var b = d.toByteArray();
  1395. var i = 0;
  1396. while(i < b.length && b[i] == 0) ++i;
  1397. if(b.length-i != n-1 || b[i] != 2)
  1398. return null;
  1399. ++i;
  1400. while(b[i] != 0)
  1401. if(++i >= b.length) return null;
  1402. var ret = "";
  1403. while(++i < b.length)
  1404. ret += String.fromCharCode(b[i]);
  1405. return ret;
  1406. }
  1407. // Set the private key fields N, e, and d from hex strings
  1408. function RSASetPrivate(N,E,D) {
  1409. if(N != null && E != null && N.length > 0 && E.length > 0) {
  1410. this.n = parseBigInt(N,16);
  1411. this.e = parseInt(E,16);
  1412. this.d = parseBigInt(D,16);
  1413. }
  1414. else
  1415. alert("Invalid RSA private key");
  1416. }
  1417. // Set the private key fields N, e, d and CRT params from hex strings
  1418. function RSASetPrivateEx(N,E,D,P,Q,DP,DQ,C) {
  1419. if(N != null && E != null && N.length > 0 && E.length > 0) {
  1420. this.n = parseBigInt(N,16);
  1421. this.e = parseInt(E,16);
  1422. this.d = parseBigInt(D,16);
  1423. this.p = parseBigInt(P,16);
  1424. this.q = parseBigInt(Q,16);
  1425. this.dmp1 = parseBigInt(DP,16);
  1426. this.dmq1 = parseBigInt(DQ,16);
  1427. this.coeff = parseBigInt(C,16);
  1428. }
  1429. else
  1430. alert("Invalid RSA private key");
  1431. }
  1432. // Generate a new random private key B bits long, using public expt E
  1433. function RSAGenerate(B,E) {
  1434. var rng = new SecureRandom();
  1435. var qs = B>>1;
  1436. this.e = parseInt(E,16);
  1437. var ee = new BigInteger(E,16);
  1438. for(;;) {
  1439. for(;;) {
  1440. this.p = new BigInteger(B-qs,1,rng);
  1441. if(this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) break;
  1442. }
  1443. for(;;) {
  1444. this.q = new BigInteger(qs,1,rng);
  1445. if(this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) break;
  1446. }
  1447. if(this.p.compareTo(this.q) <= 0) {
  1448. var t = this.p;
  1449. this.p = this.q;
  1450. this.q = t;
  1451. }
  1452. var p1 = this.p.subtract(BigInteger.ONE);
  1453. var q1 = this.q.subtract(BigInteger.ONE);
  1454. var phi = p1.multiply(q1);
  1455. if(phi.gcd(ee).compareTo(BigInteger.ONE) == 0) {
  1456. this.n = this.p.multiply(this.q);
  1457. this.d = ee.modInverse(phi);
  1458. this.dmp1 = this.d.mod(p1);
  1459. this.dmq1 = this.d.mod(q1);
  1460. this.coeff = this.q.modInverse(this.p);
  1461. break;
  1462. }
  1463. }
  1464. }
  1465. // Perform raw private operation on "x": return x^d (mod n)
  1466. function RSADoPrivate(x) {
  1467. if(this.p == null || this.q == null)
  1468. return x.modPow(this.d, this.n);
  1469. // TODO: re-calculate any missing CRT params
  1470. var xp = x.mod(this.p).modPow(this.dmp1, this.p);
  1471. var xq = x.mod(this.q).modPow(this.dmq1, this.q);
  1472. while(xp.compareTo(xq) < 0)
  1473. xp = xp.add(this.p);
  1474. return xp.subtract(xq).multiply(this.coeff).mod(this.p).multiply(this.q).add(xq);
  1475. }
  1476. // Return the PKCS#1 RSA decryption of "ctext".
  1477. // "ctext" is an even-length hex string and the output is a plain string.
  1478. function RSADecrypt(ctext) {
  1479. var c = parseBigInt(ctext, 16);
  1480. var m = this.doPrivate(c);
  1481. if(m == null) return null;
  1482. return pkcs1unpad2(m, (this.n.bitLength()+7)>>3);
  1483. }
  1484. // Return the PKCS#1 RSA decryption of "ctext".
  1485. // "ctext" is a Base64-encoded string and the output is a plain string.
  1486. //function RSAB64Decrypt(ctext) {
  1487. // var h = b64tohex(ctext);
  1488. // if(h) return this.decrypt(h); else return null;
  1489. //}
  1490. // protected
  1491. RSAKey.prototype.doPrivate = RSADoPrivate;
  1492. // public
  1493. RSAKey.prototype.setPrivate = RSASetPrivate;
  1494. RSAKey.prototype.setPrivateEx = RSASetPrivateEx;
  1495. RSAKey.prototype.generate = RSAGenerate;
  1496. RSAKey.prototype.decrypt = RSADecrypt;
  1497. //RSAKey.prototype.b64_decrypt = RSAB64Decrypt;
  1498. nValue="a5261939975948bb7a58dffe5ff54e65f0498f9175f5a09288810b8975871e99af3b5dd94057b0fc07535f5f97444504fa35169d461d0d30cf0192e307727c065168c788771c561a9400fb49175e9e6aa4e23fe11af69e9412dd23b0cb6684c4c2429bce139e848ab26d0829073351f4acd36074eafd036a5eb83359d2a698d3";
  1499. eValue="10001";
  1500. dValue="8e9912f6d3645894e8d38cb58c0db81ff516cf4c7e5a14c7f1eddb1459d2cded4d8d293fc97aee6aefb861859c8b6a3d1dfe710463e1f9ddc72048c09751971c4a580aa51eb523357a3cc48d31cfad1d4a165066ed92d4748fb6571211da5cb14bc11b6e2df7c1a559e6d5ac1cd5c94703a22891464fba23d0d965086277a161";
  1501. pValue="d090ce58a92c75233a6486cb0a9209bf3583b64f540c76f5294bb97d285eed33aec220bde14b2417951178ac152ceab6da7090905b478195498b352048f15e7d";
  1502. qValue="cab575dc652bb66df15a0359609d51d1db184750c00c6698b90ef3465c99655103edbf0d54c56aec0ce3c4d22592338092a126a0cc49f65a4a30d222b411e58f";
  1503. dmp1Value="1a24bca8e273df2f0e47c199bbf678604e7df7215480c77c8db39f49b000ce2cf7500038acfff5433b7d582a01f1826e6f4d42e1c57f5e1fef7b12aabc59fd25";
  1504. dmq1Value="3d06982efbbe47339e1f6d36b1216b8a741d410b0c662f54f7118b27b9a4ec9d914337eb39841d8666f3034408cf94f5b62f11c402fc994fe15a05493150d9fd";
  1505. coeffValue="3a3e731acd8960b7ff9eb81a7ff93bd1cfa74cbd56987db58b4594fb09c09084db1734c8143f98b602b981aaa9243ca28deb69b5b280ee8dcee0fd2625e53250";
  1506. setupEngine(am3, 28);
  1507. var TEXT = "The quick brown fox jumped over the extremely lazy frog! " +
  1508. "Now is the time for all good men to come to the party.";
  1509. var encrypted;
  1510. function encrypt() {
  1511. var RSA = new RSAKey();
  1512. RSA.setPublic(nValue, eValue);
  1513. RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue);
  1514. encrypted = RSA.encrypt(TEXT);
  1515. }
  1516. function decrypt() {
  1517. var RSA = new RSAKey();
  1518. RSA.setPublic(nValue, eValue);
  1519. RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue);
  1520. var decrypted = RSA.decrypt(encrypted);
  1521. if (decrypted != TEXT) {
  1522. throw new Error("Crypto operation failed");
  1523. }
  1524. }