Example #1
0
        private static EncryptionKeys GenerateHMAC_SHA1Keys(BigInteger K, string H, string sessionIdentifier)
        {
            EncryptionKeys keys    = new EncryptionKeys();
            List <byte>    forHash = new List <byte>();

            keys.MAClength = 20;

            var K_array      = K.ToByteArrayUnsigned();
            var K_size       = K_array.Length;
            var K_size_array = BitConverter.GetBytes(K_size);

            // reverse zbog toga da ide iz little u big endian - ("normalni")
            Array.Reverse(K_size_array);

            var H_array      = Convert.FromBase64String(H);
            var H_size       = H_array.Length;
            var H_size_array = BitConverter.GetBytes(H_size);

            // reverse zbog toga da ide iz little u big endian - ("normalni")
            Array.Reverse(H_size_array);

            var sessionIdent_array      = Convert.FromBase64String(sessionIdentifier);
            var sessionIdent_size       = sessionIdent_array.Length;
            var sessionIdent_size_array = BitConverter.GetBytes(sessionIdent_size);

            // reverse zbog toga da ide iz little u big endian - ("normalni")
            Array.Reverse(sessionIdent_size_array);

            var A = Convert.ToByte('A');
            var B = Convert.ToByte('B');
            var C = Convert.ToByte('C');
            var D = Convert.ToByte('D');
            var E = Convert.ToByte('E');
            var F = Convert.ToByte('F');

            var KH_array = new List <byte>();

            KH_array.AddRange(K_size_array);
            KH_array.AddRange(K_array);

            KH_array.AddRange(H_size_array);
            KH_array.AddRange(H_array);

            // MAC ključ k -> s
            forHash.Clear();

            forHash.AddRange(KH_array);

            forHash.Add(E);

            forHash.AddRange(sessionIdent_array);

            using (SHA1Managed sha1 = new SHA1Managed())
            {
                keys.MACKeyCS = sha1.ComputeHash(forHash.ToArray());
            }

            // MAC ključ s -> k
            forHash.Clear();

            forHash.AddRange(KH_array);

            forHash.Add(F);

            forHash.AddRange(sessionIdent_array);

            using (SHA1Managed sha1 = new SHA1Managed())
            {
                keys.MACKeySC = sha1.ComputeHash(forHash.ToArray());
            }

            return(keys);
        }
Example #2
0
        private static EncryptionKeys Generate3DESKeys(BigInteger K, string H, string sessionIdentifier)
        {
            EncryptionKeys keys    = new EncryptionKeys();
            List <byte>    forHash = new List <byte>();

            var K_array      = K.ToByteArrayUnsigned();
            var K_size       = K_array.Length;
            var K_size_array = BitConverter.GetBytes(K_size);

            // reverse zbog toga da ide iz little u big endian - ("normalni")
            Array.Reverse(K_size_array);

            var H_array      = Convert.FromBase64String(H);
            var H_size       = H_array.Length;
            var H_size_array = BitConverter.GetBytes(H_size);

            // reverse zbog toga da ide iz little u big endian - ("normalni")
            Array.Reverse(H_size_array);

            var sessionIdent_array      = Convert.FromBase64String(sessionIdentifier);
            var sessionIdent_size       = sessionIdent_array.Length;
            var sessionIdent_size_array = BitConverter.GetBytes(sessionIdent_size);

            // reverse zbog toga da ide iz little u big endian - ("normalni")
            Array.Reverse(sessionIdent_size_array);

            var A = Convert.ToByte('A');
            var B = Convert.ToByte('B');
            var C = Convert.ToByte('C');
            var D = Convert.ToByte('D');
            var E = Convert.ToByte('E');
            var F = Convert.ToByte('F');

            var KH_array = new List <byte>();

            KH_array.AddRange(K_size_array);
            KH_array.AddRange(K_array);

            KH_array.AddRange(H_size_array);
            KH_array.AddRange(H_array);

            // 3DES ključ 192b = 24B
            // aes256 je 256 bita - 32B

            // enkripcija k -> s
            forHash.Clear();

            forHash.AddRange(KH_array);

            forHash.Add(C);

            forHash.AddRange(sessionIdent_array);

            using (SHA1Managed sha1 = new SHA1Managed())
            {
                var key_list = sha1.ComputeHash(forHash.ToArray()).ToList();

                /*
                 * If the key length needed is longer than the output of the HASH, the
                 * key is extended by computing HASH of the concatenation of K and H and
                 * the entire key so far, and appending the resulting bytes (as many as
                 * HASH generates) to the key
                 */
                while (key_list.Count < 24)
                {
                    var temp = new List <byte>();
                    temp.AddRange(K_array);
                    temp.AddRange(H_array);
                    temp.AddRange(key_list);
                    key_list.AddRange(sha1.ComputeHash(temp.ToArray()));
                }

                keys.cryCS = key_list.Take(24).ToArray();
            }

            // enkripcija s -> k
            forHash.Clear();

            forHash.AddRange(KH_array);

            forHash.Add(D);

            forHash.AddRange(sessionIdent_array);

            using (SHA1Managed sha1 = new SHA1Managed())
            {
                var key_list = sha1.ComputeHash(forHash.ToArray()).ToList();

                /*
                 * If the key length needed is longer than the output of the HASH, the
                 * key is extended by computing HASH of the concatenation of K and H and
                 * the entire key so far, and appending the resulting bytes (as many as
                 * HASH generates) to the key
                 */
                while (key_list.Count < 24)
                {
                    var temp = new List <byte>();
                    temp.AddRange(K_array);
                    temp.AddRange(H_array);
                    temp.AddRange(key_list);
                    key_list.AddRange(sha1.ComputeHash(temp.ToArray()));
                }

                keys.crySC = key_list.Take(24).ToArray();
            }

            return(keys);
        }
Example #3
0
        public static EncryptionKeys GenerateEncryptionKeysFor3DES_CBC(string encryptionAl, string macAl, ref EncryptionAlgorithms encryptionAlgorithms, BigInteger K, string H, string sessionIdentifier)
        {
            // session identifier = H - ostaje isti čak i ako se ključevi promijene (i sam H je tada drugačiji...)

            /*
             * Inicijalni IV (klijent -> poslužitelj) = hash (K || H || "A" || identifikator_sjednice)
             * Inicijalni IV (poslužitelj -> klijent) = hash (K || H || "B" || identifikator_sjednice)
             * Kljuc enkripcije (klijent -> poslužitelj) = hash (K || H || "C" || identifikator_sjednice)
             * Kljuc enkripcije (poslužitelj -> klijent) = hash (K || H || "D" || identifikator_sjednice)
             * MAC kljuc (klijent -> poslužitelj) = hash (K || H || "E" || identifikator_sjednice)
             * MAC kljuc (klijent -> poslužitelj) = hash (K || H || "F" || identifikator_sjednice)
             */

            EncryptionKeys keys    = new EncryptionKeys();
            List <byte>    forHash = new List <byte>();

            var K_array      = K.ToByteArrayUnsigned();
            var K_size       = K_array.Length;
            var K_size_array = BitConverter.GetBytes(K_size);

            // reverse zbog toga da ide iz little u big endian - ("normalni")
            Array.Reverse(K_size_array);

            var H_array      = Convert.FromBase64String(H);
            var H_size       = H_array.Length;
            var H_size_array = BitConverter.GetBytes(H_size);

            // reverse zbog toga da ide iz little u big endian - ("normalni")
            Array.Reverse(H_size_array);

            var sessionIdent_array      = Convert.FromBase64String(sessionIdentifier);
            var sessionIdent_size       = sessionIdent_array.Length;
            var sessionIdent_size_array = BitConverter.GetBytes(sessionIdent_size);

            // reverse zbog toga da ide iz little u big endian - ("normalni")
            Array.Reverse(sessionIdent_size_array);

            var A = Convert.ToByte('A');
            var B = Convert.ToByte('B');
            var C = Convert.ToByte('C');
            var D = Convert.ToByte('D');
            var E = Convert.ToByte('E');
            var F = Convert.ToByte('F');

            var KH_array = new List <byte>();

            KH_array.AddRange(K_size_array);
            KH_array.AddRange(K_array);

            KH_array.AddRange(H_size_array);
            KH_array.AddRange(H_array);

            // vektor k -> s
            forHash.Clear();

            forHash.AddRange(KH_array);

            forHash.Add(A);

            forHash.AddRange(sessionIdent_array);

            using (SHA1Managed sha1 = new SHA1Managed())
            {
                var key_list = sha1.ComputeHash(forHash.ToArray()).ToList();

                /*
                 * If the key length needed is longer than the output of the HASH, the
                 * key is extended by computing HASH of the concatenation of K and H and
                 * the entire key so far, and appending the resulting bytes (as many as
                 * HASH generates) to the key
                 */
                while (key_list.Count < 8)
                {
                    var temp = new List <byte>();
                    temp.AddRange(K_array);
                    temp.AddRange(H_array);
                    temp.AddRange(key_list);
                    key_list.AddRange(sha1.ComputeHash(temp.ToArray()));
                }

                keys.vectorCS = key_list.Take(8).ToArray();
            }

            // vektor s -> k
            forHash.Clear();

            forHash.AddRange(KH_array);

            forHash.Add(B);

            forHash.AddRange(sessionIdent_array);

            using (SHA1Managed sha1 = new SHA1Managed())
            {
                var key_list = sha1.ComputeHash(forHash.ToArray()).ToList();

                /*
                 * If the key length needed is longer than the output of the HASH, the
                 * key is extended by computing HASH of the concatenation of K and H and
                 * the entire key so far, and appending the resulting bytes (as many as
                 * HASH generates) to the key
                 */
                while (key_list.Count < 8)
                {
                    var temp = new List <byte>();
                    temp.AddRange(K_array);
                    temp.AddRange(H_array);
                    temp.AddRange(key_list);
                    key_list.AddRange(sha1.ComputeHash(temp.ToArray()));
                }

                keys.vectorSC = key_list.Take(8).ToArray();
            }

            switch (encryptionAl)
            {
            case "3des-cbc":
            {
                var encryKeys = Generate3DESKeys(K, H, sessionIdentifier);
                keys.cryCS = encryKeys.cryCS;
                keys.crySC = encryKeys.crySC;
                encryptionAlgorithms.encryption = typeof(SSHHelper).GetMethod("Encrypt_Decrypt3DES_CBC");
                break;
            }
            }

            switch (macAl)
            {
            case "hmac-sha1":
            {
                EncryptionKeys encryKeys = GenerateHMAC_SHA1Keys(K, H, sessionIdentifier);
                keys.MACKeyCS            = encryKeys.MACKeyCS;
                keys.MACKeySC            = encryKeys.MACKeySC;
                keys.MAClength           = encryKeys.MAClength;
                encryptionAlgorithms.MAC = typeof(SSHHelper).GetMethod("Compute_hmac_sha1");
                break;
            }

            case "gost28147":
            {
                EncryptionKeys encryKeys = GenerateGOST28147Keys(K, H, sessionIdentifier);
                keys.MACKeyCS            = encryKeys.MACKeyCS;
                keys.MACKeySC            = encryKeys.MACKeySC;
                keys.MAClength           = encryKeys.MAClength;
                encryptionAlgorithms.MAC = typeof(SSHHelper).GetMethod("Compute_gost28147");
                break;
            }
            }

            return(keys);
        }