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

org.bouncycastle.jcajce.provider.ProvBlowfish 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.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.spec.IvParameterSpec;

import org.bouncycastle.asn1.misc.MiscObjectIdentifiers;
import org.bouncycastle.crypto.AuthenticationParametersWithIV;
import org.bouncycastle.crypto.Parameters;
import org.bouncycastle.crypto.ParametersWithIV;
import org.bouncycastle.crypto.SymmetricKeyGenerator;
import org.bouncycastle.crypto.general.Blowfish;

class ProvBlowfish
    extends AlgorithmProvider
{
    ProvBlowfish()
    {
    }

    private static final String PREFIX = ProvBlowfish.class.getName();

    private Class[] availableSpecs =
        {
            IvParameterSpec.class,
        };

    private ParametersCreatorProvider generalParametersCreatorProvider = new ParametersCreatorProvider()
    {
        public ParametersCreator get(Parameters parameters)
        {
            if (Utils.isAuthMode(parameters.getAlgorithm()))
            {
                return new AuthParametersCreator((AuthenticationParametersWithIV)parameters);
            }
            return new IvParametersCreator((ParametersWithIV)parameters);
        }
    };

    public void configure(final BouncyCastleFipsProvider provider)
    {

        provider.addAlgorithmImplementation("Cipher.BLOWFISH", PREFIX + "$ECB", new GuardedEngineCreator(new EngineCreator()
        {
            public Object createInstance(Object constructorParameter)
            {
                return new BaseCipher.Builder(provider, 64,
                    Blowfish.ECBwithPKCS7, Blowfish.ECB, Blowfish.ECBwithISO10126_2, Blowfish.ECBwithISO7816_4, Blowfish.ECBwithTBC, Blowfish.ECBwithX923,
                    Blowfish.CBC, Blowfish.CBCwithPKCS7, Blowfish.CBCwithISO10126_2, Blowfish.CBCwithISO7816_4, Blowfish.CBCwithTBC, Blowfish.CBCwithX923,
                    Blowfish.CBCwithCS1, Blowfish.CBCwithCS2, Blowfish.CBCwithCS3,
                    Blowfish.CFB64, Blowfish.CFB8,
                    Blowfish.OFB, Blowfish.OpenPGPCFB,
                    Blowfish.CTR, Blowfish.EAX)
                    .withGeneralOperators(generalParametersCreatorProvider, new Blowfish.OperatorFactory(), new Blowfish.AEADOperatorFactory())
                    .withParameters(availableSpecs).build();
            }
        }));
        provider.addAlgorithmImplementation("Cipher", MiscObjectIdentifiers.cryptlib_algorithm_blowfish_CBC, PREFIX + "$CBC", new GuardedEngineCreator(new EngineCreator()
        {
            public Object createInstance(Object constructorParameter)
            {
                return new BaseCipher.Builder(provider, 64, Blowfish.CBCwithPKCS7)
                    .withGeneralOperators(generalParametersCreatorProvider, new Blowfish.OperatorFactory(), null)
                    .withParameters(availableSpecs).build();
            }
        }));
        provider.addAlgorithmImplementation("KeyGenerator.BLOWFISH", PREFIX + "$KeyGen", new GuardedEngineCreator(new EngineCreator()
        {
            public Object createInstance(Object constructorParameter)
            {
                return new BaseKeyGenerator(provider, "Blowfish", 128, new KeyGeneratorCreator()
                {
                    public SymmetricKeyGenerator createInstance(int keySize, SecureRandom random)
                    {
                        return new Blowfish.KeyGenerator(keySize, random);
                    }
                });
            }
        }));
        provider.addAlias("KeyGenerator", "BLOWFISH", MiscObjectIdentifiers.cryptlib_algorithm_blowfish_CBC);

        provider.addAlgorithmImplementation("Mac.BLOWFISHCMAC", PREFIX + "$CMAC", new GuardedEngineCreator(new EngineCreator()
        {
            public Object createInstance(Object constructorParameter)
            {
                return new BaseMac(Blowfish.CMAC, new Blowfish.MACOperatorFactory(), new AuthParametersCreator(Blowfish.CMAC));
            }
        }));
        provider.addAlias("Mac", "BLOWFISHCMAC", "BLOWFISH-CMAC");

        provider.addAlgorithmImplementation("SecretKeyFactory.BLOWFISH", PREFIX + "$BLOWFISHKFACT", new GuardedEngineCreator(new EngineCreator()
        {
            public Object createInstance(Object constructorParameter)
            {
                return new BaseSecretKeyFactory("Blowfish", Blowfish.ALGORITHM, new BaseSecretKeyFactory.Validator()
                {
                    public byte[] validated(byte[] keyBytes)
                        throws InvalidKeySpecException
                    {
                        int keyLength = keyBytes.length * 8;
                        if (keyLength < 32 || keyLength > 448)
                        {
                            throw new InvalidKeySpecException("Blowfish key must be between 32 and 448 bits inclusive");
                        }

                        return keyBytes;
                    }
                });
            }
        }));

        provider.addAlgorithmImplementation("AlgorithmParameters.BLOWFISH", PREFIX + "$AlgParams", new GuardedEngineCreator(new EngineCreator()
        {
            public Object createInstance(Object constructorParameter)
            {
                return new ASN1AlgorithmParameters("Blowfish");
            }
        }));
        provider.addAlias("AlgorithmParameters", "BLOWFISH", MiscObjectIdentifiers.cryptlib_algorithm_blowfish_CBC);

        provider.addAlgorithmImplementation("AlgorithmParameterGenerator", MiscObjectIdentifiers.cryptlib_algorithm_blowfish_CBC, PREFIX + "$AlgParamGen", new GuardedEngineCreator(new EngineCreator()
        {
            public Object createInstance(Object constructorParameter)
            {
                return new IVAlgorithmParameterGenerator(provider, "Blowfish", 8);
            }
        }));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy