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

package com.google.crypto.tink.streamingaead;

import java.security.GeneralSecurityException;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import javax.annotation.Nullable;
import com.google.errorprone.annotations.Immutable;
import java.util.Objects;

public class AesCtrHmacStreamingParameters extends StreamingAeadParameters
{
    private final Integer keySizeBytes;
    private final Integer derivedKeySizeBytes;
    private final HashType hkdfHashType;
    private final HashType hmacHashType;
    private final Integer hmacTagSizeBytes;
    private final Integer ciphertextSegmentSizeBytes;
    
    public static Builder builder() {
        return new Builder();
    }
    
    private AesCtrHmacStreamingParameters(final Integer keySizeBytes, final Integer derivedKeySizeBytes, final HashType hkdfHashType, final HashType hmacHashType, final Integer hmacTagSizeBytes, final Integer ciphertextSegmentSizeBytes) {
        this.keySizeBytes = keySizeBytes;
        this.derivedKeySizeBytes = derivedKeySizeBytes;
        this.hkdfHashType = hkdfHashType;
        this.hmacHashType = hmacHashType;
        this.hmacTagSizeBytes = hmacTagSizeBytes;
        this.ciphertextSegmentSizeBytes = ciphertextSegmentSizeBytes;
    }
    
    public int getKeySizeBytes() {
        return this.keySizeBytes;
    }
    
    public int getDerivedKeySizeBytes() {
        return this.derivedKeySizeBytes;
    }
    
    public HashType getHkdfHashType() {
        return this.hkdfHashType;
    }
    
    public HashType getHmacHashType() {
        return this.hmacHashType;
    }
    
    public int getHmacTagSizeBytes() {
        return this.hmacTagSizeBytes;
    }
    
    public int getCiphertextSegmentSizeBytes() {
        return this.ciphertextSegmentSizeBytes;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (!(o instanceof AesCtrHmacStreamingParameters)) {
            return false;
        }
        final AesCtrHmacStreamingParameters that = (AesCtrHmacStreamingParameters)o;
        return that.getKeySizeBytes() == this.getKeySizeBytes() && that.getDerivedKeySizeBytes() == this.getDerivedKeySizeBytes() && that.getHkdfHashType() == this.getHkdfHashType() && that.getHmacHashType() == this.getHmacHashType() && that.getHmacTagSizeBytes() == this.getHmacTagSizeBytes() && that.getCiphertextSegmentSizeBytes() == this.getCiphertextSegmentSizeBytes();
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(AesCtrHmacStreamingParameters.class, this.keySizeBytes, this.derivedKeySizeBytes, this.hkdfHashType, this.hmacHashType, this.hmacTagSizeBytes, this.ciphertextSegmentSizeBytes);
    }
    
    @Override
    public String toString() {
        return "AesCtrHmacStreaming Parameters (IKM size: " + this.keySizeBytes + ", " + this.derivedKeySizeBytes + "-byte AES key, " + this.hkdfHashType + " for HKDF, " + this.hkdfHashType + " for HMAC, " + this.hmacTagSizeBytes + "-byte tags, " + this.ciphertextSegmentSizeBytes + "-byte ciphertexts)";
    }
    
    @Immutable
    public static final class HashType
    {
        public static final HashType SHA1;
        public static final HashType SHA256;
        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");
            SHA256 = new HashType("SHA256");
            SHA512 = new HashType("SHA512");
        }
    }
    
    public static final class Builder
    {
        @Nullable
        private Integer keySizeBytes;
        @Nullable
        private Integer derivedKeySizeBytes;
        @Nullable
        private HashType hkdfHashType;
        @Nullable
        private HashType hmacHashType;
        @Nullable
        private Integer hmacTagSizeBytes;
        @Nullable
        private Integer ciphertextSegmentSizeBytes;
        
        public Builder() {
            this.keySizeBytes = null;
            this.derivedKeySizeBytes = null;
            this.hkdfHashType = null;
            this.hmacHashType = null;
            this.hmacTagSizeBytes = null;
            this.ciphertextSegmentSizeBytes = null;
        }
        
        @CanIgnoreReturnValue
        public Builder setKeySizeBytes(final int keySizeBytes) {
            this.keySizeBytes = keySizeBytes;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setDerivedKeySizeBytes(final int derivedKeySizeBytes) {
            this.derivedKeySizeBytes = derivedKeySizeBytes;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setHkdfHashType(final HashType hkdfHashType) {
            this.hkdfHashType = hkdfHashType;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setHmacHashType(final HashType hmacHashType) {
            this.hmacHashType = hmacHashType;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setHmacTagSizeBytes(final Integer hmacTagSizeBytes) {
            this.hmacTagSizeBytes = hmacTagSizeBytes;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setCiphertextSegmentSizeBytes(final int ciphertextSegmentSizeBytes) {
            this.ciphertextSegmentSizeBytes = ciphertextSegmentSizeBytes;
            return this;
        }
        
        public AesCtrHmacStreamingParameters build() throws GeneralSecurityException {
            if (this.keySizeBytes == null) {
                throw new GeneralSecurityException("keySizeBytes needs to be set");
            }
            if (this.derivedKeySizeBytes == null) {
                throw new GeneralSecurityException("derivedKeySizeBytes needs to be set");
            }
            if (this.hkdfHashType == null) {
                throw new GeneralSecurityException("hkdfHashType needs to be set");
            }
            if (this.hmacHashType == null) {
                throw new GeneralSecurityException("hmacHashType needs to be set");
            }
            if (this.hmacTagSizeBytes == null) {
                throw new GeneralSecurityException("hmacTagSizeBytes needs to be set");
            }
            if (this.ciphertextSegmentSizeBytes == null) {
                throw new GeneralSecurityException("ciphertextSegmentSizeBytes needs to be set");
            }
            if (this.derivedKeySizeBytes != 16 && this.derivedKeySizeBytes != 32) {
                throw new GeneralSecurityException("derivedKeySizeBytes needs to be 16 or 32, not " + this.derivedKeySizeBytes);
            }
            if (this.keySizeBytes < this.derivedKeySizeBytes) {
                throw new GeneralSecurityException("keySizeBytes needs to be at least derivedKeySizeBytes, i.e., " + this.derivedKeySizeBytes);
            }
            if (this.ciphertextSegmentSizeBytes <= this.derivedKeySizeBytes + this.hmacTagSizeBytes + 8) {
                throw new GeneralSecurityException("ciphertextSegmentSizeBytes needs to be at least derivedKeySizeBytes + hmacTagSizeBytes + 9, i.e., " + (this.derivedKeySizeBytes + this.hmacTagSizeBytes + 9));
            }
            final int hmacTagSizeLowerBound = 10;
            int hmacTagSizeUpperBound = 0;
            if (this.hmacHashType == HashType.SHA1) {
                hmacTagSizeUpperBound = 20;
            }
            if (this.hmacHashType == HashType.SHA256) {
                hmacTagSizeUpperBound = 32;
            }
            if (this.hmacHashType == HashType.SHA512) {
                hmacTagSizeUpperBound = 64;
            }
            if (this.hmacTagSizeBytes < hmacTagSizeLowerBound || this.hmacTagSizeBytes > hmacTagSizeUpperBound) {
                throw new GeneralSecurityException("hmacTagSize must be in range [" + hmacTagSizeLowerBound + ", " + hmacTagSizeUpperBound + "], but is " + this.hmacTagSizeBytes);
            }
            return new AesCtrHmacStreamingParameters(this.keySizeBytes, this.derivedKeySizeBytes, this.hkdfHashType, this.hmacHashType, this.hmacTagSizeBytes, this.ciphertextSegmentSizeBytes, null);
        }
    }
}
