michael@0: // |jit-test| error: TypeError michael@0: function BigInteger(a,b,c) { michael@0: this.array = new Array(); michael@0: if(a != null) michael@0: if("number" == typeof a) this.fromNumber(a,b,c); michael@0: else this.fromString(a,b); michael@0: } michael@0: function nbi() { return new BigInteger(null); } michael@0: function am3(i,x,w,j,c,n) {} michael@0: setupEngine = function(fn, bits) { michael@0: dbits = bits; michael@0: BI_DB = dbits; michael@0: BI_DM = ((1<= 0) { michael@0: var x = (k==8)?s[i]&0xff:intAt(s,i); michael@0: if(sh == 0) michael@0: this_array[this.t++] = x; michael@0: else if(sh+k > BI_DB) { michael@0: this_array[this.t++] = (x>>(BI_DB-sh)); michael@0: } michael@0: sh += k; michael@0: } michael@0: } michael@0: function bnAbs() { return (this.s<0)?this.negate():this; } michael@0: function nbits(x) { michael@0: var r = 1, t; michael@0: return r; michael@0: } michael@0: function bnBitLength() {} michael@0: function bnpDLShiftTo(n,r) { michael@0: var this_array = this.array; michael@0: var r_array = r.array; michael@0: for(i = this.t-1; i >= 0; --i) r_array[i+n] = this_array[i]; michael@0: r.t = this.t+n; michael@0: } michael@0: function bnpLShiftTo(n,r) { michael@0: var bs = n%BI_DB; michael@0: var ds = Math.floor(n/BI_DB), c = (this.s< 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); } michael@0: var ys = y.t; michael@0: var i = r.t, j = i-ys, t = (q==null)?nbi():q; michael@0: y.dlShiftTo(j,t); michael@0: BigInteger.ONE.dlShiftTo(ys,t); michael@0: } michael@0: function bnMod(a) { michael@0: var r = nbi(); michael@0: this.abs().divRemTo(a,null,r); michael@0: } michael@0: function Montgomery(m) { michael@0: this.m = m; michael@0: } michael@0: function montConvert(x) { michael@0: var r = nbi(); michael@0: x.abs().dlShiftTo(this.m.t,r); michael@0: r.divRemTo(this.m,null,r); michael@0: } michael@0: function montRevert(x) { michael@0: var r = nbi(); michael@0: return r; michael@0: } michael@0: Montgomery.prototype.convert = montConvert; michael@0: Montgomery.prototype.revert = montRevert; michael@0: function bnpIsEven() {} michael@0: function bnpExp(e,z) { michael@0: var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1; michael@0: return z.revert(r); michael@0: } michael@0: function bnModPowInt(e,m) { michael@0: if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m); michael@0: return this.exp(e,z); michael@0: } michael@0: BigInteger.prototype.fromInt = bnpFromInt; michael@0: BigInteger.prototype.fromString = bnpFromString; michael@0: BigInteger.prototype.dlShiftTo = bnpDLShiftTo; michael@0: BigInteger.prototype.lShiftTo = bnpLShiftTo; michael@0: BigInteger.prototype.divRemTo = bnpDivRemTo; michael@0: BigInteger.prototype.isEven = bnpIsEven; michael@0: BigInteger.prototype.exp = bnpExp; michael@0: BigInteger.prototype.abs = bnAbs; michael@0: BigInteger.prototype.bitLength = bnBitLength; michael@0: BigInteger.prototype.mod = bnMod; michael@0: BigInteger.prototype.modPowInt = bnModPowInt; michael@0: BigInteger.ONE = nbv(1); michael@0: function parseBigInt(str,r) { michael@0: return new BigInteger(str,r); michael@0: } michael@0: function pkcs1pad2(s,n) { michael@0: var ba = new Array(); michael@0: return new BigInteger(ba); michael@0: } michael@0: function RSAKey() { michael@0: } michael@0: function RSASetPublic(N,E) { michael@0: this.n = parseBigInt(N,16); michael@0: } michael@0: function RSADoPublic(x) { michael@0: return x.modPowInt(this.e, this.n); michael@0: } michael@0: function RSAEncrypt(text) { michael@0: var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3); michael@0: var c = this.doPublic(m); michael@0: var h = c.toString(16); michael@0: if((h.length & 1) == 0) return h; else return "0" + h; michael@0: } michael@0: RSAKey.prototype.doPublic = RSADoPublic; michael@0: RSAKey.prototype.setPublic = RSASetPublic; michael@0: RSAKey.prototype.encrypt = RSAEncrypt; michael@0: function RSASetPrivateEx(N,E,D,P,Q,DP,DQ,C) { michael@0: this.p = parseBigInt(P,16); michael@0: } michael@0: function RSADoPrivate(x) { michael@0: var xp = x.mod(this.p).modPow(this.dmp1, this.p); michael@0: } michael@0: function RSADecrypt(ctext) { michael@0: var c = parseBigInt(ctext, 16); michael@0: var m = this.doPrivate(c); michael@0: } michael@0: RSAKey.prototype.doPrivate = RSADoPrivate; michael@0: RSAKey.prototype.setPrivateEx = RSASetPrivateEx; michael@0: RSAKey.prototype.decrypt = RSADecrypt; michael@0: nValue="a5261939975948bb7a58dffe5ff54e65f0498f9175f5a09288810b8975871e99af3b5dd94057b0fc07535f5f97444504fa35169d461d0d30cf0192e307727c065168c788771c561a9400fb49175e9e6aa4e23fe11af69e9412dd23b0cb6684c4c2429bce139e848ab26d0829073351f4acd36074eafd036a5eb83359d2a698d3"; michael@0: eValue="10001"; michael@0: dValue="8e9912f6d3645894e8d38cb58c0db81ff516cf4c7e5a14c7f1eddb1459d2cded4d8d293fc97aee6aefb861859c8b6a3d1dfe710463e1f9ddc72048c09751971c4a580aa51eb523357a3cc48d31cfad1d4a165066ed92d4748fb6571211da5cb14bc11b6e2df7c1a559e6d5ac1cd5c94703a22891464fba23d0d965086277a161"; michael@0: pValue="d090ce58a92c75233a6486cb0a9209bf3583b64f540c76f5294bb97d285eed33aec220bde14b2417951178ac152ceab6da7090905b478195498b352048f15e7d"; michael@0: qValue="cab575dc652bb66df15a0359609d51d1db184750c00c6698b90ef3465c99655103edbf0d54c56aec0ce3c4d22592338092a126a0cc49f65a4a30d222b411e58f"; michael@0: dmp1Value="1a24bca8e273df2f0e47c199bbf678604e7df7215480c77c8db39f49b000ce2cf7500038acfff5433b7d582a01f1826e6f4d42e1c57f5e1fef7b12aabc59fd25"; michael@0: dmq1Value="3d06982efbbe47339e1f6d36b1216b8a741d410b0c662f54f7118b27b9a4ec9d914337eb39841d8666f3034408cf94f5b62f11c402fc994fe15a05493150d9fd"; michael@0: coeffValue="3a3e731acd8960b7ff9eb81a7ff93bd1cfa74cbd56987db58b4594fb09c09084db1734c8143f98b602b981aaa9243ca28deb69b5b280ee8dcee0fd2625e53250"; michael@0: setupEngine(am3, 28); michael@0: function check_correctness(text, hash) { michael@0: var RSA = new RSAKey(); michael@0: RSA.setPublic(nValue, eValue); michael@0: RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue); michael@0: var encrypted = RSA.encrypt(text); michael@0: var decrypted = RSA.decrypt(encrypted); michael@0: } michael@0: check_correctness("Hello! I am some text.", "142b19b40fee712ab9468be296447d38c7dfe81a7850f11ae6aa21e49396a4e90bd6ba4aa385105e15960a59f95447dfad89671da6e08ed42229939583753be84d07558abb4feee4d46a92fd31d962679a1a5f4bf0fb7af414b9a756e18df7e6d1e96971cc66769f3b27d61ad932f2211373e0de388dc040557d4c3c3fe74320");