what-the-bank/sources/org/bouncycastle/jcajce/provider/asymmetric/util/BaseCipherSpi.java

149 lines
6.2 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.jcajce.provider.asymmetric.util;
import java.security.AlgorithmParameters;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.CipherSpi;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.crypto.spec.RC2ParameterSpec;
import javax.crypto.spec.RC5ParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.Wrapper;
import org.bouncycastle.jcajce.util.BCJcaJceHelper;
import org.bouncycastle.jcajce.util.JcaJceHelper;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/* loaded from: classes6.dex */
public abstract class BaseCipherSpi extends CipherSpi {
private byte[] iv;
private int ivSize;
private Class[] availableSpecs = {IvParameterSpec.class, PBEParameterSpec.class, RC2ParameterSpec.class, RC5ParameterSpec.class};
private final JcaJceHelper helper = new BCJcaJceHelper();
protected AlgorithmParameters engineParams = null;
protected Wrapper wrapEngine = null;
@Override // javax.crypto.CipherSpi
protected int engineGetBlockSize() {
return 0;
}
@Override // javax.crypto.CipherSpi
protected byte[] engineGetIV() {
return null;
}
@Override // javax.crypto.CipherSpi
protected int engineGetOutputSize(int i) {
return -1;
}
@Override // javax.crypto.CipherSpi
protected AlgorithmParameters engineGetParameters() {
return null;
}
@Override // javax.crypto.CipherSpi
protected byte[] engineWrap(Key key) throws IllegalBlockSizeException, InvalidKeyException {
byte[] encoded = key.getEncoded();
if (encoded == null) {
throw new InvalidKeyException("Cannot wrap key, null encoding.");
}
try {
Wrapper wrapper = this.wrapEngine;
return wrapper == null ? engineDoFinal(encoded, 0, encoded.length) : wrapper.wrap(encoded, 0, encoded.length);
} catch (BadPaddingException e) {
throw new IllegalBlockSizeException(e.getMessage());
}
}
@Override // javax.crypto.CipherSpi
protected Key engineUnwrap(byte[] bArr, String str, int i) throws InvalidKeyException {
try {
Wrapper wrapper = this.wrapEngine;
byte[] engineDoFinal = wrapper == null ? engineDoFinal(bArr, 0, bArr.length) : wrapper.unwrap(bArr, 0, bArr.length);
if (i == 3) {
return new SecretKeySpec(engineDoFinal, str);
}
if (str.equals("") && i == 2) {
try {
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(engineDoFinal);
PrivateKey privateKey = BouncyCastleProvider.getPrivateKey(privateKeyInfo);
if (privateKey != null) {
return privateKey;
}
StringBuilder sb = new StringBuilder("algorithm ");
sb.append(privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm());
sb.append(" not supported");
throw new InvalidKeyException(sb.toString());
} catch (Exception unused) {
throw new InvalidKeyException("Invalid key encoding.");
}
}
try {
KeyFactory createKeyFactory = this.helper.createKeyFactory(str);
if (i == 1) {
return createKeyFactory.generatePublic(new X509EncodedKeySpec(engineDoFinal));
}
if (i == 2) {
return createKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(engineDoFinal));
}
throw new InvalidKeyException("Unknown key type ".concat(String.valueOf(i)));
} catch (NoSuchAlgorithmException e) {
StringBuilder sb2 = new StringBuilder("Unknown key type ");
sb2.append(e.getMessage());
throw new InvalidKeyException(sb2.toString());
} catch (NoSuchProviderException e2) {
StringBuilder sb3 = new StringBuilder("Unknown key type ");
sb3.append(e2.getMessage());
throw new InvalidKeyException(sb3.toString());
} catch (InvalidKeySpecException e3) {
StringBuilder sb4 = new StringBuilder("Unknown key type ");
sb4.append(e3.getMessage());
throw new InvalidKeyException(sb4.toString());
}
} catch (BadPaddingException e4) {
throw new InvalidKeyException(e4.getMessage());
} catch (IllegalBlockSizeException e5) {
throw new InvalidKeyException(e5.getMessage());
} catch (InvalidCipherTextException e6) {
throw new InvalidKeyException(e6.getMessage());
}
}
@Override // javax.crypto.CipherSpi
protected void engineSetPadding(String str) throws NoSuchPaddingException {
StringBuilder sb = new StringBuilder("Padding ");
sb.append(str);
sb.append(" unknown.");
throw new NoSuchPaddingException(sb.toString());
}
@Override // javax.crypto.CipherSpi
protected void engineSetMode(String str) throws NoSuchAlgorithmException {
throw new NoSuchAlgorithmException("can't support mode ".concat(String.valueOf(str)));
}
@Override // javax.crypto.CipherSpi
protected int engineGetKeySize(Key key) {
return key.getEncoded().length;
}
/* JADX INFO: Access modifiers changed from: protected */
public final AlgorithmParameters createParametersInstance(String str) throws NoSuchAlgorithmException, NoSuchProviderException {
return this.helper.createAlgorithmParameters(str);
}
}