Exemplo n.º 1
0
        static public void Print(Formatter formatter, string curveName)
        {
            CurveNames curveEnum = GetCurveName(curveName);
            ECParams   ecp       = ECRecommendedParameters.ecParams[(int)curveEnum];

            ecp.Oid = ECRecommendedParameters.OID[(int)curveEnum];
            formatter.PrintText("OID", ecp.Oid);
            formatter.PrintBigInteger("p", "UCHAR", null, (ecp.parameters.Curve as FpCurve).Q);
            formatter.PrintBigInteger("a", "UCHAR", null, ecp.parameters.Curve.A.ToBigInteger());
            formatter.PrintBigInteger("b", "UCHAR", null, ecp.parameters.Curve.B.ToBigInteger());
            formatter.PrintBigInteger("n", "UCHAR", null, ecp.parameters.N);
            formatter.PrintBigInteger("h", "UCHAR", null, ecp.parameters.H);
            formatter.PrintPoint("g", "UCHAR", null, ecp.parameters.G as FpPoint);

            // g_1 ... g_n
            for (int i = 1; i <= NumberOfPregeneratedGenerators; i++)
            {
                formatter.PrintPoint("g" + i, "UCHAR", null, ecp.g_i[i - 1], ecp.counter[i - 1]);
            }

            // g_t
            formatter.PrintPoint("gt", "UCHAR", null, ecp.g_t, ecp.counter_t);

            // g_d
            formatter.PrintPoint("gd", "UCHAR", null, ecp.g_d, ecp.counter_d);
        }
        public static BigInteger FIPS_186_3_AnnexA_2_3(BigInteger p, BigInteger q, byte[] domain_parameter_seed, byte index, Formatter formater, out int count)
        {
            string hashAlg = null;

            if (q.BitLength >= 512)
            {
                hashAlg = "SHA-512";
            }
            else if (q.BitLength >= 256)
            {
                hashAlg = "SHA-256";
            }
            else if (q.BitLength >= 160)
            {
                hashAlg = "SHA1";
            }
            else
            {
                throw new ArgumentException("q is too small");
            }
            HashAlgorithm hash = HashAlgorithm.Create(hashAlg);

            BigInteger e = p.Subtract(BigInteger.One).Divide(q);

            if (formater != null)
            {
                formater.PrintBigInteger("vr_e", "UCHAR", null, e);
            }
            count = 1;

            while (count != 0)
            {
                int    uIndex = 0;
                byte[] U      = new byte[domain_parameter_seed.Length + ggen.Length + 2];
                Array.Copy(domain_parameter_seed, 0, U, uIndex, domain_parameter_seed.Length);
                uIndex += domain_parameter_seed.Length;
                Array.Copy(ggen, 0, U, uIndex, ggen.Length);
                uIndex         += ggen.Length;
                U[U.Length - 2] = index;
                U[U.Length - 1] = (byte)count;

                BigInteger W = new BigInteger(1, hash.ComputeHash(U));
                BigInteger g = W.ModPow(e, p);
                if (g.CompareTo(BigInteger.Two) >= 0)
                {
                    if (formater != null)
                    {
                        formater.PrintHex("vr_U", "UCHAR", null, U);
                        formater.PrintBigInteger("vr_W", "UCHAR", null, W);
                    }
                    return(g);
                }

                count++;
            }

            throw new Exception("Can't generate generator; max count reached");
        }
Exemplo n.º 3
0
        public static FpPoint GetRandomPoint(string input, FpCurve curve, int index, Formatter formater, out int finalCounter)
        {
            int            counter = 0;
            ECFieldElement x = null, y = null, z = null;

            while (y == null)
            {
                x = GetX(input, curve, index, counter);
                z = x.Multiply(x.Square().Add(curve.A)).Add(curve.B);
                if (z.ToBigInteger() == BigInteger.Zero)
                {
                    y = z;
                }
                else
                {
                    y = z.Sqrt(); // returns null if sqrt does not exist
                }
                counter++;
            }
            finalCounter = counter - 1;
            if (formater != null)
            {
                formater.PrintBigInteger("vr_z", "UCAHR", null, z.ToBigInteger());
            }
            ECFieldElement yPrime = y.Negate();

            return(new FpPoint(curve, x, y.ToBigInteger().CompareTo(yPrime.ToBigInteger()) < 0 ? y : yPrime));
        }
        internal static void Print(Formatter formatter, string groupName)
        {
            SGNames  sgNameEnum = GetSGName(groupName);
            SGParams sgp        = SubgroupRecommendedParameters.sgParams[(int)sgNameEnum];

            sgp.Oid = SubgroupRecommendedParameters.OID[(int)sgNameEnum];
            formatter.PrintText("OID", "UCHAR", null, sgp.Oid);
            formatter.PrintBigInteger("p", "UCHAR", null, sgp.p);
            formatter.PrintBigInteger("q", "UCHAR", null, sgp.q);
            formatter.PrintBigInteger("g", "UCHAR", null, sgp.g);
            formatter.PrintHex("domainParamSeed", "UCHAR", null, sgp.domain_parameter_seed);
            formatter.PrintBigInteger("e", "UCHAR", null, sgp.e);

            // g_1 ... g_n
            for (int i = 1; i <= NumberOfPregeneratedGenerators; i++)
            {
                formatter.PrintBigInteger("g" + i, "UCHAR", null, sgp.g_i[i - 1], sgp.counter[i - 1]);
            }

            // g_t
            formatter.PrintBigInteger("gt", "UCHAR", null, sgp.g_t, sgp.counter_t);

            // g_d
            formatter.PrintBigInteger("gd", "UCHAR", null, sgp.g_d, sgp.counter_d);
        }