Beispiel #1
0
        private static byte[] GostGammaMod(int len, byte[] inData)
        {
            byte[] result;
            var    dt = new byte[64 * 5];

            BytesBuilder.CopyTo(inData, dt);
            var g = new Gost28147Modified();

            g.prepareGamma(dt, new byte[160], Gost28147Modified.CryptoProA, Gost28147Modified.CryptoProC, Gost28147Modified.Sbox_Default, Gost28147Modified.ESbox_A, Gost28147Modified.ESbox_B, Gost28147Modified.ESbox_C);
            result = g.getGamma(len, 40);
            return(result);
        }
Beispiel #2
0
        public override void ProcessCryptoVariant(long variant)
        {
            var g   = new Gost28147Modified();
            var key = prepareKey(variant);
            var s   = prepareOIV(variant);

            g.prepareGamma(key, s, Gost28147Modified.CryptoProB, Gost28147Modified.CryptoProC, Gost28147Modified.CryptoProA, Gost28147Modified.CryptoProD, Gost28147Modified.ESbox_A, Gost28147Modified.ESbox_B, false);
            var gamma = g.getGamma(1024 * 128, 12);   // или 13

            if (gamma[0] == 1)
            {
                if ((variant & 1) > 0)
                {
                    c1f++;
                }
                else
                {
                    c1t++;
                }
            }

            calcResult(gamma, variant, key);
        }
Beispiel #3
0
        /*
         *              gg.prepareGamma(key, s, Gost28147Modified.CryptoProA, Gost28147Modified.CryptoProB, Gost28147Modified.CryptoProC, Gost28147Modified.CryptoProD, Gost28147Modified.ESbox_A, Gost28147Modified.ESbox_B);
         *              var gamma = gg.getGamma(GC + 1, true);*/
/*
 *                      gs.prepareGamma(key, s);
 *                      var gamma = gs.getGamma(GC + 1);
 */
        //var gamma = gg.getGOSTGamma(key, s, Gost28147Modified.ESbox_B, 8);

        /*gg.prepareGamma(key, s, Gost28147Modified.CryptoProA, Gost28147Modified.CryptoProB, Gost28147Modified.CryptoProC, Gost28147Modified.CryptoProD, Gost28147Modified.ESbox_A, Gost28147Modified.ESbox_B);
         * var gamma = gg.getGamma(512, true);*/

        /*gs.prepareGamma(key, s);
         * var gamma = gs.getGamma();*/

        //var gamma = gg.getGOSTGamma(key, s, Gost28147Modified.ESbox_B, 8);
        //var gamma = gs.getDerivatoKey(key, s, hashCount, ref pc, 512, regime);


        /*gg.prepareGamma(key, s, Gost28147Modified.CryptoProA, Gost28147Modified.CryptoProB, Gost28147Modified.CryptoProC, Gost28147Modified.CryptoProD, Gost28147Modified.ESbox_A, Gost28147Modified.ESbox_B);
         * var gamma = gg.getGamma(512, true);*/
        /*
         *                gs.prepareGamma(key, s);
         *                var gamma = gs.getGamma();
         * */
        /*
         *                  var gamma = gg.getGOSTGamma(key, s, Gost28147Modified.ESbox_B, 8);
         * /*
         *                  byte[] gamma = new byte[72];
         *                  SHA3.getMultiHash20(key, out gamma, ref pc, hashCount, gs);
         */
        /*
         * var lk = gs.GetKeysFromKeyData(key, s, hashCount, 20, ref pc);
         * var ls = gs.GetOIVectorsFromKeyData(lk.Count, s, hashCount);
         * var gamma = gs.getMACHashMod(s, lk, ls, pc, hashCount);
         */


        private static void testPermutation(object sync, int MC, int pc, int hashCount, int GC)
        {
            if (MC > 14)
            {
                throw new ArgumentOutOfRangeException("MC", "MC must be <= 14");
            }
#if !fillKey1
            tgmgSuccess = 1;
#else
            tgmgSuccess = 4;
#endif
            tgmgResultInt = 0;


#if AGOST || AD
            var SL = 32;
#elif AGOSTM
            var SL = 160;
#elif AGOSTM2 || AGOSTM2E
            var SL = 128;
#elif APH
            var SL = 256;
#else
            var SL = 64;
#endif

            for (int counter = 0; counter < tgmgSuccess; counter++)
            {
                int CK = 0;
                if (counter == 1)
                {
                    CK = SL - 4;
                }
                else
                if (counter == 2)
                {
                    CK = SL >> 1;
                }
                else
                if (counter == 3)
                {
                    CK = (SL >> 1) + (SL >> 2);
                }

                int counterA = counter;

                ThreadPool.QueueUserWorkItem
                (
                    delegate
                {
                    var error = 0;

                    int Max  = 1 << (MC + 2);
                    var key  = new byte[SL];
                    var s    = new byte[64];
                    var stat = new int[2, 16, 256, GC];
                    var stab = new int[2, 2, 16, 16, GC];

                    var gg = new Gost28147Modified();
                    var gs = new SHA3(0);
                    for (int i = 0; i < Max; i++)
                    {
                        fillKey(CK, i, ref key);

                        //byte[] gamma = MergePermutation.getGamma(key, 512, out t);

#if AGOSTM
                        gg.prepareGamma(key, s, Gost28147Modified.CryptoProA, Gost28147Modified.CryptoProB, Gost28147Modified.CryptoProC, Gost28147Modified.CryptoProD, Gost28147Modified.ESbox_A, Gost28147Modified.ESbox_B);
                        var gamma = gg.getGamma(GC + 1, 33 - 21);
#elif AGOSTM2
                        gg.prepareGamma(key, s, Gost28147Modified.CryptoProA, Gost28147Modified.CryptoProB, null, null, null, null, false);
                        var gamma = gg.getGamma(GC + 1, false);
#elif AGOSTM2E
                        gg.prepareGamma(key, s, Gost28147Modified.CryptoProA, Gost28147Modified.CryptoProB, null, null, null, null, true);
                        var gamma = gg.getGamma(GC + 1, 33 - 21);
#elif AGOST
                        var gamma = gg.getGOSTGamma(key, s, Gost28147Modified.ESbox_B, GC);
#elif ASHA3
                        gs.prepareGamma(key, s);
                        var gamma = gs.getGamma(GC);
#elif AExHash
                        var gamma = SHA3.getExHash(4, s, key);
#elif AMH20
                        byte[] gamma = new byte[GC];
                        SHA3.getMultiHash20(key, out gamma, ref pc, hashCount, gs);
#elif AMACHASHMOD
                        var lk    = gs.GetKeysFromKeyData(key, s, hashCount, 20, ref pc);
                        var ls    = gs.GetOIVectorsFromKeyData(lk.Count, s, hashCount);
                        var gamma = gs.getMACHashMod(s, lk, ls, pc, hashCount);
#elif ARC4
                        var RC4   = new RC4(key);
                        var gamma = RC4.getGamma(GC);
#elif ARC4Plus
                        var RC4   = new RC4(key);
                        var gamma = RC4.getGamma2(GC);
#elif AMD5
                        var md5   = System.Security.Cryptography.MD5.Create();
                        var gamma = md5.ComputeHash(key);
#elif APH
                        var gamma = MergePermutation.getHash(key, GC);
#elif AD
                        var gamma = SHA3.generateRandomPwdByDerivatoKey(key, GC);
#elif ADerivato
                        var gamma = gs.getDerivatoKey(key, /*new UTF8Encoding().GetBytes("passwordJjVjRyjcWwo7761qPBQUb3Sx8DACNpassword")*/ Convert.FromBase64String("dm1gZG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r/KcUCkfWY/CkkorBaCLrD9Wo0WTy2O3wCfJrcLLT8Rdkl4nk6B3FncfdBWfRyjMpocbxaVieCO5sFnCibTZIuYxxVRsd+NBBTBXifrVSkrYrHBidWgnnyMvNuRyTNodLO1AWEcX9QK2l2xlJPlvAFkQhYAv4ef+XDttRf09MjmJP44k7tIolOV3gGTFGgRToKXk5nlSXvXy8q0R42YOjhvzvOQ7AeYoI/bQ38p9cK3qu/5Di4Spjqn+B+oBoXai74LgQEmA5BdgyVC/hfdhfv47AclCoHbXIWZ+HDIUpRtQzIY9YoHtZaic/ern68niLVe0sOzeX7c2LrUsBIf6QhwZIlU5GSuoSl5m+zCGh6p3xflwd/gNd08QlIgmbE4G6aOqYB09uDjGho7VfY3Gy8qjzMjCzTSdeRc/ahbreVZUBGMqhS2TgdBoJu5u1+gnY5B320b44Ni61RFn1fKCaHMbYCJ4xusy8VzkcXmJFPqgY+0unSM3GXg5RQUx0DhOb+sdy7/7ocf+TpmOJFYSQYPsqsuqrfj27xDWiKjfRV/YyRC2BOsdItZn"),
                                                      22 >= 20 ? 16 : 1024, ref pc, GC, 22 / 10);
#elif A20
                        var gamma = new byte[GC];
                        gamma     = gs.multiCryptLZMA(gamma, key, s, 20, false, 19, 4);
                        gamma     = BytesBuilder.CloneBytes(gamma, 4 + s.Length, GC + 4 + s.Length);
#elif A22
                        var gamma = new byte[GC];
                        gamma     = gs.multiCryptLZMA(gamma, key, s, 22, false, 19, 4);
                        var L     = gamma.Length - s.Length - 4;
                        gamma     = BytesBuilder.CloneBytes(gamma, gamma.Length - L, L);
#elif ARC4Mod
                        var RC4   = new RC4Mod(key);
                        var gamma = RC4.getGamma(GC);
#elif Perm
                        int slen  = 256;
                        var gamma = MergePermutation.getGamma(key, GC, out slen);
#else
                        byte[] gamma = new byte[GC];
                        for (int gc = 0; gc < GC; gc++)
                        {
                            if ((i & 512) > 0)
                            {
                                gamma[gc] = (byte)i;
                            }
                            else
                            {
                                gamma[gc] = (byte)(i ^ 0xFFFF);
                            }
                        }
#endif

                        for (int gc = 0; gc < GC; gc++)
                        {
                            int C = 1;
                            for (int j = 0; j < MC; j++, C <<= 1)
                            {
                                stat[(i & C) > 0 ? 1 : 0, j, gamma[gc], gc]++;
                            }

                            C = 1;
                            for (int j = 0; j < MC; j++, C <<= 1)
                            {
                                int C2 = 1;
                                for (int j2 = 0; j2 < 8; j2++, C2 <<= 1)
                                {
                                    stab[(gamma[gc] & C2) > 0 ? 1 : 0, (i & C) > 0 ? 1 : 0, j, j2, gc]++;
                                }
                            }
                        }
                    }

                    int max  = 0;
                    int min  = int.MaxValue;
                    int summ = 0;

                    int max2  = 0;
                    int min2  = int.MaxValue;
                    int summ2 = 0;

                    for (int k01 = 0; k01 <= 1; k01++)
                    {
                        for (int gc = 0; gc < GC; gc++)
                        {
                            for (int i = 0; i < MC; i++)
                            {
                                for (int j = 0; j < 256; j++)
                                {
                                    if (stat[k01, i, j, gc] > max)
                                    {
                                        max = stat[k01, i, j, gc];
                                    }

                                    if (stat[k01, i, j, gc] < min)
                                    {
                                        min = stat[k01, i, j, gc];
                                    }

                                    summ += stat[k01, i, j, gc];
                                }

                                for (int ib = 0; ib < 2; ib++)
                                {
                                    for (int j2 = 0; j2 < 8; j2++)
                                    {
                                        if (stab[k01, ib, i, j2, gc] > max2)
                                        {
                                            max2 = stab[k01, ib, i, j2, gc];
                                        }

                                        if (stab[k01, ib, i, j2, gc] < min2)
                                        {
                                            min2 = stab[k01, ib, i, j2, gc];
                                        }

                                        summ2 += stab[k01, ib, i, j2, gc];
                                    }
                                }
                            }
                        }
                    }

                    /*summ  /= Max*MC;
                     * summ2 /= Max*MC*GC;
                     * */
                    float ms1 = Max * GC * MC / (2f * GC * MC * 256f);
                    float ms2 = Max * GC * MC * 8f / (2f * GC * MC * 2f * 8f);

                    if (GC < 2)
                    {
                        if ((float)max / (float)min >= 4.7)
                        {
                            error++;
                        }
                        if ((float)max / ms1 >= 1.8)
                        {
                            error++;
                        }
                        if (ms1 / (float)min >= 2.7)
                        {
                            error++;
                        }

                        if ((float)max2 / (float)min2 >= 1.07)
                        {
                            error++;
                        }
                        if ((float)max2 / ms2 >= 1.04)
                        {
                            error++;
                        }
                        if ((float)ms2 / (float)min2 >= 1.04)
                        {
                            error++;
                        }
                    }
                    else
                    if (GC < 73)
                    {
                        if ((float)max / (float)min >= 8.89)
                        {
                            error++;
                        }
                        if ((float)max / ms1 >= 2.0)
                        {
                            error++;
                        }
                        if (ms1 / (float)min >= 4.58)
                        {
                            error++;
                        }

                        if ((float)max2 / (float)min2 >= 1.11)
                        {
                            error++;
                        }
                        if ((float)max2 / ms2 >= 1.06)
                        {
                            error++;
                        }
                        if ((float)ms2 / (float)min2 >= 1.06)
                        {
                            error++;
                        }
                    }
                    else
                    {
                        if ((float)max / (float)min >= 11.51)
                        {
                            error++;
                        }
                        if ((float)max / ms1 >= 2.19)
                        {
                            error++;
                        }
                        if (ms1 / (float)min >= 5.34)
                        {
                            error++;
                        }

                        if ((float)max2 / (float)min2 >= 1.13)
                        {
                            error++;
                        }
                        if ((float)max2 / ms2 >= 1.07)
                        {
                            error++;
                        }
                        if ((float)ms2 / (float)min2 >= 1.07)
                        {
                            error++;
                        }
                    }

                    // GOST     9,29 2,03 4,57 1,11 1,05 1,06
                    //                                                         // fillKey1
                    // GOSTM    7,11 2,00 3,56 1,11 1,05 1,05
                    //
                    // SHA3     10,8 2,03 5,33 1,10 1,05 1,05
                    //          7,88 1,97 4,00 1,10 1,05 1,05
                    //          9,29 2,03 4,57 1,11 1,05 1,06
                    //          7,67 2,16 3,56 1,12 1,06 1,06
                    //
                    // MH20     6,20 1,94 3,20 1,09 1,04 1,04
                    //          7,63 1,91 4,00 1,09 1,04 1,05
                    //          6,30 1,97 3,20 1,09 1,04 1,04
                    // MACHASHMOD
                    //
                    //
                    //  RC4     8.11 2.28 3.56 1.10 1.05 1.05
                    //          1500
                    //  RC4+    --
                    //          44

                    lock (sync)
                    {
                        if (error == 0)
                        {
                            Console.WriteLine("success ({0}) EC 0 {1:F}\t{2:F}\t{3:F}\t{4:F}\t{5:F}\t{6:F}", counterA, ((float)max / (float)min), ((float)max / (float)ms1), ((float)ms1 / (float)min), ((float)max2 / (float)min2), ((float)max2 / (float)ms2), ((float)ms2 / (float)min2));
                        }
                        else
                        {
                            Console.WriteLine("FAILED ({0}) EC {7} {1:F}\t{2:F}\t{3:F}\t{4:F}\t{5:F}\t{6:F}", counterA, ((float)max / (float)min), ((float)max / (float)ms1), ((float)ms1 / (float)min), ((float)max2 / (float)min2), ((float)max2 / (float)ms2), ((float)ms2 / (float)min2), error);
                            tgmgResultInt++;
                        }

                        tgmgSuccess--;
                        Monitor.Pulse(sync);

                        System.Windows.Media.MediaPlayer player = new System.Windows.Media.MediaPlayer();
                        player.Open(new Uri("Resources/siren.wav", UriKind.Relative));
                        player.Position = new TimeSpan(0);
                        player.Play();
                    }
                }
                );
            }
        }