1 /* 2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved. 3 * Please refer to the LICENSE.txt for licensing details. 4 */ 5 package ch.ethz.ssh2.crypto.cipher; 6 7 import java.util.ArrayList; 8 import java.util.List; 9 import java.util.Vector; 10 11 /** 12 * BlockCipherFactory. 13 * 14 * @author Christian Plattner 15 * @version $Id: BlockCipherFactory.java 29 2011-05-28 21:28:32Z dkocher@sudo.ch $ 16 */ 17 public class BlockCipherFactory 18 { 19 private static final class CipherEntry 20 { 21 String type; 22 int blocksize; 23 int keysize; 24 String cipherClass; 25 CipherEntry(String type, int blockSize, int keySize, String cipherClass)26 public CipherEntry(String type, int blockSize, int keySize, String cipherClass) 27 { 28 this.type = type; 29 this.blocksize = blockSize; 30 this.keysize = keySize; 31 this.cipherClass = cipherClass; 32 } 33 } 34 35 private static final List<CipherEntry> ciphers = new Vector<CipherEntry>(); 36 37 static 38 { 39 /* Higher Priority First */ ciphers.add(new CipherEntry("aes128-ctr", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"))40 ciphers.add(new CipherEntry("aes128-ctr", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES")); ciphers.add(new CipherEntry("aes192-ctr", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"))41 ciphers.add(new CipherEntry("aes192-ctr", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES")); ciphers.add(new CipherEntry("aes256-ctr", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"))42 ciphers.add(new CipherEntry("aes256-ctr", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES")); ciphers.add(new CipherEntry("blowfish-ctr", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"))43 ciphers.add(new CipherEntry("blowfish-ctr", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish")); 44 ciphers.add(new CipherEntry("aes128-cbc", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES"))45 ciphers.add(new CipherEntry("aes128-cbc", 16, 16, "ch.ethz.ssh2.crypto.cipher.AES")); ciphers.add(new CipherEntry("aes192-cbc", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES"))46 ciphers.add(new CipherEntry("aes192-cbc", 16, 24, "ch.ethz.ssh2.crypto.cipher.AES")); ciphers.add(new CipherEntry("aes256-cbc", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES"))47 ciphers.add(new CipherEntry("aes256-cbc", 16, 32, "ch.ethz.ssh2.crypto.cipher.AES")); ciphers.add(new CipherEntry("blowfish-cbc", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish"))48 ciphers.add(new CipherEntry("blowfish-cbc", 8, 16, "ch.ethz.ssh2.crypto.cipher.BlowFish")); 49 ciphers.add(new CipherEntry("3des-ctr", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"))50 ciphers.add(new CipherEntry("3des-ctr", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede")); ciphers.add(new CipherEntry("3des-cbc", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede"))51 ciphers.add(new CipherEntry("3des-cbc", 8, 24, "ch.ethz.ssh2.crypto.cipher.DESede")); 52 } 53 getDefaultCipherList()54 public static String[] getDefaultCipherList() 55 { 56 List<String> list = new ArrayList<String>(ciphers.size()); 57 for (CipherEntry ce : ciphers) 58 { 59 list.add(ce.type); 60 } 61 return list.toArray(new String[ciphers.size()]); 62 } 63 checkCipherList(String[] cipherCandidates)64 public static void checkCipherList(String[] cipherCandidates) 65 { 66 for (String cipherCandidate : cipherCandidates) 67 { 68 getEntry(cipherCandidate); 69 } 70 } 71 72 // @SuppressWarnings("rawtypes") createCipher(String type, boolean encrypt, byte[] key, byte[] iv)73 public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv) 74 { 75 try 76 { 77 CipherEntry ce = getEntry(type); 78 Class cc = Class.forName(ce.cipherClass); 79 BlockCipher bc = (BlockCipher) cc.newInstance(); 80 81 if (type.endsWith("-cbc")) 82 { 83 bc.init(encrypt, key); 84 return new CBCMode(bc, iv, encrypt); 85 } 86 else if (type.endsWith("-ctr")) 87 { 88 bc.init(true, key); 89 return new CTRMode(bc, iv, encrypt); 90 } 91 throw new IllegalArgumentException("Cannot instantiate " + type); 92 } 93 catch (ClassNotFoundException e) 94 { 95 throw new IllegalArgumentException("Cannot instantiate " + type, e); 96 } 97 catch (InstantiationException e) 98 { 99 throw new IllegalArgumentException("Cannot instantiate " + type, e); 100 } 101 catch (IllegalAccessException e) 102 { 103 throw new IllegalArgumentException("Cannot instantiate " + type, e); 104 } 105 } 106 getEntry(String type)107 private static CipherEntry getEntry(String type) 108 { 109 for (CipherEntry ce : ciphers) 110 { 111 if (ce.type.equals(type)) 112 { 113 return ce; 114 } 115 } 116 throw new IllegalArgumentException("Unkown algorithm " + type); 117 } 118 getBlockSize(String type)119 public static int getBlockSize(String type) 120 { 121 CipherEntry ce = getEntry(type); 122 return ce.blocksize; 123 } 124 getKeySize(String type)125 public static int getKeySize(String type) 126 { 127 CipherEntry ce = getEntry(type); 128 return ce.keysize; 129 } 130 } 131