package ea;

import com.google.crypto.tink.proto.KeyData$KeyMaterialType;
import com.google.crypto.tink.proto.OutputPrefixType;
import com.google.crypto.tink.shaded.protobuf.ByteString;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import ra.y0;

/* JADX INFO: loaded from: classes2.dex */
public final class e0 implements da.a {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final byte[] f25644d = new byte[0];

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final Set f25645e;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final String f25646a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final da.l f25647b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final da.a f25648c;

    static {
        HashSet hashSet = new HashSet();
        hashSet.add("type.googleapis.com/google.crypto.tink.AesGcmKey");
        hashSet.add("type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key");
        hashSet.add("type.googleapis.com/google.crypto.tink.XChaCha20Poly1305Key");
        hashSet.add("type.googleapis.com/google.crypto.tink.AesCtrHmacAeadKey");
        hashSet.add("type.googleapis.com/google.crypto.tink.AesGcmSivKey");
        hashSet.add("type.googleapis.com/google.crypto.tink.AesEaxKey");
        f25645e = Collections.unmodifiableSet(hashSet);
    }

    public e0(y0 y0Var, la.d dVar) {
        if (!f25645e.contains(y0Var.I())) {
            throw new IllegalArgumentException("Unsupported DEK key type: " + y0Var.I() + ". Only Tink AEAD key types are supported.");
        }
        this.f25646a = y0Var.I();
        ra.x0 x0VarL = y0.L(y0Var);
        x0VarL.i(OutputPrefixType.RAW);
        this.f25647b = com.bumptech.glide.f.m(((y0) x0VarL.c()).e());
        this.f25648c = dVar;
    }

    @Override // da.a
    public final byte[] a(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        com.bumptech.glide.g gVarB = ma.m.f30537b.b(this.f25647b, null);
        byte[] bArrA = this.f25648c.a(((ma.h0) ma.t.f30555b.g(gVarB)).f30522c.k(), f25644d);
        if (bArrA.length > 4096) {
            throw new GeneralSecurityException("length of encrypted DEK too large");
        }
        byte[] bArrA2 = ((da.a) ((ma.f0) ma.s.f30553b.f30554a.get()).a(gVarB, da.a.class)).a(bArr, bArr2);
        return ByteBuffer.allocate(bArrA.length + 4 + bArrA2.length).putInt(bArrA.length).put(bArrA).put(bArrA2).array();
    }

    @Override // da.a
    public final byte[] b(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        try {
            ByteBuffer byteBufferWrap = ByteBuffer.wrap(bArr);
            int i10 = byteBufferWrap.getInt();
            if (i10 <= 0 || i10 > 4096 || i10 > bArr.length - 4) {
                throw new GeneralSecurityException("length of encrypted DEK too large");
            }
            byte[] bArr3 = new byte[i10];
            byteBufferWrap.get(bArr3, 0, i10);
            byte[] bArr4 = new byte[byteBufferWrap.remaining()];
            byteBufferWrap.get(bArr4, 0, byteBufferWrap.remaining());
            byte[] bArrB = this.f25648c.b(bArr3, f25644d);
            String str = this.f25646a;
            ByteString byteString = ByteString.f23846a;
            return ((da.a) ((ma.f0) ma.s.f30553b.f30554a.get()).a(ma.t.f30555b.a(ma.h0.a(str, ByteString.g(0, bArrB.length, bArrB), KeyData$KeyMaterialType.SYMMETRIC, OutputPrefixType.RAW, null)), da.a.class)).b(bArr4, bArr2);
        } catch (IndexOutOfBoundsException e10) {
            e = e10;
            throw new GeneralSecurityException("invalid ciphertext", e);
        } catch (NegativeArraySizeException e11) {
            e = e11;
            throw new GeneralSecurityException("invalid ciphertext", e);
        } catch (BufferUnderflowException e12) {
            e = e12;
            throw new GeneralSecurityException("invalid ciphertext", e);
        }
    }
}
