diff options
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/src/pqc/crypto/test/FalconTest.cs | 76 |
1 files changed, 62 insertions, 14 deletions
diff --git a/crypto/test/src/pqc/crypto/test/FalconTest.cs b/crypto/test/src/pqc/crypto/test/FalconTest.cs index c7421de6c..093f8e6a8 100644 --- a/crypto/test/src/pqc/crypto/test/FalconTest.cs +++ b/crypto/test/src/pqc/crypto/test/FalconTest.cs @@ -3,10 +3,11 @@ using System.Collections.Generic; using System.IO; using NUnit.Framework; - +using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Pqc.Crypto.Falcon; +using Org.BouncyCastle.Pqc.Crypto.Utilities; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; @@ -31,6 +32,56 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests Assert.AreEqual(10, FalconParameters.falcon_1024.LogN); } + [Test] + public void TestKeyEncoding512() + { + byte[] altEncKey = Base64.Decode("vsUwFR+onoGQjgYmVpVBEacknmQpGTqsW+kNYw4VntPxVoRVLdsl2U24SzIAgrCZgwBAOnVK57arZT+5nF1afZZB5DzjapIKfPBhLlGmnQ6RX2j/WBaEoMAX7l46ZSb1GgycmZMgUFWvsYLEZr5D4zxOhfSkkzJ38NCzuOUNiFgYQ4TRtwxGwDM8WVV0vpL0nkLVnQJ7B0ZRBz++dXetoI651kjWW91GB3mj+x6U9SYx/KTF4zyejiEF2wVxKzgum13RiivF6cbBHLBeQLGBqRbgk5VasoRaswUc8NBywckHUBgmhilYUv0scpREs3m+IgjcYB2JmryzGNU1p6aM6b/sOrzJffLLm38mca6ZQpoNih03a+8QeIlAn8ns4XJOZOzg0J0ZsUhQ2Zdmj4BASFREAnma1LYH2k/rOf5qpfZUpYzuDhPJ43y5zLeUrt6kKErgGo4KYJBl4/JvWdlnFA8ROEbTY3qXEAHnkRzxsBOFX8midN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b4iCNxgHYmavLMY1TWnpozpv+w6vMl98subfyZxrplCmg2KHTdr7xB4iUCfyezhck5k7ODQnRmxSFDZl2aPgEBIVEQCeZrUtgfaT+s5/mql9lSljO4OE8njfLnMt5Su3qQoSuAajgpgkGXj8m9Z2WcUDxE4RtNjepcQAeeRHPGwE4VfyaJ011FUgOdFIlNnLVE0YWRwI6ukBp3QdFVsIC8rqlPWDKA4iW07pqnXriVC7fmRIko/9rTFgUmCTbn5ZIrkNlOGNVsDXYJvRYjlBGB9ozb5BmSjx0PCxturTkww8tzLbkcYdupIZYgktYXIZvQyKy9JSccacpYvjHkkU/ZVtIC0vNt1EJOD6apwCQLmfW7Elx1kDpF5nduK5XRGcvARCFo+amba1smXjD5QCeLiZ6M5uJGjxUY1SKQrUvhKKDoOMws43aey8gRryIMBoQlRtMurLSI9Z+Xjm/0eNjboVKkdWCXQYB8EA7R5qAE45HNVfuJ6/S6oDYQYKtkIB0DlC9RNYrVFIDc7eFJcWbj7YDiOpqKebGF862e44InGFl9umRDsFYcsumXgHgt13J5fPa6DgpPMZ7K6CsOd5kKpn1Myn1RFPKoHGZCaRjHcuwZAvjAxgwDpD3aIhD7aNMEFCaRM0OVkrCNw6iLV4tI7Brcz+OGqI0DUcqwNaYpigWDmps9NgQmgNmogM3o8k/iP76NBaVhbgwuExdUUCWRalDIdZwxFz2SkS9F36EdJiXkbwNrdVIqxaCjOgxvtpBu3BDGHTOFmAskYG9gjJGQtrDDRbnAswk2Djfz5c5UCDQ6XGT7ihVfpeOmLV0iKxMy6a6R/M4ULAgaBWyZYxUGK3jCgPt9UE0TmDfw7ax0B8w=="); + + AsymmetricKeyParameter altPubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(altSubPubEnc)); + Assert.AreEqual(altEncKey, ((FalconPublicKeyParameters)altPubDec).GetEncoded()); + + Security.SecureRandom random = new Security.SecureRandom(); + FalconKeyGenerationParameters kparam = new FalconKeyGenerationParameters(random, FalconParameters.falcon_512); + FalconKeyPairGenerator kpg = new FalconKeyPairGenerator(); + kpg.Init(kparam); + AsymmetricCipherKeyPair ackp = kpg.GenerateKeyPair(); + + AsymmetricKeyParameter pub = ackp.Public; + AsymmetricKeyParameter priv = ackp.Private; + + AsymmetricKeyParameter pubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub)); + AsymmetricKeyParameter privDec = PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(priv)); + + Assert.AreEqual(((FalconPublicKeyParameters)pub).GetEncoded(), ((FalconPublicKeyParameters)pubDec).GetEncoded()); + Assert.AreEqual(((FalconPrivateKeyParameters)priv).GetEncoded(), ((FalconPrivateKeyParameters)privDec).GetEncoded()); + } + + [Test] + public void TestKeyEncoding1024() + { + byte[] altEncKey = Base64.Decode(" dzEWmS1QASD+RCFcozO0ZaxX6qSYAAiSO3AfWTQMHLX6GbwR7Ui4XZfuS1k5wWhg3oH1hoBo3r09NX1Tnt3YauWDVW1fy2rw58XW0Q5I9mrixm3qpDJgwrknl71t5NMoX7IRHtLiGM0PHuH8uqUisyiMdzjoCoxovT9wkI/lmoYYaD+GdyiY5pKPgsoYjY9q9ZWjloUiRThp7xAhJYvf1MO58GwzTFxpIjkz1UqsanwOa/dxJjREvmGjv9xVh4S1FD6KS4rCh1ftYdl3ucmgmCuZJmBkVysaeZGMfJZTa4u0xRhLaYS+UPz2qFZTxw12G0oCrGqDwk2w+2mUH0GDuZnnw1bArJeh0VscNg7nCBD1KzzHOh/hDnS67iR9T+NFkj5WLkxiRJaZ0BIPm1iBvhre2vlLoHH5woocGbLdST1H90Nh8braUbpItVe64w7SlLbd53IbcyRaVfR2FpNmtRD2M4kwcbiNEDahuXSOBAjJJyB8pKpZ/xylk/BzBhvp2gvMk+boq+8pKWUCK+gzMZivdmEIo5UkDMWuHfUomgIVDicQcXLx2oDE1bcxiYSUa30lAK4Zj5v8pfTHIa7fAEYALyoNkUEvrsNSgRoiD93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AreEqual(altEncKey, ((FalconPublicKeyParameters)altPubDec).GetEncoded()); + + Security.SecureRandom random = new Security.SecureRandom(); + FalconKeyGenerationParameters kparam = new FalconKeyGenerationParameters(random, FalconParameters.falcon_1024); + FalconKeyPairGenerator kpg = new FalconKeyPairGenerator(); + kpg.Init(kparam); + AsymmetricCipherKeyPair ackp = kpg.GenerateKeyPair(); + + AsymmetricKeyParameter pub = ackp.Public; + AsymmetricKeyParameter priv = ackp.Private; + + AsymmetricKeyParameter pubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub)); + AsymmetricKeyParameter privDec = PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(priv)); + + Assert.AreEqual(((FalconPublicKeyParameters)pub).GetEncoded(), ((FalconPublicKeyParameters)pubDec).GetEncoded()); + Assert.AreEqual(((FalconPrivateKeyParameters)priv).GetEncoded(), ((FalconPrivateKeyParameters)privDec).GetEncoded()); + } + [TestCaseSource(nameof(TestVectorFiles))] [Parallelizable(ParallelScope.All)] public void TV(string testVectorFile) @@ -69,25 +120,22 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests ParametersWithRandom skwrand = new ParametersWithRandom(ackp.Private, random); signer.Init(true, skwrand); byte[] sig = signer.GenerateSignature(msg); - byte[] ressm = new byte[2 + msg.Length + sig.Length - 1]; - ressm[0] = (byte)((sig.Length - 40 - 1) >> 8); - ressm[1] = (byte)(sig.Length - 40 - 1); + byte[] ressm = new byte[2 + msg.Length + sig.Length]; + ressm[0] = (byte)((sig.Length - 40) >> 8); + ressm[1] = (byte)(sig.Length - 40); Array.Copy(sig, 1, ressm, 2, 40); Array.Copy(msg, 0, ressm, 2 + 40, msg.Length); - Array.Copy(sig, 40 + 1, ressm, 2 + 40 + msg.Length, sig.Length - 40 - 1); - + ressm[2 + 40 + msg.Length] = (byte)(0x20 + kparam.Parameters.LogN); + Array.Copy(sig, 40 + 1, ressm, 3 + 40 + msg.Length, sig.Length - 40 - 1); + // verify FalconSigner verifier = new FalconSigner(); FalconPublicKeyParameters pkparam = (FalconPublicKeyParameters)ackp.Public; verifier.Init(false, pkparam); - byte[] noncesig = new byte[sm_len - m_len - 2 + 1]; - noncesig[0] = (byte)(0x30 + falconParameters.LogN); - Array.Copy(sm, 2, noncesig, 1, 40); - Array.Copy(sm, 2 + 40 + m_len, noncesig, 40 + 1, sm_len - 2 - 40 - m_len); - bool vrfyrespass = verifier.VerifySignature(msg, noncesig); - noncesig[42]++; // changing the signature by 1 byte should cause it to fail - bool vrfyresfail = verifier.VerifySignature(msg, noncesig); - + bool vrfyrespass = verifier.VerifySignature(msg, sig); + sig[42]++; // changing the signature by 1 byte should cause it to fail + bool vrfyresfail = verifier.VerifySignature(msg, sig); + //sign Assert.True(Arrays.AreEqual(ressm, sm), name + " " + count + " signature"); //verify |