📄 gost3410test.java
字号:
package org.bouncycastle.crypto.test;import org.bouncycastle.crypto.AsymmetricCipherKeyPair;import org.bouncycastle.crypto.generators.GOST3410KeyPairGenerator;import org.bouncycastle.crypto.generators.GOST3410ParametersGenerator;import org.bouncycastle.crypto.params.GOST3410KeyGenerationParameters;import org.bouncycastle.crypto.params.GOST3410Parameters;import org.bouncycastle.crypto.params.ParametersWithRandom;import org.bouncycastle.crypto.signers.GOST3410Signer;import org.bouncycastle.util.encoders.Hex;import org.bouncycastle.util.test.FixedSecureRandom;import org.bouncycastle.util.test.NumberParsing;import org.bouncycastle.util.test.SimpleTestResult;import org.bouncycastle.util.test.Test;import org.bouncycastle.util.test.TestResult;import java.math.BigInteger;import java.security.SecureRandom;public class GOST3410Test implements Test{ byte[] hashmessage = Hex.decode("3042453136414534424341374533364339313734453431443642453241453435"); private byte[] zeroTwo(int length) { byte[] data = new byte[length]; data[data.length - 1] = 0x02; return data; } private class GOST3410_TEST1_512 implements Test { public String getName() { return "GOST3410-TEST1-512"; } FixedSecureRandom init_random = new FixedSecureRandom(new byte[][] { Hex.decode("00005EC900007341"), zeroTwo(64) }); FixedSecureRandom random = new FixedSecureRandom(Hex.decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A")); FixedSecureRandom keyRandom = new FixedSecureRandom(Hex.decode("3036314538303830343630454235324435324234314132373832433138443046")); BigInteger pValue = new BigInteger("EE8172AE8996608FB69359B89EB82A69854510E2977A4D63BC97322CE5DC3386EA0A12B343E9190F23177539845839786BB0C345D165976EF2195EC9B1C379E3", 16); BigInteger qValue = new BigInteger("98915E7EC8265EDFCDA31E88F24809DDB064BDC7285DD50D7289F0AC6F49DD2D", 16); public TestResult perform() { BigInteger r = new BigInteger("3e5f895e276d81d2d52c0763270a458157b784c57abdbd807bc44fd43a32ac06",16); BigInteger s = new BigInteger("3f0dd5d4400d47c08e4ce505ff7434b6dbf729592e37c74856dab85115a60955",16); GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator(); pGen.init(512, 1, init_random); GOST3410Parameters params = pGen.generateParameters(); if (params.getValidationParameters() == null) { return new SimpleTestResult(false, getName() + "validation parameters wrong"); } if (params.getValidationParameters().getC() != 29505 || params.getValidationParameters().getX0() != 24265) { return new SimpleTestResult(false, getName() + "validation parameters values wrong"); } if (!init_random.isExhausted()) { return new SimpleTestResult(false, getName() + ": unexpected number of bytes used from 'init_random'."); } 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(); if (!keyRandom.isExhausted()) { return new SimpleTestResult(false, getName() + ": unexpected number of bytes used from 'keyRandom'."); } ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random); GOST3410Signer gost3410 = new GOST3410Signer(); gost3410.init(true, param); BigInteger[] sig = gost3410.generateSignature(hashmessage); if (!random.isExhausted()) { return new SimpleTestResult(false, getName() + ": unexpected number of bytes used from 'random'."); } 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_TEST2_512 implements Test { public String getName() { return "GOST3410-TEST2-512"; } FixedSecureRandom init_random = new FixedSecureRandom(new byte[][] { Hex.decode("000000003DFC46F1000000000000000D"), zeroTwo(64) }); FixedSecureRandom random = new FixedSecureRandom(Hex.decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A")); FixedSecureRandom keyRandom = new FixedSecureRandom(Hex.decode("3036314538303830343630454235324435324234314132373832433138443046")); BigInteger pValue = new BigInteger("8b08eb135af966aab39df294538580c7da26765d6d38d30cf1c06aae0d1228c3316a0e29198460fad2b19dc381c15c888c6dfd0fc2c565abb0bf1faff9518f85", 16); BigInteger qValue = new BigInteger("931a58fb6f0dcdf2fe7549bc3f19f4724b56898f7f921a076601edb18c93dc75", 16); public TestResult perform() { BigInteger r = new BigInteger("7c07c8cf035c2a1cb2b7fae5807ac7cd623dfca7a1a68f6d858317822f1ea00d",16); BigInteger s = new BigInteger("7e9e036a6ff87dbf9b004818252b1f6fc310bdd4d17cb8c37d9c36c7884de60c",16); GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator(); pGen.init(512, 2, init_random); GOST3410Parameters params = pGen.generateParameters(); if (!init_random.isExhausted()) { return new SimpleTestResult(false, getName() + ": unexpected number of bytes used from 'init_random'."); } if (params.getValidationParameters() == null) { return new SimpleTestResult(false, getName() + ": validation parameters wrong"); } if (params.getValidationParameters().getCL() != 13 || params.getValidationParameters().getX0L() != 1039943409) { return new SimpleTestResult(false, getName() + ": validation parameters values wrong"); } 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(); if (!keyRandom.isExhausted()) { return new SimpleTestResult(false, getName() + ": unexpected number of bytes used from 'keyRandom'."); } ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), random); GOST3410Signer GOST3410 = new GOST3410Signer(); GOST3410.init(true, param); BigInteger[] sig = GOST3410.generateSignature(hashmessage); if (!random.isExhausted()) { return new SimpleTestResult(false, getName() + ": unexpected number of bytes used from 'random'."); } 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_TEST1_1024 implements Test { public String getName() { return "GOST3410-TEST1-1024"; } SecureRandom init_random = new SecureRandom() { boolean firstInt = true; public int nextInt() { String x0 = "0xA565"; String c = "0x538B"; if (firstInt) { firstInt = false; return NumberParsing.decodeIntFromHex(x0); } return NumberParsing.decodeIntFromHex(c); } public void nextBytes(byte[] bytes) { byte[] d = Hex.decode("02"); 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("ab8f37938356529e871514c1f48c5cbce77b2f4fc9a2673ac2c1653da8984090c0ac73775159a26bef59909d4c9846631270e16653a6234668f2a52a01a39b921490e694c0f104b58d2e14970fccb478f98d01e975a1028b9536d912de5236d2dd2fc396b77153594d4178780e5f16f718471e2111c8ce64a7d7e196fa57142d", 16); BigInteger qValue = new BigInteger("bcc02ca0ce4f0753ec16105ee5d530aa00d39f3171842ab2c334a26b5f576e0f", 16); public TestResult perform() { BigInteger r = new BigInteger("a8790aabbd5a998ff524bad048ac69cd1faff2dab048265c8d60d1471c44a9ee",16); BigInteger s = new BigInteger("30df5ba32ac77170b9632559bef7d37620017756dff3fea1088b4267db0944b8",16); GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator(); pGen.init(1024, 1, 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_TEST2_1024 implements Test { public String getName() { return "GOST3410-TEST2-1024"; } SecureRandom init_random = new SecureRandom() { boolean firstLong = true; public long nextLong() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -