Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
state = bob.session_states[bob.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(1);
expect(state.recv_chains[0].chain_key.idx).toBe(1);
expect(state.send_chain.chain_key.idx).toBe(0);
expect(state.recv_chains[0].message_keys.length).toBe(0);
const hello_alice0_plaintext = 'Hello0';
const hello_alice0_encrypted = await bob.encrypt(hello_alice0_plaintext);
await bob.encrypt('Hello1'); // unused result
const hello_alice2_plaintext = 'Hello2';
const hello_alice2_encrypted = await bob.encrypt(hello_alice2_plaintext);
const hello_alice2_decrypted = await alice.decrypt(alice_store, hello_alice2_encrypted);
expect(sodium.to_string(hello_alice2_decrypted)).toBe(hello_alice2_plaintext);
// Alice has two skipped message keys in her new receive chain:
state = alice.session_states[alice.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(2);
expect(state.recv_chains[0].chain_key.idx).toBe(3);
expect(state.send_chain.chain_key.idx).toBe(0);
expect(state.recv_chains[0].message_keys.length).toBe(2);
expect(state.recv_chains[0].message_keys[0].counter).toBe(0);
expect(state.recv_chains[0].message_keys[1].counter).toBe(1);
const hello_bob0_plaintext = 'Hello0';
const hello_bob0_encrypted = await alice.encrypt(hello_bob0_plaintext);
const hello_bob0_decrypted = await bob.decrypt(bob_store, hello_bob0_encrypted);
expect(sodium.to_string(hello_bob0_decrypted)).toBe(hello_bob0_plaintext);
expect(sodium.to_string(hello_alice2_decrypted)).toBe(hello_alice2_plaintext);
// Alice has two skipped message keys in her new receive chain:
state = alice.session_states[alice.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(2);
expect(state.recv_chains[0].chain_key.idx).toBe(3);
expect(state.send_chain.chain_key.idx).toBe(0);
expect(state.recv_chains[0].message_keys.length).toBe(2);
expect(state.recv_chains[0].message_keys[0].counter).toBe(0);
expect(state.recv_chains[0].message_keys[1].counter).toBe(1);
const hello_bob0_plaintext = 'Hello0';
const hello_bob0_encrypted = await alice.encrypt(hello_bob0_plaintext);
const hello_bob0_decrypted = await bob.decrypt(bob_store, hello_bob0_encrypted);
expect(sodium.to_string(hello_bob0_decrypted)).toBe(hello_bob0_plaintext);
// For Bob everything is normal still. A new message from Alice means a
// new receive chain has been created and again no skipped message keys.
state = bob.session_states[bob.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(2);
expect(state.recv_chains[0].chain_key.idx).toBe(1);
expect(state.send_chain.chain_key.idx).toBe(0);
expect(state.recv_chains[0].message_keys.length).toBe(0);
const hello_alice0_decrypted = await alice.decrypt(alice_store, hello_alice0_encrypted);
expect(sodium.to_string(hello_alice0_decrypted)).toBe(hello_alice0_plaintext);
// Alice received the first of the two missing messages. Therefore
// only one message key is still skipped (counter value = 1).
// Alice received the first of the two missing messages. Therefore
// only one message key is still skipped (counter value = 1).
state = alice.session_states[alice.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(2);
expect(state.recv_chains[0].message_keys.length).toBe(1);
expect(state.recv_chains[0].message_keys[0].counter).toBe(1);
const hello_again0_plaintext = 'Again0';
const hello_again0_encrypted = await bob.encrypt(hello_again0_plaintext);
const hello_again1_plaintext = 'Again1';
const hello_again1_encrypted = await bob.encrypt(hello_again1_plaintext);
const hello_again1_decrypted = await alice.decrypt(alice_store, hello_again1_encrypted);
expect(sodium.to_string(hello_again1_decrypted)).toBe(hello_again1_plaintext);
// Alice received the first of the two missing messages. Therefore
// only one message key is still skipped (counter value = 1).
state = alice.session_states[alice.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(3);
expect(state.recv_chains[0].message_keys.length).toBe(1);
expect(state.recv_chains[1].message_keys.length).toBe(1);
expect(state.recv_chains[0].message_keys[0].counter).toBe(0);
expect(state.recv_chains[1].message_keys[0].counter).toBe(1);
const hello_again0_decrypted = await alice.decrypt(alice_store, hello_again0_encrypted);
expect(sodium.to_string(hello_again0_decrypted)).toBe(hello_again0_plaintext);
});
const alice_prekey = await alice_store.load_prekey(0);
const alice_bundle = Proteus.keys.PreKeyBundle.new(alice_ident.public_key, alice_prekey);
const alice = await Proteus.session.Session.init_from_prekey(alice_ident, bob_bundle);
const hello_bob_plaintext = 'Hello Bob!';
const hello_bob_encrypted = await alice.encrypt(hello_bob_plaintext);
const bob = await Proteus.session.Session.init_from_prekey(bob_ident, alice_bundle);
const hello_alice_plaintext = 'Hello Alice!';
const hello_alice_encrypted = await bob.encrypt(hello_alice_plaintext);
expect(alice.session_tag.toString()).not.toEqual(bob.session_tag.toString());
const hello_bob_decrypted = await bob.decrypt(bob_store, hello_bob_encrypted);
expect(sodium.to_string(hello_bob_decrypted)).toBe(hello_bob_plaintext);
const hello_alice_decrypted = await alice.decrypt(alice_store, hello_alice_encrypted);
expect(sodium.to_string(hello_alice_decrypted)).toBe(hello_alice_plaintext);
const echo_bob1_plaintext = 'Echo Bob1!';
const echo_bob1_encrypted = await alice.encrypt(echo_bob1_plaintext);
const echo_alice1_plaintext = 'Echo Alice1!';
const echo_alice1_encrypted = await bob.encrypt(echo_alice1_plaintext);
const echo_bob1_decrypted = await bob.decrypt(bob_store, echo_bob1_encrypted);
expect(sodium.to_string(echo_bob1_decrypted)).toBe(echo_bob1_plaintext);
expect(Object.keys(bob.session_states).length).toBe(2);
const echo_alice1_decrypted = await alice.decrypt(alice_store, echo_alice1_encrypted);
expect(sodium.to_string(echo_alice1_decrypted)).toBe(echo_alice1_plaintext);
expect(sodium.to_string(await alice.decrypt(alice_store, hello_alice))).toBe('Hello Alice!');
expect(alice.pending_prekey).toBe(null);
expect(alice.session_states[alice.session_tag.toString()].state.recv_chains.length).toBe(2);
expect(alice.remote_identity.fingerprint()).toBe(bob.local_identity.public_key.fingerprint());
const ping_bob_1 = await alice.encrypt('Ping1!');
const ping_bob_2 = await alice.encrypt('Ping2!');
expect(alice.session_states[alice.session_tag.toString()].state.prev_counter).toBe(2);
expect(ping_bob_1.message).toEqual(jasmine.any(Proteus.message.CipherMessage));
expect(ping_bob_2.message).toEqual(jasmine.any(Proteus.message.CipherMessage));
expect(sodium.to_string(await bob.decrypt(bob_store, ping_bob_1))).toBe('Ping1!');
expect(bob.session_states[bob.session_tag.toString()].state.recv_chains.length).toBe(2);
expect(sodium.to_string(await bob.decrypt(bob_store, ping_bob_2))).toBe('Ping2!');
expect(bob.session_states[bob.session_tag.toString()].state.recv_chains.length).toBe(2);
const pong_alice = await bob.encrypt('Pong!');
expect(sodium.to_string(await alice.decrypt(alice_store, pong_alice))).toBe('Pong!');
expect(alice.session_states[alice.session_tag.toString()].state.recv_chains.length).toBe(3);
expect(alice.session_states[alice.session_tag.toString()].state.prev_counter).toBe(2);
const delay_decrypted = await bob.decrypt(bob_store, hello_bob_delayed);
expect(sodium.to_string(delay_decrypted)).toBe('Hello delay!');
expect(sodium.to_string(await bob.decrypt(bob_store, ping_bob_1))).toBe('Ping1!');
expect(bob.session_states[bob.session_tag.toString()].state.recv_chains.length).toBe(2);
expect(sodium.to_string(await bob.decrypt(bob_store, ping_bob_2))).toBe('Ping2!');
expect(bob.session_states[bob.session_tag.toString()].state.recv_chains.length).toBe(2);
const pong_alice = await bob.encrypt('Pong!');
expect(sodium.to_string(await alice.decrypt(alice_store, pong_alice))).toBe('Pong!');
expect(alice.session_states[alice.session_tag.toString()].state.recv_chains.length).toBe(3);
expect(alice.session_states[alice.session_tag.toString()].state.prev_counter).toBe(2);
const delay_decrypted = await bob.decrypt(bob_store, hello_bob_delayed);
expect(sodium.to_string(delay_decrypted)).toBe('Hello delay!');
expect(bob.session_states[bob.session_tag.toString()].state.recv_chains.length).toBe(2);
expect(bob.session_states[bob.session_tag.toString()].state.prev_counter).toBe(1);
assert_serialise_deserialise(alice_ident, alice);
assert_serialise_deserialise(bob_ident, bob);
});
const echo_bob2_decrypted = await bob.decrypt(bob_store, echo_bob2_encrypted);
expect(sodium.to_string(echo_bob2_decrypted)).toBe(echo_bob2_plaintext);
expect(Object.keys(bob.session_states).length).toBe(2);
const echo_alice2_decrypted = await alice.decrypt(alice_store, echo_alice2_encrypted);
expect(sodium.to_string(echo_alice2_decrypted)).toBe(echo_alice2_plaintext);
expect(Object.keys(alice.session_states).length).toBe(2);
expect(alice.session_tag.toString()).not.toEqual(bob.session_tag.toString());
const stop_it_plaintext = 'Stop it!';
const stop_it_encrypted = await alice.encrypt(stop_it_plaintext);
const stop_it_decrypted = await bob.decrypt(bob_store, stop_it_encrypted);
expect(sodium.to_string(stop_it_decrypted)).toBe(stop_it_plaintext);
expect(Object.keys(bob.session_states).length).toBe(2);
const ok_plaintext = 'OK';
const ok_encrypted = await bob.encrypt(ok_plaintext);
const ok_decrypted = await alice.decrypt(alice_store, ok_encrypted);
expect(sodium.to_string(ok_decrypted)).toBe(ok_plaintext);
expect(Object.keys(alice.session_states).length).toBe(2);
expect(alice.session_tag.toString()).toEqual(bob.session_tag.toString());
assert_serialise_deserialise(alice_ident, alice);
assert_serialise_deserialise(bob_ident, bob);
});
const echo_bob1_decrypted = await bob.decrypt(bob_store, echo_bob1_encrypted);
expect(sodium.to_string(echo_bob1_decrypted)).toBe(echo_bob1_plaintext);
expect(Object.keys(bob.session_states).length).toBe(2);
const echo_alice1_decrypted = await alice.decrypt(alice_store, echo_alice1_encrypted);
expect(sodium.to_string(echo_alice1_decrypted)).toBe(echo_alice1_plaintext);
expect(Object.keys(alice.session_states).length).toBe(2);
const echo_bob2_plaintext = 'Echo Bob2!';
const echo_bob2_encrypted = await alice.encrypt(echo_bob2_plaintext);
const echo_alice2_plaintext = 'Echo Alice2!';
const echo_alice2_encrypted = await bob.encrypt(echo_alice2_plaintext);
const echo_bob2_decrypted = await bob.decrypt(bob_store, echo_bob2_encrypted);
expect(sodium.to_string(echo_bob2_decrypted)).toBe(echo_bob2_plaintext);
expect(Object.keys(bob.session_states).length).toBe(2);
const echo_alice2_decrypted = await alice.decrypt(alice_store, echo_alice2_encrypted);
expect(sodium.to_string(echo_alice2_decrypted)).toBe(echo_alice2_plaintext);
expect(Object.keys(alice.session_states).length).toBe(2);
expect(alice.session_tag.toString()).not.toEqual(bob.session_tag.toString());
const stop_it_plaintext = 'Stop it!';
const stop_it_encrypted = await alice.encrypt(stop_it_plaintext);
const stop_it_decrypted = await bob.decrypt(bob_store, stop_it_encrypted);
expect(sodium.to_string(stop_it_decrypted)).toBe(stop_it_plaintext);
expect(Object.keys(bob.session_states).length).toBe(2);
const ok_plaintext = 'OK';
const alice = await Proteus.keys.IdentityKeyPair.new();
const bob = await Proteus.keys.IdentityKeyPair.new();
const preKey = await bobStore.load_prekey(0);
const bobPreKeyBundle = Proteus.keys.PreKeyBundle.new(bob.public_key, preKey);
const aliceToBob = await Proteus.session.Session.init_from_prekey(alice, bobPreKeyBundle);
const plaintext = 'Hello Bob!';
const preKeyMessage = await aliceToBob.encrypt(plaintext);
const envelope = Proteus.message.Envelope.deserialise(preKeyMessage.serialise());
const [bobToAlice, decrypted] = await Proteus.session.Session.init_from_message(bob, bobStore, envelope);
expect(sodium.to_string(decrypted)).toBe(plaintext);
expect(bobToAlice).toBeDefined();
});
});
expect(Object.keys(alice.session_states).length).toBe(2);
expect(alice.session_tag.toString()).not.toEqual(bob.session_tag.toString());
const stop_it_plaintext = 'Stop it!';
const stop_it_encrypted = await alice.encrypt(stop_it_plaintext);
const stop_it_decrypted = await bob.decrypt(bob_store, stop_it_encrypted);
expect(sodium.to_string(stop_it_decrypted)).toBe(stop_it_plaintext);
expect(Object.keys(bob.session_states).length).toBe(2);
const ok_plaintext = 'OK';
const ok_encrypted = await bob.encrypt(ok_plaintext);
const ok_decrypted = await alice.decrypt(alice_store, ok_encrypted);
expect(sodium.to_string(ok_decrypted)).toBe(ok_plaintext);
expect(Object.keys(alice.session_states).length).toBe(2);
expect(alice.session_tag.toString()).toEqual(bob.session_tag.toString());
assert_serialise_deserialise(alice_ident, alice);
assert_serialise_deserialise(bob_ident, bob);
});