Ejemplo n.º 1
0
        public void x9_62_tests()
        {
            Random.Seed("string to make the random number generator think it has entropy");

            Console.WriteLine("some tests from X9.62");

            x9_62_test_internal(Objects.NID.X9_62_prime192v1,
                                "3342403536405981729393488334694600415596881826869351677613",
                                "5735822328888155254683894997897571951568553642892029982342");
            x9_62_test_internal(Objects.NID.X9_62_prime239v1,
                                "308636143175167811492622547300668018854959378758531778147462058306432176",
                                "323813553209797357708078776831250505931891051755007842781978505179448783");
            x9_62_test_internal(Objects.NID.X9_62_c2tnb191v1,
                                "87194383164871543355722284926904419997237591535066528048",
                                "308992691965804947361541664549085895292153777025772063598");
            x9_62_test_internal(Objects.NID.X9_62_c2tnb239v1,
                                "21596333210419611985018340039034612628818151486841789642455876922391552",
                                "197030374000731686738334997654997227052849804072198819102649413465737174");
        }
Ejemplo n.º 2
0
        public void test_builtin()
        {
            /* fill digest values with some random data */
            byte[] digest       = Random.PseudoBytes(20);
            byte[] wrong_digest = Random.PseudoBytes(20);

            /* create and verify a ecdsa signature with every availble curve
             * (with ) */
            Console.WriteLine("testing ECDSA_sign() and ECDSA_verify() with some internal curves:");

            /* get a list of all internal curves */
            BuiltinCurve[] curves = BuiltinCurve.Get();

            /* now create and verify a signature for every curve */
            foreach (BuiltinCurve curve in curves)
            {
                if (curve.Object.NID == Objects.NID.ipsec4.NID)
                {
                    continue;
                }

                /* create new ecdsa key (== EC_KEY) */
                using (Key eckey = new Key())
                {
                    using (Group group = Group.FromCurveName(curve.Object))
                    {
                        eckey.Group = group;
                    }

                    if (eckey.Group.Degree < 160)
                    {
                        /* drop the curve */
                        continue;
                    }

                    Console.Write("{0}: ", curve.Object.ShortName);

                    /* create key */
                    eckey.GenerateKey();

                    /* create second key */
                    using (Key wrong_eckey = new Key())
                    {
                        using (Group group = Group.FromCurveName(curve.Object))
                        {
                            wrong_eckey.Group = group;
                        }

                        wrong_eckey.GenerateKey();
                        Console.Write(".");

                        /* check key */
                        Assert.IsTrue(eckey.CheckKey());
                        Console.Write(".");

                        /* create signature */
                        byte[] signature = new byte[eckey.Size];
                        var    sigSize   = eckey.Sign(0, digest, signature);
                        Array.Resize(ref signature, (int)sigSize);
                        Console.Write(".");

                        /* verify signature */
                        var verifyResult = eckey.Verify(0, digest, signature);
                        Assert.IsTrue(verifyResult);
                        Console.Write(".");

                        /* verify signature with the wrong key */
                        Assert.IsFalse(wrong_eckey.Verify(0, digest, signature));
                        Console.Write(".");

                        /* wrong digest */
                        Assert.IsFalse(eckey.Verify(0, wrong_digest, signature));
                        Console.Write(".");

                        Console.WriteLine(" ok");
                    }
                }
            }
        }