public AsymmetricCipherKeyPair generateKeyPair() { BigInteger p, q, a, x, y; GOST3410Parameters GOST3410Params = param.getParameters(); SecureRandom random = param.getRandom(); q = GOST3410Params.getQ(); p = GOST3410Params.getP(); a = GOST3410Params.getA(); do { x = new BigInteger(256, random); } while (x.equals(ZERO) || x.compareTo(q) >= 0); // // calculate the public key. // y = a.modPow(x, p); return new AsymmetricCipherKeyPair( new GOST3410PublicKeyParameters(y, GOST3410Params), new GOST3410PrivateKeyParameters(x, GOST3410Params)); }
protected AlgorithmParameters engineGenerateParameters() { GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator(); if (random != null) { pGen.init(strength, 2, random); } else { pGen.init(strength, 2, new SecureRandom()); } GOST3410Parameters p = pGen.generateParameters(); AlgorithmParameters params; try { params = AlgorithmParameters.getInstance("GOST3410", BouncyCastleProvider.PROVIDER_NAME); params.init(new GOST3410ParameterSpec(new GOST3410PublicKeyParameterSetSpec(p.getP(), p.getQ(), p.getA()))); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return params; }
private void init( GOST3410ParameterSpec gParams, SecureRandom random) { GOST3410PublicKeyParameterSetSpec spec = gParams.getPublicKeyParameters(); param = new GOST3410KeyGenerationParameters(random, new GOST3410Parameters(spec.getP(), spec.getQ(), spec.getA())); engine.init(param); initialised = true; gost3410Params = gParams; }
static public AsymmetricKeyParameter generatePublicKeyParameter( PublicKey key) throws InvalidKeyException { if (key instanceof GOST3410PublicKey) { GOST3410PublicKey k = (GOST3410PublicKey)key; GOST3410PublicKeyParameterSetSpec p = k.getParameters().getPublicKeyParameters(); return new GOST3410PublicKeyParameters(k.getY(), new GOST3410Parameters(p.getP(), p.getQ(), p.getA())); } throw new InvalidKeyException("can't identify GOST3410 public key: " + key.getClass().getName()); }
static public AsymmetricKeyParameter generatePrivateKeyParameter( PrivateKey key) throws InvalidKeyException { if (key instanceof GOST3410PrivateKey) { GOST3410PrivateKey k = (GOST3410PrivateKey)key; GOST3410PublicKeyParameterSetSpec p = k.getParameters().getPublicKeyParameters(); return new GOST3410PrivateKeyParameters(k.getX(), new GOST3410Parameters(p.getP(), p.getQ(), p.getA())); } throw new InvalidKeyException("can't identify GOST3410 private key."); }
/** * generate a signature for the given message using the key we were * initialised with. For conventional GOST3410 the message should be a GOST3411 * hash of the message of interest. * * @param message the message that will be verified later. */ public BigInteger[] generateSignature( byte[] message) { byte[] mRev = new byte[message.length]; // conversion is little-endian for (int i = 0; i != mRev.length; i++) { mRev[i] = message[mRev.length - 1 - i]; } BigInteger m = new BigInteger(1, mRev); GOST3410Parameters params = key.getParameters(); BigInteger k; do { k = new BigInteger(params.getQ().bitLength(), random); } while (k.compareTo(params.getQ()) >= 0); BigInteger r = params.getA().modPow(k, params.getP()).mod(params.getQ()); BigInteger s = k.multiply(m). add(((GOST3410PrivateKeyParameters)key).getX().multiply(r)). mod(params.getQ()); BigInteger[] res = new BigInteger[2]; res[0] = r; res[1] = s; return res; }
/** * return true if the value r and s represent a GOST3410 signature for * the passed in message for standard GOST3410 the message should be a * GOST3411 hash of the real message to be verified. */ public boolean verifySignature( byte[] message, BigInteger r, BigInteger s) { byte[] mRev = new byte[message.length]; // conversion is little-endian for (int i = 0; i != mRev.length; i++) { mRev[i] = message[mRev.length - 1 - i]; } BigInteger m = new BigInteger(1, mRev); GOST3410Parameters params = key.getParameters(); BigInteger zero = BigInteger.valueOf(0); if (zero.compareTo(r) >= 0 || params.getQ().compareTo(r) <= 0) { return false; } if (zero.compareTo(s) >= 0 || params.getQ().compareTo(s) <= 0) { return false; } BigInteger v = m.modPow(params.getQ().subtract(new BigInteger("2")),params.getQ()); BigInteger z1 = s.multiply(v).mod(params.getQ()); BigInteger z2 = (params.getQ().subtract(r)).multiply(v).mod(params.getQ()); z1 = params.getA().modPow(z1, params.getP()); z2 = ((GOST3410PublicKeyParameters)key).getY().modPow(z2, params.getP()); BigInteger u = z1.multiply(z2).mod(params.getP()).mod(params.getQ()); return u.equals(r); }
public AsymmetricCipherKeyPair generateKeyPair() { BigInteger p, q, a, x, y; GOST3410Parameters GOST3410Params = param.getParameters(); SecureRandom random = param.getRandom(); q = GOST3410Params.getQ(); p = GOST3410Params.getP(); a = GOST3410Params.getA(); int minWeight = 64; for (;;) { x = new BigInteger(256, random); if (x.signum() < 1 || x.compareTo(q) >= 0) { continue; } if (WNafUtil.getNafWeight(x) < minWeight) { continue; } break; } // // calculate the public key. // y = a.modPow(x, p); return new AsymmetricCipherKeyPair( new GOST3410PublicKeyParameters(y, GOST3410Params), new GOST3410PrivateKeyParameters(x, GOST3410Params)); }
protected AlgorithmParameters engineGenerateParameters() { GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator(); if (random != null) { pGen.init(strength, 2, random); } else { pGen.init(strength, 2, new SecureRandom()); } GOST3410Parameters p = pGen.generateParameters(); AlgorithmParameters params; try { params = createParametersInstance("GOST3410"); params.init(new GOST3410ParameterSpec(new GOST3410PublicKeyParameterSetSpec(p.getP(), p.getQ(), p.getA()))); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return params; }
/** * which generates the p , q and a values from the given parameters, * returning the GOST3410Parameters object. */ public GOST3410Parameters generateParameters() { BigInteger [] pq = new BigInteger[2]; BigInteger q = null, p = null, a = null; int x0, c; long x0L, cL; if (typeproc==1) { x0 = init_random.nextInt(); c = init_random.nextInt(); switch(size) { case 512: procedure_A(x0, c, pq, 512); break; case 1024: procedure_B(x0, c, pq); break; default: throw new IllegalArgumentException("Ooops! key size 512 or 1024 bit."); } p = pq[0]; q = pq[1]; a = procedure_C(p, q); //System.out.println("p:"+p.toString(16)+"\n"+"q:"+q.toString(16)+"\n"+"a:"+a.toString(16)); //System.out.println("p:"+p+"\n"+"q:"+q+"\n"+"a:"+a); return new GOST3410Parameters(p, q, a, new GOST3410ValidationParameters(x0, c)); } else { x0L = init_random.nextLong(); cL = init_random.nextLong(); switch(size) { case 512: procedure_Aa(x0L, cL, pq, 512); break; case 1024: procedure_Bb(x0L, cL, pq); break; default: throw new IllegalStateException("Ooops! key size 512 or 1024 bit."); } p = pq[0]; q = pq[1]; a = procedure_C(p, q); //System.out.println("p:"+p.toString(16)+"\n"+"q:"+q.toString(16)+"\n"+"a:"+a.toString(16)); //System.out.println("p:"+p+"\n"+"q:"+q+"\n"+"a:"+a); return new GOST3410Parameters(p, q, a, new GOST3410ValidationParameters(x0L, cL)); } }
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." + Strings.lineSeparator() + " expecting: " + r.toString(16) + Strings.lineSeparator() + " got : " + sig[0].toString(16)); } if (!s.equals(sig[1])) { return new SimpleTestResult(false, getName() + ": s component wrong." + Strings.lineSeparator() + " expecting: " + s.toString(16) + Strings.lineSeparator() + " 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"); } }
public TestResult perform() { BigInteger r = new BigInteger("81d69a192e9c7ac21fc07da41bd07e230ba6a94eb9f3c1fd104c7bd976733ca5",16); BigInteger s = new BigInteger("315c879c8414f35feb4deb15e7cc0278c48e6ca1596325d6959338d860b0c47a",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." + Strings.lineSeparator() + " expecting: " + r.toString(16) + Strings.lineSeparator() + " got : " + sig[0].toString(16)); } if (!s.equals(sig[1])) { return new SimpleTestResult(false, getName() + ": s component wrong." + Strings.lineSeparator() + " expecting: " + s.toString(16) + Strings.lineSeparator() + " 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"); } }
public TestResult perform() { BigInteger r = new BigInteger("64a8856628e5669d85f62cd763dd4a99bc56d33dc0e1859122855d141e9e4774",16); BigInteger s = new BigInteger("319ebac97092b288d469a4b988248794f60c865bc97858d9a3135c6d1a1bf2dd",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." + Strings.lineSeparator() + " expecting: " + r.toString(16) + Strings.lineSeparator() + " got : " + sig[0].toString(16)); } if (!s.equals(sig[1])) { return new SimpleTestResult(false, getName() + ": s component wrong." + Strings.lineSeparator() + " expecting: " + s.toString(16) + Strings.lineSeparator() + " 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"); } }
public TestResult perform() { BigInteger r = new BigInteger("860d82c60e9502cd00c0e9e1f6563feafec304801974d745c5e02079946f729e",16); BigInteger s = new BigInteger("7ef49264ef022801aaa03033cd97915235fbab4c823ed936b0f360c22114688a",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." + Strings.lineSeparator() + " expecting: " + r.toString(16) + Strings.lineSeparator() + " got : " + sig[0].toString(16)); } if (!s.equals(sig[1])) { return new SimpleTestResult(false, getName() + ": s component wrong." + Strings.lineSeparator() + " expecting: " + s.toString(16) + Strings.lineSeparator() + " 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"); } }
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." + Strings.lineSeparator() + " expecting: " + r.toString(16) + Strings.lineSeparator() + " got : " + sig[0].toString(16)); } if (!s.equals(sig[1])) { return new SimpleTestResult(false, getName() + ": s component wrong." + Strings.lineSeparator() + " expecting: " + s.toString(16) + Strings.lineSeparator() + " 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"); } }
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." + Strings.lineSeparator() + " expecting: " + r.toString(16) + Strings.lineSeparator() + " got : " + sig[0].toString(16)); } if (!s.equals(sig[1])) { return new SimpleTestResult(false, getName() + ": s component wrong." + Strings.lineSeparator() + " expecting: " + s.toString(16) + Strings.lineSeparator() + " 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"); } }
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." + Strings.lineSeparator() + " expecting: " + r.toString(16) + Strings.lineSeparator() + " got : " + sig[0].toString(16)); } if (!s.equals(sig[1])) { return new SimpleTestResult(false, getName() + ": s component wrong." + Strings.lineSeparator() + " expecting: " + s.toString(16) + Strings.lineSeparator() + " 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"); } }