예제 #1
0
        public void GeneratePublicKeyFromPrivateWithSomeManipulationEnsureNoLongerValidKeyPair()
        {
            var privateKey     = new BitString("4C089A9597865D316B5163A01F85458A0B954CD542B9B2D83E39CB3CBA010441");
            var expectedPublic = new BitString("EBB9897DF6C5E4E42999578ECA0F48B0985FF99032E80244C4679032F1132A24");

            // Gen public key from private
            var subject = new EdDsa(EntropyProviderTypes.Testable);

            subject.AddEntropy(privateKey.ToPositiveBigInteger());

            var factory = new EdwardsCurveFactory();
            var curve   = factory.GetCurve(Curve.Ed25519);

            var domainParams = new EdDomainParameters(curve, new NativeShaFactory());

            var keyPair = subject.GenerateKeyPair(domainParams).KeyPair;

            Assert.AreEqual(expectedPublic, keyPair.PublicQ, "expected public key");
            // Check x/y, record
            var x = new BitString("172B32732D86C9D9D63B11957AAD1364E9D3C1EC258CD13AB012E10648942C4E")
                    .ToPositiveBigInteger();
            var y = new BitString("242A13F1329067C44402E83290F95F98B0480FCA8E579929E4E4C5F67D89B9EB")
                    .ToPositiveBigInteger();

            // encode public key (done by default, and represented in Q
            // decode public key
            var decoded = domainParams.CurveE.Decode(keyPair.PublicQ);

            // check that the decoded x/y match the previous prior to encode/decode
            Assert.AreEqual(x, decoded.X, "x decoded matches");
            Assert.AreEqual(y, decoded.Y, "y decoded matches");

            Assert.IsTrue(subject.ValidateKeyPair(domainParams, keyPair).Success, "original key pair should be valid");

            // Modify the public key value until the point is no longer on the curve
            var modifiedPublicQ = curve.Decode(keyPair.PublicQ);

            var addedX = modifiedPublicQ.X;
            var addedY = modifiedPublicQ.Y;
            var adds   = 0;

            do
            {
                modifiedPublicQ = new EdPoint(modifiedPublicQ.X, modifiedPublicQ.Y + 8);
                addedX          = modifiedPublicQ.X + 41;
                addedY          = modifiedPublicQ.Y + 23;
                keyPair         = new EdKeyPair(curve.Encode(modifiedPublicQ), keyPair.PrivateD.GetDeepCopy());
                adds++;
            } while (subject.ValidateKeyPair(domainParams, keyPair).Success);

            Assert.IsFalse(curve.PointExistsOnCurve(modifiedPublicQ), "check point not on curve prior to encode");
            Assert.IsFalse(subject.ValidateKeyPair(domainParams, keyPair).Success, "keypair should not be valid.");
        }
예제 #2
0
        /*[TestCase(Curve.Ed25519,
         *  "00066499232C0FED45301404782344A374766533B60A", "0004B549A871C577A2EC6B53DE8FBCA532B9AD7E6C61",
         *  false, TestName = "PointExistsOnCurve b-163 #2")]
         * [TestCase(Curve.Ed448,
         *  "00fac9dfcbac8313bb2139f1bb755fef65bc391f8b36f8f8eb7371fd558b", "01006a08a41903350678e58528bebf8a0beff867a7ca36716f7e01f81052",
         *  true, TestName = "PointExistsOnCurve b-233 #1")]
         * [TestCase(Curve.Ed448,
         *  "01DA664638DA59453A816E842DC776C37D7222AF808971FC4993182804E3", "01BB7AADE0594383E663A91582D86DB7F5C5551678BA1A1BBE2F97F42069",
         *  false, TestName = "PointExistsOnCurve b-233 #2")]*/
        #endregion PointsOnCurve
        public void ShouldKnowIfAPointIsOnTheCurve(Curve curve, string xHex, string yHex, bool expectedResult)
        {
            var x = LoadValue(xHex);
            var y = LoadValue(yHex);

            var a       = new EdPoint(x, y);
            var factory = new EdwardsCurveFactory();
            var subject = factory.GetCurve(curve);

            var result = subject.PointExistsOnCurve(a);

            Assert.AreEqual(expectedResult, result);
        }
예제 #3
0
        public void ShouldEncodeAndDecodeProperly(Curve curve, string encoded)
        {
            var encodedOriginal = new BitString(encoded);

            var factory = new EdwardsCurveFactory();
            var subject = factory.GetCurve(curve);

            var decoded = subject.Decode(encodedOriginal);

            Assert.IsTrue(subject.PointExistsOnCurve(decoded));

            var reEncoded = subject.Encode(decoded);

            Assert.AreEqual(encodedOriginal, reEncoded);
        }
예제 #4
0
        public void ShouldValidateKeyPairsCorrectlyWithNewMangleLogic(Curve curveEnum, string dHex, string qHex, bool expectedResult)
        {
            var d = LoadValue(dHex);
            var q = LoadValue(qHex);

            var factory = new EdwardsCurveFactory();
            var curve   = factory.GetCurve(curveEnum);

            var domainParams = new EdDomainParameters(curve, new NativeShaFactory());
            var keyPair      = new EdKeyPair(q, d);

            var subject = new EdDsa();

            var result = subject.ValidateKeyPair(domainParams, keyPair);

            Assert.AreEqual(expectedResult, result.Success);
        }
예제 #5
0
        public void ShouldDoublePointsCorrectly(Curve curve, string xHex, string yHex, string resultXHex, string resultYHex)
        {
            var x       = LoadValue(xHex);
            var y       = LoadValue(yHex);
            var resultx = LoadValue(resultXHex);
            var resulty = LoadValue(resultYHex);

            var a = new EdPoint(x, y);
            var expectedResult = new EdPoint(resultx, resulty);

            var factory = new EdwardsCurveFactory();
            var subject = factory.GetCurve(curve);

            var result = subject.Double(a);

            Assert.AreEqual(expectedResult.X, result.X, "x");
            Assert.AreEqual(expectedResult.Y, result.Y, "y");
        }
예제 #6
0
        public void ShouldMultiplyBasisCorrectly(Curve curve, string axHex, string ayHex, string multipleHex, string resultXHex, string resultYHex)
        {
            var ax       = LoadValue(axHex);
            var ay       = LoadValue(ayHex);
            var multiple = LoadValue(multipleHex);
            var resultx  = LoadValue(resultXHex);
            var resulty  = LoadValue(resultYHex);

            var a = new EdPoint(ax, ay);
            var expectedResult = new EdPoint(resultx, resulty);

            var factory = new EdwardsCurveFactory();
            var subject = factory.GetCurve(curve);

            var result = subject.Multiply(a, multiple);

            Assert.AreEqual(expectedResult.X, result.X, "x");
            Assert.AreEqual(expectedResult.Y, result.Y, "y");
        }
예제 #7
0
        public void ShouldGenerateKeyPairsCorrectly(Curve curveEnum, string dHex, string qHex)
        {
            var d = LoadValue(dHex);
            var q = LoadValue(qHex);

            var factory = new EdwardsCurveFactory();
            var curve   = factory.GetCurve(curveEnum);

            var domainParams = new EdDomainParameters(curve, new NativeShaFactory());

            var subject = new EdDsa(EntropyProviderTypes.Testable);

            subject.AddEntropy(d.ToPositiveBigInteger());

            var result = subject.GenerateKeyPair(domainParams);

            Assert.IsTrue(result.Success);
            Assert.AreEqual(result.KeyPair.PrivateD, d, "d");
            Assert.AreEqual(q, result.KeyPair.PublicQ, "q");
        }
예제 #8
0
        public void ShouldGenerateSignaturesCorrectly(Curve curveEnum, string dHex, string qHex, string msgHex, string sigHex)
        {
            var d           = LoadValue(dHex);
            var q           = LoadValue(qHex);
            var msg         = new BitString(msgHex);
            var expectedSig = LoadValue(sigHex);

            var factory = new EdwardsCurveFactory();
            var curve   = factory.GetCurve(curveEnum);

            var domainParams = new EdDomainParameters(curve, new NativeShaFactory());
            var keyPair      = new EdKeyPair(q, d);

            var subject = new EdDsa(EntropyProviderTypes.Testable);

            var result = subject.Sign(domainParams, keyPair, msg);

            Assert.IsTrue(result.Success);
            Assert.AreEqual(expectedSig, result.Signature.Sig, "sig");
        }
예제 #9
0
        public void ShouldAddTwoPointsCorrectly(Curve curve, string axHex, string ayHex, string bxHex, string byHex, string resultXHex, string resultYHex)
        {
            var ax      = LoadValue(axHex);
            var ay      = LoadValue(ayHex);
            var bx      = LoadValue(bxHex);
            var by      = LoadValue(byHex);
            var resultx = LoadValue(resultXHex);
            var resulty = LoadValue(resultYHex);

            var a = new EdPoint(ax, ay);
            var b = new EdPoint(bx, by);
            var expectedResult = new EdPoint(resultx, resulty);

            var factory = new EdwardsCurveFactory();
            var subject = factory.GetCurve(curve);

            var result = subject.Add(a, b);

            Assert.AreEqual(expectedResult.X, result.X, "x");
            Assert.AreEqual(expectedResult.Y, result.Y, "y");
        }
예제 #10
0
        public void ShouldValidateSignaturesCorrectly(Curve curveEnum, string dHex, string qHex, string msgHex, string sigHex, bool expectedResult)
        {
            var d           = LoadValue(dHex);
            var q           = LoadValue(qHex);
            var msg         = new BitString(msgHex);
            var expectedSig = LoadValue(sigHex);

            var factory = new EdwardsCurveFactory();
            var curve   = factory.GetCurve(curveEnum);

            var domainParams = new EdDomainParameters(curve, new NativeShaFactory());

            var keyPair   = new EdKeyPair(q);
            var signature = new EdSignature(expectedSig);

            var subject = new EdDsa();

            var result = subject.Verify(domainParams, keyPair, msg, signature);

            Assert.AreEqual(expectedResult, result.Success);
        }