private static GMSSParamSets.GMSSParamNames GMSSVersion = GMSSParamSets.GMSSParamNames.N2P10; //.N2P10SHA512; public static Tuple <string, string> GenerateGMSSKeys(string keyFileLabel, string workDirPath = null, bool reuseExistingFiles = true) { var privKeyFileName = GetPrivateKeyFileNameForLabel(keyFileLabel); var pubKeyFileName = GetPublicKeyFileNameForLabel(keyFileLabel); if (!String.IsNullOrEmpty(workDirPath)) { privKeyFileName = Path.Combine(workDirPath, privKeyFileName); pubKeyFileName = Path.Combine(workDirPath, pubKeyFileName); } if (!reuseExistingFiles || !File.Exists(privKeyFileName) || !File.Exists(pubKeyFileName)) { GMSSKeyGenerator mkgen = new GMSSKeyGenerator(GMSSParamSets.FromName(GMSSVersion)); IAsymmetricKeyPair akp = mkgen.GenerateKeyPair(); var privKeyPem = GetPemStringFromGMSSPrivateKey((GMSSPrivateKey)akp.PrivateKey); var pubKeyPem = GetPemStringFromGMSSPublicKey((GMSSPublicKey)akp.PublicKey); File.WriteAllText(privKeyFileName, privKeyPem); File.WriteAllText(pubKeyFileName, pubKeyPem); } return(new Tuple <string, string>(privKeyFileName, pubKeyFileName)); }
private void TestSign(GMSSParameters CipherParam) { GMSSKeyGenerator mkgen = new GMSSKeyGenerator(CipherParam); IAsymmetricKeyPair akp = mkgen.GenerateKeyPair(); byte[] data = new byte[200]; new VTDev.Libraries.CEXEngine.Crypto.Prng.CSPPrng().GetBytes(data); using (GMSSSign sgn = new GMSSSign(CipherParam)) { // sign the array sgn.Initialize(akp.PrivateKey); byte[] code = sgn.Sign(data, 0, data.Length); // verify the signature sgn.Initialize(akp.PublicKey); if (!sgn.Verify(data, 0, data.Length, code)) { throw new Exception("RLWESignTest: Sign operation failed!"); } // get the next available key (private sub-key is used only once) GMSSPrivateKey nk = ((GMSSPrivateKey)akp.PrivateKey).NextKey(); sgn.Initialize(nk); code = sgn.Sign(new MemoryStream(data)); // verify the signature sgn.Initialize(akp.PublicKey); if (!sgn.Verify(new MemoryStream(data), code)) { throw new Exception("RLWESignTest: Verify test failed!"); } } }
private static void TestSign(GMSSParameters CipherParam) { GMSSKeyGenerator mkgen = new GMSSKeyGenerator(CipherParam); IAsymmetricKeyPair akp = mkgen.GenerateKeyPair(); byte[] data = new byte[200]; new VTDev.Libraries.CEXEngine.Crypto.Prng.CSPPrng().GetBytes(data); // public key serialization test: var pubKeySerialized = akp.PublicKey.ToBytes(); var pubKeyExported = $"-----BEGIN GMSS PUBLIC KEY-----{Environment.NewLine}{Convert.ToBase64String(pubKeySerialized, Base64FormattingOptions.InsertLineBreaks)}{Environment.NewLine}-----END GMSS PUBLIC KEY-----{Environment.NewLine}"; File.WriteAllText("TestPublicKey.txt", pubKeyExported); var pubKeyImported = File.ReadAllText("TestPublicKey.txt").Replace($"-----BEGIN GMSS PUBLIC KEY-----{Environment.NewLine}", "").Replace($"-----END GMSS PUBLIC KEY-----{Environment.NewLine}", ""); var pubKey = GMSSPublicKey.From(Convert.FromBase64String(pubKeyImported)); var currentPrivKey = ((GMSSPrivateKey)akp.PrivateKey); //.NextKey(); for (int i = 0; i < 10; i++) //2000; i++) { try { //var test = JsonConvert.SerializeObject(akp); using (GMSSSign sgn = new GMSSSign(CipherParam)) { //////// sign the array //////sgn.Initialize(akp.PrivateKey); //////byte[] code = sgn.Sign(data, 0, data.Length); //////// verify the signature //////sgn.Initialize(akp.PublicKey); //////if (!sgn.Verify(data, 0, data.Length, code)) ////// throw new Exception("RLWESignTest: Sign operation failed!"); //if (i == 15) try { //var test1 = JsonConvert.SerializeObject(currentPrivKey); //var keyBytes = currentPrivKey.ToBytes(); //var currentPrivKeyCopy = currentPrivKey.DeepCopy(); // private key serialization test //if (i == 19) // currentPrivKey.DebugGetTreehashes("before"); var privKeySerialized = currentPrivKey.ToBytes(); var privKeyExported = $"-----BEGIN GMSS.{GMSSVersion} PRIVATE KEY-----{Environment.NewLine}{Convert.ToBase64String(privKeySerialized, Base64FormattingOptions.InsertLineBreaks)}{Environment.NewLine}-----END GMSS PRIVATE KEY-----{Environment.NewLine}"; File.WriteAllText("TestPrivateKey.txt", privKeyExported); var privKeyImported = File.ReadAllText("TestPrivateKey.txt").Replace($"-----BEGIN GMSS.{GMSSVersion} PRIVATE KEY-----{Environment.NewLine}", "").Replace($"-----END GMSS PRIVATE KEY-----{Environment.NewLine}", ""); var currentPrivKeyRegen = GMSSPrivateKey.From(Convert.FromBase64String(privKeyImported)); //if (i == 19) { using (SHA512Managed sha = new SHA512Managed()) { var test1 = Convert.ToBase64String(sha.ComputeHash(currentPrivKey.ToBytes())); var test2 = Convert.ToBase64String(sha.ComputeHash(currentPrivKeyRegen.ToBytes())); var iAmI = i; if (test1 != test2) { //////GMSSPrivateKey.DEBUG_HIT_NOW = true; var test1b = ByteArrayToString(currentPrivKey.ToBytes()); var test2b = ByteArrayToString(currentPrivKeyRegen.ToBytes()); } else { } } } // currentPrivKey.DebugGetTreehashes("after"); //var xxx = 1; currentPrivKey = currentPrivKeyRegen; //var testXXX = currentPrivKey.NextKey(); //var test2 = JsonConvert.SerializeObject(currentPrivKeyCopy); //var test3 = JsonConvert.SerializeObject(currentPrivKey); //if(test1 != test2 || test2 != test3) //{ //} //currentPrivKey = new GMSSPrivateKey() //var test1 = currentPrivKey.IsUsed; //using (SHA256Managed sha = new SHA256Managed()) //{ // var currentPrivKeyHash = Convert.ToBase64String(sha.ComputeHash(keyBytes)); //} } catch (Exception ex) { throw ex; } //////var test1 = currentPrivKey.ToBytes(); sgn.Initialize(currentPrivKey); var code = sgn.Sign(new MemoryStream(data)); File.WriteAllText("TestSignature.txt", Convert.ToBase64String(code)); // verify the signature sgn.Initialize(pubKey); if (!sgn.Verify(new MemoryStream(data), code)) { throw new Exception("RLWESignTest: Verify test failed!"); } try { // get the next available key (private sub-key is used only once) //////GMSSPrivateKey nk = ((GMSSPrivateKey)akp.PrivateKey).NextKey(); currentPrivKey = currentPrivKey.NextKey(); // ((GMSSPrivateKey)akp.PrivateKey).NextKey(); // currentPrivKey.NextKey(); } catch (Exception ex) { throw ex; } } } catch (Exception ex) { throw ex; } } }
private void TestEncode() { GMSSParameters mpar = GMSSParamSets.FromName(GMSSParamSets.GMSSParamNames.N2P10); GMSSKeyGenerator mkgen = new GMSSKeyGenerator(mpar); IAsymmetricKeyPair akp = mkgen.GenerateKeyPair(); GMSSPublicKey pub = (GMSSPublicKey)akp.PublicKey; byte[] enc = pub.ToBytes(); using (GMSSPublicKey pub2 = GMSSPublicKey.From(enc)) { if (!pub.Equals(pub2)) { throw new Exception("EncryptionKey: public key comparison test failed!"); } if (pub.GetHashCode() != pub2.GetHashCode()) { throw new Exception("EncryptionKey: public key hash test failed!"); } } OnProgress(new TestEventArgs("Passed public key serialization")); MemoryStream pubstr = pub.ToStream(); using (GMSSPublicKey pub2 = GMSSPublicKey.From(pubstr)) { if (!pub.Equals(pub2)) { throw new Exception("EncryptionKey: public key comparison test failed!"); } if (pub.GetHashCode() != pub2.GetHashCode()) { throw new Exception("EncryptionKey: public key hash test failed!"); } } pubstr.Dispose(); OnProgress(new TestEventArgs("Passed public key stream test")); GMSSPrivateKey pri = (GMSSPrivateKey)akp.PrivateKey; enc = pri.ToBytes(); using (GMSSPrivateKey pri2 = GMSSPrivateKey.From(enc)) { if (!pri.Equals(pri2)) { throw new Exception("EncryptionKey: private key comparison test failed!"); } if (pri.GetHashCode() != pri2.GetHashCode()) { throw new Exception("EncryptionKey: private key hash test failed!"); } } OnProgress(new TestEventArgs("Passed private key serialization")); MemoryStream pristr = pri.ToStream(); using (GMSSPrivateKey pri2 = GMSSPrivateKey.From(pristr)) { if (!pri.Equals(pri2)) { throw new Exception("EncryptionKey: private key comparison test failed!"); } if (pri.GetHashCode() != pri2.GetHashCode()) { throw new Exception("EncryptionKey: private key hash test failed!"); } } pristr.Dispose(); OnProgress(new TestEventArgs("Passed private key stream test")); pri.Dispose(); pub.Dispose(); }