Ejemplo n.º 1
0
        public byte[] Serialize <T>(CryptoDtoChannel channel, CryptoDtoMode mode, T obj)
        {
            ArrayBufferWriter <byte> arrayBufferWriter = new ArrayBufferWriter <byte>();

            Serialize(arrayBufferWriter, channel, mode, obj);
            return(arrayBufferWriter.WrittenSpan.ToArray());
        }
Ejemplo n.º 2
0
        public ReadOnlySpan <byte> GetReceiveKey(CryptoDtoMode mode)
        {
            switch (mode)
            {
            case CryptoDtoMode.ChaCha20Poly1305:
                return(aeadReceiveKey);

            default:
                throw new CryptographicException("CryptoDtoMode value not handled.");
            }
        }
        public ReadOnlySpan <byte> GetTransmitKey(string channelTag, CryptoDtoMode mode, out ulong transmitSequence)
        {
            lock (channelStoreLock)
            {
                if (!channelStore.ContainsKey(channelTag))
                {
                    throw new CryptographicException("Key tag does not exist in store. (" + channelTag + ")");
                }

                return(channelStore[channelTag].GetTransmitKey(mode, out transmitSequence));
            }
        }
Ejemplo n.º 4
0
        //[Obsolete]
        //public byte[] Pack(CryptoDtoChannelStore channelStore, string channelTag, CryptoDtoMode mode, ReadOnlySpan<byte> dtoNameBuffer, ReadOnlySpan<byte> dtoBuffer)
        //{
        //    var channel = channelStore.GetChannel(channelTag);
        //    return Pack(channel, mode, dtoNameBuffer, dtoBuffer);
        //}

        //[Obsolete]
        //public byte[] Pack(CryptoDtoChannel channel, CryptoDtoMode mode, ReadOnlySpan<byte> dtoNameBuffer, ReadOnlySpan<byte> dtoBuffer)
        //{
        //    ArrayBufferWriter<byte> arrayBufferWriter = new ArrayBufferWriter<byte>();
        //    Pack(arrayBufferWriter, channel, mode, dtoNameBuffer, dtoBuffer);
        //    return arrayBufferWriter.WrittenSpan.ToArray();
        //}

        public void Pack(IBufferWriter <byte> output, CryptoDtoChannel channel, CryptoDtoMode mode, ReadOnlySpan <byte> dtoNameBuffer, ReadOnlySpan <byte> dtoBuffer)
        {
            channel.GetTransmitKey(mode, out ulong sequenceToSend);
            CryptoDtoHeaderDto header = new CryptoDtoHeaderDto
            {
                ChannelTag = channel.ChannelTag,
                Mode       = mode,
                Sequence   = sequenceToSend
            };

            Pack(output, header, channel.TransmitChaCha20Poly1305, dtoNameBuffer, dtoBuffer);
        }
        private static byte[] Pack(string channelTag, CryptoDtoMode mode, ReadOnlySpan <byte> transmitKey, ulong sequenceToBeSent, byte[] dtoNameBuffer, byte[] dtoBuffer)
        {
            CryptoDtoHeaderDto header = new CryptoDtoHeaderDto
            {
                ChannelTag = channelTag,
                Sequence   = sequenceToBeSent,
                Mode       = mode
            };

            var    headerBuffer  = MessagePackSerializer.Serialize(header);
            ushort headerLength  = (ushort)headerBuffer.Length;
            ushort dtoNameLength = (ushort)dtoNameBuffer.Length;
            ushort dtoLength     = (ushort)dtoBuffer.Length;

            switch (header.Mode)
            {
            case CryptoDtoMode.ChaCha20Poly1305:
            {
                int aePayloadLength = 2 + dtoNameLength + 2 + dtoLength;
                var aePayloadBuffer = new byte[aePayloadLength];

                Array.Copy(BitConverter.GetBytes(dtoNameLength), 0, aePayloadBuffer, 0, 2);
                Array.Copy(dtoNameBuffer, 0, aePayloadBuffer, 2, dtoNameLength);

                Array.Copy(BitConverter.GetBytes(dtoLength), 0, aePayloadBuffer, 2 + dtoNameLength, 2);
                Array.Copy(dtoBuffer, 0, aePayloadBuffer, 2 + dtoNameLength + 2, dtoLength);

                int adPayloadLength = 2 + headerLength;
                var adPayloadBuffer = new byte[adPayloadLength];

                Array.Copy(BitConverter.GetBytes(headerLength), 0, adPayloadBuffer, 0, 2);
                Array.Copy(headerBuffer, 0, adPayloadBuffer, 2, headerLength);

                Span <byte> nonceBuffer = stackalloc byte[Aead.NonceSize];
                BinaryPrimitives.WriteUInt64LittleEndian(nonceBuffer.Slice(4), header.Sequence);

                var    aead              = new ChaCha20Poly1305(transmitKey.ToArray());
                byte[] aeadPayload       = aead.Encrypt(aePayloadBuffer, adPayloadBuffer, nonceBuffer);
                int    aeadPayloadLength = aeadPayload.Length;

                byte[] packetBuffer = new byte[2 + headerLength + aeadPayloadLength];

                Array.Copy(BitConverter.GetBytes(headerLength), 0, packetBuffer, 0, 2);
                Array.Copy(headerBuffer, 0, packetBuffer, 2, headerLength);
                Array.Copy(aeadPayload, 0, packetBuffer, 2 + headerLength, aeadPayloadLength);
                return(packetBuffer);
            }

            default:
                throw new CryptographicException("Mode not recognised");
            }
        }
Ejemplo n.º 6
0
        public ReadOnlySpan <byte> GetTransmitKey(CryptoDtoMode mode, out ulong sequenceToSend)
        {
            sequenceToSend = transmitSequence;
            transmitSequence++;
            LastTransmitUtc = DateTime.UtcNow;

            switch (mode)
            {
            case CryptoDtoMode.ChaCha20Poly1305:
                return(aeadTransmitKey);

            default:
                throw new CryptographicException("CryptoDtoMode value not handled.");
            }
        }
Ejemplo n.º 7
0
        public static long SerialiseCryptoDto <T>(this DealerSocket dealerSocket, object data, CryptoDtoChannel channel, CryptoDtoMode mode)
        {
            T typedData = (T)data;

            byte[] cryptoData = CryptoDtoSerializer.Serialize(channel, mode, typedData);

            dealerSocket
            .SendMoreFrameEmpty()
            .SendFrame(cryptoData);

            return(cryptoData.Length);
        }
 private static byte[] Serialise <T>(string channelTag, CryptoDtoMode mode, ReadOnlySpan <byte> transmitKey, ulong sequenceToBeSent, T dto)
 {
     byte[] dtoNameBuffer = GetDtoNameBytes <T>();
     byte[] dtoBuffer     = MessagePackSerializer.Serialize(dto);
     return(Pack(channelTag, mode, transmitKey, sequenceToBeSent, dtoNameBuffer, dtoBuffer));
 }
        public static byte[] Pack(CryptoDtoChannel channel, CryptoDtoMode mode, byte[] dtoNameBuffer, byte[] dtoBuffer)
        {
            var transmitKey = channel.GetTransmitKey(mode, out ulong sequenceToSend);

            return(Pack(channel.ChannelTag, mode, transmitKey, sequenceToSend, dtoNameBuffer, dtoBuffer));
        }
        public static byte[] Serialize <T>(CryptoDtoChannel channel, CryptoDtoMode mode, T obj)
        {
            var transmitKey = channel.GetTransmitKey(mode, out ulong sequenceToSend);

            return(Serialise(channel.ChannelTag, mode, transmitKey, sequenceToSend, obj));
        }
Ejemplo n.º 11
0
 // Hint for callers using ArrayBufferWriter<byte> - output.WrittenSpan contains the serialised data
 public void Serialize <T>(IBufferWriter <byte> output, CryptoDtoChannel channel, CryptoDtoMode mode, T dto)
 {
     lock (bufferLock)
     {
         ReadOnlySpan <byte> dtoNameBuffer = GetDtoNameBytes <T>();
         dtoBuffer.Clear();
         MessagePackSerializer.Serialize(dtoBuffer, dto);
         Pack(output, channel, mode, dtoNameBuffer, dtoBuffer.WrittenSpan);
     }
 }
Ejemplo n.º 12
0
        // Hint for callers using ArrayBufferWriter<byte> - output.WrittenSpan contains the serialised data
        public void Serialize <T>(IBufferWriter <byte> output, CryptoDtoChannelStore channelStore, string channelTag, CryptoDtoMode mode, T obj)
        {
            var channel = channelStore.GetChannel(channelTag);

            Serialize(output, channel, mode, obj);
        }
Ejemplo n.º 13
0
        public byte[] Serialize <T>(CryptoDtoChannelStore channelStore, string channelTag, CryptoDtoMode mode, T obj)
        {
            var channel = channelStore.GetChannel(channelTag);

            return(Serialize(channel, mode, obj));
        }