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

package org.bouncycastle.pqc.jcajce.provider.util;

import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.crypto.digests.SHAKEDigest;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.macs.KMAC;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.digests.SHA384Digest;
import org.bouncycastle.crypto.params.HKDFParameters;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.crypto.agreement.kdf.ConcatenationKDFGenerator;
import org.bouncycastle.crypto.DerivationParameters;
import org.bouncycastle.crypto.params.KDFParameters;
import org.bouncycastle.crypto.generators.KDF2BytesGenerator;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.jcajce.spec.KEMKDFSpec;

public class KdfUtil
{
    public static byte[] makeKeyBytes(final KEMKDFSpec kemkdfSpec, final byte[] array) {
        byte[] keyBytes;
        try {
            if (kemkdfSpec == null) {
                keyBytes = new byte[array.length];
                System.arraycopy(array, 0, keyBytes, 0, keyBytes.length);
            }
            else {
                keyBytes = makeKeyBytes(kemkdfSpec.getKdfAlgorithm(), array, kemkdfSpec.getOtherInfo(), kemkdfSpec.getKeySize());
            }
        }
        finally {
            Arrays.clear(array);
        }
        return keyBytes;
    }
    
    static byte[] makeKeyBytes(final AlgorithmIdentifier algorithmIdentifier, final byte[] array, final byte[] array2, final int n) {
        final byte[] array3 = new byte[(n + 7) / 8];
        if (algorithmIdentifier == null) {
            System.arraycopy(array, 0, array3, 0, array3.length);
        }
        else if (X9ObjectIdentifiers.id_kdf_kdf2.equals(algorithmIdentifier.getAlgorithm())) {
            final KDF2BytesGenerator kdf2BytesGenerator = new KDF2BytesGenerator(getDigest(AlgorithmIdentifier.getInstance(algorithmIdentifier.getParameters()).getAlgorithm()));
            kdf2BytesGenerator.init(new KDFParameters(array, array2));
            kdf2BytesGenerator.generateBytes(array3, 0, array3.length);
        }
        else if (X9ObjectIdentifiers.id_kdf_kdf3.equals(algorithmIdentifier.getAlgorithm())) {
            final ConcatenationKDFGenerator concatenationKDFGenerator = new ConcatenationKDFGenerator(getDigest(AlgorithmIdentifier.getInstance(algorithmIdentifier.getParameters()).getAlgorithm()));
            concatenationKDFGenerator.init(new KDFParameters(array, array2));
            concatenationKDFGenerator.generateBytes(array3, 0, array3.length);
        }
        else if (PKCSObjectIdentifiers.id_alg_hkdf_with_sha256.equals(algorithmIdentifier.getAlgorithm())) {
            if (algorithmIdentifier.getParameters() != null) {
                throw new IllegalStateException("HDKF parameter support not added");
            }
            final HKDFBytesGenerator hkdfBytesGenerator = new HKDFBytesGenerator(new SHA256Digest());
            hkdfBytesGenerator.init(new HKDFParameters(array, null, array2));
            hkdfBytesGenerator.generateBytes(array3, 0, array3.length);
        }
        else if (PKCSObjectIdentifiers.id_alg_hkdf_with_sha384.equals(algorithmIdentifier.getAlgorithm())) {
            if (algorithmIdentifier.getParameters() != null) {
                throw new IllegalStateException("HDKF parameter support not added");
            }
            final HKDFBytesGenerator hkdfBytesGenerator2 = new HKDFBytesGenerator(new SHA384Digest());
            hkdfBytesGenerator2.init(new HKDFParameters(array, null, array2));
            hkdfBytesGenerator2.generateBytes(array3, 0, array3.length);
        }
        else if (PKCSObjectIdentifiers.id_alg_hkdf_with_sha512.equals(algorithmIdentifier.getAlgorithm())) {
            if (algorithmIdentifier.getParameters() != null) {
                throw new IllegalStateException("HDKF parameter support not added");
            }
            final HKDFBytesGenerator hkdfBytesGenerator3 = new HKDFBytesGenerator(new SHA512Digest());
            hkdfBytesGenerator3.init(new HKDFParameters(array, null, array2));
            hkdfBytesGenerator3.generateBytes(array3, 0, array3.length);
        }
        else if (NISTObjectIdentifiers.id_Kmac128.equals(algorithmIdentifier.getAlgorithm())) {
            byte[] octets = new byte[0];
            if (algorithmIdentifier.getParameters() != null) {
                octets = ASN1OctetString.getInstance(algorithmIdentifier.getParameters()).getOctets();
            }
            final KMAC kmac = new KMAC(128, octets);
            kmac.init(new KeyParameter(array, 0, array.length));
            kmac.update(array2, 0, array2.length);
            kmac.doFinal(array3, 0, array3.length);
        }
        else if (NISTObjectIdentifiers.id_Kmac256.equals(algorithmIdentifier.getAlgorithm())) {
            byte[] octets2 = new byte[0];
            if (algorithmIdentifier.getParameters() != null) {
                octets2 = ASN1OctetString.getInstance(algorithmIdentifier.getParameters()).getOctets();
            }
            final KMAC kmac2 = new KMAC(256, octets2);
            kmac2.init(new KeyParameter(array, 0, array.length));
            kmac2.update(array2, 0, array2.length);
            kmac2.doFinal(array3, 0, array3.length);
        }
        else {
            if (!NISTObjectIdentifiers.id_shake256.equals(algorithmIdentifier.getAlgorithm())) {
                throw new IllegalArgumentException("Unrecognized KDF: " + algorithmIdentifier.getAlgorithm());
            }
            final SHAKEDigest shakeDigest = new SHAKEDigest(256);
            shakeDigest.update(array, 0, array.length);
            shakeDigest.update(array2, 0, array2.length);
            shakeDigest.doFinal(array3, 0, array3.length);
        }
        return array3;
    }
    
    static Digest getDigest(final ASN1ObjectIdentifier obj) {
        if (obj.equals(NISTObjectIdentifiers.id_sha256)) {
            return new SHA256Digest();
        }
        if (obj.equals(NISTObjectIdentifiers.id_sha512)) {
            return new SHA512Digest();
        }
        if (obj.equals(NISTObjectIdentifiers.id_shake128)) {
            return new SHAKEDigest(128);
        }
        if (obj.equals(NISTObjectIdentifiers.id_shake256)) {
            return new SHAKEDigest(256);
        }
        throw new IllegalArgumentException("unrecognized digest OID: " + obj);
    }
}
