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()); }
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()); }