package org.bouncycastle.openpgp.operator.bc;

import java.io.IOException;
import java.security.SecureRandom;
import org.bouncycastle.bcpg.AEADUtils;
import org.bouncycastle.bcpg.PublicKeyPacket;
import org.bouncycastle.bcpg.S2K;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.operator.AEADSecretKeyEncryptorBuilder;
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;

/* JADX INFO: loaded from: classes.dex */
public class BcAEADSecretKeyEncryptorBuilder implements AEADSecretKeyEncryptorBuilder {
    private int aeadAlgorithm;
    private S2K.Argon2Params argon2Params;
    private SecureRandom random = new SecureRandom();
    private int symmetricAlgorithm;

    public BcAEADSecretKeyEncryptorBuilder(int i, int i2, S2K.Argon2Params argon2Params) {
        this.aeadAlgorithm = i;
        this.symmetricAlgorithm = i2;
        this.argon2Params = argon2Params;
    }

    @Override // org.bouncycastle.openpgp.operator.AEADSecretKeyEncryptorBuilder
    public PBESecretKeyEncryptor build(char[] cArr, final PublicKeyPacket publicKeyPacket) {
        return new PBESecretKeyEncryptor(this, this.symmetricAlgorithm, this.aeadAlgorithm, this.argon2Params, this.random, cArr) { // from class: org.bouncycastle.openpgp.operator.bc.BcAEADSecretKeyEncryptorBuilder.1
            private byte[] iv = new byte[AEADUtils.getIVLength(this.aeadAlgorithm)];
            final /* synthetic */ BcAEADSecretKeyEncryptorBuilder this$0;

            {
                this.this$0 = this;
                this.random.nextBytes(this.iv);
            }

            @Override // org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor
            public byte[] encryptKeyData(byte[] bArr, byte[] bArr2, int i, int i2) throws PGPException {
                try {
                    return BcAEADUtil.processAeadKeyData(true, this.encAlgorithm, this.aeadAlgorithm, getKey(), getCipherIV(), publicKeyPacket.getPacketTag() == 6 ? 5 : 7, publicKeyPacket.getVersion(), bArr2, i, i2, publicKeyPacket.getEncodedContents());
                } catch (IOException e) {
                    throw new PGPException("Exception AEAD protecting private key material", e);
                }
            }

            @Override // org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor
            public byte[] getCipherIV() {
                return this.iv;
            }
        };
    }

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