Exemplo n.º 1
0
        public void TestTooFarInFuture()
        {
            InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore();
            InMemorySenderKeyStore bobStore   = new InMemorySenderKeyStore();

            GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore);
            GroupSessionBuilder bobSessionBuilder   = new GroupSessionBuilder(bobStore);

            SenderKeyName aliceName = GroupSender;

            GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, aliceName);
            GroupCipher bobGroupCipher   = new GroupCipher(bobStore, aliceName);

            SenderKeyDistributionMessage aliceDistributionMessage = aliceSessionBuilder.Create(aliceName);

            bobSessionBuilder.Process(aliceName, aliceDistributionMessage);

            for (int i = 0; i < 2001; i++)
            {
                aliceGroupCipher.Encrypt(Encoding.UTF8.GetBytes("up the punks"));
            }

            byte[] tooFarCiphertext = aliceGroupCipher.Encrypt(Encoding.UTF8.GetBytes("notta gonna worka"));
            try
            {
                bobGroupCipher.Decrypt(tooFarCiphertext);
                throw new Exception("Should have failed!");
            }
            catch (InvalidMessageException)
            {
                // good
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="senderKeyName"></param>
        /// <returns></returns>
        public SenderKeyRecord LoadSenderKey(SenderKeyName senderKeyName)
        {
            SenderKeysObject prekey;

            SenderKeysObjectsDic.TryGetValue(senderKeyName, out prekey);
            return(prekey != null ? new SenderKeyRecord(prekey.Record) : new SenderKeyRecord());
        }
Exemplo n.º 3
0
 public void StoreSenderKey(SenderKeyName senderKeyName, SenderKeyRecord record)
 {
     if (this.OnStoreSenderKey != null)
     {
         this.OnStoreSenderKey(senderKeyName, record);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="senderKeyName"></param>
        /// <returns></returns>
        public bool ContainsSenderKey(SenderKeyName senderKeyName)
        {
            SenderKeysObject prekey;

            SenderKeysObjectsDic.TryGetValue(senderKeyName, out prekey);
            return(prekey != null);
        }
        public void testLateJoin()
        {
            InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore();
            InMemorySenderKeyStore bobStore = new InMemorySenderKeyStore();

            GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore);


            SenderKeyName aliceName = GROUP_SENDER;

            GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, aliceName);


            SenderKeyDistributionMessage aliceDistributionMessage = aliceSessionBuilder.create(aliceName);
            // Send off to some people.

            for (int i = 0; i < 100; i++)
            {
                aliceGroupCipher.encrypt(Encoding.UTF8.GetBytes("up the punks up the punks up the punks"));
            }

            // Now Bob Joins.
            GroupSessionBuilder bobSessionBuilder = new GroupSessionBuilder(bobStore);
            GroupCipher bobGroupCipher = new GroupCipher(bobStore, aliceName);


            SenderKeyDistributionMessage distributionMessageToBob = aliceSessionBuilder.create(aliceName);
            bobSessionBuilder.process(aliceName, new SenderKeyDistributionMessage(distributionMessageToBob.serialize()));

            byte[] ciphertext = aliceGroupCipher.encrypt(Encoding.UTF8.GetBytes("welcome to the group"));
            byte[] plaintext = bobGroupCipher.decrypt(ciphertext);

            Assert.AreEqual("welcome to the group", Encoding.UTF8.GetString(plaintext));
        }
Exemplo n.º 6
0
        public bool ContainsSenderKey(SenderKeyName senderKeyName)
        {
            String senderKeyId = senderKeyName.GetSender().GetName() + " : " + senderKeyName.GetGroupId();
            SenderKeysRepository senderKeysRepository = new SenderKeysRepository();

            return(senderKeysRepository.Contains(senderKeyId));
        }
Exemplo n.º 7
0
 public void RemoveSenderKey(SenderKeyName senderKeyName)
 {
     if (this.OnRemoveSenderKey != null)
     {
         this.OnRemoveSenderKey(senderKeyName);
     }
 }
Exemplo n.º 8
0
        public void TestOutOfOrder()
        {
            InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore();
            InMemorySenderKeyStore bobStore   = new InMemorySenderKeyStore();

            GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore);
            GroupSessionBuilder bobSessionBuilder   = new GroupSessionBuilder(bobStore);

            SenderKeyName aliceName = GroupSender;

            GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, aliceName);
            GroupCipher bobGroupCipher   = new GroupCipher(bobStore, aliceName);

            SenderKeyDistributionMessage aliceDistributionMessage =
                aliceSessionBuilder.Create(aliceName);

            bobSessionBuilder.Process(aliceName, aliceDistributionMessage);

            List <byte[]> ciphertexts = new List <byte[]>(100);

            for (int i = 0; i < 100; i++)
            {
                ciphertexts.Add(aliceGroupCipher.Encrypt(Encoding.UTF8.GetBytes("up the punks")));
            }

            while (ciphertexts.Count > 0)
            {
                int    index      = (int)(RandomUInt() % ciphertexts.Count);
                byte[] ciphertext = ciphertexts[index];
                ciphertexts.RemoveAt(index);
                byte[] plaintext = bobGroupCipher.Decrypt(ciphertext);

                Assert.AreEqual("up the punks", Encoding.UTF8.GetString(plaintext));
            }
        }
Exemplo n.º 9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="senderKeyName"></param>
 public void RemoveSenderKey(SenderKeyName senderKeyName)
 {
     if (SenderKeysObjectsDic.ContainsKey(senderKeyName))
     {
         SenderKeysObjectsDic.Remove(senderKeyName);
     }
 }
Exemplo n.º 10
0
 public SenderKeyRecord LoadSenderKey(SenderKeyName senderKeyName)
 {
     if (this.OnLoadSenderKey != null)
     {
         return(this.OnLoadSenderKey(senderKeyName));
     }
     return(null);
 }
Exemplo n.º 11
0
        public bool ContainsSenderKey(SenderKeyName senderKeyName)
        {
            if (this.OnContainsSenderKey != null)
            {
                return(this.OnContainsSenderKey(senderKeyName));
            }

            return(false);
        }
Exemplo n.º 12
0
        public void RemoveSenderKey(SenderKeyName senderKeyName)
        {
            String senderKeyId = senderKeyName.GetSender().GetName() + " : " + senderKeyName.GetGroupId();
            SenderKeysRepository senderKeysRepository = new SenderKeysRepository();
            List <SenderKeys>    senderKeys           = senderKeysRepository.GetSenderKeys(senderKeyId);

            if (senderKeys != null && senderKeys.Count > 0)
            {
                SenderKeys senderKey = senderKeys.First();
                senderKeysRepository.Delete(senderKey);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="senderKeyName"></param>
        /// <param name="record"></param>
        public void StoreSenderKey(SenderKeyName senderKeyName, SenderKeyRecord record)
        {
            if (SenderKeysObjectsDic.ContainsKey(senderKeyName))
            {
                SenderKeysObjectsDic.Remove(senderKeyName);
            }

            SenderKeysObjectsDic.Add(senderKeyName, new SenderKeysObject()
            {
                SenderKeyKeyName = senderKeyName,
                Record           = record.Serialize()
            });
        }
Exemplo n.º 14
0
        public SenderKeyRecord LoadSenderKey(SenderKeyName senderKeyName)
        {
            String senderKeyId = senderKeyName.GetSender().GetName() + " : " + senderKeyName.GetGroupId();
            SenderKeysRepository senderKeysRepository = new SenderKeysRepository();
            List <SenderKeys>    senderKeys           = senderKeysRepository.GetSenderKeys(senderKeyId);

            if (senderKeys != null && senderKeys.Count > 0)
            {
                SenderKeys      senderKey       = senderKeys.First();
                SenderKeyRecord senderKeyRecord = new SenderKeyRecord(senderKey.Record);
                return(senderKeyRecord);
            }

            return(new SenderKeyRecord());
        }
Exemplo n.º 15
0
        public void StoreSenderKey(SenderKeyName senderKeyName, SenderKeyRecord record)
        {
            if (ContainsSenderKey(senderKeyName))
            {
                RemoveSenderKey(senderKeyName);
            }

            String senderKeyId = senderKeyName.GetSender().GetName() + " : " + senderKeyName.GetGroupId();
            SenderKeysRepository senderKeysRepository = new SenderKeysRepository();

            SenderKeys senderKey = new SenderKeys()
            {
                Record      = record.Serialize(),
                SenderKeyId = senderKeyId
            };

            senderKeysRepository.Save(senderKey);
        }
Exemplo n.º 16
0
        public void TestBasicRatchet()
        {
            InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore();
            InMemorySenderKeyStore bobStore   = new InMemorySenderKeyStore();

            GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore);
            GroupSessionBuilder bobSessionBuilder   = new GroupSessionBuilder(bobStore);

            SenderKeyName aliceName = GroupSender;

            GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, aliceName);
            GroupCipher bobGroupCipher   = new GroupCipher(bobStore, aliceName);

            SenderKeyDistributionMessage sentAliceDistributionMessage =
                aliceSessionBuilder.Create(aliceName);
            SenderKeyDistributionMessage receivedAliceDistributionMessage =
                new SenderKeyDistributionMessage(sentAliceDistributionMessage.Serialize());

            bobSessionBuilder.Process(aliceName, receivedAliceDistributionMessage);

            byte[] ciphertextFromAlice  = aliceGroupCipher.Encrypt(Encoding.UTF8.GetBytes("smert ze smert"));
            byte[] ciphertextFromAlice2 = aliceGroupCipher.Encrypt(Encoding.UTF8.GetBytes("smert ze smert2"));
            byte[] ciphertextFromAlice3 = aliceGroupCipher.Encrypt(Encoding.UTF8.GetBytes("smert ze smert3"));

            byte[] plaintextFromAlice = bobGroupCipher.Decrypt(ciphertextFromAlice);

            try
            {
                bobGroupCipher.Decrypt(ciphertextFromAlice);
                throw new Exception("Should have ratcheted forward!");
            }
            catch (DuplicateMessageException)
            {
                // good
            }

            byte[] plaintextFromAlice2 = bobGroupCipher.Decrypt(ciphertextFromAlice2);
            byte[] plaintextFromAlice3 = bobGroupCipher.Decrypt(ciphertextFromAlice3);

            Assert.AreEqual("smert ze smert", Encoding.UTF8.GetString(plaintextFromAlice));
            Assert.AreEqual("smert ze smert2", Encoding.UTF8.GetString(plaintextFromAlice2));
            Assert.AreEqual("smert ze smert3", Encoding.UTF8.GetString(plaintextFromAlice3));
        }
        public SenderKeyRecord loadSenderKey(SenderKeyName senderKeyName)
        {
            try
            {
                SenderKeyRecord record;
                store.TryGetValue(senderKeyName, out record);

                if (record == null)
                {
                    return(new SenderKeyRecord());
                }
                else
                {
                    return(new SenderKeyRecord(record.serialize()));
                }
            }
            catch (IOException e)
            {
                throw new Exception(e.Message);
            }
        }
Exemplo n.º 18
0
        public void TestMessageKeyLimit()
        {
            InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore();
            InMemorySenderKeyStore bobStore   = new InMemorySenderKeyStore();

            GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore);
            GroupSessionBuilder bobSessionBuilder   = new GroupSessionBuilder(bobStore);

            SenderKeyName aliceName = GroupSender;

            GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, aliceName);
            GroupCipher bobGroupCipher   = new GroupCipher(bobStore, aliceName);

            SenderKeyDistributionMessage aliceDistributionMessage = aliceSessionBuilder.Create(aliceName);

            bobSessionBuilder.Process(aliceName, aliceDistributionMessage);

            List <byte[]> inflight = new List <byte[]>();

            for (int i = 0; i < 2010; i++)
            {
                inflight.Add(aliceGroupCipher.Encrypt(Encoding.UTF8.GetBytes("up the punks")));
            }

            bobGroupCipher.Decrypt(inflight[1000]);
            bobGroupCipher.Decrypt(inflight[inflight.Count - 1]);

            try
            {
                bobGroupCipher.Decrypt(inflight[0]);
                throw new Exception("Should have failed!");
            }
            catch (DuplicateMessageException)
            {
                // good
            }
        }
 public void storeSenderKey(SenderKeyName senderKeyName, SenderKeyRecord record)
 {
     store[senderKeyName] = record;
 }