Пример #1
0
        /// <summary>
        /// Handles the pepper login response.
        /// </summary>
        public static byte[] HandlePepperLoginResponse(ref PepperInit Init, byte[] Packet, out IEncrypter SendEncrypter, out IEncrypter ReceiveEncrypter)
        {
            ++Init.State;

            byte[] Decrypted = new byte[Packet.Length + 16];

            Array.Copy(Packet, 0, Decrypted, 16, Packet.Length);

            Blake2BHasher Blake2B = new Blake2BHasher();

            Blake2B.Update(Init.Nonce);
            Blake2B.Update(Init.ClientPublicKey);
            Blake2B.Update(Init.ServerPublicKey);

            Curve25519Xsalsa20Poly1305.CryptoBoxOpen(Decrypted, Decrypted, Blake2B.Finish(), Init.ServerPublicKey, Init.ClientSecretKey);

            byte[] SecretKey    = new byte[32];
            byte[] ReceiveNonce = new byte[24];

            Packet = new byte[Decrypted.Length - 32 - 56];

            Array.Copy(Decrypted, 32, ReceiveNonce, 0, 24);
            Array.Copy(Decrypted, 32 + 24, SecretKey, 0, 32);
            Array.Copy(Decrypted, 32 + 24 + 32, Packet, 0, Packet.Length);

            SendEncrypter    = new PepperEncrypter(Init.Nonce, SecretKey);
            ReceiveEncrypter = new PepperEncrypter(ReceiveNonce, SecretKey);

            return(Packet);
        }
Пример #2
0
        /// <summary>
        /// Encryptes the login response message.
        /// </summary>
        public static byte[] SendPepperLoginResponse(ref PepperInit Init, out IEncrypter SendEncrypter, out IEncrypter ReceiveEncrypter, byte[] Data)
        {
            ++Init.State;

            Blake2BHasher Blake2 = new Blake2BHasher();

            Blake2.Update(Init.Nonce);
            Blake2.Update(Init.ClientPublicKey);
            Blake2.Update(Init.ServerPublicKey);

            byte[] M = new byte[Data.Length + 88];

            byte[] SendNonce = new byte[24];
            byte[] SecretKey = new byte[32];

            XorShift.NextBytes(SendNonce);
            XorShift.NextBytes(SecretKey);

            SendEncrypter    = new PepperEncrypter(SendNonce, SecretKey);
            ReceiveEncrypter = new PepperEncrypter(Init.Nonce, SecretKey);

            Buffer.BlockCopy(SendNonce, 0, M, 32, 24);
            Buffer.BlockCopy(SecretKey, 0, M, 56, 32);
            Buffer.BlockCopy(Data, 0, M, 88, Data.Length);

            if (Curve25519Xsalsa20Poly1305.CryptoBoxAfternm(M, M, Blake2.Finish(), Init.SharedKey) == 0)
            {
                byte[] Encrypted = new byte[M.Length - 16];
                Buffer.BlockCopy(M, 16, Encrypted, 0, M.Length - 16);
                return(Encrypted);
            }

            Logging.Error(typeof(PepperCrypto), "Unable de send pepper login response.");

            return(null);
        }
Пример #3
0
        /// <summary>
        /// Encryptes the login response message.
        /// </summary>
        internal static byte[] SendPepperLoginResponse(ref PepperInit Init, out IEncrypter SendEncrypter, out IEncrypter ReceiveEncrypter, byte[] Data)
        {
            ++Init.State;

            Blake2BHasher Blake2 = new Blake2BHasher();

            Blake2.Update(Init.Nonce);
            Blake2.Update(Init.ClientPublicKey);
            Blake2.Update(Init.ServerPublicKey);

            byte[] m = new byte[Data.Length + 88];

            byte[] SendNonce = new byte[24];
            byte[] SecretKey = new byte[32];

            Resources.Random.NextBytes(SendNonce);
            Resources.Random.NextBytes(SecretKey);

            SendEncrypter    = new PepperEncrypter(SendNonce, SecretKey);
            ReceiveEncrypter = new PepperEncrypter(Init.Nonce, SecretKey);

            Buffer.BlockCopy(SendNonce, 0, m, 32, 24);
            Buffer.BlockCopy(SecretKey, 0, m, 56, 32);
            Buffer.BlockCopy(Data, 0, m, 88, Data.Length);

            if (curve25519xsalsa20poly1305.crypto_box_afternm(m, m, Blake2.Finish(), Init.SharedKey) == 0)
            {
                byte[] Encrypted = new byte[m.Length - 16];
                Buffer.BlockCopy(m, 16, Encrypted, 0, m.Length - 16);
                return(Encrypted);
            }

            Logging.Error(typeof(PepperCrypto), "Unable de send pepper login response.");

            return(null);
        }