package org.bouncycastle.jcajce.provider.asymmetric.elgamal; import java.math.BigInteger; import java.security.AlgorithmParameters; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.InvalidParameterException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.spec.AlgorithmParameterSpec; import java.security.spec.MGF1ParameterSpec; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.interfaces.DHKey; import javax.crypto.spec.OAEPParameterSpec; import javax.crypto.spec.PSource; import org.bouncycastle.crypto.AsymmetricBlockCipher; import org.bouncycastle.crypto.BufferedAsymmetricBlockCipher; import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.Digest; import org.bouncycastle.crypto.InvalidCipherTextException; import org.bouncycastle.crypto.encodings.ISO9796d1Encoding; import org.bouncycastle.crypto.encodings.OAEPEncoding; import org.bouncycastle.crypto.encodings.PKCS1Encoding; import org.bouncycastle.crypto.engines.ElGamalEngine; import org.bouncycastle.crypto.params.ParametersWithRandom; import org.bouncycastle.jcajce.provider.asymmetric.util.BaseCipherSpi; import org.bouncycastle.jcajce.provider.util.DigestFactory; import org.bouncycastle.jce.interfaces.ElGamalKey; import org.bouncycastle.jce.interfaces.ElGamalPrivateKey; import org.bouncycastle.jce.interfaces.ElGamalPublicKey; import org.bouncycastle.pqc.jcajce.spec.McElieceCCA2KeyGenParameterSpec; import org.bouncycastle.util.Strings; /* loaded from: classes6.dex */ public class CipherSpi extends BaseCipherSpi { private BufferedAsymmetricBlockCipher cipher; private AlgorithmParameters engineParams; private AlgorithmParameterSpec paramSpec; @Override // javax.crypto.CipherSpi protected byte[] engineUpdate(byte[] bArr, int i, int i2) { this.cipher.processBytes(bArr, i, i2); return null; } @Override // javax.crypto.CipherSpi protected int engineUpdate(byte[] bArr, int i, int i2, byte[] bArr2, int i3) { this.cipher.processBytes(bArr, i, i2); return 0; } @Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseCipherSpi, javax.crypto.CipherSpi public void engineSetPadding(String str) throws NoSuchPaddingException { OAEPParameterSpec oAEPParameterSpec; BufferedAsymmetricBlockCipher bufferedAsymmetricBlockCipher; String upperCase = Strings.toUpperCase(str); if (upperCase.equals("NOPADDING")) { bufferedAsymmetricBlockCipher = new BufferedAsymmetricBlockCipher(new ElGamalEngine()); } else if (upperCase.equals("PKCS1PADDING")) { bufferedAsymmetricBlockCipher = new BufferedAsymmetricBlockCipher(new PKCS1Encoding(new ElGamalEngine())); } else { if (!upperCase.equals("ISO9796-1PADDING")) { if (!upperCase.equals("OAEPPADDING")) { if (upperCase.equals("OAEPWITHMD5ANDMGF1PADDING")) { oAEPParameterSpec = new OAEPParameterSpec("MD5", "MGF1", new MGF1ParameterSpec("MD5"), PSource.PSpecified.DEFAULT); } else if (!upperCase.equals("OAEPWITHSHA1ANDMGF1PADDING")) { if (upperCase.equals("OAEPWITHSHA224ANDMGF1PADDING")) { oAEPParameterSpec = new OAEPParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA224, "MGF1", new MGF1ParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA224), PSource.PSpecified.DEFAULT); } else if (upperCase.equals("OAEPWITHSHA256ANDMGF1PADDING")) { oAEPParameterSpec = new OAEPParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA256, "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT); } else if (upperCase.equals("OAEPWITHSHA384ANDMGF1PADDING")) { oAEPParameterSpec = new OAEPParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA384, "MGF1", MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT); } else { if (!upperCase.equals("OAEPWITHSHA512ANDMGF1PADDING")) { StringBuilder sb = new StringBuilder(); sb.append(str); sb.append(" unavailable with ElGamal."); throw new NoSuchPaddingException(sb.toString()); } oAEPParameterSpec = new OAEPParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA512, "MGF1", MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT); } } initFromSpec(oAEPParameterSpec); return; } oAEPParameterSpec = OAEPParameterSpec.DEFAULT; initFromSpec(oAEPParameterSpec); return; } bufferedAsymmetricBlockCipher = new BufferedAsymmetricBlockCipher(new ISO9796d1Encoding(new ElGamalEngine())); } this.cipher = bufferedAsymmetricBlockCipher; } @Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseCipherSpi, javax.crypto.CipherSpi public void engineSetMode(String str) throws NoSuchAlgorithmException { String upperCase = Strings.toUpperCase(str); if (!upperCase.equals("NONE") && !upperCase.equals("ECB")) { throw new NoSuchAlgorithmException("can't support mode ".concat(String.valueOf(str))); } } @Override // javax.crypto.CipherSpi protected void engineInit(int i, Key key, AlgorithmParameterSpec algorithmParameterSpec, SecureRandom secureRandom) throws InvalidKeyException { CipherParameters generatePrivateKeyParameter; BufferedAsymmetricBlockCipher bufferedAsymmetricBlockCipher; if (algorithmParameterSpec != null) { throw new IllegalArgumentException("unknown parameter type."); } if (key instanceof ElGamalPublicKey) { generatePrivateKeyParameter = ElGamalUtil.generatePublicKeyParameter((PublicKey) key); } else { if (!(key instanceof ElGamalPrivateKey)) { throw new InvalidKeyException("unknown key type passed to ElGamal"); } generatePrivateKeyParameter = ElGamalUtil.generatePrivateKeyParameter((PrivateKey) key); } if (secureRandom != null) { generatePrivateKeyParameter = new ParametersWithRandom(generatePrivateKeyParameter, secureRandom); } boolean z = true; if (i != 1) { if (i != 2) { if (i != 3) { if (i != 4) { StringBuilder sb = new StringBuilder("unknown opmode "); sb.append(i); sb.append(" passed to ElGamal"); throw new InvalidParameterException(sb.toString()); } } } bufferedAsymmetricBlockCipher = this.cipher; z = false; bufferedAsymmetricBlockCipher.init(z, generatePrivateKeyParameter); } bufferedAsymmetricBlockCipher = this.cipher; bufferedAsymmetricBlockCipher.init(z, generatePrivateKeyParameter); } @Override // javax.crypto.CipherSpi protected void engineInit(int i, Key key, SecureRandom secureRandom) throws InvalidKeyException { engineInit(i, key, (AlgorithmParameterSpec) null, secureRandom); } @Override // javax.crypto.CipherSpi protected void engineInit(int i, Key key, AlgorithmParameters algorithmParameters, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException { throw new InvalidAlgorithmParameterException("can't handle parameters in ElGamal"); } @Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseCipherSpi, javax.crypto.CipherSpi public AlgorithmParameters engineGetParameters() { if (this.engineParams == null && this.paramSpec != null) { try { AlgorithmParameters createParametersInstance = createParametersInstance("OAEP"); this.engineParams = createParametersInstance; createParametersInstance.init(this.paramSpec); } catch (Exception e) { throw new RuntimeException(e.toString()); } } return this.engineParams; } @Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseCipherSpi, javax.crypto.CipherSpi public int engineGetOutputSize(int i) { return this.cipher.getOutputBlockSize(); } @Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseCipherSpi, javax.crypto.CipherSpi public int engineGetKeySize(Key key) { BigInteger p; if (key instanceof ElGamalKey) { p = ((ElGamalKey) key).getParameters().getP(); } else { if (!(key instanceof DHKey)) { throw new IllegalArgumentException("not an ElGamal key!"); } p = ((DHKey) key).getParams().getP(); } return p.bitLength(); } @Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseCipherSpi, javax.crypto.CipherSpi public int engineGetBlockSize() { return this.cipher.getInputBlockSize(); } @Override // javax.crypto.CipherSpi protected byte[] engineDoFinal(byte[] bArr, int i, int i2) throws IllegalBlockSizeException, BadPaddingException { this.cipher.processBytes(bArr, i, i2); try { return this.cipher.doFinal(); } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } } @Override // javax.crypto.CipherSpi protected int engineDoFinal(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws IllegalBlockSizeException, BadPaddingException { this.cipher.processBytes(bArr, i, i2); try { byte[] doFinal = this.cipher.doFinal(); for (int i4 = 0; i4 != doFinal.length; i4++) { bArr2[i3 + i4] = doFinal[i4]; } return doFinal.length; } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } } /* loaded from: classes6.dex */ public static class NoPadding extends CipherSpi { public NoPadding() { super(new ElGamalEngine()); } } /* loaded from: classes6.dex */ public static class PKCS1v1_5Padding extends CipherSpi { public PKCS1v1_5Padding() { super(new PKCS1Encoding(new ElGamalEngine())); } } private void initFromSpec(OAEPParameterSpec oAEPParameterSpec) throws NoSuchPaddingException { MGF1ParameterSpec mGF1ParameterSpec = (MGF1ParameterSpec) oAEPParameterSpec.getMGFParameters(); Digest digest = DigestFactory.getDigest(mGF1ParameterSpec.getDigestAlgorithm()); if (digest != null) { this.cipher = new BufferedAsymmetricBlockCipher(new OAEPEncoding(new ElGamalEngine(), digest, ((PSource.PSpecified) oAEPParameterSpec.getPSource()).getValue())); this.paramSpec = oAEPParameterSpec; } else { StringBuilder sb = new StringBuilder("no match on OAEP constructor for digest algorithm: "); sb.append(mGF1ParameterSpec.getDigestAlgorithm()); throw new NoSuchPaddingException(sb.toString()); } } public CipherSpi(AsymmetricBlockCipher asymmetricBlockCipher) { this.cipher = new BufferedAsymmetricBlockCipher(asymmetricBlockCipher); } }