package org.bouncycastle.crypto.hpke;

import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.AEADCipher;
import org.bouncycastle.crypto.modes.ChaCha20Poly1305;
import org.bouncycastle.crypto.modes.GCMBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Bytes;
import org.bouncycastle.util.Pack;

/* JADX INFO: loaded from: classes2.dex */
public class AEAD {
    private final short aeadId;
    private final byte[] baseNonce;
    private AEADCipher cipher;
    private final byte[] key;
    private long seq;

    public AEAD(short s, byte[] bArr, byte[] bArr2) {
        AEADCipher aEADCipherNewInstance;
        this.seq = 0L;
        this.key = bArr;
        this.baseNonce = bArr2;
        this.aeadId = s;
        this.seq = 0L;
        switch (s) {
            case 1:
            case 2:
                aEADCipherNewInstance = GCMBlockCipher.newInstance(AESEngine.newInstance());
                break;
            case 3:
                aEADCipherNewInstance = new ChaCha20Poly1305();
                break;
            default:
                return;
        }
        this.cipher = aEADCipherNewInstance;
    }

    private byte[] computeNonce() throws CloneNotSupportedException {
        long j = this.seq;
        this.seq = 1 + j;
        byte[] bArrLongToBigEndian = Pack.longToBigEndian(j);
        byte[] bArrClone = Arrays.clone(this.baseNonce);
        Bytes.xorTo(8, bArrLongToBigEndian, 0, bArrClone, bArrClone.length - 8);
        return bArrClone;
    }

    private byte[] process(boolean z, byte[] bArr, byte[] bArr2, int i, int i2) throws InvalidCipherTextException {
        switch (this.aeadId) {
            case 1:
            case 2:
            case 3:
                this.cipher.init(z, new ParametersWithIV(new KeyParameter(this.key), computeNonce()));
                this.cipher.processAADBytes(bArr, 0, bArr.length);
                int outputSize = this.cipher.getOutputSize(i2);
                byte[] bArr3 = new byte[outputSize];
                int iProcessBytes = this.cipher.processBytes(bArr2, i, i2, bArr3, 0);
                if (iProcessBytes + this.cipher.doFinal(bArr3, iProcessBytes) == outputSize) {
                    return bArr3;
                }
                throw new IllegalStateException();
            default:
                throw new IllegalStateException("Export only mode, cannot be used to seal/open");
        }
    }

    public byte[] open(byte[] bArr, byte[] bArr2) throws InvalidCipherTextException {
        return process(false, bArr, bArr2, 0, bArr2.length);
    }

    public byte[] open(byte[] bArr, byte[] bArr2, int i, int i2) throws InvalidCipherTextException {
        Arrays.validateSegment(bArr2, i, i2);
        return process(false, bArr, bArr2, i, i2);
    }

    public byte[] seal(byte[] bArr, byte[] bArr2) throws InvalidCipherTextException {
        return process(true, bArr, bArr2, 0, bArr2.length);
    }

    public byte[] seal(byte[] bArr, byte[] bArr2, int i, int i2) throws InvalidCipherTextException {
        Arrays.validateSegment(bArr2, i, i2);
        return process(true, bArr, bArr2, i, i2);
    }
}
