UNPKG

ebclient.js

Version:

Client library for using EnigmaBridge crypto services

5 lines 237 kB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.eb = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ "use strict";function t(t,e,i){if(4!==e.length)throw new sjcl.exception.invalid("invalid aes block size");var s=t.b[i],n=e[0]^s[0],r=e[i?3:1]^s[1],c=e[2]^s[2];e=e[i?1:3]^s[3];var o,a,l,h,u=s.length/4-2,f=4,d=[0,0,0,0];o=t.B[i],t=o[0];var m=o[1],p=o[2],b=o[3],g=o[4];for(h=0;h<u;h++)o=t[n>>>24]^m[r>>16&255]^p[c>>8&255]^b[255&e]^s[f],a=t[r>>>24]^m[c>>16&255]^p[e>>8&255]^b[255&n]^s[f+1],l=t[c>>>24]^m[e>>16&255]^p[n>>8&255]^b[255&r]^s[f+2],e=t[e>>>24]^m[n>>16&255]^p[r>>8&255]^b[255&c]^s[f+3],f+=4,n=o,r=a,c=l;for(h=0;4>h;h++)d[i?3&-h:h]=g[n>>>24]<<24^g[r>>16&255]<<16^g[c>>8&255]<<8^g[255&e]^s[f++],o=n,n=r,r=c,c=e,e=o;return d}function z(t,e){var i,s=sjcl.random.N[t],n=[];for(i in s)s.hasOwnProperty(i)&&n.push(s[i]);for(i=0;i<n.length;i++)n[i](e)}function B(t,e){"undefined"!=typeof window&&window.performance&&"function"==typeof window.performance.now?t.addEntropy(window.performance.now(),e,"loadtime"):t.addEntropy((new Date).valueOf(),e,"loadtime")}function u(t){t.b=v(t).concat(v(t)),t.O=new sjcl.cipher.aes(t.b)}function v(t){for(var e=0;4>e&&(t.o[e]=t.o[e]+1|0,!t.o[e]);e++);return t.O.encrypt(t.o)}function A(t,e){return function(){e.apply(t,arguments)}}var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(t){this.toString=function(){return"CORRUPT: "+this.message},this.message=t},invalid:function(t){this.toString=function(){return"INVALID: "+this.message},this.message=t},bug:function(t){this.toString=function(){return"BUG: "+this.message},this.message=t},notReady:function(t){this.toString=function(){return"NOT READY: "+this.message},this.message=t}}};sjcl.cipher.aes=function(t){this.B[0][0][0]||this.S();var e,i,s,n,r=this.B[0][4],c=this.B[1];e=t.length;var o=1;if(4!==e&&6!==e&&8!==e)throw new sjcl.exception.invalid("invalid aes key size");for(this.b=[s=t.slice(0),n=[]],t=e;t<4*e+28;t++)i=s[t-1],(0===t%e||8===e&&4===t%e)&&(i=r[i>>>24]<<24^r[i>>16&255]<<16^r[i>>8&255]<<8^r[255&i],0===t%e&&(i=i<<8^i>>>24^o<<24,o=o<<1^283*(o>>7))),s[t]=s[t-e]^i;for(e=0;t;e++,t--)i=s[3&e?t:t-4],n[e]=4>=t||4>e?i:c[0][r[i>>>24]]^c[1][r[i>>16&255]]^c[2][r[i>>8&255]]^c[3][r[255&i]]},sjcl.cipher.aes.prototype={encrypt:function(e){return t(this,e,0)},decrypt:function(e){return t(this,e,1)},B:[[[],[],[],[],[]],[[],[],[],[],[]]],S:function(){var t,e,i,s,n,r,c,o=this.B[0],a=this.B[1],l=o[4],h=a[4],u=[],f=[];for(t=0;256>t;t++)f[(u[t]=t<<1^283*(t>>7))^t]=t;for(e=i=0;!l[e];e^=s||1,i=f[i]||1)for(r=i^i<<1^i<<2^i<<3^i<<4,r=r>>8^255&r^99,l[e]=r,h[r]=e,n=u[t=u[s=u[e]]],c=16843009*n^65537*t^257*s^16843008*e,n=257*u[r]^16843008*r,t=0;4>t;t++)o[t][e]=n=n<<24^n>>>8,a[t][r]=c=c<<24^c>>>8;for(t=0;5>t;t++)o[t]=o[t].slice(0),a[t]=a[t].slice(0)}},sjcl.bitArray={bitSlice:function(t,e,i){return t=sjcl.bitArray.ba(t.slice(e/32),32-(31&e)).slice(1),void 0===i?t:sjcl.bitArray.clamp(t,i-e)},extract:function(t,e,i){var s=Math.floor(-e-i&31);return((e+i-1^e)&-32?t[e/32|0]<<32-s^t[e/32+1|0]>>>s:t[e/32|0]>>>s)&(1<<i)-1},concat:function(t,e){if(0===t.length||0===e.length)return t.concat(e);var i=t[t.length-1],s=sjcl.bitArray.getPartial(i);return 32===s?t.concat(e):sjcl.bitArray.ba(e,s,0|i,t.slice(0,t.length-1))},bitLength:function(t){var e=t.length;return 0===e?0:32*(e-1)+sjcl.bitArray.getPartial(t[e-1])},clamp:function(t,e){if(32*t.length<e)return t;t=t.slice(0,Math.ceil(e/32));var i=t.length;return e&=31,0<i&&e&&(t[i-1]=sjcl.bitArray.partial(e,t[i-1]&2147483648>>e-1,1)),t},partial:function(t,e,i){return 32===t?e:(i?0|e:e<<32-t)+1099511627776*t},getPartial:function(t){return Math.round(t/1099511627776)||32},equal:function(t,e){if(sjcl.bitArray.bitLength(t)!==sjcl.bitArray.bitLength(e))return!1;var i,s=0;for(i=0;i<t.length;i++)s|=t[i]^e[i];return 0===s},ba:function(t,e,i,s){var n;for(void 0===s&&(s=[]);32<=e;e-=32)s.push(i),i=0;if(0===e)return s.concat(t);for(n=0;n<t.length;n++)s.push(i|t[n]>>>e),i=t[n]<<32-e;return t=sjcl.bitArray.getPartial(t.length?t[t.length-1]:0),s.push(sjcl.bitArray.partial(e+t&31,32<e+t?i:s.pop(),1)),s},s:function(t,e){return[t[0]^e[0],t[1]^e[1],t[2]^e[2],t[3]^e[3]]},byteswapM:function(t){var e,i;for(e=0;e<t.length;++e)i=t[e],t[e]=i>>>24|i>>>8&65280|(65280&i)<<8|i<<24;return t}},sjcl.codec.utf8String={fromBits:function(t){var e,i,s="",n=sjcl.bitArray.bitLength(t);for(e=0;e<n/8;e++)0===(3&e)&&(i=t[e/4]),s+=String.fromCharCode(i>>>24),i<<=8;return decodeURIComponent(escape(s))},toBits:function(t){t=unescape(encodeURIComponent(t));var e,i=[],s=0;for(e=0;e<t.length;e++)s=s<<8|t.charCodeAt(e),3===(3&e)&&(i.push(s),s=0);return 3&e&&i.push(sjcl.bitArray.partial(8*(3&e),s)),i}},sjcl.codec.hex={fromBits:function(t){var e,i="";for(e=0;e<t.length;e++)i+=((0|t[e])+0xf00000000000).toString(16).substr(4);return i.substr(0,sjcl.bitArray.bitLength(t)/4)},toBits:function(t){var e,i,s=[];for(t=t.replace(/\s|0x/g,""),i=t.length,t+="00000000",e=0;e<t.length;e+=8)s.push(0^parseInt(t.substr(e,8),16));return sjcl.bitArray.clamp(s,4*i)}},sjcl.codec.base32={F:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",$:"0123456789ABCDEFGHIJKLMNOPQRSTUV",BITS:32,BASE:5,REMAINING:27,fromBits:function(t,e,i){var s=sjcl.codec.base32.BASE,n=sjcl.codec.base32.REMAINING,r="",c=0,o=sjcl.codec.base32.F,a=0,l=sjcl.bitArray.bitLength(t);for(i&&(o=sjcl.codec.base32.$),i=0;r.length*s<l;)r+=o.charAt((a^t[i]>>>c)>>>n),c<s?(a=t[i]<<s-c,c+=n,i++):(a<<=s,c-=s);for(;7&r.length&&!e;)r+="=";return r},toBits:function(t,e){t=t.replace(/\s|=/g,"").toUpperCase();var i,s,n=sjcl.codec.base32.BITS,r=sjcl.codec.base32.BASE,c=sjcl.codec.base32.REMAINING,o=[],a=0,l=sjcl.codec.base32.F,h=0,u="base32";for(e&&(l=sjcl.codec.base32.$,u="base32hex"),i=0;i<t.length;i++){if(s=l.indexOf(t.charAt(i)),0>s){if(!e)try{return sjcl.codec.base32hex.toBits(t)}catch(t){}throw new sjcl.exception.invalid("this isn't "+u+"!")}a>c?(a-=c,o.push(h^s>>>a),h=s<<n-a):(a+=r,h^=s<<n-a)}return 56&a&&o.push(sjcl.bitArray.partial(56&a,h,1)),o}},sjcl.codec.base32hex={fromBits:function(t,e){return sjcl.codec.base32.fromBits(t,e,1)},toBits:function(t){return sjcl.codec.base32.toBits(t,1)}},sjcl.codec.base64={F:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",fromBits:function(t,e,i){var s="",n=0,r=sjcl.codec.base64.F,c=0,o=sjcl.bitArray.bitLength(t);for(i&&(r=r.substr(0,62)+"-_"),i=0;6*s.length<o;)s+=r.charAt((c^t[i]>>>n)>>>26),6>n?(c=t[i]<<6-n,n+=26,i++):(c<<=6,n-=6);for(;3&s.length&&!e;)s+="=";return s},toBits:function(t,e){t=t.replace(/\s|=/g,"");var i,s,n=[],r=0,c=sjcl.codec.base64.F,o=0;for(e&&(c=c.substr(0,62)+"-_"),i=0;i<t.length;i++){if(s=c.indexOf(t.charAt(i)),0>s)throw new sjcl.exception.invalid("this isn't base64!");26<r?(r-=26,n.push(o^s>>>r),o=s<<32-r):(r+=6,o^=s<<32-r)}return 56&r&&n.push(sjcl.bitArray.partial(56&r,o,1)),n}},sjcl.codec.base64url={fromBits:function(t){return sjcl.codec.base64.fromBits(t,1,1)},toBits:function(t){return sjcl.codec.base64.toBits(t,1)}},sjcl.codec.bytes={fromBits:function(t){var e,i,s=[],n=sjcl.bitArray.bitLength(t);for(e=0;e<n/8;e++)0===(3&e)&&(i=t[e/4]),s.push(i>>>24),i<<=8;return s},toBits:function(t){var e,i=[],s=0;for(e=0;e<t.length;e++)s=s<<8|t[e],3===(3&e)&&(i.push(s),s=0);return 3&e&&i.push(sjcl.bitArray.partial(8*(3&e),s)),i}},sjcl.hash.sha256=function(t){this.b[0]||this.S(),t?(this.h=t.h.slice(0),this.f=t.f.slice(0),this.c=t.c):this.reset()},sjcl.hash.sha256.hash=function(t){return(new sjcl.hash.sha256).update(t).finalize()},sjcl.hash.sha256.prototype={blockSize:512,reset:function(){return this.h=this.I.slice(0),this.f=[],this.c=0,this},update:function(t){"string"==typeof t&&(t=sjcl.codec.utf8String.toBits(t));var e,i=this.f=sjcl.bitArray.concat(this.f,t);if(e=this.c,t=this.c=e+sjcl.bitArray.bitLength(t),9007199254740991<t)throw new sjcl.exception.invalid("Cannot hash more than 2^53 - 1 bits");if("undefined"!=typeof Uint32Array){var s=new Uint32Array(i),n=0;for(e=512+e-(512+e&511);e<=t;e+=512)this.m(s.subarray(16*n,16*(n+1))),n+=1;i.splice(0,16*n)}else for(e=512+e-(512+e&511);e<=t;e+=512)this.m(i.splice(0,16));return this},finalize:function(){var t,e=this.f,i=this.h,e=sjcl.bitArray.concat(e,[sjcl.bitArray.partial(1,1)]);for(t=e.length+2;15&t;t++)e.push(0);for(e.push(Math.floor(this.c/4294967296)),e.push(0|this.c);e.length;)this.m(e.splice(0,16));return this.reset(),i},I:[],b:[],S:function(){function t(t){return 4294967296*(t-Math.floor(t))|0}for(var e,i,s=0,n=2;64>s;n++){for(i=!0,e=2;e*e<=n;e++)if(0===n%e){i=!1;break}i&&(8>s&&(this.I[s]=t(Math.pow(n,.5))),this.b[s]=t(Math.pow(n,1/3)),s++)}},m:function(t){var e,i,s,n=this.h,r=this.b,c=n[0],o=n[1],a=n[2],l=n[3],h=n[4],u=n[5],f=n[6],d=n[7];for(e=0;64>e;e++)16>e?i=t[e]:(i=t[e+1&15],s=t[e+14&15],i=t[15&e]=(i>>>7^i>>>18^i>>>3^i<<25^i<<14)+(s>>>17^s>>>19^s>>>10^s<<15^s<<13)+t[15&e]+t[e+9&15]|0),i=i+d+(h>>>6^h>>>11^h>>>25^h<<26^h<<21^h<<7)+(f^h&(u^f))+r[e],d=f,f=u,u=h,h=l+i|0,l=a,a=o,o=c,c=i+(o&a^l&(o^a))+(o>>>2^o>>>13^o>>>22^o<<30^o<<19^o<<10)|0;n[0]=n[0]+c|0,n[1]=n[1]+o|0,n[2]=n[2]+a|0,n[3]=n[3]+l|0,n[4]=n[4]+h|0,n[5]=n[5]+u|0,n[6]=n[6]+f|0,n[7]=n[7]+d|0}},sjcl.hash.sha1=function(t){t?(this.h=t.h.slice(0),this.f=t.f.slice(0),this.c=t.c):this.reset()},sjcl.hash.sha1.hash=function(t){return(new sjcl.hash.sha1).update(t).finalize()},sjcl.hash.sha1.prototype={blockSize:512,reset:function(){return this.h=this.I.slice(0),this.f=[],this.c=0,this},update:function(t){"string"==typeof t&&(t=sjcl.codec.utf8String.toBits(t));var e,i=this.f=sjcl.bitArray.concat(this.f,t);if(e=this.c,t=this.c=e+sjcl.bitArray.bitLength(t),9007199254740991<t)throw new sjcl.exception.invalid("Cannot hash more than 2^53 - 1 bits");if("undefined"!=typeof Uint32Array){var s=new Uint32Array(i),n=0;for(e=this.blockSize+e-(this.blockSize+e&this.blockSize-1);e<=t;e+=this.blockSize)this.m(s.subarray(16*n,16*(n+1))),n+=1;i.splice(0,16*n)}else for(e=this.blockSize+e-(this.blockSize+e&this.blockSize-1);e<=t;e+=this.blockSize)this.m(i.splice(0,16));return this},finalize:function(){var t,e=this.f,i=this.h,e=sjcl.bitArray.concat(e,[sjcl.bitArray.partial(1,1)]);for(t=e.length+2;15&t;t++)e.push(0);for(e.push(Math.floor(this.c/4294967296)),e.push(0|this.c);e.length;)this.m(e.splice(0,16));return this.reset(),i},I:[1732584193,4023233417,2562383102,271733878,3285377520],b:[1518500249,1859775393,2400959708,3395469782],m:function(t){var e,i,s,n,r,c,o,a=this.h;if("undefined"!=typeof Uint32Array)for(o=Array(80),i=0;16>i;i++)o[i]=t[i];else o=t;for(i=a[0],s=a[1],n=a[2],r=a[3],c=a[4],t=0;79>=t;t++)16<=t&&(e=o[t-3]^o[t-8]^o[t-14]^o[t-16],o[t]=e<<1|e>>>31),e=19>=t?s&n|~s&r:39>=t?s^n^r:59>=t?s&n|s&r|n&r:79>=t?s^n^r:void 0,e=(i<<5|i>>>27)+e+c+o[t]+this.b[Math.floor(t/20)]|0,c=r,r=n,n=s<<30|s>>>2,s=i,i=e;a[0]=a[0]+i|0,a[1]=a[1]+s|0,a[2]=a[2]+n|0,a[3]=a[3]+r|0,a[4]=a[4]+c|0}},sjcl.mode.ccm={name:"ccm",J:[],listenProgress:function(t){sjcl.mode.ccm.J.push(t)},unListenProgress:function(t){t=sjcl.mode.ccm.J.indexOf(t),-1<t&&sjcl.mode.ccm.J.splice(t,1)},ia:function(t){var e,i=sjcl.mode.ccm.J.slice();for(e=0;e<i.length;e+=1)i[e](t)},encrypt:function(t,e,i,s,n){var r,c=e.slice(0),o=sjcl.bitArray,a=o.bitLength(i)/8,l=o.bitLength(c)/8;if(n=n||64,s=s||[],7>a)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(r=2;4>r&&l>>>8*r;r++);return r<15-a&&(r=15-a),i=o.clamp(i,8*(15-r)),e=sjcl.mode.ccm.Y(t,e,i,s,n,r),c=sjcl.mode.ccm.G(t,c,i,e,n,r),o.concat(c.data,c.tag)},decrypt:function(t,e,i,s,n){n=n||64,s=s||[];var r=sjcl.bitArray,c=r.bitLength(i)/8,o=r.bitLength(e),a=r.clamp(e,o-n),l=r.bitSlice(e,o-n),o=(o-n)/8;if(7>c)throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes");for(e=2;4>e&&o>>>8*e;e++);if(e<15-c&&(e=15-c),i=r.clamp(i,8*(15-e)),a=sjcl.mode.ccm.G(t,a,i,l,n,e),t=sjcl.mode.ccm.Y(t,a.data,i,s,n,e),!r.equal(a.tag,t))throw new sjcl.exception.corrupt("ccm: tag doesn't match");return a.data},qa:function(t,e,i,s,n,r){var c=[],o=sjcl.bitArray,a=o.s;if(s=[o.partial(8,(e.length?64:0)|s-2<<2|r-1)],s=o.concat(s,i),s[3]|=n,s=t.encrypt(s),e.length)for(i=o.bitLength(e)/8,65279>=i?c=[o.partial(16,i)]:4294967295>=i&&(c=o.concat([o.partial(16,65534)],[i])),c=o.concat(c,e),e=0;e<c.length;e+=4)s=t.encrypt(a(s,c.slice(e,e+4).concat([0,0,0])));return s},Y:function(t,e,i,s,n,r){var c=sjcl.bitArray,o=c.s;if(n/=8,n%2||4>n||16<n)throw new sjcl.exception.invalid("ccm: invalid tag length");if(4294967295<s.length||4294967295<e.length)throw new sjcl.exception.bug("ccm: can't deal with 4GiB or more data");for(i=sjcl.mode.ccm.qa(t,s,i,n,c.bitLength(e)/8,r),s=0;s<e.length;s+=4)i=t.encrypt(o(i,e.slice(s,s+4).concat([0,0,0])));return c.clamp(i,8*n)},G:function(t,e,i,s,n,r){var c,o=sjcl.bitArray;c=o.s;var a=e.length,l=o.bitLength(e),h=a/50,u=h;if(i=o.concat([o.partial(8,r-1)],i).concat([0,0,0]).slice(0,4),s=o.bitSlice(c(s,t.encrypt(i)),0,n),!a)return{tag:s,data:[]};for(c=0;c<a;c+=4)c>h&&(sjcl.mode.ccm.ia(c/a),h+=u),i[3]++,n=t.encrypt(i),e[c]^=n[0],e[c+1]^=n[1],e[c+2]^=n[2],e[c+3]^=n[3];return{tag:s,data:o.clamp(e,l)}}},sjcl.mode.ocb2={name:"ocb2",encrypt:function(t,e,i,s,n,r){if(128!==sjcl.bitArray.bitLength(i))throw new sjcl.exception.invalid("ocb iv must be 128 bits");var c,o=sjcl.mode.ocb2.V,a=sjcl.bitArray,l=a.s,h=[0,0,0,0];i=o(t.encrypt(i));var u,f=[];for(s=s||[],n=n||64,c=0;c+4<e.length;c+=4)u=e.slice(c,c+4),h=l(h,u),f=f.concat(l(i,t.encrypt(l(i,u)))),i=o(i);return u=e.slice(c),e=a.bitLength(u),c=t.encrypt(l(i,[0,0,0,e])),u=a.clamp(l(u.concat([0,0,0]),c),e),h=l(h,l(u.concat([0,0,0]),c)),h=t.encrypt(l(h,l(i,o(i)))),s.length&&(h=l(h,r?s:sjcl.mode.ocb2.pmac(t,s))),f.concat(a.concat(u,a.clamp(h,n)))},decrypt:function(t,e,i,s,n,r){if(128!==sjcl.bitArray.bitLength(i))throw new sjcl.exception.invalid("ocb iv must be 128 bits");n=n||64;var c,o,a=sjcl.mode.ocb2.V,l=sjcl.bitArray,h=l.s,u=[0,0,0,0],f=a(t.encrypt(i)),d=sjcl.bitArray.bitLength(e)-n,m=[];for(s=s||[],i=0;i+4<d/32;i+=4)c=h(f,t.decrypt(h(f,e.slice(i,i+4)))),u=h(u,c),m=m.concat(c),f=a(f);if(o=d-32*i,c=t.encrypt(h(f,[0,0,0,o])),c=h(c,l.clamp(e.slice(i),o).concat([0,0,0])),u=h(u,c),u=t.encrypt(h(u,h(f,a(f)))),s.length&&(u=h(u,r?s:sjcl.mode.ocb2.pmac(t,s))),!l.equal(l.clamp(u,n),l.bitSlice(e,d)))throw new sjcl.exception.corrupt("ocb: tag doesn't match");return m.concat(l.clamp(c,o))},pmac:function(t,e){var i,s=sjcl.mode.ocb2.V,n=sjcl.bitArray,r=n.s,c=[0,0,0,0],o=t.encrypt([0,0,0,0]),o=r(o,s(s(o)));for(i=0;i+4<e.length;i+=4)o=s(o),c=r(c,t.encrypt(r(o,e.slice(i,i+4))));return i=e.slice(i),128>n.bitLength(i)&&(o=r(o,s(o)),i=n.concat(i,[-2147483648,0,0,0])),c=r(c,i),t.encrypt(r(s(r(o,s(o))),c))},V:function(t){return[t[0]<<1^t[1]>>>31,t[1]<<1^t[2]>>>31,t[2]<<1^t[3]>>>31,t[3]<<1^135*(t[0]>>>31)]}},sjcl.mode.gcm={name:"gcm",encrypt:function(t,e,i,s,n){var r=e.slice(0);return e=sjcl.bitArray,s=s||[],t=sjcl.mode.gcm.G(!0,t,r,s,i,n||128),e.concat(t.data,t.tag)},decrypt:function(t,e,i,s,n){var r=e.slice(0),c=sjcl.bitArray,o=c.bitLength(r);if(n=n||128,s=s||[],n<=o?(e=c.bitSlice(r,o-n),r=c.bitSlice(r,0,o-n)):(e=r,r=[]),t=sjcl.mode.gcm.G(!1,t,r,s,i,n),!c.equal(t.tag,e))throw new sjcl.exception.corrupt("gcm: tag doesn't match");return t.data},na:function(t,e){var i,s,n,r,c,o=sjcl.bitArray.s;for(n=[0,0,0,0],r=e.slice(0),i=0;128>i;i++){for((s=0!==(t[Math.floor(i/32)]&1<<31-i%32))&&(n=o(n,r)),c=0!==(1&r[3]),s=3;0<s;s--)r[s]=r[s]>>>1|(1&r[s-1])<<31;r[0]>>>=1,c&&(r[0]^=-520093696)}return n},u:function(t,e,i){var s,n=i.length;for(e=e.slice(0),s=0;s<n;s+=4)e[0]^=4294967295&i[s],e[1]^=4294967295&i[s+1],e[2]^=4294967295&i[s+2],e[3]^=4294967295&i[s+3],e=sjcl.mode.gcm.na(e,t);return e},G:function(t,e,i,s,n,r){var c,o,a,l,h,u,f,d,m=sjcl.bitArray;for(u=i.length,f=m.bitLength(i),d=m.bitLength(s),o=m.bitLength(n),c=e.encrypt([0,0,0,0]),96===o?(n=n.slice(0),n=m.concat(n,[1])):(n=sjcl.mode.gcm.u(c,[0,0,0,0],n),n=sjcl.mode.gcm.u(c,n,[0,0,Math.floor(o/4294967296),4294967295&o])),o=sjcl.mode.gcm.u(c,[0,0,0,0],s),h=n.slice(0),s=o.slice(0),t||(s=sjcl.mode.gcm.u(c,o,i)),l=0;l<u;l+=4)h[3]++,a=e.encrypt(h),i[l]^=a[0],i[l+1]^=a[1],i[l+2]^=a[2],i[l+3]^=a[3];return i=m.clamp(i,f),t&&(s=sjcl.mode.gcm.u(c,o,i)),t=[Math.floor(d/4294967296),4294967295&d,Math.floor(f/4294967296),4294967295&f],s=sjcl.mode.gcm.u(c,s,t),a=e.encrypt(n),s[0]^=a[0],s[1]^=a[1],s[2]^=a[2],s[3]^=a[3],{tag:m.bitSlice(s,0,r),data:i}}},sjcl.misc.hmac=function(t,e){this.Z=e=e||sjcl.hash.sha256;var i,s=[[],[]],n=e.prototype.blockSize/32;for(this.D=[new e,new e],t.length>n&&(t=e.hash(t)),i=0;i<n;i++)s[0][i]=909522486^t[i],s[1][i]=1549556828^t[i];this.D[0].update(s[0]),this.D[1].update(s[1]),this.U=new e(this.D[0])},sjcl.misc.hmac.prototype.encrypt=sjcl.misc.hmac.prototype.mac=function(t){if(this.da)throw new sjcl.exception.invalid("encrypt on already updated hmac called!");return this.update(t),this.digest(t)},sjcl.misc.hmac.prototype.reset=function(){this.U=new this.Z(this.D[0]),this.da=!1},sjcl.misc.hmac.prototype.update=function(t){this.da=!0,this.U.update(t)},sjcl.misc.hmac.prototype.digest=function(){var t=this.U.finalize(),t=new this.Z(this.D[1]).update(t).finalize();return this.reset(),t},sjcl.misc.pbkdf2=function(t,e,i,s,n){if(i=i||1e4,0>s||0>i)throw new sjcl.exception.invalid("invalid params to pbkdf2");"string"==typeof t&&(t=sjcl.codec.utf8String.toBits(t)),"string"==typeof e&&(e=sjcl.codec.utf8String.toBits(e)),n=n||sjcl.misc.hmac,t=new n(t);var r,c,o,a,l=[],h=sjcl.bitArray;for(a=1;32*l.length<(s||1);a++){for(n=r=t.encrypt(h.concat(e,[a])),c=1;c<i;c++)for(r=t.encrypt(r),o=0;o<r.length;o++)n[o]^=r[o];l=l.concat(n)}return s&&(l=h.clamp(l,s)),l},sjcl.prng=function(t){this.i=[new sjcl.hash.sha256],this.w=[0],this.T=0,this.K={},this.R=0,this.X={},this.aa=this.j=this.A=this.ka=0,this.b=[0,0,0,0,0,0,0,0],this.o=[0,0,0,0],this.O=void 0,this.P=t,this.H=!1,this.N={progress:{},seeded:{}},this.C=this.ja=0,this.L=1,this.M=2,this.fa=65536,this.W=[0,48,64,96,128,192,256,384,512,768,1024],this.ga=3e4,this.ea=80},sjcl.prng.prototype={randomWords:function(t,e){var i,s=[];i=this.isReady(e);var n;if(i===this.C)throw new sjcl.exception.notReady("generator isn't seeded");if(i&this.M){i=!(i&this.L),n=[];var r,c=0;for(this.aa=n[0]=(new Date).valueOf()+this.ga,r=0;16>r;r++)n.push(4294967296*Math.random()|0);for(r=0;r<this.i.length&&(n=n.concat(this.i[r].finalize()),c+=this.w[r],this.w[r]=0,i||!(this.T&1<<r));r++);for(this.T>=1<<this.i.length&&(this.i.push(new sjcl.hash.sha256),this.w.push(0)),this.j-=c,c>this.A&&(this.A=c),this.T++,this.b=sjcl.hash.sha256.hash(this.b.concat(n)),this.O=new sjcl.cipher.aes(this.b),i=0;4>i&&(this.o[i]=this.o[i]+1|0,!this.o[i]);i++);}for(i=0;i<t;i+=4)0===(i+1)%this.fa&&u(this),n=v(this),s.push(n[0],n[1],n[2],n[3]);return u(this),s.slice(0,t)},setDefaultParanoia:function(t,e){if(0===t&&"Setting paranoia=0 will ruin your security; use it only for testing"!==e)throw new sjcl.exception.invalid("Setting paranoia=0 will ruin your security; use it only for testing");this.P=t},addEntropy:function(t,e,i){i=i||"user";var s,n,r=(new Date).valueOf(),c=this.K[i],o=this.isReady(),a=0;switch(s=this.X[i],void 0===s&&(s=this.X[i]=this.ka++),void 0===c&&(c=this.K[i]=0),this.K[i]=(this.K[i]+1)%this.i.length,typeof t){case"number":void 0===e&&(e=1),this.i[c].update([s,this.R++,1,e,r,1,0|t]);break;case"object":if(i=Object.prototype.toString.call(t),"[object Uint32Array]"===i){for(n=[],i=0;i<t.length;i++)n.push(t[i]);t=n}else for("[object Array]"!==i&&(a=1),i=0;i<t.length&&!a;i++)"number"!=typeof t[i]&&(a=1);if(!a){if(void 0===e)for(i=e=0;i<t.length;i++)for(n=t[i];0<n;)e++,n>>>=1;this.i[c].update([s,this.R++,2,e,r,t.length].concat(t))}break;case"string":void 0===e&&(e=t.length),this.i[c].update([s,this.R++,3,e,r,t.length]),this.i[c].update(t);break;default:a=1}if(a)throw new sjcl.exception.bug("random: addEntropy only supports number, array of numbers or string");this.w[c]+=e,this.j+=e,o===this.C&&(this.isReady()!==this.C&&z("seeded",Math.max(this.A,this.j)),z("progress",this.getProgress()))},isReady:function(t){return t=this.W[void 0!==t?t:this.P],this.A&&this.A>=t?this.w[0]>this.ea&&(new Date).valueOf()>this.aa?this.M|this.L:this.L:this.j>=t?this.M|this.C:this.C},getProgress:function(t){return t=this.W[t?t:this.P],this.A>=t?1:this.j>t?1:this.j/t},startCollectors:function(){if(!this.H){if(this.a={loadTimeCollector:A(this,this.pa),mouseCollector:A(this,this.ra),keyboardCollector:A(this,this.oa),accelerometerCollector:A(this,this.ha),touchCollector:A(this,this.ta)},window.addEventListener)window.addEventListener("load",this.a.loadTimeCollector,!1),window.addEventListener("mousemove",this.a.mouseCollector,!1),window.addEventListener("keypress",this.a.keyboardCollector,!1),window.addEventListener("devicemotion",this.a.accelerometerCollector,!1),window.addEventListener("touchmove",this.a.touchCollector,!1);else{if(!document.attachEvent)throw new sjcl.exception.bug("can't attach event");document.attachEvent("onload",this.a.loadTimeCollector),document.attachEvent("onmousemove",this.a.mouseCollector),document.attachEvent("keypress",this.a.keyboardCollector)}this.H=!0}},stopCollectors:function(){this.H&&(window.removeEventListener?(window.removeEventListener("load",this.a.loadTimeCollector,!1),window.removeEventListener("mousemove",this.a.mouseCollector,!1),window.removeEventListener("keypress",this.a.keyboardCollector,!1),window.removeEventListener("devicemotion",this.a.accelerometerCollector,!1),window.removeEventListener("touchmove",this.a.touchCollector,!1)):document.detachEvent&&(document.detachEvent("onload",this.a.loadTimeCollector),document.detachEvent("onmousemove",this.a.mouseCollector),document.detachEvent("keypress",this.a.keyboardCollector)),this.H=!1)},addEventListener:function(t,e){this.N[t][this.ja++]=e},removeEventListener:function(t,e){var i,s,n=this.N[t],r=[];for(s in n)n.hasOwnProperty(s)&&n[s]===e&&r.push(s);for(i=0;i<r.length;i++)s=r[i],delete n[s]},oa:function(){B(this,1)},ra:function(t){var e,i;try{e=t.x||t.clientX||t.offsetX||0,i=t.y||t.clientY||t.offsetY||0}catch(t){i=e=0}0!=e&&0!=i&&this.addEntropy([e,i],2,"mouse"),B(this,0)},ta:function(t){t=t.touches[0]||t.changedTouches[0],this.addEntropy([t.pageX||t.clientX,t.pageY||t.clientY],1,"touch"),B(this,0)},pa:function(){B(this,2)},ha:function(t){if(t=t.accelerationIncludingGravity.x||t.accelerationIncludingGravity.y||t.accelerationIncludingGravity.z,window.orientation){var e=window.orientation;"number"==typeof e&&this.addEntropy(e,1,"accelerometer")}t&&this.addEntropy(t,2,"accelerometer"),B(this,0)}},sjcl.random=new sjcl.prng(6);t:try{var C,D,E,F;if(F="undefined"!=typeof module&&module.exports){var G;try{G=require("crypto")}catch(t){G=null}F=D=G}if(F&&D.randomBytes)C=D.randomBytes(128),C=new Uint32Array(new Uint8Array(C).buffer),sjcl.random.addEntropy(C,1024,"crypto['randomBytes']");else if("undefined"!=typeof window&&"undefined"!=typeof Uint32Array){if(E=new Uint32Array(32),window.crypto&&window.crypto.getRandomValues)window.crypto.getRandomValues(E);else{if(!window.msCrypto||!window.msCrypto.getRandomValues)break t;window.msCrypto.getRandomValues(E)}sjcl.random.addEntropy(E,1024,"crypto['getRandomValues']")}}catch(t){"undefined"!=typeof window&&window.console&&(console.log("There was an error collecting entropy from the browser:"),console.log(t))}sjcl.json={defaults:{v:1,iter:1e4,ks:128,ts:64,mode:"ccm",adata:"",cipher:"aes"},ma:function(t,e,i,s){i=i||{},s=s||{};var n,r=sjcl.json,c=r.l({iv:sjcl.random.randomWords(4,0)},r.defaults);if(r.l(c,i),i=c.adata,"string"==typeof c.salt&&(c.salt=sjcl.codec.base64.toBits(c.salt)),"string"==typeof c.iv&&(c.iv=sjcl.codec.base64.toBits(c.iv)),!sjcl.mode[c.mode]||!sjcl.cipher[c.cipher]||"string"==typeof t&&100>=c.iter||64!==c.ts&&96!==c.ts&&128!==c.ts||128!==c.ks&&192!==c.ks&&256!==c.ks||2>c.iv.length||4<c.iv.length)throw new sjcl.exception.invalid("json encrypt: invalid parameters");return"string"==typeof t?(n=sjcl.misc.cachedPbkdf2(t,c),t=n.key.slice(0,c.ks/32),c.salt=n.salt):sjcl.ecc&&t instanceof sjcl.ecc.elGamal.publicKey&&(n=t.kem(),c.kemtag=n.tag,t=n.key.slice(0,c.ks/32)),"string"==typeof e&&(e=sjcl.codec.utf8String.toBits(e)),"string"==typeof i&&(c.adata=i=sjcl.codec.utf8String.toBits(i)),n=new sjcl.cipher[c.cipher](t),r.l(s,c),s.key=t,c.ct="ccm"===c.mode&&sjcl.arrayBuffer&&sjcl.arrayBuffer.ccm&&e instanceof ArrayBuffer?sjcl.arrayBuffer.ccm.encrypt(n,e,c.iv,i,c.ts):sjcl.mode[c.mode].encrypt(n,e,c.iv,i,c.ts),c},encrypt:function(t,e,i,s){var n=sjcl.json,r=n.ma.apply(n,arguments);return n.encode(r)},la:function(t,e,i,s){i=i||{},s=s||{};var n=sjcl.json;e=n.l(n.l(n.l({},n.defaults),e),i,!0);var r,c;if(r=e.adata,"string"==typeof e.salt&&(e.salt=sjcl.codec.base64.toBits(e.salt)),"string"==typeof e.iv&&(e.iv=sjcl.codec.base64.toBits(e.iv)),!sjcl.mode[e.mode]||!sjcl.cipher[e.cipher]||"string"==typeof t&&100>=e.iter||64!==e.ts&&96!==e.ts&&128!==e.ts||128!==e.ks&&192!==e.ks&&256!==e.ks||!e.iv||2>e.iv.length||4<e.iv.length)throw new sjcl.exception.invalid("json decrypt: invalid parameters");return"string"==typeof t?(c=sjcl.misc.cachedPbkdf2(t,e),t=c.key.slice(0,e.ks/32),e.salt=c.salt):sjcl.ecc&&t instanceof sjcl.ecc.elGamal.secretKey&&(t=t.unkem(sjcl.codec.base64.toBits(e.kemtag)).slice(0,e.ks/32)),"string"==typeof r&&(r=sjcl.codec.utf8String.toBits(r)),c=new sjcl.cipher[e.cipher](t),r="ccm"===e.mode&&sjcl.arrayBuffer&&sjcl.arrayBuffer.ccm&&e.ct instanceof ArrayBuffer?sjcl.arrayBuffer.ccm.decrypt(c,e.ct,e.iv,e.tag,r,e.ts):sjcl.mode[e.mode].decrypt(c,e.ct,e.iv,r,e.ts),n.l(s,e),s.key=t,1===i.raw?r:sjcl.codec.utf8String.fromBits(r)},decrypt:function(t,e,i,s){var n=sjcl.json;return n.la(t,n.decode(e),i,s)},encode:function(t){var e,i="{",s="";for(e in t)if(t.hasOwnProperty(e)){if(!e.match(/^[a-z0-9]+$/i))throw new sjcl.exception.invalid("json encode: invalid property name");switch(i+=s+'"'+e+'":',s=",",typeof t[e]){case"number":case"boolean":i+=t[e];break;case"string":i+='"'+escape(t[e])+'"';break;case"object":i+='"'+sjcl.codec.base64.fromBits(t[e],0)+'"';break;default:throw new sjcl.exception.bug("json encode: unsupported type")}}return i+"}"},decode:function(t){if(t=t.replace(/\s/g,""),!t.match(/^\{.*\}$/))throw new sjcl.exception.invalid("json decode: this isn't json!");t=t.replace(/^\{|\}$/g,"").split(/,/);var e,i,s={};for(e=0;e<t.length;e++){if(!(i=t[e].match(/^\s*(?:(["']?)([a-z][a-z0-9]*)\1)\s*:\s*(?:(-?\d+)|"([a-z0-9+\/%*_.@=\-]*)"|(true|false))$/i)))throw new sjcl.exception.invalid("json decode: this isn't json!");null!=i[3]?s[i[2]]=parseInt(i[3],10):null!=i[4]?s[i[2]]=i[2].match(/^(ct|adata|salt|iv)$/)?sjcl.codec.base64.toBits(i[4]):unescape(i[4]):null!=i[5]&&(s[i[2]]="true"===i[5])}return s},l:function(t,e,i){if(void 0===t&&(t={}),void 0===e)return t;for(var s in e)if(e.hasOwnProperty(s)){if(i&&void 0!==t[s]&&t[s]!==e[s])throw new sjcl.exception.invalid("required parameter overridden");t[s]=e[s]}return t},va:function(t,e){var i,s={};for(i in t)t.hasOwnProperty(i)&&t[i]!==e[i]&&(s[i]=t[i]);return s},ua:function(t,e){var i,s={};for(i=0;i<e.length;i++)void 0!==t[e[i]]&&(s[e[i]]=t[e[i]]);return s}},sjcl.encrypt=sjcl.json.encrypt,sjcl.decrypt=sjcl.json.decrypt,sjcl.misc.sa={},sjcl.misc.cachedPbkdf2=function(t,e){var i,s=sjcl.misc.sa;return e=e||{},i=e.iter||1e3,s=s[t]=s[t]||{},i=s[i]=s[i]||{firstSalt:e.salt&&e.salt.length?e.salt.slice(0):sjcl.random.randomWords(2,0)},s=void 0===e.salt?i.firstSalt:e.salt,i[s]=i[s]||sjcl.misc.pbkdf2(t,s,e.iter),{key:i[s].slice(0),salt:s.slice(0)}},sjcl.bn=function(t){this.initWith(t)},sjcl.bn.prototype={radix:24,maxMul:8,g:sjcl.bn,copy:function(){return new this.g(this)},initWith:function(t){var e,i;switch(typeof t){case"object":this.limbs=t.limbs.slice(0);break;case"number":this.limbs=[t],this.normalize();break;case"string":for(t=t.replace(/^0x/,""),this.limbs=[],i=this.radix/4,e=0;e<t.length;e+=i)this.limbs.push(parseInt(t.substring(Math.max(t.length-e-i,0),t.length-e),16));break;default:this.limbs=[0]}return this},equals:function(t){"number"==typeof t&&(t=new this.g(t));var e,i=0;for(this.fullReduce(),t.fullReduce(),e=0;e<this.limbs.length||e<t.limbs.length;e++)i|=this.getLimb(e)^t.getLimb(e);return 0===i},getLimb:function(t){return t>=this.limbs.length?0:this.limbs[t]},greaterEquals:function(t){"number"==typeof t&&(t=new this.g(t));var e,i,s,n=0,r=0;for(e=Math.max(this.limbs.length,t.limbs.length)-1;0<=e;e--)i=this.getLimb(e),s=t.getLimb(e),r|=s-i&~n,n|=i-s&~r;return(r|~n)>>>31},toString:function(){this.fullReduce();var t,e,i="",s=this.limbs;for(t=0;t<this.limbs.length;t++){for(e=s[t].toString(16);t<this.limbs.length-1&&6>e.length;)e="0"+e;i=e+i}return"0x"+i},addM:function(t){"object"!=typeof t&&(t=new this.g(t));var e=this.limbs,i=t.limbs;for(t=e.length;t<i.length;t++)e[t]=0;for(t=0;t<i.length;t++)e[t]+=i[t];return this},doubleM:function(){var t,e,i=0,s=this.radix,n=this.radixMask,r=this.limbs;for(t=0;t<r.length;t++)e=r[t],e=e+e+i,r[t]=e&n,i=e>>s;return i&&r.push(i),this},halveM:function(){var t,e,i=0,s=this.radix,n=this.limbs;for(t=n.length-1;0<=t;t--)e=n[t],n[t]=e+i>>1,i=(1&e)<<s;return n[n.length-1]||n.pop(),this},subM:function(t){"object"!=typeof t&&(t=new this.g(t));var e=this.limbs,i=t.limbs;for(t=e.length;t<i.length;t++)e[t]=0;for(t=0;t<i.length;t++)e[t]-=i[t];return this},mod:function(t){var e=!this.greaterEquals(new sjcl.bn(0));t=new sjcl.bn(t).normalize();var i=new sjcl.bn(this).normalize(),s=0;for(e&&(i=new sjcl.bn(0).subM(i).normalize());i.greaterEquals(t);s++)t.doubleM();for(e&&(i=t.sub(i).normalize());0<s;s--)t.halveM(),i.greaterEquals(t)&&i.subM(t).normalize();return i.trim()},inverseMod:function(t){var e,i,s=new sjcl.bn(1),n=new sjcl.bn(0),r=new sjcl.bn(this),c=new sjcl.bn(t);if(!(1&t.limbs[0]))throw new sjcl.exception.invalid("inverseMod: p must be odd");do for(1&r.limbs[0]&&(r.greaterEquals(c)||(e=r,r=c,c=e,e=s,s=n,n=e),r.subM(c),r.normalize(),s.greaterEquals(n)||s.addM(t),s.subM(n)),r.halveM(),1&s.limbs[0]&&s.addM(t),s.normalize(),s.halveM(),e=i=0;e<r.limbs.length;e++)i|=r.limbs[e];while(i);if(!c.equals(1))throw new sjcl.exception.invalid("inverseMod: p and x must be relatively prime");return n},add:function(t){return this.copy().addM(t)},sub:function(t){return this.copy().subM(t)},mul:function(t){"number"==typeof t&&(t=new this.g(t));var e,i,s=this.limbs,n=t.limbs,r=s.length,c=n.length,o=new this.g,a=o.limbs,l=this.maxMul;for(e=0;e<this.limbs.length+t.limbs.length+1;e++)a[e]=0;for(e=0;e<r;e++){for(i=s[e],t=0;t<c;t++)a[e+t]+=i*n[t];--l||(l=this.maxMul,o.cnormalize())}return o.cnormalize().reduce()},square:function(){return this.mul(this)},power:function(t){t=new sjcl.bn(t).normalize().trim().limbs;var e,i,s=new this.g(1),n=this;for(e=0;e<t.length;e++)for(i=0;i<this.radix&&(t[e]&1<<i&&(s=s.mul(n)),e!=t.length-1||0!=t[e]>>i+1);i++)n=n.square();return s},mulmod:function(t,e){return this.mod(e).mul(t.mod(e)).mod(e)},powermod:function(t,e){if(t=new sjcl.bn(t),e=new sjcl.bn(e),1==(1&e.limbs[0])){var i=this.montpowermod(t,e);if(0!=i)return i}for(var s,n=t.normalize().trim().limbs,r=new this.g(1),c=this,i=0;i<n.length;i++)for(s=0;s<this.radix&&(n[i]&1<<s&&(r=r.mulmod(c,e)),i!=n.length-1||0!=n[i]>>s+1);s++)c=c.mulmod(c,e);return r},montpowermod:function(t,e){function i(t,e){var i=e%t.radix;return(t.limbs[Math.floor(e/t.radix)]&1<<i)>>i}function s(t,i){var s,n,r=(1<<l+1)-1;for(s=t.mul(i),n=s.mul(d),n.limbs=n.limbs.slice(0,a.limbs.length),n.limbs.length==a.limbs.length&&(n.limbs[a.limbs.length-1]&=r),n=n.mul(e),n=s.add(n).normalize().trim(),n.limbs=n.limbs.slice(a.limbs.length-1),s=0;s<n.limbs.length;s++)0<s&&(n.limbs[s-1]|=(n.limbs[s]&r)<<c-l-1),n.limbs[s]>>=l+1;return n.greaterEquals(e)&&n.subM(e),n}t=new sjcl.bn(t).normalize().trim(),e=new sjcl.bn(e);var n,r,c=this.radix,o=new this.g(1);n=this.copy();var a,l,h;for(h=t.bitLength(),a=new sjcl.bn({limbs:e.copy().normalize().trim().limbs.map(function(){return 0})}),l=this.radix;0<l;l--)if(1==(e.limbs[e.limbs.length-1]>>l&1)){a.limbs[a.limbs.length-1]=1<<l;break}if(0==h)return this;h=18>h?1:48>h?3:144>h?4:768>h?5:6;var u=a.copy(),f=e.copy();r=new sjcl.bn(1);for(var d=new sjcl.bn(0),m=a.copy();m.greaterEquals(1);)m.halveM(),0==(1&r.limbs[0])?(r.halveM(),d.halveM()):(r.addM(f),r.halveM(),d.halveM(),d.addM(u));if(r=r.normalize(),d=d.normalize(),u.doubleM(),f=u.mulmod(u,e),!u.mul(r).sub(e.mul(d)).equals(1))return!1;for(n=s(n,f),o=s(o,f),u={},r=(1<<h-1)-1,u[1]=n.copy(),u[2]=s(n,n),n=1;n<=r;n++)u[2*n+1]=s(u[2*n-1],u[2]); for(n=t.bitLength()-1;0<=n;)if(0==i(t,n))o=s(o,o),--n;else{for(f=n-h+1;0==i(t,f);)f++;for(m=0,r=f;r<=n;r++)m+=i(t,r)<<r-f,o=s(o,o);o=s(o,u[m]),n=f-1}return s(o,1)},trim:function(){var t,e=this.limbs;do t=e.pop();while(e.length&&0===t);return e.push(t),this},reduce:function(){return this},fullReduce:function(){return this.normalize()},normalize:function(){var t,e,i=0,s=this.placeVal,n=this.ipv,r=this.limbs,c=r.length,o=this.radixMask;for(t=0;t<c||0!==i&&-1!==i;t++)i=(r[t]||0)+i,e=r[t]=i&o,i=(i-e)*n;return-1===i&&(r[t-1]-=s),this.trim(),this},cnormalize:function(){var t,e,i=0,s=this.ipv,n=this.limbs,r=n.length,c=this.radixMask;for(t=0;t<r-1;t++)i=n[t]+i,e=n[t]=i&c,i=(i-e)*s;return n[t]+=i,this},toBits:function(t){this.fullReduce(),t=t||this.exponent||this.bitLength();var e=Math.floor((t-1)/24),i=sjcl.bitArray,s=[i.partial((t+7&-8)%this.radix||this.radix,this.getLimb(e))];for(e--;0<=e;e--)s=i.concat(s,[i.partial(Math.min(this.radix,t),this.getLimb(e))]),t-=this.radix;return s},bitLength:function(){this.fullReduce();for(var t=this.radix*(this.limbs.length-1),e=this.limbs[this.limbs.length-1];e;e>>>=1)t++;return t+7&-8}},sjcl.bn.fromBits=function(t){var e=new this,i=[],s=sjcl.bitArray,n=this.prototype,r=Math.min(this.bitLength||4294967296,s.bitLength(t)),c=r%n.radix||n.radix;for(i[0]=s.extract(t,0,c);c<r;c+=n.radix)i.unshift(s.extract(t,c,n.radix));return e.limbs=i,e},sjcl.bn.prototype.ipv=1/(sjcl.bn.prototype.placeVal=Math.pow(2,sjcl.bn.prototype.radix)),sjcl.bn.prototype.radixMask=(1<<sjcl.bn.prototype.radix)-1,sjcl.bn.pseudoMersennePrime=function(t,e){function i(t){this.initWith(t)}var s,n,r=i.prototype=new sjcl.bn;for(s=r.modOffset=Math.ceil(n=t/r.radix),r.exponent=t,r.offset=[],r.factor=[],r.minOffset=s,r.fullMask=0,r.fullOffset=[],r.fullFactor=[],r.modulus=i.modulus=new sjcl.bn(Math.pow(2,t)),r.fullMask=0|-Math.pow(2,t%r.radix),s=0;s<e.length;s++)r.offset[s]=Math.floor(e[s][0]/r.radix-n),r.fullOffset[s]=Math.ceil(e[s][0]/r.radix-n),r.factor[s]=e[s][1]*Math.pow(.5,t-e[s][0]+r.offset[s]*r.radix),r.fullFactor[s]=e[s][1]*Math.pow(.5,t-e[s][0]+r.fullOffset[s]*r.radix),r.modulus.addM(new sjcl.bn(Math.pow(2,e[s][0])*e[s][1])),r.minOffset=Math.min(r.minOffset,-r.offset[s]);return r.g=i,r.modulus.cnormalize(),r.reduce=function(){var t,e,i,s,n=this.modOffset,r=this.limbs,c=this.offset,o=this.offset.length,a=this.factor;for(t=this.minOffset;r.length>n;){for(i=r.pop(),s=r.length,e=0;e<o;e++)r[s+c[e]]-=a[e]*i;t--,t||(r.push(0),this.cnormalize(),t=this.minOffset)}return this.cnormalize(),this},r.ca=-1===r.fullMask?r.reduce:function(){var t,e,i=this.limbs,s=i.length-1;if(this.reduce(),s===this.modOffset-1){for(e=i[s]&this.fullMask,i[s]-=e,t=0;t<this.fullOffset.length;t++)i[s+this.fullOffset[t]]-=this.fullFactor[t]*e;this.normalize()}},r.fullReduce=function(){var t,e;for(this.ca(),this.addM(this.modulus),this.addM(this.modulus),this.normalize(),this.ca(),e=this.limbs.length;e<this.modOffset;e++)this.limbs[e]=0;for(t=this.greaterEquals(this.modulus),e=0;e<this.limbs.length;e++)this.limbs[e]-=this.modulus.limbs[e]*t;return this.cnormalize(),this},r.inverse=function(){return this.power(this.modulus.sub(2))},i.fromBits=sjcl.bn.fromBits,i};var H=sjcl.bn.pseudoMersennePrime;sjcl.bn.prime={p127:H(127,[[0,-1]]),p25519:H(255,[[0,-19]]),p192k:H(192,[[32,-1],[12,-1],[8,-1],[7,-1],[6,-1],[3,-1],[0,-1]]),p224k:H(224,[[32,-1],[12,-1],[11,-1],[9,-1],[7,-1],[4,-1],[1,-1],[0,-1]]),p256k:H(256,[[32,-1],[9,-1],[8,-1],[7,-1],[6,-1],[4,-1],[0,-1]]),p192:H(192,[[0,-1],[64,-1]]),p224:H(224,[[0,1],[96,-1]]),p256:H(256,[[0,-1],[96,1],[192,1],[224,-1]]),p384:H(384,[[0,-1],[32,1],[96,-1],[128,-1]]),p521:H(521,[[0,-1]])},sjcl.bn.random=function(t,e){"object"!=typeof t&&(t=new sjcl.bn(t));for(var i,s,n=t.limbs.length,r=t.limbs[n-1]+1,c=new sjcl.bn;;){do i=sjcl.random.randomWords(n,e),0>i[n-1]&&(i[n-1]+=4294967296);while(Math.floor(i[n-1]/r)===Math.floor(4294967296/r));for(i[n-1]%=r,s=0;s<n-1;s++)i[s]&=t.radixMask;if(c.limbs=i,!c.greaterEquals(t))return c}},"undefined"!=typeof module&&module.exports&&(module.exports=sjcl),"function"==typeof define&&define([],function(){return sjcl}); },{"crypto":undefined}],2:[function(require,module,exports){ "use strict";var sprintf=require("./eb-util-sprintf"),ebextend=require("./eb-util-extend"),inherit=require("./eb-util-inherit"),RetryHandler=require("./eb-util-retry"),sjcl=require("./built/sjcl/sjcl"),BigInteger=require("jsbn").BigInteger,superagent=require("superagent"),modurl=require("url"),Promise=require("bluebird");Function.prototype.inheritsFrom=function(e,t){return e.constructor===Function?(this.prototype=new e,this.prototype.constructor=this,this.prototype.parent=e.prototype,this.superclass=e.prototype,this.prototype=ebextend(this.prototype,t)):(this.prototype=e,this.prototype.constructor=this,this.prototype.parent=e,this.superclass=e),this};var eb={name:"EB",exception:{},codec:{},padding:{},math:{},comm:{},client:{}};eb.exception={corrupt:function(e){this.toString=function(){return"CORRUPT: "+this.message},this.message=e},invalid:function(e){this.toString=function(){return"INVALID: "+this.message},this.message=e}},eb.misc={name:"misc",MAX_SAFE_INTEGER:Math.pow(2,53)-1,MIN_SAFE_INTEGER:-(Math.pow(2,53)-1),EPSILON:2.220446049250313e-16,sprintf:sprintf,extend:ebextend,inherit:inherit,RetryHandler:RetryHandler,sjcl:sjcl,BigInteger:BigInteger,superagent:superagent,url:modurl,Promise:Promise,genNonce:function(e,t){var i="",s=t.length,o=0;for(o=0;o<e;o++)i+=t.charAt((65535&sjcl.random.randomWords(1)[0])%s);return i},genHexNonce:function(e){return this.genNonce(e,"0123456789abcdef")},genAlphaNonce:function(e){return this.genNonce(e,"0123456789abcdefghijklmnopqrstuvwxyz")},xor:function(e,t){return[e[0]^t[0],e[1]^t[1],e[2]^t[2],e[3]^t[3]]},xor8:function(e,t){return[e[0]^t[0],e[1]^t[1],e[2]^t[2],e[3]^t[3],e[4]^t[4],e[5]^t[5],e[6]^t[6],e[7]^t[7]]},absorb:function(e,t){if(void 0===t)return e;for(var i in t)t.hasOwnProperty(i)&&(e[i]=t[i]);return e},absorbKey:function(e,t,i){return void 0!==t&&i in t&&(e[i]=t[i]),e},absorbKeyEx:function(e,t,i,s){return void 0!==i&&s in i&&(e[t]=i[s]),e},absorbKeyIfNotSet:function(e,t,i,s){return!(void 0!==i&&s in i&&void 0!==i[s]&&void 0!==e)||t in e&&void 0!==e[t]||(e[t]=i[s]),e},absorbValue:function(e,t,i,s){void 0!==t?e[i]=t:void 0!==s&&(e[i]=s)},inputToBits:function(e){if("number"==typeof e)return sjcl.codec.hex.toBits(sprintf("%02x",e));if("string"==typeof e){if(e=e.trim().replace(/^0x/,""),!e.match(/^[0-9A-Fa-f]+$/))throw new eb.exception.invalid("Invalid hex coded number");return sjcl.codec.hex.toBits(e)}return e},inputToHex:function(e){if("number"==typeof e)return sprintf("%x",e);if("string"==typeof e){if(e=e.trim().replace(/^0x/,""),!e.match(/^[0-9A-Fa-f]+$/))throw new eb.exception.invalid("Invalid hex coded number");return e}return sjcl.codec.hex.fromBits(e)},inputToHexNum:function(e,t){if("number"==typeof e)return e;if("string"==typeof e){if(e=e.trim().replace(/^0x/,""),!e.match(/^[0-9A-Fa-f]+$/))throw new eb.exception.invalid("Invalid hex coded number");return parseInt(e,16)}if(void 0!==t&&t)return e;throw new eb.exception.invalid("Invalid argument - not a number or string")},getZeroBits:function(e){if(e<=0)return[];var t,i=[0,0,0,0,0,0,0,0];for(t=256;t<e;t+=32)i.push(0);return sjcl.bitArray.bitSlice(i,0,e)},getRandomBits:function(e){return sjcl.bitArray.clamp(sjcl.random.randomWords(Math.ceil(e/32)),e)},numberToBits:function(e,t){if(t>32)throw new eb.exception.invalid("num can be maximally 32bit wide");return 32==t?[e]:sjcl.bitArray.bitSlice([e],32-t,32)},replacePart:function(e,t,i,s){var o=sjcl.bitArray,n=o.concat(o.bitSlice(e,0,t),s);return n=o.concat(n,o.bitSlice(e,i))},transformPart:function(e,t,i,s){var o=sjcl.bitArray,n=o.bitSlice(e,t,i),r=o.concat(o.bitSlice(e,0,t),s(n));return r=o.concat(r,o.bitSlice(e,i))},serialize64bit:function(e){return[Math.floor(e/4294967296),0|e]},deserialize64bit:function(e,t){t=t||0;var i=sjcl.bitArray,s=i.extract32(e,t),o=i.extract32(e,t+32);return 4294967296*s+o+(o<0?4294967296:0)},padHexToEven:function(e){return e=e.trim().replace(/[\s]+/g,"").replace(/^0x/,""),1==(1&e.length)?"0"+e:e},padHexToSize:function(e,t){return e=e.trim().replace(/[\s]+/g,"").replace(/^0x/,""),e.length<t?"0".repeat(t-e.length)+e:e},padToBlockSize:function(e,t){return e+(t-e%t)},strByteLength:function(e){for(var t=e.length,i=e.length-1;i>=0;i--){var s=e.charCodeAt(i);s>127&&s<=2047?t++:s>2047&&s<=65535&&(t+=2),s>=56320&&s<=57343&&i--}return t},isDefined:function(e,t){return void 0!==e&&t in e&&void 0!==e[t]},genChecksumValue:function(e,t){var i,s=eb.misc.inputToBits(e),o=sjcl.codec.hex.fromBits(s)+","+t+","+sjcl.bitArray.bitLength(s),n=sjcl.hash.sha256.hash(o),r=sjcl.hash.sha256.hash(sjcl.codec.hex.fromBits(n)+o),c=[];for(i=0;i<8;i++)c[i]=n[i]^r[i];var a=sjcl.codec.base32.fromBits(c);return a.substring(0,t)},genChecksumValueFromString:function(e,t){return eb.misc.genChecksumValue(sjcl.hash.sha256.hash(e),t)},assert:function(e,t){if(!e){if(t=t||"Assertion failed","undefined"!=typeof Error)throw new Error(t);throw t}},parseUrl:function(e,t){t=t||!1;var i=eb.misc.url.parse(e,t);if("undefined"!=typeof i.protocol){var s=i.protocol;i.scheme=":"===s.slice(-1)?s.substring(0,s.length-1):s}return i},regenerateCommKeys:function(e){var t=sjcl.bitArray,i=eb.misc.inputToBits(e),s=sjcl.hash.sha256.hash(t.concat(i,[1])),o=sjcl.hash.sha256.hash(t.concat(i,[2]));return{enc:s,mac:o}},def:function(e,t){return"undefined"==typeof e?t:e}},eb.codec.utf8={toHex:function(e,t){var i,s=e.length,o="";for(i=0;i<s;i++){var n=e.charAt(i),r=s-i-1;if("\\"!==n){var c,a,h=unescape(encodeURIComponent(n));for(c=0,a=h.length;c<a;c++){var u=Number(h.charCodeAt(c)).toString(16);1==(1&u.length)&&(u="0"+u),o+=u}}else{if(r>=3){var p=e.substring(i,i+4),d=/\\x([a-fA-F0-9]{2})/g,l=d.exec(p);if(l){o+=l[1],i+=3;continue}}if(r>=1){var m=e.substring(i+1,i+2);if("\\"===m){o+=Number("\\".charCodeAt(0)).toString(16),i+=1;continue}}}}return o},fromHex:function(e,t){var i,s,o,n=eb.codec.utf8.fromHexParse(e,t),r="";for(s=0,o=n.parsed.length;s<o;s++)i=n.parsed[s],r+=i.utf8?i.rep:i.enc;return r},fromHexParse:function(e,t){var i={acceptUtf8:!0,acceptOnlyUtf8:!1};t=ebextend(i,t||{});var s=sjcl.codec.hex,o=t&&t.acceptUtf8,n=t&&t.acceptOnlyUtf8,r=e.length;1==(1&r)&&(r-=1);var c,a,h,u,p=0,d=[];for(c=0;c<r;c+=2)if(a=(e[c]+e[c+1]).toUpperCase(),h=s.toBits(a),u=sjcl.bitArray.extract(h,0,8),o&&0!==(128&u)){var l=(r-c-2)/2,m=!1,b=0;for(b=2;b<=6;b++){var f=Math.pow(2,b)-1<<1,g=u>>8-b-1;if(f===g){if(l<b-1)break;var y=s.toBits(sprintf("0000%x",u&(1<<8-b-1)-1)),_=sjcl.bitArray.bitLength(y);_>8-b-1&&(y=sjcl.bitArray.bitSlice(y,_-(8-b-1)));var v=0,T=!0;for(v=0;v<b-1;v++){var R=eb.codec.utf8.getByte(e,c+2+2*v);if(R>>>6!=2){T=!1;break}var A=s.toBits(sprintf("0000%x",63&R)),x=sjcl.bitArray.bitLength(A);x>=7&&(A=sjcl.bitArray.bitSlice(A,x-6)),y=sjcl.bitArray.concat(y,A)}if(!T)break;if(_=sjcl.bitArray.bitLength(y),0!==(7&_)){var S=8-(7&_);y=sjcl.bitArray.concat(sjcl.bitArray.bitSlice([0,0,0,0],0,S),y)}m=!0,d.push({b:b,utf8:!0,hex:a+e.substring(c+2,c+2+2*(b-1)),enc:"\\u"+s.fromBits(y),rep:String.fromCharCode(parseInt(s.fromBits(y),16))}),c+=2*(b-1);break}}m||n||(d.push({b:1,utf8:!1,hex:a,enc:"\\x"+a,rep:"\\x"+a}),p+=1)}else{var O=String.fromCharCode(u);"\\"===O&&(O="\\\\"),d.push({b:1,utf8:!0,hex:a,enc:String.fromCharCode(u),rep:u<32||u>=127?"\\x"+a:O})}return{nonUtf8Chars:p,parsed:d}},getByte:function(e,t){var i=e[t]+e[t+1],s=sjcl.codec.hex.toBits(i);return sjcl.bitArray.extract(s,0,8)}},eb.padding={name:"padding"},eb.padding.empty={name:"empty",pad:function(e,t){return e},unpad:function(e,t){return e}},eb.padding.pkcs7={name:"pkcs7",pad:function(e,t){if(t=t||16,!t||t&t-1)throw new sjcl.exception.corrupt("blocklength has to be power of 2");if(16!=t)throw new sjcl.exception.corrupt("blocklength different than 16 is not implemented yet");var i=sjcl.bitArray.bitLength(e),s=16-(i>>3&15),o=16843009*s;return sjcl.bitArray.concat(e,[o,o,o,o]).slice(0,(i>>3)+s>>2)},unpad:function(e,t){if(t=t||16,!t||t&t-1)throw new sjcl.exception.corrupt("blocklength has to be power of 2");if(16!=t)throw new sjcl.exception.corrupt("blocklength different than 16 is not implemented yet");var i=sjcl.bitArray,s=i.bitLength(e);if(127&s||!e.length)throw new sjcl.exception.corrupt("input must be a positive multiple of the block size");var o=255&e[(s>>3>>2)-1];if(0===o||o>16)throw new sjcl.exception.corrupt("pkcs#5 padding corrupt");var n=16843009*o;if(!i.equal(i.bitSlice([n,n,n,n],0,o<<3),i.bitSlice(e,(e.length<<5)-(o<<3),e.length<<5)))throw new sjcl.exception.corrupt("pkcs#5 padding corrupt");return i.bitSlice(e,0,(e.length<<5)-(o<<3))}},eb.padding.pkcs15={name:"pkcs1.5",pad:function(e,t,i){var s=sjcl.bitArray,o=s.bitLength(e),n=o/8;if(void 0===i&&(i=0),7&o||!e.length)throw new sjcl.exception.corrupt("input type has to have be byte padded, bl="+o);if(0!==i&&1!==i&&2!==i)throw new sjcl.exception.corrupt("invalid BT size");if(n+3>t)throw new sjcl.exception.corrupt("data to pad is too big for the padding block length");var r,c=t-3-n,a=[],h=0;for(r=0;r<c;r++){var u=0;if(1==i)u=255;else if(2==i)do u=255&sjcl.random.randomWords(1)[0];while(0===u);h=h<<8|u,3===(3&r)&&(a.push(h),h=0)}3&r&&a.push(sjcl.bitArray.partial(8*(3&r),h));var p=[sjcl.bitArray.partial(8,0)];return p=s.concat(p,[sjcl.bitArray.partial(8,i)]),p=s.concat(p,a),p=s.concat(p,[sjcl.bitArray.partial(8,0)]),s.concat(p,e)},unpad:function(e){var t=sjcl.bitArray,i=t.bitLength(e),s=i/8;if(7&i||s<3||!e.length)throw new sjcl.exception.corrupt("data size block is invalid");var o=0,n=t.extract(e,o,8);if(0!==n)throw new sjcl.exception.corrupt("data size block is invalid");o+=8;var r=t.extract(e,o,8);if(0!==r&&1!==r&&2!==r)throw new sjcl.exception.corrupt("Padding data error, BT is outside of the definition set");var c=-1,a=0,h=0;if(0===r){for(a=2;a<s;a++)if(h=t.extract(e,8*a,8),0!==h){c=a;break}}else if(1==r){var u=!0;for(a=2;a<s;a++)if(h=t.extract(e,8*a,8),0!==h&&255!==h&&(u=!1),0===h){c=a+1;break}if(!u)throw new sjcl.exception.corrupt("Trail of 0xFF in padding contains also unexpected characters")}else for(a=2;a<s;a++)if(h=t.extract(e,8*a,8),0===h){c=a+1;break}if(c<0||c>s)throw new sjcl.exception.corrupt("Padding could not be parsed, dataStart="+c+", len="+s);return t.bitSlice(e,8*c)}},sjcl.bitArray.extract32=function(e,t){var i,s=Math.floor(-t-32&31);return i=(t+32-1^t)&-32?e[t/32|0]<<32-s^e[t/32+1|0]>>>s:e[t/32|0]>>>s},sjcl.misc.hmac_cbc=function(e,t,i){this._cipher=e,this._bs=t=t||16,this._padding=i=i||eb.padding.empty},sjcl.misc.hmac_cbc.prototype.encrypt=sjcl.misc.hmac_cbc.prototype.mac=function(e){var t,i=sjcl.bitArray,s=i.bitLength(e),o=0,n=eb.misc.xor,r=this._bs<<3;e=this._padding.pad(e,this._bs);var c=eb.misc.getZeroBits(8*this._bs);for(t=0;o+r<=s;t+=4,o+=r)c=this._cipher.encrypt(n(c,e.slice(t,t+4)));return c},sjcl.mode.cbc={name:"cbc",encrypt:function(e,t,i,s,o){if(s&&s.length)throw new sjcl.exception.invalid("cbc can't authenticate data");if(128!==sjcl.bitArray.bitLength(i))throw new sjcl.exception.invalid("cbc iv must be 128 bits");var n,r=sjcl.bitArray,c=r.bitLength(t),a=0,h=[],u=eb.misc.xor;if(o&&0!==(127&c))throw new sjcl.exception.invalid("when padding is disabled, plaintext has to be a positive multiple of a block size");if(0!==(7&c))throw new sjcl.exception.invalid("pkcs#5 padding only works for multiples of a byte");for(n=0;a+128<=c;n+=4,a+=128)i=e.encrypt(u(i,t.slice(n,n+4))),h.splice(n,0,i[0],i[1],i[2],i[3]);return o||(c=16843009*(16-(c>>3&15)),i=e.encrypt(u(i,r.concat(t,[c,c,c,c]).slice(n,n+4))),h.splice(n,0,i[0],i[1],i[2],i[3])),h},decrypt:function(e,t,i,s,o){if(s&&s.length)throw new sjcl.exception.invalid("cbc can't authenticate data");if(128!==sjcl.bitArray.bitLength(i))throw new sjcl.exception.invalid("cbc iv must be 128 bits");if(127&sjcl.bitArray.bitLength(t)||!t.length)throw new sjcl.exception.corrupt("cbc ciphertext must be a positive multiple of the block size");var n,r,c,a=sjcl.bitArray,h=[],u=eb.misc.xor;for(s=s||[],n=0;n<t.length;n+=4)r=t.slice(n,n+4),c=u(i,e.decrypt(r)),h.splice(n,0,c[0],c[1],c[2],c[3]),i=r;if(o)return h;if(r=255&h[n-1],0===r||r>16)throw new sjcl.exception.corrupt("pkcs#5 padding corrupt");if(c=16843009*r,!a.equal(a.bitSlice([c,c,c,c],0,r<<3),a.bitSlice(h,(h.length<<5)-(r<<3),h.length<<5)))throw new sjcl.exception.corrupt("pkcs#5 padding corrupt");return a.bitSlice(h,0,(h.length<<5)-(r<<3))}},eb.comm={name:"comm",REQ_METHOD_GET:"GET",REQ_METHOD_POST:"POST",status:{ERROR_CLASS_SECURITY:8192,ERROR_CLASS_WRONGDATA:32768,SW_INVALID_TLV_FORMAT:32844,SW_WRONG_PADDING:32829,SW_STAT_INVALID_APIKEY:32872,SW_AUTHMETHOD_NOT_ALLOWED:32953,ERROR_CLASS_SECURITY_USER:40960,SW_HOTP_KEY_WRONG_LENGTH:41046,SW_HOTP_TOO_MANY_FAILED_TRIES:41062,SW_HOTP_WRONG_CODE:41136,SW_HOTP_COUNTER_OVERFLOW:41139,SW_AUTHMETHOD_UNKNOWN:41146,SW_AUTH_TOO_MANY_FAILED_TRIES:41137,SW_AUTH_MISMATCH_USER_ID:41142,SW_PASSWD_TOO_MANY_FAILED_TRIES:41059,SW_PASSWD_INVALID_LENGTH:41060,SW_WRONG_PASSWD:41061,SW_STAT_OK:36864,ERROR_CLASS_ERR_CHECK_ERRORS_6f:28416,PDATA_FAIL_CONNECTION:1,PDATA_FAIL_RESPONSE_PARSING:3,PDATA_FAIL_RESPONSE_FAILED:2},demangleNonce:function(e){var t=sjcl.bitArray,i=t.bitLength(e);if(0