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