package com.android.org.conscrypt;

import com.android.org.conscrypt.OpenSSLCipher;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;

/* JADX INFO: loaded from: classes.dex */
public class OpenSSLCipherChaCha20 extends OpenSSLCipher {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    private static final int BLOCK_SIZE_BYTES = 64;
    private static final int NONCE_SIZE_BYTES = 12;
    private int currentBlockConsumedBytes = 0;
    private int blockCounter = 0;

    private void reset() {
        this.blockCounter = 0;
        this.currentBlockConsumedBytes = 0;
    }

    @Override // com.android.org.conscrypt.OpenSSLCipher
    void checkSupportedKeySize(int i3) throws InvalidKeyException {
        if (i3 == 32) {
            return;
        }
        throw new InvalidKeyException("Unsupported key size: " + i3 + " bytes (must be 32)");
    }

    @Override // com.android.org.conscrypt.OpenSSLCipher
    void checkSupportedMode(OpenSSLCipher.Mode mode) throws NoSuchAlgorithmException {
        if (mode != OpenSSLCipher.Mode.NONE) {
            throw new NoSuchAlgorithmException("Mode must be NONE");
        }
    }

    @Override // com.android.org.conscrypt.OpenSSLCipher
    void checkSupportedPadding(OpenSSLCipher.Padding padding) throws NoSuchPaddingException {
        if (padding != OpenSSLCipher.Padding.NOPADDING) {
            throw new NoSuchPaddingException("Must be NoPadding");
        }
    }

    @Override // com.android.org.conscrypt.OpenSSLCipher
    int doFinalInternal(byte[] bArr, int i3, int i4) throws BadPaddingException, IllegalBlockSizeException, ShortBufferException {
        reset();
        return 0;
    }

    @Override // com.android.org.conscrypt.OpenSSLCipher
    void engineInitInternal(byte[] bArr, AlgorithmParameterSpec algorithmParameterSpec, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException {
        if (algorithmParameterSpec instanceof IvParameterSpec) {
            IvParameterSpec ivParameterSpec = (IvParameterSpec) algorithmParameterSpec;
            if (ivParameterSpec.getIV().length != 12) {
                throw new InvalidAlgorithmParameterException("IV must be 12 bytes long");
            }
            this.iv = ivParameterSpec.getIV();
            return;
        }
        if (!isEncrypting()) {
            throw new InvalidAlgorithmParameterException("IV must be specified when decrypting");
        }
        byte[] bArr2 = new byte[12];
        this.iv = bArr2;
        if (secureRandom != null) {
            secureRandom.nextBytes(bArr2);
        } else {
            NativeCrypto.RAND_bytes(bArr2);
        }
    }

    @Override // com.android.org.conscrypt.OpenSSLCipher
    String getBaseCipherName() {
        return "ChaCha20";
    }

    @Override // com.android.org.conscrypt.OpenSSLCipher
    int getCipherBlockSize() {
        return 0;
    }

    @Override // com.android.org.conscrypt.OpenSSLCipher
    int getOutputSizeForFinal(int i3) {
        return i3;
    }

    @Override // com.android.org.conscrypt.OpenSSLCipher
    int getOutputSizeForUpdate(int i3) {
        return i3;
    }

    @Override // com.android.org.conscrypt.OpenSSLCipher
    int updateInternal(byte[] bArr, int i3, int i4, byte[] bArr2, int i5, int i6) throws ShortBufferException {
        int i7;
        int i8;
        int i9;
        if (i4 > bArr2.length - i5) {
            throw new ShortBufferWithoutStackTraceException("Insufficient output space");
        }
        int i10 = this.currentBlockConsumedBytes;
        if (i10 > 0) {
            int iMin = Math.min(64 - i10, i4);
            byte[] bArr3 = new byte[BLOCK_SIZE_BYTES];
            byte[] bArr4 = new byte[BLOCK_SIZE_BYTES];
            System.arraycopy(bArr, i3, bArr3, this.currentBlockConsumedBytes, iMin);
            NativeCrypto.chacha20_encrypt_decrypt(bArr3, 0, bArr4, 0, BLOCK_SIZE_BYTES, this.encodedKey, this.iv, this.blockCounter);
            System.arraycopy(bArr4, this.currentBlockConsumedBytes, bArr2, i5, iMin);
            int i11 = this.currentBlockConsumedBytes + iMin;
            this.currentBlockConsumedBytes = i11;
            if (i11 < BLOCK_SIZE_BYTES) {
                return iMin;
            }
            this.currentBlockConsumedBytes = 0;
            int i12 = i3 + iMin;
            int i13 = i5 + iMin;
            int i14 = i4 - iMin;
            this.blockCounter++;
            i9 = i13;
            i7 = i12;
            i8 = i14;
        } else {
            i7 = i3;
            i8 = i4;
            i9 = i5;
        }
        NativeCrypto.chacha20_encrypt_decrypt(bArr, i7, bArr2, i9, i8, this.encodedKey, this.iv, this.blockCounter);
        this.currentBlockConsumedBytes = i8 % BLOCK_SIZE_BYTES;
        this.blockCounter += i8 / BLOCK_SIZE_BYTES;
        return i4;
    }
}
