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

package org.bouncycastle.cms.bc;

import org.bouncycastle.cms.CMSAlgorithm;
import java.util.HashSet;
import org.bouncycastle.crypto.util.CipherKeyGeneratorFactory;
import org.bouncycastle.crypto.CipherKeyGenerator;
import org.bouncycastle.crypto.util.AlgorithmIdentifierFactory;
import java.security.SecureRandom;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.util.CipherFactory;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.crypto.engines.RC2Engine;
import org.bouncycastle.crypto.engines.DESEngine;
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
import org.bouncycastle.crypto.engines.DESedeEngine;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.engines.RFC3211WrapEngine;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.crypto.Wrapper;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.operator.OperatorCreationException;
import java.util.Collections;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.digests.SHA384Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA224Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.ExtendedDigest;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.operator.bc.BcDigestProvider;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import java.util.HashMap;
import java.util.Set;
import java.util.Map;

class EnvelopedDataHelper
{
    protected static final Map BASE_CIPHER_NAMES;
    protected static final Map MAC_ALG_NAMES;
    private static final Set authEnvelopedAlgorithms;
    private static final Map prfs;
    
    private static Map createTable() {
        final HashMap m = new HashMap();
        m.put(PKCSObjectIdentifiers.id_hmacWithSHA1, new BcDigestProvider() {
            @Override
            public ExtendedDigest get(final AlgorithmIdentifier algorithmIdentifier) {
                return new SHA1Digest();
            }
        });
        m.put(PKCSObjectIdentifiers.id_hmacWithSHA224, new BcDigestProvider() {
            @Override
            public ExtendedDigest get(final AlgorithmIdentifier algorithmIdentifier) {
                return new SHA224Digest();
            }
        });
        m.put(PKCSObjectIdentifiers.id_hmacWithSHA256, new BcDigestProvider() {
            @Override
            public ExtendedDigest get(final AlgorithmIdentifier algorithmIdentifier) {
                return SHA256Digest.newInstance();
            }
        });
        m.put(PKCSObjectIdentifiers.id_hmacWithSHA384, new BcDigestProvider() {
            @Override
            public ExtendedDigest get(final AlgorithmIdentifier algorithmIdentifier) {
                return new SHA384Digest();
            }
        });
        m.put(PKCSObjectIdentifiers.id_hmacWithSHA512, new BcDigestProvider() {
            @Override
            public ExtendedDigest get(final AlgorithmIdentifier algorithmIdentifier) {
                return new SHA512Digest();
            }
        });
        return Collections.unmodifiableMap((Map<?, ?>)m);
    }
    
    static ExtendedDigest getPRF(final AlgorithmIdentifier algorithmIdentifier) throws OperatorCreationException {
        return EnvelopedDataHelper.prfs.get(algorithmIdentifier.getAlgorithm()).get(null);
    }
    
    static Wrapper createRFC3211Wrapper(final ASN1ObjectIdentifier obj) throws CMSException {
        if (NISTObjectIdentifiers.id_aes128_CBC.equals(obj) || NISTObjectIdentifiers.id_aes192_CBC.equals(obj) || NISTObjectIdentifiers.id_aes256_CBC.equals(obj)) {
            return new RFC3211WrapEngine(AESEngine.newInstance());
        }
        if (PKCSObjectIdentifiers.des_EDE3_CBC.equals(obj)) {
            return new RFC3211WrapEngine(new DESedeEngine());
        }
        if (OIWObjectIdentifiers.desCBC.equals(obj)) {
            return new RFC3211WrapEngine(new DESEngine());
        }
        if (PKCSObjectIdentifiers.RC2_CBC.equals(obj)) {
            return new RFC3211WrapEngine(new RC2Engine());
        }
        throw new CMSException("cannot recognise wrapper: " + obj);
    }
    
    static Object createContentCipher(final boolean b, final CipherParameters cipherParameters, final AlgorithmIdentifier algorithmIdentifier) throws CMSException {
        try {
            return CipherFactory.createContentCipher(b, cipherParameters, algorithmIdentifier);
        }
        catch (final IllegalArgumentException ex) {
            throw new CMSException(ex.getMessage(), ex);
        }
    }
    
    AlgorithmIdentifier generateEncryptionAlgID(final ASN1ObjectIdentifier asn1ObjectIdentifier, final KeyParameter keyParameter, final SecureRandom secureRandom) throws CMSException {
        try {
            return AlgorithmIdentifierFactory.generateEncryptionAlgID(asn1ObjectIdentifier, keyParameter.getKey().length * 8, secureRandom);
        }
        catch (final IllegalArgumentException ex) {
            throw new CMSException(ex.getMessage(), ex);
        }
    }
    
    CipherKeyGenerator createKeyGenerator(final ASN1ObjectIdentifier asn1ObjectIdentifier, final int n, final SecureRandom secureRandom) throws CMSException {
        try {
            return CipherKeyGeneratorFactory.createKeyGenerator(asn1ObjectIdentifier, secureRandom);
        }
        catch (final IllegalArgumentException ex) {
            throw new CMSException(ex.getMessage(), ex);
        }
    }
    
    boolean isAuthEnveloped(final ASN1ObjectIdentifier asn1ObjectIdentifier) {
        return EnvelopedDataHelper.authEnvelopedAlgorithms.contains(asn1ObjectIdentifier);
    }
    
    static {
        BASE_CIPHER_NAMES = new HashMap();
        MAC_ALG_NAMES = new HashMap();
        authEnvelopedAlgorithms = new HashSet();
        prfs = createTable();
        EnvelopedDataHelper.BASE_CIPHER_NAMES.put(CMSAlgorithm.DES_EDE3_CBC, "DESEDE");
        EnvelopedDataHelper.BASE_CIPHER_NAMES.put(CMSAlgorithm.AES128_CBC, "AES");
        EnvelopedDataHelper.BASE_CIPHER_NAMES.put(CMSAlgorithm.AES192_CBC, "AES");
        EnvelopedDataHelper.BASE_CIPHER_NAMES.put(CMSAlgorithm.AES256_CBC, "AES");
        EnvelopedDataHelper.MAC_ALG_NAMES.put(CMSAlgorithm.DES_EDE3_CBC, "DESEDEMac");
        EnvelopedDataHelper.MAC_ALG_NAMES.put(CMSAlgorithm.AES128_CBC, "AESMac");
        EnvelopedDataHelper.MAC_ALG_NAMES.put(CMSAlgorithm.AES192_CBC, "AESMac");
        EnvelopedDataHelper.MAC_ALG_NAMES.put(CMSAlgorithm.AES256_CBC, "AESMac");
        EnvelopedDataHelper.MAC_ALG_NAMES.put(CMSAlgorithm.RC2_CBC, "RC2Mac");
        EnvelopedDataHelper.authEnvelopedAlgorithms.add(CMSAlgorithm.AES128_GCM);
        EnvelopedDataHelper.authEnvelopedAlgorithms.add(CMSAlgorithm.AES192_GCM);
        EnvelopedDataHelper.authEnvelopedAlgorithms.add(CMSAlgorithm.AES256_GCM);
        EnvelopedDataHelper.authEnvelopedAlgorithms.add(CMSAlgorithm.AES128_CCM);
        EnvelopedDataHelper.authEnvelopedAlgorithms.add(CMSAlgorithm.AES192_CCM);
        EnvelopedDataHelper.authEnvelopedAlgorithms.add(CMSAlgorithm.AES256_CCM);
        EnvelopedDataHelper.authEnvelopedAlgorithms.add(CMSAlgorithm.ChaCha20Poly1305);
    }
}
