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

275 lines
12 KiB
Java

package org.bouncycastle.jcajce.provider.symmetric.util;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.InvalidParameterException;
import java.security.Key;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
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.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.StreamCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.jcajce.PKCS12Key;
import org.bouncycastle.jcajce.PKCS12KeyWithParameters;
import org.bouncycastle.jcajce.provider.symmetric.util.PBE;
/* loaded from: classes6.dex */
public class BaseStreamCipher extends BaseWrapCipher implements PBE {
private Class[] availableSpecs;
private StreamCipher cipher;
private int digest;
private int ivLength;
private ParametersWithIV ivParam;
private int keySizeInBits;
private String pbeAlgorithm;
private PBEParameterSpec pbeSpec;
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected int engineGetBlockSize() {
return 0;
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected int engineGetOutputSize(int i) {
return i;
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected byte[] engineUpdate(byte[] bArr, int i, int i2) {
byte[] bArr2 = new byte[i2];
this.cipher.processBytes(bArr, i, i2, bArr2, 0);
return bArr2;
}
@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 (i3 + i2 > bArr2.length) {
throw new ShortBufferException("output buffer too short for input.");
}
try {
this.cipher.processBytes(bArr, i, i2, bArr2, i3);
return i2;
} catch (DataLengthException e) {
throw new IllegalStateException(e.getMessage());
}
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected void engineSetPadding(String str) throws NoSuchPaddingException {
if (str.equalsIgnoreCase("NoPadding")) {
return;
}
StringBuilder sb = new StringBuilder("Padding ");
sb.append(str);
sb.append(" unknown.");
throw new NoSuchPaddingException(sb.toString());
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected void engineSetMode(String str) {
if (!str.equalsIgnoreCase("ECB")) {
throw new IllegalArgumentException("can't support mode ".concat(String.valueOf(str)));
}
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected void engineInit(int i, Key key, AlgorithmParameterSpec algorithmParameterSpec, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters cipherParameters;
CipherParameters keyParameter;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.engineParams = null;
if (!(key instanceof SecretKey)) {
StringBuilder sb = new StringBuilder("Key for algorithm ");
sb.append(key.getAlgorithm());
sb.append(" not suitable for symmetric enryption.");
throw new InvalidKeyException(sb.toString());
}
if (key instanceof PKCS12Key) {
PKCS12Key pKCS12Key = (PKCS12Key) key;
PBEParameterSpec pBEParameterSpec = (PBEParameterSpec) algorithmParameterSpec;
this.pbeSpec = pBEParameterSpec;
if ((pKCS12Key instanceof PKCS12KeyWithParameters) && pBEParameterSpec == null) {
PKCS12KeyWithParameters pKCS12KeyWithParameters = (PKCS12KeyWithParameters) pKCS12Key;
this.pbeSpec = new PBEParameterSpec(pKCS12KeyWithParameters.getSalt(), pKCS12KeyWithParameters.getIterationCount());
}
cipherParameters = PBE.Util.makePBEParameters(pKCS12Key.getEncoded(), 2, this.digest, this.keySizeInBits, this.ivLength << 3, this.pbeSpec, this.cipher.getAlgorithmName());
} else {
if (key instanceof BCPBEKey) {
BCPBEKey bCPBEKey = (BCPBEKey) key;
this.pbeAlgorithm = bCPBEKey.getOID() != null ? bCPBEKey.getOID().getId() : bCPBEKey.getAlgorithm();
if (bCPBEKey.getParam() != null) {
keyParameter = bCPBEKey.getParam();
this.pbeSpec = new PBEParameterSpec(bCPBEKey.getSalt(), bCPBEKey.getIterationCount());
} else {
if (!(algorithmParameterSpec instanceof PBEParameterSpec)) {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
CipherParameters makePBEParameters = PBE.Util.makePBEParameters(bCPBEKey, algorithmParameterSpec, this.cipher.getAlgorithmName());
this.pbeSpec = (PBEParameterSpec) algorithmParameterSpec;
keyParameter = makePBEParameters;
}
if (bCPBEKey.getIvSize() != 0) {
this.ivParam = (ParametersWithIV) keyParameter;
}
} else if (algorithmParameterSpec == null) {
if (this.digest > 0) {
throw new InvalidKeyException("Algorithm requires a PBE key");
}
keyParameter = new KeyParameter(key.getEncoded());
} else {
if (!(algorithmParameterSpec instanceof IvParameterSpec)) {
throw new InvalidAlgorithmParameterException("unknown parameter type.");
}
ParametersWithIV parametersWithIV = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) algorithmParameterSpec).getIV());
this.ivParam = parametersWithIV;
cipherParameters = parametersWithIV;
}
cipherParameters = keyParameter;
}
if (this.ivLength != 0 && !(cipherParameters instanceof ParametersWithIV)) {
if (secureRandom == null) {
secureRandom = new SecureRandom();
}
if (i != 1 && i != 3) {
throw new InvalidAlgorithmParameterException("no IV set when one expected");
}
byte[] bArr = new byte[this.ivLength];
secureRandom.nextBytes(bArr);
ParametersWithIV parametersWithIV2 = new ParametersWithIV(cipherParameters, bArr);
this.ivParam = parametersWithIV2;
cipherParameters = parametersWithIV2;
}
try {
if (i != 1) {
if (i != 2) {
if (i != 3) {
if (i != 4) {
StringBuilder sb2 = new StringBuilder("unknown opmode ");
sb2.append(i);
sb2.append(" passed");
throw new InvalidParameterException(sb2.toString());
}
}
}
this.cipher.init(false, cipherParameters);
return;
}
this.cipher.init(true, cipherParameters);
} catch (Exception e) {
throw new InvalidKeyException(e.getMessage());
}
}
@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;
}
try {
algorithmParameterSpec = algorithmParameters.getParameterSpec(clsArr[i2]);
break;
} catch (Exception unused) {
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 || this.pbeSpec == null) {
return this.engineParams;
}
try {
AlgorithmParameters createParametersInstance = createParametersInstance(this.pbeAlgorithm);
createParametersInstance.init(this.pbeSpec);
return createParametersInstance;
} catch (Exception unused) {
return null;
}
}
@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() {
ParametersWithIV parametersWithIV = this.ivParam;
if (parametersWithIV != null) {
return parametersWithIV.getIV();
}
return null;
}
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher, javax.crypto.CipherSpi
protected byte[] engineDoFinal(byte[] bArr, int i, int i2) {
if (i2 == 0) {
this.cipher.reset();
return new byte[0];
}
byte[] engineUpdate = engineUpdate(bArr, i, i2);
this.cipher.reset();
return engineUpdate;
}
@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 ShortBufferException {
if (i3 + i2 > bArr2.length) {
throw new ShortBufferException("output buffer too short for input.");
}
if (i2 != 0) {
this.cipher.processBytes(bArr, i, i2, bArr2, i3);
}
this.cipher.reset();
return i2;
}
public BaseStreamCipher(StreamCipher streamCipher, int i, int i2, int i3) {
this.availableSpecs = new Class[]{RC2ParameterSpec.class, RC5ParameterSpec.class, IvParameterSpec.class, PBEParameterSpec.class};
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.cipher = streamCipher;
this.ivLength = i;
this.keySizeInBits = i2;
this.digest = i3;
}
public BaseStreamCipher(StreamCipher streamCipher, int i) {
this(streamCipher, i, -1, -1);
}
}