📄 gost3410test.java
字号:
+ " got : " + sig[0].toString(16)); } if (!s.equals(sig[1])) { return new SimpleTestResult(false, getName() + ": s component wrong." + System.getProperty("line.separator") + " expecting: " + s.toString(16) + System.getProperty("line.separator") + " got : " + sig[1].toString(16)); } GOST3410.init(false, pair.getPublic()); if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) { return new SimpleTestResult(true, getName() + ": Okay"); } else { return new SimpleTestResult(false, getName() + ": verification fails"); } } } private class GOST3410_CParam implements Test { public String getName() { return "GOST3410-CParam"; } SecureRandom init_random = new SecureRandom() { boolean firstLong = true; public long nextLong() { String x0 = "0x43848744"; String c = "0xB50A826D"; if (firstLong) { firstLong = false; return NumberParsing.decodeLongFromHex(x0); } return NumberParsing.decodeLongFromHex(c); } public void nextBytes(byte[] bytes) { byte[] d = Hex.decode("7F575E8194BC5BDF"); System.arraycopy(d, 0, bytes, bytes.length-d.length, d.length); } }; SecureRandom random = new SecureRandom() { public void nextBytes(byte[] bytes) { byte[] k = Hex.decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A"); int i; for (i = 0; i < (bytes.length - k.length); i += k.length) { System.arraycopy(k, 0, bytes, i, k.length); } if (i > bytes.length) { System.arraycopy(k, 0, bytes, i - k.length, bytes.length - (i - k.length)); } else { System.arraycopy(k, 0, bytes, i, bytes.length - i); } } }; SecureRandom keyRandom = new SecureRandom() { public void nextBytes(byte[] bytes) { byte[] x = Hex.decode("3036314538303830343630454235324435324234314132373832433138443046"); int i; for (i = 0; i < (bytes.length - x.length); i += x.length) { System.arraycopy(x, 0, bytes, i, x.length); } if (i > bytes.length) { System.arraycopy(x, 0, bytes, i - x.length, bytes.length - (i - x.length)); } else { System.arraycopy(x, 0, bytes, i, bytes.length - i); } } }; BigInteger pValue = new BigInteger("9d88e6d7fe3313bd2e745c7cdd2ab9ee4af3c8899e847de74a33783ea68bc30588ba1f738c6aaf8ab350531f1854c3837cc3c860ffd7e2e106c3f63b3d8a4c034ce73942a6c3d585b599cf695ed7a3c4a93b2b947b7157bb1a1c043ab41ec8566c6145e938a611906de0d32e562494569d7e999a0dda5c879bdd91fe124df1e9", 16); BigInteger qValue = new BigInteger("fadd197abd19a1b4653eecf7eca4d6a22b1f7f893b641f901641fbb555354faf", 16); public TestResult perform() { BigInteger r = new BigInteger("4deb95a0b35e7ed7edebe9bef5a0f93739e16b7ff27fe794d989d0c13159cfbc",16); BigInteger s = new BigInteger("e1d0d30345c24cfeb33efde3deee5fbbda78ddc822b719d860cd0ba1fb6bd43b",16); GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator(); pGen.init(1024, 2, init_random); GOST3410Parameters params = pGen.generateParameters(); if (!pValue.equals(params.getP()) || !qValue.equals(params.getQ())) { return new SimpleTestResult(false, getName() + ": p or q wrong"); } GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator(); GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(keyRandom, params); GOST3410KeyGen.init(genParam); AsymmetricCipherKeyPair pair = GOST3410KeyGen.generateKeyPair(); ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random); GOST3410Signer GOST3410 = new GOST3410Signer(); GOST3410.init(true, param); BigInteger[] sig = GOST3410.generateSignature(hashmessage); if (!r.equals(sig[0])) { return new SimpleTestResult(false, getName() + ": r component wrong." + System.getProperty("line.separator") + " expecting: " + r.toString(16) + System.getProperty("line.separator") + " got : " + sig[0].toString(16)); } if (!s.equals(sig[1])) { return new SimpleTestResult(false, getName() + ": s component wrong." + System.getProperty("line.separator") + " expecting: " + s.toString(16) + System.getProperty("line.separator") + " got : " + sig[1].toString(16)); } GOST3410.init(false, pair.getPublic()); if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) { return new SimpleTestResult(true, getName() + ": Okay"); } else { return new SimpleTestResult(false, getName() + ": verification fails"); } } } private class GOST3410_DParam implements Test { public String getName() { return "GOST3410-DParam"; } SecureRandom init_random = new SecureRandom() { boolean firstLong = true; public long nextLong() { String x0 = "0x13DA8B9D"; String c = "0xA0E9DE4B"; if (firstLong) { firstLong = false; return NumberParsing.decodeLongFromHex(x0); } return NumberParsing.decodeLongFromHex(c); } public void nextBytes(byte[] bytes) { byte[] d = Hex.decode("41ab97857f42614355d32db0b1069f109a4da283676c7c53a68185b4"); System.arraycopy(d, 0, bytes, bytes.length-d.length, d.length); } }; SecureRandom random = new SecureRandom() { public void nextBytes(byte[] bytes) { byte[] k = Hex.decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A"); int i; for (i = 0; i < (bytes.length - k.length); i += k.length) { System.arraycopy(k, 0, bytes, i, k.length); } if (i > bytes.length) { System.arraycopy(k, 0, bytes, i - k.length, bytes.length - (i - k.length)); } else { System.arraycopy(k, 0, bytes, i, bytes.length - i); } } }; SecureRandom keyRandom = new SecureRandom() { public void nextBytes(byte[] bytes) { byte[] x = Hex.decode("3036314538303830343630454235324435324234314132373832433138443046"); int i; for (i = 0; i < (bytes.length - x.length); i += x.length) { System.arraycopy(x, 0, bytes, i, x.length); } if (i > bytes.length) { System.arraycopy(x, 0, bytes, i - x.length, bytes.length - (i - x.length)); } else { System.arraycopy(x, 0, bytes, i, bytes.length - i); } } }; BigInteger pValue = new BigInteger("80f102d32b0fd167d069c27a307adad2c466091904dbaa55d5b8cc7026f2f7a1919b890cb652c40e054e1e9306735b43d7b279eddf9102001cd9e1a831fe8a163eed89ab07cf2abe8242ac9dedddbf98d62cddd1ea4f5f15d3a42a6677bdd293b24260c0f27c0f1d15948614d567b66fa902baa11a69ae3bceadbb83e399c9b5", 16); BigInteger qValue = new BigInteger("f0f544c418aac234f683f033511b65c21651a6078bda2d69bb9f732867502149", 16); public TestResult perform() { BigInteger r = new BigInteger("712592d285b792e33b8a9a11e8e6c4f512ddf0042972bbfd1abb0a93e8fc6f54",16); BigInteger s = new BigInteger("2cf26758321258b130d5612111339f09ceb8668241f3482e38baa56529963f07",16); GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator(); pGen.init(1024, 2, init_random); GOST3410Parameters params = pGen.generateParameters(); if (!pValue.equals(params.getP()) || !qValue.equals(params.getQ())) { return new SimpleTestResult(false, getName() + ": p or q wrong"); } GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator(); GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(keyRandom, params); GOST3410KeyGen.init(genParam); AsymmetricCipherKeyPair pair = GOST3410KeyGen.generateKeyPair(); ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random); GOST3410Signer GOST3410 = new GOST3410Signer(); GOST3410.init(true, param); BigInteger[] sig = GOST3410.generateSignature(hashmessage); if (!r.equals(sig[0])) { return new SimpleTestResult(false, getName() + ": r component wrong." + System.getProperty("line.separator") + " expecting: " + r.toString(16) + System.getProperty("line.separator") + " got : " + sig[0].toString(16)); } if (!s.equals(sig[1])) { return new SimpleTestResult(false, getName() + ": s component wrong." + System.getProperty("line.separator") + " expecting: " + s.toString(16) + System.getProperty("line.separator") + " got : " + sig[1].toString(16)); } GOST3410.init(false, pair.getPublic()); if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) { return new SimpleTestResult(true, getName() + ": Okay"); } else { return new SimpleTestResult(false, getName() + ": verification fails"); } } } private class GOST3410_AExParam implements Test { public String getName() { return "GOST3410-AExParam"; } SecureRandom init_random = new SecureRandom() { boolean firstLong = true; public long nextLong() { String x0 = "0xD05E9F14"; String c = "0x46304C5F"; if (firstLong) { firstLong = false; return NumberParsing.decodeLongFromHex(x0); } return NumberParsing.decodeLongFromHex(c); } public void nextBytes(byte[] bytes) { byte[] d = Hex.decode("35ab875399cda33c146ca629660e5a5e5c07714ca326db032dd6751995cdb90a612b9228932d8302704ec24a5def7739c5813d83"); System.arraycopy(d, 0, bytes, bytes.length-d.length, d.length); } }; SecureRandom random = new SecureRandom() { public void nextBytes(byte[] bytes) { byte[] k = Hex.decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A"); int i; for (i = 0; i < (bytes.length - k.length); i += k.length) { System.arraycopy(k, 0, bytes, i, k.length); } if (i > bytes.length) { System.arraycopy(k, 0, bytes, i - k.length, bytes.length - (i - k.length)); } else { System.arraycopy(k, 0, bytes, i, bytes.length - i); } } }; SecureRandom keyRandom = new SecureRandom() { public void nextBytes(byte[] bytes) { byte[] x = Hex.decode("3036314538303830343630454235324435324234314132373832433138443046"); int i; for (i = 0; i < (bytes.length - x.length); i += x.length) { System.arraycopy(x, 0, bytes, i, x.length); } if (i > bytes.length) { System.arraycopy(x, 0, bytes, i - x.length, bytes.length - (i - x.length)); } else { System.arraycopy(x, 0, bytes, i, bytes.length - i); } } }; BigInteger pValue = new BigInteger("ca3b3f2eee9fd46317d49595a9e7518e6c63d8f4eb4d22d10d28af0b8839f079f8289e603b03530784b9bb5a1e76859e4850c670c7b71c0df84ca3e0d6c177fe9f78a9d8433230a883cd82a2b2b5c7a3306980278570cdb79bf01074a69c9623348824b0c53791d53c6a78cab69e1cfb28368611a397f50f541e16db348dbe5f", 16);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -