001: package ch.ethz.ssh2.crypto.cipher;
002:
003: import java.util.Vector;
004:
005: /**
006: * BlockCipherFactory.
007: *
008: * @author Christian Plattner, plattner@inf.ethz.ch
009: * @version $Id: BlockCipherFactory.java,v 1.4 2005/12/05 17:13:27 cplattne Exp $
010: */
011: public class BlockCipherFactory {
012: static class CipherEntry {
013: String type;
014: int blocksize;
015: int keysize;
016: String cipherClass;
017:
018: public CipherEntry(String type, int blockSize, int keySize,
019: String cipherClass) {
020: this .type = type;
021: this .blocksize = blockSize;
022: this .keysize = keySize;
023: this .cipherClass = cipherClass;
024: }
025: }
026:
027: static Vector ciphers = new Vector();
028:
029: static {
030: /* Higher Priority First */
031:
032: ciphers.addElement(new CipherEntry("aes256-ctr", 16, 32,
033: "ch.ethz.ssh2.crypto.cipher.AES"));
034: ciphers.addElement(new CipherEntry("aes192-ctr", 16, 24,
035: "ch.ethz.ssh2.crypto.cipher.AES"));
036: ciphers.addElement(new CipherEntry("aes128-ctr", 16, 16,
037: "ch.ethz.ssh2.crypto.cipher.AES"));
038: ciphers.addElement(new CipherEntry("blowfish-ctr", 8, 16,
039: "ch.ethz.ssh2.crypto.cipher.BlowFish"));
040:
041: ciphers.addElement(new CipherEntry("aes256-cbc", 16, 32,
042: "ch.ethz.ssh2.crypto.cipher.AES"));
043: ciphers.addElement(new CipherEntry("aes192-cbc", 16, 24,
044: "ch.ethz.ssh2.crypto.cipher.AES"));
045: ciphers.addElement(new CipherEntry("aes128-cbc", 16, 16,
046: "ch.ethz.ssh2.crypto.cipher.AES"));
047: ciphers.addElement(new CipherEntry("blowfish-cbc", 8, 16,
048: "ch.ethz.ssh2.crypto.cipher.BlowFish"));
049:
050: ciphers.addElement(new CipherEntry("3des-ctr", 8, 24,
051: "ch.ethz.ssh2.crypto.cipher.DESede"));
052: ciphers.addElement(new CipherEntry("3des-cbc", 8, 24,
053: "ch.ethz.ssh2.crypto.cipher.DESede"));
054: }
055:
056: public static String[] getDefaultCipherList() {
057: String list[] = new String[ciphers.size()];
058: for (int i = 0; i < ciphers.size(); i++) {
059: CipherEntry ce = (CipherEntry) ciphers.elementAt(i);
060: list[i] = new String(ce.type);
061: }
062: return list;
063: }
064:
065: public static void checkCipherList(String[] cipherCandidates) {
066: for (int i = 0; i < cipherCandidates.length; i++)
067: getEntry(cipherCandidates[i]);
068: }
069:
070: public static BlockCipher createCipher(String type,
071: boolean encrypt, byte[] key, byte[] iv) {
072: try {
073: CipherEntry ce = getEntry(type);
074: Class cc = Class.forName(ce.cipherClass);
075: BlockCipher bc = (BlockCipher) cc.newInstance();
076:
077: if (type.endsWith("-cbc")) {
078: bc.init(encrypt, key);
079: return new CBCMode(bc, iv, encrypt);
080: } else if (type.endsWith("-ctr")) {
081: bc.init(true, key);
082: return new CTRMode(bc, iv, encrypt);
083: }
084: throw new IllegalArgumentException("Cannot instantiate "
085: + type);
086: } catch (Exception e) {
087: throw new IllegalArgumentException("Cannot instantiate "
088: + type);
089: }
090: }
091:
092: private static CipherEntry getEntry(String type) {
093: for (int i = 0; i < ciphers.size(); i++) {
094: CipherEntry ce = (CipherEntry) ciphers.elementAt(i);
095: if (ce.type.equals(type))
096: return ce;
097: }
098: throw new IllegalArgumentException("Unkown algorithm " + type);
099: }
100:
101: public static int getBlockSize(String type) {
102: CipherEntry ce = getEntry(type);
103: return ce.blocksize;
104: }
105:
106: public static int getKeySize(String type) {
107: CipherEntry ce = getEntry(type);
108: return ce.keysize;
109: }
110: }
|