📄 gost3410test.java
字号:
BigInteger qValue = new BigInteger("cae4d85f80c147704b0ca48e85fb00a9057aa4acc44668e17f1996d7152690d9", 16); public TestResult perform() { BigInteger r = new BigInteger("90892707282f433398488f19d31ac48523a8e2ded68944e0da91c6895ee7045e",16); BigInteger s = new BigInteger("3be4620ee88f1ee8f9dd63c7d145b7e554839feeca125049118262ea4651e9de",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_BExParam implements Test { public String getName() { return "GOST3410-BExParam"; } SecureRandom init_random = new SecureRandom() { boolean firstLong = true; public long nextLong() { String x0 = "0x7A007804"; String c = "0xD31A4FF7"; if (firstLong) { firstLong = false; return NumberParsing.decodeLongFromHex(x0); } return NumberParsing.decodeLongFromHex(c); } public void nextBytes(byte[] bytes) { byte[] d = Hex.decode("7ec123d161477762838c2bea9dbdf33074af6d41d108a066a1e7a07ab3048de2"); 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("9286dbda91eccfc3060aa5598318e2a639f5ba90a4ca656157b2673fb191cd0589ee05f4cef1bd13508408271458c30851ce7a4ef534742bfb11f4743c8f787b11193ba304c0e6bca25701bf88af1cb9b8fd4711d89f88e32b37d95316541bf1e5dbb4989b3df13659b88c0f97a3c1087b9f2d5317d557dcd4afc6d0a754e279", 16); BigInteger qValue = new BigInteger("c966e9b3b8b7cdd82ff0f83af87036c38f42238ec50a876cd390e43d67b6013f", 16); public TestResult perform() { BigInteger r = new BigInteger("8f79a582513df84dc247bcb624340cc0e5a34c4324a20ce7fe3ab8ff38a9db71",16); BigInteger s = new BigInteger("7508d22fd6cbb45efd438cb875e43f137247088d0f54b29a7c91f68a65b5fa85",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_CExParam implements Test { public String getName() { return "GOST3410-CExParam"; } SecureRandom init_random = new SecureRandom() { boolean firstLong = true; public long nextLong() { String x0 = "0x162AB910"; String c = "0x93F828D3"; if (firstLong) { firstLong = false; return NumberParsing.decodeLongFromHex(x0); } return NumberParsing.decodeLongFromHex(c); } public void nextBytes(byte[] bytes) { byte[] d = Hex.decode("ca82cce78a738bc46f103d53b9bf809745ec845e4f6da462606c51f60ecf302e31204b81"); 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("b194036ace14139d36d64295ae6c50fc4b7d65d8b340711366ca93f383653908ee637be428051d86612670ad7b402c09b820fa77d9da29c8111a8496da6c261a53ed252e4d8a69a20376e6addb3bdcd331749a491a184b8fda6d84c31cf05f9119b5ed35246ea4562d85928ba1136a8d0e5a7e5c764ba8902029a1336c631a1d", 16); BigInteger qValue = new BigInteger("96120477df0f3896628e6f4a88d83c93204c210ff262bccb7dae450355125259", 16); public TestResult perform() { BigInteger r = new BigInteger("169fdb2dc09f690b71332432bfec806042e258fa9a21dafe73c6abfbc71407d9",16); BigInteger s = new BigInteger("9002551808ae40d19f6f31fb67e4563101243cf07cffd5f2f8ff4c537b0c9866",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"); } } } Test tests[] = { new GOST3410_TEST1_512(), new GOST3410_TEST2_512(),// new GOST3410_TEST1_1024(),// new GOST3410_TEST2_1024(),// new GOST3410_AParam(),// new GOST3410_BParam(),// new GOST3410_CParam(),// new GOST3410_DParam(),// new GOST3410_AExParam(),// new GOST3410_BExParam(),// new GOST3410_CExParam() }; public String getName() { return "GOST3410"; } public TestResult perform() { for (int i = 0; i != tests.length; i++) { TestResult result = tests[i].perform(); if (!result.isSuccessful()) { return result; } } return new SimpleTestResult(true, "GOST3410: Okay"); } public static void main( String[] args) { GOST3410Test test = new GOST3410Test(); TestResult result = test.perform(); System.out.println(result); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -