package com.android.org.conscrypt;

import com.android.org.conscrypt.OpenSSLCipher;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidParameterSpecException;

/* JADX INFO: loaded from: classes.dex */
public abstract class OpenSSLAeadCipherAES extends OpenSSLAeadCipher {
    private static final int AES_BLOCK_SIZE = 16;

    public static class GCM extends OpenSSLAeadCipherAES {

        public static class AES_128 extends GCM {
            @Override // com.android.org.conscrypt.OpenSSLAeadCipherAES, com.android.org.conscrypt.OpenSSLCipher
            void checkSupportedKeySize(int i3) throws InvalidKeyException {
                if (i3 == OpenSSLAeadCipherAES.AES_BLOCK_SIZE) {
                    return;
                }
                throw new InvalidKeyException("Unsupported key size: " + i3 + " bytes (must be 16)");
            }
        }

        public static class AES_256 extends GCM {
            @Override // com.android.org.conscrypt.OpenSSLAeadCipherAES, 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)");
            }
        }

        public GCM() {
            super(OpenSSLCipher.Mode.GCM);
        }

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

        @Override // com.android.org.conscrypt.OpenSSLAeadCipher
        long getEVP_AEAD(int i3) throws InvalidKeyException {
            if (i3 == OpenSSLAeadCipherAES.AES_BLOCK_SIZE) {
                return NativeCrypto.EVP_aead_aes_128_gcm();
            }
            if (i3 == 32) {
                return NativeCrypto.EVP_aead_aes_256_gcm();
            }
            throw new RuntimeException("Unexpected key length: " + i3);
        }
    }

    public static class GCM_SIV extends OpenSSLAeadCipherAES {

        public static class AES_128 extends GCM_SIV {
            @Override // com.android.org.conscrypt.OpenSSLAeadCipherAES, com.android.org.conscrypt.OpenSSLCipher
            void checkSupportedKeySize(int i3) throws InvalidKeyException {
                if (i3 == OpenSSLAeadCipherAES.AES_BLOCK_SIZE) {
                    return;
                }
                throw new InvalidKeyException("Unsupported key size: " + i3 + " bytes (must be 16)");
            }
        }

        public static class AES_256 extends GCM_SIV {
            @Override // com.android.org.conscrypt.OpenSSLAeadCipherAES, 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)");
            }
        }

        public GCM_SIV() {
            super(OpenSSLCipher.Mode.GCM_SIV);
        }

        @Override // com.android.org.conscrypt.OpenSSLAeadCipher
        boolean allowsNonceReuse() {
            return true;
        }

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

        @Override // com.android.org.conscrypt.OpenSSLAeadCipher
        void checkSupportedTagLength(int i3) throws InvalidAlgorithmParameterException {
            if (i3 != 128) {
                throw new InvalidAlgorithmParameterException("Tag length must be 128 bits");
            }
        }

        @Override // com.android.org.conscrypt.OpenSSLAeadCipher
        long getEVP_AEAD(int i3) throws InvalidKeyException {
            if (i3 == OpenSSLAeadCipherAES.AES_BLOCK_SIZE) {
                return NativeCrypto.EVP_aead_aes_128_gcm_siv();
            }
            if (i3 == 32) {
                return NativeCrypto.EVP_aead_aes_256_gcm_siv();
            }
            throw new RuntimeException("Unexpected key length: " + i3);
        }
    }

    OpenSSLAeadCipherAES(OpenSSLCipher.Mode mode) {
        super(mode);
    }

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

    @Override // com.android.org.conscrypt.OpenSSLCipher, javax.crypto.CipherSpi
    protected AlgorithmParameters engineGetParameters() {
        byte[] bArr = this.iv;
        if (bArr == null) {
            return null;
        }
        AlgorithmParameterSpec gCMParameterSpec = Platform.toGCMParameterSpec(this.tagLengthInBytes * 8, bArr);
        if (gCMParameterSpec == null) {
            return super.engineGetParameters();
        }
        try {
            AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance("GCM");
            algorithmParameters.init(gCMParameterSpec);
            return algorithmParameters;
        } catch (NoSuchAlgorithmException e3) {
            throw ((Error) new AssertionError("GCM not supported").initCause(e3));
        } catch (InvalidParameterSpecException unused) {
            return null;
        }
    }

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

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

    @Override // com.android.org.conscrypt.OpenSSLAeadCipher, com.android.org.conscrypt.OpenSSLCipher
    int getOutputSizeForFinal(int i3) {
        return isEncrypting() ? this.bufCount + i3 + this.tagLengthInBytes : Math.max(0, (this.bufCount + i3) - this.tagLengthInBytes);
    }

    @Override // com.android.org.conscrypt.OpenSSLCipher
    protected AlgorithmParameterSpec getParameterSpec(AlgorithmParameters algorithmParameters) throws InvalidAlgorithmParameterException {
        if (algorithmParameters == null) {
            return null;
        }
        AlgorithmParameterSpec algorithmParameterSpecFromGCMParameters = Platform.fromGCMParameters(algorithmParameters);
        return algorithmParameterSpecFromGCMParameters != null ? algorithmParameterSpecFromGCMParameters : super.getParameterSpec(algorithmParameters);
    }
}
