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)); }
public static bool VerifyGMSSSignature(byte[] data, byte[] signature, string pulicKeyFileName) { var publicKey = GetGMSSPublicKeyFromPemString(File.ReadAllText(pulicKeyFileName)); using (GMSSSign sgn = new GMSSSign(GMSSParamSets.FromName(GMSSVersion))) { sgn.Initialize(publicKey); return(sgn.Verify(new MemoryStream(data), signature)); } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); // GMSS/JWT test string testLabel = "MarekTest123"; var keyPairFiles = GMSS.GenerateGMSSKeys(testLabel); JwtPayload jwtPayload = new JwtPayload(); jwtPayload.Issuer = "Marek"; jwtPayload.IssuedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); jwtPayload.NotBefore = jwtPayload.IssuedAt; jwtPayload.Expires = jwtPayload.NotBefore + 3600; jwtPayload.Subject = "MarekTester"; jwtPayload.Audience = "MarekTest"; var jwtEncoded = JsonWebToken.Encode(jwtPayload, keyPairFiles.Item1, JwtAlgorithm.GMSS512); var jwtDecoded = JsonWebToken.Decode(jwtEncoded, keyPairFiles.Item2, "", ""); return; // GMSS TEST TestSign(GMSSParamSets.FromName(GMSSVersion)); return; // RSM/TSM TEST: KeyParams keyParams = null; byte[] test = Encoding.UTF8.GetBytes("ala ma kotka"); var testStream = new MemoryStream(test); using (KeyGenerator kg = new KeyGenerator()) { keyParams = kg.GetKeyParams(192, 32, 32); // for TSM: kg.GetKeyParams(192, 16, 16); // for RSM: kg.GetKeyParams(192, 32, 32); } var testEncrypted = Encrypt(test, keyParams); var testDecrypted = Decrypt(testEncrypted, keyParams); var xxxxx = Encoding.UTF8.GetString(testDecrypted); }
public static byte[] SignWithGMSS(byte[] data, string privateKeyFileName) { var privateKey = GetGMSSPrivateKeyFromPemString(File.ReadAllText(privateKeyFileName)); using (GMSSSign sgn = new GMSSSign(GMSSParamSets.FromName(GMSSVersion))) { sgn.Initialize(privateKey); var signature = sgn.Sign(new MemoryStream(data)); // Updating private key with the next one & storing on disk: privateKey = privateKey.NextKey(); // TODO: If catch exception about that no more signatures, then just erase the priv key file or sth File.WriteAllText(privateKeyFileName, GetPemStringFromGMSSPrivateKey(privateKey)); return(signature); } }
/// <summary> /// Tests the validity of the GMSSSign implementation /// </summary> /// /// <returns>State</returns> public string Run() { try { TestSign(GMSSParamSets.FromName(GMSSParamSets.GMSSParamNames.N2P10)); OnProgress(new TestEventArgs("N33L5 params Passed Key Generation, Sign, and Verify Tests..")); // too slow on debug.. if (!System.Diagnostics.Debugger.IsAttached) { TestSign(GMSSParamSets.FromName(GMSSParamSets.GMSSParamNames.N2P20)); OnProgress(new TestEventArgs("N49L5 params Passed Key Generation, Sign, and Verify Tests..")); TestSign(GMSSParamSets.FromName(GMSSParamSets.GMSSParamNames.N2P40)); OnProgress(new TestEventArgs("N54L5 params Passed Key Generation, Sign, and Verify Tests..")); } return(SUCCESS); } catch (Exception Ex) { string message = Ex.Message == null ? "" : Ex.Message; throw new Exception(FAILURE + message); } }
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(); }