Example #1
0
 // Internal methods for encryption :)
 private static uint KSchedCore(uint input, int iteration)
 {
     input = Rotate(input);
     byte[] bytes = Support.WriteToArray(new byte[4], input, 0);
     for (int i = 0; i < bytes.Length; ++i)
     {
         bytes[i] = SBox(bytes[i]);
     }
     bytes[bytes.Length - 1] ^= RCON(iteration);
     return((uint)Support.ReadInt(bytes, 0));
 }
Example #2
0
 // Check if the data we want to convert into an RSA-instance will cause a crash if we try to parse it
 public static bool CanDeserialize(IEnumerable <byte> data)
 {
     try
     {
         int size = Support.ReadInt(data, 0), size2;
         if (size >= data.Count() - 8)
         {
             return(false);
         }
         size2 = Support.ReadInt(data, 4 + size);
         if (size2 > data.Count() - size - 8)
         {
             return(false);
         }
         return(true);
     }
     catch (Exception) { }
     return(false);
 }
Example #3
0
        private static byte[] KeySchedule(byte[] key, BitMode mode)
        {
            int n = mode == BitMode.Bit128 ? 16 : mode == BitMode.Bit192 ? 24 : 32;
            int b = mode == BitMode.Bit128 ? 176 : mode == BitMode.Bit192 ? 208 : 240;

            byte[] output = new byte[b];
            Array.Copy(key, output, n);

            int rcon_iter = 1;

            int accruedBytes = n;

            while (accruedBytes < b)
            {
                // Generate 4 new bytes of extended key
                byte[] t = Support.WriteToArray(new byte[4], KSchedCore((uint)Support.ReadInt(output, accruedBytes - 4), rcon_iter), 0);
                ++rcon_iter;
                for (int i = 0; i < 4; ++i)
                {
                    t[i] ^= output[accruedBytes - n + i];
                }
                Array.Copy(t, 0, output, accruedBytes, 4);
                accruedBytes += 4;

                // Generate 12 new bytes of extended key
                for (int i = 0; i < 3; ++i)
                {
                    Array.Copy(output, accruedBytes - 4, t, 0, 4);
                    for (int j = 0; j < 4; ++j)
                    {
                        t[j] ^= output[accruedBytes - n + j];
                    }
                    Array.Copy(t, 0, output, accruedBytes, 4);
                    accruedBytes += 4;
                }

                // Special processing for 256-bit key schedule
                if (mode == BitMode.Bit256)
                {
                    Array.Copy(output, accruedBytes - 4, t, 0, 4);
                    for (int j = 0; j < 4; ++j)
                    {
                        t[j] = (byte)(SBox(t[j]) ^ output[accruedBytes - n + j]);
                    }
                    Array.Copy(t, 0, output, accruedBytes, 4);
                    accruedBytes += 4;
                }
                // Special processing for 192-bit key schedule
                if (mode != BitMode.Bit128)
                {
                    for (int i = mode == BitMode.Bit192 ? 1 : 2; i >= 0; --i)
                    {
                        Array.Copy(output, accruedBytes - 4, t, 0, 4);
                        for (int j = 0; j < 4; ++j)
                        {
                            t[j] ^= output[accruedBytes - n + j];
                        }
                        Array.Copy(t, 0, output, accruedBytes, 4);
                        accruedBytes += 4;
                    }
                }
            }

            return(output);
        }
Example #4
0
 public static byte[] FromHeaded(byte[] msg, int offset) => msg.SubArray(offset + 4, offset + 4 + Support.ReadInt(msg, offset));
Example #5
0
        protected internal bool SyncListener(ref bool cryptoEstablished, ref int mLen, out bool acceptedData, Queue <byte> ibuf, byte[] buffer)
        {
            if (cryptoEstablished)
            {
                lock (messageBuffer)
                {
                    foreach (byte[] message in messageBuffer)
                    {
                        Connection.Send(NetSupport.WithHeader(message));
                    }
                    messageBuffer.Clear();
                }
            }
            if (acceptedData = Connection.Available > 0)
            {
                int read = Connection.Receive(buffer);
                ibuf.EnqueueAll(buffer, 0, read);
            }
            if (mLen == 0 && ibuf.Count >= 4)
            {
                mLen = Support.ReadInt(ibuf.Dequeue(4), 0);
            }
            if (mLen != 0 && ibuf.Count >= mLen)
            {
                // Got a full message. Parse!
                byte[] message = ibuf.Dequeue(mLen);

                if (!cryptoEstablished)
                {
                    if (ServerSide)
                    {
                        try
                        {
                            if (Crypto == null)
                            {
                                Crypto = Rijndael128.Deserialize(decrypt.Decrypt(message), out int _);
                            }
                            else
                            {
                                CBC = new PCBC(Crypto, decrypt.Decrypt(message));
                            }
                        }
                        catch (Exception) {
                            Console.WriteLine("A fatal error ocurrd when attempting to establish a secure channel! Stopping...");
                            Thread.Sleep(5000);
                            Environment.Exit(-1);
                        }
                    }
                    else
                    {
                        // Reconstruct RSA object from remote public keys and use it to encrypt our serialized AES key/iv
                        RSA asymm = RSA.Deserialize(message, out int _);
                        Connection.Send(NetSupport.WithHeader(asymm.Encrypt(Crypto.Serialize())));
                        Connection.Send(NetSupport.WithHeader(asymm.Encrypt(CBC.IV)));
                    }
                    if (CBC != null)
                    {
                        cryptoEstablished = true;
                        onConn(this);
                    }
                }
                else
                {
                    // Decrypt the incoming message
                    byte[] read = Crypto.Decrypt(message);

                    // Read the decrypted message length
                    int mlenInner = Support.ReadInt(read, 0);

                    // Send the message to the handler and get a response
                    string response = handler(read.SubArray(4, 4 + mlenInner).ToUTF8String(), out bool live);

                    // Send the response (if given one) and drop the connection if the handler tells us to
                    if (response != null)
                    {
                        Connection.Send(NetSupport.WithHeader(Crypto.Encrypt(NetSupport.WithHeader(response.ToUTF8Bytes()))));
                    }
                    if (!live)
                    {
                        Running = false;
                        try
                        {
                            Connection.Close();
                        }
                        catch (Exception) { }
                        return(true);
                    }
                }

                // Reset expexted message length
                mLen = 0;
            }
            return(false);
        }