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

202 lines
11 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.jcajce.provider.symmetric.util;
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.SecretKey;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle.crypto.digests.GOST3411Digest;
import org.bouncycastle.crypto.digests.MD2Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.digests.SHA224Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA384Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.digests.TigerDigest;
import org.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator;
import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator;
import org.bouncycastle.crypto.generators.PKCS5S1ParametersGenerator;
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator;
import org.bouncycastle.crypto.params.DESParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
/* loaded from: classes6.dex */
public interface PBE {
public static final int GOST3411 = 6;
public static final int MD2 = 5;
public static final int MD5 = 0;
public static final int OPENSSL = 3;
public static final int PKCS12 = 2;
public static final int PKCS5S1 = 0;
public static final int PKCS5S1_UTF8 = 4;
public static final int PKCS5S2 = 1;
public static final int PKCS5S2_UTF8 = 5;
public static final int RIPEMD160 = 2;
public static final int SHA1 = 1;
public static final int SHA224 = 7;
public static final int SHA256 = 4;
public static final int SHA384 = 8;
public static final int SHA512 = 9;
public static final int TIGER = 3;
/* loaded from: classes6.dex */
public static class Util {
public static CipherParameters makePBEParameters(byte[] bArr, int i, int i2, int i3, int i4, AlgorithmParameterSpec algorithmParameterSpec, String str) throws InvalidAlgorithmParameterException {
if (algorithmParameterSpec == null || !(algorithmParameterSpec instanceof PBEParameterSpec)) {
throw new InvalidAlgorithmParameterException("Need a PBEParameter spec with a PBE key.");
}
PBEParameterSpec pBEParameterSpec = (PBEParameterSpec) algorithmParameterSpec;
PBEParametersGenerator makePBEGenerator = makePBEGenerator(i, i2);
makePBEGenerator.init(bArr, pBEParameterSpec.getSalt(), pBEParameterSpec.getIterationCount());
CipherParameters generateDerivedParameters = i4 != 0 ? makePBEGenerator.generateDerivedParameters(i3, i4) : makePBEGenerator.generateDerivedParameters(i3);
if (str.startsWith("DES")) {
if (generateDerivedParameters instanceof ParametersWithIV) {
DESParameters.setOddParity(((KeyParameter) ((ParametersWithIV) generateDerivedParameters).getParameters()).getKey());
} else {
DESParameters.setOddParity(((KeyParameter) generateDerivedParameters).getKey());
}
}
return generateDerivedParameters;
}
public static CipherParameters makePBEParameters(BCPBEKey bCPBEKey, AlgorithmParameterSpec algorithmParameterSpec, String str) {
if (algorithmParameterSpec == null || !(algorithmParameterSpec instanceof PBEParameterSpec)) {
throw new IllegalArgumentException("Need a PBEParameter spec with a PBE key.");
}
PBEParameterSpec pBEParameterSpec = (PBEParameterSpec) algorithmParameterSpec;
PBEParametersGenerator makePBEGenerator = makePBEGenerator(bCPBEKey.getType(), bCPBEKey.getDigest());
byte[] encoded = bCPBEKey.getEncoded();
if (bCPBEKey.shouldTryWrongPKCS12()) {
encoded = new byte[2];
}
makePBEGenerator.init(encoded, pBEParameterSpec.getSalt(), pBEParameterSpec.getIterationCount());
CipherParameters generateDerivedParameters = bCPBEKey.getIvSize() != 0 ? makePBEGenerator.generateDerivedParameters(bCPBEKey.getKeySize(), bCPBEKey.getIvSize()) : makePBEGenerator.generateDerivedParameters(bCPBEKey.getKeySize());
if (str.startsWith("DES")) {
if (generateDerivedParameters instanceof ParametersWithIV) {
DESParameters.setOddParity(((KeyParameter) ((ParametersWithIV) generateDerivedParameters).getParameters()).getKey());
} else {
DESParameters.setOddParity(((KeyParameter) generateDerivedParameters).getKey());
}
}
return generateDerivedParameters;
}
public static CipherParameters makePBEParameters(PBEKeySpec pBEKeySpec, int i, int i2, int i3, int i4) {
PBEParametersGenerator makePBEGenerator = makePBEGenerator(i, i2);
byte[] convertPassword = convertPassword(i, pBEKeySpec);
makePBEGenerator.init(convertPassword, pBEKeySpec.getSalt(), pBEKeySpec.getIterationCount());
CipherParameters generateDerivedParameters = i4 != 0 ? makePBEGenerator.generateDerivedParameters(i3, i4) : makePBEGenerator.generateDerivedParameters(i3);
for (int i5 = 0; i5 != convertPassword.length; i5++) {
convertPassword[i5] = 0;
}
return generateDerivedParameters;
}
public static CipherParameters makePBEMacParameters(BCPBEKey bCPBEKey, AlgorithmParameterSpec algorithmParameterSpec) {
if (algorithmParameterSpec == null || !(algorithmParameterSpec instanceof PBEParameterSpec)) {
throw new IllegalArgumentException("Need a PBEParameter spec with a PBE key.");
}
PBEParameterSpec pBEParameterSpec = (PBEParameterSpec) algorithmParameterSpec;
PBEParametersGenerator makePBEGenerator = makePBEGenerator(bCPBEKey.getType(), bCPBEKey.getDigest());
makePBEGenerator.init(bCPBEKey.getEncoded(), pBEParameterSpec.getSalt(), pBEParameterSpec.getIterationCount());
return makePBEGenerator.generateDerivedMacParameters(bCPBEKey.getKeySize());
}
public static CipherParameters makePBEMacParameters(PBEKeySpec pBEKeySpec, int i, int i2, int i3) {
PBEParametersGenerator makePBEGenerator = makePBEGenerator(i, i2);
byte[] convertPassword = convertPassword(i, pBEKeySpec);
makePBEGenerator.init(convertPassword, pBEKeySpec.getSalt(), pBEKeySpec.getIterationCount());
CipherParameters generateDerivedMacParameters = makePBEGenerator.generateDerivedMacParameters(i3);
for (int i4 = 0; i4 != convertPassword.length; i4++) {
convertPassword[i4] = 0;
}
return generateDerivedMacParameters;
}
public static CipherParameters makePBEMacParameters(SecretKey secretKey, int i, int i2, int i3, PBEParameterSpec pBEParameterSpec) {
PBEParametersGenerator makePBEGenerator = makePBEGenerator(i, i2);
byte[] encoded = secretKey.getEncoded();
makePBEGenerator.init(secretKey.getEncoded(), pBEParameterSpec.getSalt(), pBEParameterSpec.getIterationCount());
CipherParameters generateDerivedMacParameters = makePBEGenerator.generateDerivedMacParameters(i3);
for (int i4 = 0; i4 != encoded.length; i4++) {
encoded[i4] = 0;
}
return generateDerivedMacParameters;
}
private static PBEParametersGenerator makePBEGenerator(int i, int i2) {
if (i == 0 || i == 4) {
if (i2 == 0) {
return new PKCS5S1ParametersGenerator(new MD5Digest());
}
if (i2 == 1) {
return new PKCS5S1ParametersGenerator(new SHA1Digest());
}
if (i2 == 5) {
return new PKCS5S1ParametersGenerator(new MD2Digest());
}
throw new IllegalStateException("PKCS5 scheme 1 only supports MD2, MD5 and SHA1.");
}
if (i != 1 && i != 5) {
if (i != 2) {
return new OpenSSLPBEParametersGenerator();
}
switch (i2) {
case 0:
return new PKCS12ParametersGenerator(new MD5Digest());
case 1:
return new PKCS12ParametersGenerator(new SHA1Digest());
case 2:
return new PKCS12ParametersGenerator(new RIPEMD160Digest());
case 3:
return new PKCS12ParametersGenerator(new TigerDigest());
case 4:
return new PKCS12ParametersGenerator(new SHA256Digest());
case 5:
return new PKCS12ParametersGenerator(new MD2Digest());
case 6:
return new PKCS12ParametersGenerator(new GOST3411Digest());
default:
throw new IllegalStateException("unknown digest scheme for PBE encryption.");
}
}
switch (i2) {
case 0:
return new PKCS5S2ParametersGenerator(new MD5Digest());
case 1:
return new PKCS5S2ParametersGenerator(new SHA1Digest());
case 2:
return new PKCS5S2ParametersGenerator(new RIPEMD160Digest());
case 3:
return new PKCS5S2ParametersGenerator(new TigerDigest());
case 4:
return new PKCS5S2ParametersGenerator(new SHA256Digest());
case 5:
return new PKCS5S2ParametersGenerator(new MD2Digest());
case 6:
return new PKCS5S2ParametersGenerator(new GOST3411Digest());
case 7:
return new PKCS5S2ParametersGenerator(new SHA224Digest());
case 8:
return new PKCS5S2ParametersGenerator(new SHA384Digest());
case 9:
return new PKCS5S2ParametersGenerator(new SHA512Digest());
default:
throw new IllegalStateException("unknown digest scheme for PBE PKCS5S2 encryption.");
}
}
private static byte[] convertPassword(int i, PBEKeySpec pBEKeySpec) {
if (i == 2) {
return PBEParametersGenerator.PKCS12PasswordToBytes(pBEKeySpec.getPassword());
}
return (i == 5 || i == 4) ? PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(pBEKeySpec.getPassword()) : PBEParametersGenerator.PKCS5PasswordToBytes(pBEKeySpec.getPassword());
}
}
}