use of com.github.zhenwei.core.crypto.params.GOST3410PublicKeyParameters in project LinLong-Java by zhenwei1108.
the class GOST3410KeyPairGenerator method generateKeyPair.
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 = BigIntegers.createRandomBigInteger(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));
}
use of com.github.zhenwei.core.crypto.params.GOST3410PublicKeyParameters in project LinLong-Java by zhenwei1108.
the class KeyPairGeneratorSpi method generateKeyPair.
public KeyPair generateKeyPair() {
if (!initialised) {
init(new GOST3410ParameterSpec(CryptoProObjectIdentifiers.gostR3410_94_CryptoPro_A.getId()), CryptoServicesRegistrar.getSecureRandom());
}
AsymmetricCipherKeyPair pair = engine.generateKeyPair();
GOST3410PublicKeyParameters pub = (GOST3410PublicKeyParameters) pair.getPublic();
GOST3410PrivateKeyParameters priv = (GOST3410PrivateKeyParameters) pair.getPrivate();
return new KeyPair(new BCGOST3410PublicKey(pub, gost3410Params), new BCGOST3410PrivateKey(priv, gost3410Params));
}
use of com.github.zhenwei.core.crypto.params.GOST3410PublicKeyParameters in project LinLong-Java by zhenwei1108.
the class GOST3410Util method generatePublicKeyParameter.
public static 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());
}
use of com.github.zhenwei.core.crypto.params.GOST3410PublicKeyParameters in project LinLong-Java by zhenwei1108.
the class GOST3410Signer method verifySignature.
/**
* 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) {
// conversion is little-endian
byte[] mRev = Arrays.reverse(message);
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);
}
Aggregations