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

package com.google.crypto.tink.signature;

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

public final class RsaSsaPssParameters extends SignatureParameters
{
    public static final BigInteger F4;
    private final int modulusSizeBits;
    private final BigInteger publicExponent;
    private final Variant variant;
    private final HashType sigHashType;
    private final HashType mgf1HashType;
    private final int saltLengthBytes;
    
    private RsaSsaPssParameters(final int modulusSizeBits, final BigInteger publicExponent, final Variant variant, final HashType sigHashType, final HashType mgf1HashType, final int saltLengthBytes) {
        this.modulusSizeBits = modulusSizeBits;
        this.publicExponent = publicExponent;
        this.variant = variant;
        this.sigHashType = sigHashType;
        this.mgf1HashType = mgf1HashType;
        this.saltLengthBytes = saltLengthBytes;
    }
    
    public static Builder builder() {
        return new Builder();
    }
    
    public int getModulusSizeBits() {
        return this.modulusSizeBits;
    }
    
    public BigInteger getPublicExponent() {
        return this.publicExponent;
    }
    
    public Variant getVariant() {
        return this.variant;
    }
    
    public HashType getSigHashType() {
        return this.sigHashType;
    }
    
    public HashType getMgf1HashType() {
        return this.mgf1HashType;
    }
    
    public int getSaltLengthBytes() {
        return this.saltLengthBytes;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (!(o instanceof RsaSsaPssParameters)) {
            return false;
        }
        final RsaSsaPssParameters that = (RsaSsaPssParameters)o;
        return that.getModulusSizeBits() == this.getModulusSizeBits() && Objects.equals(that.getPublicExponent(), this.getPublicExponent()) && Objects.equals(that.getVariant(), this.getVariant()) && Objects.equals(that.getSigHashType(), this.getSigHashType()) && Objects.equals(that.getMgf1HashType(), this.getMgf1HashType()) && that.getSaltLengthBytes() == this.getSaltLengthBytes();
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(RsaSsaPssParameters.class, this.modulusSizeBits, this.publicExponent, this.variant, this.sigHashType, this.mgf1HashType, this.saltLengthBytes);
    }
    
    @Override
    public boolean hasIdRequirement() {
        return this.variant != Variant.NO_PREFIX;
    }
    
    @Override
    public String toString() {
        return "RSA SSA PSS Parameters (variant: " + this.variant + ", signature hashType: " + this.sigHashType + ", mgf1 hashType: " + this.mgf1HashType + ", saltLengthBytes: " + this.saltLengthBytes + ", publicExponent: " + this.publicExponent + ", and " + this.modulusSizeBits + "-bit modulus)";
    }
    
    static {
        F4 = BigInteger.valueOf(65537L);
    }
    
    @Immutable
    public static final class Variant
    {
        public static final Variant TINK;
        public static final Variant CRUNCHY;
        public static final Variant LEGACY;
        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");
            LEGACY = new Variant("LEGACY");
            NO_PREFIX = new Variant("NO_PREFIX");
        }
    }
    
    @Immutable
    public static final class HashType
    {
        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 {
            SHA256 = new HashType("SHA256");
            SHA384 = new HashType("SHA384");
            SHA512 = new HashType("SHA512");
        }
    }
    
    public static final class Builder
    {
        @Nullable
        private Integer modulusSizeBits;
        @Nullable
        private BigInteger publicExponent;
        @Nullable
        private HashType sigHashType;
        @Nullable
        private HashType mgf1HashType;
        @Nullable
        private Integer saltLengthBytes;
        private Variant variant;
        private static final BigInteger TWO;
        private static final BigInteger PUBLIC_EXPONENT_UPPER_BOUND;
        private static final int MIN_RSA_MODULUS_SIZE = 2048;
        
        private Builder() {
            this.modulusSizeBits = null;
            this.publicExponent = RsaSsaPssParameters.F4;
            this.sigHashType = null;
            this.mgf1HashType = null;
            this.saltLengthBytes = null;
            this.variant = Variant.NO_PREFIX;
        }
        
        @CanIgnoreReturnValue
        public Builder setModulusSizeBits(final int modulusSizeBits) {
            this.modulusSizeBits = modulusSizeBits;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setPublicExponent(final BigInteger e) {
            this.publicExponent = e;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setVariant(final Variant variant) {
            this.variant = variant;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setSigHashType(final HashType sigHashType) {
            this.sigHashType = sigHashType;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setMgf1HashType(final HashType mgf1HashType) {
            this.mgf1HashType = mgf1HashType;
            return this;
        }
        
        @CanIgnoreReturnValue
        public Builder setSaltLengthBytes(final int saltLengthBytes) throws GeneralSecurityException {
            if (saltLengthBytes < 0) {
                throw new GeneralSecurityException(String.format("Invalid salt length in bytes %d; salt length must be positive", saltLengthBytes));
            }
            this.saltLengthBytes = saltLengthBytes;
            return this;
        }
        
        private void validatePublicExponent(final BigInteger publicExponent) throws InvalidAlgorithmParameterException {
            final int c = publicExponent.compareTo(RsaSsaPssParameters.F4);
            if (c == 0) {
                return;
            }
            if (c < 0) {
                throw new InvalidAlgorithmParameterException("Public exponent must be at least 65537.");
            }
            if (publicExponent.mod(Builder.TWO).equals(BigInteger.ZERO)) {
                throw new InvalidAlgorithmParameterException("Invalid public exponent");
            }
            if (publicExponent.compareTo(Builder.PUBLIC_EXPONENT_UPPER_BOUND) > 0) {
                throw new InvalidAlgorithmParameterException("Public exponent cannot be larger than 2^256.");
            }
        }
        
        public RsaSsaPssParameters build() throws GeneralSecurityException {
            if (this.modulusSizeBits == null) {
                throw new GeneralSecurityException("key size is not set");
            }
            if (this.publicExponent == null) {
                throw new GeneralSecurityException("publicExponent is not set");
            }
            if (this.sigHashType == null) {
                throw new GeneralSecurityException("signature hash type is not set");
            }
            if (this.mgf1HashType == null) {
                throw new GeneralSecurityException("mgf1 hash type is not set");
            }
            if (this.variant == null) {
                throw new GeneralSecurityException("variant is not set");
            }
            if (this.saltLengthBytes == null) {
                throw new GeneralSecurityException("salt length is not set");
            }
            if (this.modulusSizeBits < 2048) {
                throw new InvalidAlgorithmParameterException(String.format("Invalid key size in bytes %d; must be at least %d bits", this.modulusSizeBits, 2048));
            }
            if (this.sigHashType != this.mgf1HashType) {
                throw new GeneralSecurityException("MGF1 hash is different from signature hash");
            }
            this.validatePublicExponent(this.publicExponent);
            return new RsaSsaPssParameters(this.modulusSizeBits, this.publicExponent, this.variant, this.sigHashType, this.mgf1HashType, this.saltLengthBytes, null);
        }
        
        static {
            TWO = BigInteger.valueOf(2L);
            PUBLIC_EXPONENT_UPPER_BOUND = Builder.TWO.pow(256);
        }
    }
}
