noise-c.wasm
Version:
rweather/noise-c compiled to WebAssembly using Emscripten and optimized for small size
1 lines • 7.94 kB
JavaScript
(function(){var o;function e(e,t){return function(c,f,e,t){var n,s,l,u;if(n="function"==typeof e?{}:e,"function"!=typeof(s=t||e))throw new TypeError("noise-c.wasm: Callback is not a function");function d(e,t){var n,s;return n=u(),c._NoiseBuffer_create(n,e,t,e.length),s=n.dereference(),n.free(),s}function y(e,t){var n,s;if(e!==f.NOISE_ERROR_NONE)for(n in s=f)if(s[n]===e){if(t)try{t.free()}catch(e){}throw new Error(n)}}function r(e){var t,n,s,r,_,i,a,h,o;if(e!==f.NOISE_DH_CURVE448&&e!==f.NOISE_DH_CURVE25519)throw new TypeError("Invalid keypair type");t=u(),y(n=c._noise_dhstate_new_by_id(t,e),t),s=t.dereference(),t.free(),n=c._noise_dhstate_generate_keypair(s);try{y(n)}catch(e){throw r=e,c._noise_dhstate_free(s),r}_=e===f.NOISE_DH_CURVE448?56:32,i=l(_),a=l(_),n=c._noise_dhstate_get_keypair(s,i,i.length,a,a.length),c._noise_dhstate_free(s);try{return y(n),h=i.get(),i.free(),o=a.get(),a.free(),[h,o]}catch(e){throw r=e,i.free(),a.free(),r}}function _(e){var t;if(!(this instanceof _))return new _(e);t=u(),y(c._noise_cipherstate_new_by_id(t,e),t),this._state=t.dereference(),this._mac_length=c._noise_cipherstate_get_mac_length(this._state),t.free()}function i(e){this._state=e,this._mac_length=c._noise_cipherstate_get_mac_length(this._state)}function a(e){var t,n=this;if(!(this instanceof a))return new a(e);t=u(),e=l(0,e),y(c._noise_symmetricstate_new_by_name(t,e),t),this._state=t.dereference(),t.free(),e.free(),Object.defineProperty(this,"_mac_length",{configurable:!0,get:function(){var e;return 0<(e=c._noise_symmetricstate_get_mac_length(n._state))&&(n._mac_length=e),e}})}function h(e,t){var n,s;if(!(this instanceof h))return new h(e,t);n=u(),e=l(0,e),s=c._noise_handshakestate_new_by_name(n,e,t),e.free(),y(s,n),this._state=n.dereference(),n.free()}(c=c(n))._random_bytes=o,l=c.allocateBytes,u=c.allocatePointer,_.prototype={InitializeKey:function(e){var t;e=l(0,e),t=c._noise_cipherstate_init_key(this._state,e,e.length),e.free(),y(t,this)},HasKey:function(){return 1===c._noise_cipherstate_has_key(this._state)},SetNonce:function(e){y(c._noise_cipherstate_set_nonce(this._state,e),this)},EncryptWithAd:function(e,t){var n,s,r;return e=l(0,e),n=d(t=l(t.length+this._mac_length,t),t.length-this._mac_length),s=c._noise_cipherstate_encrypt_with_ad(this._state,e,e.length,n),r=t.get(),e.free(),t.free(),n.free(),y(s),r},DecryptWithAd:function(e,t){var n,s,r;return e=l(0,e),n=d(t=l(0,t),t.length),s=c._noise_cipherstate_decrypt_with_ad(this._state,e,e.length,n),r=t.get().slice(0,t.length-this._mac_length),e.free(),t.free(),n.free(),y(s),r},Rekey:function(){throw"Not implemented"},free:function(){var e;e=c._noise_cipherstate_free(this._state),delete this._state,delete this._mac_length,y(e)}},Object.defineProperty(_.prototype,"constructor",{enumerable:!1,value:_}),i.prototype=Object.create(_.prototype),Object.defineProperty(i.prototype,"constructor",{enumerable:!1,value:i}),a.prototype={MixKey:function(e){var t;e=l(0,e),t=c._noise_symmetricstate_mix_key(this._state,e,e.length),e.free(),y(t,this)},MixHash:function(e){var t;e=l(0,e),t=c._noise_symmetricstate_mix_hash(this._state,e,e.length),e.free(),y(t,this)},MixKeyAndHash:function(e){var t,n,s,r;this.MixKey(e),t=u(),n=c._SymmetricState_get_ck(this._state,t),s=t.dereference(n),t.free(),r=s.get(),s.free(),this.MixHash(r)},EncryptAndHash:function(e){var t,n,s;return t=d(e=l(e.length+this._mac_length,e),e.length-this._mac_length),n=c._noise_symmetricstate_encrypt_and_hash(this._state,t),s=e.get(),e.free(),t.free(),y(n,this),s},DecryptAndHash:function(e){var t,n,s;return t=d(e=l(0,e),e.length),n=c._noise_symmetricstate_decrypt_and_hash(this._state,t),s=e.get().slice(0,e.length-this._mac_length),e.free(),t.free(),y(n,this),s},Split:function(){var t,n,e,s,r,_;t=u(),n=u(),e=c._noise_symmetricstate_split(this._state,t,n);try{y(e)}catch(e){throw s=e,t.free(),n.free(),s}r=new i(t.dereference()),_=new i(n.dereference()),t.free(),n.free();try{this.free()}catch(e){s=e;try{r.free()}catch(e){}try{_.free()}catch(e){}throw s}return[r,_]},free:function(){var e;e=c._noise_symmetricstate_free(this._state),delete this._state,delete this._mac_length,y(e)}},Object.defineProperty(a.prototype,"constructor",{enumerable:!1,value:a}),h.prototype={Initialize:function(e,t,n,s){var r,_;if(null==e&&(e=null),null==t&&(t=null),null==n&&(n=null),null==s&&(s=null),e&&(e=l(0,e),r=c._noise_handshakestate_set_prologue(this._state,e,e.length),e.free(),y(r,this)),s&&1===c._noise_handshakestate_needs_pre_shared_key(this._state)&&(s=l(0,s),r=c._noise_handshakestate_set_pre_shared_key(this._state,s,s.length),s.free(),y(r,this)),1===c._noise_handshakestate_needs_local_keypair(this._state)){if(!t)throw new Error("Local static private key (s) required, but not provided");_=c._noise_handshakestate_get_local_keypair_dh(this._state),t=l(0,t),r=c._noise_dhstate_set_keypair_private(_,t,t.length),t.free(),y(r,this)}if(1===c._noise_handshakestate_needs_remote_public_key(this._state)){if(!n)throw new Error("Remote static public key (rs) required, but not provided");_=c._noise_handshakestate_get_remote_public_key_dh(this._state),n=l(0,n),r=c._noise_dhstate_set_public_key(_,n,n.length),n.free(),y(r,this)}y(r=c._noise_handshakestate_start(this._state),this)},GetAction:function(){return c._noise_handshakestate_get_action(this._state)},FallbackTo:function(e){null==e&&(e=f.NOISE_PATTERN_XX_FALLBACK),y(c._noise_handshakestate_fallback_to(this._state,e),this)},WriteMessage:function(e){var t,n,s,r,_,i,a;null==e&&(e=null),n=d(t=l(f.NOISE_MAX_PAYLOAD_LEN),0),s=null,e&&(s=d(e=l(0,e),e.length)),r=c._noise_handshakestate_write_message(this._state,n,s),e&&(e.free(),s.free());try{y(r,this)}catch(e){throw _=e,t.free(),n.free(),_}return i=c._NoiseBuffer_get_size(n),a=t.get().slice(0,i),t.free(),n.free(),a},ReadMessage:function(e,t,n){var s,r,_,i,a,h,o;null==t&&(t=!1),null==n&&(n=!1),s=d(e=l(0,e),e.length),r=null,t&&(r=d(_=l(f.NOISE_MAX_PAYLOAD_LEN),_.length)),i=c._noise_handshakestate_read_message(this._state,s,r),e.free(),s.free();try{y(i,n?void 0:this)}catch(e){throw a=e,t&&(_.free(),r.free()),a}return h=null,t&&(o=c._NoiseBuffer_get_size(r),h=_.get().slice(0,o),_.free(),r.free()),h},Split:function(){var t,n,e,s,r,_;t=u(),n=u(),e=c._noise_handshakestate_split(this._state,t,n);try{y(e,this)}catch(e){throw s=e,t.free(),n.free(),s}r=new i(t.dereference()),_=new i(n.dereference()),t.free(),n.free();try{this.free()}catch(e){s=e;try{r.free()}catch(e){}try{_.free()}catch(e){}throw s}return[r,_]},GetHandshakeHash:function(){var t,e,n,s,r;t=l(64),e=c._noise_handshakestate_get_handshake_hash(this._state,t,t.length);try{switch(y(e),n=c._NoiseHandshakeState_get_hash_id(this._state)){case f.NOISE_HASH_BLAKE2s:case f.NOISE_HASH_SHA256:s=t.get().slice(0,32);break;case f.NOISE_HASH_BLAKE2b:case f.NOISE_HASH_SHA512:s=t.get();break;default:throw new Error("invalid hash id:"+n)}t.free(),t=null}catch(e){throw r=e,t.free(),t=null,r}return s},GetRemotePublicKey:function(){var e,t,n,s,r,_;if(c._noise_handshakestate_has_remote_public_key(this._state)&&(e=c._noise_handshakestate_get_remote_public_key_dh(this._state),t=c._noise_dhstate_get_public_key_length(e))){n=l(t),s=c._noise_dhstate_get_public_key(e,n,t),e=null;try{return y(s),r=n.get(),n.free(),r}catch(e){throw _=e,n.free(),_}}return null},free:function(){var e;e=c._noise_handshakestate_free(this._state),delete this._state,y(e)}},Object.defineProperty(h.prototype,"constructor",{enumerable:!1,value:h}),c.then(function(){s({constants:f,CipherState:_,SymmetricState:a,HandshakeState:h,CreateKeyPair:r,_lib_internal:c})})}.bind(this,e,t)}o="undefined"!=typeof crypto?function(e){var t;return t=new Uint8Array(e),crypto.getRandomValues(t),t}:require("crypto").randomBytes,"function"==typeof define&&define.amd?define(["./noise-c","./constants"],e):"object"==typeof exports?module.exports=e(require("./noise-c"),require("./constants")):this.noise_c_wasm=e(this.__noise_c_wasm,this.__noise_c_wasm_constants)}).call(this);