index.umd.js 15 KB

1
  1. !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("to-arraybuffer"),require("buffer"),require("jsbn")):"function"==typeof define&&define.amd?define(["exports","to-arraybuffer","buffer","jsbn"],e):e((t=t||self).gmCrypto={},t.toArraybuffer,t.buffer,t.jsbn)}(this,function(t,e,r,n){e=e&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e;var i=n.BigInteger.prototype.Barrett;function o(t,e){this.x=e,this.q=t}function u(t,e,r,i){this.curve=t,this.x=e,this.y=r,this.z=null==i?n.BigInteger.ONE:i,this.zinv=null}function f(t,e,r){this.q=t,this.a=this.fromBigInteger(e),this.b=this.fromBigInteger(r),this.infinity=new u(this,null,null),this.reducer=new i(this.q)}o.prototype.equals=function(t){return t==this||this.q.equals(t.q)&&this.x.equals(t.x)},o.prototype.toBigInteger=function(){return this.x},o.prototype.negate=function(){return new o(this.q,this.x.negate().mod(this.q))},o.prototype.add=function(t){return new o(this.q,this.x.add(t.toBigInteger()).mod(this.q))},o.prototype.subtract=function(t){return new o(this.q,this.x.subtract(t.toBigInteger()).mod(this.q))},o.prototype.multiply=function(t){return new o(this.q,this.x.multiply(t.toBigInteger()).mod(this.q))},o.prototype.square=function(){return new o(this.q,this.x.square().mod(this.q))},o.prototype.divide=function(t){return new o(this.q,this.x.multiply(t.toBigInteger().modInverse(this.q)).mod(this.q))},u.prototype.getX=function(){null==this.zinv&&(this.zinv=this.z.modInverse(this.curve.q));var t=this.x.toBigInteger().multiply(this.zinv);return this.curve.reduce(t),this.curve.fromBigInteger(t)},u.prototype.getY=function(){null==this.zinv&&(this.zinv=this.z.modInverse(this.curve.q));var t=this.y.toBigInteger().multiply(this.zinv);return this.curve.reduce(t),this.curve.fromBigInteger(t)},u.prototype.equals=function(t){return t==this||(this.isInfinity()?t.isInfinity():t.isInfinity()?this.isInfinity():!!t.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(t.z)).mod(this.curve.q).equals(n.BigInteger.ZERO)&&t.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(t.z)).mod(this.curve.q).equals(n.BigInteger.ZERO))},u.prototype.isInfinity=function(){return null==this.x&&null==this.y||this.z.equals(n.BigInteger.ZERO)&&!this.y.toBigInteger().equals(n.BigInteger.ZERO)},u.prototype.negate=function(){return new u(this.curve,this.x,this.y.negate(),this.z)},u.prototype.add=function(t){if(this.isInfinity())return t;if(t.isInfinity())return this;var e=t.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(t.z)).mod(this.curve.q),r=t.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(t.z)).mod(this.curve.q);if(n.BigInteger.ZERO.equals(r))return n.BigInteger.ZERO.equals(e)?this.twice():this.curve.getInfinity();var i=new n.BigInteger("3"),o=this.x.toBigInteger(),f=this.y.toBigInteger(),s=(t.x.toBigInteger(),t.y.toBigInteger(),r.square()),g=s.multiply(r),a=o.multiply(s),h=e.square().multiply(this.z),c=h.subtract(a.shiftLeft(1)).multiply(t.z).subtract(g).multiply(r).mod(this.curve.q),l=a.multiply(i).multiply(e).subtract(f.multiply(g)).subtract(h.multiply(e)).multiply(t.z).add(e.multiply(g)).mod(this.curve.q),B=g.multiply(this.z).multiply(t.z).mod(this.curve.q);return new u(this.curve,this.curve.fromBigInteger(c),this.curve.fromBigInteger(l),B)},u.prototype.twice=function(){if(this.isInfinity())return this;if(0==this.y.toBigInteger().signum())return this.curve.getInfinity();var t=new n.BigInteger("3"),e=this.x.toBigInteger(),r=this.y.toBigInteger(),i=r.multiply(this.z),o=i.multiply(r).mod(this.curve.q),f=this.curve.a.toBigInteger(),s=e.square().multiply(t);n.BigInteger.ZERO.equals(f)||(s=s.add(this.z.square().multiply(f)));var g=(s=s.mod(this.curve.q)).square().subtract(e.shiftLeft(3).multiply(o)).shiftLeft(1).multiply(i).mod(this.curve.q),a=s.multiply(t).multiply(e).subtract(o.shiftLeft(1)).shiftLeft(2).multiply(o).subtract(s.square().multiply(s)).mod(this.curve.q),h=i.square().multiply(i).shiftLeft(3).mod(this.curve.q);return new u(this.curve,this.curve.fromBigInteger(g),this.curve.fromBigInteger(a),h)},u.prototype.multiply=function(t){if(this.isInfinity())return this;if(0==t.signum())return this.curve.getInfinity();var e,r=t,i=r.multiply(new n.BigInteger("3")),o=this.negate(),u=this;for(e=i.bitLength()-2;e>0;--e){u=u.twice();var f=i.testBit(e);f!=r.testBit(e)&&(u=u.add(f?this:o))}return u},u.prototype.multiplyTwo=function(t,e,r){var n;n=t.bitLength()>r.bitLength()?t.bitLength()-1:r.bitLength()-1;for(var i=this.curve.getInfinity(),o=this.add(e);n>=0;)i=i.twice(),t.testBit(n)?i=r.testBit(n)?i.add(o):i.add(this):r.testBit(n)&&(i=i.add(e)),--n;return i},f.prototype.getQ=function(){return this.q},f.prototype.getA=function(){return this.a},f.prototype.getB=function(){return this.b},f.prototype.equals=function(t){return t==this||this.q.equals(t.q)&&this.a.equals(t.a)&&this.b.equals(t.b)},f.prototype.getInfinity=function(){return this.infinity},f.prototype.fromBigInteger=function(t){return new o(this.q,t)},f.prototype.reduce=function(t){this.reducer.reduce(t)},f.prototype.decodePointHex=function(t){switch(parseInt(t.substr(0,2),16)){case 0:return this.infinity;case 2:case 3:return null;case 4:case 6:case 7:var e=(t.length-2)/2,r=t.substr(2,e),i=t.substr(e+2,e);return new u(this,this.fromBigInteger(new n.BigInteger(r,16)),this.fromBigInteger(new n.BigInteger(i,16)));default:return null}},f.prototype.encodePointHex=function(t){if(t.isInfinity())return"00";var e=t.getX().toBigInteger().toString(16),r=t.getY().toBigInteger().toString(16),n=this.getQ().toString(16).length;for(n%2!=0&&n++;e.length<n;)e="0"+e;for(;r.length<n;)r="0"+r;return"04"+e+r};var s=function(t,e){return t<<(e%=32)|t>>>32-e},g=function(t,e){var r=e-t.length;return(r>0?"0".repeat(r):"")+t},a=function(t){return t<16?2043430169:2055708042},h=function(t,e,r,n){return n<16?t^e^r:t&e|t&r|e&r},c=function(t,e,r,n){return n<16?t^e^r:t&e|~t&r},l=function(t,e,r){for(var n,i,o,u,f,g=function(t){var e,r=new Array(132);t.forEach(function(t,e){r[e]=t});for(var n=16;n<68;n++)r[n]=(e=r[n-16]^r[n-9]^s(r[n-3],15))^s(e,15)^s(e,23)^s(r[n-13],7)^r[n-6];for(var i=0;i<64;i++)r[i+68]=r[i]^r[i+4];return r}(e),l=t[0],B=t[1],p=t[2],F=t[3],y=t[4],d=t[5],I=t[6],v=t[7],m=0;m<64;m++)i=(n=s(s(l,12)+y+s(a(m),m),7))^s(l,12),o=h(l,B,p,m)+F+i+g[m+68],u=c(y,d,I,m)+v+n+g[m],F=p,p=s(B,9),B=l,l=o,v=I,I=s(d,19),d=y,y=(f=u)^s(f,9)^s(f,17);return[l^t[0],B^t[1],p^t[2],F^t[3],y^t[4],d^t[5],I^t[6],v^t[7]]},B=function(t,n,i){if("string"==typeof t?t=r.Buffer.from(t,n||"utf8"):t instanceof ArrayBuffer&&(t=r.Buffer.from(t)),!r.Buffer.isBuffer(t))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(t)+'"');var o,u,f,s,g,a;o=t,u=r.Buffer.alloc(1,128),s=r.Buffer.alloc(f=(f=o.length%64)>=56?64-f%56-1:56-f-1,0),g=r.Buffer.alloc(8),a=8*o.length,g.writeUInt32BE(Math.floor(a/Math.pow(2,32)),0),g.writeUInt32BE(a%Math.pow(2,32),4);for(var h=(t=r.Buffer.concat([o,u,s,g],o.length+1+f+8)).length/64,c=new Array(h),B=0;B<h;B++){c[B]=new Array(16);for(var p=0;p<16;p++)c[B][p]=t.readUInt32BE(64*B+4*p)}var F=new Array(h);F[0]=[1937774191,1226093241,388252375,3666478592,2842636476,372324522,3817729613,2969243214];for(var y=0;y<h;y++)F[y+1]=l(F[y],c[y]);var d=r.Buffer.alloc(32);return F[h].forEach(function(t,e){return d.writeInt32BE(t,4*e)}),i?d.toString(i):e(d)},p={__proto__:null,digest:B},F=new n.SecureRandom,y=function(){var t=new f(new n.BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",16),new n.BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",16),new n.BigInteger("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",16)),e=t.decodePointHex("0432C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0");return{curve:t,G:e,n:new n.BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",16)}}(),d=y.curve,I=y.G,v=y.n;function m(t,e){for(var n=[],i=Math.ceil(e/32),o=e%32,u=1;u<=i;u++){var f=r.Buffer.allocUnsafe(4);f.writeUInt32BE(u);var s=B(r.Buffer.concat([t,f]));n.push(u===i&&o?r.Buffer.from(s).slice(0,o):r.Buffer.from(s))}return r.Buffer.concat(n,e)}var E=[[214,144,233,254,204,225,61,183,22,182,20,194,40,251,44,5],[43,103,154,118,42,190,4,195,170,68,19,38,73,134,6,153],[156,66,80,244,145,239,152,122,51,84,11,67,237,207,172,98],[228,179,28,169,201,8,232,149,128,223,148,250,117,143,63,166],[71,7,167,252,243,115,23,186,131,89,60,25,230,133,79,168],[104,107,129,178,113,100,218,139,248,235,15,75,112,86,157,53],[30,36,14,94,99,88,209,162,37,34,124,59,1,33,120,135],[212,0,70,87,159,211,39,82,76,54,2,231,160,196,200,158],[234,191,138,210,64,199,56,181,163,247,242,206,249,97,21,161],[224,174,93,164,155,52,26,85,173,147,50,48,245,140,177,227],[29,246,226,46,130,102,202,96,192,41,35,171,13,83,78,111],[213,219,55,69,222,253,142,47,3,255,106,114,109,108,91,81],[141,27,175,146,187,221,188,127,17,217,92,65,31,16,90,216],[10,193,49,136,165,205,123,189,45,116,208,18,184,229,180,176],[137,105,151,74,12,150,119,126,101,185,241,9,197,110,198,132],[24,240,125,236,58,220,77,32,121,238,95,62,215,203,57,72]],w=[2746333894,1453994832,1736282519,2993693404],q=[462357,472066609,943670861,1415275113,1886879365,2358483617,2830087869,3301692121,3773296373,4228057617,404694573,876298825,1347903077,1819507329,2291111581,2762715833,3234320085,3705924337,4177462797,337322537,808926789,1280531041,1752135293,2223739545,2695343797,3166948049,3638552301,4110090761,269950501,741554753,1213159005,1684763257],x=/^[0-9a-f]{32}$/i,b=function(t){return E[(4026531840&t)>>>28][(251658240&t)>>>24]<<24|E[(15728640&t)>>>20][(983040&t)>>>16]<<16|E[(61440&t)>>>12][(3840&t)>>>8]<<8|E[(240&t)>>>4][(15&t)>>>0]<<0},S=function(t){return(e=b(t))^s(e,2)^s(e,10)^s(e,18)^s(e,24);var e},A=function(t){var e,r=new Array(36);r[0]=t[0]^w[0],r[1]=t[1]^w[1],r[2]=t[2]^w[2],r[3]=t[3]^w[3];for(var n=new Array(32),i=0;i<32;i++)r[i+4]=r[i]^(e=b(r[i+1]^r[i+2]^r[i+3]^q[i]))^s(e,13)^s(e,23),n[i]=r[i+4];return n},z=function(t,e){for(var r=A(e),n=0;n<32;n++)t[n+4]=t[n]^S(t[n+1]^t[n+2]^t[n+3]^r[n]);return[t[35],t[34],t[33],t[32]]},C=function(t,e){for(var r=A(e).reverse(),n=0;n<32;n++)t[n+4]=t[n]^S(t[n+1]^t[n+2]^t[n+3]^r[n]);return[t[35],t[34],t[33],t[32]]},O=function(t){return[t.readInt32BE(0),t.readInt32BE(4),t.readInt32BE(8),t.readInt32BE(12)]},L=function(t){for(var e=r.Buffer.alloc(16),n=0;n<4;n++)e.writeInt32BE(t[n],4*n);return e},_={__proto__:null,constants:{ECB:1,CBC:2},encrypt:function(t,n,i){var o=i||{},u=o.mode,f=o.iv,s=o.outputEncoding;if("string"==typeof t?t=r.Buffer.from(t,o.inputEncoding||"utf8"):t instanceof ArrayBuffer&&(t=r.Buffer.from(t)),!r.Buffer.isBuffer(t))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(t)+'"');if(!x.test(n))throw new TypeError("Invalid value of cipher `key`");if(n=r.Buffer.from(n,"hex"),2===u&&!x.test(f))throw new TypeError("Invalid value of `iv` option");return function(t,n,i,o){i&&(i=O(i)),n=O(n);for(var u=[],f=(t=function(t){var e=16-t.length%16,n=r.Buffer.alloc(e,e);return r.Buffer.concat([t,n],t.length+e)}(t)).length/16,s=0;s<f;s++)if(i){var g=16*s,a=[i[0]^t.readInt32BE(g),i[1]^t.readInt32BE(g+4),i[2]^t.readInt32BE(g+8),i[3]^t.readInt32BE(g+12)],h=z(a,n);u.push(L(h)),i=h.slice(0)}else{var c=16*s,l=[t.readInt32BE(c),t.readInt32BE(c+4),t.readInt32BE(c+8),t.readInt32BE(c+12)],B=z(l,n);u.push(L(B))}var p=r.Buffer.concat(u,t.length);return o?p.toString(o):e(p)}(t,n,f=2===u?r.Buffer.from(f,"hex"):null,s)},decrypt:function(t,n,i){var o=i||{},u=o.mode,f=o.iv,s=o.outputEncoding;if("string"==typeof t?t=r.Buffer.from(t,o.inputEncoding):t instanceof ArrayBuffer&&(t=r.Buffer.from(t)),!r.Buffer.isBuffer(t))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(t)+'"');if(!x.test(n))throw new TypeError("Invalid value of cipher `key`");if(n=r.Buffer.from(n,"hex"),2===u&&!x.test(f))throw new TypeError("Invalid value of `iv` option");return function(t,n,i,o){i&&(i=O(i)),n=O(n);var u=[],f=t.length/16;if(i)for(var s=f-1;s>=0;s--){var g,a=16*s;g=s>0?[t.readInt32BE(a-16),t.readInt32BE(a-16+4),t.readInt32BE(a-16+8),t.readInt32BE(a-16+12)]:i;var h=[t.readInt32BE(a),t.readInt32BE(a+4),t.readInt32BE(a+8),t.readInt32BE(a+12)],c=C(h,n);u.unshift(L([c[0]^g[0],c[1]^g[1],c[2]^g[2],c[3]^g[3]]))}else for(var l=0;l<f;l++){var B=16*l,p=[t.readInt32BE(B),t.readInt32BE(B+4),t.readInt32BE(B+8),t.readInt32BE(B+12)],F=C(p,n);u.push(L(F))}var y=r.Buffer.concat(u,t.length-u[u.length-1][15]);return o?y.toString(o):e(y)}(t,n,f=2===u?r.Buffer.from(f,"hex"):null,s)}};t.SM2={__proto__:null,constants:{C1C2C3:0,C1C3C2:1,PC:"04"},generateKeyPair:function(){var t=new n.BigInteger(v.bitLength(),F).mod(v.subtract(new n.BigInteger("2"))).add(n.BigInteger.ONE),e=g(t.toString(16),64),r=I.multiply(t);return{privateKey:e,publicKey:"04"+g(r.getX().toBigInteger().toString(16),64)+g(r.getY().toBigInteger().toString(16),64)}},encrypt:function(t,i,o){var u=o||{},f=u.mode,s=void 0===f?1:f,a=u.outputEncoding,h=u.pc;if("string"==typeof t?t=r.Buffer.from(t,u.inputEncoding||"utf8"):t instanceof ArrayBuffer&&(t=r.Buffer.from(t)),!r.Buffer.isBuffer(t))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(t)+'"');var c=new n.BigInteger(v.bitLength(),F).mod(v.subtract(n.BigInteger.ONE)).add(n.BigInteger.ONE),l=I.multiply(c),p=g(l.getX().toBigInteger().toString(16),64)+g(l.getY().toBigInteger().toString(16),64),y=d.decodePointHex(i).multiply(c),E=g(y.getX().toBigInteger().toString(16),64),w=g(y.getY().toBigInteger().toString(16),64),q=m(r.Buffer.from(E+w,"hex"),t.length),x=g(new n.BigInteger(t.toString("hex"),16).xor(new n.BigInteger(q.toString("hex"),16)).toString(16),2*t.length),b=B(E+t.toString("hex")+w,"hex","hex"),S=r.Buffer.from((h?"04":"")+(0===s?p+x+b:p+b+x),"hex");return a?S.toString(a):e(S)},decrypt:function(t,i,o){var u=o||{},f=u.mode,s=void 0===f?1:f,a=u.outputEncoding,h=u.pc;if("string"==typeof t?t=r.Buffer.from(t,u.inputEncoding):t instanceof ArrayBuffer&&(t=r.Buffer.from(t)),!r.Buffer.isBuffer(t))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(t)+'"');var c=(t=h?t.slice(1):t).slice(0,32).toString("hex"),l=t.slice(32,64).toString("hex"),p=d.decodePointHex("04"+c+l).multiply(new n.BigInteger(i,16)),F=g(p.getX().toBigInteger().toString(16),64),y=g(p.getY().toBigInteger().toString(16),64),I=t.slice(64,96),v=t.slice(96);0===s&&(I=t.slice(t.length-32),v=t.slice(64,t.length-32));var E=m(r.Buffer.from(F+y,"hex"),v.length),w=new n.BigInteger(v.toString("hex"),16).xor(new n.BigInteger(E.toString("hex"),16)).toString(16),q=B(F+w+y,"hex","hex")===I.toString("hex")?r.Buffer.from(w,"hex"):r.Buffer.alloc(0);return a?q.toString(a):e(q)}},t.SM3=p,t.SM4=_});