1// Generated by LiveScript 1.6.0 2/** 3 * @package noise-c.wasm 4 * @author Nazar Mokrynskyi <[email protected]> 5 * @license 0BSD 6 */ 7(function(){ 8 var random_bytes; 9 if (typeof crypto !== 'undefined') { 10 /** 11 * @param {number} size 12 * 13 * @return {!Uint8Array} 14 */ 15 random_bytes = function(size){ 16 var array; 17 array = new Uint8Array(size); 18 crypto.getRandomValues(array); 19 return array; 20 }; 21 } else { 22 /** 23 * @param {string} size 24 * 25 * @return {!Uint8Array} 26 */ 27 random_bytes = require('crypto').randomBytes; 28 } 29 function CreateLib(lib, constants, arg1, arg2){ 30 var options, callback, allocate, allocate_pointer; 31 options = typeof arg1 === 'function' ? {} : arg1; 32 callback = arg2 || arg1; 33 if (typeof callback !== 'function') { 34 throw new TypeError('noise-c.wasm: Callback is not a function'); 35 } 36 lib = lib(options); 37 lib['_random_bytes'] = random_bytes; 38 allocate = lib['allocateBytes']; 39 allocate_pointer = lib['allocatePointer']; 40 function allocate_buffer(data, size){ 41 var tmp, buffer; 42 tmp = allocate_pointer(); 43 lib._NoiseBuffer_create(tmp, data, size, data.length); 44 buffer = tmp.dereference(); 45 tmp.free(); 46 return buffer; 47 } 48 function assert_no_error(error, object_to_free){ 49 var key, ref$, code; 50 if (error === constants.NOISE_ERROR_NONE) { 51 return; 52 } 53 for (key in ref$ = constants) { 54 code = ref$[key]; 55 if (code === error) { 56 if (object_to_free) { 57 try { 58 object_to_free.free(); 59 } catch (e$) {} 60 } 61 throw new Error(key); 62 } 63 } 64 } 65 /** 66 * Create a new X25519 or X448 keypair 67 * 68 * @param {number} curve_id constants.NOISE_DH_CURVE25519 or constants.NOISE_DH_CURVE448 69 * 70 * @return {!Uint8Array[]} `[private_key, public_key]` 71 * 72 * @throws {TypeError} In case incorrect `curve_id` specified 73 */ 74 function CreateKeyPair(curve_id){ 75 var tmp, error, dh, e, key_length, private_buffer, public_buffer, private_key, public_key; 76 if (!(curve_id === constants.NOISE_DH_CURVE448 || curve_id === constants.NOISE_DH_CURVE25519)) { 77 throw new TypeError('Invalid keypair type'); 78 } 79 tmp = allocate_pointer(); 80 error = lib._noise_dhstate_new_by_id(tmp, curve_id); 81 assert_no_error(error, tmp); 82 dh = tmp.dereference(); 83 tmp.free(); 84 error = lib._noise_dhstate_generate_keypair(dh); 85 try { 86 assert_no_error(error); 87 } catch (e$) { 88 e = e$; 89 lib._noise_dhstate_free(dh); 90 throw e; 91 } 92 if (curve_id === constants.NOISE_DH_CURVE448) { 93 key_length = 56; 94 } else { 95 key_length = 32; 96 } 97 private_buffer = allocate(key_length); 98 public_buffer = allocate(key_length); 99 error = lib._noise_dhstate_get_keypair(dh, private_buffer, private_buffer.length, public_buffer, public_buffer.length); 100 lib._noise_dhstate_free(dh); 101 try { 102 assert_no_error(error); 103 private_key = private_buffer.get(); 104 private_buffer.free(); 105 public_key = public_buffer.get(); 106 public_buffer.free(); 107 return [private_key, public_key]; 108 } catch (e$) { 109 e = e$; 110 private_buffer.free(); 111 public_buffer.free(); 112 throw e; 113 } 114 } 115 /** 116 * The CipherState object, API is close to the spec: http://noiseprotocol.org/noise.html#the-cipherstate-object 117 * 118 * NOTE: If you ever get an exception with Error object, whose message is one of constants.NOISE_ERROR_* keys, object is no longer usable and there is no need 119 * to call free() method, as it was called for you automatically already (except in EncryptWithAd and DecryptWithAd) 120 * 121 * @param {string} cipher constants.NOISE_CIPHER_CHACHAPOLY, constants.NOISE_CIPHER_AESGCM, etc. 122 */ 123 function CipherState(cipher){ 124 var tmp, error; 125 if (!(this instanceof CipherState)) { 126 return new CipherState(cipher); 127 } 128 tmp = allocate_pointer(); 129 error = lib._noise_cipherstate_new_by_id(tmp, cipher); 130 assert_no_error(error, tmp); 131 this._state = tmp.dereference(); 132 this._mac_length = lib._noise_cipherstate_get_mac_length(this._state); 133 tmp.free(); 134 } 135 CipherState.prototype = { 136 /** 137 * @param {Uint8Array} key 138 */ 139 InitializeKey: function(key){ 140 var error; 141 key = allocate(0, key); 142 error = lib._noise_cipherstate_init_key(this._state, key, key.length); 143 key.free(); 144 assert_no_error(error, this); 145 }, 146 HasKey: function(){ 147 return lib._noise_cipherstate_has_key(this._state) === 1; 148 } 149 /** 150 * @param {number} nonce 151 */, 152 SetNonce: function(nonce){ 153 var error; 154 error = lib._noise_cipherstate_set_nonce(this._state, nonce); 155 assert_no_error(error, this); 156 } 157 /** 158 * @param {Uint8Array} ad 159 * @param {Uint8Array} plaintext 160 * 161 * @return {Uint8Array} 162 */, 163 EncryptWithAd: function(ad, plaintext){ 164 var buffer, error, ciphertext; 165 ad = allocate(0, ad); 166 plaintext = allocate(plaintext.length + this._mac_length, plaintext); 167 buffer = allocate_buffer(plaintext, plaintext.length - this._mac_length); 168 error = lib._noise_cipherstate_encrypt_with_ad(this._state, ad, ad.length, buffer); 169 ciphertext = plaintext.get();
170 ad.free(); 171 plaintext.free(); 172 buffer.free(); 173 assert_no_error(error); 174 return ciphertext; 175 } 176 /** 177 * @param {Uint8Array} ad 178 * @param {Uint8Array} ciphertext 179 * 180 * @return {Uint8Array} 181 */, 182 DecryptWithAd: function(ad, ciphertext){ 183 var buffer, error, plaintext; 184 ad = allocate(0, ad); 185 ciphertext = allocate(0, ciphertext); 186 buffer = allocate_buffer(ciphertext, ciphertext.length); 187 error = lib._noise_cipherstate_decrypt_with_ad(this._state, ad, ad.length, buffer); 188 plaintext = ciphertext.get().slice(0, ciphertext.length - this._mac_length); 189 ad.free(); 190 ciphertext.free(); 191 buffer.free(); 192 assert_no_error(error); 193 return plaintext; 194 } 195 /** 196 * @return {boolean} 197 */, 198 Rekey: function(){ 199 throw 'Not implemented'; 200 } 201 /** 202 * Call this when object is not needed anymore to avoid memory leaks 203 */, 204 free: function(){ 205 var error; 206 error = lib._noise_cipherstate_free(this._state); 207 delete this._state; 208 delete this._mac_length; 209 assert_no_error(error); 210 } 211 }; 212 Object.defineProperty(CipherState.prototype, 'constructor', { 213 enumerable: false, 214 value: CipherState 215 }); 216 function CipherState_split(state){ 217 this._state = state; 218 this._mac_length = lib._noise_cipherstate_get_mac_length(this._state); 219 } 220 CipherState_split.prototype = Object.create(CipherState.prototype); 221 Object.defineProperty(CipherState_split.prototype, 'constructor', { 222 enumerable: false, 223 value: CipherState_split 224 }); 225 /** 226 * The SymmetricState object, API is close to the spec: http://noiseprotocol.org/noise.html#the-symmetricstate-object 227 * 228 * NOTE: If you ever get an exception with Error object, whose message is one of constants.NOISE_ERROR_* keys, object is no longer usable and there is no need 229 * to call free() method, as it was called for you automatically already 230 * 231 * @param {string} protocol_name The name of the Noise protocol to use, for instance, Noise_N_25519_ChaChaPoly_BLAKE2b 232 */ 233 function SymmetricState(protocol_name){ 234 var tmp, error, this$ = this; 235 if (!(this instanceof SymmetricState)) { 236 return new SymmetricState(protocol_name); 237 } 238 tmp = allocate_pointer(); 239 protocol_name = allocate(0, protocol_name); 240 error = lib._noise_symmetricstate_new_by_name(tmp, protocol_name); 241 assert_no_error(error, tmp); 242 this._state = tmp.dereference(); 243 tmp.free(); 244 protocol_name.free(); 245 Object.defineProperty(this, '_mac_length', { 246 configurable: true, 247 get: function(){ 248 var mac_length; 249 mac_length = lib._noise_symmetricstate_get_mac_length(this$._state); 250 if (mac_length > 0) { 251 this$._mac_length = mac_length; 252 } 253 return mac_length; 254 } 255 }); 256 } 257 SymmetricState.prototype = { 258 /** 259 * @param {Uint8Array} input_key_material 260 */ 261 MixKey: function(input_key_material){ 262 var error; 263 input_key_material = allocate(0, input_key_material); 264 error = lib._noise_symmetricstate_mix_key(this._state, input_key_material, input_key_material.length); 265 input_key_material.free(); 266 assert_no_error(error, this); 267 } 268 /** 269 * @param {Uint8Array} data 270 */, 271 MixHash: function(data){ 272 var error; 273 data = allocate(0, data); 274 error = lib._noise_symmetricstate_mix_hash(this._state, data, data.length); 275 data.free(); 276 assert_no_error(error, this); 277 } 278 /** 279 * @param {Uint8Array} input_key_material 280 */, 281 MixKeyAndHash: function(input_key_material){ 282 var tmp, length, ck, data; 283 this.MixKey(input_key_material); 284 tmp = allocate_pointer(); 285 length = lib._SymmetricState_get_ck(this._state, tmp); 286 ck = tmp.dereference(length); 287 tmp.free(); 288 data = ck.get(); 289 ck.free(); 290 this.MixHash(data); 291 } 292 /** 293 * @param {Uint8Array} plaintext 294 * 295 * @return {Uint8Array} 296 */, 297 EncryptAndHash: function(plaintext){ 298 var buffer, error, ciphertext; 299 plaintext = allocate(plaintext.length + this._mac_length, plaintext); 300 buffer = allocate_buffer(plaintext, plaintext.length - this._mac_length); 301 error = lib._noise_symmetricstate_encrypt_and_hash(this._state, buffer); 302 ciphertext = plaintext.get(); 303 plaintext.free(); 304 buffer.free(); 305 assert_no_error(error, this); 306 return ciphertext; 307 } 308 /** 309 * @param {Uint8Array} ciphertext 310 * 311 * @return {Uint8Array} 312 */, 313 DecryptAndHash: function(ciphertext){ 314 var buffer, error, plaintext; 315 ciphertext = allocate(0, ciphertext); 316 buffer = allocate_buffer(ciphertext, ciphertext.length); 317 error = lib._noise_symmetricstate_decrypt_and_hash(this._state, buffer); 318 plaintext = ciphertext.get().slice(0, ciphertext.length - this._mac_length); 319 ciphertext.free(); 320 buffer.free(); 321 assert_no_error(error, this); 322 return plaintext; 323 } 324 /** 325 * @return {CipherState[]} 326 */, 327 Split: function(){ 328 var tmp1, tmp2, error, e, cs1, cs2; 329 tmp1 = allocate_pointer(); 330 tmp2 = allocate_pointer(); 331 error = lib._noise_symmetricstate_split(this._state, tmp1, tmp2); 332 try { 333 assert_no_error(error); 334 } catch (e$) { 335 e = e$; 336 tmp1.free(); 337 tmp2.free(); 338 throw e; 339 } 340 cs1 = new CipherState_split(tmp1.dereference()); 341 cs2 = new CipherState_split(tmp2.dereference()); 342 tmp1.free(); 343 tmp2.free(); 344 try { 345 this.free(); 346 } catch (e$) { 347 e = e$; 348 try { 349 cs1.free(); 350 } catch (e$) {} 351 try { 352 cs2.free(); 353 } catch (e$) {} 354 throw e; 355 } 356 return [cs1, cs2]; 357 } 358 /** 359 * Call this when object is not needed anymore to avoid memory leaks 360 */, 361 free: function(){ 362 var error; 363 error = lib._noise_symmetricstate_free(this._state); 364 delete this._state; 365 delete this._mac_length; 366 assert_no_error(error); 367 } 368 };
369 Object.defineProperty(SymmetricState.prototype, 'constructor', { 370 enumerable: false, 371 value: SymmetricState 372 }); 373 /** 374 * The HandshakeState object, API is close to the spec: http://noiseprotocol.org/noise.html#the-handshakestate-object 375 * 376 * NOTE: If you ever get an exception with Error object, whose message is one of constants.NOISE_ERROR_* keys, object is no longer usable and there is no need 377 * to call free() method, as it was called for you automatically already (except in ReadMessage with fallback_supported == true) 378 * 379 * @param {string} protocol_name The name of the Noise protocol to use, for instance, Noise_N_25519_ChaChaPoly_BLAKE2b 380 * @param {number} role The role for the new object, either constants.NOISE_ROLE_INITIATOR or constants.NOISE_ROLE_RESPONDER 381 */ 382 function HandshakeState(protocol_name, role){ 383 var tmp, error; 384 if (!(this instanceof HandshakeState)) { 385 return new HandshakeState(protocol_name, role); 386 } 387 tmp = allocate_pointer(); 388 protocol_name = allocate(0, protocol_name); 389 error = lib._noise_handshakestate_new_by_name(tmp, protocol_name, role); 390 protocol_name.free(); 391 assert_no_error(error, tmp); 392 this._state = tmp.dereference(); 393 tmp.free(); 394 } 395 HandshakeState.prototype = { 396 /** 397 * Must be called after object creation and after switch to a fallback handshake. 398 * 399 * In case of fallback handshake it is not required to specify values that are the same as in previous Initialize() call, those will be used by default 400 * 401 * @param {null|Uint8Array} prologue Prologue value 402 * @param {null|Uint8Array} s Local static private key 403 * @param {null|Uint8Array} rs Remote static public key 404 * @param {null|Uint8Array} psk Pre-shared symmetric key 405 */ 406 Initialize: function(prologue, s, rs, psk){ 407 var error, dh; 408 prologue == null && (prologue = null); 409 s == null && (s = null); 410 rs == null && (rs = null); 411 psk == null && (psk = null); 412 if (prologue) { 413 prologue = allocate(0, prologue); 414 error = lib._noise_handshakestate_set_prologue(this._state, prologue, prologue.length); 415 prologue.free(); 416 assert_no_error(error, this); 417 } 418 if (psk && lib._noise_handshakestate_needs_pre_shared_key(this._state) === 1) { 419 psk = allocate(0, psk); 420 error = lib._noise_handshakestate_set_pre_shared_key(this._state, psk, psk.length); 421 psk.free(); 422 assert_no_error(error, this); 423 } 424 if (lib._noise_handshakestate_needs_local_keypair(this._state) === 1) { 425 if (!s) { 426 throw new Error('Local static private key (s) required, but not provided'); 427 } 428 dh = lib._noise_handshakestate_get_local_keypair_dh(this._state); 429 s = allocate(0, s); 430 error = lib._noise_dhstate_set_keypair_private(dh, s, s.length); 431 s.free(); 432 assert_no_error(error, this); 433 } 434 if (lib._noise_handshakestate_needs_remote_public_key(this._state) === 1) { 435 if (!rs) { 436 throw new Error('Remote static public key (rs) required, but not provided'); 437 } 438 dh = lib._noise_handshakestate_get_remote_public_key_dh(this._state); 439 rs = allocate(0, rs); 440 error = lib._noise_dhstate_set_public_key(dh, rs, rs.length); 441 rs.free(); 442 assert_no_error(error, this); 443 } 444 error = lib._noise_handshakestate_start(this._state); 445 assert_no_error(error, this); 446 } 447 /** 448 * @return {number} One of constants.NOISE_ACTION_* 449 */, 450 GetAction: function(){ 451 return lib._noise_handshakestate_get_action(this._state); 452 } 453 /** 454 * Might be called when GetAction() returned constants.NOISE_ACTION_FAILED and switching to fallback protocol is desired, don't forget to call Initialize() 455 * after FallbackTo() 456 * 457 * @param {number} pattern_id One of constants.NOISE_PATTERN_*_FALLBACK* 458 */, 459 FallbackTo: function(pattern_id){ 460 var error; 461 pattern_id == null && (pattern_id = constants.NOISE_PATTERN_XX_FALLBACK); 462 error = lib._noise_handshakestate_fallback_to(this._state, pattern_id); 463 assert_no_error(error, this); 464 } 465 /** 466 * @param {null|Uint8Array} payload null if no payload is required 467 * 468 * @return {Uint8Array} Message that should be sent to the other side 469 */, 470 WriteMessage: function(payload){ 471 var message, message_buffer, payload_buffer, error, e, message_length, real_message; 472 payload == null && (payload = null); 473 message = allocate(constants.NOISE_MAX_PAYLOAD_LEN); 474 message_buffer = allocate_buffer(message, 0); 475 payload_buffer = null; 476 if (payload) { 477 payload = allocate(0, payload); 478 payload_buffer = allocate_buffer(payload, payload.length); 479 } 480 error = lib._noise_handshakestate_write_message(this._state, message_buffer, payload_buffer); 481 if (payload) { 482 payload.free(); 483 payload_buffer.free(); 484 } 485 try { 486 assert_no_error(error, this); 487 } catch (e$) { 488 e = e$; 489 message.free(); 490 message_buffer.free(); 491 throw e; 492 } 493 message_length = lib._NoiseBuffer_get_size(message_buffer); 494 real_message = message.get().slice(0, message_length); 495 message.free(); 496 message_buffer.free(); 497 return real_message; 498 } 499 /** 500 * @param {Uint8Array} message Message received from the other side 501 * @param {boolean} payload_needed false if the application does not need the message payload 502 * @param {boolean} fallback_supported true if application is ready to switch to fallback pattern (will throw, but without free() call on read failure) 503 * 504 * @return {null|Uint8Array} 505 */, 506 ReadMessage: function(message, payload_needed, fallback_supported){ 507 var message_buffer, payload_buffer, payload, error, e, real_payload, payload_length;
508 payload_needed == null && (payload_needed = false); 509 fallback_supported == null && (fallback_supported = false); 510 message = allocate(0, message); 511 message_buffer = allocate_buffer(message, message.length); 512 payload_buffer = null; 513 if (payload_needed) { 514 payload = allocate(constants.NOISE_MAX_PAYLOAD_LEN); 515 payload_buffer = allocate_buffer(payload, payload.length); 516 } 517 error = lib._noise_handshakestate_read_message(this._state, message_buffer, payload_buffer); 518 message.free(); 519 message_buffer.free(); 520 try { 521 assert_no_error(error, fallback_supported ? undefined : this); 522 } catch (e$) { 523 e = e$; 524 if (payload_needed) { 525 payload.free(); 526 payload_buffer.free(); 527 } 528 throw e; 529 } 530 real_payload = null; 531 if (payload_needed) { 532 payload_length = lib._NoiseBuffer_get_size(payload_buffer); 533 real_payload = payload.get().slice(0, payload_length); 534 payload.free(); 535 payload_buffer.free(); 536 } 537 return real_payload; 538 } 539 /** 540 * @return {CipherState[]} [send, receive] 541 */, 542 Split: function(){ 543 var tmp1, tmp2, error, e, cs1, cs2; 544 tmp1 = allocate_pointer(); 545 tmp2 = allocate_pointer(); 546 error = lib._noise_handshakestate_split(this._state, tmp1, tmp2); 547 try { 548 assert_no_error(error, this); 549 } catch (e$) { 550 e = e$; 551 tmp1.free(); 552 tmp2.free(); 553 throw e; 554 } 555 cs1 = new CipherState_split(tmp1.dereference()); 556 cs2 = new CipherState_split(tmp2.dereference()); 557 tmp1.free(); 558 tmp2.free(); 559 try { 560 this.free(); 561 } catch (e$) { 562 e = e$; 563 try { 564 cs1.free(); 565 } catch (e$) {} 566 try { 567 cs2.free(); 568 } catch (e$) {} 569 throw e; 570 } 571 return [cs1, cs2]; 572 } 573 /** 574 * Gets the channel binding has if the handshake is completed otherwise throws `Error` 575 * 576 * @return {Uint8Array} 577 * 578 * @throws {Error} 579 */, 580 GetHandshakeHash: function(){ 581 var hash_buffer, error, hs_id, real_hash, e; 582 hash_buffer = allocate(64); 583 error = lib._noise_handshakestate_get_handshake_hash(this._state, hash_buffer, hash_buffer.length); 584 try { 585 assert_no_error(error); 586 hs_id = lib._NoiseHandshakeState_get_hash_id(this._state); 587 switch (hs_id) { 588 case constants.NOISE_HASH_BLAKE2s: 589 case constants.NOISE_HASH_SHA256: 590 real_hash = hash_buffer.get().slice(0, 32); 591 break; 592 case constants.NOISE_HASH_BLAKE2b: 593 case constants.NOISE_HASH_SHA512: 594 real_hash = hash_buffer.get(); 595 break; 596 default: 597 throw new Error("invalid hash id:" + hs_id); 598 } 599 hash_buffer.free(); 600 hash_buffer = null; 601 } catch (e$) { 602 e = e$; 603 hash_buffer.free(); 604 hash_buffer = null; 605 throw e; 606 } 607 return real_hash; 608 } 609 /** 610 * Gets raw remote static public key if available 611 * 612 * @return {Uint8Array} `null` if not available 613 */, 614 GetRemotePublicKey: function(){ 615 var dhs, key_length, key_buffer, error, real_key, e; 616 if (lib._noise_handshakestate_has_remote_public_key(this._state)) { 617 dhs = lib._noise_handshakestate_get_remote_public_key_dh(this._state); 618 key_length = lib._noise_dhstate_get_public_key_length(dhs); 619 if (key_length) { 620 key_buffer = allocate(key_length); 621 error = lib._noise_dhstate_get_public_key(dhs, key_buffer, key_length); 622 dhs = null; 623 try { 624 assert_no_error(error); 625 real_key = key_buffer.get(); 626 key_buffer.free(); 627 return real_key; 628 } catch (e$) { 629 e = e$; 630 key_buffer.free(); 631 throw e; 632 } 633 } 634 } 635 return null; 636 } 637 /** 638 * Call this when object is not needed anymore to avoid memory leaks 639 */, 640 free: function(){ 641 var error; 642 error = lib._noise_handshakestate_free(this._state); 643 delete this._state; 644 assert_no_error(error); 645 } 646 };
647 Object.defineProperty(HandshakeState.prototype, 'constructor', { 648 enumerable: false, 649 value: HandshakeState 650 }); 651 lib.then(function(){ 652 callback({ 653 'constants': constants, 654 'CipherState': CipherState, 655 'SymmetricState': SymmetricState, 656 'HandshakeState': HandshakeState, 657 'CreateKeyPair': CreateKeyPair, 658 '_lib_internal': lib 659 }); 660 }); 661 } 662 function Wrapper(lib, constants){ 663 return CreateLib.bind(this, lib, constants); 664 } 665 if (typeof define === 'function' && define['amd']) { 666 define(['./noise-c', './constants'], Wrapper); 667 } else if (typeof exports === 'object') { 668 module.exports = Wrapper(require('./noise-c'), require('./constants')); 669 } else { 670 this['noise_c_wasm'] = Wrapper(this['__noise_c_wasm'], this['__noise_c_wasm_constants']); 671 } 672}).call(this);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.