0001: package org.bouncycastle.crypto.test;
0002:
0003: import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
0004: import org.bouncycastle.crypto.generators.GOST3410KeyPairGenerator;
0005: import org.bouncycastle.crypto.generators.GOST3410ParametersGenerator;
0006: import org.bouncycastle.crypto.params.GOST3410KeyGenerationParameters;
0007: import org.bouncycastle.crypto.params.GOST3410Parameters;
0008: import org.bouncycastle.crypto.params.ParametersWithRandom;
0009: import org.bouncycastle.crypto.signers.GOST3410Signer;
0010: import org.bouncycastle.util.encoders.Hex;
0011: import org.bouncycastle.util.test.FixedSecureRandom;
0012: import org.bouncycastle.util.test.NumberParsing;
0013: import org.bouncycastle.util.test.SimpleTestResult;
0014: import org.bouncycastle.util.test.Test;
0015: import org.bouncycastle.util.test.TestResult;
0016:
0017: import java.math.BigInteger;
0018: import java.security.SecureRandom;
0019:
0020: public class GOST3410Test implements Test {
0021: byte[] hashmessage = Hex
0022: .decode("3042453136414534424341374533364339313734453431443642453241453435");
0023:
0024: private byte[] zeroTwo(int length) {
0025: byte[] data = new byte[length];
0026: data[data.length - 1] = 0x02;
0027: return data;
0028: }
0029:
0030: private class GOST3410_TEST1_512 implements Test {
0031: public String getName() {
0032: return "GOST3410-TEST1-512";
0033: }
0034:
0035: FixedSecureRandom init_random = new FixedSecureRandom(
0036: new byte[][] { Hex.decode("00005EC900007341"),
0037: zeroTwo(64) });
0038: FixedSecureRandom random = new FixedSecureRandom(
0039: Hex
0040: .decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A"));
0041: FixedSecureRandom keyRandom = new FixedSecureRandom(
0042: Hex
0043: .decode("3036314538303830343630454235324435324234314132373832433138443046"));
0044:
0045: BigInteger pValue = new BigInteger(
0046: "EE8172AE8996608FB69359B89EB82A69854510E2977A4D63BC97322CE5DC3386EA0A12B343E9190F23177539845839786BB0C345D165976EF2195EC9B1C379E3",
0047: 16);
0048: BigInteger qValue = new BigInteger(
0049: "98915E7EC8265EDFCDA31E88F24809DDB064BDC7285DD50D7289F0AC6F49DD2D",
0050: 16);
0051:
0052: public TestResult perform() {
0053: BigInteger r = new BigInteger(
0054: "3e5f895e276d81d2d52c0763270a458157b784c57abdbd807bc44fd43a32ac06",
0055: 16);
0056: BigInteger s = new BigInteger(
0057: "3f0dd5d4400d47c08e4ce505ff7434b6dbf729592e37c74856dab85115a60955",
0058: 16);
0059: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
0060:
0061: pGen.init(512, 1, init_random);
0062:
0063: GOST3410Parameters params = pGen.generateParameters();
0064:
0065: if (params.getValidationParameters() == null) {
0066: return new SimpleTestResult(false, getName()
0067: + "validation parameters wrong");
0068: }
0069: if (params.getValidationParameters().getC() != 29505
0070: || params.getValidationParameters().getX0() != 24265) {
0071: return new SimpleTestResult(false, getName()
0072: + "validation parameters values wrong");
0073: }
0074: if (!init_random.isExhausted()) {
0075: return new SimpleTestResult(
0076: false,
0077: getName()
0078: + ": unexpected number of bytes used from 'init_random'.");
0079: }
0080:
0081: if (!pValue.equals(params.getP())
0082: || !qValue.equals(params.getQ())) {
0083: return new SimpleTestResult(false, getName()
0084: + ": p or q wrong");
0085: }
0086:
0087: GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator();
0088: GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(
0089: keyRandom, params);
0090:
0091: GOST3410KeyGen.init(genParam);
0092:
0093: AsymmetricCipherKeyPair pair = GOST3410KeyGen
0094: .generateKeyPair();
0095:
0096: if (!keyRandom.isExhausted()) {
0097: return new SimpleTestResult(
0098: false,
0099: getName()
0100: + ": unexpected number of bytes used from 'keyRandom'.");
0101: }
0102:
0103: ParametersWithRandom param = new ParametersWithRandom(pair
0104: .getPrivate(), random);
0105:
0106: GOST3410Signer gost3410 = new GOST3410Signer();
0107:
0108: gost3410.init(true, param);
0109:
0110: BigInteger[] sig = gost3410.generateSignature(hashmessage);
0111:
0112: if (!random.isExhausted()) {
0113: return new SimpleTestResult(
0114: false,
0115: getName()
0116: + ": unexpected number of bytes used from 'random'.");
0117: }
0118:
0119: if (!r.equals(sig[0])) {
0120: return new SimpleTestResult(false, getName()
0121: + ": r component wrong."
0122: + System.getProperty("line.separator")
0123: + " expecting: " + r.toString(16)
0124: + System.getProperty("line.separator")
0125: + " got : " + sig[0].toString(16));
0126: }
0127:
0128: if (!s.equals(sig[1])) {
0129: return new SimpleTestResult(false, getName()
0130: + ": s component wrong."
0131: + System.getProperty("line.separator")
0132: + " expecting: " + s.toString(16)
0133: + System.getProperty("line.separator")
0134: + " got : " + sig[1].toString(16));
0135: }
0136:
0137: gost3410.init(false, pair.getPublic());
0138:
0139: if (gost3410.verifySignature(hashmessage, sig[0], sig[1])) {
0140: return new SimpleTestResult(true, getName() + ": Okay");
0141: } else {
0142: return new SimpleTestResult(false, getName()
0143: + ": verification fails");
0144: }
0145: }
0146: }
0147:
0148: private class GOST3410_TEST2_512 implements Test {
0149: public String getName() {
0150: return "GOST3410-TEST2-512";
0151: }
0152:
0153: FixedSecureRandom init_random = new FixedSecureRandom(
0154: new byte[][] {
0155: Hex.decode("000000003DFC46F1000000000000000D"),
0156: zeroTwo(64) });
0157: FixedSecureRandom random = new FixedSecureRandom(
0158: Hex
0159: .decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A"));
0160: FixedSecureRandom keyRandom = new FixedSecureRandom(
0161: Hex
0162: .decode("3036314538303830343630454235324435324234314132373832433138443046"));
0163:
0164: BigInteger pValue = new BigInteger(
0165: "8b08eb135af966aab39df294538580c7da26765d6d38d30cf1c06aae0d1228c3316a0e29198460fad2b19dc381c15c888c6dfd0fc2c565abb0bf1faff9518f85",
0166: 16);
0167: BigInteger qValue = new BigInteger(
0168: "931a58fb6f0dcdf2fe7549bc3f19f4724b56898f7f921a076601edb18c93dc75",
0169: 16);
0170:
0171: public TestResult perform() {
0172: BigInteger r = new BigInteger(
0173: "7c07c8cf035c2a1cb2b7fae5807ac7cd623dfca7a1a68f6d858317822f1ea00d",
0174: 16);
0175: BigInteger s = new BigInteger(
0176: "7e9e036a6ff87dbf9b004818252b1f6fc310bdd4d17cb8c37d9c36c7884de60c",
0177: 16);
0178: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
0179:
0180: pGen.init(512, 2, init_random);
0181:
0182: GOST3410Parameters params = pGen.generateParameters();
0183:
0184: if (!init_random.isExhausted()) {
0185: return new SimpleTestResult(
0186: false,
0187: getName()
0188: + ": unexpected number of bytes used from 'init_random'.");
0189: }
0190:
0191: if (params.getValidationParameters() == null) {
0192: return new SimpleTestResult(false, getName()
0193: + ": validation parameters wrong");
0194: }
0195:
0196: if (params.getValidationParameters().getCL() != 13
0197: || params.getValidationParameters().getX0L() != 1039943409) {
0198: return new SimpleTestResult(false, getName()
0199: + ": validation parameters values wrong");
0200: }
0201:
0202: if (!pValue.equals(params.getP())
0203: || !qValue.equals(params.getQ())) {
0204: return new SimpleTestResult(false, getName()
0205: + ": p or q wrong");
0206: }
0207:
0208: GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator();
0209: GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(
0210: keyRandom, params);
0211:
0212: GOST3410KeyGen.init(genParam);
0213:
0214: AsymmetricCipherKeyPair pair = GOST3410KeyGen
0215: .generateKeyPair();
0216:
0217: if (!keyRandom.isExhausted()) {
0218: return new SimpleTestResult(
0219: false,
0220: getName()
0221: + ": unexpected number of bytes used from 'keyRandom'.");
0222: }
0223:
0224: ParametersWithRandom param = new ParametersWithRandom(pair
0225: .getPrivate(), random);
0226:
0227: GOST3410Signer GOST3410 = new GOST3410Signer();
0228:
0229: GOST3410.init(true, param);
0230:
0231: BigInteger[] sig = GOST3410.generateSignature(hashmessage);
0232:
0233: if (!random.isExhausted()) {
0234: return new SimpleTestResult(
0235: false,
0236: getName()
0237: + ": unexpected number of bytes used from 'random'.");
0238: }
0239:
0240: if (!r.equals(sig[0])) {
0241: return new SimpleTestResult(false, getName()
0242: + ": r component wrong."
0243: + System.getProperty("line.separator")
0244: + " expecting: " + r.toString(16)
0245: + System.getProperty("line.separator")
0246: + " got : " + sig[0].toString(16));
0247: }
0248:
0249: if (!s.equals(sig[1])) {
0250: return new SimpleTestResult(false, getName()
0251: + ": s component wrong."
0252: + System.getProperty("line.separator")
0253: + " expecting: " + s.toString(16)
0254: + System.getProperty("line.separator")
0255: + " got : " + sig[1].toString(16));
0256: }
0257:
0258: GOST3410.init(false, pair.getPublic());
0259:
0260: if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) {
0261: return new SimpleTestResult(true, getName() + ": Okay");
0262: } else {
0263: return new SimpleTestResult(false, getName()
0264: + ": verification fails");
0265: }
0266: }
0267: }
0268:
0269: private class GOST3410_TEST1_1024 implements Test {
0270: public String getName() {
0271: return "GOST3410-TEST1-1024";
0272: }
0273:
0274: SecureRandom init_random = new SecureRandom() {
0275: boolean firstInt = true;
0276:
0277: public int nextInt() {
0278: String x0 = "0xA565";
0279: String c = "0x538B";
0280:
0281: if (firstInt) {
0282: firstInt = false;
0283: return NumberParsing.decodeIntFromHex(x0);
0284: }
0285: return NumberParsing.decodeIntFromHex(c);
0286: }
0287:
0288: public void nextBytes(byte[] bytes) {
0289:
0290: byte[] d = Hex.decode("02");
0291:
0292: System.arraycopy(d, 0, bytes, bytes.length - d.length,
0293: d.length);
0294: }
0295: };
0296:
0297: SecureRandom random = new SecureRandom() {
0298: public void nextBytes(byte[] bytes) {
0299: byte[] k = Hex
0300: .decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");
0301:
0302: int i;
0303:
0304: for (i = 0; i < (bytes.length - k.length); i += k.length) {
0305: System.arraycopy(k, 0, bytes, i, k.length);
0306: }
0307:
0308: if (i > bytes.length) {
0309: System.arraycopy(k, 0, bytes, i - k.length,
0310: bytes.length - (i - k.length));
0311: } else {
0312: System.arraycopy(k, 0, bytes, i, bytes.length - i);
0313: }
0314: }
0315: };
0316:
0317: SecureRandom keyRandom = new SecureRandom() {
0318: public void nextBytes(byte[] bytes) {
0319: byte[] x = Hex
0320: .decode("3036314538303830343630454235324435324234314132373832433138443046");
0321:
0322: int i;
0323:
0324: for (i = 0; i < (bytes.length - x.length); i += x.length) {
0325: System.arraycopy(x, 0, bytes, i, x.length);
0326: }
0327:
0328: if (i > bytes.length) {
0329: System.arraycopy(x, 0, bytes, i - x.length,
0330: bytes.length - (i - x.length));
0331: } else {
0332: System.arraycopy(x, 0, bytes, i, bytes.length - i);
0333: }
0334: }
0335: };
0336:
0337: BigInteger pValue = new BigInteger(
0338: "ab8f37938356529e871514c1f48c5cbce77b2f4fc9a2673ac2c1653da8984090c0ac73775159a26bef59909d4c9846631270e16653a6234668f2a52a01a39b921490e694c0f104b58d2e14970fccb478f98d01e975a1028b9536d912de5236d2dd2fc396b77153594d4178780e5f16f718471e2111c8ce64a7d7e196fa57142d",
0339: 16);
0340: BigInteger qValue = new BigInteger(
0341: "bcc02ca0ce4f0753ec16105ee5d530aa00d39f3171842ab2c334a26b5f576e0f",
0342: 16);
0343:
0344: public TestResult perform() {
0345: BigInteger r = new BigInteger(
0346: "a8790aabbd5a998ff524bad048ac69cd1faff2dab048265c8d60d1471c44a9ee",
0347: 16);
0348: BigInteger s = new BigInteger(
0349: "30df5ba32ac77170b9632559bef7d37620017756dff3fea1088b4267db0944b8",
0350: 16);
0351: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
0352:
0353: pGen.init(1024, 1, init_random);
0354:
0355: GOST3410Parameters params = pGen.generateParameters();
0356:
0357: if (!pValue.equals(params.getP())
0358: || !qValue.equals(params.getQ())) {
0359: return new SimpleTestResult(false, getName()
0360: + ": p or q wrong");
0361: }
0362:
0363: GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator();
0364: GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(
0365: keyRandom, params);
0366:
0367: GOST3410KeyGen.init(genParam);
0368:
0369: AsymmetricCipherKeyPair pair = GOST3410KeyGen
0370: .generateKeyPair();
0371:
0372: ParametersWithRandom param = new ParametersWithRandom(pair
0373: .getPrivate(), random);
0374:
0375: GOST3410Signer GOST3410 = new GOST3410Signer();
0376:
0377: GOST3410.init(true, param);
0378:
0379: BigInteger[] sig = GOST3410.generateSignature(hashmessage);
0380:
0381: if (!r.equals(sig[0])) {
0382: return new SimpleTestResult(false, getName()
0383: + ": r component wrong."
0384: + System.getProperty("line.separator")
0385: + " expecting: " + r.toString(16)
0386: + System.getProperty("line.separator")
0387: + " got : " + sig[0].toString(16));
0388: }
0389:
0390: if (!s.equals(sig[1])) {
0391: return new SimpleTestResult(false, getName()
0392: + ": s component wrong."
0393: + System.getProperty("line.separator")
0394: + " expecting: " + s.toString(16)
0395: + System.getProperty("line.separator")
0396: + " got : " + sig[1].toString(16));
0397: }
0398:
0399: GOST3410.init(false, pair.getPublic());
0400:
0401: if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) {
0402: return new SimpleTestResult(true, getName() + ": Okay");
0403: } else {
0404: return new SimpleTestResult(false, getName()
0405: + ": verification fails");
0406: }
0407: }
0408: }
0409:
0410: private class GOST3410_TEST2_1024 implements Test {
0411: public String getName() {
0412: return "GOST3410-TEST2-1024";
0413: }
0414:
0415: SecureRandom init_random = new SecureRandom() {
0416: boolean firstLong = true;
0417:
0418: public long nextLong() {
0419: String x0 = "0x3DFC46F1";
0420: String c = "0xD";
0421:
0422: if (firstLong) {
0423: firstLong = false;
0424: return NumberParsing.decodeLongFromHex(x0);
0425: }
0426: return NumberParsing.decodeLongFromHex(c);
0427: }
0428:
0429: public void nextBytes(byte[] bytes) {
0430:
0431: byte[] d = Hex.decode("02");
0432:
0433: System.arraycopy(d, 0, bytes, bytes.length - d.length,
0434: d.length);
0435: }
0436: };
0437:
0438: SecureRandom random = new SecureRandom() {
0439: public void nextBytes(byte[] bytes) {
0440: byte[] k = Hex
0441: .decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");
0442:
0443: int i;
0444:
0445: for (i = 0; i < (bytes.length - k.length); i += k.length) {
0446: System.arraycopy(k, 0, bytes, i, k.length);
0447: }
0448:
0449: if (i > bytes.length) {
0450: System.arraycopy(k, 0, bytes, i - k.length,
0451: bytes.length - (i - k.length));
0452: } else {
0453: System.arraycopy(k, 0, bytes, i, bytes.length - i);
0454: }
0455: }
0456: };
0457:
0458: SecureRandom keyRandom = new SecureRandom() {
0459: public void nextBytes(byte[] bytes) {
0460: byte[] x = Hex
0461: .decode("3036314538303830343630454235324435324234314132373832433138443046");
0462:
0463: int i;
0464:
0465: for (i = 0; i < (bytes.length - x.length); i += x.length) {
0466: System.arraycopy(x, 0, bytes, i, x.length);
0467: }
0468:
0469: if (i > bytes.length) {
0470: System.arraycopy(x, 0, bytes, i - x.length,
0471: bytes.length - (i - x.length));
0472: } else {
0473: System.arraycopy(x, 0, bytes, i, bytes.length - i);
0474: }
0475: }
0476: };
0477:
0478: BigInteger pValue = new BigInteger(
0479: "e2c4191c4b5f222f9ac2732562f6d9b4f18e7fb67a290ea1e03d750f0b9806755fc730d975bf3faa606d05c218b35a6c3706919aab92e0c58b1de4531c8fa8e7af43c2bff016251e21b2870897f6a27ac4450bca235a5b748ad386e4a0e4dfcb09152435abcfe48bd0b126a8122c7382f285a9864615c66decddf6afd355dfb7",
0480: 16);
0481: BigInteger qValue = new BigInteger(
0482: "931a58fb6f0dcdf2fe7549bc3f19f4724b56898f7f921a076601edb18c93dc75",
0483: 16);
0484:
0485: public TestResult perform() {
0486: BigInteger r = new BigInteger(
0487: "81d69a192e9c7ac21fc07da41bd07e230ba6a94eb9f3c1fd104c7bd976733ca5",
0488: 16);
0489: BigInteger s = new BigInteger(
0490: "315c879c8414f35feb4deb15e7cc0278c48e6ca1596325d6959338d860b0c47a",
0491: 16);
0492: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
0493:
0494: pGen.init(1024, 2, init_random);
0495:
0496: GOST3410Parameters params = pGen.generateParameters();
0497:
0498: if (!pValue.equals(params.getP())
0499: || !qValue.equals(params.getQ())) {
0500: return new SimpleTestResult(false, getName()
0501: + ": p or q wrong");
0502: }
0503:
0504: GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator();
0505: GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(
0506: keyRandom, params);
0507:
0508: GOST3410KeyGen.init(genParam);
0509:
0510: AsymmetricCipherKeyPair pair = GOST3410KeyGen
0511: .generateKeyPair();
0512:
0513: ParametersWithRandom param = new ParametersWithRandom(pair
0514: .getPrivate(), random);
0515:
0516: GOST3410Signer GOST3410 = new GOST3410Signer();
0517:
0518: GOST3410.init(true, param);
0519:
0520: BigInteger[] sig = GOST3410.generateSignature(hashmessage);
0521:
0522: if (!r.equals(sig[0])) {
0523: return new SimpleTestResult(false, getName()
0524: + ": r component wrong."
0525: + System.getProperty("line.separator")
0526: + " expecting: " + r.toString(16)
0527: + System.getProperty("line.separator")
0528: + " got : " + sig[0].toString(16));
0529: }
0530:
0531: if (!s.equals(sig[1])) {
0532: return new SimpleTestResult(false, getName()
0533: + ": s component wrong."
0534: + System.getProperty("line.separator")
0535: + " expecting: " + s.toString(16)
0536: + System.getProperty("line.separator")
0537: + " got : " + sig[1].toString(16));
0538: }
0539:
0540: GOST3410.init(false, pair.getPublic());
0541:
0542: if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) {
0543: return new SimpleTestResult(true, getName() + ": Okay");
0544: } else {
0545: return new SimpleTestResult(false, getName()
0546: + ": verification fails");
0547: }
0548: }
0549: }
0550:
0551: private class GOST3410_AParam implements Test {
0552: public String getName() {
0553: return "GOST3410-AParam";
0554: }
0555:
0556: SecureRandom init_random = new SecureRandom() {
0557: boolean firstLong = true;
0558:
0559: public long nextLong() {
0560: String x0 = "0x520874F5";
0561: String c = "0xEE39ADB3";
0562:
0563: if (firstLong) {
0564: firstLong = false;
0565: return NumberParsing.decodeLongFromHex(x0);
0566: }
0567: return NumberParsing.decodeLongFromHex(c);
0568: }
0569:
0570: public void nextBytes(byte[] bytes) {
0571:
0572: byte[] d = Hex.decode("02");
0573:
0574: System.arraycopy(d, 0, bytes, bytes.length - d.length,
0575: d.length);
0576: }
0577: };
0578:
0579: SecureRandom random = new SecureRandom() {
0580: public void nextBytes(byte[] bytes) {
0581: byte[] k = Hex
0582: .decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");
0583:
0584: int i;
0585:
0586: for (i = 0; i < (bytes.length - k.length); i += k.length) {
0587: System.arraycopy(k, 0, bytes, i, k.length);
0588: }
0589:
0590: if (i > bytes.length) {
0591: System.arraycopy(k, 0, bytes, i - k.length,
0592: bytes.length - (i - k.length));
0593: } else {
0594: System.arraycopy(k, 0, bytes, i, bytes.length - i);
0595: }
0596: }
0597: };
0598:
0599: SecureRandom keyRandom = new SecureRandom() {
0600: public void nextBytes(byte[] bytes) {
0601: byte[] x = Hex
0602: .decode("3036314538303830343630454235324435324234314132373832433138443046");
0603:
0604: int i;
0605:
0606: for (i = 0; i < (bytes.length - x.length); i += x.length) {
0607: System.arraycopy(x, 0, bytes, i, x.length);
0608: }
0609:
0610: if (i > bytes.length) {
0611: System.arraycopy(x, 0, bytes, i - x.length,
0612: bytes.length - (i - x.length));
0613: } else {
0614: System.arraycopy(x, 0, bytes, i, bytes.length - i);
0615: }
0616: }
0617: };
0618:
0619: BigInteger pValue = new BigInteger(
0620: "b4e25efb018e3c8b87505e2a67553c5edc56c2914b7e4f89d23f03f03377e70a2903489dd60e78418d3d851edb5317c4871e40b04228c3b7902963c4b7d85d52b9aa88f2afdbeb28da8869d6df846a1d98924e925561bd69300b9ddd05d247b5922d967cbb02671881c57d10e5ef72d3e6dad4223dc82aa1f7d0294651a480df",
0621: 16);
0622: BigInteger qValue = new BigInteger(
0623: "972432a437178b30bd96195b773789ab2fff15594b176dd175b63256ee5af2cf",
0624: 16);
0625:
0626: public TestResult perform() {
0627: BigInteger r = new BigInteger(
0628: "64a8856628e5669d85f62cd763dd4a99bc56d33dc0e1859122855d141e9e4774",
0629: 16);
0630: BigInteger s = new BigInteger(
0631: "319ebac97092b288d469a4b988248794f60c865bc97858d9a3135c6d1a1bf2dd",
0632: 16);
0633: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
0634:
0635: pGen.init(1024, 2, init_random);
0636:
0637: GOST3410Parameters params = pGen.generateParameters();
0638:
0639: if (!pValue.equals(params.getP())
0640: || !qValue.equals(params.getQ())) {
0641: return new SimpleTestResult(false, getName()
0642: + ": p or q wrong");
0643: }
0644:
0645: GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator();
0646: GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(
0647: keyRandom, params);
0648:
0649: GOST3410KeyGen.init(genParam);
0650:
0651: AsymmetricCipherKeyPair pair = GOST3410KeyGen
0652: .generateKeyPair();
0653:
0654: ParametersWithRandom param = new ParametersWithRandom(pair
0655: .getPrivate(), random);
0656:
0657: GOST3410Signer GOST3410 = new GOST3410Signer();
0658:
0659: GOST3410.init(true, param);
0660:
0661: BigInteger[] sig = GOST3410.generateSignature(hashmessage);
0662:
0663: if (!r.equals(sig[0])) {
0664: return new SimpleTestResult(false, getName()
0665: + ": r component wrong."
0666: + System.getProperty("line.separator")
0667: + " expecting: " + r.toString(16)
0668: + System.getProperty("line.separator")
0669: + " got : " + sig[0].toString(16));
0670: }
0671:
0672: if (!s.equals(sig[1])) {
0673: return new SimpleTestResult(false, getName()
0674: + ": s component wrong."
0675: + System.getProperty("line.separator")
0676: + " expecting: " + s.toString(16)
0677: + System.getProperty("line.separator")
0678: + " got : " + sig[1].toString(16));
0679: }
0680:
0681: GOST3410.init(false, pair.getPublic());
0682:
0683: if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) {
0684: return new SimpleTestResult(true, getName() + ": Okay");
0685: } else {
0686: return new SimpleTestResult(false, getName()
0687: + ": verification fails");
0688: }
0689: }
0690: }
0691:
0692: private class GOST3410_BParam implements Test {
0693: public String getName() {
0694: return "GOST3410-BParam";
0695: }
0696:
0697: SecureRandom init_random = new SecureRandom() {
0698: boolean firstLong = true;
0699:
0700: public long nextLong() {
0701: String x0 = "0x5B977CDB";
0702: String c = "0x6E9692DD";
0703:
0704: if (firstLong) {
0705: firstLong = false;
0706: return NumberParsing.decodeLongFromHex(x0);
0707: }
0708: return NumberParsing.decodeLongFromHex(c);
0709: }
0710:
0711: public void nextBytes(byte[] bytes) {
0712: byte[] d = Hex
0713: .decode("bc3cbbdb7e6f848286e19ad9a27a8e297e5b71c53dd974cdf60f937356df69cbc97a300ccc71685c553046147f11568c4fddf363d9d886438345a62c3b75963d6546adfabf31b31290d12cae65ecb8309ef66782");
0714:
0715: System.arraycopy(d, 0, bytes, bytes.length - d.length,
0716: d.length);
0717: }
0718: };
0719:
0720: SecureRandom random = new SecureRandom() {
0721: public void nextBytes(byte[] bytes) {
0722: byte[] k = Hex
0723: .decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");
0724:
0725: int i;
0726:
0727: for (i = 0; i < (bytes.length - k.length); i += k.length) {
0728: System.arraycopy(k, 0, bytes, i, k.length);
0729: }
0730:
0731: if (i > bytes.length) {
0732: System.arraycopy(k, 0, bytes, i - k.length,
0733: bytes.length - (i - k.length));
0734: } else {
0735: System.arraycopy(k, 0, bytes, i, bytes.length - i);
0736: }
0737: }
0738: };
0739:
0740: SecureRandom keyRandom = new SecureRandom() {
0741: public void nextBytes(byte[] bytes) {
0742: byte[] x = Hex
0743: .decode("3036314538303830343630454235324435324234314132373832433138443046");
0744:
0745: int i;
0746:
0747: for (i = 0; i < (bytes.length - x.length); i += x.length) {
0748: System.arraycopy(x, 0, bytes, i, x.length);
0749: }
0750:
0751: if (i > bytes.length) {
0752: System.arraycopy(x, 0, bytes, i - x.length,
0753: bytes.length - (i - x.length));
0754: } else {
0755: System.arraycopy(x, 0, bytes, i, bytes.length - i);
0756: }
0757: }
0758: };
0759:
0760: BigInteger pValue = new BigInteger(
0761: "c6971fc57524b30c9018c5e621de15499736854f56a6f8aee65a7a404632b3540f09020f67f04dc2e6783b141dceffd21a703035b7d0187c6e12cb4229922bafdb2225b73e6b23a0de36e20047065aea000c1a374283d0ad8dc1981e3995f0bb8c72526041fcb98ae6163e1e71a669d8364e9c4c3188f673c5f8ee6fadb41abf",
0762: 16);
0763: BigInteger qValue = new BigInteger(
0764: "b09d634c10899cd7d4c3a7657403e05810b07c61a688bab2c37f475e308b0607",
0765: 16);
0766:
0767: public TestResult perform() {
0768: BigInteger r = new BigInteger(
0769: "860d82c60e9502cd00c0e9e1f6563feafec304801974d745c5e02079946f729e",
0770: 16);
0771: BigInteger s = new BigInteger(
0772: "7ef49264ef022801aaa03033cd97915235fbab4c823ed936b0f360c22114688a",
0773: 16);
0774: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
0775:
0776: pGen.init(1024, 2, init_random);
0777:
0778: GOST3410Parameters params = pGen.generateParameters();
0779:
0780: if (!pValue.equals(params.getP())
0781: || !qValue.equals(params.getQ())) {
0782: return new SimpleTestResult(false, getName()
0783: + ": p or q wrong");
0784: }
0785:
0786: GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator();
0787: GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(
0788: keyRandom, params);
0789:
0790: GOST3410KeyGen.init(genParam);
0791:
0792: AsymmetricCipherKeyPair pair = GOST3410KeyGen
0793: .generateKeyPair();
0794:
0795: ParametersWithRandom param = new ParametersWithRandom(pair
0796: .getPrivate(), random);
0797:
0798: GOST3410Signer GOST3410 = new GOST3410Signer();
0799:
0800: GOST3410.init(true, param);
0801:
0802: BigInteger[] sig = GOST3410.generateSignature(hashmessage);
0803:
0804: if (!r.equals(sig[0])) {
0805: return new SimpleTestResult(false, getName()
0806: + ": r component wrong."
0807: + System.getProperty("line.separator")
0808: + " expecting: " + r.toString(16)
0809: + System.getProperty("line.separator")
0810: + " got : " + sig[0].toString(16));
0811: }
0812:
0813: if (!s.equals(sig[1])) {
0814: return new SimpleTestResult(false, getName()
0815: + ": s component wrong."
0816: + System.getProperty("line.separator")
0817: + " expecting: " + s.toString(16)
0818: + System.getProperty("line.separator")
0819: + " got : " + sig[1].toString(16));
0820: }
0821:
0822: GOST3410.init(false, pair.getPublic());
0823:
0824: if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) {
0825: return new SimpleTestResult(true, getName() + ": Okay");
0826: } else {
0827: return new SimpleTestResult(false, getName()
0828: + ": verification fails");
0829: }
0830: }
0831: }
0832:
0833: private class GOST3410_CParam implements Test {
0834: public String getName() {
0835: return "GOST3410-CParam";
0836: }
0837:
0838: SecureRandom init_random = new SecureRandom() {
0839: boolean firstLong = true;
0840:
0841: public long nextLong() {
0842: String x0 = "0x43848744";
0843: String c = "0xB50A826D";
0844:
0845: if (firstLong) {
0846: firstLong = false;
0847: return NumberParsing.decodeLongFromHex(x0);
0848: }
0849: return NumberParsing.decodeLongFromHex(c);
0850: }
0851:
0852: public void nextBytes(byte[] bytes) {
0853: byte[] d = Hex.decode("7F575E8194BC5BDF");
0854:
0855: System.arraycopy(d, 0, bytes, bytes.length - d.length,
0856: d.length);
0857: }
0858: };
0859:
0860: SecureRandom random = new SecureRandom() {
0861: public void nextBytes(byte[] bytes) {
0862: byte[] k = Hex
0863: .decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");
0864:
0865: int i;
0866:
0867: for (i = 0; i < (bytes.length - k.length); i += k.length) {
0868: System.arraycopy(k, 0, bytes, i, k.length);
0869: }
0870:
0871: if (i > bytes.length) {
0872: System.arraycopy(k, 0, bytes, i - k.length,
0873: bytes.length - (i - k.length));
0874: } else {
0875: System.arraycopy(k, 0, bytes, i, bytes.length - i);
0876: }
0877: }
0878: };
0879:
0880: SecureRandom keyRandom = new SecureRandom() {
0881: public void nextBytes(byte[] bytes) {
0882: byte[] x = Hex
0883: .decode("3036314538303830343630454235324435324234314132373832433138443046");
0884:
0885: int i;
0886:
0887: for (i = 0; i < (bytes.length - x.length); i += x.length) {
0888: System.arraycopy(x, 0, bytes, i, x.length);
0889: }
0890:
0891: if (i > bytes.length) {
0892: System.arraycopy(x, 0, bytes, i - x.length,
0893: bytes.length - (i - x.length));
0894: } else {
0895: System.arraycopy(x, 0, bytes, i, bytes.length - i);
0896: }
0897: }
0898: };
0899:
0900: BigInteger pValue = new BigInteger(
0901: "9d88e6d7fe3313bd2e745c7cdd2ab9ee4af3c8899e847de74a33783ea68bc30588ba1f738c6aaf8ab350531f1854c3837cc3c860ffd7e2e106c3f63b3d8a4c034ce73942a6c3d585b599cf695ed7a3c4a93b2b947b7157bb1a1c043ab41ec8566c6145e938a611906de0d32e562494569d7e999a0dda5c879bdd91fe124df1e9",
0902: 16);
0903: BigInteger qValue = new BigInteger(
0904: "fadd197abd19a1b4653eecf7eca4d6a22b1f7f893b641f901641fbb555354faf",
0905: 16);
0906:
0907: public TestResult perform() {
0908: BigInteger r = new BigInteger(
0909: "4deb95a0b35e7ed7edebe9bef5a0f93739e16b7ff27fe794d989d0c13159cfbc",
0910: 16);
0911: BigInteger s = new BigInteger(
0912: "e1d0d30345c24cfeb33efde3deee5fbbda78ddc822b719d860cd0ba1fb6bd43b",
0913: 16);
0914: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
0915:
0916: pGen.init(1024, 2, init_random);
0917:
0918: GOST3410Parameters params = pGen.generateParameters();
0919:
0920: if (!pValue.equals(params.getP())
0921: || !qValue.equals(params.getQ())) {
0922: return new SimpleTestResult(false, getName()
0923: + ": p or q wrong");
0924: }
0925:
0926: GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator();
0927: GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(
0928: keyRandom, params);
0929:
0930: GOST3410KeyGen.init(genParam);
0931:
0932: AsymmetricCipherKeyPair pair = GOST3410KeyGen
0933: .generateKeyPair();
0934:
0935: ParametersWithRandom param = new ParametersWithRandom(pair
0936: .getPrivate(), random);
0937:
0938: GOST3410Signer GOST3410 = new GOST3410Signer();
0939:
0940: GOST3410.init(true, param);
0941:
0942: BigInteger[] sig = GOST3410.generateSignature(hashmessage);
0943:
0944: if (!r.equals(sig[0])) {
0945: return new SimpleTestResult(false, getName()
0946: + ": r component wrong."
0947: + System.getProperty("line.separator")
0948: + " expecting: " + r.toString(16)
0949: + System.getProperty("line.separator")
0950: + " got : " + sig[0].toString(16));
0951: }
0952:
0953: if (!s.equals(sig[1])) {
0954: return new SimpleTestResult(false, getName()
0955: + ": s component wrong."
0956: + System.getProperty("line.separator")
0957: + " expecting: " + s.toString(16)
0958: + System.getProperty("line.separator")
0959: + " got : " + sig[1].toString(16));
0960: }
0961:
0962: GOST3410.init(false, pair.getPublic());
0963:
0964: if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) {
0965: return new SimpleTestResult(true, getName() + ": Okay");
0966: } else {
0967: return new SimpleTestResult(false, getName()
0968: + ": verification fails");
0969: }
0970: }
0971: }
0972:
0973: private class GOST3410_DParam implements Test {
0974: public String getName() {
0975: return "GOST3410-DParam";
0976: }
0977:
0978: SecureRandom init_random = new SecureRandom() {
0979: boolean firstLong = true;
0980:
0981: public long nextLong() {
0982: String x0 = "0x13DA8B9D";
0983: String c = "0xA0E9DE4B";
0984:
0985: if (firstLong) {
0986: firstLong = false;
0987: return NumberParsing.decodeLongFromHex(x0);
0988: }
0989: return NumberParsing.decodeLongFromHex(c);
0990: }
0991:
0992: public void nextBytes(byte[] bytes) {
0993:
0994: byte[] d = Hex
0995: .decode("41ab97857f42614355d32db0b1069f109a4da283676c7c53a68185b4");
0996:
0997: System.arraycopy(d, 0, bytes, bytes.length - d.length,
0998: d.length);
0999: }
1000: };
1001:
1002: SecureRandom random = new SecureRandom() {
1003: public void nextBytes(byte[] bytes) {
1004: byte[] k = Hex
1005: .decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");
1006:
1007: int i;
1008:
1009: for (i = 0; i < (bytes.length - k.length); i += k.length) {
1010: System.arraycopy(k, 0, bytes, i, k.length);
1011: }
1012:
1013: if (i > bytes.length) {
1014: System.arraycopy(k, 0, bytes, i - k.length,
1015: bytes.length - (i - k.length));
1016: } else {
1017: System.arraycopy(k, 0, bytes, i, bytes.length - i);
1018: }
1019: }
1020: };
1021:
1022: SecureRandom keyRandom = new SecureRandom() {
1023: public void nextBytes(byte[] bytes) {
1024: byte[] x = Hex
1025: .decode("3036314538303830343630454235324435324234314132373832433138443046");
1026:
1027: int i;
1028:
1029: for (i = 0; i < (bytes.length - x.length); i += x.length) {
1030: System.arraycopy(x, 0, bytes, i, x.length);
1031: }
1032:
1033: if (i > bytes.length) {
1034: System.arraycopy(x, 0, bytes, i - x.length,
1035: bytes.length - (i - x.length));
1036: } else {
1037: System.arraycopy(x, 0, bytes, i, bytes.length - i);
1038: }
1039: }
1040: };
1041:
1042: BigInteger pValue = new BigInteger(
1043: "80f102d32b0fd167d069c27a307adad2c466091904dbaa55d5b8cc7026f2f7a1919b890cb652c40e054e1e9306735b43d7b279eddf9102001cd9e1a831fe8a163eed89ab07cf2abe8242ac9dedddbf98d62cddd1ea4f5f15d3a42a6677bdd293b24260c0f27c0f1d15948614d567b66fa902baa11a69ae3bceadbb83e399c9b5",
1044: 16);
1045: BigInteger qValue = new BigInteger(
1046: "f0f544c418aac234f683f033511b65c21651a6078bda2d69bb9f732867502149",
1047: 16);
1048:
1049: public TestResult perform() {
1050: BigInteger r = new BigInteger(
1051: "712592d285b792e33b8a9a11e8e6c4f512ddf0042972bbfd1abb0a93e8fc6f54",
1052: 16);
1053: BigInteger s = new BigInteger(
1054: "2cf26758321258b130d5612111339f09ceb8668241f3482e38baa56529963f07",
1055: 16);
1056: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
1057:
1058: pGen.init(1024, 2, init_random);
1059:
1060: GOST3410Parameters params = pGen.generateParameters();
1061:
1062: if (!pValue.equals(params.getP())
1063: || !qValue.equals(params.getQ())) {
1064: return new SimpleTestResult(false, getName()
1065: + ": p or q wrong");
1066: }
1067:
1068: GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator();
1069: GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(
1070: keyRandom, params);
1071:
1072: GOST3410KeyGen.init(genParam);
1073:
1074: AsymmetricCipherKeyPair pair = GOST3410KeyGen
1075: .generateKeyPair();
1076:
1077: ParametersWithRandom param = new ParametersWithRandom(pair
1078: .getPrivate(), random);
1079:
1080: GOST3410Signer GOST3410 = new GOST3410Signer();
1081:
1082: GOST3410.init(true, param);
1083:
1084: BigInteger[] sig = GOST3410.generateSignature(hashmessage);
1085:
1086: if (!r.equals(sig[0])) {
1087: return new SimpleTestResult(false, getName()
1088: + ": r component wrong."
1089: + System.getProperty("line.separator")
1090: + " expecting: " + r.toString(16)
1091: + System.getProperty("line.separator")
1092: + " got : " + sig[0].toString(16));
1093: }
1094:
1095: if (!s.equals(sig[1])) {
1096: return new SimpleTestResult(false, getName()
1097: + ": s component wrong."
1098: + System.getProperty("line.separator")
1099: + " expecting: " + s.toString(16)
1100: + System.getProperty("line.separator")
1101: + " got : " + sig[1].toString(16));
1102: }
1103:
1104: GOST3410.init(false, pair.getPublic());
1105:
1106: if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) {
1107: return new SimpleTestResult(true, getName() + ": Okay");
1108: } else {
1109: return new SimpleTestResult(false, getName()
1110: + ": verification fails");
1111: }
1112: }
1113: }
1114:
1115: private class GOST3410_AExParam implements Test {
1116: public String getName() {
1117: return "GOST3410-AExParam";
1118: }
1119:
1120: SecureRandom init_random = new SecureRandom() {
1121: boolean firstLong = true;
1122:
1123: public long nextLong() {
1124: String x0 = "0xD05E9F14";
1125: String c = "0x46304C5F";
1126:
1127: if (firstLong) {
1128: firstLong = false;
1129: return NumberParsing.decodeLongFromHex(x0);
1130: }
1131: return NumberParsing.decodeLongFromHex(c);
1132: }
1133:
1134: public void nextBytes(byte[] bytes) {
1135: byte[] d = Hex
1136: .decode("35ab875399cda33c146ca629660e5a5e5c07714ca326db032dd6751995cdb90a612b9228932d8302704ec24a5def7739c5813d83");
1137:
1138: System.arraycopy(d, 0, bytes, bytes.length - d.length,
1139: d.length);
1140: }
1141: };
1142:
1143: SecureRandom random = new SecureRandom() {
1144: public void nextBytes(byte[] bytes) {
1145: byte[] k = Hex
1146: .decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");
1147:
1148: int i;
1149:
1150: for (i = 0; i < (bytes.length - k.length); i += k.length) {
1151: System.arraycopy(k, 0, bytes, i, k.length);
1152: }
1153:
1154: if (i > bytes.length) {
1155: System.arraycopy(k, 0, bytes, i - k.length,
1156: bytes.length - (i - k.length));
1157: } else {
1158: System.arraycopy(k, 0, bytes, i, bytes.length - i);
1159: }
1160: }
1161: };
1162:
1163: SecureRandom keyRandom = new SecureRandom() {
1164: public void nextBytes(byte[] bytes) {
1165: byte[] x = Hex
1166: .decode("3036314538303830343630454235324435324234314132373832433138443046");
1167:
1168: int i;
1169:
1170: for (i = 0; i < (bytes.length - x.length); i += x.length) {
1171: System.arraycopy(x, 0, bytes, i, x.length);
1172: }
1173:
1174: if (i > bytes.length) {
1175: System.arraycopy(x, 0, bytes, i - x.length,
1176: bytes.length - (i - x.length));
1177: } else {
1178: System.arraycopy(x, 0, bytes, i, bytes.length - i);
1179: }
1180: }
1181: };
1182:
1183: BigInteger pValue = new BigInteger(
1184: "ca3b3f2eee9fd46317d49595a9e7518e6c63d8f4eb4d22d10d28af0b8839f079f8289e603b03530784b9bb5a1e76859e4850c670c7b71c0df84ca3e0d6c177fe9f78a9d8433230a883cd82a2b2b5c7a3306980278570cdb79bf01074a69c9623348824b0c53791d53c6a78cab69e1cfb28368611a397f50f541e16db348dbe5f",
1185: 16);
1186: BigInteger qValue = new BigInteger(
1187: "cae4d85f80c147704b0ca48e85fb00a9057aa4acc44668e17f1996d7152690d9",
1188: 16);
1189:
1190: public TestResult perform() {
1191: BigInteger r = new BigInteger(
1192: "90892707282f433398488f19d31ac48523a8e2ded68944e0da91c6895ee7045e",
1193: 16);
1194: BigInteger s = new BigInteger(
1195: "3be4620ee88f1ee8f9dd63c7d145b7e554839feeca125049118262ea4651e9de",
1196: 16);
1197: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
1198:
1199: pGen.init(1024, 2, init_random);
1200:
1201: GOST3410Parameters params = pGen.generateParameters();
1202:
1203: if (!pValue.equals(params.getP())
1204: || !qValue.equals(params.getQ())) {
1205: return new SimpleTestResult(false, getName()
1206: + ": p or q wrong");
1207: }
1208:
1209: GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator();
1210: GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(
1211: keyRandom, params);
1212:
1213: GOST3410KeyGen.init(genParam);
1214:
1215: AsymmetricCipherKeyPair pair = GOST3410KeyGen
1216: .generateKeyPair();
1217:
1218: ParametersWithRandom param = new ParametersWithRandom(pair
1219: .getPrivate(), random);
1220:
1221: GOST3410Signer GOST3410 = new GOST3410Signer();
1222:
1223: GOST3410.init(true, param);
1224:
1225: BigInteger[] sig = GOST3410.generateSignature(hashmessage);
1226:
1227: if (!r.equals(sig[0])) {
1228: return new SimpleTestResult(false, getName()
1229: + ": r component wrong."
1230: + System.getProperty("line.separator")
1231: + " expecting: " + r.toString(16)
1232: + System.getProperty("line.separator")
1233: + " got : " + sig[0].toString(16));
1234: }
1235:
1236: if (!s.equals(sig[1])) {
1237: return new SimpleTestResult(false, getName()
1238: + ": s component wrong."
1239: + System.getProperty("line.separator")
1240: + " expecting: " + s.toString(16)
1241: + System.getProperty("line.separator")
1242: + " got : " + sig[1].toString(16));
1243: }
1244:
1245: GOST3410.init(false, pair.getPublic());
1246:
1247: if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) {
1248: return new SimpleTestResult(true, getName() + ": Okay");
1249: } else {
1250: return new SimpleTestResult(false, getName()
1251: + ": verification fails");
1252: }
1253: }
1254: }
1255:
1256: private class GOST3410_BExParam implements Test {
1257: public String getName() {
1258: return "GOST3410-BExParam";
1259: }
1260:
1261: SecureRandom init_random = new SecureRandom() {
1262: boolean firstLong = true;
1263:
1264: public long nextLong() {
1265: String x0 = "0x7A007804";
1266: String c = "0xD31A4FF7";
1267:
1268: if (firstLong) {
1269: firstLong = false;
1270: return NumberParsing.decodeLongFromHex(x0);
1271: }
1272: return NumberParsing.decodeLongFromHex(c);
1273: }
1274:
1275: public void nextBytes(byte[] bytes) {
1276: byte[] d = Hex
1277: .decode("7ec123d161477762838c2bea9dbdf33074af6d41d108a066a1e7a07ab3048de2");
1278:
1279: System.arraycopy(d, 0, bytes, bytes.length - d.length,
1280: d.length);
1281: }
1282: };
1283:
1284: SecureRandom random = new SecureRandom() {
1285: public void nextBytes(byte[] bytes) {
1286: byte[] k = Hex
1287: .decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");
1288:
1289: int i;
1290:
1291: for (i = 0; i < (bytes.length - k.length); i += k.length) {
1292: System.arraycopy(k, 0, bytes, i, k.length);
1293: }
1294:
1295: if (i > bytes.length) {
1296: System.arraycopy(k, 0, bytes, i - k.length,
1297: bytes.length - (i - k.length));
1298: } else {
1299: System.arraycopy(k, 0, bytes, i, bytes.length - i);
1300: }
1301: }
1302: };
1303:
1304: SecureRandom keyRandom = new SecureRandom() {
1305: public void nextBytes(byte[] bytes) {
1306: byte[] x = Hex
1307: .decode("3036314538303830343630454235324435324234314132373832433138443046");
1308:
1309: int i;
1310:
1311: for (i = 0; i < (bytes.length - x.length); i += x.length) {
1312: System.arraycopy(x, 0, bytes, i, x.length);
1313: }
1314:
1315: if (i > bytes.length) {
1316: System.arraycopy(x, 0, bytes, i - x.length,
1317: bytes.length - (i - x.length));
1318: } else {
1319: System.arraycopy(x, 0, bytes, i, bytes.length - i);
1320: }
1321: }
1322: };
1323:
1324: BigInteger pValue = new BigInteger(
1325: "9286dbda91eccfc3060aa5598318e2a639f5ba90a4ca656157b2673fb191cd0589ee05f4cef1bd13508408271458c30851ce7a4ef534742bfb11f4743c8f787b11193ba304c0e6bca25701bf88af1cb9b8fd4711d89f88e32b37d95316541bf1e5dbb4989b3df13659b88c0f97a3c1087b9f2d5317d557dcd4afc6d0a754e279",
1326: 16);
1327: BigInteger qValue = new BigInteger(
1328: "c966e9b3b8b7cdd82ff0f83af87036c38f42238ec50a876cd390e43d67b6013f",
1329: 16);
1330:
1331: public TestResult perform() {
1332: BigInteger r = new BigInteger(
1333: "8f79a582513df84dc247bcb624340cc0e5a34c4324a20ce7fe3ab8ff38a9db71",
1334: 16);
1335: BigInteger s = new BigInteger(
1336: "7508d22fd6cbb45efd438cb875e43f137247088d0f54b29a7c91f68a65b5fa85",
1337: 16);
1338: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
1339:
1340: pGen.init(1024, 2, init_random);
1341:
1342: GOST3410Parameters params = pGen.generateParameters();
1343:
1344: if (!pValue.equals(params.getP())
1345: || !qValue.equals(params.getQ())) {
1346: return new SimpleTestResult(false, getName()
1347: + ": p or q wrong");
1348: }
1349:
1350: GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator();
1351: GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(
1352: keyRandom, params);
1353:
1354: GOST3410KeyGen.init(genParam);
1355:
1356: AsymmetricCipherKeyPair pair = GOST3410KeyGen
1357: .generateKeyPair();
1358:
1359: ParametersWithRandom param = new ParametersWithRandom(pair
1360: .getPrivate(), random);
1361:
1362: GOST3410Signer GOST3410 = new GOST3410Signer();
1363:
1364: GOST3410.init(true, param);
1365:
1366: BigInteger[] sig = GOST3410.generateSignature(hashmessage);
1367:
1368: if (!r.equals(sig[0])) {
1369: return new SimpleTestResult(false, getName()
1370: + ": r component wrong."
1371: + System.getProperty("line.separator")
1372: + " expecting: " + r.toString(16)
1373: + System.getProperty("line.separator")
1374: + " got : " + sig[0].toString(16));
1375: }
1376:
1377: if (!s.equals(sig[1])) {
1378: return new SimpleTestResult(false, getName()
1379: + ": s component wrong."
1380: + System.getProperty("line.separator")
1381: + " expecting: " + s.toString(16)
1382: + System.getProperty("line.separator")
1383: + " got : " + sig[1].toString(16));
1384: }
1385:
1386: GOST3410.init(false, pair.getPublic());
1387:
1388: if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) {
1389: return new SimpleTestResult(true, getName() + ": Okay");
1390: } else {
1391: return new SimpleTestResult(false, getName()
1392: + ": verification fails");
1393: }
1394: }
1395: }
1396:
1397: private class GOST3410_CExParam implements Test {
1398: public String getName() {
1399: return "GOST3410-CExParam";
1400: }
1401:
1402: SecureRandom init_random = new SecureRandom() {
1403: boolean firstLong = true;
1404:
1405: public long nextLong() {
1406: String x0 = "0x162AB910";
1407: String c = "0x93F828D3";
1408:
1409: if (firstLong) {
1410: firstLong = false;
1411: return NumberParsing.decodeLongFromHex(x0);
1412: }
1413: return NumberParsing.decodeLongFromHex(c);
1414: }
1415:
1416: public void nextBytes(byte[] bytes) {
1417: byte[] d = Hex
1418: .decode("ca82cce78a738bc46f103d53b9bf809745ec845e4f6da462606c51f60ecf302e31204b81");
1419:
1420: System.arraycopy(d, 0, bytes, bytes.length - d.length,
1421: d.length);
1422: }
1423: };
1424:
1425: SecureRandom random = new SecureRandom() {
1426: public void nextBytes(byte[] bytes) {
1427: byte[] k = Hex
1428: .decode("90F3A564439242F5186EBB224C8E223811B7105C64E4F5390807E6362DF4C72A");
1429:
1430: int i;
1431:
1432: for (i = 0; i < (bytes.length - k.length); i += k.length) {
1433: System.arraycopy(k, 0, bytes, i, k.length);
1434: }
1435:
1436: if (i > bytes.length) {
1437: System.arraycopy(k, 0, bytes, i - k.length,
1438: bytes.length - (i - k.length));
1439: } else {
1440: System.arraycopy(k, 0, bytes, i, bytes.length - i);
1441: }
1442: }
1443: };
1444:
1445: SecureRandom keyRandom = new SecureRandom() {
1446: public void nextBytes(byte[] bytes) {
1447: byte[] x = Hex
1448: .decode("3036314538303830343630454235324435324234314132373832433138443046");
1449:
1450: int i;
1451:
1452: for (i = 0; i < (bytes.length - x.length); i += x.length) {
1453: System.arraycopy(x, 0, bytes, i, x.length);
1454: }
1455:
1456: if (i > bytes.length) {
1457: System.arraycopy(x, 0, bytes, i - x.length,
1458: bytes.length - (i - x.length));
1459: } else {
1460: System.arraycopy(x, 0, bytes, i, bytes.length - i);
1461: }
1462: }
1463: };
1464:
1465: BigInteger pValue = new BigInteger(
1466: "b194036ace14139d36d64295ae6c50fc4b7d65d8b340711366ca93f383653908ee637be428051d86612670ad7b402c09b820fa77d9da29c8111a8496da6c261a53ed252e4d8a69a20376e6addb3bdcd331749a491a184b8fda6d84c31cf05f9119b5ed35246ea4562d85928ba1136a8d0e5a7e5c764ba8902029a1336c631a1d",
1467: 16);
1468: BigInteger qValue = new BigInteger(
1469: "96120477df0f3896628e6f4a88d83c93204c210ff262bccb7dae450355125259",
1470: 16);
1471:
1472: public TestResult perform() {
1473: BigInteger r = new BigInteger(
1474: "169fdb2dc09f690b71332432bfec806042e258fa9a21dafe73c6abfbc71407d9",
1475: 16);
1476: BigInteger s = new BigInteger(
1477: "9002551808ae40d19f6f31fb67e4563101243cf07cffd5f2f8ff4c537b0c9866",
1478: 16);
1479: GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
1480:
1481: pGen.init(1024, 2, init_random);
1482:
1483: GOST3410Parameters params = pGen.generateParameters();
1484:
1485: if (!pValue.equals(params.getP())
1486: || !qValue.equals(params.getQ())) {
1487: return new SimpleTestResult(false, getName()
1488: + ": p or q wrong");
1489: }
1490:
1491: GOST3410KeyPairGenerator GOST3410KeyGen = new GOST3410KeyPairGenerator();
1492: GOST3410KeyGenerationParameters genParam = new GOST3410KeyGenerationParameters(
1493: keyRandom, params);
1494:
1495: GOST3410KeyGen.init(genParam);
1496:
1497: AsymmetricCipherKeyPair pair = GOST3410KeyGen
1498: .generateKeyPair();
1499:
1500: ParametersWithRandom param = new ParametersWithRandom(pair
1501: .getPrivate(), random);
1502:
1503: GOST3410Signer GOST3410 = new GOST3410Signer();
1504:
1505: GOST3410.init(true, param);
1506:
1507: BigInteger[] sig = GOST3410.generateSignature(hashmessage);
1508:
1509: if (!r.equals(sig[0])) {
1510: return new SimpleTestResult(false, getName()
1511: + ": r component wrong."
1512: + System.getProperty("line.separator")
1513: + " expecting: " + r.toString(16)
1514: + System.getProperty("line.separator")
1515: + " got : " + sig[0].toString(16));
1516: }
1517:
1518: if (!s.equals(sig[1])) {
1519: return new SimpleTestResult(false, getName()
1520: + ": s component wrong."
1521: + System.getProperty("line.separator")
1522: + " expecting: " + s.toString(16)
1523: + System.getProperty("line.separator")
1524: + " got : " + sig[1].toString(16));
1525: }
1526:
1527: GOST3410.init(false, pair.getPublic());
1528:
1529: if (GOST3410.verifySignature(hashmessage, sig[0], sig[1])) {
1530: return new SimpleTestResult(true, getName() + ": Okay");
1531: } else {
1532: return new SimpleTestResult(false, getName()
1533: + ": verification fails");
1534: }
1535: }
1536: }
1537:
1538: Test tests[] = { new GOST3410_TEST1_512(),
1539: new GOST3410_TEST2_512(),
1540: // new GOST3410_TEST1_1024(),
1541: // new GOST3410_TEST2_1024(),
1542: // new GOST3410_AParam(),
1543: // new GOST3410_BParam(),
1544: // new GOST3410_CParam(),
1545: // new GOST3410_DParam(),
1546: // new GOST3410_AExParam(),
1547: // new GOST3410_BExParam(),
1548: // new GOST3410_CExParam()
1549: };
1550:
1551: public String getName() {
1552: return "GOST3410";
1553: }
1554:
1555: public TestResult perform() {
1556: for (int i = 0; i != tests.length; i++) {
1557: TestResult result = tests[i].perform();
1558:
1559: if (!result.isSuccessful()) {
1560: return result;
1561: }
1562: }
1563:
1564: return new SimpleTestResult(true, "GOST3410: Okay");
1565: }
1566:
1567: public static void main(String[] args) {
1568: GOST3410Test test = new GOST3410Test();
1569: TestResult result = test.perform();
1570:
1571: System.out.println(result);
1572: }
1573: }
|