public static MemoryBuffer SliceShortProtectedPacketBytes( MemoryCursor cursor, IAead aead, ICipher cipher, int startPacketOffset, PacketFirstByte firstByte, PacketNumber largestAcknowledgedPacketNumber, out PacketNumber packetNumber) { var startPacketNumberOffset = cursor.AsOffset(); cursor.Move(SkipToSampleLength); var sample = cursor.Move(SampleLength); var mask = cipher.CreateMask(sample.Span); cursor.Set(startPacketOffset); firstByte.Mask(mask).Write(cursor); cursor.Set(startPacketNumberOffset); var packetNumberBytes = firstByte.Mask(mask).SlicePacketNumberBytes(cursor); var headerLength = cursor - startPacketOffset; var headerBytes = cursor.Peek(-headerLength); var startPayloadOffset = cursor.AsOffset(); cursor.MoveEnd(); var tagBytes = cursor.Move(-aead.TagLength); var payloadLength = cursor - startPayloadOffset; var payloadBytes = cursor.Peek(-payloadLength); var payload = cursor.Slice(-payloadLength); var encodedPacketNumber = PacketNumber.Parse(packetNumberBytes.Span, mask); encodedPacketNumber.Fill(packetNumberBytes.Span); packetNumber = encodedPacketNumber.DecodeByLargestAcknowledged(largestAcknowledgedPacketNumber); packetNumber.Decrypt(aead, payloadBytes.Span, tagBytes.Span, headerBytes.Span); cursor.MoveEnd(); return(payload); }
public void Dispose() { var payloadLength = cursor - startPayloadOffset; var payload = cursor.Peek(-payloadLength); Span <byte> payloadBuffer = stackalloc byte[payloadLength]; Span <byte> tagBuffer = stackalloc byte[aead.TagLength]; payload.Span.CopyTo(payloadBuffer); packetFirstByte.SlicePacketNumberBytes(cursor); var packetLength = cursor - startPacketOffset; cursor.Set(startPayloadOffset); cursor.EncodeVariable32(packetLength); var packetNumberBytes = packetFirstByte.SlicePacketNumberBytes(cursor); var encodedPacketNumber = largestAcknowledgedPacketNumber.HasValue ? packetNumber.EncodeByLargestAcknowledged(largestAcknowledgedPacketNumber.Value) : packetNumber; encodedPacketNumber.Fill(packetNumberBytes.Span); var headerLength = cursor - startPacketOffset; var header = cursor.Peek(-headerLength); packetNumber.Encrypt(aead, payloadBuffer, tagBuffer, header.Span); payloadBuffer.CopyTo(cursor); tagBuffer.CopyTo(cursor); var endPacketOffset = cursor.AsOffset(); var encryptedLength = payloadBuffer.Length + tagBuffer.Length; var encryptedData = cursor.Peek(-encryptedLength); var sample = encryptedData.Slice(0, SampleLength); var mask = cipher.CreateMask(sample.Span); encodedPacketNumber.Fill(packetNumberBytes.Span, mask); cursor.Set(startPacketOffset); packetFirstByte.Mask(mask).Write(cursor); cursor.Set(endPacketOffset); }
public void Dispose() { var payloadLength = cursor - startPayloadOffset; var payload = cursor.Move(-payloadLength); Span <byte> payloadBuffer = stackalloc byte[payloadLength]; Span <byte> tagBuffer = stackalloc byte[aead.TagLength]; payload.Span.CopyTo(payloadBuffer); var encodedPacketNumber = packetNumber.EncodeByLargestAcknowledged(largestAcknowledgedPacketNumber); var minPacketNumberLength = SkipToSampleLength - payloadLength; var packetNumberLength = minPacketNumberLength < 0 ? encodedPacketNumber.Write(cursor) : encodedPacketNumber.Write(cursor, minPacketNumberLength); var startEncryptedPayloadOffset = cursor.AsOffset(); var packetFirstByteWithNumberLength = packetFirstByte.SetPacketNumberLength(packetNumberLength); var packetNumberBytes = cursor.Peek(-packetNumberLength); var headerLength = cursor - startPacketOffset; var header = cursor.Move(-headerLength); packetFirstByteWithNumberLength.Write(cursor); cursor.Set(startEncryptedPayloadOffset); packetNumber.Encrypt(aead, payloadBuffer, tagBuffer, header.Span); payloadBuffer.CopyTo(cursor); tagBuffer.CopyTo(cursor); var endPacketOffset = cursor.AsOffset(); cursor.Set(startPayloadOffset); cursor.Move(SkipToSampleLength); var sample = cursor.Move(SampleLength); var mask = cipher.CreateMask(sample.Span); encodedPacketNumber.Fill(packetNumberBytes.Span, mask); cursor.Set(startPacketOffset); packetFirstByteWithNumberLength.Mask(mask).Write(cursor); cursor.Set(endPacketOffset); }