package org.bouncycastle.openpgp.operator.bc;

import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.edec.EdECObjectIdentifiers;
import org.bouncycastle.bcpg.ECDHPublicBCPGKey;
import org.bouncycastle.bcpg.MPInteger;
import org.bouncycastle.bcpg.PublicKeyPacket;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
import org.bouncycastle.crypto.KeyGenerationParameters;
import org.bouncycastle.crypto.RawAgreement;
import org.bouncycastle.crypto.Wrapper;
import org.bouncycastle.crypto.agreement.BasicRawAgreement;
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement;
import org.bouncycastle.crypto.agreement.X25519Agreement;
import org.bouncycastle.crypto.agreement.X448Agreement;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.generators.X25519KeyPairGenerator;
import org.bouncycastle.crypto.generators.X448KeyPairGenerator;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.params.X25519KeyGenerationParameters;
import org.bouncycastle.crypto.params.X25519PublicKeyParameters;
import org.bouncycastle.crypto.params.X448KeyGenerationParameters;
import org.bouncycastle.crypto.params.X448PublicKeyParameters;
import org.bouncycastle.jcajce.spec.XDHParameterSpec;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.operator.PGPPad;
import org.bouncycastle.openpgp.operator.PublicKeyKeyEncryptionMethodGenerator;
import org.bouncycastle.openpgp.operator.RFC6637Utils;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public class BcPublicKeyKeyEncryptionMethodGenerator extends PublicKeyKeyEncryptionMethodGenerator {
    private static final byte X_HDR = 64;
    private BcPGPKeyConverter keyConverter;
    private SecureRandom random;

    @FunctionalInterface
    private interface EphPubEncodingOperation {
        void getEphPubEncoding(AsymmetricKeyParameter asymmetricKeyParameter, byte[] bArr);
    }

    public BcPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey pGPPublicKey) {
        super(pGPPublicKey);
        this.keyConverter = new BcPGPKeyConverter();
    }

    private byte[] encryptSessionInfoWithECDHKey(byte[] bArr, byte[] bArr2, byte[] bArr3, byte[] bArr4, int i, int i2) throws IOException, PGPException {
        return getSessionInfo(new MPInteger(new BigInteger(1, bArr4)).getEncoded(), (byte) 0, getWrapper(i2, new KeyParameter(new RFC6637KDFCalculator(new BcPGPDigestCalculatorProvider().get(i), i2).createKey(bArr2, bArr3)), PGPPad.padSessionData(bArr, this.sessionKeyObfuscation)));
    }

    private byte[] encryptSessionInfoWithX25519X448Key(PublicKeyPacket publicKeyPacket, byte[] bArr, int i, int i2, String str, AsymmetricCipherKeyPairGenerator asymmetricCipherKeyPairGenerator, KeyGenerationParameters keyGenerationParameters, RawAgreement rawAgreement, AsymmetricKeyParameter asymmetricKeyParameter, int i3, EphPubEncodingOperation ephPubEncodingOperation, byte b) throws PGPException {
        AsymmetricCipherKeyPair asymmetricCipherKeyPair = getAsymmetricCipherKeyPair(asymmetricCipherKeyPairGenerator, keyGenerationParameters);
        byte[] secret = BcUtil.getSecret(rawAgreement, asymmetricCipherKeyPair.getPrivate(), asymmetricKeyParameter);
        byte[] bArr2 = new byte[i3];
        ephPubEncodingOperation.getEphPubEncoding(asymmetricCipherKeyPair.getPublic(), bArr2);
        return getSessionInfo(bArr2, b, getWrapper(i2, new KeyParameter(RFC6637KDFCalculator.createKey(i, i2, Arrays.concatenate(bArr2, publicKeyPacket.getKey().getEncoded(), secret), "OpenPGP " + str)), bArr));
    }

    private AsymmetricCipherKeyPair getAsymmetricCipherKeyPair(AsymmetricCipherKeyPairGenerator asymmetricCipherKeyPairGenerator, KeyGenerationParameters keyGenerationParameters) {
        asymmetricCipherKeyPairGenerator.init(keyGenerationParameters);
        return asymmetricCipherKeyPairGenerator.generateKeyPair();
    }

    private byte[] getWrapper(int i, KeyParameter keyParameter, byte[] bArr) throws PGPException {
        Wrapper wrapperCreateWrapper = BcImplProvider.createWrapper(i);
        wrapperCreateWrapper.init(true, new ParametersWithRandom(keyParameter, this.random));
        return wrapperCreateWrapper.wrap(bArr, 0, bArr.length);
    }

    @Override // org.bouncycastle.openpgp.operator.PublicKeyKeyEncryptionMethodGenerator
    protected byte[] encryptSessionInfo(PGPPublicKey pGPPublicKey, byte[] bArr, byte b, boolean z) throws PGPException {
        try {
            AsymmetricKeyParameter publicKey = this.keyConverter.getPublicKey(pGPPublicKey);
            PublicKeyPacket publicKeyPacket = pGPPublicKey.getPublicKeyPacket();
            if (pGPPublicKey.getAlgorithm() != 18) {
                if (pGPPublicKey.getAlgorithm() == 25) {
                    return encryptSessionInfoWithX25519X448Key(publicKeyPacket, bArr, 8, 7, XDHParameterSpec.X25519, new X25519KeyPairGenerator(), new X25519KeyGenerationParameters(this.random), new X25519Agreement(), publicKey, 32, new EphPubEncodingOperation() { // from class: org.bouncycastle.openpgp.operator.bc.BcPublicKeyKeyEncryptionMethodGenerator.1
                        @Override // org.bouncycastle.openpgp.operator.bc.BcPublicKeyKeyEncryptionMethodGenerator.EphPubEncodingOperation
                        public void getEphPubEncoding(AsymmetricKeyParameter asymmetricKeyParameter, byte[] bArr2) {
                            ((X25519PublicKeyParameters) asymmetricKeyParameter).encode(bArr2, 0);
                        }
                    }, z ? b : (byte) 0);
                }
                if (pGPPublicKey.getAlgorithm() == 26) {
                    return encryptSessionInfoWithX25519X448Key(publicKeyPacket, bArr, 10, 9, XDHParameterSpec.X448, new X448KeyPairGenerator(), new X448KeyGenerationParameters(this.random), new X448Agreement(), publicKey, 56, new EphPubEncodingOperation() { // from class: org.bouncycastle.openpgp.operator.bc.BcPublicKeyKeyEncryptionMethodGenerator.2
                        @Override // org.bouncycastle.openpgp.operator.bc.BcPublicKeyKeyEncryptionMethodGenerator.EphPubEncodingOperation
                        public void getEphPubEncoding(AsymmetricKeyParameter asymmetricKeyParameter, byte[] bArr2) {
                            ((X448PublicKeyParameters) asymmetricKeyParameter).encode(bArr2, 0);
                        }
                    }, z ? b : (byte) 0);
                }
                AsymmetricBlockCipher asymmetricBlockCipherCreatePublicKeyCipher = BcImplProvider.createPublicKeyCipher(pGPPublicKey.getAlgorithm());
                asymmetricBlockCipherCreatePublicKeyCipher.init(true, new ParametersWithRandom(publicKey, this.random));
                byte[] bArrCreateSessionInfo = createSessionInfo(z ? b : (byte) 0, bArr);
                return asymmetricBlockCipherCreatePublicKeyCipher.processBlock(bArrCreateSessionInfo, 0, bArrCreateSessionInfo.length);
            }
            ECDHPublicBCPGKey eCDHPublicBCPGKey = (ECDHPublicBCPGKey) publicKeyPacket.getKey();
            byte[] bArrCreateSessionInfo2 = createSessionInfo(z ? b : (byte) 0, bArr);
            byte[] bArrCreateUserKeyingMaterial = RFC6637Utils.createUserKeyingMaterial(publicKeyPacket, new BcKeyFingerprintCalculator());
            if (BcUtil.isX25519(eCDHPublicBCPGKey.getCurveOID())) {
                AsymmetricCipherKeyPair asymmetricCipherKeyPair = getAsymmetricCipherKeyPair(new X25519KeyPairGenerator(), new X25519KeyGenerationParameters(this.random));
                byte[] secret = BcUtil.getSecret(new X25519Agreement(), asymmetricCipherKeyPair.getPrivate(), publicKey);
                byte[] bArr2 = new byte[33];
                bArr2[0] = 64;
                ((X25519PublicKeyParameters) asymmetricCipherKeyPair.getPublic()).encode(bArr2, 1);
                return encryptSessionInfoWithECDHKey(bArrCreateSessionInfo2, secret, bArrCreateUserKeyingMaterial, bArr2, eCDHPublicBCPGKey.getHashAlgorithm(), eCDHPublicBCPGKey.getSymmetricKeyAlgorithm());
            }
            if (!eCDHPublicBCPGKey.getCurveOID().equals((ASN1Primitive) EdECObjectIdentifiers.id_X448)) {
                AsymmetricCipherKeyPair asymmetricCipherKeyPair2 = getAsymmetricCipherKeyPair(new ECKeyPairGenerator(), new ECKeyGenerationParameters(((ECPublicKeyParameters) publicKey).getParameters(), this.random));
                return encryptSessionInfoWithECDHKey(bArrCreateSessionInfo2, BcUtil.getSecret(new BasicRawAgreement(new ECDHBasicAgreement()), asymmetricCipherKeyPair2.getPrivate(), publicKey), bArrCreateUserKeyingMaterial, ((ECPublicKeyParameters) asymmetricCipherKeyPair2.getPublic()).getQ().getEncoded(false), eCDHPublicBCPGKey.getHashAlgorithm(), eCDHPublicBCPGKey.getSymmetricKeyAlgorithm());
            }
            AsymmetricCipherKeyPair asymmetricCipherKeyPair3 = getAsymmetricCipherKeyPair(new X448KeyPairGenerator(), new X448KeyGenerationParameters(this.random));
            byte[] secret2 = BcUtil.getSecret(new X448Agreement(), asymmetricCipherKeyPair3.getPrivate(), publicKey);
            byte[] bArr3 = new byte[57];
            bArr3[0] = 64;
            ((X448PublicKeyParameters) asymmetricCipherKeyPair3.getPublic()).encode(bArr3, 1);
            return encryptSessionInfoWithECDHKey(bArrCreateSessionInfo2, secret2, bArrCreateUserKeyingMaterial, bArr3, eCDHPublicBCPGKey.getHashAlgorithm(), eCDHPublicBCPGKey.getSymmetricKeyAlgorithm());
        } catch (Exception e) {
            throw new PGPException("exception encrypting session info: " + e.getMessage(), e);
        }
    }

    public BcPublicKeyKeyEncryptionMethodGenerator setSecureRandom(SecureRandom secureRandom) {
        this.random = secureRandom;
        return this;
    }
}
