96 lines
4.1 KiB
Java
96 lines
4.1 KiB
Java
package org.bouncycastle.crypto.signers;
|
|
|
|
import java.math.BigInteger;
|
|
import java.security.SecureRandom;
|
|
import org.bouncycastle.crypto.CipherParameters;
|
|
import org.bouncycastle.crypto.DSA;
|
|
import org.bouncycastle.crypto.params.DSAKeyParameters;
|
|
import org.bouncycastle.crypto.params.DSAParameters;
|
|
import org.bouncycastle.crypto.params.DSAPrivateKeyParameters;
|
|
import org.bouncycastle.crypto.params.DSAPublicKeyParameters;
|
|
import org.bouncycastle.crypto.params.ParametersWithRandom;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class DSASigner implements DSA {
|
|
private final DSAKCalculator kCalculator;
|
|
private DSAKeyParameters key;
|
|
private SecureRandom random;
|
|
|
|
@Override // org.bouncycastle.crypto.DSA
|
|
public boolean verifySignature(byte[] bArr, BigInteger bigInteger, BigInteger bigInteger2) {
|
|
DSAParameters parameters = this.key.getParameters();
|
|
BigInteger q = parameters.getQ();
|
|
BigInteger calculateE = calculateE(q, bArr);
|
|
BigInteger valueOf = BigInteger.valueOf(0L);
|
|
if (valueOf.compareTo(bigInteger) >= 0 || q.compareTo(bigInteger) <= 0 || valueOf.compareTo(bigInteger2) >= 0 || q.compareTo(bigInteger2) <= 0) {
|
|
return false;
|
|
}
|
|
BigInteger modInverse = bigInteger2.modInverse(q);
|
|
BigInteger mod = calculateE.multiply(modInverse).mod(q);
|
|
BigInteger mod2 = bigInteger.multiply(modInverse).mod(q);
|
|
BigInteger p = parameters.getP();
|
|
return parameters.getG().modPow(mod, p).multiply(((DSAPublicKeyParameters) this.key).getY().modPow(mod2, p)).mod(p).mod(q).equals(bigInteger);
|
|
}
|
|
|
|
protected SecureRandom initSecureRandom(boolean z, SecureRandom secureRandom) {
|
|
if (z) {
|
|
return secureRandom == null ? new SecureRandom() : secureRandom;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.DSA
|
|
public void init(boolean z, CipherParameters cipherParameters) {
|
|
DSAKeyParameters dSAKeyParameters;
|
|
SecureRandom secureRandom;
|
|
if (!z) {
|
|
dSAKeyParameters = (DSAPublicKeyParameters) cipherParameters;
|
|
} else {
|
|
if (cipherParameters instanceof ParametersWithRandom) {
|
|
ParametersWithRandom parametersWithRandom = (ParametersWithRandom) cipherParameters;
|
|
this.key = (DSAPrivateKeyParameters) parametersWithRandom.getParameters();
|
|
secureRandom = parametersWithRandom.getRandom();
|
|
this.random = initSecureRandom((z || this.kCalculator.isDeterministic()) ? false : true, secureRandom);
|
|
}
|
|
dSAKeyParameters = (DSAPrivateKeyParameters) cipherParameters;
|
|
}
|
|
this.key = dSAKeyParameters;
|
|
secureRandom = null;
|
|
this.random = initSecureRandom((z || this.kCalculator.isDeterministic()) ? false : true, secureRandom);
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.DSA
|
|
public BigInteger[] generateSignature(byte[] bArr) {
|
|
DSAParameters parameters = this.key.getParameters();
|
|
BigInteger q = parameters.getQ();
|
|
BigInteger calculateE = calculateE(q, bArr);
|
|
BigInteger x = ((DSAPrivateKeyParameters) this.key).getX();
|
|
if (this.kCalculator.isDeterministic()) {
|
|
this.kCalculator.init(q, x, bArr);
|
|
} else {
|
|
this.kCalculator.init(q, this.random);
|
|
}
|
|
BigInteger nextK = this.kCalculator.nextK();
|
|
BigInteger mod = parameters.getG().modPow(nextK, parameters.getP()).mod(q);
|
|
return new BigInteger[]{mod, nextK.modInverse(q).multiply(calculateE.add(x.multiply(mod))).mod(q)};
|
|
}
|
|
|
|
private BigInteger calculateE(BigInteger bigInteger, byte[] bArr) {
|
|
if (bigInteger.bitLength() >= (bArr.length << 3)) {
|
|
return new BigInteger(1, bArr);
|
|
}
|
|
int bitLength = bigInteger.bitLength() / 8;
|
|
byte[] bArr2 = new byte[bitLength];
|
|
System.arraycopy(bArr, 0, bArr2, 0, bitLength);
|
|
return new BigInteger(1, bArr2);
|
|
}
|
|
|
|
public DSASigner(DSAKCalculator dSAKCalculator) {
|
|
this.kCalculator = dSAKCalculator;
|
|
}
|
|
|
|
public DSASigner() {
|
|
this.kCalculator = new RandomDSAKCalculator();
|
|
}
|
|
}
|