Exemplo n.º 1
0
        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!");
                }
            }
        }
Exemplo n.º 2
0
        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));
            }
        }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
        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;
                }
            }
        }