Ejemplo n.º 1
0
        private static void TestSig(string sigAlg)
        {
            byte[] public_key;
            byte[] secret_key;
            byte[] signature;
            byte[] message = new byte[100];
            Random random  = new Random();

            random.NextBytes(message);
            log("message: " + BytesToHex(message));

            // successful case
            Sig sig = new Sig(sigAlg);

            if (sigAlg != "DEFAULT")
            {
                Assert.AreEqual(sig.AlgorithmName, sigAlg);
            }
            Assert.IsTrue(sig.IsUsable, "IsUsable after constructor");

            sig.keypair(out public_key, out secret_key);
            log("public_key: " + BytesToHex(public_key));
            log("secret_key: " + BytesToHex(secret_key));
            Assert.IsTrue((UInt64)public_key.Length <= sig.PublicKeyLength, "public key length");
            Assert.IsTrue((UInt64)secret_key.Length <= sig.SecretKeyLength, "secret key length");

            sig.sign(out signature, message, secret_key);
            log("signature: " + BytesToHex(signature));
            Assert.IsTrue((UInt64)signature.Length <= sig.SignatureLength, "signature length");
            Assert.IsTrue(sig.verify(message, signature, public_key), "signature verification");

            // failure cases

            // wrong message
            byte[] wrong_message = new byte[100];
            random.NextBytes(wrong_message);
            log("wrong_message: " + BytesToHex(wrong_message));
            Assert.IsFalse(sig.verify(wrong_message, signature, public_key), "wrong message, verification should have failed");

            // wrong signature
            byte[] wrong_signature = new byte[signature.Length];
            random.NextBytes(wrong_signature);
            log("wrong_signature: " + BytesToHex(wrong_signature));
            Assert.IsFalse(sig.verify(message, wrong_signature, public_key), "wrong signature, verification should have failed");

            // wrong public key
            byte[] wrong_public_key = new byte[public_key.Length];
            random.NextBytes(wrong_public_key);
            log("wrong_public_key: " + BytesToHex(wrong_public_key));
            Assert.IsFalse(sig.verify(message, signature, wrong_public_key), "wrong public key, verification should have failed");

            // clean-up
            sig.Dispose();
            Assert.IsFalse(sig.IsUsable, "IsUsable after cleanup");
        }
 public async Task <bool> VerifySignature([FromBody] VerifySignature message)
 {
     using (var verifier = new Sig(message.SigMechanism))
     {
         byte[] messageBytes = new System.Text.UTF8Encoding().GetBytes(message.Message);
         return(verifier.verify(messageBytes, message.Signature, message.PublicKey));
     }
 }
Ejemplo n.º 3
0
        static void example_sig()
        {
            Console.WriteLine("Starting signature example");

            // get the list of enabled signature mechanisms
            Console.WriteLine("Enabled signature mechanisms: ");
            foreach (string alg in Sig.EnabledMechanisms)
            {
                Console.WriteLine(" - " + alg);
            }

            // Instantiate the signer and verifier
            using (Sig signer = new Sig("Dilithium2"),
                   verifier = new Sig("Dilithium2"))
            {
                Console.WriteLine("Sign with Dilithium2 mechanism");

                // Print out some info about the mechanism
                Console.WriteLine("Mechanism details:");
                Console.WriteLine(" - Alg name: " + signer.AlgorithmName);
                Console.WriteLine(" - Alg version: " + signer.AlgorithmVersion);
                Console.WriteLine(" - Claimed NIST level: " + signer.ClaimedNistLevel);
                Console.WriteLine(" - Is EUF_CMA?: " + signer.IsEufCma);
                Console.WriteLine(" - Secret key length: " + signer.SecretKeyLength);
                Console.WriteLine(" - Public key length: " + signer.PublicKeyLength);
                Console.WriteLine(" - Signature length: " + signer.SignatureLength);

                // The message to sign
                byte[] message = new System.Text.UTF8Encoding().GetBytes("Message to sign");

                // Generate the signer's key pair
                byte[] public_key;
                byte[] secret_key;
                signer.keypair(out public_key, out secret_key);

                // The signer sends the public key to the verifier

                // The signer signs the message
                byte[] signature;
                signer.sign(out signature, message, secret_key);

                // The signer sends the signature to the verifier

                // The verifier verifies the signature
                if (verifier.verify(message, signature, public_key))
                {
                    Console.WriteLine("Signature verification succeeded");
                }
                else
                {
                    Console.WriteLine("Signature verification failed");
                }
                Console.WriteLine();
            }
        }