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

245 lines
12 KiB
Java

package org.bouncycastle.jcajce.provider.asymmetric.util;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.crypto.KeyAgreementSpi;
import javax.crypto.SecretKey;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
import org.bouncycastle.asn1.gnu.GNUObjectIdentifiers;
import org.bouncycastle.asn1.kisa.KISAObjectIdentifiers;
import org.bouncycastle.asn1.misc.MiscObjectIdentifiers;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.asn1.ntt.NTTObjectIdentifiers;
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.crypto.DerivationFunction;
import org.bouncycastle.crypto.agreement.kdf.DHKDFParameters;
import org.bouncycastle.crypto.agreement.kdf.DHKEKGenerator;
import org.bouncycastle.crypto.params.DESParameters;
import org.bouncycastle.crypto.params.KDFParameters;
import org.bouncycastle.util.Integers;
import org.bouncycastle.util.Strings;
/* loaded from: classes6.dex */
public abstract class BaseAgreementSpi extends KeyAgreementSpi {
private static final Map<String, ASN1ObjectIdentifier> defaultOids;
private static final Hashtable des;
private static final Map<String, Integer> keySizes;
private static final Map<String, String> nameTable;
private static final Hashtable oids;
private final String kaAlgorithm;
private final DerivationFunction kdf;
public byte[] ukmParameters;
protected abstract byte[] calcSecret();
@Override // javax.crypto.KeyAgreementSpi
public byte[] engineGenerateSecret() throws IllegalStateException {
if (this.kdf == null) {
return calcSecret();
}
throw new UnsupportedOperationException("KDF can only be used when algorithm is known");
}
@Override // javax.crypto.KeyAgreementSpi
public SecretKey engineGenerateSecret(String str) throws NoSuchAlgorithmException {
byte[] calcSecret = calcSecret();
String upperCase = Strings.toUpperCase(str);
Hashtable hashtable = oids;
String id = hashtable.containsKey(upperCase) ? ((ASN1ObjectIdentifier) hashtable.get(upperCase)).getId() : str;
int keySize = getKeySize(id);
DerivationFunction derivationFunction = this.kdf;
if (derivationFunction != null) {
if (keySize < 0) {
throw new NoSuchAlgorithmException("unknown algorithm encountered: ".concat(String.valueOf(id)));
}
int i = keySize / 8;
byte[] bArr = new byte[i];
if (derivationFunction instanceof DHKEKGenerator) {
try {
this.kdf.init(new DHKDFParameters(new ASN1ObjectIdentifier(id), keySize, calcSecret, this.ukmParameters));
} catch (IllegalArgumentException unused) {
throw new NoSuchAlgorithmException("no OID for algorithm: ".concat(String.valueOf(id)));
}
} else {
this.kdf.init(new KDFParameters(calcSecret, this.ukmParameters));
}
this.kdf.generateBytes(bArr, 0, i);
calcSecret = bArr;
} else if (keySize > 0) {
int i2 = keySize / 8;
byte[] bArr2 = new byte[i2];
System.arraycopy(calcSecret, 0, bArr2, 0, i2);
calcSecret = bArr2;
}
if (des.containsKey(id)) {
DESParameters.setOddParity(calcSecret);
}
return new SecretKeySpec(calcSecret, getAlgorithm(str));
}
@Override // javax.crypto.KeyAgreementSpi
public int engineGenerateSecret(byte[] bArr, int i) throws IllegalStateException, ShortBufferException {
byte[] engineGenerateSecret = engineGenerateSecret();
if (bArr.length - i >= engineGenerateSecret.length) {
System.arraycopy(engineGenerateSecret, 0, bArr, i, engineGenerateSecret.length);
return engineGenerateSecret.length;
}
StringBuilder sb = new StringBuilder();
sb.append(this.kaAlgorithm);
sb.append(" key agreement: need ");
sb.append(engineGenerateSecret.length);
sb.append(" bytes");
throw new ShortBufferException(sb.toString());
}
/* JADX INFO: Access modifiers changed from: protected */
public static byte[] trimZeroes(byte[] bArr) {
if (bArr[0] != 0) {
return bArr;
}
int i = 0;
while (i < bArr.length && bArr[i] == 0) {
i++;
}
int length = bArr.length - i;
byte[] bArr2 = new byte[length];
System.arraycopy(bArr, i, bArr2, 0, length);
return bArr2;
}
protected static int getKeySize(String str) {
if (str.indexOf(91) > 0) {
return (Integer.parseInt(str.substring(str.indexOf(91) + 1, str.indexOf(93))) + 7) / 8;
}
String upperCase = Strings.toUpperCase(str);
Map<String, Integer> map = keySizes;
if (map.containsKey(upperCase)) {
return map.get(upperCase).intValue();
}
return -1;
}
protected static String getAlgorithm(String str) {
if (str.indexOf(91) > 0) {
return str.substring(0, str.indexOf(91));
}
if (str.startsWith(NISTObjectIdentifiers.aes.getId())) {
return "AES";
}
if (str.startsWith(GNUObjectIdentifiers.Serpent.getId())) {
return "Serpent";
}
String str2 = nameTable.get(Strings.toUpperCase(str));
return str2 != null ? str2 : str;
}
public BaseAgreementSpi(String str, DerivationFunction derivationFunction) {
this.kaAlgorithm = str;
this.kdf = derivationFunction;
}
static {
HashMap hashMap = new HashMap();
defaultOids = hashMap;
HashMap hashMap2 = new HashMap();
keySizes = hashMap2;
HashMap hashMap3 = new HashMap();
nameTable = hashMap3;
Hashtable hashtable = new Hashtable();
oids = hashtable;
Hashtable hashtable2 = new Hashtable();
des = hashtable2;
Integer valueOf = Integers.valueOf(64);
Integer valueOf2 = Integers.valueOf(128);
Integer valueOf3 = Integers.valueOf(192);
Integer valueOf4 = Integers.valueOf(256);
hashMap2.put("DES", valueOf);
hashMap2.put("DESEDE", valueOf3);
hashMap2.put("BLOWFISH", valueOf2);
hashMap2.put("AES", valueOf4);
hashMap2.put(NISTObjectIdentifiers.id_aes128_ECB.getId(), valueOf2);
hashMap2.put(NISTObjectIdentifiers.id_aes192_ECB.getId(), valueOf3);
hashMap2.put(NISTObjectIdentifiers.id_aes256_ECB.getId(), valueOf4);
hashMap2.put(NISTObjectIdentifiers.id_aes128_CBC.getId(), valueOf2);
hashMap2.put(NISTObjectIdentifiers.id_aes192_CBC.getId(), valueOf3);
hashMap2.put(NISTObjectIdentifiers.id_aes256_CBC.getId(), valueOf4);
hashMap2.put(NISTObjectIdentifiers.id_aes128_CFB.getId(), valueOf2);
hashMap2.put(NISTObjectIdentifiers.id_aes192_CFB.getId(), valueOf3);
hashMap2.put(NISTObjectIdentifiers.id_aes256_CFB.getId(), valueOf4);
hashMap2.put(NISTObjectIdentifiers.id_aes128_OFB.getId(), valueOf2);
hashMap2.put(NISTObjectIdentifiers.id_aes192_OFB.getId(), valueOf3);
hashMap2.put(NISTObjectIdentifiers.id_aes256_OFB.getId(), valueOf4);
hashMap2.put(NISTObjectIdentifiers.id_aes128_wrap.getId(), valueOf2);
hashMap2.put(NISTObjectIdentifiers.id_aes192_wrap.getId(), valueOf3);
hashMap2.put(NISTObjectIdentifiers.id_aes256_wrap.getId(), valueOf4);
hashMap2.put(NISTObjectIdentifiers.id_aes128_CCM.getId(), valueOf2);
hashMap2.put(NISTObjectIdentifiers.id_aes192_CCM.getId(), valueOf3);
hashMap2.put(NISTObjectIdentifiers.id_aes256_CCM.getId(), valueOf4);
hashMap2.put(NISTObjectIdentifiers.id_aes128_GCM.getId(), valueOf2);
hashMap2.put(NISTObjectIdentifiers.id_aes192_GCM.getId(), valueOf3);
hashMap2.put(NISTObjectIdentifiers.id_aes256_GCM.getId(), valueOf4);
hashMap2.put(NTTObjectIdentifiers.id_camellia128_wrap.getId(), valueOf2);
hashMap2.put(NTTObjectIdentifiers.id_camellia192_wrap.getId(), valueOf3);
hashMap2.put(NTTObjectIdentifiers.id_camellia256_wrap.getId(), valueOf4);
hashMap2.put(KISAObjectIdentifiers.id_npki_app_cmsSeed_wrap.getId(), valueOf2);
hashMap2.put(PKCSObjectIdentifiers.id_alg_CMS3DESwrap.getId(), valueOf3);
hashMap2.put(PKCSObjectIdentifiers.des_EDE3_CBC.getId(), valueOf3);
hashMap2.put(OIWObjectIdentifiers.desCBC.getId(), valueOf);
hashMap2.put(PKCSObjectIdentifiers.id_hmacWithSHA1.getId(), Integers.valueOf(160));
hashMap2.put(PKCSObjectIdentifiers.id_hmacWithSHA256.getId(), valueOf4);
hashMap2.put(PKCSObjectIdentifiers.id_hmacWithSHA384.getId(), Integers.valueOf(384));
hashMap2.put(PKCSObjectIdentifiers.id_hmacWithSHA512.getId(), Integers.valueOf(512));
hashMap.put("DESEDE", PKCSObjectIdentifiers.des_EDE3_CBC);
hashMap.put("AES", NISTObjectIdentifiers.id_aes256_CBC);
hashMap.put("CAMELLIA", NTTObjectIdentifiers.id_camellia256_cbc);
hashMap.put("SEED", KISAObjectIdentifiers.id_seedCBC);
hashMap.put("DES", OIWObjectIdentifiers.desCBC);
hashMap3.put(MiscObjectIdentifiers.cast5CBC.getId(), "CAST5");
hashMap3.put(MiscObjectIdentifiers.as_sys_sec_alg_ideaCBC.getId(), "IDEA");
hashMap3.put(MiscObjectIdentifiers.cryptlib_algorithm_blowfish_ECB.getId(), "Blowfish");
hashMap3.put(MiscObjectIdentifiers.cryptlib_algorithm_blowfish_CBC.getId(), "Blowfish");
hashMap3.put(MiscObjectIdentifiers.cryptlib_algorithm_blowfish_CFB.getId(), "Blowfish");
hashMap3.put(MiscObjectIdentifiers.cryptlib_algorithm_blowfish_OFB.getId(), "Blowfish");
hashMap3.put(OIWObjectIdentifiers.desECB.getId(), "DES");
hashMap3.put(OIWObjectIdentifiers.desCBC.getId(), "DES");
hashMap3.put(OIWObjectIdentifiers.desCFB.getId(), "DES");
hashMap3.put(OIWObjectIdentifiers.desOFB.getId(), "DES");
hashMap3.put(OIWObjectIdentifiers.desEDE.getId(), "DESede");
hashMap3.put(PKCSObjectIdentifiers.des_EDE3_CBC.getId(), "DESede");
hashMap3.put(PKCSObjectIdentifiers.id_alg_CMS3DESwrap.getId(), "DESede");
hashMap3.put(PKCSObjectIdentifiers.id_alg_CMSRC2wrap.getId(), "RC2");
hashMap3.put(PKCSObjectIdentifiers.id_hmacWithSHA1.getId(), "HmacSHA1");
hashMap3.put(PKCSObjectIdentifiers.id_hmacWithSHA224.getId(), "HmacSHA224");
hashMap3.put(PKCSObjectIdentifiers.id_hmacWithSHA256.getId(), "HmacSHA256");
hashMap3.put(PKCSObjectIdentifiers.id_hmacWithSHA384.getId(), "HmacSHA384");
hashMap3.put(PKCSObjectIdentifiers.id_hmacWithSHA512.getId(), "HmacSHA512");
hashMap3.put(NTTObjectIdentifiers.id_camellia128_cbc.getId(), "Camellia");
hashMap3.put(NTTObjectIdentifiers.id_camellia192_cbc.getId(), "Camellia");
hashMap3.put(NTTObjectIdentifiers.id_camellia256_cbc.getId(), "Camellia");
hashMap3.put(NTTObjectIdentifiers.id_camellia128_wrap.getId(), "Camellia");
hashMap3.put(NTTObjectIdentifiers.id_camellia192_wrap.getId(), "Camellia");
hashMap3.put(NTTObjectIdentifiers.id_camellia256_wrap.getId(), "Camellia");
hashMap3.put(KISAObjectIdentifiers.id_npki_app_cmsSeed_wrap.getId(), "SEED");
hashMap3.put(KISAObjectIdentifiers.id_seedCBC.getId(), "SEED");
hashMap3.put(KISAObjectIdentifiers.id_seedMAC.getId(), "SEED");
hashMap3.put(CryptoProObjectIdentifiers.gostR28147_gcfb.getId(), "GOST28147");
hashMap3.put(NISTObjectIdentifiers.id_aes128_wrap.getId(), "AES");
hashMap3.put(NISTObjectIdentifiers.id_aes128_CCM.getId(), "AES");
hashMap3.put(NISTObjectIdentifiers.id_aes128_CCM.getId(), "AES");
hashtable.put("DESEDE", PKCSObjectIdentifiers.des_EDE3_CBC);
hashtable.put("AES", NISTObjectIdentifiers.id_aes256_CBC);
hashtable.put("DES", OIWObjectIdentifiers.desCBC);
hashtable2.put("DES", "DES");
hashtable2.put("DESEDE", "DES");
hashtable2.put(OIWObjectIdentifiers.desCBC.getId(), "DES");
hashtable2.put(PKCSObjectIdentifiers.des_EDE3_CBC.getId(), "DES");
hashtable2.put(PKCSObjectIdentifiers.id_alg_CMS3DESwrap.getId(), "DES");
}
}