public function test_basicKeyExchange() { $aliceStore = new InMemoryAxolotlStore(); $aliceSessionBuilder = new SessionBuilder($aliceStore, $aliceStore, $aliceStore, $aliceStore, self::BOB_RECIPIENT_ID, 1); $bobStore = new InMemoryAxolotlStore(); $bobSessionBuilder = new SessionBuilder($bobStore, $bobStore, $bobStore, $bobStore, self::ALICE_RECIPIENT_ID, 1); $aliceKeyExchangeMessage = $aliceSessionBuilder->processInitKeyExchangeMessage(); $this->assertTrue($aliceKeyExchangeMessage != null); $aliceKeyExchangeMessageBytes = $aliceKeyExchangeMessage->serialize(); $bobKeyExchangeMessage = $bobSessionBuilder->processKeyExchangeMessage(new KeyExchangeMessage(null, null, null, null, null, null, null, $aliceKeyExchangeMessageBytes)); $this->assertTrue($bobKeyExchangeMessage != null); define("TEST", true); $bobKeyExchangeMessageBytes = $bobKeyExchangeMessage->serialize(); $response = $aliceSessionBuilder->processKeyExchangeMessage(new KeyExchangeMessage(null, null, null, null, null, null, null, $bobKeyExchangeMessageBytes)); $this->assertTrue($response == null); $this->assertTrue($aliceStore->containsSession(self::BOB_RECIPIENT_ID, 1)); $this->assertTrue($bobStore->containsSession(self::ALICE_RECIPIENT_ID, 1)); $this->runInteraction($aliceStore, $bobStore); $aliceStore = new InMemoryAxolotlStore(); $aliceSessionBuilder = new SessionBuilder($aliceStore, $aliceStore, $aliceStore, $aliceStore, self::BOB_RECIPIENT_ID, 1); $aliceKeyExchangeMessage = $aliceSessionBuilder->processInitKeyExchangeMessage(); try { $bobKeyExchangeMessage = $bobSessionBuilder->processKeyExchangeMessage($aliceKeyExchangeMessage); throw new AssertionError("This identity shouldn't be trusted!"); } catch (UntrustedIdentityException $ex) { $bobStore->saveIdentity(self::ALICE_RECIPIENT_ID, $aliceKeyExchangeMessage->getIdentityKey()); } $bobKeyExchangeMessage = $bobSessionBuilder->processKeyExchangeMessage($aliceKeyExchangeMessage); $this->assertTrue($aliceSessionBuilder->processKeyExchangeMessage($bobKeyExchangeMessage) == null); self . runInteraction($aliceStore, $bobStore); }
protected function runInteraction($aliceSessionRecord, $bobSessionRecord) { $aliceStore = new InMemoryAxolotlStore(); $bobStore = new InMemoryAxolotlStore(); $aliceStore->storeSession(2, 1, $aliceSessionRecord); $bobStore->storeSession(3, 1, $bobSessionRecord); $aliceCipher = new SessionCipher($aliceStore, $aliceStore, $aliceStore, $aliceStore, 2, 1); $bobCipher = new SessionCipher($bobStore, $bobStore, $bobStore, $bobStore, 3, 1); $alicePlaintext = 'This is a plaintext message.'; $message = $aliceCipher->encrypt($alicePlaintext); $bobPlaintext = $bobCipher->decryptMsg(new WhisperMessage(null, null, null, null, null, null, null, null, $message->serialize())); $this->assertEquals($alicePlaintext, $bobPlaintext); $bobReply = 'This is a message from Bob.'; $reply = $bobCipher->encrypt($bobReply); $receivedReply = $aliceCipher->decryptMsg(new WhisperMessage(null, null, null, null, null, null, null, null, $reply->serialize())); $this->assertEquals($bobReply, $receivedReply); $aliceCiphertextMessages = []; $alicePlaintextMessages = []; for ($i = 0; $i < 50; $i++) { $alicePlaintextMessages[] = 'смерть за смерть ' . $i; $aliceCiphertextMessages[] = $aliceCipher->encrypt("смерть за смерть {$i}"); } //shuffle(aliceCiphertextMessages) //shuffle(alicePlaintextMessages) for ($i = 0; $i < count($aliceCiphertextMessages) / 2; $i++) { $receivedPlaintext = $bobCipher->decryptMsg(new WhisperMessage(null, null, null, null, null, null, null, null, $aliceCiphertextMessages[$i]->serialize())); $this->assertEquals($receivedPlaintext, $alicePlaintextMessages[$i]); } }