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()); }
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()); }
private static Service GetService(List <Key> commonKeys) { var service_local_keyStore = new ServiceKeyBuffer(512); var destilationBuffer = new DestilationBuffer(); var service_common_service = new ServiceKeyBuffer(2); foreach (var key in commonKeys) { service_common_service.AddKey(key); } var service_common_keyStores = new CommonKeyStoreSystem(service_common_service); var service_authenticator = new WegmanCarterAuthenticator(service_common_service, 16); return(new Service() { LocalKeyStore = service_local_keyStore, CommonKeyStore = service_common_keyStores, MessageAuthenticator = service_authenticator, DestilationBuffer = destilationBuffer }); }
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()); }
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()); }