Esempio n. 1
0
 protected override Message ReceiveAuthenticatedMessage(AuthenticatedMessage message)
 {
     if (message is BlocksParityRequest)
     {
         var blockParityRequestMessage = message as BlocksParityRequest;
         var blocksPayload             = message.Payload as Blocks;
         for (int i = 0; i < blocksPayload.BlocksArray.Length; i++)
         {
             var block  = blocksPayload.BlocksArray[i];
             var parity = Cascade.Parity(DestilationBuffer.GetBits(block.Index, block.Length));
             blocksPayload.BlocksArray[i].Parity = parity;
         }
         var mac = MessageAuthenticator.GetMAC(blocksPayload.GetBytes());
         return(new BlocksParityResponse(mac, blocksPayload));
     }
     if (message is CheckParity)
     {
         var checkParityMessage  = message as CheckParity;
         var blockIdentification = checkParityMessage.Payload as BlockIdentifiaction;
         var parity  = Cascade.Parity(DestilationBuffer.GetBits(blockIdentification.Index, blockIdentification.Length));
         var payload = new ParityPayload(blockIdentification.Index, blockIdentification.Length, parity);
         var mac     = MessageAuthenticator.GetMAC(payload.GetBytes());
         return(new Parity(mac, payload));
     }
     if (message is RequestBits)
     {
         var requestBitsMessage  = message as RequestBits;
         var blockIdentification = requestBitsMessage.Payload as BlockIdentifiaction;
         var bytes   = DestilationBuffer.GetBytes(blockIdentification.Index, blockIdentification.Length);
         var payload = new BitsAsBytes(blockIdentification.Index, blockIdentification.Length, bytes);
         var mac     = MessageAuthenticator.GetMAC(payload.GetBytes());
         return(new Bytes(mac, payload, requestBitsMessage.Demand));
     }
     if (message is AddKey)
     {
         var addKeyMessage = message as AddKey;
         var messagPayload = (addKeyMessage.Payload as BlockIdAndKeyAllocation);
         if (messagPayload.BlockId == _currentKeyChunk + 1)
         {
             var key = LocalKeyStore.GetKey();
             CommonKeyStore.AddNewKey(key, messagPayload.KeyAllocation);
             _currentKeyChunk += 1;
             return(PrepareAckForCommonKeyProposition(_currentKeyChunk));
         }
         else
         {
             if (messagPayload.BlockId == _currentKeyChunk)
             {
                 return(PrepareAckForCommonKeyProposition(_currentKeyChunk));
             }
             else
             {
                 return(new NOP());
             }
         }
     }
     return(new NOP());
 }
Esempio n. 2
0
        protected override Message ReceiveAuthenticatedMessage(AuthenticatedMessage message)
        {
            if (message is Bytes)
            {
                var bytesMessage       = message as Bytes;
                var bitsAsBytesPayload = bytesMessage.Payload as BitsAsBytes;

                if (bytesMessage.Demand == Demand.Estimation)
                {
                    var bobBits       = new BitArray(bitsAsBytesPayload.GetBytes().ToList().Skip(8).ToArray());
                    var aliceBits     = new BitArray(DestilationBuffer.GetBytes(bitsAsBytesPayload.Index, bitsAsBytesPayload.Length));
                    var estimatedQBER = Cascade.EstimateQBER(aliceBits, bobBits);
                    DestilationBuffer.SetEstimatedQBER(estimatedQBER);
                }
                if (bytesMessage.Demand == Demand.Confirmation)
                {
                }
            }
            if (message is AddingKeyAck)
            {
                var addingKeyAckMessage = message as AddingKeyAck;

                if (DoesAckCorrespondsToLastSentKeyIndex(addingKeyAckMessage.Payload as BlockIdAndKeyAllocation))
                {
                    _lastReceivedKeyIndexAck = _lastSendKeyIndex;

                    var key = LocalKeyStore.GetKey();
                    CommonKeyStore.AddNewKey(key, (addingKeyAckMessage.Payload as BlockIdAndKeyAllocation).KeyAllocation);

                    if (!LocalKeyStore.IsEmpty())
                    {
                        _lastSendKeyIndex += 1;
                        return(PrepareMessageForNewKeyChunk(_lastSendKeyIndex));
                    }
                    else
                    {
                        return(new NOP());
                    }
                }
                else
                {
                    throw new UnexpectedMessageException();
                }
            }
            return(new NOP());
        }