public void SetReceiverChainKey(IEcPublicKey senderEphemeral, ChainKey chainKey)
        {
            Pair <SessionStructure.Types.Chain, uint> chainAndIndex = GetReceiverChain(senderEphemeral);

            SessionStructure.Types.Chain chain = chainAndIndex.First();

            SessionStructure.Types.Chain.Types.ChainKey chainKeyStructure = new SessionStructure.Types.Chain.Types.ChainKey
            {
                Key   = ByteString.CopyFrom(chainKey.GetKey()),
                Index = chainKey.GetIndex()
            };

            chain.ChainKey = chainKeyStructure;

            _sessionStructure.ReceiverChains[(int)chainAndIndex.Second()] = chain;
        }
        public void SetSenderChain(EcKeyPair senderRatchetKeyPair, ChainKey chainKey)
        {
            SessionStructure.Types.Chain.Types.ChainKey chainKeyStructure = new SessionStructure.Types.Chain.Types.ChainKey
            {
                Key   = ByteString.CopyFrom(chainKey.GetKey()),
                Index = chainKey.GetIndex()
            };

            SessionStructure.Types.Chain senderChain = new SessionStructure.Types.Chain
            {
                SenderRatchetKey        = ByteString.CopyFrom(senderRatchetKeyPair.GetPublicKey().Serialize()),
                SenderRatchetKeyPrivate = ByteString.CopyFrom(senderRatchetKeyPair.GetPrivateKey().Serialize()),
                ChainKey = chainKeyStructure
            };

            _sessionStructure.SenderChain = senderChain;
        }
        public ChainKey GetReceiverChainKey(IEcPublicKey senderEphemeral)
        {
            Pair <SessionStructure.Types.Chain, uint> receiverChainAndIndex = GetReceiverChain(senderEphemeral);

            SessionStructure.Types.Chain receiverChain = receiverChainAndIndex.First();

            if (receiverChain == null)
            {
                return(null);
            }
            else
            {
                return(new ChainKey(Hkdf.CreateFor(GetSessionVersion()),
                                    receiverChain.ChainKey.Key.ToByteArray(),
                                    receiverChain.ChainKey.Index));
            }
        }
        public void AddReceiverChain(IEcPublicKey senderRatchetKey, ChainKey chainKey)
        {
            SessionStructure.Types.Chain.Types.ChainKey chainKeyStructure = new SessionStructure.Types.Chain.Types.ChainKey
            {
                Key   = ByteString.CopyFrom(chainKey.GetKey()),
                Index = chainKey.GetIndex()
            };

            SessionStructure.Types.Chain chain = new SessionStructure.Types.Chain
            {
                ChainKey         = chainKeyStructure,
                SenderRatchetKey = ByteString.CopyFrom(senderRatchetKey.Serialize())
            };
            _sessionStructure.ReceiverChains.Add(chain);

            while (_sessionStructure.ReceiverChains.Count > 5)
            {
                _sessionStructure.ReceiverChains.RemoveAt(0); //TODO why was here a TODO?
            }
        }
        public void SetMessageKeys(IEcPublicKey senderEphemeral, MessageKeys messageKeys)
        {
            Pair <SessionStructure.Types.Chain, uint> chainAndIndex = GetReceiverChain(senderEphemeral);

            SessionStructure.Types.Chain chain = chainAndIndex.First();
            SessionStructure.Types.Chain.Types.MessageKey messageKeyStructure = new SessionStructure.Types.Chain.Types.MessageKey
            {
                CipherKey = ByteString.CopyFrom(messageKeys.GetCipherKey()),
                MacKey    = ByteString.CopyFrom(messageKeys.GetMacKey()),
                Index     = messageKeys.GetCounter(),
                Iv        = ByteString.CopyFrom(messageKeys.GetIv())
            };

            chain.MessageKeys.Add(messageKeyStructure);
            if (chain.MessageKeys.Count > MaxMessageKeys)
            {
                chain.MessageKeys.RemoveAt(0);
            }

            _sessionStructure.ReceiverChains[(int)chainAndIndex.Second()] = chain;
        }
        public bool HasMessageKeys(IEcPublicKey senderEphemeral, uint counter)
        {
            Pair <SessionStructure.Types.Chain, uint> chainAndIndex = GetReceiverChain(senderEphemeral);

            SessionStructure.Types.Chain chain = chainAndIndex.First();

            if (chain == null)
            {
                return(false);
            }

            IList <SessionStructure.Types.Chain.Types.MessageKey> messageKeyList = chain.MessageKeys;

            foreach (SessionStructure.Types.Chain.Types.MessageKey messageKey in messageKeyList)
            {
                if (messageKey.Index == counter)
                {
                    return(true);
                }
            }

            return(false);
        }
        public MessageKeys RemoveMessageKeys(IEcPublicKey senderEphemeral, uint counter)
        {
            Pair <SessionStructure.Types.Chain, uint> chainAndIndex = GetReceiverChain(senderEphemeral);

            SessionStructure.Types.Chain chain = chainAndIndex.First();

            if (chain == null)
            {
                return(null);
            }

            List <SessionStructure.Types.Chain.Types.MessageKey>        messageKeyList     = new List <SessionStructure.Types.Chain.Types.MessageKey>(chain.MessageKeys);
            IEnumerator <SessionStructure.Types.Chain.Types.MessageKey> messageKeyIterator = messageKeyList.GetEnumerator();
            MessageKeys result = null;

            while (messageKeyIterator.MoveNext())                                                      //hasNext()
            {
                SessionStructure.Types.Chain.Types.MessageKey messageKey = messageKeyIterator.Current; // next()

                if (messageKey.Index == counter)
                {
                    result = new MessageKeys(messageKey.CipherKey.ToByteArray(),
                                             messageKey.MacKey.ToByteArray(),
                                             messageKey.Iv.ToByteArray(),
                                             messageKey.Index);

                    messageKeyList.Remove(messageKey);                     //messageKeyIterator.remove();
                    break;
                }
            }

            chain.MessageKeys.Clear();
            chain.MessageKeys.AddRange(messageKeyList);

            _sessionStructure.ReceiverChains[(int)chainAndIndex.Second()] = chain;
            return(result);
        }