use of com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class DSASigner method verifySignature.
/**
* return true if the value r and s represent a DSA signature for the passed in message for
* standard DSA the message should be a SHA-1 hash of the real message to be verified.
*/
public boolean verifySignature(byte[] message, BigInteger r, BigInteger s) {
DSAParameters params = key.getParameters();
BigInteger q = params.getQ();
BigInteger m = calculateE(q, message);
BigInteger zero = BigInteger.valueOf(0);
if (zero.compareTo(r) >= 0 || q.compareTo(r) <= 0) {
return false;
}
if (zero.compareTo(s) >= 0 || q.compareTo(s) <= 0) {
return false;
}
BigInteger w = BigIntegers.modOddInverseVar(q, s);
BigInteger u1 = m.multiply(w).mod(q);
BigInteger u2 = r.multiply(w).mod(q);
BigInteger p = params.getP();
u1 = params.getG().modPow(u1, p);
u2 = ((DSAPublicKeyParameters) key).getY().modPow(u2, p);
BigInteger v = u1.multiply(u2).mod(p).mod(q);
return v.equals(r);
}
use of com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class OpenSSHPublicKeyUtil method parsePublicKey.
/**
* Parse a public key from an SSHBuffer instance.
*
* @param buffer containing the SSH public key.
* @return A CipherParameters instance.
*/
public static AsymmetricKeyParameter parsePublicKey(SSHBuffer buffer) {
AsymmetricKeyParameter result = null;
String magic = buffer.readString();
if (RSA.equals(magic)) {
BigInteger e = buffer.readBigNumPositive();
BigInteger n = buffer.readBigNumPositive();
result = new RSAKeyParameters(false, n, e);
} else if (DSS.equals(magic)) {
BigInteger p = buffer.readBigNumPositive();
BigInteger q = buffer.readBigNumPositive();
BigInteger g = buffer.readBigNumPositive();
BigInteger pubKey = buffer.readBigNumPositive();
result = new DSAPublicKeyParameters(pubKey, new DSAParameters(p, q, g));
} else if (magic.startsWith(ECDSA)) {
String curveName = buffer.readString();
ASN1ObjectIdentifier oid = SSHNamedCurves.getByName(curveName);
X9ECParameters x9ECParameters = SSHNamedCurves.getParameters(oid);
if (x9ECParameters == null) {
throw new IllegalStateException("unable to find curve for " + magic + " using curve name " + curveName);
}
ECCurve curve = x9ECParameters.getCurve();
byte[] pointRaw = buffer.readBlock();
result = new ECPublicKeyParameters(curve.decodePoint(pointRaw), new ECNamedDomainParameters(oid, x9ECParameters));
} else if (ED_25519.equals(magic)) {
byte[] pubKeyBytes = buffer.readBlock();
if (pubKeyBytes.length != Ed25519PublicKeyParameters.KEY_SIZE) {
throw new IllegalStateException("public key value of wrong length");
}
result = new Ed25519PublicKeyParameters(pubKeyBytes, 0);
}
if (result == null) {
throw new IllegalArgumentException("unable to parse key");
}
if (buffer.hasRemaining()) {
throw new IllegalArgumentException("decoded key has trailing data");
}
return result;
}
use of com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class BCDSAPublicKey method readObject.
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
BigInteger p = (BigInteger) in.readObject();
if (p.equals(ZERO)) {
this.dsaSpec = null;
} else {
this.dsaSpec = new DSAParameterSpec(p, (BigInteger) in.readObject(), (BigInteger) in.readObject());
}
this.lwKeyParams = new DSAPublicKeyParameters(y, DSAUtil.toDSAParameters(dsaSpec));
}
use of com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class KeyPairGeneratorSpi method generateKeyPair.
public KeyPair generateKeyPair() {
if (!initialised) {
Integer paramStrength = Integers.valueOf(strength);
if (params.containsKey(paramStrength)) {
param = (DSAKeyGenerationParameters) params.get(paramStrength);
} else {
synchronized (lock) {
// our key size.
if (params.containsKey(paramStrength)) {
param = (DSAKeyGenerationParameters) params.get(paramStrength);
} else {
DSAParametersGenerator pGen;
DSAParameterGenerationParameters dsaParams;
int certainty = PrimeCertaintyCalculator.getDefaultCertainty(strength);
// For legacy keysize that is less than 1024-bit, we just use the 186-2 style parameters
if (strength == 1024) {
pGen = new DSAParametersGenerator();
if (Properties.isOverrideSet("org.bouncycastle.dsa.FIPS186-2for1024bits")) {
pGen.init(strength, certainty, random);
} else {
dsaParams = new DSAParameterGenerationParameters(1024, 160, certainty, random);
pGen.init(dsaParams);
}
} else if (strength > 1024) {
dsaParams = new DSAParameterGenerationParameters(strength, 256, certainty, random);
pGen = new DSAParametersGenerator(new SHA256Digest());
pGen.init(dsaParams);
} else {
pGen = new DSAParametersGenerator();
pGen.init(strength, certainty, random);
}
param = new DSAKeyGenerationParameters(random, pGen.generateParameters());
params.put(paramStrength, param);
}
}
}
engine.init(param);
initialised = true;
}
AsymmetricCipherKeyPair pair = engine.generateKeyPair();
DSAPublicKeyParameters pub = (DSAPublicKeyParameters) pair.getPublic();
DSAPrivateKeyParameters priv = (DSAPrivateKeyParameters) pair.getPrivate();
return new KeyPair(new BCDSAPublicKey(pub), new BCDSAPrivateKey(priv));
}
use of com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters in project LinLong-Java by zhenwei1108.
the class DSAKeyPairGenerator method generateKeyPair.
public AsymmetricCipherKeyPair generateKeyPair() {
DSAParameters dsaParams = param.getParameters();
BigInteger x = generatePrivateKey(dsaParams.getQ(), param.getRandom());
BigInteger y = calculatePublicKey(dsaParams.getP(), dsaParams.getG(), x);
return new AsymmetricCipherKeyPair(new DSAPublicKeyParameters(y, dsaParams), new DSAPrivateKeyParameters(x, dsaParams));
}
Aggregations