changed git call from https to git readonly
[atutor.git] / mods / ldap / rsa / jsbn.js
1 // Copyright (c) 2005  Tom Wu
2 // All Rights Reserved.
3 // See "LICENSE" for details.
4
5 // Basic JavaScript BN library - subset useful for RSA encryption.
6
7 // Bits per digit
8 var dbits;
9
10 // JavaScript engine analysis
11 var canary = 0xdeadbeefcafe;
12 var j_lm = ((canary&0xffffff)==0xefcafe);
13
14 // (public) Constructor
15 function BigInteger(a,b,c) {
16   if(a != null)
17     if("number" == typeof a) this.fromNumber(a,b,c);
18     else if(b == null && "string" != typeof a) this.fromString(a,256);
19     else this.fromString(a,b);
20 }
21
22 // return new, unset BigInteger
23 function nbi() { return new BigInteger(null); }
24
25 // am: Compute w_j += (x*this_i), propagate carries,
26 // c is initial carry, returns final carry.
27 // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
28 // We need to select the fastest one that works in this environment.
29
30 // am1: use a single mult and divide to get the high bits,
31 // max digit bits should be 26 because
32 // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
33 function am1(i,x,w,j,c,n) {
34   while(--n >= 0) {
35     var v = x*this[i++]+w[j]+c;
36     c = Math.floor(v/0x4000000);
37     w[j++] = v&0x3ffffff;
38   }
39   return c;
40 }
41 // am2 avoids a big mult-and-extract completely.
42 // Max digit bits should be <= 30 because we do bitwise ops
43 // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
44 function am2(i,x,w,j,c,n) {
45   var xl = x&0x7fff, xh = x>>15;
46   while(--n >= 0) {
47     var l = this[i]&0x7fff;
48     var h = this[i++]>>15;
49     var m = xh*l+h*xl;
50     l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);
51     c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
52     w[j++] = l&0x3fffffff;
53   }
54   return c;
55 }
56 // Alternately, set max digit bits to 28 since some
57 // browsers slow down when dealing with 32-bit numbers.
58 function am3(i,x,w,j,c,n) {
59   var xl = x&0x3fff, xh = x>>14;
60   while(--n >= 0) {
61     var l = this[i]&0x3fff;
62     var h = this[i++]>>14;
63     var m = xh*l+h*xl;
64     l = xl*l+((m&0x3fff)<<14)+w[j]+c;
65     c = (l>>28)+(m>>14)+xh*h;
66     w[j++] = l&0xfffffff;
67   }
68   return c;
69 }
70 if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
71   BigInteger.prototype.am = am2;
72   dbits = 30;
73 }
74 else if(j_lm && (navigator.appName != "Netscape")) {
75   BigInteger.prototype.am = am1;
76   dbits = 26;
77 }
78 else { // Mozilla/Netscape seems to prefer am3
79   BigInteger.prototype.am = am3;
80   dbits = 28;
81 }
82
83 BigInteger.prototype.DB = dbits;
84 BigInteger.prototype.DM = ((1<<dbits)-1);
85 BigInteger.prototype.DV = (1<<dbits);
86
87 var BI_FP = 52;
88 BigInteger.prototype.FV = Math.pow(2,BI_FP);
89 BigInteger.prototype.F1 = BI_FP-dbits;
90 BigInteger.prototype.F2 = 2*dbits-BI_FP;
91
92 // Digit conversions
93 var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
94 var BI_RC = new Array();
95 var rr,vv;
96 rr = "0".charCodeAt(0);
97 for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
98 rr = "a".charCodeAt(0);
99 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
100 rr = "A".charCodeAt(0);
101 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
102
103 function int2char(n) { return BI_RM.charAt(n); }
104 function intAt(s,i) {
105   var c = BI_RC[s.charCodeAt(i)];
106   return (c==null)?-1:c;
107 }
108
109 // (protected) copy this to r
110 function bnpCopyTo(r) {
111   for(var i = this.t-1; i >= 0; --i) r[i] = this[i];
112   r.t = this.t;
113   r.s = this.s;
114 }
115
116 // (protected) set from integer value x, -DV <= x < DV
117 function bnpFromInt(x) {
118   this.t = 1;
119   this.s = (x<0)?-1:0;
120   if(x > 0) this[0] = x;
121   else if(x < -1) this[0] = x+DV;
122   else this.t = 0;
123 }
124
125 // return bigint initialized to value
126 function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
127
128 // (protected) set from string and radix
129 function bnpFromString(s,b) {
130   var k;
131   if(b == 16) k = 4;
132   else if(b == 8) k = 3;
133   else if(b == 256) k = 8; // byte array
134   else if(b == 2) k = 1;
135   else if(b == 32) k = 5;
136   else if(b == 4) k = 2;
137   else { this.fromRadix(s,b); return; }
138   this.t = 0;
139   this.s = 0;
140   var i = s.length, mi = false, sh = 0;
141   while(--i >= 0) {
142     var x = (k==8)?s[i]&0xff:intAt(s,i);
143     if(x < 0) {
144       if(s.charAt(i) == "-") mi = true;
145       continue;
146     }
147     mi = false;
148     if(sh == 0)
149       this[this.t++] = x;
150     else if(sh+k > this.DB) {
151       this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
152       this[this.t++] = (x>>(this.DB-sh));
153     }
154     else
155       this[this.t-1] |= x<<sh;
156     sh += k;
157     if(sh >= this.DB) sh -= this.DB;
158   }
159   if(k == 8 && (s[0]&0x80) != 0) {
160     this.s = -1;
161     if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh;
162   }
163   this.clamp();
164   if(mi) BigInteger.ZERO.subTo(this,this);
165 }
166
167 // (protected) clamp off excess high words
168 function bnpClamp() {
169   var c = this.s&this.DM;
170   while(this.t > 0 && this[this.t-1] == c) --this.t;
171 }
172
173 // (public) return string representation in given radix
174 function bnToString(b) {
175   if(this.s < 0) return "-"+this.negate().toString(b);
176   var k;
177   if(b == 16) k = 4;
178   else if(b == 8) k = 3;
179   else if(b == 2) k = 1;
180   else if(b == 32) k = 5;
181   else if(b == 4) k = 2;
182   else return this.toRadix(b);
183   var km = (1<<k)-1, d, m = false, r = "", i = this.t;
184   var p = this.DB-(i*this.DB)%k;
185   if(i-- > 0) {
186     if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = int2char(d); }
187     while(i >= 0) {
188       if(p < k) {
189         d = (this[i]&((1<<p)-1))<<(k-p);
190         d |= this[--i]>>(p+=this.DB-k);
191       }
192       else {
193         d = (this[i]>>(p-=k))&km;
194         if(p <= 0) { p += this.DB; --i; }
195       }
196       if(d > 0) m = true;
197       if(m) r += int2char(d);
198     }
199   }
200   return m?r:"0";
201 }
202
203 // (public) -this
204 function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
205
206 // (public) |this|
207 function bnAbs() { return (this.s<0)?this.negate():this; }
208
209 // (public) return + if this > a, - if this < a, 0 if equal
210 function bnCompareTo(a) {
211   var r = this.s-a.s;
212   if(r != 0) return r;
213   var i = this.t;
214   r = i-a.t;
215   if(r != 0) return r;
216   while(--i >= 0) if((r=this[i]-a[i]) != 0) return r;
217   return 0;
218 }
219
220 // returns bit length of the integer x
221 function nbits(x) {
222   var r = 1, t;
223   if((t=x>>>16) != 0) { x = t; r += 16; }
224   if((t=x>>8) != 0) { x = t; r += 8; }
225   if((t=x>>4) != 0) { x = t; r += 4; }
226   if((t=x>>2) != 0) { x = t; r += 2; }
227   if((t=x>>1) != 0) { x = t; r += 1; }
228   return r;
229 }
230
231 // (public) return the number of bits in "this"
232 function bnBitLength() {
233   if(this.t <= 0) return 0;
234   return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM));
235 }
236
237 // (protected) r = this << n*DB
238 function bnpDLShiftTo(n,r) {
239   var i;
240   for(i = this.t-1; i >= 0; --i) r[i+n] = this[i];
241   for(i = n-1; i >= 0; --i) r[i] = 0;
242   r.t = this.t+n;
243   r.s = this.s;
244 }
245
246 // (protected) r = this >> n*DB
247 function bnpDRShiftTo(n,r) {
248   for(var i = n; i < this.t; ++i) r[i-n] = this[i];
249   r.t = Math.max(this.t-n,0);
250   r.s = this.s;
251 }
252
253 // (protected) r = this << n
254 function bnpLShiftTo(n,r) {
255   var bs = n%this.DB;
256   var cbs = this.DB-bs;
257   var bm = (1<<cbs)-1;
258   var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i;
259   for(i = this.t-1; i >= 0; --i) {
260     r[i+ds+1] = (this[i]>>cbs)|c;
261     c = (this[i]&bm)<<bs;
262   }
263   for(i = ds-1; i >= 0; --i) r[i] = 0;
264   r[ds] = c;
265   r.t = this.t+ds+1;
266   r.s = this.s;
267   r.clamp();
268 }
269
270 // (protected) r = this >> n
271 function bnpRShiftTo(n,r) {
272   r.s = this.s;
273   var ds = Math.floor(n/this.DB);
274   if(ds >= this.t) { r.t = 0; return; }
275   var bs = n%this.DB;
276   var cbs = this.DB-bs;
277   var bm = (1<<bs)-1;
278   r[0] = this[ds]>>bs;
279   for(var i = ds+1; i < this.t; ++i) {
280     r[i-ds-1] |= (this[i]&bm)<<cbs;
281     r[i-ds] = this[i]>>bs;
282   }
283   if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs;
284   r.t = this.t-ds;
285   r.clamp();
286 }
287
288 // (protected) r = this - a
289 function bnpSubTo(a,r) {
290   var i = 0, c = 0, m = Math.min(a.t,this.t);
291   while(i < m) {
292     c += this[i]-a[i];
293     r[i++] = c&this.DM;
294     c >>= this.DB;
295   }
296   if(a.t < this.t) {
297     c -= a.s;
298     while(i < this.t) {
299       c += this[i];
300       r[i++] = c&this.DM;
301       c >>= this.DB;
302     }
303     c += this.s;
304   }
305   else {
306     c += this.s;
307     while(i < a.t) {
308       c -= a[i];
309       r[i++] = c&this.DM;
310       c >>= this.DB;
311     }
312     c -= a.s;
313   }
314   r.s = (c<0)?-1:0;
315   if(c < -1) r[i++] = this.DV+c;
316   else if(c > 0) r[i++] = c;
317   r.t = i;
318   r.clamp();
319 }
320
321 // (protected) r = this * a, r != this,a (HAC 14.12)
322 // "this" should be the larger one if appropriate.
323 function bnpMultiplyTo(a,r) {
324   var x = this.abs(), y = a.abs();
325   var i = x.t;
326   r.t = i+y.t;
327   while(--i >= 0) r[i] = 0;
328   for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t);
329   r.s = 0;
330   r.clamp();
331   if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
332 }
333
334 // (protected) r = this^2, r != this (HAC 14.16)
335 function bnpSquareTo(r) {
336   var x = this.abs();
337   var i = r.t = 2*x.t;
338   while(--i >= 0) r[i] = 0;
339   for(i = 0; i < x.t-1; ++i) {
340     var c = x.am(i,x[i],r,2*i,0,1);
341     if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x.DV) {
342       r[i+x.t] -= x.DV;
343       r[i+x.t+1] = 1;
344     }
345   }
346   if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1);
347   r.s = 0;
348   r.clamp();
349 }
350
351 // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
352 // r != q, this != m.  q or r may be null.
353 function bnpDivRemTo(m,q,r) {
354   var pm = m.abs();
355   if(pm.t <= 0) return;
356   var pt = this.abs();
357   if(pt.t < pm.t) {
358     if(q != null) q.fromInt(0);
359     if(r != null) this.copyTo(r);
360     return;
361   }
362   if(r == null) r = nbi();
363   var y = nbi(), ts = this.s, ms = m.s;
364   var nsh = this.DB-nbits(pm[pm.t-1]);  // normalize modulus
365   if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
366   else { pm.copyTo(y); pt.copyTo(r); }
367   var ys = y.t;
368   var y0 = y[ys-1];
369   if(y0 == 0) return;
370   var yt = y0*(1<<this.F1)+((ys>1)?y[ys-2]>>this.F2:0);
371   var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2;
372   var i = r.t, j = i-ys, t = (q==null)?nbi():q;
373   y.dlShiftTo(j,t);
374   if(r.compareTo(t) >= 0) {
375     r[r.t++] = 1;
376     r.subTo(t,r);
377   }
378   BigInteger.ONE.dlShiftTo(ys,t);
379   t.subTo(y,y); // "negative" y so we can replace sub with am later
380   while(y.t < ys) y[y.t++] = 0;
381   while(--j >= 0) {
382     // Estimate quotient digit
383     var qd = (r[--i]==y0)?this.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2);
384     if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) {      // Try it out
385       y.dlShiftTo(j,t);
386       r.subTo(t,r);
387       while(r[i] < --qd) r.subTo(t,r);
388     }
389   }
390   if(q != null) {
391     r.drShiftTo(ys,q);
392     if(ts != ms) BigInteger.ZERO.subTo(q,q);
393   }
394   r.t = ys;
395   r.clamp();
396   if(nsh > 0) r.rShiftTo(nsh,r);        // Denormalize remainder
397   if(ts < 0) BigInteger.ZERO.subTo(r,r);
398 }
399
400 // (public) this mod a
401 function bnMod(a) {
402   var r = nbi();
403   this.abs().divRemTo(a,null,r);
404   if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
405   return r;
406 }
407
408 // Modular reduction using "classic" algorithm
409 function Classic(m) { this.m = m; }
410 function cConvert(x) {
411   if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
412   else return x;
413 }
414 function cRevert(x) { return x; }
415 function cReduce(x) { x.divRemTo(this.m,null,x); }
416 function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
417 function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
418
419 Classic.prototype.convert = cConvert;
420 Classic.prototype.revert = cRevert;
421 Classic.prototype.reduce = cReduce;
422 Classic.prototype.mulTo = cMulTo;
423 Classic.prototype.sqrTo = cSqrTo;
424
425 // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
426 // justification:
427 //         xy == 1 (mod m)
428 //         xy =  1+km
429 //   xy(2-xy) = (1+km)(1-km)
430 // x[y(2-xy)] = 1-k^2m^2
431 // x[y(2-xy)] == 1 (mod m^2)
432 // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
433 // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
434 // JS multiply "overflows" differently from C/C++, so care is needed here.
435 function bnpInvDigit() {
436   if(this.t < 1) return 0;
437   var x = this[0];
438   if((x&1) == 0) return 0;
439   var y = x&3;          // y == 1/x mod 2^2
440   y = (y*(2-(x&0xf)*y))&0xf;    // y == 1/x mod 2^4
441   y = (y*(2-(x&0xff)*y))&0xff;  // y == 1/x mod 2^8
442   y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff;   // y == 1/x mod 2^16
443   // last step - calculate inverse mod DV directly;
444   // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
445   y = (y*(2-x*y%this.DV))%this.DV;              // y == 1/x mod 2^dbits
446   // we really want the negative inverse, and -DV < y < DV
447   return (y>0)?this.DV-y:-y;
448 }
449
450 // Montgomery reduction
451 function Montgomery(m) {
452   this.m = m;
453   this.mp = m.invDigit();
454   this.mpl = this.mp&0x7fff;
455   this.mph = this.mp>>15;
456   this.um = (1<<(m.DB-15))-1;
457   this.mt2 = 2*m.t;
458 }
459
460 // xR mod m
461 function montConvert(x) {
462   var r = nbi();
463   x.abs().dlShiftTo(this.m.t,r);
464   r.divRemTo(this.m,null,r);
465   if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
466   return r;
467 }
468
469 // x/R mod m
470 function montRevert(x) {
471   var r = nbi();
472   x.copyTo(r);
473   this.reduce(r);
474   return r;
475 }
476
477 // x = x/R mod m (HAC 14.32)
478 function montReduce(x) {
479   while(x.t <= this.mt2)        // pad x so am has enough room later
480     x[x.t++] = 0;
481   for(var i = 0; i < this.m.t; ++i) {
482     // faster way of calculating u0 = x[i]*mp mod DV
483     var j = x[i]&0x7fff;
484     var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.DM;
485     // use am to combine the multiply-shift-add into one call
486     j = i+this.m.t;
487     x[j] += this.m.am(0,u0,x,i,0,this.m.t);
488     // propagate carry
489     while(x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; }
490   }
491   x.clamp();
492   x.drShiftTo(this.m.t,x);
493   if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
494 }
495
496 // r = "x^2/R mod m"; x != r
497 function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
498
499 // r = "xy/R mod m"; x,y != r
500 function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
501
502 Montgomery.prototype.convert = montConvert;
503 Montgomery.prototype.revert = montRevert;
504 Montgomery.prototype.reduce = montReduce;
505 Montgomery.prototype.mulTo = montMulTo;
506 Montgomery.prototype.sqrTo = montSqrTo;
507
508 // (protected) true iff this is even
509 function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; }
510
511 // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
512 function bnpExp(e,z) {
513   if(e > 0xffffffff || e < 1) return BigInteger.ONE;
514   var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
515   g.copyTo(r);
516   while(--i >= 0) {
517     z.sqrTo(r,r2);
518     if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
519     else { var t = r; r = r2; r2 = t; }
520   }
521   return z.revert(r);
522 }
523
524 // (public) this^e % m, 0 <= e < 2^32
525 function bnModPowInt(e,m) {
526   var z;
527   if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
528   return this.exp(e,z);
529 }
530
531 // protected
532 BigInteger.prototype.copyTo = bnpCopyTo;
533 BigInteger.prototype.fromInt = bnpFromInt;
534 BigInteger.prototype.fromString = bnpFromString;
535 BigInteger.prototype.clamp = bnpClamp;
536 BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
537 BigInteger.prototype.drShiftTo = bnpDRShiftTo;
538 BigInteger.prototype.lShiftTo = bnpLShiftTo;
539 BigInteger.prototype.rShiftTo = bnpRShiftTo;
540 BigInteger.prototype.subTo = bnpSubTo;
541 BigInteger.prototype.multiplyTo = bnpMultiplyTo;
542 BigInteger.prototype.squareTo = bnpSquareTo;
543 BigInteger.prototype.divRemTo = bnpDivRemTo;
544 BigInteger.prototype.invDigit = bnpInvDigit;
545 BigInteger.prototype.isEven = bnpIsEven;
546 BigInteger.prototype.exp = bnpExp;
547
548 // public
549 BigInteger.prototype.toString = bnToString;
550 BigInteger.prototype.negate = bnNegate;
551 BigInteger.prototype.abs = bnAbs;
552 BigInteger.prototype.compareTo = bnCompareTo;
553 BigInteger.prototype.bitLength = bnBitLength;
554 BigInteger.prototype.mod = bnMod;
555 BigInteger.prototype.modPowInt = bnModPowInt;
556
557 // "constants"
558 BigInteger.ZERO = nbv(0);
559 BigInteger.ONE = nbv(1);