Esempio n. 1
0
        protected override Message ReceiveOfflineMessage(OfflineMessage message)
        {
            if (message is AmplifiePrivacy)
            {
                var privacyLevel = (message as AmplifiePrivacy).PrivacyLevel;

                DestilationBuffer.Permute(CommonKeyStore.ServiceKeyBuffer.GetKey().GetBytes());
                DestilationBuffer.ShiftedXOr(CommonKeyStore.ServiceKeyBuffer.GetKey().GetBytes());
                DestilationBuffer.Slice(1 - privacyLevel);
            }
            if (message is MoreUndestiledBitsArrived)
            {
                var moreUndestiledBits = message as MoreUndestiledBitsArrived;
                DestilationBuffer.AddBytes(moreUndestiledBits.Bytes);
            }
            if (message is MoreBitsArrived)
            {
                var moreBitsArrivedMessage = message as MoreBitsArrived;
                foreach (var key in moreBitsArrivedMessage.Keys)
                {
                    LocalKeyStore.AddKey(key);
                }
                return(new NOP());
            }
            if (message is GetOutBufferKey)
            {
                var getOutBufferKeyMessage = message as GetOutBufferKey;
                ApplicationKeyBuffer correspondingKeyStore;
                CommonKeyStore.ApplicationKeyBuffers.TryGetValue(getOutBufferKeyMessage.Handle, out correspondingKeyStore);
                if (correspondingKeyStore != null)
                {
                    if (correspondingKeyStore.GetLength() >= getOutBufferKeyMessage.Count)
                    {
                        return(new OutKey(correspondingKeyStore.GetKey(getOutBufferKeyMessage.Count)));
                    }
                    else
                    {
                        return(new NOP());
                    }
                }
                else
                {
                    return(new NOP());
                }
            }
            if (message is OutKey)
            {
                Console.WriteLine((message as OutKey).Key.ToString());
                return(new NOP());
            }
            if (message is CreateKeyBuffer)
            {
                var createKeyBufferMessage = message as CreateKeyBuffer;
                CommonKeyStore.AddKeyBuffer(createKeyBufferMessage.Handle, createKeyBufferMessage.QualityOfService);
                return(new NOP());
            }
            if (message is CloseKeyBuffer)
            {
                var closeKeyBuffer = message as CloseKeyBuffer;
                CommonKeyStore.RemoveKeyBuffer(closeKeyBuffer.Handle);
                return(new NOP());
            }
            return(new NOP());
        }
Esempio n. 2
0
        protected override Message ReceiveOfflineMessage(OfflineMessage message)
        {
            if (message is AmplifiePrivacy)
            {
                var privacyLevel = (message as AmplifiePrivacy).PrivacyLevel;

                DestilationBuffer.Permute(CommonKeyStore.ServiceKeyBuffer.GetKey().GetBytes());
                DestilationBuffer.ShiftedXOr(CommonKeyStore.ServiceKeyBuffer.GetKey().GetBytes());
                DestilationBuffer.Slice(1 - privacyLevel);
            }
            if (message is InitiateCascade)
            {
                var initMesssage           = message as InitiateCascade;
                var alpha                  = 0.6;
                var initialBlockSize       = (int)(alpha / DestilationBuffer.GetEstimatedQBER());
                var cascade                = new Cascade(this, initMesssage.Service, initMesssage.Bus);
                var properInitialBlockSize = initialBlockSize;
                int rem;
                int div           = Math.DivRem(DestilationBuffer.GetSize(), properInitialBlockSize, out rem);
                var initialBlocks = new List <Block>();
                for (int i = 0; i < div; i++)
                {
                    initialBlocks.Add(new Block()
                    {
                        Index = i * properInitialBlockSize, Length = properInitialBlockSize
                    });
                }
                if (rem != 0)
                {
                    initialBlocks.Add(new Block()
                    {
                        Index = properInitialBlockSize * div, Length = rem
                    });
                }
                cascade.CorrectErrors(initialBlocks);
            }
            if (message is MoreUndestiledBitsArrived)
            {
                var moreUndestiledBits = message as MoreUndestiledBitsArrived;
                DestilationBuffer.AddBytes(moreUndestiledBits.Bytes);
            }
            if (message is InitiateDestilation)
            {
                if (!DestilationBuffer.IsEmpty())
                {
                    return(new NOP());
                }
                throw new DestilationBufferIsEmptyExeption();
            }
            if (message is EstimateQBER)
            {
                var estimateQBERMessage = message as EstimateQBER;
                var payload             = new BlockIdentifiaction(estimateQBERMessage.Index, estimateQBERMessage.Length);
                var mac = MessageAuthenticator.GetMAC(payload.GetBytes());
                return(new RequestBits(mac, payload, Demand.Estimation));
            }
            if (message is MoreBitsArrived)
            {
                var moreBitArrivedMessage = message as MoreBitsArrived;
                foreach (var key in moreBitArrivedMessage.Keys)
                {
                    LocalKeyStore.AddKey(key);
                }
                return(new NOP());
            }
            if (message is Initiate)
            {
                if (!LocalKeyStore.IsEmpty())
                {
                    if (DoesLasReceivedAckCorrespondsToLastSentKeyIndex())
                    {
                        _lastSendKeyIndex += 1;
                        return(PrepareMessageForNewKeyChunk(_lastSendKeyIndex));
                    }
                    else
                    {
                        return(PrepareMessageForNewKeyChunk(_lastSendKeyIndex));
                    }
                }
            }
            if (message is GetOutBufferKey)
            {
                var getOutBufferKeyMessage = message as GetOutBufferKey;
                ApplicationKeyBuffer keyStore;
                CommonKeyStore.ApplicationKeyBuffers.TryGetValue(getOutBufferKeyMessage.Handle, out keyStore);
                if (keyStore != null)
                {
                    if (keyStore.GetLength() >= getOutBufferKeyMessage.Count)
                    {
                        return(new OutKey(keyStore.GetKey(getOutBufferKeyMessage.Count)));
                    }
                    else
                    {
                        return(new NOP());
                    }
                }
                else
                {
                    return(new NOP());
                }
            }
            if (message is OutKey)
            {
                Console.WriteLine((message as OutKey).Key.ToString());
                return(new NOP());
            }
            if (message is CreateKeyBuffer)
            {
                var createKeyBufferMessage = message as CreateKeyBuffer;
                CommonKeyStore.AddKeyBuffer(createKeyBufferMessage.Handle, createKeyBufferMessage.QualityOfService);
                return(new NOP());
            }
            if (message is CloseKeyBuffer)
            {
                var closeKeyBuffer = message as CloseKeyBuffer;
                CommonKeyStore.RemoveKeyBuffer(closeKeyBuffer.Handle);
                return(new NOP());
            }
            return(new NOP());
        }