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

org.bouncycastle.jcajce.provider.GcmSpecUtil Maven / Gradle / Ivy

Go to download

The FIPS 140-2 Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms certified to FIPS 140-2 level 1. This jar contains the debug version JCE provider and low-level API for the BC-FJA version 1.0.2.3, FIPS Certificate #3514. Please note the debug jar is not certified.

There is a newer version: 2.0.0
Show newest version
package org.bouncycastle.jcajce.provider;

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

import javax.crypto.spec.IvParameterSpec;

import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.internal.asn1.cms.GCMParameters;
import org.bouncycastle.jcajce.spec.AEADParameterSpec;
import org.bouncycastle.util.Integers;

class GcmSpecUtil
{
    static final Class gcmSpecClass = ClassUtil.lookup("javax.crypto.spec.GCMParameterSpec");

    static final Method tLen;
    static final Method iv;

    static
    {
        if (gcmSpecClass != null)
        {
            tLen = ClassUtil.extractMethod(gcmSpecClass, "getTLen");
            iv = ClassUtil.extractMethod(gcmSpecClass, "getIV");
        }
        else
        {
            tLen = null;
            iv = null;
        }
    }

    static boolean gcmSpecExists()
    {
        return gcmSpecClass != null;
    }

    static boolean isGcmSpec(AlgorithmParameterSpec paramSpec)
    {
        return gcmSpecClass != null && gcmSpecClass.isInstance(paramSpec);
    }

    static boolean isGcmSpec(Class paramSpecClass)
    {
        return gcmSpecClass == paramSpecClass;
    }

    static Class[] getCipherSpecClasses()
    {
        if (gcmSpecExists())
        {
            return new Class[]{GcmSpecUtil.gcmSpecClass, IvParameterSpec.class};
        }
        else
        {
            return new Class[]{AEADParameterSpec.class,IvParameterSpec.class};
        }
    }

    static AlgorithmParameterSpec extractGcmSpec(final ASN1Primitive spec)
        throws InvalidParameterSpecException
    {
        Object rv = AccessController.doPrivileged(new PrivilegedAction()
        {
            public Object run()
            {
                try
                {
                    GCMParameters gcmParams = GCMParameters.getInstance(spec);
                    Constructor constructor = gcmSpecClass.getConstructor(new Class[]{Integer.TYPE, byte[].class});

                    return constructor.newInstance(new Object[]{Integers.valueOf(gcmParams.getIcvLen() * 8), gcmParams.getNonce()});
                }
                catch (NoSuchMethodException e)
                {
                    return new InvalidParameterSpecException("no constructor found!");   // should never happen
                }
                catch (Exception e)
                {
                    return new InvalidParameterSpecException("construction failed: " + e.getMessage());   // should never happen
                }
            }
        });
        if (rv instanceof AlgorithmParameterSpec)
        {
            return (AlgorithmParameterSpec)rv;
        }
        else
        {
            throw (InvalidParameterSpecException)rv;
        }
    }

    static GCMParameters extractGcmParameters(final AlgorithmParameterSpec paramSpec)
        throws InvalidParameterSpecException
    {
        Object rv = AccessController.doPrivileged(new PrivilegedAction()
        {
            public Object run()
            {
                try
                {
                    return new GCMParameters((byte[])iv.invoke(paramSpec, new Object[0]), ((Integer)tLen.invoke(paramSpec, new Object[0])).intValue() / 8);
                }
                catch (Exception e)
                {
                    return new InvalidParameterSpecException("cannot process GCMParameterSpec: " + e.getMessage());
                }
            }
        });
        if (rv instanceof GCMParameters)
        {
            return (GCMParameters)rv;
        }
        else
        {
            throw (InvalidParameterSpecException)rv;
        }
    }
}