UNPKG

bif-sm

Version:

An implementation of GM/T industry standards

2 lines (1 loc) 17.6 kB
!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).bifSm={},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(),g=(t.x.toBigInteger(),t.y.toBigInteger(),r.square()),s=g.multiply(r),a=o.multiply(g),l=e.square().multiply(this.z),c=l.subtract(a.shiftLeft(1)).multiply(t.z).subtract(s).multiply(r).mod(this.curve.q),h=a.multiply(i).multiply(e).subtract(f.multiply(s)).subtract(l.multiply(e)).multiply(t.z).add(e.multiply(s)).mod(this.curve.q),B=s.multiply(this.z).multiply(t.z).mod(this.curve.q);return new u(this.curve,this.curve.fromBigInteger(c),this.curve.fromBigInteger(h),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(),g=e.square().multiply(t);n.BigInteger.ZERO.equals(f)||(g=g.add(this.z.square().multiply(f)));var s=(g=g.mod(this.curve.q)).square().subtract(e.shiftLeft(3).multiply(o)).shiftLeft(1).multiply(i).mod(this.curve.q),a=g.multiply(t).multiply(e).subtract(o.shiftLeft(1)).shiftLeft(2).multiply(o).subtract(g.square().multiply(g)).mod(this.curve.q),l=i.square().multiply(i).shiftLeft(3).mod(this.curve.q);return new u(this.curve,this.curve.fromBigInteger(s),this.curve.fromBigInteger(a),l)},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 g="04",s=function(t,e){return t<<(e%=32)|t>>>32-e},a=function(t,e){var r=e-t.length;return(r>0?"0".repeat(r):"")+t},l=function(t){var e=r.Buffer.alloc(1,128),n=t.length%64,i=r.Buffer.alloc(n=n>=56?64-n%56-1:56-n-1,0),o=r.Buffer.alloc(8),u=8*t.length;return o.writeUInt32BE(Math.floor(u/Math.pow(2,32)),0),o.writeUInt32BE(u%Math.pow(2,32),4),r.Buffer.concat([t,e,i,o],t.length+1+n+8)},c=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},B=function(t,e,r,n){return n<16?t^e^r:t&e|~t&r},p=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),a=t[0],l=t[1],p=t[2],y=t[3],d=t[4],I=t[5],F=t[6],v=t[7],m=0;m<64;m++)i=(n=s(s(a,12)+d+s(c(m),m),7))^s(a,12),o=h(a,l,p,m)+y+i+g[m+68],u=B(d,I,F,m)+v+n+g[m],y=p,p=s(l,9),l=a,a=o,v=F,F=s(I,19),I=d,d=(f=u)^s(f,9)^s(f,17);return[a^t[0],l^t[1],p^t[2],y^t[3],d^t[4],I^t[5],F^t[6],v^t[7]]},y=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)+'"');for(var o=(t=l(t)).length/64,u=new Array(o),f=0;f<o;f++){u[f]=new Array(16);for(var g=0;g<16;g++)u[f][g]=t.readUInt32BE(64*f+4*g)}var s=new Array(o);s[0]=[1937774191,1226093241,388252375,3666478592,2842636476,372324522,3817729613,2969243214];for(var a=0;a<o;a++)s[a+1]=p(s[a],u[a]);var c=r.Buffer.alloc(32);return s[o].forEach(function(t,e){return c.writeInt32BE(t,4*e)}),i?c.toString(i):e(c)},d={__proto__:null,padding:l,digest:y},I={C1C2C3:0,C1C3C2:1,PC:g},F=new n.SecureRandom,v=function(){var t=new n.BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",16),e=new n.BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",16),r=new n.BigInteger("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",16),i=new f(t,e,r),o="32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",u="BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0",s=i.decodePointHex(g+o+u);return{a:e,b:r,gx:o,gy:u,curve:i,G:s,n:new n.BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",16)}}(),m=v.a,E=v.b,w=v.gx,q=v.gy,x=v.curve,b=v.G,S=v.n;function A(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 g=y(r.Buffer.concat([t,f]));n.push(u===i&&o?r.Buffer.from(g).slice(0,o):r.Buffer.from(g))}return r.Buffer.concat(n,e)}var O=function(){var t=new n.BigInteger(S.bitLength(),F).mod(S.subtract(new n.BigInteger("2"))).add(n.BigInteger.ONE),e=a(t.toString(16),64),r=b.multiply(t),i=a(r.getX().toBigInteger().toString(16),64),o=a(r.getY().toBigInteger().toString(16),64);return{privateKey:e,publicKey:g+i+o}};function z(){var t=[m,E,new n.BigInteger(w,16),new n.BigInteger(q,16)],e=[];return t.forEach(function(t){var r=t.toByteArray();e=e.concat(r.length>32?r.slice(-32):r)}),r.Buffer.from(y(r.Buffer.from(e)))}function C(){return z()}function R(t,e){t instanceof ArrayBuffer?t=r.Buffer.from(t):"string"==typeof t&&(t.startsWith("0x")&&(t=t.substring(2)),t=r.Buffer.from(t));try{e="string"==typeof e?e:e.toString("hex")}catch(t){console.error("privateKey转化有问题")}e.startsWith("0x")&&(e=e.substring(2));var i,o,u=C(),f=r.Buffer.concat([u,t]),g=new n.BigInteger(e,16),s=new n.BigInteger(y(f,"buffer","hex"),16),a=null,l=null,c=null,h=null;do{do{var B=(i=void 0,o=void 0,i=O(),o=x.decodePointHex(i.publicKey),i.k=new n.BigInteger(i.privateKey,16),i.x1=o.getX().toBigInteger(),i.y1=o.getY().toBigInteger(),i);a=B.k,l=s.add(B.x1).mod(S),h=0==B.y1.mod(new n.BigInteger("2",10))?n.BigInteger.ZERO:n.BigInteger.ONE}while(l.equals(n.BigInteger.ZERO)||l.add(a).equals(S));c=g.add(n.BigInteger.ONE).modInverse(S).multiply(a.subtract(l.multiply(g))).mod(S)}while(c.equals(n.BigInteger.ZERO));return{signature:l.toString(16)+c.toString(16)+"0"+h.toString(16),v:h,r:l,s:c}}function Z(t,e,i){var o="string"==typeof t?t:t.toString("hex"),u=r.Buffer.concat([C(),r.Buffer.from(o)]),f=new n.BigInteger(y(u,"buffer","hex"),16),g=new n.BigInteger(e.substring(0,64),16),s=new n.BigInteger(e.substring(64,128),16),a=x.decodePointHex(i),l=g.add(s).mod(S);if(l.equals(n.BigInteger.ZERO))return!1;var c=b.multiply(s).add(a.multiply(l)),h=f.add(c.getX().toBigInteger()).mod(S);return g.equals(h)}var L=[[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]],_=[2746333894,1453994832,1736282519,2993693404],D=[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],P=/^[0-9a-f]{32}$/i,j=function(t){return L[(4026531840&t)>>>28][(251658240&t)>>>24]<<24|L[(15728640&t)>>>20][(983040&t)>>>16]<<16|L[(61440&t)>>>12][(3840&t)>>>8]<<8|L[(240&t)>>>4][(15&t)>>>0]<<0},N=function(t){return(e=j(t))^s(e,2)^s(e,10)^s(e,18)^s(e,24);var e},T=function(t){var e,r=new Array(36);r[0]=t[0]^_[0],r[1]=t[1]^_[1],r[2]=t[2]^_[2],r[3]=t[3]^_[3];for(var n=new Array(32),i=0;i<32;i++)r[i+4]=r[i]^(e=j(r[i+1]^r[i+2]^r[i+3]^D[i]))^s(e,13)^s(e,23),n[i]=r[i+4];return n},X=function(t,e){for(var r=T(e),n=0;n<32;n++)t[n+4]=t[n]^N(t[n+1]^t[n+2]^t[n+3]^r[n]);return[t[35],t[34],t[33],t[32]]},K=function(t,e){for(var r=T(e).reverse(),n=0;n<32;n++)t[n+4]=t[n]^N(t[n+1]^t[n+2]^t[n+3]^r[n]);return[t[35],t[34],t[33],t[32]]},Y=function(t){return[t.readInt32BE(0),t.readInt32BE(4),t.readInt32BE(8),t.readInt32BE(12)]},H=function(t){for(var e=r.Buffer.alloc(16),n=0;n<4;n++)e.writeInt32BE(t[n],4*n);return e},M={__proto__:null,constants:{ECB:1,CBC:2},encrypt:function(t,n,i){var o=i||{},u=o.mode,f=o.iv,g=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(!P.test(n))throw new TypeError("Invalid value of cipher `key`");if(n=r.Buffer.from(n,"hex"),2===u&&!P.test(f))throw new TypeError("Invalid value of `iv` option");return function(t,n,i,o){i&&(i=Y(i)),n=Y(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,g=0;g<f;g++)if(i){var s=16*g,a=[i[0]^t.readInt32BE(s),i[1]^t.readInt32BE(s+4),i[2]^t.readInt32BE(s+8),i[3]^t.readInt32BE(s+12)],l=X(a,n);u.push(H(l)),i=l.slice(0)}else{var c=16*g,h=[t.readInt32BE(c),t.readInt32BE(c+4),t.readInt32BE(c+8),t.readInt32BE(c+12)],B=X(h,n);u.push(H(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,g)},decrypt:function(t,n,i){var o=i||{},u=o.mode,f=o.iv,g=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(!P.test(n))throw new TypeError("Invalid value of cipher `key`");if(n=r.Buffer.from(n,"hex"),2===u&&!P.test(f))throw new TypeError("Invalid value of `iv` option");return function(t,n,i,o){i&&(i=Y(i)),n=Y(n);var u=[],f=t.length/16;if(i)for(var g=f-1;g>=0;g--){var s,a=16*g;s=g>0?[t.readInt32BE(a-16),t.readInt32BE(a-16+4),t.readInt32BE(a-16+8),t.readInt32BE(a-16+12)]:i;var l=[t.readInt32BE(a),t.readInt32BE(a+4),t.readInt32BE(a+8),t.readInt32BE(a+12)],c=K(l,n);u.unshift(H([c[0]^s[0],c[1]^s[1],c[2]^s[2],c[3]^s[3]]))}else for(var h=0;h<f;h++){var B=16*h,p=[t.readInt32BE(B),t.readInt32BE(B+4),t.readInt32BE(B+8),t.readInt32BE(B+12)],y=K(p,n);u.push(H(y))}var d=r.Buffer.concat(u,t.length-u[u.length-1][15]);return o?d.toString(o):e(d)}(t,n,f=2===u?r.Buffer.from(f,"hex"):null,g)}};t.SM2={__proto__:null,constants:I,generateKeyPair:O,keyFromPrivate:function(t){var e=b.multiply(new n.BigInteger(t.slice(2),16)),r=a(e.getX().toBigInteger().toString(16),64),i=a(e.getY().toBigInteger().toString(16),64);return{privateKey:t,publicKey:g+r+i}},encrypt:function(t,i,o){var u=o||{},f=u.mode,g=void 0===f?1:f,s=u.outputEncoding;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 l=new n.BigInteger(S.bitLength(),F).mod(S.subtract(n.BigInteger.ONE)).add(n.BigInteger.ONE),c=b.multiply(l),h=a(c.getX().toBigInteger().toString(16),64)+a(c.getY().toBigInteger().toString(16),64),B=x.decodePointHex(i).multiply(l),p=a(B.getX().toBigInteger().toString(16),64),d=a(B.getY().toBigInteger().toString(16),64),I=A(r.Buffer.from(p+d,"hex"),t.length),v=a(new n.BigInteger(t.toString("hex"),16).xor(new n.BigInteger(I.toString("hex"),16)).toString(16),2*t.length),m=y(p+t.toString("hex")+d,"hex","hex"),E=r.Buffer.from(0===g?h+v+m:h+m+v,"hex");return s?E.toString(s):e(E)},decrypt:function(t,i,o){var u=o||{},f=u.mode,s=void 0===f?1:f,l=u.outputEncoding;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.slice(0,32).toString("hex"),h=t.slice(32,64).toString("hex"),B=x.decodePointHex(g+c+h).multiply(new n.BigInteger(i,16)),p=a(B.getX().toBigInteger().toString(16),64),d=a(B.getY().toBigInteger().toString(16),64),I=t.slice(64,96),F=t.slice(96);0===s&&(I=t.slice(t.length-32),F=t.slice(64,t.length-32));var v=A(r.Buffer.from(p+d,"hex"),F.length),m=new n.BigInteger(F.toString("hex"),16).xor(new n.BigInteger(v.toString("hex"),16)).toString(16),E=y(p+m+d,"hex","hex")===I.toString("hex")?r.Buffer.from(m,"hex"):r.Buffer.alloc(0);return l?E.toString(l):e(E)},sm3hashZA:z,smSign:R,smSignJava:function(t,e){try{e="string"==typeof e?e:e.toString("hex")}catch(t){console.error("privateKey转化有问题")}var i=C(),o=r.Buffer.concat([i,t]),u=new n.BigInteger(e,16),f=new n.BigInteger(y(o,"buffer","hex"),16),g=null,s=null,a=null,l=null;do{do{g=new n.BigInteger(S.bitLength(),F).mod(S.subtract(n.BigInteger.ONE)).add(n.BigInteger.ONE);var c=b.multiply(g),h=c.getX().toBigInteger(),B=c.getY().toBigInteger();s=f.add(h).mod(S),l=0==B.mod(new n.BigInteger("2",10))?n.BigInteger.ZERO:n.BigInteger.ONE}while(s.equals(n.BigInteger.ZERO)||s.add(g).equals(S));a=u.add(n.BigInteger.ONE).modInverse(S).multiply(g.subtract(s.multiply(u))).mod(S)}while(a.equals(n.BigInteger.ZERO));return{signature:s.toString(16)+a.toString(16)+"0"+l.toString(16),v:l,r:s,s:a}},smVerify:Z,sign:R,verfy:Z},t.SM3=d,t.SM4=M});