/**
         * Test encoding with and without point compression.
         *
         * @param p
         *            The point to be encoded and decoded.
         */
        private void implTestEncoding(ECPoint p)
        {
            // Not Point Compression
            ECPoint unCompP;

            // Point compression
            ECPoint compP;

            if (p is FPPoint)
            {
                unCompP = new FPPoint(p.Curve, p.X, p.Y, false);
                compP   = new FPPoint(p.Curve, p.X, p.Y, true);
            }
            else
            {
                unCompP = new F2MPoint(p.Curve, p.X, p.Y, false);
                compP   = new F2MPoint(p.Curve, p.X, p.Y, true);
            }

            byte[]  unCompBarr = unCompP.GetEncoded();
            ECPoint decUnComp  = p.Curve.DecodePoint(unCompBarr);

            Assert.AreEqual(p, decUnComp, "Error decoding uncompressed point");

            byte[]  compBarr = compP.GetEncoded();
            ECPoint decComp  = p.Curve.DecodePoint(compBarr);

            Assert.AreEqual(p, decComp, "Error decoding compressed point");
        }
 /**
  * Creates the points on the curve with literature values.
  */
 internal static void CreatePoints()
 {
     for (var i = 0; i < PointSource.Length / 2; i++)
     {
         var x = new FPFieldElement(Q, new BigInteger(PointSource[2 * i].ToString(CultureInfo.InvariantCulture)));
         var y = new FPFieldElement(Q, new BigInteger(PointSource[2 * i + 1].ToString(CultureInfo.InvariantCulture)));
         P[i] = new FPPoint(Curve, x, y);
     }
 }
        public void TestPointCreationConsistency()
        {
            try
            {
                var bad = new FPPoint(Fp.Curve, new FPFieldElement(
                                          Fp.Q, new BigInteger("12")), null);
                Assert.Fail();
            }
            catch (ArgumentException)
            {
                // Expected
            }

            try
            {
                var bad = new FPPoint(Fp.Curve, null,
                                      new FPFieldElement(Fp.Q, new BigInteger("12")));
                Assert.Fail();
            }
            catch (ArgumentException)
            {
                // Expected
            }

            try
            {
                var bad = new F2MPoint(F2M.Curve, new F2MFieldElement(
                                           F2M.M, F2M.K1, new BigInteger("1011")), null);
                Assert.Fail();
            }
            catch (ArgumentException)
            {
                // Expected
            }

            try
            {
                var bad = new F2MPoint(F2M.Curve, null,
                                       new F2MFieldElement(F2M.M, F2M.K1,
                                                           new BigInteger("1011")));
                Assert.Fail();
            }
            catch (ArgumentException)
            {
                // Expected
            }
        }
示例#4
0
        /*
         * we generate a self signed certificate for the sake of testing - SHA224withECDSA
         */
        private void createECRequest(
            string algorithm,
            DerObjectIdentifier algOid)
        {
            FPCurve curve = new FPCurve(
                new BigInteger("6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151"), // q (or p)
                new BigInteger("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", 16),                      // a
                new BigInteger("0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00", 16));                     // b

            ECDomainParameters spec = new ECDomainParameters(
                curve,
//				curve.DecodePoint(Hex.Decode("02C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66")), // G
                curve.DecodePoint(Hex.Decode("0200C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66")),     // G
                new BigInteger("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409", 16));                 // n

            ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(
                new BigInteger("5769183828869504557786041598510887460263120754767955773309066354712783118202294874205844512909370791582896372147797293913785865682804434049019366394746072023"),                 // d
                spec);

            ECPublicKeyParameters pubKey = new ECPublicKeyParameters(
//				curve.DecodePoint(Hex.Decode("026BFDD2C9278B63C92D6624F151C9D7A822CC75BD983B17D25D74C26740380022D3D8FAF304781E416175EADF4ED6E2B47142D2454A7AC7801DD803CF44A4D1F0AC")), // Q
                curve.DecodePoint(Hex.Decode("02006BFDD2C9278B63C92D6624F151C9D7A822CC75BD983B17D25D74C26740380022D3D8FAF304781E416175EADF4ED6E2B47142D2454A7AC7801DD803CF44A4D1F0AC")),                 // Q
                spec);

//			//
//			// set up the keys
//			//
//			IAsymmetricKeyParameter privKey;
//			IAsymmetricKeyParameter pubKey;
//
//			KeyFactory fact = KeyFactory.getInstance("ECDSA");
//
//			privKey = fact.generatePrivate(privKeySpec);
//			pubKey = fact.generatePublic(pubKeySpec);

            Pkcs10CertificationRequest req = new Pkcs10CertificationRequest(
                algorithm, new X509Name("CN=XXX"), pubKey, null, privKey);

            if (!req.Verify())
            {
                Fail("Failed Verify check EC.");
            }

            req = new Pkcs10CertificationRequest(req.GetEncoded());
            if (!req.Verify())
            {
                Fail("Failed Verify check EC encoded.");
            }

            //
            // try with point compression turned off
            //
//			((ECPointEncoder)pubKey).setPointFormat("UNCOMPRESSED");
            FPPoint q = (FPPoint)pubKey.Q;

            pubKey = new ECPublicKeyParameters(
                pubKey.AlgorithmName,
                new FPPoint(q.Curve, q.X, q.Y, false),
                pubKey.Parameters);

            req = new Pkcs10CertificationRequest(
                algorithm, new X509Name("CN=XXX"), pubKey, null, privKey);
            if (!req.Verify())
            {
                Fail("Failed Verify check EC uncompressed.");
            }

            req = new Pkcs10CertificationRequest(req.GetEncoded());
            if (!req.Verify())
            {
                Fail("Failed Verify check EC uncompressed encoded.");
            }

            if (!req.SignatureAlgorithm.ObjectID.Equals(algOid))
            {
                Fail("ECDSA oid incorrect.");
            }

            if (req.SignatureAlgorithm.Parameters != null)
            {
                Fail("ECDSA parameters incorrect.");
            }

            ISigner sig = SignerUtilities.GetSigner(algorithm);

            sig.Init(false, pubKey);

            byte[] b = req.GetCertificationRequestInfo().GetEncoded();
            sig.BlockUpdate(b, 0, b.Length);

            if (!sig.VerifySignature(req.Signature.GetBytes()))
            {
                Fail("signature not mapped correctly.");
            }
        }