what-the-bank/sources/org/bouncycastle/crypto/signers/X931Signer.java

142 lines
5.2 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.signers;
import java.math.BigInteger;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.Signer;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.BigIntegers;
/* loaded from: classes6.dex */
public class X931Signer implements Signer {
public static final int TRAILER_IMPLICIT = 188;
public static final int TRAILER_RIPEMD128 = 13004;
public static final int TRAILER_RIPEMD160 = 12748;
public static final int TRAILER_SHA1 = 13260;
public static final int TRAILER_SHA224 = 14540;
public static final int TRAILER_SHA256 = 13516;
public static final int TRAILER_SHA384 = 14028;
public static final int TRAILER_SHA512 = 13772;
public static final int TRAILER_WHIRLPOOL = 14284;
private byte[] block;
private AsymmetricBlockCipher cipher;
private Digest digest;
private RSAKeyParameters kParam;
private int keyBits;
private int trailer;
@Override // org.bouncycastle.crypto.Signer
public boolean verifySignature(byte[] bArr) {
try {
this.block = this.cipher.processBlock(bArr, 0, bArr.length);
BigInteger bigInteger = new BigInteger(1, this.block);
if ((bigInteger.intValue() & 15) != 12) {
bigInteger = this.kParam.getModulus().subtract(bigInteger);
if ((bigInteger.intValue() & 15) != 12) {
return false;
}
}
createSignatureBlock();
byte[] asUnsignedByteArray = BigIntegers.asUnsignedByteArray(this.block.length, bigInteger);
boolean constantTimeAreEqual = Arrays.constantTimeAreEqual(this.block, asUnsignedByteArray);
clearBlock(this.block);
clearBlock(asUnsignedByteArray);
return constantTimeAreEqual;
} catch (Exception unused) {
return false;
}
}
@Override // org.bouncycastle.crypto.Signer
public void update(byte[] bArr, int i, int i2) {
this.digest.update(bArr, i, i2);
}
@Override // org.bouncycastle.crypto.Signer
public void update(byte b) {
this.digest.update(b);
}
@Override // org.bouncycastle.crypto.Signer
public void reset() {
this.digest.reset();
}
@Override // org.bouncycastle.crypto.Signer
public void init(boolean z, CipherParameters cipherParameters) {
RSAKeyParameters rSAKeyParameters = (RSAKeyParameters) cipherParameters;
this.kParam = rSAKeyParameters;
this.cipher.init(z, rSAKeyParameters);
int bitLength = this.kParam.getModulus().bitLength();
this.keyBits = bitLength;
this.block = new byte[(bitLength + 7) / 8];
reset();
}
@Override // org.bouncycastle.crypto.Signer
public byte[] generateSignature() throws CryptoException {
createSignatureBlock();
AsymmetricBlockCipher asymmetricBlockCipher = this.cipher;
byte[] bArr = this.block;
BigInteger bigInteger = new BigInteger(1, asymmetricBlockCipher.processBlock(bArr, 0, bArr.length));
clearBlock(this.block);
return BigIntegers.asUnsignedByteArray((this.kParam.getModulus().bitLength() + 7) / 8, bigInteger.min(this.kParam.getModulus().subtract(bigInteger)));
}
private void createSignatureBlock() {
int length;
int digestSize = this.digest.getDigestSize();
if (this.trailer == 188) {
byte[] bArr = this.block;
length = (bArr.length - digestSize) - 1;
this.digest.doFinal(bArr, length);
this.block[r0.length - 1] = PSSSigner.TRAILER_IMPLICIT;
} else {
byte[] bArr2 = this.block;
length = (bArr2.length - digestSize) - 2;
this.digest.doFinal(bArr2, length);
byte[] bArr3 = this.block;
int length2 = bArr3.length;
int i = this.trailer;
bArr3[length2 - 2] = (byte) (i >>> 8);
bArr3[bArr3.length - 1] = (byte) i;
}
this.block[0] = 107;
for (int i2 = length - 2; i2 != 0; i2--) {
this.block[i2] = -69;
}
this.block[length - 1] = -70;
}
private void clearBlock(byte[] bArr) {
for (int i = 0; i != bArr.length; i++) {
bArr[i] = 0;
}
}
public X931Signer(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, boolean z) {
int intValue;
this.cipher = asymmetricBlockCipher;
this.digest = digest;
if (z) {
intValue = 188;
} else {
Integer trailer = ISOTrailers.getTrailer(digest);
if (trailer == null) {
StringBuilder sb = new StringBuilder("no valid trailer for digest: ");
sb.append(digest.getAlgorithmName());
throw new IllegalArgumentException(sb.toString());
}
intValue = trailer.intValue();
}
this.trailer = intValue;
}
public X931Signer(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest) {
this(asymmetricBlockCipher, digest, false);
}
}