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

161 lines
6.9 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.signers;
import java.io.IOException;
import java.util.Hashtable;
import org.bouncycastle.asn1.ASN1Encoding;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.DigestInfo;
import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.Signer;
import org.bouncycastle.crypto.encodings.PKCS1Encoding;
import org.bouncycastle.crypto.engines.RSABlindedEngine;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.pqc.jcajce.spec.McElieceCCA2KeyGenParameterSpec;
import org.bouncycastle.pqc.jcajce.spec.SPHINCS256KeyGenParameterSpec;
import org.bouncycastle.util.Arrays;
/* loaded from: classes6.dex */
public class RSADigestSigner implements Signer {
private static final Hashtable oidMap;
private final AlgorithmIdentifier algId;
private final Digest digest;
private boolean forSigning;
private final AsymmetricBlockCipher rsaEngine;
@Override // org.bouncycastle.crypto.Signer
public boolean verifySignature(byte[] bArr) {
byte[] processBlock;
byte[] derEncode;
if (this.forSigning) {
throw new IllegalStateException("RSADigestSigner not initialised for verification");
}
int digestSize = this.digest.getDigestSize();
byte[] bArr2 = new byte[digestSize];
this.digest.doFinal(bArr2, 0);
try {
processBlock = this.rsaEngine.processBlock(bArr, 0, bArr.length);
derEncode = derEncode(bArr2);
} catch (Exception unused) {
}
if (processBlock.length == derEncode.length) {
return Arrays.constantTimeAreEqual(processBlock, derEncode);
}
if (processBlock.length != derEncode.length - 2) {
Arrays.constantTimeAreEqual(derEncode, derEncode);
return false;
}
int length = (processBlock.length - digestSize) - 2;
int length2 = derEncode.length;
derEncode[1] = (byte) (derEncode[1] - 2);
derEncode[3] = (byte) (derEncode[3] - 2);
int i = 0;
for (int i2 = 0; i2 < digestSize; i2++) {
i |= processBlock[length + i2] ^ derEncode[((length2 - digestSize) - 2) + i2];
}
for (int i3 = 0; i3 < length; i3++) {
i |= processBlock[i3] ^ derEncode[i3];
}
return i == 0;
}
@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) {
this.forSigning = z;
AsymmetricKeyParameter asymmetricKeyParameter = cipherParameters instanceof ParametersWithRandom ? (AsymmetricKeyParameter) ((ParametersWithRandom) cipherParameters).getParameters() : (AsymmetricKeyParameter) cipherParameters;
if (z && !asymmetricKeyParameter.isPrivate()) {
throw new IllegalArgumentException("signing requires private key");
}
if (!z && asymmetricKeyParameter.isPrivate()) {
throw new IllegalArgumentException("verification requires public key");
}
reset();
this.rsaEngine.init(z, cipherParameters);
}
public String getAlgorithmName() {
StringBuilder sb = new StringBuilder();
sb.append(this.digest.getAlgorithmName());
sb.append("withRSA");
return sb.toString();
}
@Override // org.bouncycastle.crypto.Signer
public byte[] generateSignature() throws CryptoException, DataLengthException {
if (!this.forSigning) {
throw new IllegalStateException("RSADigestSigner not initialised for signature generation.");
}
byte[] bArr = new byte[this.digest.getDigestSize()];
this.digest.doFinal(bArr, 0);
try {
byte[] derEncode = derEncode(bArr);
return this.rsaEngine.processBlock(derEncode, 0, derEncode.length);
} catch (IOException e) {
StringBuilder sb = new StringBuilder("unable to encode signature: ");
sb.append(e.getMessage());
throw new CryptoException(sb.toString(), e);
}
}
private byte[] derEncode(byte[] bArr) throws IOException {
return new DigestInfo(this.algId, bArr).getEncoded(ASN1Encoding.DER);
}
public RSADigestSigner(Digest digest, ASN1ObjectIdentifier aSN1ObjectIdentifier) {
this.rsaEngine = new PKCS1Encoding(new RSABlindedEngine());
this.digest = digest;
this.algId = new AlgorithmIdentifier(aSN1ObjectIdentifier, DERNull.INSTANCE);
}
public RSADigestSigner(Digest digest) {
this(digest, (ASN1ObjectIdentifier) oidMap.get(digest.getAlgorithmName()));
}
static {
Hashtable hashtable = new Hashtable();
oidMap = hashtable;
hashtable.put("RIPEMD128", TeleTrusTObjectIdentifiers.ripemd128);
hashtable.put("RIPEMD160", TeleTrusTObjectIdentifiers.ripemd160);
hashtable.put("RIPEMD256", TeleTrusTObjectIdentifiers.ripemd256);
hashtable.put(McElieceCCA2KeyGenParameterSpec.SHA1, X509ObjectIdentifiers.id_SHA1);
hashtable.put(McElieceCCA2KeyGenParameterSpec.SHA224, NISTObjectIdentifiers.id_sha224);
hashtable.put(McElieceCCA2KeyGenParameterSpec.SHA256, NISTObjectIdentifiers.id_sha256);
hashtable.put(McElieceCCA2KeyGenParameterSpec.SHA384, NISTObjectIdentifiers.id_sha384);
hashtable.put(McElieceCCA2KeyGenParameterSpec.SHA512, NISTObjectIdentifiers.id_sha512);
hashtable.put("SHA-512/224", NISTObjectIdentifiers.id_sha512_224);
hashtable.put("SHA-512/256", NISTObjectIdentifiers.id_sha512_256);
hashtable.put("SHA3-224", NISTObjectIdentifiers.id_sha3_224);
hashtable.put(SPHINCS256KeyGenParameterSpec.SHA3_256, NISTObjectIdentifiers.id_sha3_256);
hashtable.put("SHA3-384", NISTObjectIdentifiers.id_sha3_384);
hashtable.put("SHA3-512", NISTObjectIdentifiers.id_sha3_512);
hashtable.put("MD2", PKCSObjectIdentifiers.md2);
hashtable.put("MD4", PKCSObjectIdentifiers.md4);
hashtable.put("MD5", PKCSObjectIdentifiers.md5);
}
}