diff options
author | David Hook <david.hook@keyfactor.com> | 2022-11-08 12:03:20 +1100 |
---|---|---|
committer | David Hook <david.hook@keyfactor.com> | 2022-11-08 12:03:20 +1100 |
commit | ca0885f6900a239067f317e6605059db3ebd892f (patch) | |
tree | 8743088c2302b40fb6a71873c8555536ecd60ba2 /crypto/test | |
parent | Merge remote-tracking branch 'refs/remotes/origin/master' (diff) | |
download | BouncyCastle.NET-ed25519-ca0885f6900a239067f317e6605059db3ebd892f.tar.xz |
added raw encoding for Dilithium keys
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs b/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs index e1121a5e3..2b74c44d4 100644 --- a/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs +++ b/crypto/test/src/pqc/crypto/test/CrystalsDilithiumTest.cs @@ -1,8 +1,9 @@ +using System; using System.Collections.Generic; using System.IO; using NUnit.Framework; - +using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium; using Org.BouncyCastle.Pqc.Crypto.Utilities; @@ -53,6 +54,58 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests RunTestVectorFile(testVectorFile); } + [Test] + public void TestKeyEncodingDilithium2() + { + byte[] altEncKey = Base64.Decode("oED4ZvqhK4kChPclP5VhOPvvnwof3QeTFq/zR5UtSr6SYicBkpe1TkafnSk5whDHgEp8imVwfeP2LbSopHb5KQuUFstGqsdII9o8Zf+50yempJ1+C5G64KfQlqkI8/Y6cShWWNgeWm/5OGyxhF0hYVGIwaTTs6AN3QaPjSutLPNdZ82Vd4BSNB5dzZMaoYV619rNO7HGlfwrWkyCoDVQftpgw4NjiAnJ0BPcDJTzPVD3cq3Dw5f14AwNsUijLZ4CuRzzCBBx0B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AaWLoKUIS2ZucI1/ouGTJI9g6wWNUwJaNSUkdC057KUeCAOHk="); + + AsymmetricKeyParameter altPubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(altSubPubEnc)); + Assert.AreEqual(altEncKey, ((DilithiumPublicKeyParameters)altPubDec).GetEncoded()); + + Security.SecureRandom random = new Security.SecureRandom(); + DilithiumKeyGenerationParameters kparam = new DilithiumKeyGenerationParameters(random, DilithiumParameters.Dilithium2); + DilithiumKeyPairGenerator kpg = new DilithiumKeyPairGenerator(); + 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)); + + Console.Out.WriteLine(Base64.ToBase64String(((DilithiumPublicKeyParameters)pub).GetEncoded())); + Console.Out.WriteLine(Base64.ToBase64String(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub).GetEncoded())); + Assert.AreEqual(((DilithiumPublicKeyParameters)pub).GetEncoded(), ((DilithiumPublicKeyParameters)pubDec).GetEncoded()); + Assert.AreEqual(((DilithiumPrivateKeyParameters)priv).GetEncoded(), ((DilithiumPrivateKeyParameters)privDec).GetEncoded()); + } + + [Test] + public void TestKeyEncodingDilithium2Aes() + { + byte[] altEncKey = Base64.Decode("CKJBWPvTOpeLU/2y0fhlWLc9HKYgH2pAARm2IzwF927Nnnugbw4T7cmjtSfbD/e4CamjVdehDBPqYBWfbZumLD+EuPQUqWaCYDS9JcezFmFXACFHRQPwWDqwqvfuy7zeN4HVb/ygiNJI/mWNoqRn3Ffz59J2iPLr9xe8TUNgQ6VK3tVwIqkxAXv1GVX5MCJI0U2v7d00RnfEDnYL2AVZj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Sr/AqUT2JBO6rdE6OWSt/ibdXFRkRv/f7XASwqwVsFkBgnNBmioZLSOdIYw12ZcvotlFtNKfi2BMe6lj2ZY2DqA/rnnekaIPh2dBIrevzWQgLStqrn/2g59F2saBH6bg="); + + AsymmetricKeyParameter altPubDec = PublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(altSubPubEnc)); + Assert.AreEqual(altEncKey, ((DilithiumPublicKeyParameters)altPubDec).GetEncoded()); + + Security.SecureRandom random = new Security.SecureRandom(); + DilithiumKeyGenerationParameters kparam = new DilithiumKeyGenerationParameters(random, DilithiumParameters.Dilithium2Aes); + DilithiumKeyPairGenerator kpg = new DilithiumKeyPairGenerator(); + 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(((DilithiumPublicKeyParameters)pub).GetEncoded(), ((DilithiumPublicKeyParameters)pubDec).GetEncoded()); + Assert.AreEqual(((DilithiumPrivateKeyParameters)priv).GetEncoded(), ((DilithiumPrivateKeyParameters)privDec).GetEncoded()); + } + private static void RunTestVector(string name, IDictionary<string, string> buf) { string count = buf["count"]; |