Ejemplo n.º 1
0
        /// <summary>
        /// Encrypts the message, using the Sodium cryptography.
        /// </summary>
        internal override void Encrypt()
        {
            Blake2BHasher Blake2B = new Blake2BHasher();

            Blake2B.Update(Keys.Sodium.PublicKey);
            Blake2B.Update("AC30DCBEA27E213407519BC05BE8E9D930E63F873858479946C144895FA3A26B".HexaToBytes());

            byte[] Nonce = Blake2B.Finish();

            Crypto CryptoKeys = new Crypto();

            CryptoKeys.SNonce    = new byte[24];
            CryptoKeys.RNonce    = new byte[24];
            CryptoKeys.PublicKey = Keys.Sodium.PublicKey;

            Sodium Sodium = new Sodium(CryptoKeys);

            byte[] Encrypted = Sodium.Encrypt(this.Data.ToArray(), Nonce);
            Encrypted = CryptoKeys.PublicKey.Concat(Encrypted).ToArray();

            this.Data.Clear();
            this.Data.AddRange(Encrypted);

            this.Length = (uint)Encrypted.Length;
        }
Ejemplo n.º 2
0
        internal virtual void EncryptSodium()
        {
            if (this.Device.PlayerState >= Client_State.LOGGED)
            {
                this.Device.Crypto.RNonce.Increment();

                this.Data = new List <byte>(Sodium.Encrypt(this.Data.ToArray(), this.Device.Crypto.RNonce, this.Device.Crypto.PublicKey).Skip(16).ToArray());
            }

            this.Length = (ushort)this.Data.Count;
        }
        internal override void Encrypt()
        {
            this.Device.Keys.RNonce.Increment();

            this.Data = new List <byte>(Sodium
                                        .Encrypt(this.Data.ToArray(), this.Device.Keys.RNonce, this.Device.Keys.PublicKey)
                                        .Skip(16)
                                        .ToArray());

            this.Length = (ushort)this.Data.Count;
        }
Ejemplo n.º 4
0
        public virtual void Encrypt()
        {
            if (Device.State >= State.LOGGED)
            {
                Device.RNonce.Increment();

                Data = new List <byte>(
                    Sodium.Encrypt(Data.ToArray(), Device.RNonce, Device.PublicKey).Skip(16).ToArray());
            }

            Length = Data.Count;
        }
Ejemplo n.º 5
0
        internal virtual void Encrypt()
        {
            if (Device.PlayerState >= State.LOGGED)
            {
                Device.Keys.RNonce.Increment();

                Data = new List <byte>(Sodium.Encrypt(Data.ToArray(), Device.Keys.RNonce, Device.Keys.PublicKey)
                                       .Skip(16)
                                       .ToArray());
            }

            Length = (ushort)Data.Count;
        }
        internal override void Encrypt()
        {
            var blake = new Blake2BHasher();

            blake.Update(Device.Keys.SNonce);
            blake.Update(Device.Keys.PublicKey);
            blake.Update(Key.PublicKey);

            var Nonce     = blake.Finish();
            var encrypted = Device.Keys.RNonce.Concat(Device.Keys.PublicKey).Concat(Data).ToArray();

            Data   = new List <byte>(Sodium.Encrypt(encrypted, Nonce, Key.PrivateKey, Device.Keys.PublicKey));
            Length = (ushort)Data.Count;
        }
Ejemplo n.º 7
0
        internal void PreparePacket(ReadOnlySpan <byte> pcm, ref Memory <byte> target)
        {
            var audioFormat = AudioFormat;

            var packetArray = ArrayPool <byte> .Shared.Rent(Rtp.CalculatePacketSize(audioFormat.SampleCountToSampleSize(audioFormat.CalculateMaximumFrameSize()), SelectedEncryptionMode));

            var packet = packetArray.AsSpan();

            Rtp.EncodeHeader(Sequence, Timestamp, SSRC, packet);
            var opus = packet.Slice(Rtp.HeaderSize, pcm.Length);

            Opus.Encode(pcm, ref opus);

            Sequence++;
            Timestamp += (uint)audioFormat.CalculateFrameSize(audioFormat.CalculateSampleDuration(pcm.Length));

            Span <byte> nonce = new byte[Sodium.NonceSize];

            switch (SelectedEncryptionMode)
            {
            case EncryptionMode.XSalsa20_Poly1305:
                Sodium.GenerateNonce(packet.Slice(0, Rtp.HeaderSize), nonce);
                break;

            case EncryptionMode.XSalsa20_Poly1305_Suffix:
                Sodium.GenerateNonce(nonce);
                break;

            case EncryptionMode.XSalsa20_Poly1305_Lite:
                Sodium.GenerateNonce(Nonce++, nonce);
                break;

            default:
                ArrayPool <byte> .Shared.Return(packetArray);

                throw new Exception("Unsupported encryption mode.");
            }

            Span <byte> encrypted = new byte[Sodium.CalculateTargetSize(opus)];

            Sodium.Encrypt(opus, encrypted, nonce);
            encrypted.CopyTo(packet.Slice(Rtp.HeaderSize));
            packet = packet.Slice(0, Rtp.CalculatePacketSize(encrypted.Length, SelectedEncryptionMode));
            Sodium.AppendNonce(nonce, packet, SelectedEncryptionMode);

            target = target.Slice(0, packet.Length);
            packet.CopyTo(target.Span);
            ArrayPool <byte> .Shared.Return(packetArray);
        }
Ejemplo n.º 8
0
        internal override void Encrypt()
        {
            Blake2BHasher blake = new Blake2BHasher();

            blake.Update(this.Device.Keys.SNonce);
            blake.Update(this.Device.Keys.PublicKey);
            blake.Update(Key.PublicKey);

            byte[] Nonce     = blake.Finish();
            byte[] encrypted = this.Device.Keys.RNonce.Concat(this.Device.Keys.PublicKey).Concat(this.Data).ToArray();

            this.Data = new List <byte>(Sodium.Encrypt(encrypted, Nonce, Key.PrivateKey, this.Device.Keys.PublicKey));

            this.Length = (ushort)this.Data.Count;
        }
Ejemplo n.º 9
0
        /// <summary>
        ///     <see cref="Encrypt" /> this instance.
        /// </summary>
        public override void Encrypt()
        {
            Blake2BHasher _Hasher = new Blake2BHasher();

            _Hasher.Update(this.Client.SNonce);
            _Hasher.Update(this.Client.PublicKey);
            _Hasher.Update(Keys.Sodium.PublicKey);

            byte[] _Nonce = _Hasher.Finish();

            this.Data = this.Client.RNonce.Concat(this.Client.PublicKey).Concat(this.Writer).ToArray();
            this.Data = Sodium.Encrypt(this.Data, _Nonce, Keys.Sodium.PrivateKey, this.Client.PublicKey);

            this.Length = this.Data.Length;
        }
Ejemplo n.º 10
0
        /// <summary>
        ///     <see cref="Encrypt" /> this instance.
        /// </summary>
        public virtual void Encrypt()
        {
            if (this.Client.State >= State.LOGGED)
            {
                this.Client.RNonce.Increment();

                this.Data =
                    Sodium.Encrypt(this.Writer.ToArray(), this.Client.RNonce, this.Client.PublicKey).Skip(16).ToArray();
                this.Length = this.Data.Length;
            }
            else
            {
                this.Data   = this.Writer.ToArray();
                this.Length = this.Data.Length;
            }
        }
Ejemplo n.º 11
0
        internal override void EncryptSodium()
        {
            if (this.Device.PlayerState >= Client_State.LOGIN)
            {
                Blake2BHasher Blake = new Blake2BHasher();

                Blake.Update(this.Device.Crypto.SNonce);
                Blake.Update(this.Device.Crypto.PublicKey);
                Blake.Update(Key.Crypto.PublicKey);

                byte[] Nonce     = Blake.Finish();
                byte[] Encrypted = this.Device.Crypto.RNonce.Concat(this.Device.Crypto.PublicKey).Concat(this.Data).ToArray();

                this.Data = new List <byte>(Sodium.Encrypt(Encrypted, Nonce, Key.Crypto.PrivateKey, this.Device.Crypto.PublicKey));
            }

            this.Length = (ushort)this.Data.Count;
        }
Ejemplo n.º 12
0
        public override void Encrypt()
        {
            if (Device.State >= State.LOGIN)
            {
                var Blake = new Blake2BHasher();

                Blake.Update(Device.SNonce);
                Blake.Update(Device.PublicKey);
                Blake.Update(Key.PublicKey);

                var Nonce     = Blake.Finish();
                var Encrypted = Device.RNonce.Concat(Device.PublicKey).Concat(Data).ToArray();

                Data = new List <byte>(Sodium.Encrypt(Encrypted, Nonce, Key.Crypto.PrivateKey, Device.PublicKey));
            }

            Length = (ushort)Data.Count;
        }
Ejemplo n.º 13
0
        /// <summary>
        ///     <see cref="Encrypt" /> this instance.
        /// </summary>
        public override void Encrypt()
        {
            if (this.Client.State > State.SESSION_OK)
            {
                Blake2BHasher _Blake = new Blake2BHasher();

                _Blake.Update(this.Client.SNonce);
                _Blake.Update(this.Client.PublicKey);
                _Blake.Update(Keys.Sodium.PublicKey);

                byte[] _Nonce = _Blake.Finish();

                this.Data = this.Client.RNonce.Concat(this.Client.PublicKey).Concat(this.Writer).ToArray();
                this.Data = Sodium.Encrypt(this.Data, _Nonce, Keys.Sodium.PrivateKey, this.Client.PublicKey);

                this.Length = this.Data.Length;
            }
            else
            {
                this.Data   = this.Writer.ToArray();
                this.Length = this.Data.Length;
            }
        }
Ejemplo n.º 14
0
        private void SendVoiceAsync(CancellationToken cancelToken)
        {
            try
            {
                while (!cancelToken.IsCancellationRequested && _state != (int)WebSocketState.Connected)
                {
                    Thread.Sleep(1);
                }

                if (cancelToken.IsCancellationRequested)
                {
                    return;
                }

                byte[]    frame               = new byte[_encoder.FrameSize];
                byte[]    encodedFrame        = new byte[MaxOpusSize];
                byte[]    udpPacket, nonce    = null;
                uint      timestamp           = 0;
                double    nextTicks           = 0.0;
                double    ticksPerMillisecond = Stopwatch.Frequency / 1000.0;
                double    ticksPerFrame       = ticksPerMillisecond * _encoder.FrameLength;
                double    spinLockThreshold   = 3 * ticksPerMillisecond;
                uint      samplesPerFrame     = (uint)_encoder.SamplesPerFrame;
                Stopwatch sw = Stopwatch.StartNew();

                if (_isEncrypted)
                {
                    nonce     = new byte[24];
                    udpPacket = new byte[MaxOpusSize + 12 + 16];
                }
                else
                {
                    udpPacket = new byte[MaxOpusSize + 12];
                }

                int rtpPacketLength = 0;
                udpPacket[0]  = 0x80;                //Flags;
                udpPacket[1]  = 0x78;                //Payload Type
                udpPacket[8]  = (byte)((_ssrc >> 24) & 0xFF);
                udpPacket[9]  = (byte)((_ssrc >> 16) & 0xFF);
                udpPacket[10] = (byte)((_ssrc >> 8) & 0xFF);
                udpPacket[11] = (byte)((_ssrc >> 0) & 0xFF);

                if (_isEncrypted)
                {
                    Buffer.BlockCopy(udpPacket, 0, nonce, 0, 12);
                }

                while (!cancelToken.IsCancellationRequested)
                {
                    double ticksToNextFrame = nextTicks - sw.ElapsedTicks;
                    if (ticksToNextFrame <= 0.0)
                    {
                        while (sw.ElapsedTicks > nextTicks)
                        {
                            if (_sendBuffer.Pop(frame))
                            {
                                ushort sequence = unchecked (_sequence++);
                                udpPacket[2] = (byte)((sequence >> 8) & 0xFF);
                                udpPacket[3] = (byte)((sequence >> 0) & 0xFF);
                                udpPacket[4] = (byte)((timestamp >> 24) & 0xFF);
                                udpPacket[5] = (byte)((timestamp >> 16) & 0xFF);
                                udpPacket[6] = (byte)((timestamp >> 8) & 0xFF);
                                udpPacket[7] = (byte)((timestamp >> 0) & 0xFF);

                                //Encode
                                int encodedLength = _encoder.EncodeFrame(frame, 0, encodedFrame);

                                //Encrypt
                                if (_isEncrypted)
                                {
                                    Buffer.BlockCopy(udpPacket, 2, nonce, 2, 6);                                     //Update nonce
                                    int ret = Sodium.Encrypt(encodedFrame, encodedLength, udpPacket, 12, nonce, _secretKey);
                                    if (ret != 0)
                                    {
                                        continue;
                                    }
                                    rtpPacketLength = encodedLength + 12 + 16;
                                }
                                else
                                {
                                    Buffer.BlockCopy(encodedFrame, 0, udpPacket, 12, encodedLength);
                                    rtpPacketLength = encodedLength + 12;
                                }
                                _udp.Send(udpPacket, rtpPacketLength);
                            }
                            timestamp  = unchecked (timestamp + samplesPerFrame);
                            nextTicks += ticksPerFrame;
                        }
                    }
                    //Dont sleep if we need to output audio in the next spinLockThreshold
                    else if (ticksToNextFrame > spinLockThreshold)
                    {
                        int time = (int)Math.Ceiling((ticksToNextFrame - spinLockThreshold) / ticksPerMillisecond);
                        Thread.Sleep(1);
                    }
                }
            }
            catch (OperationCanceledException) { }
            catch (InvalidOperationException) { }             //Includes ObjectDisposedException
        }