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