Пример #1
0
        public void TestEd448Consistency()
        {
            byte[] sk   = new byte[Ed448.SecretKeySize];
            byte[] pk   = new byte[Ed448.PublicKeySize];
            byte[] ctx  = new byte[Random.NextInt() & 7];
            byte[] m    = new byte[255];
            byte[] sig1 = new byte[Ed448.SignatureSize];
            byte[] sig2 = new byte[Ed448.SignatureSize];

            Random.NextBytes(ctx);
            Random.NextBytes(m);

            for (int i = 0; i < 10; ++i)
            {
                Random.NextBytes(sk);
                Ed448.GeneratePublicKey(sk, 0, pk, 0);

                int mLen = Random.NextInt() & 255;

                Ed448.Sign(sk, 0, ctx, m, 0, mLen, sig1, 0);
                Ed448.Sign(sk, 0, pk, 0, ctx, m, 0, mLen, sig2, 0);

                Assert.IsTrue(Arrays.AreEqual(sig1, sig2), "Ed448 consistent signatures #" + i);

                bool shouldVerify = Ed448.Verify(sig1, 0, pk, 0, ctx, m, 0, mLen);

                Assert.IsTrue(shouldVerify, "Ed448 consistent sign/verify #" + i);

                sig1[Ed448.PublicKeySize - 1] ^= 0x80;
                bool shouldNotVerify = Ed448.Verify(sig1, 0, pk, 0, ctx, m, 0, mLen);

                Assert.IsFalse(shouldNotVerify, "Ed448 consistent verification failure #" + i);
            }
        }
Пример #2
0
        private static void CheckEd448Vector(string sSK, string sPK, string sM, string sCTX, string sSig, string text)
        {
            byte[] sk = Hex.Decode(sSK);
            byte[] pk = Hex.Decode(sPK);

            byte[] pkGen = new byte[Ed448.PublicKeySize];
            Ed448.GeneratePublicKey(sk, 0, pkGen, 0);
            Assert.IsTrue(Arrays.AreEqual(pk, pkGen), text);

            byte[] m      = Hex.Decode(sM);
            byte[] ctx    = Hex.Decode(sCTX);
            byte[] sig    = Hex.Decode(sSig);
            byte[] sigGen = new byte[Ed448.SignatureSize];

            byte[] badsig = Arrays.Clone(sig);
            badsig[Ed448.SignatureSize - 1] ^= 0x80;

            Ed448.Sign(sk, 0, ctx, m, 0, m.Length, sigGen, 0);
            Assert.IsTrue(Arrays.AreEqual(sig, sigGen), text);

            Ed448.Sign(sk, 0, pk, 0, ctx, m, 0, m.Length, sigGen, 0);
            Assert.IsTrue(Arrays.AreEqual(sig, sigGen), text);

            bool shouldVerify = Ed448.Verify(sig, 0, pk, 0, ctx, m, 0, m.Length);

            Assert.IsTrue(shouldVerify, text);

            bool shouldNotVerify = Ed448.Verify(badsig, 0, pk, 0, ctx, m, 0, m.Length);

            Assert.IsFalse(shouldNotVerify, text);
        }
Пример #3
0
        public void Sign(Ed448.Algorithm algorithm, byte[] ctx, byte[] msg, int msgOff, int msgLen,
                         byte[] sig, int sigOff)
        {
            Ed448PublicKeyParameters publicKey = GeneratePublicKey();

            byte[] pk = new byte[Ed448.PublicKeySize];
            publicKey.Encode(pk, 0);

            switch (algorithm)
            {
            case Ed448.Algorithm.Ed448:
            {
                Ed448.Sign(data, 0, pk, 0, ctx, msg, msgOff, msgLen, sig, sigOff);
                break;
            }

            case Ed448.Algorithm.Ed448ph:
            {
                if (Ed448.PrehashSize != msgLen)
                {
                    throw new ArgumentException("msgLen");
                }

                Ed448.SignPrehash(data, 0, pk, 0, ctx, msg, msgOff, sig, sigOff);
                break;
            }

            default:
            {
                throw new ArgumentException("algorithm");
            }
            }
        }