private PreKeyBundle CreateBobPreKeyBundle(ISignalProtocolStore bobStore)
        {
            EcKeyPair bobUnsignedPreKey   = Curve.GenerateKeyPair();
            int       bobUnsignedPreKeyId = new Random().Next((int)Medium.MaxValue);

            byte[] bobSignature = Curve.CalculateSignature(bobStore.GetIdentityKeyPair().GetPrivateKey(),
                                                           BobSignedPreKey.GetPublicKey().Serialize());

            PreKeyBundle bobPreKeyBundle = new PreKeyBundle(1, 1,
                                                            (uint)bobUnsignedPreKeyId, bobUnsignedPreKey.GetPublicKey(),
                                                            BobSignedPreKeyId, BobSignedPreKey.GetPublicKey(),
                                                            bobSignature, bobStore.GetIdentityKeyPair().GetPublicKey());

            bobStore.StoreSignedPreKey(BobSignedPreKeyId, new SignedPreKeyRecord(BobSignedPreKeyId, (ulong)DateTime.UtcNow.Ticks, BobSignedPreKey, bobSignature));
            bobStore.StorePreKey((uint)bobUnsignedPreKeyId, new PreKeyRecord((uint)bobUnsignedPreKeyId, bobUnsignedPreKey));

            return(bobPreKeyBundle);
        }
        private PreKeyBundle CreateAlicePreKeyBundle(ISignalProtocolStore aliceStore)
        {
            EcKeyPair aliceUnsignedPreKey   = Curve.GenerateKeyPair();
            int       aliceUnsignedPreKeyId = new Random().Next((int)Medium.MaxValue);

            byte[] aliceSignature = Curve.CalculateSignature(aliceStore.GetIdentityKeyPair().GetPrivateKey(),
                                                             AliceSignedPreKey.GetPublicKey().Serialize());

            PreKeyBundle alicePreKeyBundle = new PreKeyBundle(1, 1,
                                                              (uint)aliceUnsignedPreKeyId, aliceUnsignedPreKey.GetPublicKey(),
                                                              AliceSignedPreKeyId, AliceSignedPreKey.GetPublicKey(),
                                                              aliceSignature, aliceStore.GetIdentityKeyPair().GetPublicKey());

            aliceStore.StoreSignedPreKey(AliceSignedPreKeyId, new SignedPreKeyRecord(AliceSignedPreKeyId, (ulong)DateTime.UtcNow.Ticks, AliceSignedPreKey, aliceSignature));
            aliceStore.StorePreKey((uint)aliceUnsignedPreKeyId, new PreKeyRecord((uint)aliceUnsignedPreKeyId, aliceUnsignedPreKey));

            return(alicePreKeyBundle);
        }
 /// <summary>
 /// Constructs a SessionBuilder
 /// </summary>
 /// <param name="store">The <see "SignalProtocolStore" /> to store all state information in.</param>
 /// <param name="remoteAddress">The address of the remote user to build a session with.</param>
 /// <returns></returns>
 public SessionBuilder(ISignalProtocolStore store, SignalProtocolAddress remoteAddress) : this(store, store, store, store, remoteAddress)
 {
 }
 private bool IsSessionIdEqual(ISignalProtocolStore aliceStore, ISignalProtocolStore bobStore)
 {
     return(ByteUtil.IsEqual(aliceStore.LoadSession(BobAddress).GetSessionState().GetAliceBaseKey(),
                             bobStore.LoadSession(AliceAddress).GetSessionState().GetAliceBaseKey()));
 }
Beispiel #5
0
 public BobDecryptionCallback(ISignalProtocolStore bobStore, String originalMessage)
 {
     _bobStore        = bobStore;
     _originalMessage = originalMessage;
 }
Beispiel #6
0
        private void RunInteraction(ISignalProtocolStore aliceStore, ISignalProtocolStore bobStore)
        {
            SessionCipher aliceSessionCipher = new SessionCipher(aliceStore, BobAddress);
            SessionCipher bobSessionCipher   = new SessionCipher(bobStore, AliceAddress);

            String            originalMessage = "smert ze smert";
            CiphertextMessage aliceMessage    = aliceSessionCipher.Encrypt(Encoding.UTF8.GetBytes(originalMessage));

            Assert.AreEqual(CiphertextMessage.WhisperType, aliceMessage.GetMessageType());

            byte[] plaintext = bobSessionCipher.Decrypt(new SignalMessage(aliceMessage.Serialize()));
            Assert.AreEqual(originalMessage, Encoding.UTF8.GetString(plaintext));

            CiphertextMessage bobMessage = bobSessionCipher.Encrypt(Encoding.UTF8.GetBytes(originalMessage));

            Assert.AreEqual(CiphertextMessage.WhisperType, bobMessage.GetMessageType());

            plaintext = aliceSessionCipher.Decrypt(new SignalMessage(bobMessage.Serialize()));
            Assert.AreEqual(originalMessage, Encoding.UTF8.GetString(plaintext));

            for (int i = 0; i < 10; i++)
            {
                String loopingMessage = ("What do we mean by saying that existence precedes essence? " +
                                         "We mean that man first of all exists, encounters himself, " +
                                         "surges up in the world--and defines himself aftward. " + i);
                CiphertextMessage aliceLoopingMessage = aliceSessionCipher.Encrypt(Encoding.UTF8.GetBytes(loopingMessage));

                byte[] loopingPlaintext = bobSessionCipher.Decrypt(new SignalMessage(aliceLoopingMessage.Serialize()));
                Assert.AreEqual(loopingMessage, Encoding.UTF8.GetString(loopingPlaintext));
            }

            for (int i = 0; i < 10; i++)
            {
                String loopingMessage = ("What do we mean by saying that existence precedes essence? " +
                                         "We mean that man first of all exists, encounters himself, " +
                                         "surges up in the world--and defines himself aftward. " + i);
                CiphertextMessage bobLoopingMessage = bobSessionCipher.Encrypt(Encoding.UTF8.GetBytes(loopingMessage));

                byte[] loopingPlaintext = aliceSessionCipher.Decrypt(new SignalMessage(bobLoopingMessage.Serialize()));
                Assert.AreEqual(loopingMessage, Encoding.UTF8.GetString(loopingPlaintext));
            }

            HashSet <Pair <String, CiphertextMessage> > aliceOutOfOrderMessages = new HashSet <Pair <String, CiphertextMessage> >();

            for (int i = 0; i < 10; i++)
            {
                String loopingMessage = ("What do we mean by saying that existence precedes essence? " +
                                         "We mean that man first of all exists, encounters himself, " +
                                         "surges up in the world--and defines himself aftward. " + i);
                CiphertextMessage aliceLoopingMessage = aliceSessionCipher.Encrypt(Encoding.UTF8.GetBytes(loopingMessage));

                aliceOutOfOrderMessages.Add(new Pair <String, CiphertextMessage>(loopingMessage, aliceLoopingMessage));
            }

            for (int i = 0; i < 10; i++)
            {
                String loopingMessage = ("What do we mean by saying that existence precedes essence? " +
                                         "We mean that man first of all exists, encounters himself, " +
                                         "surges up in the world--and defines himself aftward. " + i);
                CiphertextMessage aliceLoopingMessage = aliceSessionCipher.Encrypt(Encoding.UTF8.GetBytes(loopingMessage));

                byte[] loopingPlaintext = bobSessionCipher.Decrypt(new SignalMessage(aliceLoopingMessage.Serialize()));
                Assert.AreEqual(loopingMessage, Encoding.UTF8.GetString(loopingPlaintext));
            }

            for (int i = 0; i < 10; i++)
            {
                String            loopingMessage    = ("You can only desire based on what you know: " + i);
                CiphertextMessage bobLoopingMessage = bobSessionCipher.Encrypt(Encoding.UTF8.GetBytes(loopingMessage));

                byte[] loopingPlaintext = aliceSessionCipher.Decrypt(new SignalMessage(bobLoopingMessage.Serialize()));
                Assert.AreEqual(loopingMessage, Encoding.UTF8.GetString(loopingPlaintext));
            }

            foreach (Pair <String, CiphertextMessage> aliceOutOfOrderMessage in aliceOutOfOrderMessages)
            {
                byte[] outOfOrderPlaintext = bobSessionCipher.Decrypt(new SignalMessage(aliceOutOfOrderMessage.Second().Serialize()));
                Assert.AreEqual(aliceOutOfOrderMessage.First(), Encoding.UTF8.GetString(outOfOrderPlaintext));
            }
        }