134 lines
5.2 KiB
Java
134 lines
5.2 KiB
Java
|
package org.bouncycastle.jcajce.provider.symmetric.util;
|
||
|
|
||
|
import java.security.InvalidAlgorithmParameterException;
|
||
|
import java.security.InvalidKeyException;
|
||
|
import java.security.Key;
|
||
|
import java.security.spec.AlgorithmParameterSpec;
|
||
|
import java.util.Hashtable;
|
||
|
import java.util.Map;
|
||
|
import javax.crypto.MacSpi;
|
||
|
import javax.crypto.SecretKey;
|
||
|
import javax.crypto.interfaces.PBEKey;
|
||
|
import javax.crypto.spec.IvParameterSpec;
|
||
|
import javax.crypto.spec.PBEParameterSpec;
|
||
|
import org.bouncycastle.crypto.CipherParameters;
|
||
|
import org.bouncycastle.crypto.Mac;
|
||
|
import org.bouncycastle.crypto.params.KeyParameter;
|
||
|
import org.bouncycastle.crypto.params.ParametersWithIV;
|
||
|
import org.bouncycastle.crypto.params.SkeinParameters;
|
||
|
import org.bouncycastle.jcajce.PKCS12Key;
|
||
|
import org.bouncycastle.jcajce.provider.symmetric.util.PBE;
|
||
|
import org.bouncycastle.jcajce.spec.SkeinParameterSpec;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public class BaseMac extends MacSpi implements PBE {
|
||
|
private int keySize;
|
||
|
private Mac macEngine;
|
||
|
private int pbeHash;
|
||
|
private int scheme;
|
||
|
|
||
|
@Override // javax.crypto.MacSpi
|
||
|
protected void engineUpdate(byte[] bArr, int i, int i2) {
|
||
|
this.macEngine.update(bArr, i, i2);
|
||
|
}
|
||
|
|
||
|
@Override // javax.crypto.MacSpi
|
||
|
protected void engineUpdate(byte b) {
|
||
|
this.macEngine.update(b);
|
||
|
}
|
||
|
|
||
|
@Override // javax.crypto.MacSpi
|
||
|
protected void engineReset() {
|
||
|
this.macEngine.reset();
|
||
|
}
|
||
|
|
||
|
@Override // javax.crypto.MacSpi
|
||
|
protected void engineInit(Key key, AlgorithmParameterSpec algorithmParameterSpec) throws InvalidKeyException, InvalidAlgorithmParameterException {
|
||
|
int i;
|
||
|
CipherParameters makePBEMacParameters;
|
||
|
if (key == null) {
|
||
|
throw new InvalidKeyException("key is null");
|
||
|
}
|
||
|
if (key instanceof PKCS12Key) {
|
||
|
try {
|
||
|
SecretKey secretKey = (SecretKey) key;
|
||
|
try {
|
||
|
PBEParameterSpec pBEParameterSpec = (PBEParameterSpec) algorithmParameterSpec;
|
||
|
if ((secretKey instanceof PBEKey) && pBEParameterSpec == null) {
|
||
|
PBEKey pBEKey = (PBEKey) secretKey;
|
||
|
pBEParameterSpec = new PBEParameterSpec(pBEKey.getSalt(), pBEKey.getIterationCount());
|
||
|
}
|
||
|
int i2 = 256;
|
||
|
if (this.macEngine.getAlgorithmName().startsWith("GOST")) {
|
||
|
i = 6;
|
||
|
} else if (this.macEngine.getAlgorithmName().startsWith("SHA256")) {
|
||
|
i = 4;
|
||
|
} else {
|
||
|
i = 1;
|
||
|
i2 = 160;
|
||
|
}
|
||
|
makePBEMacParameters = PBE.Util.makePBEMacParameters(secretKey, 2, i, i2, pBEParameterSpec);
|
||
|
} catch (Exception unused) {
|
||
|
throw new InvalidAlgorithmParameterException("PKCS12 requires a PBEParameterSpec");
|
||
|
}
|
||
|
} catch (Exception unused2) {
|
||
|
throw new InvalidKeyException("PKCS12 requires a SecretKey/PBEKey");
|
||
|
}
|
||
|
} else if (key instanceof BCPBEKey) {
|
||
|
BCPBEKey bCPBEKey = (BCPBEKey) key;
|
||
|
if (bCPBEKey.getParam() != null) {
|
||
|
makePBEMacParameters = bCPBEKey.getParam();
|
||
|
} else {
|
||
|
if (!(algorithmParameterSpec instanceof PBEParameterSpec)) {
|
||
|
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
|
||
|
}
|
||
|
makePBEMacParameters = PBE.Util.makePBEMacParameters(bCPBEKey, algorithmParameterSpec);
|
||
|
}
|
||
|
} else if (algorithmParameterSpec instanceof IvParameterSpec) {
|
||
|
makePBEMacParameters = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) algorithmParameterSpec).getIV());
|
||
|
} else if (algorithmParameterSpec instanceof SkeinParameterSpec) {
|
||
|
makePBEMacParameters = new SkeinParameters.Builder(copyMap(((SkeinParameterSpec) algorithmParameterSpec).getParameters())).setKey(key.getEncoded()).build();
|
||
|
} else {
|
||
|
if (algorithmParameterSpec != null) {
|
||
|
throw new InvalidAlgorithmParameterException("unknown parameter type.");
|
||
|
}
|
||
|
makePBEMacParameters = new KeyParameter(key.getEncoded());
|
||
|
}
|
||
|
this.macEngine.init(makePBEMacParameters);
|
||
|
}
|
||
|
|
||
|
@Override // javax.crypto.MacSpi
|
||
|
protected int engineGetMacLength() {
|
||
|
return this.macEngine.getMacSize();
|
||
|
}
|
||
|
|
||
|
@Override // javax.crypto.MacSpi
|
||
|
protected byte[] engineDoFinal() {
|
||
|
byte[] bArr = new byte[engineGetMacLength()];
|
||
|
this.macEngine.doFinal(bArr, 0);
|
||
|
return bArr;
|
||
|
}
|
||
|
|
||
|
private static Hashtable copyMap(Map map) {
|
||
|
Hashtable hashtable = new Hashtable();
|
||
|
for (Object obj : map.keySet()) {
|
||
|
hashtable.put(obj, map.get(obj));
|
||
|
}
|
||
|
return hashtable;
|
||
|
}
|
||
|
|
||
|
public BaseMac(Mac mac, int i, int i2, int i3) {
|
||
|
this.macEngine = mac;
|
||
|
this.scheme = i;
|
||
|
this.pbeHash = i2;
|
||
|
this.keySize = i3;
|
||
|
}
|
||
|
|
||
|
public BaseMac(Mac mac) {
|
||
|
this.scheme = 2;
|
||
|
this.pbeHash = 1;
|
||
|
this.keySize = 160;
|
||
|
this.macEngine = mac;
|
||
|
}
|
||
|
}
|