All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.tls.crypto.impl.jcajce.GCMUtil Maven / Gradle / Ivy

Go to download

The Bouncy Castle Java APIs for the TLS, including a JSSE provider. The APIs are designed primarily to be used in conjunction with the BC FIPS provider. The APIs may also be used with other providers although if being used in a FIPS context it is the responsibility of the user to ensure that any other providers used are FIPS certified and used appropriately.

There is a newer version: 2.0.19
Show newest version
package org.bouncycastle.tls.crypto.impl.jcajce;

import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.security.spec.AlgorithmParameterSpec;

import org.bouncycastle.util.Integers;

class GCMUtil
{
    static final Constructor gcmParameterSpec = getConstructor();

    static AlgorithmParameterSpec createGCMParameterSpec(final int tLen, final byte[] src)
        throws Exception
    {
        if (gcmParameterSpec == null)
        {
            throw new IllegalStateException();
        }

        return AccessController.doPrivileged(new PrivilegedExceptionAction()
        {
            public AlgorithmParameterSpec run()
                throws Exception
            {
                return gcmParameterSpec.newInstance(new Object[]{ Integers.valueOf(tLen), src });
            }
        });
    }

    static boolean isGCMParameterSpecAvailable()
    {
        return gcmParameterSpec != null;
    }

    private static Constructor getConstructor()
    {
        return AccessController.doPrivileged(new PrivilegedAction>()
        {
            public Constructor run()
            {
                try
                {
                    String className = "javax.crypto.spec.GCMParameterSpec";

                    ClassLoader classLoader = GCMUtil.class.getClassLoader();
                    Class clazz = (null == classLoader)
                        ?   Class.forName(className)
                        :   classLoader.loadClass(className);
                    if (clazz != null && AlgorithmParameterSpec.class.isAssignableFrom(clazz))
                    {
                        @SuppressWarnings("unchecked")
                        Class typedClazz = (Class)clazz;
                        return typedClazz.getConstructor(new Class[]{ Integer.TYPE, byte[].class });
                    }
                }
                catch (Exception e)
                {
                }

                return null;
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy