// 
// Decompiled by Procyon v0.6.0
// 

package com.google.crypto.tink.hybrid;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Immutable;
import com.google.crypto.tink.internal.TinkBugException;
import java.util.Objects;
import java.security.GeneralSecurityException;
import java.util.Collections;
import com.google.crypto.tink.daead.AesSivParameters;
import com.google.crypto.tink.aead.XChaCha20Poly1305Parameters;
import com.google.crypto.tink.aead.AesCtrHmacAeadParameters;
import com.google.crypto.tink.aead.AesGcmParameters;
import java.util.HashSet;
import com.google.crypto.tink.util.Bytes;
import javax.annotation.Nullable;
import com.google.crypto.tink.Parameters;
import java.util.Set;

public final class EciesParameters extends HybridParameters
{
    private static final Set<Parameters> acceptedDemParameters;
    private final CurveType curveType;
    private final HashType hashType;
    @Nullable
    private final PointFormat nistCurvePointFormat;
    private final Variant variant;
    private final Parameters demParameters;
    @Nullable
    private final Bytes salt;
    
    private static Set<Parameters> listAcceptedDemParameters() throws GeneralSecurityException {
        final HashSet<Parameters> acceptedDemParameters = new HashSet<Parameters>();
        acceptedDemParameters.add(AesGcmParameters.builder().setIvSizeBytes(12).setKeySizeBytes(16).setTagSizeBytes(16).setVariant(AesGcmParameters.Variant.NO_PREFIX).build());
        acceptedDemParameters.add(AesGcmParameters.builder().setIvSizeBytes(12).setKeySizeBytes(32).setTagSizeBytes(16).setVariant(AesGcmParameters.Variant.NO_PREFIX).build());
        acceptedDemParameters.add(AesCtrHmacAeadParameters.builder().setAesKeySizeBytes(16).setHmacKeySizeBytes(32).setTagSizeBytes(16).setIvSizeBytes(16).setHashType(AesCtrHmacAeadParameters.HashType.SHA256).setVariant(AesCtrHmacAeadParameters.Variant.NO_PREFIX).build());
        acceptedDemParameters.add(AesCtrHmacAeadParameters.builder().setAesKeySizeBytes(32).setHmacKeySizeBytes(32).setTagSizeBytes(32).setIvSizeBytes(16).setHashType(AesCtrHmacAeadParameters.HashType.SHA256).setVariant(AesCtrHmacAeadParameters.Variant.NO_PREFIX).build());
        acceptedDemParameters.add(XChaCha20Poly1305Parameters.create());
        acceptedDemParameters.add(AesSivParameters.builder().setKeySizeBytes(64).setVariant(AesSivParameters.Variant.NO_PREFIX).build());
        return Collections.unmodifiableSet((Set<? extends Parameters>)acceptedDemParameters);
    }
    
    private EciesParameters(final CurveType curveType, final HashType hashType, @Nullable final PointFormat pointFormat, final Parameters demParameters, final Variant variant, final Bytes salt) {
        this.curveType = curveType;
        this.hashType = hashType;
        this.nistCurvePointFormat = pointFormat;
        this.demParameters = demParameters;
        this.variant = variant;
        this.salt = salt;
    }
    
    public static Builder builder() {
        return new Builder();
    }
    
    public CurveType getCurveType() {
        return this.curveType;
    }
    
    public HashType getHashType() {
        return this.hashType;
    }
    
    @Nullable
    public PointFormat getNistCurvePointFormat() {
        return this.nistCurvePointFormat;
    }
    
    public Parameters getDemParameters() {
        return this.demParameters;
    }
    
    public Variant getVariant() {
        return this.variant;
    }
    
    @Nullable
    public Bytes getSalt() {
        return this.salt;
    }
    
    @Override
    public boolean hasIdRequirement() {
        return this.variant != Variant.NO_PREFIX;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (!(o instanceof EciesParameters)) {
            return false;
        }
        final EciesParameters that = (EciesParameters)o;
        return Objects.equals(that.getCurveType(), this.getCurveType()) && Objects.equals(that.getHashType(), this.getHashType()) && Objects.equals(that.getNistCurvePointFormat(), this.getNistCurvePointFormat()) && Objects.equals(that.getDemParameters(), this.getDemParameters()) && Objects.equals(that.getVariant(), this.getVariant()) && Objects.equals(that.getSalt(), this.getSalt());
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(EciesParameters.class, this.curveType, this.hashType, this.nistCurvePointFormat, this.demParameters, this.variant, this.salt);
    }
    
    @Override
    public String toString() {
        return String.format("EciesParameters(curveType=%s, hashType=%s, pointFormat=%s, demParameters=%s, variant=%s, salt=%s)", this.curveType, this.hashType, this.nistCurvePointFormat, this.demParameters, this.variant, this.salt);
    }
    
    static {
        acceptedDemParameters = TinkBugException.exceptionIsBug(() -> listAcceptedDemParameters());
    }
    
    @Immutable
    public static final class Variant
    {
        public static final Variant TINK;
        public static final Variant CRUNCHY;
        public static final Variant NO_PREFIX;
        private final String name;
        
        private Variant(final String name) {
            this.name = name;
        }
        
        @Override
        public String toString() {
            return this.name;
        }
        
        static {
            TINK = new Variant("TINK");
            CRUNCHY = new Variant("CRUNCHY");
            NO_PREFIX = new Variant("NO_PREFIX");
        }
    }
    
    @Immutable
    public static final class CurveType
    {
        public static final CurveType NIST_P256;
        public static final CurveType NIST_P384;
        public static final CurveType NIST_P521;
        public static final CurveType X25519;
        private final String name;
        
        private CurveType(final String name) {
            this.name = name;
        }
        
        @Override
        public String toString() {
            return this.name;
        }
        
        static {
            NIST_P256 = new CurveType("NIST_P256");
            NIST_P384 = new CurveType("NIST_P384");
            NIST_P521 = new CurveType("NIST_P521");
            X25519 = new CurveType("X25519");
        }
    }
    
    @Immutable
    public static final class HashType
    {
        public static final HashType SHA1;
        public static final HashType SHA224;
        public static final HashType SHA256;
        public static final HashType SHA384;
        public static final HashType SHA512;
        private final String name;
        
        private HashType(final String name) {
            this.name = name;
        }
        
        @Override
        public String toString() {
            return this.name;
        }
        
        static {
            SHA1 = new HashType("SHA1");
            SHA224 = new HashType("SHA224");
            SHA256 = new HashType("SHA256");
            SHA384 = new HashType("SHA384");
            SHA512 = new HashType("SHA512");
        }
    }
    
    @Immutable
    public static final class PointFormat
    {
        public static final PointFormat COMPRESSED;
        public static final PointFormat UNCOMPRESSED;
        public static final PointFormat LEGACY_UNCOMPRESSED;
        private final String name;
        
        private PointFormat(final String name) {
            this.name = name;
        }
        
        @Override
        public String toString() {
            return this.name;
        }
        
        static {
            COMPRESSED = new PointFormat("COMPRESSED");
            UNCOMPRESSED = new PointFormat("UNCOMPRESSED");
            LEGACY_UNCOMPRESSED = new PointFormat("LEGACY_UNCOMPRESSED");
        }
    }
    
    public static final class Builder
    {
        private CurveType curveType;
        private HashType hashType;
        private PointFormat nistCurvePointFormat;
        private Parameters demParameters;
        private Variant variant;
        @Nullable
        private Bytes salt;
        
        private Builder() {
            this.curveType = null;
            this.hashType = null;
            this.nistCurvePointFormat = null;
            this.demParameters = null;
            this.variant = Variant.NO_PREFIX;
            this.salt = null;
        }
        
        @CanIgnoreReturnValue
        public Builder setCurveType(final CurveType curveType) {
            this.curveType = curveType;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setHashType(final HashType hashType) {
            this.hashType = hashType;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setNistCurvePointFormat(final PointFormat pointFormat) {
            this.nistCurvePointFormat = pointFormat;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setDemParameters(final Parameters demParameters) throws GeneralSecurityException {
            if (!EciesParameters.acceptedDemParameters.contains(demParameters)) {
                throw new GeneralSecurityException("Invalid DEM parameters " + demParameters + "; only AES128_GCM_RAW, AES256_GCM_RAW, AES128_CTR_HMAC_SHA256_RAW, AES256_CTR_HMAC_SHA256_RAW XCHACHA20_POLY1305_RAW and AES256_SIV_RAW are currently supported.");
            }
            this.demParameters = demParameters;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setVariant(final Variant variant) {
            this.variant = variant;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setSalt(final Bytes salt) {
            if (salt.size() == 0) {
                this.salt = null;
                return this;
            }
            this.salt = salt;
            return this;
        }
        
        public EciesParameters build() throws GeneralSecurityException {
            if (this.curveType == null) {
                throw new GeneralSecurityException("Elliptic curve type is not set");
            }
            if (this.hashType == null) {
                throw new GeneralSecurityException("Hash type is not set");
            }
            if (this.demParameters == null) {
                throw new GeneralSecurityException("DEM parameters are not set");
            }
            if (this.variant == null) {
                throw new GeneralSecurityException("Variant is not set");
            }
            if (this.curveType != CurveType.X25519 && this.nistCurvePointFormat == null) {
                throw new GeneralSecurityException("Point format is not set");
            }
            if (this.curveType == CurveType.X25519 && this.nistCurvePointFormat != null) {
                throw new GeneralSecurityException("For Curve25519 point format must not be set");
            }
            return new EciesParameters(this.curveType, this.hashType, this.nistCurvePointFormat, this.demParameters, this.variant, this.salt, null);
        }
    }
}
