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

package org.bouncycastle.jcajce.provider.kdf.hkdf;

import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA384Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.DerivationParameters;
import org.bouncycastle.crypto.params.HKDFParameters;
import org.bouncycastle.jcajce.spec.HKDFParameterSpec;
import java.security.NoSuchAlgorithmException;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.SecretKey;
import java.security.spec.AlgorithmParameterSpec;
import java.security.InvalidAlgorithmParameterException;
import org.bouncycastle.crypto.Digest;
import javax.crypto.KDFParameters;
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
import javax.crypto.KDFSpi;

class HKDFSpi extends KDFSpi
{
    protected HKDFBytesGenerator hkdf;
    
    public HKDFSpi(final KDFParameters kdfParameters, final Digest digest) throws InvalidAlgorithmParameterException {
        super(requireNull(kdfParameters, "HKDF does not support parameters"));
        this.hkdf = new HKDFBytesGenerator(digest);
    }
    
    protected KDFParameters engineGetParameters() {
        return null;
    }
    
    protected SecretKey engineDeriveKey(final String algorithm, final AlgorithmParameterSpec algorithmParameterSpec) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
        return new SecretKeySpec(this.engineDeriveData(algorithmParameterSpec), algorithm);
    }
    
    protected byte[] engineDeriveData(final AlgorithmParameterSpec algorithmParameterSpec) throws InvalidAlgorithmParameterException {
        if (algorithmParameterSpec == null || (!(algorithmParameterSpec instanceof HKDFParameterSpec) && !(algorithmParameterSpec instanceof javax.crypto.spec.HKDFParameterSpec))) {
            throw new InvalidAlgorithmParameterException("Invalid AlgorithmParameterSpec provided");
        }
        if (algorithmParameterSpec instanceof final javax.crypto.spec.HKDFParameterSpec.ExtractThenExpand extractThenExpand) {
            final HKDFParameters hkdfParameters = new HKDFParameters(extractThenExpand.ikms().get(0).getEncoded(), extractThenExpand.salts().get(0).getEncoded(), extractThenExpand.info());
            final int length = extractThenExpand.length();
            this.hkdf.init(hkdfParameters);
            final byte[] array = new byte[length];
            this.hkdf.generateBytes(array, 0, length);
            return array;
        }
        if (algorithmParameterSpec instanceof final javax.crypto.spec.HKDFParameterSpec.Extract extract) {
            return this.hkdf.extractPRK(((SecretKey)extract.salts().get(0)).getEncoded(), ((SecretKey)extract.ikms().get(0)).getEncoded());
        }
        if (algorithmParameterSpec instanceof final HKDFParameterSpec hkdfParameterSpec) {
            final HKDFParameters hkdfParameters2 = new HKDFParameters(hkdfParameterSpec.getIKM(), hkdfParameterSpec.getSalt(), hkdfParameterSpec.getInfo());
            final int outputLength = hkdfParameterSpec.getOutputLength();
            this.hkdf.init(hkdfParameters2);
            final byte[] array2 = new byte[outputLength];
            this.hkdf.generateBytes(array2, 0, outputLength);
            return array2;
        }
        throw new InvalidAlgorithmParameterException("invalid HKDFParameterSpec provided");
    }
    
    private static KDFParameters requireNull(final KDFParameters kdfParameters, final String msg) throws InvalidAlgorithmParameterException {
        if (kdfParameters != null) {
            throw new InvalidAlgorithmParameterException(msg);
        }
        return null;
    }
    
    public static class HKDFwithSHA512 extends HKDFSpi
    {
        public HKDFwithSHA512(final KDFParameters kdfParameters) throws InvalidAlgorithmParameterException {
            super(kdfParameters, (Digest)new SHA512Digest());
        }
        
        public HKDFwithSHA512() throws InvalidAlgorithmParameterException {
            this(null);
        }
    }
    
    public static class HKDFwithSHA384 extends HKDFSpi
    {
        public HKDFwithSHA384(final KDFParameters kdfParameters) throws InvalidAlgorithmParameterException {
            super(kdfParameters, (Digest)new SHA384Digest());
        }
        
        public HKDFwithSHA384() throws InvalidAlgorithmParameterException {
            this(null);
        }
    }
    
    public static class HKDFwithSHA256 extends HKDFSpi
    {
        public HKDFwithSHA256(final KDFParameters kdfParameters) throws InvalidAlgorithmParameterException {
            super(kdfParameters, (Digest)new SHA256Digest());
        }
        
        public HKDFwithSHA256() throws InvalidAlgorithmParameterException {
            this(null);
        }
    }
}
