package org.spongycastle.jcajce.provider.symmetric; import java.security.AlgorithmParameters; import java.security.InvalidAlgorithmParameterException; import java.security.SecureRandom; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.spec.IvParameterSpec; import org.spongycastle.asn1.nist.NISTObjectIdentifiers; import org.spongycastle.crypto.BufferedBlockCipher; import org.spongycastle.crypto.CipherKeyGenerator; import org.spongycastle.crypto.engines.AESFastEngine; import org.spongycastle.crypto.engines.AESWrapEngine; import org.spongycastle.crypto.engines.RFC3211WrapEngine; import org.spongycastle.crypto.macs.CMac; import org.spongycastle.crypto.modes.CBCBlockCipher; import org.spongycastle.crypto.modes.CFBBlockCipher; import org.spongycastle.crypto.modes.OFBBlockCipher; import org.spongycastle.jcajce.provider.config.ConfigurableProvider; import org.spongycastle.jcajce.provider.symmetric.util.BaseAlgorithmParameterGenerator; import org.spongycastle.jcajce.provider.symmetric.util.BaseBlockCipher; import org.spongycastle.jcajce.provider.symmetric.util.BaseKeyGenerator; import org.spongycastle.jcajce.provider.symmetric.util.BaseMac; import org.spongycastle.jcajce.provider.symmetric.util.BaseWrapCipher; import org.spongycastle.jcajce.provider.symmetric.util.IvAlgorithmParameters; import org.spongycastle.jcajce.provider.util.AlgorithmProvider; import org.spongycastle.jce.provider.BouncyCastleProvider; public final class AES { private AES() { } public static class ECB extends BaseBlockCipher { public ECB() { super(new AESFastEngine()); } } public static class CBC extends BaseBlockCipher { public CBC() { super(new CBCBlockCipher(new AESFastEngine()), 128); } } static public class CFB extends BaseBlockCipher { public CFB() { super(new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 128)), 128); } } static public class OFB extends BaseBlockCipher { public OFB() { super(new BufferedBlockCipher(new OFBBlockCipher(new AESFastEngine(), 128)), 128); } } public static class AESCMAC extends BaseMac { public AESCMAC() { super(new CMac(new AESFastEngine())); } } static public class Wrap extends BaseWrapCipher { public Wrap() { super(new AESWrapEngine()); } } public static class RFC3211Wrap extends BaseWrapCipher { public RFC3211Wrap() { super(new RFC3211WrapEngine(new AESFastEngine()), 16); } } public static class KeyGen extends BaseKeyGenerator { public KeyGen() { this(192); } public KeyGen(int keySize) { super("AES", keySize, new CipherKeyGenerator()); } } public static class KeyGen128 extends KeyGen { public KeyGen128() { super(128); } } public static class KeyGen192 extends KeyGen { public KeyGen192() { super(192); } } public static class KeyGen256 extends KeyGen { public KeyGen256() { super(256); } } public static class AlgParamGen extends BaseAlgorithmParameterGenerator { protected void engineInit( AlgorithmParameterSpec genParamSpec, SecureRandom random) throws InvalidAlgorithmParameterException { throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for AES parameter generation."); } protected AlgorithmParameters engineGenerateParameters() { byte[] iv = new byte[16]; if (random == null) { random = new SecureRandom(); } random.nextBytes(iv); AlgorithmParameters params; try { params = AlgorithmParameters.getInstance("AES", BouncyCastleProvider.PROVIDER_NAME); params.init(new IvParameterSpec(iv)); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return params; } } public static class AlgParams extends IvAlgorithmParameters { protected String engineToString() { return "AES IV"; } } public static class Mappings extends AlgorithmProvider { private static final String PREFIX = AES.class.getName(); /** * These three got introduced in some messages as a result of a typo in an * early document. We don't produce anything using these OID values, but we'll * read them. */ private static final String wrongAES128 = "2.16.840.1.101.3.4.2"; private static final String wrongAES192 = "2.16.840.1.101.3.4.22"; private static final String wrongAES256 = "2.16.840.1.101.3.4.42"; public Mappings() { } public void configure(ConfigurableProvider provider) { provider.addAlgorithm("AlgorithmParameters.AES", PREFIX + "$AlgParams"); provider.addAlgorithm("Alg.Alias.AlgorithmParameters." + wrongAES128, "AES"); provider.addAlgorithm("Alg.Alias.AlgorithmParameters." + wrongAES192, "AES"); provider.addAlgorithm("Alg.Alias.AlgorithmParameters." + wrongAES256, "AES"); provider.addAlgorithm("Alg.Alias.AlgorithmParameters." + NISTObjectIdentifiers.id_aes128_CBC, "AES"); provider.addAlgorithm("Alg.Alias.AlgorithmParameters." + NISTObjectIdentifiers.id_aes192_CBC, "AES"); provider.addAlgorithm("Alg.Alias.AlgorithmParameters." + NISTObjectIdentifiers.id_aes256_CBC, "AES"); provider.addAlgorithm("AlgorithmParameterGenerator.AES", PREFIX + "$AlgParamGen"); provider.addAlgorithm("Alg.Alias.AlgorithmParameterGenerator." + wrongAES128, "AES"); provider.addAlgorithm("Alg.Alias.AlgorithmParameterGenerator." + wrongAES192, "AES"); provider.addAlgorithm("Alg.Alias.AlgorithmParameterGenerator." + wrongAES256, "AES"); provider.addAlgorithm("Alg.Alias.AlgorithmParameterGenerator." + NISTObjectIdentifiers.id_aes128_CBC, "AES"); provider.addAlgorithm("Alg.Alias.AlgorithmParameterGenerator." + NISTObjectIdentifiers.id_aes192_CBC, "AES"); provider.addAlgorithm("Alg.Alias.AlgorithmParameterGenerator." + NISTObjectIdentifiers.id_aes256_CBC, "AES"); provider.addAlgorithm("Cipher.AES", PREFIX + "$ECB"); provider.addAlgorithm("Alg.Alias.Cipher." + wrongAES128, "AES"); provider.addAlgorithm("Alg.Alias.Cipher." + wrongAES192, "AES"); provider.addAlgorithm("Alg.Alias.Cipher." + wrongAES256, "AES"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes128_ECB, PREFIX + "$ECB"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes192_ECB, PREFIX + "$ECB"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes256_ECB, PREFIX + "$ECB"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes128_CBC, PREFIX + "$CBC"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes192_CBC, PREFIX + "$CBC"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes256_CBC, PREFIX + "$CBC"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes128_OFB, PREFIX + "$OFB"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes192_OFB, PREFIX + "$OFB"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes256_OFB, PREFIX + "$OFB"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes128_CFB, PREFIX + "$CFB"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes192_CFB, PREFIX + "$CFB"); provider.addAlgorithm("Cipher." + NISTObjectIdentifiers.id_aes256_CFB, PREFIX + "$CFB"); provider.addAlgorithm("Cipher.AESWRAP", PREFIX + "$Wrap"); provider.addAlgorithm("Alg.Alias.Cipher." + NISTObjectIdentifiers.id_aes128_wrap, "AESWRAP"); provider.addAlgorithm("Alg.Alias.Cipher." + NISTObjectIdentifiers.id_aes192_wrap, "AESWRAP"); provider.addAlgorithm("Alg.Alias.Cipher." + NISTObjectIdentifiers.id_aes256_wrap, "AESWRAP"); provider.addAlgorithm("Cipher.AESRFC3211WRAP", PREFIX + "$RFC3211Wrap"); provider.addAlgorithm("KeyGenerator.AES", PREFIX + "$KeyGen"); provider.addAlgorithm("KeyGenerator." + wrongAES128, PREFIX + "$KeyGen128"); provider.addAlgorithm("KeyGenerator." + wrongAES192, PREFIX + "$KeyGen192"); provider.addAlgorithm("KeyGenerator." + wrongAES256, PREFIX + "$KeyGen256"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes128_ECB, PREFIX + "$KeyGen128"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes128_CBC, PREFIX + "$KeyGen128"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes128_OFB, PREFIX + "$KeyGen128"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes128_CFB, PREFIX + "$KeyGen128"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes192_ECB, PREFIX + "$KeyGen192"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes192_CBC, PREFIX + "$KeyGen192"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes192_OFB, PREFIX + "$KeyGen192"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes192_CFB, PREFIX + "$KeyGen192"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes256_ECB, PREFIX + "$KeyGen256"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes256_CBC, PREFIX + "$KeyGen256"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes256_OFB, PREFIX + "$KeyGen256"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes256_CFB, PREFIX + "$KeyGen256"); provider.addAlgorithm("KeyGenerator.AESWRAP", PREFIX + "$KeyGen"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes128_wrap, PREFIX + "$KeyGen128"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes192_wrap, PREFIX + "$KeyGen192"); provider.addAlgorithm("KeyGenerator." + NISTObjectIdentifiers.id_aes256_wrap, PREFIX + "$KeyGen256"); provider.addAlgorithm("Mac.AESCMAC", PREFIX + "$AESCMAC"); } } }