index.esm.js 14 KB

1
  1. import t from"to-arraybuffer";import{Buffer as r}from"buffer";import{BigInteger as e,SecureRandom as n}from"jsbn";var i=e.prototype.Barrett;function o(t,r){this.x=r,this.q=t}function u(t,r,n,i){this.curve=t,this.x=r,this.y=n,this.z=null==i?e.ONE:i,this.zinv=null}function s(t,r,e){this.q=t,this.a=this.fromBigInteger(r),this.b=this.fromBigInteger(e),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(e.ZERO)&&t.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(t.z)).mod(this.curve.q).equals(e.ZERO))},u.prototype.isInfinity=function(){return null==this.x&&null==this.y||this.z.equals(e.ZERO)&&!this.y.toBigInteger().equals(e.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 r=t.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(t.z)).mod(this.curve.q),n=t.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(t.z)).mod(this.curve.q);if(e.ZERO.equals(n))return e.ZERO.equals(r)?this.twice():this.curve.getInfinity();var i=new e("3"),o=this.x.toBigInteger(),s=this.y.toBigInteger(),f=(t.x.toBigInteger(),t.y.toBigInteger(),n.square()),a=f.multiply(n),h=o.multiply(f),l=r.square().multiply(this.z),c=l.subtract(h.shiftLeft(1)).multiply(t.z).subtract(a).multiply(n).mod(this.curve.q),g=h.multiply(i).multiply(r).subtract(s.multiply(a)).subtract(l.multiply(r)).multiply(t.z).add(r.multiply(a)).mod(this.curve.q),p=a.multiply(this.z).multiply(t.z).mod(this.curve.q);return new u(this.curve,this.curve.fromBigInteger(c),this.curve.fromBigInteger(g),p)},u.prototype.twice=function(){if(this.isInfinity())return this;if(0==this.y.toBigInteger().signum())return this.curve.getInfinity();var t=new e("3"),r=this.x.toBigInteger(),n=this.y.toBigInteger(),i=n.multiply(this.z),o=i.multiply(n).mod(this.curve.q),s=this.curve.a.toBigInteger(),f=r.square().multiply(t);e.ZERO.equals(s)||(f=f.add(this.z.square().multiply(s)));var a=(f=f.mod(this.curve.q)).square().subtract(r.shiftLeft(3).multiply(o)).shiftLeft(1).multiply(i).mod(this.curve.q),h=f.multiply(t).multiply(r).subtract(o.shiftLeft(1)).shiftLeft(2).multiply(o).subtract(f.square().multiply(f)).mod(this.curve.q),l=i.square().multiply(i).shiftLeft(3).mod(this.curve.q);return new u(this.curve,this.curve.fromBigInteger(a),this.curve.fromBigInteger(h),l)},u.prototype.multiply=function(t){if(this.isInfinity())return this;if(0==t.signum())return this.curve.getInfinity();var r,n=t,i=n.multiply(new e("3")),o=this.negate(),u=this;for(r=i.bitLength()-2;r>0;--r){u=u.twice();var s=i.testBit(r);s!=n.testBit(r)&&(u=u.add(s?this:o))}return u},u.prototype.multiplyTwo=function(t,r,e){var n;n=t.bitLength()>e.bitLength()?t.bitLength()-1:e.bitLength()-1;for(var i=this.curve.getInfinity(),o=this.add(r);n>=0;)i=i.twice(),t.testBit(n)?i=e.testBit(n)?i.add(o):i.add(this):e.testBit(n)&&(i=i.add(r)),--n;return i},s.prototype.getQ=function(){return this.q},s.prototype.getA=function(){return this.a},s.prototype.getB=function(){return this.b},s.prototype.equals=function(t){return t==this||this.q.equals(t.q)&&this.a.equals(t.a)&&this.b.equals(t.b)},s.prototype.getInfinity=function(){return this.infinity},s.prototype.fromBigInteger=function(t){return new o(this.q,t)},s.prototype.reduce=function(t){this.reducer.reduce(t)},s.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 r=(t.length-2)/2,n=t.substr(2,r),i=t.substr(r+2,r);return new u(this,this.fromBigInteger(new e(n,16)),this.fromBigInteger(new e(i,16)));default:return null}},s.prototype.encodePointHex=function(t){if(t.isInfinity())return"00";var r=t.getX().toBigInteger().toString(16),e=t.getY().toBigInteger().toString(16),n=this.getQ().toString(16).length;for(n%2!=0&&n++;r.length<n;)r="0"+r;for(;e.length<n;)e="0"+e;return"04"+r+e};var f=function(t,r){return t<<(r%=32)|t>>>32-r},a=function(t,r){var e=r-t.length;return(e>0?"0".repeat(e):"")+t},h=function(t){return t<16?2043430169:2055708042},l=function(t,r,e,n){return n<16?t^r^e:t&r|t&e|r&e},c=function(t,r,e,n){return n<16?t^r^e:t&r|~t&e},g=function(t,r,e){for(var n,i,o,u,s,a=function(t){var r,e=new Array(132);t.forEach(function(t,r){e[r]=t});for(var n=16;n<68;n++)e[n]=(r=e[n-16]^e[n-9]^f(e[n-3],15))^f(r,15)^f(r,23)^f(e[n-13],7)^e[n-6];for(var i=0;i<64;i++)e[i+68]=e[i]^e[i+4];return e}(r),g=t[0],p=t[1],F=t[2],y=t[3],d=t[4],v=t[5],B=t[6],m=t[7],I=0;I<64;I++)i=(n=f(f(g,12)+d+f(h(I),I),7))^f(g,12),o=l(g,p,F,I)+y+i+a[I+68],u=c(d,v,B,I)+m+n+a[I],y=F,F=f(p,9),p=g,g=o,m=B,B=f(v,19),v=d,d=(s=u)^f(s,9)^f(s,17);return[g^t[0],p^t[1],F^t[2],y^t[3],d^t[4],v^t[5],B^t[6],m^t[7]]},p=function(e,n,i){if("string"==typeof e?e=r.from(e,n||"utf8"):e instanceof ArrayBuffer&&(e=r.from(e)),!r.isBuffer(e))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(e)+'"');var o,u,s,f,a,h;o=e,u=r.alloc(1,128),f=r.alloc(s=(s=o.length%64)>=56?64-s%56-1:56-s-1,0),a=r.alloc(8),h=8*o.length,a.writeUInt32BE(Math.floor(h/Math.pow(2,32)),0),a.writeUInt32BE(h%Math.pow(2,32),4);for(var l=(e=r.concat([o,u,f,a],o.length+1+s+8)).length/64,c=new Array(l),p=0;p<l;p++){c[p]=new Array(16);for(var F=0;F<16;F++)c[p][F]=e.readUInt32BE(64*p+4*F)}var y=new Array(l);y[0]=[1937774191,1226093241,388252375,3666478592,2842636476,372324522,3817729613,2969243214];for(var d=0;d<l;d++)y[d+1]=g(y[d],c[d]);var v=r.alloc(32);return y[l].forEach(function(t,r){return v.writeInt32BE(t,4*r)}),i?v.toString(i):t(v)},F={__proto__:null,digest:p},y=new n,d=function(){var t=new s(new e("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",16),new e("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",16),new e("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",16)),r=t.decodePointHex("0432C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0");return{curve:t,G:r,n:new e("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",16)}}(),v=d.curve,B=d.G,m=d.n;function I(t,e){for(var n=[],i=Math.ceil(e/32),o=e%32,u=1;u<=i;u++){var s=r.allocUnsafe(4);s.writeUInt32BE(u);var f=p(r.concat([t,s]));n.push(u===i&&o?r.from(f).slice(0,o):r.from(f))}return r.concat(n,e)}var E={__proto__:null,constants:{C1C2C3:0,C1C3C2:1,PC:"04"},generateKeyPair:function(){var t=new e(m.bitLength(),y).mod(m.subtract(new e("2"))).add(e.ONE),r=a(t.toString(16),64),n=B.multiply(t);return{privateKey:r,publicKey:"04"+a(n.getX().toBigInteger().toString(16),64)+a(n.getY().toBigInteger().toString(16),64)}},encrypt:function(n,i,o){var u=o||{},s=u.mode,f=void 0===s?1:s,h=u.outputEncoding,l=u.pc;if("string"==typeof n?n=r.from(n,u.inputEncoding||"utf8"):n instanceof ArrayBuffer&&(n=r.from(n)),!r.isBuffer(n))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(n)+'"');var c=new e(m.bitLength(),y).mod(m.subtract(e.ONE)).add(e.ONE),g=B.multiply(c),F=a(g.getX().toBigInteger().toString(16),64)+a(g.getY().toBigInteger().toString(16),64),d=v.decodePointHex(i).multiply(c),E=a(d.getX().toBigInteger().toString(16),64),w=a(d.getY().toBigInteger().toString(16),64),q=I(r.from(E+w,"hex"),n.length),x=a(new e(n.toString("hex"),16).xor(new e(q.toString("hex"),16)).toString(16),2*n.length),b=p(E+n.toString("hex")+w,"hex","hex"),S=r.from((l?"04":"")+(0===f?F+x+b:F+b+x),"hex");return h?S.toString(h):t(S)},decrypt:function(n,i,o){var u=o||{},s=u.mode,f=void 0===s?1:s,h=u.outputEncoding,l=u.pc;if("string"==typeof n?n=r.from(n,u.inputEncoding):n instanceof ArrayBuffer&&(n=r.from(n)),!r.isBuffer(n))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(n)+'"');var c=(n=l?n.slice(1):n).slice(0,32).toString("hex"),g=n.slice(32,64).toString("hex"),F=v.decodePointHex("04"+c+g).multiply(new e(i,16)),y=a(F.getX().toBigInteger().toString(16),64),d=a(F.getY().toBigInteger().toString(16),64),B=n.slice(64,96),m=n.slice(96);0===f&&(B=n.slice(n.length-32),m=n.slice(64,n.length-32));var E=I(r.from(y+d,"hex"),m.length),w=new e(m.toString("hex"),16).xor(new e(E.toString("hex"),16)).toString(16),q=p(y+w+d,"hex","hex")===B.toString("hex")?r.from(w,"hex"):r.alloc(0);return h?q.toString(h):t(q)}},w=[[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]],q=[2746333894,1453994832,1736282519,2993693404],x=[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],b=/^[0-9a-f]{32}$/i,S=function(t){return w[(4026531840&t)>>>28][(251658240&t)>>>24]<<24|w[(15728640&t)>>>20][(983040&t)>>>16]<<16|w[(61440&t)>>>12][(3840&t)>>>8]<<8|w[(240&t)>>>4][(15&t)>>>0]<<0},A=function(t){return(r=S(t))^f(r,2)^f(r,10)^f(r,18)^f(r,24);var r},z=function(t){var r,e=new Array(36);e[0]=t[0]^q[0],e[1]=t[1]^q[1],e[2]=t[2]^q[2],e[3]=t[3]^q[3];for(var n=new Array(32),i=0;i<32;i++)e[i+4]=e[i]^(r=S(e[i+1]^e[i+2]^e[i+3]^x[i]))^f(r,13)^f(r,23),n[i]=e[i+4];return n},C=function(t,r){for(var e=z(r),n=0;n<32;n++)t[n+4]=t[n]^A(t[n+1]^t[n+2]^t[n+3]^e[n]);return[t[35],t[34],t[33],t[32]]},O=function(t,r){for(var e=z(r).reverse(),n=0;n<32;n++)t[n+4]=t[n]^A(t[n+1]^t[n+2]^t[n+3]^e[n]);return[t[35],t[34],t[33],t[32]]},L=function(t){return[t.readInt32BE(0),t.readInt32BE(4),t.readInt32BE(8),t.readInt32BE(12)]},_=function(t){for(var e=r.alloc(16),n=0;n<4;n++)e.writeInt32BE(t[n],4*n);return e},D={__proto__:null,constants:{ECB:1,CBC:2},encrypt:function(e,n,i){var o=i||{},u=o.mode,s=o.iv,f=o.outputEncoding;if("string"==typeof e?e=r.from(e,o.inputEncoding||"utf8"):e instanceof ArrayBuffer&&(e=r.from(e)),!r.isBuffer(e))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(e)+'"');if(!b.test(n))throw new TypeError("Invalid value of cipher `key`");if(n=r.from(n,"hex"),2===u&&!b.test(s))throw new TypeError("Invalid value of `iv` option");return function(e,n,i,o){i&&(i=L(i)),n=L(n);for(var u=[],s=(e=function(t){var e=16-t.length%16,n=r.alloc(e,e);return r.concat([t,n],t.length+e)}(e)).length/16,f=0;f<s;f++)if(i){var a=16*f,h=[i[0]^e.readInt32BE(a),i[1]^e.readInt32BE(a+4),i[2]^e.readInt32BE(a+8),i[3]^e.readInt32BE(a+12)],l=C(h,n);u.push(_(l)),i=l.slice(0)}else{var c=16*f,g=[e.readInt32BE(c),e.readInt32BE(c+4),e.readInt32BE(c+8),e.readInt32BE(c+12)],p=C(g,n);u.push(_(p))}var F=r.concat(u,e.length);return o?F.toString(o):t(F)}(e,n,s=2===u?r.from(s,"hex"):null,f)},decrypt:function(e,n,i){var o=i||{},u=o.mode,s=o.iv,f=o.outputEncoding;if("string"==typeof e?e=r.from(e,o.inputEncoding):e instanceof ArrayBuffer&&(e=r.from(e)),!r.isBuffer(e))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(e)+'"');if(!b.test(n))throw new TypeError("Invalid value of cipher `key`");if(n=r.from(n,"hex"),2===u&&!b.test(s))throw new TypeError("Invalid value of `iv` option");return function(e,n,i,o){i&&(i=L(i)),n=L(n);var u=[],s=e.length/16;if(i)for(var f=s-1;f>=0;f--){var a,h=16*f;a=f>0?[e.readInt32BE(h-16),e.readInt32BE(h-16+4),e.readInt32BE(h-16+8),e.readInt32BE(h-16+12)]:i;var l=[e.readInt32BE(h),e.readInt32BE(h+4),e.readInt32BE(h+8),e.readInt32BE(h+12)],c=O(l,n);u.unshift(_([c[0]^a[0],c[1]^a[1],c[2]^a[2],c[3]^a[3]]))}else for(var g=0;g<s;g++){var p=16*g,F=[e.readInt32BE(p),e.readInt32BE(p+4),e.readInt32BE(p+8),e.readInt32BE(p+12)],y=O(F,n);u.push(_(y))}var d=r.concat(u,e.length-u[u.length-1][15]);return o?d.toString(o):t(d)}(e,n,s=2===u?r.from(s,"hex"):null,f)}};export{E as SM2,F as SM3,D as SM4};