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

250 lines
11 KiB
Java

package org.bouncycastle.jcajce.provider.symmetric;
import java.io.IOException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.KeySpec;
import javax.crypto.SecretKey;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.bouncycastle.asn1.ASN1Encoding;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PBKDF2Params;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
import org.bouncycastle.jcajce.provider.symmetric.util.BCPBEKey;
import org.bouncycastle.jcajce.provider.symmetric.util.BaseAlgorithmParameters;
import org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory;
import org.bouncycastle.jcajce.provider.symmetric.util.PBE;
import org.bouncycastle.jcajce.provider.util.AlgorithmProvider;
import org.bouncycastle.jcajce.spec.PBKDF2KeySpec;
/* loaded from: classes6.dex */
public class PBEPBKDF2 {
/* loaded from: classes6.dex */
public static class AlgParams extends BaseAlgorithmParameters {
PBKDF2Params params;
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseAlgorithmParameters
public AlgorithmParameterSpec localEngineGetParameterSpec(Class cls) throws InvalidParameterSpecException {
if (cls == PBEParameterSpec.class) {
return new PBEParameterSpec(this.params.getSalt(), this.params.getIterationCount().intValue());
}
throw new InvalidParameterSpecException("unknown parameter spec passed to PBKDF2 PBE parameters object.");
}
@Override // java.security.AlgorithmParametersSpi
protected String engineToString() {
return "PBKDF2 Parameters";
}
@Override // java.security.AlgorithmParametersSpi
protected void engineInit(byte[] bArr, String str) throws IOException {
if (!isASN1FormatString(str)) {
throw new IOException("Unknown parameters format in PBKDF2 parameters object");
}
engineInit(bArr);
}
@Override // java.security.AlgorithmParametersSpi
protected void engineInit(byte[] bArr) throws IOException {
this.params = PBKDF2Params.getInstance(ASN1Primitive.fromByteArray(bArr));
}
@Override // java.security.AlgorithmParametersSpi
protected void engineInit(AlgorithmParameterSpec algorithmParameterSpec) throws InvalidParameterSpecException {
if (!(algorithmParameterSpec instanceof PBEParameterSpec)) {
throw new InvalidParameterSpecException("PBEParameterSpec required to initialise a PBKDF2 PBE parameters algorithm parameters object");
}
PBEParameterSpec pBEParameterSpec = (PBEParameterSpec) algorithmParameterSpec;
this.params = new PBKDF2Params(pBEParameterSpec.getSalt(), pBEParameterSpec.getIterationCount());
}
@Override // java.security.AlgorithmParametersSpi
protected byte[] engineGetEncoded(String str) {
if (isASN1FormatString(str)) {
return engineGetEncoded();
}
return null;
}
@Override // java.security.AlgorithmParametersSpi
protected byte[] engineGetEncoded() {
try {
return this.params.getEncoded(ASN1Encoding.DER);
} catch (IOException e) {
StringBuilder sb = new StringBuilder("Oooops! ");
sb.append(e.toString());
throw new RuntimeException(sb.toString());
}
}
}
/* loaded from: classes6.dex */
public static class BasePBKDF2 extends BaseSecretKeyFactory {
private int defaultDigest;
private int scheme;
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory, javax.crypto.SecretKeyFactorySpi
public SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException {
if (!(keySpec instanceof PBEKeySpec)) {
throw new InvalidKeySpecException("Invalid KeySpec");
}
PBEKeySpec pBEKeySpec = (PBEKeySpec) keySpec;
if (pBEKeySpec.getSalt() == null) {
throw new InvalidKeySpecException("missing required salt");
}
if (pBEKeySpec.getIterationCount() <= 0) {
StringBuilder sb = new StringBuilder("positive iteration count required: ");
sb.append(pBEKeySpec.getIterationCount());
throw new InvalidKeySpecException(sb.toString());
}
if (pBEKeySpec.getKeyLength() <= 0) {
StringBuilder sb2 = new StringBuilder("positive key length required: ");
sb2.append(pBEKeySpec.getKeyLength());
throw new InvalidKeySpecException(sb2.toString());
}
if (pBEKeySpec.getPassword().length == 0) {
throw new IllegalArgumentException("password empty");
}
if (pBEKeySpec instanceof PBKDF2KeySpec) {
int digestCode = getDigestCode(((PBKDF2KeySpec) pBEKeySpec).getPrf().getAlgorithm());
int keyLength = pBEKeySpec.getKeyLength();
return new BCPBEKey(this.algName, this.algOid, this.scheme, digestCode, keyLength, -1, pBEKeySpec, PBE.Util.makePBEMacParameters(pBEKeySpec, this.scheme, digestCode, keyLength));
}
int i = this.defaultDigest;
int keyLength2 = pBEKeySpec.getKeyLength();
return new BCPBEKey(this.algName, this.algOid, this.scheme, i, keyLength2, -1, pBEKeySpec, PBE.Util.makePBEMacParameters(pBEKeySpec, this.scheme, i, keyLength2));
}
private int getDigestCode(ASN1ObjectIdentifier aSN1ObjectIdentifier) throws InvalidKeySpecException {
if (aSN1ObjectIdentifier.equals(CryptoProObjectIdentifiers.gostR3411Hmac)) {
return 6;
}
if (aSN1ObjectIdentifier.equals(PKCSObjectIdentifiers.id_hmacWithSHA1)) {
return 1;
}
if (aSN1ObjectIdentifier.equals(PKCSObjectIdentifiers.id_hmacWithSHA256)) {
return 4;
}
if (aSN1ObjectIdentifier.equals(PKCSObjectIdentifiers.id_hmacWithSHA224)) {
return 7;
}
if (aSN1ObjectIdentifier.equals(PKCSObjectIdentifiers.id_hmacWithSHA384)) {
return 8;
}
if (aSN1ObjectIdentifier.equals(PKCSObjectIdentifiers.id_hmacWithSHA512)) {
return 9;
}
throw new InvalidKeySpecException("Invalid KeySpec: unknown PRF algorithm ".concat(String.valueOf(aSN1ObjectIdentifier)));
}
public BasePBKDF2(String str, int i, int i2) {
super(str, PKCSObjectIdentifiers.id_PBKDF2);
this.scheme = i;
this.defaultDigest = i2;
}
public BasePBKDF2(String str, int i) {
this(str, i, 1);
}
}
/* loaded from: classes6.dex */
public static class Mappings extends AlgorithmProvider {
private static final String PREFIX = PBEPBKDF2.class.getName();
@Override // org.bouncycastle.jcajce.provider.util.AlgorithmProvider
public void configure(ConfigurableProvider configurableProvider) {
StringBuilder sb = new StringBuilder();
String str = PREFIX;
sb.append(str);
sb.append("$AlgParams");
configurableProvider.addAlgorithm("AlgorithmParameters.PBKDF2", sb.toString());
StringBuilder sb2 = new StringBuilder("Alg.Alias.AlgorithmParameters.");
sb2.append(PKCSObjectIdentifiers.id_PBKDF2);
configurableProvider.addAlgorithm(sb2.toString(), "PBKDF2");
StringBuilder sb3 = new StringBuilder();
sb3.append(str);
sb3.append("$PBKDF2withUTF8");
configurableProvider.addAlgorithm("SecretKeyFactory.PBKDF2", sb3.toString());
configurableProvider.addAlgorithm("Alg.Alias.SecretKeyFactory.PBKDF2WITHHMACSHA1", "PBKDF2");
configurableProvider.addAlgorithm("Alg.Alias.SecretKeyFactory.PBKDF2WITHHMACSHA1ANDUTF8", "PBKDF2");
StringBuilder sb4 = new StringBuilder("Alg.Alias.SecretKeyFactory.");
sb4.append(PKCSObjectIdentifiers.id_PBKDF2);
configurableProvider.addAlgorithm(sb4.toString(), "PBKDF2");
StringBuilder sb5 = new StringBuilder();
sb5.append(str);
sb5.append("$PBKDF2with8BIT");
configurableProvider.addAlgorithm("SecretKeyFactory.PBKDF2WITHASCII", sb5.toString());
configurableProvider.addAlgorithm("Alg.Alias.SecretKeyFactory.PBKDF2WITH8BIT", "PBKDF2WITHASCII");
configurableProvider.addAlgorithm("Alg.Alias.SecretKeyFactory.PBKDF2WITHHMACSHA1AND8BIT", "PBKDF2WITHASCII");
StringBuilder sb6 = new StringBuilder();
sb6.append(str);
sb6.append("$PBKDF2withSHA224");
configurableProvider.addAlgorithm("SecretKeyFactory.PBKDF2WITHHMACSHA224", sb6.toString());
StringBuilder sb7 = new StringBuilder();
sb7.append(str);
sb7.append("$PBKDF2withSHA256");
configurableProvider.addAlgorithm("SecretKeyFactory.PBKDF2WITHHMACSHA256", sb7.toString());
StringBuilder sb8 = new StringBuilder();
sb8.append(str);
sb8.append("$PBKDF2withSHA384");
configurableProvider.addAlgorithm("SecretKeyFactory.PBKDF2WITHHMACSHA384", sb8.toString());
StringBuilder sb9 = new StringBuilder();
sb9.append(str);
sb9.append("$PBKDF2withSHA512");
configurableProvider.addAlgorithm("SecretKeyFactory.PBKDF2WITHHMACSHA512", sb9.toString());
}
}
/* loaded from: classes6.dex */
public static class PBKDF2with8BIT extends BasePBKDF2 {
public PBKDF2with8BIT() {
super("PBKDF2", 1);
}
}
/* loaded from: classes6.dex */
public static class PBKDF2withSHA224 extends BasePBKDF2 {
public PBKDF2withSHA224() {
super("PBKDF2", 5, 7);
}
}
/* loaded from: classes6.dex */
public static class PBKDF2withSHA256 extends BasePBKDF2 {
public PBKDF2withSHA256() {
super("PBKDF2", 5, 4);
}
}
/* loaded from: classes6.dex */
public static class PBKDF2withSHA384 extends BasePBKDF2 {
public PBKDF2withSHA384() {
super("PBKDF2", 5, 8);
}
}
/* loaded from: classes6.dex */
public static class PBKDF2withSHA512 extends BasePBKDF2 {
public PBKDF2withSHA512() {
super("PBKDF2", 5, 9);
}
}
/* loaded from: classes6.dex */
public static class PBKDF2withUTF8 extends BasePBKDF2 {
public PBKDF2withUTF8() {
super("PBKDF2", 5);
}
}
private PBEPBKDF2() {
}
}