what-the-bank/sources/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java

772 lines
38 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.jcajce.provider.symmetric.util;
import com.google.android.gms.stats.CodePackage;
import java.lang.reflect.Constructor;
import java.nio.ByteBuffer;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.crypto.spec.RC2ParameterSpec;
import javax.crypto.spec.RC5ParameterSpec;
import org.bouncycastle.asn1.cms.GCMParameters;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.OutputLengthException;
import org.bouncycastle.crypto.modes.AEADBlockCipher;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.modes.CCMBlockCipher;
import org.bouncycastle.crypto.modes.CFBBlockCipher;
import org.bouncycastle.crypto.modes.CTSBlockCipher;
import org.bouncycastle.crypto.modes.EAXBlockCipher;
import org.bouncycastle.crypto.modes.GCFBBlockCipher;
import org.bouncycastle.crypto.modes.GCMBlockCipher;
import org.bouncycastle.crypto.modes.GOFBBlockCipher;
import org.bouncycastle.crypto.modes.OCBBlockCipher;
import org.bouncycastle.crypto.modes.OFBBlockCipher;
import org.bouncycastle.crypto.modes.OpenPGPCFBBlockCipher;
import org.bouncycastle.crypto.modes.PGPCFBBlockCipher;
import org.bouncycastle.crypto.modes.SICBlockCipher;
import org.bouncycastle.crypto.paddings.BlockCipherPadding;
import org.bouncycastle.crypto.paddings.ISO10126d2Padding;
import org.bouncycastle.crypto.paddings.ISO7816d4Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.paddings.TBCPadding;
import org.bouncycastle.crypto.paddings.X923Padding;
import org.bouncycastle.crypto.paddings.ZeroBytePadding;
import org.bouncycastle.crypto.params.AEADParameters;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.crypto.params.ParametersWithSBox;
import org.bouncycastle.jcajce.spec.GOST28147ParameterSpec;
import org.bouncycastle.util.Strings;
/* loaded from: classes6.dex */
public class BaseBlockCipher extends BaseWrapCipher implements PBE {
private static final Class gcmSpecClass = lookup("javax.crypto.spec.GCMParameterSpec");
private AEADParameters aeadParams;
private Class[] availableSpecs;
private BlockCipher baseEngine;
private GenericBlockCipher cipher;
private int digest;
private BlockCipherProvider engineProvider;
private boolean fixedIv;
private int ivLength;
private ParametersWithIV ivParam;
private int keySizeInBits;
private String modeName;
private boolean padded;
private String pbeAlgorithm;
private PBEParameterSpec pbeSpec;
private int scheme;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public interface GenericBlockCipher {
int doFinal(byte[] bArr, int i) throws IllegalStateException, BadPaddingException;
String getAlgorithmName();
int getOutputSize(int i);
BlockCipher getUnderlyingCipher();
int getUpdateOutputSize(int i);
void init(boolean z, CipherParameters cipherParameters) throws IllegalArgumentException;
int processByte(byte b, byte[] bArr, int i) throws DataLengthException;
int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws DataLengthException;
void updateAAD(byte[] bArr, int i, int i2);
boolean wrapOnNoPadding();
}
@Override // javax.crypto.CipherSpi
protected void engineUpdateAAD(byte[] bArr, int i, int i2) {
this.cipher.updateAAD(bArr, i, i2);
}
@Override // javax.crypto.CipherSpi
protected void engineUpdateAAD(ByteBuffer byteBuffer) {
engineUpdateAAD(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), byteBuffer.limit() - byteBuffer.position());
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected byte[] engineUpdate(byte[] bArr, int i, int i2) {
int updateOutputSize = this.cipher.getUpdateOutputSize(i2);
if (updateOutputSize <= 0) {
this.cipher.processBytes(bArr, i, i2, null, 0);
return null;
}
byte[] bArr2 = new byte[updateOutputSize];
int processBytes = this.cipher.processBytes(bArr, i, i2, bArr2, 0);
if (processBytes == 0) {
return null;
}
if (processBytes == updateOutputSize) {
return bArr2;
}
byte[] bArr3 = new byte[processBytes];
System.arraycopy(bArr2, 0, bArr3, 0, processBytes);
return bArr3;
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected int engineUpdate(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws ShortBufferException {
if (this.cipher.getUpdateOutputSize(i2) + i3 > bArr2.length) {
throw new ShortBufferException("output buffer too short for input.");
}
try {
return this.cipher.processBytes(bArr, i, i2, bArr2, i3);
} catch (DataLengthException e) {
throw new IllegalStateException(e.toString());
}
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected void engineSetPadding(String str) throws NoSuchPaddingException {
BufferedGenericBlockCipher bufferedGenericBlockCipher;
String upperCase = Strings.toUpperCase(str);
if (upperCase.equals("NOPADDING")) {
if (!this.cipher.wrapOnNoPadding()) {
return;
} else {
bufferedGenericBlockCipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(this.cipher.getUnderlyingCipher()));
}
} else if (upperCase.equals("WITHCTS")) {
bufferedGenericBlockCipher = new BufferedGenericBlockCipher(new CTSBlockCipher(this.cipher.getUnderlyingCipher()));
} else {
this.padded = true;
if (isAEADModeName(this.modeName)) {
throw new NoSuchPaddingException("Only NoPadding can be used with AEAD modes.");
}
if (upperCase.equals("PKCS5PADDING") || upperCase.equals("PKCS7PADDING")) {
bufferedGenericBlockCipher = new BufferedGenericBlockCipher(this.cipher.getUnderlyingCipher());
} else if (upperCase.equals("ZEROBYTEPADDING")) {
bufferedGenericBlockCipher = new BufferedGenericBlockCipher(this.cipher.getUnderlyingCipher(), new ZeroBytePadding());
} else if (upperCase.equals("ISO10126PADDING") || upperCase.equals("ISO10126-2PADDING")) {
bufferedGenericBlockCipher = new BufferedGenericBlockCipher(this.cipher.getUnderlyingCipher(), new ISO10126d2Padding());
} else if (upperCase.equals("X9.23PADDING") || upperCase.equals("X923PADDING")) {
bufferedGenericBlockCipher = new BufferedGenericBlockCipher(this.cipher.getUnderlyingCipher(), new X923Padding());
} else if (upperCase.equals("ISO7816-4PADDING") || upperCase.equals("ISO9797-1PADDING")) {
bufferedGenericBlockCipher = new BufferedGenericBlockCipher(this.cipher.getUnderlyingCipher(), new ISO7816d4Padding());
} else {
if (!upperCase.equals("TBCPADDING")) {
StringBuilder sb = new StringBuilder("Padding ");
sb.append(str);
sb.append(" unknown.");
throw new NoSuchPaddingException(sb.toString());
}
bufferedGenericBlockCipher = new BufferedGenericBlockCipher(this.cipher.getUnderlyingCipher(), new TBCPadding());
}
}
this.cipher = bufferedGenericBlockCipher;
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected void engineSetMode(String str) throws NoSuchAlgorithmException {
GenericBlockCipher aEADGenericBlockCipher;
BufferedGenericBlockCipher bufferedGenericBlockCipher;
BufferedGenericBlockCipher bufferedGenericBlockCipher2;
String upperCase = Strings.toUpperCase(str);
this.modeName = upperCase;
if (upperCase.equals("ECB")) {
this.ivLength = 0;
aEADGenericBlockCipher = new BufferedGenericBlockCipher(this.baseEngine);
} else if (this.modeName.equals("CBC")) {
this.ivLength = this.baseEngine.getBlockSize();
aEADGenericBlockCipher = new BufferedGenericBlockCipher(new CBCBlockCipher(this.baseEngine));
} else {
if (!this.modeName.startsWith("OFB")) {
if (this.modeName.startsWith("CFB")) {
this.ivLength = this.baseEngine.getBlockSize();
if (this.modeName.length() != 3) {
bufferedGenericBlockCipher = new BufferedGenericBlockCipher(new CFBBlockCipher(this.baseEngine, Integer.parseInt(this.modeName.substring(3))));
} else {
BlockCipher blockCipher = this.baseEngine;
bufferedGenericBlockCipher2 = new BufferedGenericBlockCipher(new CFBBlockCipher(blockCipher, blockCipher.getBlockSize() << 3));
aEADGenericBlockCipher = bufferedGenericBlockCipher2;
}
} else if (this.modeName.startsWith("PGP")) {
boolean equalsIgnoreCase = this.modeName.equalsIgnoreCase("PGPCFBwithIV");
this.ivLength = this.baseEngine.getBlockSize();
bufferedGenericBlockCipher = new BufferedGenericBlockCipher(new PGPCFBBlockCipher(this.baseEngine, equalsIgnoreCase));
} else if (this.modeName.equalsIgnoreCase("OpenPGPCFB")) {
this.ivLength = 0;
aEADGenericBlockCipher = new BufferedGenericBlockCipher(new OpenPGPCFBBlockCipher(this.baseEngine));
} else if (this.modeName.startsWith("SIC")) {
int blockSize = this.baseEngine.getBlockSize();
this.ivLength = blockSize;
if (blockSize < 16) {
throw new IllegalArgumentException("Warning: SIC-Mode can become a twotime-pad if the blocksize of the cipher is too small. Use a cipher with a block size of at least 128 bits (e.g. AES)");
}
this.fixedIv = false;
aEADGenericBlockCipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new SICBlockCipher(this.baseEngine)));
} else if (this.modeName.startsWith("CTR")) {
this.ivLength = this.baseEngine.getBlockSize();
this.fixedIv = false;
aEADGenericBlockCipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new SICBlockCipher(this.baseEngine)));
} else if (this.modeName.startsWith("GOFB")) {
this.ivLength = this.baseEngine.getBlockSize();
aEADGenericBlockCipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new GOFBBlockCipher(this.baseEngine)));
} else if (this.modeName.startsWith("GCFB")) {
this.ivLength = this.baseEngine.getBlockSize();
aEADGenericBlockCipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new GCFBBlockCipher(this.baseEngine)));
} else if (this.modeName.startsWith("CTS")) {
this.ivLength = this.baseEngine.getBlockSize();
aEADGenericBlockCipher = new BufferedGenericBlockCipher(new CTSBlockCipher(new CBCBlockCipher(this.baseEngine)));
} else if (this.modeName.startsWith("CCM")) {
this.ivLength = 13;
aEADGenericBlockCipher = new AEADGenericBlockCipher(new CCMBlockCipher(this.baseEngine));
} else if (this.modeName.startsWith("OCB")) {
if (this.engineProvider == null) {
throw new NoSuchAlgorithmException("can't support mode ".concat(String.valueOf(str)));
}
this.ivLength = 15;
aEADGenericBlockCipher = new AEADGenericBlockCipher(new OCBBlockCipher(this.baseEngine, this.engineProvider.get()));
} else if (this.modeName.startsWith("EAX")) {
this.ivLength = this.baseEngine.getBlockSize();
aEADGenericBlockCipher = new AEADGenericBlockCipher(new EAXBlockCipher(this.baseEngine));
} else {
if (!this.modeName.startsWith(CodePackage.GCM)) {
throw new NoSuchAlgorithmException("can't support mode ".concat(String.valueOf(str)));
}
this.ivLength = this.baseEngine.getBlockSize();
aEADGenericBlockCipher = new AEADGenericBlockCipher(new GCMBlockCipher(this.baseEngine));
}
this.cipher = bufferedGenericBlockCipher;
return;
}
this.ivLength = this.baseEngine.getBlockSize();
if (this.modeName.length() != 3) {
bufferedGenericBlockCipher = new BufferedGenericBlockCipher(new OFBBlockCipher(this.baseEngine, Integer.parseInt(this.modeName.substring(3))));
this.cipher = bufferedGenericBlockCipher;
return;
} else {
BlockCipher blockCipher2 = this.baseEngine;
bufferedGenericBlockCipher2 = new BufferedGenericBlockCipher(new OFBBlockCipher(blockCipher2, blockCipher2.getBlockSize() << 3));
aEADGenericBlockCipher = bufferedGenericBlockCipher2;
}
}
this.cipher = aEADGenericBlockCipher;
}
/* JADX WARN: Code restructure failed: missing block: B:146:0x01de, code lost:
r21.ivParam = (org.bouncycastle.crypto.params.ParametersWithIV) r5;
r5 = r5;
*/
/* JADX WARN: Code restructure failed: missing block: B:156:0x00d1, code lost:
if (r7 != false) goto L95;
*/
/* JADX WARN: Code restructure failed: missing block: B:170:0x0120, code lost:
if (r7 != false) goto L95;
*/
/* JADX WARN: Code restructure failed: missing block: B:210:0x01dc, code lost:
if (r7 != false) goto L95;
*/
/* JADX WARN: Code restructure failed: missing block: B:25:0x0089, code lost:
if (r7 != false) goto L95;
*/
/* JADX WARN: Multi-variable type inference failed */
/* JADX WARN: Removed duplicated region for block: B:45:0x03c7 */
/* JADX WARN: Removed duplicated region for block: B:60:0x040b */
/* JADX WARN: Removed duplicated region for block: B:65:0x0417 */
/* JADX WARN: Type inference failed for: r2v54, types: [org.bouncycastle.crypto.params.ParametersWithIV] */
/* JADX WARN: Type inference failed for: r5v0 */
/* JADX WARN: Type inference failed for: r5v12 */
/* JADX WARN: Type inference failed for: r5v15, types: [org.bouncycastle.crypto.params.RC5Parameters, org.bouncycastle.crypto.CipherParameters] */
/* JADX WARN: Type inference failed for: r5v16, types: [org.bouncycastle.crypto.params.RC2Parameters, org.bouncycastle.crypto.CipherParameters] */
/* JADX WARN: Type inference failed for: r5v17 */
/* JADX WARN: Type inference failed for: r5v18, types: [org.bouncycastle.crypto.params.ParametersWithSBox, org.bouncycastle.crypto.CipherParameters] */
/* JADX WARN: Type inference failed for: r5v19, types: [org.bouncycastle.crypto.CipherParameters] */
/* JADX WARN: Type inference failed for: r5v24, types: [org.bouncycastle.crypto.params.ParametersWithIV] */
/* JADX WARN: Type inference failed for: r5v25 */
/* JADX WARN: Type inference failed for: r5v44 */
/* JADX WARN: Type inference failed for: r5v45 */
/* JADX WARN: Type inference failed for: r5v46 */
/* JADX WARN: Type inference failed for: r5v47 */
/* JADX WARN: Type inference failed for: r5v48 */
/* JADX WARN: Type inference failed for: r5v49 */
/* JADX WARN: Type inference failed for: r5v9, types: [org.bouncycastle.crypto.CipherParameters] */
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct add '--show-bad-code' argument
*/
protected void engineInit(int r22, java.security.Key r23, java.security.spec.AlgorithmParameterSpec r24, java.security.SecureRandom r25) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException {
/*
Method dump skipped, instructions count: 1139
To view this dump add '--comments-level debug' option
*/
throw new UnsupportedOperationException("Method not decompiled: org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(int, java.security.Key, java.security.spec.AlgorithmParameterSpec, java.security.SecureRandom):void");
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected void engineInit(int i, Key key, SecureRandom secureRandom) throws InvalidKeyException {
try {
engineInit(i, key, (AlgorithmParameterSpec) null, secureRandom);
} catch (InvalidAlgorithmParameterException e) {
throw new InvalidKeyException(e.getMessage());
}
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected void engineInit(int i, Key key, AlgorithmParameters algorithmParameters, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException {
AlgorithmParameterSpec algorithmParameterSpec = null;
if (algorithmParameters != null) {
int i2 = 0;
while (true) {
Class[] clsArr = this.availableSpecs;
if (i2 == clsArr.length) {
break;
}
Class cls = clsArr[i2];
if (cls != null) {
try {
algorithmParameterSpec = algorithmParameters.getParameterSpec(cls);
break;
} catch (Exception unused) {
continue;
}
}
i2++;
}
if (algorithmParameterSpec == null) {
StringBuilder sb = new StringBuilder("can't handle parameter ");
sb.append(algorithmParameters.toString());
throw new InvalidAlgorithmParameterException(sb.toString());
}
}
engineInit(i, key, algorithmParameterSpec, secureRandom);
this.engineParams = algorithmParameters;
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected AlgorithmParameters engineGetParameters() {
if (this.engineParams == null) {
if (this.pbeSpec != null) {
try {
this.engineParams = createParametersInstance(this.pbeAlgorithm);
this.engineParams.init(this.pbeSpec);
} catch (Exception unused) {
return null;
}
} else if (this.ivParam != null) {
String algorithmName = this.cipher.getUnderlyingCipher().getAlgorithmName();
if (algorithmName.indexOf(47) >= 0) {
algorithmName = algorithmName.substring(0, algorithmName.indexOf(47));
}
try {
this.engineParams = createParametersInstance(algorithmName);
this.engineParams.init(this.ivParam.getIV());
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
} else if (this.aeadParams != null) {
try {
this.engineParams = createParametersInstance(CodePackage.GCM);
this.engineParams.init(new GCMParameters(this.aeadParams.getNonce(), this.aeadParams.getMacSize() / 8).getEncoded());
} catch (Exception e2) {
throw new RuntimeException(e2.toString());
}
}
}
return this.engineParams;
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected int engineGetOutputSize(int i) {
return this.cipher.getOutputSize(i);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected int engineGetKeySize(Key key) {
return key.getEncoded().length << 3;
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected byte[] engineGetIV() {
AEADParameters aEADParameters = this.aeadParams;
if (aEADParameters != null) {
return aEADParameters.getNonce();
}
ParametersWithIV parametersWithIV = this.ivParam;
if (parametersWithIV != null) {
return parametersWithIV.getIV();
}
return null;
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected int engineGetBlockSize() {
return this.baseEngine.getBlockSize();
}
/* loaded from: classes6.dex */
static class BufferedGenericBlockCipher implements GenericBlockCipher {
private BufferedBlockCipher cipher;
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public boolean wrapOnNoPadding() {
return !(this.cipher instanceof CTSBlockCipher);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public void updateAAD(byte[] bArr, int i, int i2) {
throw new UnsupportedOperationException("AAD is not supported in the current mode.");
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws DataLengthException {
return this.cipher.processBytes(bArr, i, i2, bArr2, i3);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public int processByte(byte b, byte[] bArr, int i) throws DataLengthException {
return this.cipher.processByte(b, bArr, i);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public void init(boolean z, CipherParameters cipherParameters) throws IllegalArgumentException {
this.cipher.init(z, cipherParameters);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public int getUpdateOutputSize(int i) {
return this.cipher.getUpdateOutputSize(i);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public BlockCipher getUnderlyingCipher() {
return this.cipher.getUnderlyingCipher();
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public int getOutputSize(int i) {
return this.cipher.getOutputSize(i);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public String getAlgorithmName() {
return this.cipher.getUnderlyingCipher().getAlgorithmName();
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public int doFinal(byte[] bArr, int i) throws IllegalStateException, BadPaddingException {
try {
return this.cipher.doFinal(bArr, i);
} catch (InvalidCipherTextException e) {
throw new BadPaddingException(e.getMessage());
}
}
BufferedGenericBlockCipher(BufferedBlockCipher bufferedBlockCipher) {
this.cipher = bufferedBlockCipher;
}
BufferedGenericBlockCipher(BlockCipher blockCipher, BlockCipherPadding blockCipherPadding) {
this.cipher = new PaddedBufferedBlockCipher(blockCipher, blockCipherPadding);
}
BufferedGenericBlockCipher(BlockCipher blockCipher) {
this.cipher = new PaddedBufferedBlockCipher(blockCipher);
}
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected byte[] engineDoFinal(byte[] bArr, int i, int i2) throws IllegalBlockSizeException, BadPaddingException {
int engineGetOutputSize = engineGetOutputSize(i2);
byte[] bArr2 = new byte[engineGetOutputSize];
int processBytes = i2 != 0 ? this.cipher.processBytes(bArr, i, i2, bArr2, 0) : 0;
try {
int doFinal = processBytes + this.cipher.doFinal(bArr2, processBytes);
if (doFinal == engineGetOutputSize) {
return bArr2;
}
byte[] bArr3 = new byte[doFinal];
System.arraycopy(bArr2, 0, bArr3, 0, doFinal);
return bArr3;
} catch (DataLengthException e) {
throw new IllegalBlockSizeException(e.getMessage());
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public static class AEADGenericBlockCipher implements GenericBlockCipher {
private static final Constructor aeadBadTagConstructor;
private AEADBlockCipher cipher;
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public boolean wrapOnNoPadding() {
return false;
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public void updateAAD(byte[] bArr, int i, int i2) {
this.cipher.processAADBytes(bArr, i, i2);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws DataLengthException {
return this.cipher.processBytes(bArr, i, i2, bArr2, i3);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public int processByte(byte b, byte[] bArr, int i) throws DataLengthException {
return this.cipher.processByte(b, bArr, i);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public void init(boolean z, CipherParameters cipherParameters) throws IllegalArgumentException {
this.cipher.init(z, cipherParameters);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public int getUpdateOutputSize(int i) {
return this.cipher.getUpdateOutputSize(i);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public BlockCipher getUnderlyingCipher() {
return this.cipher.getUnderlyingCipher();
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public int getOutputSize(int i) {
return this.cipher.getOutputSize(i);
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public String getAlgorithmName() {
return this.cipher.getUnderlyingCipher().getAlgorithmName();
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.GenericBlockCipher
public int doFinal(byte[] bArr, int i) throws IllegalStateException, BadPaddingException {
BadPaddingException badPaddingException;
try {
return this.cipher.doFinal(bArr, i);
} catch (InvalidCipherTextException e) {
Constructor constructor = aeadBadTagConstructor;
if (constructor != null) {
try {
badPaddingException = (BadPaddingException) constructor.newInstance(e.getMessage());
} catch (Exception unused) {
badPaddingException = null;
}
if (badPaddingException != null) {
throw badPaddingException;
}
}
throw new BadPaddingException(e.getMessage());
}
}
private static Constructor findExceptionConstructor(Class cls) {
try {
return cls.getConstructor(String.class);
} catch (Exception unused) {
return null;
}
}
AEADGenericBlockCipher(AEADBlockCipher aEADBlockCipher) {
this.cipher = aEADBlockCipher;
}
static {
Class lookup = BaseBlockCipher.lookup("javax.crypto.AEADBadTagException");
aeadBadTagConstructor = lookup != null ? findExceptionConstructor(lookup) : null;
}
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected int engineDoFinal(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws IllegalBlockSizeException, BadPaddingException, ShortBufferException {
int processBytes;
if (engineGetOutputSize(i2) + i3 > bArr2.length) {
throw new ShortBufferException("output buffer too short for input.");
}
if (i2 != 0) {
try {
processBytes = this.cipher.processBytes(bArr, i, i2, bArr2, i3);
} catch (OutputLengthException e) {
throw new IllegalBlockSizeException(e.getMessage());
} catch (DataLengthException e2) {
throw new IllegalBlockSizeException(e2.getMessage());
}
} else {
processBytes = 0;
}
return processBytes + this.cipher.doFinal(bArr2, i3 + processBytes);
}
/* JADX INFO: Access modifiers changed from: private */
public static Class lookup(String str) {
try {
return BaseBlockCipher.class.getClassLoader().loadClass(str);
} catch (Exception unused) {
return null;
}
}
private boolean isAEADModeName(String str) {
return "CCM".equals(str) || "EAX".equals(str) || CodePackage.GCM.equals(str) || "OCB".equals(str);
}
/* JADX WARN: Multi-variable type inference failed */
private CipherParameters adjustParameters(AlgorithmParameterSpec algorithmParameterSpec, CipherParameters cipherParameters) {
ParametersWithSBox parametersWithSBox;
ParametersWithIV parametersWithIV;
if (cipherParameters instanceof ParametersWithIV) {
CipherParameters parameters = ((ParametersWithIV) cipherParameters).getParameters();
if (algorithmParameterSpec instanceof IvParameterSpec) {
parametersWithIV = new ParametersWithIV(parameters, ((IvParameterSpec) algorithmParameterSpec).getIV());
} else {
if (!(algorithmParameterSpec instanceof GOST28147ParameterSpec)) {
return cipherParameters;
}
GOST28147ParameterSpec gOST28147ParameterSpec = (GOST28147ParameterSpec) algorithmParameterSpec;
ParametersWithSBox parametersWithSBox2 = new ParametersWithSBox(cipherParameters, gOST28147ParameterSpec.getSbox());
if (gOST28147ParameterSpec.getIV() == null || this.ivLength == 0) {
return parametersWithSBox2;
}
parametersWithIV = new ParametersWithIV(parameters, gOST28147ParameterSpec.getIV());
}
this.ivParam = parametersWithIV;
return parametersWithIV;
}
if (algorithmParameterSpec instanceof IvParameterSpec) {
ParametersWithIV parametersWithIV2 = new ParametersWithIV(cipherParameters, ((IvParameterSpec) algorithmParameterSpec).getIV());
this.ivParam = parametersWithIV2;
parametersWithSBox = parametersWithIV2;
} else {
if (!(algorithmParameterSpec instanceof GOST28147ParameterSpec)) {
return cipherParameters;
}
GOST28147ParameterSpec gOST28147ParameterSpec2 = (GOST28147ParameterSpec) algorithmParameterSpec;
ParametersWithSBox parametersWithSBox3 = new ParametersWithSBox(cipherParameters, gOST28147ParameterSpec2.getSbox());
parametersWithSBox = parametersWithSBox3;
if (gOST28147ParameterSpec2.getIV() != null) {
parametersWithSBox = parametersWithSBox3;
if (this.ivLength != 0) {
return new ParametersWithIV(parametersWithSBox3, gOST28147ParameterSpec2.getIV());
}
}
}
return parametersWithSBox;
}
public BaseBlockCipher(BlockCipherProvider blockCipherProvider) {
this.availableSpecs = new Class[]{RC2ParameterSpec.class, RC5ParameterSpec.class, gcmSpecClass, IvParameterSpec.class, PBEParameterSpec.class, GOST28147ParameterSpec.class};
this.scheme = -1;
this.ivLength = 0;
this.fixedIv = true;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.modeName = null;
this.baseEngine = blockCipherProvider.get();
this.engineProvider = blockCipherProvider;
this.cipher = new BufferedGenericBlockCipher(blockCipherProvider.get());
}
public BaseBlockCipher(AEADBlockCipher aEADBlockCipher, boolean z, int i) {
this.availableSpecs = new Class[]{RC2ParameterSpec.class, RC5ParameterSpec.class, gcmSpecClass, IvParameterSpec.class, PBEParameterSpec.class, GOST28147ParameterSpec.class};
this.scheme = -1;
this.ivLength = 0;
this.fixedIv = true;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.modeName = null;
this.baseEngine = aEADBlockCipher.getUnderlyingCipher();
this.fixedIv = z;
this.ivLength = i;
this.cipher = new AEADGenericBlockCipher(aEADBlockCipher);
}
public BaseBlockCipher(AEADBlockCipher aEADBlockCipher) {
this.availableSpecs = new Class[]{RC2ParameterSpec.class, RC5ParameterSpec.class, gcmSpecClass, IvParameterSpec.class, PBEParameterSpec.class, GOST28147ParameterSpec.class};
this.scheme = -1;
this.ivLength = 0;
this.fixedIv = true;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.modeName = null;
BlockCipher underlyingCipher = aEADBlockCipher.getUnderlyingCipher();
this.baseEngine = underlyingCipher;
this.ivLength = underlyingCipher.getBlockSize();
this.cipher = new AEADGenericBlockCipher(aEADBlockCipher);
}
public BaseBlockCipher(BufferedBlockCipher bufferedBlockCipher, int i) {
this.availableSpecs = new Class[]{RC2ParameterSpec.class, RC5ParameterSpec.class, gcmSpecClass, IvParameterSpec.class, PBEParameterSpec.class, GOST28147ParameterSpec.class};
this.scheme = -1;
this.ivLength = 0;
this.fixedIv = true;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.modeName = null;
this.baseEngine = bufferedBlockCipher.getUnderlyingCipher();
this.cipher = new BufferedGenericBlockCipher(bufferedBlockCipher);
this.ivLength = i / 8;
}
public BaseBlockCipher(BlockCipher blockCipher, int i, int i2, int i3, int i4) {
this.availableSpecs = new Class[]{RC2ParameterSpec.class, RC5ParameterSpec.class, gcmSpecClass, IvParameterSpec.class, PBEParameterSpec.class, GOST28147ParameterSpec.class};
this.fixedIv = true;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.modeName = null;
this.baseEngine = blockCipher;
this.scheme = i;
this.digest = i2;
this.keySizeInBits = i3;
this.ivLength = i4;
this.cipher = new BufferedGenericBlockCipher(blockCipher);
}
public BaseBlockCipher(BlockCipher blockCipher, int i) {
this.availableSpecs = new Class[]{RC2ParameterSpec.class, RC5ParameterSpec.class, gcmSpecClass, IvParameterSpec.class, PBEParameterSpec.class, GOST28147ParameterSpec.class};
this.scheme = -1;
this.ivLength = 0;
this.fixedIv = true;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.modeName = null;
this.baseEngine = blockCipher;
this.cipher = new BufferedGenericBlockCipher(blockCipher);
this.ivLength = i / 8;
}
public BaseBlockCipher(BlockCipher blockCipher) {
this.availableSpecs = new Class[]{RC2ParameterSpec.class, RC5ParameterSpec.class, gcmSpecClass, IvParameterSpec.class, PBEParameterSpec.class, GOST28147ParameterSpec.class};
this.scheme = -1;
this.ivLength = 0;
this.fixedIv = true;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.modeName = null;
this.baseEngine = blockCipher;
this.cipher = new BufferedGenericBlockCipher(blockCipher);
}
}