Exemple #1
0
        public void TestECDH()
        {
            byte[] kA = new byte[X448.ScalarSize];
            byte[] kB = new byte[X448.ScalarSize];
            byte[] qA = new byte[X448.PointSize];
            byte[] qB = new byte[X448.PointSize];
            byte[] sA = new byte[X448.PointSize];
            byte[] sB = new byte[X448.PointSize];

            for (int i = 1; i <= 100; ++i)
            {
                // Each party generates an ephemeral private key, ...
                Random.NextBytes(kA);
                Random.NextBytes(kB);

                // ... publishes their public key, ...
                X448.ScalarMultBase(kA, 0, qA, 0);
                X448.ScalarMultBase(kB, 0, qB, 0);

                // ... computes the shared secret, ...
                X448.ScalarMult(kA, 0, qB, 0, sA, 0);
                X448.ScalarMult(kB, 0, qA, 0, sB, 0);

                // ... which is the same for both parties.
                Assert.IsTrue(Arrays.AreEqual(sA, sB), "ECDH #" + i);
            }
        }
Exemple #2
0
 private static void CheckX448Vector(String sk, String su, String se, String text)
 {
     byte[] k = Hex.Decode(sk);
     byte[] u = Hex.Decode(su);
     byte[] r = new byte[56];
     X448.ScalarMult(k, 0, u, 0, r, 0);
     CheckValue(r, text, se);
 }
 public void GenerateSecret(X448PublicKeyParameters publicKey, byte[] buf, int off)
 {
     byte[] encoded = new byte[X448.PointSize];
     publicKey.Encode(encoded, 0);
     if (!X448.CalculateAgreement(data, 0, encoded, 0, buf, off))
     {
         throw new InvalidOperationException("X448 agreement failed");
     }
 }
Exemple #4
0
        private static void CheckX448Vector(string sk, string su, string se, string text)
        {
            byte[] k = Hex.Decode(sk);
            Assert.AreEqual(X448.ScalarSize, k.Length);

            byte[] u = Hex.Decode(su);
            Assert.AreEqual(X448.PointSize, u.Length);

            byte[] r = new byte[X448.PointSize];
            X448.ScalarMult(k, 0, u, 0, r, 0);
            CheckValue(r, text, se);
        }
Exemple #5
0
        public void TestConsistency()
        {
            byte[] u  = new byte[X448.PointSize];       u[0] = 5;
            byte[] k  = new byte[X448.ScalarSize];
            byte[] rF = new byte[X448.PointSize];
            byte[] rV = new byte[X448.PointSize];

            for (int i = 1; i <= 100; ++i)
            {
                Random.NextBytes(k);
                X448.ScalarMultBase(k, 0, rF, 0);
                X448.ScalarMult(k, 0, u, 0, rV, 0);
                Assert.IsTrue(Arrays.AreEqual(rF, rV), "Consistency #" + i);
            }
        }
Exemple #6
0
        private static void CheckECDHVector(string sA, string sAPub, string sB, string sBPub, string sK, string text)
        {
            byte[] a = Hex.Decode(sA);
            byte[] b = Hex.Decode(sB);

            byte[] aPub = new byte[56];
            X448.ScalarMultBase(a, 0, aPub, 0);
            CheckValue(aPub, text, sAPub);

            byte[] bPub = new byte[56];
            X448.ScalarMultBase(b, 0, bPub, 0);
            CheckValue(bPub, text, sBPub);

            byte[] aK = new byte[56];
            X448.ScalarMult(a, 0, bPub, 0, aK, 0);
            CheckValue(aK, text, sK);

            byte[] bK = new byte[56];
            X448.ScalarMult(b, 0, aPub, 0, bK, 0);
            CheckValue(bK, text, sK);
        }
Exemple #7
0
        private static void CheckIterated(int count)
        {
            Assert.AreEqual(X448.PointSize, X448.ScalarSize);

            byte[] k = new byte[X448.PointSize];    k[0] = 5;
            byte[] u = new byte[X448.PointSize];    u[0] = 5;
            byte[] r = new byte[X448.PointSize];

            int iterations = 0;

            while (iterations < count)
            {
                X448.ScalarMult(k, 0, u, 0, r, 0);

                Array.Copy(k, 0, u, 0, X448.PointSize);
                Array.Copy(r, 0, k, 0, X448.PointSize);

                switch (++iterations)
                {
                case 1:
                    CheckValue(k, "Iterated @1",
                               "3f482c8a9f19b01e6c46ee9711d9dc14fd4bf67af30765c2ae2b846a4d23a8cd0db897086239492caf350b51f833868b9bc2b3bca9cf4113");
                    break;

                case 1000:
                    CheckValue(k, "Iterated @1000",
                               "aa3b4749d55b9daf1e5b00288826c467274ce3ebbdd5c17b975e09d4af6c67cf10d087202db88286e2b79fceea3ec353ef54faa26e219f38");
                    break;

                case 1000000:
                    CheckValue(k, "Iterated @1000000",
                               "077f453681caca3693198420bbe515cae0002472519b3e67661a7e89cab94695c8f4bcd66e61b9b9c946da8d524de3d69bd9d9d66b997e37");
                    break;

                default:
                    break;
                }
            }
        }
Exemple #8
0
        private static void CheckECDHVector(string sA, string sAPub, string sB, string sBPub, string sK, string text)
        {
            byte[] a = Hex.Decode(sA);
            Assert.AreEqual(X448.ScalarSize, a.Length);

            byte[] b = Hex.Decode(sB);
            Assert.AreEqual(X448.ScalarSize, b.Length);

            byte[] aPub = new byte[X448.PointSize];
            X448.ScalarMultBase(a, 0, aPub, 0);
            CheckValue(aPub, text, sAPub);

            byte[] bPub = new byte[X448.PointSize];
            X448.ScalarMultBase(b, 0, bPub, 0);
            CheckValue(bPub, text, sBPub);

            byte[] aK = new byte[X448.PointSize];
            X448.ScalarMult(a, 0, bPub, 0, aK, 0);
            CheckValue(aK, text, sK);

            byte[] bK = new byte[X448.PointSize];
            X448.ScalarMult(b, 0, aPub, 0, bK, 0);
            CheckValue(bK, text, sK);
        }
Exemple #9
0
 public void SetUp()
 {
     X448.Precompute();
 }
 public X448PublicKeyParameters GeneratePublicKey()
 {
     byte[] publicKey = new byte[X448.PointSize];
     X448.GeneratePublicKey(data, 0, publicKey, 0);
     return(new X448PublicKeyParameters(publicKey, 0));
 }
 public X448PrivateKeyParameters(SecureRandom random)
     : base(true)
 {
     X448.GeneratePrivateKey(random, data);
 }
Exemple #12
0
 public void GenerateSecret(X448PublicKeyParameters publicKey, byte[] buf, int off)
 {
     byte[] encoded = new byte[X448.PointSize];
     publicKey.Encode(encoded, 0);
     X448.ScalarMult(data, 0, encoded, 0, buf, off);
 }