001: package org.bouncycastle.jce.provider.symmetric;
002:
003: import org.bouncycastle.crypto.CipherKeyGenerator;
004: import org.bouncycastle.crypto.engines.AESEngine;
005: import org.bouncycastle.crypto.engines.AESFastEngine;
006: import org.bouncycastle.crypto.engines.AESWrapEngine;
007: import org.bouncycastle.crypto.engines.RFC3211WrapEngine;
008: import org.bouncycastle.crypto.modes.CBCBlockCipher;
009: import org.bouncycastle.crypto.modes.CFBBlockCipher;
010: import org.bouncycastle.crypto.modes.OFBBlockCipher;
011: import org.bouncycastle.jce.provider.JCEBlockCipher;
012: import org.bouncycastle.jce.provider.JCEKeyGenerator;
013: import org.bouncycastle.jce.provider.JDKAlgorithmParameterGenerator;
014: import org.bouncycastle.jce.provider.JDKAlgorithmParameters;
015: import org.bouncycastle.jce.provider.WrapCipherSpi;
016:
017: import javax.crypto.spec.IvParameterSpec;
018: import java.security.AlgorithmParameters;
019: import java.security.InvalidAlgorithmParameterException;
020: import java.security.SecureRandom;
021: import java.security.spec.AlgorithmParameterSpec;
022:
023: public final class AES {
024: private AES() {
025: }
026:
027: public static class ECB extends JCEBlockCipher {
028: public ECB() {
029: super (new AESFastEngine());
030: }
031: }
032:
033: public static class CBC extends JCEBlockCipher {
034: public CBC() {
035: super (new CBCBlockCipher(new AESFastEngine()), 128);
036: }
037: }
038:
039: static public class CFB extends JCEBlockCipher {
040: public CFB() {
041: super (new CFBBlockCipher(new AESFastEngine(), 128), 128);
042: }
043: }
044:
045: static public class OFB extends JCEBlockCipher {
046: public OFB() {
047: super (new OFBBlockCipher(new AESFastEngine(), 128), 128);
048: }
049: }
050:
051: static public class Wrap extends WrapCipherSpi {
052: public Wrap() {
053: super (new AESWrapEngine());
054: }
055: }
056:
057: public static class RFC3211Wrap extends WrapCipherSpi {
058: public RFC3211Wrap() {
059: super (new RFC3211WrapEngine(new AESEngine()), 16);
060: }
061: }
062:
063: public static class KeyGen extends JCEKeyGenerator {
064: public KeyGen() {
065: this (192);
066: }
067:
068: public KeyGen(int keySize) {
069: super ("AES", keySize, new CipherKeyGenerator());
070: }
071: }
072:
073: public static class KeyGen128 extends KeyGen {
074: public KeyGen128() {
075: super (128);
076: }
077: }
078:
079: public static class KeyGen192 extends KeyGen {
080: public KeyGen192() {
081: super (192);
082: }
083: }
084:
085: public static class KeyGen256 extends KeyGen {
086: public KeyGen256() {
087: super (256);
088: }
089: }
090:
091: public static class AlgParamGen extends
092: JDKAlgorithmParameterGenerator {
093: protected void engineInit(AlgorithmParameterSpec genParamSpec,
094: SecureRandom random)
095: throws InvalidAlgorithmParameterException {
096: throw new InvalidAlgorithmParameterException(
097: "No supported AlgorithmParameterSpec for AES parameter generation.");
098: }
099:
100: protected AlgorithmParameters engineGenerateParameters() {
101: byte[] iv = new byte[16];
102:
103: if (random == null) {
104: random = new SecureRandom();
105: }
106:
107: random.nextBytes(iv);
108:
109: AlgorithmParameters params;
110:
111: try {
112: params = AlgorithmParameters.getInstance("AES", "BC");
113: params.init(new IvParameterSpec(iv));
114: } catch (Exception e) {
115: throw new RuntimeException(e.getMessage());
116: }
117:
118: return params;
119: }
120: }
121:
122: public static class AlgParams extends
123: JDKAlgorithmParameters.IVAlgorithmParameters {
124: protected String engineToString() {
125: return "AES IV";
126: }
127: }
128: }
|